-
Notifications
You must be signed in to change notification settings - Fork 20
Conversation
Adds types to all src and tests and fixes all tsc warnings.
Codecov Report
@@ Coverage Diff @@
## master #189 +/- ##
===========================================
- Coverage 97.57% 77.59% -19.98%
===========================================
Files 16 15 -1
Lines 206 366 +160
===========================================
+ Hits 201 284 +83
- Misses 5 82 +77
Continue to review full report at Codecov.
|
Also of note: removes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only did a brief review. This contains lots of breaking changes. I fear that it would result in a release that breaks many things and it would be a lot of work to fix all those. So I'm not sure if an in-depth review with additional testing makes is time well spent. Or if we e.g. just cut a major release without much thoughts and don't upgrade js-ipld to it (which would mean that js-ipfs would bundle two versions of js-ipld-dag-pb.
I'd prefer if it would just add types and won't do anything else. Especially with the background that we hopefully will use js-dag-pb
instead.
branches: | ||
only: | ||
- master | ||
- /^release\/.*$/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean that it doesn't run CI if a branch is pushed without doing a PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not enabled, you can configure this setting in travis to build every non-pr branch
src/genCid.js
Outdated
const cid = async (binaryBlob, userOptions = {}) => { | ||
const options = { | ||
cidVersion: userOptions.cidVersion == null ? 1 : userOptions.cidVersion, | ||
hashAlg: userOptions.hashAlg || defaultHashAlg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would break if you use the identity hash 0x00
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
This should definitely go out as a major release, that's how we're handling all the TS stuff as setups that are already using typecheckers may suddenly find what they thought was the API was not actually the API (intentional breaking changes/API simplifications aside).
I see that as separate to the typing effort. |
Yes for sure. Though I don't know if it makes sense to introduce breaking changes like having some properties enumerable that weren't before, which I don't think add much value. I'd keep breaking changes to a minimum in order to minimize potential breakage and time for review and testing. Anyway, if you think it makes sense I'll spend some time on it to make sure it doesn't break anything in unexpected ways. |
I've made it a draft PR for now, I'll bubble this all the way up to IPFS and we can merge it then (doing the UnixFS importer/exporter types now). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some type information has no return value defined, sometimes it was even removed. What is the reason for that?
It would be great if you could list all the breaking changes in the commit message, so that it automatically shows up in the changelog.
this.Name = name || '' | ||
this.Tsize = size | ||
this.Hash = new CID(cid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the values read-only is something dag-pb did (IIRC) from the very beginning. I'm not sure if it is important or not (if it's not important for unixfs, it is probably not).
@rvagg do you have oponions on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not something that unixfs uses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change it back to the original code. I don't see a reason to make a change that might break things in a subtle way if we don't have to.
return serializeDAGNode(this) | ||
const buf = serializeDAGNode(this) | ||
|
||
this._serializedSize = buf.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test for that fix would be great.
predicate = (/** @type {DAGLink} */ link) => link.Name === nameOrCid | ||
} else if (nameOrCid instanceof Uint8Array) { | ||
predicate = (/** @type {DAGLink} */ link) => uint8ArrayEquals(link.Hash.bytes, nameOrCid) | ||
} else if (CID.isCID(nameOrCid)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test for this fix would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH DAGNode.rmLink
accepts DAGLink | string | CID
so the type checker would object to you passing a Uint8Array
in, but not everyone uses a type checker.
const cid = async (binaryBlob, userOptions = {}) => { | ||
const options = { | ||
cidVersion: userOptions.cidVersion == null ? 1 : userOptions.cidVersion, | ||
hashAlg: userOptions.hashAlg == null ? defaultHashAlg : userOptions.hashAlg | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was the old code buggy? I'm asking as I find the old code clearer then the new one, but I might miss some subtle difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change wasn't necessary - I think there was a complaint from tsc but it looks like this wasn't the problem, I can revert if preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always prefer smaller diffs :) But if it's a hassle, it doesn't bother me much. I was more interested in the reason why.
src/resolver.js
Outdated
@@ -37,9 +40,11 @@ exports.resolve = (binaryBlob, path) => { | |||
|
|||
// There wasn't even a matching named link | |||
throw new Error(`Object has no property '${key}'`) | |||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this change. I usually use if … else
if it is a branch whether either one thing or the other happens and the computation goes on with either of them. I you only and if
if it is an early return. And in this case it is an early return, the if
branch never continues to later parts of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was mostly to shut the type checker up. Will remove.
Links: links | ||
})) | ||
Links: links.map((link) => { | ||
return createDagLinkFromB58EncodedHash(link) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick (no need to change): other similar cases in the code base still have an instanceof DagLink
test if something is already a link.
} | ||
|
||
exports.serializeDAGNode = serializeDAGNode | ||
exports.serializeDAGNodeLike = serializeDAGNodeLike | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be some test so that future changes already get noticed when run this libraries tests (I'm not sure how hard it is to make a proper test and whether it's worth the effort or not)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the likely lifespan of this module it's probably not worth the effort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Famous last words..
@@ -58,9 +59,4 @@ describe('DAGLink', () => { | |||
const link = new DAGLink('hello', 3, cid) | |||
expect(link.Hash.toBaseEncodedString()).to.equal(cid) | |||
}) | |||
|
|||
it('has an immutable CID', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a breaking change because it makes sense or because of breaking things while we can?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Object.defineProperties
makes it had to infer what's a class property and what's a dynamically assigned property.
Immutability of programatic structures at this level doesn't really give us much so it seemed simpler to remove it..
test/dag-node-test.spec.js
Outdated
it('creates links from objects with .Size properties', () => { | ||
const node = new DAGNode(uint8ArrayFromString('some data'), [{ | ||
Hash: 'QmUxD5gZfKzm8UN4WaguAMAZjw2TzZ2ZUmcqm2qXPtais7', | ||
Size: 9001 | ||
}]) | ||
|
||
expect(node.Links[0].Tsize).to.eql(9001) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this still work (I guess with Tsize
instead of Size
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more that Hash
should be a CID but we do still handle the case. Will reinstate.
The idea is you let TSC derive the return type of a function - you can see what it'll be by mousing over the function definition or using IntelliSense or looking at the generated If you manually specify the return type, TSC will take what you specify, which sometimes is useful but more often can cause bugs because the return type of a method is not what you think it is, so it's usually better to just let the tool figure it out. |
Make sense. Will the API docs TypeScript generates auto-fill the return values? For me (likely a minority) as API docs reader it would be unfortunate if I won't get information what the return values of a function are. Though I can totally see the point of not having return values. |
Yes, they all appear in the swanky new API docs aegir generates. This has been rolled up to js-ipfs here: ipfs/js-ipfs#3550 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there.
@@ -13,7 +18,6 @@ | |||
"release": "aegir release", | |||
"release-minor": "aegir release --type minor", | |||
"release-major": "aegir release --type major", | |||
"build": "aegir build", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the removal of the build
command is intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - prepare
is run after installing and before publishing so there's no need for a separate build command.
this.Name = name || '' | ||
this.Tsize = size | ||
this.Hash = new CID(cid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change it back to the original code. I don't see a reason to make a change that might break things in a subtle way if we don't have to.
src/dag-node/dagNode.js
Outdated
this.Data = data | ||
this.Links = sortedLinks | ||
this._serializedSize = serializedSize | ||
this._size = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an issue. I propose changing it back to the original code.
Adds types to all src and tests and fixes all tsc warnings.