Skip to content

Commit 9067e03

Browse files
committed
Sync CJS and ESM
1 parent 9ad9805 commit 9067e03

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

‎index.cjs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ let pool, poolOffset
1212

1313
let fillPool = bytes => {
1414
if (bytes < 0 || bytes > 1024) throw new RangeError('Wrong ID size')
15-
if (!pool || pool.length < bytes) {
16-
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
17-
crypto.randomFillSync(pool)
18-
poolOffset = 0
19-
} else if (poolOffset + bytes > pool.length) {
20-
crypto.randomFillSync(pool)
21-
poolOffset = 0
15+
try {
16+
if (!pool || pool.length < bytes) {
17+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
18+
crypto.randomFillSync(pool)
19+
poolOffset = 0
20+
} else if (poolOffset + bytes > pool.length) {
21+
crypto.randomFillSync(pool)
22+
poolOffset = 0
23+
}
24+
} catch (e) {
25+
pool = undefined
26+
throw e
2227
}
2328
poolOffset += bytes
2429
}

‎index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const POOL_SIZE_MULTIPLIER = 128
1111
let pool, poolOffset
1212

1313
let fillPool = bytes => {
14-
if (bytes < 0) throw new RangeError('Wrong ID size')
14+
if (bytes < 0 || bytes > 1024) throw new RangeError('Wrong ID size')
1515
try {
1616
if (!pool || pool.length < bytes) {
1717
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)

‎test/index.test.cjs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
let { test } = require('uvu')
2-
let { is, ok, equal, match, not } = require('uvu/assert')
2+
let { is, ok, equal, match, not, throws } = require('uvu/assert')
33

44
let browser = require('../index.browser.js')
5+
let nodeCjs = require('../index.cjs')
56
let node = require('../index.js')
67

8+
// `index.js` and `index.cjs` are two entries of the same package.
9+
// They must behave identically, so both are tested here.
10+
let entries = { browser, cjs: nodeCjs, node }
11+
712
test.before(() => {
813
global.crypto = {
914
getRandomValues(array) {
@@ -19,9 +24,9 @@ test.after(() => {
1924
delete global.crypto
2025
})
2126

22-
for (let type of ['node', 'browser']) {
27+
for (let type of ['node', 'cjs', 'browser']) {
2328
let { nanoid, customAlphabet, customRandom, random, urlAlphabet } =
24-
type === 'node' ? node : browser
29+
entries[type]
2530

2631
test(`${type} / nanoid / generates URL-friendly IDs`, () => {
2732
for (let i = 0; i < 100; i++) {
@@ -176,7 +181,15 @@ for (let type of ['node', 'browser']) {
176181
}
177182
})
178183

179-
if (type === 'node') {
184+
if (type !== 'browser') {
185+
test(`${type} / nanoid / limits ID size`, () => {
186+
is(nanoid(1024).length, 1024)
187+
throws(() => nanoid(1025), /Wrong ID size/)
188+
throws(() => random(1025), /Wrong ID size/)
189+
// A refused ID must not break the pool for the next request.
190+
is(nanoid().length, 21)
191+
})
192+
180193
test(`${type} / proxy number / prevent collision`, () => {
181194
let makeProxyNumberToReproducePreviousID = () => {
182195
let step = 0

0 commit comments

Comments
 (0)