Skip to content

Commit 49e001a

Browse files
committed
jsr
1 parent 46f5468 commit 49e001a

File tree

4 files changed

+155
-115
lines changed

4 files changed

+155
-115
lines changed

nats-base-client/deno.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@nats-io/nats-core",
3+
"version": "3.0.0-1",
4+
"exports": {
5+
".": "./src/mod.ts"
6+
},
7+
"publish": {
8+
"include": [
9+
"./src/**/*",
10+
"jsr.json"
11+
]
12+
}
13+
}

nats-base-client/nuid.ts

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -13,119 +13,5 @@
1313
* limitations under the License.
1414
*/
1515

16-
"use strict";
16+
export {Nuid, nuid} from "../nuid/src/mod.ts"
1717

18-
const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
19-
const base = 36;
20-
const preLen = 12;
21-
const seqLen = 10;
22-
const maxSeq = 3656158440062976; // base^seqLen == 36^10
23-
const minInc = 33;
24-
const maxInc = 333;
25-
const totalLen = preLen + seqLen;
26-
27-
function _getRandomValues(a: Uint8Array) {
28-
for (let i = 0; i < a.length; i++) {
29-
a[i] = Math.floor(Math.random() * 255);
30-
}
31-
}
32-
33-
function fillRandom(a: Uint8Array) {
34-
if (globalThis?.crypto?.getRandomValues) {
35-
globalThis.crypto.getRandomValues(a);
36-
} else {
37-
_getRandomValues(a);
38-
}
39-
}
40-
41-
/**
42-
* Create and initialize a nuid.
43-
*
44-
* @api private
45-
*/
46-
export class Nuid {
47-
buf: Uint8Array;
48-
seq!: number;
49-
inc!: number;
50-
inited: boolean;
51-
52-
constructor() {
53-
this.buf = new Uint8Array(totalLen);
54-
this.inited = false;
55-
}
56-
57-
/**
58-
* Initializes a nuid with a crypto random prefix,
59-
* and pseudo-random sequence and increment.
60-
*
61-
* @api private
62-
*/
63-
private init() {
64-
this.inited = true;
65-
this.setPre();
66-
this.initSeqAndInc();
67-
this.fillSeq();
68-
}
69-
70-
/**
71-
* Initializes the pseudo randmon sequence number and the increment range.
72-
*
73-
* @api private
74-
*/
75-
private initSeqAndInc() {
76-
this.seq = Math.floor(Math.random() * maxSeq);
77-
this.inc = Math.floor(Math.random() * (maxInc - minInc) + minInc);
78-
}
79-
80-
/**
81-
* Sets the prefix from crypto random bytes. Converts to base36.
82-
*
83-
* @api private
84-
*/
85-
private setPre() {
86-
const cbuf = new Uint8Array(preLen);
87-
fillRandom(cbuf);
88-
for (let i = 0; i < preLen; i++) {
89-
const di = cbuf[i] % base;
90-
this.buf[i] = digits.charCodeAt(di);
91-
}
92-
}
93-
94-
/**
95-
* Fills the sequence part of the nuid as base36 from this.seq.
96-
*
97-
* @api private
98-
*/
99-
private fillSeq() {
100-
let n = this.seq;
101-
for (let i = totalLen - 1; i >= preLen; i--) {
102-
this.buf[i] = digits.charCodeAt(n % base);
103-
n = Math.floor(n / base);
104-
}
105-
}
106-
107-
/**
108-
* Returns the next nuid.
109-
*
110-
* @api private
111-
*/
112-
next(): string {
113-
if (!this.inited) {
114-
this.init();
115-
}
116-
this.seq += this.inc;
117-
if (this.seq > maxSeq) {
118-
this.setPre();
119-
this.initSeqAndInc();
120-
}
121-
this.fillSeq();
122-
// @ts-ignore - Uint8Arrays can be an argument
123-
return String.fromCharCode.apply(String, this.buf);
124-
}
125-
126-
reset() {
127-
this.init();
128-
}
129-
}
130-
131-
export const nuid = new Nuid();

nuid/jsr.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@nats-io/nuid",
3+
"version": "2.0.0-1",
4+
"exports": {
5+
".": "./src/mod.ts"
6+
}
7+
}

nuid/src/mod.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2016-2024 The NATS Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
"use strict";
17+
18+
const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
19+
const base = 36;
20+
const preLen = 12;
21+
const seqLen = 10;
22+
const maxSeq = 3656158440062976; // base^seqLen == 36^10
23+
const minInc = 33;
24+
const maxInc = 333;
25+
const totalLen = preLen + seqLen;
26+
27+
function _getRandomValues(a: Uint8Array) {
28+
for (let i = 0; i < a.length; i++) {
29+
a[i] = Math.floor(Math.random() * 255);
30+
}
31+
}
32+
33+
function fillRandom(a: Uint8Array) {
34+
if (globalThis?.crypto?.getRandomValues) {
35+
globalThis.crypto.getRandomValues(a);
36+
} else {
37+
_getRandomValues(a);
38+
}
39+
}
40+
41+
/**
42+
* Create and initialize a nuid.
43+
*
44+
* @api private
45+
*/
46+
export class Nuid {
47+
buf: Uint8Array;
48+
seq!: number;
49+
inc!: number;
50+
inited: boolean;
51+
52+
constructor() {
53+
this.buf = new Uint8Array(totalLen);
54+
this.inited = false;
55+
}
56+
57+
/**
58+
* Initializes a nuid with a crypto random prefix,
59+
* and pseudo-random sequence and increment.
60+
*
61+
* @api private
62+
*/
63+
private init() {
64+
this.inited = true;
65+
this.setPre();
66+
this.initSeqAndInc();
67+
this.fillSeq();
68+
}
69+
70+
/**
71+
* Initializes the pseudo randmon sequence number and the increment range.
72+
*
73+
* @api private
74+
*/
75+
private initSeqAndInc() {
76+
this.seq = Math.floor(Math.random() * maxSeq);
77+
this.inc = Math.floor(Math.random() * (maxInc - minInc) + minInc);
78+
}
79+
80+
/**
81+
* Sets the prefix from crypto random bytes. Converts to base36.
82+
*
83+
* @api private
84+
*/
85+
private setPre() {
86+
const cbuf = new Uint8Array(preLen);
87+
fillRandom(cbuf);
88+
for (let i = 0; i < preLen; i++) {
89+
const di = cbuf[i] % base;
90+
this.buf[i] = digits.charCodeAt(di);
91+
}
92+
}
93+
94+
/**
95+
* Fills the sequence part of the nuid as base36 from this.seq.
96+
*
97+
* @api private
98+
*/
99+
private fillSeq() {
100+
let n = this.seq;
101+
for (let i = totalLen - 1; i >= preLen; i--) {
102+
this.buf[i] = digits.charCodeAt(n % base);
103+
n = Math.floor(n / base);
104+
}
105+
}
106+
107+
/**
108+
* Returns the next nuid.
109+
*
110+
* @api private
111+
*/
112+
next(): string {
113+
if (!this.inited) {
114+
this.init();
115+
}
116+
this.seq += this.inc;
117+
if (this.seq > maxSeq) {
118+
this.setPre();
119+
this.initSeqAndInc();
120+
}
121+
this.fillSeq();
122+
// @ts-ignore - Uint8Arrays can be an argument
123+
return String.fromCharCode.apply(String, this.buf);
124+
}
125+
126+
reset() {
127+
this.init();
128+
}
129+
}
130+
131+
/**
132+
* global nuid instance
133+
*/
134+
export const nuid: Nuid = new Nuid();

0 commit comments

Comments
 (0)