-
Notifications
You must be signed in to change notification settings - Fork 24
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
Complete the Jest DI with crypto #234
Conversation
src/setup/jest.ts
Outdated
async getSymmKey(keyName: string, cfg?: Partial<Config>): Promise<CryptoKey> { | ||
const mergedCfg = config.merge(this.cfg, cfg) | ||
const maybeKey = this.store[keyName] | ||
if(maybeKey !== null) { | ||
return maybeKey | ||
} | ||
const key = await aes.makeKey(config.symmKeyOpts(mergedCfg)) | ||
this.store[keyName] = key | ||
return key | ||
} |
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 duplicate some code here from keystore-idb.
We should probably eventually make it possible to use our dependencies in node. However, doing that for keystore-idb could be questionable, given it has "idb" in its name 😄
Any way, this feels kind of quick-and-dirty right now. Let me know if you have any other ideas.
Switch to using Uint8Arrays instead of node Buffers. This is something the js-ipfs folks apparently have gone through, too: ipfs/js-ipfs#3220
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.
@@ -36,7 +36,7 @@ | |||
"prebuild": "rimraf dist && node scripts/gen-version.js", | |||
"build": "tsc && rollup -c rollup.config.ts", | |||
"start": "tsc -w", | |||
"test": "jest", | |||
"test": "jest --forceExit", |
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.
Hrm. This smells like there's something wrong with the tests... async operations not resolving?
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.
Yep. I tried to track it down for a while, but gave up. From my testing it seems like in some edge-cases it's possible that ipfs leaves a child process and some sockets hanging after await ipfs.stop()
. Not sure what's up with that, but after fighting with it for a while I just gave up.
The IPFS we're using in the tests is basically in-memory only. It doesn't write to disk. So killing it shouldn't be an issue.
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.
Cool. yeah, something we can track down and fix as a follow up later, imo.
@@ -1,8 +1,9 @@ | |||
import crypto from "crypto" | |||
import Ipfs, { IPFS } from "ipfs" | |||
import { IPFS } from "ipfs-core" |
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 size thing? (full IPFS includes more code than we use, etc?)
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.
Yep, exactly. It's just much less dependencies.
I think the difference is that ipfs also contains the CLI and an http-server, which ipfs-core does not.
Yeah. Weird. |
async function runRoundTrip(message) { | ||
const keyStr = await webnative.crypto.aes.genKeyStr() | ||
|
||
const encoded = webnative.cbor.encode(message) |
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 test is failing for me with:
Evaluation failed: TypeError: Cannot read property 'encode' of undefined
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.
Did you yarn build
before you yarn test
ed?
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 did not 🤦 and that was it. After building, this test and all the others are passing for me 💯
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.
Admittedly, it's really confusing. The puppeteer html file fixture uses the index.umd.min.js file, so unless one yarn build
s before running the tests, it'll use an outdated webnative, but only for the puppeteer stuff.
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.
Looks great! Thanks for implementing these Jest DI crypto functions 🎉 🙏
This is preparation work for making it possible to write more sophisticated test, making test-driven-development for internal (non-browser-bundle-exposed) APIs possible.