Skip to content

Commit

Permalink
url,buffer: implement URL.createObjectURL
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed Aug 7, 2021
1 parent 51fcc3e commit e91203d
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -4952,6 +4952,20 @@ added: v3.0.0

An alias for [`buffer.constants.MAX_STRING_LENGTH`][].

### `buffer.resolveObjectURL(id)`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* `id` {string} A `'blob:node:...` URL string returned by a prior call to
`URL.createObjectURL()`.
* Returns: {Blob}

Resolves a `'blob:node:...'` an associated {Blob} object registered using
a prior call to `URL.createObjectURL()`.

### `buffer.transcode(source, fromEnc, toEnc)`
<!-- YAML
added: v7.1.0
Expand Down
48 changes: 48 additions & 0 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,54 @@ console.log(JSON.stringify(myURLs));
// Prints ["https://www.example.com/","https://test.example.org/"]
```

#### `URL.createObjectURL(blob)`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* `blob` {Blob}
* Returns: {string}

Creates a `'blob:node:...'` 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. The `threadId` of the owning thread
is encoded within the generated object URL.

#### `URL.revokeObjectURL(id)`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* `id` {string} A `'blob:node:...` URL string returned by a prior call to
`URL.createObjectURL()`.

Removes the stored {Blob} identified by the given ID.

### Class: `URLSearchParams`
<!-- YAML
added:
Expand Down
2 changes: 2 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const {

const {
Blob,
resolveObjectURL,
} = require('internal/blob');

FastBuffer.prototype.constructor = Buffer;
Expand Down Expand Up @@ -1239,6 +1240,7 @@ function atob(input) {

module.exports = {
Blob,
resolveObjectURL,
Buffer,
SlowBuffer,
transcode,
Expand Down
48 changes: 48 additions & 0 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
const {
createBlob: _createBlob,
FixedSizeBlobCopyJob,
getDataObject,
} = internalBinding('buffer');

const { TextDecoder } = require('internal/encoding');
Expand Down Expand Up @@ -65,6 +66,14 @@ const disallowedTypeCharacters = /[^\u{0020}-\u{007E}]/u;

let Buffer;
let ReadableStream;
let threadId;
let URL;

function lazyURL(id) {
if (URL === undefined)
URL = require('url').URL;
return new URL(id);
}

function lazyBuffer() {
if (Buffer === undefined)
Expand All @@ -80,6 +89,12 @@ function lazyReadableStream(options) {
return new ReadableStream(options);
}

function lazyThreadId() {
if (threadId === undefined)
threadId = require('worker_threads').threadId;
return threadId;
}

function isBlob(object) {
return object?.[kHandle] !== undefined;
}
Expand Down Expand Up @@ -315,9 +330,42 @@ ObjectDefineProperty(Blob.prototype, SymbolToStringTag, {
value: 'Blob',
});

function resolveObjectURL(url) {
url = `${url}`;
try {
const parsed = new lazyURL(url);
const {
0: base,
1: threadId,
2: id,
} = parsed.pathname.split(':');

if (base !== 'node' || +threadId !== lazyThreadId())
return;

const ret = getDataObject(id);

if (ret === undefined)
return;

const {
0: handle,
1: length,
2: type,
} = ret;

if (handle !== undefined)
return createBlob(handle, length, type);
} catch {
// If there's an error, it's ignored and nothing is returned
}
}

module.exports = {
Blob,
ClonedBlob,
createBlob,
isBlob,
kHandle,
resolveObjectURL,
};
51 changes: 51 additions & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ const {
kSchemeStart
} = internalBinding('url');

const {
storeDataObject,
revokeDataObject,
} = internalBinding('buffer');

const context = Symbol('context');
const cannotBeBase = Symbol('cannot-be-base');
const cannotHaveUsernamePasswordPort =
Expand All @@ -108,6 +113,28 @@ const special = Symbol('special');
const searchParams = Symbol('query');
const kFormat = Symbol('format');

let blob;
let cryptoRandom;
let threadId;

function lazyBlob() {
if (blob === undefined)
blob = require('internal/blob');
return blob;
}

function lazyCryptoRandom() {
if (cryptoRandom === undefined)
cryptoRandom = require('internal/crypto/random');
return cryptoRandom;
}

function lazyThreadId() {
if (threadId === undefined)
threadId = require('worker_threads').threadId;
return threadId;
}

// https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object
const IteratorPrototype = ObjectGetPrototypeOf(
ObjectGetPrototypeOf([][SymbolIterator]())
Expand Down Expand Up @@ -930,6 +957,30 @@ class URL {
toJSON() {
return this[kFormat]({});
}

static createObjectURL(obj) {
const blob = lazyBlob();
if (!blob.isBlob(obj))
throw new ERR_INVALID_ARG_TYPE('obj', 'Blob', obj);

const id = lazyCryptoRandom().randomUUID();

storeDataObject(id, obj[blob.kHandle], obj.size, obj.type);

return `blob:node:${lazyThreadId()}:${id}`;
}

static revokeObjectURL(url) {
url = `${url}`;
try {
const parsed = new URL(url);
const { 2: id } = parsed.pathname.split(':');
if (id !== undefined)
revokeDataObject(id);
} catch {
// If there's an error, it's ignored.
}
}
}

ObjectDefineProperties(URL.prototype, {
Expand Down
19 changes: 19 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,25 @@ void Environment::set_process_exit_handler(
process_exit_handler_ = std::move(handler);
}

void Environment::store_data_object(
const std::string& uuid,
const Environment::StoredDataObject& data_object) {
data_objects[uuid] = data_object;
}

void Environment::erase_data_object(const std::string& uuid) {
data_objects.erase(uuid);
}

Environment::StoredDataObject Environment::get_data_object(
const std::string& uuid) {
auto item = data_objects.find(uuid);
if (item == data_objects.end()) {
return Environment::StoredDataObject {};
}
return item->second;
}

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
Expand Down
2 changes: 2 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ void Environment::CleanupHandles() {
!handle_wrap_queue_.IsEmpty()) {
uv_run(event_loop(), UV_RUN_ONCE);
}

data_objects.clear();
}

void Environment::StartProfilerIdleNotifier() {
Expand Down
17 changes: 17 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,20 @@ class Environment : public MemoryRetainer {

inline int32_t stack_trace_limit() const { return 10; }

struct StoredDataObject {
BaseObjectPtr<BaseObject> data_object;
size_t length;
std::string type;
};

inline void store_data_object(
const std::string& uuid,
const StoredDataObject& object);

inline void erase_data_object(const std::string& uuid);

inline StoredDataObject get_data_object(const std::string& uuid);

#if HAVE_INSPECTOR
void set_coverage_connection(
std::unique_ptr<profiler::V8CoverageConnection> connection);
Expand Down Expand Up @@ -1611,6 +1625,9 @@ class Environment : public MemoryRetainer {
// a given pointer.
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>
released_allocated_buffers_;

// Maintains stored Blobs used with the URL.createObjectURL() API
std::unordered_map<std::string, StoredDataObject> data_objects;
};

} // namespace node
Expand Down
Loading

0 comments on commit e91203d

Please sign in to comment.