@wener/utils 1.1.22 → 1.1.24

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.
Files changed (46) hide show
  1. package/dist/cjs/index.cjs +6 -6
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/servers/jsdom.cjs +2 -0
  4. package/dist/cjs/servers/jsdom.cjs.map +1 -0
  5. package/dist/cjs/servers/node-fetch.cjs +2 -0
  6. package/dist/cjs/servers/node-fetch.cjs.map +1 -0
  7. package/dist/cjs/servers/ws.cjs +2 -0
  8. package/dist/cjs/servers/ws.cjs.map +1 -0
  9. package/dist/esm/index.js +12 -12
  10. package/dist/esm/index.js.map +1 -1
  11. package/dist/esm/servers/jsdom.js +2 -0
  12. package/dist/esm/servers/jsdom.js.map +1 -0
  13. package/dist/esm/servers/node-fetch.js +2 -0
  14. package/dist/esm/servers/node-fetch.js.map +1 -0
  15. package/dist/esm/servers/ws.js +2 -0
  16. package/dist/esm/servers/ws.js.map +1 -0
  17. package/dist/system/index.js +6 -6
  18. package/dist/system/index.js.map +1 -1
  19. package/dist/system/servers/jsdom.js +2 -0
  20. package/dist/system/servers/jsdom.js.map +1 -0
  21. package/dist/system/servers/node-fetch.js +2 -0
  22. package/dist/system/servers/node-fetch.js.map +1 -0
  23. package/dist/system/servers/ws.js +2 -0
  24. package/dist/system/servers/ws.js.map +1 -0
  25. package/lib/crypto/hashing.js +53 -12
  26. package/lib/crypto/hashing.js.map +1 -1
  27. package/lib/index.js +1 -1
  28. package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js +38 -0
  29. package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js.map +1 -0
  30. package/lib/servers/jsdom.js +2 -0
  31. package/lib/servers/jsdom.js.map +1 -0
  32. package/lib/servers/node-fetch.js +3 -0
  33. package/lib/servers/node-fetch.js.map +1 -0
  34. package/lib/servers/polyfill/polyfillFetch.js +24 -0
  35. package/lib/servers/polyfill/polyfillFetch.js.map +1 -0
  36. package/lib/servers/polyfill/polyfillJsDom.js +57 -0
  37. package/lib/servers/polyfill/polyfillJsDom.js.map +1 -0
  38. package/lib/servers/polyfill/polyfillWebSocket.js +16 -0
  39. package/lib/servers/polyfill/polyfillWebSocket.js.map +1 -0
  40. package/lib/servers/ws.js +2 -0
  41. package/lib/servers/ws.js.map +1 -0
  42. package/package.json +9 -5
  43. package/src/crypto/hashing.test.ts +40 -2
  44. package/src/crypto/hashing.ts +95 -11
  45. package/src/fetch/types.ts +4 -1
  46. package/src/index.ts +1 -1
@@ -0,0 +1,2 @@
1
+ System.register(["../getGlobalThis-8951eb0e.js"],function(f,a){"use strict";var i;return{setters:[function(s){i=s.g}],execute:function(){f("polyfillJsDom",s);async function s(){if(typeof window<"u")return!1;const c=i(),{ResourceLoader:y,JSDOM:g}=await a.import("jsdom");class u{constructor(n={}){const{proxy:o,strictSSL:r,userAgent:e,...d}=n,O=new y({proxy:o,strictSSL:r,userAgent:e});return new g("",Object.assign(d,{resources:O})).window}}const l={},p=(()=>Object.getOwnPropertyNames(new u(l)).filter(t=>typeof c[t]<"u"))();function w(...t){const n=t.filter(e=>Array.isArray(e))[0],o=t.filter(e=>!Array.isArray(e))[0],r=new u(Object.assign({},o,l));return Object.getOwnPropertyNames(r).filter(e=>!p.includes(e)).filter(e=>!(n&&n.indexOf(e)===-1)).filter(e=>{switch(e){case"undefined":return!1}return!0}).forEach(e=>{Object.defineProperty(c,e,{configurable:!0,get:()=>r[e]})}),r}return w({url:"http://localhost"}),!0}}}});
2
+ //# sourceMappingURL=jsdom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsdom.js","sources":["../../../src/servers/polyfill/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":["polyfillJsDom","globalThis","getGlobalThis","ResourceLoader","JSDOM","Window","opts","proxy","strictSSL","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AA,CAAS,CAChB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAQA,EACN,CAAA,CAAA,CAAA,CAAA,CAAK,CACH,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CACX,CAAA,CACA,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CACT,CAAC,CAAA,CAGA,CAASA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAS,CAEjB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,eAAeb,CAAYa,CAAAA,CAAAA,CAAM,CACtC,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAc,CACd,CAAA,CAAA,CAAA,CAAA,CAAA,CAAK,CAAMM,CAAAA,CAAAA,CAAAA,CAAAA,CAAON,CAAoB,CACxC,CAAC,CACH,CAAC,CAAA,CAEIM,CACT,CAEA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAL,CAAW,CAAA,CAAE,CAAK,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAmB,CAAC,CAAA,CAC/B,CACT,CAAA;"}
@@ -0,0 +1,2 @@
1
+ System.register(["../getGlobalThis-8951eb0e.js"],function(F,f){"use strict";var u;return{setters:[function(c){u=c.g}],execute:function(){F({createFetchWithProxyByNodeFetch:g,polyfillFetch:c});function c(o){if("fetch"in globalThis)return!1;if(o){const{default:t,Response:i,Headers:e,Request:s,AbortError:n,FetchError:a,FormData:r,Blob:h,File:l}=o;return Object.assign(globalThis,{fetch:t,Response:i,Headers:e,Request:s,AbortError:n,FetchError:a,FormData:r,Blob:h,File:l}),!0}return f.import("node-fetch").then(t=>c(t))}function g({proxy:o,fetch:t}={}){const i=u();if(!o)return t||i.fetch;let e;const s=i.Request;let n,a;return async(r,h)=>{if(!e){const{default:l}=await f.import("https-proxy-agent");e=l(o)}return n||({Request:n,default:a}=await f.import("node-fetch")),t||=a,r instanceof s?t(new s(r,{agent:e})):r instanceof n?t(new n(r,{agent:e})):t(r,{...h,agent:e})}}}}});
2
+ //# sourceMappingURL=node-fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-fetch.js","sources":["../../../src/servers/polyfill/polyfillFetch.ts","../../../src/servers/fetch/createFetchWithProxyByNodeFetch.ts"],"sourcesContent":["import type { MaybePromise } from '../../asyncs/MaybePromise';\n\nexport function polyfillFetch(nodeFetch: typeof import('node-fetch')): boolean;\nexport function polyfillFetch(nodeFetch?: undefined): Promise<boolean>;\nexport function polyfillFetch(nodeFetch?: typeof import('node-fetch')): MaybePromise<boolean> {\n if ('fetch' in globalThis) {\n return false;\n }\n // sync mode\n if (nodeFetch) {\n const { default: fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = nodeFetch;\n Object.assign(globalThis, {\n fetch,\n Response,\n Headers,\n Request,\n AbortError,\n FetchError,\n FormData,\n Blob,\n File,\n });\n // abort controller\n return true;\n }\n return import('node-fetch').then((v) => polyfillFetch(v));\n}\n","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":["polyfillFetch","nodeFetch","fetch","Response","Headers","Request","AbortError","FetchError","FormData","Blob","File","module","v","createFetchWithProxyByNodeFetch","proxy","globalThis","getGlobalThis","agent","NodeRequest","NodeFetch","url","init","createHttpsProxyAgent"],"mappings":"gMAIO,CAASA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAcC,CAAgE,CAAA,CAC5F,GAAI,CAAW,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CACb,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAGT,CAAIA,CAAAA,CAAAA,CAAAA,CAAW,CACb,CAAM,CAAA,CAAA,CAAA,CAAA,CAAE,CAASC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAO,CAAAC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAU,QAAAC,CAAS,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAC,EAAS,CAAAC,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAY,WAAAC,CAAY,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAC,CAAU,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAC,CAAM,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAC,CAAK,CAAIT,CAAAA,CAAAA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A,CAAAA,CAAAA,CAAAA,CACTH,CAAc,CAAA,CAAA,CAAA,CAAA,CAAIG,EAAQe,CAAK,CAAA,CAAE,CAAAH,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAM,CAAQ,CAAC,EAErDG,CAAuBF,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAClBhB,CAAc,CAAA,CAAA,CAAA,CAAA,CAAIgB,CAAYE,CAAAA,CAAAA,CAAK,CAAE,CAAAH,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAM,CAAQ,CAAC,CAEtDf,CAAAA,CAAAA,CAAckB,EAAK,CACzB,CAAA,CAAA,CAAGC,CACH,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAJ,CACF,CAAQ,CACV,CACF;"}
@@ -0,0 +1,2 @@
1
+ System.register(["../getGlobalThis-8951eb0e.js"],function(c,s){"use strict";var o;return{setters:[function(t){o=t.g}],execute:function(){c("polyfillWebSocket",t);function t(n){const i=o();if("WebSocket"in i)return!1;if(n){const{WebSocket:e}=n;Object.assign(i,{WebSocket:e})}return s.import("ws").then(e=>t(e))}}}});
2
+ //# sourceMappingURL=ws.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ws.js","sources":["../../../src/servers/polyfill/polyfillWebSocket.ts"],"sourcesContent":["import type { MaybePromise } from '../../asyncs/MaybePromise';\nimport { getGlobalThis } from '../../isomorphics/getGlobalThis';\n\nexport function polyfillWebSocket(ws: any): boolean;\nexport function polyfillWebSocket(ws?: undefined): Promise<boolean>;\nexport function polyfillWebSocket(ws?: any): MaybePromise<boolean> {\n const globalThis = getGlobalThis();\n if ('WebSocket' in globalThis) {\n return false;\n }\n if (ws) {\n const { WebSocket } = ws;\n Object.assign(globalThis, { WebSocket });\n }\n return import('ws').then((ws) => polyfillWebSocket(ws));\n}\n"],"names":["polyfillWebSocket","ws","globalThis","getGlobalThis","WebSocket","module"],"mappings":"mKAKgBA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,CAAkBC,CAAiC,CAAA,CACjE,MAAMC,CAAaC,CAAAA,CAAAA,CACnB,CAAA,CAAA,CAAA,CAAA,CAAI,cAAeD,CACjB,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAO,CAET,CAAA,CAAA,CAAA,CAAA,CAAID,EAAI,CACN,CAAA,CAAA,CAAA,CAAA,CAAM,CAAE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAG,CAAU,CAAIH,CAAAA,CAAAA,CACtB,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAOC,EAAY,CAAE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAAE,CAAU,CAAC,CACzC,CACA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAOC,CAAO,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,IAAI,CAAE,CAAA,CAAA,CAAA,CAAA,CAAA,CAAMJ,CAAOD,CAAAA,CAAAA,CAAAA,CAAkBC,CAAE,CAAC,CACxD;"}
@@ -1,24 +1,65 @@
1
- function sha1(s) {
2
- return digestOf("SHA-1", s);
1
+ import { ArrayBuffers } from '../io/ArrayBuffers.js';
2
+
3
+ function sha1(s, o) {
4
+ return digestOf("SHA-1", s, o);
3
5
  }
4
- function sha256(s) {
5
- return digestOf("SHA-256", s);
6
+ function sha256(s, o) {
7
+ return digestOf("SHA-256", s, o);
6
8
  }
7
- function sha384(s) {
8
- return digestOf("SHA-384", s);
9
+ function sha384(s, o) {
10
+ return digestOf("SHA-384", s, o);
9
11
  }
10
- function sha512(s) {
11
- return digestOf("SHA-512", s);
12
+ function sha512(s, o) {
13
+ return digestOf("SHA-512", s, o);
12
14
  }
13
- function digestOf(a, s) {
14
- return crypto.subtle.digest(a, bufferOf(s));
15
+ function digestOf(a, s, o) {
16
+ let buffer = crypto.subtle.digest(a, binaryOf(s));
17
+ return o ? buffer.then((v) => encode(v, o)) : o;
15
18
  }
16
- function bufferOf(s) {
19
+ function binaryOf(s) {
17
20
  if (typeof s === "string") {
18
21
  return new TextEncoder().encode(s);
19
22
  }
20
23
  return s;
21
24
  }
25
+ function encode(buf, o) {
26
+ if (o) {
27
+ switch (o) {
28
+ case "hex":
29
+ case "base64":
30
+ return ArrayBuffers.toString(buf, o);
31
+ }
32
+ }
33
+ return buf;
34
+ }
35
+ async function hmac(hash, key, data, o) {
36
+ let ck = key instanceof CryptoKey ? key : await crypto.subtle.importKey(
37
+ "raw",
38
+ binaryOf(key),
39
+ {
40
+ name: "HMAC",
41
+ hash: {
42
+ name: normalizeHash(hash)
43
+ }
44
+ },
45
+ false,
46
+ ["sign"]
47
+ );
48
+ let buffer = await crypto.subtle.sign(
49
+ {
50
+ name: "HMAC",
51
+ hash: {
52
+ name: hash
53
+ }
54
+ },
55
+ ck,
56
+ binaryOf(data)
57
+ );
58
+ return encode(buffer, o);
59
+ }
60
+ function normalizeHash(hash) {
61
+ return hash.replace(/^sha/i, "SHA-");
62
+ }
22
63
 
23
- export { sha1, sha256, sha384, sha512 };
64
+ export { hmac, sha1, sha256, sha384, sha512 };
24
65
  //# sourceMappingURL=hashing.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hashing.js","sources":["../../src/crypto/hashing.ts"],"sourcesContent":["export function sha1(s: string | BufferSource) {\n return digestOf('SHA-1', s);\n}\n\nexport function sha256(s: string | BufferSource) {\n return digestOf('SHA-256', s);\n}\n\nexport function sha384(s: string | BufferSource) {\n return digestOf('SHA-384', s);\n}\n\nexport function sha512(s: string | BufferSource) {\n return digestOf('SHA-512', s);\n}\n\nfunction digestOf(a: string, s: string | BufferSource) {\n return crypto.subtle.digest(a, bufferOf(s));\n}\n\nfunction bufferOf(s: string | BufferSource) {\n // ArrayBuffer, TypedArray, DataView\n if (typeof s === 'string') {\n return new TextEncoder().encode(s);\n }\n return s;\n}\n"],"names":[],"mappings":"AAAO,SAAS,KAAK,CAA0B,EAAA;AAC7C,EAAO,OAAA,QAAA,CAAS,SAAS,CAAC,CAAA,CAAA;AAC5B,CAAA;AAEO,SAAS,OAAO,CAA0B,EAAA;AAC/C,EAAO,OAAA,QAAA,CAAS,WAAW,CAAC,CAAA,CAAA;AAC9B,CAAA;AAEO,SAAS,OAAO,CAA0B,EAAA;AAC/C,EAAO,OAAA,QAAA,CAAS,WAAW,CAAC,CAAA,CAAA;AAC9B,CAAA;AAEO,SAAS,OAAO,CAA0B,EAAA;AAC/C,EAAO,OAAA,QAAA,CAAS,WAAW,CAAC,CAAA,CAAA;AAC9B,CAAA;AAEA,SAAS,QAAA,CAAS,GAAW,CAA0B,EAAA;AACrD,EAAA,OAAO,OAAO,MAAO,CAAA,MAAA,CAAO,CAAG,EAAA,QAAA,CAAS,CAAC,CAAC,CAAA,CAAA;AAC5C,CAAA;AAEA,SAAS,SAAS,CAA0B,EAAA;AAE1C,EAAI,IAAA,OAAO,MAAM,QAAU,EAAA;AACzB,IAAA,OAAO,IAAI,WAAA,EAAc,CAAA,MAAA,CAAO,CAAC,CAAA,CAAA;AAAA,GACnC;AACA,EAAO,OAAA,CAAA,CAAA;AACT;;;;"}
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)) : o;\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 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":";;AAKgB,SAAA,IAAA,CAAK,GAAe,CAAmB,EAAA;AACrD,EAAO,OAAA,QAAA,CAAS,OAAS,EAAA,CAAA,EAAG,CAAC,CAAA,CAAA;AAC/B,CAAA;AAKgB,SAAA,MAAA,CAAO,GAAe,CAAmB,EAAA;AACvD,EAAO,OAAA,QAAA,CAAS,SAAW,EAAA,CAAA,EAAG,CAAC,CAAA,CAAA;AACjC,CAAA;AAKgB,SAAA,MAAA,CAAO,GAAe,CAAmB,EAAA;AACvD,EAAO,OAAA,QAAA,CAAS,SAAW,EAAA,CAAA,EAAG,CAAC,CAAA,CAAA;AACjC,CAAA;AAKgB,SAAA,MAAA,CAAO,GAAe,CAAmB,EAAA;AACvD,EAAO,OAAA,QAAA,CAAS,SAAW,EAAA,CAAA,EAAG,CAAC,CAAA,CAAA;AACjC,CAAA;AAEA,SAAS,QAAA,CAAS,CAAW,EAAA,CAAA,EAAe,CAAmB,EAAA;AAC7D,EAAA,IAAI,SAAS,MAAO,CAAA,MAAA,CAAO,OAAO,CAAG,EAAA,QAAA,CAAS,CAAC,CAAC,CAAA,CAAA;AAChD,EAAO,OAAA,CAAA,GAAI,OAAO,IAAK,CAAA,CAAC,MAAM,MAAO,CAAA,CAAA,EAAG,CAAC,CAAC,CAAI,GAAA,CAAA,CAAA;AAChD,CAAA;AAWA,SAAS,SAAS,CAAe,EAAA;AAE/B,EAAI,IAAA,OAAO,MAAM,QAAU,EAAA;AACzB,IAAA,OAAO,IAAI,WAAA,EAAc,CAAA,MAAA,CAAO,CAAC,CAAA,CAAA;AAAA,GACnC;AACA,EAAO,OAAA,CAAA,CAAA;AACT,CAAA;AAEA,SAAS,MAAA,CAAO,KAAmB,CAAmB,EAAA;AACpD,EAAA,IAAI,CAAG,EAAA;AACL,IAAA,QAAQ,CAAG;AAAA,MACT,KAAK,KAAA,CAAA;AAAA,MACL,KAAK,QAAA;AACH,QAAO,OAAA,YAAA,CAAa,QAAS,CAAA,GAAA,EAAK,CAAC,CAAA,CAAA;AAAA,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/index.js CHANGED
@@ -47,7 +47,7 @@ export { getGlobalThis } from './isomorphics/getGlobalThis.js';
47
47
  export { structuredClone } from './isomorphics/structuredClone.js';
48
48
  export { randomUUID } from './crypto/randomUUID.js';
49
49
  export { getRandomValues } from './crypto/getRandomValues.js';
50
- export { sha1, sha256, sha384, sha512 } from './crypto/hashing.js';
50
+ export { hmac, sha1, sha256, sha384, sha512 } from './crypto/hashing.js';
51
51
  export { md5 } from './crypto/md5.js';
52
52
  export { hex } from './crypto/base.js';
53
53
  export { createULID, isULID, parseULID, ulid } from './crypto/ulid.js';
@@ -0,0 +1,38 @@
1
+ import { getGlobalThis } from '../../isomorphics/getGlobalThis.js';
2
+
3
+ function createFetchWithProxyByNodeFetch({
4
+ proxy,
5
+ fetch
6
+ } = {}) {
7
+ const globalThis = getGlobalThis();
8
+ if (!proxy) {
9
+ return fetch || globalThis.fetch;
10
+ }
11
+ let agent;
12
+ const Request = globalThis.Request;
13
+ let NodeRequest;
14
+ let NodeFetch;
15
+ return async (url, init) => {
16
+ if (!agent) {
17
+ const { default: createHttpsProxyAgent } = await import('https-proxy-agent');
18
+ agent = createHttpsProxyAgent(proxy);
19
+ }
20
+ if (!NodeRequest) {
21
+ ({ Request: NodeRequest, default: NodeFetch } = await import('node-fetch'));
22
+ }
23
+ fetch || (fetch = NodeFetch);
24
+ if (url instanceof Request) {
25
+ return fetch(new Request(url, { agent }));
26
+ }
27
+ if (url instanceof NodeRequest) {
28
+ return fetch(new NodeRequest(url, { agent }));
29
+ }
30
+ return fetch(url, {
31
+ ...init,
32
+ agent
33
+ });
34
+ };
35
+ }
36
+
37
+ export { createFetchWithProxyByNodeFetch };
38
+ //# sourceMappingURL=createFetchWithProxyByNodeFetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createFetchWithProxyByNodeFetch.js","sources":["../../../src/servers/fetch/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,2 @@
1
+ export { polyfillJsDom } from './polyfill/polyfillJsDom.js';
2
+ //# sourceMappingURL=jsdom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsdom.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ export { polyfillFetch } from './polyfill/polyfillFetch.js';
2
+ export { createFetchWithProxyByNodeFetch } from './fetch/createFetchWithProxyByNodeFetch.js';
3
+ //# sourceMappingURL=node-fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-fetch.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,24 @@
1
+ function polyfillFetch(nodeFetch) {
2
+ if ("fetch" in globalThis) {
3
+ return false;
4
+ }
5
+ if (nodeFetch) {
6
+ const { default: fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = nodeFetch;
7
+ Object.assign(globalThis, {
8
+ fetch,
9
+ Response,
10
+ Headers,
11
+ Request,
12
+ AbortError,
13
+ FetchError,
14
+ FormData,
15
+ Blob,
16
+ File
17
+ });
18
+ return true;
19
+ }
20
+ return import('node-fetch').then((v) => polyfillFetch(v));
21
+ }
22
+
23
+ export { polyfillFetch };
24
+ //# sourceMappingURL=polyfillFetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polyfillFetch.js","sources":["../../../src/servers/polyfill/polyfillFetch.ts"],"sourcesContent":["import type { MaybePromise } from '../../asyncs/MaybePromise';\n\nexport function polyfillFetch(nodeFetch: typeof import('node-fetch')): boolean;\nexport function polyfillFetch(nodeFetch?: undefined): Promise<boolean>;\nexport function polyfillFetch(nodeFetch?: typeof import('node-fetch')): MaybePromise<boolean> {\n if ('fetch' in globalThis) {\n return false;\n }\n // sync mode\n if (nodeFetch) {\n const { default: fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = nodeFetch;\n Object.assign(globalThis, {\n fetch,\n Response,\n Headers,\n Request,\n AbortError,\n FetchError,\n FormData,\n Blob,\n File,\n });\n // abort controller\n return true;\n }\n return import('node-fetch').then((v) => polyfillFetch(v));\n}\n"],"names":[],"mappings":"AAIO,SAAS,cAAc,SAAgE,EAAA;AAC5F,EAAA,IAAI,WAAW,UAAY,EAAA;AACzB,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAEA,EAAA,IAAI,SAAW,EAAA;AACb,IAAM,MAAA,EAAE,OAAS,EAAA,KAAA,EAAO,QAAU,EAAA,OAAA,EAAS,OAAS,EAAA,UAAA,EAAY,UAAY,EAAA,QAAA,EAAU,IAAM,EAAA,IAAA,EAAS,GAAA,SAAA,CAAA;AACrG,IAAA,MAAA,CAAO,OAAO,UAAY,EAAA;AAAA,MACxB,KAAA;AAAA,MACA,QAAA;AAAA,MACA,OAAA;AAAA,MACA,OAAA;AAAA,MACA,UAAA;AAAA,MACA,UAAA;AAAA,MACA,QAAA;AAAA,MACA,IAAA;AAAA,MACA,IAAA;AAAA,KACD,CAAA,CAAA;AAED,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AACA,EAAO,OAAA,OAAO,YAAY,CAAE,CAAA,IAAA,CAAK,CAAC,CAAM,KAAA,aAAA,CAAc,CAAC,CAAC,CAAA,CAAA;AAC1D;;;;"}
@@ -0,0 +1,57 @@
1
+ import { getGlobalThis } from '../../isomorphics/getGlobalThis.js';
2
+
3
+ async function polyfillJsDom() {
4
+ if (typeof window !== "undefined") {
5
+ return false;
6
+ }
7
+ const globalThis = getGlobalThis();
8
+ const { ResourceLoader, JSDOM } = await import('jsdom');
9
+ class Window {
10
+ constructor(opts = {}) {
11
+ const { proxy, strictSSL, userAgent, ...jsdomOpts } = opts;
12
+ const resources = new ResourceLoader({
13
+ proxy,
14
+ strictSSL,
15
+ userAgent
16
+ });
17
+ return new JSDOM(
18
+ "",
19
+ Object.assign(jsdomOpts, {
20
+ resources
21
+ })
22
+ ).window;
23
+ }
24
+ }
25
+ const defaultJsdomConfig = {
26
+ // features: {
27
+ // FetchExternalResources: false,
28
+ // ProcessExternalResources: false,
29
+ // },
30
+ };
31
+ const protectedProperties = (() => Object.getOwnPropertyNames(new Window(defaultJsdomConfig)).filter(
32
+ (prop) => typeof globalThis[prop] !== "undefined"
33
+ ))();
34
+ function installEnv(...args) {
35
+ const properties = args.filter((arg) => Array.isArray(arg))[0];
36
+ const userJsdomConfig = args.filter((arg) => !Array.isArray(arg))[0];
37
+ const window2 = new Window(Object.assign({}, userJsdomConfig, defaultJsdomConfig));
38
+ Object.getOwnPropertyNames(window2).filter((prop) => !protectedProperties.includes(prop)).filter((prop) => !(properties && properties.indexOf(prop) === -1)).filter((prop) => {
39
+ switch (prop) {
40
+ case "undefined":
41
+ return false;
42
+ }
43
+ return true;
44
+ }).forEach((prop) => {
45
+ Object.defineProperty(globalThis, prop, {
46
+ configurable: true,
47
+ get: () => window2[prop]
48
+ });
49
+ });
50
+ return window2;
51
+ }
52
+ installEnv({ url: "http://localhost" });
53
+ return true;
54
+ }
55
+
56
+ export { polyfillJsDom };
57
+ //# sourceMappingURL=polyfillJsDom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polyfillJsDom.js","sources":["../../../src/servers/polyfill/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;AAAA,EAItD,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,CA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globalThis) {
6
+ return false;
7
+ }
8
+ if (ws) {
9
+ const { WebSocket } = ws;
10
+ Object.assign(globalThis, { WebSocket });
11
+ }
12
+ return import('ws').then((ws2) => polyfillWebSocket(ws2));
13
+ }
14
+
15
+ export { polyfillWebSocket };
16
+ //# sourceMappingURL=polyfillWebSocket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polyfillWebSocket.js","sources":["../../../src/servers/polyfill/polyfillWebSocket.ts"],"sourcesContent":["import type { MaybePromise } from '../../asyncs/MaybePromise';\nimport { getGlobalThis } from '../../isomorphics/getGlobalThis';\n\nexport function polyfillWebSocket(ws: any): boolean;\nexport function polyfillWebSocket(ws?: undefined): Promise<boolean>;\nexport function polyfillWebSocket(ws?: any): MaybePromise<boolean> {\n const globalThis = getGlobalThis();\n if ('WebSocket' in globalThis) {\n return false;\n }\n if (ws) {\n const { WebSocket } = ws;\n Object.assign(globalThis, { WebSocket });\n }\n return import('ws').then((ws) => polyfillWebSocket(ws));\n}\n"],"names":["ws"],"mappings":";;AAKO,SAAS,kBAAkB,EAAiC,EAAA;AACjE,EAAA,MAAM,aAAa,aAAc,EAAA,CAAA;AACjC,EAAA,IAAI,eAAe,UAAY,EAAA;AAC7B,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACA,EAAA,IAAI,EAAI,EAAA;AACN,IAAM,MAAA,EAAE,WAAc,GAAA,EAAA,CAAA;AACtB,IAAA,MAAA,CAAO,MAAO,CAAA,UAAA,EAAY,EAAE,SAAA,EAAW,CAAA,CAAA;AAAA,GACzC;AACA,EAAO,OAAA,OAAO,IAAI,CAAE,CAAA,IAAA,CAAK,CAACA,GAAO,KAAA,iBAAA,CAAkBA,GAAE,CAAC,CAAA,CAAA;AACxD;;;;"}
@@ -0,0 +1,2 @@
1
+ export { polyfillWebSocket } from './polyfill/polyfillWebSocket.js';
2
+ //# sourceMappingURL=ws.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ws.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wener/utils",
3
- "version": "1.1.22",
3
+ "version": "1.1.24",
4
4
  "type": "module",
5
5
  "description": "Utils for daily use",
6
6
  "repository": {
@@ -39,12 +39,12 @@
39
39
  }
40
40
  },
41
41
  "devDependencies": {
42
- "@types/lodash": "^4.14.199",
43
- "@types/ws": "^8.5.7",
42
+ "@types/lodash": "^4.14.200",
43
+ "@types/ws": "^8.5.8",
44
44
  "https-proxy-agent": "^7.0.2",
45
45
  "lodash": "^4.17.21",
46
46
  "node-fetch": "^3.3.2",
47
- "undici": "^5.25.4"
47
+ "undici": "^5.27.0"
48
48
  },
49
49
  "publishConfig": {
50
50
  "registry": "https://registry.npmjs.org",
@@ -54,13 +54,17 @@
54
54
  "rollup": {
55
55
  "input": [
56
56
  "./src/index.ts",
57
- "./src/server.ts"
57
+ "./src/server.ts",
58
+ "./src/servers/ws.ts",
59
+ "./src/servers/jsdom.ts",
60
+ "./src/servers/node-fetch.ts"
58
61
  ],
59
62
  "external": [
60
63
  "crypto",
61
64
  "jsdom",
62
65
  "node-fetch",
63
66
  "ws",
67
+ "https-proxy-agent",
64
68
  "undici"
65
69
  ]
66
70
  },
@@ -1,8 +1,9 @@
1
- import { test, beforeAll, expect } from 'vitest';
1
+ import { assert, beforeAll, expect, test } from 'vitest';
2
+ import { ArrayBuffers } from '../io/ArrayBuffers';
2
3
  import { polyfillCrypto } from '../servers/polyfill/polyfillCrypto';
3
4
  import { isUUID } from '../validations/isUUID';
4
5
  import { hex } from './base';
5
- import { sha1, sha256, sha384, sha512 } from './hashing';
6
+ import { hmac, sha1, sha256, sha384, sha512 } from './hashing';
6
7
  import { md5 } from './md5';
7
8
  import { _randomUUID } from './randomUUID';
8
9
 
@@ -12,6 +13,11 @@ beforeAll(async () => {
12
13
 
13
14
  test('sha', async () => {
14
15
  expect(hex(await sha1(''))).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709');
16
+ expect(await sha1('', 'hex')).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709');
17
+ expect(ArrayBuffers.toString(await sha1(''), 'base64')).toBe('2jmj7l5rSw0yVb/vlWAYkK/YBwk=');
18
+ expect(await sha1('', 'base64')).toBe('2jmj7l5rSw0yVb/vlWAYkK/YBwk=');
19
+
20
+ expect(await sha1('', 'hex')).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709');
15
21
  expect(hex(await sha1('abc'))).toBe('a9993e364706816aba3e25717850c26c9cd0d89d');
16
22
  expect(hex(await sha256(''))).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
17
23
  expect(hex(await sha384(''))).toBe(
@@ -29,3 +35,35 @@ test('randomUUID', () => {
29
35
  expect(isUUID(_randomUUID())).toBeTruthy();
30
36
  }
31
37
  });
38
+
39
+ test('hmac', async () => {
40
+ let key = await crypto.subtle.importKey(
41
+ 'raw',
42
+ new TextEncoder().encode('YourAccessKeySecret'),
43
+ {
44
+ name: 'HMAC',
45
+ hash: {
46
+ name: 'SHA-256',
47
+ },
48
+ },
49
+ false,
50
+ ['sign', 'verify'],
51
+ );
52
+ const out = hex(
53
+ await crypto.subtle.sign(
54
+ {
55
+ name: 'HMAC',
56
+ hash: {
57
+ name: 'SHA-256',
58
+ },
59
+ },
60
+ key,
61
+ new TextEncoder().encode(''),
62
+ ),
63
+ );
64
+ console.log(out);
65
+ const { createHmac } = await import('node:crypto');
66
+ let fromNode = createHmac('sha256', 'YourAccessKeySecret').update('').digest('hex');
67
+ assert.equal(out, fromNode);
68
+ assert.equal(out, await hmac('sha256', 'YourAccessKeySecret', '', 'hex'));
69
+ });
@@ -1,27 +1,111 @@
1
- export function sha1(s: string | BufferSource) {
2
- return digestOf('SHA-1', s);
1
+ import { ArrayBuffers } from '../io/ArrayBuffers';
2
+
3
+ export function sha1(s: BinaryLike, o?: undefined): Promise<Buffer>;
4
+ export function sha1(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;
5
+
6
+ export function sha1(s: BinaryLike, o?: DigestOptions) {
7
+ return digestOf('SHA-1', s, o);
3
8
  }
4
9
 
5
- export function sha256(s: string | BufferSource) {
6
- return digestOf('SHA-256', s);
10
+ export function sha256(s: BinaryLike, o?: undefined): Promise<Buffer>;
11
+ export function sha256(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;
12
+
13
+ export function sha256(s: BinaryLike, o?: DigestOptions) {
14
+ return digestOf('SHA-256', s, o);
7
15
  }
8
16
 
9
- export function sha384(s: string | BufferSource) {
10
- return digestOf('SHA-384', s);
17
+ export function sha384(s: BinaryLike, o?: undefined): Promise<Buffer>;
18
+ export function sha384(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;
19
+
20
+ export function sha384(s: BinaryLike, o?: DigestOptions) {
21
+ return digestOf('SHA-384', s, o);
11
22
  }
12
23
 
13
- export function sha512(s: string | BufferSource) {
14
- return digestOf('SHA-512', s);
24
+ export function sha512(s: BinaryLike, o?: undefined): Promise<Buffer>;
25
+ export function sha512(s: BinaryLike, o: 'hex' | 'base64'): Promise<string>;
26
+
27
+ export function sha512(s: BinaryLike, o?: DigestOptions) {
28
+ return digestOf('SHA-512', s, o);
15
29
  }
16
30
 
17
- function digestOf(a: string, s: string | BufferSource) {
18
- return crypto.subtle.digest(a, bufferOf(s));
31
+ function digestOf(a: string, s: BinaryLike, o?: DigestOptions) {
32
+ let buffer = crypto.subtle.digest(a, binaryOf(s));
33
+ return o ? buffer.then((v) => encode(v, o)) : o;
19
34
  }
20
35
 
21
- function bufferOf(s: string | BufferSource) {
36
+ export type DigestOptions =
37
+ | 'hex'
38
+ | 'base64'
39
+ | {
40
+ encoding: 'hex' | 'base64';
41
+ };
42
+
43
+ type BinaryLike = string | BufferSource;
44
+
45
+ function binaryOf(s: BinaryLike) {
22
46
  // ArrayBuffer, TypedArray, DataView
23
47
  if (typeof s === 'string') {
24
48
  return new TextEncoder().encode(s);
25
49
  }
26
50
  return s;
27
51
  }
52
+
53
+ function encode(buf: BufferSource, o?: DigestOptions) {
54
+ if (o) {
55
+ switch (o) {
56
+ case 'hex':
57
+ case 'base64':
58
+ return ArrayBuffers.toString(buf, o);
59
+ }
60
+ }
61
+ return buf;
62
+ }
63
+
64
+ type StringEncoding = 'hex' | 'base64';
65
+ type IsStringCoding<T> = T extends StringEncoding ? true : T extends { encoding: StringEncoding } ? true : false;
66
+
67
+ export function hmac<O extends DigestOptions>(
68
+ hash: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512' | 'sha1' | 'sha256' | 'sha384' | 'sha512',
69
+ key: BinaryLike | CryptoKey,
70
+ data: BinaryLike,
71
+ o?: O,
72
+ ): Promise<IsStringCoding<O> extends true ? string : Buffer>;
73
+
74
+ export async function hmac(
75
+ hash: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512' | 'sha1' | 'sha256' | 'sha384' | 'sha512',
76
+ key: BinaryLike | CryptoKey,
77
+ data: BinaryLike,
78
+ o?: DigestOptions,
79
+ ) {
80
+ let ck =
81
+ key instanceof CryptoKey
82
+ ? key
83
+ : await crypto.subtle.importKey(
84
+ 'raw',
85
+ binaryOf(key),
86
+ {
87
+ name: 'HMAC',
88
+ hash: {
89
+ name: normalizeHash(hash),
90
+ },
91
+ },
92
+ false,
93
+ ['sign'],
94
+ );
95
+ let buffer = await crypto.subtle.sign(
96
+ {
97
+ name: 'HMAC',
98
+ hash: {
99
+ name: hash,
100
+ },
101
+ },
102
+ ck,
103
+ binaryOf(data),
104
+ );
105
+
106
+ return encode(buffer, o);
107
+ }
108
+
109
+ function normalizeHash(hash: string) {
110
+ return hash.replace(/^sha/i, 'SHA-');
111
+ }
@@ -1 +1,4 @@
1
- export type FetchLike<R extends RequestInit = RequestInit> = (url: string | Request, init?: R) => Promise<Response>;
1
+ export type FetchLike<R extends RequestInit = RequestInit> = (
2
+ url: string | URL | Request,
3
+ init?: R,
4
+ ) => Promise<Response>;
package/src/index.ts CHANGED
@@ -85,7 +85,7 @@ export { structuredClone } from './isomorphics/structuredClone';
85
85
  // crypto
86
86
  export { randomUUID } from './crypto/randomUUID';
87
87
  export { getRandomValues } from './crypto/getRandomValues';
88
- export { sha1, sha256, sha384, sha512 } from './crypto/hashing';
88
+ export { sha1, sha256, sha384, sha512, hmac, type DigestOptions } from './crypto/hashing';
89
89
  export { md5 } from './crypto/md5';
90
90
  export { hex } from './crypto/base';
91
91
  export { isULID, createULID, ulid, parseULID } from './crypto/ulid';