Skip to content
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

First working version with code extracted from Node.js 15 #1

Merged
merged 30 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
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 Feb 8, 2021
7187269
Apply polyfill to `crypto.webcrypto` object
piranna Feb 8, 2021
77663fa
Replaced Node.js specific code
piranna Feb 8, 2021
da791c5
Update README.md
piranna Feb 27, 2021
f9ea277
Update randomUUID.js
piranna Feb 27, 2021
11271b5
Updated docs
piranna Feb 27, 2021
fd230bb
Renamed `index.js` to `node.js`
piranna Feb 27, 2021
fb3b0aa
Use standard `TypeError`s
piranna Feb 27, 2021
c5f8765
Tests for both `node` and `jsdom` Jest testEnvironments
piranna Feb 27, 2021
f38a17d
Tests for both polyfills and main implementation
piranna Feb 27, 2021
3f00140
Remove explicit checks of 'Out of memory' `TypeError`s to get 100% co…
piranna Feb 27, 2021
718feda
Fix tests in `jsdom` environment
piranna Feb 27, 2021
477c102
Ensure it works with Node.js v12 (v10 gave errors on `jest`)
piranna Feb 27, 2021
0f427b9
Clean-up
piranna Feb 27, 2021
1c63802
Update README.md
piranna Mar 14, 2021
30df9c0
Update README.md
piranna Mar 14, 2021
e62ae12
Update README.md
piranna Mar 14, 2021
f041782
Update README.md
piranna Mar 14, 2021
fbf93c2
Update __tests__/randomUUID.js
piranna Mar 14, 2021
133f68d
Update __tests__/randomUUID.js
piranna Mar 14, 2021
954d27e
Update __tests__/randomUUID.js
piranna Mar 14, 2021
a0e82b6
Update README.md
piranna Mar 14, 2021
e618922
Set link for ESM modules status issue.
piranna Mar 14, 2021
c533d73
Enable linting
piranna Mar 14, 2021
7ce268c
Apply linting
piranna Mar 14, 2021
a569cbd
Use JSON config files instead of Javascript ones
piranna Mar 14, 2021
bffa109
Don't check linting of tests coverage autogenerated files
piranna Mar 14, 2021
43f024f
Fix execution of tests on `jsdom` emulated browser environment
piranna Mar 14, 2021
9baeb0e
Added `.npmignore` file
piranna Mar 14, 2021
7563ec3
Add compatibility with CI using Node.js v14 and npm v6
piranna Mar 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .babelrc.json
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 }]]
}
}
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
18 changes: 18 additions & 0 deletions .eslintrc.json
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"]
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage/
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__tests__/
coverage/
jsdom/
.babelrc.json
.eslint*
.prettierrc
*.tgz
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arrowParens": "always",
"printWidth": 100,
"proseWrap": "never",
"singleQuote": true,
"trailingComma": "es5"
}
24 changes: 22 additions & 2 deletions README.md
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
piranna marked this conversation as resolved.
Show resolved Hide resolved
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.
8 changes: 8 additions & 0 deletions __tests__/polyfill.js
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');
});
43 changes: 43 additions & 0 deletions __tests__/randomUUID.js
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',
});
});
1 change: 1 addition & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./polyfill')(window.crypto);
Empty file removed index.js
Empty file.
3 changes: 3 additions & 0 deletions jsdom/jest.setup.js
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 };
3 changes: 3 additions & 0 deletions jsdom/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { sync } = require('browser-resolve');

module.exports = sync;
8 changes: 8 additions & 0 deletions node.js
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;
Loading