-
Notifications
You must be signed in to change notification settings - Fork 6
First working version with code extracted from Node.js 15 #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
c02166b
First working version with code extracted from Node.js 15
piranna 7187269
Apply polyfill to `crypto.webcrypto` object
piranna 77663fa
Replaced Node.js specific code
piranna da791c5
Update README.md
piranna f9ea277
Update randomUUID.js
piranna 11271b5
Updated docs
piranna fd230bb
Renamed `index.js` to `node.js`
piranna fb3b0aa
Use standard `TypeError`s
piranna c5f8765
Tests for both `node` and `jsdom` Jest testEnvironments
piranna f38a17d
Tests for both polyfills and main implementation
piranna 3f00140
Remove explicit checks of 'Out of memory' `TypeError`s to get 100% co…
piranna 718feda
Fix tests in `jsdom` environment
piranna 477c102
Ensure it works with Node.js v12 (v10 gave errors on `jest`)
piranna 0f427b9
Clean-up
piranna 1c63802
Update README.md
piranna 30df9c0
Update README.md
piranna e62ae12
Update README.md
piranna f041782
Update README.md
piranna fbf93c2
Update __tests__/randomUUID.js
piranna 133f68d
Update __tests__/randomUUID.js
piranna 954d27e
Update __tests__/randomUUID.js
piranna a0e82b6
Update README.md
piranna e618922
Set link for ESM modules status issue.
piranna c533d73
Enable linting
piranna 7ce268c
Apply linting
piranna a569cbd
Use JSON config files instead of Javascript ones
piranna bffa109
Don't check linting of tests coverage autogenerated files
piranna 43f024f
Fix execution of tests on `jsdom` emulated browser environment
piranna 9baeb0e
Added `.npmignore` file
piranna 7563ec3
Add compatibility with CI using Node.js v14 and npm v6
piranna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); |
Empty file.
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.