Comparing version 0.1.11 to 0.2.0
@@ -1,4 +0,3 @@ | ||
import { TNamespaceId } from './TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from './TypeAliases/TUUIDVersion'; | ||
export declare function clockSequenceGetter(version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string): Uint8Array; | ||
export declare function clockSequenceGetter(version: TUUIDVersion, hash?: string): Uint8Array; | ||
export default clockSequenceGetter; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./convertBinStrToUint8Array", "./getHashFromNamespaceIdAndName", "./TypeGuards/isUUIDVersion", "./lastResults", "./randomBytesGenerator", "./strings", "./uintArrayAsNumber"], factory); | ||
define(["require", "exports", "./convertBinStrToUint8Array", "./TypeGuards/isUUIDVersion", "./lastResults", "./randomBytesGenerator", "./strings", "./uintArrayAsNumber"], factory); | ||
} | ||
@@ -14,3 +14,2 @@ })(function (require, exports) { | ||
const convertBinStrToUint8Array_1 = require("./convertBinStrToUint8Array"); | ||
const getHashFromNamespaceIdAndName_1 = require("./getHashFromNamespaceIdAndName"); | ||
const isUUIDVersion_1 = require("./TypeGuards/isUUIDVersion"); | ||
@@ -21,3 +20,3 @@ const lastResults_1 = require("./lastResults"); | ||
const uintArrayAsNumber_1 = require("./uintArrayAsNumber"); | ||
function clockSequenceGetter(version, namespaceId, name) { | ||
function clockSequenceGetter(version, hash) { | ||
if (!isUUIDVersion_1.isUUIDVersion(version)) { | ||
@@ -48,3 +47,5 @@ throw new Error(strings_1.strings.UUID_VERSION_INVALID); | ||
else { | ||
const hash = getHashFromNamespaceIdAndName_1.getHashFromNamespaceIdAndName(version, namespaceId, name); | ||
if (!hash) { | ||
throw new Error(strings_1.strings.HASH_ARGUMENT_MISSING); | ||
} | ||
let clockSequenceStr = ''; | ||
@@ -51,0 +52,0 @@ clockSequenceStr += hash.slice(16, 18); |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "crypto", "./strings"], factory); | ||
define(["require", "exports", "crypto-js", "./strings"], factory); | ||
} | ||
@@ -13,3 +13,3 @@ })(function (require, exports) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const crypto_1 = require("crypto"); | ||
const crypto_js_1 = require("crypto-js"); | ||
const strings_1 = require("./strings"); | ||
@@ -23,9 +23,2 @@ exports.getHashFromNamespaceIdAndName = (version, namespaceId, name) => { | ||
} | ||
let hasher; | ||
if (version.toString() === '3') { | ||
hasher = crypto_1.createHash('md5'); | ||
} | ||
else { | ||
hasher = crypto_1.createHash('sha1'); | ||
} | ||
const bigEndianNamespaceId = namespaceId.split('-').map((segment) => { | ||
@@ -39,5 +32,12 @@ const len = segment.length; | ||
}).join('-'); | ||
hasher.update(bigEndianNamespaceId + name); | ||
return hasher.digest('hex'); | ||
const toHash = bigEndianNamespaceId + name; | ||
let hash; | ||
if (version.toString() === '3') { | ||
hash = crypto_js_1.MD5(toHash); | ||
} | ||
else { | ||
hash = crypto_js_1.SHA1(toHash); | ||
} | ||
return crypto_js_1.enc.Hex.stringify(hash); | ||
}; | ||
}); |
@@ -0,6 +1,6 @@ | ||
export * from './UUID/UUID'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './Enums/NamespaceIds'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUID'; | ||
export * from './clockSequenceGetter'; | ||
@@ -7,0 +7,0 @@ export * from './getHashFromNamespaceIdAndName'; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./Enums/NamespaceIds", "./UUID/UUIDOptions/UUIDOptions", "./UUID/UUID", "./clockSequenceGetter", "./getHashFromNamespaceIdAndName", "./getHundredsOfNanosecondsSinceGregorianReform", "./nodeIdentifierGetter", "./numberAsLittleEndianHexStr", "./timestampGetter", "./uintArrayAsNumber"], factory); | ||
define(["require", "exports", "./UUID/UUID", "./UUID/UUIDOptions/UUIDOptions", "./Enums/NamespaceIds", "./clockSequenceGetter", "./getHashFromNamespaceIdAndName", "./getHundredsOfNanosecondsSinceGregorianReform", "./nodeIdentifierGetter", "./numberAsLittleEndianHexStr", "./timestampGetter", "./uintArrayAsNumber"], factory); | ||
} | ||
@@ -16,5 +16,5 @@ })(function (require, exports) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./UUID/UUID")); | ||
__export(require("./UUID/UUIDOptions/UUIDOptions")); | ||
__export(require("./Enums/NamespaceIds")); | ||
__export(require("./UUID/UUIDOptions/UUIDOptions")); | ||
__export(require("./UUID/UUID")); | ||
__export(require("./clockSequenceGetter")); | ||
@@ -21,0 +21,0 @@ __export(require("./getHashFromNamespaceIdAndName")); |
@@ -1,4 +0,3 @@ | ||
import { TNamespaceId } from './TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from './TypeAliases/TUUIDVersion'; | ||
export declare function nodeIdentifierGetter(version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string): Uint8Array; | ||
export declare function nodeIdentifierGetter(version: TUUIDVersion, hash?: string): Uint8Array; | ||
export default nodeIdentifierGetter; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./convertBinStrToUint8Array", "./getHashFromNamespaceIdAndName", "./getMAC", "./lastResults", "./randomBytesGenerator", "./strings"], factory); | ||
define(["require", "exports", "./convertBinStrToUint8Array", "./getMAC", "./lastResults", "./randomBytesGenerator", "./strings"], factory); | ||
} | ||
@@ -14,3 +14,2 @@ })(function (require, exports) { | ||
const convertBinStrToUint8Array_1 = require("./convertBinStrToUint8Array"); | ||
const getHashFromNamespaceIdAndName_1 = require("./getHashFromNamespaceIdAndName"); | ||
const getMAC_1 = require("./getMAC"); | ||
@@ -20,3 +19,3 @@ const lastResults_1 = require("./lastResults"); | ||
const strings_1 = require("./strings"); | ||
function nodeIdentifierGetter(version, namespaceId, name) { | ||
function nodeIdentifierGetter(version, hash) { | ||
let nodeIdentifier; | ||
@@ -32,3 +31,5 @@ if (version.toString() === '1') { | ||
else if (/^[35]$/.test(version.toString())) { | ||
const hash = getHashFromNamespaceIdAndName_1.getHashFromNamespaceIdAndName(version, namespaceId, name); | ||
if (!hash) { | ||
throw new Error(strings_1.strings.HASH_ARGUMENT_MISSING); | ||
} | ||
let nodeIdentifierStr = ''; | ||
@@ -35,0 +36,0 @@ nodeIdentifierStr += hash.slice(20, 32); |
@@ -13,4 +13,7 @@ export declare const strings: { | ||
CLOCK_HIGH_AND_RESERVED_INVALID: string; | ||
HASH_ARGUMENT_MISSING: string; | ||
MAC_ADDRESS_UNAVAILABLE: string; | ||
MAC_ADDRESS_INVALID: string; | ||
MD5_IN_BROWSER: string; | ||
NO_HASHER_FOUND: string; | ||
UUID_LAST_RESULTS_INVALID: string; | ||
@@ -17,0 +20,0 @@ UUID_STRING_INVALID: string; |
@@ -30,5 +30,10 @@ (function (factory) { | ||
'malformed.', | ||
HASH_ARGUMENT_MISSING: 'The hash argument was not provided, without which a v3 or v5 UUID ' + | ||
'cannot be created.', | ||
MAC_ADDRESS_UNAVAILABLE: 'The MAC address is unavailable, which makes creating the version 1 ' + | ||
'node ID impossible.', | ||
MAC_ADDRESS_INVALID: 'The MAC address was found, but it was malformed.', | ||
MD5_IN_BROWSER: 'MD5 is a dangerously outmoded hash algorithm and cannot be used in ' + | ||
'modern browsers.', | ||
NO_HASHER_FOUND: 'No suitable hash function could be found.', | ||
UUID_LAST_RESULTS_INVALID: 'The provided last results object did not meet the isValidLastResults ' + | ||
@@ -35,0 +40,0 @@ 'type guard.', |
@@ -1,4 +0,3 @@ | ||
import { TNamespaceId } from './TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from './TypeAliases/TUUIDVersion'; | ||
export declare function timestampGetter(version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string): Uint8Array; | ||
export declare function timestampGetter(version: TUUIDVersion, hash?: string): Uint8Array; | ||
export default timestampGetter; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./convertBinStrToUint8Array", "./getHashFromNamespaceIdAndName", "./getHundredsOfNanosecondsSinceGregorianReform", "./TypeGuards/isUUIDVersion", "./lastResults", "./randomBytesGenerator", "./strings", "./uintArrayAsNumber"], factory); | ||
define(["require", "exports", "./convertBinStrToUint8Array", "./getHundredsOfNanosecondsSinceGregorianReform", "./TypeGuards/isUUIDVersion", "./lastResults", "./randomBytesGenerator", "./strings", "./uintArrayAsNumber"], factory); | ||
} | ||
@@ -14,3 +14,2 @@ })(function (require, exports) { | ||
const convertBinStrToUint8Array_1 = require("./convertBinStrToUint8Array"); | ||
const getHashFromNamespaceIdAndName_1 = require("./getHashFromNamespaceIdAndName"); | ||
const getHundredsOfNanosecondsSinceGregorianReform_1 = require("./getHundredsOfNanosecondsSinceGregorianReform"); | ||
@@ -22,3 +21,3 @@ const isUUIDVersion_1 = require("./TypeGuards/isUUIDVersion"); | ||
const uintArrayAsNumber_1 = require("./uintArrayAsNumber"); | ||
function timestampGetter(version, namespaceId, name) { | ||
function timestampGetter(version, hash) { | ||
if (!isUUIDVersion_1.isUUIDVersion(version)) { | ||
@@ -45,3 +44,5 @@ throw new Error(strings_1.strings.UUID_VERSION_INVALID); | ||
else if (/^[35]$/.test(version.toString())) { | ||
const hash = getHashFromNamespaceIdAndName_1.getHashFromNamespaceIdAndName(version, namespaceId, name); | ||
if (!hash) { | ||
throw new Error(strings_1.strings.HASH_ARGUMENT_MISSING); | ||
} | ||
let timestampStr = ''; | ||
@@ -48,0 +49,0 @@ timestampStr = hash.slice(0, 8); |
@@ -5,3 +5,3 @@ import { IUUID } from './IUUID'; | ||
export declare class UUID implements IUUID { | ||
constructor(options?: Partial<IUUIDOptions>); | ||
constructor(opts?: Partial<IUUIDOptions>); | ||
private __version; | ||
@@ -8,0 +8,0 @@ readonly version: TUUIDVersion; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "../isNode", "../TypeGuards/isUUIDVersion", "../numberAsLittleEndianHexStr", "../strings", "../uintArrayAsNumber", "./UUIDOptions/UUIDOptions", "../writeNewResults"], factory); | ||
define(["require", "exports", "../getHashFromNamespaceIdAndName", "../isNode", "../TypeGuards/isUUIDVersion", "../numberAsLittleEndianHexStr", "../strings", "../uintArrayAsNumber", "./UUIDOptions/UUIDOptions", "../writeNewResults"], factory); | ||
} | ||
@@ -13,2 +13,3 @@ })(function (require, exports) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const getHashFromNamespaceIdAndName_1 = require("../getHashFromNamespaceIdAndName"); | ||
const isNode_1 = require("../isNode"); | ||
@@ -22,25 +23,22 @@ const isUUIDVersion_1 = require("../TypeGuards/isUUIDVersion"); | ||
class UUID { | ||
constructor(options) { | ||
const opts = new UUIDOptions_1.UUIDOptions(); | ||
if (typeof options === 'object' && options) { | ||
if (options.version) { | ||
opts.version = options.version; | ||
constructor(opts) { | ||
const options = new UUIDOptions_1.UUIDOptions(); | ||
if (opts && typeof opts === 'object') { | ||
if (opts.version) { | ||
options.version = opts.version; | ||
} | ||
if (options.clockSequenceGetter) { | ||
opts.clockSequenceGetter = options.clockSequenceGetter; | ||
if (opts.clockSequenceGetter) { | ||
options.clockSequenceGetter = opts.clockSequenceGetter; | ||
} | ||
if (options.timestampGetter) { | ||
opts.timestampGetter = options.timestampGetter; | ||
if (opts.timestampGetter) { | ||
options.timestampGetter = opts.timestampGetter; | ||
} | ||
if (options.nodeIdentifierGetter) { | ||
opts.nodeIdentifierGetter = options.nodeIdentifierGetter; | ||
if (opts.nodeIdentifierGetter) { | ||
options.nodeIdentifierGetter = opts.nodeIdentifierGetter; | ||
} | ||
if (options.namespaceId) { | ||
opts.namespaceId = options.namespaceId; | ||
if (opts.namespaceId) { | ||
options.namespaceId = opts.namespaceId; | ||
} | ||
if (options.name) { | ||
opts.name = options.name; | ||
} | ||
} | ||
let version = opts.version; | ||
let version = options.version; | ||
if (!isUUIDVersion_1.isUUIDVersion(version)) { | ||
@@ -59,15 +57,22 @@ throw new Error(strings_1.strings.UUID_VERSION_INVALID); | ||
if (/^[35]$/.test(version.toString())) { | ||
const clockSequence = opts.clockSequenceGetter(version, opts.namespaceId, opts.name); | ||
if (!options.namespaceId) { | ||
throw new Error(strings_1.strings.NAMESPACE_ID_MISSING); | ||
} | ||
else if (!options.name) { | ||
throw new Error(strings_1.strings.NAME_MISSING); | ||
} | ||
const hash = getHashFromNamespaceIdAndName_1.getHashFromNamespaceIdAndName(version, options.namespaceId, options.name); | ||
const clockSequence = options.clockSequenceGetter(version, hash); | ||
this.__clockSequence = clockSequence; | ||
const timestamp = opts.timestampGetter(version, opts.namespaceId, opts.name); | ||
const timestamp = options.timestampGetter(version, hash); | ||
this.__timestamp = timestamp; | ||
const nodeIdentifier = opts.nodeIdentifierGetter(version, opts.namespaceId, opts.name); | ||
const nodeIdentifier = options.nodeIdentifierGetter(version, hash); | ||
this.__nodeIdentifier = nodeIdentifier; | ||
} | ||
else { | ||
const clockSequence = opts.clockSequenceGetter(version); | ||
const clockSequence = options.clockSequenceGetter(version); | ||
this.__clockSequence = clockSequence; | ||
const timestamp = opts.timestampGetter(version); | ||
const timestamp = options.timestampGetter(version); | ||
this.__timestamp = timestamp; | ||
const nodeIdentifier = opts.nodeIdentifierGetter(version); | ||
const nodeIdentifier = options.nodeIdentifierGetter(version); | ||
this.__nodeIdentifier = nodeIdentifier; | ||
@@ -74,0 +79,0 @@ if (isNode_1.isNode() && this.version.toString() === '1') { |
@@ -0,7 +1,7 @@ | ||
import { TNamespaceId } from '../../TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from '../../TypeAliases/TUUIDVersion'; | ||
import { TNamespaceId } from '../../TypeAliases/TNamespaceId'; | ||
export interface IUUIDOptions { | ||
version: TUUIDVersion; | ||
namespaceId?: TNamespaceId; | ||
name?: string; | ||
namespaceId?: TNamespaceId; | ||
nodeIdentifierGetter: (version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string) => Uint8Array; | ||
@@ -8,0 +8,0 @@ timestampGetter: (version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string) => Uint8Array; |
@@ -9,7 +9,7 @@ import { clockSequenceGetter } from '../../clockSequenceGetter'; | ||
version: TUUIDVersion; | ||
namespaceId?: TNamespaceId; | ||
name?: string; | ||
clockSequenceGetter: typeof clockSequenceGetter; | ||
nodeIdentifierGetter: typeof nodeIdentifierGetter; | ||
timestampGetter: typeof timestampGetter; | ||
name?: string; | ||
namespaceId?: TNamespaceId; | ||
constructor(_args?: { | ||
@@ -16,0 +16,0 @@ [key: string]: any; |
@@ -30,15 +30,15 @@ (function (factory) { | ||
} | ||
if (typeof args.clockSequenceGetter === 'function') { | ||
if (args.clockSequenceGetter) { | ||
this.clockSequenceGetter = args.clockSequenceGetter; | ||
} | ||
if (typeof args.nodeIdentifierGetter === 'function') { | ||
if (args.nodeIdentifierGetter) { | ||
this.nodeIdentifierGetter = args.nodeIdentifierGetter; | ||
} | ||
if (typeof args.timestampGetter === 'function') { | ||
if (args.timestampGetter) { | ||
this.timestampGetter = args.timestampGetter; | ||
} | ||
if (args.name && typeof args.name === 'string') { | ||
this.name = args.name; | ||
if (args.namespaceId) { | ||
this.namespaceId = args.namespaceId; | ||
} | ||
if (args.namespaceId && typeof args.namespaceId === 'string') { | ||
if (args.name) { | ||
this.namespaceId = args.namespaceId; | ||
@@ -45,0 +45,0 @@ } |
@@ -1,4 +0,3 @@ | ||
import { TNamespaceId } from './TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from './TypeAliases/TUUIDVersion'; | ||
export declare function clockSequenceGetter(version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string): Uint8Array; | ||
export declare function clockSequenceGetter(version: TUUIDVersion, hash?: string): Uint8Array; | ||
export default clockSequenceGetter; |
import { convertBinStrToUint8Array, } from './convertBinStrToUint8Array'; | ||
import { getHashFromNamespaceIdAndName, } from './getHashFromNamespaceIdAndName'; | ||
import { isUUIDVersion, } from './TypeGuards/isUUIDVersion'; | ||
@@ -8,3 +7,3 @@ import { lastResults, } from './lastResults'; | ||
import { uintArrayAsNumber, } from './uintArrayAsNumber'; | ||
export function clockSequenceGetter(version, namespaceId, name) { | ||
export function clockSequenceGetter(version, hash) { | ||
if (!isUUIDVersion(version)) { | ||
@@ -35,3 +34,5 @@ throw new Error(strings.UUID_VERSION_INVALID); | ||
else { | ||
const hash = getHashFromNamespaceIdAndName(version, namespaceId, name); | ||
if (!hash) { | ||
throw new Error(strings.HASH_ARGUMENT_MISSING); | ||
} | ||
let clockSequenceStr = ''; | ||
@@ -38,0 +39,0 @@ clockSequenceStr += hash.slice(16, 18); |
@@ -1,2 +0,2 @@ | ||
import { createHash, } from 'crypto'; | ||
import { enc, SHA1, MD5, } from 'crypto-js'; | ||
import { strings, } from './strings'; | ||
@@ -10,9 +10,2 @@ export const getHashFromNamespaceIdAndName = (version, namespaceId, name) => { | ||
} | ||
let hasher; | ||
if (version.toString() === '3') { | ||
hasher = createHash('md5'); | ||
} | ||
else { | ||
hasher = createHash('sha1'); | ||
} | ||
const bigEndianNamespaceId = namespaceId.split('-').map((segment) => { | ||
@@ -26,4 +19,11 @@ const len = segment.length; | ||
}).join('-'); | ||
hasher.update(bigEndianNamespaceId + name); | ||
return hasher.digest('hex'); | ||
const toHash = bigEndianNamespaceId + name; | ||
let hash; | ||
if (version.toString() === '3') { | ||
hash = MD5(toHash); | ||
} | ||
else { | ||
hash = SHA1(toHash); | ||
} | ||
return enc.Hex.stringify(hash); | ||
}; |
@@ -0,6 +1,6 @@ | ||
export * from './UUID/UUID'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './Enums/NamespaceIds'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUID'; | ||
export * from './clockSequenceGetter'; | ||
@@ -7,0 +7,0 @@ export * from './getHashFromNamespaceIdAndName'; |
@@ -0,4 +1,4 @@ | ||
export * from './UUID/UUID'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './Enums/NamespaceIds'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/UUID'; | ||
export * from './clockSequenceGetter'; | ||
@@ -5,0 +5,0 @@ export * from './getHashFromNamespaceIdAndName'; |
@@ -1,4 +0,3 @@ | ||
import { TNamespaceId } from './TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from './TypeAliases/TUUIDVersion'; | ||
export declare function nodeIdentifierGetter(version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string): Uint8Array; | ||
export declare function nodeIdentifierGetter(version: TUUIDVersion, hash?: string): Uint8Array; | ||
export default nodeIdentifierGetter; |
import { convertBinStrToUint8Array, } from './convertBinStrToUint8Array'; | ||
import { getHashFromNamespaceIdAndName, } from './getHashFromNamespaceIdAndName'; | ||
import { getMAC, } from './getMAC'; | ||
@@ -7,3 +6,3 @@ import { lastResults, } from './lastResults'; | ||
import { strings, } from './strings'; | ||
export function nodeIdentifierGetter(version, namespaceId, name) { | ||
export function nodeIdentifierGetter(version, hash) { | ||
let nodeIdentifier; | ||
@@ -19,3 +18,5 @@ if (version.toString() === '1') { | ||
else if (/^[35]$/.test(version.toString())) { | ||
const hash = getHashFromNamespaceIdAndName(version, namespaceId, name); | ||
if (!hash) { | ||
throw new Error(strings.HASH_ARGUMENT_MISSING); | ||
} | ||
let nodeIdentifierStr = ''; | ||
@@ -22,0 +23,0 @@ nodeIdentifierStr += hash.slice(20, 32); |
@@ -13,4 +13,7 @@ export declare const strings: { | ||
CLOCK_HIGH_AND_RESERVED_INVALID: string; | ||
HASH_ARGUMENT_MISSING: string; | ||
MAC_ADDRESS_UNAVAILABLE: string; | ||
MAC_ADDRESS_INVALID: string; | ||
MD5_IN_BROWSER: string; | ||
NO_HASHER_FOUND: string; | ||
UUID_LAST_RESULTS_INVALID: string; | ||
@@ -17,0 +20,0 @@ UUID_STRING_INVALID: string; |
@@ -19,5 +19,10 @@ export const strings = { | ||
'malformed.', | ||
HASH_ARGUMENT_MISSING: 'The hash argument was not provided, without which a v3 or v5 UUID ' + | ||
'cannot be created.', | ||
MAC_ADDRESS_UNAVAILABLE: 'The MAC address is unavailable, which makes creating the version 1 ' + | ||
'node ID impossible.', | ||
MAC_ADDRESS_INVALID: 'The MAC address was found, but it was malformed.', | ||
MD5_IN_BROWSER: 'MD5 is a dangerously outmoded hash algorithm and cannot be used in ' + | ||
'modern browsers.', | ||
NO_HASHER_FOUND: 'No suitable hash function could be found.', | ||
UUID_LAST_RESULTS_INVALID: 'The provided last results object did not meet the isValidLastResults ' + | ||
@@ -24,0 +29,0 @@ 'type guard.', |
@@ -1,4 +0,3 @@ | ||
import { TNamespaceId } from './TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from './TypeAliases/TUUIDVersion'; | ||
export declare function timestampGetter(version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string): Uint8Array; | ||
export declare function timestampGetter(version: TUUIDVersion, hash?: string): Uint8Array; | ||
export default timestampGetter; |
import { convertBinStrToUint8Array, } from './convertBinStrToUint8Array'; | ||
import { getHashFromNamespaceIdAndName, } from './getHashFromNamespaceIdAndName'; | ||
import { getHundredsOfNanosecondsSinceGregorianReform, } from './getHundredsOfNanosecondsSinceGregorianReform'; | ||
@@ -9,3 +8,3 @@ import { isUUIDVersion, } from './TypeGuards/isUUIDVersion'; | ||
import { uintArrayAsNumber, } from './uintArrayAsNumber'; | ||
export function timestampGetter(version, namespaceId, name) { | ||
export function timestampGetter(version, hash) { | ||
if (!isUUIDVersion(version)) { | ||
@@ -32,3 +31,5 @@ throw new Error(strings.UUID_VERSION_INVALID); | ||
else if (/^[35]$/.test(version.toString())) { | ||
const hash = getHashFromNamespaceIdAndName(version, namespaceId, name); | ||
if (!hash) { | ||
throw new Error(strings.HASH_ARGUMENT_MISSING); | ||
} | ||
let timestampStr = ''; | ||
@@ -35,0 +36,0 @@ timestampStr = hash.slice(0, 8); |
@@ -5,3 +5,3 @@ import { IUUID } from './IUUID'; | ||
export declare class UUID implements IUUID { | ||
constructor(options?: Partial<IUUIDOptions>); | ||
constructor(opts?: Partial<IUUIDOptions>); | ||
private __version; | ||
@@ -8,0 +8,0 @@ readonly version: TUUIDVersion; |
@@ -0,1 +1,2 @@ | ||
import { getHashFromNamespaceIdAndName, } from '../getHashFromNamespaceIdAndName'; | ||
import { isNode, } from '../isNode'; | ||
@@ -9,25 +10,22 @@ import { isUUIDVersion, } from '../TypeGuards/isUUIDVersion'; | ||
export class UUID { | ||
constructor(options) { | ||
const opts = new UUIDOptions(); | ||
if (typeof options === 'object' && options) { | ||
if (options.version) { | ||
opts.version = options.version; | ||
constructor(opts) { | ||
const options = new UUIDOptions(); | ||
if (opts && typeof opts === 'object') { | ||
if (opts.version) { | ||
options.version = opts.version; | ||
} | ||
if (options.clockSequenceGetter) { | ||
opts.clockSequenceGetter = options.clockSequenceGetter; | ||
if (opts.clockSequenceGetter) { | ||
options.clockSequenceGetter = opts.clockSequenceGetter; | ||
} | ||
if (options.timestampGetter) { | ||
opts.timestampGetter = options.timestampGetter; | ||
if (opts.timestampGetter) { | ||
options.timestampGetter = opts.timestampGetter; | ||
} | ||
if (options.nodeIdentifierGetter) { | ||
opts.nodeIdentifierGetter = options.nodeIdentifierGetter; | ||
if (opts.nodeIdentifierGetter) { | ||
options.nodeIdentifierGetter = opts.nodeIdentifierGetter; | ||
} | ||
if (options.namespaceId) { | ||
opts.namespaceId = options.namespaceId; | ||
if (opts.namespaceId) { | ||
options.namespaceId = opts.namespaceId; | ||
} | ||
if (options.name) { | ||
opts.name = options.name; | ||
} | ||
} | ||
let version = opts.version; | ||
let version = options.version; | ||
if (!isUUIDVersion(version)) { | ||
@@ -46,15 +44,22 @@ throw new Error(strings.UUID_VERSION_INVALID); | ||
if (/^[35]$/.test(version.toString())) { | ||
const clockSequence = opts.clockSequenceGetter(version, opts.namespaceId, opts.name); | ||
if (!options.namespaceId) { | ||
throw new Error(strings.NAMESPACE_ID_MISSING); | ||
} | ||
else if (!options.name) { | ||
throw new Error(strings.NAME_MISSING); | ||
} | ||
const hash = getHashFromNamespaceIdAndName(version, options.namespaceId, options.name); | ||
const clockSequence = options.clockSequenceGetter(version, hash); | ||
this.__clockSequence = clockSequence; | ||
const timestamp = opts.timestampGetter(version, opts.namespaceId, opts.name); | ||
const timestamp = options.timestampGetter(version, hash); | ||
this.__timestamp = timestamp; | ||
const nodeIdentifier = opts.nodeIdentifierGetter(version, opts.namespaceId, opts.name); | ||
const nodeIdentifier = options.nodeIdentifierGetter(version, hash); | ||
this.__nodeIdentifier = nodeIdentifier; | ||
} | ||
else { | ||
const clockSequence = opts.clockSequenceGetter(version); | ||
const clockSequence = options.clockSequenceGetter(version); | ||
this.__clockSequence = clockSequence; | ||
const timestamp = opts.timestampGetter(version); | ||
const timestamp = options.timestampGetter(version); | ||
this.__timestamp = timestamp; | ||
const nodeIdentifier = opts.nodeIdentifierGetter(version); | ||
const nodeIdentifier = options.nodeIdentifierGetter(version); | ||
this.__nodeIdentifier = nodeIdentifier; | ||
@@ -61,0 +66,0 @@ if (isNode() && this.version.toString() === '1') { |
@@ -0,7 +1,7 @@ | ||
import { TNamespaceId } from '../../TypeAliases/TNamespaceId'; | ||
import { TUUIDVersion } from '../../TypeAliases/TUUIDVersion'; | ||
import { TNamespaceId } from '../../TypeAliases/TNamespaceId'; | ||
export interface IUUIDOptions { | ||
version: TUUIDVersion; | ||
namespaceId?: TNamespaceId; | ||
name?: string; | ||
namespaceId?: TNamespaceId; | ||
nodeIdentifierGetter: (version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string) => Uint8Array; | ||
@@ -8,0 +8,0 @@ timestampGetter: (version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string) => Uint8Array; |
@@ -9,7 +9,7 @@ import { clockSequenceGetter } from '../../clockSequenceGetter'; | ||
version: TUUIDVersion; | ||
namespaceId?: TNamespaceId; | ||
name?: string; | ||
clockSequenceGetter: typeof clockSequenceGetter; | ||
nodeIdentifierGetter: typeof nodeIdentifierGetter; | ||
timestampGetter: typeof timestampGetter; | ||
name?: string; | ||
namespaceId?: TNamespaceId; | ||
constructor(_args?: { | ||
@@ -16,0 +16,0 @@ [key: string]: any; |
@@ -19,15 +19,15 @@ import { clockSequenceGetter, } from '../../clockSequenceGetter'; | ||
} | ||
if (typeof args.clockSequenceGetter === 'function') { | ||
if (args.clockSequenceGetter) { | ||
this.clockSequenceGetter = args.clockSequenceGetter; | ||
} | ||
if (typeof args.nodeIdentifierGetter === 'function') { | ||
if (args.nodeIdentifierGetter) { | ||
this.nodeIdentifierGetter = args.nodeIdentifierGetter; | ||
} | ||
if (typeof args.timestampGetter === 'function') { | ||
if (args.timestampGetter) { | ||
this.timestampGetter = args.timestampGetter; | ||
} | ||
if (args.name && typeof args.name === 'string') { | ||
this.name = args.name; | ||
if (args.namespaceId) { | ||
this.namespaceId = args.namespaceId; | ||
} | ||
if (args.namespaceId && typeof args.namespaceId === 'string') { | ||
if (args.name) { | ||
this.namespaceId = args.namespaceId; | ||
@@ -34,0 +34,0 @@ } |
{ | ||
"name": "big-uuid", | ||
"version": "0.1.11", | ||
"version": "0.2.0", | ||
"description": "An RFC 4112 conformant UUID generator creating v1, v3, v4, and v5 UUIDs.", | ||
"main": "dist/node.es6/index.js", | ||
"browser": "dist/umd/index.js", | ||
"browser": "dist/browser/index.js", | ||
"types": "src/index.d.ts", | ||
"scripts": { | ||
"build": "cross-env NODE_ENV=production npm run build-node && npm run build-browser && npm run copy-declaration-file", | ||
"build": "cross-env NODE_ENV=production npm run build-node && npm run build-browser && npm run copy-declaration-file", | ||
"build-node": "npm run build-node-esnext && npm run build-node-es6", | ||
@@ -38,2 +38,3 @@ "build-node-esnext": "tsc", | ||
"@babel/preset-env": "^7.1.0", | ||
"@types/crypto-js": "^3.1.43", | ||
"@types/jest": "^23.3.5", | ||
@@ -66,3 +67,6 @@ "@types/node": "^10.11.7", | ||
} | ||
}, | ||
"dependencies": { | ||
"crypto-js": "^3.1.9-1" | ||
} | ||
} |
@@ -5,5 +5,2 @@ import { | ||
import { | ||
getHashFromNamespaceIdAndName, | ||
} from './getHashFromNamespaceIdAndName'; | ||
import { | ||
isUUIDVersion, | ||
@@ -21,5 +18,2 @@ } from './TypeGuards/isUUIDVersion'; | ||
import { | ||
TNamespaceId, | ||
} from './TypeAliases/TNamespaceId'; | ||
import { | ||
TUUIDVersion, | ||
@@ -33,4 +27,3 @@ } from './TypeAliases/TUUIDVersion'; | ||
version: TUUIDVersion, | ||
namespaceId?: TNamespaceId, | ||
name?: string, | ||
hash?: string, | ||
): Uint8Array | ||
@@ -67,7 +60,5 @@ { | ||
/* Version is 3 or 5. */ | ||
const hash = getHashFromNamespaceIdAndName( | ||
version, | ||
namespaceId!, | ||
name!, | ||
); | ||
if (!hash) { | ||
throw new Error(strings.HASH_ARGUMENT_MISSING); | ||
} | ||
@@ -74,0 +65,0 @@ let clockSequenceStr = ''; |
import { | ||
createHash, | ||
} from 'crypto'; | ||
enc, | ||
SHA1, | ||
MD5, | ||
} from 'crypto-js'; | ||
import { | ||
@@ -25,9 +27,3 @@ strings, | ||
let hasher; | ||
if (version.toString() === '3') { | ||
hasher = createHash('md5'); | ||
} else { | ||
hasher = createHash('sha1'); | ||
} | ||
/* Put the namespace ID into "network byte order" (big-endian) */ | ||
const bigEndianNamespaceId = namespaceId.split('-').map((segment) => { | ||
@@ -45,4 +41,11 @@ const len = segment.length; | ||
hasher.update(bigEndianNamespaceId + name); | ||
return hasher.digest('hex'); | ||
const toHash = bigEndianNamespaceId + name; | ||
let hash; | ||
if (version.toString() === '3') { | ||
hash = MD5(toHash); | ||
} else { | ||
hash = SHA1(toHash); | ||
} | ||
return enc.Hex.stringify(hash); | ||
} |
@@ -0,6 +1,6 @@ | ||
export * from './UUID/UUID'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './Enums/NamespaceIds'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUID'; | ||
export * from './clockSequenceGetter'; | ||
@@ -7,0 +7,0 @@ export * from './getHashFromNamespaceIdAndName'; |
/* An RFC 4112 UUID generator for generating IFIDs, or Interactive Fiction | ||
* Identifiers. */ | ||
export * from './UUID/UUID'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './Enums/NamespaceIds'; | ||
export * from './UUID/UUIDOptions/IUUIDOptions'; | ||
export * from './UUID/UUIDOptions/UUIDOptions'; | ||
export * from './UUID/IUUID'; | ||
export * from './UUID/UUID'; | ||
export * from './clockSequenceGetter'; | ||
@@ -12,0 +12,0 @@ export * from './getHashFromNamespaceIdAndName'; |
@@ -5,5 +5,2 @@ import { | ||
import { | ||
getHashFromNamespaceIdAndName, | ||
} from './getHashFromNamespaceIdAndName'; | ||
import { | ||
getMAC, | ||
@@ -21,5 +18,2 @@ } from './getMAC'; | ||
import { | ||
TNamespaceId, | ||
} from './TypeAliases/TNamespaceId'; | ||
import { | ||
TUUIDVersion, | ||
@@ -30,4 +24,3 @@ } from './TypeAliases/TUUIDVersion'; | ||
version: TUUIDVersion, | ||
namespaceId?: TNamespaceId, | ||
name?: string, | ||
hash?: string, | ||
): Uint8Array | ||
@@ -46,7 +39,5 @@ { | ||
} else if (/^[35]$/.test(version.toString())) { | ||
const hash = getHashFromNamespaceIdAndName( | ||
version, | ||
namespaceId!, | ||
name!, | ||
); | ||
if (!hash) { | ||
throw new Error(strings.HASH_ARGUMENT_MISSING); | ||
} | ||
@@ -53,0 +44,0 @@ let nodeIdentifierStr = ''; |
@@ -41,2 +41,6 @@ export const strings = { | ||
HASH_ARGUMENT_MISSING: | ||
'The hash argument was not provided, without which a v3 or v5 UUID ' + | ||
'cannot be created.', | ||
MAC_ADDRESS_UNAVAILABLE: | ||
@@ -49,2 +53,9 @@ 'The MAC address is unavailable, which makes creating the version 1 ' + | ||
MD5_IN_BROWSER: | ||
'MD5 is a dangerously outmoded hash algorithm and cannot be used in ' + | ||
'modern browsers.', | ||
NO_HASHER_FOUND: | ||
'No suitable hash function could be found.', | ||
UUID_LAST_RESULTS_INVALID: | ||
@@ -51,0 +62,0 @@ 'The provided last results object did not meet the isValidLastResults ' + |
@@ -5,5 +5,2 @@ import { | ||
import { | ||
getHashFromNamespaceIdAndName, | ||
} from './getHashFromNamespaceIdAndName'; | ||
import { | ||
getHundredsOfNanosecondsSinceGregorianReform, | ||
@@ -24,5 +21,2 @@ } from './getHundredsOfNanosecondsSinceGregorianReform'; | ||
import { | ||
TNamespaceId, | ||
} from './TypeAliases/TNamespaceId'; | ||
import { | ||
TUUIDVersion, | ||
@@ -36,4 +30,3 @@ } from './TypeAliases/TUUIDVersion'; | ||
version: TUUIDVersion, | ||
namespaceId?: TNamespaceId, | ||
name?: string, | ||
hash?: string, | ||
): Uint8Array | ||
@@ -68,7 +61,5 @@ { | ||
/* Version is 3 or 5. */ | ||
const hash = getHashFromNamespaceIdAndName( | ||
version, | ||
namespaceId!, | ||
name!, | ||
); | ||
if (!hash) { | ||
throw new Error(strings.HASH_ARGUMENT_MISSING); | ||
} | ||
@@ -75,0 +66,0 @@ let timestampStr = ''; |
import { | ||
getHashFromNamespaceIdAndName, | ||
} from '../getHashFromNamespaceIdAndName'; | ||
import { | ||
isNode, | ||
@@ -54,32 +57,28 @@ } from '../isNode'; | ||
export class UUID implements IUUID { | ||
constructor(options?: Partial<IUUIDOptions>) { | ||
const opts = new UUIDOptions(); | ||
constructor(opts?: Partial<IUUIDOptions>) { | ||
const options = new UUIDOptions(); | ||
if (typeof options === 'object' && options) { | ||
if (options.version) { | ||
opts.version = options.version; | ||
if (opts && typeof opts === 'object') { | ||
if (opts.version) { | ||
options.version = opts.version; | ||
} | ||
if (options.clockSequenceGetter) { | ||
opts.clockSequenceGetter = options.clockSequenceGetter; | ||
if (opts.clockSequenceGetter) { | ||
options.clockSequenceGetter = opts.clockSequenceGetter; | ||
} | ||
if (options.timestampGetter) { | ||
opts.timestampGetter = options.timestampGetter; | ||
if (opts.timestampGetter) { | ||
options.timestampGetter = opts.timestampGetter; | ||
} | ||
if (options.nodeIdentifierGetter) { | ||
opts.nodeIdentifierGetter = options.nodeIdentifierGetter; | ||
if (opts.nodeIdentifierGetter) { | ||
options.nodeIdentifierGetter = opts.nodeIdentifierGetter; | ||
} | ||
if (options.namespaceId) { | ||
opts.namespaceId = options.namespaceId; | ||
if (opts.namespaceId) { | ||
options.namespaceId = opts.namespaceId; | ||
} | ||
if (options.name) { | ||
opts.name = options.name; | ||
} | ||
} | ||
let version = opts.version; | ||
let version = options.version; | ||
if (!isUUIDVersion(version)) { | ||
@@ -102,8 +101,19 @@ throw new Error(strings.UUID_VERSION_INVALID); | ||
if (/^[35]$/.test(version.toString())) { | ||
if (!options.namespaceId) { | ||
throw new Error(strings.NAMESPACE_ID_MISSING); | ||
} else if (!options.name) { | ||
throw new Error(strings.NAME_MISSING); | ||
} | ||
const hash = getHashFromNamespaceIdAndName( | ||
version, | ||
options.namespaceId, | ||
options.name, | ||
); | ||
/* Clock sequence is highly dependent on other values and their | ||
* availability, so it should be generated first. */ | ||
const clockSequence = opts.clockSequenceGetter( | ||
const clockSequence = options.clockSequenceGetter( | ||
version, | ||
opts.namespaceId, | ||
opts.name | ||
hash, | ||
); | ||
@@ -113,6 +123,5 @@ | ||
const timestamp = opts.timestampGetter( | ||
const timestamp = options.timestampGetter( | ||
version, | ||
opts.namespaceId, | ||
opts.name, | ||
hash, | ||
); | ||
@@ -122,6 +131,5 @@ | ||
const nodeIdentifier = opts.nodeIdentifierGetter( | ||
const nodeIdentifier = options.nodeIdentifierGetter( | ||
version, | ||
opts.namespaceId, | ||
opts.name, | ||
hash, | ||
); | ||
@@ -131,9 +139,9 @@ | ||
} else { | ||
const clockSequence = opts.clockSequenceGetter(version); | ||
const clockSequence = options.clockSequenceGetter(version); | ||
this.__clockSequence = clockSequence; | ||
const timestamp = opts.timestampGetter(version); | ||
const timestamp = options.timestampGetter(version); | ||
this.__timestamp = timestamp; | ||
const nodeIdentifier = opts.nodeIdentifierGetter(version); | ||
const nodeIdentifier = options.nodeIdentifierGetter(version); | ||
this.__nodeIdentifier = nodeIdentifier; | ||
@@ -140,0 +148,0 @@ |
import { | ||
TNamespaceId, | ||
} from '../../TypeAliases/TNamespaceId'; | ||
import { | ||
TUUIDVersion, | ||
} from '../../TypeAliases/TUUIDVersion'; | ||
import { | ||
TNamespaceId, | ||
} from '../../TypeAliases/TNamespaceId'; | ||
export interface IUUIDOptions { | ||
version: TUUIDVersion; | ||
name?: string; | ||
namespaceId?: TNamespaceId; | ||
namespaceId?: TNamespaceId, | ||
name?: string, | ||
nodeIdentifierGetter: (version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string) => Uint8Array; | ||
@@ -13,0 +13,0 @@ timestampGetter: (version: TUUIDVersion, namespaceId?: TNamespaceId, name?: string) => Uint8Array; |
@@ -28,7 +28,7 @@ import { | ||
version: TUUIDVersion = '4'; | ||
namespaceId?: TNamespaceId; | ||
name?: string; | ||
clockSequenceGetter = clockSequenceGetter; | ||
nodeIdentifierGetter = nodeIdentifierGetter; | ||
timestampGetter = timestampGetter; | ||
name?: string; | ||
namespaceId?: TNamespaceId; | ||
@@ -45,19 +45,19 @@ constructor(_args: { [key: string]: any } = {}) { | ||
if (typeof args.clockSequenceGetter === 'function') { | ||
if (args.clockSequenceGetter) { | ||
this.clockSequenceGetter = args.clockSequenceGetter; | ||
} | ||
if (typeof args.nodeIdentifierGetter === 'function') { | ||
if (args.nodeIdentifierGetter) { | ||
this.nodeIdentifierGetter = args.nodeIdentifierGetter; | ||
} | ||
if (typeof args.timestampGetter === 'function') { | ||
if (args.timestampGetter) { | ||
this.timestampGetter = args.timestampGetter; | ||
} | ||
if (args.name && typeof args.name === 'string') { | ||
this.name = args.name; | ||
if (args.namespaceId) { | ||
this.namespaceId = args.namespaceId; | ||
} | ||
if (args.namespaceId && typeof args.namespaceId === 'string') { | ||
if (args.name) { | ||
this.namespaceId = args.namespaceId; | ||
@@ -64,0 +64,0 @@ } |
@@ -36,3 +36,3 @@ const path = require('path'); | ||
output: { | ||
path: path.resolve(__dirname, 'dist/umd/'), | ||
path: path.resolve(__dirname, 'dist/browser/'), | ||
filename: 'index.js', | ||
@@ -39,0 +39,0 @@ library: 'bigUUID', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
664602
154
3606
1
15
+ Addedcrypto-js@^3.1.9-1
+ Addedcrypto-js@3.3.0(transitive)