Skip to content

Commit 9f7acc6

Browse files
authored
Remove global buffer use (#3)
* Remove global buffer use * Feedback * Rename param
1 parent b9b23e3 commit 9f7acc6

File tree

6 files changed

+174
-96
lines changed

6 files changed

+174
-96
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-exact = true

dist/esm/index.mjs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { base58btc, blake2b256 } from '@chelonia/multiformats';
22
import scrypt from 'scrypt-async';
33
import nacl from 'tweetnacl';
4-
const strToBuf = (str) => {
4+
const bufToStr = (() => {
5+
const textDecoder = new TextDecoder();
6+
return (buf) => {
7+
return textDecoder.decode(buf);
8+
};
9+
})();
10+
const strToBuf = (() => {
511
const textEncoder = new TextEncoder();
6-
return textEncoder.encode(str);
7-
};
12+
return (str) => {
13+
return textEncoder.encode(str);
14+
};
15+
})();
816
const blake32Hash = (data) => {
917
const uint8array = typeof data === 'string' ? strToBuf(data) : data;
1018
const digest = blake2b256.digest(uint8array);
@@ -393,7 +401,7 @@ export const decrypt = (inKey, data, ad) => {
393401
if (!decrypted) {
394402
throw new Error('Could not decrypt message');
395403
}
396-
return Buffer.from(decrypted).toString('utf-8');
404+
return bufToStr(decrypted);
397405
}
398406
else if (key.type === CURVE25519XSALSA20POLY1305) {
399407
if (!key.secretKey) {
@@ -414,7 +422,7 @@ export const decrypt = (inKey, data, ad) => {
414422
if (!decrypted) {
415423
throw new Error('Could not decrypt message');
416424
}
417-
return Buffer.from(decrypted).toString('utf-8');
425+
return bufToStr(decrypted);
418426
}
419427
throw new Error('Unsupported algorithm');
420428
};

dist/umd/index.cjs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1616
const multiformats_1 = require("@chelonia/multiformats");
1717
const scrypt_async_1 = __importDefault(require("scrypt-async"));
1818
const tweetnacl_1 = __importDefault(require("tweetnacl"));
19-
const strToBuf = (str) => {
19+
const bufToStr = (() => {
20+
const textDecoder = new TextDecoder();
21+
return (buf) => {
22+
return textDecoder.decode(buf);
23+
};
24+
})();
25+
const strToBuf = (() => {
2026
const textEncoder = new TextEncoder();
21-
return textEncoder.encode(str);
22-
};
27+
return (str) => {
28+
return textEncoder.encode(str);
29+
};
30+
})();
2331
const blake32Hash = (data) => {
2432
const uint8array = typeof data === 'string' ? strToBuf(data) : data;
2533
const digest = multiformats_1.blake2b256.digest(uint8array);
@@ -418,7 +426,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
418426
if (!decrypted) {
419427
throw new Error('Could not decrypt message');
420428
}
421-
return Buffer.from(decrypted).toString('utf-8');
429+
return bufToStr(decrypted);
422430
}
423431
else if (key.type === exports.CURVE25519XSALSA20POLY1305) {
424432
if (!key.secretKey) {
@@ -439,7 +447,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
439447
if (!decrypted) {
440448
throw new Error('Could not decrypt message');
441449
}
442-
return Buffer.from(decrypted).toString('utf-8');
450+
return bufToStr(decrypted);
443451
}
444452
throw new Error('Unsupported algorithm');
445453
};

0 commit comments

Comments
 (0)