| 'use strict' |
|
|
| const { Blob, File: NativeFile } = require('buffer') |
| const { types } = require('util') |
| const { kState } = require('./symbols') |
| const { isBlobLike } = require('./util') |
| const { webidl } = require('./webidl') |
| const { parseMIMEType, serializeAMimeType } = require('./dataURL') |
| const { kEnumerableProperty } = require('../core/util') |
| const encoder = new TextEncoder() |
|
|
| class File extends Blob { |
| constructor (fileBits, fileName, options = {}) { |
| |
| |
| |
| webidl.argumentLengthCheck(arguments, 2, { header: 'File constructor' }) |
|
|
| fileBits = webidl.converters['sequence<BlobPart>'](fileBits) |
| fileName = webidl.converters.USVString(fileName) |
| options = webidl.converters.FilePropertyBag(options) |
|
|
| |
| |
| |
|
|
| |
| const n = fileName |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| let t = options.type |
| let d |
|
|
| |
| substep: { |
| if (t) { |
| t = parseMIMEType(t) |
|
|
| if (t === 'failure') { |
| t = '' |
| |
| break substep |
| } |
|
|
| t = serializeAMimeType(t).toLowerCase() |
| } |
|
|
| |
| |
| |
| |
| d = options.lastModified |
| } |
|
|
| |
| |
| |
| |
| |
| |
|
|
| super(processBlobParts(fileBits, options), { type: t }) |
| this[kState] = { |
| name: n, |
| lastModified: d, |
| type: t |
| } |
| } |
|
|
| get name () { |
| webidl.brandCheck(this, File) |
|
|
| return this[kState].name |
| } |
|
|
| get lastModified () { |
| webidl.brandCheck(this, File) |
|
|
| return this[kState].lastModified |
| } |
|
|
| get type () { |
| webidl.brandCheck(this, File) |
|
|
| return this[kState].type |
| } |
| } |
|
|
| class FileLike { |
| constructor (blobLike, fileName, options = {}) { |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| |
| const n = fileName |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| const t = options.type |
|
|
| |
| |
|
|
| |
| |
| |
| |
| const d = options.lastModified ?? Date.now() |
|
|
| |
| |
| |
| |
| |
| |
|
|
| this[kState] = { |
| blobLike, |
| name: n, |
| type: t, |
| lastModified: d |
| } |
| } |
|
|
| stream (...args) { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].blobLike.stream(...args) |
| } |
|
|
| arrayBuffer (...args) { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].blobLike.arrayBuffer(...args) |
| } |
|
|
| slice (...args) { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].blobLike.slice(...args) |
| } |
|
|
| text (...args) { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].blobLike.text(...args) |
| } |
|
|
| get size () { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].blobLike.size |
| } |
|
|
| get type () { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].blobLike.type |
| } |
|
|
| get name () { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].name |
| } |
|
|
| get lastModified () { |
| webidl.brandCheck(this, FileLike) |
|
|
| return this[kState].lastModified |
| } |
|
|
| get [Symbol.toStringTag] () { |
| return 'File' |
| } |
| } |
|
|
| Object.defineProperties(File.prototype, { |
| [Symbol.toStringTag]: { |
| value: 'File', |
| configurable: true |
| }, |
| name: kEnumerableProperty, |
| lastModified: kEnumerableProperty |
| }) |
|
|
| webidl.converters.Blob = webidl.interfaceConverter(Blob) |
|
|
| webidl.converters.BlobPart = function (V, opts) { |
| if (webidl.util.Type(V) === 'Object') { |
| if (isBlobLike(V)) { |
| return webidl.converters.Blob(V, { strict: false }) |
| } |
|
|
| if ( |
| ArrayBuffer.isView(V) || |
| types.isAnyArrayBuffer(V) |
| ) { |
| return webidl.converters.BufferSource(V, opts) |
| } |
| } |
|
|
| return webidl.converters.USVString(V, opts) |
| } |
|
|
| webidl.converters['sequence<BlobPart>'] = webidl.sequenceConverter( |
| webidl.converters.BlobPart |
| ) |
|
|
| |
| webidl.converters.FilePropertyBag = webidl.dictionaryConverter([ |
| { |
| key: 'lastModified', |
| converter: webidl.converters['long long'], |
| get defaultValue () { |
| return Date.now() |
| } |
| }, |
| { |
| key: 'type', |
| converter: webidl.converters.DOMString, |
| defaultValue: '' |
| }, |
| { |
| key: 'endings', |
| converter: (value) => { |
| value = webidl.converters.DOMString(value) |
| value = value.toLowerCase() |
|
|
| if (value !== 'native') { |
| value = 'transparent' |
| } |
|
|
| return value |
| }, |
| defaultValue: 'transparent' |
| } |
| ]) |
|
|
| |
| |
| |
| |
| |
| function processBlobParts (parts, options) { |
| |
| |
| const bytes = [] |
|
|
| |
| for (const element of parts) { |
| |
| if (typeof element === 'string') { |
| |
| let s = element |
|
|
| |
| |
| |
| if (options.endings === 'native') { |
| s = convertLineEndingsNative(s) |
| } |
|
|
| |
| bytes.push(encoder.encode(s)) |
| } else if ( |
| types.isAnyArrayBuffer(element) || |
| types.isTypedArray(element) |
| ) { |
| |
| |
| |
| if (!element.buffer) { |
| bytes.push(new Uint8Array(element)) |
| } else { |
| bytes.push( |
| new Uint8Array(element.buffer, element.byteOffset, element.byteLength) |
| ) |
| } |
| } else if (isBlobLike(element)) { |
| |
| |
| bytes.push(element) |
| } |
| } |
|
|
| |
| return bytes |
| } |
|
|
| |
| |
| |
| |
| function convertLineEndingsNative (s) { |
| |
| let nativeLineEnding = '\n' |
|
|
| |
| |
| |
| |
| if (process.platform === 'win32') { |
| nativeLineEnding = '\r\n' |
| } |
|
|
| return s.replace(/\r?\n/g, nativeLineEnding) |
| } |
|
|
| |
| |
| |
| function isFileLike (object) { |
| return ( |
| (NativeFile && object instanceof NativeFile) || |
| object instanceof File || ( |
| object && |
| (typeof object.stream === 'function' || |
| typeof object.arrayBuffer === 'function') && |
| object[Symbol.toStringTag] === 'File' |
| ) |
| ) |
| } |
|
|
| module.exports = { File, FileLike, isFileLike } |
|
|