From b4315450aef7a168535bcbe623bc3abd20d7f5b1 Mon Sep 17 00:00:00 2001 From: ayman Date: Mon, 24 Jun 2024 18:16:14 +0530 Subject: [PATCH] feat: hybrid cjs and esm support --- README.md | 2 +- fixup.sh | 18 ++++++++++++++++++ package-lock.json | 29 ++++++++++++----------------- package.json | 3 ++- src/cjs/{base.js => base.cjs} | 5 +++-- src/cjs/{index.js => index.cjs} | 2 +- src/esm/base.js | 5 +++-- 7 files changed, 40 insertions(+), 24 deletions(-) create mode 100755 fixup.sh rename src/cjs/{base.js => base.cjs} (94%) rename src/cjs/{index.js => index.cjs} (88%) diff --git a/README.md b/README.md index f6f7305..58cf6f0 100644 --- a/README.md +++ b/README.md @@ -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') diff --git a/fixup.sh b/fixup.sh new file mode 100755 index 0000000..03203d5 --- /dev/null +++ b/fixup.sh @@ -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 diff --git a/package-lock.json b/package-lock.json index 7cc0722..7e8eb47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@noble/hashes": "^1.2.0", - "bs58": "../bs58" + "bs58": "^6.0.0" }, "devDependencies": { "@types/node": "^20.14.8", @@ -24,20 +24,6 @@ "typescript": "^5.5.2" } }, - "../bs58": { - "version": "5.0.0", - "license": "MIT", - "dependencies": { - "base-x": "^5.0.0" - }, - "devDependencies": { - "@types/node": "^20.14.8", - "rimraf": "^5.0.7", - "tape": "^5.3.0", - "ts-standard": "^12.0.2", - "typescript": "^5.5.2" - } - }, "node_modules/@bcoe/v8-coverage": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz", @@ -933,6 +919,11 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "node_modules/base-x": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-5.0.0.tgz", + "integrity": "sha512-sMW3VGSX1QWVFA6l8U62MLKz29rRfpTlYdCqLdpLo1/Yd4zZwSbnUaDfciIAowAqvq7YFnWq9hrhdg1KYgc1lQ==" + }, "node_modules/bindings": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", @@ -980,8 +971,12 @@ } }, "node_modules/bs58": { - "resolved": "../bs58", - "link": true + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-6.0.0.tgz", + "integrity": "sha512-PD0wEnEYg6ijszw/u8s+iI3H17cTymlrwkKhDhPZq+Sokl3AU4htyBFTjAeNAlCCmg0f53g6ih3jATyCKftTfw==", + "dependencies": { + "base-x": "^5.0.0" + } }, "node_modules/builtins": { "version": "5.1.0", diff --git a/package.json b/package.json index 6006e16..fe7f4c1 100644 --- a/package.json +++ b/package.json @@ -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", @@ -52,7 +53,7 @@ }, "dependencies": { "@noble/hashes": "^1.2.0", - "bs58": "../bs58" + "bs58": "^6.0.0" }, "devDependencies": { "@types/node": "^20.14.8", diff --git a/src/cjs/base.js b/src/cjs/base.cjs similarity index 94% rename from src/cjs/base.js rename to src/cjs/base.cjs index 785e81b..addab50 100644 --- a/src/cjs/base.js +++ b/src/cjs/base.cjs @@ -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] | @@ -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; } diff --git a/src/cjs/index.js b/src/cjs/index.cjs similarity index 88% rename from src/cjs/index.js rename to src/cjs/index.cjs index 221eebc..f456f27 100644 --- a/src/cjs/index.js +++ b/src/cjs/index.cjs @@ -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)); diff --git a/src/esm/base.js b/src/esm/base.js index 7e2da2e..bf3d26e 100644 --- a/src/esm/base.js +++ b/src/esm/base.js @@ -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] | @@ -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; }