Skip to content

Commit

Permalink
ensure all of + and / are replaced to - and _ not only the first occu…
Browse files Browse the repository at this point in the history
…rances; closes issue #2
  • Loading branch information
Granitosaurus committed Jun 21, 2024
1 parent 1cf1fb3 commit 095713a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ describe('urlsafe_b64encode', () => {
it('should encode a string to base64', () => {
expect(urlsafe_b64encode('hello+foo/bar=====')).toBe('aGVsbG8rZm9vL2Jhcj09PT09');
});
it('multiple characters should be encoded correctly', () => {
// XXX: investigate how to produce a string that contains + or / when b64 encoded.
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrapfly-sdk",
"version": "0.5.0",
"version": "0.5.1",
"description": "SDK for Scrapfly.io web scraping service",
"type": "module",
"types": "build/src/main.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export function urlsafe_b64encode(data: string): string {
return Buffer.from(data, 'utf-8').toString('base64').replace('+', '-').replace('/', '_').replace(/=+$/, '');
return Buffer.from(data, 'utf-8').toString('base64')
.replace(/\+/g, '-') // Replace all instances of '+' with '-'
.replace(/\//g, '_') // Replace all instances of '/' with '_'
.replace(/=+$/, ''); // Remove trailing '=' characters
}

0 comments on commit 095713a

Please sign in to comment.