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 16, 2024
1 parent dd4c308 commit feb41aa
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 137 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm ci
- run: npm run unit
format:
runs-on: ubuntu-latest
Expand All @@ -25,7 +25,7 @@ jobs:
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm ci
- run: npm run standard
gitdiff:
runs-on: ubuntu-latest
Expand All @@ -35,5 +35,5 @@ jobs:
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm ci
- run: npm run gitdiff
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Base58

``` javascript
var BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var bs58 = require('base-x')(BASE58)
import basex from 'base-x'
var bs58 = basex(BASE58)

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

Expand Down
49 changes: 26 additions & 23 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
'use strict'
var crypto = require('crypto')
var benchmark = require('benchmark')
var XorShift128Plus = require('xorshift.js').XorShift128Plus
const crypto = require('crypto')
const benchmark = require('benchmark')
const XorShift128Plus = require('xorshift.js').XorShift128Plus

var bs58ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var bs58 = require('../')(bs58ALPHABET)
const bs58ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const basex = require('../src/cjs/index.cjs').default
const bs58 = basex(bs58ALPHABET)

var fixtureIndex = 0
var resetFixtureIndex = function () { fixtureIndex = 0 }
var fixtures = new Array(10000)
var getNextFixture = function () {
var fixture = fixtures[fixtureIndex++]
// const bs58 = basex(bs58ALPHABET)

let fixtureIndex = 0
const resetFixtureIndex = function () { fixtureIndex = 0 }
const fixtures = new Array(10000)
const getNextFixture = function () {
const fixture = fixtures[fixtureIndex++]
if (fixtureIndex === fixtures.length) {
fixtureIndex = 0
}

return fixture
}

var seed = process.env.SEED || crypto.randomBytes(16).toString('hex')
const seed = process.env.SEED || crypto.randomBytes(16).toString('hex')
console.log('Seed: ' + seed)
var prng = new XorShift128Plus(seed)
for (var i = 0; i < fixtures.length; ++i) {
let source = prng.randomBytes(32)
const prng = new XorShift128Plus(seed)
for (let i = 0; i < fixtures.length; ++i) {
const source = prng.randomBytes(32)
fixtures[i] = { source, string: bs58.encode(source) }
}

Expand All @@ -49,12 +52,12 @@ new benchmark.Suite({
console.log('==================================================')
}
})
.add('encode', function () {
var fixture = getNextFixture()
bs58.encode(fixture.source)
}, {onStart: resetFixtureIndex, onCycle: resetFixtureIndex})
.add('decode', function () {
var fixture = getNextFixture()
bs58.decode(fixture.string)
}, {onStart: resetFixtureIndex, onCycle: resetFixtureIndex})
.run()
.add('encode', function () {
const fixture = getNextFixture()
bs58.encode(fixture.source)
}, { onStart: resetFixtureIndex, onCycle: resetFixtureIndex })
.add('decode', function () {
const fixture = getNextFixture()
bs58.decode(fixture.string)
}, { onStart: resetFixtureIndex, onCycle: resetFixtureIndex })
.run()
24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "base-x",
"version": "4.0.0",
"description": "Fast base encoding / decoding of any given alphabet",
"type": "module",
"keywords": [
"base-x",
"base58",
Expand All @@ -23,24 +24,33 @@
"files": [
"src"
],
"main": "src/index.js",
"types": "src/index.d.ts",
"main": "src/cjs/index.cjs",
"module": "src/esm/index.js",
"types": "src/cjs/index.d.ts",
"exports": {
".": {
"require": "./src/cjs/index.cjs",
"import": "./src/esm/index.js",
"types": "./src/cjs/index.d.ts"
}
},
"repository": {
"type": "git",
"url": "https://github.com/cryptocoinjs/base-x.git"
},
"scripts": {
"build": "tsc -p ./tsconfig.json ; standard --fix",
"build": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json; standard --fix --ignore test",
"postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;",
"gitdiff": "npm run build && git diff --exit-code",
"prepublish": "npm run gitdiff",
"standard": "standard",
"test": "npm run unit && npm run standard",
"test": "npm run unit && npm run standard -- --ignore test",
"unit": "tape test/*.js"
},
"devDependencies": {
"@types/node": "12.0.10",
"standard": "^10.0.3",
"tape": "^4.5.1",
"typescript": "3.5.2"
"standard": "^17.1.0",
"tape": "^5.3.0",
"typescript": "^5.4.5"
}
}
100 changes: 50 additions & 50 deletions src/index.js → src/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
Object.defineProperty(exports, '__esModule', { value: true })
function base (ALPHABET) {
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
var BASE_MAP = new Uint8Array(256)
for (var j = 0; j < BASE_MAP.length; j++) {
const BASE_MAP = new Uint8Array(256)
for (let j = 0; j < BASE_MAP.length; j++) {
BASE_MAP[j] = 255
}
for (var i = 0; i < ALPHABET.length; i++) {
var x = ALPHABET.charAt(i)
var xc = x.charCodeAt(0)
for (let i = 0; i < ALPHABET.length; i++) {
const x = ALPHABET.charAt(i)
const xc = x.charCodeAt(0)
if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
BASE_MAP[xc] = i
}
var BASE = ALPHABET.length
var LEADER = ALPHABET.charAt(0)
var FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
var iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
const BASE = ALPHABET.length
const LEADER = ALPHABET.charAt(0)
const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
function encode (source) {
if (source instanceof Uint8Array) {
} else if (ArrayBuffer.isView(source)) {
if (ArrayBuffer.isView(source)) {
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)
} else if (Array.isArray(source)) {
source = Uint8Array.from(source)
}
if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
if (source.length === 0) { return '' }
// Skip & count leading zeroes.
var zeroes = 0
var length = 0
var pbegin = 0
var pend = source.length
// Skip & count leading zeroes.
let zeroes = 0
let length = 0
let pbegin = 0
const pend = source.length
while (pbegin !== pend && source[pbegin] === 0) {
pbegin++
zeroes++
}
// Allocate enough space in big-endian base58 representation.
var size = ((pend - pbegin) * iFACTOR + 1) >>> 0
var b58 = new Uint8Array(size)
// Process the bytes.
// Allocate enough space in big-endian base58 representation.
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
const b58 = new Uint8Array(size)
// Process the bytes.
while (pbegin !== pend) {
var carry = source[pbegin]
// Apply "b58 = b58 * 256 + ch".
var i = 0
for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
let carry = source[pbegin]
// Apply "b58 = b58 * 256 + ch".
let i = 0
for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
carry += (256 * b58[it1]) >>> 0
b58[it1] = (carry % BASE) >>> 0
carry = (carry / BASE) >>> 0
Expand All @@ -55,38 +55,38 @@ function base (ALPHABET) {
length = i
pbegin++
}
// Skip leading zeroes in base58 result.
var it2 = size - length
// Skip leading zeroes in base58 result.
let it2 = size - length
while (it2 !== size && b58[it2] === 0) {
it2++
}
// Translate the result into a string.
var str = LEADER.repeat(zeroes)
// Translate the result into a string.
let str = LEADER.repeat(zeroes)
for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]) }
return str
}
function decodeUnsafe (source) {
if (typeof source !== 'string') { throw new TypeError('Expected String') }
if (source.length === 0) { return new Uint8Array() }
var psz = 0
// Skip and count leading '1's.
var zeroes = 0
var length = 0
let psz = 0
// Skip and count leading '1's.
let zeroes = 0
let length = 0
while (source[psz] === LEADER) {
zeroes++
psz++
}
// Allocate enough space in big-endian base256 representation.
var size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
var b256 = new Uint8Array(size)
// Process the characters.
// Allocate enough space in big-endian base256 representation.
const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
const b256 = new Uint8Array(size)
// Process the characters.
while (source[psz]) {
// Decode character
var carry = BASE_MAP[source.charCodeAt(psz)]
// Invalid character
// Decode character
let carry = BASE_MAP[source.charCodeAt(psz)]
// Invalid character
if (carry === 255) { return }
var i = 0
for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
let i = 0
for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
carry += (BASE * b256[it3]) >>> 0
b256[it3] = (carry % 256) >>> 0
carry = (carry / 256) >>> 0
Expand All @@ -95,27 +95,27 @@ function base (ALPHABET) {
length = i
psz++
}
// Skip leading zeroes in b256.
var it4 = size - length
// Skip leading zeroes in b256.
let it4 = size - length
while (it4 !== size && b256[it4] === 0) {
it4++
}
var vch = new Uint8Array(zeroes + (size - it4))
var j = zeroes
const vch = new Uint8Array(zeroes + (size - it4))
let j = zeroes
while (it4 !== size) {
vch[j++] = b256[it4++]
}
return vch
}
function decode (string) {
var buffer = decodeUnsafe(string)
const buffer = decodeUnsafe(string)
if (buffer) { return buffer }
throw new Error('Non-base' + BASE + ' character')
}
return {
encode: encode,
decodeUnsafe: decodeUnsafe,
decode: decode
encode,
decodeUnsafe,
decode
}
}
module.exports = base
exports.default = base
2 changes: 1 addition & 1 deletion src/index.d.ts → src/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare function base(ALPHABET: string): base.BaseConverter;
export = base;
export default base;
declare namespace base {
interface BaseConverter {
encode(buffer: Uint8Array | number[]): string;
Expand Down
9 changes: 9 additions & 0 deletions src/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare function base(ALPHABET: string): base.BaseConverter;
export default base;
declare namespace base {
interface BaseConverter {
encode(buffer: Uint8Array | number[]): string;
decodeUnsafe(string: string): Uint8Array | undefined;
decode(string: string): Uint8Array;
}
}
Loading

0 comments on commit feb41aa

Please sign in to comment.