Skip to content

Commit

Permalink
feat: make File's type more strict (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Oct 15, 2022
1 parent d8ae8e6 commit 8d6ddb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
42 changes: 34 additions & 8 deletions lib/fetch/file.js
Expand Up @@ -5,6 +5,7 @@ const { types } = require('util')
const { kState } = require('./symbols')
const { isBlobLike } = require('./util')
const { webidl } = require('./webidl')
const { parseMIMEType, serializeAMimeType } = require('./dataURL')

class File extends Blob {
constructor (fileBits, fileName, options = {}) {
Expand Down Expand Up @@ -34,13 +35,29 @@ class File extends Blob {
// outside the range U+0020 to U+007E, then set t to the empty string
// and return from these substeps.
// 2. Convert every character in t to ASCII lowercase.
// Note: Blob handles both of these steps for us
let t = options.type
let d

// 3. If the lastModified member is provided, let d be set to the
// lastModified dictionary member. If it is not provided, set d to the
// current date and time represented as the number of milliseconds since
// the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
const d = options.lastModified
// eslint-disable-next-line no-labels
substep: {
if (t) {
t = parseMIMEType(t)

if (t === 'failure') {
t = ''
// eslint-disable-next-line no-labels
break substep
}

t = serializeAMimeType(t).toLowerCase()
}

// 3. If the lastModified member is provided, let d be set to the
// lastModified dictionary member. If it is not provided, set d to the
// current date and time represented as the number of milliseconds since
// the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
d = options.lastModified
}

// 4. Return a new File object F such that:
// F refers to the bytes byte sequence.
Expand All @@ -49,10 +66,11 @@ class File extends Blob {
// F.type is set to t.
// F.lastModified is set to d.

super(processBlobParts(fileBits, options), { type: options.type })
super(processBlobParts(fileBits, options), { type: t })
this[kState] = {
name: n,
lastModified: d
lastModified: d,
type: t
}
}

Expand All @@ -72,6 +90,14 @@ class File extends Blob {
return this[kState].lastModified
}

get type () {
if (!(this instanceof File)) {
throw new TypeError('Illegal invocation')
}

return this[kState].type
}

get [Symbol.toStringTag] () {
return this.constructor.name
}
Expand Down
8 changes: 7 additions & 1 deletion test/wpt/status/FileAPI.status.json
@@ -1 +1,7 @@
{}
{
"File-constructor.any.js": {
"fail": [
"Using type in File constructor: nonparsable"
]
}
}

0 comments on commit 8d6ddb7

Please sign in to comment.