Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,16 @@ added:

Enable exposition of [EventSource Web API][] on the global scope.

### `--experimental-fileapi`

<!-- YAML
added: REPLACEME
-->

Enable exposition of the [File API][] globals `FileReader` and `ProgressEvent`
on the global scope. `FileReaderSync` is exposed only inside a Web Worker
started with `--experimental-web-worker`.

### `--experimental-import-meta-resolve`

<!-- YAML
Expand Down Expand Up @@ -4305,6 +4315,7 @@ one is included in the list below.
* `--experimental-detect-module`
* `--experimental-dtls`
* `--experimental-eventsource`
* `--experimental-fileapi`
* `--experimental-import-meta-resolve`
* `--experimental-import-text`
* `--experimental-json-modules`
Expand Down Expand Up @@ -4920,6 +4931,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[EventSource Web API]: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
[FIPS mode]: crypto.md#fips-mode
[File API]: https://w3c.github.io/FileAPI/
[File System Permissions]: permissions.md#file-system-permissions
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
[Module resolution and loading]: packages.md#module-resolution-and-loading
Expand Down
87 changes: 87 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,62 @@ added: v20.0.0

See {File}.

## Class: `FileReader`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.1 - Active development. Enable this API with the
> [`--experimental-fileapi`][] CLI flag.

An implementation of {FileReader} from the [File API][]. `readAsArrayBuffer()`,
`readAsBinaryString()`, `readAsText()`, and `readAsDataURL()` read a {Blob}
asynchronously and report the result through `loadstart`, `progress`, `load`,
`abort`, `error`, and `loadend` events. `readyState` is `FileReader.EMPTY`
(`0`), `FileReader.LOADING` (`1`), or `FileReader.DONE` (`2`).

`readAsDataURL()` produces a base64 `data:` URL. When the blob's type is the
empty string, the URL has no media type (`data:;base64,...`). Chrome and
Firefox use `application/octet-stream` in that case.

`readAsText()` follows {TextDecoder} for the chosen encoding, including its
ICU requirement. Labels that the Encoding Standard maps to the replacement
encoding, such as `'iso-2022-kr'`, decode to U+FFFD when the input is not
empty.

```js
const reader = new FileReader();
reader.addEventListener('load', () => {
console.log(reader.result); // Prints: hello
});
reader.readAsText(new Blob(['hello']));
```

## Class: `FileReaderSync`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.1 - Active development. Enable this API with the
> [`--experimental-fileapi`][] CLI flag.

An implementation of {FileReaderSync} from the [File API][]. It is
`[Exposed=(DedicatedWorker,SharedWorker)]`, so it exists only inside a
[Web Worker][] (`--experimental-web-worker`). It is not available on the main
thread or in threads created with `node:worker_threads`.

`readAsArrayBuffer()`, `readAsBinaryString()`, `readAsText()`, and
`readAsDataURL()` read a {Blob} synchronously. A file-backed blob, such as
one returned by [`fs.openAsBlob()`][], throws a `NotReadableError`.

```js
// Inside a Web Worker (`--experimental-web-worker`):
const text = new FileReaderSync().readAsText(new Blob(['hello']));
postMessage(text); // Sends 'hello'
```

## Class: `FormData`

<!-- YAML
Expand Down Expand Up @@ -942,6 +998,33 @@ added: v0.1.7

The process object. See the [`process` object][] section.

## Class: `ProgressEvent`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.1 - Active development. Enable this API with the
> [`--experimental-fileapi`][] CLI flag.

An implementation of {ProgressEvent} from the [XMLHttpRequest Standard][].
`FileReader`
dispatches `ProgressEvent`s for `loadstart`, `progress`, `load`, `abort`,
`error`, and `loadend`. Those events do not bubble and are not cancelable.
`lengthComputable`, `loaded`, and `total` return the values they were
initialized to (`false`, `0`, and `0` when omitted). The File API's
"fire a progress event" algorithm does not set those three fields, so events
fired by `FileReader` leave them at the defaults.

```js
const event = new ProgressEvent('progress', {
lengthComputable: true,
loaded: 50,
total: 100,
});
console.log(event.loaded / event.total); // Prints: 0.5
```

## `queueMicrotask(callback)`

<!-- YAML
Expand Down Expand Up @@ -1475,12 +1558,16 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[CommonJS module]: modules.md
[CommonJS modules]: modules.md
[ECMAScript module]: esm.md
[File API]: https://w3c.github.io/FileAPI/
[HTML Standard]: https://html.spec.whatwg.org/multipage/workers.html
[Navigator API]: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
[RFC 5646]: https://www.rfc-editor.org/rfc/rfc5646.txt
[Refable protocol]: process.md#processrefmayberefable
[Web Crypto API]: webcrypto.md
[Web Worker]: #class-worker
[XMLHttpRequest Standard]: https://xhr.spec.whatwg.org/#interface-progressevent
[`--experimental-eventsource`]: cli.md#--experimental-eventsource
[`--experimental-fileapi`]: cli.md#--experimental-fileapi
[`--experimental-web-worker`]: cli.md#--experimental-web-worker
[`--localstorage-file`]: cli.md#--localstorage-filefile
[`--no-experimental-global-navigator`]: cli.md#--no-experimental-global-navigator
Expand Down
7 changes: 7 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,11 @@ dtls documentation for details.
.It Fl -experimental-eventsource
Enable exposition of EventSource Web API on the global scope.
.
.It Fl -experimental-fileapi
Enable exposition of the File API globals \fBFileReader\fR and \fBProgressEvent\fR
on the global scope. \fBFileReaderSync\fR is exposed only inside a Web Worker
started with \fB--experimental-web-worker\fR.
.
.It Fl -experimental-import-meta-resolve
Enable experimental \fBimport.meta.resolve()\fR parent URL support, which allows
passing a second \fBparentURL\fR argument for contextual resolution.
Expand Down Expand Up @@ -2266,6 +2271,8 @@ one is included in the list below.
.It
\fB--experimental-eventsource\fR
.It
\fB--experimental-fileapi\fR
.It
\fB--experimental-import-meta-resolve\fR
.It
\fB--experimental-import-text\fR
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ export default [
DisposableStack: 'readonly',
EventSource: 'readonly',
fetch: 'readonly',
FileReader: 'readonly',
FileReaderSync: 'readonly',
Float16Array: 'readonly',
FormData: 'readonly',
localStorage: 'readonly',
navigator: 'readonly',
ProgressEvent: 'readonly',
QuotaExceededError: 'readonly',
ReadableByteStreamController: 'readonly',
ReadableStream: 'readonly',
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ exposeLazyInterfaces(globalThis, 'internal/webworker', ['Worker']);
exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
// https://www.w3.org/TR/FileAPI/#dfn-file
exposeLazyInterfaces(globalThis, 'internal/file', ['File']);
// https://w3c.github.io/FileAPI/#APIASynch
exposeLazyInterfaces(globalThis, 'internal/filereader', ['FileReader']);
// https://xhr.spec.whatwg.org/#interface-progressevent
exposeLazyInterfaces(globalThis, 'internal/progress_event', ['ProgressEvent']);
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
exposeLazyInterfaces(globalThis, 'perf_hooks', [
'Performance', 'PerformanceEntry', 'PerformanceMark', 'PerformanceMeasure',
Expand Down
1 change: 1 addition & 0 deletions lib/internal/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,5 @@ ObjectDefineProperties(File.prototype, {
module.exports = {
File,
TransferableFile,
isFile,
};
Loading
Loading