@wener/utils 1.1.24 → 1.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +9 -9
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/esm/index.js +11 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/system/index.js +9 -9
- package/dist/system/index.js.map +1 -1
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/lib/crypto/hashing.js +1 -1
- package/lib/crypto/hashing.js.map +1 -1
- package/lib/errors/Errors.js +26 -0
- package/lib/errors/Errors.js.map +1 -1
- package/lib/fetch/createFetchWith.js.map +1 -1
- package/lib/{servers/fetch → fetch}/createFetchWithRetry.js +1 -1
- package/lib/fetch/createFetchWithRetry.js.map +1 -0
- package/lib/fetch/dumpRequest.js +3 -1
- package/lib/fetch/dumpRequest.js.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/server.js +0 -1
- package/lib/server.js.map +1 -1
- package/package.json +1 -1
- package/src/crypto/hashing.ts +2 -1
- package/src/errors/Errors.ts +34 -4
- package/src/fetch/createFetchWith.ts +1 -1
- package/src/{servers/fetch → fetch}/createFetchWithRetry.ts +3 -3
- package/src/fetch/dumpRequest.ts +5 -1
- package/src/fetch/index.ts +1 -0
- package/src/index.ts +9 -1
- package/src/server.ts +0 -1
- package/src/typedoc.ts +2 -1
- package/lib/servers/fetch/createFetchWithRetry.js.map +0 -1
package/dist/system/server.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
System.register(["./getGlobalThis-8951eb0e.js","node:crypto","node:fs","node:path","node:url"],function(
|
|
1
|
+
System.register(["./getGlobalThis-8951eb0e.js","node:crypto","node:fs","node:path","node:url"],function(f,o){"use strict";var y,l,h,i,d;return{setters:[function(e){y=e.g},function(e){l=e.createHash},function(e){h=e.default},function(e){i=e.default},function(e){d=e.fileURLToPath}],execute:function(){f({createFetchWithProxyByUndici:p,getPackageDir:k,md5:w,polyfillCrypto:e});async function e(){const t=y();return"crypto"in t?!1:(t.crypto=(await o.import("node:crypto")).webcrypto,!0)}function p({proxy:t,token:S,fetch:c,undici:g}={}){if(!t)return c||globalThis.fetch;let a;return async(...m)=>{const s=m[1]||={};{const r=s.body;typeof r=="object"&&r&&(r instanceof ReadableStream||Symbol.asyncIterator in r)&&(s.duplex||="half")}if(!a){let r=t,u=S;{let n;try{n=new URL(t)}catch{}!u&&n&&(n.username||n.password)&&(u=`Basic ${btoa(`${n.username||""}:${n.password}`)}`,n.username="",n.password="",r=n.toString())}g||=o.import("undici");const b=await g,T=b.ProxyAgent;c||=b.fetch,a=new T({uri:r,token:u})}return s.dispatcher=a,await c(...m)}}const U=f("createFetchWithProxy",p);function w(t){return l("md5").update(t).digest("hex")}const x=d(o.meta.url),P=i.dirname(x);function k(t=P){for(;!h.existsSync(i.join(t,"package.json"));)if(t=i.resolve(t,".."),t==="/")return;return t}}}});
|
|
2
2
|
//# sourceMappingURL=server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sources":["../../src/servers/polyfill/polyfillCrypto.ts","../../src/servers/fetch/createFetchWithProxyByUndici.ts","../../src/servers/fetch/createFetchWithProxy.ts","../../src/servers/fetch/createFetchWithRetry.ts","../../src/servers/crypto/md5.ts","../../src/servers/getPackageDir.ts"],"sourcesContent":["import { getGlobalThis } from '../../isomorphics/getGlobalThis';\n\nexport async function polyfillCrypto() {\n const globalThis = getGlobalThis();\n if ('crypto' in globalThis) {\n return false;\n }\n (globalThis as any).crypto = (await import('node:crypto')).webcrypto as Crypto;\n return true;\n}\n","import { MaybePromise } from '../../asyncs/MaybePromise';\nimport { FetchLike } from '../../fetch';\n\nexport function createFetchWithProxyByUndici({\n proxy,\n token: _token,\n fetch,\n undici,\n}: {\n proxy?: string;\n token?: string;\n fetch?: FetchLike;\n undici?: MaybePromise<{ fetch: any; ProxyAgent: any }>;\n} = {}): FetchLike {\n if (!proxy) {\n return fetch || globalThis.fetch;\n }\n let agent: any;\n // https://github.com/nodejs/undici/blob/main/docs/best-practices/proxy.md\n return async (...args) => {\n const init = (args[1] ||= {}) as RequestInit & {\n duplex?: string;\n dispatcher?: any;\n };\n {\n const body = init.body;\n if (typeof body === 'object' && body && (body instanceof ReadableStream || Symbol.asyncIterator in body)) {\n // request.duplex must be set if request.body is ReadableStream or Async Iterables\n init.duplex ||= 'half';\n }\n }\n if (!agent) {\n let uri = proxy;\n let token = _token;\n {\n let u: URL | undefined;\n try {\n u = new URL(proxy);\n } catch (e) {}\n if (!token && u && (u.username || u.password)) {\n token = `Basic ${btoa(`${u.username || ''}:${u.password}`)}`;\n u.username = '';\n u.password = '';\n uri = u.toString();\n }\n }\n // if in next use 'next/dist/compiled/undici'\n undici ||= import('undici');\n const mod = await undici;\n const ProxyAgent = mod.ProxyAgent as new (_: any) => any;\n fetch ||= mod.fetch as FetchLike;\n // https://github.com/nodejs/undici/blob/main/docs/api/ProxyAgent.md\n agent = new ProxyAgent({\n uri,\n token,\n });\n // https://github.com/nodejs/node/issues/43187#issuecomment-1134634174\n // (global as any)[Symbol.for('undici.globalDispatcher.1')] = agent;\n // fixme should unwrap error https://github.com/nodejs/undici/issues/1248\n }\n init.dispatcher = agent;\n return await fetch!(...args);\n };\n}\n","import { createFetchWithProxyByUndici } from './createFetchWithProxyByUndici';\n\nexport const createFetchWithProxy = createFetchWithProxyByUndici;\n","import { FetchLike } from '../../fetch';\nimport { getGlobalThis } from '../../isomorphics/getGlobalThis';\n\ntype RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;\ntype RequestRetryOnFunction = (\n attempt: number,\n error: Error | null,\n response: Response | null,\n) => boolean | Promise<boolean>;\n\nexport interface FetchWithRetryOptions {\n fetch?: FetchLike;\n retries?: number;\n retryDelay?: number | RequestDelayFunction;\n retryOn?: number[] | RequestRetryOnFunction;\n}\n\nexport function createFetchWithRetry({\n fetch = getGlobalThis().fetch,\n retries = 3,\n retryDelay = 1000,\n retryOn = [],\n}: FetchWithRetryOptions = {}): FetchLike {\n // https://github.com/jonbern/fetch-retry/blob/master/index.js\n\n return function fetchRetry(input: string | Request, init?: RequestInit) {\n return new Promise(function (resolve, reject) {\n var wrappedFetch = function (attempt: number) {\n // As of node 18, this is no longer needed since node comes with native support for fetch:\n /* istanbul ignore next */\n var _input = typeof Request !== 'undefined' && input instanceof Request ? input.clone() : input;\n fetch(_input, init)\n .then(function (response) {\n if (Array.isArray(retryOn) && retryOn.indexOf(response.status) === -1) {\n resolve(response);\n } else if (typeof retryOn === 'function') {\n try {\n return Promise.resolve(retryOn(attempt, null, response))\n .then(function (retryOnResponse) {\n if (retryOnResponse) {\n retry(attempt, null, response);\n } else {\n resolve(response);\n }\n })\n .catch(reject);\n } catch (error) {\n reject(error);\n }\n } else {\n if (attempt < retries) {\n retry(attempt, null, response);\n } else {\n resolve(response);\n }\n }\n return;\n })\n .catch(function (error) {\n if (typeof retryOn === 'function') {\n try {\n // eslint-disable-next-line no-undef\n Promise.resolve(retryOn(attempt, error, null))\n .then(function (retryOnResponse) {\n if (retryOnResponse) {\n retry(attempt, error, null);\n } else {\n reject(error);\n }\n })\n .catch(function (error) {\n reject(error);\n });\n } catch (error) {\n reject(error);\n }\n } else if (attempt < retries) {\n retry(attempt, error, null);\n } else {\n reject(error);\n }\n });\n };\n\n function retry(attempt: number, error: any, response: Response | null) {\n let delay = typeof retryDelay === 'function' ? retryDelay(attempt, error, response) : retryDelay;\n setTimeout(function () {\n wrappedFetch(++attempt);\n }, delay);\n }\n\n wrappedFetch(0);\n });\n };\n}\n","import { createHash } from 'node:crypto';\n\nexport function md5(input: Buffer | string) {\n return createHash('md5').update(input).digest('hex') as string;\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nexport function getPackageDir(currentDir: string = __dirname) {\n while (!fs.existsSync(path.join(currentDir, 'package.json'))) {\n currentDir = path.resolve(currentDir, '..');\n if (currentDir === '/') {\n return undefined;\n }\n }\n return currentDir;\n}\n"],"names":["polyfillCrypto","globalThis","getGlobalThis","module","createFetchWithProxyByUndici","proxy","_token","fetch","undici","agent","args","init","body","uri","token","u","mod","ProxyAgent","createFetchWithProxy","createFetchWithRetry","retries","retryDelay","retryOn","input","resolve","reject","wrappedFetch","attempt","_input","response","retryOnResponse","retry","error","delay","md5","createHash","__filename","fileURLToPath","__dirname","path","getPackageDir","currentDir","fs"],"mappings":"6YAEA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAsBA,CAAiB,CAAA,CAAA,CACrC,CAAMC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAaC,CAAc,CAAA,CAAA,CACjC,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAYD,CACP,CAAA,CAAA,CAAA,CAAA,CAERA,CAAmB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAU,CAAaE,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAa,GAAG,CACpD,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CACT,CCNO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAASC,CAA6B,CAAA,CAC3C,CAAAC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,C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AAAA,CAAAA,CAAAA,CAAAA,CAAaC,CAAclC,CAAAA,CAAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAe,CAC1CmC,CAAAA,CAAAA,CAAYC,EAAK,CAAQH,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAU,CAElC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAASI,CAAcC,CAAAA,CAAAA,CAAqBH,CAAW,CAAA,CAC5D,CAAO,CAAA,CAAA,CAAA,CAAA,CAACI,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAWH,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAKE,CAAY,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAc,CAAC,CAAA,CAAA,CAEzD,CADAA,CAAAA,CAAAA,CAAAA,CAAaF,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQE,CAAY,CAAA,CAAA,CAAA,CAAA,CAAI,CACtCA,CAAAA,CAAAA,CAAAA,CAAAA,CAAe,CACjB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAGJ,CAAOA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CACT;"}
|
|
1
|
+
{"version":3,"file":"server.js","sources":["../../src/servers/polyfill/polyfillCrypto.ts","../../src/servers/fetch/createFetchWithProxyByUndici.ts","../../src/servers/fetch/createFetchWithProxy.ts","../../src/servers/crypto/md5.ts","../../src/servers/getPackageDir.ts"],"sourcesContent":["import { getGlobalThis } from '../../isomorphics/getGlobalThis';\n\nexport async function polyfillCrypto() {\n const globalThis = getGlobalThis();\n if ('crypto' in globalThis) {\n return false;\n }\n (globalThis as any).crypto = (await import('node:crypto')).webcrypto as Crypto;\n return true;\n}\n","import { MaybePromise } from '../../asyncs/MaybePromise';\nimport { FetchLike } from '../../fetch';\n\nexport function createFetchWithProxyByUndici({\n proxy,\n token: _token,\n fetch,\n undici,\n}: {\n proxy?: string;\n token?: string;\n fetch?: FetchLike;\n undici?: MaybePromise<{ fetch: any; ProxyAgent: any }>;\n} = {}): FetchLike {\n if (!proxy) {\n return fetch || globalThis.fetch;\n }\n let agent: any;\n // https://github.com/nodejs/undici/blob/main/docs/best-practices/proxy.md\n return async (...args) => {\n const init = (args[1] ||= {}) as RequestInit & {\n duplex?: string;\n dispatcher?: any;\n };\n {\n const body = init.body;\n if (typeof body === 'object' && body && (body instanceof ReadableStream || Symbol.asyncIterator in body)) {\n // request.duplex must be set if request.body is ReadableStream or Async Iterables\n init.duplex ||= 'half';\n }\n }\n if (!agent) {\n let uri = proxy;\n let token = _token;\n {\n let u: URL | undefined;\n try {\n u = new URL(proxy);\n } catch (e) {}\n if (!token && u && (u.username || u.password)) {\n token = `Basic ${btoa(`${u.username || ''}:${u.password}`)}`;\n u.username = '';\n u.password = '';\n uri = u.toString();\n }\n }\n // if in next use 'next/dist/compiled/undici'\n undici ||= import('undici');\n const mod = await undici;\n const ProxyAgent = mod.ProxyAgent as new (_: any) => any;\n fetch ||= mod.fetch as FetchLike;\n // https://github.com/nodejs/undici/blob/main/docs/api/ProxyAgent.md\n agent = new ProxyAgent({\n uri,\n token,\n });\n // https://github.com/nodejs/node/issues/43187#issuecomment-1134634174\n // (global as any)[Symbol.for('undici.globalDispatcher.1')] = agent;\n // fixme should unwrap error https://github.com/nodejs/undici/issues/1248\n }\n init.dispatcher = agent;\n return await fetch!(...args);\n };\n}\n","import { createFetchWithProxyByUndici } from './createFetchWithProxyByUndici';\n\nexport const createFetchWithProxy = createFetchWithProxyByUndici;\n","import { createHash } from 'node:crypto';\n\nexport function md5(input: Buffer | string) {\n return createHash('md5').update(input).digest('hex') as string;\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nexport function getPackageDir(currentDir: string = __dirname) {\n while (!fs.existsSync(path.join(currentDir, 'package.json'))) {\n currentDir = path.resolve(currentDir, '..');\n if (currentDir === '/') {\n return undefined;\n }\n }\n return currentDir;\n}\n"],"names":["polyfillCrypto","globalThis","getGlobalThis","module","createFetchWithProxyByUndici","proxy","_token","fetch","undici","agent","args","init","body","uri","token","u","mod","ProxyAgent","createFetchWithProxy","md5","input","createHash","__filename","fileURLToPath","__dirname","path","getPackageDir","currentDir","fs"],"mappings":"uXAEA,CAAsBA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAiB,CACrC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAMC,CAAaC,CAAAA,CAAAA,CAAc,EACjC,CAAI,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAYD,CACP,CAAA,CAAA,CAAA,CAAA,CAERA,EAAmB,CAAU,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAaE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,aAAa,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CACpD,CACT,CAAA,CAAA,CCNO,CAASC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAA6B,CAC3C,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAC,EACA,CAAOC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CACP,CAAAC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CACA,OAAA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,EAElC,CAASI,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAcC,CAAqBH,CAAAA,CAAAA,CAAW,CAC5D,CAAO,CAAA,CAAA,CAAA,CAAA,CAACI,CAAG,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAWH,EAAK,CAAKE,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAY,CAAc,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAC,GAEzD,CADAA,CAAAA,CAAAA,CAAAA,CAAaF,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQE,EAAY,CAAI,CAAA,CAAA,CAAA,CAAA,CACtCA,CAAe,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CACjB,OAGJ,CAAOA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CACT;"}
|
package/lib/crypto/hashing.js
CHANGED
|
@@ -14,7 +14,7 @@ function sha512(s, o) {
|
|
|
14
14
|
}
|
|
15
15
|
function digestOf(a, s, o) {
|
|
16
16
|
let buffer = crypto.subtle.digest(a, binaryOf(s));
|
|
17
|
-
return o ? buffer.then((v) => encode(v, o)) :
|
|
17
|
+
return o ? buffer.then((v) => encode(v, o)) : buffer;
|
|
18
18
|
}
|
|
19
19
|
function binaryOf(s) {
|
|
20
20
|
if (typeof s === "string") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hashing.js","sources":["../../src/crypto/hashing.ts"],"sourcesContent":["import { ArrayBuffers } from '../io/ArrayBuffers';\n\nexport function sha1(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha1(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha1(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-1', s, o);\n}\n\nexport function sha256(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha256(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha256(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-256', s, o);\n}\n\nexport function sha384(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha384(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha384(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-384', s, o);\n}\n\nexport function sha512(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha512(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha512(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-512', s, o);\n}\n\nfunction digestOf(a: string, s: BinaryLike, o?: DigestOptions) {\n let buffer = crypto.subtle.digest(a, binaryOf(s));\n return o ? buffer.then((v) => encode(v, o)) :
|
|
1
|
+
{"version":3,"file":"hashing.js","sources":["../../src/crypto/hashing.ts"],"sourcesContent":["import { ArrayBuffers } from '../io/ArrayBuffers';\n\nexport function sha1(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha1(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha1(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-1', s, o);\n}\n\nexport function sha256(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha256(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha256(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-256', s, o);\n}\n\nexport function sha384(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha384(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha384(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-384', s, o);\n}\n\nexport function sha512(s: BinaryLike, o?: undefined): Promise<Buffer>;\nexport function sha512(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;\n\nexport function sha512(s: BinaryLike, o?: DigestOptions) {\n return digestOf('SHA-512', s, o);\n}\n\nfunction digestOf(a: string, s: BinaryLike, o?: DigestOptions) {\n let buffer = crypto.subtle.digest(a, binaryOf(s));\n return o ? buffer.then((v) => encode(v, o)) : buffer;\n}\n\nexport type DigestOptions =\n | 'hex'\n | 'base64'\n | {\n encoding: 'hex' | 'base64';\n };\n\ntype BinaryLike = string | BufferSource;\n\nfunction binaryOf(s: BinaryLike) {\n // ArrayBuffer, TypedArray, DataView\n if (typeof s === 'string') {\n return new TextEncoder().encode(s);\n }\n // ArrayBuffer, Buffer, TypedArray, DataView\n return s;\n}\n\nfunction encode(buf: BufferSource, o?: DigestOptions) {\n if (o) {\n switch (o) {\n case 'hex':\n case 'base64':\n return ArrayBuffers.toString(buf, o);\n }\n }\n return buf;\n}\n\ntype StringEncoding = 'hex' | 'base64';\ntype IsStringCoding<T> = T extends StringEncoding ? true : T extends { encoding: StringEncoding } ? true : false;\n\nexport function hmac<O extends DigestOptions>(\n hash: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512' | 'sha1' | 'sha256' | 'sha384' | 'sha512',\n key: BinaryLike | CryptoKey,\n data: BinaryLike,\n o?: O,\n): Promise<IsStringCoding<O> extends true ? string : Buffer>;\n\nexport async function hmac(\n hash: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512' | 'sha1' | 'sha256' | 'sha384' | 'sha512',\n key: BinaryLike | CryptoKey,\n data: BinaryLike,\n o?: DigestOptions,\n) {\n let ck =\n key instanceof CryptoKey\n ? key\n : await crypto.subtle.importKey(\n 'raw',\n binaryOf(key),\n {\n name: 'HMAC',\n hash: {\n name: normalizeHash(hash),\n },\n },\n false,\n ['sign'],\n );\n let buffer = await crypto.subtle.sign(\n {\n name: 'HMAC',\n hash: {\n name: hash,\n },\n },\n ck,\n binaryOf(data),\n );\n\n return encode(buffer, o);\n}\n\nfunction normalizeHash(hash: string) {\n return hash.replace(/^sha/i, 'SHA-');\n}\n"],"names":[],"mappings":";;AA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AAA,KACvC;AAAA,GACF;AACA,EAAO,OAAA,GAAA,CAAA;AACT,CAAA;AAYA,eAAsB,IACpB,CAAA,IAAA,EACA,GACA,EAAA,IAAA,EACA,CACA,EAAA;AACA,EAAA,IAAI,KACF,GAAe,YAAA,SAAA,GACX,GACA,GAAA,MAAM,OAAO,MAAO,CAAA,SAAA;AAAA,IAClB,KAAA;AAAA,IACA,SAAS,GAAG,CAAA;AAAA,IACZ;AAAA,MACE,IAAM,EAAA,MAAA;AAAA,MACN,IAAM,EAAA;AAAA,QACJ,IAAA,EAAM,cAAc,IAAI,CAAA;AAAA,OAC1B;AAAA,KACF;AAAA,IACA,KAAA;AAAA,IACA,CAAC,MAAM,CAAA;AAAA,GACT,CAAA;AACN,EAAI,IAAA,MAAA,GAAS,MAAM,MAAA,CAAO,MAAO,CAAA,IAAA;AAAA,IAC/B;AAAA,MACE,IAAM,EAAA,MAAA;AAAA,MACN,IAAM,EAAA;AAAA,QACJ,IAAM,EAAA,IAAA;AAAA,OACR;AAAA,KACF;AAAA,IACA,EAAA;AAAA,IACA,SAAS,IAAI,CAAA;AAAA,GACf,CAAA;AAEA,EAAO,OAAA,MAAA,CAAO,QAAQ,CAAC,CAAA,CAAA;AACzB,CAAA;AAEA,SAAS,cAAc,IAAc,EAAA;AACnC,EAAO,OAAA,IAAA,CAAK,OAAQ,CAAA,OAAA,EAAS,MAAM,CAAA,CAAA;AACrC;;;;"}
|
package/lib/errors/Errors.js
CHANGED
|
@@ -83,6 +83,25 @@ class DetailHolder {
|
|
|
83
83
|
throw(o) {
|
|
84
84
|
throw this.asError(o);
|
|
85
85
|
}
|
|
86
|
+
toJSON() {
|
|
87
|
+
return {
|
|
88
|
+
code: this.code,
|
|
89
|
+
message: this.message,
|
|
90
|
+
status: this.status,
|
|
91
|
+
description: this.description,
|
|
92
|
+
cause: this.cause,
|
|
93
|
+
metadata: this.metadata
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
asResponse() {
|
|
97
|
+
return new Response(JSON.stringify(this.toJSON()), {
|
|
98
|
+
status: this.status,
|
|
99
|
+
statusText: this.description,
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json"
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
86
105
|
}
|
|
87
106
|
class Errors {
|
|
88
107
|
static BadRequest = this.with({ status: 400 });
|
|
@@ -92,6 +111,7 @@ class Errors {
|
|
|
92
111
|
static InternalServerError = this.with({ status: 501 });
|
|
93
112
|
static NotImplemented = this.with({ status: 501 });
|
|
94
113
|
static ServiceUnavailable = this.with({ status: 503 });
|
|
114
|
+
static resolvers = [];
|
|
95
115
|
static with(init) {
|
|
96
116
|
return new DetailHolder(init);
|
|
97
117
|
}
|
|
@@ -116,6 +136,12 @@ class Errors {
|
|
|
116
136
|
if (e instanceof DetailError) {
|
|
117
137
|
return e.detail;
|
|
118
138
|
}
|
|
139
|
+
for (const resolver of this.resolvers) {
|
|
140
|
+
const r = resolver(e);
|
|
141
|
+
if (r) {
|
|
142
|
+
return r;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
119
145
|
if (e instanceof Error) {
|
|
120
146
|
const { message, code, status } = e;
|
|
121
147
|
return new DetailHolder({
|
package/lib/errors/Errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Errors.js","sources":["../../src/errors/Errors.ts"],"sourcesContent":["import { getHttpStatusText } from '../http/HttpStatus';\n\nexport interface ErrorDetailInit {\n message?: string;\n status: number;\n description?: string;\n code?: number | string;\n metadata?: Record<string, any>;\n cause?: any;\n}\n\nexport class DetailError extends Error {\n readonly status: number;\n readonly description?: string;\n\n constructor(readonly detail: ErrorDetail) {\n super(detail.message, {\n cause: detail.cause,\n });\n this.status = detail.status;\n this.description = detail.description;\n }\n\n toJSON() {\n return {\n code: this.detail.code,\n message: this.message,\n status: this.status,\n description: this.description,\n cause: this.cause,\n };\n }\n}\n\nclass DetailHolder implements ErrorDetail {\n readonly message: string;\n readonly status: number;\n readonly code: number | string;\n readonly metadata?: Record<string, any>;\n readonly description?: string;\n readonly cause?: any;\n\n constructor({\n status,\n message = getHttpStatusText(status),\n code = status,\n metadata,\n description,\n cause,\n }: ErrorDetailInit) {\n this.message = message ?? String(status);\n this.status = status;\n this.code = code;\n this.description = description;\n this.metadata = metadata;\n this.cause = cause;\n }\n\n with(o?: Partial<ErrorDetailInit> | string): DetailHolder {\n if (typeof o === 'string') {\n o = { message: o };\n }\n\n if (o === undefined) {\n return new DetailHolder(this);\n }\n\n return new DetailHolder({\n status: this.status,\n code: this.code,\n message: this.message,\n metadata: this.metadata,\n cause: this.cause,\n ...o,\n });\n }\n\n asError(o?: Partial<ErrorDetailInit> | string): Error {\n if (typeof o === 'string') {\n o = { message: o };\n }\n\n return new DetailError(this.with(o));\n }\n\n require(v: any, o?: Partial<ErrorDetailInit> | string): any {\n if (v === undefined || v === null) {\n throw this.asError(o);\n }\n\n return v;\n }\n\n check(cond: any, o?: Partial<ErrorDetailInit> | string) {\n switch (cond) {\n case false:\n case undefined:\n case null: {\n throw this.asError(o);\n }\n }\n }\n\n throw(o?: Partial<ErrorDetailInit>): never {\n throw this.asError(o);\n }\n}\n\nexport interface ErrorDetail {\n readonly message: string;\n readonly status: number;\n readonly code?: number | string;\n readonly metadata?: Record<string, any>;\n readonly description?: string;\n readonly cause?: any;\n\n with(message: string): ErrorDetail;\n\n with(o?: Partial<ErrorDetailInit>): ErrorDetail;\n\n asError(o?: Partial<ErrorDetailInit>): Error;\n\n asError(message: string): Error;\n\n throw(o?: Partial<ErrorDetailInit>): never;\n\n require<T>(v: T | undefined, o?: Partial<ErrorDetailInit> | string): NonNullable<T>;\n\n // 不支持 return value\n // https://stackoverflow.com/a/73252858/1870054\n\n check(condition: boolean, o?: Partial<ErrorDetailInit> | string): asserts condition;\n\n check<T>(v: T | undefined | null, o?: Partial<ErrorDetailInit> | string): asserts v is NonNullable<T>;\n}\n\nexport class Errors {\n static BadRequest: ErrorDetail = this.with({ status: 400 });\n static Unauthorized: ErrorDetail = this.with({ status: 403 });\n static Forbidden: ErrorDetail = this.with({ status: 403 });\n static NotFound: ErrorDetail = this.with({ status: 404 });\n static InternalServerError: ErrorDetail = this.with({ status: 501 });\n static NotImplemented: ErrorDetail = this.with({ status: 501 });\n static ServiceUnavailable: ErrorDetail = this.with({ status: 503 });\n\n static with(init: ErrorDetailInit): ErrorDetail {\n return new DetailHolder(init);\n }\n\n static ok(res: Response, o?: Partial<ErrorDetailInit>) {\n if (res.ok) {\n return res;\n }\n throw this.with({\n description: res.statusText,\n status: res.status,\n ...o,\n metadata: {\n ...o?.metadata,\n response: res,\n },\n }).asError();\n }\n\n static resolve(e: any): ErrorDetail {\n if (e instanceof DetailHolder) {\n return e;\n }\n\n if (e instanceof DetailError) {\n return e.detail;\n }\n\n if (e instanceof Error) {\n const { message, code, status } = e as any;\n // can get status from NestJS HttpException\n return new DetailHolder({\n message,\n status: parseInt(status) || 500,\n code,\n cause: e,\n });\n }\n\n return new DetailHolder({\n message: e.message,\n status: 500,\n cause: e,\n });\n }\n}\n\n// interface ZodError {\n// path: Array<string | number>;\n// message: string;\n// code: string;\n// expected?: any;\n// received?: any;\n// }\n"],"names":[],"mappings":";;AAWO,MAAM,oBAAoB,KAAM,CAAA;AAAA,EAIrC,YAAqB,MAAqB,EAAA;AACxC,IAAA,KAAA,CAAM,OAAO,OAAS,EAAA;AAAA,MACpB,OAAO,MAAO,CAAA,KAAA;AAAA,KACf,CAAA,CAAA;AAHkB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA,CAAA;AAInB,IAAA,IAAA,CAAK,SAAS,MAAO,CAAA,MAAA,CAAA;AACrB,IAAA,IAAA,CAAK,cAAc,MAAO,CAAA,WAAA,CAAA;AAAA,GAC5B;AAAA,EATS,MAAA,CAAA;AAAA,EACA,WAAA,CAAA;AAAA,EAUT,MAAS,GAAA;AACP,IAAO,OAAA;AAAA,MACL,IAAA,EAAM,KAAK,MAAO,CAAA,IAAA;AAAA,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AA,EAAW,GAAA,CAAA,CAAA;AAElC,MAAA,OAAO,IAAI,YAAa,CAAA;AAAA,QACtB,OAAA;AAAA,QACA,MAAA,EAAQ,QAAS,CAAA,MAAM,CAAK,IAAA,GAAA;AAAA,QAC5B,IAAA;AAAA,QACA,KAAO,EAAA,CAAA;AAAA,OACR,CAAA,CAAA;AAAA,KACH;AAEA,IAAA,OAAO,IAAI,YAAa,CAAA;AAAA,MACtB,SAAS,CAAE,CAAA,OAAA;AAAA,MACX,MAAQ,EAAA,GAAA;AAAA,MACR,KAAO,EAAA,CAAA;AAAA,KACR,CAAA,CAAA;AAAA,GACH;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"Errors.js","sources":["../../src/errors/Errors.ts"],"sourcesContent":["import { getHttpStatusText } from '../http/HttpStatus';\n\nexport interface ErrorDetailInit {\n message?: string;\n status: number;\n description?: string;\n code?: number | string;\n metadata?: Record<string, any>;\n cause?: any;\n}\n\nexport class DetailError extends Error {\n readonly status: number;\n readonly description?: string;\n\n constructor(readonly detail: ErrorDetail) {\n super(detail.message, {\n cause: detail.cause,\n });\n this.status = detail.status;\n this.description = detail.description;\n }\n\n toJSON() {\n return {\n code: this.detail.code,\n message: this.message,\n status: this.status,\n description: this.description,\n cause: this.cause,\n };\n }\n}\n\nclass DetailHolder implements ErrorDetail {\n readonly message: string;\n readonly status: number;\n readonly code: number | string;\n readonly metadata?: Record<string, any>;\n readonly description?: string;\n readonly cause?: any;\n\n constructor({\n status,\n message = getHttpStatusText(status),\n code = status,\n metadata,\n description,\n cause,\n }: ErrorDetailInit) {\n this.message = message ?? String(status);\n this.status = status;\n this.code = code;\n this.description = description;\n this.metadata = metadata;\n this.cause = cause;\n }\n\n with(o?: Partial<ErrorDetailInit> | string): DetailHolder {\n if (typeof o === 'string') {\n o = { message: o };\n }\n\n if (o === undefined) {\n return new DetailHolder(this);\n }\n\n return new DetailHolder({\n status: this.status,\n code: this.code,\n message: this.message,\n metadata: this.metadata,\n cause: this.cause,\n ...o,\n });\n }\n\n asError(o?: Partial<ErrorDetailInit> | string): Error {\n if (typeof o === 'string') {\n o = { message: o };\n }\n\n return new DetailError(this.with(o));\n }\n\n require(v: any, o?: Partial<ErrorDetailInit> | string): any {\n if (v === undefined || v === null) {\n throw this.asError(o);\n }\n\n return v;\n }\n\n check(cond: any, o?: Partial<ErrorDetailInit> | string) {\n switch (cond) {\n case false:\n case undefined:\n case null: {\n throw this.asError(o);\n }\n }\n }\n\n throw(o?: string | Partial<ErrorDetailInit>): never {\n throw this.asError(o);\n }\n\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n status: this.status,\n description: this.description,\n cause: this.cause,\n metadata: this.metadata,\n };\n }\n\n asResponse(): Response {\n return new Response(JSON.stringify(this.toJSON()), {\n status: this.status,\n statusText: this.description,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n }\n}\n\nexport interface ErrorDetail {\n readonly message: string;\n readonly status: number;\n readonly code?: number | string;\n readonly metadata?: Record<string, any>;\n readonly description?: string;\n readonly cause?: any;\n\n with(message: string): ErrorDetail;\n\n with(o?: Partial<ErrorDetailInit>): ErrorDetail;\n\n asError(o?: string | Partial<ErrorDetailInit>): Error;\n\n asResponse(): Response;\n\n throw(o?: string | Partial<ErrorDetailInit>): never;\n\n require<T>(v: T | undefined, o?: Partial<ErrorDetailInit> | string): NonNullable<T>;\n\n // 不支持 return value\n // https://stackoverflow.com/a/73252858/1870054\n\n check(condition: boolean, o?: Partial<ErrorDetailInit> | string): asserts condition;\n\n check<T>(v: T | undefined | null, o?: Partial<ErrorDetailInit> | string): asserts v is NonNullable<T>;\n}\n\nexport class Errors {\n static BadRequest: ErrorDetail = this.with({ status: 400 });\n static Unauthorized: ErrorDetail = this.with({ status: 403 });\n static Forbidden: ErrorDetail = this.with({ status: 403 });\n static NotFound: ErrorDetail = this.with({ status: 404 });\n static InternalServerError: ErrorDetail = this.with({ status: 501 });\n static NotImplemented: ErrorDetail = this.with({ status: 501 });\n static ServiceUnavailable: ErrorDetail = this.with({ status: 503 });\n\n static resolvers: ((e: any) => ErrorDetail | void)[] = [];\n\n static with(init: ErrorDetailInit): ErrorDetail {\n return new DetailHolder(init);\n }\n\n static ok(res: Response, o?: Partial<ErrorDetailInit>) {\n if (res.ok) {\n return res;\n }\n throw this.with({\n description: res.statusText,\n status: res.status,\n ...o,\n metadata: {\n ...o?.metadata,\n response: res,\n },\n }).asError();\n }\n\n static resolve(e: any): ErrorDetail {\n if (e instanceof DetailHolder) {\n return e;\n }\n\n if (e instanceof DetailError) {\n return e.detail;\n }\n\n for (const resolver of this.resolvers) {\n const r = resolver(e);\n if (r) {\n return r;\n }\n }\n\n if (e instanceof Error) {\n const { message, code, status } = e as any;\n // can get status from NestJS HttpException\n return new DetailHolder({\n message,\n status: parseInt(status) || 500,\n code,\n cause: e,\n });\n }\n\n return new DetailHolder({\n message: e.message,\n status: 500,\n cause: e,\n });\n }\n}\n\n// interface ZodError {\n// path: Array<string | number>;\n// message: string;\n// code: string;\n// expected?: any;\n// received?: any;\n// }\n"],"names":[],"mappings":";;AAWO,MAAM,oBAAoB,KAAM,CAAA;AAAA,EAIrC,YAAqB,MAAqB,EAAA;AACxC,IAAA,KAAA,CAAM,OAAO,OAAS,EAAA;AAAA,MACpB,OAAO,MAAO,CAAA,KAAA;AAAA,KACf,CAAA,CAAA;AAHkB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA,CAAA;AAInB,IAAA,IAAA,CAAK,SAAS,MAAO,CAAA,MAAA,CAAA;AACrB,IAAA,IAAA,CAAK,cAAc,MAAO,CAAA,WAAA,CAAA;AAAA,GAC5B;AAAA,EATS,MAAA,CAAA;AAAA,EACA,WAAA,CAAA;AAAA,EAUT,MAAS,GAAA;AACP,IAAO,OAAA;AAAA,MACL,IAAA,EAAM,KAAK,MAAO,CAAA,IAAA;AAAA,MAClB,SAAS,IAAK,CAAA,OAAA;AAAA,MACd,QAAQ,IAAK,CAAA,MAAA;AAAA,MACb,aAAa,IAAK,CAAA,WAAA;AA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;AAAA,MACZ,GAAG,CAAA;AAAA,MACH,QAAU,EAAA;AAAA,QACR,GAAG,CAAG,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,CAAA,CAAA,QAAA;AAAA,QACN,QAAU,EAAA,GAAA;AAAA,OACZ;AAAA,KACD,EAAE,OAAQ,EAAA,CAAA;AAAA,GACb;AAAA,EAEA,OAAO,QAAQ,CAAqB,EAAA;AAClC,IAAA,IAAI,aAAa,YAAc,EAAA;AAC7B,MAAO,OAAA,CAAA,CAAA;AAAA,KACT;AAEA,IAAA,IAAI,aAAa,WAAa,EAAA;AAC5B,MAAA,OAAO,CAAE,CAAA,MAAA,CAAA;AAAA,KACX;AAEA,IAAW,KAAA,MAAA,QAAA,IAAY,KAAK,SAAW,EAAA;AACrC,MAAM,MAAA,CAAA,GAAI,SAAS,CAAC,CAAA,CAAA;AACpB,MAAA,IAAI,CAAG,EAAA;AACL,QAAO,OAAA,CAAA,CAAA;AAAA,OACT;AAAA,KACF;AAEA,IAAA,IAAI,aAAa,KAAO,EAAA;AACtB,MAAA,MAAM,EAAE,OAAA,EAAS,IAAM,EAAA,MAAA,EAAW,GAAA,CAAA,CAAA;AAElC,MAAA,OAAO,IAAI,YAAa,CAAA;AAAA,QACtB,OAAA;AAAA,QACA,MAAA,EAAQ,QAAS,CAAA,MAAM,CAAK,IAAA,GAAA;AAAA,QAC5B,IAAA;AAAA,QACA,KAAO,EAAA,CAAA;AAAA,OACR,CAAA,CAAA;AAAA,KACH;AAEA,IAAA,OAAO,IAAI,YAAa,CAAA;AAAA,MACtB,SAAS,CAAE,CAAA,OAAA;AAAA,MACX,MAAQ,EAAA,GAAA;AAAA,MACR,KAAO,EAAA,CAAA;AAAA,KACR,CAAA,CAAA;AAAA,GACH;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFetchWith.js","sources":["../../src/fetch/createFetchWith.ts"],"sourcesContent":["import { MaybePromise } from '../asyncs/MaybePromise';\nimport { getGlobalThis } from '../isomorphics/getGlobalThis';\nimport { type FetchLike } from './types';\n\nexport function createFetchWith({\n fetch = getGlobalThis().fetch,\n onRequest = (ctx) => ctx.next(ctx.url, ctx.req),\n onResponse = (ctx) => ctx.res,\n}: {\n fetch?: FetchLike;\n onRequest?: (ctx: {\n url: string;\n req: RequestInit;\n next: (url: string, req: RequestInit) => Promise<Response>;\n }) => MaybePromise<Response | void>;\n onResponse?: (ctx: { url: string; req: RequestInit; res: Response }) => MaybePromise<Response>;\n}) {\n return async (urlOrRequest: string | Request, init?: RequestInit & { fetch?: FetchLike }) => {\n const url = String(urlOrRequest);\n let req = init || {};\n const nextFetch = req.fetch || fetch;\n const res =\n (await onRequest({\n url,\n req,\n next: (url, init) => {\n req = init;\n return nextFetch(url, init);\n },\n })) ?? (await nextFetch(url, init));\n return onResponse({ url, req, res });\n };\n}\n"],"names":["url","init"],"mappings":";;AAIO,SAAS,eAAgB,CAAA;AAAA,EAC9B,KAAA,GAAQ,eAAgB,CAAA,KAAA;AAAA,EACxB,SAAA,GAAY,CAAC,GAAQ,KAAA,GAAA,CAAI,KAAK,GAAI,CAAA,GAAA,EAAK,IAAI,GAAG,CAAA;AAAA,EAC9C,UAAA,GAAa,CAAC,GAAA,KAAQ,GAAI,CAAA,GAAA;AAC5B,CAQG,EAAA;AACD,EAAO,OAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"createFetchWith.js","sources":["../../src/fetch/createFetchWith.ts"],"sourcesContent":["import { MaybePromise } from '../asyncs/MaybePromise';\nimport { getGlobalThis } from '../isomorphics/getGlobalThis';\nimport { type FetchLike } from './types';\n\nexport function createFetchWith({\n fetch = getGlobalThis().fetch,\n onRequest = (ctx) => ctx.next(ctx.url, ctx.req),\n onResponse = (ctx) => ctx.res,\n}: {\n fetch?: FetchLike;\n onRequest?: (ctx: {\n url: string;\n req: RequestInit;\n next: (url: string, req: RequestInit) => Promise<Response>;\n }) => MaybePromise<Response | void>;\n onResponse?: (ctx: { url: string; req: RequestInit; res: Response }) => MaybePromise<Response>;\n}) {\n return async (urlOrRequest: string | URL | Request, init?: RequestInit & { fetch?: FetchLike }) => {\n const url = String(urlOrRequest);\n let req = init || {};\n const nextFetch = req.fetch || fetch;\n const res =\n (await onRequest({\n url,\n req,\n next: (url, init) => {\n req = init;\n return nextFetch(url, init);\n },\n })) ?? (await nextFetch(url, init));\n return onResponse({ url, req, res });\n };\n}\n"],"names":["url","init"],"mappings":";;AAIO,SAAS,eAAgB,CAAA;AAAA,EAC9B,KAAA,GAAQ,eAAgB,CAAA,KAAA;AAAA,EACxB,SAAA,GAAY,CAAC,GAAQ,KAAA,GAAA,CAAI,KAAK,GAAI,CAAA,GAAA,EAAK,IAAI,GAAG,CAAA;AAAA,EAC9C,UAAA,GAAa,CAAC,GAAA,KAAQ,GAAI,CAAA,GAAA;AAC5B,CAQG,EAAA;AACD,EAAO,OAAA,OAAO,cAAsC,IAA+C,KAAA;AAjBrG,IAAA,IAAA,EAAA,CAAA;AAkBI,IAAM,MAAA,GAAA,GAAM,OAAO,YAAY,CAAA,CAAA;AAC/B,IAAI,IAAA,GAAA,GAAM,QAAQ,EAAC,CAAA;AACnB,IAAM,MAAA,SAAA,GAAY,IAAI,KAAS,IAAA,KAAA,CAAA;AAC/B,IAAM,MAAA,GAAA,GAAA,CACH,WAAM,SAAU,CAAA;AAAA,MACf,GAAA;AAAA,MACA,GAAA;AAAA,MACA,IAAA,EAAM,CAACA,IAAAA,EAAKC,KAAS,KAAA;AACnB,QAAMA,GAAAA,GAAAA,KAAAA,CAAAA;AACN,QAAO,OAAA,SAAA,CAAUD,MAAKC,KAAI,CAAA,CAAA;AAAA,OAC5B;AAAA,KACD,CAPA,KAAA,IAAA,GAAA,EAAA,GAOO,MAAM,SAAA,CAAU,KAAK,IAAI,CAAA,CAAA;AACnC,IAAA,OAAO,UAAW,CAAA,EAAE,GAAK,EAAA,GAAA,EAAK,KAAK,CAAA,CAAA;AAAA,GACrC,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createFetchWithRetry.js","sources":["../../src/fetch/createFetchWithRetry.ts"],"sourcesContent":["import { getGlobalThis } from '../isomorphics/getGlobalThis';\nimport { FetchLike } from './types';\n\ntype RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;\ntype RequestRetryOnFunction = (\n attempt: number,\n error: Error | null,\n response: Response | null,\n) => boolean | Promise<boolean>;\n\nexport interface FetchWithRetryOptions {\n fetch?: FetchLike;\n retries?: number;\n retryDelay?: number | RequestDelayFunction;\n retryOn?: number[] | RequestRetryOnFunction;\n}\n\nexport function createFetchWithRetry({\n fetch = getGlobalThis().fetch,\n retries = 3,\n retryDelay = 1000,\n retryOn = [],\n}: FetchWithRetryOptions = {}): FetchLike {\n // https://github.com/jonbern/fetch-retry/blob/master/index.js\n\n return function fetchRetry(input: string | URL | Request, init?: RequestInit) {\n return new Promise(function (resolve, reject) {\n var wrappedFetch = function (attempt: number) {\n // As of node 18, this is no longer needed since node comes with native support for fetch:\n /* istanbul ignore next */\n var _input = typeof Request !== 'undefined' && input instanceof Request ? input.clone() : input;\n fetch(_input, init)\n .then(function (response) {\n if (Array.isArray(retryOn) && retryOn.indexOf(response.status) === -1) {\n resolve(response);\n } else if (typeof retryOn === 'function') {\n try {\n return Promise.resolve(retryOn(attempt, null, response))\n .then(function (retryOnResponse) {\n if (retryOnResponse) {\n retry(attempt, null, response);\n } else {\n resolve(response);\n }\n })\n .catch(reject);\n } catch (error) {\n reject(error);\n }\n } else {\n if (attempt < retries) {\n retry(attempt, null, response);\n } else {\n resolve(response);\n }\n }\n return;\n })\n .catch(function (error) {\n if (typeof retryOn === 'function') {\n try {\n // eslint-disable-next-line no-undef\n Promise.resolve(retryOn(attempt, error, null))\n .then(function (retryOnResponse) {\n if (retryOnResponse) {\n retry(attempt, error, null);\n } else {\n reject(error);\n }\n })\n .catch(function (error) {\n reject(error);\n });\n } catch (error) {\n reject(error);\n }\n } else if (attempt < retries) {\n retry(attempt, error, null);\n } else {\n reject(error);\n }\n });\n };\n\n function retry(attempt: number, error: any, response: Response | null) {\n let delay = typeof retryDelay === 'function' ? retryDelay(attempt, error, response) : retryDelay;\n setTimeout(function () {\n wrappedFetch(++attempt);\n }, delay);\n }\n\n wrappedFetch(0);\n });\n };\n}\n"],"names":["error"],"mappings":";;AAiBO,SAAS,oBAAqB,CAAA;AAAA,EACnC,KAAA,GAAQ,eAAgB,CAAA,KAAA;AAAA,EACxB,OAAU,GAAA,CAAA;AAAA,EACV,UAAa,GAAA,GAAA;AAAA,EACb,UAAU,EAAC;AACb,CAAA,GAA2B,EAAe,EAAA;AAGxC,EAAO,OAAA,SAAS,UAAW,CAAA,KAAA,EAA+B,IAAoB,EAAA;AAC5E,IAAA,OAAO,IAAI,OAAA,CAAQ,SAAU,OAAA,EAAS,MAAQ,EAAA;AAC5C,MAAI,IAAA,YAAA,GAAe,SAAU,OAAiB,EAAA;AAG5C,QAAI,IAAA,MAAA,GAAS,OAAO,OAAY,KAAA,WAAA,IAAe,iBAAiB,OAAU,GAAA,KAAA,CAAM,OAAU,GAAA,KAAA,CAAA;AAC1F,QAAA,KAAA,CAAM,MAAQ,EAAA,IAAI,CACf,CAAA,IAAA,CAAK,SAAU,QAAU,EAAA;AACxB,UAAI,IAAA,KAAA,CAAM,QAAQ,OAAO,CAAA,IAAK,QAAQ,OAAQ,CAAA,QAAA,CAAS,MAAM,CAAA,KAAM,CAAI,CAAA,EAAA;AACrE,YAAA,OAAA,CAAQ,QAAQ,CAAA,CAAA;AAAA,WAClB,MAAA,IAAW,OAAO,OAAA,KAAY,UAAY,EAAA;AACxC,YAAI,IAAA;AACF,cAAO,OAAA,OAAA,CAAQ,OAAQ,CAAA,OAAA,CAAQ,OAAS,EAAA,IAAA,EAAM,QAAQ,CAAC,CAAA,CACpD,IAAK,CAAA,SAAU,eAAiB,EAAA;AAC/B,gBAAA,IAAI,eAAiB,EAAA;AACnB,kBAAM,KAAA,CAAA,OAAA,EAAS,MAA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A,CAAA;AAAA,OACL,CAAA;AAEA,MAAS,SAAA,KAAA,CAAM,OAAiB,EAAA,KAAA,EAAY,QAA2B,EAAA;AACrE,QAAI,IAAA,KAAA,GAAQ,OAAO,UAAe,KAAA,UAAA,GAAa,WAAW,OAAS,EAAA,KAAA,EAAO,QAAQ,CAAI,GAAA,UAAA,CAAA;AACtF,QAAA,UAAA,CAAW,WAAY;AACrB,UAAA,YAAA,CAAa,EAAE,OAAO,CAAA,CAAA;AAAA,WACrB,KAAK,CAAA,CAAA;AAAA,OACV;AAEA,MAAA,YAAA,CAAa,CAAC,CAAA,CAAA;AAAA,KACf,CAAA,CAAA;AAAA,GACH,CAAA;AACF;;;;"}
|
package/lib/fetch/dumpRequest.js
CHANGED
|
@@ -9,7 +9,9 @@ ${Array.from(new Headers(req.headers).entries()).map(([k, v]) => `${k}: ${v}`).j
|
|
|
9
9
|
`;
|
|
10
10
|
let done;
|
|
11
11
|
if (req.body) {
|
|
12
|
-
|
|
12
|
+
let hdr = new Headers(req.headers);
|
|
13
|
+
let ct = hdr.get("content-type");
|
|
14
|
+
if (ct === "application/octet-stream") ; else if (req.body instanceof ReadableStream) {
|
|
13
15
|
const [a, b] = req.body.tee();
|
|
14
16
|
req.body = a;
|
|
15
17
|
const signal = req.signal;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dumpRequest.js","sources":["../../src/fetch/dumpRequest.ts"],"sourcesContent":["export function dumpRequest({\n url,\n req = {},\n log = console.log,\n}: {\n url: string;\n req?: RequestInit;\n log?: (s: string) => void;\n}) {\n const { method = 'GET' } = req;\n let out = `-> ${method} ${url}\n${Array.from(new Headers(req.headers).entries())\n .map(([k, v]) => `${k}: ${v}`)\n .join('\\n')}\n`;\n\n let done: Promise<string> | undefined;\n if (req.body) {\n if (req.body instanceof ReadableStream) {\n const [a, b] = req.body.tee();\n req.body = a;\n const signal = req.signal;\n done = Promise.resolve().then(async () => {\n const reader = b.getReader();\n log(out);\n while (true) {\n if (signal?.aborted) {\n break;\n }\n\n let { done, value } = await reader.read();\n value instanceof Uint8Array && (value = new TextDecoder().decode(value));\n out += value;\n log(value);\n if (!done) {\n break;\n }\n }\n // maybe for archive\n out += `\\n`;\n return out;\n });\n } else {\n out += `\n${req.body}\n`;\n }\n log(out);\n }\n return done || Promise.resolve(out);\n}\n"],"names":["done"],"mappings":"AAAO,SAAS,WAAY,CAAA;AAAA,EAC1B,GAAA;AAAA,EACA,MAAM,EAAC;AAAA,EACP,MAAM,OAAQ,CAAA,GAAA;AAChB,CAIG,EAAA;AACD,EAAM,MAAA,EAAE,MAAS,GAAA,KAAA,EAAU,GAAA,GAAA,CAAA;AAC3B,EAAA,IAAI,GAAM,GAAA,CAAA,GAAA,EAAM,MAAM,CAAA,CAAA,EAAI,GAAG,CAAA;AAAA,EAC7B,KAAA,CAAM,IAAK,CAAA,IAAI,OAAQ,CAAA,GAAA,CAAI,OAAO,CAAE,CAAA,OAAA,EAAS,CAAA,CAC5C,GAAI,CAAA,CAAC,CAAC,CAAG,EAAA,CAAC,CAAM,KAAA,CAAA,EAAG,CAAC,CAAA,EAAA,EAAK,CAAC,CAAE,CAAA,CAAA,CAC5B,IAAK,CAAA,IAAI,CAAC,CAAA;AAAA,CAAA,CAAA;AAGX,EAAI,IAAA,IAAA,CAAA;AACJ,EAAA,IAAI,IAAI,IAAM,EAAA;AACZ,IAAI,IAAA,GAAA,CAAI,
|
|
1
|
+
{"version":3,"file":"dumpRequest.js","sources":["../../src/fetch/dumpRequest.ts"],"sourcesContent":["export function dumpRequest({\n url,\n req = {},\n log = console.log,\n}: {\n url: string;\n req?: RequestInit;\n log?: (s: string) => void;\n}) {\n const { method = 'GET' } = req;\n let out = `-> ${method} ${url}\n${Array.from(new Headers(req.headers).entries())\n .map(([k, v]) => `${k}: ${v}`)\n .join('\\n')}\n`;\n\n let done: Promise<string> | undefined;\n if (req.body) {\n let hdr = new Headers(req.headers);\n let ct = hdr.get('content-type');\n if (ct === 'application/octet-stream') {\n // skip\n } else if (req.body instanceof ReadableStream) {\n const [a, b] = req.body.tee();\n req.body = a;\n const signal = req.signal;\n done = Promise.resolve().then(async () => {\n const reader = b.getReader();\n log(out);\n while (true) {\n if (signal?.aborted) {\n break;\n }\n\n let { done, value } = await reader.read();\n value instanceof Uint8Array && (value = new TextDecoder().decode(value));\n out += value;\n log(value);\n if (!done) {\n break;\n }\n }\n // maybe for archive\n out += `\\n`;\n return out;\n });\n } else {\n out += `\n${req.body}\n`;\n }\n log(out);\n }\n return done || Promise.resolve(out);\n}\n"],"names":["done"],"mappings":"AAAO,SAAS,WAAY,CAAA;AAAA,EAC1B,GAAA;AAAA,EACA,MAAM,EAAC;AAAA,EACP,MAAM,OAAQ,CAAA,GAAA;AAChB,CAIG,EAAA;AACD,EAAM,MAAA,EAAE,MAAS,GAAA,KAAA,EAAU,GAAA,GAAA,CAAA;AAC3B,EAAA,IAAI,GAAM,GAAA,CAAA,GAAA,EAAM,MAAM,CAAA,CAAA,EAAI,GAAG,CAAA;AAAA,EAC7B,KAAA,CAAM,IAAK,CAAA,IAAI,OAAQ,CAAA,GAAA,CAAI,OAAO,CAAE,CAAA,OAAA,EAAS,CAAA,CAC5C,GAAI,CAAA,CAAC,CAAC,CAAG,EAAA,CAAC,CAAM,KAAA,CAAA,EAAG,CAAC,CAAA,EAAA,EAAK,CAAC,CAAE,CAAA,CAAA,CAC5B,IAAK,CAAA,IAAI,CAAC,CAAA;AAAA,CAAA,CAAA;AAGX,EAAI,IAAA,IAAA,CAAA;AACJ,EAAA,IAAI,IAAI,IAAM,EAAA;AACZ,IAAA,IAAI,GAAM,GAAA,IAAI,OAAQ,CAAA,GAAA,CAAI,OAAO,CAAA,CAAA;AACjC,IAAI,IAAA,EAAA,GAAK,GAAI,CAAA,GAAA,CAAI,cAAc,CAAA,CAAA;AAC/B,IAAA,IAAI,OAAO,0BAA4B,EAAA,CAEvC,MAAA,IAAW,GAAI,CAAA,IAAA,YAAgB,cAAgB,EAAA;AAC7C,MAAA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fetch/createFetchWithLogging.js';
|
|
58
58
|
export { dumpRequest } from './fetch/dumpRequest.js';
|
|
59
59
|
export { dumpResponse } from './fetch/dumpResponse.js';
|
|
60
|
+
export { createFetchWithRetry } from './fetch/createFetchWithRetry.js';
|
|
60
61
|
export { default as ms } from './libs/ms.js';
|
|
61
62
|
export { DetailError, Errors } from './errors/Errors.js';
|
|
62
63
|
export { getHttpStatusText, isRetryableHttpStatus } from './http/HttpStatus.js';
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/lib/server.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { polyfillCrypto } from './servers/polyfill/polyfillCrypto.js';
|
|
2
2
|
export { createFetchWithProxyByUndici } from './servers/fetch/createFetchWithProxyByUndici.js';
|
|
3
3
|
export { createFetchWithProxy } from './servers/fetch/createFetchWithProxy.js';
|
|
4
|
-
export { createFetchWithRetry } from './servers/fetch/createFetchWithRetry.js';
|
|
5
4
|
export { md5 } from './servers/crypto/md5.js';
|
|
6
5
|
export { getPackageDir } from './servers/getPackageDir.js';
|
|
7
6
|
//# sourceMappingURL=server.js.map
|
package/lib/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"server.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
package/package.json
CHANGED
package/src/crypto/hashing.ts
CHANGED
|
@@ -30,7 +30,7 @@ export function sha512(s: BinaryLike, o?: DigestOptions) {
|
|
|
30
30
|
|
|
31
31
|
function digestOf(a: string, s: BinaryLike, o?: DigestOptions) {
|
|
32
32
|
let buffer = crypto.subtle.digest(a, binaryOf(s));
|
|
33
|
-
return o ? buffer.then((v) => encode(v, o)) :
|
|
33
|
+
return o ? buffer.then((v) => encode(v, o)) : buffer;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export type DigestOptions =
|
|
@@ -47,6 +47,7 @@ function binaryOf(s: BinaryLike) {
|
|
|
47
47
|
if (typeof s === 'string') {
|
|
48
48
|
return new TextEncoder().encode(s);
|
|
49
49
|
}
|
|
50
|
+
// ArrayBuffer, Buffer, TypedArray, DataView
|
|
50
51
|
return s;
|
|
51
52
|
}
|
|
52
53
|
|
package/src/errors/Errors.ts
CHANGED
|
@@ -101,9 +101,30 @@ class DetailHolder implements ErrorDetail {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
throw(o?: Partial<ErrorDetailInit>): never {
|
|
104
|
+
throw(o?: string | Partial<ErrorDetailInit>): never {
|
|
105
105
|
throw this.asError(o);
|
|
106
106
|
}
|
|
107
|
+
|
|
108
|
+
toJSON() {
|
|
109
|
+
return {
|
|
110
|
+
code: this.code,
|
|
111
|
+
message: this.message,
|
|
112
|
+
status: this.status,
|
|
113
|
+
description: this.description,
|
|
114
|
+
cause: this.cause,
|
|
115
|
+
metadata: this.metadata,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
asResponse(): Response {
|
|
120
|
+
return new Response(JSON.stringify(this.toJSON()), {
|
|
121
|
+
status: this.status,
|
|
122
|
+
statusText: this.description,
|
|
123
|
+
headers: {
|
|
124
|
+
'Content-Type': 'application/json',
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
107
128
|
}
|
|
108
129
|
|
|
109
130
|
export interface ErrorDetail {
|
|
@@ -118,11 +139,11 @@ export interface ErrorDetail {
|
|
|
118
139
|
|
|
119
140
|
with(o?: Partial<ErrorDetailInit>): ErrorDetail;
|
|
120
141
|
|
|
121
|
-
asError(o?: Partial<ErrorDetailInit>): Error;
|
|
142
|
+
asError(o?: string | Partial<ErrorDetailInit>): Error;
|
|
122
143
|
|
|
123
|
-
|
|
144
|
+
asResponse(): Response;
|
|
124
145
|
|
|
125
|
-
throw(o?: Partial<ErrorDetailInit>): never;
|
|
146
|
+
throw(o?: string | Partial<ErrorDetailInit>): never;
|
|
126
147
|
|
|
127
148
|
require<T>(v: T | undefined, o?: Partial<ErrorDetailInit> | string): NonNullable<T>;
|
|
128
149
|
|
|
@@ -143,6 +164,8 @@ export class Errors {
|
|
|
143
164
|
static NotImplemented: ErrorDetail = this.with({ status: 501 });
|
|
144
165
|
static ServiceUnavailable: ErrorDetail = this.with({ status: 503 });
|
|
145
166
|
|
|
167
|
+
static resolvers: ((e: any) => ErrorDetail | void)[] = [];
|
|
168
|
+
|
|
146
169
|
static with(init: ErrorDetailInit): ErrorDetail {
|
|
147
170
|
return new DetailHolder(init);
|
|
148
171
|
}
|
|
@@ -171,6 +194,13 @@ export class Errors {
|
|
|
171
194
|
return e.detail;
|
|
172
195
|
}
|
|
173
196
|
|
|
197
|
+
for (const resolver of this.resolvers) {
|
|
198
|
+
const r = resolver(e);
|
|
199
|
+
if (r) {
|
|
200
|
+
return r;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
174
204
|
if (e instanceof Error) {
|
|
175
205
|
const { message, code, status } = e as any;
|
|
176
206
|
// can get status from NestJS HttpException
|
|
@@ -15,7 +15,7 @@ export function createFetchWith({
|
|
|
15
15
|
}) => MaybePromise<Response | void>;
|
|
16
16
|
onResponse?: (ctx: { url: string; req: RequestInit; res: Response }) => MaybePromise<Response>;
|
|
17
17
|
}) {
|
|
18
|
-
return async (urlOrRequest: string | Request, init?: RequestInit & { fetch?: FetchLike }) => {
|
|
18
|
+
return async (urlOrRequest: string | URL | Request, init?: RequestInit & { fetch?: FetchLike }) => {
|
|
19
19
|
const url = String(urlOrRequest);
|
|
20
20
|
let req = init || {};
|
|
21
21
|
const nextFetch = req.fetch || fetch;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { getGlobalThis } from '../isomorphics/getGlobalThis';
|
|
2
|
+
import { FetchLike } from './types';
|
|
3
3
|
|
|
4
4
|
type RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;
|
|
5
5
|
type RequestRetryOnFunction = (
|
|
@@ -23,7 +23,7 @@ export function createFetchWithRetry({
|
|
|
23
23
|
}: FetchWithRetryOptions = {}): FetchLike {
|
|
24
24
|
// https://github.com/jonbern/fetch-retry/blob/master/index.js
|
|
25
25
|
|
|
26
|
-
return function fetchRetry(input: string | Request, init?: RequestInit) {
|
|
26
|
+
return function fetchRetry(input: string | URL | Request, init?: RequestInit) {
|
|
27
27
|
return new Promise(function (resolve, reject) {
|
|
28
28
|
var wrappedFetch = function (attempt: number) {
|
|
29
29
|
// As of node 18, this is no longer needed since node comes with native support for fetch:
|
package/src/fetch/dumpRequest.ts
CHANGED
|
@@ -16,7 +16,11 @@ ${Array.from(new Headers(req.headers).entries())
|
|
|
16
16
|
|
|
17
17
|
let done: Promise<string> | undefined;
|
|
18
18
|
if (req.body) {
|
|
19
|
-
|
|
19
|
+
let hdr = new Headers(req.headers);
|
|
20
|
+
let ct = hdr.get('content-type');
|
|
21
|
+
if (ct === 'application/octet-stream') {
|
|
22
|
+
// skip
|
|
23
|
+
} else if (req.body instanceof ReadableStream) {
|
|
20
24
|
const [a, b] = req.body.tee();
|
|
21
25
|
req.body = a;
|
|
22
26
|
const signal = req.signal;
|
package/src/fetch/index.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { createFetchWith } from './createFetchWith';
|
|
|
3
3
|
export { createFetchWithLogging } from './createFetchWithLogging';
|
|
4
4
|
export { dumpRequest } from './dumpRequest';
|
|
5
5
|
export { dumpResponse } from './dumpResponse';
|
|
6
|
+
export { createFetchWithRetry, type FetchWithRetryOptions } from './createFetchWithRetry';
|
package/src/index.ts
CHANGED
|
@@ -95,7 +95,15 @@ export { PEM } from './crypto/pem/pem';
|
|
|
95
95
|
export { createRandom } from './maths/random';
|
|
96
96
|
|
|
97
97
|
// network
|
|
98
|
-
export {
|
|
98
|
+
export {
|
|
99
|
+
type FetchLike,
|
|
100
|
+
createFetchWith,
|
|
101
|
+
createFetchWithLogging,
|
|
102
|
+
dumpResponse,
|
|
103
|
+
dumpRequest,
|
|
104
|
+
createFetchWithRetry,
|
|
105
|
+
type FetchWithRetryOptions,
|
|
106
|
+
} from './fetch';
|
|
99
107
|
|
|
100
108
|
// bundled
|
|
101
109
|
export { default as ms } from './libs/ms';
|
package/src/server.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { polyfillCrypto } from './servers/polyfill/polyfillCrypto';
|
|
2
2
|
export { createFetchWithProxyByUndici } from './servers/fetch/createFetchWithProxyByUndici';
|
|
3
3
|
export { createFetchWithProxy } from './servers/fetch/createFetchWithProxy';
|
|
4
|
-
export { createFetchWithRetry, type FetchWithRetryOptions } from './servers/fetch/createFetchWithRetry';
|
|
5
4
|
|
|
6
5
|
export { md5 } from './servers/crypto/md5';
|
|
7
6
|
export { getPackageDir } from './servers/getPackageDir';
|
package/src/typedoc.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createFetchWithRetry.js","sources":["../../../src/servers/fetch/createFetchWithRetry.ts"],"sourcesContent":["import { FetchLike } from '../../fetch';\nimport { getGlobalThis } from '../../isomorphics/getGlobalThis';\n\ntype RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;\ntype RequestRetryOnFunction = (\n attempt: number,\n error: Error | null,\n response: Response | null,\n) => boolean | Promise<boolean>;\n\nexport interface FetchWithRetryOptions {\n fetch?: FetchLike;\n retries?: number;\n retryDelay?: number | RequestDelayFunction;\n retryOn?: number[] | RequestRetryOnFunction;\n}\n\nexport function createFetchWithRetry({\n fetch = getGlobalThis().fetch,\n retries = 3,\n retryDelay = 1000,\n retryOn = [],\n}: FetchWithRetryOptions = {}): FetchLike {\n // https://github.com/jonbern/fetch-retry/blob/master/index.js\n\n return function fetchRetry(input: string | Request, init?: RequestInit) {\n return new Promise(function (resolve, reject) {\n var wrappedFetch = function (attempt: number) {\n // As of node 18, this is no longer needed since node comes with native support for fetch:\n /* istanbul ignore next */\n var _input = typeof Request !== 'undefined' && input instanceof Request ? input.clone() : input;\n fetch(_input, init)\n .then(function (response) {\n if (Array.isArray(retryOn) && retryOn.indexOf(response.status) === -1) {\n resolve(response);\n } else if (typeof retryOn === 'function') {\n try {\n return Promise.resolve(retryOn(attempt, null, response))\n .then(function (retryOnResponse) {\n if (retryOnResponse) {\n retry(attempt, null, response);\n } else {\n resolve(response);\n }\n })\n .catch(reject);\n } catch (error) {\n reject(error);\n }\n } else {\n if (attempt < retries) {\n retry(attempt, null, response);\n } else {\n resolve(response);\n }\n }\n return;\n })\n .catch(function (error) {\n if (typeof retryOn === 'function') {\n try {\n // eslint-disable-next-line no-undef\n Promise.resolve(retryOn(attempt, error, null))\n .then(function (retryOnResponse) {\n if (retryOnResponse) {\n retry(attempt, error, null);\n } else {\n reject(error);\n }\n })\n .catch(function (error) {\n reject(error);\n });\n } catch (error) {\n reject(error);\n }\n } else if (attempt < retries) {\n retry(attempt, error, null);\n } else {\n reject(error);\n }\n });\n };\n\n function retry(attempt: number, error: any, response: Response | null) {\n let delay = typeof retryDelay === 'function' ? retryDelay(attempt, error, response) : retryDelay;\n setTimeout(function () {\n wrappedFetch(++attempt);\n }, delay);\n }\n\n wrappedFetch(0);\n });\n };\n}\n"],"names":["error"],"mappings":";;AAiBO,SAAS,oBAAqB,CAAA;AAAA,EACnC,KAAA,GAAQ,eAAgB,CAAA,KAAA;AAAA,EACxB,OAAU,GAAA,CAAA;AAAA,EACV,UAAa,GAAA,GAAA;AAAA,EACb,UAAU,EAAC;AACb,CAAA,GAA2B,EAAe,EAAA;AAGxC,EAAO,OAAA,SAAS,UAAW,CAAA,KAAA,EAAyB,IAAoB,EAAA;AACtE,IAAA,O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