Repository navigation
lib: add FileAPI support - #67277
lib: add FileAPI support#67277avivkeller wants to merge 1 commit into
Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67277 +/- ##
==========================================
- Coverage 90.69% 90.68% -0.01%
==========================================
Files 801 802 +1
Lines 281559 281474 -85
Branches 54250 54282 +32
==========================================
- Hits 255347 255251 -96
+ Misses 16357 16335 -22
- Partials 9855 9888 +33
🚀 New features to boost your workflow:
|
jasnell
left a comment
There was a problem hiding this comment.
Please mark initially as experimental.
| A browser-compatible implementation of {FileReaderSync} from the [File API][]. | ||
| It is exposed only inside a [Web Worker][] (`--experimental-web-worker`), | ||
| matching `[Exposed=(DedicatedWorker,SharedWorker)]`. It is not available on | ||
| the main thread or in threads created with `node:worker_threads`. |
There was a problem hiding this comment.
I question whether this restriction is actually necessary in Node.js. Specifically the "It is not available on the main thread" or in worker threads.
The restrictions that apply to the browser do not necessarily apply here.
There was a problem hiding this comment.
This was marked resolved but it's still limited to web workers. The question is still open here.
|
A separate AI-agent review flagged the following additional items:
|
panva
left a comment
There was a problem hiding this comment.
Shouldn't this now be semver-minor with the new globals behind a default-off experimental flag?
The empty-MIME data URL results follow the current package-data wording, while WPT expects application/octet-stream. Progress counter initialization is also underspecified; that leaves an interoperability decision rather than a requirement to always report zero.
| // | ||
| // "Event handler for the load or error events could have started another | ||
| // load, if that happens the loadend event for this load is not fired." | ||
| queueFileReadingTask(fr, generation, () => { |
There was a problem hiding this comment.
The read operation fires loadend in the same task as load/error. Scheduling another task here lets a setImmediate scheduled inside onload start a second read and suppress the first read's loadend.
Please perform the state check and dispatch within the completion task.
const reader = new FileReader();
reader.onload = () => setImmediate(() => console.log('immediate'));
reader.onloadend = () => console.log('loadend');
reader.readAsText(new Blob(['x']));Prints immediate then loadend; expected loadend then immediate.
There was a problem hiding this comment.
Fixed locally, will push shortly
There was a problem hiding this comment.
Adding a second queueMicrotask() only moves the failure to a longer promise chain:
const reader = new FileReader();
reader.onload = () => {
reader.onload = null;
Promise.resolve()
.then(() => {})
.then(() => {})
.then(() => reader.readAsText(new Blob(['second'])));
};
reader.onloadend = () => console.log('loadend');
reader.readAsText(new Blob(['first']));Prints loadend twice; expected once.
| // "Let promise be the result of reading all bytes from stream with | ||
| // reader." | ||
| // "Wait for promise to be fulfilled or rejected." | ||
| const buffer = getBlobDataSync(blob); |
There was a problem hiding this comment.
undefined here means this helper cannot obtain the bytes synchronously, not that the Blob is unreadable. All four methods consequently throw NotReadableError for a healthy fs.openAsBlob() result that FileReader reads successfully.
The synchronous methods must wait for the read and return its bytes. Can we support file-backed Blobs through a synchronous read path?
import { openAsBlob } from 'node:fs';
import { isMainThread } from 'node:worker_threads';
if (isMainThread) {
const worker = new Worker(import.meta.url, { type: 'module' });
worker.onmessage = ({ data }) => console.log(data);
} else {
const blob = await openAsBlob(import.meta.filename);
try {
postMessage(new FileReaderSync().readAsText(blob));
} catch (err) {
postMessage(err.name);
}
close();
}Prints NotReadableError; expected the source file contents.
There was a problem hiding this comment.
There is no synchronous pull for a file-backed blob, but we could add one?
There was a problem hiding this comment.
This threads bob::OPTIONS_SYNC through the Blob reader and adds blocking reads using FileHandle's existing offset and length. It preserves snapshot checks and error handling, including for slices and mixed Blob parts.
| if (mimeType === '') { | ||
| return `data:;base64,${encoded}`; | ||
| } | ||
| return `data:${mimeType};base64,${encoded}`; |
There was a problem hiding this comment.
Data URL serialization needs another look for MIME types containing URL delimiters. text/plain;x=# currently produces an unfetchable URL. RFC 2397 §3 calls for escaping where necessary, although the exact File API serialization algorithm remains unresolved.
const reader = new FileReader();
reader.onload = () => fetch(reader.result).catch(console.log);
reader.readAsDataURL(new Blob(['hello'], { type: 'text/plain;x=#' }));This logs TypeError: fetch failed; the generated URL cannot be fetched back to the original bytes.
There was a problem hiding this comment.
The # case is fixed now, but a comma inside a quoted MIME parameter still breaks the result
const reader = new FileReader();
reader.onload = async () => {
console.log(await (await fetch(reader.result)).text());
};
reader.readAsDataURL(new Blob(['hello'], {
type: 'text/plain;x="a,b"',
}));There was a problem hiding this comment.
What should we be looking for? if the spec doesn't define exactly what we should be escaping, what should we be referencing?
There was a problem hiding this comment.
Fetch's data URL processor, step 5, splits at the first literal comma, even inside quotes. You can escape commas in the MIME metadata as %2C and add a regression test checking that fetching the result returns the original Blob bytes.
bbe0d52 to
0c0ca85
Compare
Adds support for the following FileAPI interfaces:
FileReaderFileReaderSyncProgressEvent