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 2 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"ecmaVersion": 13
}
},
"release": {
Expand Down
22 changes: 7 additions & 15 deletions src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ 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 @@ -86,20 +89,11 @@ 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,
this.#asCID = this
}

asCID: hidden
})
get asCID () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is making it public again? so this is essentially a hack to make a hidden property? sneaky.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is going to survive the structural cloning which is what going to occur when transferring CIDs across realms/threads.

If it does not it would defeat the purpose

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gozala are there any tests that guard against regressions around this? I can't see anything obvious.

Copy link
Contributor

@Gozala Gozala Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have, but we did not as far as I can tell. I have created one #206

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I don't believe there is any problem with making asCID public other than it would break JSON.stringify(cid) which could be addressed by having toJSON method.

So maybe it's worth just making it a regular property and call it a day.

return this.#asCID
}

/**
Expand Down Expand Up @@ -568,5 +562,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 }
8 changes: 8 additions & 0 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,12 @@ describe('CID', () => {
const encoded = varint.encodeTo(2, new Uint8Array(32))
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)
})
})