From 38a42b354312b0e825aab647f929a88ea3025202 Mon Sep 17 00:00:00 2001 From: Prakash Senthil Vel <23444145+prakashsvmx@users.noreply.github.com> Date: Thu, 25 Apr 2024 08:50:02 +0530 Subject: [PATCH] uri escape to comply with s3 (#1280) --- src/internal/helper.ts | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/src/internal/helper.ts b/src/internal/helper.ts index a41c9bc3..b3c3063c 100644 --- a/src/internal/helper.ts +++ b/src/internal/helper.ts @@ -38,38 +38,10 @@ export function hashBinary(buf: Buffer, enableSHA256: boolean) { return { md5sum, sha256sum } } -/** - * All characters in string which are NOT unreserved should be percent encoded. - * Unreserved characters are : ALPHA / DIGIT / "-" / "." / "_" / "~" - * Reference https://tools.ietf.org/html/rfc3986#section-2.2 - */ -export function uriEscape(string: string) { - return string.split('').reduce((acc: string, elem: string) => { - const buf = Buffer.from(elem) - if (buf.length === 1) { - // length 1 indicates that elem is not a unicode character. - // Check if it is an unreserved characer. - if ( - ('A' <= elem && elem <= 'Z') || - ('a' <= elem && elem <= 'z') || - ('0' <= elem && elem <= '9') || - elem === '_' || - elem === '.' || - elem === '~' || - elem === '-' - ) { - // Unreserved characer should not be encoded. - acc = acc + elem - return acc - } - } - // elem needs encoding - i.e elem should be encoded if it's not unreserved - // character or if it's a unicode character. - for (const char of buf) { - acc = acc + '%' + char.toString(16).toUpperCase() - } - return acc - }, '') +// S3 percent-encodes some extra non-standard characters in a URI . So comply with S3. +const encodeAsHex = (c: string) => `%${c.charCodeAt(0).toString(16).toUpperCase()}` +export function uriEscape(uriStr: string): string { + return encodeURIComponent(uriStr).replace(/[!'()*]/g, encodeAsHex) } export function uriResourceEscape(string: string) {