@wener/utils 1.1.13 → 1.1.15
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/LICENSE.txt +1 -135
- package/dist/cjs/{globalThis-ee7c1669.js → getGlobalThis-304f74e0.js} +2 -2
- package/dist/cjs/getGlobalThis-304f74e0.js.map +1 -0
- package/dist/cjs/index.cjs +16 -5
- 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/{globalThis-000611c3.js → getGlobalThis-b7ad0cf9.js} +2 -2
- package/dist/esm/getGlobalThis-b7ad0cf9.js.map +1 -0
- package/dist/esm/index.js +15 -4
- 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/getGlobalThis-8951eb0e.js +2 -0
- package/dist/system/getGlobalThis-8951eb0e.js.map +1 -0
- package/dist/system/index.js +16 -5
- 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/asyncs/timeout.js.map +1 -1
- package/lib/browsers/download.js.map +1 -1
- package/lib/browsers/loaders.js.map +1 -1
- package/lib/crypto/getRandomValues.js +2 -1
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/crypto/randomUUID.js +2 -1
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/fetch/createFetchWith.js +25 -0
- package/lib/fetch/createFetchWith.js.map +1 -0
- package/lib/fetch/createFetchWithLogging.js +21 -0
- package/lib/fetch/createFetchWithLogging.js.map +1 -0
- package/lib/fetch/dumpRequest.js +46 -0
- package/lib/fetch/dumpRequest.js.map +1 -0
- package/lib/fetch/dumpResponse.js +23 -0
- package/lib/fetch/dumpResponse.js.map +1 -0
- package/lib/index.js +4 -1
- package/lib/index.js.map +1 -1
- package/lib/io/ArrayBuffers.js +2 -2
- package/lib/io/ArrayBuffers.js.map +1 -1
- package/lib/io/isTransferable.js +2 -1
- package/lib/io/isTransferable.js.map +1 -1
- package/lib/isomorphics/structuredClone.js +2 -1
- package/lib/isomorphics/structuredClone.js.map +1 -1
- package/lib/logging/createLogger.js.map +1 -1
- package/lib/objects/get.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/server.js +3 -1
- package/lib/server.js.map +1 -1
- package/lib/servers/createFetchWithProxy.js +12 -0
- package/lib/servers/createFetchWithProxy.js.map +1 -0
- package/lib/servers/{createProxyFetch.js → createFetchWithProxyByNodeFetch.js} +8 -4
- package/lib/servers/createFetchWithProxyByNodeFetch.js.map +1 -0
- package/lib/servers/createFetchWithProxyByUndici.js +28 -0
- package/lib/servers/createFetchWithProxyByUndici.js.map +1 -0
- package/lib/servers/polyfillCrypto.js +2 -1
- package/lib/servers/polyfillCrypto.js.map +1 -1
- package/lib/servers/polyfillJsDom.js +2 -1
- package/lib/servers/polyfillJsDom.js.map +1 -1
- package/package.json +39 -45
- package/src/asyncs/createLazyPromise.test.ts +21 -21
- package/src/asyncs/timeout.ts +3 -1
- package/src/browsers/download.ts +3 -1
- package/src/browsers/loaders.ts +6 -2
- package/src/crypto/getRandomValues.ts +2 -1
- package/src/crypto/hashing.test.ts +10 -12
- package/src/crypto/pem/__snapshots__/pem.test.ts.snap +18 -0
- package/src/crypto/pem/pem.test.ts +14 -17
- package/src/crypto/randomUUID.ts +3 -1
- package/src/crypto/ulid.test.ts +9 -9
- package/src/fetch/createFetchWith.ts +32 -0
- package/src/fetch/createFetchWithLogging.ts +20 -0
- package/src/fetch/dumpRequest.ts +51 -0
- package/src/fetch/dumpResponse.ts +28 -0
- package/src/fetch/index.ts +4 -0
- package/src/i18n/createTranslate.test.ts +76 -88
- package/src/index.ts +4 -2
- package/src/io/ArrayBuffer.test-d.ts +4 -2
- package/src/io/ArrayBuffers.base64.test.ts +17 -17
- package/src/io/ArrayBuffers.test.ts +8 -8
- package/src/io/ArrayBuffers.ts +2 -2
- package/src/io/Buffer.test.ts +4 -4
- package/src/io/isBuffer.test.ts +4 -4
- package/src/io/isTransferable.test.ts +7 -6
- package/src/io/isTransferable.ts +3 -1
- package/src/isomorphics/structuredClone.test.ts +4 -4
- package/src/isomorphics/structuredClone.ts +3 -1
- package/src/langs/deepEqual.test.ts +4 -4
- package/src/langs/langs.test.ts +3 -3
- package/src/libs/ms.test.ts +311 -0
- package/src/logging/createLogger.ts +15 -5
- package/src/logging/logger.test.ts +8 -8
- package/src/modules/parseModuleId.test.ts +3 -3
- package/src/objects/get.test-d.ts +30 -22
- package/src/objects/get.ts +1 -1
- package/src/objects/parseObjectPath.test.ts +3 -3
- package/src/objects/set.test.ts +98 -117
- package/src/objects/set.ts +1 -1
- package/src/server.ts +3 -1
- package/src/servers/createFetchWithProxy.ts +12 -0
- package/src/servers/{createProxyFetch.ts → createFetchWithProxyByNodeFetch.ts} +7 -3
- package/src/servers/createFetchWithProxyByUndici.ts +36 -0
- package/src/servers/polyfillBrowser.test.ts +9 -9
- package/src/servers/polyfillCrypto.ts +2 -1
- package/src/servers/polyfillJsDom.ts +3 -1
- package/src/servers/polyfillWebSocket.ts +2 -1
- package/src/strings/renderTemplate.test.ts +6 -9
- package/src/validations/parseTimestamp.test.ts +4 -4
- package/dist/cjs/globalThis-ee7c1669.js.map +0 -1
- package/dist/cjs/index-da9513d6.js +0 -13
- package/dist/cjs/index-da9513d6.js.map +0 -1
- package/dist/esm/globalThis-000611c3.js.map +0 -1
- package/dist/esm/index-c696799a.js +0 -13
- package/dist/esm/index-c696799a.js.map +0 -1
- package/dist/system/globalThis-7bba6592.js +0 -2
- package/dist/system/globalThis-7bba6592.js.map +0 -1
- package/dist/system/index-2dfef0f3.js +0 -13
- package/dist/system/index-2dfef0f3.js.map +0 -1
- package/lib/isomorphics/globalThis.js +0 -6
- package/lib/isomorphics/globalThis.js.map +0 -1
- package/lib/servers/createProxyFetch.js.map +0 -1
- package/src/crypto/pem/pem.test.ts.md +0 -24
- package/src/crypto/pem/pem.test.ts.snap +0 -0
- package/src/isomorphics/globalThis.ts +0 -3
- package/src/objects/get.test.ts +0 -63
package/lib/server.js
CHANGED
|
@@ -2,5 +2,7 @@ export { polyfillCrypto } from './servers/polyfillCrypto.js';
|
|
|
2
2
|
export { polyfillFetch } from './servers/polyfillFetch.js';
|
|
3
3
|
export { polyfillJsDom } from './servers/polyfillJsDom.js';
|
|
4
4
|
export { polyfillBrowser } from './servers/polyfillBrowser.js';
|
|
5
|
-
export {
|
|
5
|
+
export { createFetchWithProxyByNodeFetch } from './servers/createFetchWithProxyByNodeFetch.js';
|
|
6
|
+
export { createFetchWithProxyByUndici } from './servers/createFetchWithProxyByUndici.js';
|
|
7
|
+
export { createFetchWithProxy } from './servers/createFetchWithProxy.js';
|
|
6
8
|
//# 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":";;;;;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createFetchWithProxyByNodeFetch } from './createFetchWithProxyByNodeFetch.js';
|
|
2
|
+
import { createFetchWithProxyByUndici } from './createFetchWithProxyByUndici.js';
|
|
3
|
+
|
|
4
|
+
function createFetchWithProxy({ proxy, fetch }) {
|
|
5
|
+
if (!proxy) {
|
|
6
|
+
return fetch || globalThis.fetch;
|
|
7
|
+
}
|
|
8
|
+
return parseInt(process.versions.node) >= 18 ? createFetchWithProxyByUndici({ proxy, fetch }) : createFetchWithProxyByNodeFetch({ proxy, fetch });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { createFetchWithProxy };
|
|
12
|
+
//# sourceMappingURL=createFetchWithProxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createFetchWithProxy.js","sources":["../../src/servers/createFetchWithProxy.ts"],"sourcesContent":["import { FetchLike } from '../fetch';\nimport { createFetchWithProxyByNodeFetch } from '../server';\nimport { createFetchWithProxyByUndici } from './createFetchWithProxyByUndici';\n\nexport function createFetchWithProxy({ proxy, fetch }: { proxy?: string; fetch?: FetchLike }): FetchLike {\n if (!proxy) {\n return fetch || globalThis.fetch;\n }\n return parseInt(process.versions.node) >= 18\n ? createFetchWithProxyByUndici({ proxy, fetch })\n : createFetchWithProxyByNodeFetch({ proxy, fetch });\n}\n"],"names":[],"mappings":";;;AAIO,SAAS,oBAAqB,CAAA,EAAE,KAAO,EAAA,KAAA,EAA2D,EAAA;AACvG,EAAA,IAAI,CAAC,KAAO,EAAA;AACV,IAAA,OAAO,SAAS,UAAW,CAAA,KAAA,CAAA;AAAA,GAC7B;AACA,EAAA,OAAO,SAAS,OAAQ,CAAA,QAAA,CAAS,IAAI,CAAA,IAAK,KACtC,4BAA6B,CAAA,EAAE,KAAO,EAAA,KAAA,EAAO,CAC7C,GAAA,+BAAA,CAAgC,EAAE,KAAA,EAAO,OAAO,CAAA,CAAA;AACtD;;;;"}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getGlobalThis } from '../isomorphics/getGlobalThis.js';
|
|
2
2
|
|
|
3
|
-
function
|
|
3
|
+
function createFetchWithProxyByNodeFetch({
|
|
4
|
+
proxy,
|
|
5
|
+
fetch
|
|
6
|
+
} = {}) {
|
|
7
|
+
const globalThis = getGlobalThis();
|
|
4
8
|
if (!proxy) {
|
|
5
9
|
return fetch || globalThis.fetch;
|
|
6
10
|
}
|
|
@@ -30,5 +34,5 @@ function createProxyFetch(proxy, fetch) {
|
|
|
30
34
|
};
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
export {
|
|
34
|
-
//# sourceMappingURL=
|
|
37
|
+
export { createFetchWithProxyByNodeFetch };
|
|
38
|
+
//# sourceMappingURL=createFetchWithProxyByNodeFetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createFetchWithProxyByNodeFetch.js","sources":["../../src/servers/createFetchWithProxyByNodeFetch.ts"],"sourcesContent":["import { type FetchLike } from '../fetch';\nimport { getGlobalThis } from '../isomorphics/getGlobalThis';\n\nexport function createFetchWithProxyByNodeFetch({\n proxy,\n fetch,\n}: { proxy?: string; fetch?: FetchLike } = {}): FetchLike {\n const globalThis = getGlobalThis();\n if (!proxy) {\n return fetch || globalThis.fetch;\n }\n\n let agent: any;\n const Request = globalThis.Request;\n let NodeRequest: any;\n let NodeFetch: any;\n return async (url, init?: RequestInit) => {\n if (!agent) {\n const { default: createHttpsProxyAgent } = await import('https-proxy-agent');\n agent = (createHttpsProxyAgent as any)(proxy);\n }\n\n // node-fetch 才可以,node v18 fetch 不支持\n if (!NodeRequest) {\n ({ Request: NodeRequest, default: NodeFetch } = await import('node-fetch'));\n }\n\n fetch ||= NodeFetch;\n\n if (url instanceof Request) {\n return (fetch as any)(new Request(url, { agent } as any));\n }\n if ((url as any) instanceof NodeRequest) {\n return (fetch as any)(new NodeRequest(url, { agent } as any));\n }\n return (fetch as any)(url, {\n ...init,\n agent,\n } as any);\n };\n}\n"],"names":[],"mappings":";;AAGO,SAAS,+BAAgC,CAAA;AAAA,EAC9C,KAAA;AAAA,EACA,KAAA;AACF,CAAA,GAA2C,EAAe,EAAA;AACxD,EAAA,MAAM,aAAa,aAAc,EAAA,CAAA;AACjC,EAAA,IAAI,CAAC,KAAO,EAAA;AACV,IAAA,OAAO,SAAS,UAAW,CAAA,KAAA,CAAA;AAAA,GAC7B;AAEA,EAAI,IAAA,KAAA,CAAA;AACJ,EAAA,MAAM,UAAU,UAAW,CAAA,OAAA,CAAA;AAC3B,EAAI,IAAA,WAAA,CAAA;AACJ,EAAI,IAAA,SAAA,CAAA;AACJ,EAAO,OAAA,OAAO,KAAK,IAAuB,KAAA;AACxC,IAAA,IAAI,CAAC,KAAO,EAAA;AACV,MAAA,MAAM,EAAE,OAAS,EAAA,qBAAA,EAA0B,GAAA,MAAM,OAAO,mBAAmB,CAAA,CAAA;AAC3E,MAAA,KAAA,GAAS,sBAA8B,KAAK,CAAA,CAAA;AAAA,KAC9C;AAGA,IAAA,IAAI,CAAC,WAAa,EAAA;AAChB,MAAC,CAAA,EAAE,SAAS,WAAa,EAAA,OAAA,EAAS,WAAc,GAAA,MAAM,OAAO,YAAY,CAAA,EAAA;AAAA,KAC3E;AAEA,IAAU,KAAA,KAAA,KAAA,GAAA,SAAA,CAAA,CAAA;AAEV,IAAA,IAAI,eAAe,OAAS,EAAA;AAC1B,MAAA,OAAQ,MAAc,IAAI,OAAA,CAAQ,KAAK,EAAE,KAAA,EAAc,CAAC,CAAA,CAAA;AAAA,KAC1D;AACA,IAAA,IAAK,eAAuB,WAAa,EAAA;AACvC,MAAA,OAAQ,MAAc,IAAI,WAAA,CAAY,KAAK,EAAE,KAAA,EAAc,CAAC,CAAA,CAAA;AAAA,KAC9D;AACA,IAAA,OAAQ,MAAc,GAAK,EAAA;AAAA,MACzB,GAAG,IAAA;AAAA,MACH,KAAA;AAAA,KACM,CAAA,CAAA;AAAA,GACV,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function createFetchWithProxyByUndici({
|
|
2
|
+
proxy,
|
|
3
|
+
fetch,
|
|
4
|
+
undici
|
|
5
|
+
} = {}) {
|
|
6
|
+
if (!proxy) {
|
|
7
|
+
return fetch || globalThis.fetch;
|
|
8
|
+
}
|
|
9
|
+
let agent;
|
|
10
|
+
return async (...args) => {
|
|
11
|
+
const init = args[1] || (args[1] = {});
|
|
12
|
+
if (init.body instanceof ReadableStream) {
|
|
13
|
+
init.duplex || (init.duplex = "half");
|
|
14
|
+
}
|
|
15
|
+
if (!agent) {
|
|
16
|
+
undici || (undici = import('undici'));
|
|
17
|
+
const mod = await undici;
|
|
18
|
+
const ProxyAgent = mod.ProxyAgent;
|
|
19
|
+
fetch || (fetch = mod.fetch);
|
|
20
|
+
agent = new ProxyAgent(proxy);
|
|
21
|
+
}
|
|
22
|
+
init.dispatcher = agent;
|
|
23
|
+
return await fetch(...args);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { createFetchWithProxyByUndici };
|
|
28
|
+
//# sourceMappingURL=createFetchWithProxyByUndici.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createFetchWithProxyByUndici.js","sources":["../../src/servers/createFetchWithProxyByUndici.ts"],"sourcesContent":["import { MaybePromise } from 'rollup';\nimport { FetchLike } from '../fetch';\n\nexport function createFetchWithProxyByUndici({\n proxy,\n fetch,\n undici,\n}: {\n proxy?: 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 return async (...args) => {\n const init: RequestInit & { duplex?: string; dispatcher?: any } = (args[1] ||= {});\n if (init.body instanceof ReadableStream) {\n // https://github.com/nodejs/node/issues/46221\n init.duplex ||= 'half';\n }\n if (!agent) {\n // if in next use 'next/dist/compiled/undici'\n undici ||= import('undici');\n const mod = await undici;\n const ProxyAgent = mod.ProxyAgent;\n fetch ||= mod.fetch;\n agent = new ProxyAgent(proxy);\n // https://github.com/nodejs/node/issues/43187#issuecomment-1134634174\n // (global as any)[Symbol.for('undici.globalDispatcher.1')] = agent;\n }\n init.dispatcher = agent;\n return await fetch!(...args);\n };\n}\n"],"names":[],"mappings":"AAGO,SAAS,4BAA6B,CAAA;AAAA,EAC3C,KAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AACF,CAAA,GAII,EAAe,EAAA;AACjB,EAAA,IAAI,CAAC,KAAO,EAAA;AACV,IAAA,OAAO,SAAS,UAAW,CAAA,KAAA,CAAA;AAAA,GAC7B;AACA,EAAI,IAAA,KAAA,CAAA;AACJ,EAAA,OAAO,UAAU,IAAS,KAAA;AACxB,IAAM,MAAA,IAAA,GAA6D,sBAAY,EAAC,CAAA,CAAA;AAChF,IAAI,IAAA,IAAA,CAAK,gBAAgB,cAAgB,EAAA;AAEvC,MAAK,IAAA,CAAA,MAAA,KAAL,KAAK,MAAW,GAAA,MAAA,CAAA,CAAA;AAAA,KAClB;AACA,IAAA,IAAI,CAAC,KAAO,EAAA;AAEV,MAAA,MAAA,KAAA,MAAA,GAAW,OAAO,QAAQ,CAAA,CAAA,CAAA;AAC1B,MAAA,MAAM,MAAM,MAAM,MAAA,CAAA;AAClB,MAAA,MAAM,aAAa,GAAI,CAAA,UAAA,CAAA;AACvB,MAAA,KAAA,KAAA,KAAA,GAAU,GAAI,CAAA,KAAA,CAAA,CAAA;AACd,MAAQ,KAAA,GAAA,IAAI,WAAW,KAAK,CAAA,CAAA;AAAA,KAG9B;AACA,IAAA,IAAA,CAAK,UAAa,GAAA,KAAA,CAAA;AAClB,IAAO,OAAA,MAAM,KAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAAA,GAC7B,CAAA;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polyfillCrypto.js","sources":["../../src/servers/polyfillCrypto.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"polyfillCrypto.js","sources":["../../src/servers/polyfillCrypto.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"],"names":[],"mappings":";;AAEA,eAAsB,cAAiB,GAAA;AACrC,EAAA,MAAM,aAAa,aAAc,EAAA,CAAA;AACjC,EAAA,IAAI,YAAY,UAAY,EAAA;AAC1B,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACA,EAAC,UAAmB,CAAA,MAAA,GAAA,CAAU,MAAM,OAAO,aAAa,CAAG,EAAA,SAAA,CAAA;AAC3D,EAAO,OAAA,IAAA,CAAA;AACT;;;;"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getGlobalThis } from '../isomorphics/getGlobalThis.js';
|
|
2
2
|
|
|
3
3
|
async function polyfillJsDom() {
|
|
4
4
|
if (typeof window !== "undefined") {
|
|
5
5
|
return false;
|
|
6
6
|
}
|
|
7
|
+
const globalThis = getGlobalThis();
|
|
7
8
|
const { ResourceLoader, JSDOM } = await import('jsdom');
|
|
8
9
|
class Window {
|
|
9
10
|
constructor(opts = {}) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polyfillJsDom.js","sources":["../../src/servers/polyfillJsDom.ts"],"sourcesContent":["import type { ConstructorOptions, ResourceLoaderConstructorOptions } from 'jsdom';\nimport {
|
|
1
|
+
{"version":3,"file":"polyfillJsDom.js","sources":["../../src/servers/polyfillJsDom.ts"],"sourcesContent":["import type { ConstructorOptions, ResourceLoaderConstructorOptions } from 'jsdom';\nimport { getGlobalThis } from '../isomorphics/getGlobalThis';\n\nexport async function polyfillJsDom() {\n if (typeof window !== 'undefined') {\n return false;\n }\n\n const globalThis = getGlobalThis();\n\n const { ResourceLoader, JSDOM } = await import('jsdom');\n\n // https://github.com/lukechilds/window/blob/master/src/index.js\n // eslint-disable-next-line @typescript-eslint/no-extraneous-class\n class Window {\n constructor(opts: ResourceLoaderConstructorOptions & ConstructorOptions = {}) {\n const { proxy, strictSSL, userAgent, ...jsdomOpts } = opts;\n const resources = new ResourceLoader({\n proxy,\n strictSSL,\n userAgent,\n });\n return new JSDOM(\n '',\n Object.assign(jsdomOpts, {\n resources,\n }),\n ).window;\n }\n }\n\n // https://github.com/lukechilds/browser-env/blob/master/src/index.js\n // Default jsdom config.\n // These settings must override any custom settings to make sure we can iterate\n // over the window object.\n const defaultJsdomConfig = {\n // features: {\n // FetchExternalResources: false,\n // ProcessExternalResources: false,\n // },\n };\n // IIFE executed on import to return an array of global Node.js properties that\n // conflict with global browser properties.\n const protectedProperties = (() =>\n Object.getOwnPropertyNames(new Window(defaultJsdomConfig)).filter(\n (prop) => typeof globalThis[prop as keyof typeof globalThis] !== 'undefined',\n ))();\n\n function installEnv(...args: any[]) {\n // Sets up global browser environment\n // Extract options from args\n const properties = args.filter((arg: any) => Array.isArray(arg))[0];\n const userJsdomConfig = args.filter((arg: any) => !Array.isArray(arg))[0];\n\n // Create window object\n const window = new Window(Object.assign({}, userJsdomConfig, defaultJsdomConfig));\n\n // Get all global browser properties\n Object.getOwnPropertyNames(window)\n\n // Remove protected properties\n .filter((prop) => !protectedProperties.includes(prop))\n\n // If we're only applying specific required properties remove everything else\n .filter((prop) => !(properties && properties.indexOf(prop) === -1))\n .filter((prop) => {\n switch (prop) {\n case 'undefined':\n return false;\n }\n return true;\n })\n\n // Copy what's left to the Node.js global scope\n .forEach((prop) => {\n // console.debug(`define globalThis.${prop}`);\n Object.defineProperty(globalThis, prop, {\n configurable: true,\n get: () => window[prop as keyof Window] as any,\n });\n });\n\n return window;\n }\n\n installEnv({ url: 'http://localhost' });\n return true;\n}\n"],"names":["window"],"mappings":";;AAGA,eAAsB,aAAgB,GAAA;AACpC,EAAI,IAAA,OAAO,WAAW,WAAa,EAAA;AACjC,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAA,MAAM,aAAa,aAAc,EAAA,CAAA;AAEjC,EAAA,MAAM,EAAE,cAAgB,EAAA,KAAA,EAAU,GAAA,MAAM,OAAO,OAAO,CAAA,CAAA;AAItD,EAAA,MAAM,MAAO,CAAA;AAAA,IACX,WAAA,CAAY,IAA8D,GAAA,EAAI,EAAA;AAC5E,MAAA,MAAM,EAAE,KAAO,EAAA,SAAA,EAAW,SAAW,EAAA,GAAG,WAAc,GAAA,IAAA,CAAA;AACtD,MAAM,MAAA,SAAA,GAAY,IAAI,cAAe,CAAA;AAAA,QACnC,KAAA;AAAA,QACA,SAAA;AAAA,QACA,SAAA;AAAA,OACD,CAAA,CAAA;AACD,MAAA,OAAO,IAAI,KAAA;AAAA,QACT,EAAA;AAAA,QACA,MAAA,CAAO,OAAO,SAAW,EAAA;AAAA,UACvB,SAAA;AAAA,SACD,CAAA;AAAA,OACD,CAAA,MAAA,CAAA;AAAA,KACJ;AAAA,GACF;AAMA,EAAA,MAAM,kBAAqB,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA,GAK3B,CAAA;AAGA,EAAM,MAAA,mBAAA,GAAA,CAAuB,MAC3B,MAAO,CAAA,mBAAA,CAAoB,IAAI,MAAO,CAAA,kBAAkB,CAAC,CAAE,CAAA,MAAA;AAAA,IACzD,CAAC,IAAA,KAAS,OAAO,UAAA,CAAW,IAA+B,CAAM,KAAA,WAAA;AAAA,GAChE,GAAA,CAAA;AAEL,EAAA,SAAS,cAAc,IAAa,EAAA;AAGlC,IAAM,MAAA,UAAA,GAAa,IAAK,CAAA,MAAA,CAAO,CAAC,GAAA,KAAa,MAAM,OAAQ,CAAA,GAAG,CAAC,CAAA,CAAE,CAAC,CAAA,CAAA;AAClE,IAAM,MAAA,eAAA,GAAkB,IAAK,CAAA,MAAA,CAAO,CAAC,GAAA,KAAa,CAAC,KAAA,CAAM,OAAQ,CAAA,GAAG,CAAC,CAAA,CAAE,CAAC,CAAA,CAAA;AAGxE,IAAMA,MAAAA,OAAAA,GAAS,IAAI,MAAO,CAAA,MAAA,CAAO,OAAO,EAAC,EAAG,eAAiB,EAAA,kBAAkB,CAAC,CAAA,CAAA;AAGhF,IAAO,MAAA,CAAA,mBAAA,CAAoBA,OAAM,CAAA,CAG9B,MAAO,CAAA,CAAC,SAAS,CAAC,mBAAA,CAAoB,QAAS,CAAA,IAAI,CAAC,CAAA,CAGpD,OAAO,CAAC,IAAA,KAAS,EAAE,UAAA,IAAc,UAAW,CAAA,OAAA,CAAQ,IAAI,CAAA,KAAM,CAAG,CAAA,CAAA,CAAA,CACjE,MAAO,CAAA,CAAC,IAAS,KAAA;AAChB,MAAA,QAAQ,IAAM;AAAA,QACZ,KAAK,WAAA;AACH,UAAO,OAAA,KAAA,CAAA;AAAA,OACX;AACA,MAAO,OAAA,IAAA,CAAA;AAAA,KACR,CAAA,CAGA,OAAQ,CAAA,CAAC,IAAS,KAAA;AAEjB,MAAO,MAAA,CAAA,cAAA,CAAe,YAAY,IAAM,EAAA;AAAA,QACtC,YAAc,EAAA,IAAA;AAAA,QACd,GAAA,EAAK,MAAMA,OAAAA,CAAO,IAAoB,CAAA;AAAA,OACvC,CAAA,CAAA;AAAA,KACF,CAAA,CAAA;AAEH,IAAOA,OAAAA,OAAAA,CAAAA;AAAA,GACT;AAEA,EAAW,UAAA,CAAA,EAAE,GAAK,EAAA,kBAAA,EAAoB,CAAA,CAAA;AACtC,EAAO,OAAA,IAAA,CAAA;AACT;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wener/utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Utils for daily use",
|
|
6
6
|
"repository": {
|
|
@@ -12,31 +12,6 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"main": "dist/cjs/index.cjs",
|
|
14
14
|
"module": "lib/index.js",
|
|
15
|
-
"exports": {
|
|
16
|
-
".": {
|
|
17
|
-
"types": "./src/index.ts",
|
|
18
|
-
"typescript": "./src/index.ts",
|
|
19
|
-
"import": "./lib/index.js",
|
|
20
|
-
"system": {
|
|
21
|
-
"default": "./dist/system/index.js"
|
|
22
|
-
},
|
|
23
|
-
"require": "./dist/cjs/index.cjs"
|
|
24
|
-
},
|
|
25
|
-
"./types": {
|
|
26
|
-
"types": "./types.d.ts"
|
|
27
|
-
},
|
|
28
|
-
"./server": {
|
|
29
|
-
"types": "./src/server.ts",
|
|
30
|
-
"typescript": "./src/server.ts",
|
|
31
|
-
"import": "./lib/server.js",
|
|
32
|
-
"system": {
|
|
33
|
-
"default": "./dist/system/server.js"
|
|
34
|
-
},
|
|
35
|
-
"require": "./dist/cjs/server.cjs"
|
|
36
|
-
},
|
|
37
|
-
"./src/*": "./src/*",
|
|
38
|
-
"./package.json": "./package.json"
|
|
39
|
-
},
|
|
40
15
|
"types": "src/index.ts",
|
|
41
16
|
"files": [
|
|
42
17
|
"dist",
|
|
@@ -56,30 +31,34 @@
|
|
|
56
31
|
"template",
|
|
57
32
|
"lodash"
|
|
58
33
|
],
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"https-proxy-agent": "*",
|
|
36
|
+
"undici": "*"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"https-proxy-agent": {
|
|
40
|
+
"optional": true
|
|
41
|
+
},
|
|
42
|
+
"node-fetch": {
|
|
43
|
+
"optional": true
|
|
44
|
+
},
|
|
45
|
+
"undici": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
48
|
+
},
|
|
59
49
|
"devDependencies": {
|
|
60
|
-
"@types/lodash": "^4.14.
|
|
50
|
+
"@types/lodash": "^4.14.195",
|
|
61
51
|
"@types/ws": "^8.5.4",
|
|
62
|
-
"
|
|
63
|
-
"https-proxy-agent": "^5.0.1",
|
|
52
|
+
"https-proxy-agent": "^7.0.0",
|
|
64
53
|
"lodash": "^4.17.21",
|
|
65
|
-
"
|
|
54
|
+
"node-fetch": "^3.3.1",
|
|
55
|
+
"undici": "^5.22.1"
|
|
66
56
|
},
|
|
67
57
|
"publishConfig": {
|
|
68
58
|
"registry": "https://registry.npmjs.org",
|
|
69
59
|
"access": "public"
|
|
70
60
|
},
|
|
71
61
|
"sideEffects": false,
|
|
72
|
-
"ava": {
|
|
73
|
-
"extensions": {
|
|
74
|
-
"ts": "module"
|
|
75
|
-
},
|
|
76
|
-
"nodeArguments": [
|
|
77
|
-
"--conditions=typescript",
|
|
78
|
-
"--experimental-import-meta-resolve",
|
|
79
|
-
"--require=@wener/wode/suppress-experimental.cjs",
|
|
80
|
-
"--loader=tsx"
|
|
81
|
-
]
|
|
82
|
-
},
|
|
83
62
|
"rollup": {
|
|
84
63
|
"input": [
|
|
85
64
|
"./src/index.ts",
|
|
@@ -90,14 +69,29 @@
|
|
|
90
69
|
"node-fetch"
|
|
91
70
|
]
|
|
92
71
|
},
|
|
93
|
-
"typedoc": {
|
|
94
|
-
"entryPoint": "./src/typedoc.ts",
|
|
95
|
-
"readmeFile": "./README.md"
|
|
96
|
-
},
|
|
97
72
|
"scripts": {
|
|
98
73
|
"build": "make prepublish",
|
|
99
74
|
"clean": "make clean",
|
|
100
75
|
"dev": "make dev",
|
|
101
76
|
"test": "make test"
|
|
77
|
+
},
|
|
78
|
+
"exports": {
|
|
79
|
+
".": {
|
|
80
|
+
"types": "./src/index.ts",
|
|
81
|
+
"typescript": "./src/index.ts",
|
|
82
|
+
"system": "./dist/system/index.js",
|
|
83
|
+
"import": "./lib/index.js"
|
|
84
|
+
},
|
|
85
|
+
"./types": {
|
|
86
|
+
"types": "./types.d.ts"
|
|
87
|
+
},
|
|
88
|
+
"./server": {
|
|
89
|
+
"types": "./src/server.ts",
|
|
90
|
+
"typescript": "./src/server.ts",
|
|
91
|
+
"system": "./dist/system/server.js",
|
|
92
|
+
"import": "./lib/server.js"
|
|
93
|
+
},
|
|
94
|
+
"./src/*": "./src/*",
|
|
95
|
+
"./package.json": "./package.json"
|
|
102
96
|
}
|
|
103
97
|
}
|
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, expect } from 'vitest';
|
|
2
2
|
import { createLazyPromise } from './createLazyPromise';
|
|
3
3
|
import { sleep } from './sleep';
|
|
4
4
|
|
|
5
|
-
test('basic', async (
|
|
5
|
+
test('basic', async () => {
|
|
6
6
|
const promise = createLazyPromise();
|
|
7
7
|
let r = 0;
|
|
8
8
|
void promise.then((v) => (r = v));
|
|
9
|
-
|
|
9
|
+
expect(r).toBe(0);
|
|
10
10
|
const { resolve, reject } = promise;
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
expect(resolve).toBeTruthy();
|
|
12
|
+
expect(reject).toBeTruthy();
|
|
13
13
|
resolve(1);
|
|
14
|
-
|
|
14
|
+
expect(r).toBe(0);
|
|
15
15
|
await promise;
|
|
16
|
-
|
|
16
|
+
expect(r).toBe(1);
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
test('manual resolve skip exec', async (
|
|
19
|
+
test('manual resolve skip exec', async () => {
|
|
20
20
|
const promise = createLazyPromise<number>(() => {
|
|
21
|
-
|
|
21
|
+
expect.fail();
|
|
22
22
|
});
|
|
23
23
|
promise.resolve(-1);
|
|
24
|
-
|
|
24
|
+
expect(await promise).toBe(-1);
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
-
test('exec', async (
|
|
27
|
+
test('exec', async () => {
|
|
28
28
|
let r = 0;
|
|
29
29
|
const promise = createLazyPromise((resolve) => {
|
|
30
30
|
r++;
|
|
31
31
|
resolve(10);
|
|
32
32
|
});
|
|
33
33
|
await sleep(10);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
expect(r).toBe(0);
|
|
35
|
+
expect(await promise).toBe(10);
|
|
36
|
+
expect(r).toBe(1);
|
|
37
37
|
promise.resolve(20);
|
|
38
|
-
|
|
38
|
+
expect(await promise).toBe(10);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
-
test('exec auto', async (
|
|
41
|
+
test('exec auto', async () => {
|
|
42
42
|
let r = 0;
|
|
43
43
|
const promise = createLazyPromise(() => {
|
|
44
44
|
r++;
|
|
45
45
|
return 10;
|
|
46
46
|
});
|
|
47
47
|
await sleep(10);
|
|
48
|
-
|
|
48
|
+
expect(r).toBe(0);
|
|
49
49
|
void promise.then(() => undefined);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
expect(await promise).toBe(10);
|
|
51
|
+
expect(r).toBe(1);
|
|
52
|
+
expect(await promise).toBe(10);
|
|
53
|
+
expect(r).toBe(1);
|
|
54
54
|
});
|
package/src/asyncs/timeout.ts
CHANGED
|
@@ -4,7 +4,9 @@ export function timeout<T = any>(v: Promise<T>, ms: number): Promise<T> {
|
|
|
4
4
|
return Promise.race([
|
|
5
5
|
v,
|
|
6
6
|
new Promise((_resolve, reject) => {
|
|
7
|
-
timeout = setTimeout(() => {
|
|
7
|
+
timeout = setTimeout(() => {
|
|
8
|
+
reject(error);
|
|
9
|
+
}, ms);
|
|
8
10
|
}),
|
|
9
11
|
]).then(
|
|
10
12
|
(v) => {
|
package/src/browsers/download.ts
CHANGED
|
@@ -26,7 +26,9 @@ export function download(filename: string, data: any, { type = 'application/octe
|
|
|
26
26
|
|
|
27
27
|
if (data instanceof File || data instanceof Blob || data instanceof MediaSource) {
|
|
28
28
|
a.href = URL.createObjectURL(data);
|
|
29
|
-
closer = () => {
|
|
29
|
+
closer = () => {
|
|
30
|
+
URL.revokeObjectURL(a.href);
|
|
31
|
+
};
|
|
30
32
|
} else {
|
|
31
33
|
console.error(`invalid download data`, data);
|
|
32
34
|
throw new Error(`can not download ${Object.getPrototypeOf(data)}`);
|
package/src/browsers/loaders.ts
CHANGED
|
@@ -4,13 +4,17 @@ function load(
|
|
|
4
4
|
reject: (v: any) => void,
|
|
5
5
|
options: { attributes: Record<string, string> } | undefined,
|
|
6
6
|
) {
|
|
7
|
-
el.onload = () => {
|
|
7
|
+
el.onload = () => {
|
|
8
|
+
resolve(el);
|
|
9
|
+
};
|
|
8
10
|
el.onerror = (e) => {
|
|
9
11
|
el.remove();
|
|
10
12
|
reject(e);
|
|
11
13
|
};
|
|
12
14
|
const { attributes = {} } = options || {};
|
|
13
|
-
Object.entries(attributes).forEach(([k, v]) => {
|
|
15
|
+
Object.entries(attributes).forEach(([k, v]) => {
|
|
16
|
+
el.setAttribute(k, v);
|
|
17
|
+
});
|
|
14
18
|
|
|
15
19
|
document.head.appendChild(el);
|
|
16
20
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
|
2
2
|
import type { TypedArray } from '../io/ArrayBuffers';
|
|
3
|
-
import {
|
|
3
|
+
import { getGlobalThis } from '../isomorphics/getGlobalThis';
|
|
4
4
|
import { getNodeCrypto } from './getNodeCrypto';
|
|
5
5
|
|
|
6
|
+
const globalThis = getGlobalThis();
|
|
6
7
|
export let getRandomValues: <T extends Exclude<TypedArray, Float32Array | Float64Array>>(typedArray: T) => T =
|
|
7
8
|
globalThis.crypto?.getRandomValues?.bind(globalThis.crypto) ||
|
|
8
9
|
(globalThis as any).msCrypto?.getRandomValues?.bind((globalThis as any).msCrypto) ||
|
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, beforeAll, expect } from 'vitest';
|
|
2
2
|
import { polyfillCrypto } from '../servers/polyfillCrypto';
|
|
3
3
|
import { isUUID } from '../validations/isUUID';
|
|
4
4
|
import { hex } from './base';
|
|
5
5
|
import { sha1, sha256, sha384, sha512 } from './hashing';
|
|
6
6
|
import { _randomUUID } from './randomUUID';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
beforeAll(async () => {
|
|
9
9
|
await polyfillCrypto();
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
-
test('sha', async (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
hex(await sha384('')),
|
|
12
|
+
test('sha', async () => {
|
|
13
|
+
expect(hex(await sha1(''))).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709');
|
|
14
|
+
expect(hex(await sha1('abc'))).toBe('a9993e364706816aba3e25717850c26c9cd0d89d');
|
|
15
|
+
expect(hex(await sha256(''))).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
|
|
16
|
+
expect(hex(await sha384(''))).toBe(
|
|
18
17
|
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b',
|
|
19
18
|
);
|
|
20
|
-
|
|
21
|
-
hex(await sha512('')),
|
|
19
|
+
expect(hex(await sha512(''))).toBe(
|
|
22
20
|
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
|
|
23
21
|
);
|
|
24
22
|
});
|
|
25
23
|
|
|
26
|
-
test('randomUUID', (
|
|
24
|
+
test('randomUUID', () => {
|
|
27
25
|
for (let i = 0; i < 20; i++) {
|
|
28
|
-
|
|
26
|
+
expect(isUUID(_randomUUID())).toBeTruthy();
|
|
29
27
|
}
|
|
30
28
|
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`cases 1`] = `
|
|
4
|
+
"-----BEGIN RSA PRIVATE KEY-----
|
|
5
|
+
SGVsbG8=
|
|
6
|
+
-----END RSA PRIVATE KEY-----
|
|
7
|
+
"
|
|
8
|
+
`;
|
|
9
|
+
|
|
10
|
+
exports[`cases 2`] = `
|
|
11
|
+
"-----BEGIN RSA PRIVATE KEY-----
|
|
12
|
+
Name: N-:1/=
|
|
13
|
+
Age: 1234
|
|
14
|
+
|
|
15
|
+
TmljZQ==
|
|
16
|
+
-----END RSA PRIVATE KEY-----
|
|
17
|
+
"
|
|
18
|
+
`;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
2
|
import { ArrayBuffers } from '../../io/ArrayBuffers';
|
|
3
3
|
import { PEM } from './pem';
|
|
4
4
|
|
|
5
5
|
const { decode, encode } = PEM;
|
|
6
6
|
|
|
7
|
-
test('pem', (
|
|
7
|
+
test('pem', () => {
|
|
8
8
|
const pem = `-----BEGIN RSA PRIVATE KEY-----
|
|
9
9
|
Proc-Type: 4,ENCRYPTED
|
|
10
10
|
DEK-Info: DES-EDE3-CBC,ABC
|
|
@@ -27,10 +27,10 @@ MDAwMDAwMDAwMDAwMDAwMA==
|
|
|
27
27
|
head: 'Nice\n',
|
|
28
28
|
tail: 'Hello\n',
|
|
29
29
|
};
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
expect(decode(input)).toEqual(out);
|
|
31
|
+
expect(encode(out.block)).toBe(pem);
|
|
32
32
|
});
|
|
33
|
-
test('cases', (
|
|
33
|
+
test('cases', () => {
|
|
34
34
|
for (const b of [
|
|
35
35
|
{
|
|
36
36
|
type: 'RSA PRIVATE KEY',
|
|
@@ -47,17 +47,14 @@ test('cases', (t) => {
|
|
|
47
47
|
]) {
|
|
48
48
|
const s = encode(b);
|
|
49
49
|
const act = decode(s);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
},
|
|
60
|
-
);
|
|
61
|
-
t.snapshot(s);
|
|
50
|
+
expect({
|
|
51
|
+
...act.block,
|
|
52
|
+
bytes: ArrayBuffers.toString(act.block.bytes),
|
|
53
|
+
}).toStrictEqual({
|
|
54
|
+
header: {},
|
|
55
|
+
...b,
|
|
56
|
+
bytes: ArrayBuffers.toString(b.bytes),
|
|
57
|
+
});
|
|
58
|
+
expect(s).matchSnapshot();
|
|
62
59
|
}
|
|
63
60
|
});
|
package/src/crypto/randomUUID.ts
CHANGED
package/src/crypto/ulid.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
2
|
import { createULID, isULID, parseULID } from './ulid';
|
|
3
3
|
|
|
4
|
-
test('ulid', (
|
|
4
|
+
test('ulid', () => {
|
|
5
5
|
// monotonic
|
|
6
6
|
{
|
|
7
7
|
let lastTime = 0;
|
|
@@ -13,18 +13,18 @@ test('ulid', (t) => {
|
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
const ulid1 = ulid();
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
expect(parseULID(ulid1).timestamp).toBe(lastTime);
|
|
17
|
+
expect(isULID(ulid1), ulid1).toBeTruthy();
|
|
18
18
|
|
|
19
19
|
const ulid2 = ulid();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
expect(ulid1 < ulid2).toBeTruthy();
|
|
21
|
+
expect(isULID(ulid2), ulid2).toBeTruthy();
|
|
22
|
+
expect(parseULID(ulid2).timestamp).toBe(lastTime);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
{
|
|
26
26
|
const next = createULID();
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
expect(isULID(next().toLowerCase())).toBeTruthy();
|
|
28
|
+
expect(isULID('ttttttttttrrrrrrrrrrrrrrrr')).toBeTruthy();
|
|
29
29
|
}
|
|
30
30
|
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { MaybePromise } from '../asyncs/MaybePromise';
|
|
2
|
+
import { getGlobalThis } from '../isomorphics/getGlobalThis';
|
|
3
|
+
import { FetchLike } from './index';
|
|
4
|
+
|
|
5
|
+
export function createFetchWith({
|
|
6
|
+
fetch = getGlobalThis().fetch,
|
|
7
|
+
onRequest = (ctx) => ctx.next(ctx.url, ctx.req),
|
|
8
|
+
onResponse = (ctx) => ctx.res,
|
|
9
|
+
}: {
|
|
10
|
+
fetch?: FetchLike;
|
|
11
|
+
onRequest?: (ctx: {
|
|
12
|
+
url: string;
|
|
13
|
+
req: RequestInit;
|
|
14
|
+
next: (url: string, req: RequestInit) => Promise<Response>;
|
|
15
|
+
}) => MaybePromise<Response | void>;
|
|
16
|
+
onResponse?: (ctx: { url: string; req: RequestInit; res: Response }) => MaybePromise<Response>;
|
|
17
|
+
}): FetchLike {
|
|
18
|
+
return async (urlOrRequest, init) => {
|
|
19
|
+
const url = String(urlOrRequest);
|
|
20
|
+
let req: RequestInit = init || {};
|
|
21
|
+
const res =
|
|
22
|
+
(await onRequest({
|
|
23
|
+
url,
|
|
24
|
+
req,
|
|
25
|
+
next: (url, init) => {
|
|
26
|
+
req = init;
|
|
27
|
+
return fetch(url, init);
|
|
28
|
+
},
|
|
29
|
+
})) ?? (await fetch(url, init));
|
|
30
|
+
return onResponse({ url, req, res });
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { dumpRequest } from './dumpRequest';
|
|
2
|
+
import { createFetchWith, dumpResponse, FetchLike } from './index';
|
|
3
|
+
|
|
4
|
+
export function createFetchWithLogging({
|
|
5
|
+
fetch,
|
|
6
|
+
log = console.log,
|
|
7
|
+
}: {
|
|
8
|
+
fetch?: FetchLike;
|
|
9
|
+
log?: (s: string) => void;
|
|
10
|
+
} = {}): FetchLike {
|
|
11
|
+
return createFetchWith({
|
|
12
|
+
fetch,
|
|
13
|
+
onRequest: ({ url, req }) => {
|
|
14
|
+
void dumpRequest({ url, req, log });
|
|
15
|
+
},
|
|
16
|
+
onResponse: ({ url, req, res }) => {
|
|
17
|
+
return dumpResponse({ url, req, res, log });
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|