From 49e001ad8253a3dc088bcaa51fba04883e4c8e1e Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Thu, 9 May 2024 16:16:07 -0500 Subject: [PATCH] jsr --- nats-base-client/deno.json | 13 ++++ nats-base-client/nuid.ts | 116 +------------------------------- nuid/jsr.json | 7 ++ nuid/src/mod.ts | 134 +++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 115 deletions(-) create mode 100644 nats-base-client/deno.json create mode 100644 nuid/jsr.json create mode 100644 nuid/src/mod.ts diff --git a/nats-base-client/deno.json b/nats-base-client/deno.json new file mode 100644 index 00000000..79920d36 --- /dev/null +++ b/nats-base-client/deno.json @@ -0,0 +1,13 @@ +{ + "name": "@nats-io/nats-core", + "version": "3.0.0-1", + "exports": { + ".": "./src/mod.ts" + }, + "publish": { + "include": [ + "./src/**/*", + "jsr.json" + ] + } +} diff --git a/nats-base-client/nuid.ts b/nats-base-client/nuid.ts index ae859240..05e760f2 100644 --- a/nats-base-client/nuid.ts +++ b/nats-base-client/nuid.ts @@ -13,119 +13,5 @@ * limitations under the License. */ -"use strict"; +export {Nuid, nuid} from "../nuid/src/mod.ts" -const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -const base = 36; -const preLen = 12; -const seqLen = 10; -const maxSeq = 3656158440062976; // base^seqLen == 36^10 -const minInc = 33; -const maxInc = 333; -const totalLen = preLen + seqLen; - -function _getRandomValues(a: Uint8Array) { - for (let i = 0; i < a.length; i++) { - a[i] = Math.floor(Math.random() * 255); - } -} - -function fillRandom(a: Uint8Array) { - if (globalThis?.crypto?.getRandomValues) { - globalThis.crypto.getRandomValues(a); - } else { - _getRandomValues(a); - } -} - -/** - * Create and initialize a nuid. - * - * @api private - */ -export class Nuid { - buf: Uint8Array; - seq!: number; - inc!: number; - inited: boolean; - - constructor() { - this.buf = new Uint8Array(totalLen); - this.inited = false; - } - - /** - * Initializes a nuid with a crypto random prefix, - * and pseudo-random sequence and increment. - * - * @api private - */ - private init() { - this.inited = true; - this.setPre(); - this.initSeqAndInc(); - this.fillSeq(); - } - - /** - * Initializes the pseudo randmon sequence number and the increment range. - * - * @api private - */ - private initSeqAndInc() { - this.seq = Math.floor(Math.random() * maxSeq); - this.inc = Math.floor(Math.random() * (maxInc - minInc) + minInc); - } - - /** - * Sets the prefix from crypto random bytes. Converts to base36. - * - * @api private - */ - private setPre() { - const cbuf = new Uint8Array(preLen); - fillRandom(cbuf); - for (let i = 0; i < preLen; i++) { - const di = cbuf[i] % base; - this.buf[i] = digits.charCodeAt(di); - } - } - - /** - * Fills the sequence part of the nuid as base36 from this.seq. - * - * @api private - */ - private fillSeq() { - let n = this.seq; - for (let i = totalLen - 1; i >= preLen; i--) { - this.buf[i] = digits.charCodeAt(n % base); - n = Math.floor(n / base); - } - } - - /** - * Returns the next nuid. - * - * @api private - */ - next(): string { - if (!this.inited) { - this.init(); - } - this.seq += this.inc; - if (this.seq > maxSeq) { - this.setPre(); - this.initSeqAndInc(); - } - this.fillSeq(); - // @ts-ignore - Uint8Arrays can be an argument - return String.fromCharCode.apply(String, this.buf); - } - - reset() { - this.init(); - } -} - -export const nuid = new Nuid(); diff --git a/nuid/jsr.json b/nuid/jsr.json new file mode 100644 index 00000000..19ec9aa1 --- /dev/null +++ b/nuid/jsr.json @@ -0,0 +1,7 @@ +{ + "name": "@nats-io/nuid", + "version": "2.0.0-1", + "exports": { + ".": "./src/mod.ts" + } +} \ No newline at end of file diff --git a/nuid/src/mod.ts b/nuid/src/mod.ts new file mode 100644 index 00000000..efec75b4 --- /dev/null +++ b/nuid/src/mod.ts @@ -0,0 +1,134 @@ +/* + * Copyright 2016-2024 The NATS Authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +"use strict"; + +const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +const base = 36; +const preLen = 12; +const seqLen = 10; +const maxSeq = 3656158440062976; // base^seqLen == 36^10 +const minInc = 33; +const maxInc = 333; +const totalLen = preLen + seqLen; + +function _getRandomValues(a: Uint8Array) { + for (let i = 0; i < a.length; i++) { + a[i] = Math.floor(Math.random() * 255); + } +} + +function fillRandom(a: Uint8Array) { + if (globalThis?.crypto?.getRandomValues) { + globalThis.crypto.getRandomValues(a); + } else { + _getRandomValues(a); + } +} + +/** + * Create and initialize a nuid. + * + * @api private + */ +export class Nuid { + buf: Uint8Array; + seq!: number; + inc!: number; + inited: boolean; + + constructor() { + this.buf = new Uint8Array(totalLen); + this.inited = false; + } + + /** + * Initializes a nuid with a crypto random prefix, + * and pseudo-random sequence and increment. + * + * @api private + */ + private init() { + this.inited = true; + this.setPre(); + this.initSeqAndInc(); + this.fillSeq(); + } + + /** + * Initializes the pseudo randmon sequence number and the increment range. + * + * @api private + */ + private initSeqAndInc() { + this.seq = Math.floor(Math.random() * maxSeq); + this.inc = Math.floor(Math.random() * (maxInc - minInc) + minInc); + } + + /** + * Sets the prefix from crypto random bytes. Converts to base36. + * + * @api private + */ + private setPre() { + const cbuf = new Uint8Array(preLen); + fillRandom(cbuf); + for (let i = 0; i < preLen; i++) { + const di = cbuf[i] % base; + this.buf[i] = digits.charCodeAt(di); + } + } + + /** + * Fills the sequence part of the nuid as base36 from this.seq. + * + * @api private + */ + private fillSeq() { + let n = this.seq; + for (let i = totalLen - 1; i >= preLen; i--) { + this.buf[i] = digits.charCodeAt(n % base); + n = Math.floor(n / base); + } + } + + /** + * Returns the next nuid. + * + * @api private + */ + next(): string { + if (!this.inited) { + this.init(); + } + this.seq += this.inc; + if (this.seq > maxSeq) { + this.setPre(); + this.initSeqAndInc(); + } + this.fillSeq(); + // @ts-ignore - Uint8Arrays can be an argument + return String.fromCharCode.apply(String, this.buf); + } + + reset() { + this.init(); + } +} + +/** + * global nuid instance + */ +export const nuid: Nuid = new Nuid();