Skip to content

fix: remove use of Object.defineProperties in CID class #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,6 @@ export class CID {
// Circular reference
/** @readonly */
this.asCID = this

// Configure private properties
Object.defineProperties(this, {
byteOffset: hidden,
byteLength: hidden,

code: readonly,
version: readonly,
multihash: readonly,
bytes: readonly,

asCID: hidden
})
}

/**
Expand Down Expand Up @@ -568,5 +555,3 @@ const encodeCID = (version, code, multihash) => {
}

const cidSymbol = Symbol.for('@ipld/js-cid/CID')
const readonly = { writable: false, configurable: false, enumerable: true }
const hidden = { writable: false, enumerable: false, configurable: false }
12 changes: 12 additions & 0 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,16 @@ describe('CID', () => {
const encoded = varint.encodeTo(2, new Uint8Array(32))
assert.throws(() => CID.decode(encoded), 'Invalid CID version 2')
})

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.strictEqual(cid2.asCID, cid2)
sender.close()
receiver.close()
})
})