Skip to content

Commit

Permalink
Merge pull request #1 from piranna/main
Browse files Browse the repository at this point in the history
First working version with code extracted from Node.js 15
  • Loading branch information
broofa authored Mar 23, 2021
2 parents 180b567 + 7563ec3 commit 8b7d247
Show file tree
Hide file tree
Showing 18 changed files with 6,378 additions and 6 deletions.
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
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

0 comments on commit 8b7d247

Please sign in to comment.