Skip to content

Commit 095713a

Browse files
committed
ensure all of + and / are replaced to - and _ not only the first occurances; closes issue #2
1 parent 1cf1fb3 commit 095713a

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

__tests__/utils.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ describe('urlsafe_b64encode', () => {
55
it('should encode a string to base64', () => {
66
expect(urlsafe_b64encode('hello+foo/bar=====')).toBe('aGVsbG8rZm9vL2Jhcj09PT09');
77
});
8+
it('multiple characters should be encoded correctly', () => {
9+
// XXX: investigate how to produce a string that contains + or / when b64 encoded.
10+
});
811
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scrapfly-sdk",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "SDK for Scrapfly.io web scraping service",
55
"type": "module",
66
"types": "build/src/main.d.ts",

src/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export function urlsafe_b64encode(data: string): string {
2-
return Buffer.from(data, 'utf-8').toString('base64').replace('+', '-').replace('/', '_').replace(/=+$/, '');
2+
return Buffer.from(data, 'utf-8').toString('base64')
3+
.replace(/\+/g, '-') // Replace all instances of '+' with '-'
4+
.replace(/\//g, '_') // Replace all instances of '/' with '_'
5+
.replace(/=+$/, ''); // Remove trailing '=' characters
36
}

0 commit comments

Comments
 (0)