Skip to content

Commit 4f55442

Browse files
committed
Remove CJS support
1 parent 77c05f0 commit 4f55442

18 files changed

+102
-212
lines changed

async/index.browser.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
1+
export let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
22

3-
let customAlphabet = (alphabet, defaultSize = 21) => {
3+
export let customAlphabet = (alphabet, defaultSize = 21) => {
44
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
55
// values closer to the alphabet size. The bitmask calculates the closest
66
// `2^31 - 1` number, which exceeds the alphabet size.
@@ -39,7 +39,7 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
3939
}
4040
}
4141

42-
let nanoid = async (size = 21) => {
42+
export let nanoid = async (size = 21) => {
4343
let id = ''
4444
let bytes = crypto.getRandomValues(new Uint8Array(size))
4545

@@ -65,5 +65,3 @@ let nanoid = async (size = 21) => {
6565
}
6666
return id
6767
}
68-
69-
module.exports = { nanoid, customAlphabet, random }

async/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
let crypto = require('crypto')
1+
import { randomFill } from 'crypto'
22

3-
let { urlAlphabet } = require('../url-alphabet')
3+
import { urlAlphabet } from '../url-alphabet/index.js'
44

55
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
66
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
7-
let random = bytes =>
7+
export let random = bytes =>
88
new Promise((resolve, reject) => {
99
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
1010
// Memory flushing is unnecessary since the buffer allocation itself resets
1111
// the memory with the new bytes.
12-
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
12+
randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
1313
if (err) {
14+
/* c8 ignore next */
1415
reject(err)
1516
} else {
1617
resolve(buf)
1718
}
1819
})
1920
})
2021

21-
let customAlphabet = (alphabet, defaultSize = 21) => {
22+
export let customAlphabet = (alphabet, defaultSize = 21) => {
2223
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
2324
// values closer to the alphabet size. The bitmask calculates the closest
2425
// `2^31 - 1` number, which exceeds the alphabet size.
@@ -47,13 +48,14 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
4748
id += alphabet[bytes[i] & mask] || ''
4849
if (id.length === size) return id
4950
}
51+
/* c8 ignore next */
5052
return tick(id, size)
5153
})
5254

5355
return size => tick('', size)
5456
}
5557

56-
let nanoid = (size = 21) =>
58+
export let nanoid = (size = 21) =>
5759
random(size).then(bytes => {
5860
let id = ''
5961
// A compact alternative for `for (var i = 0; i < step; i++)`.
@@ -67,5 +69,3 @@ let nanoid = (size = 21) =>
6769
}
6870
return id
6971
})
70-
71-
module.exports = { nanoid, customAlphabet, random }

async/index.native.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
let { getRandomBytesAsync } = require('expo-random')
1+
import { getRandomBytesAsync } from 'expo-random'
22

3-
let { urlAlphabet } = require('../url-alphabet')
3+
import { urlAlphabet } from '../url-alphabet/index.js'
44

5-
let random = getRandomBytesAsync
5+
export let random = getRandomBytesAsync
66

7-
let customAlphabet = (alphabet, defaultSize = 21) => {
7+
export let customAlphabet = (alphabet, defaultSize = 21) => {
88
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
99
// values closer to the alphabet size. The bitmask calculates the closest
1010
// `2^31 - 1` number, which exceeds the alphabet size.
@@ -39,7 +39,7 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
3939
return size => tick('', size)
4040
}
4141

42-
let nanoid = (size = 21) =>
42+
export let nanoid = (size = 21) =>
4343
random(size).then(bytes => {
4444
let id = ''
4545
// A compact alternative for `for (var i = 0; i < step; i++)`.
@@ -53,5 +53,3 @@ let nanoid = (size = 21) =>
5353
}
5454
return id
5555
})
56-
57-
module.exports = { nanoid, customAlphabet, random }

bin/nanoid.cjs renamed to bin/nanoid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
let { nanoid, customAlphabet } = require('..')
3+
import { nanoid, customAlphabet } from '../index.js'
44

55
function print(msg) {
66
process.stdout.write(msg + '\n')

index.browser.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// This file replaces `index.js` in bundlers like webpack or Rollup,
22
// according to `browser` config in `package.json`.
33

4-
let { urlAlphabet } = require('./url-alphabet')
4+
export { urlAlphabet } from './url-alphabet/index.js'
55

6-
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
6+
export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
77

8-
let customRandom = (alphabet, defaultSize, getRandom) => {
8+
export let customRandom = (alphabet, defaultSize, getRandom) => {
99
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
1010
// values closer to the alphabet size. The bitmask calculates the closest
1111
// `2^31 - 1` number, which exceeds the alphabet size.
@@ -44,10 +44,10 @@ let customRandom = (alphabet, defaultSize, getRandom) => {
4444
}
4545
}
4646

47-
let customAlphabet = (alphabet, size = 21) =>
47+
export let customAlphabet = (alphabet, size = 21) =>
4848
customRandom(alphabet, size, random)
4949

50-
let nanoid = (size = 21) =>
50+
export let nanoid = (size = 21) =>
5151
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
5252
// It is incorrect to use bytes exceeding the alphabet size.
5353
// The following mask reduces the random byte in the 0-255 value
@@ -69,4 +69,3 @@ let nanoid = (size = 21) =>
6969
return id
7070
}, '')
7171

72-
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }

index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
let crypto = require('crypto')
1+
import { randomFillSync } from 'crypto'
22

3-
let { urlAlphabet } = require('./url-alphabet')
3+
import { urlAlphabet } from './url-alphabet/index.js'
4+
5+
export { urlAlphabet }
46

57
// It is best to make fewer, larger requests to the crypto module to
68
// avoid system call overhead. So, random numbers are generated in a
@@ -13,22 +15,22 @@ let pool, poolOffset
1315
let fillPool = bytes => {
1416
if (!pool || pool.length < bytes) {
1517
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
16-
crypto.randomFillSync(pool)
18+
randomFillSync(pool)
1719
poolOffset = 0
1820
} else if (poolOffset + bytes > pool.length) {
19-
crypto.randomFillSync(pool)
21+
randomFillSync(pool)
2022
poolOffset = 0
2123
}
2224
poolOffset += bytes
2325
}
2426

25-
let random = bytes => {
27+
export let random = bytes => {
2628
// `-=` convert `bytes` to number to prevent `valueOf` abusing
2729
fillPool((bytes -= 0))
2830
return pool.subarray(poolOffset - bytes, poolOffset)
2931
}
3032

31-
let customRandom = (alphabet, defaultSize, getRandom) => {
33+
export let customRandom = (alphabet, defaultSize, getRandom) => {
3234
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
3335
// values closer to the alphabet size. The bitmask calculates the closest
3436
// `2^31 - 1` number, which exceeds the alphabet size.
@@ -63,10 +65,10 @@ let customRandom = (alphabet, defaultSize, getRandom) => {
6365
}
6466
}
6567

66-
let customAlphabet = (alphabet, size = 21) =>
68+
export let customAlphabet = (alphabet, size = 21) =>
6769
customRandom(alphabet, size, random)
6870

69-
let nanoid = (size = 21) => {
71+
export let nanoid = (size = 21) => {
7072
// `-=` convert `size` to number to prevent `valueOf` abusing
7173
fillPool((size -= 0))
7274
let id = ''
@@ -81,5 +83,3 @@ let nanoid = (size = 21) => {
8183
}
8284
return id
8385
}
84-
85-
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }

nanoid.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

non-secure/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let urlAlphabet =
88
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
99

10-
let customAlphabet = (alphabet, defaultSize = 21) => {
10+
export let customAlphabet = (alphabet, defaultSize = 21) => {
1111
return (size = defaultSize) => {
1212
let id = ''
1313
// A compact alternative for `for (var i = 0; i < step; i++)`.
@@ -20,7 +20,7 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
2020
}
2121
}
2222

23-
let nanoid = (size = 21) => {
23+
export let nanoid = (size = 21) => {
2424
let id = ''
2525
// A compact alternative for `for (var i = 0; i < step; i++)`.
2626
let i = size
@@ -30,5 +30,3 @@ let nanoid = (size = 21) => {
3030
}
3131
return id
3232
}
33-
34-
module.exports = { nanoid, customAlphabet }

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,25 @@
1414
"test": "c8 pnpm unit && eslint . && pnpm clean && size-limit",
1515
"start": "vite test/demo/ --open"
1616
},
17+
"type": "module",
1718
"engines": {
1819
"node": "^14 || ^16 || >=18"
1920
},
2021
"author": "Andrey Sitnik <[email protected]>",
2122
"license": "MIT",
2223
"repository": "ai/nanoid",
24+
"exports": {
25+
".": {
26+
"browser": "./index.browser.js",
27+
"default": "./index.js"
28+
},
29+
"./async": {
30+
"browser": "./async/index.browser.js",
31+
"default": "./async/index.js"
32+
},
33+
"./non-secure": "./non-secure/index.js",
34+
"./package.json": "./package.json"
35+
},
2336
"browser": {
2437
"./index.js": "./index.browser.js",
2538
"./async/index.js": "./async/index.browser.js",
@@ -28,7 +41,7 @@
2841
"react-native": {
2942
"./async/index.js": "./async/index.native.js"
3043
},
31-
"bin": "./bin/nanoid.cjs",
44+
"bin": "./bin/nanoid.js",
3245
"sideEffects": false,
3346
"types": "./index.d.ts",
3447
"devDependencies": {
@@ -37,21 +50,19 @@
3750
"@lukeed/uuid": "^2.0.0",
3851
"@napi-rs/uuid": "^0.2.1",
3952
"@originjs/vite-plugin-commonjs": "^1.0.3",
40-
"@size-limit/dual-publish": "^7.0.8",
4153
"@size-limit/file": "^7.0.8",
4254
"@size-limit/webpack": "^7.0.8",
4355
"@types/node": "^17.0.40",
4456
"benchmark": "^2.1.4",
4557
"c8": "^7.11.3",
58+
"clean-publish": "^4.0.0",
4659
"cuid": "^2.1.8",
47-
"dual-publish": "^3.0.1",
4860
"eslint": "^8.17.0",
4961
"eslint-config-standard": "^17.0.0",
5062
"eslint-plugin-import": "^2.26.0",
5163
"eslint-plugin-n": "^15.2.1",
5264
"eslint-plugin-prefer-let": "^3.0.1",
5365
"eslint-plugin-promise": "^6.0.0",
54-
"nanospy": "^0.5.0",
5566
"picocolors": "^1.0.0",
5667
"postcss": "^8.4.14",
5768
"rndm": "^1.2.0",

0 commit comments

Comments
 (0)