Skip to content

Commit

Permalink
Merge pull request #109 from ulid/maint/refresh
Browse files Browse the repository at this point in the history
maint(dependencies): Cleanup, update dependencies
  • Loading branch information
perry-mitchell authored Feb 3, 2025
2 parents c5c4231 + d2d6af3 commit 4c6a1dc
Show file tree
Hide file tree
Showing 8 changed files with 4,681 additions and 1,277 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Tests

on:
push:
pull_request:

jobs:
nodejs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 22.x]
steps:
- uses: actions/checkout@v2
- name: Node.js specs ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
- run: npm run test
types:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [22.x]
steps:
- uses: actions/checkout@v2
- name: Type checks ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
- run: npx --yes @arethetypeswrong/cli@latest --pack .
82 changes: 41 additions & 41 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
function createError(message) {
var err = new Error(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!
var ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
var ENCODING_LEN = ENCODING.length;
var TIME_MAX = Math.pow(2, 48) - 1;
var TIME_LEN = 10;
var RANDOM_LEN = 16;
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) {
var done = undefined;
var index = str.length;
var char = void 0;
var charIndex = void 0;
var maxCharIndex = ENCODING_LEN - 1;
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);
Expand All @@ -40,7 +40,7 @@ function incrementBase32(str) {
throw createError("cannot increment this string");
}
function randomChar(prng) {
var rand = Math.floor(prng() * ENCODING_LEN);
let rand = Math.floor(prng() * ENCODING_LEN);
if (rand === ENCODING_LEN) {
rand = ENCODING_LEN - 1;
}
Expand All @@ -59,8 +59,8 @@ function encodeTime(now, len) {
if (Number.isInteger(now) === false) {
throw createError("time must be an integer");
}
var mod = void 0;
var str = "";
let mod;
let str = "";
for (; len > 0; len--) {
mod = now % ENCODING_LEN;
str = ENCODING.charAt(mod) + str;
Expand All @@ -69,7 +69,7 @@ function encodeTime(now, len) {
return str;
}
function encodeRandom(len, prng) {
var str = "";
let str = "";
for (; len > 0; len--) {
str = randomChar(prng) + str;
}
Expand All @@ -79,47 +79,47 @@ function decodeTime(id) {
if (id.length !== TIME_LEN + RANDOM_LEN) {
throw createError("malformed ulid");
}
var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) {
var encodingIndex = ENCODING.indexOf(char);
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);
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
}, 0);
if (time > TIME_MAX) {
throw createError("malformed ulid, timestamp too large");
}
return time;
}
function detectPrng() {
var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var root = arguments[1];

function detectPrng(allowInsecure = false, root) {
if (!root) {
root = typeof window !== "undefined" ? window : null;
}
var browserCrypto = root && (root.crypto || root.msCrypto);
const browserCrypto = root && (root.crypto || root.msCrypto);
if (browserCrypto) {
return function () {
var buffer = new Uint8Array(1);
return () => {
const buffer = new Uint8Array(1);
browserCrypto.getRandomValues(buffer);
return buffer[0] / 0xff;
};
} else {
}
else {
try {
var nodeCrypto = require("crypto");
return function () {
return nodeCrypto.randomBytes(1).readUInt8() / 0xff;
};
} catch (e) {}
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 function () {
return Math.random();
};
}
catch (e) { }
return () => Math.random();
}
throw createError("secure crypto unusable, insecure Math.random not allowed");
}
Expand All @@ -138,21 +138,21 @@ function monotonicFactory(currPrng) {
if (!currPrng) {
currPrng = detectPrng();
}
var lastTime = 0;
var lastRandom = void 0;
let lastTime = 0;
let lastRandom;
return function ulid(seedTime) {
if (isNaN(seedTime)) {
seedTime = Date.now();
}
if (seedTime <= lastTime) {
var incrementedRandom = lastRandom = incrementBase32(lastRandom);
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
}
lastTime = seedTime;
var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng);
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
return encodeTime(seedTime, TIME_LEN) + newRandom;
};
}
var ulid = factory();
const ulid = factory();

export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid };
export { decodeTime, detectPrng, encodeRandom, encodeTime, factory, incrementBase32, monotonicFactory, randomChar, replaceCharAt, ulid };
Loading

0 comments on commit 4c6a1dc

Please sign in to comment.