diff --git a/src/promise-fs-opts.js b/src/promise-fs-opts.js new file mode 100644 index 0000000..7f82ba0 --- /dev/null +++ b/src/promise-fs-opts.js @@ -0,0 +1,46 @@ +import fs from 'fs' +import { promisify } from 'util' + +const hasFS = Boolean(fs) + +export { hasFS } + +/** + * @type {any} + */ +let _fsReadFn +/** + * @description This function is needed not to initialize the `fs.read` on load time. To run in cf workers without polyfill. + * @param {number} fd + * @param {Uint8Array} buffer + * @param {number} offset + * @param {number} length + * @param {number} position + * @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>} + */ +export function fsread (fd, buffer, offset, length, position) { + if (!_fsReadFn) { + _fsReadFn = promisify(fs.read) + } + return _fsReadFn(fd, buffer, offset, length, position) +} + +/** + * @type {any} + */ +let _fsWriteFn +/** + * @description This function is needed not to initialize the `fs.write` on load time. To run in cf workers without polyfill. + * @param {number} fd + * @param {Uint8Array} buffer + * @param {number} offset + * @param {number} length + * @param {number} position + * @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>} + */ +export function fswrite (fd, buffer, offset, length, position) { + if (!_fsWriteFn) { + _fsWriteFn = promisify(fs.write) + } + return _fsWriteFn(fd, buffer, offset, length, position) +} diff --git a/src/reader.js b/src/reader.js index 3e36f5b..a78cbac 100644 --- a/src/reader.js +++ b/src/reader.js @@ -1,15 +1,13 @@ -import fs from 'fs' -import { promisify } from 'util' +import { fsread, hasFS } from './promise-fs-opts.js' import { CarReader as BrowserCarReader } from './reader-browser.js' /** * @typedef {import('./api').Block} Block * @typedef {import('./api').BlockIndex} BlockIndex * @typedef {import('./api').CarReader} CarReaderIface + * @typedef {import('fs').promises.FileHandle} FileHandle */ -const fsread = promisify(fs.read) - /** * @class * @implements {CarReaderIface} @@ -27,7 +25,7 @@ export class CarReader extends BrowserCarReader { * @async * @static * @memberof CarReader - * @param {fs.promises.FileHandle | number} fd - A file descriptor from the + * @param {FileHandle | number} fd - A file descriptor from the * Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle` * from `fs.promises.open()`. * @param {BlockIndex} blockIndex - An index pointing to the location of the @@ -53,4 +51,4 @@ export class CarReader extends BrowserCarReader { } } -export const __browser = false +export const __browser = !hasFS diff --git a/src/writer.js b/src/writer.js index cee84b4..7aa6aeb 100644 --- a/src/writer.js +++ b/src/writer.js @@ -1,15 +1,12 @@ -import fs from 'fs' -import { promisify } from 'util' import { readHeader, chunkReader } from './decoder.js' import { createHeader } from './encoder.js' +import { fsread, fswrite, hasFS } from './promise-fs-opts.js' import { CarWriter as BrowserCarWriter } from './writer-browser.js' -const fsread = promisify(fs.read) -const fswrite = promisify(fs.write) - /** * @typedef {import('multiformats/cid').CID} CID * @typedef {import('./api').BlockWriter} BlockWriter + * @typedef {import('fs').promises.FileHandle} FileHandle */ /** @@ -35,7 +32,7 @@ export class CarWriter extends BrowserCarWriter { * @async * @static * @memberof CarWriter - * @param {fs.promises.FileHandle | number} fd - A file descriptor from the + * @param {FileHandle | number} fd - A file descriptor from the * Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle` * from `fs.promises.open()`. * @param {CID[]} roots - A new list of roots to replace the existing list in @@ -81,4 +78,4 @@ export class CarWriter extends BrowserCarWriter { } } -export const __browser = false +export const __browser = !hasFS