Skip to content

Commit 8d6ddb7

Browse files
authored
feat: make File's type more strict (nodejs#1703)
1 parent d8ae8e6 commit 8d6ddb7

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

lib/fetch/file.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { types } = require('util')
55
const { kState } = require('./symbols')
66
const { isBlobLike } = require('./util')
77
const { webidl } = require('./webidl')
8+
const { parseMIMEType, serializeAMimeType } = require('./dataURL')
89

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

39-
// 3. If the lastModified member is provided, let d be set to the
40-
// lastModified dictionary member. If it is not provided, set d to the
41-
// current date and time represented as the number of milliseconds since
42-
// the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
43-
const d = options.lastModified
41+
// eslint-disable-next-line no-labels
42+
substep: {
43+
if (t) {
44+
t = parseMIMEType(t)
45+
46+
if (t === 'failure') {
47+
t = ''
48+
// eslint-disable-next-line no-labels
49+
break substep
50+
}
51+
52+
t = serializeAMimeType(t).toLowerCase()
53+
}
54+
55+
// 3. If the lastModified member is provided, let d be set to the
56+
// lastModified dictionary member. If it is not provided, set d to the
57+
// current date and time represented as the number of milliseconds since
58+
// the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
59+
d = options.lastModified
60+
}
4461

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

52-
super(processBlobParts(fileBits, options), { type: options.type })
69+
super(processBlobParts(fileBits, options), { type: t })
5370
this[kState] = {
5471
name: n,
55-
lastModified: d
72+
lastModified: d,
73+
type: t
5674
}
5775
}
5876

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

93+
get type () {
94+
if (!(this instanceof File)) {
95+
throw new TypeError('Illegal invocation')
96+
}
97+
98+
return this[kState].type
99+
}
100+
75101
get [Symbol.toStringTag] () {
76102
return this.constructor.name
77103
}

test/wpt/status/FileAPI.status.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
{}
1+
{
2+
"File-constructor.any.js": {
3+
"fail": [
4+
"Using type in File constructor: nonparsable"
5+
]
6+
}
7+
}

0 commit comments

Comments
 (0)