UNPKG

serverless-spy

Version:

CDK-based library for writing elegant integration tests on AWS serverless architecture and an additional web console to monitor events in real time.

53 lines (51 loc) 1.5 kB
const __dirname = import.meta.dirname; import { fromNodeProviderChain } from "@aws-sdk/credential-providers"; import * as aws4 from "aws4"; //#region common/getWebSocketUrl.ts async function getSignedWebSocketUrl(url, credentials) { let hostParts; let pathname; if (!url) throw new Error(`Missing websocket URL`); try { new URL(url); const urlParsed = parseUrl(url); pathname = urlParsed.pathname; hostParts = urlParsed.host.split("."); } catch { throw new Error(`Invalid websocket URL ${url}`); } if (!credentials) credentials = await fromNodeProviderChain()(); const AWS_REGION = hostParts[2]; const SOCKET_HOST = hostParts[0]; const ENV = pathname.replace(/^\//, ""); const WEBSOCKET_URL = `${SOCKET_HOST}.execute-api.${AWS_REGION}.amazonaws.com`; const { path } = aws4.sign({ method: "GET", host: WEBSOCKET_URL, path: `/${ENV}`, service: "execute-api", region: AWS_REGION, signQuery: true }, { accessKeyId: credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey, sessionToken: credentials.sessionToken }); return `wss://${WEBSOCKET_URL}${path}`; } function parseUrl(href) { const match = href.match(/^(wss?:)\/\/(([^:/?#]*)(?::([0-9]+))?)([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/); return match && { href, protocol: match[1], host: match[2], hostname: match[3], port: match[4], pathname: match[5], search: match[6], hash: match[7] }; } //#endregion export { getSignedWebSocketUrl }; //# sourceMappingURL=getWebSocketUrl.mjs.map