UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

86 lines (68 loc) 1.84 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/file.js import { Blob } from "nstdlib/lib/internal/blob"; import { customInspectSymbol as kInspect, kEnumerableProperty, kEmptyObject, } from "nstdlib/lib/internal/util"; import { codes as __codes__ } from "nstdlib/lib/internal/errors"; import { inspect } from "nstdlib/lib/internal/util/inspect"; const { ERR_MISSING_ARGS } = __codes__; class File extends Blob { /** @type {string} */ #name; /** @type {number} */ #lastModified; constructor(fileBits, fileName, options = kEmptyObject) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("fileBits", "fileName"); } super(fileBits, options); let { lastModified } = options ?? kEmptyObject; if (lastModified !== undefined) { // Using Number(...) will not throw an error for bigints. lastModified = +lastModified; if (Number.isNaN(lastModified)) { lastModified = 0; } } else { lastModified = Date.now(); } this.#name = String.prototype.toWellFormed.call(`${fileName}`); this.#lastModified = lastModified; } get name() { return this.#name; } get lastModified() { return this.#lastModified; } [kInspect](depth, options) { if (depth < 0) { return this; } const opts = { ...options, depth: options.depth == null ? null : options.depth - 1, }; return `File ${inspect( { size: this.size, type: this.type, name: this.#name, lastModified: this.#lastModified, }, opts, )}`; } } Object.defineProperties(File.prototype, { name: kEnumerableProperty, lastModified: kEnumerableProperty, [Symbol.toStringTag]: { __proto__: null, configurable: true, value: "File", }, }); export { File };