Files
node/lib/internal/file.js
Filip Skokan aead708af4 buffer: align Blob and File with Web IDL
Convert constructor arguments in Web IDL order before processing blob
parts, including inherited dictionary members and File.lastModified's
long long conversion.

Read buffer slots and copy bytes without invoking user getters, slice
methods, or species constructors. Treat detached parts as empty and
track Blob brands without invoking user getters or accepting proxies.

Keep rejecting bare SharedArrayBuffers and shared-memory views for
browser compatibility pending resolution of the union conversion rules.

Refs: https://github.com/whatwg/webidl/issues/1491
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/66776
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-11-05 22:26:21 +00:00

155 lines
3.2 KiB
JavaScript

'use strict';
const {
DateNow,
FunctionPrototypeCall,
ObjectDefineProperties,
ObjectSetPrototypeOf,
Symbol,
SymbolToStringTag,
} = primordials;
const {
Blob,
blobPartsConverter,
blobPropertyBag,
kAlreadyConverted,
TransferableBlob,
} = require('internal/blob');
const {
converters,
createDictionaryConverter,
} = require('internal/webidl');
const {
customInspectSymbol: kInspect,
kEnumerableProperty,
kEmptyObject,
} = require('internal/util');
const {
codes: {
ERR_INVALID_THIS,
ERR_MISSING_ARGS,
},
} = require('internal/errors');
const {
inspect,
} = require('internal/util/inspect');
const {
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');
const kState = Symbol('state');
const filePropertyBagConverter = createDictionaryConverter('FilePropertyBag', [
blobPropertyBag,
[{ key: 'lastModified', converter: converters['long long'] }],
]);
function isFile(object) {
return object?.[kState] !== undefined;
}
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');
}
fileBits = blobPartsConverter(fileBits, { __proto__: null, context: 'fileBits' });
fileName = converters.USVString(fileName, { __proto__: null, context: 'fileName' });
options = filePropertyBagConverter(options, { __proto__: null, context: 'options' });
super(fileBits, options, kAlreadyConverted);
this[kState] = new FileState(fileName, options.lastModified ?? DateNow());
}
get name() {
if (!isFile(this))
throw new ERR_INVALID_THIS('File');
return this[kState].name;
}
get lastModified() {
if (!isFile(this))
throw new ERR_INVALID_THIS('File');
return this[kState].lastModified;
}
[kInspect](depth, options) {
if (depth < 0) {
return this;
}
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1,
};
return `File ${inspect({
size: this.size,
type: this.type,
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 = '') {
FunctionPrototypeCall(TransferableBlob, this, handle, length, type);
ObjectSetPrototypeOf(this, File.prototype);
}
ObjectSetPrototypeOf(TransferableFile.prototype, File.prototype);
ObjectSetPrototypeOf(TransferableFile, File);
ObjectDefineProperties(File.prototype, {
name: kEnumerableProperty,
lastModified: kEnumerableProperty,
[SymbolToStringTag]: {
__proto__: null,
configurable: true,
value: 'File',
},
});
module.exports = {
File,
TransferableFile,
};