Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nuid

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nuid - npm Package Compare versions

Comparing version 1.1.6 to 2.0.0-2

.github/ISSUE_TEMPLATE/config.yml

3

CODE-OF-CONDUCT.md
## Community Code of Conduct
NATS follows the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md).
NATS follows the
[CNCF Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md).

@@ -5,13 +5,13 @@ # External Dependencies

| Dependency | License |
|-|-|
| No runtime dependencies | |
| Dev Dependencies (fanout 347) | |
| coveralls@3.0.9 | BSD-2-Clause |
| dependency-check@4.1.0 | BSD-3-Clause |
| eslint@6.8.0 | MIT |
| mocha-lcov-reporter@1.3.0 | BSD-2-Clause |
| mocha@7.0.1 | MIT |
| nyc@15.0.0 | ISC |
| should@13.2.3 | MIT |
| standard@14.3.1 | MIT |
| Dependency | License |
| ----------------------------- | ------------ |
| No runtime dependencies | |
| Dev Dependencies (fanout 347) | |
| coveralls@3.0.9 | BSD-2-Clause |
| dependency-check@4.1.0 | BSD-3-Clause |
| eslint@6.8.0 | MIT |
| mocha-lcov-reporter@1.3.0 | BSD-2-Clause |
| mocha@7.0.1 | MIT |
| nyc@15.0.0 | ISC |
| should@13.2.3 | MIT |
| standard@14.3.1 | MIT |
/*
* Copyright 2016-2020 The NATS Authors
* Copyright 2016-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");

@@ -15,134 +15,112 @@ * you may not use this file except in compliance with the License.

*/
'use strict'
/**
* Module Dependencies
*/
const crypto = require('crypto')
/**
* Constants
*/
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
exports.version = require('../package.json').version
/**
* Create and initialize a nuid.
*
* @api private
*/
function Nuid () {
this.buf = Buffer.alloc(totalLen)
this.init()
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nuid = exports.Nuid = void 0;
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) {
for (let i = 0; i < a.length; i++) {
a[i] = Math.floor(Math.random() * 255);
}
}
/**
* Initializes a nuid with a crypto random prefix,
* and pseudo-random sequence and increment.
*
* @api private
*/
Nuid.prototype.init = function () {
this.setPre()
this.initSeqAndInc()
this.fillSeq()
function fillRandom(a) {
var _a;
if ((_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.crypto) === null || _a === void 0 ? void 0 : _a.getRandomValues) {
globalThis.crypto.getRandomValues(a);
}
else {
_getRandomValues(a);
}
}
/**
* Initializes the pseudo randmon sequence number and the increment range.
*
* @api private
* Nuid is a class that generates unique identifiers.
*/
Nuid.prototype.initSeqAndInc = function () {
this.seq = Math.floor(Math.random() * maxSeq)
this.inc = Math.floor((Math.random() * (maxInc - minInc)) + minInc)
class Nuid {
constructor() {
this.buf = new Uint8Array(totalLen);
this.inited = false;
}
/**
* Initializes a nuid with a crypto random prefix,
* and pseudo-random sequence and increment. This function
* is only called if any api on a nuid is called.
*
* @api private
*/
init() {
this.inited = true;
this.setPre();
this.initSeqAndInc();
this.fillSeq();
}
/**
* Initializes the pseudo random sequence number and the increment range.
*
* @api 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 them to base36.
*
* @api 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
*/
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.
*/
next() {
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);
}
/**
* Resets the prefix and counter for the nuid. This is typically
* called automatically from within next() if the current sequence
* exceeds the resolution of the nuid.
*/
reset() {
this.init();
}
}
exports.Nuid = Nuid;
/**
* Sets the prefix from crypto random bytes. Converts to base36.
*
* @api private
* A nuid instance you can use by simply calling `next()` on it.
*/
Nuid.prototype.setPre = function () {
const cbuf = crypto.randomBytes(preLen)
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
*/
Nuid.prototype.fillSeq = function () {
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
*/
Nuid.prototype.next = function () {
this.seq += this.inc
if (this.seq > maxSeq) {
this.setPre()
this.initSeqAndInc()
}
this.fillSeq()
return (this.buf.toString('ascii'))
}
/* Global Nuid */
const g = new Nuid()
/**
* Resets the prefix of the global nuid, as well as the
* pseudo random sequence number and increment amounts.
*
* @api public
*/
exports.reset = function () {
g.init()
}
/**
* Returns the next nuid from the global.
*
* @api public
*/
exports.next = function () {
return g.next()
}
/**
* This here to facilitate testing
* @api private
*/
exports.getGlobalNuid = function () {
return g
}
exports.nuid = new Nuid();
//# sourceMappingURL=nuid.js.map

@@ -6,4 +6,6 @@ # Maintainers

### Maintainers
- Alberto Ricart <alberto@nats.io> [@aricart](https://github.com/aricart)
- Derek Collison <derek@nats.io> [@derekcollison](https://github.com/derekcollison)
- Ivan Kozlovic <ivan@nats.io> [@kozlovic](https://github.com/kozlovic)
- Alberto Ricart <alberto@nats.io> [@aricart](https://github.com/aricart)
- Derek Collison <derek@nats.io>
[@derekcollison](https://github.com/derekcollison)
- Ivan Kozlovic <ivan@nats.io> [@kozlovic](https://github.com/kozlovic)
{
"name": "nuid",
"version": "1.1.6",
"version": "2.0.0-2",
"description": "NUID - A highly performant unique identifier generator.",

@@ -10,9 +10,14 @@ "keywords": [

],
"homepage": "https://nats.io",
"exports": {
"types": "./lib/nuid.d.ts",
"require": "./lib/nuid.js",
"import": "./esm/index.mjs"
},
"homepage": "https://nats.io/nuid.js",
"repository": {
"type": "git",
"url": "git@github.com:nats-io/node-nuid.git"
"url": "git@github.com:nats-io/nuid.js.git"
},
"bugs": {
"url": "https://github.com/nats-io/node-nuid/issues"
"url": "https://github.com/nats-io/nuid.js/issues"
},

@@ -27,35 +32,17 @@ "license": "Apache-2.0",

"scripts": {
"depcheck": "dependency-check --no-dev package.json",
"depcheck:unused": "dependency-check package.json --no-dev --entry ./**/*.js",
"test:unit": "mkdir -p reports/ && NODE_ENV=test multi='spec=- xunit=reports/mocha-xunit.xml' nyc mocha --timeout 10000 --slow 750",
"test": "npm run depcheck && npm run depcheck:unused && npm run lint && npm run test:unit",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"cover": "nyc report --reporter=html && open coverage/index.html",
"lint": "standard './**/*.js'",
"fmt": "standard --fix './**/*.js'"
"clean": "shx rm -f esm/index.mjs lib/nuid.d.ts lib/nuid.js lib/nuid.js.map",
"build": "npm run clean && deno bundle src/nuid.ts esm/index.mjs && tsc",
"test": "npm run build && node --test test/basics.js && deno test",
"doc": "rm -Rf ./docs && node_modules/.bin/typedoc --out ./docs/ && touch ./docs/.nojekyll",
"prepack": "npm run build",
"lint": "deno lint --ignore=docs/,debug/,lib/"
},
"engines": {
"node": ">= 8.16.0"
"node": ">= 18.x"
},
"devDependencies": {
"coveralls": "^3.0.9",
"dependency-check": "^4.1.0",
"eslint": "^6.8.0",
"mocha": "^7.0.1",
"mocha-lcov-reporter": "1.3.0",
"nyc": "^15.0.0",
"should": "^13.2.3",
"standard": "^14.3.1"
},
"nyc": {
"include": [
"lib/**"
],
"exclude": [
"test/**",
"examples/**",
"benchmark/**"
]
},
"typings": "./index.d.ts"
"shx": "^0.3.4",
"typescript": "^5.4.5",
"typedoc": "^0.25.13"
}
}
# NODE NUID
[![license](https://img.shields.io/github/license/nats-io/node-nuid.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Travis branch](https://img.shields.io/travis/nats-io/node-nuid/master.svg)](https://travis-ci.org/nats-io/node-nuid)
[![Coveralls github branch](https://img.shields.io/coveralls/github/nats-io/node-nuid/master.svg)](https://coveralls.io/github/nats-io/node-nuid)
[![npm](https://img.shields.io/npm/v/nuid.svg)](https://www.npmjs.com/package/nuid)
[![npm](https://img.shields.io/npm/dt/nuid.svg)](https://www.npmjs.com/package/nuid)
[![npm](https://img.shields.io/npm/dm/nuid.svg)](https://www.npmjs.com/package/nuid)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

@@ -15,36 +12,80 @@ A highly performant unique identifier generator.

Use the `npm` command:
Import the library for node/bun:
$ npm install nuid
```bash
npm install nuid
```
For web and deno you can also:
```bash
npx jsr add @nats-io/nuid
```
## Basic Usage
Reference the library in your code. If using you can `import` or `require` the
npm nuid library supports both Common JS and ESM:
```javascript
const { nuid, Nuid } = require("nuid");
// or
import { next, Nuid } from "nuid";
const NUID = require('nuid');
let nuid = NUID.next();
// `nuid` is a global instance of nuid, you can use it directly
// `Nuid` is the actual class implementing the nuids, so you can also
// `new Nuid()`.
```
```javascript
// To generate a bunch of nuids:
let id = nuid.next();
id = nuid.next();
//
// Generate a new crypto/rand seeded prefix.
// Generally not needed, happens automatically.
NUID.reset();
// To generate a new prefix:
nuid.reset();
// note that prefixes are automatically rolled whenever all
// the nuids for the specific prefix have been used.
id = nuid.next();
```
## Performance
NUID needs to be very fast to generate and be truly unique, all while being entropy pool friendly.
NUID uses 12 bytes of crypto generated data (entropy draining), and 10 bytes of pseudo-random
sequential data that increments with a pseudo-random increment.
NUID needs to be very fast to generate and be truly unique, all while being
entropy pool friendly. NUID uses 12 bytes of crypto generated data (entropy
draining), and 10 bytes of pseudo-random sequential data that increments with a
pseudo-random increment.
Total length of a NUID string is 22 bytes of base 36 ascii text, so 36^22 or
17324272922341479351919144385642496 possibilities.
## Supported Node Versions
## Migration
Support policy for Nodejs versions follows
[Nodejs release support]( https://github.com/nodejs/Release).
We will support and build node-nats on even Nodejs versions that are current
or in maintenance.
The 2.x version of the npm module support both CJS and ESM modules, an ESM only
version of the module is available via
[jsr @nats-io/nuid](https://jsr.io/@nats-io/nuid)
If you are migrating from the 1.x.x series, note that `getGlobalNuid()`,
`next()` and `reset()` and `version` property have been removed. Instead, access
the exported constant `nuid` and call `next()` or `reset()` on it as shown in
the examples above. For version information please refer to your installed
module's version information.
If you are migrating from the `js-nuid` module in npm, there should be no
changes except to the location of the import in the npm bundle:
```typescript
import { nuid } from "./node_modules/esm/index.js";
```
## Supported Node Versions
Support policy for Nodejs versions follows
[Nodejs release support](https://github.com/nodejs/Release). We will support and
build nuid on even Nodejs versions that are current or in maintenance.
## License
Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.
Unless otherwise noted, the NATS source files are distributed under the Apache
Version 2.0 license found in the LICENSE file.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc