create-microact-app 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +95 -0
- package/package.json +21 -0
- package/templates/vanilla/.github/workflows/deploy.yml +38 -0
- package/templates/vanilla/index.html +13 -0
- package/templates/vanilla/node_modules/.package-lock.json +207 -0
- package/templates/vanilla/node_modules/@esbuild/darwin-x64/README.md +3 -0
- package/templates/vanilla/node_modules/@esbuild/darwin-x64/bin/esbuild +0 -0
- package/templates/vanilla/node_modules/@esbuild/darwin-x64/package.json +17 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/README.md +154 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/dist/microact.cjs.js +1749 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/dist/microact.cjs.js.map +1 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/dist/microact.esm.js +1743 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/dist/microact.esm.js.map +1 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/dist/microact.umd.js +2 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/dist/microact.umd.js.map +1 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/example/index.html +13 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/example/index.js +63 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/package.json +38 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/rollup.config.cjs +30 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/src/Component.js +831 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/src/DOMUpdater.js +320 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/src/EventBus.js +123 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/src/Router.js +253 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/src/UpdateScheduler.js +218 -0
- package/templates/vanilla/node_modules/@monygroupcorp/microact/src/index.js +6 -0
- package/templates/vanilla/node_modules/esbuild/LICENSE.md +21 -0
- package/templates/vanilla/node_modules/esbuild/README.md +3 -0
- package/templates/vanilla/node_modules/esbuild/bin/esbuild +0 -0
- package/templates/vanilla/node_modules/esbuild/install.js +287 -0
- package/templates/vanilla/node_modules/esbuild/lib/main.d.ts +660 -0
- package/templates/vanilla/node_modules/esbuild/lib/main.js +2393 -0
- package/templates/vanilla/node_modules/esbuild/package.json +42 -0
- package/templates/vanilla/node_modules/nanoid/LICENSE +20 -0
- package/templates/vanilla/node_modules/nanoid/README.md +39 -0
- package/templates/vanilla/node_modules/nanoid/async/index.browser.cjs +69 -0
- package/templates/vanilla/node_modules/nanoid/async/index.browser.js +34 -0
- package/templates/vanilla/node_modules/nanoid/async/index.cjs +71 -0
- package/templates/vanilla/node_modules/nanoid/async/index.d.ts +56 -0
- package/templates/vanilla/node_modules/nanoid/async/index.js +35 -0
- package/templates/vanilla/node_modules/nanoid/async/index.native.js +26 -0
- package/templates/vanilla/node_modules/nanoid/async/package.json +12 -0
- package/templates/vanilla/node_modules/nanoid/bin/nanoid.cjs +55 -0
- package/templates/vanilla/node_modules/nanoid/index.browser.cjs +72 -0
- package/templates/vanilla/node_modules/nanoid/index.browser.js +34 -0
- package/templates/vanilla/node_modules/nanoid/index.cjs +85 -0
- package/templates/vanilla/node_modules/nanoid/index.d.cts +91 -0
- package/templates/vanilla/node_modules/nanoid/index.d.ts +91 -0
- package/templates/vanilla/node_modules/nanoid/index.js +45 -0
- package/templates/vanilla/node_modules/nanoid/nanoid.js +1 -0
- package/templates/vanilla/node_modules/nanoid/non-secure/index.cjs +34 -0
- package/templates/vanilla/node_modules/nanoid/non-secure/index.d.ts +33 -0
- package/templates/vanilla/node_modules/nanoid/non-secure/index.js +21 -0
- package/templates/vanilla/node_modules/nanoid/non-secure/package.json +6 -0
- package/templates/vanilla/node_modules/nanoid/package.json +89 -0
- package/templates/vanilla/node_modules/nanoid/url-alphabet/index.cjs +7 -0
- package/templates/vanilla/node_modules/nanoid/url-alphabet/index.js +3 -0
- package/templates/vanilla/node_modules/nanoid/url-alphabet/package.json +6 -0
- package/templates/vanilla/node_modules/picocolors/LICENSE +15 -0
- package/templates/vanilla/node_modules/picocolors/README.md +21 -0
- package/templates/vanilla/node_modules/picocolors/package.json +25 -0
- package/templates/vanilla/node_modules/picocolors/picocolors.browser.js +4 -0
- package/templates/vanilla/node_modules/picocolors/picocolors.d.ts +5 -0
- package/templates/vanilla/node_modules/picocolors/picocolors.js +75 -0
- package/templates/vanilla/node_modules/picocolors/types.d.ts +51 -0
- package/templates/vanilla/node_modules/postcss/LICENSE +20 -0
- package/templates/vanilla/node_modules/postcss/README.md +29 -0
- package/templates/vanilla/node_modules/postcss/lib/at-rule.d.ts +140 -0
- package/templates/vanilla/node_modules/postcss/lib/at-rule.js +25 -0
- package/templates/vanilla/node_modules/postcss/lib/comment.d.ts +68 -0
- package/templates/vanilla/node_modules/postcss/lib/comment.js +13 -0
- package/templates/vanilla/node_modules/postcss/lib/container.d.ts +483 -0
- package/templates/vanilla/node_modules/postcss/lib/container.js +447 -0
- package/templates/vanilla/node_modules/postcss/lib/css-syntax-error.d.ts +248 -0
- package/templates/vanilla/node_modules/postcss/lib/css-syntax-error.js +133 -0
- package/templates/vanilla/node_modules/postcss/lib/declaration.d.ts +151 -0
- package/templates/vanilla/node_modules/postcss/lib/declaration.js +24 -0
- package/templates/vanilla/node_modules/postcss/lib/document.d.ts +69 -0
- package/templates/vanilla/node_modules/postcss/lib/document.js +33 -0
- package/templates/vanilla/node_modules/postcss/lib/fromJSON.d.ts +9 -0
- package/templates/vanilla/node_modules/postcss/lib/fromJSON.js +54 -0
- package/templates/vanilla/node_modules/postcss/lib/input.d.ts +227 -0
- package/templates/vanilla/node_modules/postcss/lib/input.js +265 -0
- package/templates/vanilla/node_modules/postcss/lib/lazy-result.d.ts +190 -0
- package/templates/vanilla/node_modules/postcss/lib/lazy-result.js +550 -0
- package/templates/vanilla/node_modules/postcss/lib/list.d.ts +60 -0
- package/templates/vanilla/node_modules/postcss/lib/list.js +58 -0
- package/templates/vanilla/node_modules/postcss/lib/map-generator.js +368 -0
- package/templates/vanilla/node_modules/postcss/lib/no-work-result.d.ts +46 -0
- package/templates/vanilla/node_modules/postcss/lib/no-work-result.js +138 -0
- package/templates/vanilla/node_modules/postcss/lib/node.d.ts +556 -0
- package/templates/vanilla/node_modules/postcss/lib/node.js +449 -0
- package/templates/vanilla/node_modules/postcss/lib/parse.d.ts +9 -0
- package/templates/vanilla/node_modules/postcss/lib/parse.js +42 -0
- package/templates/vanilla/node_modules/postcss/lib/parser.js +611 -0
- package/templates/vanilla/node_modules/postcss/lib/postcss.d.mts +69 -0
- package/templates/vanilla/node_modules/postcss/lib/postcss.d.ts +458 -0
- package/templates/vanilla/node_modules/postcss/lib/postcss.js +101 -0
- package/templates/vanilla/node_modules/postcss/lib/postcss.mjs +30 -0
- package/templates/vanilla/node_modules/postcss/lib/previous-map.d.ts +81 -0
- package/templates/vanilla/node_modules/postcss/lib/previous-map.js +144 -0
- package/templates/vanilla/node_modules/postcss/lib/processor.d.ts +115 -0
- package/templates/vanilla/node_modules/postcss/lib/processor.js +67 -0
- package/templates/vanilla/node_modules/postcss/lib/result.d.ts +205 -0
- package/templates/vanilla/node_modules/postcss/lib/result.js +42 -0
- package/templates/vanilla/node_modules/postcss/lib/root.d.ts +87 -0
- package/templates/vanilla/node_modules/postcss/lib/root.js +61 -0
- package/templates/vanilla/node_modules/postcss/lib/rule.d.ts +126 -0
- package/templates/vanilla/node_modules/postcss/lib/rule.js +27 -0
- package/templates/vanilla/node_modules/postcss/lib/stringifier.d.ts +46 -0
- package/templates/vanilla/node_modules/postcss/lib/stringifier.js +353 -0
- package/templates/vanilla/node_modules/postcss/lib/stringify.d.ts +9 -0
- package/templates/vanilla/node_modules/postcss/lib/stringify.js +11 -0
- package/templates/vanilla/node_modules/postcss/lib/symbols.js +5 -0
- package/templates/vanilla/node_modules/postcss/lib/terminal-highlight.js +70 -0
- package/templates/vanilla/node_modules/postcss/lib/tokenize.js +266 -0
- package/templates/vanilla/node_modules/postcss/lib/warn-once.js +13 -0
- package/templates/vanilla/node_modules/postcss/lib/warning.d.ts +147 -0
- package/templates/vanilla/node_modules/postcss/lib/warning.js +37 -0
- package/templates/vanilla/node_modules/postcss/package.json +88 -0
- package/templates/vanilla/node_modules/rollup/LICENSE.md +695 -0
- package/templates/vanilla/node_modules/rollup/README.md +125 -0
- package/templates/vanilla/node_modules/rollup/dist/bin/rollup +1715 -0
- package/templates/vanilla/node_modules/rollup/dist/es/getLogFilter.js +64 -0
- package/templates/vanilla/node_modules/rollup/dist/es/package.json +1 -0
- package/templates/vanilla/node_modules/rollup/dist/es/rollup.js +17 -0
- package/templates/vanilla/node_modules/rollup/dist/es/shared/node-entry.js +27273 -0
- package/templates/vanilla/node_modules/rollup/dist/es/shared/watch.js +4857 -0
- package/templates/vanilla/node_modules/rollup/dist/getLogFilter.d.ts +5 -0
- package/templates/vanilla/node_modules/rollup/dist/getLogFilter.js +69 -0
- package/templates/vanilla/node_modules/rollup/dist/loadConfigFile.d.ts +20 -0
- package/templates/vanilla/node_modules/rollup/dist/loadConfigFile.js +29 -0
- package/templates/vanilla/node_modules/rollup/dist/rollup.d.ts +1012 -0
- package/templates/vanilla/node_modules/rollup/dist/rollup.js +31 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/fsevents-importer.js +37 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/index.js +4571 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/loadConfigFile.js +546 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/rollup.js +27351 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/watch-cli.js +561 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/watch-proxy.js +87 -0
- package/templates/vanilla/node_modules/rollup/dist/shared/watch.js +316 -0
- package/templates/vanilla/node_modules/rollup/package.json +181 -0
- package/templates/vanilla/node_modules/source-map-js/LICENSE +28 -0
- package/templates/vanilla/node_modules/source-map-js/README.md +765 -0
- package/templates/vanilla/node_modules/source-map-js/lib/array-set.js +121 -0
- package/templates/vanilla/node_modules/source-map-js/lib/base64-vlq.js +140 -0
- package/templates/vanilla/node_modules/source-map-js/lib/base64.js +67 -0
- package/templates/vanilla/node_modules/source-map-js/lib/binary-search.js +111 -0
- package/templates/vanilla/node_modules/source-map-js/lib/mapping-list.js +79 -0
- package/templates/vanilla/node_modules/source-map-js/lib/quick-sort.js +132 -0
- package/templates/vanilla/node_modules/source-map-js/lib/source-map-consumer.d.ts +1 -0
- package/templates/vanilla/node_modules/source-map-js/lib/source-map-consumer.js +1188 -0
- package/templates/vanilla/node_modules/source-map-js/lib/source-map-generator.d.ts +1 -0
- package/templates/vanilla/node_modules/source-map-js/lib/source-map-generator.js +444 -0
- package/templates/vanilla/node_modules/source-map-js/lib/source-node.d.ts +1 -0
- package/templates/vanilla/node_modules/source-map-js/lib/source-node.js +413 -0
- package/templates/vanilla/node_modules/source-map-js/lib/util.js +594 -0
- package/templates/vanilla/node_modules/source-map-js/package.json +71 -0
- package/templates/vanilla/node_modules/source-map-js/source-map.d.ts +104 -0
- package/templates/vanilla/node_modules/source-map-js/source-map.js +8 -0
- package/templates/vanilla/node_modules/vite/LICENSE.md +3396 -0
- package/templates/vanilla/node_modules/vite/README.md +20 -0
- package/templates/vanilla/node_modules/vite/bin/openChrome.applescript +95 -0
- package/templates/vanilla/node_modules/vite/bin/vite.js +61 -0
- package/templates/vanilla/node_modules/vite/client.d.ts +281 -0
- package/templates/vanilla/node_modules/vite/dist/client/client.mjs +725 -0
- package/templates/vanilla/node_modules/vite/dist/client/client.mjs.map +1 -0
- package/templates/vanilla/node_modules/vite/dist/client/env.mjs +30 -0
- package/templates/vanilla/node_modules/vite/dist/client/env.mjs.map +1 -0
- package/templates/vanilla/node_modules/vite/dist/node/chunks/dep-7ec6f216.js +914 -0
- package/templates/vanilla/node_modules/vite/dist/node/chunks/dep-827b23df.js +66713 -0
- package/templates/vanilla/node_modules/vite/dist/node/chunks/dep-c423598f.js +561 -0
- package/templates/vanilla/node_modules/vite/dist/node/chunks/dep-f0c7dae0.js +7930 -0
- package/templates/vanilla/node_modules/vite/dist/node/chunks/dep-f1e8587f.js +7646 -0
- package/templates/vanilla/node_modules/vite/dist/node/cli.js +929 -0
- package/templates/vanilla/node_modules/vite/dist/node/constants.js +130 -0
- package/templates/vanilla/node_modules/vite/dist/node/index.d.ts +3548 -0
- package/templates/vanilla/node_modules/vite/dist/node/index.js +158 -0
- package/templates/vanilla/node_modules/vite/dist/node-cjs/publicUtils.cjs +4555 -0
- package/templates/vanilla/node_modules/vite/index.cjs +34 -0
- package/templates/vanilla/node_modules/vite/package.json +173 -0
- package/templates/vanilla/node_modules/vite/types/customEvent.d.ts +35 -0
- package/templates/vanilla/node_modules/vite/types/hmrPayload.d.ts +61 -0
- package/templates/vanilla/node_modules/vite/types/hot.d.ts +32 -0
- package/templates/vanilla/node_modules/vite/types/importGlob.d.ts +97 -0
- package/templates/vanilla/node_modules/vite/types/importMeta.d.ts +26 -0
- package/templates/vanilla/node_modules/vite/types/metadata.d.ts +10 -0
- package/templates/vanilla/node_modules/vite/types/package.json +4 -0
- package/templates/vanilla/package-lock.json +589 -0
- package/templates/vanilla/package.json +17 -0
- package/templates/vanilla/src/components/App.js +60 -0
- package/templates/vanilla/src/components/Card.js +21 -0
- package/templates/vanilla/src/components/Hero.js +15 -0
- package/templates/vanilla/src/components/InteractiveDemo.js +59 -0
- package/templates/vanilla/src/main.js +9 -0
- package/templates/vanilla/src/style/main.css +172 -0
- package/templates/vanilla/vite.config.js +8 -0
- package/templates/web3/.env.example +15 -0
- package/templates/web3/.github/workflows/deploy.yml +38 -0
- package/templates/web3/README.md +33 -0
- package/templates/web3/contracts/foundry.toml +11 -0
- package/templates/web3/contracts/script/Deploy.s.sol +13 -0
- package/templates/web3/contracts/src/Counter.sol +21 -0
- package/templates/web3/index.html +13 -0
- package/templates/web3/package.json +25 -0
- package/templates/web3/scripts/chain-start.mjs +305 -0
- package/templates/web3/scripts/chain-stop.mjs +34 -0
- package/templates/web3/scripts/deploy.mjs +155 -0
- package/templates/web3/scripts/setup.mjs +42 -0
- package/templates/web3/src/components/App.js +49 -0
- package/templates/web3/src/components/CounterCard.js +111 -0
- package/templates/web3/src/main.js +54 -0
- package/templates/web3/src/style/main.css +345 -0
- package/templates/web3/vite.config.js +29 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "esbuild",
|
|
3
|
+
"version": "0.18.20",
|
|
4
|
+
"description": "An extremely fast JavaScript and CSS bundler and minifier.",
|
|
5
|
+
"repository": "https://github.com/evanw/esbuild",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"postinstall": "node install.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "lib/main.js",
|
|
10
|
+
"types": "lib/main.d.ts",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=12"
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"esbuild": "bin/esbuild"
|
|
16
|
+
},
|
|
17
|
+
"optionalDependencies": {
|
|
18
|
+
"@esbuild/android-arm": "0.18.20",
|
|
19
|
+
"@esbuild/android-arm64": "0.18.20",
|
|
20
|
+
"@esbuild/android-x64": "0.18.20",
|
|
21
|
+
"@esbuild/darwin-arm64": "0.18.20",
|
|
22
|
+
"@esbuild/darwin-x64": "0.18.20",
|
|
23
|
+
"@esbuild/freebsd-arm64": "0.18.20",
|
|
24
|
+
"@esbuild/freebsd-x64": "0.18.20",
|
|
25
|
+
"@esbuild/linux-arm": "0.18.20",
|
|
26
|
+
"@esbuild/linux-arm64": "0.18.20",
|
|
27
|
+
"@esbuild/linux-ia32": "0.18.20",
|
|
28
|
+
"@esbuild/linux-loong64": "0.18.20",
|
|
29
|
+
"@esbuild/linux-mips64el": "0.18.20",
|
|
30
|
+
"@esbuild/linux-ppc64": "0.18.20",
|
|
31
|
+
"@esbuild/linux-riscv64": "0.18.20",
|
|
32
|
+
"@esbuild/linux-s390x": "0.18.20",
|
|
33
|
+
"@esbuild/linux-x64": "0.18.20",
|
|
34
|
+
"@esbuild/netbsd-x64": "0.18.20",
|
|
35
|
+
"@esbuild/openbsd-x64": "0.18.20",
|
|
36
|
+
"@esbuild/sunos-x64": "0.18.20",
|
|
37
|
+
"@esbuild/win32-arm64": "0.18.20",
|
|
38
|
+
"@esbuild/win32-ia32": "0.18.20",
|
|
39
|
+
"@esbuild/win32-x64": "0.18.20"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT"
|
|
42
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright 2017 Andrey Sitnik <andrey@sitnik.ru>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Nano ID
|
|
2
|
+
|
|
3
|
+
<img src="https://ai.github.io/nanoid/logo.svg" align="right"
|
|
4
|
+
alt="Nano ID logo by Anton Lovchikov" width="180" height="94">
|
|
5
|
+
|
|
6
|
+
**English** | [Русский](./README.ru.md) | [简体中文](./README.zh-CN.md) | [Bahasa Indonesia](./README.id-ID.md)
|
|
7
|
+
|
|
8
|
+
A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
|
|
9
|
+
|
|
10
|
+
> “An amazing level of senseless perfectionism,
|
|
11
|
+
> which is simply impossible not to respect.”
|
|
12
|
+
|
|
13
|
+
* **Small.** 130 bytes (minified and gzipped). No dependencies.
|
|
14
|
+
[Size Limit] controls the size.
|
|
15
|
+
* **Fast.** It is 2 times faster than UUID.
|
|
16
|
+
* **Safe.** It uses hardware random generator. Can be used in clusters.
|
|
17
|
+
* **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
|
|
18
|
+
So ID size was reduced from 36 to 21 symbols.
|
|
19
|
+
* **Portable.** Nano ID was ported
|
|
20
|
+
to [20 programming languages](#other-programming-languages).
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { nanoid } from 'nanoid'
|
|
24
|
+
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Supports modern browsers, IE [with Babel], Node.js and React Native.
|
|
28
|
+
|
|
29
|
+
[online tool]: https://gitpod.io/#https://github.com/ai/nanoid/
|
|
30
|
+
[with Babel]: https://developer.epages.com/blog/coding/how-to-transpile-node-modules-with-babel-and-webpack-in-a-monorepo/
|
|
31
|
+
[Size Limit]: https://github.com/ai/size-limit
|
|
32
|
+
|
|
33
|
+
<a href="https://evilmartians.com/?utm_source=nanoid">
|
|
34
|
+
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
|
|
35
|
+
alt="Sponsored by Evil Martians" width="236" height="54">
|
|
36
|
+
</a>
|
|
37
|
+
|
|
38
|
+
## Docs
|
|
39
|
+
Read full docs **[here](https://github.com/ai/nanoid#readme)**.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
2
|
+
|
|
3
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
4
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
5
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
6
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
7
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
8
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
|
9
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
10
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
11
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
12
|
+
// the random bytes redundancy has to be satisfied.
|
|
13
|
+
|
|
14
|
+
// Note: every hardware random generator call is performance expensive,
|
|
15
|
+
// because the system call for entropy collection takes a lot of time.
|
|
16
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
17
|
+
|
|
18
|
+
// Next, a step determines how many random bytes to generate.
|
|
19
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
20
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
21
|
+
// according to benchmarks).
|
|
22
|
+
|
|
23
|
+
// `-~f => Math.ceil(f)` if f is a float
|
|
24
|
+
// `-~i => i + 1` if i is an integer
|
|
25
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
26
|
+
|
|
27
|
+
return async (size = defaultSize) => {
|
|
28
|
+
let id = ''
|
|
29
|
+
while (true) {
|
|
30
|
+
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
|
31
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
32
|
+
let i = step | 0
|
|
33
|
+
while (i--) {
|
|
34
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
35
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
36
|
+
if (id.length === size) return id
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let nanoid = async (size = 21) => {
|
|
43
|
+
let id = ''
|
|
44
|
+
let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
|
|
45
|
+
|
|
46
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
47
|
+
while (size--) {
|
|
48
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
49
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
50
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
51
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
52
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
53
|
+
let byte = bytes[size] & 63
|
|
54
|
+
if (byte < 36) {
|
|
55
|
+
// `0-9a-z`
|
|
56
|
+
id += byte.toString(36)
|
|
57
|
+
} else if (byte < 62) {
|
|
58
|
+
// `A-Z`
|
|
59
|
+
id += (byte - 26).toString(36).toUpperCase()
|
|
60
|
+
} else if (byte < 63) {
|
|
61
|
+
id += '_'
|
|
62
|
+
} else {
|
|
63
|
+
id += '-'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return id
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
2
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
3
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
4
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
5
|
+
return async (size = defaultSize) => {
|
|
6
|
+
let id = ''
|
|
7
|
+
while (true) {
|
|
8
|
+
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
|
9
|
+
let i = step | 0
|
|
10
|
+
while (i--) {
|
|
11
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
12
|
+
if (id.length === size) return id
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let nanoid = async (size = 21) => {
|
|
18
|
+
let id = ''
|
|
19
|
+
let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
|
|
20
|
+
while (size--) {
|
|
21
|
+
let byte = bytes[size] & 63
|
|
22
|
+
if (byte < 36) {
|
|
23
|
+
id += byte.toString(36)
|
|
24
|
+
} else if (byte < 62) {
|
|
25
|
+
id += (byte - 26).toString(36).toUpperCase()
|
|
26
|
+
} else if (byte < 63) {
|
|
27
|
+
id += '_'
|
|
28
|
+
} else {
|
|
29
|
+
id += '-'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return id
|
|
33
|
+
}
|
|
34
|
+
export { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
let crypto = require('crypto')
|
|
2
|
+
|
|
3
|
+
let { urlAlphabet } = require('../url-alphabet/index.cjs')
|
|
4
|
+
|
|
5
|
+
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
|
6
|
+
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
|
7
|
+
let random = bytes =>
|
|
8
|
+
new Promise((resolve, reject) => {
|
|
9
|
+
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
|
10
|
+
// Memory flushing is unnecessary since the buffer allocation itself resets
|
|
11
|
+
// the memory with the new bytes.
|
|
12
|
+
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
13
|
+
if (err) {
|
|
14
|
+
reject(err)
|
|
15
|
+
} else {
|
|
16
|
+
resolve(buf)
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
22
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
23
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
24
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
25
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
26
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
27
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
28
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
29
|
+
// the random bytes redundancy has to be satisfied.
|
|
30
|
+
|
|
31
|
+
// Note: every hardware random generator call is performance expensive,
|
|
32
|
+
// because the system call for entropy collection takes a lot of time.
|
|
33
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
34
|
+
|
|
35
|
+
// Next, a step determines how many random bytes to generate.
|
|
36
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
37
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
38
|
+
// according to benchmarks).
|
|
39
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
40
|
+
|
|
41
|
+
let tick = (id, size = defaultSize) =>
|
|
42
|
+
random(step).then(bytes => {
|
|
43
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
44
|
+
let i = step
|
|
45
|
+
while (i--) {
|
|
46
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
47
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
48
|
+
if (id.length >= size) return id
|
|
49
|
+
}
|
|
50
|
+
return tick(id, size)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
return size => tick('', size)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let nanoid = (size = 21) =>
|
|
57
|
+
random((size |= 0)).then(bytes => {
|
|
58
|
+
let id = ''
|
|
59
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
60
|
+
while (size--) {
|
|
61
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
62
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
63
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
64
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
65
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
66
|
+
id += urlAlphabet[bytes[size] & 63]
|
|
67
|
+
}
|
|
68
|
+
return id
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
module.exports = { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate secure URL-friendly unique ID. The non-blocking version.
|
|
3
|
+
*
|
|
4
|
+
* By default, the ID will have 21 symbols to have a collision probability
|
|
5
|
+
* similar to UUID v4.
|
|
6
|
+
*
|
|
7
|
+
* ```js
|
|
8
|
+
* import { nanoid } from 'nanoid/async'
|
|
9
|
+
* nanoid().then(id => {
|
|
10
|
+
* model.id = id
|
|
11
|
+
* })
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @param size Size of the ID. The default size is 21.
|
|
15
|
+
* @returns A promise with a random string.
|
|
16
|
+
*/
|
|
17
|
+
export function nanoid(size?: number): Promise<string>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A low-level function.
|
|
21
|
+
* Generate secure unique ID with custom alphabet. The non-blocking version.
|
|
22
|
+
*
|
|
23
|
+
* Alphabet must contain 256 symbols or less. Otherwise, the generator
|
|
24
|
+
* will not be secure.
|
|
25
|
+
*
|
|
26
|
+
* @param alphabet Alphabet used to generate the ID.
|
|
27
|
+
* @param defaultSize Size of the ID. The default size is 21.
|
|
28
|
+
* @returns A function that returns a promise with a random string.
|
|
29
|
+
*
|
|
30
|
+
* ```js
|
|
31
|
+
* import { customAlphabet } from 'nanoid/async'
|
|
32
|
+
* const nanoid = customAlphabet('0123456789абвгдеё', 5)
|
|
33
|
+
* nanoid().then(id => {
|
|
34
|
+
* model.id = id //=> "8ё56а"
|
|
35
|
+
* })
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function customAlphabet(
|
|
39
|
+
alphabet: string,
|
|
40
|
+
defaultSize?: number
|
|
41
|
+
): (size?: number) => Promise<string>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Generate an array of random bytes collected from hardware noise.
|
|
45
|
+
*
|
|
46
|
+
* ```js
|
|
47
|
+
* import { random } from 'nanoid/async'
|
|
48
|
+
* random(5).then(bytes => {
|
|
49
|
+
* bytes //=> [10, 67, 212, 67, 89]
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @param bytes Size of the array.
|
|
54
|
+
* @returns A promise with a random bytes array.
|
|
55
|
+
*/
|
|
56
|
+
export function random(bytes: number): Promise<Uint8Array>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import crypto from 'crypto'
|
|
2
|
+
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
3
|
+
let random = bytes =>
|
|
4
|
+
new Promise((resolve, reject) => {
|
|
5
|
+
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
6
|
+
if (err) {
|
|
7
|
+
reject(err)
|
|
8
|
+
} else {
|
|
9
|
+
resolve(buf)
|
|
10
|
+
}
|
|
11
|
+
})
|
|
12
|
+
})
|
|
13
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
14
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
15
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
16
|
+
let tick = (id, size = defaultSize) =>
|
|
17
|
+
random(step).then(bytes => {
|
|
18
|
+
let i = step
|
|
19
|
+
while (i--) {
|
|
20
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
21
|
+
if (id.length >= size) return id
|
|
22
|
+
}
|
|
23
|
+
return tick(id, size)
|
|
24
|
+
})
|
|
25
|
+
return size => tick('', size)
|
|
26
|
+
}
|
|
27
|
+
let nanoid = (size = 21) =>
|
|
28
|
+
random((size |= 0)).then(bytes => {
|
|
29
|
+
let id = ''
|
|
30
|
+
while (size--) {
|
|
31
|
+
id += urlAlphabet[bytes[size] & 63]
|
|
32
|
+
}
|
|
33
|
+
return id
|
|
34
|
+
})
|
|
35
|
+
export { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getRandomBytesAsync } from 'expo-random'
|
|
2
|
+
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
3
|
+
let random = getRandomBytesAsync
|
|
4
|
+
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
5
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
6
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
7
|
+
let tick = (id, size = defaultSize) =>
|
|
8
|
+
random(step).then(bytes => {
|
|
9
|
+
let i = step
|
|
10
|
+
while (i--) {
|
|
11
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
12
|
+
if (id.length >= size) return id
|
|
13
|
+
}
|
|
14
|
+
return tick(id, size)
|
|
15
|
+
})
|
|
16
|
+
return size => tick('', size)
|
|
17
|
+
}
|
|
18
|
+
let nanoid = (size = 21) =>
|
|
19
|
+
random((size |= 0)).then(bytes => {
|
|
20
|
+
let id = ''
|
|
21
|
+
while (size--) {
|
|
22
|
+
id += urlAlphabet[bytes[size] & 63]
|
|
23
|
+
}
|
|
24
|
+
return id
|
|
25
|
+
})
|
|
26
|
+
export { nanoid, customAlphabet, random }
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
let { nanoid, customAlphabet } = require('..')
|
|
4
|
+
|
|
5
|
+
function print(msg) {
|
|
6
|
+
process.stdout.write(msg + '\n')
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function error(msg) {
|
|
10
|
+
process.stderr.write(msg + '\n')
|
|
11
|
+
process.exit(1)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
15
|
+
print(`
|
|
16
|
+
Usage
|
|
17
|
+
$ nanoid [options]
|
|
18
|
+
|
|
19
|
+
Options
|
|
20
|
+
-s, --size Generated ID size
|
|
21
|
+
-a, --alphabet Alphabet to use
|
|
22
|
+
-h, --help Show this help
|
|
23
|
+
|
|
24
|
+
Examples
|
|
25
|
+
$ nanoid --s 15
|
|
26
|
+
S9sBF77U6sDB8Yg
|
|
27
|
+
|
|
28
|
+
$ nanoid --size 10 --alphabet abc
|
|
29
|
+
bcabababca`)
|
|
30
|
+
process.exit()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let alphabet, size
|
|
34
|
+
for (let i = 2; i < process.argv.length; i++) {
|
|
35
|
+
let arg = process.argv[i]
|
|
36
|
+
if (arg === '--size' || arg === '-s') {
|
|
37
|
+
size = Number(process.argv[i + 1])
|
|
38
|
+
i += 1
|
|
39
|
+
if (Number.isNaN(size) || size <= 0) {
|
|
40
|
+
error('Size must be positive integer')
|
|
41
|
+
}
|
|
42
|
+
} else if (arg === '--alphabet' || arg === '-a') {
|
|
43
|
+
alphabet = process.argv[i + 1]
|
|
44
|
+
i += 1
|
|
45
|
+
} else {
|
|
46
|
+
error('Unknown argument ' + arg)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (alphabet) {
|
|
51
|
+
let customNanoid = customAlphabet(alphabet, size)
|
|
52
|
+
print(customNanoid())
|
|
53
|
+
} else {
|
|
54
|
+
print(nanoid(size))
|
|
55
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// This file replaces `index.js` in bundlers like webpack or Rollup,
|
|
2
|
+
// according to `browser` config in `package.json`.
|
|
3
|
+
|
|
4
|
+
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
|
5
|
+
|
|
6
|
+
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
7
|
+
|
|
8
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
9
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
10
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
11
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
12
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
13
|
+
// `Math.clz32` is not used, because it is not available in browsers.
|
|
14
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
15
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
16
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
17
|
+
// the random bytes redundancy has to be satisfied.
|
|
18
|
+
|
|
19
|
+
// Note: every hardware random generator call is performance expensive,
|
|
20
|
+
// because the system call for entropy collection takes a lot of time.
|
|
21
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
22
|
+
|
|
23
|
+
// Next, a step determines how many random bytes to generate.
|
|
24
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
25
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
26
|
+
// according to benchmarks).
|
|
27
|
+
|
|
28
|
+
// `-~f => Math.ceil(f)` if f is a float
|
|
29
|
+
// `-~i => i + 1` if i is an integer
|
|
30
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
31
|
+
|
|
32
|
+
return (size = defaultSize) => {
|
|
33
|
+
let id = ''
|
|
34
|
+
while (true) {
|
|
35
|
+
let bytes = getRandom(step)
|
|
36
|
+
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
37
|
+
let j = step | 0
|
|
38
|
+
while (j--) {
|
|
39
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
40
|
+
id += alphabet[bytes[j] & mask] || ''
|
|
41
|
+
if (id.length === size) return id
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let customAlphabet = (alphabet, size = 21) =>
|
|
48
|
+
customRandom(alphabet, size, random)
|
|
49
|
+
|
|
50
|
+
let nanoid = (size = 21) =>
|
|
51
|
+
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
52
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
53
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
54
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
55
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
56
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
57
|
+
byte &= 63
|
|
58
|
+
if (byte < 36) {
|
|
59
|
+
// `0-9a-z`
|
|
60
|
+
id += byte.toString(36)
|
|
61
|
+
} else if (byte < 62) {
|
|
62
|
+
// `A-Z`
|
|
63
|
+
id += (byte - 26).toString(36).toUpperCase()
|
|
64
|
+
} else if (byte > 62) {
|
|
65
|
+
id += '-'
|
|
66
|
+
} else {
|
|
67
|
+
id += '_'
|
|
68
|
+
}
|
|
69
|
+
return id
|
|
70
|
+
}, '')
|
|
71
|
+
|
|
72
|
+
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { urlAlphabet } from './url-alphabet/index.js'
|
|
2
|
+
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
3
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
4
|
+
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
5
|
+
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
6
|
+
return (size = defaultSize) => {
|
|
7
|
+
let id = ''
|
|
8
|
+
while (true) {
|
|
9
|
+
let bytes = getRandom(step)
|
|
10
|
+
let j = step | 0
|
|
11
|
+
while (j--) {
|
|
12
|
+
id += alphabet[bytes[j] & mask] || ''
|
|
13
|
+
if (id.length === size) return id
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
let customAlphabet = (alphabet, size = 21) =>
|
|
19
|
+
customRandom(alphabet, size, random)
|
|
20
|
+
let nanoid = (size = 21) =>
|
|
21
|
+
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
22
|
+
byte &= 63
|
|
23
|
+
if (byte < 36) {
|
|
24
|
+
id += byte.toString(36)
|
|
25
|
+
} else if (byte < 62) {
|
|
26
|
+
id += (byte - 26).toString(36).toUpperCase()
|
|
27
|
+
} else if (byte > 62) {
|
|
28
|
+
id += '-'
|
|
29
|
+
} else {
|
|
30
|
+
id += '_'
|
|
31
|
+
}
|
|
32
|
+
return id
|
|
33
|
+
}, '')
|
|
34
|
+
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
let crypto = require('crypto')
|
|
2
|
+
|
|
3
|
+
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
|
4
|
+
|
|
5
|
+
// It is best to make fewer, larger requests to the crypto module to
|
|
6
|
+
// avoid system call overhead. So, random numbers are generated in a
|
|
7
|
+
// pool. The pool is a Buffer that is larger than the initial random
|
|
8
|
+
// request size by this multiplier. The pool is enlarged if subsequent
|
|
9
|
+
// requests exceed the maximum buffer size.
|
|
10
|
+
const POOL_SIZE_MULTIPLIER = 128
|
|
11
|
+
let pool, poolOffset
|
|
12
|
+
|
|
13
|
+
let fillPool = bytes => {
|
|
14
|
+
if (!pool || pool.length < bytes) {
|
|
15
|
+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
|
|
16
|
+
crypto.randomFillSync(pool)
|
|
17
|
+
poolOffset = 0
|
|
18
|
+
} else if (poolOffset + bytes > pool.length) {
|
|
19
|
+
crypto.randomFillSync(pool)
|
|
20
|
+
poolOffset = 0
|
|
21
|
+
}
|
|
22
|
+
poolOffset += bytes
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let random = bytes => {
|
|
26
|
+
// `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution
|
|
27
|
+
fillPool((bytes |= 0))
|
|
28
|
+
return pool.subarray(poolOffset - bytes, poolOffset)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
32
|
+
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
33
|
+
// values closer to the alphabet size. The bitmask calculates the closest
|
|
34
|
+
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
35
|
+
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
36
|
+
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
37
|
+
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
38
|
+
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
39
|
+
// the random bytes redundancy has to be satisfied.
|
|
40
|
+
|
|
41
|
+
// Note: every hardware random generator call is performance expensive,
|
|
42
|
+
// because the system call for entropy collection takes a lot of time.
|
|
43
|
+
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
44
|
+
|
|
45
|
+
// Next, a step determines how many random bytes to generate.
|
|
46
|
+
// The number of random bytes gets decided upon the ID size, mask,
|
|
47
|
+
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
48
|
+
// according to benchmarks).
|
|
49
|
+
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
50
|
+
|
|
51
|
+
return (size = defaultSize) => {
|
|
52
|
+
let id = ''
|
|
53
|
+
while (true) {
|
|
54
|
+
let bytes = getRandom(step)
|
|
55
|
+
// A compact alternative for `for (let i = 0; i < step; i++)`.
|
|
56
|
+
let i = step
|
|
57
|
+
while (i--) {
|
|
58
|
+
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
59
|
+
id += alphabet[bytes[i] & mask] || ''
|
|
60
|
+
if (id.length === size) return id
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let customAlphabet = (alphabet, size = 21) =>
|
|
67
|
+
customRandom(alphabet, size, random)
|
|
68
|
+
|
|
69
|
+
let nanoid = (size = 21) => {
|
|
70
|
+
// `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution
|
|
71
|
+
fillPool((size |= 0))
|
|
72
|
+
let id = ''
|
|
73
|
+
// We are reading directly from the random pool to avoid creating new array
|
|
74
|
+
for (let i = poolOffset - size; i < poolOffset; i++) {
|
|
75
|
+
// It is incorrect to use bytes exceeding the alphabet size.
|
|
76
|
+
// The following mask reduces the random byte in the 0-255 value
|
|
77
|
+
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
78
|
+
// as empty string fallback or magic numbers, is unneccessary because
|
|
79
|
+
// the bitmask trims bytes down to the alphabet size.
|
|
80
|
+
id += urlAlphabet[pool[i] & 63]
|
|
81
|
+
}
|
|
82
|
+
return id
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|