/
file.js
201 lines (155 loc) Β· 5.37 KB
/
file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict'
const { Blob } = require('buffer')
const { kState } = require('./symbols')
class File extends Blob {
constructor (fileBits, fileName, options = {}) {
// TODO: argument idl type check
// The File constructor is invoked with two or three parameters, depending
// on whether the optional dictionary parameter is used. When the File()
// constructor is invoked, user agents must run the following steps:
// 1. Let bytes be the result of processing blob parts given fileBits and
// options.
// TODO
// 2. Let n be the fileName argument to the constructor.
const n = fileName
// 3. Process FilePropertyBag dictionary argument by running the following
// substeps:
// 1. If the type member is provided and is not the empty string, let t
// be set to the type dictionary member. If t contains any characters
// outside the range U+0020 to U+007E, then set t to the empty string
// and return from these substeps.
// TODO
const t = options.type
// 2. Convert every character in t to ASCII lowercase.
// TODO
// 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 ?? Date.now()
// 4. Return a new File object F such that:
// F refers to the bytes byte sequence.
// F.size is set to the number of total bytes in bytes.
// F.name is set to n.
// F.type is set to t.
// F.lastModified is set to d.
// TODO
super(fileBits, { type: t })
this[kState] = {
name: n,
lastModified: d
}
}
get name () {
if (!(this instanceof File)) {
throw new TypeError('Illegal invocation')
}
return this[kState].name
}
get lastModified () {
if (!(this instanceof File)) {
throw new TypeError('Illegal invocation')
}
return this[kState].lastModified
}
get [Symbol.toStringTag] () {
if (!(this instanceof File)) {
throw new TypeError('Illegal invocation')
}
return this.constructor.name
}
}
class FileLike {
constructor (blobLike, fileName, options = {}) {
// TODO: argument idl type check
// The File constructor is invoked with two or three parameters, depending
// on whether the optional dictionary parameter is used. When the File()
// constructor is invoked, user agents must run the following steps:
// 1. Let bytes be the result of processing blob parts given fileBits and
// options.
// 2. Let n be the fileName argument to the constructor.
const n = fileName
// 3. Process FilePropertyBag dictionary argument by running the following
// substeps:
// 1. If the type member is provided and is not the empty string, let t
// be set to the type dictionary member. If t contains any characters
// outside the range U+0020 to U+007E, then set t to the empty string
// and return from these substeps.
// TODO
const t = options.type
// 2. Convert every character in t to ASCII lowercase.
// TODO
// 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 ?? Date.now()
// 4. Return a new File object F such that:
// F refers to the bytes byte sequence.
// F.size is set to the number of total bytes in bytes.
// F.name is set to n.
// F.type is set to t.
// F.lastModified is set to d.
this[kState] = {
blobLike,
name: n,
type: t,
lastModified: d
}
}
stream (...args) {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].blobLike.stream(...args)
}
arrayBuffer (...args) {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].blobLike.arrayBuffer(...args)
}
slice (...args) {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].blobLike.slice(...args)
}
text (...args) {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].blobLike.text(...args)
}
get size () {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].blobLike.size
}
get type () {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].blobLike.type
}
get name () {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].name
}
get lastModified () {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return this[kState].lastModified
}
get [Symbol.toStringTag] () {
if (!(this instanceof FileLike)) {
throw new TypeError('Illegal invocation')
}
return 'File'
}
}
module.exports = { File: globalThis.File ?? File, FileLike }