Skip to content

Commit 3315c6f

Browse files
committed
chore: copied encode function from base62 to avoid dependency
1 parent d93353c commit 3315c6f

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

package-lock.json

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/utilities/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@apify/consts": "^2.37.0",
52-
"@apify/log": "^2.5.13",
53-
"base62": "^2.0.2"
52+
"@apify/log": "^2.5.13"
5453
},
5554
"devDependencies": {
5655
"@types/create-hmac": "^1.1.0"

packages/utilities/src/hmac.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import crypto from 'crypto';
22

3-
import base62 from 'base62';
3+
const CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
4+
5+
/**
6+
* Encodes number to base62.
7+
* To avoid new dependency, this function was copied from https://github.com/base62/base62.js/blob/master/lib/ascii.js
8+
*/
9+
function encodeBase62(num: number) {
10+
if (num === 0) {
11+
return CHARSET[0];
12+
}
13+
14+
let res = '';
15+
while (num > 0) {
16+
res = CHARSET[num % 62] + res;
17+
num = Math.floor(num / 62);
18+
}
19+
return res;
20+
}
421

522
/**
623
* Generates an HMAC signature and encodes it using Base62.
@@ -16,5 +33,5 @@ export function createHmacSignature(secretKey: string, message: string): string
1633
.digest('hex')
1734
.substring(0, 30);
1835

19-
return base62.encode(parseInt(signature, 16));
36+
return encodeBase62(parseInt(signature, 16));
2037
}

0 commit comments

Comments
 (0)