Skip to content

Commit 56f419e

Browse files
committed
fix: do not use URL object for non-rtmp protocol
1 parent 43c5f96 commit 56f419e

File tree

1 file changed

+10
-8
lines changed
  • packages/cloudfront-signer/src

1 file changed

+10
-8
lines changed

packages/cloudfront-signer/src/sign.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ export function getSignedUrl({
136136
baseUrl = resources[0].replace("*://", "https://");
137137
}
138138

139-
const newURL = new URL(baseUrl!);
140-
newURL.search = Array.from(newURL.searchParams.entries())
141-
.concat(Object.entries(cloudfrontSignBuilder.createCloudfrontAttribute()))
139+
const startFlag = baseUrl!.includes("?") ? "&" : "?";
140+
const params = Object.entries(cloudfrontSignBuilder.createCloudfrontAttribute())
142141
.filter(([, value]) => value !== undefined)
143142
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
144143
.join("&");
144+
const urlString = baseUrl + startFlag + params;
145145

146-
return getResource(newURL);
146+
return getResource(urlString);
147147
}
148148

149149
/**
@@ -249,14 +249,16 @@ function getPolicyResources(policy: string | Policy) {
249249
/**
250250
* @internal
251251
*/
252-
function getResource(url: URL): string {
253-
switch (url.protocol) {
252+
function getResource(urlString: string): string {
253+
const protocol = urlString.slice(0, urlString.indexOf("//"));
254+
switch (protocol) {
254255
case "http:":
255256
case "https:":
256257
case "ws:":
257258
case "wss:":
258-
return url.toString();
259+
return urlString;
259260
case "rtmp:":
261+
const url = new URL(urlString);
260262
return url.pathname.replace(/^\//, "") + url.search + url.hash;
261263
default:
262264
throw new Error("Invalid URI scheme. Scheme must be one of http, https, or rtmp");
@@ -420,7 +422,7 @@ class CloudfrontSignBuilder {
420422
if (!url || !dateLessThan) {
421423
return false;
422424
}
423-
const resource = getResource(new URL(url));
425+
const resource = getResource(url);
424426
const parsedDates = this.parseDateWindow(dateLessThan, dateGreaterThan);
425427
this.dateLessThan = parsedDates.dateLessThan;
426428
this.customPolicy = Boolean(parsedDates.dateGreaterThan) || Boolean(ipAddress);

0 commit comments

Comments
 (0)