-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat!: export object instead of default export #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,12 @@ | ||||||
import * as cborg from 'cborg' | ||||||
import CID from 'multiformats/cid' | ||||||
import { codec } from 'multiformats/codecs/codec' | ||||||
import { Decoder, Encoder } from 'multiformats/codecs/codec' | ||||||
|
||||||
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692 | ||||||
const CID_CBOR_TAG = 42 | ||||||
/** @type {number} */ | ||||||
const code = 0x71 | ||||||
/** @type {string} */ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const name = 'dag-cbor' | ||||||
|
||||||
// this will receive all Objects, we need to filter out anything that's not | ||||||
|
@@ -104,4 +106,7 @@ function decode (data) { | |||||
return cborg.decode(data, decodeOptions) | ||||||
} | ||||||
|
||||||
export default codec({ name, code, encode, decode }) | ||||||
const decoder = new Decoder(name, code, decode) | ||||||
const encoder = new Encoder(name, code, encode) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d suggest following instead: export const {name, code, encode, decode, encoder, decoder} = codec({ name, code, encode, decode }) That is because this would result in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. annotating those at definition site is also another option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I tried that. But it fails to build ( $ npm run build
> @ipld/dag-cbor@0.0.0-dev build
> npm run build:js && npm run build:types
> @ipld/dag-cbor@0.0.0-dev build:js
> npm_config_yes=true npx ipjs@latest build --tests
parsing file:///home/vmx/src/pl/js-dag-cbor/index.js
file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:3010
var err = new SyntaxError(message);
^
SyntaxError: Identifier 'name' has already been declared (112:14)
at Parser.pp$4.raise (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:3010:13)
at Parser.pp$5.declareName (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:3084:26)
at Parser.pp$2.checkLValSimple (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1899:48)
at Parser.pp$2.checkLValPattern (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1941:10)
at Parser.pp$2.checkLValInnerPattern (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1963:10)
at Parser.pp$2.checkLValInnerPattern (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1951:10)
at Parser.pp$2.checkLValPattern (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1928:10)
at Parser.pp$1.parseVarId (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1256:8)
at Parser.pp$1.parseVar (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1238:10)
at Parser.pp$1.parseVarStatement (file:///home/vmx/.npm/_npx/ef1ca190cb7ddd1d/node_modules/acorn/dist/acorn.mjs:1104:8) {
pos: 2740,
loc: Position { line: 112, column: 14 },
raisedAt: 2787
} So I guess annotating them is the quicker way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a JS thing, I tried with Node.js: $ node index.js
file:///home/vmx/src/pl/js-dag-cbor/index.js:112
export const {name, code, encode, decode, encoder, decoder} = codec({ name, code, encode, decode })
^
SyntaxError: Identifier 'name' has already been declared
at Loader.moduleStrategy (node:internal/modules/esm/translators:147:18)
at async link (node:internal/modules/esm/module_job:48:21) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah that makes sense because we define There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @rvagg made a change to a export const { name, code, decoder, encoder } = codec({ name: 'dag-cbor', code: 0x71, encode, decode }) |
||||||
|
||||||
export { name, code, decode, encode, decoder, encoder } |
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.
We want to use actual literal type here, so TS won't generalize it to number.
Note: This is not needed if
codec({ code: 0x71, ...
is used as TS can infer 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.
now that's a weird annotation!
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.
Without the annotation it also isn't generalaized. @Gozala I misunderstood you, I thought you want that generalization.
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 pretty sure TS generalizes type of code in
export const code = 0x71
to number.it did not generalized it in original code
codec({ code: 0x71, ... })
becausecodec
user generics.I apologize for confusion, but I do want non generalized types as it helps with compositions allowing codecs to reject incompatible blocks at compile time
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 see. The code (without the generalization) produces:
which I thought is a sign that it would also use those specific types directly and not the generalized ones. The reason why I though it is that the generalized version produces:
which is clearly generalized.
@Gozala I just checked it with your proposed type annotations, it produces:
Isn't that the same as the non-generalized version?
Anyway, please tell me which version, the one without any type annotion or the one with your type annotation, would be preferred and I'll push an updated version.