UNPKG

@zhengxs/http

Version:

A lightweight cross-platform http request library

94 lines (93 loc) 3.17 kB
import { ReadStream as a } from "node:fs"; import "node:http"; import { Readable as l } from "node:stream"; import { ReadableStream as m } from "node:stream/web"; import i from "agentkeepalive"; import { FormDataEncoder as g } from "form-data-encoder"; import { M as d } from "../MultipartBody.mjs"; import { s as u } from "../registry.mjs"; class h extends globalThis.Blob { static [Symbol.hasInstance](e) { return e instanceof Blob && // @ts-expect-error e[Symbol.toStringTag] === "File" && typeof e.name == "string"; } /** * Returns the name of the file referenced by the File object. */ #e; /** * The last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). Files without a known last modified date return the current date. */ #t = 0; /** * Creates a new File instance. * * @param fileBits - An `Array` strings, or [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), [`ArrayBufferView`](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView), [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects, or a mix of any of such objects, that will be put inside the [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File). * @param name - The name of the file. * @param options - An options object containing optional attributes for the file. */ constructor(e, o, r = {}) { if (super(e, r), arguments.length < 2) throw new TypeError( `Failed to construct 'File': 2 arguments required, but only ${arguments.length} present.` ); this.#e = String(o); const n = r.lastModified === void 0 ? Date.now() : Number(r.lastModified); Number.isNaN(n) || (this.#t = n); } /** * Name of the file referenced by the File object. */ get name() { return this.#e; } /* c8 ignore next 3 */ get webkitRelativePath() { return ""; } /** * The last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). Files without a known last modified date return the current date. */ get lastModified() { return this.#t; } get [Symbol.toStringTag]() { return "File"; } } const f = new i({ keepAlive: !0, timeout: 5 * 60 * 1e3 }), c = new i.HttpsAgent({ keepAlive: !0, timeout: 5 * 60 * 1e3 }); async function b(t, e) { const o = new g(t), r = l.from(o), n = new d(r), s = { ...e.headers, ...o.headers, "Content-Length": o.contentLength }; return { ...e, body: n, headers: s }; } function p() { return { kind: "node", getMultipartRequestOptions: b, getDefaultAgent: (t) => t.startsWith("https") ? c : f, isFsReadStream: (t) => t instanceof a, // Added in: v16.15.0 fetch: globalThis.fetch, Request: globalThis.Request, Response: globalThis.Response, Headers: globalThis.Headers, FormData: globalThis.FormData, // Added in: v18.0.0 Blob: globalThis.Blob, ReadableStream: m, // Added in: v20.0.0 File: globalThis.File || h }; } u(p()); //# sourceMappingURL=node.mjs.map