2024-01-20 20:17:11 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
|
|
2024-02-13 18:52:29 +00:00
|
|
|
/**
|
2024-12-17 17:25:54 +00:00
|
|
|
* Read file and parse as JSON
|
2024-02-13 18:52:29 +00:00
|
|
|
*
|
|
|
|
|
* @param {string} filePath
|
|
|
|
|
* @returns {any}
|
|
|
|
|
*/
|
|
|
|
|
export function fileToJson(filePath) {
|
|
|
|
|
const buffer = fs.readFileSync(filePath)
|
|
|
|
|
return JSON.parse(buffer.toString())
|
|
|
|
|
}
|
2024-05-28 20:59:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make a DataReader from bytes
|
|
|
|
|
*
|
2024-12-02 16:47:42 +00:00
|
|
|
* @import {DataReader} from '../src/types.d.ts'
|
2024-05-28 20:59:06 +00:00
|
|
|
* @param {number[]} bytes
|
2024-12-02 16:47:42 +00:00
|
|
|
* @returns {DataReader}
|
2024-05-28 20:59:06 +00:00
|
|
|
*/
|
|
|
|
|
export function reader(bytes) {
|
|
|
|
|
return { view: new DataView(new Uint8Array(bytes).buffer), offset: 0 }
|
|
|
|
|
}
|
2025-05-19 09:13:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|