-
-
Notifications
You must be signed in to change notification settings - Fork 35.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 1 commit
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
Next
Next commit
buffer: introduce File
- Loading branch information
commit f8940e9a81a0af8bfb099266ef9786defed843f0
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
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,117 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| DateNow, | ||
| NumberIsNaN, | ||
| ObjectDefineProperties, | ||
| ObjectPrototypeHasOwnProperty, | ||
| Symbol, | ||
| 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'); | ||
|
|
||
| const kState = Symbol('kState'); | ||
|
|
||
| function isFile(object) { | ||
| return object?.[kState] !== undefined || object instanceof Blob; | ||
|
KhafraDev marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| class File extends Blob { | ||
| 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.
|
||
|
|
||
| let lastModified; | ||
|
|
||
| if (ObjectPrototypeHasOwnProperty(options, 'lastModified')) { | ||
|
KhafraDev marked this conversation as resolved.
Outdated
|
||
| // Using Number(...) will not throw an error for bigints. | ||
| lastModified = +options.lastModified; | ||
|
KhafraDev marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (NumberIsNaN(lastModified)) { | ||
| lastModified = 0; | ||
| } | ||
| } else { | ||
| lastModified = DateNow(); | ||
| } | ||
|
|
||
| this[kState] = { | ||
| name: toUSVString(fileName), | ||
| lastModified: lastModified, | ||
|
KhafraDev marked this conversation as resolved.
Outdated
|
||
| }; | ||
| } | ||
|
|
||
| get name() { | ||
| if (!isFile(this)) { | ||
| throw new ERR_INVALID_THIS('File'); | ||
| } | ||
|
KhafraDev marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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.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,122 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { Blob, File } = require('buffer'); | ||
| const { inspect } = require('util'); | ||
|
|
||
| { | ||
| // ensure File extends Blob | ||
| assert.deepStrictEqual(Object.getPrototypeOf(File.prototype), Blob.prototype); | ||
| } | ||
|
|
||
| { | ||
| assert.throws(() => new File(), TypeError); | ||
| assert.throws(() => new File([]), TypeError); | ||
| } | ||
|
|
||
| { | ||
| const properties = ['name', 'lastModified']; | ||
|
|
||
| for (const prop of properties) { | ||
| const desc = Object.getOwnPropertyDescriptor(File.prototype, prop); | ||
| assert.notStrictEqual(desc, undefined); | ||
| // Ensure these properties are getters. | ||
| assert.strictEqual(desc.get?.name, `get ${prop}`); | ||
| assert.strictEqual(desc.set, undefined); | ||
| assert.strictEqual(desc.enumerable, true); | ||
| assert.strictEqual(desc.configurable, true); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| const file = new File([], ''); | ||
| assert.strictEqual(file[Symbol.toStringTag], 'File'); | ||
| assert.strictEqual(File.prototype[Symbol.toStringTag], 'File'); | ||
| } | ||
|
|
||
| { | ||
| assert.throws(() => File.prototype.name, TypeError); | ||
| assert.throws(() => File.prototype.lastModified, TypeError); | ||
| } | ||
|
|
||
| { | ||
| const keys = Object.keys(File.prototype).sort(); | ||
| assert.deepStrictEqual(keys, ['lastModified', 'name']); | ||
| } | ||
|
|
||
| { | ||
| const file = new File([], 'dummy.txt.exe'); | ||
| assert.strictEqual(file.name, 'dummy.txt.exe'); | ||
| assert.strictEqual(file.size, 0); | ||
| assert.strictEqual(typeof file.lastModified, 'number'); | ||
| assert(file.lastModified <= Date.now()); | ||
| } | ||
|
|
||
| { | ||
| const emptyFile = new File([], 'empty.txt'); | ||
| const blob = new Blob(['hello world']); | ||
|
|
||
| emptyFile.text.call(blob).then(common.mustCall((text) => { | ||
| assert.strictEqual(text, 'hello world'); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| const toPrimitive = { | ||
| [Symbol.toPrimitive]() { | ||
| return 'NaN'; | ||
| } | ||
| }; | ||
|
|
||
| const invalidLastModified = [ | ||
| null, | ||
| undefined, | ||
| 'string', | ||
| false, | ||
| toPrimitive, | ||
| ]; | ||
|
|
||
| for (const lastModified of invalidLastModified) { | ||
| const file = new File([], '', { lastModified }); | ||
| assert.strictEqual(file.lastModified, 0); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| const toPrimitive = { | ||
| [Symbol.toPrimitive]() { | ||
| throw new TypeError('boom'); | ||
| } | ||
| }; | ||
|
|
||
| const throwValues = [ | ||
| BigInt(3n), | ||
| toPrimitive, | ||
| ]; | ||
|
|
||
| for (const lastModified of throwValues) { | ||
| assert.throws(() => new File([], '', { lastModified }), TypeError); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| const valid = [ | ||
| { | ||
| [Symbol.toPrimitive]() { | ||
| return 10; | ||
| } | ||
| }, | ||
| new Number(10), | ||
| 10, | ||
| ]; | ||
|
|
||
| for (const lastModified of valid) { | ||
| assert.strictEqual(new File([], '', { lastModified }).lastModified, 10); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| const file = new File([], ''); | ||
| assert(inspect(file).startsWith('File { size: 0, type: \'\', name: \'\', lastModified:')); | ||
| } |
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.