Skip to content

Commit 38a42b3

Browse files
authored
uri escape to comply with s3 (#1280)
1 parent d449fcd commit 38a42b3

File tree

1 file changed

+4
-32
lines changed

1 file changed

+4
-32
lines changed

src/internal/helper.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,10 @@ export function hashBinary(buf: Buffer, enableSHA256: boolean) {
3838
return { md5sum, sha256sum }
3939
}
4040

41-
/**
42-
* All characters in string which are NOT unreserved should be percent encoded.
43-
* Unreserved characters are : ALPHA / DIGIT / "-" / "." / "_" / "~"
44-
* Reference https://tools.ietf.org/html/rfc3986#section-2.2
45-
*/
46-
export function uriEscape(string: string) {
47-
return string.split('').reduce((acc: string, elem: string) => {
48-
const buf = Buffer.from(elem)
49-
if (buf.length === 1) {
50-
// length 1 indicates that elem is not a unicode character.
51-
// Check if it is an unreserved characer.
52-
if (
53-
('A' <= elem && elem <= 'Z') ||
54-
('a' <= elem && elem <= 'z') ||
55-
('0' <= elem && elem <= '9') ||
56-
elem === '_' ||
57-
elem === '.' ||
58-
elem === '~' ||
59-
elem === '-'
60-
) {
61-
// Unreserved characer should not be encoded.
62-
acc = acc + elem
63-
return acc
64-
}
65-
}
66-
// elem needs encoding - i.e elem should be encoded if it's not unreserved
67-
// character or if it's a unicode character.
68-
for (const char of buf) {
69-
acc = acc + '%' + char.toString(16).toUpperCase()
70-
}
71-
return acc
72-
}, '')
41+
// S3 percent-encodes some extra non-standard characters in a URI . So comply with S3.
42+
const encodeAsHex = (c: string) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
43+
export function uriEscape(uriStr: string): string {
44+
return encodeURIComponent(uriStr).replace(/[!'()*]/g, encodeAsHex)
7345
}
7446

7547
export function uriResourceEscape(string: string) {

0 commit comments

Comments
 (0)