1'use strict'; 2 3const { 4 DateNow, 5 NumberIsNaN, 6 ObjectDefineProperties, 7 SymbolToStringTag, 8} = primordials; 9 10const { 11 Blob, 12} = require('internal/blob'); 13 14const { 15 customInspectSymbol: kInspect, 16 emitExperimentalWarning, 17 kEnumerableProperty, 18 kEmptyObject, 19 toUSVString, 20} = require('internal/util'); 21 22const { 23 codes: { 24 ERR_MISSING_ARGS, 25 }, 26} = require('internal/errors'); 27 28const { 29 inspect, 30} = require('internal/util/inspect'); 31 32class File extends Blob { 33 /** @type {string} */ 34 #name; 35 36 /** @type {number} */ 37 #lastModified; 38 39 constructor(fileBits, fileName, options = kEmptyObject) { 40 emitExperimentalWarning('buffer.File'); 41 42 if (arguments.length < 2) { 43 throw new ERR_MISSING_ARGS('fileBits', 'fileName'); 44 } 45 46 super(fileBits, options); 47 48 let { lastModified } = options ?? kEmptyObject; 49 50 if (lastModified !== undefined) { 51 // Using Number(...) will not throw an error for bigints. 52 lastModified = +lastModified; 53 54 if (NumberIsNaN(lastModified)) { 55 lastModified = 0; 56 } 57 } else { 58 lastModified = DateNow(); 59 } 60 61 this.#name = toUSVString(fileName); 62 this.#lastModified = lastModified; 63 } 64 65 get name() { 66 return this.#name; 67 } 68 69 get lastModified() { 70 return this.#lastModified; 71 } 72 73 [kInspect](depth, options) { 74 if (depth < 0) { 75 return this; 76 } 77 78 const opts = { 79 ...options, 80 depth: options.depth == null ? null : options.depth - 1, 81 }; 82 83 return `File ${inspect({ 84 size: this.size, 85 type: this.type, 86 name: this.#name, 87 lastModified: this.#lastModified, 88 }, opts)}`; 89 } 90} 91 92ObjectDefineProperties(File.prototype, { 93 name: kEnumerableProperty, 94 lastModified: kEnumerableProperty, 95 [SymbolToStringTag]: { 96 __proto__: null, 97 configurable: true, 98 value: 'File', 99 }, 100}); 101 102module.exports = { 103 File, 104}; 105