Skip to content

Commit 62f3588

Browse files
committed
feat!: named exports instead of default exports
BREAKING CHANGE: Only use names exports as default exports don't play well with CommonJS + Typescript typing. This means that e.g. the raw codec is not longer imported as ```js const raw = require('multiformats/codecs/raw') import raw from 'multiformats/codecs/raw' but as ```js const { rawCodec } = require('multiformats/codec/raw') import { rawCodec } from 'multiformats/codecs/raw' ```
1 parent 5fccaba commit 62f3588

File tree

13 files changed

+35
-36
lines changed

13 files changed

+35
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Library provides implementations for most basics and many others can be found in
99
## Interfaces
1010

1111
```js
12-
import CID from 'multiformats/cid'
13-
import json from 'multiformats/codecs/json'
12+
import { CID } from 'multiformats/cid'
13+
import { jsonCodec } from 'multiformats/codecs/json'
1414
import { sha256 } from 'multiformats/hashes/sha2'
1515

1616
const bytes = json.encode({ hello: 'world' })

src/basics.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import * as base32 from './bases/base32.js'
44
import * as base58 from './bases/base58.js'
55
import * as sha2 from './hashes/sha2.js'
66

7-
import raw from './codecs/raw.js'
8-
import json from './codecs/json.js'
7+
import { rawCodec } from './codecs/raw.js'
8+
import { jsonCodec } from './codecs/json.js'
99

1010
import { CID, hasher, digest, varint, bytes } from './index.js'
1111

1212
const bases = { ...base32, ...base58 }
1313
const hashes = { ...sha2 }
14-
const codecs = { raw, json }
14+
const codecs = { raw: rawCodec, json: jsonCodec }
1515

1616
export { CID, hasher, digest, varint, bytes, hashes, bases, codecs }

src/cid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { coerce } from './bytes.js'
1818
* @typedef {import('./bases/interface').MultibaseDecoder<Prefix>} MultibaseDecoder
1919
*/
2020

21-
export default class CID {
21+
export class CID {
2222
/**
2323
* @param {0|1} version
2424
* @param {number} code

src/codecs/json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { codec } from './codec.js'
44

5-
export default codec({
5+
export const jsonCodec = codec({
66
name: 'json',
77
code: 0x0200,
88
encode: json => new TextEncoder().encode(JSON.stringify(json)),

src/codecs/raw.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { codec } from './codec.js'
77
* @param {Uint8Array} bytes
88
* @returns {Uint8Array}
99
*/
10-
const raw = (bytes) => coerce(bytes)
10+
const rawEncodeDecode = (bytes) => coerce(bytes)
1111

12-
export default codec({
12+
export const rawCodec = codec({
1313
name: 'raw',
1414
code: 85,
15-
decode: raw,
16-
encode: raw
15+
decode: rawEncodeDecode,
16+
encode: rawEncodeDecode
1717
})

src/hashes/identity.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { from } from './hasher.js'
44
import { coerce } from '../bytes.js'
55

6-
export default from({
6+
export const identity = from({
77
name: 'identity',
88
code: 0x0,
99
encode: (input) => coerce(input)

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CID from './cid.js'
1+
import { CID } from './cid.js'
22
import * as varint from './varint.js'
33
import * as bytes from './bytes.js'
44
import * as hasher from './hashes/hasher.js'

src/legacy.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import OldCID from 'cids'
22
import * as bytes from './bytes.js'
33
import { Buffer } from 'buffer'
4-
import CID from './cid.js'
4+
import { CID } from './cid.js'
55

66
/**
77
* @template {number} Code
@@ -11,7 +11,7 @@ import CID from './cid.js'
1111
* @param {Object<string, MultihashHasher>} options.hashes
1212
*/
1313

14-
const legacy = (codec, { hashes }) => {
14+
export const legacy = (codec, { hashes }) => {
1515
/**
1616
* @param {*} obj
1717
*/
@@ -139,7 +139,6 @@ const legacy = (codec, { hashes }) => {
139139
return { defaultHashAlg, codec: codec.code, util, resolver }
140140
}
141141

142-
export default legacy
143142
/**
144143
* @typedef {import('./hashes/interface').MultihashHasher} MultihashHasher
145144
*/

test/test-block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* globals describe, it */
2-
import codec from 'multiformats/codecs/json'
2+
import { jsonCodec as codec } from 'multiformats/codecs/json'
33
import { sha256 as hasher } from 'multiformats/hashes/sha2'
44
import * as main from 'multiformats/block'
55
import { CID, bytes } from 'multiformats'

test/test-legacy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* globals before, describe, it */
22
import { Buffer } from 'buffer'
33
import assert from 'assert'
4-
import legacy from 'multiformats/legacy'
5-
import rawCodec from 'multiformats/codecs/raw'
6-
import jsonCodec from 'multiformats/codecs/json'
4+
import { legacy } from 'multiformats/legacy'
5+
import { rawCodec } from 'multiformats/codecs/raw'
6+
import { jsonCodec } from 'multiformats/codecs/json'
77
import { sha256, sha512 } from 'multiformats/hashes/sha2'
88
import { codec } from 'multiformats/codecs/codec'
9-
import CID from 'multiformats/cid'
9+
import { CID } from 'multiformats/cid'
1010

1111
const same = assert.deepStrictEqual
1212
const test = it

test/test-multicodec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* globals describe, it */
22
import * as bytes from '../src/bytes.js'
33
import assert from 'assert'
4-
import raw from 'multiformats/codecs/raw'
5-
import json from 'multiformats/codecs/json'
4+
import { rawCodec } from 'multiformats/codecs/raw'
5+
import { jsonCodec } from 'multiformats/codecs/json'
66
import { codec } from 'multiformats/codecs/codec'
77
const same = assert.deepStrictEqual
88
const test = it
@@ -20,35 +20,35 @@ const testThrow = async (fn, message) => {
2020

2121
describe('multicodec', () => {
2222
test('encode/decode raw', () => {
23-
const buff = raw.encode(bytes.fromString('test'))
23+
const buff = rawCodec.encode(bytes.fromString('test'))
2424
same(buff, bytes.fromString('test'))
25-
same(raw.decode(buff, 'raw'), bytes.fromString('test'))
25+
same(rawCodec.decode(buff, 'raw'), bytes.fromString('test'))
2626
})
2727

2828
test('encode/decode json', () => {
29-
const buff = json.encode({ hello: 'world' })
29+
const buff = jsonCodec.encode({ hello: 'world' })
3030
same(buff, bytes.fromString(JSON.stringify({ hello: 'world' })))
31-
same(json.decode(buff), { hello: 'world' })
31+
same(jsonCodec.decode(buff), { hello: 'world' })
3232
})
3333

3434
test('json.encoder', () => {
35-
const { encoder } = json
36-
same(encoder === json.encoder, true, 'getter cached decoder')
35+
const { encoder } = jsonCodec
36+
same(encoder === jsonCodec.encoder, true, 'getter cached decoder')
3737

3838
const buff = encoder.encode({ hello: 'world' })
3939
same(buff, bytes.fromString(JSON.stringify({ hello: 'world' })))
4040
})
4141

4242
test('json.decoder', () => {
43-
const { decoder } = json
44-
same(decoder === json.decoder, true, 'getter cached encoder')
43+
const { decoder } = jsonCodec
44+
same(decoder === jsonCodec.decoder, true, 'getter cached encoder')
4545

46-
const buff = json.encode({ hello: 'world' })
46+
const buff = jsonCodec.encode({ hello: 'world' })
4747
same(decoder.decode(buff), { hello: 'world' })
4848
})
4949

5050
test('raw cannot encode string', async () => {
51-
await testThrow(() => raw.encode('asdf'), 'Unknown type, must be binary type')
51+
await testThrow(() => rawCodec.encode('asdf'), 'Unknown type, must be binary type')
5252
})
5353

5454
test('add with function', () => {

test/test-multihash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import valid from './fixtures/valid-multihash.js'
55
import invalid from './fixtures/invalid-multihash.js'
66
import crypto from 'crypto'
77
import { sha256, sha512, __browser } from 'multiformats/hashes/sha2'
8-
import identity from 'multiformats/hashes/identity'
8+
import { identity } from 'multiformats/hashes/identity'
99
import { decode as decodeDigest, create as createDigest } from 'multiformats/hashes/digest'
1010
const test = it
1111
const encode = name => data => coerce(crypto.createHash(name).update(data).digest())

test/ts-use/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as Block from 'multiformats/block'
22
import { sha256 } from 'multiformats/hashes/sha2'
3-
import json from 'multiformats/codecs/json'
3+
import { jsonCodec } from 'multiformats/codecs/json'
44

55
const main = async () => {
66
const block = await Block.encode({
77
value: { hello: 'world' },
88
hasher: sha256,
9-
codec: json
9+
codec: jsonCodec
1010
})
1111

1212
console.log(block)

0 commit comments

Comments
 (0)