Skip to content
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

fix: move to async functions where possible #106

Merged
merged 1 commit into from May 3, 2022
Merged

Conversation

wraithgar
Copy link
Member

This refactors the code to use async functions where possible.
It also uses @npmcli/fs consistently (it was already a dependency,
just not used).
It also reorders the test files to match their sources in ./lib.

Tests were not refactored (except where needed to move to @npmcli/fs)
in the interest of an easier PR review.

@wraithgar wraithgar requested a review from a team as a code owner April 28, 2022 14:49
@wraithgar
Copy link
Member Author

wraithgar commented Apr 28, 2022

Linting is failing until npm/template-oss#141 lands. There was a test that was enforcing this, moved it to a linting rule but now the main eslintrc.js file is failing that very rule. Irony.

ETA: this has been done

lib/content/read.js Outdated Show resolved Hide resolved
return lstat(cpath).then((stat) => ({ stat, cpath, sri }))
}).then(({ stat, cpath, sri }) => {
// Set all this up to run on the stream and then just return the stream
Promise.resolve().then(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it took me a second to see what this was doing.. seems like the intent here is to run the code within the .then() handler after a short delay?

i don't have a solid suggestion for making this more readable, setImmediate(async () => {}) feels like it would be, but you'd have to also put a try/catch inside the function. maybe just a comment explaining is enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I fiddled a bit w/ this, this was as good as it got. The comment was supposed to explain it lol.

const content = await hasContent(cache, integrity)
// ~pretty~ sure we can't end up with a content lacking sri, but be safe
if (content && content.sri) {
await rimraf(contentPath(cache, content.sri))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we've brought @npmcli/fs in already, this could be fs.rm() instead of rimraf and the rimraf dependency can go away (after checking for other uses, of course)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just did more review and found that we use rimraf.sync in at least one place, so the above would actually require removing the synchronous interface from cacache. i'm personally in favor of doing so, but it should very much be a separate pull request

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this was ONLY supposed to move to @npmcli/fs. Switching to its functionality specifically was always intented to be a separate PR.

}

return Promise.resolve(inferOwner(cache)).then((owner) => {
const { uid, gid } = owner
const { uid, gid } = await inferOwner(cache)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the infer-owner dependency here can also leverage @npmcli/fs in a future refactor pass

lib/util/move-file.js Outdated Show resolved Hide resolved
lib/util/move-file.js Outdated Show resolved Hide resolved
lib/verify.js Outdated Show resolved Hide resolved
lib/verify.js Outdated Show resolved Hide resolved
This refactors the code to use async functions where possible.
It also uses `@npmcli/fs` consistently (it was already a dependency,
just not used).
It also reorders the test files to match their sources in `./lib`.

Tests were not refactored (except where needed to move to `@npmcli/fs`)
in the interest of an easier PR review.
Copy link
Contributor

@nlf nlf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a great first pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants