Skip to content

Commit

Permalink
fix: make CID#asCID a regular property
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 12, 2022
1 parent e8def36 commit a74f1c7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 13
"sourceType": "module"
}
},
"release": {
Expand Down
9 changes: 1 addition & 8 deletions src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ const baseCache = cid => {
*/

export class CID {
/** @type {CID} */
#asCID

/**
* @param {Version} version - Version of the CID
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
Expand All @@ -89,11 +86,7 @@ export class CID {

// Circular reference
/** @readonly */
this.#asCID = this
}

get asCID () {
return this.#asCID
this.asCID = this
}

/**
Expand Down
12 changes: 3 additions & 9 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,21 +697,15 @@ describe('CID', () => {
assert.throws(() => CID.decode(encoded), 'Invalid CID version 2')
})

it('asCID is non-enumerable', () => {
const cid = CID.parse('bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu')

assert.isFalse(Object.prototype.propertyIsEnumerable.call(cid, 'asCID'))
assert.isFalse(Object.keys(cid).includes('asCID'))
assert.equal(cid.asCID, cid)
})

it('CID can be moved across JS realms', async () => {
const cid = CID.parse('bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu')
const { port1: sender, port2: receiver } = new MessageChannel()
sender.postMessage(cid)
const cid2 = await new Promise((resolve) => {
receiver.onmessage = (event) => { resolve(event.data) }
})
assert.equal(cid2.asCID, cid2)
assert.strictEqual(cid2.asCID, cid2)
sender.close()
receiver.close()
})
})

0 comments on commit a74f1c7

Please sign in to comment.