-
Notifications
You must be signed in to change notification settings - Fork 55
Implement CID.asCID and depracte CID.isCID #23
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa99692
chore: remove is-class dependency
Gozala 2067626
feat: implement CID.asCID method
Gozala 04573a1
chore: deprecate CID.isCID in favor of CID.asCID
Gozala 853e284
fix: compatibility with js-cids
Gozala 6abc7c5
chore: address review comments
Gozala 1bdfe49
Merge branch 'master' into as-cid
mikeal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, { | ||
|
|
@@ -9,6 +8,18 @@ const readonly = (object, key, value) => { | |
| }) | ||
| } | ||
|
|
||
| // ESM does not support importing package.json where this version info | ||
| // should come from. To workaround it version is copied here. | ||
| const version = '0.0.0-dev' | ||
| // Start throwing exceptions on major version bump | ||
| const deprecate = (range, message) => { | ||
| if (range.test(version)) { | ||
| console.warn(message) | ||
| } else { | ||
| throw new Error(message) | ||
| } | ||
| } | ||
|
|
||
| export default multiformats => { | ||
| const { multibase, varint, multihash } = multiformats | ||
| const parse = buff => { | ||
|
|
@@ -22,6 +33,9 @@ export default multiformats => { | |
| ...multihash | ||
| ]) | ||
| } | ||
|
|
||
| const cidSymbol = Symbol.for('@ipld/js-cid/CID') | ||
|
|
||
| class CID { | ||
| constructor (cid, ...args) { | ||
| Object.defineProperty(this, '_baseCache', { | ||
|
|
@@ -30,7 +44,7 @@ export default multiformats => { | |
| enumerable: false | ||
| }) | ||
| readonly(this, 'asCID', this) | ||
| 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)) | ||
|
|
@@ -104,11 +118,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 () { | ||
|
|
@@ -146,11 +160,57 @@ export default multiformats => { | |
| this.version === other.version && | ||
| bytes.equals(this.multihash, other.multihash) | ||
| } | ||
|
|
||
| get [Symbol.toStringTag] () { | ||
| return 'CID' | ||
| } | ||
|
|
||
| get [cidSymbol] () { | ||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * Takes any input `value` and returns a `CID` instance if it was | ||
| * a `CID` otherwise returns `null`. If `value` is instanceof `CID` | ||
| * it will return value back. If `value` is not instance of this CID | ||
| * class, but is compatible CID it will return new instance of this | ||
| * `CID` class. Otherwise returs null. | ||
| * | ||
| * This allows two different incompatible versions of CID library to | ||
| * co-exist and interop as long as binary interface is compatible. | ||
| * @param {any} value | ||
| * @returns {CID|null} | ||
| */ | ||
| static asCID (value) { | ||
| if (value instanceof CID) { | ||
| return value | ||
| } else if (value != null && value.asCID === value) { | ||
| const { version, code, multihash } = value | ||
| return new CID(version, code, multihash) | ||
| } else { | ||
Gozala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return null | ||
| } | ||
| } | ||
|
|
||
| static isCID (value) { | ||
| deprecate(/^0\.0/, `CID.isCID(v) is deprecated and will be removed in the next major release. | ||
|
||
| Following code pattern: | ||
|
|
||
| if (CID.isCID(value)) { | ||
| doSomethingWithCID(value) | ||
| } | ||
|
|
||
| Is replaced with: | ||
|
|
||
| const cid = CID.asCID(value) | ||
| if (cid) { | ||
| // Make sure to use cid instead of value | ||
| doSomethingWithCID(cid) | ||
| } | ||
| `) | ||
| return !!(value && value[cidSymbol]) | ||
| } | ||
| } | ||
|
|
||
| const _CID = withIs(CID, { | ||
| className: 'CID', | ||
| symbolName: '@ipld/js-cid/CID' | ||
| }) | ||
| return _CID | ||
| return CID | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.