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
83 changes: 83 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,60 @@ added: v20.0.0

See {File}.

## Class: `FileReader`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.0 Active development?


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 - Experimental

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 +996,32 @@ added: v0.1.7

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

## Class: `ProgressEvent`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

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,11 +1555,14 @@ 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-web-worker`]: cli.md#--experimental-web-worker
[`--localstorage-file`]: cli.md#--localstorage-filefile
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