diff --git a/packages/random/README.md b/packages/random/README.md index 50efc5e09c..832b158a11 100644 --- a/packages/random/README.md +++ b/packages/random/README.md @@ -7,7 +7,7 @@ [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi) > [!NOTE] -> This is one of 194 standalone projects, maintained as part +> This is one of 197 standalone projects, maintained as part > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo > and anti-framework. > @@ -62,7 +62,6 @@ Partially ported from C implementations taken from [c.thi.ng](http://c.thi.ng). - [`randomID()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-id.ts) - [`weightedRandom()` / `weightedRandomKey()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/weighted-random.ts) - [`uniqueIndices()` / `uniqueValuesFrom()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/unique-indices.ts) -- [`uuidv4Bytes()` / `uuid()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/uuid.ts) ## Status @@ -104,14 +103,12 @@ For Node.js REPL: const rnd = await import("@thi.ng/random"); ``` -Package sizes (brotli'd, pre-treeshake): ESM: 2.02 KB +Package sizes (brotli'd, pre-treeshake): ESM: 1.92 KB ## Dependencies - [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api) -- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks) - [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors) -- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex) ## Usage examples diff --git a/packages/random/package.json b/packages/random/package.json index a781a41107..4cae61ebe8 100644 --- a/packages/random/package.json +++ b/packages/random/package.json @@ -37,9 +37,7 @@ }, "dependencies": { "@thi.ng/api": "^8.11.6", - "@thi.ng/checks": "^3.6.8", - "@thi.ng/errors": "^2.5.12", - "@thi.ng/hex": "^2.3.50" + "@thi.ng/errors": "^2.5.12" }, "devDependencies": { "@microsoft/api-extractor": "^7.47.0", @@ -62,7 +60,6 @@ "typedarray", "typescript", "weighted", - "uuid", "uniform" ], "publishConfig": { diff --git a/packages/random/src/crypto.ts b/packages/random/src/crypto.ts index 6984584938..ff5eed4382 100644 --- a/packages/random/src/crypto.ts +++ b/packages/random/src/crypto.ts @@ -4,8 +4,8 @@ import { randomBytes } from "./random-bytes.js"; /** * Currently browser only, a `window.crypto` backed {@link IRandom} - * implementation. Random values are buffered to minimize overhead. Buffer size - * is configurable via ctor. + * implementation. Random values are repeatedly buffered to minimize overhead. + * Buffer size is configurable via ctor. * * @remarks * Internally uses {@link randomBytes} to source values, which falls back to diff --git a/packages/random/src/index.ts b/packages/random/src/index.ts index b032b0c4fa..02794f4d68 100644 --- a/packages/random/src/index.ts +++ b/packages/random/src/index.ts @@ -14,7 +14,6 @@ export * from "./sfc32.js"; export * from "./smush32.js"; export * from "./system.js"; export * from "./unique-indices.js"; -export * from "./uuid.js"; export * from "./weighted-probability.js"; export * from "./weighted-random.js"; export * from "./xorshift128.js"; diff --git a/packages/random/src/random-bytes.ts b/packages/random/src/random-bytes.ts index 16d4d67bec..2ad337950e 100644 --- a/packages/random/src/random-bytes.ts +++ b/packages/random/src/random-bytes.ts @@ -1,4 +1,3 @@ -import { hasCrypto } from "@thi.ng/checks/has-crypto"; import type { IRandom } from "./api.js"; import { SYSTEM } from "./system.js"; @@ -32,9 +31,10 @@ export const randomBytesFrom = ( * @param start - * @param end - */ -export const randomBytes = hasCrypto() - ? (buf: Uint8Array, start = 0, end = buf.length) => ( - window.crypto.getRandomValues(buf.subarray(start, end)), buf - ) - : (buf: Uint8Array, start?: number, end?: number) => - randomBytesFrom(SYSTEM, buf, start, end); +export const randomBytes = + typeof window !== "undefined" && window["crypto"] !== undefined + ? (buf: Uint8Array, start = 0, end = buf.length) => ( + window.crypto.getRandomValues(buf.subarray(start, end)), buf + ) + : (buf: Uint8Array, start?: number, end?: number) => + randomBytesFrom(SYSTEM, buf, start, end); diff --git a/packages/random/src/uuid.ts b/packages/random/src/uuid.ts deleted file mode 100644 index 867aa56da5..0000000000 --- a/packages/random/src/uuid.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { uuid as $uuid } from "@thi.ng/hex"; -import type { IRandom } from "./api.js"; -import { randomBytes, randomBytesFrom } from "./random-bytes.js"; - -/** - * Depending on if `rnd` is given, uses {@link randomBytesFrom} or - * {@link randomBytes} to fill given (optional) byte array with a new UUIDv4. - * Creates new Uint8Array if none given. - * - * @param buf - - * @param rnd - - */ -export const uuidv4Bytes = (buf?: Uint8Array, rnd?: IRandom) => { - buf = buf || new Uint8Array(16); - buf = rnd ? randomBytesFrom(rnd, buf) : randomBytes(buf); - buf[6] = 0x40 | (buf[6] & 0x0f); - buf[8] = 0x80 | (buf[8] & 0x3f); - return buf; -}; - -/** @internal */ -const __buf = new Uint8Array(16); - -/** - * Returns a UUID string, either from given byte array, or if omitted, using a - * new UUID v4 produced by {@link uuidv4Bytes}. - * - * @param id - byte array - * @param i - start index - */ -export const uuid = (id?: ArrayLike, i = 0) => - $uuid(id || uuidv4Bytes(__buf), i); diff --git a/packages/random/test/uuid.test.ts b/packages/random/test/uuid.test.ts deleted file mode 100644 index 4f8472fe13..0000000000 --- a/packages/random/test/uuid.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { expect, test } from "bun:test"; -import { Xoshiro128, uuid, uuidv4Bytes } from "../src/index.js"; - -test("from seeded rnd", () => { - const rnd = new Xoshiro128(); - let buf = uuidv4Bytes(undefined, rnd); - expect(buf).toEqual( - new Uint8Array([ - 157, 255, 141, 148, 201, 143, 77, 74, 163, 71, 204, 243, 50, 107, - 98, 44, - ]) - ); - expect(uuid(buf)).toBe("9dff8d94-c98f-4d4a-a347-ccf3326b622c"); - buf = uuidv4Bytes(undefined, rnd); - expect(buf).toEqual( - new Uint8Array([ - 147, 9, 138, 29, 127, 233, 67, 121, 173, 249, 45, 60, 153, 190, 193, - 197, - ]) - ); - expect(uuid(buf)).toBe("93098a1d-7fe9-4379-adf9-2d3c99bec1c5"); -}); diff --git a/packages/random/tpl.readme.md b/packages/random/tpl.readme.md index 4f0654d2a4..c1c338e0f8 100644 --- a/packages/random/tpl.readme.md +++ b/packages/random/tpl.readme.md @@ -37,7 +37,6 @@ Partially ported from C implementations taken from [c.thi.ng](http://c.thi.ng). - [`randomID()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-id.ts) - [`weightedRandom()` / `weightedRandomKey()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/weighted-random.ts) - [`uniqueIndices()` / `uniqueValuesFrom()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/unique-indices.ts) -- [`uuidv4Bytes()` / `uuid()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/uuid.ts) {{meta.status}}