mirror of
https://github.com/asadbek064/hyparquet.git
synced 2025-12-06 06:51:54 +00:00
44 lines
969 B
JavaScript
44 lines
969 B
JavaScript
import fs from 'fs'
|
|
|
|
/**
|
|
* Read file and parse as JSON
|
|
*
|
|
* @param {string} filePath
|
|
* @returns {any}
|
|
*/
|
|
export function fileToJson(filePath) {
|
|
const buffer = fs.readFileSync(filePath)
|
|
return JSON.parse(buffer.toString())
|
|
}
|
|
|
|
/**
|
|
* Make a DataReader from bytes
|
|
*
|
|
* @import {DataReader} from '../src/types.d.ts'
|
|
* @param {number[]} bytes
|
|
* @returns {DataReader}
|
|
*/
|
|
export function reader(bytes) {
|
|
return { view: new DataView(new Uint8Array(bytes).buffer), offset: 0 }
|
|
}
|
|
|
|
/**
|
|
* Wraps an AsyncBuffer to count the number of fetches made
|
|
*
|
|
* @import {AsyncBuffer} from '../src/types.js'
|
|
* @param {AsyncBuffer} asyncBuffer
|
|
* @returns {AsyncBuffer & {fetches: number, bytes: number}}
|
|
*/
|
|
export function countingBuffer(asyncBuffer) {
|
|
return {
|
|
...asyncBuffer,
|
|
fetches: 0,
|
|
bytes: 0,
|
|
slice(start, end) {
|
|
this.fetches++
|
|
this.bytes += (end ?? asyncBuffer.byteLength) - start
|
|
return asyncBuffer.slice(start, end)
|
|
},
|
|
}
|
|
}
|