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.
55 lines (53 loc) • 1.75 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
let __aws_sdk_credential_providers = require("@aws-sdk/credential-providers");
__aws_sdk_credential_providers = require_rolldown_runtime.__toESM(__aws_sdk_credential_providers);
let aws4 = require("aws4");
aws4 = require_rolldown_runtime.__toESM(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 (0, __aws_sdk_credential_providers.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
exports.getSignedWebSocketUrl = getSignedWebSocketUrl;
//# sourceMappingURL=getWebSocketUrl.js.map