multiformats
Advanced tools
Comparing version
@@ -14,2 +14,3 @@ 'use strict'; | ||
var invalidMultihash = require('./fixtures/invalid-multihash.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
var cid = require('../src/cid.js'); | ||
@@ -32,12 +33,2 @@ var varint = require('../src/varint.js'); | ||
}; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
const testThrowAny = async fn => { | ||
@@ -86,3 +77,3 @@ try { | ||
test('throws on invalid BS58Str multihash ', async () => { | ||
const msg = 'Non-base58 character'; | ||
const msg = 'Non-base58btc character'; | ||
await testThrow(() => cid.CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
@@ -89,0 +80,0 @@ }); |
@@ -5,2 +5,5 @@ 'use strict'; | ||
var assert = require('assert'); | ||
var base2 = require('../src/bases/base2.js'); | ||
var base8 = require('../src/bases/base8.js'); | ||
var base10 = require('../src/bases/base10.js'); | ||
var base16$1 = require('../src/bases/base16.js'); | ||
@@ -11,2 +14,3 @@ var base32$1 = require('../src/bases/base32.js'); | ||
var base64$1 = require('../src/bases/base64.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
@@ -25,12 +29,2 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
const test = it; | ||
const testThrow = (fn, message) => { | ||
try { | ||
fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multibase', () => { | ||
@@ -63,3 +57,3 @@ for (const base of [ | ||
const str = base.prefix + '#$%^&*&^%$#'; | ||
const msg = base === base58btc ? 'Non-base58 character' : `invalid ${ base.name } character`; | ||
const msg = `Non-${ base.name } character`; | ||
testThrow(() => base.decode(str), msg); | ||
@@ -105,2 +99,11 @@ }); | ||
}; | ||
describe('base2', () => { | ||
baseTest(base2); | ||
}); | ||
describe('base8', () => { | ||
baseTest(base8); | ||
}); | ||
describe('base10', () => { | ||
baseTest(base10); | ||
}); | ||
describe('base16', () => { | ||
@@ -144,4 +147,4 @@ baseTest(base16$1); | ||
])); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'unexpected end of data'); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
}); | ||
}); |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var json = require('../src/codecs/json.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
@@ -15,12 +16,2 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
const test = it; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multicodec', () => { | ||
@@ -27,0 +18,0 @@ test('encode/decode raw', () => { |
@@ -14,2 +14,3 @@ 'use strict'; | ||
var invalidMultihash = require('./fixtures/invalid-multihash.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
var cid = require('../src/cid.js'); | ||
@@ -32,12 +33,2 @@ var varint = require('../src/varint.js'); | ||
}; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
const testThrowAny = async fn => { | ||
@@ -86,3 +77,3 @@ try { | ||
test('throws on invalid BS58Str multihash ', async () => { | ||
const msg = 'Non-base58 character'; | ||
const msg = 'Non-base58btc character'; | ||
await testThrow(() => cid.CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
@@ -89,0 +80,0 @@ }); |
@@ -5,2 +5,5 @@ 'use strict'; | ||
var assert = require('assert'); | ||
var base2 = require('../src/bases/base2.js'); | ||
var base8 = require('../src/bases/base8.js'); | ||
var base10 = require('../src/bases/base10.js'); | ||
var base16$1 = require('../src/bases/base16.js'); | ||
@@ -11,2 +14,3 @@ var base32$1 = require('../src/bases/base32.js'); | ||
var base64$1 = require('../src/bases/base64.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
@@ -25,12 +29,2 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
const test = it; | ||
const testThrow = (fn, message) => { | ||
try { | ||
fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multibase', () => { | ||
@@ -63,3 +57,3 @@ for (const base of [ | ||
const str = base.prefix + '#$%^&*&^%$#'; | ||
const msg = base === base58btc ? 'Non-base58 character' : `invalid ${ base.name } character`; | ||
const msg = `Non-${ base.name } character`; | ||
testThrow(() => base.decode(str), msg); | ||
@@ -105,2 +99,11 @@ }); | ||
}; | ||
describe('base2', () => { | ||
baseTest(base2); | ||
}); | ||
describe('base8', () => { | ||
baseTest(base8); | ||
}); | ||
describe('base10', () => { | ||
baseTest(base10); | ||
}); | ||
describe('base16', () => { | ||
@@ -144,4 +147,4 @@ baseTest(base16$1); | ||
])); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'unexpected end of data'); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
}); | ||
}); |
@@ -7,2 +7,3 @@ 'use strict'; | ||
var json = require('../src/codecs/json.js'); | ||
var testThrow = require('./fixtures/test-throw.js'); | ||
@@ -15,12 +16,2 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
const test = it; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multicodec', () => { | ||
@@ -27,0 +18,0 @@ test('encode/decode raw', () => { |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var baseX = require('../../vendor/base-x.js'); | ||
var baseX$1 = require('../../vendor/base-x.js'); | ||
var bytes = require('../bytes.js'); | ||
@@ -89,8 +89,10 @@ | ||
const from = ({name, prefix, encode, decode}) => new Codec(name, prefix, encode, decode); | ||
const implement = alphabet => { | ||
const {encode, decode} = baseX(alphabet); | ||
return { | ||
const baseX = ({prefix, name, alphabet}) => { | ||
const {encode, decode} = baseX$1(alphabet, name); | ||
return from({ | ||
prefix, | ||
name, | ||
encode, | ||
decode: text => bytes.coerce(decode(text)) | ||
}; | ||
}); | ||
}; | ||
@@ -113,3 +115,3 @@ const decode = (string, alphabet, bitsPerChar, name) => { | ||
if (value === undefined) { | ||
throw new SyntaxError(`invalid ${ name } character`); | ||
throw new SyntaxError(`Non-${ name } character`); | ||
} | ||
@@ -124,3 +126,3 @@ buffer = buffer << bitsPerChar | value; | ||
if (bits >= bitsPerChar || 255 & buffer << 8 - bits) { | ||
throw new SyntaxError('unexpected end of data'); | ||
throw new SyntaxError('Unexpected end of data'); | ||
} | ||
@@ -167,4 +169,4 @@ return out; | ||
exports.Codec = Codec; | ||
exports.baseX = baseX; | ||
exports.from = from; | ||
exports.implement = implement; | ||
exports.rfc4648 = rfc4648; |
@@ -22,3 +22,3 @@ 'use strict'; | ||
name: 'base32pad', | ||
alphabet: 'abcdefghijklmnopqrstuvwxyz234567', | ||
alphabet: 'abcdefghijklmnopqrstuvwxyz234567=', | ||
bitsPerChar: 5 | ||
@@ -29,3 +29,3 @@ }); | ||
name: 'base32padupper', | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=', | ||
bitsPerChar: 5 | ||
@@ -48,3 +48,3 @@ }); | ||
name: 'base32hexpad', | ||
alphabet: '0123456789abcdefghijklmnopqrstuv', | ||
alphabet: '0123456789abcdefghijklmnopqrstuv=', | ||
bitsPerChar: 5 | ||
@@ -55,3 +55,3 @@ }); | ||
name: 'base32hexpadupper', | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV', | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=', | ||
bitsPerChar: 5 | ||
@@ -58,0 +58,0 @@ }); |
@@ -7,11 +7,11 @@ 'use strict'; | ||
const base36 = base.from({ | ||
const base36 = base.baseX({ | ||
prefix: 'k', | ||
name: 'base36', | ||
...base.implement('0123456789abcdefghijklmnopqrstuvwxyz') | ||
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz' | ||
}); | ||
const base36upper = base.from({ | ||
const base36upper = base.baseX({ | ||
prefix: 'K', | ||
name: 'base36upper', | ||
...base.implement('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
}); | ||
@@ -18,0 +18,0 @@ |
@@ -7,11 +7,11 @@ 'use strict'; | ||
const base58btc = base.from({ | ||
const base58btc = base.baseX({ | ||
name: 'base58btc', | ||
prefix: 'z', | ||
...base.implement('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz') | ||
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||
}); | ||
const base58flickr = base.from({ | ||
const base58flickr = base.baseX({ | ||
name: 'base58flickr', | ||
prefix: 'Z', | ||
...base.implement('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ') | ||
alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' | ||
}); | ||
@@ -18,0 +18,0 @@ |
@@ -5,2 +5,6 @@ 'use strict'; | ||
var identity = require('./bases/identity.js'); | ||
var base2 = require('./bases/base2.js'); | ||
var base8 = require('./bases/base8.js'); | ||
var base10 = require('./bases/base10.js'); | ||
var base16 = require('./bases/base16.js'); | ||
@@ -12,3 +16,3 @@ var base32 = require('./bases/base32.js'); | ||
var sha2 = require('./hashes/sha2.js'); | ||
var identity = require('./hashes/identity.js'); | ||
var identity$1 = require('./hashes/identity.js'); | ||
var raw = require('./codecs/raw.js'); | ||
@@ -24,2 +28,6 @@ var json = require('./codecs/json.js'); | ||
const bases = { | ||
...identity, | ||
...base2, | ||
...base8, | ||
...base10, | ||
...base16, | ||
@@ -33,3 +41,3 @@ ...base32, | ||
...sha2, | ||
...identity | ||
...identity$1 | ||
}; | ||
@@ -36,0 +44,0 @@ const codecs = { |
'use strict'; | ||
function base(ALPHABET) { | ||
function base(ALPHABET, name) { | ||
if (ALPHABET.length >= 255) { | ||
@@ -125,3 +125,3 @@ throw new TypeError('Alphabet too long'); | ||
} | ||
throw new Error('Non-base' + BASE + ' character'); | ||
throw new Error(`Non-${ name } character`); | ||
} | ||
@@ -128,0 +128,0 @@ return { |
@@ -22,2 +22,3 @@ import OLDCID from 'cids'; | ||
import invalidMultihash from './fixtures/invalid-multihash.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
const test = it; | ||
@@ -31,12 +32,2 @@ const same = (actual, expected) => { | ||
}; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
const testThrowAny = async fn => { | ||
@@ -85,3 +76,3 @@ try { | ||
test('throws on invalid BS58Str multihash ', async () => { | ||
const msg = 'Non-base58 character'; | ||
const msg = 'Non-base58btc character'; | ||
await testThrow(() => CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
@@ -88,0 +79,0 @@ }); |
import * as bytes from '../src/bytes.js'; | ||
import assert from 'assert'; | ||
import * as b2 from '../src/bases/base2.js'; | ||
import * as b8 from '../src/bases/base8.js'; | ||
import * as b10 from '../src/bases/base10.js'; | ||
import * as b16 from '../src/bases/base16.js'; | ||
@@ -8,2 +11,3 @@ import * as b32 from '../src/bases/base32.js'; | ||
import * as b64 from '../src/bases/base64.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
const {base16, base32, base58btc, base64} = { | ||
@@ -17,12 +21,2 @@ ...b16, | ||
const test = it; | ||
const testThrow = (fn, message) => { | ||
try { | ||
fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multibase', () => { | ||
@@ -55,3 +49,3 @@ for (const base of [ | ||
const str = base.prefix + '#$%^&*&^%$#'; | ||
const msg = base === base58btc ? 'Non-base58 character' : `invalid ${ base.name } character`; | ||
const msg = `Non-${ base.name } character`; | ||
testThrow(() => base.decode(str), msg); | ||
@@ -97,2 +91,11 @@ }); | ||
}; | ||
describe('base2', () => { | ||
baseTest(b2); | ||
}); | ||
describe('base8', () => { | ||
baseTest(b8); | ||
}); | ||
describe('base10', () => { | ||
baseTest(b10); | ||
}); | ||
describe('base16', () => { | ||
@@ -136,4 +139,4 @@ baseTest(b16); | ||
])); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'unexpected end of data'); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
}); | ||
}); |
@@ -5,14 +5,5 @@ import * as bytes from '../src/bytes.js'; | ||
import * as json from '../src/codecs/json.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
const same = assert.deepStrictEqual; | ||
const test = it; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multicodec', () => { | ||
@@ -19,0 +10,0 @@ test('encode/decode raw', () => { |
@@ -22,2 +22,3 @@ import OLDCID from 'cids'; | ||
import invalidMultihash from './fixtures/invalid-multihash.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
const test = it; | ||
@@ -31,12 +32,2 @@ const same = (actual, expected) => { | ||
}; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
const testThrowAny = async fn => { | ||
@@ -85,3 +76,3 @@ try { | ||
test('throws on invalid BS58Str multihash ', async () => { | ||
const msg = 'Non-base58 character'; | ||
const msg = 'Non-base58btc character'; | ||
await testThrow(() => CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg); | ||
@@ -88,0 +79,0 @@ }); |
import * as bytes from '../src/bytes.js'; | ||
import assert from 'assert'; | ||
import * as b2 from '../src/bases/base2.js'; | ||
import * as b8 from '../src/bases/base8.js'; | ||
import * as b10 from '../src/bases/base10.js'; | ||
import * as b16 from '../src/bases/base16.js'; | ||
@@ -8,2 +11,3 @@ import * as b32 from '../src/bases/base32.js'; | ||
import * as b64 from '../src/bases/base64.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
const {base16, base32, base58btc, base64} = { | ||
@@ -17,12 +21,2 @@ ...b16, | ||
const test = it; | ||
const testThrow = (fn, message) => { | ||
try { | ||
fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multibase', () => { | ||
@@ -55,3 +49,3 @@ for (const base of [ | ||
const str = base.prefix + '#$%^&*&^%$#'; | ||
const msg = base === base58btc ? 'Non-base58 character' : `invalid ${ base.name } character`; | ||
const msg = `Non-${ base.name } character`; | ||
testThrow(() => base.decode(str), msg); | ||
@@ -97,2 +91,11 @@ }); | ||
}; | ||
describe('base2', () => { | ||
baseTest(b2); | ||
}); | ||
describe('base8', () => { | ||
baseTest(b8); | ||
}); | ||
describe('base10', () => { | ||
baseTest(b10); | ||
}); | ||
describe('base16', () => { | ||
@@ -136,4 +139,4 @@ baseTest(b16); | ||
])); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'unexpected end of data'); | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data'); | ||
}); | ||
}); |
@@ -5,14 +5,5 @@ import * as bytes from '../src/bytes.js'; | ||
import * as json from '../src/codecs/json.js'; | ||
import testThrow from './fixtures/test-throw.js'; | ||
const same = assert.deepStrictEqual; | ||
const test = it; | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
if (e.message !== message) | ||
throw e; | ||
return; | ||
} | ||
throw new Error('Test failed to throw'); | ||
}; | ||
describe('multicodec', () => { | ||
@@ -19,0 +10,0 @@ test('encode/decode raw', () => { |
@@ -1,2 +0,2 @@ | ||
import baseX from '../../vendor/base-x.js'; | ||
import basex from '../../vendor/base-x.js'; | ||
import { coerce } from '../bytes.js'; | ||
@@ -83,8 +83,10 @@ class Encoder { | ||
export const from = ({name, prefix, encode, decode}) => new Codec(name, prefix, encode, decode); | ||
export const implement = alphabet => { | ||
const {encode, decode} = baseX(alphabet); | ||
return { | ||
export const baseX = ({prefix, name, alphabet}) => { | ||
const {encode, decode} = basex(alphabet, name); | ||
return from({ | ||
prefix, | ||
name, | ||
encode, | ||
decode: text => coerce(decode(text)) | ||
}; | ||
}); | ||
}; | ||
@@ -107,3 +109,3 @@ const decode = (string, alphabet, bitsPerChar, name) => { | ||
if (value === undefined) { | ||
throw new SyntaxError(`invalid ${ name } character`); | ||
throw new SyntaxError(`Non-${ name } character`); | ||
} | ||
@@ -118,3 +120,3 @@ buffer = buffer << bitsPerChar | value; | ||
if (bits >= bitsPerChar || 255 & buffer << 8 - bits) { | ||
throw new SyntaxError('unexpected end of data'); | ||
throw new SyntaxError('Unexpected end of data'); | ||
} | ||
@@ -121,0 +123,0 @@ return out; |
@@ -17,3 +17,3 @@ import { rfc4648 } from './base.js'; | ||
name: 'base32pad', | ||
alphabet: 'abcdefghijklmnopqrstuvwxyz234567', | ||
alphabet: 'abcdefghijklmnopqrstuvwxyz234567=', | ||
bitsPerChar: 5 | ||
@@ -24,3 +24,3 @@ }); | ||
name: 'base32padupper', | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=', | ||
bitsPerChar: 5 | ||
@@ -43,3 +43,3 @@ }); | ||
name: 'base32hexpad', | ||
alphabet: '0123456789abcdefghijklmnopqrstuv', | ||
alphabet: '0123456789abcdefghijklmnopqrstuv=', | ||
bitsPerChar: 5 | ||
@@ -50,3 +50,3 @@ }); | ||
name: 'base32hexpadupper', | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV', | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=', | ||
bitsPerChar: 5 | ||
@@ -53,0 +53,0 @@ }); |
@@ -1,14 +0,11 @@ | ||
import { | ||
from, | ||
implement | ||
} from './base.js'; | ||
export const base36 = from({ | ||
import { baseX } from './base.js'; | ||
export const base36 = baseX({ | ||
prefix: 'k', | ||
name: 'base36', | ||
...implement('0123456789abcdefghijklmnopqrstuvwxyz') | ||
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz' | ||
}); | ||
export const base36upper = from({ | ||
export const base36upper = baseX({ | ||
prefix: 'K', | ||
name: 'base36upper', | ||
...implement('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
}); |
@@ -1,14 +0,11 @@ | ||
import { | ||
from, | ||
implement | ||
} from './base.js'; | ||
export const base58btc = from({ | ||
import { baseX } from './base.js'; | ||
export const base58btc = baseX({ | ||
name: 'base58btc', | ||
prefix: 'z', | ||
...implement('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz') | ||
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||
}); | ||
export const base58flickr = from({ | ||
export const base58flickr = baseX({ | ||
name: 'base58flickr', | ||
prefix: 'Z', | ||
...implement('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ') | ||
alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' | ||
}); |
@@ -0,1 +1,5 @@ | ||
import * as identityBase from './bases/identity.js'; | ||
import * as base2 from './bases/base2.js'; | ||
import * as base8 from './bases/base8.js'; | ||
import * as base10 from './bases/base10.js'; | ||
import * as base16 from './bases/base16.js'; | ||
@@ -18,2 +22,6 @@ import * as base32 from './bases/base32.js'; | ||
const bases = { | ||
...identityBase, | ||
...base2, | ||
...base8, | ||
...base10, | ||
...base16, | ||
@@ -20,0 +28,0 @@ ...base32, |
@@ -1,2 +0,2 @@ | ||
function base(ALPHABET) { | ||
function base(ALPHABET, name) { | ||
if (ALPHABET.length >= 255) { | ||
@@ -123,3 +123,3 @@ throw new TypeError('Alphabet too long'); | ||
} | ||
throw new Error('Non-base' + BASE + ' character'); | ||
throw new Error(`Non-${ name } character`); | ||
} | ||
@@ -126,0 +126,0 @@ return { |
{ | ||
"name": "multiformats", | ||
"version": "9.3.1", | ||
"version": "9.4.0", | ||
"description": "Interface for multihash, multicodec, multibase and CID", | ||
@@ -58,2 +58,22 @@ "main": "./cjs/src/index.js", | ||
}, | ||
"./bases/identity": { | ||
"browser": "./esm/src/bases/identity.js", | ||
"require": "./cjs/src/bases/identity.js", | ||
"import": "./esm/src/bases/identity.js" | ||
}, | ||
"./bases/base2": { | ||
"browser": "./esm/src/bases/base2.js", | ||
"require": "./cjs/src/bases/base2.js", | ||
"import": "./esm/src/bases/base2.js" | ||
}, | ||
"./bases/base8": { | ||
"browser": "./esm/src/bases/base8.js", | ||
"require": "./cjs/src/bases/base8.js", | ||
"import": "./esm/src/bases/base8.js" | ||
}, | ||
"./bases/base10": { | ||
"browser": "./esm/src/bases/base10.js", | ||
"require": "./cjs/src/bases/base10.js", | ||
"import": "./esm/src/bases/base10.js" | ||
}, | ||
"./bases/base16": { | ||
@@ -161,2 +181,6 @@ "browser": "./esm/src/bases/base16.js", | ||
"./block": "./cjs/src/block.js", | ||
"./bases/identity": "./cjs/src/bases/identity.js", | ||
"./bases/base2": "./cjs/src/bases/base2.js", | ||
"./bases/base8": "./cjs/src/bases/base8.js", | ||
"./bases/base10": "./cjs/src/bases/base10.js", | ||
"./bases/base16": "./cjs/src/bases/base16.js", | ||
@@ -163,0 +187,0 @@ "./bases/base32": "./cjs/src/bases/base32.js", |
@@ -1,2 +0,2 @@ | ||
import baseX from '../../vendor/base-x.js' | ||
import basex from '../../vendor/base-x.js' | ||
import { coerce } from '../bytes.js' | ||
@@ -237,7 +237,15 @@ | ||
/** | ||
* @param {string} alphabet | ||
* @template {string} Base | ||
* @template {string} Prefix | ||
* @param {Object} options | ||
* @param {Base} options.name | ||
* @param {Prefix} options.prefix | ||
* @param {string} options.alphabet | ||
* @returns {Codec<Base, Prefix>} | ||
*/ | ||
export const implement = (alphabet) => { | ||
const { encode, decode } = baseX(alphabet) | ||
return { | ||
export const baseX = ({ prefix, name, alphabet }) => { | ||
const { encode, decode } = basex(alphabet, name) | ||
return from({ | ||
prefix, | ||
name, | ||
encode, | ||
@@ -248,3 +256,3 @@ /** | ||
decode: text => coerce(decode(text)) | ||
} | ||
}) | ||
} | ||
@@ -284,3 +292,3 @@ | ||
if (value === undefined) { | ||
throw new SyntaxError(`invalid ${name} character`) | ||
throw new SyntaxError(`Non-${name} character`) | ||
} | ||
@@ -301,3 +309,3 @@ | ||
if (bits >= bitsPerChar || 0xff & (buffer << (8 - bits))) { | ||
throw new SyntaxError('unexpected end of data') | ||
throw new SyntaxError('Unexpected end of data') | ||
} | ||
@@ -304,0 +312,0 @@ |
@@ -20,3 +20,3 @@ import { rfc4648 } from './base.js' | ||
name: 'base32pad', | ||
alphabet: 'abcdefghijklmnopqrstuvwxyz234567', | ||
alphabet: 'abcdefghijklmnopqrstuvwxyz234567=', | ||
bitsPerChar: 5 | ||
@@ -28,3 +28,3 @@ }) | ||
name: 'base32padupper', | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', | ||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=', | ||
bitsPerChar: 5 | ||
@@ -50,3 +50,3 @@ }) | ||
name: 'base32hexpad', | ||
alphabet: '0123456789abcdefghijklmnopqrstuv', | ||
alphabet: '0123456789abcdefghijklmnopqrstuv=', | ||
bitsPerChar: 5 | ||
@@ -58,3 +58,3 @@ }) | ||
name: 'base32hexpadupper', | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV', | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=', | ||
bitsPerChar: 5 | ||
@@ -61,0 +61,0 @@ }) |
@@ -1,13 +0,13 @@ | ||
import { from, implement } from './base.js' | ||
import { baseX } from './base.js' | ||
export const base36 = from({ | ||
export const base36 = baseX({ | ||
prefix: 'k', | ||
name: 'base36', | ||
...implement('0123456789abcdefghijklmnopqrstuvwxyz') | ||
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz' | ||
}) | ||
export const base36upper = from({ | ||
export const base36upper = baseX({ | ||
prefix: 'K', | ||
name: 'base36upper', | ||
...implement('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') | ||
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
}) |
@@ -1,13 +0,13 @@ | ||
import { from, implement } from './base.js' | ||
import { baseX } from './base.js' | ||
export const base58btc = from({ | ||
export const base58btc = baseX({ | ||
name: 'base58btc', | ||
prefix: 'z', | ||
...implement('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz') | ||
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||
}) | ||
export const base58flickr = from({ | ||
export const base58flickr = baseX({ | ||
name: 'base58flickr', | ||
prefix: 'Z', | ||
...implement('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ') | ||
alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' | ||
}) |
// @ts-check | ||
import * as identityBase from './bases/identity.js' | ||
import * as base2 from './bases/base2.js' | ||
import * as base8 from './bases/base8.js' | ||
import * as base10 from './bases/base10.js' | ||
import * as base16 from './bases/base16.js' | ||
@@ -16,3 +20,3 @@ import * as base32 from './bases/base32.js' | ||
const bases = { ...base16, ...base32, ...base36, ...base58, ...base64 } | ||
const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64 } | ||
const hashes = { ...sha2, ...identity } | ||
@@ -19,0 +23,0 @@ const codecs = { raw, json } |
@@ -14,2 +14,3 @@ /* globals describe, it */ | ||
import invalidMultihash from './fixtures/invalid-multihash.js' | ||
import testThrow from './fixtures/test-throw.js' | ||
@@ -25,14 +26,2 @@ const test = it | ||
// eslint-disable-next-line no-unused-vars | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn() | ||
} catch (e) { | ||
if (e.message !== message) throw e | ||
return | ||
} | ||
/* c8 ignore next */ | ||
throw new Error('Test failed to throw') | ||
} | ||
const testThrowAny = async fn => { | ||
@@ -85,3 +74,3 @@ try { | ||
test('throws on invalid BS58Str multihash ', async () => { | ||
const msg = 'Non-base58 character' | ||
const msg = 'Non-base58btc character' | ||
await testThrow(() => CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg) | ||
@@ -88,0 +77,0 @@ }) |
/* globals describe, it */ | ||
import * as bytes from '../src/bytes.js' | ||
import assert from 'assert' | ||
import * as b2 from 'multiformats/bases/base2' | ||
import * as b8 from 'multiformats/bases/base8' | ||
import * as b10 from 'multiformats/bases/base10' | ||
import * as b16 from 'multiformats/bases/base16' | ||
@@ -9,2 +12,3 @@ import * as b32 from 'multiformats/bases/base32' | ||
import * as b64 from 'multiformats/bases/base64' | ||
import testThrow from './fixtures/test-throw.js' | ||
@@ -16,13 +20,2 @@ const { base16, base32, base58btc, base64 } = { ...b16, ...b32, ...b58, ...b64 } | ||
const testThrow = (fn, message) => { | ||
try { | ||
fn() | ||
} catch (e) { | ||
if (e.message !== message) throw e | ||
return | ||
} | ||
/* c8 ignore next */ | ||
throw new Error('Test failed to throw') | ||
} | ||
describe('multibase', () => { | ||
@@ -51,3 +44,3 @@ for (const base of [base16, base32, base58btc, base64]) { | ||
const str = base.prefix + '#$%^&*&^%$#' | ||
const msg = base === base58btc ? 'Non-base58 character' : `invalid ${base.name} character` | ||
const msg = `Non-${base.name} character` | ||
testThrow(() => base.decode(str), msg) | ||
@@ -94,2 +87,11 @@ }) | ||
} | ||
describe('base2', () => { | ||
baseTest(b2) | ||
}) | ||
describe('base8', () => { | ||
baseTest(b8) | ||
}) | ||
describe('base10', () => { | ||
baseTest(b10) | ||
}) | ||
describe('base16', () => { | ||
@@ -140,4 +142,4 @@ baseTest(b16) | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'unexpected end of data') | ||
testThrow(() => base64.decode(b64.substring(0, b64.length - 1)), 'Unexpected end of data') | ||
}) | ||
}) |
@@ -6,2 +6,3 @@ /* globals describe, it */ | ||
import * as json from 'multiformats/codecs/json' | ||
import testThrow from './fixtures/test-throw.js' | ||
@@ -11,13 +12,2 @@ const same = assert.deepStrictEqual | ||
const testThrow = async (fn, message) => { | ||
try { | ||
await fn() | ||
} catch (e) { | ||
if (e.message !== message) throw e | ||
return | ||
} | ||
/* c8 ignore next */ | ||
throw new Error('Test failed to throw') | ||
} | ||
describe('multicodec', () => { | ||
@@ -24,0 +14,0 @@ test('encode/decode raw', () => { |
@@ -45,9 +45,7 @@ /** | ||
}): Codec<Base, Prefix>; | ||
export function implement(alphabet: string): { | ||
encode: (buffer: Uint8Array | number[]) => string; | ||
/** | ||
* @param {string} text | ||
*/ | ||
decode: (text: string) => Uint8Array; | ||
}; | ||
export function baseX<Base extends string, Prefix extends string>({ prefix, name, alphabet }: { | ||
name: Base; | ||
prefix: Prefix; | ||
alphabet: string; | ||
}): Codec<Base, Prefix>; | ||
export function rfc4648({ name, prefix, bitsPerChar, alphabet }: { | ||
@@ -54,0 +52,0 @@ name: string; |
@@ -31,2 +31,6 @@ import { CID } from "./index.js"; | ||
base16upper: import("./bases/base.js").Codec<string, string>; | ||
base10: import("./bases/base.js").Codec<"base10", "9">; | ||
base8: import("./bases/base.js").Codec<string, string>; | ||
base2: import("./bases/base.js").Codec<string, string>; | ||
identity: import("./bases/base.js").Codec<"identity", "\0">; | ||
}; | ||
@@ -33,0 +37,0 @@ export namespace codecs { |
@@ -1,2 +0,2 @@ | ||
declare function base(ALPHABET: string): base.BaseConverter; | ||
declare function base(ALPHABET: string, name: string): base.BaseConverter; | ||
export = base; | ||
@@ -3,0 +3,0 @@ declare namespace base { |
@@ -6,3 +6,3 @@ // base-x encoding / decoding | ||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
function base (ALPHABET) { | ||
function base (ALPHABET, name) { | ||
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') } | ||
@@ -116,3 +116,3 @@ var BASE_MAP = new Uint8Array(256); | ||
if (buffer) { return buffer } | ||
throw new Error('Non-base' + BASE + ' character') | ||
throw new Error(`Non-${name} character`) | ||
} | ||
@@ -119,0 +119,0 @@ return { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
432334
15.7%188
18.99%13523
23.7%