UNPKG

@zhengxs/http

Version:

A lightweight cross-platform http request library

1 lines 7.01 kB
{"version":3,"file":"node.mjs","sources":["../../src/_polyfills/File.ts","../../src/_shims/node18-runtime.ts","../../src/shims/node.ts"],"sourcesContent":["export interface FilePropertyBag extends BlobPropertyBag {\n /**\n * 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.\n */\n lastModified?: number;\n}\n\n/**\n * The **File** interface provides information about files and allows JavaScript to access their content.\n */\nexport class File extends globalThis.Blob {\n static [Symbol.hasInstance](value: unknown): value is File {\n return (\n value instanceof Blob &&\n // @ts-expect-error\n value[Symbol.toStringTag] === 'File' &&\n typeof (value as File).name === 'string'\n );\n }\n\n /**\n * Returns the name of the file referenced by the File object.\n */\n readonly #name: string;\n\n /**\n * 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.\n */\n readonly #lastModified: number = 0;\n\n /**\n * Creates a new File instance.\n *\n * @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).\n * @param name - The name of the file.\n * @param options - An options object containing optional attributes for the file.\n */\n constructor(\n fileBits: BlobPart[],\n name: string,\n options: FilePropertyBag = {},\n ) {\n super(fileBits, options);\n\n if (arguments.length < 2) {\n throw new TypeError(\n \"Failed to construct 'File': 2 arguments required, \" +\n `but only ${arguments.length} present.`,\n );\n }\n\n this.#name = String(name);\n\n // Simulate WebIDL type casting for NaN value in lastModified option.\n const lastModified =\n options.lastModified === undefined\n ? Date.now()\n : Number(options.lastModified);\n\n if (!Number.isNaN(lastModified)) {\n this.#lastModified = lastModified;\n }\n }\n\n /**\n * Name of the file referenced by the File object.\n */\n get name(): string {\n return this.#name;\n }\n\n /* c8 ignore next 3 */\n get webkitRelativePath(): string {\n return '';\n }\n\n /**\n * 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.\n */\n get lastModified(): number {\n return this.#lastModified;\n }\n\n get [Symbol.toStringTag](): string {\n return 'File';\n }\n}\n","import { ReadStream as FsReadStream } from 'node:fs';\nimport { type Agent } from 'node:http';\nimport { Readable } from 'node:stream';\nimport { ReadableStream } from 'node:stream/web';\n\nimport KeepAliveAgent from 'agentkeepalive';\nimport { FormDataEncoder } from 'form-data-encoder';\n\nimport { File } from '../_polyfills/File';\nimport { type RequestOptions } from '../types';\nimport { MultipartBody } from './MultipartBody';\nimport { type Shims } from './registry';\n\nconst defaultHttpAgent: Agent = new KeepAliveAgent({\n keepAlive: true,\n timeout: 5 * 60 * 1000,\n});\nconst defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({\n keepAlive: true,\n timeout: 5 * 60 * 1000,\n});\n\nasync function getMultipartRequestOptions<\n T extends NonNullable<unknown> = Record<string, unknown>,\n>(\n form: globalThis.FormData,\n opts: RequestOptions<T>,\n): Promise<RequestOptions<T>> {\n const encoder = new FormDataEncoder(form);\n const readable = Readable.from(encoder);\n const body = new MultipartBody(readable);\n\n const headers = {\n ...opts.headers,\n ...encoder.headers,\n 'Content-Length': encoder.contentLength,\n };\n\n return { ...opts, body: body as any, headers };\n}\n\nexport function getRuntime(): Shims {\n return {\n kind: 'node',\n getMultipartRequestOptions,\n getDefaultAgent: (url: string): Agent =>\n url.startsWith('https') ? defaultHttpsAgent : defaultHttpAgent,\n isFsReadStream: (value: any): value is FsReadStream =>\n value instanceof FsReadStream,\n\n // Added in: v16.15.0\n fetch: globalThis.fetch,\n Request: globalThis.Request,\n Response: globalThis.Response,\n Headers: globalThis.Headers,\n FormData: globalThis.FormData,\n\n // Added in: v18.0.0\n Blob: globalThis.Blob,\n ReadableStream,\n\n // Added in: v20.0.0\n File: globalThis.File || File,\n };\n}\n","import { getRuntime } from '../_shims/node18-runtime';\nimport { setShims } from '../_shims/registry';\n\nsetShims(getRuntime());\n"],"names":["File","value","#name","#lastModified","fileBits","name","options","lastModified","defaultHttpAgent","KeepAliveAgent","defaultHttpsAgent","getMultipartRequestOptions","form","opts","encoder","FormDataEncoder","readable","Readable","body","MultipartBody","headers","getRuntime","url","FsReadStream","ReadableStream","setShims"],"mappings":";;;;;;;;AAUa,MAAAA,UAAa,WAAW,KAAK;AAAA,EACxC,QAAQ,OAAO,WAAW,EAAEC,GAA+B;AACzD,WACEA,aAAiB;AAAA,IAEjBA,EAAM,OAAO,WAAW,MAAM,UAC9B,OAAQA,EAAe,QAAS;AAAA,EAEpC;AAAA;AAAA;AAAA;AAAA,EAKSC;AAAA;AAAA;AAAA;AAAA,EAKAC,KAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASjC,YACEC,GACAC,GACAC,IAA2B,CAAA,GAC3B;AAGI,QAFJ,MAAMF,GAAUE,CAAO,GAEnB,UAAU,SAAS;AACrB,YAAM,IAAI;AAAA,QACR,8DACc,UAAU,MAAM;AAAA,MAAA;AAI7B,SAAAJ,KAAQ,OAAOG,CAAI;AAGlB,UAAAE,IACJD,EAAQ,iBAAiB,SACrB,KAAK,QACL,OAAOA,EAAQ,YAAY;AAEjC,IAAK,OAAO,MAAMC,CAAY,MAC5B,KAAKJ,KAAgBI;AAAA,EAEzB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,OAAe;AACjB,WAAO,KAAKL;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,qBAA6B;AACxB,WAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,eAAuB;AACzB,WAAO,KAAKC;AAAA,EACd;AAAA,EAEA,KAAK,OAAO,WAAW,IAAY;AAC1B,WAAA;AAAA,EACT;AACF;ACzEA,MAAMK,IAA0B,IAAIC,EAAe;AAAA,EACj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