-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make reader lazy on fs.read to run ignore polyfill (#168)
To help with esbuild and use in Cloudflare workers.
- Loading branch information
Showing
3 changed files
with
54 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters