UNPKG

@zhengxs/http

Version:

A lightweight cross-platform http request library

1 lines 6.7 kB
{"version":3,"file":"node.cjs","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":"mPAUa,MAAAA,UAAa,WAAW,IAAK,CACxC,OAAQ,OAAO,WAAW,EAAEC,EAA+B,CACzD,OACEA,aAAiB,MAEjBA,EAAM,OAAO,WAAW,IAAM,QAC9B,OAAQA,EAAe,MAAS,QAEpC,CAKSC,GAKAC,GAAwB,EASjC,YACEC,EACAC,EACAC,EAA2B,CAAA,EAC3B,CAGI,GAFJ,MAAMF,EAAUE,CAAO,EAEnB,UAAU,OAAS,EACrB,MAAM,IAAI,UACR,8DACc,UAAU,MAAM,WAAA,EAI7B,KAAAJ,GAAQ,OAAOG,CAAI,EAGlB,MAAAE,EACJD,EAAQ,eAAiB,OACrB,KAAK,MACL,OAAOA,EAAQ,YAAY,EAE5B,OAAO,MAAMC,CAAY,IAC5B,KAAKJ,GAAgBI,EAEzB,CAKA,IAAI,MAAe,CACjB,OAAO,KAAKL,EACd,CAGA,IAAI,oBAA6B,CACxB,MAAA,EACT,CAKA,IAAI,cAAuB,CACzB,OAAO,KAAKC,EACd,CAEA,IAAK,OAAO,WAAW,GAAY,CAC1B,MAAA,MACT,CACF,CCzEA,MAAMK,EAA0B,IAAIC,EAAe,CACjD,UAAW,GACX,QAAS,EAAI,GAAK,GACpB,CAAC,EACKC,EAA2B,IAAID,EAAe,WAAW,CAC7D,UAAW,GACX,QAAS,EAAI,GAAK,GACpB,CAAC,EAED,eAAeE,EAGbC,EACAC,EAC4B,CACtB,MAAAC,EAAU,IAAIC,kBAAgBH,CAAI,EAClCI,EAAWC,EAAAA,SAAS,KAAKH,CAAO,EAChC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