Socket
Socket
Sign inDemoInstall

nanoid

Package Overview
Dependencies
0
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.1.6 to 3.1.7

4

async/index.browser.js

@@ -7,3 +7,3 @@ let customAlphabet = (alphabet, size) => {

// `Math.clz32` is not used, because it is not available in browsers.
let mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
// Though, the bitmask solution is not perfect since the bytes exceeding

@@ -24,3 +24,3 @@ // the alphabet size are refused. Therefore, to reliably generate the ID,

// `-~i => i + 1` if i is an integer
let step = -~(1.6 * mask * size / alphabet.length)
let step = -~((1.6 * mask * size) / alphabet.length)

@@ -27,0 +27,0 @@ return () => {

@@ -39,3 +39,4 @@ /**

export function customAlphabet (
alphabet: string, size: number
alphabet: string,
size: number
): () => Promise<string>

@@ -42,0 +43,0 @@

@@ -7,14 +7,15 @@ import crypto from 'crypto'

// because it is possible to use in combination with `Buffer.allocUnsafe()`.
let random = bytes => new Promise((resolve, reject) => {
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
// Memory flushing is unnecessary since the buffer allocation itself resets
// the memory with the new bytes.
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
if (err) {
reject(err)
} else {
resolve(buf)
}
let random = bytes =>
new Promise((resolve, reject) => {
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
// Memory flushing is unnecessary since the buffer allocation itself resets
// the memory with the new bytes.
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
if (err) {
reject(err)
} else {
resolve(buf)
}
})
})
})

@@ -26,3 +27,3 @@ let customAlphabet = (alphabet, size) => {

// For example, the bitmask for the alphabet size 30 is 31 (00011111).
let mask = (2 << 31 - Math.clz32((alphabet.length - 1) | 1)) - 1
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
// Though, the bitmask solution is not perfect since the bytes exceeding

@@ -40,15 +41,16 @@ // the alphabet size are refused. Therefore, to reliably generate the ID,

// according to benchmarks).
let step = Math.ceil(1.6 * mask * size / alphabet.length)
let step = Math.ceil((1.6 * mask * size) / alphabet.length)
let tick = id => random(step).then(bytes => {
// A compact alternative for `for (var i = 0; i < step; i++)`.
let i = step
while (i--) {
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
id += alphabet[bytes[i] & mask] || ''
// `id.length + 1 === size` is a more compact option.
if (id.length === +size) return id
}
return tick(id)
})
let tick = id =>
random(step).then(bytes => {
// A compact alternative for `for (var i = 0; i < step; i++)`.
let i = step
while (i--) {
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
id += alphabet[bytes[i] & mask] || ''
// `id.length + 1 === size` is a more compact option.
if (id.length === +size) return id
}
return tick(id)
})

@@ -58,16 +60,17 @@ return () => tick('')

let nanoid = (size = 21) => random(size).then(bytes => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
while (size--) {
// It is incorrect to use bytes exceeding the alphabet size.
// The following mask reduces the random byte in the 0-255 value
// range to the 0-63 value range. Therefore, adding hacks, such
// as empty string fallback or magic numbers, is unneccessary because
// the bitmask trims bytes down to the alphabet size.
id += urlAlphabet[bytes[size] & 63]
}
return id
})
let nanoid = (size = 21) =>
random(size).then(bytes => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
while (size--) {
// It is incorrect to use bytes exceeding the alphabet size.
// The following mask reduces the random byte in the 0-255 value
// range to the 0-63 value range. Therefore, adding hacks, such
// as empty string fallback or magic numbers, is unneccessary because
// the bitmask trims bytes down to the alphabet size.
id += urlAlphabet[bytes[size] & 63]
}
return id
})
export { nanoid, customAlphabet }
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).
## 3.1.7
* Clean up code.
## 3.1.6

@@ -5,0 +8,0 @@ * Avoid `self` using.

@@ -13,4 +13,4 @@ // This file replaces `index.js` in bundlers like webpack or Rollup,

'React Native does not have a built-in secure random generator. ' +
'If you don’t need unpredictable IDs, you can use `nanoid/non-secure`. ' +
'For secure IDs, import `react-native-get-random-values` before Nano ID.'
'If you don’t need unpredictable IDs, you can use `nanoid/non-secure`. ' +
'For secure IDs, import `react-native-get-random-values` before Nano ID.'
)

@@ -21,3 +21,3 @@ }

'Add `if (!window.crypto) window.crypto = window.msCrypto` ' +
'before Nano ID to fix IE 11 support'
'before Nano ID to fix IE 11 support'
)

@@ -28,3 +28,3 @@ }

'Your browser does not have secure random generator. ' +
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
)

@@ -47,3 +47,3 @@ }

// `Math.clz32` is not used, because it is not available in browsers.
let mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
// Though, the bitmask solution is not perfect since the bytes exceeding

@@ -64,3 +64,3 @@ // the alphabet size are refused. Therefore, to reliably generate the ID,

// `-~i => i + 1` if i is an integer
let step = -~(1.6 * mask * size / alphabet.length)
let step = -~((1.6 * mask * size) / alphabet.length)

@@ -67,0 +67,0 @@ return () => {

@@ -63,3 +63,3 @@ /**

size: number,
random: (bytes: number) => Uint8Array,
random: (bytes: number) => Uint8Array
): () => string

@@ -66,0 +66,0 @@

@@ -8,3 +8,3 @@ import crypto from 'crypto'

// for better performance.
let buffers = { }
let buffers = {}
let random = bytes => {

@@ -27,3 +27,3 @@ let buffer = buffers[bytes]

// For example, the bitmask for the alphabet size 30 is 31 (00011111).
let mask = (2 << 31 - Math.clz32((alphabet.length - 1) | 1)) - 1
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
// Though, the bitmask solution is not perfect since the bytes exceeding

@@ -41,3 +41,3 @@ // the alphabet size are refused. Therefore, to reliably generate the ID,

// according to benchmarks).
let step = Math.ceil(1.6 * mask * size / alphabet.length)
let step = Math.ceil((1.6 * mask * size) / alphabet.length)

@@ -44,0 +44,0 @@ return () => {

@@ -13,3 +13,3 @@ // This alphabet uses `A-Za-z0-9_-` symbols. The genetic algorithm helped

// `| 0` is more compact and faster than `Math.floor()`.
id += alphabet[Math.random() * alphabet.length | 0]
id += alphabet[(Math.random() * alphabet.length) | 0]
}

@@ -26,3 +26,3 @@ return id

// `| 0` is more compact and faster than `Math.floor()`.
id += urlAlphabet[Math.random() * 64 | 0]
id += urlAlphabet[(Math.random() * 64) | 0]
}

@@ -29,0 +29,0 @@ return id

{
"name": "nanoid",
"version": "3.1.6",
"version": "3.1.7",
"description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",

@@ -5,0 +5,0 @@ "keywords": [

@@ -208,3 +208,3 @@ # Nano ID

If you want to use Nano ID in the `key` prop, you must set some string prefix
If you want to use Nano ID in the `id` prop, you must set some string prefix
(it is invalid for the HTML ID to start with a number).

@@ -211,0 +211,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc