backend v4 half
This commit is contained in:
+53
-33
@@ -1,36 +1,56 @@
|
||||
import parse from './parse.js';
|
||||
import { unsafeStringify } from './stringify.js';
|
||||
export function stringToBytes(str) {
|
||||
str = unescape(encodeURIComponent(str));
|
||||
const bytes = new Uint8Array(str.length);
|
||||
for (let i = 0; i < str.length; ++i) {
|
||||
bytes[i] = str.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
import bytesToUuid from './bytesToUuid.js';
|
||||
|
||||
function uuidToBytes(uuid) {
|
||||
// Note: We assume we're being passed a valid uuid string
|
||||
var bytes = [];
|
||||
uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
|
||||
bytes.push(parseInt(hex, 16));
|
||||
});
|
||||
return bytes;
|
||||
}
|
||||
export const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
||||
export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
|
||||
export default function v35(version, hash, value, namespace, buf, offset) {
|
||||
const valueBytes = typeof value === 'string' ? stringToBytes(value) : value;
|
||||
const namespaceBytes = typeof namespace === 'string' ? parse(namespace) : namespace;
|
||||
if (typeof namespace === 'string') {
|
||||
namespace = parse(namespace);
|
||||
}
|
||||
if (namespace?.length !== 16) {
|
||||
throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
|
||||
}
|
||||
let bytes = new Uint8Array(16 + valueBytes.length);
|
||||
bytes.set(namespaceBytes);
|
||||
bytes.set(valueBytes, namespaceBytes.length);
|
||||
bytes = hash(bytes);
|
||||
bytes[6] = (bytes[6] & 0x0f) | version;
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
||||
|
||||
function stringToBytes(str) {
|
||||
str = unescape(encodeURIComponent(str)); // UTF8 escape
|
||||
|
||||
var bytes = new Array(str.length);
|
||||
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
bytes[i] = str.charCodeAt(i);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export var DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
||||
export var URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
|
||||
export default function (name, version, hashfunc) {
|
||||
var generateUUID = function generateUUID(value, namespace, buf, offset) {
|
||||
var off = buf && offset || 0;
|
||||
if (typeof value == 'string') value = stringToBytes(value);
|
||||
if (typeof namespace == 'string') namespace = uuidToBytes(namespace);
|
||||
if (!Array.isArray(value)) throw TypeError('value must be an array of bytes');
|
||||
if (!Array.isArray(namespace) || namespace.length !== 16) throw TypeError('namespace must be uuid string or an Array of 16 byte values'); // Per 4.3
|
||||
|
||||
var bytes = hashfunc(namespace.concat(value));
|
||||
bytes[6] = bytes[6] & 0x0f | version;
|
||||
bytes[8] = bytes[8] & 0x3f | 0x80;
|
||||
|
||||
if (buf) {
|
||||
offset = offset || 0;
|
||||
for (let i = 0; i < 16; ++i) {
|
||||
buf[offset + i] = bytes[i];
|
||||
}
|
||||
return buf;
|
||||
for (var idx = 0; idx < 16; ++idx) {
|
||||
buf[off + idx] = bytes[idx];
|
||||
}
|
||||
}
|
||||
return unsafeStringify(bytes);
|
||||
}
|
||||
|
||||
return buf || bytesToUuid(bytes);
|
||||
}; // Function#name is not settable on some platforms (#270)
|
||||
|
||||
|
||||
try {
|
||||
generateUUID.name = name;
|
||||
} catch (err) {} // For CommonJS default export support
|
||||
|
||||
|
||||
generateUUID.DNS = DNS;
|
||||
generateUUID.URL = URL;
|
||||
return generateUUID;
|
||||
}
|
||||
Reference in New Issue
Block a user