Skip to content

Commit

Permalink
feat: hybrid cjs and esm support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie committed Jun 24, 2024
1 parent 2e789e8 commit b431545
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A straight forward implementation of base58check extending upon bs58.
## Example

```javascript
var bs58check = require('bs58check')
import bs58check from 'bs58check'

var decoded = bs58check.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')

Expand Down
18 changes: 18 additions & 0 deletions fixup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

find src/cjs -type f -name "*.js" -exec bash -c 'mv "$0" "${0%.js}.cjs"' {} \;

replace_in_file() {
local file="$1"
local regex="$2"
local replacement="$3"

sed -i "s/${regex}/${replacement}/g" "$file"
}

search_pattern='__importDefault(require("\([^"]*\)\.js"));'
replace_pattern='__importDefault(require("\1.cjs"));'

find src/cjs -type f -name "*.cjs" | while read -r file; do
replace_in_file "$file" "$search_pattern" "$replace_pattern"
done
29 changes: 12 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"build": "npm run clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json",
"clean": "rimraf src",
"gitdiff": "npm run build && git diff --exit-code",
"postbuild": "chmod +x ./fixup.sh && ./fixup.sh",
"coverage-report": "c8 report --reporter=lcov",
"coverage": "c8 --check-coverage --branches 90 --functions 90 npm run unit",
"standard": "ts-standard --ignore test --ignore src",
Expand All @@ -52,7 +53,7 @@
},
"dependencies": {
"@noble/hashes": "^1.2.0",
"bs58": "../bs58"
"bs58": "^6.0.0"
},
"devDependencies": {
"@types/node": "^20.14.8",
Expand Down
5 changes: 3 additions & 2 deletions src/cjs/base.js → src/cjs/base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function default_1(checksumFn) {
var payload = buffer.slice(0, -4);
var checksum = buffer.slice(-4);
var newChecksum = checksumFn(payload);
// eslint-disable-next-line
if (checksum[0] ^ newChecksum[0] |
checksum[1] ^ newChecksum[1] |
checksum[2] ^ newChecksum[2] |
Expand All @@ -30,14 +31,14 @@ function default_1(checksumFn) {
// Decode a base58-check encoded string to a buffer, no result if checksum is wrong
function decodeUnsafe(str) {
var buffer = bs58_1.default.decodeUnsafe(str);
if (!buffer)
if (buffer == null)
return;
return decodeRaw(buffer);
}
function decode(str) {
var buffer = bs58_1.default.decode(str);
var payload = decodeRaw(buffer);
if (!payload)
if (payload == null)
throw new Error('Invalid checksum');
return payload;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cjs/index.js → src/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
var sha256_1 = require("@noble/hashes/sha256");
var base_js_1 = __importDefault(require("./base.js"));
var base_js_1 = __importDefault(require("./base.cjs"));
// SHA256(SHA256(buffer))
function sha256x2(buffer) {
return (0, sha256_1.sha256)((0, sha256_1.sha256)(buffer));
Expand Down
5 changes: 3 additions & 2 deletions src/esm/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function (checksumFn) {
var payload = buffer.slice(0, -4);
var checksum = buffer.slice(-4);
var newChecksum = checksumFn(payload);
// eslint-disable-next-line
if (checksum[0] ^ newChecksum[0] |
checksum[1] ^ newChecksum[1] |
checksum[2] ^ newChecksum[2] |
Expand All @@ -25,14 +26,14 @@ export default function (checksumFn) {
// Decode a base58-check encoded string to a buffer, no result if checksum is wrong
function decodeUnsafe(str) {
var buffer = base58.decodeUnsafe(str);
if (!buffer)
if (buffer == null)
return;
return decodeRaw(buffer);
}
function decode(str) {
var buffer = base58.decode(str);
var payload = decodeRaw(buffer);
if (!payload)
if (payload == null)
throw new Error('Invalid checksum');
return payload;
}
Expand Down

0 comments on commit b431545

Please sign in to comment.