From 0ae84b16805d0c5f692fad73d1f3f6b9dd71ce4e Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 6 Aug 2021 19:26:37 -0700 Subject: [PATCH] url,buffer: implement URL.createObjectURL Signed-off-by: James M Snell --- doc/api/buffer.md | 14 ++ doc/api/url.md | 47 ++++++ lib/buffer.js | 2 + lib/internal/blob.js | 67 +++++++-- lib/internal/url.js | 79 ++++++++-- src/node_binding.cc | 1 + src/node_blob.cc | 166 ++++++++++++++++++++- src/node_blob.h | 56 ++++++- src/node_buffer.cc | 5 - src/node_external_reference.h | 1 + src/node_snapshotable.cc | 1 + src/node_snapshotable.h | 3 +- test/parallel/test-blob-createobjecturl.js | 48 ++++++ test/parallel/test-bootstrap-modules.js | 1 + 14 files changed, 462 insertions(+), 29 deletions(-) create mode 100644 test/parallel/test-blob-createobjecturl.js diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 7106d5c6b36fda..f419be1181a61a 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -4952,6 +4952,20 @@ added: v3.0.0 An alias for [`buffer.constants.MAX_STRING_LENGTH`][]. +### `buffer.resolveObjectURL(id)` + + +> Stability: 1 - Experimental + +* `id` {string} A `'blob:nodedata:...` URL string returned by a prior call to + `URL.createObjectURL()`. +* Returns: {Blob} + +Resolves a `'blob:nodedata:...'` an associated {Blob} object registered using +a prior call to `URL.createObjectURL()`. + ### `buffer.transcode(source, fromEnc, toEnc)` + +> Stability: 1 - Experimental + +* `blob` {Blob} +* Returns: {string} + +Creates a `'blob:nodedata:...'` URL string that represents the given {Blob} +object and can be used to retrieve the `Blob` later. + +```js +const { + Blob, + resolveObjectURL, +} = require('buffer'); + +const blob = new Blob(['hello']); +const id = URL.createObjectURL(blob); + +// later... + +const otherBlob = resolveObjectURL(id); +console.log(otherBlob.size); +``` + +The data stored by the registered {Blob} will be retained in memory until +`URL.revokeObjectURL()` is called to remove it. + +`Blob` objects are registered within the current thread. If using Worker +Threads, `Blob` objects registered within one Worker will not be available +to other workers or the main thread. + +#### `URL.revokeObjectURL(id)` + + +> Stability: 1 - Experimental + +* `id` {string} A `'blob:nodedata:...` URL string returned by a prior call to + `URL.createObjectURL()`. + +Removes the stored {Blob} identified by the given ID. + ### Class: `URLSearchParams`