Skip to content
Closed
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
28 changes: 19 additions & 9 deletions cid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as bytes from './bytes.js'
import withIs from 'class-is'

const readonly = (object, key, value) => {
Object.defineProperty(object, key, {
Expand All @@ -22,14 +21,17 @@ export default multiformats => {
...multihash
])
}

const cidSymbol = Symbol.for('@ipld/js-cid/CID')

class CID {
constructor (cid, ...args) {
Object.defineProperty(this, '_baseCache', {
value: new Map(),
writable: false,
enumerable: false
})
if (_CID.isCID(cid)) {
if (CID.isCID(cid)) {
readonly(this, 'version', cid.version)
readonly(this, 'multihash', bytes.coerce(cid.multihash))
readonly(this, 'buffer', bytes.coerce(cid.buffer))
Expand Down Expand Up @@ -103,11 +105,11 @@ export default multiformats => {
throw new Error('Cannot convert non sha2-256 multihash CID to CIDv0')
}

return new _CID(0, this.code, this.multihash)
return new CID(0, this.code, this.multihash)
}

toV1 () {
return new _CID(1, this.code, this.multihash)
return new CID(1, this.code, this.multihash)
}

get toBaseEncodedString () {
Expand Down Expand Up @@ -145,11 +147,19 @@ export default multiformats => {
this.version === other.version &&
bytes.equals(this.multihash, other.multihash)
}

get [Symbol.toStringTag] () {
return 'CID'
}

get [cidSymbol] () {
return true
}

static isCID (value) {
return !!(value && value[cidSymbol])
}
}

const _CID = withIs(CID, {
className: 'CID',
symbolName: '@ipld/js-cid/CID'
})
return _CID
return CID
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
"base-x": "^3.0.8",
"buffer": "^5.6.0",
"cids": "^0.8.3",
"class-is": "^1.1.0",
"varint": "^5.0.0"
},
"directories": {
Expand All @@ -113,4 +112,4 @@
"url": "https://github.com/multiformats/js-multiformats/issues"
},
"homepage": "https://github.com/multiformats/js-multiformats#readme"
}
}