Skip to content

Commit

Permalink
Remove CJS support
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jun 7, 2022
1 parent 77c05f0 commit 4f55442
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 212 deletions.
8 changes: 3 additions & 5 deletions async/index.browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
export let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))

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

let nanoid = async (size = 21) => {
export let nanoid = async (size = 21) => {
let id = ''
let bytes = crypto.getRandomValues(new Uint8Array(size))

Expand All @@ -65,5 +65,3 @@ let nanoid = async (size = 21) => {
}
return id
}

module.exports = { nanoid, customAlphabet, random }
16 changes: 8 additions & 8 deletions async/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
let crypto = require('crypto')
import { randomFill } from 'crypto'

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

// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
let random = bytes =>
export 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) => {
randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
if (err) {
/* c8 ignore next */
reject(err)
} else {
resolve(buf)
}
})
})

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

return size => tick('', size)
}

let nanoid = (size = 21) =>
export let nanoid = (size = 21) =>
random(size).then(bytes => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
Expand All @@ -67,5 +69,3 @@ let nanoid = (size = 21) =>
}
return id
})

module.exports = { nanoid, customAlphabet, random }
12 changes: 5 additions & 7 deletions async/index.native.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let { getRandomBytesAsync } = require('expo-random')
import { getRandomBytesAsync } from 'expo-random'

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

let random = getRandomBytesAsync
export let random = getRandomBytesAsync

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

let nanoid = (size = 21) =>
export let nanoid = (size = 21) =>
random(size).then(bytes => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
Expand All @@ -53,5 +53,3 @@ let nanoid = (size = 21) =>
}
return id
})

module.exports = { nanoid, customAlphabet, random }
2 changes: 1 addition & 1 deletion bin/nanoid.cjs → bin/nanoid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

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

function print(msg) {
process.stdout.write(msg + '\n')
Expand Down
11 changes: 5 additions & 6 deletions index.browser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// This file replaces `index.js` in bundlers like webpack or Rollup,
// according to `browser` config in `package.json`.

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

let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))

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

let customAlphabet = (alphabet, size = 21) =>
export let customAlphabet = (alphabet, size = 21) =>
customRandom(alphabet, size, random)

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

module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
let crypto = require('crypto')
import { randomFillSync } from 'crypto'

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

export { urlAlphabet }

// It is best to make fewer, larger requests to the crypto module to
// avoid system call overhead. So, random numbers are generated in a
Expand All @@ -13,22 +15,22 @@ let pool, poolOffset
let fillPool = bytes => {
if (!pool || pool.length < bytes) {
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
crypto.randomFillSync(pool)
randomFillSync(pool)
poolOffset = 0
} else if (poolOffset + bytes > pool.length) {
crypto.randomFillSync(pool)
randomFillSync(pool)
poolOffset = 0
}
poolOffset += bytes
}

let random = bytes => {
export let random = bytes => {
// `-=` convert `bytes` to number to prevent `valueOf` abusing
fillPool((bytes -= 0))
return pool.subarray(poolOffset - bytes, poolOffset)
}

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

let customAlphabet = (alphabet, size = 21) =>
export let customAlphabet = (alphabet, size = 21) =>
customRandom(alphabet, size, random)

let nanoid = (size = 21) => {
export let nanoid = (size = 21) => {
// `-=` convert `size` to number to prevent `valueOf` abusing
fillPool((size -= 0))
let id = ''
Expand All @@ -81,5 +83,3 @@ let nanoid = (size = 21) => {
}
return id
}

module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
2 changes: 1 addition & 1 deletion nanoid.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions non-secure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let urlAlphabet =
'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'

let customAlphabet = (alphabet, defaultSize = 21) => {
export let customAlphabet = (alphabet, defaultSize = 21) => {
return (size = defaultSize) => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
Expand All @@ -20,7 +20,7 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
}
}

let nanoid = (size = 21) => {
export let nanoid = (size = 21) => {
let id = ''
// A compact alternative for `for (var i = 0; i < step; i++)`.
let i = size
Expand All @@ -30,5 +30,3 @@ let nanoid = (size = 21) => {
}
return id
}

module.exports = { nanoid, customAlphabet }
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@
"test": "c8 pnpm unit && eslint . && pnpm clean && size-limit",
"start": "vite test/demo/ --open"
},
"type": "module",
"engines": {
"node": "^14 || ^16 || >=18"
},
"author": "Andrey Sitnik <andrey@sitnik.ru>",
"license": "MIT",
"repository": "ai/nanoid",
"exports": {
".": {
"browser": "./index.browser.js",
"default": "./index.js"
},
"./async": {
"browser": "./async/index.browser.js",
"default": "./async/index.js"
},
"./non-secure": "./non-secure/index.js",
"./package.json": "./package.json"
},
"browser": {
"./index.js": "./index.browser.js",
"./async/index.js": "./async/index.browser.js",
Expand All @@ -28,7 +41,7 @@
"react-native": {
"./async/index.js": "./async/index.native.js"
},
"bin": "./bin/nanoid.cjs",
"bin": "./bin/nanoid.js",
"sideEffects": false,
"types": "./index.d.ts",
"devDependencies": {
Expand All @@ -37,21 +50,19 @@
"@lukeed/uuid": "^2.0.0",
"@napi-rs/uuid": "^0.2.1",
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@size-limit/dual-publish": "^7.0.8",
"@size-limit/file": "^7.0.8",
"@size-limit/webpack": "^7.0.8",
"@types/node": "^17.0.40",
"benchmark": "^2.1.4",
"c8": "^7.11.3",
"clean-publish": "^4.0.0",
"cuid": "^2.1.8",
"dual-publish": "^3.0.1",
"eslint": "^8.17.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.2.1",
"eslint-plugin-prefer-let": "^3.0.1",
"eslint-plugin-promise": "^6.0.0",
"nanospy": "^0.5.0",
"picocolors": "^1.0.0",
"postcss": "^8.4.14",
"rndm": "^1.2.0",
Expand Down
Loading

0 comments on commit 4f55442

Please sign in to comment.