-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fake io * rename to virtual * forest get * cache * fix cli * test virtual io * remove log
- Loading branch information
1 parent
05e1619
commit 6b4a60b
Showing
5 changed files
with
123 additions
and
14 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
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
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,56 @@ | ||
/** @typedef {import('./io.mjs').IO} IO */ | ||
|
||
const notImplemented = () => { throw 'not implemented' } | ||
|
||
/** | ||
* @typedef {{[index: string]: Uint8Array}} FileSystem | ||
*/ | ||
|
||
/** @type {(fs: FileSystem) => (path: string) => Promise<Uint8Array>} */ | ||
const read = fs => async (path) => { | ||
const buffer = fs[path] | ||
if (buffer === undefined) { | ||
throw 'file not found' | ||
} | ||
return buffer | ||
} | ||
|
||
/** @type {(fs: FileSystem) => (path: string, buffer: Uint8Array) => Promise<void>} */ | ||
const append = fs => async (path, buffer) => { | ||
const cur = fs[path] | ||
if (buffer === undefined) { | ||
throw 'file not found' | ||
} | ||
fs[path] = new Uint8Array([...cur, ...buffer]) | ||
} | ||
|
||
/** @type {(fs: FileSystem) => (path: string, buffer: Uint8Array) => Promise<void>} */ | ||
const write = fs => async (path, buffer) => { | ||
fs[path] = buffer | ||
} | ||
|
||
/** @type {(fs: FileSystem) => (oldPath: string, newPath: string) => Promise<void>} */ | ||
const rename = fs => async (oldPath, newPath) => { | ||
const buffer = fs[oldPath] | ||
if (buffer === undefined) { | ||
throw 'file not found' | ||
} | ||
delete fs[oldPath] | ||
fs[newPath] = buffer | ||
} | ||
|
||
/** @type {(fs: FileSystem) => IO} */ | ||
const virtual = fs => { | ||
return { | ||
read: read(fs), | ||
append: append(fs), | ||
write: write(fs), | ||
rename: rename(fs), | ||
fetch: notImplemented, | ||
document: undefined | ||
} | ||
} | ||
|
||
export default { | ||
virtual | ||
} |
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