This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
normalise-input.js
613 lines (559 loc) · 17.5 KB
/
normalise-input.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// @ts-check
'use strict'
const errCode = require('err-code')
const { File, Blob, readBlob } = require('./blob')
/**
* @template T
* @typedef {Iterable<T>|AsyncIterable<T>|ReadableStream<T>} Multiple
*/
/**
* @typedef {ExtendedFile | FileStream | Directory} NormalizedAddInput
* @typedef {SingleFileInput | MultiFileInput} Input
* @typedef {Blob|Bytes|string|FileObject|Iterable<Number>|Multiple<Bytes>} SingleFileInput
* @typedef {Multiple<Blob>|Multiple<string>|Multiple<FileObject>} MultiFileInput
*
* @typedef {Object} FileObject
* @property {string} [path]
* @property {string} [type] - MIME type of the file.
* @property {FileContent} [content]
* @property {Mode} [mode]
* @property {UnixFSTime} [mtime]
* @typedef {Blob|Bytes|string|Iterable<number>|Multiple<Bytes>} FileContent
*
* @typedef {ArrayBuffer|ArrayBufferView} Bytes
*
*@typedef {string|number|InstanceType<typeof String>} Mode
* @typedef {Date|UnixFSTime|UnixFSTimeSpec|HRTime} MTime
* @typedef {Object} UnixFSTime
* @property {number} secs
* @property {number} [nsecs]
*
* @typedef {Object} UnixFSTimeSpec
* @property {number} Seconds
* @property {number} [FractionalNanoseconds]
*
* @typedef {[number, number]} HRTime - Node process.hrtime
*/
/**
* Normalizes input into async iterable of extended File or custom FileStream
* objects.
*
* @param {Input} input
* @return {AsyncIterable<NormalizedAddInput>}
*/
module.exports = async function * normaliseInput (input) {
// must give us something
if (input == null) {
throw errCode(new Error(`Unexpected input: ${input}`), 'ERR_UNEXPECTED_INPUT')
}
// If input is a one of the following types
// - string
// - ArrayBuffer
// - ArrayBufferView
// - Blob
// - FileObject
// It is turned into collection of one file (with that content)
const file = asFile(input)
if (file != null) {
yield file
return
}
// If input is sync iterable we expect it to be a homogenous collection &
// need to probe it's first item to tell if input to be interpreted as single
// file with multiple chunks or multiple files.
// NOTE: We had to ensure that input was not string or arraybuffer view
// because those are also iterables.
/** @type {null|Iterable<*>} */
const iterable = asIterable(input)
if (iterable != null) {
yield * normaliseIterableInput(iterable)
// Return here since we have have exhasted an input iterator.
return
}
// If we got here than we are dealing with async input, which can be either
// readable stream or an async iterable (casting former to later)
const stream = asReadableStream(input)
const asyncIterable = stream
? iterateReadableStream(stream)
: asAsyncIterable(input)
// Async iterable (which we assume to be homogenous) may represent single file
// with multilpe chunks or multiple files, to decide we probe it's first item.
if (asyncIterable != null) {
// Create peekable to be able to probe head without consuming it.
const peekable = AsyncPeekable.from(asyncIterable)
const { done, value } = await peekable.peek()
// If done input was empty so we return early.
if (done) {
return
}
// If first item is array buffer or one of it's views input represents a
// single file with multiple chunks.
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
yield new FileStream(peekable, '')
// Otherwise we interpret input as async collection of multiple files.
// In that case itemss of input can be either `string`, `Blob` or
// `FileObject`, so we normalize each to a file. If item is anything else
// we throw an exception.
} else {
for await (const content of peekable) {
// Note: If content here is `ArrayBuffer` or a view this will turn it
// into a file, but that can only occur if async iterable contained
// variadic chunks which is not supported.
const file = asFile(content)
if (file) {
yield file
} else {
throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
}
}
}
return
}
throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
}
/**
*
* @param {Iterable<ArrayBuffer>|Iterable<ArrayBufferView>} iterable
* @returns {Iterable<ExtendedFile|FileStream|Directory>}
* @typedef {Iterable<number>|Iterable<ArrayBuffer>|Iterable<ArrayBufferView>} IterableFileContent
* @typedef {Iterable<string>|Iterable<Blob>|Iterable<FileObject>} IterableFiles
*/
const normaliseIterableInput = function * (iterable) {
// In order to peek at first without loosing capablitiy to iterate, we
// create peekable which allows us to do that.
const peekable = Peekable.from(iterable)
// First try to interpret it a single file content chunks.
const bytes = asIterableBytes(peekable)
if (bytes != null) {
yield new ExtendedFile(bytes, '')
// If first item is a `Blob`, `string`, or a `FileObject` we treat this
// input as collection of files. We iterate and normalize each each value
// into a file.
} else {
for (const content of peekable) {
const file = asFile(content)
if (file) {
yield file
} else {
throw errCode(new Error('Unexpected input: ' + typeof content), 'ERR_UNEXPECTED_INPUT')
}
}
}
// Otherwise eslint complains about lack of return
return undefined
}
/**
* Utility function takes any input and returns a `File|FileStream|Directoriy`
* (containing that input) if input was one of the following types (or `null`
* otherwise):
* - `ArrayBuffer`
* - `ArrayBufferView`
* - `string`
* - `Blob`
* - `FileObject`
* It will return `File` instance when content is of known size (not a stream)
* other it returns a `FileStream`. If input is `FileObject` with no `content`
* returns `Directory`.
* @param {any} input
* @param {string} [name] - optional name for the file
* @returns {null|ExtendedFile|FileStream|Directory}
*/
const asFile = (input, name) => {
const file = asFileFromBlobPart(input, name)
if (file) {
return file
} else {
// If input is a `FileObject`
const fileObject = asFileObject(input)
if (fileObject) {
return fileFromFileObject(fileObject)
} else {
return null
}
}
}
/**
* Utility function takes any input and returns a `File` (containing it)
* if `input` is of `BlobPart` type, otherwise returns `null`. If optional
* `name` is passed it will be used as a file name.
* @param {any} content
* @param {string} [name]
* @param {Object} [options]
* @param {string} [options.path]
* @param {Mode} [options.mode]
* @param {MTime} [options.mtime]
* @returns {ExtendedFile|null}
*/
const asFileFromBlobPart = (content, name, options = {}) => {
if (
typeof content === 'string' ||
ArrayBuffer.isView(content) ||
content instanceof ArrayBuffer
) {
return new ExtendedFile([content], name || '', options)
} else if (content instanceof File) {
// Preserver file name if new name is not provided
return new ExtendedFile([content], name == null ? content.name : '', {
type: content.type,
...options
})
} else if (content instanceof Blob) {
// Preserve a mime type.
return new ExtendedFile([content], name || '', {
type: content.type,
...options
})
} else if (content instanceof String) {
return new ExtendedFile([content.toString()], name || '', options)
} else {
return null
}
}
/**
* Utility function takes a `FileObject` and returns a web `File` (with extended)
* attributes if content is of known size or a `FileStream` if content is an
* async stream or `Directory` if it has no content.
* @param {FileObject} fileObject
* @returns {null|ExtendedFile|FileStream|Directory}
*/
const fileFromFileObject = (fileObject) => {
const { path, mtime, mode, content, type } = fileObject
// `lastModified` is set to `undefined` as we do not want to preserve
// it in case `file.content` was instanceo of a `File`.
const ext = { mtime, mode, path, type, lastModified: undefined }
const name = path == null ? '' : basename(path)
const file = asFileFromBlobPart(content, name, ext)
if (file) {
return file
} else {
// If content is empty it is a diretory
if (content == null) {
return new Directory(name, ext)
}
// First try to interpret it a single file content chunks.
const iterable = asIterable(content)
if (iterable != null) {
const peekable = Peekable.from(iterable)
// File object content can only contain iterable of numbers or array
// buffers (or it's views). If so we create an object otherwise
// throw an exception.
const bytes = asIterableBytes(peekable)
if (bytes != null) {
return new ExtendedFile(bytes, name, ext)
} else {
throw errCode(new Error('Unexpected FileObject content: ' + typeof content), 'ERR_UNEXPECTED_INPUT')
}
}
// If we got here than we are dealing with async input, which can be either
// readable stream or an async iterable (casting former to later)
const stream = asReadableStream(content)
const asyncIterable = stream
? iterateReadableStream(stream)
: asAsyncIterable(content)
if (asyncIterable != null) {
return new FileStream(asyncIterable, name, ext)
}
throw errCode(new Error(`Unexpected FileObject content: ${content}`), 'ERR_UNEXPECTED_INPUT')
}
}
/**
* @param {Peekable<any>} content
* @returns {ArrayBufferView[]|ArrayBuffer[]|null}
*/
const asIterableBytes = (content) => {
const { done, value } = content.peek()
// If it is done input was empty collection so we return early.
if (done) {
return []
}
// If first item is an integer we treat input as a byte array and result
// will be collection of one file contaning those bytes.
if (Number.isInteger(value)) {
const bytes = new Uint8Array(content)
return [bytes]
// If first item is array buffer or it's view, it is interpreted as chunks
// of one file. In that case we collect all chunks and normalize input into
// collection with a single file containing those chunks.
// Note: Since this is a synchronous iterator all chunks are already in
// memory so by by collecting them into a single file we are not allocate
// new memory (unless iterator is generating content, but that is exotic
// enough use case that we prefer to go with File over FileStream).
} else if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
return [...content].map(normalizeArrayBufferView)
} else {
return null
}
}
/**
* @param {*} input
* @returns {Uint8Array}
*/
const normalizeArrayBufferView = (input) => {
if (input instanceof Uint8Array) {
return input
} else if (ArrayBuffer.isView(input)) {
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength)
} else if (input instanceof ArrayBuffer) {
return new Uint8Array(input)
} else {
throw errCode(new Error(`Unexpected input: ${input}`), 'ERR_UNEXPECTED_INPUT')
}
}
/**
* Pattern matches given `input` as `ReadableStream` and return back either
* matched input or `null`.
*
* @param {any} input
* @returns {ReadableStream<Uint8Array>|null}
*/
const asReadableStream = input => {
if (input && typeof input.getReader === 'function') {
return input
} else {
return null
}
}
/**
* Pattern matches given `input` as `AsyncIterable<I>` and returns back either
* matched `AsyncIterable` or `null`.
* @template I
* @param {AsyncIterable<I>|Input} input
* @returns {AsyncIterable<I>|null}
*/
const asAsyncIterable = input => {
/** @type {*} */
const object = input
if (object && typeof object[Symbol.asyncIterator] === 'function') {
return object
} else {
return null
}
}
/**
* Pattern matches given input as `Iterable<I>` and returns back either matched
* iterable or `null`.
* @template I
* @param {Iterable<I>|Input} input
* @returns {Iterable<I>|null}
*/
const asIterable = input => {
/** @type {*} */
const object = input
if (object && typeof object[Symbol.iterator] === 'function') {
return object
} else {
return null
}
}
/**
* Pattern matches given input as "FileObject" and returns back eithr matched
* input or `null`.
* @param {*} input
* @returns {FileObject|null}
*/
const asFileObject = input => {
if (typeof input === 'object' && input && (input.path || input.content)) {
return input
} else {
return null
}
}
/**
* @template T
* @param {ReadableStream<T>} stream
* @returns {AsyncIterable<T>}
*/
const iterateReadableStream = async function * (stream) {
const reader = stream.getReader()
while (true) {
const result = await reader.read()
if (result.done) {
return
}
yield result.value
}
}
/**
* @template T
*/
class Peekable {
/**
* @template T
* @template {Iterable<T>} I
* @param {I} iterable
* @returns {Peekable<T>}
*/
static from (iterable) {
return new Peekable(iterable)
}
/**
* @private
* @param {Iterable<T>} iterable
*/
constructor (iterable) {
const iterator = iterable[Symbol.iterator]()
/** @private */
this.first = iterator.next()
/** @private */
this.rest = iterator
}
peek () {
return this.first
}
next () {
const { first, rest } = this
this.first = rest.next()
return first
}
[Symbol.iterator] () {
return this
}
[Symbol.asyncIterator] () {
return this
}
}
/**
* @template T
*/
class AsyncPeekable {
/**
* @template T
* @template {AsyncIterable<T>} I
* @param {I} iterable
* @returns {AsyncPeekable<T>}
*/
static from (iterable) {
return new AsyncPeekable(iterable)
}
/**
* @private
* @param {AsyncIterable<T>} iterable
*/
constructor (iterable) {
const iterator = iterable[Symbol.asyncIterator]()
/** @private */
this.first = iterator.next()
/** @private */
this.rest = iterator
}
peek () {
return this.first
}
next () {
const { first, rest } = this
this.first = rest.next()
return first
}
[Symbol.asyncIterator] () {
return this
}
}
/**
* @param {string} path
* @returns {string}
*/
const basename = (path) =>
path.split(/\\|\//).pop()
class ExtendedFile extends File {
/**
* @param {BlobPart[]} init
* @param {string} name - A USVString representing the file name or the path
* to the file.
* @param {Object} [options]
* @param {string} [options.type] - A DOMString representing the MIME type
* of the content that will be put into the file. Defaults to a value of "".
* @param {number} [options.lastModified] - A number representing the number
* of milliseconds between the Unix time epoch and when the file was last
* modified. Defaults to a value of Date.now().
* @param {string} [options.path]
* @param {Mode} [options.mode]
* @param {MTime} [options.mtime]
*/
constructor (init, name, options = {}) {
super(init, name, options)
const { path, mode, mtime, lastModified } = options
this.path = path || name
this.mode = mode
// If `mtime` isn't provided but `lastModified` is, derive `mtime` from it.
// If neither is provided keep `mtime` undefined. This way if input was a
// File it's `lastModified` is used otherwise `mtime` is not set.
this.mtime = mtime || (lastModified && new Date(lastModified))
/** @type {'file'} */
this.kind = 'file'
}
/**
* @returns {AsyncIterable<Uint8Array>}
*/
get content () {
return readBlob(this)
}
}
// It appears that in electron native `File` has read-only `path` property,
// overriding it the property so that constructor can set a `path`.
Object.defineProperty(ExtendedFile.prototype, 'path', { writable: true })
module.exports.ExtendedFile = ExtendedFile
class FileStream {
/**
* @param {AsyncIterable<ArrayBuffer|ArrayBufferView>} source
* @param {string} name
* @param {Object} [options]
* @param {string} [options.type]
* @param {number} [options.lastModified]
* @param {string} [options.path]
* @param {MTime} [options.mtime]
* @param {Mode} [options.mode]
*/
constructor (source, name, options = {}) {
this.source = source
this.name = name
this.type = options.type || ''
this.lastModified = options.lastModified || Date.now()
this.path = options.path || ''
this.mtime = options.mtime || (options.lastModified && new Date(options.lastModified))
this.mode = options.mode
/** @type {'file-stream'} */
this.kind = 'file-stream'
}
get size () {
throw Error('File size is unknown')
}
async * [Symbol.asyncIterator] () {
for await (const chunk of this.source) {
if (ArrayBuffer.isView(chunk)) {
yield chunk
} else if (chunk instanceof ArrayBuffer) {
yield new Uint8Array(chunk)
} else {
throw errCode(new Error(`Unexpected file content: ${chunk}`), 'ERR_UNEXPECTED_INPUT')
}
}
}
get content () {
return this
}
}
module.exports.FileStream = FileStream
class Directory {
/**
* @param {string} name
* @param {Object} [options]
* @param {string} [options.type]
* @param {number} [options.lastModified]
* @param {string} [options.path]
* @param {MTime} [options.mtime]
* @param {Mode} [options.mode]
*/
constructor (name, options = {}) {
this.name = name
this.type = options.type || ''
this.lastModified = options.lastModified || Date.now()
this.path = options.path || ''
this.mtime = options.mtime
this.mode = options.mode
/** @type {'directory'} */
this.kind = 'directory'
/** @type {void} */
this.content = undefined
}
}
module.exports.Directory = Directory