Skip to content

Commit

Permalink
feat!: named exports instead of default exports
Browse files Browse the repository at this point in the history
It also removes the `Codec` class and exports a plain object
instead. This has consequences for imports.

BREAKING CHANGE:

Only use names exports as default exports don't play well with
CommonJS + Typescript typing.

This means that when you use ESM imports, e.g. the raw codec is
no longer imported as

```js
import raw from 'multiformats/codecs/raw'
```

but as

```js
import * as raw from 'multiformats/codecs/raw'
```

The CJS import for codecs don't change, it's still `const raw = require('multiformats/codecs/raw`.

Though other imports change, so

```js
import CID from 'multiformats/cid'
const CID = require('multiformats/cid')
```

is now

```js
import { CID } from 'multiformats/cid'
const { CID } = require ('multiformats/cid')
```
  • Loading branch information
vmx authored and rvagg committed Mar 31, 2021
1 parent 5fccaba commit cea9063
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 75 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Library provides implementations for most basics and many others can be found in
## Interfaces

```js
import CID from 'multiformats/cid'
import json from 'multiformats/codecs/json'
import { CID } from 'multiformats/cid'
import * as json from 'multiformats/codecs/json'
import { sha256 } from 'multiformats/hashes/sha2'

const bytes = json.encode({ hello: 'world' })
Expand Down
4 changes: 2 additions & 2 deletions src/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as base32 from './bases/base32.js'
import * as base58 from './bases/base58.js'
import * as sha2 from './hashes/sha2.js'

import raw from './codecs/raw.js'
import json from './codecs/json.js'
import * as raw from './codecs/raw.js'
import * as json from './codecs/json.js'

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

Expand Down
3 changes: 2 additions & 1 deletion src/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const equals = (aa, bb) => {
}

/**
* @param {ArrayBufferView|ArrayBuffer} o
* @param {ArrayBufferView|ArrayBuffer|Uint8Array} o
* @returns {Uint8Array}
*/
const coerce = o => {
if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') return o
Expand Down
2 changes: 1 addition & 1 deletion src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { coerce } from './bytes.js'
* @typedef {import('./bases/interface').MultibaseDecoder<Prefix>} MultibaseDecoder
*/

export default class CID {
export class CID {
/**
* @param {0|1} version
* @param {number} code
Expand Down
51 changes: 7 additions & 44 deletions src/codecs/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
* @param {Code} options.code
* @param {(data:T) => Uint8Array} options.encode
* @param {(bytes:Uint8Array) => T} options.decode
* @returns {import('./interface').BlockCodec<Code, T>}
*/
export const codec = ({ name, code, decode, encode }) =>
new Codec(name, code, encode, decode)
export const codec = ({ name, code, decode, encode }) => {
const decoder = new Decoder(name, code, decode)
const encoder = new Encoder(name, code, encode)

return { name, code, decode, encode, decoder, encoder }
}

/**
* @template {number} Code
Expand Down Expand Up @@ -64,45 +69,3 @@ export class Decoder {
this.decode = decode
}
}

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec
*/

/**
* @class
* @template {string} Name
* @template {number} Code
* @template T
* @implements {BlockCodec<Code, T>}
*/
export class Codec {
/**
* @param {Name} name
* @param {Code} code
* @param {(data:T) => Uint8Array} encode
* @param {(bytes:Uint8Array) => T} decode
*/
constructor (name, code, encode, decode) {
this.name = name
this.code = code
this.encode = encode
this.decode = decode
}

get decoder () {
const { name, code, decode } = this
const decoder = new Decoder(name, code, decode)
Object.defineProperty(this, 'decoder', { value: decoder })
return decoder
}

get encoder () {
const { name, code, encode } = this
const encoder = new Encoder(name, code, encode)
Object.defineProperty(this, 'encoder', { value: encoder })
return encoder
}
}
5 changes: 4 additions & 1 deletion src/codecs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export interface BlockDecoder<Code extends number, T> {
* separate those capabilties as sender requires encoder and receiver
* requires decoder.
*/
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> { }
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> {
encoder: BlockEncoder<Code, T>,
decoder: BlockDecoder<Code, T>
}


// This just a hack to retain type information abouth the data that
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

export default codec({
export const { name, code, decode, encode, decoder, encoder } = codec({
name: 'json',
code: 0x0200,
encode: json => new TextEncoder().encode(JSON.stringify(json)),
Expand Down
12 changes: 3 additions & 9 deletions src/codecs/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
import { coerce } from '../bytes.js'
import { codec } from './codec.js'

/**
* @param {Uint8Array} bytes
* @returns {Uint8Array}
*/
const raw = (bytes) => coerce(bytes)

export default codec({
export const { name, code, decode, encode, decoder, encoder } = codec({
name: 'raw',
code: 85,
decode: raw,
encode: raw
decode: coerce,
encode: coerce
})
2 changes: 1 addition & 1 deletion src/hashes/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { from } from './hasher.js'
import { coerce } from '../bytes.js'

export default from({
export const identity = from({
name: 'identity',
code: 0x0,
encode: (input) => coerce(input)
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CID from './cid.js'
import { CID } from './cid.js'
import * as varint from './varint.js'
import * as bytes from './bytes.js'
import * as hasher from './hashes/hasher.js'
Expand Down
5 changes: 2 additions & 3 deletions src/legacy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OldCID from 'cids'
import * as bytes from './bytes.js'
import { Buffer } from 'buffer'
import CID from './cid.js'
import { CID } from './cid.js'

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

const legacy = (codec, { hashes }) => {
export const legacy = (codec, { hashes }) => {
/**
* @param {*} obj
*/
Expand Down Expand Up @@ -139,7 +139,6 @@ const legacy = (codec, { hashes }) => {
return { defaultHashAlg, codec: codec.code, util, resolver }
}

export default legacy
/**
* @typedef {import('./hashes/interface').MultihashHasher} MultihashHasher
*/
Expand Down
2 changes: 1 addition & 1 deletion test/test-block.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* globals describe, it */
import codec from 'multiformats/codecs/json'
import * as codec from 'multiformats/codecs/json'
import { sha256 as hasher } from 'multiformats/hashes/sha2'
import * as main from 'multiformats/block'
import { CID, bytes } from 'multiformats'
Expand Down
8 changes: 4 additions & 4 deletions test/test-legacy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* globals before, describe, it */
import { Buffer } from 'buffer'
import assert from 'assert'
import legacy from 'multiformats/legacy'
import rawCodec from 'multiformats/codecs/raw'
import jsonCodec from 'multiformats/codecs/json'
import { legacy } from 'multiformats/legacy'
import * as rawCodec from 'multiformats/codecs/raw'
import * as jsonCodec from 'multiformats/codecs/json'
import { sha256, sha512 } from 'multiformats/hashes/sha2'
import { codec } from 'multiformats/codecs/codec'
import CID from 'multiformats/cid'
import { CID } from 'multiformats/cid'

const same = assert.deepStrictEqual
const test = it
Expand Down
4 changes: 2 additions & 2 deletions test/test-multicodec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals describe, it */
import * as bytes from '../src/bytes.js'
import assert from 'assert'
import raw from 'multiformats/codecs/raw'
import json from 'multiformats/codecs/json'
import * as raw from 'multiformats/codecs/raw'
import * as json from 'multiformats/codecs/json'
import { codec } from 'multiformats/codecs/codec'
const same = assert.deepStrictEqual
const test = it
Expand Down
2 changes: 1 addition & 1 deletion test/test-multihash.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import valid from './fixtures/valid-multihash.js'
import invalid from './fixtures/invalid-multihash.js'
import crypto from 'crypto'
import { sha256, sha512, __browser } from 'multiformats/hashes/sha2'
import identity from 'multiformats/hashes/identity'
import { identity } from 'multiformats/hashes/identity'
import { decode as decodeDigest, create as createDigest } from 'multiformats/hashes/digest'
const test = it
const encode = name => data => coerce(crypto.createHash(name).update(data).digest())
Expand Down
2 changes: 1 addition & 1 deletion test/ts-use/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Block from 'multiformats/block'
import { sha256 } from 'multiformats/hashes/sha2'
import json from 'multiformats/codecs/json'
import * as json from 'multiformats/codecs/json'

const main = async () => {
const block = await Block.encode({
Expand Down

0 comments on commit cea9063

Please sign in to comment.