-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
buffer: introduce File #45139
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
Merged
Merged
buffer: introduce File #45139
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f8940e9
buffer: introduce File
KhafraDev 5cd047d
fix: update type-parser
KhafraDev 991927d
fix: change linebreak to lf
KhafraDev 8325e81
fix: format docs
KhafraDev 246bb6f
fix: use shorthand property
KhafraDev 1944749
fix: add FileAPI/file WPTs
KhafraDev 5133587
fix: wpt add expected failures
KhafraDev d162b86
fix: bootstrap modules test & add in benchmark
KhafraDev 3763753
fix: apply suggestions from review
KhafraDev 2b33ee0
fix: apply suggestions from review
KhafraDev b0703ee
fix: handle invalid this
KhafraDev 0dcb54b
fix: and lint
KhafraDev da4de22
fix: add newline to wpt status
KhafraDev a4bb5f1
fix: move benchmark
KhafraDev 0c60761
fix: rebase & fix null options
KhafraDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const { File } = require('buffer'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
bytes: [128, 1024, 1024 ** 2], | ||
n: [1e6], | ||
operation: ['text', 'arrayBuffer'] | ||
}); | ||
|
||
const options = { | ||
lastModified: Date.now() - 1e6, | ||
}; | ||
|
||
async function run(n, bytes, operation) { | ||
const buff = Buffer.allocUnsafe(bytes); | ||
const source = new File(buff, 'dummy.txt', options); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
switch (operation) { | ||
case 'text': | ||
await source.text(); | ||
break; | ||
case 'arrayBuffer': | ||
await source.arrayBuffer(); | ||
break; | ||
} | ||
} | ||
bench.end(n); | ||
} | ||
|
||
function main(conf) { | ||
run(conf.n, conf.bytes, conf.operation).catch(console.log); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
const { | ||
DateNow, | ||
NumberIsNaN, | ||
ObjectDefineProperties, | ||
SymbolToStringTag, | ||
} = primordials; | ||
|
||
const { | ||
Blob, | ||
} = require('internal/blob'); | ||
|
||
const { | ||
customInspectSymbol: kInspect, | ||
emitExperimentalWarning, | ||
kEnumerableProperty, | ||
kEmptyObject, | ||
toUSVString, | ||
} = require('internal/util'); | ||
|
||
const { | ||
codes: { | ||
ERR_INVALID_THIS, | ||
ERR_MISSING_ARGS, | ||
}, | ||
} = require('internal/errors'); | ||
|
||
const { | ||
inspect, | ||
} = require('internal/util/inspect'); | ||
|
||
class File extends Blob { | ||
/** @type {string} */ | ||
#name; | ||
|
||
/** @type {number} */ | ||
#lastModified; | ||
|
||
constructor(fileBits, fileName, options = kEmptyObject) { | ||
emitExperimentalWarning('buffer.File'); | ||
|
||
if (arguments.length < 2) { | ||
throw new ERR_MISSING_ARGS('fileBits', 'fileName'); | ||
} | ||
|
||
super(fileBits, options); | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let { lastModified } = options ?? kEmptyObject; | ||
|
||
if (lastModified !== undefined) { | ||
// Using Number(...) will not throw an error for bigints. | ||
lastModified = +lastModified; | ||
|
||
if (NumberIsNaN(lastModified)) { | ||
lastModified = 0; | ||
} | ||
} else { | ||
lastModified = DateNow(); | ||
} | ||
|
||
this.#name = toUSVString(fileName); | ||
this.#lastModified = lastModified; | ||
} | ||
|
||
get name() { | ||
if (!this || !(#name in this)) { | ||
throw new ERR_INVALID_THIS('File'); | ||
} | ||
|
||
return this.#name; | ||
} | ||
|
||
get lastModified() { | ||
if (!this || !(#name in this)) { | ||
throw new ERR_INVALID_THIS('File'); | ||
} | ||
|
||
return this.#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.#name, | ||
lastModified: this.#lastModified, | ||
}, opts)}`; | ||
} | ||
} | ||
|
||
ObjectDefineProperties(File.prototype, { | ||
name: kEnumerableProperty, | ||
lastModified: kEnumerableProperty, | ||
[SymbolToStringTag]: { | ||
__proto__: null, | ||
configurable: true, | ||
value: 'File', | ||
} | ||
}); | ||
|
||
module.exports = { | ||
File, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// META: title=File constructor | ||
|
||
const to_string_obj = { toString: () => 'a string' }; | ||
const to_string_throws = { toString: () => { throw new Error('expected'); } }; | ||
|
||
test(function() { | ||
assert_true("File" in globalThis, "globalThis should have a File property."); | ||
}, "File interface object exists"); | ||
|
||
test(t => { | ||
assert_throws_js(TypeError, () => new File(), | ||
'Bits argument is required'); | ||
assert_throws_js(TypeError, () => new File([]), | ||
'Name argument is required'); | ||
}, 'Required arguments'); | ||
|
||
function test_first_argument(arg1, expectedSize, testName) { | ||
test(function() { | ||
var file = new File(arg1, "dummy"); | ||
assert_true(file instanceof File); | ||
assert_equals(file.name, "dummy"); | ||
assert_equals(file.size, expectedSize); | ||
assert_equals(file.type, ""); | ||
// assert_false(file.isClosed); XXX: File.isClosed doesn't seem to be implemented | ||
assert_not_equals(file.lastModified, ""); | ||
}, testName); | ||
} | ||
|
||
test_first_argument([], 0, "empty fileBits"); | ||
test_first_argument(["bits"], 4, "DOMString fileBits"); | ||
test_first_argument(["𝓽𝓮𝔁𝓽"], 16, "Unicode DOMString fileBits"); | ||
test_first_argument([new String('string object')], 13, "String object fileBits"); | ||
test_first_argument([new Blob()], 0, "Empty Blob fileBits"); | ||
test_first_argument([new Blob(["bits"])], 4, "Blob fileBits"); | ||
test_first_argument([new File([], 'world.txt')], 0, "Empty File fileBits"); | ||
test_first_argument([new File(["bits"], 'world.txt')], 4, "File fileBits"); | ||
test_first_argument([new ArrayBuffer(8)], 8, "ArrayBuffer fileBits"); | ||
test_first_argument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4, "Typed array fileBits"); | ||
test_first_argument(["bits", new Blob(["bits"]), new Blob(), new Uint8Array([0x50, 0x41]), | ||
new Uint16Array([0x5353]), new Uint32Array([0x53534150])], 16, "Various fileBits"); | ||
test_first_argument([12], 2, "Number in fileBits"); | ||
test_first_argument([[1,2,3]], 5, "Array in fileBits"); | ||
test_first_argument([{}], 15, "Object in fileBits"); // "[object Object]" | ||
if (globalThis.document !== undefined) { | ||
test_first_argument([document.body], 24, "HTMLBodyElement in fileBits"); // "[object HTMLBodyElement]" | ||
} | ||
test_first_argument([to_string_obj], 8, "Object with toString in fileBits"); | ||
test_first_argument({[Symbol.iterator]() { | ||
let i = 0; | ||
return {next: () => [ | ||
{done:false, value:'ab'}, | ||
{done:false, value:'cde'}, | ||
{done:true} | ||
][i++]}; | ||
}}, 5, 'Custom @@iterator'); | ||
|
||
[ | ||
'hello', | ||
0, | ||
null | ||
].forEach(arg => { | ||
test(t => { | ||
assert_throws_js(TypeError, () => new File(arg, 'world.html'), | ||
'Constructor should throw for invalid bits argument'); | ||
}, `Invalid bits argument: ${JSON.stringify(arg)}`); | ||
}); | ||
|
||
test(t => { | ||
assert_throws_js(Error, () => new File([to_string_throws], 'name.txt'), | ||
'Constructor should propagate exceptions'); | ||
}, 'Bits argument: object that throws'); | ||
|
||
|
||
function test_second_argument(arg2, expectedFileName, testName) { | ||
test(function() { | ||
var file = new File(["bits"], arg2); | ||
assert_true(file instanceof File); | ||
assert_equals(file.name, expectedFileName); | ||
}, testName); | ||
} | ||
|
||
test_second_argument("dummy", "dummy", "Using fileName"); | ||
test_second_argument("dummy/foo", "dummy/foo", | ||
"No replacement when using special character in fileName"); | ||
test_second_argument(null, "null", "Using null fileName"); | ||
test_second_argument(1, "1", "Using number fileName"); | ||
test_second_argument('', '', "Using empty string fileName"); | ||
if (globalThis.document !== undefined) { | ||
test_second_argument(document.body, '[object HTMLBodyElement]', "Using object fileName"); | ||
} | ||
|
||
// testing the third argument | ||
[ | ||
{type: 'text/plain', expected: 'text/plain'}, | ||
{type: 'text/plain;charset=UTF-8', expected: 'text/plain;charset=utf-8'}, | ||
{type: 'TEXT/PLAIN', expected: 'text/plain'}, | ||
{type: '𝓽𝓮𝔁𝓽/𝔭𝔩𝔞𝔦𝔫', expected: ''}, | ||
{type: 'ascii/nonprintable\u001F', expected: ''}, | ||
{type: 'ascii/nonprintable\u007F', expected: ''}, | ||
{type: 'nonascii\u00EE', expected: ''}, | ||
{type: 'nonascii\u1234', expected: ''}, | ||
{type: 'nonparsable', expected: 'nonparsable'} | ||
].forEach(testCase => { | ||
test(t => { | ||
var file = new File(["bits"], "dummy", { type: testCase.type}); | ||
assert_true(file instanceof File); | ||
assert_equals(file.type, testCase.expected); | ||
}, `Using type in File constructor: ${testCase.type}`); | ||
}); | ||
test(function() { | ||
var file = new File(["bits"], "dummy", { lastModified: 42 }); | ||
assert_true(file instanceof File); | ||
assert_equals(file.lastModified, 42); | ||
}, "Using lastModified"); | ||
test(function() { | ||
var file = new File(["bits"], "dummy", { name: "foo" }); | ||
assert_true(file instanceof File); | ||
assert_equals(file.name, "dummy"); | ||
}, "Misusing name"); | ||
test(function() { | ||
var file = new File(["bits"], "dummy", { unknownKey: "value" }); | ||
assert_true(file instanceof File); | ||
assert_equals(file.name, "dummy"); | ||
}, "Unknown properties are ignored"); | ||
|
||
[ | ||
123, | ||
123.4, | ||
true, | ||
'abc' | ||
].forEach(arg => { | ||
test(t => { | ||
assert_throws_js(TypeError, () => new File(['bits'], 'name.txt', arg), | ||
'Constructor should throw for invalid property bag type'); | ||
}, `Invalid property bag: ${JSON.stringify(arg)}`); | ||
}); | ||
|
||
[ | ||
null, | ||
undefined, | ||
[1,2,3], | ||
/regex/, | ||
function() {} | ||
].forEach(arg => { | ||
test(t => { | ||
assert_equals(new File(['bits'], 'name.txt', arg).size, 4, | ||
'Constructor should accept object-ish property bag type'); | ||
}, `Unusual but valid property bag: ${arg}`); | ||
}); | ||
|
||
test(t => { | ||
assert_throws_js(Error, | ||
() => new File(['bits'], 'name.txt', {type: to_string_throws}), | ||
'Constructor should propagate exceptions'); | ||
}, 'Property bag propagates exceptions'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.