UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

1 lines 5 kB
{"version":3,"file":"net.cjs","names":["hashjs"],"sources":["../../src/helpers/net.ts"],"sourcesContent":["import canonicalize from \"canonicalize\";\nimport hashjs from \"hash.js\";\nimport type { Logger } from \"../middleware/logger.ts\";\nimport { logOnce } from \"./log.ts\";\n\nconst { hmac, sha256 } = hashjs;\n\n/**\n * Send an HTTP request with the given signing key. If the response is a 401 or\n * 403, then try again with the fallback signing key\n */\nexport async function fetchWithAuthFallback<TFetch extends typeof fetch>({\n authToken,\n authTokenFallback,\n fetch,\n options,\n url,\n}: {\n authToken?: string;\n authTokenFallback?: string;\n fetch: TFetch;\n options?: Parameters<TFetch>[1];\n url: URL | string;\n}): Promise<Response> {\n let res = await fetch(url, {\n ...options,\n headers: {\n ...options?.headers,\n Authorization: `Bearer ${authToken}`,\n },\n });\n\n if ([401, 403].includes(res.status) && authTokenFallback) {\n res = await fetch(url, {\n ...options,\n headers: {\n ...options?.headers,\n Authorization: `Bearer ${authTokenFallback}`,\n },\n });\n }\n\n return res;\n}\n\nexport function signWithHashJs(\n data: unknown,\n signingKey: string,\n ts: string,\n): string {\n // Calculate the HMAC of the request body ourselves.\n // We make the assumption here that a stringified body is the same as the\n // raw bytes; it may be pertinent in the future to always parse, then\n // canonicalize the body to ensure it's consistent.\n const encoded = typeof data === \"string\" ? data : canonicalize(data);\n // Remove the `/signkey-[test|prod]-/` prefix from our signing key to calculate the HMAC.\n const key = signingKey.replace(/signkey-\\w+-/, \"\");\n // biome-ignore lint/suspicious/noExplicitAny: intentional\n const mac = hmac(sha256 as any, key)\n .update(encoded)\n .update(ts)\n .digest(\"hex\");\n\n return mac;\n}\n\n// Cache for CryptoKeys to avoid repeated importKey calls\nconst cryptoKeyCache = new Map<string, CryptoKey>();\n\nasync function signWithNative(\n subtle: SubtleCrypto,\n data: unknown,\n signingKey: string,\n ts: string,\n): Promise<string> {\n const encoded = typeof data === \"string\" ? data : canonicalize(data);\n const key = signingKey.replace(/signkey-\\w+-/, \"\");\n\n let cryptoKey = cryptoKeyCache.get(key);\n if (!cryptoKey) {\n cryptoKey = await subtle.importKey(\n \"raw\",\n new TextEncoder().encode(key),\n { name: \"HMAC\", hash: \"SHA-256\" },\n false,\n [\"sign\"],\n );\n cryptoKeyCache.set(key, cryptoKey);\n }\n\n const signature = await subtle.sign(\n \"HMAC\",\n cryptoKey,\n new TextEncoder().encode(encoded + ts),\n );\n\n return Array.from(new Uint8Array(signature))\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Sign data with a signing key using HMAC-SHA256.\n * Uses native crypto.subtle when available, falls back to hash.js.\n */\nexport async function signDataWithKey(\n data: unknown,\n signingKey: string,\n ts: string,\n logger: Logger,\n): Promise<string> {\n const subtle = globalThis.crypto?.subtle;\n\n logOnce(\n logger,\n \"debug\",\n \"crypto-implementation\",\n subtle\n ? \"Using native Web Crypto for request signing\"\n : \"Using hash.js fallback for request signing (native crypto unavailable)\",\n );\n\n if (subtle) {\n try {\n return await signWithNative(subtle, data, signingKey, ts);\n } catch (err) {\n logger.debug({ err }, \"Native crypto failed, falling back to hash.js\");\n }\n }\n return signWithHashJs(data, signingKey, ts);\n}\n"],"mappings":";;;;;;;;AAKA,MAAM,EAAE,MAAM,WAAWA;;;;;AAMzB,eAAsB,sBAAmD,EACvE,WACA,mBACA,OACA,SACA,OAOoB;CACpB,IAAI,MAAM,MAAM,MAAM,KAAK;EACzB,GAAG;EACH,SAAS;GACP,GAAG,SAAS;GACZ,eAAe,UAAU;GAC1B;EACF,CAAC;AAEF,KAAI,CAAC,KAAK,IAAI,CAAC,SAAS,IAAI,OAAO,IAAI,kBACrC,OAAM,MAAM,MAAM,KAAK;EACrB,GAAG;EACH,SAAS;GACP,GAAG,SAAS;GACZ,eAAe,UAAU;GAC1B;EACF,CAAC;AAGJ,QAAO;;AAGT,SAAgB,e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