Skip to content

Commit

Permalink
feat: make reader lazy on fs.read to run ignore polyfill (#168)
Browse files Browse the repository at this point in the history
To help with esbuild and use in Cloudflare workers.
  • Loading branch information
mabels authored Jan 6, 2025
1 parent 2e85211 commit ccb8edc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
46 changes: 46 additions & 0 deletions src/promise-fs-opts.js
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 4 additions & 6 deletions src/reader.js
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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
Expand All @@ -53,4 +51,4 @@ export class CarReader extends BrowserCarReader {
}
}

export const __browser = false
export const __browser = !hasFS
11 changes: 4 additions & 7 deletions src/writer.js
Original file line number Diff line number Diff line change
@@ -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
*/

/**
Expand All @@ -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
Expand Down Expand Up @@ -81,4 +78,4 @@ export class CarWriter extends BrowserCarWriter {
}
}

export const __browser = false
export const __browser = !hasFS

0 comments on commit ccb8edc

Please sign in to comment.