init
This commit is contained in:
21
node_modules/ulid/LICENSE
generated
vendored
Normal file
21
node_modules/ulid/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Alizain Feerasta
|
||||
|
||||
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.
|
||||
183
node_modules/ulid/README.md
generated
vendored
Normal file
183
node_modules/ulid/README.md
generated
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
<h1 align="center">
|
||||
<br>
|
||||
<br>
|
||||
<img width="360" src="logo.png" alt="ulid">
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
</h1>
|
||||
|
||||
[](https://github.com/ulid/javascript/actions/workflows/test.yml)
|
||||
[](https://codecov.io/gh/ulid/javascript)
|
||||
[](https://www.npmjs.com/package/ulid)
|
||||
|
||||
# Universally Unique Lexicographically Sortable Identifier
|
||||
|
||||
UUID can be suboptimal for many uses-cases because:
|
||||
|
||||
- It isn't the most character efficient way of encoding 128 bits of randomness
|
||||
- UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
|
||||
- UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
|
||||
- UUID v4 provides no other information than randomness which can cause fragmentation in many data structures
|
||||
|
||||
Instead, herein is proposed ULID:
|
||||
|
||||
- 128-bit compatibility with UUID
|
||||
- 1.21e+24 unique ULIDs per millisecond
|
||||
- Lexicographically sortable!
|
||||
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
|
||||
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
|
||||
- Case insensitive
|
||||
- No special characters (URL safe)
|
||||
- Monotonic sort order (correctly detects and handles the same millisecond)
|
||||
|
||||
## Install with a script tag
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/ulid@{{VERSION_NUMBER}}/dist/index.umd.js"></script>
|
||||
<script>
|
||||
ULID.ulid()
|
||||
</script>
|
||||
```
|
||||
|
||||
## Install with NPM
|
||||
|
||||
```
|
||||
npm install --save ulid
|
||||
```
|
||||
|
||||
### Import
|
||||
|
||||
**TypeScript, ES6+, Babel, Webpack, Rollup, etc.. environments**
|
||||
```javascript
|
||||
import { ulid } from 'ulid'
|
||||
|
||||
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
|
||||
```
|
||||
|
||||
**CommonJS environments**
|
||||
```javascript
|
||||
const ULID = require('ulid')
|
||||
|
||||
ULID.ulid()
|
||||
```
|
||||
|
||||
**AMD (RequireJS) environments**
|
||||
```javascript
|
||||
define(['ULID'] , function (ULID) {
|
||||
ULID.ulid()
|
||||
});
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
To generate a ULID, simply run the function!
|
||||
|
||||
```javascript
|
||||
import { ulid } from 'ulid'
|
||||
|
||||
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
|
||||
```
|
||||
|
||||
### Seed Time
|
||||
|
||||
You can also input a seed time which will consistently give you the same string for the time component. This is useful for migrating to ulid.
|
||||
|
||||
```javascript
|
||||
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV
|
||||
```
|
||||
|
||||
### Monotonic ULIDs
|
||||
|
||||
To generate monotonically increasing ULIDs, create a monotonic counter.
|
||||
|
||||
*Note that the same seed time is being passed in for this example to demonstrate its behaviour when generating multiple ULIDs within the same millisecond*
|
||||
|
||||
```javascript
|
||||
import { monotonicFactory } from 'ulid'
|
||||
|
||||
const ulid = monotonicFactory()
|
||||
|
||||
// Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR8
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR9
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRA
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRB
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRC
|
||||
|
||||
// Even if a lower timestamp is passed (or generated), it will preserve sort order
|
||||
ulid(100000) // 000XAL6S41ACTAV9WEVGEMMVRD
|
||||
```
|
||||
|
||||
### Pseudo-Random Number Generators
|
||||
|
||||
`ulid` automatically detects a suitable (cryptographically-secure) PRNG. In the browser it will use `crypto.getRandomValues` and on node it will use `crypto.randomBytes`.
|
||||
|
||||
#### Allowing the insecure `Math.random`
|
||||
|
||||
By default, `ulid` will not use `Math.random`, because that is insecure. To allow the use of `Math.random`, you'll have to use `factory` and `detectPrng`.
|
||||
|
||||
```javascript
|
||||
import { factory, detectPrng } from 'ulid'
|
||||
|
||||
const prng = detectPrng(true) // pass `true` to allow insecure
|
||||
const ulid = factory(prng)
|
||||
|
||||
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
|
||||
```
|
||||
|
||||
#### Use your own PRNG
|
||||
|
||||
To use your own pseudo-random number generator, import the factory, and pass it your generator function.
|
||||
|
||||
```javascript
|
||||
import { factory } from 'ulid'
|
||||
import prng from 'somewhere'
|
||||
|
||||
const ulid = factory(prng)
|
||||
|
||||
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
|
||||
```
|
||||
|
||||
You can also pass in a `prng` to the `monotonicFactory` function.
|
||||
|
||||
```javascript
|
||||
import { monotonicFactory } from 'ulid'
|
||||
import prng from 'somewhere'
|
||||
|
||||
const ulid = monotonicFactory(prng)
|
||||
|
||||
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
|
||||
```
|
||||
|
||||
## Implementations in other languages
|
||||
|
||||
Refer to [ulid/spec](https://github.com/ulid/spec)
|
||||
|
||||
## Specification
|
||||
|
||||
Refer to [ulid/spec](https://github.com/ulid/spec)
|
||||
|
||||
## Test Suite
|
||||
|
||||
```
|
||||
npm test
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
```
|
||||
npm run perf
|
||||
```
|
||||
|
||||
```
|
||||
ulid
|
||||
336,331,131 op/s » encodeTime
|
||||
102,041,736 op/s » encodeRandom
|
||||
17,408 op/s » generate
|
||||
|
||||
|
||||
Suites: 1
|
||||
Benches: 3
|
||||
Elapsed: 7,285.75 ms
|
||||
```
|
||||
4
node_modules/ulid/bin/cli.js
generated
vendored
Executable file
4
node_modules/ulid/bin/cli.js
generated
vendored
Executable file
@@ -0,0 +1,4 @@
|
||||
#! /usr/bin/env node
|
||||
|
||||
var ULID = require('../dist/index.umd.js')
|
||||
process.stdout.write(ULID.ulid() + '\n')
|
||||
19
node_modules/ulid/dist/index.d.ts
generated
vendored
Normal file
19
node_modules/ulid/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
export interface PRNG {
|
||||
(): number;
|
||||
}
|
||||
export interface ULID {
|
||||
(seedTime?: number): string;
|
||||
}
|
||||
export interface LibError extends Error {
|
||||
source: string;
|
||||
}
|
||||
export declare function replaceCharAt(str: string, index: number, char: string): string;
|
||||
export declare function incrementBase32(str: string): string;
|
||||
export declare function randomChar(prng: PRNG): string;
|
||||
export declare function encodeTime(now: number, len: number): string;
|
||||
export declare function encodeRandom(len: number, prng: PRNG): string;
|
||||
export declare function decodeTime(id: string): number;
|
||||
export declare function detectPrng(allowInsecure?: boolean, root?: any): PRNG;
|
||||
export declare function factory(currPrng?: PRNG): ULID;
|
||||
export declare function monotonicFactory(currPrng?: PRNG): ULID;
|
||||
export declare const ulid: ULID;
|
||||
158
node_modules/ulid/dist/index.esm.js
generated
vendored
Normal file
158
node_modules/ulid/dist/index.esm.js
generated
vendored
Normal file
@@ -0,0 +1,158 @@
|
||||
function createError(message) {
|
||||
const err = new Error(message);
|
||||
err.source = "ulid";
|
||||
return err;
|
||||
}
|
||||
// These values should NEVER change. If
|
||||
// they do, we're no longer making ulids!
|
||||
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
||||
const ENCODING_LEN = ENCODING.length;
|
||||
const TIME_MAX = Math.pow(2, 48) - 1;
|
||||
const TIME_LEN = 10;
|
||||
const RANDOM_LEN = 16;
|
||||
function replaceCharAt(str, index, char) {
|
||||
if (index > str.length - 1) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(0, index) + char + str.substr(index + 1);
|
||||
}
|
||||
function incrementBase32(str) {
|
||||
let done = undefined;
|
||||
let index = str.length;
|
||||
let char;
|
||||
let charIndex;
|
||||
const maxCharIndex = ENCODING_LEN - 1;
|
||||
while (!done && index-- >= 0) {
|
||||
char = str[index];
|
||||
charIndex = ENCODING.indexOf(char);
|
||||
if (charIndex === -1) {
|
||||
throw createError("incorrectly encoded string");
|
||||
}
|
||||
if (charIndex === maxCharIndex) {
|
||||
str = replaceCharAt(str, index, ENCODING[0]);
|
||||
continue;
|
||||
}
|
||||
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
||||
}
|
||||
if (typeof done === "string") {
|
||||
return done;
|
||||
}
|
||||
throw createError("cannot increment this string");
|
||||
}
|
||||
function randomChar(prng) {
|
||||
let rand = Math.floor(prng() * ENCODING_LEN);
|
||||
if (rand === ENCODING_LEN) {
|
||||
rand = ENCODING_LEN - 1;
|
||||
}
|
||||
return ENCODING.charAt(rand);
|
||||
}
|
||||
function encodeTime(now, len) {
|
||||
if (isNaN(now)) {
|
||||
throw new Error(now + " must be a number");
|
||||
}
|
||||
if (now > TIME_MAX) {
|
||||
throw createError("cannot encode time greater than " + TIME_MAX);
|
||||
}
|
||||
if (now < 0) {
|
||||
throw createError("time must be positive");
|
||||
}
|
||||
if (Number.isInteger(Number(now)) === false) {
|
||||
throw createError("time must be an integer");
|
||||
}
|
||||
let mod;
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
mod = now % ENCODING_LEN;
|
||||
str = ENCODING.charAt(mod) + str;
|
||||
now = (now - mod) / ENCODING_LEN;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function encodeRandom(len, prng) {
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
str = randomChar(prng) + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function decodeTime(id) {
|
||||
if (id.length !== TIME_LEN + RANDOM_LEN) {
|
||||
throw createError("malformed ulid");
|
||||
}
|
||||
var time = id
|
||||
.substr(0, TIME_LEN)
|
||||
.split("")
|
||||
.reverse()
|
||||
.reduce((carry, char, index) => {
|
||||
const encodingIndex = ENCODING.indexOf(char);
|
||||
if (encodingIndex === -1) {
|
||||
throw createError("invalid character found: " + char);
|
||||
}
|
||||
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
|
||||
}, 0);
|
||||
if (time > TIME_MAX) {
|
||||
throw createError("malformed ulid, timestamp too large");
|
||||
}
|
||||
return time;
|
||||
}
|
||||
function detectPrng(allowInsecure = false, root) {
|
||||
if (!root) {
|
||||
root = typeof window !== "undefined" ? window : null;
|
||||
}
|
||||
const browserCrypto = root && (root.crypto || root.msCrypto);
|
||||
if (browserCrypto) {
|
||||
return () => {
|
||||
const buffer = new Uint8Array(1);
|
||||
browserCrypto.getRandomValues(buffer);
|
||||
return buffer[0] / 0xff;
|
||||
};
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const nodeCrypto = require("crypto");
|
||||
return () => nodeCrypto.randomBytes(1).readUInt8() / 0xff;
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
if (allowInsecure) {
|
||||
try {
|
||||
console.error("secure crypto unusable, falling back to insecure Math.random()!");
|
||||
}
|
||||
catch (e) { }
|
||||
return () => Math.random();
|
||||
}
|
||||
throw createError("secure crypto unusable, insecure Math.random not allowed");
|
||||
}
|
||||
function factory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
|
||||
};
|
||||
}
|
||||
function monotonicFactory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
let lastTime = 0;
|
||||
let lastRandom;
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
if (seedTime <= lastTime) {
|
||||
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
|
||||
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
|
||||
}
|
||||
lastTime = seedTime;
|
||||
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
|
||||
return encodeTime(seedTime, TIME_LEN) + newRandom;
|
||||
};
|
||||
}
|
||||
const ulid = factory();
|
||||
|
||||
export { decodeTime, detectPrng, encodeRandom, encodeTime, factory, incrementBase32, monotonicFactory, randomChar, replaceCharAt, ulid };
|
||||
156
node_modules/ulid/dist/index.js
generated
vendored
Normal file
156
node_modules/ulid/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
function createError(message) {
|
||||
const err = new Error(message);
|
||||
err.source = "ulid";
|
||||
return err;
|
||||
}
|
||||
// These values should NEVER change. If
|
||||
// they do, we're no longer making ulids!
|
||||
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
||||
const ENCODING_LEN = ENCODING.length;
|
||||
const TIME_MAX = Math.pow(2, 48) - 1;
|
||||
const TIME_LEN = 10;
|
||||
const RANDOM_LEN = 16;
|
||||
export function replaceCharAt(str, index, char) {
|
||||
if (index > str.length - 1) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(0, index) + char + str.substr(index + 1);
|
||||
}
|
||||
export function incrementBase32(str) {
|
||||
let done = undefined;
|
||||
let index = str.length;
|
||||
let char;
|
||||
let charIndex;
|
||||
const maxCharIndex = ENCODING_LEN - 1;
|
||||
while (!done && index-- >= 0) {
|
||||
char = str[index];
|
||||
charIndex = ENCODING.indexOf(char);
|
||||
if (charIndex === -1) {
|
||||
throw createError("incorrectly encoded string");
|
||||
}
|
||||
if (charIndex === maxCharIndex) {
|
||||
str = replaceCharAt(str, index, ENCODING[0]);
|
||||
continue;
|
||||
}
|
||||
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
||||
}
|
||||
if (typeof done === "string") {
|
||||
return done;
|
||||
}
|
||||
throw createError("cannot increment this string");
|
||||
}
|
||||
export function randomChar(prng) {
|
||||
let rand = Math.floor(prng() * ENCODING_LEN);
|
||||
if (rand === ENCODING_LEN) {
|
||||
rand = ENCODING_LEN - 1;
|
||||
}
|
||||
return ENCODING.charAt(rand);
|
||||
}
|
||||
export function encodeTime(now, len) {
|
||||
if (isNaN(now)) {
|
||||
throw new Error(now + " must be a number");
|
||||
}
|
||||
if (now > TIME_MAX) {
|
||||
throw createError("cannot encode time greater than " + TIME_MAX);
|
||||
}
|
||||
if (now < 0) {
|
||||
throw createError("time must be positive");
|
||||
}
|
||||
if (Number.isInteger(Number(now)) === false) {
|
||||
throw createError("time must be an integer");
|
||||
}
|
||||
let mod;
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
mod = now % ENCODING_LEN;
|
||||
str = ENCODING.charAt(mod) + str;
|
||||
now = (now - mod) / ENCODING_LEN;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
export function encodeRandom(len, prng) {
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
str = randomChar(prng) + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
export function decodeTime(id) {
|
||||
if (id.length !== TIME_LEN + RANDOM_LEN) {
|
||||
throw createError("malformed ulid");
|
||||
}
|
||||
var time = id
|
||||
.substr(0, TIME_LEN)
|
||||
.split("")
|
||||
.reverse()
|
||||
.reduce((carry, char, index) => {
|
||||
const encodingIndex = ENCODING.indexOf(char);
|
||||
if (encodingIndex === -1) {
|
||||
throw createError("invalid character found: " + char);
|
||||
}
|
||||
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
|
||||
}, 0);
|
||||
if (time > TIME_MAX) {
|
||||
throw createError("malformed ulid, timestamp too large");
|
||||
}
|
||||
return time;
|
||||
}
|
||||
export function detectPrng(allowInsecure = false, root) {
|
||||
if (!root) {
|
||||
root = typeof window !== "undefined" ? window : null;
|
||||
}
|
||||
const browserCrypto = root && (root.crypto || root.msCrypto);
|
||||
if (browserCrypto) {
|
||||
return () => {
|
||||
const buffer = new Uint8Array(1);
|
||||
browserCrypto.getRandomValues(buffer);
|
||||
return buffer[0] / 0xff;
|
||||
};
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const nodeCrypto = require("crypto");
|
||||
return () => nodeCrypto.randomBytes(1).readUInt8() / 0xff;
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
if (allowInsecure) {
|
||||
try {
|
||||
console.error("secure crypto unusable, falling back to insecure Math.random()!");
|
||||
}
|
||||
catch (e) { }
|
||||
return () => Math.random();
|
||||
}
|
||||
throw createError("secure crypto unusable, insecure Math.random not allowed");
|
||||
}
|
||||
export function factory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
|
||||
};
|
||||
}
|
||||
export function monotonicFactory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
let lastTime = 0;
|
||||
let lastRandom;
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
if (seedTime <= lastTime) {
|
||||
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
|
||||
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
|
||||
}
|
||||
lastTime = seedTime;
|
||||
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
|
||||
return encodeTime(seedTime, TIME_LEN) + newRandom;
|
||||
};
|
||||
}
|
||||
export const ulid = factory();
|
||||
175
node_modules/ulid/dist/index.umd.js
generated
vendored
Normal file
175
node_modules/ulid/dist/index.umd.js
generated
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ULID = {}));
|
||||
})(this, (function (exports) { 'use strict';
|
||||
|
||||
function createError(message) {
|
||||
const err = new Error(message);
|
||||
err.source = "ulid";
|
||||
return err;
|
||||
}
|
||||
// These values should NEVER change. If
|
||||
// they do, we're no longer making ulids!
|
||||
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
||||
const ENCODING_LEN = ENCODING.length;
|
||||
const TIME_MAX = Math.pow(2, 48) - 1;
|
||||
const TIME_LEN = 10;
|
||||
const RANDOM_LEN = 16;
|
||||
function replaceCharAt(str, index, char) {
|
||||
if (index > str.length - 1) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(0, index) + char + str.substr(index + 1);
|
||||
}
|
||||
function incrementBase32(str) {
|
||||
let done = undefined;
|
||||
let index = str.length;
|
||||
let char;
|
||||
let charIndex;
|
||||
const maxCharIndex = ENCODING_LEN - 1;
|
||||
while (!done && index-- >= 0) {
|
||||
char = str[index];
|
||||
charIndex = ENCODING.indexOf(char);
|
||||
if (charIndex === -1) {
|
||||
throw createError("incorrectly encoded string");
|
||||
}
|
||||
if (charIndex === maxCharIndex) {
|
||||
str = replaceCharAt(str, index, ENCODING[0]);
|
||||
continue;
|
||||
}
|
||||
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
||||
}
|
||||
if (typeof done === "string") {
|
||||
return done;
|
||||
}
|
||||
throw createError("cannot increment this string");
|
||||
}
|
||||
function randomChar(prng) {
|
||||
let rand = Math.floor(prng() * ENCODING_LEN);
|
||||
if (rand === ENCODING_LEN) {
|
||||
rand = ENCODING_LEN - 1;
|
||||
}
|
||||
return ENCODING.charAt(rand);
|
||||
}
|
||||
function encodeTime(now, len) {
|
||||
if (isNaN(now)) {
|
||||
throw new Error(now + " must be a number");
|
||||
}
|
||||
if (now > TIME_MAX) {
|
||||
throw createError("cannot encode time greater than " + TIME_MAX);
|
||||
}
|
||||
if (now < 0) {
|
||||
throw createError("time must be positive");
|
||||
}
|
||||
if (Number.isInteger(Number(now)) === false) {
|
||||
throw createError("time must be an integer");
|
||||
}
|
||||
let mod;
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
mod = now % ENCODING_LEN;
|
||||
str = ENCODING.charAt(mod) + str;
|
||||
now = (now - mod) / ENCODING_LEN;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function encodeRandom(len, prng) {
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
str = randomChar(prng) + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function decodeTime(id) {
|
||||
if (id.length !== TIME_LEN + RANDOM_LEN) {
|
||||
throw createError("malformed ulid");
|
||||
}
|
||||
var time = id
|
||||
.substr(0, TIME_LEN)
|
||||
.split("")
|
||||
.reverse()
|
||||
.reduce((carry, char, index) => {
|
||||
const encodingIndex = ENCODING.indexOf(char);
|
||||
if (encodingIndex === -1) {
|
||||
throw createError("invalid character found: " + char);
|
||||
}
|
||||
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
|
||||
}, 0);
|
||||
if (time > TIME_MAX) {
|
||||
throw createError("malformed ulid, timestamp too large");
|
||||
}
|
||||
return time;
|
||||
}
|
||||
function detectPrng(allowInsecure = false, root) {
|
||||
if (!root) {
|
||||
root = typeof window !== "undefined" ? window : null;
|
||||
}
|
||||
const browserCrypto = root && (root.crypto || root.msCrypto);
|
||||
if (browserCrypto) {
|
||||
return () => {
|
||||
const buffer = new Uint8Array(1);
|
||||
browserCrypto.getRandomValues(buffer);
|
||||
return buffer[0] / 0xff;
|
||||
};
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const nodeCrypto = require("crypto");
|
||||
return () => nodeCrypto.randomBytes(1).readUInt8() / 0xff;
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
if (allowInsecure) {
|
||||
try {
|
||||
console.error("secure crypto unusable, falling back to insecure Math.random()!");
|
||||
}
|
||||
catch (e) { }
|
||||
return () => Math.random();
|
||||
}
|
||||
throw createError("secure crypto unusable, insecure Math.random not allowed");
|
||||
}
|
||||
function factory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
|
||||
};
|
||||
}
|
||||
function monotonicFactory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
let lastTime = 0;
|
||||
let lastRandom;
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
if (seedTime <= lastTime) {
|
||||
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
|
||||
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
|
||||
}
|
||||
lastTime = seedTime;
|
||||
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
|
||||
return encodeTime(seedTime, TIME_LEN) + newRandom;
|
||||
};
|
||||
}
|
||||
const ulid = factory();
|
||||
|
||||
exports.decodeTime = decodeTime;
|
||||
exports.detectPrng = detectPrng;
|
||||
exports.encodeRandom = encodeRandom;
|
||||
exports.encodeTime = encodeTime;
|
||||
exports.factory = factory;
|
||||
exports.incrementBase32 = incrementBase32;
|
||||
exports.monotonicFactory = monotonicFactory;
|
||||
exports.randomChar = randomChar;
|
||||
exports.replaceCharAt = replaceCharAt;
|
||||
exports.ulid = ulid;
|
||||
|
||||
}));
|
||||
51
node_modules/ulid/package.json
generated
vendored
Normal file
51
node_modules/ulid/package.json
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "ulid",
|
||||
"version": "2.4.0",
|
||||
"description": "A universally-unique, lexicographically-sortable, identifier generator",
|
||||
"main": "./dist/index.umd.js",
|
||||
"module": "./dist/index.esm.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"esnext": "./dist/index.js",
|
||||
"bin": "./bin/cli.js",
|
||||
"scripts": {
|
||||
"build": "npm run clean && npm run ts && npm run rollup",
|
||||
"clean": "rm -rf ./dist",
|
||||
"perf": "./node_modules/.bin/matcha perf.js",
|
||||
"rollup": "./node_modules/.bin/rollup -c",
|
||||
"test": "npm run test:coverage",
|
||||
"test:specs": "mocha -- -R spec",
|
||||
"test:coverage": "c8 --check-coverage --lines 87 --functions 100 --src ./lib npm run test:specs",
|
||||
"ts": "./node_modules/.bin/tsc -p ."
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
"dist",
|
||||
"stubs"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/ulid/javascript.git"
|
||||
},
|
||||
"browser": {
|
||||
"crypto": "./stubs/crypto.js"
|
||||
},
|
||||
"author": "Alizain Feerasta",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/ulid/javascript/issues"
|
||||
},
|
||||
"homepage": "https://github.com/ulid/javascript#readme",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-babel": "^6.0.4",
|
||||
"@rollup/plugin-typescript": "^11.1.6",
|
||||
"@types/node": "^22.13.10",
|
||||
"c8": "^10.1.3",
|
||||
"lolex": "^2.7.5",
|
||||
"matcha": "^0.7.0",
|
||||
"mocha": "^11.1.0",
|
||||
"rollup": "^4.35.0",
|
||||
"tslib": "^2.8.1",
|
||||
"typedoc": "^0.9.0",
|
||||
"typescript": "^5.8.2"
|
||||
}
|
||||
}
|
||||
0
node_modules/ulid/stubs/crypto.js
generated
vendored
Normal file
0
node_modules/ulid/stubs/crypto.js
generated
vendored
Normal file
Reference in New Issue
Block a user