Skip to content

Commit

Permalink
feat!: Make link.toJSON return a DAG-JSON link
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored and rvagg committed Jan 2, 2023
1 parent a246054 commit 9e087d6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
22 changes: 17 additions & 5 deletions src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ export const format = (link, base) => {
}
}

/**
* @template {API.UnknownLink} Link
* @param {Link} link
* @returns {API.LinkJSON<Link>}
*/
export const toJSON = (link) => ({
'/': format(link)
})

/**
* @template {API.UnknownLink} Link
* @param {API.LinkJSON<Link>} json
*/
export const fromJSON = (json) =>
CID.parse(json['/'])

/** @type {WeakMap<API.UnknownLink, Map<string, string>>} */
const cache = new WeakMap()

Expand Down Expand Up @@ -200,11 +216,7 @@ export class CID {
}

toJSON () {
return {
code: this.code,
version: this.version,
hash: this.multihash.bytes
}
return { '/': format(this) }
}

link () {
Expand Down
4 changes: 2 additions & 2 deletions src/link.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Linter can see that API is used in types.
// eslint-disable-next-line
import * as API from "./link/interface.js"
import { CID, format } from './cid.js'
import { CID, format, toJSON, fromJSON } from './cid.js'
// This way TS will also expose all the types from module
export * from './link/interface.js'

Expand Down Expand Up @@ -73,7 +73,7 @@ export const isLink = value => {
*/
export const parse = (source, base) => CID.parse(source, base)

export { format }
export { format, toJSON, fromJSON }

/**
* Decoded a CID from its binary representation. The byte array must contain
Expand Down
5 changes: 4 additions & 1 deletion src/link/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ export interface Link<
equals: (other: unknown) => other is Link<Data, Format, Alg, Version>

toString: <Prefix extends string>(base?: MultibaseEncoder<Prefix>) => ToString<Link<Data, Format, Alg, Version>, Prefix>
toJSON: () => { version: V, code: Format, hash: Uint8Array }
link: () => Link<Data, Format, Alg, V>

toV1: () => Link<Data, Format, Alg, 1>
}

export interface LinkJSON<T extends UnknownLink = UnknownLink> {
'/': ToString<T>
}

export interface LegacyLink<T extends unknown = unknown> extends Link<T, DAG_PB, SHA_256, 0> {
}

Expand Down
9 changes: 3 additions & 6 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,10 @@ describe('CID', () => {
it('toJSON()', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
const json = cid.toJSON()

assert.deepStrictEqual(
{ ...json, hash: null },
{ code: 112, version: 1, hash: null }
)
assert.ok(equals(json.hash, hash.bytes))
assert.deepStrictEqual(cid.toJSON(), {
'/': cid.toString()
})
})

it('asCID', async () => {
Expand Down
36 changes: 36 additions & 0 deletions test/test-link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ describe('Link', () => {
assert.ok(t2)
})
})

describe('toJSON', () => {
assert.deepStrictEqual(Link.toJSON(Link.parse(h1)), {
'/': h1
})

assert.deepStrictEqual(Link.toJSON(Link.parse(h4)), {
'/': h4
})
})

describe('fromJSON', () => {
assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
'/': h1
}))

assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
'/': h1,
// @ts-expect-error
foo: 1
}))

assert.deepStrictEqual(Link.parse(h4), Link.fromJSON({
'/': h4
}))
})

describe('JSON.stringify', () => {
assert.equal(JSON.stringify(Link.parse(h1)), JSON.stringify({
'/': h1
}))

assert.equal(JSON.stringify(Link.parse(h4)), JSON.stringify({
'/': h4
}))
})
})

describe('decode', () => {
Expand Down

0 comments on commit 9e087d6

Please sign in to comment.