You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I bet this was changed since the commit made to the code.
If you check this answer on stackoverflow it describes the situation, although it seems like that the node authors just modified the crypto library and kept the pseudoRandomBytes for backward compatibility.
pseudoRandomBytes api has been removed from Nodejs since v4.
That's a long long time ago. It should be removed from here, since tmp only supports Nodejs v8+.
You are probably right in most of the cases. However, it could happen that crypto.randomBytes() throws an error and in that case, we are fallbacking to the crypto.pseudoRandomBytes().
I'll need to investigate deeper before we can safely remove this fallback mechanism.
Then at runtime, Nodejs rejects it because the ESM exports (I guess Nodejs internal) of crypo doesn't have pseudoRandomBytes.
import {randomBytes, pseudoRandomBytes} from 'crypto';
^^^^^^^^^^^^^^^^^
SyntaxError: The requested module 'crypto' does not provide an export named 'pseudoRandomBytes'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
at async loadESM (node:internal/process/esm_loader:85:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
> cat > file.mjs
import {randomBytes, pseudoRandomBytes} from 'crypto';
> node file.mjs
file:///Users/huocp/playground/file.mjs:1
import {randomBytes, pseudoRandomBytes} from 'crypto';
^^^^^^^^^^^^^^^^^
SyntaxError: The requested module 'crypto' does not provide an export named 'pseudoRandomBytes'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
at async loadESM (node:internal/process/esm_loader:85:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
Node.js v18.4.0
@3cp Good point. I also thought about having a UUIDv4 based name generation strategy. However, this will require an additional dependency for node < v15.6.0, as you pointed out.
Maybe we will revive the old 'naive' random name generation code that simply used random ints and a lookup table as a fallback.
Activity
raszi commentedon Oct 31, 2015
I bet this was changed since the commit made to the code.
If you check this answer on stackoverflow it describes the situation, although it seems like that the node authors just modified the crypto library and kept the
pseudoRandomBytesfor backward compatibility.See the following short snippet:
See also:
douxing commentedon Dec 29, 2015
Sorry for the late, thank you for the explanation.
3cp commentedon Jun 18, 2022
pseudoRandomBytes api has been removed from Nodejs since v4.
That's a long long time ago. It should be removed from here, since
tmponly supports Nodejs v8+.silkentrance commentedon Jun 20, 2022
While it seems to have been deprecated, it is still available in Node v16.15.1.
Which is why we never changed it.
3cp commentedon Jun 20, 2022
The code branch is never reachable in Nodejs v8+.
raszi commentedon Jun 20, 2022
You are probably right in most of the cases. However, it could happen that
crypto.randomBytes()throws an error and in that case, we are fallbacking to thecrypto.pseudoRandomBytes().I'll need to investigate deeper before we can safely remove this fallback mechanism.
3cp commentedon Jun 20, 2022
My encounter of the issue is Nodejs's EMS export.
When I use esbuild to bundle my lib, it transpile this lib into esm. Something like this:
Then at runtime, Nodejs rejects it because the ESM exports (I guess Nodejs internal) of
crypodoesn't have pseudoRandomBytes.raszi commentedon Jun 20, 2022
It is there, but I guess the type definitions are not showing that anymore.
3cp commentedon Jun 20, 2022
It's there for commonjs access, not esm access.
3cp commentedon Jun 20, 2022
FYI, Node.js v12.22.12 also gives the same error.
raszi commentedon Jun 20, 2022
I don't really understand your point.
tmpis importing thecryptomodule usingrequire():node-tmp/lib/tmp.js
Line 15 in 4b51e90
How is your example relevant to
tmp?raszi commentedon Jun 20, 2022
Sorry, I missed this part. I don't think this is an issue of
tmpbut rather anesbuildissue.3cp commentedon Jun 20, 2022
Not esbuild, did you see my example above?
The cat and node commands, without esbuild.
raszi commentedon Jun 20, 2022
This source code is not from
tmp. How is this related then?4 remaining items
3cp commentedon Aug 25, 2022
From nodejs doc, crypto.randomUUID does not throw, but it's only available in v15.6.0+.
silkentrance commentedon Aug 26, 2022
reopening since this is definitely an issue and we might run out of entropy, especially during system start.
silkentrance commentedon Aug 26, 2022
@3cp Good point. I also thought about having a UUIDv4 based name generation strategy. However, this will require an additional dependency for node < v15.6.0, as you pointed out.
Maybe we will revive the old 'naive' random name generation code that simply used random ints and a lookup table as a fallback.
silkentrance commentedon Aug 26, 2022
I will create a new issue and link to this one in the description.
silkentrance commentedon Aug 26, 2022
Please continue the conversation over at #280