-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from piranna/main
First working version with code extracted from Node.js 15
- Loading branch information
Showing
18 changed files
with
6,378 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"presets": [], | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties" | ||
], | ||
"env": { | ||
"commonjs": { | ||
"presets": [["@babel/preset-env", { "targets": { "node": "8" }, "modules": "commonjs" }]] | ||
}, | ||
"esmBrowser": { | ||
"presets": [["@babel/preset-env", { "modules": false }]] | ||
}, | ||
"esmNode": { | ||
"presets": [["@babel/preset-env", { "targets": { "node": "8" }, "modules": false }]] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"root": true, | ||
"env": { | ||
"browser": true, | ||
"commonjs": true, | ||
"es6": true, | ||
"jest": true, | ||
"node": true | ||
}, | ||
"extends": ["eslint:recommended", "standard", "plugin:prettier/recommended"], | ||
"globals": { | ||
"msCrypto": true | ||
}, | ||
"parser": "@babel/eslint-parser", | ||
"rules": { | ||
"no-var": ["error"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__tests__/ | ||
coverage/ | ||
jsdom/ | ||
.babelrc.json | ||
.eslint* | ||
.prettierrc | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"arrowParens": "always", | ||
"printWidth": 100, | ||
"proseWrap": "never", | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
# randomUUID | ||
|
||
Stub project for polyfilling `randomUUID` as proposed for standardization in | ||
https://github.com/WICG/uuid and implemented in Node.js v15.6.0 | ||
https://github.com/nodejs/node/pull/36729 | ||
Polyfill for the `crypto.randomUUID()` method as proposed in | ||
the [WICG randomUUID specification](https://github.com/WICG/uuid) and recently implemented in | ||
[Node.js v15.6.0](https://github.com/nodejs/node/pull/36729). | ||
|
||
## Usage | ||
|
||
Require module to polyfill `crypto.randomUUID()` method. | ||
|
||
```js | ||
require('randomuuid') | ||
``` | ||
|
||
|
||
NOTE: ESM `import`able version not yet available. See | ||
[this issue](https://github.com/uuidjs/randomUUID/issues/2) for status. | ||
## API | ||
|
||
See Node.js documentation for | ||
[randomUUID()](https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_crypto_randomuuid_options) | ||
for API usage. | ||
|
||
Note: The current implementation is based on Node.js `lib/internal/crypto/random.js`, however the intent of this project is to follow the spec, so node-specific details (such as the `disableEntropyCache` option) may or may not survive in future versions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require('..'); | ||
|
||
const { randomUUID } = typeof window === 'undefined' ? require('crypto') : window.crypto; | ||
|
||
test('Apply polyfill', function () { | ||
// expect(randomUUID).toBeInstanceOf(Function) | ||
expect(typeof randomUUID).toBe('function'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const randomUUID = require('../randomUUID'); | ||
|
||
function testMatch(uuid) { | ||
assert.match(uuid, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); | ||
} | ||
|
||
// Generate a number of UUID's to make sure we're not just generating the same | ||
// value over and over and to make sure the batching changes the random bytes. | ||
test('Generate multiple UUIDs', function () { | ||
const last = new Set(['00000000-0000-0000-0000-000000000000']); | ||
|
||
for (let n = 0; n < 130; n++) { | ||
const uuid = randomUUID(); | ||
assert(!last.has(uuid)); | ||
last.add(uuid); | ||
testMatch(uuid); | ||
|
||
// Check that version 4 identifier was populated. | ||
assert.strictEqual(uuid.substr(14, 1), '4'); | ||
|
||
// Check that clock_seq_hi_and_reserved was populated with reserved bits. | ||
assert.match(uuid.substr(19, 1), /[89ab]/); | ||
} | ||
}); | ||
|
||
test("Test non-buffered UUID's", function () { | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
|
||
assert.throws(() => randomUUID(1), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
|
||
assert.throws(() => randomUUID({ disableEntropyCache: '' }), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./polyfill')(window.crypto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { randomFillSync: getRandomValues, webcrypto } = require('crypto'); | ||
|
||
if (!window.crypto) window.crypto = webcrypto || { getRandomValues }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { sync } = require('browser-resolve'); | ||
|
||
module.exports = sync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const crypto = require('crypto'); | ||
|
||
require('./polyfill')(crypto); | ||
|
||
let { webcrypto } = crypto; | ||
if (!webcrypto) crypto.webcrypto = webcrypto = {}; | ||
|
||
if (!webcrypto.randomUUID) webcrypto.randomUUID = crypto.randomUUID; |
Oops, something went wrong.