From 9e087d64ee3c90d8b019dd48989936b17b1cb2f3 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 13 Dec 2022 12:31:25 -0800 Subject: [PATCH] feat!: Make link.toJSON return a DAG-JSON link --- src/cid.js | 22 +++++++++++++++++----- src/link.js | 4 ++-- src/link/interface.ts | 5 ++++- test/test-cid.spec.js | 9 +++------ test/test-link.spec.js | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/cid.js b/src/cid.js index 7e6c2d01..a13d4ec8 100644 --- a/src/cid.js +++ b/src/cid.js @@ -35,6 +35,22 @@ export const format = (link, base) => { } } +/** + * @template {API.UnknownLink} Link + * @param {Link} link + * @returns {API.LinkJSON} + */ +export const toJSON = (link) => ({ + '/': format(link) +}) + +/** + * @template {API.UnknownLink} Link + * @param {API.LinkJSON} json + */ +export const fromJSON = (json) => + CID.parse(json['/']) + /** @type {WeakMap>} */ const cache = new WeakMap() @@ -200,11 +216,7 @@ export class CID { } toJSON () { - return { - code: this.code, - version: this.version, - hash: this.multihash.bytes - } + return { '/': format(this) } } link () { diff --git a/src/link.js b/src/link.js index a52bf173..0cd0a849 100644 --- a/src/link.js +++ b/src/link.js @@ -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' @@ -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 diff --git a/src/link/interface.ts b/src/link/interface.ts index af8b7e47..06955a2c 100644 --- a/src/link/interface.ts +++ b/src/link/interface.ts @@ -35,12 +35,15 @@ export interface Link< equals: (other: unknown) => other is Link toString: (base?: MultibaseEncoder) => ToString, Prefix> - toJSON: () => { version: V, code: Format, hash: Uint8Array } link: () => Link toV1: () => Link } +export interface LinkJSON { + '/': ToString +} + export interface LegacyLink extends Link { } diff --git a/test/test-cid.spec.js b/test/test-cid.spec.js index b1958b5a..cf59103a 100644 --- a/test/test-cid.spec.js +++ b/test/test-cid.spec.js @@ -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 () => { diff --git a/test/test-link.spec.js b/test/test-link.spec.js index cec2f7c2..7fdc1f05 100644 --- a/test/test-link.spec.js +++ b/test/test-link.spec.js @@ -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', () => {