Skip to content

Commit

Permalink
stdlib: fork uuid v4 from uuidjs/uuid
Browse files Browse the repository at this point in the history
Had issues with ES exports and Jest see uuidjs/uuid#451
  • Loading branch information
dirkdev98 committed May 15, 2020
1 parent 906753b commit 662d5a4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
4 changes: 1 addition & 3 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"packages": [
"packages/*"
],
"packages": ["packages/*"],
"version": "0.0.17",
"npmClient": "yarn",
"useWorkspaces": true
Expand Down
3 changes: 1 addition & 2 deletions packages/stdlib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"dependencies": {
"@lbu/insight": "^0.0.17",
"dotenv": "8.2.0",
"lodash.merge": "4.6.2",
"uuid": "8.0.0"
"lodash.merge": "4.6.2"
},
"author": {
"name": "Dirk de Visser",
Expand Down
2 changes: 1 addition & 1 deletion packages/stdlib/src/datatypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 } from "uuid";
import { v4 } from "./vendor/uuid.js";

/**
* Return a new uuid v4
Expand Down
79 changes: 79 additions & 0 deletions packages/stdlib/src/vendor/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
The MIT License (MIT)
Copyright (c) 2010-2020 Robert Kieffer and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// From: https://github.com/uuidjs/uuid/tree/a21e4d8c539bd7bfad29e360ab69766f941667b6

import { randomFillSync } from "crypto";

const rnds8 = new Uint8Array(16);
const byteToHex = [];

for (let i = 0; i < 256; ++i) {
byteToHex.push((i + 0x100).toString(16).substr(1));
}

/**
* Convert array of 16 byte values to UUID string format of the form:
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
*/
function bytesToUuid(buf, offset) {
const i = offset || 0;

const bth = byteToHex;

// Note: Be careful editing this code! It's been tuned for performance
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
return (
bth[buf[i + 0]] +
bth[buf[i + 1]] +
bth[buf[i + 2]] +
bth[buf[i + 3]] +
"-" +
bth[buf[i + 4]] +
bth[buf[i + 5]] +
"-" +
bth[buf[i + 6]] +
bth[buf[i + 7]] +
"-" +
bth[buf[i + 8]] +
bth[buf[i + 9]] +
"-" +
bth[buf[i + 10]] +
bth[buf[i + 11]] +
bth[buf[i + 12]] +
bth[buf[i + 13]] +
bth[buf[i + 14]] +
bth[buf[i + 15]]
).toLowerCase();
}

export function v4() {
const rnds = randomFillSync(rnds8);

// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;

return bytesToUuid(rnds);
}

0 comments on commit 662d5a4

Please sign in to comment.