-
Notifications
You must be signed in to change notification settings - Fork 106
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
Atomic rename #47
Atomic rename #47
Conversation
# Conflicts: # package.json # test/test_bad_config.js
@malept Could you please review? |
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.
RIP, not getting notifications from this repo 😆
LGTM, only issue would be breach of scope (the package.json changes, but they all look fair enough so I'm 👍 )
Probably want a second set of eyes before merge into a key module |
package.json
Outdated
"mkdirp": "^0.5.1", | ||
"tape": "^4.6.0", |
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.
The changes in this file are unnecessary.
lib/index.js
Outdated
}) | ||
debug('moving', filename, 'from', this.cache, 'to', target) | ||
fs.rename(path.join(this.cache, filename), target, (err) => { | ||
if (err) return cb(err) |
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.
So if rename fails, we're just going to leave the file in the cache dir?
This should clean up after itself.
lib/index.js
Outdated
downloadFile (url, filename, cacheFilename, cb, onSuccess) { | ||
debug('downloading', url, 'to', this.tmpdir) | ||
downloadFile (url, cacheFilename, cb, onSuccess) { | ||
const tempFileName = `tmp-${process.pid}-${(tmpFileCounter++).toString(16)}-${path.basename(cacheFilename)}` |
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'd rather have this use something like tempnam instead of introducing global variables and hand-rolling it.
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.
tempnam
is not suitable because using fs. I think it is ok to not use external module for two lines of code (also, to reduce number of external deps and increase security).
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.
tempnam
is not suitable because using fs.
Could you please explain this?
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 don't want to use something like mkdtemp here because only our tool creates temp file here — process.pid
allows us to be sure that will be no conflicts with other processes. To not complicate and keep it simple.
lib/index.js
Outdated
@@ -10,6 +10,8 @@ const pathExists = require('path-exists') | |||
const semver = require('semver') | |||
const sumchecker = require('sumchecker') | |||
|
|||
let tmpFileCounter = 0 |
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 you're going to keep this, at least make it a class variable or something.
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 must be file level variable because class can be created several times.
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, a class variable, not an instance variable.
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 class variable, not an instance variable.
Hmm... ES6 doesn't support
static tmpFileCounter = 0
SyntaxError: Unexpected token =
We cannot use this syntax because node doesn't support it.
What do you mean? And for what we should use class variable here? It should be module level variable. e.g. I write in Kotlin and prefer to use file level vars instead of static.
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.
@develar That syntax isn't supported but the syntax ClassName.tmpFileCounter = 0
does work and has the same affect 👍
lib/index.js
Outdated
downloadFile (url, filename, cacheFilename, cb, onSuccess) { | ||
debug('downloading', url, 'to', this.tmpdir) | ||
downloadFile (url, cacheFilename, cb, onSuccess) { | ||
const tempFileName = `tmp-${process.pid}-${(tmpFileCounter++).toString(16)}-${path.basename(cacheFilename)}` |
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.
tempnam
is not suitable because using fs.
Could you please explain this?
lib/index.js
Outdated
fs.rename(path.join(cache, filename), target, (err) => { | ||
if (err) { | ||
try { | ||
fs.unlinkSync(cache) |
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.
Why not use the async version and get rid of the try...catch?
lib/index.js
Outdated
try { | ||
fs.unlinkSync(cache) | ||
} catch (error) { | ||
console.error(`Error deleting cache file: ${error.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 needs to show the filename that couldn't be removed so the user can deal with it themselves.
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 don't think this was addressed.
I have answered to all questions, what's left? |
This looks good to me, what do you think @malept? |
} | ||
} finally { | ||
cb(err) | ||
} |
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.
Not sure what the point of the try...finally
is. Is an exception being thrown?
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.
☝️ Yeah it seems unnecessary...
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.
User application can override console.error
. e.g. Jest does it. As we must call user cb
in any case, it is more safe to use try-finally
here. Otherwise we can get unresolved promise and so on.
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.
Jest does it
Yikes. Ok.
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 agree with @malept's recent feedback all around. Otherwise, looks good to me!
I don't like |
Right now electron-download should be not released after #24 — I get strange errors like @Siilwyn maybe you will have some ideas (workaround — remove |
Friendly ping :) |
Thanks @develar 👍 🚢 |
#36 (comment)
Already used in the electron-builder. During test period no issues were discovered.
Close #36