Skip to content

Commit

Permalink
Apply linting
Browse files Browse the repository at this point in the history
  • Loading branch information
piranna committed Mar 14, 2021
1 parent c533d73 commit 7ce268c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 70 deletions.
13 changes: 5 additions & 8 deletions __tests__/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
require('..')
require('..');

const {randomUUID} = typeof window === 'undefined'
? require('crypto')
: window.crypto
const { randomUUID } = typeof window === 'undefined' ? require('crypto') : window.crypto;

test('Apply polyfill', function()
{
test('Apply polyfill', function () {
// expect(randomUUID).toBeInstanceOf(Function)
expect(typeof randomUUID).toBe('function')
})
expect(typeof randomUUID).toBe('function');
});
24 changes: 8 additions & 16 deletions __tests__/randomUUID.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ 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}$/);
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'
]);
test('Generate multiple UUIDs', function () {
const last = new Set(['00000000-0000-0000-0000-000000000000']);

for (let n = 0; n < 130; n++) {
const uuid = randomUUID();
Expand All @@ -32,20 +25,19 @@ test("Generate multiple UUIDs", function()
// 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()
{
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'
code: 'ERR_INVALID_ARG_TYPE',
});

assert.throws(() => randomUUID({ disableEntropyCache: '' }), {
code: 'ERR_INVALID_ARG_TYPE'
code: 'ERR_INVALID_ARG_TYPE',
});
})
});
2 changes: 1 addition & 1 deletion browser.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./polyfill')(window.crypto)
require('./polyfill')(window.crypto);
4 changes: 2 additions & 2 deletions jsdom/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const {randomFillSync: getRandomValues, webcrypto} = require('crypto')
const { randomFillSync: getRandomValues, webcrypto } = require('crypto');

if(!window.crypto) window.crypto = webcrypto || {getRandomValues}
if (!window.crypto) window.crypto = webcrypto || { getRandomValues };
11 changes: 5 additions & 6 deletions node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const crypto = require('crypto')
const crypto = require('crypto');

require('./polyfill')(crypto);

require('./polyfill')(crypto)
let { webcrypto } = crypto;
if (!webcrypto) crypto.webcrypto = webcrypto = {};

let {webcrypto} = crypto
if(!webcrypto) crypto.webcrypto = webcrypto = {}

if(!webcrypto.randomUUID) webcrypto.randomUUID = crypto.randomUUID
if (!webcrypto.randomUUID) webcrypto.randomUUID = crypto.randomUUID;
7 changes: 3 additions & 4 deletions polyfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = function(crypto)
{
if(!crypto.randomUUID) crypto.randomUUID = require('./randomUUID')
}
module.exports = function (crypto) {
if (!crypto.randomUUID) crypto.randomUUID = require('./randomUUID');
};
48 changes: 15 additions & 33 deletions randomUUID.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,45 @@
// to Node.js license found at:
// https://raw.githubusercontent.com/nodejs/node/master/LICENSE


//
// internal/errors
//
class ERR_INVALID_ARG_TYPE extends TypeError
{
constructor(name, type, value)
{
super(`${name} variable is not of type ${type} (value: '${value}')`)
class ERR_INVALID_ARG_TYPE extends TypeError {
constructor(name, type, value) {
super(`${name} variable is not of type ${type} (value: '${value}')`);
}

code = 'ERR_INVALID_ARG_TYPE'
code = 'ERR_INVALID_ARG_TYPE';
}


//
// internal/validators
//

function validateBoolean(value, name) {
if (typeof value !== 'boolean')
throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
if (typeof value !== 'boolean') throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
}

function validateObject(value, name) {
if (value === null ||
Array.isArray(value) ||
typeof value !== 'object') {
if (value === null || Array.isArray(value) || typeof value !== 'object') {
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
}
};

}

//
// crypto
//

const randomFillSync = typeof window === 'undefined'
? require('crypto').randomFillSync
: window.crypto.getRandomValues

const randomFillSync =
typeof window === 'undefined' ? require('crypto').randomFillSync : window.crypto.getRandomValues;

// Implements an RFC 4122 version 4 random UUID.
// To improve performance, random data is generated in batches
// large enough to cover kBatchSize UUID's at a time. The uuidData
// and uuid buffers are reused. Each call to randomUUID() consumes
// 16 bytes from the buffer.

const kHexDigits = [
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 97, 98, 99, 100, 101, 102
];
const kHexDigits = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102];

const kBatchSize = 128;
let uuidData;
Expand All @@ -68,15 +55,12 @@ function getBufferedUUID() {

if (uuidBatch === 0) randomFillSync(uuidData);
uuidBatch = (uuidBatch + 1) % kBatchSize;
return uuidData.slice(uuidBatch * 16, (uuidBatch * 16) + 16);
return uuidData.slice(uuidBatch * 16, uuidBatch * 16 + 16);
}

function randomUUID(options) {
if (options !== undefined)
validateObject(options, 'options');
const {
disableEntropyCache = false,
} = { ...options };
if (options !== undefined) validateObject(options, 'options');
const { disableEntropyCache = false } = { ...options };

validateBoolean(disableEntropyCache, 'options.disableEntropyCache');

Expand All @@ -91,8 +75,7 @@ function randomUUID(options) {
uuidBuf = getBufferedUUID();
} else {
uuidBuf = uuidNotBuffered;
if (uuidBuf === undefined)
uuidBuf = uuidNotBuffered = new Uint8Array(16);
if (uuidBuf === undefined) uuidBuf = uuidNotBuffered = new Uint8Array(16);
randomFillSync(uuidBuf);
}

Expand Down Expand Up @@ -144,9 +127,8 @@ function randomUUID(options) {
return String.fromCharCode.apply(null, uuid);
}


//
// Export `randomUUID` function
//

module.exports = randomUUID
module.exports = randomUUID;

0 comments on commit 7ce268c

Please sign in to comment.