StableLib / stablelib

A stable library of useful TypeScript/JavaScript code
https://www.stablelib.com
Other
175 stars 36 forks source link

Returning a promise from Scrypt.deriveKey() is pointless #1

Closed palant closed 7 years ago

palant commented 7 years ago

The method Scrypt.deriveKey() is synchronous. Still, it wraps the result in a promise just for the sake of it. This is pointless and complicates using the library unnecessarily. If the point is using Scrypt.deriveKey() and Scrypt.deriveKeyNonBlocking() in the same way, the caller can call Promise.resolve() on the call result themselves.

Side-note: is there really still a value in Scrypt.deriveKeyNonBlocking()? The proper way of doing this would be creating a worker and running Scrypt.deriveKey() there.

dchest commented 7 years ago

Agree on deriveKey. I don't remember the original reasoning for it to return Promise, I guess I kept it when thinking I'd add asynchrony to it, but instead added deriveKeyNonBlocking for that. I'll think about changing it.

deriveKeyNonBlocking allows rendering to proceed while calculating result — it adjusts timeouts automatically to make pieces of calculation take no more than 100ms. The original idea was to use a worker there, but workers are more complicated and not supported everywhere, so it's like that for now. I think for a worker (if it will be implemented) I'll have another method rather than change this one.

dchest commented 7 years ago

Made deriveKey return Uint8Array and published as v0.9.0.