Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: make File cloneable #47613

Merged
merged 5 commits into from
Sep 26, 2024
Merged
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
3 changes: 3 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -5094,6 +5094,9 @@ added:
- v19.2.0
- v18.13.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47613
description: Makes File instances cloneable.
- version: v20.0.0
pr-url: https://github.com/nodejs/node/pull/47153
description: No longer experimental.
Expand Down
1 change: 1 addition & 0 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,5 @@ module.exports = {
isBlob,
kHandle,
resolveObjectURL,
TransferableBlob,
};
72 changes: 61 additions & 11 deletions lib/internal/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

const {
DateNow,
FunctionPrototypeApply,
NumberIsNaN,
ObjectDefineProperties,
ObjectSetPrototypeOf,
StringPrototypeToWellFormed,
Symbol,
SymbolToStringTag,
} = primordials;

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

const {
Expand All @@ -20,6 +24,7 @@ const {

const {
codes: {
ERR_INVALID_THIS,
ERR_MISSING_ARGS,
},
} = require('internal/errors');
Expand All @@ -28,13 +33,32 @@ const {
inspect,
} = require('internal/util/inspect');

class File extends Blob {
/** @type {string} */
#name;
const {
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');

const kState = Symbol('state');

function isFile(object) {
return object?.[kState] !== undefined;
}

/** @type {number} */
#lastModified;
class FileState {
name;
lastModified;

/**
* @param {string} name
* @param {number} lastModified
*/
constructor(name, lastModified) {
this.name = name;
this.lastModified = lastModified;
}
}

class File extends Blob {
constructor(fileBits, fileName, options = kEmptyObject) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('fileBits', 'fileName');
Expand All @@ -55,16 +79,21 @@ class File extends Blob {
lastModified = DateNow();
}

this.#name = StringPrototypeToWellFormed(`${fileName}`);
this.#lastModified = lastModified;
this[kState] = new FileState(StringPrototypeToWellFormed(`${fileName}`), lastModified);
}

get name() {
return this.#name;
if (!isFile(this))
throw new ERR_INVALID_THIS('File');

return this[kState].name;
}

get lastModified() {
return this.#lastModified;
if (!isFile(this))
throw new ERR_INVALID_THIS('File');

return this[kState].lastModified;
}

[kInspect](depth, options) {
Expand All @@ -80,12 +109,32 @@ class File extends Blob {
return `File ${inspect({
size: this.size,
type: this.type,
name: this.#name,
lastModified: this.#lastModified,
name: this[kState].name,
lastModified: this[kState].lastModified,
}, opts)}`;
}

[kClone]() {
return {
data: { ...super[kClone]().data, ...this[kState] },
deserializeInfo: 'internal/file:TransferableFile',
};
}

[kDeserialize](data) {
super[kDeserialize](data);

this[kState] = new FileState(data.name, data.lastModified);
}
}

function TransferableFile(handle, length, type = '') {
FunctionPrototypeApply(TransferableBlob, this, [handle, length, type]);
}

ObjectSetPrototypeOf(TransferableFile.prototype, File.prototype);
ObjectSetPrototypeOf(TransferableFile, File);

ObjectDefineProperties(File.prototype, {
name: kEnumerableProperty,
lastModified: kEnumerableProperty,
Expand All @@ -98,4 +147,5 @@ ObjectDefineProperties(File.prototype, {

module.exports = {
File,
TransferableFile,
};
22 changes: 22 additions & 0 deletions test/parallel/test-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,25 @@ const { inspect } = require('util');
);
});
}

(async () => {
// File should be cloneable via structuredClone.
// Refs: https://github.com/nodejs/node/issues/47612

const body = ['hello, ', 'world'];
const lastModified = Date.now() - 10_000;
const name = 'hello_world.txt';

const file = new File(body, name, { lastModified });
const clonedFile = structuredClone(file);

assert.deepStrictEqual(await clonedFile.text(), await file.text());
assert.deepStrictEqual(clonedFile.lastModified, file.lastModified);
assert.deepStrictEqual(clonedFile.name, file.name);

const clonedFile2 = structuredClone(clonedFile);

assert.deepStrictEqual(await clonedFile2.text(), await clonedFile.text());
assert.deepStrictEqual(clonedFile2.lastModified, clonedFile.lastModified);
assert.deepStrictEqual(clonedFile2.name, clonedFile.name);
})().then(common.mustCall());
8 changes: 1 addition & 7 deletions test/wpt/status/html/webappapis/structured-clone.json
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{
"structured-clone.any.js": {
"fail": {
"expected": ["File basic"]
}
}
}
{}
Loading