Spaces:
Paused
Paused
Upload folder using huggingface_hub (part 7)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +2 -0
- node_modules/@eslint-community/eslint-utils/package.json +89 -0
- node_modules/@eslint-community/regexpp/LICENSE +21 -0
- node_modules/@eslint-community/regexpp/README.md +177 -0
- node_modules/@eslint-community/regexpp/index.d.ts +1163 -0
- node_modules/@eslint-community/regexpp/index.js +0 -0
- node_modules/@eslint-community/regexpp/index.js.map +0 -0
- node_modules/@eslint-community/regexpp/index.mjs +0 -0
- node_modules/@eslint-community/regexpp/index.mjs.map +0 -0
- node_modules/@eslint-community/regexpp/package.json +91 -0
- node_modules/@exodus/bytes/LICENSE +21 -0
- node_modules/@exodus/bytes/README.md +818 -0
- node_modules/@exodus/bytes/array.d.ts +89 -0
- node_modules/@exodus/bytes/array.js +35 -0
- node_modules/@exodus/bytes/assert.js +26 -0
- node_modules/@exodus/bytes/base32.d.ts +112 -0
- node_modules/@exodus/bytes/base32.js +37 -0
- node_modules/@exodus/bytes/base58.d.ts +64 -0
- node_modules/@exodus/bytes/base58.js +219 -0
- node_modules/@exodus/bytes/base58check.d.ts +135 -0
- node_modules/@exodus/bytes/base58check.js +18 -0
- node_modules/@exodus/bytes/base58check.node.js +14 -0
- node_modules/@exodus/bytes/base64.d.ts +102 -0
- node_modules/@exodus/bytes/base64.js +181 -0
- node_modules/@exodus/bytes/bech32.d.ts +76 -0
- node_modules/@exodus/bytes/bech32.js +257 -0
- node_modules/@exodus/bytes/bigint.d.ts +49 -0
- node_modules/@exodus/bytes/bigint.js +14 -0
- node_modules/@exodus/bytes/encoding-browser.browser.js +55 -0
- node_modules/@exodus/bytes/encoding-browser.d.ts +35 -0
- node_modules/@exodus/bytes/encoding-browser.js +1 -0
- node_modules/@exodus/bytes/encoding-browser.native.js +10 -0
- node_modules/@exodus/bytes/encoding-lite.d.ts +63 -0
- node_modules/@exodus/bytes/encoding-lite.js +12 -0
- node_modules/@exodus/bytes/encoding.d.ts +169 -0
- node_modules/@exodus/bytes/encoding.js +18 -0
- node_modules/@exodus/bytes/fallback/_utils.js +60 -0
- node_modules/@exodus/bytes/fallback/base32.js +242 -0
- node_modules/@exodus/bytes/fallback/base58check.js +52 -0
- node_modules/@exodus/bytes/fallback/base64.js +191 -0
- node_modules/@exodus/bytes/fallback/encoding.api.js +38 -0
- node_modules/@exodus/bytes/fallback/encoding.js +369 -0
- node_modules/@exodus/bytes/fallback/encoding.labels.js +50 -0
- node_modules/@exodus/bytes/fallback/encoding.util.js +65 -0
- node_modules/@exodus/bytes/fallback/hex.js +126 -0
- node_modules/@exodus/bytes/fallback/latin1.js +152 -0
- node_modules/@exodus/bytes/fallback/multi-byte.encodings.cjs +1 -0
- node_modules/@exodus/bytes/fallback/multi-byte.encodings.json +0 -0
- node_modules/@exodus/bytes/fallback/multi-byte.js +962 -0
- node_modules/@exodus/bytes/fallback/multi-byte.table.js +118 -0
.gitattributes
CHANGED
|
@@ -42,3 +42,5 @@ docs/screenshots/journey.png filter=lfs diff=lfs merge=lfs -text
|
|
| 42 |
docs/screenshots/trip-iceland.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
docs/screenshots/trip-planner.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
docs/screenshots/vacay.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 42 |
docs/screenshots/trip-iceland.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
docs/screenshots/trip-planner.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
docs/screenshots/vacay.png filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
node_modules/@jimp/js-bmp/src/images/windows95.bmp filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
node_modules/@jimp/js-bmp/src/images/windows95.png filter=lfs diff=lfs merge=lfs -text
|
node_modules/@eslint-community/eslint-utils/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@eslint-community/eslint-utils",
|
| 3 |
+
"version": "4.9.1",
|
| 4 |
+
"description": "Utilities for ESLint plugins.",
|
| 5 |
+
"keywords": [
|
| 6 |
+
"eslint"
|
| 7 |
+
],
|
| 8 |
+
"homepage": "https://github.com/eslint-community/eslint-utils#readme",
|
| 9 |
+
"bugs": {
|
| 10 |
+
"url": "https://github.com/eslint-community/eslint-utils/issues"
|
| 11 |
+
},
|
| 12 |
+
"repository": {
|
| 13 |
+
"type": "git",
|
| 14 |
+
"url": "https://github.com/eslint-community/eslint-utils"
|
| 15 |
+
},
|
| 16 |
+
"license": "MIT",
|
| 17 |
+
"author": "Toru Nagashima",
|
| 18 |
+
"sideEffects": false,
|
| 19 |
+
"exports": {
|
| 20 |
+
".": {
|
| 21 |
+
"import": "./index.mjs",
|
| 22 |
+
"require": "./index.js"
|
| 23 |
+
},
|
| 24 |
+
"./package.json": "./package.json"
|
| 25 |
+
},
|
| 26 |
+
"main": "index",
|
| 27 |
+
"module": "index.mjs",
|
| 28 |
+
"files": [
|
| 29 |
+
"index.*"
|
| 30 |
+
],
|
| 31 |
+
"scripts": {
|
| 32 |
+
"prebuild": "npm run -s clean",
|
| 33 |
+
"build": "npm run build:dts && npm run build:rollup",
|
| 34 |
+
"build:dts": "tsc -p tsconfig.build.json",
|
| 35 |
+
"build:rollup": "rollup -c",
|
| 36 |
+
"clean": "rimraf .nyc_output coverage index.* dist",
|
| 37 |
+
"coverage": "opener ./coverage/lcov-report/index.html",
|
| 38 |
+
"docs:build": "vitepress build docs",
|
| 39 |
+
"docs:watch": "vitepress dev docs",
|
| 40 |
+
"format": "npm run -s format:prettier -- --write",
|
| 41 |
+
"format:prettier": "prettier .",
|
| 42 |
+
"format:check": "npm run -s format:prettier -- --check",
|
| 43 |
+
"lint:eslint": "eslint .",
|
| 44 |
+
"lint:format": "npm run -s format:check",
|
| 45 |
+
"lint:installed-check": "installed-check -v -i installed-check -i npm-run-all2 -i knip -i rollup-plugin-dts",
|
| 46 |
+
"lint:knip": "knip",
|
| 47 |
+
"lint": "run-p lint:*",
|
| 48 |
+
"test-coverage": "c8 mocha --reporter dot \"test/*.mjs\"",
|
| 49 |
+
"test": "mocha --reporter dot \"test/*.mjs\"",
|
| 50 |
+
"preversion": "npm run test-coverage && npm run -s build",
|
| 51 |
+
"postversion": "git push && git push --tags",
|
| 52 |
+
"prewatch": "npm run -s clean",
|
| 53 |
+
"watch": "warun \"{src,test}/**/*.mjs\" -- npm run -s test:mocha"
|
| 54 |
+
},
|
| 55 |
+
"dependencies": {
|
| 56 |
+
"eslint-visitor-keys": "^3.4.3"
|
| 57 |
+
},
|
| 58 |
+
"devDependencies": {
|
| 59 |
+
"@eslint-community/eslint-plugin-mysticatea": "^15.6.1",
|
| 60 |
+
"@types/eslint": "^9.6.1",
|
| 61 |
+
"@types/estree": "^1.0.7",
|
| 62 |
+
"@typescript-eslint/parser": "^5.62.0",
|
| 63 |
+
"@typescript-eslint/types": "^5.62.0",
|
| 64 |
+
"c8": "^8.0.1",
|
| 65 |
+
"dot-prop": "^7.2.0",
|
| 66 |
+
"eslint": "^8.57.1",
|
| 67 |
+
"installed-check": "^8.0.1",
|
| 68 |
+
"knip": "^5.33.3",
|
| 69 |
+
"mocha": "^9.2.2",
|
| 70 |
+
"npm-run-all2": "^6.2.3",
|
| 71 |
+
"opener": "^1.5.2",
|
| 72 |
+
"prettier": "2.8.8",
|
| 73 |
+
"rimraf": "^3.0.2",
|
| 74 |
+
"rollup": "^2.79.2",
|
| 75 |
+
"rollup-plugin-dts": "^4.2.3",
|
| 76 |
+
"rollup-plugin-sourcemaps": "^0.6.3",
|
| 77 |
+
"semver": "^7.6.3",
|
| 78 |
+
"typescript": "^4.9.5",
|
| 79 |
+
"vitepress": "^1.4.1",
|
| 80 |
+
"warun": "^1.0.0"
|
| 81 |
+
},
|
| 82 |
+
"peerDependencies": {
|
| 83 |
+
"eslint": "^6.0.0 || ^7.0.0 || >=8.0.0"
|
| 84 |
+
},
|
| 85 |
+
"engines": {
|
| 86 |
+
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
| 87 |
+
},
|
| 88 |
+
"funding": "https://opencollective.com/eslint"
|
| 89 |
+
}
|
node_modules/@eslint-community/regexpp/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2018 Toru Nagashima
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, 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,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
node_modules/@eslint-community/regexpp/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @eslint-community/regexpp
|
| 2 |
+
|
| 3 |
+
[](https://www.npmjs.com/package/@eslint-community/regexpp)
|
| 4 |
+
[](http://www.npmtrends.com/@eslint-community/regexpp)
|
| 5 |
+
[](https://github.com/eslint-community/regexpp/actions)
|
| 6 |
+
[](https://codecov.io/gh/eslint-community/regexpp)
|
| 7 |
+
|
| 8 |
+
A regular expression parser for ECMAScript.
|
| 9 |
+
|
| 10 |
+
## 💿 Installation
|
| 11 |
+
|
| 12 |
+
```bash
|
| 13 |
+
$ npm install @eslint-community/regexpp
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
- require Node@^12.0.0 || ^14.0.0 || >=16.0.0.
|
| 17 |
+
|
| 18 |
+
## 📖 Usage
|
| 19 |
+
|
| 20 |
+
```ts
|
| 21 |
+
import {
|
| 22 |
+
AST,
|
| 23 |
+
RegExpParser,
|
| 24 |
+
RegExpValidator,
|
| 25 |
+
RegExpVisitor,
|
| 26 |
+
parseRegExpLiteral,
|
| 27 |
+
validateRegExpLiteral,
|
| 28 |
+
visitRegExpAST
|
| 29 |
+
} from "@eslint-community/regexpp"
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
### parseRegExpLiteral(source, options?)
|
| 33 |
+
|
| 34 |
+
Parse a given regular expression literal then make AST object.
|
| 35 |
+
|
| 36 |
+
This is equivalent to `new RegExpParser(options).parseLiteral(source)`.
|
| 37 |
+
|
| 38 |
+
- **Parameters:**
|
| 39 |
+
- `source` (`string | RegExp`) The source code to parse.
|
| 40 |
+
- `options?` ([`RegExpParser.Options`]) The options to parse.
|
| 41 |
+
- **Return:**
|
| 42 |
+
- The AST of the regular expression.
|
| 43 |
+
|
| 44 |
+
### validateRegExpLiteral(source, options?)
|
| 45 |
+
|
| 46 |
+
Validate a given regular expression literal.
|
| 47 |
+
|
| 48 |
+
This is equivalent to `new RegExpValidator(options).validateLiteral(source)`.
|
| 49 |
+
|
| 50 |
+
- **Parameters:**
|
| 51 |
+
- `source` (`string`) The source code to validate.
|
| 52 |
+
- `options?` ([`RegExpValidator.Options`]) The options to validate.
|
| 53 |
+
|
| 54 |
+
### visitRegExpAST(ast, handlers)
|
| 55 |
+
|
| 56 |
+
Visit each node of a given AST.
|
| 57 |
+
|
| 58 |
+
This is equivalent to `new RegExpVisitor(handlers).visit(ast)`.
|
| 59 |
+
|
| 60 |
+
- **Parameters:**
|
| 61 |
+
- `ast` ([`AST.Node`]) The AST to visit.
|
| 62 |
+
- `handlers` ([`RegExpVisitor.Handlers`]) The callbacks.
|
| 63 |
+
|
| 64 |
+
### RegExpParser
|
| 65 |
+
|
| 66 |
+
#### new RegExpParser(options?)
|
| 67 |
+
|
| 68 |
+
- **Parameters:**
|
| 69 |
+
- `options?` ([`RegExpParser.Options`]) The options to parse.
|
| 70 |
+
|
| 71 |
+
#### parser.parseLiteral(source, start?, end?)
|
| 72 |
+
|
| 73 |
+
Parse a regular expression literal.
|
| 74 |
+
|
| 75 |
+
- **Parameters:**
|
| 76 |
+
- `source` (`string`) The source code to parse. E.g. `"/abc/g"`.
|
| 77 |
+
- `start?` (`number`) The start index in the source code. Default is `0`.
|
| 78 |
+
- `end?` (`number`) The end index in the source code. Default is `source.length`.
|
| 79 |
+
- **Return:**
|
| 80 |
+
- The AST of the regular expression.
|
| 81 |
+
|
| 82 |
+
#### parser.parsePattern(source, start?, end?, flags?)
|
| 83 |
+
|
| 84 |
+
Parse a regular expression pattern.
|
| 85 |
+
|
| 86 |
+
- **Parameters:**
|
| 87 |
+
- `source` (`string`) The source code to parse. E.g. `"abc"`.
|
| 88 |
+
- `start?` (`number`) The start index in the source code. Default is `0`.
|
| 89 |
+
- `end?` (`number`) The end index in the source code. Default is `source.length`.
|
| 90 |
+
- `flags?` (`{ unicode?: boolean, unicodeSets?: boolean }`) The flags to enable Unicode mode, and Unicode Set mode.
|
| 91 |
+
- **Return:**
|
| 92 |
+
- The AST of the regular expression pattern.
|
| 93 |
+
|
| 94 |
+
#### parser.parseFlags(source, start?, end?)
|
| 95 |
+
|
| 96 |
+
Parse a regular expression flags.
|
| 97 |
+
|
| 98 |
+
- **Parameters:**
|
| 99 |
+
- `source` (`string`) The source code to parse. E.g. `"gim"`.
|
| 100 |
+
- `start?` (`number`) The start index in the source code. Default is `0`.
|
| 101 |
+
- `end?` (`number`) The end index in the source code. Default is `source.length`.
|
| 102 |
+
- **Return:**
|
| 103 |
+
- The AST of the regular expression flags.
|
| 104 |
+
|
| 105 |
+
### RegExpValidator
|
| 106 |
+
|
| 107 |
+
#### new RegExpValidator(options)
|
| 108 |
+
|
| 109 |
+
- **Parameters:**
|
| 110 |
+
- `options` ([`RegExpValidator.Options`]) The options to validate.
|
| 111 |
+
|
| 112 |
+
#### validator.validateLiteral(source, start, end)
|
| 113 |
+
|
| 114 |
+
Validate a regular expression literal.
|
| 115 |
+
|
| 116 |
+
- **Parameters:**
|
| 117 |
+
- `source` (`string`) The source code to validate.
|
| 118 |
+
- `start?` (`number`) The start index in the source code. Default is `0`.
|
| 119 |
+
- `end?` (`number`) The end index in the source code. Default is `source.length`.
|
| 120 |
+
|
| 121 |
+
#### validator.validatePattern(source, start, end, flags)
|
| 122 |
+
|
| 123 |
+
Validate a regular expression pattern.
|
| 124 |
+
|
| 125 |
+
- **Parameters:**
|
| 126 |
+
- `source` (`string`) The source code to validate.
|
| 127 |
+
- `start?` (`number`) The start index in the source code. Default is `0`.
|
| 128 |
+
- `end?` (`number`) The end index in the source code. Default is `source.length`.
|
| 129 |
+
- `flags?` (`{ unicode?: boolean, unicodeSets?: boolean }`) The flags to enable Unicode mode, and Unicode Set mode.
|
| 130 |
+
|
| 131 |
+
#### validator.validateFlags(source, start, end)
|
| 132 |
+
|
| 133 |
+
Validate a regular expression flags.
|
| 134 |
+
|
| 135 |
+
- **Parameters:**
|
| 136 |
+
- `source` (`string`) The source code to validate.
|
| 137 |
+
- `start?` (`number`) The start index in the source code. Default is `0`.
|
| 138 |
+
- `end?` (`number`) The end index in the source code. Default is `source.length`.
|
| 139 |
+
|
| 140 |
+
### RegExpVisitor
|
| 141 |
+
|
| 142 |
+
#### new RegExpVisitor(handlers)
|
| 143 |
+
|
| 144 |
+
- **Parameters:**
|
| 145 |
+
- `handlers` ([`RegExpVisitor.Handlers`]) The callbacks.
|
| 146 |
+
|
| 147 |
+
#### visitor.visit(ast)
|
| 148 |
+
|
| 149 |
+
Validate a regular expression literal.
|
| 150 |
+
|
| 151 |
+
- **Parameters:**
|
| 152 |
+
- `ast` ([`AST.Node`]) The AST to visit.
|
| 153 |
+
|
| 154 |
+
## 📰 Changelog
|
| 155 |
+
|
| 156 |
+
- [GitHub Releases](https://github.com/eslint-community/regexpp/releases)
|
| 157 |
+
|
| 158 |
+
## 🍻 Contributing
|
| 159 |
+
|
| 160 |
+
Welcome contributing!
|
| 161 |
+
|
| 162 |
+
Please use GitHub's Issues/PRs.
|
| 163 |
+
|
| 164 |
+
### Development Tools
|
| 165 |
+
|
| 166 |
+
- `npm test` runs tests and measures coverage.
|
| 167 |
+
- `npm run build` compiles TypeScript source code to `index.js`, `index.js.map`, and `index.d.ts`.
|
| 168 |
+
- `npm run clean` removes the temporary files which are created by `npm test` and `npm run build`.
|
| 169 |
+
- `npm run lint` runs ESLint.
|
| 170 |
+
- `npm run update:test` updates test fixtures.
|
| 171 |
+
- `npm run update:ids` updates `src/unicode/ids.ts`.
|
| 172 |
+
- `npm run watch` runs tests with `--watch` option.
|
| 173 |
+
|
| 174 |
+
[`AST.Node`]: src/ast.ts#L4
|
| 175 |
+
[`RegExpParser.Options`]: src/parser.ts#L743
|
| 176 |
+
[`RegExpValidator.Options`]: src/validator.ts#L220
|
| 177 |
+
[`RegExpVisitor.Handlers`]: src/visitor.ts#L291
|
node_modules/@eslint-community/regexpp/index.d.ts
ADDED
|
@@ -0,0 +1,1163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Generated by dts-bundle v0.7.3
|
| 2 |
+
|
| 3 |
+
declare module "@eslint-community/regexpp" {
|
| 4 |
+
import * as AST from "@eslint-community/regexpp/ast";
|
| 5 |
+
import { RegExpParser } from "@eslint-community/regexpp/parser";
|
| 6 |
+
import { RegExpValidator } from "@eslint-community/regexpp/validator";
|
| 7 |
+
import { RegExpVisitor } from "@eslint-community/regexpp/visitor";
|
| 8 |
+
export { RegExpSyntaxError } from "@eslint-community/regexpp/regexp-syntax-error";
|
| 9 |
+
export { AST, RegExpParser, RegExpValidator };
|
| 10 |
+
/**
|
| 11 |
+
* Parse a given regular expression literal then make AST object.
|
| 12 |
+
* @param source The source code to parse.
|
| 13 |
+
* @param options The options to parse.
|
| 14 |
+
* @returns The AST of the regular expression.
|
| 15 |
+
*/
|
| 16 |
+
export function parseRegExpLiteral(
|
| 17 |
+
source: RegExp | string,
|
| 18 |
+
options?: RegExpParser.Options
|
| 19 |
+
): AST.RegExpLiteral;
|
| 20 |
+
/**
|
| 21 |
+
* Validate a given regular expression literal.
|
| 22 |
+
* @param source The source code to validate.
|
| 23 |
+
* @param options The options to validate.
|
| 24 |
+
*/
|
| 25 |
+
export function validateRegExpLiteral(
|
| 26 |
+
source: string,
|
| 27 |
+
options?: RegExpValidator.Options
|
| 28 |
+
): void;
|
| 29 |
+
export function visitRegExpAST(
|
| 30 |
+
node: AST.Node,
|
| 31 |
+
handlers: RegExpVisitor.Handlers
|
| 32 |
+
): void;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
declare module "@eslint-community/regexpp/ast" {
|
| 36 |
+
/**
|
| 37 |
+
* The type which includes all nodes.
|
| 38 |
+
*/
|
| 39 |
+
export type Node = BranchNode | LeafNode;
|
| 40 |
+
/**
|
| 41 |
+
* The type which includes all branch nodes.
|
| 42 |
+
*/
|
| 43 |
+
export type BranchNode =
|
| 44 |
+
| Alternative
|
| 45 |
+
| CapturingGroup
|
| 46 |
+
| CharacterClass
|
| 47 |
+
| CharacterClassRange
|
| 48 |
+
| ClassIntersection
|
| 49 |
+
| ClassStringDisjunction
|
| 50 |
+
| ClassSubtraction
|
| 51 |
+
| ExpressionCharacterClass
|
| 52 |
+
| Group
|
| 53 |
+
| LookaroundAssertion
|
| 54 |
+
| Modifiers
|
| 55 |
+
| Pattern
|
| 56 |
+
| Quantifier
|
| 57 |
+
| RegExpLiteral
|
| 58 |
+
| StringAlternative;
|
| 59 |
+
/**
|
| 60 |
+
* The type which includes all leaf nodes.
|
| 61 |
+
*/
|
| 62 |
+
export type LeafNode =
|
| 63 |
+
| Backreference
|
| 64 |
+
| BoundaryAssertion
|
| 65 |
+
| Character
|
| 66 |
+
| CharacterSet
|
| 67 |
+
| Flags
|
| 68 |
+
| ModifierFlags;
|
| 69 |
+
/**
|
| 70 |
+
* The type which includes all atom nodes.
|
| 71 |
+
*/
|
| 72 |
+
export type Element = Assertion | QuantifiableElement | Quantifier;
|
| 73 |
+
/**
|
| 74 |
+
* The type which includes all atom nodes that Quantifier node can have as children.
|
| 75 |
+
*/
|
| 76 |
+
export type QuantifiableElement =
|
| 77 |
+
| Backreference
|
| 78 |
+
| CapturingGroup
|
| 79 |
+
| Character
|
| 80 |
+
| CharacterClass
|
| 81 |
+
| CharacterSet
|
| 82 |
+
| ExpressionCharacterClass
|
| 83 |
+
| Group
|
| 84 |
+
| LookaheadAssertion;
|
| 85 |
+
/**
|
| 86 |
+
* The type which includes all character class atom nodes.
|
| 87 |
+
*/
|
| 88 |
+
export type CharacterClassElement =
|
| 89 |
+
| ClassRangesCharacterClassElement
|
| 90 |
+
| UnicodeSetsCharacterClassElement;
|
| 91 |
+
export type ClassRangesCharacterClassElement =
|
| 92 |
+
| Character
|
| 93 |
+
| CharacterClassRange
|
| 94 |
+
| CharacterUnicodePropertyCharacterSet
|
| 95 |
+
| EscapeCharacterSet;
|
| 96 |
+
export type UnicodeSetsCharacterClassElement =
|
| 97 |
+
| Character
|
| 98 |
+
| CharacterClassRange
|
| 99 |
+
| ClassStringDisjunction
|
| 100 |
+
| EscapeCharacterSet
|
| 101 |
+
| ExpressionCharacterClass
|
| 102 |
+
| UnicodePropertyCharacterSet
|
| 103 |
+
| UnicodeSetsCharacterClass;
|
| 104 |
+
/**
|
| 105 |
+
* The type which defines common properties for all node types.
|
| 106 |
+
*/
|
| 107 |
+
export interface NodeBase {
|
| 108 |
+
/** The node type. */
|
| 109 |
+
type: Node["type"];
|
| 110 |
+
/** The parent node. */
|
| 111 |
+
parent: Node["parent"];
|
| 112 |
+
/** The 0-based index that this node starts. */
|
| 113 |
+
start: number;
|
| 114 |
+
/** The 0-based index that this node ends. */
|
| 115 |
+
end: number;
|
| 116 |
+
/** The raw text of this node. */
|
| 117 |
+
raw: string;
|
| 118 |
+
}
|
| 119 |
+
/**
|
| 120 |
+
* The root node.
|
| 121 |
+
*/
|
| 122 |
+
export interface RegExpLiteral extends NodeBase {
|
| 123 |
+
type: "RegExpLiteral";
|
| 124 |
+
parent: null;
|
| 125 |
+
pattern: Pattern;
|
| 126 |
+
flags: Flags;
|
| 127 |
+
}
|
| 128 |
+
/**
|
| 129 |
+
* The pattern.
|
| 130 |
+
*/
|
| 131 |
+
export interface Pattern extends NodeBase {
|
| 132 |
+
type: "Pattern";
|
| 133 |
+
parent: RegExpLiteral | null;
|
| 134 |
+
alternatives: Alternative[];
|
| 135 |
+
}
|
| 136 |
+
/**
|
| 137 |
+
* The alternative.
|
| 138 |
+
* E.g. `a|b`
|
| 139 |
+
*/
|
| 140 |
+
export interface Alternative extends NodeBase {
|
| 141 |
+
type: "Alternative";
|
| 142 |
+
parent: CapturingGroup | Group | LookaroundAssertion | Pattern;
|
| 143 |
+
elements: Element[];
|
| 144 |
+
}
|
| 145 |
+
/**
|
| 146 |
+
* The uncapturing group.
|
| 147 |
+
* E.g. `(?:ab)`
|
| 148 |
+
*/
|
| 149 |
+
export interface Group extends NodeBase {
|
| 150 |
+
type: "Group";
|
| 151 |
+
parent: Alternative | Quantifier;
|
| 152 |
+
modifiers: Modifiers | null;
|
| 153 |
+
alternatives: Alternative[];
|
| 154 |
+
}
|
| 155 |
+
/**
|
| 156 |
+
* The capturing group.
|
| 157 |
+
* E.g. `(ab)`, `(?<name>ab)`
|
| 158 |
+
*/
|
| 159 |
+
export interface CapturingGroup extends NodeBase {
|
| 160 |
+
type: "CapturingGroup";
|
| 161 |
+
parent: Alternative | Quantifier;
|
| 162 |
+
name: string | null;
|
| 163 |
+
alternatives: Alternative[];
|
| 164 |
+
references: Backreference[];
|
| 165 |
+
}
|
| 166 |
+
/**
|
| 167 |
+
* The lookaround assertion.
|
| 168 |
+
*/
|
| 169 |
+
export type LookaroundAssertion = LookaheadAssertion | LookbehindAssertion;
|
| 170 |
+
/**
|
| 171 |
+
* The lookahead assertion.
|
| 172 |
+
* E.g. `(?=ab)`, `(?!ab)`
|
| 173 |
+
*/
|
| 174 |
+
export interface LookaheadAssertion extends NodeBase {
|
| 175 |
+
type: "Assertion";
|
| 176 |
+
parent: Alternative | Quantifier;
|
| 177 |
+
kind: "lookahead";
|
| 178 |
+
negate: boolean;
|
| 179 |
+
alternatives: Alternative[];
|
| 180 |
+
}
|
| 181 |
+
/**
|
| 182 |
+
* The lookbehind assertion.
|
| 183 |
+
* E.g. `(?<=ab)`, `(?<!ab)`
|
| 184 |
+
*/
|
| 185 |
+
export interface LookbehindAssertion extends NodeBase {
|
| 186 |
+
type: "Assertion";
|
| 187 |
+
parent: Alternative;
|
| 188 |
+
kind: "lookbehind";
|
| 189 |
+
negate: boolean;
|
| 190 |
+
alternatives: Alternative[];
|
| 191 |
+
}
|
| 192 |
+
/**
|
| 193 |
+
* The quantifier.
|
| 194 |
+
* E.g. `a?`, `a*`, `a+`, `a{1,2}`, `a??`, `a*?`, `a+?`, `a{1,2}?`
|
| 195 |
+
*/
|
| 196 |
+
export interface Quantifier extends NodeBase {
|
| 197 |
+
type: "Quantifier";
|
| 198 |
+
parent: Alternative;
|
| 199 |
+
min: number;
|
| 200 |
+
max: number;
|
| 201 |
+
greedy: boolean;
|
| 202 |
+
element: QuantifiableElement;
|
| 203 |
+
}
|
| 204 |
+
/**
|
| 205 |
+
* The character class.
|
| 206 |
+
* E.g. `[ab]`, `[^ab]`
|
| 207 |
+
*/
|
| 208 |
+
export type CharacterClass =
|
| 209 |
+
| ClassRangesCharacterClass
|
| 210 |
+
| UnicodeSetsCharacterClass;
|
| 211 |
+
interface BaseCharacterClass extends NodeBase {
|
| 212 |
+
type: "CharacterClass";
|
| 213 |
+
parent:
|
| 214 |
+
| Alternative
|
| 215 |
+
| ClassIntersection
|
| 216 |
+
| ClassSubtraction
|
| 217 |
+
| Quantifier
|
| 218 |
+
| UnicodeSetsCharacterClass;
|
| 219 |
+
unicodeSets: boolean;
|
| 220 |
+
negate: boolean;
|
| 221 |
+
elements: CharacterClassElement[];
|
| 222 |
+
}
|
| 223 |
+
/**
|
| 224 |
+
* The character class used in legacy (neither `u` nor `v` flag) and Unicode mode (`u` flag).
|
| 225 |
+
*
|
| 226 |
+
* This character class is guaranteed to **not** contain strings.
|
| 227 |
+
*
|
| 228 |
+
* In Unicode sets mode (`v` flag), {@link UnicodeSetsCharacterClass} is used.
|
| 229 |
+
*/
|
| 230 |
+
export interface ClassRangesCharacterClass extends BaseCharacterClass {
|
| 231 |
+
parent: Alternative | Quantifier;
|
| 232 |
+
unicodeSets: false;
|
| 233 |
+
elements: ClassRangesCharacterClassElement[];
|
| 234 |
+
}
|
| 235 |
+
/**
|
| 236 |
+
* The character class used in Unicode sets mode (`v` flag).
|
| 237 |
+
*
|
| 238 |
+
* This character class may contain strings.
|
| 239 |
+
*/
|
| 240 |
+
export interface UnicodeSetsCharacterClass extends BaseCharacterClass {
|
| 241 |
+
parent:
|
| 242 |
+
| Alternative
|
| 243 |
+
| ClassIntersection
|
| 244 |
+
| ClassSubtraction
|
| 245 |
+
| Quantifier
|
| 246 |
+
| UnicodeSetsCharacterClass;
|
| 247 |
+
unicodeSets: true;
|
| 248 |
+
elements: UnicodeSetsCharacterClassElement[];
|
| 249 |
+
}
|
| 250 |
+
/**
|
| 251 |
+
* The character class.
|
| 252 |
+
* E.g. `[a-b]`
|
| 253 |
+
*/
|
| 254 |
+
export interface CharacterClassRange extends NodeBase {
|
| 255 |
+
type: "CharacterClassRange";
|
| 256 |
+
parent: CharacterClass;
|
| 257 |
+
min: Character;
|
| 258 |
+
max: Character;
|
| 259 |
+
}
|
| 260 |
+
/**
|
| 261 |
+
* The assertion.
|
| 262 |
+
*/
|
| 263 |
+
export type Assertion = BoundaryAssertion | LookaroundAssertion;
|
| 264 |
+
/**
|
| 265 |
+
* The boundary assertion.
|
| 266 |
+
*/
|
| 267 |
+
export type BoundaryAssertion = EdgeAssertion | WordBoundaryAssertion;
|
| 268 |
+
/**
|
| 269 |
+
* The edge boundary assertion.
|
| 270 |
+
* E.g. `^`, `$`
|
| 271 |
+
*/
|
| 272 |
+
export interface EdgeAssertion extends NodeBase {
|
| 273 |
+
type: "Assertion";
|
| 274 |
+
parent: Alternative | Quantifier;
|
| 275 |
+
kind: "end" | "start";
|
| 276 |
+
}
|
| 277 |
+
/**
|
| 278 |
+
* The word bondary assertion.
|
| 279 |
+
* E.g. `\b`, `\B`
|
| 280 |
+
*/
|
| 281 |
+
export interface WordBoundaryAssertion extends NodeBase {
|
| 282 |
+
type: "Assertion";
|
| 283 |
+
parent: Alternative | Quantifier;
|
| 284 |
+
kind: "word";
|
| 285 |
+
negate: boolean;
|
| 286 |
+
}
|
| 287 |
+
/**
|
| 288 |
+
* The character set.
|
| 289 |
+
*/
|
| 290 |
+
export type CharacterSet =
|
| 291 |
+
| AnyCharacterSet
|
| 292 |
+
| EscapeCharacterSet
|
| 293 |
+
| UnicodePropertyCharacterSet;
|
| 294 |
+
/**
|
| 295 |
+
* The dot.
|
| 296 |
+
* E.g. `.`
|
| 297 |
+
*/
|
| 298 |
+
export interface AnyCharacterSet extends NodeBase {
|
| 299 |
+
type: "CharacterSet";
|
| 300 |
+
parent: Alternative | Quantifier;
|
| 301 |
+
kind: "any";
|
| 302 |
+
}
|
| 303 |
+
/**
|
| 304 |
+
* The character class escape.
|
| 305 |
+
* E.g. `\d`, `\s`, `\w`, `\D`, `\S`, `\W`
|
| 306 |
+
*/
|
| 307 |
+
export interface EscapeCharacterSet extends NodeBase {
|
| 308 |
+
type: "CharacterSet";
|
| 309 |
+
parent:
|
| 310 |
+
| Alternative
|
| 311 |
+
| CharacterClass
|
| 312 |
+
| ClassIntersection
|
| 313 |
+
| ClassSubtraction
|
| 314 |
+
| Quantifier;
|
| 315 |
+
kind: "digit" | "space" | "word";
|
| 316 |
+
negate: boolean;
|
| 317 |
+
}
|
| 318 |
+
/**
|
| 319 |
+
* The unicode property escape.
|
| 320 |
+
* E.g. `\p{ASCII}`, `\P{ASCII}`, `\p{Script=Hiragana}`
|
| 321 |
+
*/
|
| 322 |
+
export type UnicodePropertyCharacterSet =
|
| 323 |
+
| CharacterUnicodePropertyCharacterSet
|
| 324 |
+
| StringsUnicodePropertyCharacterSet;
|
| 325 |
+
interface BaseUnicodePropertyCharacterSet extends NodeBase {
|
| 326 |
+
type: "CharacterSet";
|
| 327 |
+
parent:
|
| 328 |
+
| Alternative
|
| 329 |
+
| CharacterClass
|
| 330 |
+
| ClassIntersection
|
| 331 |
+
| ClassSubtraction
|
| 332 |
+
| Quantifier;
|
| 333 |
+
kind: "property";
|
| 334 |
+
strings: boolean;
|
| 335 |
+
key: string;
|
| 336 |
+
value: string | null;
|
| 337 |
+
negate: boolean;
|
| 338 |
+
}
|
| 339 |
+
export interface CharacterUnicodePropertyCharacterSet
|
| 340 |
+
extends BaseUnicodePropertyCharacterSet {
|
| 341 |
+
strings: false;
|
| 342 |
+
value: string | null;
|
| 343 |
+
negate: boolean;
|
| 344 |
+
}
|
| 345 |
+
/** StringsUnicodePropertyCharacterSet is Unicode property escape with property of strings. */
|
| 346 |
+
export interface StringsUnicodePropertyCharacterSet
|
| 347 |
+
extends BaseUnicodePropertyCharacterSet {
|
| 348 |
+
parent:
|
| 349 |
+
| Alternative
|
| 350 |
+
| ClassIntersection
|
| 351 |
+
| ClassSubtraction
|
| 352 |
+
| Quantifier
|
| 353 |
+
| UnicodeSetsCharacterClass;
|
| 354 |
+
strings: true;
|
| 355 |
+
value: null;
|
| 356 |
+
negate: false;
|
| 357 |
+
}
|
| 358 |
+
/**
|
| 359 |
+
* The expression character class.
|
| 360 |
+
* E.g. `[a--b]`, `[a&&b]`,`[^a--b]`, `[^a&&b]`
|
| 361 |
+
*/
|
| 362 |
+
export interface ExpressionCharacterClass extends NodeBase {
|
| 363 |
+
type: "ExpressionCharacterClass";
|
| 364 |
+
parent:
|
| 365 |
+
| Alternative
|
| 366 |
+
| ClassIntersection
|
| 367 |
+
| ClassSubtraction
|
| 368 |
+
| Quantifier
|
| 369 |
+
| UnicodeSetsCharacterClass;
|
| 370 |
+
negate: boolean;
|
| 371 |
+
expression: ClassIntersection | ClassSubtraction;
|
| 372 |
+
}
|
| 373 |
+
export type ClassSetOperand =
|
| 374 |
+
| Character
|
| 375 |
+
| ClassStringDisjunction
|
| 376 |
+
| EscapeCharacterSet
|
| 377 |
+
| ExpressionCharacterClass
|
| 378 |
+
| UnicodePropertyCharacterSet
|
| 379 |
+
| UnicodeSetsCharacterClass;
|
| 380 |
+
/**
|
| 381 |
+
* The character class intersection.
|
| 382 |
+
* E.g. `a&&b`
|
| 383 |
+
*/
|
| 384 |
+
export interface ClassIntersection extends NodeBase {
|
| 385 |
+
type: "ClassIntersection";
|
| 386 |
+
parent: ClassIntersection | ExpressionCharacterClass;
|
| 387 |
+
left: ClassIntersection | ClassSetOperand;
|
| 388 |
+
right: ClassSetOperand;
|
| 389 |
+
}
|
| 390 |
+
/**
|
| 391 |
+
* The character class subtraction.
|
| 392 |
+
* E.g. `a--b`
|
| 393 |
+
*/
|
| 394 |
+
export interface ClassSubtraction extends NodeBase {
|
| 395 |
+
type: "ClassSubtraction";
|
| 396 |
+
parent: ClassSubtraction | ExpressionCharacterClass;
|
| 397 |
+
left: ClassSetOperand | ClassSubtraction;
|
| 398 |
+
right: ClassSetOperand;
|
| 399 |
+
}
|
| 400 |
+
/**
|
| 401 |
+
* The character class string disjunction.
|
| 402 |
+
* E.g. `\q{a|b}`
|
| 403 |
+
*/
|
| 404 |
+
export interface ClassStringDisjunction extends NodeBase {
|
| 405 |
+
type: "ClassStringDisjunction";
|
| 406 |
+
parent: ClassIntersection | ClassSubtraction | UnicodeSetsCharacterClass;
|
| 407 |
+
alternatives: StringAlternative[];
|
| 408 |
+
}
|
| 409 |
+
/** StringAlternative is only used for `\q{alt}`({@link ClassStringDisjunction}). */
|
| 410 |
+
export interface StringAlternative extends NodeBase {
|
| 411 |
+
type: "StringAlternative";
|
| 412 |
+
parent: ClassStringDisjunction;
|
| 413 |
+
elements: Character[];
|
| 414 |
+
}
|
| 415 |
+
/**
|
| 416 |
+
* The character.
|
| 417 |
+
* This includes escape sequences which mean a character.
|
| 418 |
+
* E.g. `a`, `あ`, `✿`, `\x65`, `\u0065`, `\u{65}`, `\/`
|
| 419 |
+
*/
|
| 420 |
+
export interface Character extends NodeBase {
|
| 421 |
+
type: "Character";
|
| 422 |
+
parent:
|
| 423 |
+
| Alternative
|
| 424 |
+
| CharacterClass
|
| 425 |
+
| CharacterClassRange
|
| 426 |
+
| ClassIntersection
|
| 427 |
+
| ClassSubtraction
|
| 428 |
+
| Quantifier
|
| 429 |
+
| StringAlternative;
|
| 430 |
+
value: number;
|
| 431 |
+
}
|
| 432 |
+
/**
|
| 433 |
+
* The backreference.
|
| 434 |
+
* E.g. `\1`, `\k<name>`
|
| 435 |
+
*/
|
| 436 |
+
export type Backreference = AmbiguousBackreference | UnambiguousBackreference;
|
| 437 |
+
interface BaseBackreference extends NodeBase {
|
| 438 |
+
type: "Backreference";
|
| 439 |
+
parent: Alternative | Quantifier;
|
| 440 |
+
ref: number | string;
|
| 441 |
+
ambiguous: boolean;
|
| 442 |
+
resolved: CapturingGroup | CapturingGroup[];
|
| 443 |
+
}
|
| 444 |
+
export interface AmbiguousBackreference extends BaseBackreference {
|
| 445 |
+
ref: string;
|
| 446 |
+
ambiguous: true;
|
| 447 |
+
resolved: CapturingGroup[];
|
| 448 |
+
}
|
| 449 |
+
export interface UnambiguousBackreference extends BaseBackreference {
|
| 450 |
+
ambiguous: false;
|
| 451 |
+
resolved: CapturingGroup;
|
| 452 |
+
}
|
| 453 |
+
/**
|
| 454 |
+
* The modifiers.
|
| 455 |
+
*/
|
| 456 |
+
export interface Modifiers extends NodeBase {
|
| 457 |
+
type: "Modifiers";
|
| 458 |
+
parent: Group;
|
| 459 |
+
/**
|
| 460 |
+
* The add modifier flags.
|
| 461 |
+
*/
|
| 462 |
+
add: ModifierFlags;
|
| 463 |
+
/**
|
| 464 |
+
* The remove modifier flags.
|
| 465 |
+
*
|
| 466 |
+
* `null` means no remove modifier flags. e.g. `(?ims:x)`
|
| 467 |
+
* The reason for `null` is that there is no position where the remove modifier flags appears. Must be behind the minus mark.
|
| 468 |
+
*/
|
| 469 |
+
remove: ModifierFlags | null;
|
| 470 |
+
}
|
| 471 |
+
/**
|
| 472 |
+
* The modifier flags.
|
| 473 |
+
*/
|
| 474 |
+
export interface ModifierFlags extends NodeBase {
|
| 475 |
+
type: "ModifierFlags";
|
| 476 |
+
parent: Modifiers;
|
| 477 |
+
dotAll: boolean;
|
| 478 |
+
ignoreCase: boolean;
|
| 479 |
+
multiline: boolean;
|
| 480 |
+
}
|
| 481 |
+
/**
|
| 482 |
+
* The flags.
|
| 483 |
+
*/
|
| 484 |
+
export interface Flags extends NodeBase {
|
| 485 |
+
type: "Flags";
|
| 486 |
+
parent: RegExpLiteral | null;
|
| 487 |
+
dotAll: boolean;
|
| 488 |
+
global: boolean;
|
| 489 |
+
hasIndices: boolean;
|
| 490 |
+
ignoreCase: boolean;
|
| 491 |
+
multiline: boolean;
|
| 492 |
+
sticky: boolean;
|
| 493 |
+
unicode: boolean;
|
| 494 |
+
unicodeSets: boolean;
|
| 495 |
+
}
|
| 496 |
+
export {};
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
declare module "@eslint-community/regexpp/parser" {
|
| 500 |
+
import type {
|
| 501 |
+
Flags,
|
| 502 |
+
RegExpLiteral,
|
| 503 |
+
Pattern,
|
| 504 |
+
} from "@eslint-community/regexpp/ast";
|
| 505 |
+
import type { EcmaVersion } from "@eslint-community/regexpp/ecma-versions";
|
| 506 |
+
export namespace RegExpParser {
|
| 507 |
+
/**
|
| 508 |
+
* The options for RegExpParser construction.
|
| 509 |
+
*/
|
| 510 |
+
interface Options {
|
| 511 |
+
/**
|
| 512 |
+
* The flag to disable Annex B syntax. Default is `false`.
|
| 513 |
+
*/
|
| 514 |
+
strict?: boolean;
|
| 515 |
+
/**
|
| 516 |
+
* ECMAScript version. Default is `2025`.
|
| 517 |
+
* - `2015` added `u` and `y` flags.
|
| 518 |
+
* - `2018` added `s` flag, Named Capturing Group, Lookbehind Assertion,
|
| 519 |
+
* and Unicode Property Escape.
|
| 520 |
+
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
|
| 521 |
+
* - `2022` added `d` flag.
|
| 522 |
+
* - `2023` added more valid Unicode Property Escapes.
|
| 523 |
+
* - `2024` added `v` flag.
|
| 524 |
+
* - `2025` added duplicate named capturing groups, modifiers.
|
| 525 |
+
*/
|
| 526 |
+
ecmaVersion?: EcmaVersion;
|
| 527 |
+
}
|
| 528 |
+
}
|
| 529 |
+
export class RegExpParser {
|
| 530 |
+
/**
|
| 531 |
+
* Initialize this parser.
|
| 532 |
+
* @param options The options of parser.
|
| 533 |
+
*/
|
| 534 |
+
constructor(options?: RegExpParser.Options);
|
| 535 |
+
/**
|
| 536 |
+
* Parse a regular expression literal. E.g. "/abc/g"
|
| 537 |
+
* @param source The source code to parse.
|
| 538 |
+
* @param start The start index in the source code.
|
| 539 |
+
* @param end The end index in the source code.
|
| 540 |
+
* @returns The AST of the given regular expression.
|
| 541 |
+
*/
|
| 542 |
+
parseLiteral(source: string, start?: number, end?: number): RegExpLiteral;
|
| 543 |
+
/**
|
| 544 |
+
* Parse a regular expression flags. E.g. "gim"
|
| 545 |
+
* @param source The source code to parse.
|
| 546 |
+
* @param start The start index in the source code.
|
| 547 |
+
* @param end The end index in the source code.
|
| 548 |
+
* @returns The AST of the given flags.
|
| 549 |
+
*/
|
| 550 |
+
parseFlags(source: string, start?: number, end?: number): Flags;
|
| 551 |
+
/**
|
| 552 |
+
* Parse a regular expression pattern. E.g. "abc"
|
| 553 |
+
* @param source The source code to parse.
|
| 554 |
+
* @param start The start index in the source code.
|
| 555 |
+
* @param end The end index in the source code.
|
| 556 |
+
* @param flags The flags.
|
| 557 |
+
* @returns The AST of the given pattern.
|
| 558 |
+
*/
|
| 559 |
+
parsePattern(
|
| 560 |
+
source: string,
|
| 561 |
+
start?: number,
|
| 562 |
+
end?: number,
|
| 563 |
+
flags?: {
|
| 564 |
+
unicode?: boolean;
|
| 565 |
+
unicodeSets?: boolean;
|
| 566 |
+
}
|
| 567 |
+
): Pattern;
|
| 568 |
+
/**
|
| 569 |
+
* @deprecated Backward compatibility
|
| 570 |
+
* Use object `flags` instead of boolean `uFlag`.
|
| 571 |
+
*
|
| 572 |
+
* @param source The source code to parse.
|
| 573 |
+
* @param start The start index in the source code.
|
| 574 |
+
* @param end The end index in the source code.
|
| 575 |
+
* @param uFlag The flag to set unicode mode.
|
| 576 |
+
* @returns The AST of the given pattern.
|
| 577 |
+
*/
|
| 578 |
+
parsePattern(
|
| 579 |
+
source: string,
|
| 580 |
+
start?: number,
|
| 581 |
+
end?: number,
|
| 582 |
+
uFlag?: boolean
|
| 583 |
+
): Pattern;
|
| 584 |
+
}
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
declare module "@eslint-community/regexpp/validator" {
|
| 588 |
+
import type { EcmaVersion } from "@eslint-community/regexpp/ecma-versions";
|
| 589 |
+
export type RegExpValidatorSourceContext = {
|
| 590 |
+
readonly source: string;
|
| 591 |
+
readonly start: number;
|
| 592 |
+
readonly end: number;
|
| 593 |
+
readonly kind: "flags" | "literal" | "pattern";
|
| 594 |
+
};
|
| 595 |
+
export namespace RegExpValidator {
|
| 596 |
+
/**
|
| 597 |
+
* The options for RegExpValidator construction.
|
| 598 |
+
*/
|
| 599 |
+
interface Options {
|
| 600 |
+
/**
|
| 601 |
+
* The flag to disable Annex B syntax. Default is `false`.
|
| 602 |
+
*/
|
| 603 |
+
strict?: boolean;
|
| 604 |
+
/**
|
| 605 |
+
* ECMAScript version. Default is `2025`.
|
| 606 |
+
* - `2015` added `u` and `y` flags.
|
| 607 |
+
* - `2018` added `s` flag, Named Capturing Group, Lookbehind Assertion,
|
| 608 |
+
* and Unicode Property Escape.
|
| 609 |
+
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
|
| 610 |
+
* - `2022` added `d` flag.
|
| 611 |
+
* - `2023` added more valid Unicode Property Escapes.
|
| 612 |
+
* - `2024` added `v` flag.
|
| 613 |
+
* - `2025` added duplicate named capturing groups, modifiers.
|
| 614 |
+
*/
|
| 615 |
+
ecmaVersion?: EcmaVersion;
|
| 616 |
+
/**
|
| 617 |
+
* A function that is called when the validator entered a RegExp literal.
|
| 618 |
+
* @param start The 0-based index of the first character.
|
| 619 |
+
*/
|
| 620 |
+
onLiteralEnter?: (start: number) => void;
|
| 621 |
+
/**
|
| 622 |
+
* A function that is called when the validator left a RegExp literal.
|
| 623 |
+
* @param start The 0-based index of the first character.
|
| 624 |
+
* @param end The next 0-based index of the last character.
|
| 625 |
+
*/
|
| 626 |
+
onLiteralLeave?: (start: number, end: number) => void;
|
| 627 |
+
/**
|
| 628 |
+
* A function that is called when the validator found flags.
|
| 629 |
+
* @param start The 0-based index of the first character.
|
| 630 |
+
* @param end The next 0-based index of the last character.
|
| 631 |
+
* @param flags.global `g` flag.
|
| 632 |
+
* @param flags.ignoreCase `i` flag.
|
| 633 |
+
* @param flags.multiline `m` flag.
|
| 634 |
+
* @param flags.unicode `u` flag.
|
| 635 |
+
* @param flags.sticky `y` flag.
|
| 636 |
+
* @param flags.dotAll `s` flag.
|
| 637 |
+
* @param flags.hasIndices `d` flag.
|
| 638 |
+
* @param flags.unicodeSets `v` flag.
|
| 639 |
+
*/
|
| 640 |
+
onRegExpFlags?: (
|
| 641 |
+
start: number,
|
| 642 |
+
end: number,
|
| 643 |
+
flags: {
|
| 644 |
+
global: boolean;
|
| 645 |
+
ignoreCase: boolean;
|
| 646 |
+
multiline: boolean;
|
| 647 |
+
unicode: boolean;
|
| 648 |
+
sticky: boolean;
|
| 649 |
+
dotAll: boolean;
|
| 650 |
+
hasIndices: boolean;
|
| 651 |
+
unicodeSets: boolean;
|
| 652 |
+
}
|
| 653 |
+
) => void;
|
| 654 |
+
/**
|
| 655 |
+
* A function that is called when the validator found flags.
|
| 656 |
+
* @param start The 0-based index of the first character.
|
| 657 |
+
* @param end The next 0-based index of the last character.
|
| 658 |
+
* @param global `g` flag.
|
| 659 |
+
* @param ignoreCase `i` flag.
|
| 660 |
+
* @param multiline `m` flag.
|
| 661 |
+
* @param unicode `u` flag.
|
| 662 |
+
* @param sticky `y` flag.
|
| 663 |
+
* @param dotAll `s` flag.
|
| 664 |
+
* @param hasIndices `d` flag.
|
| 665 |
+
*
|
| 666 |
+
* @deprecated Use `onRegExpFlags` instead.
|
| 667 |
+
*/
|
| 668 |
+
onFlags?: (
|
| 669 |
+
start: number,
|
| 670 |
+
end: number,
|
| 671 |
+
global: boolean,
|
| 672 |
+
ignoreCase: boolean,
|
| 673 |
+
multiline: boolean,
|
| 674 |
+
unicode: boolean,
|
| 675 |
+
sticky: boolean,
|
| 676 |
+
dotAll: boolean,
|
| 677 |
+
hasIndices: boolean
|
| 678 |
+
) => void;
|
| 679 |
+
/**
|
| 680 |
+
* A function that is called when the validator entered a pattern.
|
| 681 |
+
* @param start The 0-based index of the first character.
|
| 682 |
+
*/
|
| 683 |
+
onPatternEnter?: (start: number) => void;
|
| 684 |
+
/**
|
| 685 |
+
* A function that is called when the validator left a pattern.
|
| 686 |
+
* @param start The 0-based index of the first character.
|
| 687 |
+
* @param end The next 0-based index of the last character.
|
| 688 |
+
*/
|
| 689 |
+
onPatternLeave?: (start: number, end: number) => void;
|
| 690 |
+
/**
|
| 691 |
+
* A function that is called when the validator entered a disjunction.
|
| 692 |
+
* @param start The 0-based index of the first character.
|
| 693 |
+
*/
|
| 694 |
+
onDisjunctionEnter?: (start: number) => void;
|
| 695 |
+
/**
|
| 696 |
+
* A function that is called when the validator left a disjunction.
|
| 697 |
+
* @param start The 0-based index of the first character.
|
| 698 |
+
* @param end The next 0-based index of the last character.
|
| 699 |
+
*/
|
| 700 |
+
onDisjunctionLeave?: (start: number, end: number) => void;
|
| 701 |
+
/**
|
| 702 |
+
* A function that is called when the validator entered an alternative.
|
| 703 |
+
* @param start The 0-based index of the first character.
|
| 704 |
+
* @param index The 0-based index of alternatives in a disjunction.
|
| 705 |
+
*/
|
| 706 |
+
onAlternativeEnter?: (start: number, index: number) => void;
|
| 707 |
+
/**
|
| 708 |
+
* A function that is called when the validator left an alternative.
|
| 709 |
+
* @param start The 0-based index of the first character.
|
| 710 |
+
* @param end The next 0-based index of the last character.
|
| 711 |
+
* @param index The 0-based index of alternatives in a disjunction.
|
| 712 |
+
*/
|
| 713 |
+
onAlternativeLeave?: (start: number, end: number, index: number) => void;
|
| 714 |
+
/**
|
| 715 |
+
* A function that is called when the validator entered an uncapturing group.
|
| 716 |
+
* @param start The 0-based index of the first character.
|
| 717 |
+
*/
|
| 718 |
+
onGroupEnter?: (start: number) => void;
|
| 719 |
+
/**
|
| 720 |
+
* A function that is called when the validator left an uncapturing group.
|
| 721 |
+
* @param start The 0-based index of the first character.
|
| 722 |
+
* @param end The next 0-based index of the last character.
|
| 723 |
+
*/
|
| 724 |
+
onGroupLeave?: (start: number, end: number) => void;
|
| 725 |
+
/**
|
| 726 |
+
* A function that is called when the validator entered a modifiers.
|
| 727 |
+
* @param start The 0-based index of the first character.
|
| 728 |
+
*/
|
| 729 |
+
onModifiersEnter?: (start: number) => void;
|
| 730 |
+
/**
|
| 731 |
+
* A function that is called when the validator left a modifiers.
|
| 732 |
+
* @param start The 0-based index of the first character.
|
| 733 |
+
* @param end The next 0-based index of the last character.
|
| 734 |
+
*/
|
| 735 |
+
onModifiersLeave?: (start: number, end: number) => void;
|
| 736 |
+
/**
|
| 737 |
+
* A function that is called when the validator found an add modifiers.
|
| 738 |
+
* @param start The 0-based index of the first character.
|
| 739 |
+
* @param end The next 0-based index of the last character.
|
| 740 |
+
* @param flags flags.
|
| 741 |
+
* @param flags.ignoreCase `i` flag.
|
| 742 |
+
* @param flags.multiline `m` flag.
|
| 743 |
+
* @param flags.dotAll `s` flag.
|
| 744 |
+
*/
|
| 745 |
+
onAddModifiers?: (
|
| 746 |
+
start: number,
|
| 747 |
+
end: number,
|
| 748 |
+
flags: {
|
| 749 |
+
ignoreCase: boolean;
|
| 750 |
+
multiline: boolean;
|
| 751 |
+
dotAll: boolean;
|
| 752 |
+
}
|
| 753 |
+
) => void;
|
| 754 |
+
/**
|
| 755 |
+
* A function that is called when the validator found a remove modifiers.
|
| 756 |
+
* @param start The 0-based index of the first character.
|
| 757 |
+
* @param end The next 0-based index of the last character.
|
| 758 |
+
* @param flags flags.
|
| 759 |
+
* @param flags.ignoreCase `i` flag.
|
| 760 |
+
* @param flags.multiline `m` flag.
|
| 761 |
+
* @param flags.dotAll `s` flag.
|
| 762 |
+
*/
|
| 763 |
+
onRemoveModifiers?: (
|
| 764 |
+
start: number,
|
| 765 |
+
end: number,
|
| 766 |
+
flags: {
|
| 767 |
+
ignoreCase: boolean;
|
| 768 |
+
multiline: boolean;
|
| 769 |
+
dotAll: boolean;
|
| 770 |
+
}
|
| 771 |
+
) => void;
|
| 772 |
+
/**
|
| 773 |
+
* A function that is called when the validator entered a capturing group.
|
| 774 |
+
* @param start The 0-based index of the first character.
|
| 775 |
+
* @param name The group name.
|
| 776 |
+
*/
|
| 777 |
+
onCapturingGroupEnter?: (start: number, name: string | null) => void;
|
| 778 |
+
/**
|
| 779 |
+
* A function that is called when the validator left a capturing group.
|
| 780 |
+
* @param start The 0-based index of the first character.
|
| 781 |
+
* @param end The next 0-based index of the last character.
|
| 782 |
+
* @param name The group name.
|
| 783 |
+
*/
|
| 784 |
+
onCapturingGroupLeave?: (
|
| 785 |
+
start: number,
|
| 786 |
+
end: number,
|
| 787 |
+
name: string | null
|
| 788 |
+
) => void;
|
| 789 |
+
/**
|
| 790 |
+
* A function that is called when the validator found a quantifier.
|
| 791 |
+
* @param start The 0-based index of the first character.
|
| 792 |
+
* @param end The next 0-based index of the last character.
|
| 793 |
+
* @param min The minimum number of repeating.
|
| 794 |
+
* @param max The maximum number of repeating.
|
| 795 |
+
* @param greedy The flag to choose the longest matching.
|
| 796 |
+
*/
|
| 797 |
+
onQuantifier?: (
|
| 798 |
+
start: number,
|
| 799 |
+
end: number,
|
| 800 |
+
min: number,
|
| 801 |
+
max: number,
|
| 802 |
+
greedy: boolean
|
| 803 |
+
) => void;
|
| 804 |
+
/**
|
| 805 |
+
* A function that is called when the validator entered a lookahead/lookbehind assertion.
|
| 806 |
+
* @param start The 0-based index of the first character.
|
| 807 |
+
* @param kind The kind of the assertion.
|
| 808 |
+
* @param negate The flag which represents that the assertion is negative.
|
| 809 |
+
*/
|
| 810 |
+
onLookaroundAssertionEnter?: (
|
| 811 |
+
start: number,
|
| 812 |
+
kind: "lookahead" | "lookbehind",
|
| 813 |
+
negate: boolean
|
| 814 |
+
) => void;
|
| 815 |
+
/**
|
| 816 |
+
* A function that is called when the validator left a lookahead/lookbehind assertion.
|
| 817 |
+
* @param start The 0-based index of the first character.
|
| 818 |
+
* @param end The next 0-based index of the last character.
|
| 819 |
+
* @param kind The kind of the assertion.
|
| 820 |
+
* @param negate The flag which represents that the assertion is negative.
|
| 821 |
+
*/
|
| 822 |
+
onLookaroundAssertionLeave?: (
|
| 823 |
+
start: number,
|
| 824 |
+
end: number,
|
| 825 |
+
kind: "lookahead" | "lookbehind",
|
| 826 |
+
negate: boolean
|
| 827 |
+
) => void;
|
| 828 |
+
/**
|
| 829 |
+
* A function that is called when the validator found an edge boundary assertion.
|
| 830 |
+
* @param start The 0-based index of the first character.
|
| 831 |
+
* @param end The next 0-based index of the last character.
|
| 832 |
+
* @param kind The kind of the assertion.
|
| 833 |
+
*/
|
| 834 |
+
onEdgeAssertion?: (
|
| 835 |
+
start: number,
|
| 836 |
+
end: number,
|
| 837 |
+
kind: "end" | "start"
|
| 838 |
+
) => void;
|
| 839 |
+
/**
|
| 840 |
+
* A function that is called when the validator found a word boundary assertion.
|
| 841 |
+
* @param start The 0-based index of the first character.
|
| 842 |
+
* @param end The next 0-based index of the last character.
|
| 843 |
+
* @param kind The kind of the assertion.
|
| 844 |
+
* @param negate The flag which represents that the assertion is negative.
|
| 845 |
+
*/
|
| 846 |
+
onWordBoundaryAssertion?: (
|
| 847 |
+
start: number,
|
| 848 |
+
end: number,
|
| 849 |
+
kind: "word",
|
| 850 |
+
negate: boolean
|
| 851 |
+
) => void;
|
| 852 |
+
/**
|
| 853 |
+
* A function that is called when the validator found a dot.
|
| 854 |
+
* @param start The 0-based index of the first character.
|
| 855 |
+
* @param end The next 0-based index of the last character.
|
| 856 |
+
* @param kind The kind of the character set.
|
| 857 |
+
*/
|
| 858 |
+
onAnyCharacterSet?: (start: number, end: number, kind: "any") => void;
|
| 859 |
+
/**
|
| 860 |
+
* A function that is called when the validator found a character set escape.
|
| 861 |
+
* @param start The 0-based index of the first character.
|
| 862 |
+
* @param end The next 0-based index of the last character.
|
| 863 |
+
* @param kind The kind of the character set.
|
| 864 |
+
* @param negate The flag which represents that the character set is negative.
|
| 865 |
+
*/
|
| 866 |
+
onEscapeCharacterSet?: (
|
| 867 |
+
start: number,
|
| 868 |
+
end: number,
|
| 869 |
+
kind: "digit" | "space" | "word",
|
| 870 |
+
negate: boolean
|
| 871 |
+
) => void;
|
| 872 |
+
/**
|
| 873 |
+
* A function that is called when the validator found a Unicode proerty escape.
|
| 874 |
+
* @param start The 0-based index of the first character.
|
| 875 |
+
* @param end The next 0-based index of the last character.
|
| 876 |
+
* @param kind The kind of the character set.
|
| 877 |
+
* @param key The property name.
|
| 878 |
+
* @param value The property value.
|
| 879 |
+
* @param negate The flag which represents that the character set is negative.
|
| 880 |
+
* @param strings If true, the given property is property of strings.
|
| 881 |
+
*/
|
| 882 |
+
onUnicodePropertyCharacterSet?: (
|
| 883 |
+
start: number,
|
| 884 |
+
end: number,
|
| 885 |
+
kind: "property",
|
| 886 |
+
key: string,
|
| 887 |
+
value: string | null,
|
| 888 |
+
negate: boolean,
|
| 889 |
+
strings: boolean
|
| 890 |
+
) => void;
|
| 891 |
+
/**
|
| 892 |
+
* A function that is called when the validator found a character.
|
| 893 |
+
* @param start The 0-based index of the first character.
|
| 894 |
+
* @param end The next 0-based index of the last character.
|
| 895 |
+
* @param value The code point of the character.
|
| 896 |
+
*/
|
| 897 |
+
onCharacter?: (start: number, end: number, value: number) => void;
|
| 898 |
+
/**
|
| 899 |
+
* A function that is called when the validator found a backreference.
|
| 900 |
+
* @param start The 0-based index of the first character.
|
| 901 |
+
* @param end The next 0-based index of the last character.
|
| 902 |
+
* @param ref The key of the referred capturing group.
|
| 903 |
+
*/
|
| 904 |
+
onBackreference?: (
|
| 905 |
+
start: number,
|
| 906 |
+
end: number,
|
| 907 |
+
ref: number | string
|
| 908 |
+
) => void;
|
| 909 |
+
/**
|
| 910 |
+
* A function that is called when the validator entered a character class.
|
| 911 |
+
* @param start The 0-based index of the first character.
|
| 912 |
+
* @param negate The flag which represents that the character class is negative.
|
| 913 |
+
* @param unicodeSets `true` if unicodeSets mode.
|
| 914 |
+
*/
|
| 915 |
+
onCharacterClassEnter?: (
|
| 916 |
+
start: number,
|
| 917 |
+
negate: boolean,
|
| 918 |
+
unicodeSets: boolean
|
| 919 |
+
) => void;
|
| 920 |
+
/**
|
| 921 |
+
* A function that is called when the validator left a character class.
|
| 922 |
+
* @param start The 0-based index of the first character.
|
| 923 |
+
* @param end The next 0-based index of the last character.
|
| 924 |
+
* @param negate The flag which represents that the character class is negative.
|
| 925 |
+
*/
|
| 926 |
+
onCharacterClassLeave?: (
|
| 927 |
+
start: number,
|
| 928 |
+
end: number,
|
| 929 |
+
negate: boolean
|
| 930 |
+
) => void;
|
| 931 |
+
/**
|
| 932 |
+
* A function that is called when the validator found a character class range.
|
| 933 |
+
* @param start The 0-based index of the first character.
|
| 934 |
+
* @param end The next 0-based index of the last character.
|
| 935 |
+
* @param min The minimum code point of the range.
|
| 936 |
+
* @param max The maximum code point of the range.
|
| 937 |
+
*/
|
| 938 |
+
onCharacterClassRange?: (
|
| 939 |
+
start: number,
|
| 940 |
+
end: number,
|
| 941 |
+
min: number,
|
| 942 |
+
max: number
|
| 943 |
+
) => void;
|
| 944 |
+
/**
|
| 945 |
+
* A function that is called when the validator found a class intersection.
|
| 946 |
+
* @param start The 0-based index of the first character.
|
| 947 |
+
* @param end The next 0-based index of the last character.
|
| 948 |
+
*/
|
| 949 |
+
onClassIntersection?: (start: number, end: number) => void;
|
| 950 |
+
/**
|
| 951 |
+
* A function that is called when the validator found a class subtraction.
|
| 952 |
+
* @param start The 0-based index of the first character.
|
| 953 |
+
* @param end The next 0-based index of the last character.
|
| 954 |
+
*/
|
| 955 |
+
onClassSubtraction?: (start: number, end: number) => void;
|
| 956 |
+
/**
|
| 957 |
+
* A function that is called when the validator entered a class string disjunction.
|
| 958 |
+
* @param start The 0-based index of the first character.
|
| 959 |
+
*/
|
| 960 |
+
onClassStringDisjunctionEnter?: (start: number) => void;
|
| 961 |
+
/**
|
| 962 |
+
* A function that is called when the validator left a class string disjunction.
|
| 963 |
+
* @param start The 0-based index of the first character.
|
| 964 |
+
* @param end The next 0-based index of the last character.
|
| 965 |
+
*/
|
| 966 |
+
onClassStringDisjunctionLeave?: (start: number, end: number) => void;
|
| 967 |
+
/**
|
| 968 |
+
* A function that is called when the validator entered a string alternative.
|
| 969 |
+
* @param start The 0-based index of the first character.
|
| 970 |
+
* @param index The 0-based index of alternatives in a disjunction.
|
| 971 |
+
*/
|
| 972 |
+
onStringAlternativeEnter?: (start: number, index: number) => void;
|
| 973 |
+
/**
|
| 974 |
+
* A function that is called when the validator left a string alternative.
|
| 975 |
+
* @param start The 0-based index of the first character.
|
| 976 |
+
* @param end The next 0-based index of the last character.
|
| 977 |
+
* @param index The 0-based index of alternatives in a disjunction.
|
| 978 |
+
*/
|
| 979 |
+
onStringAlternativeLeave?: (
|
| 980 |
+
start: number,
|
| 981 |
+
end: number,
|
| 982 |
+
index: number
|
| 983 |
+
) => void;
|
| 984 |
+
}
|
| 985 |
+
}
|
| 986 |
+
/**
|
| 987 |
+
* The regular expression validator.
|
| 988 |
+
*/
|
| 989 |
+
export class RegExpValidator {
|
| 990 |
+
/**
|
| 991 |
+
* Initialize this validator.
|
| 992 |
+
* @param options The options of validator.
|
| 993 |
+
*/
|
| 994 |
+
constructor(options?: RegExpValidator.Options);
|
| 995 |
+
/**
|
| 996 |
+
* Validate a regular expression literal. E.g. "/abc/g"
|
| 997 |
+
* @param source The source code to validate.
|
| 998 |
+
* @param start The start index in the source code.
|
| 999 |
+
* @param end The end index in the source code.
|
| 1000 |
+
*/
|
| 1001 |
+
validateLiteral(source: string, start?: number, end?: number): void;
|
| 1002 |
+
/**
|
| 1003 |
+
* Validate a regular expression flags. E.g. "gim"
|
| 1004 |
+
* @param source The source code to validate.
|
| 1005 |
+
* @param start The start index in the source code.
|
| 1006 |
+
* @param end The end index in the source code.
|
| 1007 |
+
*/
|
| 1008 |
+
validateFlags(source: string, start?: number, end?: number): void;
|
| 1009 |
+
/**
|
| 1010 |
+
* Validate a regular expression pattern. E.g. "abc"
|
| 1011 |
+
* @param source The source code to validate.
|
| 1012 |
+
* @param start The start index in the source code.
|
| 1013 |
+
* @param end The end index in the source code.
|
| 1014 |
+
* @param flags The flags.
|
| 1015 |
+
*/
|
| 1016 |
+
validatePattern(
|
| 1017 |
+
source: string,
|
| 1018 |
+
start?: number,
|
| 1019 |
+
end?: number,
|
| 1020 |
+
flags?: {
|
| 1021 |
+
unicode?: boolean;
|
| 1022 |
+
unicodeSets?: boolean;
|
| 1023 |
+
}
|
| 1024 |
+
): void;
|
| 1025 |
+
/**
|
| 1026 |
+
* @deprecated Backward compatibility
|
| 1027 |
+
* Use object `flags` instead of boolean `uFlag`.
|
| 1028 |
+
* @param source The source code to validate.
|
| 1029 |
+
* @param start The start index in the source code.
|
| 1030 |
+
* @param end The end index in the source code.
|
| 1031 |
+
* @param uFlag The flag to set unicode mode.
|
| 1032 |
+
*/
|
| 1033 |
+
validatePattern(
|
| 1034 |
+
source: string,
|
| 1035 |
+
start?: number,
|
| 1036 |
+
end?: number,
|
| 1037 |
+
uFlag?: boolean
|
| 1038 |
+
): void;
|
| 1039 |
+
}
|
| 1040 |
+
}
|
| 1041 |
+
|
| 1042 |
+
declare module "@eslint-community/regexpp/visitor" {
|
| 1043 |
+
import type {
|
| 1044 |
+
Alternative,
|
| 1045 |
+
Assertion,
|
| 1046 |
+
Backreference,
|
| 1047 |
+
CapturingGroup,
|
| 1048 |
+
Character,
|
| 1049 |
+
CharacterClass,
|
| 1050 |
+
CharacterClassRange,
|
| 1051 |
+
CharacterSet,
|
| 1052 |
+
ClassIntersection,
|
| 1053 |
+
ClassStringDisjunction,
|
| 1054 |
+
ClassSubtraction,
|
| 1055 |
+
ExpressionCharacterClass,
|
| 1056 |
+
Flags,
|
| 1057 |
+
Group,
|
| 1058 |
+
ModifierFlags,
|
| 1059 |
+
Modifiers,
|
| 1060 |
+
Node,
|
| 1061 |
+
Pattern,
|
| 1062 |
+
Quantifier,
|
| 1063 |
+
RegExpLiteral,
|
| 1064 |
+
StringAlternative,
|
| 1065 |
+
} from "@eslint-community/regexpp/ast";
|
| 1066 |
+
/**
|
| 1067 |
+
* The visitor to walk on AST.
|
| 1068 |
+
*/
|
| 1069 |
+
export class RegExpVisitor {
|
| 1070 |
+
/**
|
| 1071 |
+
* Initialize this visitor.
|
| 1072 |
+
* @param handlers Callbacks for each node.
|
| 1073 |
+
*/
|
| 1074 |
+
constructor(handlers: RegExpVisitor.Handlers);
|
| 1075 |
+
/**
|
| 1076 |
+
* Visit a given node and descendant nodes.
|
| 1077 |
+
* @param node The root node to visit tree.
|
| 1078 |
+
*/
|
| 1079 |
+
visit(node: Node): void;
|
| 1080 |
+
}
|
| 1081 |
+
export namespace RegExpVisitor {
|
| 1082 |
+
interface Handlers {
|
| 1083 |
+
onAlternativeEnter?: (node: Alternative) => void;
|
| 1084 |
+
onAlternativeLeave?: (node: Alternative) => void;
|
| 1085 |
+
onAssertionEnter?: (node: Assertion) => void;
|
| 1086 |
+
onAssertionLeave?: (node: Assertion) => void;
|
| 1087 |
+
onBackreferenceEnter?: (node: Backreference) => void;
|
| 1088 |
+
onBackreferenceLeave?: (node: Backreference) => void;
|
| 1089 |
+
onCapturingGroupEnter?: (node: CapturingGroup) => void;
|
| 1090 |
+
onCapturingGroupLeave?: (node: CapturingGroup) => void;
|
| 1091 |
+
onCharacterEnter?: (node: Character) => void;
|
| 1092 |
+
onCharacterLeave?: (node: Character) => void;
|
| 1093 |
+
onCharacterClassEnter?: (node: CharacterClass) => void;
|
| 1094 |
+
onCharacterClassLeave?: (node: CharacterClass) => void;
|
| 1095 |
+
onCharacterClassRangeEnter?: (node: CharacterClassRange) => void;
|
| 1096 |
+
onCharacterClassRangeLeave?: (node: CharacterClassRange) => void;
|
| 1097 |
+
onCharacterSetEnter?: (node: CharacterSet) => void;
|
| 1098 |
+
onCharacterSetLeave?: (node: CharacterSet) => void;
|
| 1099 |
+
onClassIntersectionEnter?: (node: ClassIntersection) => void;
|
| 1100 |
+
onClassIntersectionLeave?: (node: ClassIntersection) => void;
|
| 1101 |
+
onClassStringDisjunctionEnter?: (node: ClassStringDisjunction) => void;
|
| 1102 |
+
onClassStringDisjunctionLeave?: (node: ClassStringDisjunction) => void;
|
| 1103 |
+
onClassSubtractionEnter?: (node: ClassSubtraction) => void;
|
| 1104 |
+
onClassSubtractionLeave?: (node: ClassSubtraction) => void;
|
| 1105 |
+
onExpressionCharacterClassEnter?: (
|
| 1106 |
+
node: ExpressionCharacterClass
|
| 1107 |
+
) => void;
|
| 1108 |
+
onExpressionCharacterClassLeave?: (
|
| 1109 |
+
node: ExpressionCharacterClass
|
| 1110 |
+
) => void;
|
| 1111 |
+
onFlagsEnter?: (node: Flags) => void;
|
| 1112 |
+
onFlagsLeave?: (node: Flags) => void;
|
| 1113 |
+
onGroupEnter?: (node: Group) => void;
|
| 1114 |
+
onGroupLeave?: (node: Group) => void;
|
| 1115 |
+
onModifierFlagsEnter?: (node: ModifierFlags) => void;
|
| 1116 |
+
onModifierFlagsLeave?: (node: ModifierFlags) => void;
|
| 1117 |
+
onModifiersEnter?: (node: Modifiers) => void;
|
| 1118 |
+
onModifiersLeave?: (node: Modifiers) => void;
|
| 1119 |
+
onPatternEnter?: (node: Pattern) => void;
|
| 1120 |
+
onPatternLeave?: (node: Pattern) => void;
|
| 1121 |
+
onQuantifierEnter?: (node: Quantifier) => void;
|
| 1122 |
+
onQuantifierLeave?: (node: Quantifier) => void;
|
| 1123 |
+
onRegExpLiteralEnter?: (node: RegExpLiteral) => void;
|
| 1124 |
+
onRegExpLiteralLeave?: (node: RegExpLiteral) => void;
|
| 1125 |
+
onStringAlternativeEnter?: (node: StringAlternative) => void;
|
| 1126 |
+
onStringAlternativeLeave?: (node: StringAlternative) => void;
|
| 1127 |
+
}
|
| 1128 |
+
}
|
| 1129 |
+
}
|
| 1130 |
+
|
| 1131 |
+
declare module "@eslint-community/regexpp/regexp-syntax-error" {
|
| 1132 |
+
import type { RegExpValidatorSourceContext } from "@eslint-community/regexpp/validator";
|
| 1133 |
+
export class RegExpSyntaxError extends SyntaxError {
|
| 1134 |
+
index: number;
|
| 1135 |
+
constructor(message: string, index: number);
|
| 1136 |
+
}
|
| 1137 |
+
export function newRegExpSyntaxError(
|
| 1138 |
+
srcCtx: RegExpValidatorSourceContext,
|
| 1139 |
+
flags: {
|
| 1140 |
+
unicode: boolean;
|
| 1141 |
+
unicodeSets: boolean;
|
| 1142 |
+
},
|
| 1143 |
+
index: number,
|
| 1144 |
+
message: string
|
| 1145 |
+
): RegExpSyntaxError;
|
| 1146 |
+
}
|
| 1147 |
+
|
| 1148 |
+
declare module "@eslint-community/regexpp/ecma-versions" {
|
| 1149 |
+
export type EcmaVersion =
|
| 1150 |
+
| 5
|
| 1151 |
+
| 2015
|
| 1152 |
+
| 2016
|
| 1153 |
+
| 2017
|
| 1154 |
+
| 2018
|
| 1155 |
+
| 2019
|
| 1156 |
+
| 2020
|
| 1157 |
+
| 2021
|
| 1158 |
+
| 2022
|
| 1159 |
+
| 2023
|
| 1160 |
+
| 2024
|
| 1161 |
+
| 2025;
|
| 1162 |
+
export const latestEcmaVersion = 2025;
|
| 1163 |
+
}
|
node_modules/@eslint-community/regexpp/index.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
node_modules/@eslint-community/regexpp/index.js.map
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
node_modules/@eslint-community/regexpp/index.mjs
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
node_modules/@eslint-community/regexpp/index.mjs.map
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
node_modules/@eslint-community/regexpp/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@eslint-community/regexpp",
|
| 3 |
+
"version": "4.12.2",
|
| 4 |
+
"description": "Regular expression parser for ECMAScript.",
|
| 5 |
+
"keywords": [
|
| 6 |
+
"regexp",
|
| 7 |
+
"regular",
|
| 8 |
+
"expression",
|
| 9 |
+
"parser",
|
| 10 |
+
"validator",
|
| 11 |
+
"ast",
|
| 12 |
+
"abstract",
|
| 13 |
+
"syntax",
|
| 14 |
+
"tree",
|
| 15 |
+
"ecmascript",
|
| 16 |
+
"es2015",
|
| 17 |
+
"es2016",
|
| 18 |
+
"es2017",
|
| 19 |
+
"es2018",
|
| 20 |
+
"es2019",
|
| 21 |
+
"es2020",
|
| 22 |
+
"es2021",
|
| 23 |
+
"annexB"
|
| 24 |
+
],
|
| 25 |
+
"homepage": "https://github.com/eslint-community/regexpp#readme",
|
| 26 |
+
"bugs": {
|
| 27 |
+
"url": "https://github.com/eslint-community/regexpp/issues"
|
| 28 |
+
},
|
| 29 |
+
"repository": {
|
| 30 |
+
"type": "git",
|
| 31 |
+
"url": "https://github.com/eslint-community/regexpp"
|
| 32 |
+
},
|
| 33 |
+
"license": "MIT",
|
| 34 |
+
"author": "Toru Nagashima",
|
| 35 |
+
"exports": {
|
| 36 |
+
".": {
|
| 37 |
+
"types": "./index.d.ts",
|
| 38 |
+
"import": "./index.mjs",
|
| 39 |
+
"default": "./index.js"
|
| 40 |
+
},
|
| 41 |
+
"./package.json": "./package.json"
|
| 42 |
+
},
|
| 43 |
+
"main": "index",
|
| 44 |
+
"files": [
|
| 45 |
+
"index.*"
|
| 46 |
+
],
|
| 47 |
+
"scripts": {
|
| 48 |
+
"prebuild": "npm run -s clean",
|
| 49 |
+
"build": "run-s build:*",
|
| 50 |
+
"build:tsc": "tsc --module es2015",
|
| 51 |
+
"build:rollup": "rollup -c",
|
| 52 |
+
"build:dts": "npm run -s build:tsc -- --removeComments false && dts-bundle --name @eslint-community/regexpp --main .temp/index.d.ts --out ../index.d.ts && prettier --write index.d.ts",
|
| 53 |
+
"clean": "rimraf .temp index.*",
|
| 54 |
+
"lint": "eslint . --ext .ts",
|
| 55 |
+
"test": "nyc _mocha \"test/*.ts\" --reporter dot --timeout 10000",
|
| 56 |
+
"debug": "mocha --require ts-node/register/transpile-only \"test/*.ts\" --reporter dot --timeout 10000",
|
| 57 |
+
"update:test": "ts-node scripts/update-fixtures.ts",
|
| 58 |
+
"update:unicode": "run-s update:unicode:*",
|
| 59 |
+
"update:unicode:ids": "ts-node scripts/update-unicode-ids.ts",
|
| 60 |
+
"update:unicode:props": "ts-node scripts/update-unicode-properties.ts",
|
| 61 |
+
"update:test262:extract": "ts-node -T scripts/extract-test262.ts",
|
| 62 |
+
"preversion": "npm test && npm run -s build",
|
| 63 |
+
"postversion": "git push && git push --tags",
|
| 64 |
+
"prewatch": "npm run -s clean",
|
| 65 |
+
"watch": "_mocha \"test/*.ts\" --require ts-node/register --reporter dot --timeout 10000 --watch-extensions ts --watch --growl"
|
| 66 |
+
},
|
| 67 |
+
"dependencies": {},
|
| 68 |
+
"devDependencies": {
|
| 69 |
+
"@eslint-community/eslint-plugin-mysticatea": "^15.5.1",
|
| 70 |
+
"@rollup/plugin-node-resolve": "^14.1.0",
|
| 71 |
+
"@types/eslint": "^8.44.3",
|
| 72 |
+
"@types/jsdom": "^16.2.15",
|
| 73 |
+
"@types/mocha": "^9.1.1",
|
| 74 |
+
"@types/node": "^12.20.55",
|
| 75 |
+
"dts-bundle": "^0.7.3",
|
| 76 |
+
"eslint": "^8.50.0",
|
| 77 |
+
"js-tokens": "^8.0.2",
|
| 78 |
+
"jsdom": "^19.0.0",
|
| 79 |
+
"mocha": "^9.2.2",
|
| 80 |
+
"npm-run-all2": "^6.2.2",
|
| 81 |
+
"nyc": "^14.1.1",
|
| 82 |
+
"rimraf": "^3.0.2",
|
| 83 |
+
"rollup": "^2.79.1",
|
| 84 |
+
"rollup-plugin-sourcemaps": "^0.6.3",
|
| 85 |
+
"ts-node": "^10.9.1",
|
| 86 |
+
"typescript": "~5.0.2"
|
| 87 |
+
},
|
| 88 |
+
"engines": {
|
| 89 |
+
"node": "^12.0.0 || ^14.0.0 || >=16.0.0"
|
| 90 |
+
}
|
| 91 |
+
}
|
node_modules/@exodus/bytes/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2024-2025 Exodus Movement
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, 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,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
node_modules/@exodus/bytes/README.md
ADDED
|
@@ -0,0 +1,818 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @exodus/bytes
|
| 2 |
+
|
| 3 |
+
[](https://npmjs.org/package/@exodus/bytes)
|
| 4 |
+
[](https://github.com/ExodusOSS/bytes/releases)
|
| 5 |
+
[](https://www.npmcharts.com/compare/@exodus/bytes?minimal=true)
|
| 6 |
+
[](https://github.com/ExodusOSS/bytes/blob/HEAD/LICENSE)
|
| 7 |
+
[](https://github.com/ExodusOSS/bytes/actions/workflows/test.yml?query=branch%3Amain)
|
| 8 |
+
[](https://exodusoss.github.io/bytes/)
|
| 9 |
+
|
| 10 |
+
`Uint8Array` conversion to and from `base64`, `base32`, `base58`, `hex`, `utf8`, `utf16`, `bech32` and `wif`
|
| 11 |
+
|
| 12 |
+
And a [`TextEncoder` / `TextDecoder` polyfill](#textencoder--textdecoder-polyfill)
|
| 13 |
+
|
| 14 |
+
See [documentation](https://exodusoss.github.io/bytes/).
|
| 15 |
+
|
| 16 |
+
## Strict
|
| 17 |
+
|
| 18 |
+
Performs proper input validation, ensures no garbage-in-garbage-out
|
| 19 |
+
|
| 20 |
+
Tested in CI with [@exodus/test](https://github.com/ExodusMovement/test#exodustest) on:
|
| 21 |
+
|
| 22 |
+
[](https://nodejs.org/api/test.html)
|
| 23 |
+
[](https://deno.com/)
|
| 24 |
+
[](https://bun.sh/)
|
| 25 |
+
[](http://electronjs.org/)
|
| 26 |
+
[](https://github.com/cloudflare/workerd)\
|
| 27 |
+
[](https://www.chromium.org/Home/)
|
| 28 |
+
[](http://webkit.org/)
|
| 29 |
+
[](https://github.com/mozilla-firefox)
|
| 30 |
+
[](https://servo.org/)\
|
| 31 |
+
[](https://hermesengine.dev)
|
| 32 |
+
[](https://v8.dev/docs/d8)
|
| 33 |
+
[](https://docs.webkit.org/Deep%20Dive/JSC/JavaScriptCore.html)
|
| 34 |
+
[](https://spidermonkey.dev/)\
|
| 35 |
+
[](https://github.com/quickjs-ng/quickjs)
|
| 36 |
+
[](https://github.com/Moddable-OpenSource/moddable)
|
| 37 |
+
[](https://github.com/oracle/graaljs)
|
| 38 |
+
|
| 39 |
+
## Fast
|
| 40 |
+
|
| 41 |
+
* `10-20x` faster than `Buffer` polyfill
|
| 42 |
+
* `2-10x` faster than `iconv-lite`
|
| 43 |
+
|
| 44 |
+
The above was for the js fallback
|
| 45 |
+
|
| 46 |
+
It's up to `100x` when native impl is available \
|
| 47 |
+
e.g. in `utf8fromString` on Hermes / React Native or `fromHex` in Chrome
|
| 48 |
+
|
| 49 |
+
Also:
|
| 50 |
+
* `3-8x` faster than `bs58`
|
| 51 |
+
* `10-30x` faster than `@scure/base` (or `>100x` on Node.js <25)
|
| 52 |
+
* Faster in `utf8toString` / `utf8fromString` than `Buffer` or `TextDecoder` / `TextEncoder` on Node.js
|
| 53 |
+
|
| 54 |
+
See [Performance](./Performance.md) for more info
|
| 55 |
+
|
| 56 |
+
## TextEncoder / TextDecoder polyfill
|
| 57 |
+
|
| 58 |
+
```js
|
| 59 |
+
import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
|
| 60 |
+
import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
Less than half the bundle size of [text-encoding](https://npmjs.com/text-encoding), [whatwg-encoding](https://npmjs.com/whatwg-encoding) or [iconv-lite](https://npmjs.com/iconv-lite) (gzipped or not).\
|
| 64 |
+
Also [much faster](#fast) than all of those.
|
| 65 |
+
|
| 66 |
+
> [!TIP]
|
| 67 |
+
> See also the [lite version](#lite-version) to get this down to 8 KiB gzipped.
|
| 68 |
+
|
| 69 |
+
Spec compliant, passing WPT and covered with extra tests.\
|
| 70 |
+
Moreover, tests for this library uncovered [bugs in all major implementations](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit).\
|
| 71 |
+
Including all three major browser engines being wrong at UTF-8.\
|
| 72 |
+
See [WPT pull request](https://github.com/web-platform-tests/wpt/pull/56892).
|
| 73 |
+
|
| 74 |
+
It works correctly even in environments that have native implementations broken (that's all of them currently).\
|
| 75 |
+
Runs (and passes WPT) on Node.js built without ICU.
|
| 76 |
+
|
| 77 |
+
> [!NOTE]
|
| 78 |
+
> [Faster than Node.js native implementation on Node.js](https://github.com/nodejs/node/issues/61041#issuecomment-3649242024).
|
| 79 |
+
>
|
| 80 |
+
> The JS multi-byte version is as fast as native impl in Node.js and browsers, but (unlike them) returns correct results.
|
| 81 |
+
>
|
| 82 |
+
> For encodings where native version is known to be fast and correct, it is automatically used.\
|
| 83 |
+
> Some single-byte encodings are faster than native in all three major browser engines.
|
| 84 |
+
|
| 85 |
+
See [analysis table](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit) for more info.
|
| 86 |
+
|
| 87 |
+
### Caveat: `TextDecoder` / `TextEncoder` APIs are lossy by default per spec
|
| 88 |
+
|
| 89 |
+
_These are only provided as a compatibility layer, prefer hardened APIs instead in new code._
|
| 90 |
+
|
| 91 |
+
* `TextDecoder` can (and should) be used with `{ fatal: true }` option for all purposes demanding correctness / lossless transforms
|
| 92 |
+
|
| 93 |
+
* `TextEncoder` does not support a fatal mode per spec, it always performs replacement.
|
| 94 |
+
|
| 95 |
+
That is not suitable for hashing, cryptography or consensus applications.\
|
| 96 |
+
Otherwise there would be non-equal strings with equal signatures and hashes — the collision is caused by the lossy transform of a JS string to bytes.
|
| 97 |
+
Those also survive e.g. `JSON.stringify`/`JSON.parse` or being sent over network.
|
| 98 |
+
|
| 99 |
+
Use strict APIs in new applications, see `utf8fromString` / `utf16fromString` below.\
|
| 100 |
+
Those throw on non-well-formed strings by default.
|
| 101 |
+
|
| 102 |
+
### Lite version
|
| 103 |
+
|
| 104 |
+
Alternate exports exist that can help reduce bundle size, see comparison:
|
| 105 |
+
|
| 106 |
+
| import | size |
|
| 107 |
+
| - | - |
|
| 108 |
+
| [@exodus/bytes/encoding-browser.js](#exodusbytesencoding-browserjs-) | <sub></sub> |
|
| 109 |
+
| [@exodus/bytes/encoding-lite.js](#exodusbytesencoding-litejs-) | <sub></sub> |
|
| 110 |
+
| [@exodus/bytes/encoding.js](#exodusbytesencodingjs-) | <sub></sub> |
|
| 111 |
+
| `text-encoding` | <sub></sub> |
|
| 112 |
+
| `iconv-lite` | <sub></sub> |
|
| 113 |
+
| `whatwg-encoding` | <sub></sub> |
|
| 114 |
+
|
| 115 |
+
Libraries are advised to use single-purpose hardened `@exodus/bytes/utf8.js` / `@exodus/bytes/utf16.js` APIs for Unicode.
|
| 116 |
+
|
| 117 |
+
Applications (including React Native apps) are advised to load either `@exodus/bytes/encoding-lite.js` or `@exodus/bytes/encoding.js`
|
| 118 |
+
(depending on whether legacy multi-byte support is needed) and use that as a global polyfill.
|
| 119 |
+
|
| 120 |
+
#### `@exodus/bytes/encoding-lite.js`
|
| 121 |
+
|
| 122 |
+
If you don't need support for legacy multi-byte encodings.
|
| 123 |
+
|
| 124 |
+
Reduces the bundle size ~12x, while still keeping `utf-8`, `utf-16le`, `utf-16be` and all single-byte encodings specified by the spec.
|
| 125 |
+
The only difference is support for legacy multi-byte encodings.
|
| 126 |
+
|
| 127 |
+
See [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).
|
| 128 |
+
|
| 129 |
+
This can be useful for example in React Native global TextDecoder polyfill,
|
| 130 |
+
if you are sure that you don't need legacy multi-byte encodings support.
|
| 131 |
+
|
| 132 |
+
#### `@exodus/bytes/encoding-browser.js`
|
| 133 |
+
|
| 134 |
+
Resolves to a tiny import in browser bundles, preferring native `TextDecoder` / `TextEncoder`.
|
| 135 |
+
|
| 136 |
+
For non-browsers (Node.js, React Native), loads a full implementation.
|
| 137 |
+
|
| 138 |
+
> [!NOTE]
|
| 139 |
+
> This is not the default behavior for `@exodus/bytes/encoding.js` because all major browser implementations have bugs,
|
| 140 |
+
> which `@exodus/bytes/encoding.js` fixes. Only use if you are ok with that.
|
| 141 |
+
|
| 142 |
+
## API
|
| 143 |
+
|
| 144 |
+
### @exodus/bytes/utf8.js <sub></sub>
|
| 145 |
+
|
| 146 |
+
UTF-8 encoding/decoding
|
| 147 |
+
|
| 148 |
+
```js
|
| 149 |
+
import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
|
| 150 |
+
|
| 151 |
+
// loose
|
| 152 |
+
import { utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
|
| 156 |
+
_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
|
| 157 |
+
|
| 158 |
+
#### `utf8fromString(string, format = 'uint8')`
|
| 159 |
+
|
| 160 |
+
Encode a string to UTF-8 bytes (strict mode)
|
| 161 |
+
|
| 162 |
+
Throws on invalid Unicode (unpaired surrogates)
|
| 163 |
+
|
| 164 |
+
This is similar to the following snippet (but works on all engines):
|
| 165 |
+
```js
|
| 166 |
+
// Strict encode, requiring Unicode codepoints to be valid
|
| 167 |
+
if (typeof string !== 'string' || !string.isWellFormed()) throw new TypeError()
|
| 168 |
+
return new TextEncoder().encode(string)
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
#### `utf8fromStringLoose(string, format = 'uint8')`
|
| 172 |
+
|
| 173 |
+
Encode a string to UTF-8 bytes (loose mode)
|
| 174 |
+
|
| 175 |
+
Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
|
| 176 |
+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
|
| 177 |
+
|
| 178 |
+
_Such replacement is a non-injective function, is irreversable and causes collisions.\
|
| 179 |
+
Prefer using strict throwing methods for cryptography applications._
|
| 180 |
+
|
| 181 |
+
This is similar to the following snippet (but works on all engines):
|
| 182 |
+
```js
|
| 183 |
+
// Loose encode, replacing invalid Unicode codepoints with U+FFFD
|
| 184 |
+
if (typeof string !== 'string') throw new TypeError()
|
| 185 |
+
return new TextEncoder().encode(string)
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
#### `utf8toString(arr)`
|
| 189 |
+
|
| 190 |
+
Decode UTF-8 bytes to a string (strict mode)
|
| 191 |
+
|
| 192 |
+
Throws on invalid UTF-8 byte sequences
|
| 193 |
+
|
| 194 |
+
This is similar to `new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(arr)`,
|
| 195 |
+
but works on all engines.
|
| 196 |
+
|
| 197 |
+
#### `utf8toStringLoose(arr)`
|
| 198 |
+
|
| 199 |
+
Decode UTF-8 bytes to a string (loose mode)
|
| 200 |
+
|
| 201 |
+
Replaces invalid UTF-8 byte sequences with replacement codepoints `U+FFFD`
|
| 202 |
+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
|
| 203 |
+
|
| 204 |
+
_Such replacement is a non-injective function, is irreversable and causes collisions.\
|
| 205 |
+
Prefer using strict throwing methods for cryptography applications._
|
| 206 |
+
|
| 207 |
+
This is similar to `new TextDecoder('utf-8', { ignoreBOM: true }).decode(arr)`,
|
| 208 |
+
but works on all engines.
|
| 209 |
+
|
| 210 |
+
### @exodus/bytes/utf16.js <sub></sub>
|
| 211 |
+
|
| 212 |
+
UTF-16 encoding/decoding
|
| 213 |
+
|
| 214 |
+
```js
|
| 215 |
+
import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'
|
| 216 |
+
|
| 217 |
+
// loose
|
| 218 |
+
import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\
|
| 222 |
+
_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_
|
| 223 |
+
|
| 224 |
+
#### `utf16fromString(string, format = 'uint16')`
|
| 225 |
+
|
| 226 |
+
Encode a string to UTF-16 bytes (strict mode)
|
| 227 |
+
|
| 228 |
+
Throws on invalid Unicode (unpaired surrogates)
|
| 229 |
+
|
| 230 |
+
#### `utf16fromStringLoose(string, format = 'uint16')`
|
| 231 |
+
|
| 232 |
+
Encode a string to UTF-16 bytes (loose mode)
|
| 233 |
+
|
| 234 |
+
Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
|
| 235 |
+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
|
| 236 |
+
|
| 237 |
+
_Such replacement is a non-injective function, is irreversible and causes collisions.\
|
| 238 |
+
Prefer using strict throwing methods for cryptography applications._
|
| 239 |
+
|
| 240 |
+
#### `utf16toString(arr, format = 'uint16')`
|
| 241 |
+
|
| 242 |
+
Decode UTF-16 bytes to a string (strict mode)
|
| 243 |
+
|
| 244 |
+
Throws on invalid UTF-16 byte sequences
|
| 245 |
+
|
| 246 |
+
Throws on non-even byte length.
|
| 247 |
+
|
| 248 |
+
#### `utf16toStringLoose(arr, format = 'uint16')`
|
| 249 |
+
|
| 250 |
+
Decode UTF-16 bytes to a string (loose mode)
|
| 251 |
+
|
| 252 |
+
Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`
|
| 253 |
+
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
|
| 254 |
+
|
| 255 |
+
_Such replacement is a non-injective function, is irreversible and causes collisions.\
|
| 256 |
+
Prefer using strict throwing methods for cryptography applications._
|
| 257 |
+
|
| 258 |
+
Throws on non-even byte length.
|
| 259 |
+
|
| 260 |
+
### @exodus/bytes/bigint.js <sub></sub>
|
| 261 |
+
|
| 262 |
+
Convert between BigInt and Uint8Array
|
| 263 |
+
|
| 264 |
+
```js
|
| 265 |
+
import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
#### `fromBigInt(bigint, { length, format = 'uint8' })`
|
| 269 |
+
|
| 270 |
+
Convert a BigInt to a Uint8Array or Buffer
|
| 271 |
+
|
| 272 |
+
The output bytes are in big-endian format.
|
| 273 |
+
|
| 274 |
+
Throws if the BigInt is negative or cannot fit into the specified length.
|
| 275 |
+
|
| 276 |
+
#### `toBigInt(arr)`
|
| 277 |
+
|
| 278 |
+
Convert a Uint8Array or Buffer to a BigInt
|
| 279 |
+
|
| 280 |
+
The bytes are interpreted as a big-endian unsigned integer.
|
| 281 |
+
|
| 282 |
+
### @exodus/bytes/hex.js <sub></sub>
|
| 283 |
+
|
| 284 |
+
Implements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
|
| 285 |
+
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
|
| 286 |
+
|
| 287 |
+
```js
|
| 288 |
+
import { fromHex, toHex } from '@exodus/bytes/hex.js'
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
+
#### `fromHex(string, format = 'uint8')`
|
| 292 |
+
|
| 293 |
+
Decode a hex string to bytes
|
| 294 |
+
|
| 295 |
+
Unlike `Buffer.from()`, throws on invalid input
|
| 296 |
+
|
| 297 |
+
#### `toHex(arr)`
|
| 298 |
+
|
| 299 |
+
Encode a `Uint8Array` to a lowercase hex string
|
| 300 |
+
|
| 301 |
+
### @exodus/bytes/base64.js <sub></sub>
|
| 302 |
+
|
| 303 |
+
Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
|
| 304 |
+
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
|
| 305 |
+
|
| 306 |
+
```js
|
| 307 |
+
import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
|
| 308 |
+
import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
|
| 309 |
+
import { fromBase64any } from '@exodus/bytes/base64.js'
|
| 310 |
+
```
|
| 311 |
+
|
| 312 |
+
#### `fromBase64(string, { format = 'uint8', padding = 'both' })`
|
| 313 |
+
|
| 314 |
+
Decode a base64 string to bytes
|
| 315 |
+
|
| 316 |
+
Operates in strict mode for last chunk, does not allow whitespace
|
| 317 |
+
|
| 318 |
+
#### `fromBase64url(string, { format = 'uint8', padding = false })`
|
| 319 |
+
|
| 320 |
+
Decode a base64url string to bytes
|
| 321 |
+
|
| 322 |
+
Operates in strict mode for last chunk, does not allow whitespace
|
| 323 |
+
|
| 324 |
+
#### `fromBase64any(string, { format = 'uint8', padding = 'both' })`
|
| 325 |
+
|
| 326 |
+
Decode either base64 or base64url string to bytes
|
| 327 |
+
|
| 328 |
+
Automatically detects the variant based on characters present
|
| 329 |
+
|
| 330 |
+
#### `toBase64(arr, { padding = true })`
|
| 331 |
+
|
| 332 |
+
Encode a `Uint8Array` to a base64 string (RFC 4648)
|
| 333 |
+
|
| 334 |
+
#### `toBase64url(arr, { padding = false })`
|
| 335 |
+
|
| 336 |
+
Encode a `Uint8Array` to a base64url string (RFC 4648)
|
| 337 |
+
|
| 338 |
+
### @exodus/bytes/base32.js <sub></sub>
|
| 339 |
+
|
| 340 |
+
Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
|
| 341 |
+
(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
|
| 342 |
+
|
| 343 |
+
```js
|
| 344 |
+
import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
|
| 345 |
+
import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
#### `fromBase32(string, { format = 'uint8', padding = 'both' })`
|
| 349 |
+
|
| 350 |
+
Decode a base32 string to bytes
|
| 351 |
+
|
| 352 |
+
Operates in strict mode for last chunk, does not allow whitespace
|
| 353 |
+
|
| 354 |
+
#### `fromBase32hex(string, { format = 'uint8', padding = 'both' })`
|
| 355 |
+
|
| 356 |
+
Decode a base32hex string to bytes
|
| 357 |
+
|
| 358 |
+
Operates in strict mode for last chunk, does not allow whitespace
|
| 359 |
+
|
| 360 |
+
#### `fromBase32crockford(string, options)`
|
| 361 |
+
|
| 362 |
+
Decode a Crockford base32 string to bytes
|
| 363 |
+
|
| 364 |
+
Operates in strict mode for last chunk, does not allow whitespace
|
| 365 |
+
|
| 366 |
+
Crockford base32 decoding follows extra mapping per spec: `LIli -> 1, Oo -> 0`
|
| 367 |
+
|
| 368 |
+
#### `toBase32(arr, { padding = false })`
|
| 369 |
+
|
| 370 |
+
Encode a `Uint8Array` to a base32 string (RFC 4648)
|
| 371 |
+
|
| 372 |
+
#### `toBase32hex(arr, { padding = false })`
|
| 373 |
+
|
| 374 |
+
Encode a `Uint8Array` to a base32hex string (RFC 4648)
|
| 375 |
+
|
| 376 |
+
#### `toBase32crockford(arr, options)`
|
| 377 |
+
|
| 378 |
+
Encode a `Uint8Array` to a Crockford base32 string
|
| 379 |
+
|
| 380 |
+
### @exodus/bytes/bech32.js <sub></sub>
|
| 381 |
+
|
| 382 |
+
Implements bech32 and bech32m from
|
| 383 |
+
[BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
|
| 384 |
+
and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
|
| 385 |
+
|
| 386 |
+
```js
|
| 387 |
+
import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
|
| 388 |
+
import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'
|
| 389 |
+
import { getPrefix } from '@exodus/bytes/bech32.js'
|
| 390 |
+
```
|
| 391 |
+
|
| 392 |
+
#### `getPrefix(string, limit = 90)`
|
| 393 |
+
|
| 394 |
+
Extract the prefix from a bech32 or bech32m string without full validation
|
| 395 |
+
|
| 396 |
+
This is a quick check that skips most validation.
|
| 397 |
+
|
| 398 |
+
#### `fromBech32(string, limit = 90)`
|
| 399 |
+
|
| 400 |
+
Decode a bech32 string to bytes
|
| 401 |
+
|
| 402 |
+
#### `toBech32(prefix, bytes, limit = 90)`
|
| 403 |
+
|
| 404 |
+
Encode bytes to a bech32 string
|
| 405 |
+
|
| 406 |
+
#### `fromBech32m(string, limit = 90)`
|
| 407 |
+
|
| 408 |
+
Decode a bech32m string to bytes
|
| 409 |
+
|
| 410 |
+
#### `toBech32m(prefix, bytes, limit = 90)`
|
| 411 |
+
|
| 412 |
+
Encode bytes to a bech32m string
|
| 413 |
+
|
| 414 |
+
### @exodus/bytes/base58.js <sub></sub>
|
| 415 |
+
|
| 416 |
+
Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.
|
| 417 |
+
|
| 418 |
+
Supports both standard base58 and XRP variant alphabets.
|
| 419 |
+
|
| 420 |
+
```js
|
| 421 |
+
import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
|
| 422 |
+
import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
|
| 423 |
+
```
|
| 424 |
+
|
| 425 |
+
#### `fromBase58(string, format = 'uint8')`
|
| 426 |
+
|
| 427 |
+
Decode a base58 string to bytes
|
| 428 |
+
|
| 429 |
+
Uses the standard Bitcoin base58 alphabet
|
| 430 |
+
|
| 431 |
+
#### `toBase58(arr)`
|
| 432 |
+
|
| 433 |
+
Encode a `Uint8Array` to a base58 string
|
| 434 |
+
|
| 435 |
+
Uses the standard Bitcoin base58 alphabet
|
| 436 |
+
|
| 437 |
+
#### `fromBase58xrp(string, format = 'uint8')`
|
| 438 |
+
|
| 439 |
+
Decode a base58 string to bytes using XRP alphabet
|
| 440 |
+
|
| 441 |
+
Uses the XRP variant base58 alphabet
|
| 442 |
+
|
| 443 |
+
#### `toBase58xrp(arr)`
|
| 444 |
+
|
| 445 |
+
Encode a `Uint8Array` to a base58 string using XRP alphabet
|
| 446 |
+
|
| 447 |
+
Uses the XRP variant base58 alphabet
|
| 448 |
+
|
| 449 |
+
### @exodus/bytes/base58check.js <sub></sub>
|
| 450 |
+
|
| 451 |
+
Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
|
| 452 |
+
|
| 453 |
+
```js
|
| 454 |
+
import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
|
| 455 |
+
import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
|
| 456 |
+
import { makeBase58check } from '@exodus/bytes/base58check.js'
|
| 457 |
+
```
|
| 458 |
+
|
| 459 |
+
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
|
| 460 |
+
|
| 461 |
+
#### `async fromBase58check(string, format = 'uint8')`
|
| 462 |
+
|
| 463 |
+
Decode a base58check string to bytes asynchronously
|
| 464 |
+
|
| 465 |
+
Validates the checksum using double SHA-256
|
| 466 |
+
|
| 467 |
+
#### `async toBase58check(arr)`
|
| 468 |
+
|
| 469 |
+
Encode bytes to base58check string asynchronously
|
| 470 |
+
|
| 471 |
+
Uses double SHA-256 for checksum calculation
|
| 472 |
+
|
| 473 |
+
#### `fromBase58checkSync(string, format = 'uint8')`
|
| 474 |
+
|
| 475 |
+
Decode a base58check string to bytes synchronously
|
| 476 |
+
|
| 477 |
+
Validates the checksum using double SHA-256
|
| 478 |
+
|
| 479 |
+
#### `toBase58checkSync(arr)`
|
| 480 |
+
|
| 481 |
+
Encode bytes to base58check string synchronously
|
| 482 |
+
|
| 483 |
+
Uses double SHA-256 for checksum calculation
|
| 484 |
+
|
| 485 |
+
#### `makeBase58check(hashAlgo, hashAlgoSync)`
|
| 486 |
+
|
| 487 |
+
Create a base58check encoder/decoder with custom hash functions
|
| 488 |
+
|
| 489 |
+
### @exodus/bytes/wif.js <sub></sub>
|
| 490 |
+
|
| 491 |
+
Wallet Import Format (WIF) encoding and decoding.
|
| 492 |
+
|
| 493 |
+
```js
|
| 494 |
+
import { fromWifString, toWifString } from '@exodus/bytes/wif.js'
|
| 495 |
+
import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
|
| 496 |
+
```
|
| 497 |
+
|
| 498 |
+
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
|
| 499 |
+
|
| 500 |
+
#### `async fromWifString(string[, version])`
|
| 501 |
+
|
| 502 |
+
Decode a WIF string to WIF data
|
| 503 |
+
|
| 504 |
+
Returns a promise that resolves to an object with `{ version, privateKey, compressed }`.
|
| 505 |
+
|
| 506 |
+
The optional `version` parameter validates the version byte.
|
| 507 |
+
|
| 508 |
+
Throws if the WIF string is invalid or version doesn't match.
|
| 509 |
+
|
| 510 |
+
#### `fromWifStringSync(string[, version])`
|
| 511 |
+
|
| 512 |
+
Decode a WIF string to WIF data (synchronous)
|
| 513 |
+
|
| 514 |
+
Returns an object with `{ version, privateKey, compressed }`.
|
| 515 |
+
|
| 516 |
+
The optional `version` parameter validates the version byte.
|
| 517 |
+
|
| 518 |
+
Throws if the WIF string is invalid or version doesn't match.
|
| 519 |
+
|
| 520 |
+
#### `async toWifString({ version, privateKey, compressed })`
|
| 521 |
+
|
| 522 |
+
Encode WIF data to a WIF string
|
| 523 |
+
|
| 524 |
+
#### `toWifStringSync({ version, privateKey, compressed })`
|
| 525 |
+
|
| 526 |
+
Encode WIF data to a WIF string (synchronous)
|
| 527 |
+
|
| 528 |
+
### @exodus/bytes/array.js <sub></sub>
|
| 529 |
+
|
| 530 |
+
TypedArray utils and conversions.
|
| 531 |
+
|
| 532 |
+
```js
|
| 533 |
+
import { typedCopyBytes, typedView } from '@exodus/bytes/array.js'
|
| 534 |
+
```
|
| 535 |
+
|
| 536 |
+
#### `typedCopyBytes(arr, format = 'uint8')`
|
| 537 |
+
|
| 538 |
+
Create a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
|
| 539 |
+
|
| 540 |
+
This does not copy _values_, but copies the underlying bytes.
|
| 541 |
+
The result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.
|
| 542 |
+
|
| 543 |
+
> [!WARNING]
|
| 544 |
+
> Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`)
|
| 545 |
+
> is platform endianness-dependent.
|
| 546 |
+
|
| 547 |
+
> [!NOTE]
|
| 548 |
+
> Buffer might be pooled.
|
| 549 |
+
> Uint8Array return values are not pooled and match their underlying ArrayBuffer.
|
| 550 |
+
|
| 551 |
+
#### `typedView(arr, format = 'uint8')`
|
| 552 |
+
|
| 553 |
+
Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
|
| 554 |
+
|
| 555 |
+
> [!IMPORTANT]
|
| 556 |
+
> Does not copy data, returns a view on the same underlying buffer
|
| 557 |
+
|
| 558 |
+
> [!WARNING]
|
| 559 |
+
> Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`) as bytes
|
| 560 |
+
> is platform endianness-dependent.
|
| 561 |
+
|
| 562 |
+
### @exodus/bytes/encoding.js <sub></sub>
|
| 563 |
+
|
| 564 |
+
Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
|
| 565 |
+
[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
|
| 566 |
+
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder),
|
| 567 |
+
[TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream),
|
| 568 |
+
[TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream),
|
| 569 |
+
some [hooks](https://encoding.spec.whatwg.org/#specification-hooks).
|
| 570 |
+
|
| 571 |
+
```js
|
| 572 |
+
import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
|
| 573 |
+
import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
|
| 574 |
+
import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding.js'
|
| 575 |
+
|
| 576 |
+
// Hooks for standards
|
| 577 |
+
import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
|
| 578 |
+
```
|
| 579 |
+
|
| 580 |
+
#### `new TextDecoder(label = 'utf-8', { fatal = false, ignoreBOM = false })`
|
| 581 |
+
|
| 582 |
+
[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
|
| 583 |
+
|
| 584 |
+
Decode bytes to strings according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
|
| 585 |
+
|
| 586 |
+
#### `new TextEncoder()`
|
| 587 |
+
|
| 588 |
+
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) implementation/polyfill.
|
| 589 |
+
|
| 590 |
+
Encode strings to UTF-8 bytes according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
|
| 591 |
+
|
| 592 |
+
#### `new TextDecoderStream(label = 'utf-8', { fatal = false, ignoreBOM = false })`
|
| 593 |
+
|
| 594 |
+
[TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream) implementation/polyfill.
|
| 595 |
+
|
| 596 |
+
A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextDecoder`.
|
| 597 |
+
|
| 598 |
+
Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
|
| 599 |
+
[polyfilled](https://npmjs.com/package/web-streams-polyfill).
|
| 600 |
+
|
| 601 |
+
#### `new TextEncoderStream()`
|
| 602 |
+
|
| 603 |
+
[TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream) implementation/polyfill.
|
| 604 |
+
|
| 605 |
+
A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.
|
| 606 |
+
|
| 607 |
+
Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
|
| 608 |
+
[polyfilled](https://npmjs.com/package/web-streams-polyfill).
|
| 609 |
+
|
| 610 |
+
#### `isomorphicDecode(input)`
|
| 611 |
+
|
| 612 |
+
Implements [isomorphic decode](https://infra.spec.whatwg.org/#isomorphic-decode).
|
| 613 |
+
|
| 614 |
+
Given a `TypedArray` or an `ArrayBuffer` instance `input`, creates a string of the same length
|
| 615 |
+
as input byteLength, using bytes from input as codepoints.
|
| 616 |
+
|
| 617 |
+
E.g. for `Uint8Array` input, this is similar to `String.fromCodePoint(...input)`.
|
| 618 |
+
|
| 619 |
+
Wider `TypedArray` inputs, e.g. `Uint16Array`, are interpreted as underlying _bytes_.
|
| 620 |
+
|
| 621 |
+
#### `isomorphicEncode(str)`
|
| 622 |
+
|
| 623 |
+
Implements [isomorphic encode](https://infra.spec.whatwg.org/#isomorphic-encode).
|
| 624 |
+
|
| 625 |
+
Given a string, creates an `Uint8Array` of the same length with the string codepoints as byte values.
|
| 626 |
+
|
| 627 |
+
Accepts only [isomorphic string](https://infra.spec.whatwg.org/#isomorphic-string) input
|
| 628 |
+
and asserts that, throwing on any strings containing codepoints higher than `U+00FF`.
|
| 629 |
+
|
| 630 |
+
#### `labelToName(label)`
|
| 631 |
+
|
| 632 |
+
Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
|
| 633 |
+
|
| 634 |
+
Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
|
| 635 |
+
as a case-sensitive string.
|
| 636 |
+
|
| 637 |
+
If an encoding with that label does not exist, returns `null`.
|
| 638 |
+
|
| 639 |
+
All encoding names are also valid labels for corresponding encodings.
|
| 640 |
+
|
| 641 |
+
#### `normalizeEncoding(label)`
|
| 642 |
+
|
| 643 |
+
Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
|
| 644 |
+
as an ASCII-lowercased string.
|
| 645 |
+
|
| 646 |
+
If an encoding with that label does not exist, returns `null`.
|
| 647 |
+
|
| 648 |
+
This is the same as [`decoder.encoding` getter](https://encoding.spec.whatwg.org/#dom-textdecoder-encoding),
|
| 649 |
+
except that it:
|
| 650 |
+
1. Supports [`replacement` encoding](https://encoding.spec.whatwg.org/#replacement) and its
|
| 651 |
+
[labels](https://encoding.spec.whatwg.org/#ref-for-replacement%E2%91%A1)
|
| 652 |
+
2. Does not throw for invalid labels and instead returns `null`
|
| 653 |
+
|
| 654 |
+
It is identical to:
|
| 655 |
+
```js
|
| 656 |
+
labelToName(label)?.toLowerCase() ?? null
|
| 657 |
+
```
|
| 658 |
+
|
| 659 |
+
All encoding names are also valid labels for corresponding encodings.
|
| 660 |
+
|
| 661 |
+
#### `getBOMEncoding(input)`
|
| 662 |
+
|
| 663 |
+
Implements [BOM sniff](https://encoding.spec.whatwg.org/#bom-sniff) legacy hook.
|
| 664 |
+
|
| 665 |
+
Given a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:
|
| 666 |
+
- `'utf-8'`, if `input` starts with UTF-8 byte order mark.
|
| 667 |
+
- `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
|
| 668 |
+
- `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
|
| 669 |
+
- `null` otherwise.
|
| 670 |
+
|
| 671 |
+
#### `legacyHookDecode(input, fallbackEncoding = 'utf-8')`
|
| 672 |
+
|
| 673 |
+
Implements [decode](https://encoding.spec.whatwg.org/#decode) legacy hook.
|
| 674 |
+
|
| 675 |
+
Given a `TypedArray` or an `ArrayBuffer` instance `input` and an optional `fallbackEncoding`
|
| 676 |
+
encoding [label](https://encoding.spec.whatwg.org/#names-and-labels),
|
| 677 |
+
sniffs encoding from BOM with `fallbackEncoding` fallback and then
|
| 678 |
+
decodes the `input` using that encoding, skipping BOM if it was present.
|
| 679 |
+
|
| 680 |
+
Notes:
|
| 681 |
+
|
| 682 |
+
- BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
|
| 683 |
+
Use with care.
|
| 684 |
+
- Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
|
| 685 |
+
aka replacement. It can convert different byte sequences to equal strings.
|
| 686 |
+
|
| 687 |
+
This method is similar to the following code, except that it doesn't support encoding labels and
|
| 688 |
+
only expects lowercased encoding name:
|
| 689 |
+
|
| 690 |
+
```js
|
| 691 |
+
new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)
|
| 692 |
+
```
|
| 693 |
+
|
| 694 |
+
### @exodus/bytes/encoding-lite.js <sub></sub>
|
| 695 |
+
|
| 696 |
+
The exact same exports as `@exodus/bytes/encoding.js` are also exported as
|
| 697 |
+
`@exodus/bytes/encoding-lite.js`, with the difference that the lite version does not load
|
| 698 |
+
multi-byte `TextDecoder` encodings by default to reduce bundle size ~12x.
|
| 699 |
+
|
| 700 |
+
```js
|
| 701 |
+
import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'
|
| 702 |
+
import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js' // Requires Streams
|
| 703 |
+
import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding-lite.js'
|
| 704 |
+
|
| 705 |
+
// Hooks for standards
|
| 706 |
+
import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
|
| 707 |
+
```
|
| 708 |
+
|
| 709 |
+
The only affected encodings are: `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`
|
| 710 |
+
and their [labels](https://encoding.spec.whatwg.org/#names-and-labels) when used with `TextDecoder`.
|
| 711 |
+
|
| 712 |
+
Legacy single-byte encodingds are loaded by default in both cases.
|
| 713 |
+
|
| 714 |
+
`TextEncoder` and hooks for standards (including `labelToName` / `normalizeEncoding`) do not have any behavior
|
| 715 |
+
differences in the lite version and support full range if inputs.
|
| 716 |
+
|
| 717 |
+
To avoid inconsistencies, the exported classes and methods are exactly the same objects.
|
| 718 |
+
|
| 719 |
+
```console
|
| 720 |
+
> lite = require('@exodus/bytes/encoding-lite.js')
|
| 721 |
+
[Module: null prototype] {
|
| 722 |
+
TextDecoder: [class TextDecoder],
|
| 723 |
+
TextDecoderStream: [class TextDecoderStream],
|
| 724 |
+
TextEncoder: [class TextEncoder],
|
| 725 |
+
TextEncoderStream: [class TextEncoderStream],
|
| 726 |
+
getBOMEncoding: [Function: getBOMEncoding],
|
| 727 |
+
labelToName: [Function: labelToName],
|
| 728 |
+
legacyHookDecode: [Function: legacyHookDecode],
|
| 729 |
+
normalizeEncoding: [Function: normalizeEncoding]
|
| 730 |
+
}
|
| 731 |
+
> new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
| 732 |
+
Uncaught:
|
| 733 |
+
Error: Legacy multi-byte encodings are disabled in /encoding-lite.js, use /encoding.js for full encodings range support
|
| 734 |
+
|
| 735 |
+
> full = require('@exodus/bytes/encoding.js')
|
| 736 |
+
[Module: null prototype] {
|
| 737 |
+
TextDecoder: [class TextDecoder],
|
| 738 |
+
TextDecoderStream: [class TextDecoderStream],
|
| 739 |
+
TextEncoder: [class TextEncoder],
|
| 740 |
+
TextEncoderStream: [class TextEncoderStream],
|
| 741 |
+
getBOMEncoding: [Function: getBOMEncoding],
|
| 742 |
+
labelToName: [Function: labelToName],
|
| 743 |
+
legacyHookDecode: [Function: legacyHookDecode],
|
| 744 |
+
normalizeEncoding: [Function: normalizeEncoding]
|
| 745 |
+
}
|
| 746 |
+
> full.TextDecoder === lite.TextDecoder
|
| 747 |
+
true
|
| 748 |
+
> new full.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
| 749 |
+
'%'
|
| 750 |
+
> new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
| 751 |
+
'%'
|
| 752 |
+
```
|
| 753 |
+
|
| 754 |
+
### @exodus/bytes/encoding-browser.js <sub></sub>
|
| 755 |
+
|
| 756 |
+
Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
|
| 757 |
+
browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
|
| 758 |
+
|
| 759 |
+
Does not provide `isomorphicDecode` and `isomorphicEncode` exports.
|
| 760 |
+
|
| 761 |
+
```js
|
| 762 |
+
import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'
|
| 763 |
+
import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams
|
| 764 |
+
|
| 765 |
+
// Hooks for standards
|
| 766 |
+
import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'
|
| 767 |
+
```
|
| 768 |
+
|
| 769 |
+
Under non-browser engines (Node.js, React Native, etc.) a full polyfill is used as those platforms
|
| 770 |
+
do not provide sufficiently complete / non-buggy `TextDecoder` APIs.
|
| 771 |
+
|
| 772 |
+
> [!NOTE]
|
| 773 |
+
> Implementations in browsers [have bugs](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit),
|
| 774 |
+
> but they are fixing them and the expected update window is short.\
|
| 775 |
+
> If you want to circumvent browser bugs, use full `@exodus/bytes/encoding.js` import.
|
| 776 |
+
|
| 777 |
+
### @exodus/bytes/whatwg.js <sub></sub>
|
| 778 |
+
|
| 779 |
+
WHATWG helpers
|
| 780 |
+
|
| 781 |
+
```js
|
| 782 |
+
import '@exodus/bytes/encoding.js' // For full legacy multi-byte encodings support
|
| 783 |
+
import { percentEncodeAfterEncoding } from '@exodus/bytes/whatwg.js'
|
| 784 |
+
```
|
| 785 |
+
|
| 786 |
+
#### `percentEncodeAfterEncoding(encoding, input, percentEncodeSet, spaceAsPlus = false)`
|
| 787 |
+
|
| 788 |
+
Implements [percent-encode after encoding](https://url.spec.whatwg.org/#string-percent-encode-after-encoding)
|
| 789 |
+
per WHATWG URL specification.
|
| 790 |
+
|
| 791 |
+
> [!IMPORTANT]
|
| 792 |
+
> You must import `@exodus/bytes/encoding.js` for this API to accept legacy multi-byte encodings.
|
| 793 |
+
|
| 794 |
+
Encodings `utf16-le`, `utf16-be`, and `replacement` are not accepted.
|
| 795 |
+
|
| 796 |
+
[C0 control percent-encode set](https://url.spec.whatwg.org/#c0-control-percent-encode-set) is
|
| 797 |
+
always percent-encoded.
|
| 798 |
+
|
| 799 |
+
`percentEncodeSet` is an addition to that, and must be a string of unique increasing codepoints
|
| 800 |
+
in range 0x20 - 0x7e, e.g. `' "#<>'`.
|
| 801 |
+
|
| 802 |
+
This method accepts [DOMStrings](https://webidl.spec.whatwg.org/#idl-DOMString) and converts them
|
| 803 |
+
to [USVStrings](https://webidl.spec.whatwg.org/#idl-USVString).
|
| 804 |
+
This is different from e.g. `encodeURI` and `encodeURIComponent` which throw on surrogates:
|
| 805 |
+
```js
|
| 806 |
+
> percentEncodeAfterEncoding('utf8', '\ud800', ' "#$%&+,/:;<=>?@[\\]^`{|}') // component
|
| 807 |
+
'%EF%BF%BD'
|
| 808 |
+
> encodeURIComponent('\ud800')
|
| 809 |
+
Uncaught URIError: URI malformed
|
| 810 |
+
```
|
| 811 |
+
|
| 812 |
+
## Changelog
|
| 813 |
+
|
| 814 |
+
See [GitHub Releases](https://github.com/ExodusOSS/bytes/releases) tab
|
| 815 |
+
|
| 816 |
+
## License
|
| 817 |
+
|
| 818 |
+
[MIT](./LICENSE)
|
node_modules/@exodus/bytes/array.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* TypedArray utils and conversions.
|
| 3 |
+
*
|
| 4 |
+
* ```js
|
| 5 |
+
* import { typedCopyBytes, typedView } from '@exodus/bytes/array.js'
|
| 6 |
+
* ```
|
| 7 |
+
*
|
| 8 |
+
* @module @exodus/bytes/array.js
|
| 9 |
+
*/
|
| 10 |
+
|
| 11 |
+
/// <reference types="node" />
|
| 12 |
+
|
| 13 |
+
// >= TypeScript 5.9 made Uint8Array templated with <> and defaulted to ArrayBufferLike
|
| 14 |
+
// which would incorrectly accept SharedArrayBuffer instances.
|
| 15 |
+
// < TypeScript 5.7 doesn't support templates for Uint8Array.
|
| 16 |
+
// So this type is defined as a workaround to evaluate to Uint8Array<ArrayBuffer> on all versions of TypeScript.
|
| 17 |
+
|
| 18 |
+
/**
|
| 19 |
+
* This is `Uint8Array<ArrayBuffer>`
|
| 20 |
+
* (as opposed to `Uint8Array<SharedArrayBuffer>` and `Uint8Array<ArrayBufferLike>`)
|
| 21 |
+
* on TypeScript versions that support that distinction.
|
| 22 |
+
*
|
| 23 |
+
* On TypeScript < 5.7, this is just `Uint8Array`, as it's not a template there.
|
| 24 |
+
*/
|
| 25 |
+
export type Uint8ArrayBuffer = ReturnType<typeof Uint8Array.from>;
|
| 26 |
+
|
| 27 |
+
/**
|
| 28 |
+
* This is `Uint16Array<ArrayBuffer>`
|
| 29 |
+
* (as opposed to `Uint16Array<SharedArrayBuffer>` and `Uint16Array<ArrayBufferLike>`)
|
| 30 |
+
* on TypeScript versions that support that distinction.
|
| 31 |
+
*
|
| 32 |
+
* On TypeScript < 5.7, this is just `Uint16Array`, as it's not a template there.
|
| 33 |
+
*/
|
| 34 |
+
export type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>;
|
| 35 |
+
|
| 36 |
+
/**
|
| 37 |
+
* This is `Uint32Array<ArrayBuffer>`
|
| 38 |
+
* (as opposed to `Uint32Array<SharedArrayBuffer>` and `Uint32Array<ArrayBufferLike>`)
|
| 39 |
+
* on TypeScript versions that support that distinction.
|
| 40 |
+
*
|
| 41 |
+
* On TypeScript < 5.7, this is just `Uint32Array`, as it's not a template there.
|
| 42 |
+
*/
|
| 43 |
+
export type Uint32ArrayBuffer = ReturnType<typeof Uint32Array.from>;
|
| 44 |
+
|
| 45 |
+
/**
|
| 46 |
+
* Output format for typed array conversions
|
| 47 |
+
*/
|
| 48 |
+
export type OutputFormat = 'uint8' | 'arraybuffer' | 'buffer';
|
| 49 |
+
|
| 50 |
+
/**
|
| 51 |
+
* Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
|
| 52 |
+
*
|
| 53 |
+
* > [!IMPORTANT]
|
| 54 |
+
* > Does not copy data, returns a view on the same underlying buffer
|
| 55 |
+
*
|
| 56 |
+
* > [!WARNING]
|
| 57 |
+
* > Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`) as bytes
|
| 58 |
+
* > is platform endianness-dependent.
|
| 59 |
+
*
|
| 60 |
+
* @param arr - The input TypedArray
|
| 61 |
+
* @param format - The desired output format (`'uint8'` or `'buffer'`)
|
| 62 |
+
* @returns A view on the same underlying buffer
|
| 63 |
+
*/
|
| 64 |
+
export function typedView(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
|
| 65 |
+
export function typedView(arr: ArrayBufferView, format: 'buffer'): Buffer;
|
| 66 |
+
export function typedView(arr: ArrayBufferView, format: 'uint8' | 'buffer'): Uint8Array | Buffer;
|
| 67 |
+
|
| 68 |
+
/**
|
| 69 |
+
* Create a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
|
| 70 |
+
*
|
| 71 |
+
* This does not copy _values_, but copies the underlying bytes.
|
| 72 |
+
* The result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.
|
| 73 |
+
*
|
| 74 |
+
* > [!WARNING]
|
| 75 |
+
* > Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`)
|
| 76 |
+
* > is platform endianness-dependent.
|
| 77 |
+
*
|
| 78 |
+
* > [!NOTE]
|
| 79 |
+
* > Buffer might be pooled.
|
| 80 |
+
* > Uint8Array return values are not pooled and match their underlying ArrayBuffer.
|
| 81 |
+
*
|
| 82 |
+
* @param arr - The input TypedArray
|
| 83 |
+
* @param format - The desired output format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
|
| 84 |
+
* @returns A copy of the underlying buffer
|
| 85 |
+
*/
|
| 86 |
+
export function typedCopyBytes(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
|
| 87 |
+
export function typedCopyBytes(arr: ArrayBufferView, format: 'arraybuffer'): ArrayBuffer;
|
| 88 |
+
export function typedCopyBytes(arr: ArrayBufferView, format: 'buffer'): Buffer;
|
| 89 |
+
export function typedCopyBytes(arr: ArrayBufferView, format: OutputFormat): Uint8Array | ArrayBuffer | Buffer;
|
node_modules/@exodus/bytes/array.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { assertTypedArray } from './assert.js'
|
| 2 |
+
|
| 3 |
+
const Buffer = globalThis.Buffer // Buffer is optional
|
| 4 |
+
|
| 5 |
+
export function typedView(arr, format) {
|
| 6 |
+
assertTypedArray(arr)
|
| 7 |
+
switch (format) {
|
| 8 |
+
case 'uint8':
|
| 9 |
+
if (arr.constructor === Uint8Array) return arr // fast path
|
| 10 |
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)
|
| 11 |
+
case 'buffer':
|
| 12 |
+
if (arr.constructor === Buffer && Buffer.isBuffer(arr)) return arr
|
| 13 |
+
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
throw new TypeError('Unexpected format')
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export function typedCopyBytes(arr, format) {
|
| 20 |
+
assertTypedArray(arr)
|
| 21 |
+
if (!(arr instanceof Uint8Array)) {
|
| 22 |
+
arr = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
switch (format) {
|
| 26 |
+
case 'uint8':
|
| 27 |
+
return Uint8Array.from(arr) // never pooled
|
| 28 |
+
case 'buffer':
|
| 29 |
+
return Buffer.from(arr)
|
| 30 |
+
case 'arraybuffer':
|
| 31 |
+
return Uint8Array.from(arr).buffer
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
throw new TypeError('Unexpected format')
|
| 35 |
+
}
|
node_modules/@exodus/bytes/assert.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function assertEmptyRest(rest) {
|
| 2 |
+
if (Object.keys(rest).length > 0) throw new TypeError('Unexpected extra options')
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
// eslint-disable-next-line sonarjs/no-nested-template-literals
|
| 6 |
+
const makeMessage = (name, extra) => `Expected${name ? ` ${name} to be` : ''} an Uint8Array${extra}`
|
| 7 |
+
|
| 8 |
+
const TypedArray = Object.getPrototypeOf(Uint8Array)
|
| 9 |
+
|
| 10 |
+
export function assertTypedArray(arr) {
|
| 11 |
+
if (arr && arr instanceof TypedArray) return
|
| 12 |
+
throw new TypeError('Expected a TypedArray instance')
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
export function assertUint8(arr, options) {
|
| 16 |
+
if (!options) {
|
| 17 |
+
// fast path
|
| 18 |
+
if (arr instanceof Uint8Array) return
|
| 19 |
+
throw new TypeError('Expected an Uint8Array')
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const { name, length, ...rest } = options
|
| 23 |
+
assertEmptyRest(rest)
|
| 24 |
+
if (arr instanceof Uint8Array && (length === undefined || arr.length === length)) return
|
| 25 |
+
throw new TypeError(makeMessage(name, length === undefined ? '' : ` of size ${Number(length)}`))
|
| 26 |
+
}
|
node_modules/@exodus/bytes/base32.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Implements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
|
| 3 |
+
* (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
|
| 4 |
+
*
|
| 5 |
+
* ```js
|
| 6 |
+
* import { fromBase32, toBase32 } from '@exodus/bytes/base32.js'
|
| 7 |
+
* import { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'
|
| 8 |
+
* ```
|
| 9 |
+
*
|
| 10 |
+
* @module @exodus/bytes/base32.js
|
| 11 |
+
*/
|
| 12 |
+
|
| 13 |
+
/// <reference types="node" />
|
| 14 |
+
|
| 15 |
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
| 16 |
+
|
| 17 |
+
/**
|
| 18 |
+
* Options for base32 encoding
|
| 19 |
+
*/
|
| 20 |
+
export interface ToBase32Options {
|
| 21 |
+
/** Whether to include padding characters (default: false) */
|
| 22 |
+
padding?: boolean;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/**
|
| 26 |
+
* Padding mode for base32 decoding
|
| 27 |
+
* - `true`: padding is required
|
| 28 |
+
* - `false`: padding is not allowed
|
| 29 |
+
* - `'both'`: padding is optional (default)
|
| 30 |
+
*/
|
| 31 |
+
export type PaddingMode = boolean | 'both';
|
| 32 |
+
|
| 33 |
+
/**
|
| 34 |
+
* Options for base32 decoding
|
| 35 |
+
*/
|
| 36 |
+
export interface FromBase32Options {
|
| 37 |
+
/** Output format (default: 'uint8') */
|
| 38 |
+
format?: OutputFormat;
|
| 39 |
+
/** Padding mode */
|
| 40 |
+
padding?: PaddingMode;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/**
|
| 44 |
+
* Encode a `Uint8Array` to a base32 string (RFC 4648)
|
| 45 |
+
*
|
| 46 |
+
* @param arr - The input bytes
|
| 47 |
+
* @param options - Encoding options
|
| 48 |
+
* @returns The base32 encoded string
|
| 49 |
+
*/
|
| 50 |
+
export function toBase32(arr: Uint8Array, options?: ToBase32Options): string;
|
| 51 |
+
|
| 52 |
+
/**
|
| 53 |
+
* Encode a `Uint8Array` to a base32hex string (RFC 4648)
|
| 54 |
+
*
|
| 55 |
+
* @param arr - The input bytes
|
| 56 |
+
* @param options - Encoding options (padding defaults to false)
|
| 57 |
+
* @returns The base32hex encoded string
|
| 58 |
+
*/
|
| 59 |
+
export function toBase32hex(arr: Uint8Array, options?: ToBase32Options): string;
|
| 60 |
+
|
| 61 |
+
/**
|
| 62 |
+
* Encode a `Uint8Array` to a Crockford base32 string
|
| 63 |
+
*
|
| 64 |
+
* @param arr - The input bytes
|
| 65 |
+
* @param options - Encoding options (padding defaults to false)
|
| 66 |
+
* @returns The Crockford base32 encoded string
|
| 67 |
+
*/
|
| 68 |
+
export function toBase32crockford(arr: Uint8Array, options?: ToBase32Options): string;
|
| 69 |
+
|
| 70 |
+
/**
|
| 71 |
+
* Decode a base32 string to bytes
|
| 72 |
+
*
|
| 73 |
+
* Operates in strict mode for last chunk, does not allow whitespace
|
| 74 |
+
*
|
| 75 |
+
* @param string - The base32 encoded string
|
| 76 |
+
* @param options - Decoding options
|
| 77 |
+
* @returns The decoded bytes
|
| 78 |
+
*/
|
| 79 |
+
export function fromBase32(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
|
| 80 |
+
export function fromBase32(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
|
| 81 |
+
export function fromBase32(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
|
| 82 |
+
export function fromBase32(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 83 |
+
|
| 84 |
+
/**
|
| 85 |
+
* Decode a base32hex string to bytes
|
| 86 |
+
*
|
| 87 |
+
* Operates in strict mode for last chunk, does not allow whitespace
|
| 88 |
+
*
|
| 89 |
+
* @param string - The base32hex encoded string
|
| 90 |
+
* @param options - Decoding options
|
| 91 |
+
* @returns The decoded bytes
|
| 92 |
+
*/
|
| 93 |
+
export function fromBase32hex(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
|
| 94 |
+
export function fromBase32hex(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
|
| 95 |
+
export function fromBase32hex(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
|
| 96 |
+
export function fromBase32hex(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 97 |
+
|
| 98 |
+
/**
|
| 99 |
+
* Decode a Crockford base32 string to bytes
|
| 100 |
+
*
|
| 101 |
+
* Operates in strict mode for last chunk, does not allow whitespace
|
| 102 |
+
*
|
| 103 |
+
* Crockford base32 decoding follows extra mapping per spec: `LIli -> 1, Oo -> 0`
|
| 104 |
+
*
|
| 105 |
+
* @param string - The Crockford base32 encoded string
|
| 106 |
+
* @param options - Decoding options
|
| 107 |
+
* @returns The decoded bytes
|
| 108 |
+
*/
|
| 109 |
+
export function fromBase32crockford(string: string, options?: FromBase32Options & { format?: 'uint8' }): Uint8ArrayBuffer;
|
| 110 |
+
export function fromBase32crockford(string: string, options: FromBase32Options & { format: 'arraybuffer' }): ArrayBuffer;
|
| 111 |
+
export function fromBase32crockford(string: string, options: FromBase32Options & { format: 'buffer' }): Buffer;
|
| 112 |
+
export function fromBase32crockford(string: string, options?: FromBase32Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
node_modules/@exodus/bytes/base32.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { assertEmptyRest } from './assert.js'
|
| 2 |
+
import { fromUint8, E_STRING } from './fallback/_utils.js'
|
| 3 |
+
import * as js from './fallback/base32.js'
|
| 4 |
+
|
| 5 |
+
// See https://datatracker.ietf.org/doc/html/rfc4648
|
| 6 |
+
|
| 7 |
+
// 8 chars per 5 bytes
|
| 8 |
+
|
| 9 |
+
function fromBase32common(str, mode, padding, format, rest) {
|
| 10 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 11 |
+
if (rest !== null) assertEmptyRest(rest)
|
| 12 |
+
|
| 13 |
+
if (padding === true) {
|
| 14 |
+
if (str.length % 8 !== 0) throw new SyntaxError(js.E_PADDING)
|
| 15 |
+
} else if (padding === false) {
|
| 16 |
+
if (str.endsWith('=')) throw new SyntaxError('Did not expect padding in base32 input')
|
| 17 |
+
} else if (padding !== 'both') {
|
| 18 |
+
throw new TypeError('Invalid padding option')
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
return fromUint8(js.fromBase32(str, mode), format)
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// By default, valid padding is accepted but not required
|
| 25 |
+
const fromBase32wrap = (mode) => (str, options) => {
|
| 26 |
+
if (!options) return fromBase32common(str, mode, 'both', 'uint8', null)
|
| 27 |
+
const { format = 'uint8', padding = 'both', ...rest } = options
|
| 28 |
+
return fromBase32common(str, mode, padding, format, rest)
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
export const fromBase32 = fromBase32wrap(0)
|
| 32 |
+
export const fromBase32hex = fromBase32wrap(1)
|
| 33 |
+
export const fromBase32crockford = fromBase32wrap(2)
|
| 34 |
+
|
| 35 |
+
export const toBase32 = (arr, { padding = false } = {}) => js.toBase32(arr, 0, padding)
|
| 36 |
+
export const toBase32hex = (arr, { padding = false } = {}) => js.toBase32(arr, 1, padding)
|
| 37 |
+
export const toBase32crockford = (arr, { padding = false } = {}) => js.toBase32(arr, 2, padding)
|
node_modules/@exodus/bytes/base58.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Implements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.
|
| 3 |
+
*
|
| 4 |
+
* Supports both standard base58 and XRP variant alphabets.
|
| 5 |
+
*
|
| 6 |
+
* ```js
|
| 7 |
+
* import { fromBase58, toBase58 } from '@exodus/bytes/base58.js'
|
| 8 |
+
* import { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'
|
| 9 |
+
* ```
|
| 10 |
+
*
|
| 11 |
+
* @module @exodus/bytes/base58.js
|
| 12 |
+
*/
|
| 13 |
+
|
| 14 |
+
/// <reference types="node" />
|
| 15 |
+
|
| 16 |
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
| 17 |
+
|
| 18 |
+
/**
|
| 19 |
+
* Encode a `Uint8Array` to a base58 string
|
| 20 |
+
*
|
| 21 |
+
* Uses the standard Bitcoin base58 alphabet
|
| 22 |
+
*
|
| 23 |
+
* @param arr - The input bytes
|
| 24 |
+
* @returns The base58 encoded string
|
| 25 |
+
*/
|
| 26 |
+
export function toBase58(arr: Uint8Array): string;
|
| 27 |
+
|
| 28 |
+
/**
|
| 29 |
+
* Decode a base58 string to bytes
|
| 30 |
+
*
|
| 31 |
+
* Uses the standard Bitcoin base58 alphabet
|
| 32 |
+
*
|
| 33 |
+
* @param string - The base58 encoded string
|
| 34 |
+
* @param format - Output format (default: 'uint8')
|
| 35 |
+
* @returns The decoded bytes
|
| 36 |
+
*/
|
| 37 |
+
export function fromBase58(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
| 38 |
+
export function fromBase58(string: string, format: 'arraybuffer'): ArrayBuffer;
|
| 39 |
+
export function fromBase58(string: string, format: 'buffer'): Buffer;
|
| 40 |
+
export function fromBase58(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 41 |
+
|
| 42 |
+
/**
|
| 43 |
+
* Encode a `Uint8Array` to a base58 string using XRP alphabet
|
| 44 |
+
*
|
| 45 |
+
* Uses the XRP variant base58 alphabet
|
| 46 |
+
*
|
| 47 |
+
* @param arr - The input bytes
|
| 48 |
+
* @returns The base58 encoded string
|
| 49 |
+
*/
|
| 50 |
+
export function toBase58xrp(arr: Uint8Array): string;
|
| 51 |
+
|
| 52 |
+
/**
|
| 53 |
+
* Decode a base58 string to bytes using XRP alphabet
|
| 54 |
+
*
|
| 55 |
+
* Uses the XRP variant base58 alphabet
|
| 56 |
+
*
|
| 57 |
+
* @param string - The base58 encoded string
|
| 58 |
+
* @param format - Output format (default: 'uint8')
|
| 59 |
+
* @returns The decoded bytes
|
| 60 |
+
*/
|
| 61 |
+
export function fromBase58xrp(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
| 62 |
+
export function fromBase58xrp(string: string, format: 'arraybuffer'): ArrayBuffer;
|
| 63 |
+
export function fromBase58xrp(string: string, format: 'buffer'): Buffer;
|
| 64 |
+
export function fromBase58xrp(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
node_modules/@exodus/bytes/base58.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { assertU8, fromUint8, E_STRING } from './fallback/_utils.js'
|
| 2 |
+
import { nativeDecoder, nativeEncoder, isHermes } from './fallback/platform.js'
|
| 3 |
+
import { encodeAscii, decodeAscii } from './fallback/latin1.js'
|
| 4 |
+
|
| 5 |
+
const alphabet58 = [...'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz']
|
| 6 |
+
const alphabetXRP = [...'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz']
|
| 7 |
+
const codes58 = new Uint8Array(alphabet58.map((x) => x.charCodeAt(0)))
|
| 8 |
+
const codesXRP = new Uint8Array(alphabetXRP.map((x) => x.charCodeAt(0)))
|
| 9 |
+
|
| 10 |
+
const _0n = BigInt(0)
|
| 11 |
+
const _1n = BigInt(1)
|
| 12 |
+
const _8n = BigInt(8)
|
| 13 |
+
const _32n = BigInt(32)
|
| 14 |
+
const _58n = BigInt(58)
|
| 15 |
+
const _0xffffffffn = BigInt(0xff_ff_ff_ff)
|
| 16 |
+
|
| 17 |
+
let table // 15 * 82, diagonal, <1kb
|
| 18 |
+
const fromMaps = new Map()
|
| 19 |
+
|
| 20 |
+
const E_CHAR = 'Invalid character in base58 input'
|
| 21 |
+
|
| 22 |
+
const shouldUseBigIntFrom = isHermes // faster only on Hermes, numbers path beats it on normal engines
|
| 23 |
+
|
| 24 |
+
function toBase58core(arr, alphabet, codes) {
|
| 25 |
+
assertU8(arr)
|
| 26 |
+
const length = arr.length
|
| 27 |
+
if (length === 0) return ''
|
| 28 |
+
|
| 29 |
+
const ZERO = alphabet[0]
|
| 30 |
+
let zeros = 0
|
| 31 |
+
while (zeros < length && arr[zeros] === 0) zeros++
|
| 32 |
+
|
| 33 |
+
if (length > 60) {
|
| 34 |
+
// Slow path. Can be optimized ~10%, but the main factor is /58n division anyway, so doesn't matter much
|
| 35 |
+
let x = _0n
|
| 36 |
+
for (let i = 0; i < arr.length; i++) x = (x << _8n) | BigInt(arr[i])
|
| 37 |
+
|
| 38 |
+
let out = ''
|
| 39 |
+
while (x) {
|
| 40 |
+
const d = x / _58n
|
| 41 |
+
out = alphabet[Number(x - _58n * d)] + out
|
| 42 |
+
x = d
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
return ZERO.repeat(zeros) + out
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// We run fast mode operations only on short (<=60 bytes) inputs, via precomputation table
|
| 49 |
+
if (!table) {
|
| 50 |
+
table = []
|
| 51 |
+
let x = _1n
|
| 52 |
+
for (let i = 0; i < 15; i++) {
|
| 53 |
+
// Convert x to base 58 digits
|
| 54 |
+
const in58 = []
|
| 55 |
+
let y = x
|
| 56 |
+
while (y) {
|
| 57 |
+
const d = y / _58n
|
| 58 |
+
in58.push(Number(y - _58n * d))
|
| 59 |
+
y = d
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
table.push(new Uint8Array(in58))
|
| 63 |
+
x <<= _32n
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
const res = []
|
| 68 |
+
{
|
| 69 |
+
let j = 0
|
| 70 |
+
// We group each 4 bytes into 32-bit chunks
|
| 71 |
+
// Not using u32arr to not deal with remainder + BE/LE differences
|
| 72 |
+
for (let i = length - 1; i >= 0; i -= 4) {
|
| 73 |
+
let c
|
| 74 |
+
if (i > 2) {
|
| 75 |
+
c = (arr[i] | (arr[i - 1] << 8) | (arr[i - 2] << 16) | (arr[i - 3] << 24)) >>> 0
|
| 76 |
+
} else if (i > 1) {
|
| 77 |
+
c = arr[i] | (arr[i - 1] << 8) | (arr[i - 2] << 16)
|
| 78 |
+
} else {
|
| 79 |
+
c = i === 1 ? arr[i] | (arr[i - 1] << 8) : arr[i]
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
const row = table[j++]
|
| 83 |
+
if (c === 0) continue
|
| 84 |
+
const olen = res.length
|
| 85 |
+
const nlen = row.length
|
| 86 |
+
let k = 0
|
| 87 |
+
for (; k < olen; k++) res[k] += c * row[k]
|
| 88 |
+
while (k < nlen) res.push(c * row[k++])
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// We can now do a single scan over regular numbers under MAX_SAFE_INTEGER
|
| 93 |
+
// Note: can't use int32 operations on them, as they are outside of 2**32 range
|
| 94 |
+
// This is faster though
|
| 95 |
+
{
|
| 96 |
+
let carry = 0
|
| 97 |
+
let i = 0
|
| 98 |
+
while (i < res.length) {
|
| 99 |
+
const c = res[i] + carry
|
| 100 |
+
carry = Math.floor(c / 58)
|
| 101 |
+
res[i++] = c - carry * 58
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
while (carry) {
|
| 105 |
+
const c = carry
|
| 106 |
+
carry = Math.floor(c / 58)
|
| 107 |
+
res.push(c - carry * 58)
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
if (nativeDecoder) {
|
| 112 |
+
const oa = new Uint8Array(res.length)
|
| 113 |
+
let j = 0
|
| 114 |
+
for (let i = res.length - 1; i >= 0; i--) oa[j++] = codes[res[i]]
|
| 115 |
+
return ZERO.repeat(zeros) + decodeAscii(oa)
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
let out = ''
|
| 119 |
+
for (let i = res.length - 1; i >= 0; i--) out += alphabet[res[i]]
|
| 120 |
+
return ZERO.repeat(zeros) + out
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
function fromBase58core(str, alphabet, codes, format = 'uint8') {
|
| 124 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 125 |
+
const length = str.length
|
| 126 |
+
if (length === 0) return fromUint8(new Uint8Array(), format)
|
| 127 |
+
|
| 128 |
+
const zeroC = codes[0]
|
| 129 |
+
let zeros = 0
|
| 130 |
+
while (zeros < length && str.charCodeAt(zeros) === zeroC) zeros++
|
| 131 |
+
|
| 132 |
+
let fromMap = fromMaps.get(alphabet)
|
| 133 |
+
if (!fromMap) {
|
| 134 |
+
fromMap = new Int8Array(256).fill(-1)
|
| 135 |
+
for (let i = 0; i < 58; i++) fromMap[alphabet[i].charCodeAt(0)] = i
|
| 136 |
+
fromMaps.set(alphabet, fromMap)
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
const size = zeros + (((length - zeros + 1) * 3) >> 2) // 3/4 rounded up, larger than ~0.73 coef to fit everything
|
| 140 |
+
const res = new Uint8Array(size)
|
| 141 |
+
let at = size // where is the first significant byte written
|
| 142 |
+
|
| 143 |
+
if (shouldUseBigIntFrom) {
|
| 144 |
+
let x = _0n
|
| 145 |
+
|
| 146 |
+
// nativeEncoder gives a benefit here
|
| 147 |
+
if (nativeEncoder) {
|
| 148 |
+
const codes = encodeAscii(str, E_CHAR)
|
| 149 |
+
for (let i = zeros; i < length; i++) {
|
| 150 |
+
const c = fromMap[codes[i]]
|
| 151 |
+
if (c < 0) throw new SyntaxError(E_CHAR)
|
| 152 |
+
x = x * _58n + BigInt(c)
|
| 153 |
+
}
|
| 154 |
+
} else {
|
| 155 |
+
for (let i = zeros; i < length; i++) {
|
| 156 |
+
const charCode = str.charCodeAt(i)
|
| 157 |
+
const c = fromMap[charCode]
|
| 158 |
+
if (charCode > 255 || c < 0) throw new SyntaxError(E_CHAR)
|
| 159 |
+
x = x * _58n + BigInt(c)
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
while (x) {
|
| 164 |
+
let y = Number(x & _0xffffffffn)
|
| 165 |
+
x >>= _32n
|
| 166 |
+
res[--at] = y & 0xff
|
| 167 |
+
y >>>= 8
|
| 168 |
+
if (!x && !y) break
|
| 169 |
+
res[--at] = y & 0xff
|
| 170 |
+
y >>>= 8
|
| 171 |
+
if (!x && !y) break
|
| 172 |
+
res[--at] = y & 0xff
|
| 173 |
+
y >>>= 8
|
| 174 |
+
if (!x && !y) break
|
| 175 |
+
res[--at] = y & 0xff
|
| 176 |
+
}
|
| 177 |
+
} else {
|
| 178 |
+
for (let i = zeros; i < length; i++) {
|
| 179 |
+
const charCode = str.charCodeAt(i)
|
| 180 |
+
let c = fromMap[charCode]
|
| 181 |
+
if (charCode > 255 || c < 0) throw new SyntaxError(E_CHAR)
|
| 182 |
+
|
| 183 |
+
let k = size - 1
|
| 184 |
+
for (;;) {
|
| 185 |
+
if (c === 0 && k < at) break
|
| 186 |
+
c += 58 * res[k]
|
| 187 |
+
res[k] = c & 0xff
|
| 188 |
+
c >>>= 8
|
| 189 |
+
k--
|
| 190 |
+
// unroll a bit
|
| 191 |
+
if (c === 0 && k < at) break
|
| 192 |
+
c += 58 * res[k]
|
| 193 |
+
res[k] = c & 0xff
|
| 194 |
+
c >>>= 8
|
| 195 |
+
k--
|
| 196 |
+
if (c === 0 && k < at) break
|
| 197 |
+
c += 58 * res[k]
|
| 198 |
+
res[k] = c & 0xff
|
| 199 |
+
c >>>= 8
|
| 200 |
+
k--
|
| 201 |
+
if (c === 0 && k < at) break
|
| 202 |
+
c += 58 * res[k]
|
| 203 |
+
res[k] = c & 0xff
|
| 204 |
+
c >>>= 8
|
| 205 |
+
k--
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
at = k + 1
|
| 209 |
+
if (c !== 0 || at < zeros) /* c8 ignore next */ throw new Error('Unexpected') // unreachable
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
return fromUint8(res.slice(at - zeros), format)
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
export const toBase58 = (arr) => toBase58core(arr, alphabet58, codes58)
|
| 217 |
+
export const fromBase58 = (str, format) => fromBase58core(str, alphabet58, codes58, format)
|
| 218 |
+
export const toBase58xrp = (arr) => toBase58core(arr, alphabetXRP, codesXRP)
|
| 219 |
+
export const fromBase58xrp = (str, format) => fromBase58core(str, alphabetXRP, codesXRP, format)
|
node_modules/@exodus/bytes/base58check.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Implements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.
|
| 3 |
+
*
|
| 4 |
+
* ```js
|
| 5 |
+
* import { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'
|
| 6 |
+
* import { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'
|
| 7 |
+
* import { makeBase58check } from '@exodus/bytes/base58check.js'
|
| 8 |
+
* ```
|
| 9 |
+
*
|
| 10 |
+
* On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
|
| 11 |
+
*
|
| 12 |
+
* @module @exodus/bytes/base58check.js
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/// <reference types="node" />
|
| 16 |
+
|
| 17 |
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
| 18 |
+
|
| 19 |
+
/**
|
| 20 |
+
* Hash function type that takes Uint8Array and returns a Promise of Uint8Array
|
| 21 |
+
*/
|
| 22 |
+
export type HashFunction = (data: Uint8Array) => Promise<Uint8Array>;
|
| 23 |
+
|
| 24 |
+
/**
|
| 25 |
+
* Synchronous hash function type that takes Uint8Array and returns Uint8Array
|
| 26 |
+
*/
|
| 27 |
+
export type HashFunctionSync = (data: Uint8Array) => Uint8Array;
|
| 28 |
+
|
| 29 |
+
/**
|
| 30 |
+
* Base58Check encoder/decoder instance with async methods
|
| 31 |
+
*/
|
| 32 |
+
export interface Base58CheckAsync {
|
| 33 |
+
/**
|
| 34 |
+
* Encode bytes to base58check string asynchronously
|
| 35 |
+
*
|
| 36 |
+
* @param arr - The input bytes to encode
|
| 37 |
+
* @returns A Promise that resolves to the base58check encoded string
|
| 38 |
+
*/
|
| 39 |
+
encode(arr: Uint8Array): Promise<string>;
|
| 40 |
+
|
| 41 |
+
/**
|
| 42 |
+
* Decode a base58check string to bytes asynchronously
|
| 43 |
+
*
|
| 44 |
+
* @param string - The base58check encoded string
|
| 45 |
+
* @param format - Output format (default: 'uint8')
|
| 46 |
+
* @returns A Promise that resolves to the decoded bytes
|
| 47 |
+
*/
|
| 48 |
+
decode(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
|
| 49 |
+
decode(string: string, format: 'arraybuffer'): Promise<ArrayBuffer>;
|
| 50 |
+
decode(string: string, format: 'buffer'): Promise<Buffer>;
|
| 51 |
+
decode(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | ArrayBuffer | Buffer>;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* Base58Check encoder/decoder instance with both async and sync methods
|
| 56 |
+
*/
|
| 57 |
+
export interface Base58CheckSync extends Base58CheckAsync {
|
| 58 |
+
/**
|
| 59 |
+
* Encode bytes to base58check string synchronously
|
| 60 |
+
*
|
| 61 |
+
* @param arr - The input bytes to encode
|
| 62 |
+
* @returns The base58check encoded string
|
| 63 |
+
*/
|
| 64 |
+
encodeSync(arr: Uint8Array): string;
|
| 65 |
+
|
| 66 |
+
/**
|
| 67 |
+
* Decode a base58check string to bytes synchronously
|
| 68 |
+
*
|
| 69 |
+
* @param string - The base58check encoded string
|
| 70 |
+
* @param format - Output format (default: 'uint8')
|
| 71 |
+
* @returns The decoded bytes
|
| 72 |
+
*/
|
| 73 |
+
decodeSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
| 74 |
+
decodeSync(string: string, format: 'arraybuffer'): ArrayBuffer;
|
| 75 |
+
decodeSync(string: string, format: 'buffer'): Buffer;
|
| 76 |
+
decodeSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/**
|
| 80 |
+
* Create a base58check encoder/decoder with custom hash functions
|
| 81 |
+
*
|
| 82 |
+
* @param hashAlgo - Async hash function (typically double SHA-256)
|
| 83 |
+
* @param hashAlgoSync - Optional sync hash function
|
| 84 |
+
* @returns Base58Check encoder/decoder instance
|
| 85 |
+
*/
|
| 86 |
+
export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync: HashFunctionSync): Base58CheckSync;
|
| 87 |
+
export function makeBase58check(hashAlgo: HashFunction | HashFunctionSync, hashAlgoSync?: undefined): Base58CheckAsync;
|
| 88 |
+
|
| 89 |
+
/**
|
| 90 |
+
* Encode bytes to base58check string asynchronously
|
| 91 |
+
*
|
| 92 |
+
* Uses double SHA-256 for checksum calculation
|
| 93 |
+
*
|
| 94 |
+
* @param arr - The input bytes to encode
|
| 95 |
+
* @returns A Promise that resolves to the base58check encoded string
|
| 96 |
+
*/
|
| 97 |
+
export function toBase58check(arr: Uint8Array): Promise<string>;
|
| 98 |
+
|
| 99 |
+
/**
|
| 100 |
+
* Decode a base58check string to bytes asynchronously
|
| 101 |
+
*
|
| 102 |
+
* Validates the checksum using double SHA-256
|
| 103 |
+
*
|
| 104 |
+
* @param string - The base58check encoded string
|
| 105 |
+
* @param format - Output format (default: 'uint8')
|
| 106 |
+
* @returns A Promise that resolves to the decoded bytes
|
| 107 |
+
*/
|
| 108 |
+
export function fromBase58check(string: string, format?: 'uint8'): Promise<Uint8ArrayBuffer>;
|
| 109 |
+
export function fromBase58check(string: string, format: 'arraybuffer'): Promise<ArrayBuffer>;
|
| 110 |
+
export function fromBase58check(string: string, format: 'buffer'): Promise<Buffer>;
|
| 111 |
+
export function fromBase58check(string: string, format?: OutputFormat): Promise<Uint8ArrayBuffer | ArrayBuffer | Buffer>;
|
| 112 |
+
|
| 113 |
+
/**
|
| 114 |
+
* Encode bytes to base58check string synchronously
|
| 115 |
+
*
|
| 116 |
+
* Uses double SHA-256 for checksum calculation
|
| 117 |
+
*
|
| 118 |
+
* @param arr - The input bytes to encode
|
| 119 |
+
* @returns The base58check encoded string
|
| 120 |
+
*/
|
| 121 |
+
export function toBase58checkSync(arr: Uint8Array): string;
|
| 122 |
+
|
| 123 |
+
/**
|
| 124 |
+
* Decode a base58check string to bytes synchronously
|
| 125 |
+
*
|
| 126 |
+
* Validates the checksum using double SHA-256
|
| 127 |
+
*
|
| 128 |
+
* @param string - The base58check encoded string
|
| 129 |
+
* @param format - Output format (default: 'uint8')
|
| 130 |
+
* @returns The decoded bytes
|
| 131 |
+
*/
|
| 132 |
+
export function fromBase58checkSync(string: string, format?: 'uint8'): Uint8ArrayBuffer;
|
| 133 |
+
export function fromBase58checkSync(string: string, format: 'arraybuffer'): ArrayBuffer;
|
| 134 |
+
export function fromBase58checkSync(string: string, format: 'buffer'): Buffer;
|
| 135 |
+
export function fromBase58checkSync(string: string, format?: OutputFormat): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
node_modules/@exodus/bytes/base58check.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { sha256 } from '@noble/hashes/sha2.js'
|
| 2 |
+
import { makeBase58check } from './fallback/base58check.js'
|
| 3 |
+
|
| 4 |
+
// Note: while API is async, we use hashSync for now until we improve webcrypto perf for hash256
|
| 5 |
+
// Inputs to base58 are typically very small, and that makes a difference
|
| 6 |
+
|
| 7 |
+
// Note: using native WebCrypto will have to have account for SharedArrayBuffer
|
| 8 |
+
|
| 9 |
+
const hash256sync = (x) => sha256(sha256(x))
|
| 10 |
+
const hash256 = hash256sync // See note at the top
|
| 11 |
+
|
| 12 |
+
const b58c = /* @__PURE__ */ makeBase58check(hash256, hash256sync)
|
| 13 |
+
export const toBase58check = /* @__PURE__ */ (() => b58c.encode)()
|
| 14 |
+
export const fromBase58check = /* @__PURE__ */ (() => b58c.decode)()
|
| 15 |
+
export const toBase58checkSync = /* @__PURE__ */ (() => b58c.encodeSync)()
|
| 16 |
+
export const fromBase58checkSync = /* @__PURE__ */ (() => b58c.decodeSync)()
|
| 17 |
+
|
| 18 |
+
export { makeBase58check } from './fallback/base58check.js'
|
node_modules/@exodus/bytes/base58check.node.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { hash } from 'node:crypto'
|
| 2 |
+
import { makeBase58check } from './fallback/base58check.js'
|
| 3 |
+
|
| 4 |
+
const sha256 = (x) => hash('sha256', x, 'buffer')
|
| 5 |
+
const hash256 = (x) => sha256(sha256(x))
|
| 6 |
+
const {
|
| 7 |
+
encode: toBase58check,
|
| 8 |
+
decode: fromBase58check,
|
| 9 |
+
encodeSync: toBase58checkSync,
|
| 10 |
+
decodeSync: fromBase58checkSync,
|
| 11 |
+
} = makeBase58check(hash256, hash256)
|
| 12 |
+
|
| 13 |
+
export { makeBase58check } from './fallback/base58check.js'
|
| 14 |
+
export { toBase58check, fromBase58check, toBase58checkSync, fromBase58checkSync }
|
node_modules/@exodus/bytes/base64.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Implements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)
|
| 3 |
+
* (no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).
|
| 4 |
+
*
|
| 5 |
+
* ```js
|
| 6 |
+
* import { fromBase64, toBase64 } from '@exodus/bytes/base64.js'
|
| 7 |
+
* import { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'
|
| 8 |
+
* import { fromBase64any } from '@exodus/bytes/base64.js'
|
| 9 |
+
* ```
|
| 10 |
+
*
|
| 11 |
+
* @module @exodus/bytes/base64.js
|
| 12 |
+
*/
|
| 13 |
+
|
| 14 |
+
/// <reference types="node" />
|
| 15 |
+
|
| 16 |
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
| 17 |
+
|
| 18 |
+
/**
|
| 19 |
+
* Options for base64 encoding
|
| 20 |
+
*/
|
| 21 |
+
export interface ToBase64Options {
|
| 22 |
+
/** Whether to include padding characters (default: true for base64, false for base64url) */
|
| 23 |
+
padding?: boolean;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/**
|
| 27 |
+
* Padding mode for base64 decoding
|
| 28 |
+
* - `true`: padding is required
|
| 29 |
+
* - `false`: padding is not allowed (default for base64url)
|
| 30 |
+
* - `'both'`: padding is optional (default for base64)
|
| 31 |
+
*/
|
| 32 |
+
export type PaddingMode = boolean | 'both';
|
| 33 |
+
|
| 34 |
+
/**
|
| 35 |
+
* Options for base64 decoding
|
| 36 |
+
*/
|
| 37 |
+
export interface FromBase64Options {
|
| 38 |
+
/** Output format (default: 'uint8') */
|
| 39 |
+
format?: OutputFormat;
|
| 40 |
+
/** Padding mode */
|
| 41 |
+
padding?: PaddingMode;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
/**
|
| 45 |
+
* Encode a `Uint8Array` to a base64 string (RFC 4648)
|
| 46 |
+
*
|
| 47 |
+
* @param arr - The input bytes
|
| 48 |
+
* @param options - Encoding options
|
| 49 |
+
* @returns The base64 encoded string
|
| 50 |
+
*/
|
| 51 |
+
export function toBase64(arr: Uint8Array, options?: ToBase64Options): string;
|
| 52 |
+
|
| 53 |
+
/**
|
| 54 |
+
* Encode a `Uint8Array` to a base64url string (RFC 4648)
|
| 55 |
+
*
|
| 56 |
+
* @param arr - The input bytes
|
| 57 |
+
* @param options - Encoding options (padding defaults to false)
|
| 58 |
+
* @returns The base64url encoded string
|
| 59 |
+
*/
|
| 60 |
+
export function toBase64url(arr: Uint8Array, options?: ToBase64Options): string;
|
| 61 |
+
|
| 62 |
+
/**
|
| 63 |
+
* Decode a base64 string to bytes
|
| 64 |
+
*
|
| 65 |
+
* Operates in strict mode for last chunk, does not allow whitespace
|
| 66 |
+
*
|
| 67 |
+
* @param string - The base64 encoded string
|
| 68 |
+
* @param options - Decoding options
|
| 69 |
+
* @returns The decoded bytes
|
| 70 |
+
*/
|
| 71 |
+
export function fromBase64(string: string, options?: FromBase64Options & { format?: 'uint8' }): Uint8ArrayBuffer;
|
| 72 |
+
export function fromBase64(string: string, options: FromBase64Options & { format: 'arraybuffer' }): ArrayBuffer;
|
| 73 |
+
export function fromBase64(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
|
| 74 |
+
export function fromBase64(string: string, options?: FromBase64Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 75 |
+
|
| 76 |
+
/**
|
| 77 |
+
* Decode a base64url string to bytes
|
| 78 |
+
*
|
| 79 |
+
* Operates in strict mode for last chunk, does not allow whitespace
|
| 80 |
+
*
|
| 81 |
+
* @param string - The base64url encoded string
|
| 82 |
+
* @param options - Decoding options (padding defaults to false)
|
| 83 |
+
* @returns The decoded bytes
|
| 84 |
+
*/
|
| 85 |
+
export function fromBase64url(string: string, options?: FromBase64Options & { format?: 'uint8' }): Uint8ArrayBuffer;
|
| 86 |
+
export function fromBase64url(string: string, options: FromBase64Options & { format: 'arraybuffer' }): ArrayBuffer;
|
| 87 |
+
export function fromBase64url(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
|
| 88 |
+
export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 89 |
+
|
| 90 |
+
/**
|
| 91 |
+
* Decode either base64 or base64url string to bytes
|
| 92 |
+
*
|
| 93 |
+
* Automatically detects the variant based on characters present
|
| 94 |
+
*
|
| 95 |
+
* @param string - The base64 or base64url encoded string
|
| 96 |
+
* @param options - Decoding options
|
| 97 |
+
* @returns The decoded bytes
|
| 98 |
+
*/
|
| 99 |
+
export function fromBase64any(string: string, options?: FromBase64Options & { format?: 'uint8' }): Uint8ArrayBuffer;
|
| 100 |
+
export function fromBase64any(string: string, options: FromBase64Options & { format: 'arraybuffer' }): ArrayBuffer;
|
| 101 |
+
export function fromBase64any(string: string, options: FromBase64Options & { format: 'buffer' }): Buffer;
|
| 102 |
+
export function fromBase64url(string: string, options?: FromBase64Options): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
node_modules/@exodus/bytes/base64.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { assertEmptyRest } from './assert.js'
|
| 2 |
+
import { assertU8, fromUint8, fromBuffer, E_STRING } from './fallback/_utils.js'
|
| 3 |
+
import { isHermes } from './fallback/platform.js'
|
| 4 |
+
import { decodeLatin1, encodeLatin1 } from './fallback/latin1.js'
|
| 5 |
+
import * as js from './fallback/base64.js'
|
| 6 |
+
|
| 7 |
+
// See https://datatracker.ietf.org/doc/html/rfc4648
|
| 8 |
+
|
| 9 |
+
// base64: A-Za-z0-9+/ and = if padding not disabled
|
| 10 |
+
// base64url: A-Za-z0-9_- and = if padding enabled
|
| 11 |
+
|
| 12 |
+
const { Buffer, atob, btoa } = globalThis // Buffer is optional, only used when native
|
| 13 |
+
const haveNativeBuffer = Buffer && !Buffer.TYPED_ARRAY_SUPPORT
|
| 14 |
+
const { toBase64: web64 } = Uint8Array.prototype // Modern engines have this
|
| 15 |
+
|
| 16 |
+
const { E_CHAR, E_PADDING, E_LENGTH, E_LAST } = js
|
| 17 |
+
|
| 18 |
+
// faster only on Hermes (and a little in old Chrome), js path beats it on normal engines
|
| 19 |
+
const shouldUseBtoa = btoa && isHermes
|
| 20 |
+
const shouldUseAtob = atob && isHermes
|
| 21 |
+
|
| 22 |
+
// For native Buffer codepaths only
|
| 23 |
+
const isBuffer = (x) => x.constructor === Buffer && Buffer.isBuffer(x)
|
| 24 |
+
const toBuffer = (x) => (isBuffer(x) ? x : Buffer.from(x.buffer, x.byteOffset, x.byteLength))
|
| 25 |
+
|
| 26 |
+
function maybeUnpad(res, padding) {
|
| 27 |
+
if (padding) return res
|
| 28 |
+
const at = res.indexOf('=', res.length - 3)
|
| 29 |
+
return at === -1 ? res : res.slice(0, at)
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
function maybePad(res, padding) {
|
| 33 |
+
return padding && res.length % 4 !== 0 ? res + '='.repeat(4 - (res.length % 4)) : res
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
const toUrl = (x) => x.replaceAll('+', '-').replaceAll('/', '_')
|
| 37 |
+
const haveWeb = (x) => web64 && x.toBase64 === web64
|
| 38 |
+
|
| 39 |
+
export function toBase64(x, { padding = true } = {}) {
|
| 40 |
+
assertU8(x)
|
| 41 |
+
if (haveWeb(x)) return padding ? x.toBase64() : x.toBase64({ omitPadding: !padding }) // Modern, optionless is slightly faster
|
| 42 |
+
if (haveNativeBuffer) return maybeUnpad(toBuffer(x).base64Slice(0, x.byteLength), padding) // Older Node.js
|
| 43 |
+
if (shouldUseBtoa) return maybeUnpad(btoa(decodeLatin1(x)), padding)
|
| 44 |
+
return js.toBase64(x, false, padding) // Fallback
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// NOTE: base64url omits padding by default
|
| 48 |
+
export function toBase64url(x, { padding = false } = {}) {
|
| 49 |
+
assertU8(x)
|
| 50 |
+
if (haveWeb(x)) return x.toBase64({ alphabet: 'base64url', omitPadding: !padding }) // Modern
|
| 51 |
+
if (haveNativeBuffer) return maybePad(toBuffer(x).base64urlSlice(0, x.byteLength), padding) // Older Node.js
|
| 52 |
+
if (shouldUseBtoa) return maybeUnpad(toUrl(btoa(decodeLatin1(x))), padding)
|
| 53 |
+
return js.toBase64(x, true, padding) // Fallback
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// Unlike Buffer.from(), throws on invalid input (non-base64 symbols and incomplete chunks)
|
| 57 |
+
// Unlike Buffer.from() and Uint8Array.fromBase64(), does not allow spaces
|
| 58 |
+
// NOTE: Always operates in strict mode for last chunk
|
| 59 |
+
|
| 60 |
+
// By default accepts both padded and non-padded variants, only strict base64
|
| 61 |
+
export function fromBase64(str, options) {
|
| 62 |
+
if (typeof options === 'string') options = { format: options } // Compat due to usage, TODO: remove
|
| 63 |
+
if (!options) return fromBase64common(str, false, 'both', 'uint8', null)
|
| 64 |
+
const { format = 'uint8', padding = 'both', ...rest } = options
|
| 65 |
+
return fromBase64common(str, false, padding, format, rest)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// By default accepts only non-padded strict base64url
|
| 69 |
+
export function fromBase64url(str, options) {
|
| 70 |
+
if (!options) return fromBase64common(str, true, false, 'uint8', null)
|
| 71 |
+
const { format = 'uint8', padding = false, ...rest } = options
|
| 72 |
+
return fromBase64common(str, true, padding, format, rest)
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// By default accepts both padded and non-padded variants, base64 or base64url
|
| 76 |
+
export function fromBase64any(str, { format = 'uint8', padding = 'both', ...rest } = {}) {
|
| 77 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 78 |
+
const isBase64url = !str.includes('+') && !str.includes('/') // likely to fail fast, as most input is non-url, also double scan is faster than regex
|
| 79 |
+
return fromBase64common(str, isBase64url, padding, format, rest)
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
function fromBase64common(str, isBase64url, padding, format, rest) {
|
| 83 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 84 |
+
if (rest !== null) assertEmptyRest(rest)
|
| 85 |
+
const auto = padding === 'both' ? str.endsWith('=') : undefined
|
| 86 |
+
// Older JSC supporting Uint8Array.fromBase64 lacks proper checks
|
| 87 |
+
if (padding === true || auto === true) {
|
| 88 |
+
if (str.length % 4 !== 0) throw new SyntaxError(E_PADDING) // JSC misses this
|
| 89 |
+
if (str[str.length - 3] === '=') throw new SyntaxError(E_PADDING) // no more than two = at the end
|
| 90 |
+
} else if (padding === false || auto === false) {
|
| 91 |
+
if (str.length % 4 === 1) throw new SyntaxError(E_LENGTH) // JSC misses this in fromBase64
|
| 92 |
+
if (padding === false && str.endsWith('=')) {
|
| 93 |
+
throw new SyntaxError('Did not expect padding in base64 input') // inclusion is checked separately
|
| 94 |
+
}
|
| 95 |
+
} else {
|
| 96 |
+
throw new TypeError('Invalid padding option')
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
return fromBase64impl(str, isBase64url, padding, format)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE
|
| 103 |
+
const ASCII_WHITESPACE = /[\t\n\f\r ]/ // non-u for JSC perf
|
| 104 |
+
|
| 105 |
+
function noWhitespaceSeen(str, arr) {
|
| 106 |
+
const at = str.indexOf('=', str.length - 3)
|
| 107 |
+
const paddingLength = at >= 0 ? str.length - at : 0
|
| 108 |
+
const chars = str.length - paddingLength
|
| 109 |
+
const e = chars % 4 // extra chars past blocks of 4
|
| 110 |
+
const b = arr.length - ((chars - e) / 4) * 3 // remaining bytes not covered by full blocks of chars
|
| 111 |
+
return (e === 0 && b === 0) || (e === 2 && b === 1) || (e === 3 && b === 2)
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
let fromBase64impl
|
| 115 |
+
if (Uint8Array.fromBase64) {
|
| 116 |
+
// NOTICE: this is actually slower than our JS impl in older JavaScriptCore and (slightly) in SpiderMonkey, but faster on V8 and new JavaScriptCore
|
| 117 |
+
fromBase64impl = (str, isBase64url, padding, format) => {
|
| 118 |
+
const alphabet = isBase64url ? 'base64url' : 'base64'
|
| 119 |
+
|
| 120 |
+
let arr
|
| 121 |
+
if (padding === true) {
|
| 122 |
+
// Padding is required from user, and we already checked that string length is divisible by 4
|
| 123 |
+
// Padding might still be wrong due to whitespace, but in that case native impl throws expected error
|
| 124 |
+
arr = Uint8Array.fromBase64(str, { alphabet, lastChunkHandling: 'strict' })
|
| 125 |
+
} else {
|
| 126 |
+
try {
|
| 127 |
+
const padded = str.length % 4 > 0 ? `${str}${'='.repeat(4 - (str.length % 4))}` : str
|
| 128 |
+
arr = Uint8Array.fromBase64(padded, { alphabet, lastChunkHandling: 'strict' })
|
| 129 |
+
} catch (err) {
|
| 130 |
+
// Normalize error: whitespace in input could have caused added padding to be invalid
|
| 131 |
+
// But reporting that as a padding error would be confusing
|
| 132 |
+
throw ASCII_WHITESPACE.test(str) ? new SyntaxError(E_CHAR) : err
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// We don't allow whitespace in input, but that can be rechecked based on output length
|
| 137 |
+
// All other chars are checked natively
|
| 138 |
+
if (!noWhitespaceSeen(str, arr)) throw new SyntaxError(E_CHAR)
|
| 139 |
+
return fromUint8(arr, format)
|
| 140 |
+
}
|
| 141 |
+
} else if (haveNativeBuffer) {
|
| 142 |
+
fromBase64impl = (str, isBase64url, padding, format) => {
|
| 143 |
+
const size = Buffer.byteLength(str, 'base64')
|
| 144 |
+
const arr = Buffer.allocUnsafeSlow(size) // non-pooled
|
| 145 |
+
if (arr.base64Write(str) !== size) throw new SyntaxError(E_PADDING)
|
| 146 |
+
// Rechecking by re-encoding is cheaper than regexes on Node.js
|
| 147 |
+
const got = isBase64url ? maybeUnpad(str, padding === false) : maybePad(str, padding !== true)
|
| 148 |
+
const valid = isBase64url ? arr.base64urlSlice(0, arr.length) : arr.base64Slice(0, arr.length)
|
| 149 |
+
if (got !== valid) throw new SyntaxError(E_PADDING)
|
| 150 |
+
return fromBuffer(arr, format) // fully checked
|
| 151 |
+
}
|
| 152 |
+
} else if (shouldUseAtob) {
|
| 153 |
+
// atob is faster than manual parsing on Hermes
|
| 154 |
+
fromBase64impl = (str, isBase64url, padding, format) => {
|
| 155 |
+
let arr
|
| 156 |
+
if (isBase64url) {
|
| 157 |
+
if (/[\t\n\f\r +/]/.test(str)) throw new SyntaxError(E_CHAR) // atob verifies other invalid input
|
| 158 |
+
str = str.replaceAll('-', '+').replaceAll('_', '/') // from url to normal
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
try {
|
| 162 |
+
arr = encodeLatin1(atob(str))
|
| 163 |
+
} catch {
|
| 164 |
+
throw new SyntaxError(E_CHAR) // convert atob errors
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
if (!isBase64url && !noWhitespaceSeen(str, arr)) throw new SyntaxError(E_CHAR) // base64url checks input above
|
| 168 |
+
|
| 169 |
+
if (arr.length % 3 !== 0) {
|
| 170 |
+
// Check last chunk to be strict if it was incomplete
|
| 171 |
+
const expected = toBase64(arr.subarray(-(arr.length % 3))) // str is normalized to non-url already
|
| 172 |
+
const end = str.length % 4 === 0 ? str.slice(-4) : str.slice(-(str.length % 4)).padEnd(4, '=')
|
| 173 |
+
if (expected !== end) throw new SyntaxError(E_LAST)
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
return fromUint8(arr, format)
|
| 177 |
+
}
|
| 178 |
+
} else {
|
| 179 |
+
fromBase64impl = (str, isBase64url, padding, format) =>
|
| 180 |
+
fromUint8(js.fromBase64(str, isBase64url), format) // validated in js
|
| 181 |
+
}
|
node_modules/@exodus/bytes/bech32.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Implements bech32 and bech32m from
|
| 3 |
+
* [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)
|
| 4 |
+
* and [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).
|
| 5 |
+
*
|
| 6 |
+
* ```js
|
| 7 |
+
* import { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'
|
| 8 |
+
* import { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'
|
| 9 |
+
* import { getPrefix } from '@exodus/bytes/bech32.js'
|
| 10 |
+
* ```
|
| 11 |
+
*
|
| 12 |
+
* @module @exodus/bytes/bech32.js
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/// <reference types="node" />
|
| 16 |
+
|
| 17 |
+
import type { Uint8ArrayBuffer } from './array.js';
|
| 18 |
+
|
| 19 |
+
/**
|
| 20 |
+
* Result of decoding a bech32 or bech32m string
|
| 21 |
+
*/
|
| 22 |
+
export interface Bech32DecodeResult {
|
| 23 |
+
/** The human-readable prefix */
|
| 24 |
+
prefix: string;
|
| 25 |
+
/** The decoded bytes */
|
| 26 |
+
bytes: Uint8ArrayBuffer;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
/**
|
| 30 |
+
* Encode bytes to a bech32 string
|
| 31 |
+
*
|
| 32 |
+
* @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin)
|
| 33 |
+
* @param bytes - The input bytes to encode
|
| 34 |
+
* @param limit - Maximum length of the encoded string (default: 90)
|
| 35 |
+
* @returns The bech32 encoded string
|
| 36 |
+
*/
|
| 37 |
+
export function toBech32(prefix: string, bytes: Uint8Array, limit?: number): string;
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* Decode a bech32 string to bytes
|
| 41 |
+
*
|
| 42 |
+
* @param string - The bech32 encoded string
|
| 43 |
+
* @param limit - Maximum length of the input string (default: 90)
|
| 44 |
+
* @returns The decoded prefix and bytes
|
| 45 |
+
*/
|
| 46 |
+
export function fromBech32(string: string, limit?: number): Bech32DecodeResult;
|
| 47 |
+
|
| 48 |
+
/**
|
| 49 |
+
* Encode bytes to a bech32m string
|
| 50 |
+
*
|
| 51 |
+
* @param prefix - The human-readable prefix (e.g., 'bc' for Bitcoin)
|
| 52 |
+
* @param bytes - The input bytes to encode
|
| 53 |
+
* @param limit - Maximum length of the encoded string (default: 90)
|
| 54 |
+
* @returns The bech32m encoded string
|
| 55 |
+
*/
|
| 56 |
+
export function toBech32m(prefix: string, bytes: Uint8Array, limit?: number): string;
|
| 57 |
+
|
| 58 |
+
/**
|
| 59 |
+
* Decode a bech32m string to bytes
|
| 60 |
+
*
|
| 61 |
+
* @param string - The bech32m encoded string
|
| 62 |
+
* @param limit - Maximum length of the input string (default: 90)
|
| 63 |
+
* @returns The decoded prefix and bytes
|
| 64 |
+
*/
|
| 65 |
+
export function fromBech32m(string: string, limit?: number): Bech32DecodeResult;
|
| 66 |
+
|
| 67 |
+
/**
|
| 68 |
+
* Extract the prefix from a bech32 or bech32m string without full validation
|
| 69 |
+
*
|
| 70 |
+
* This is a quick check that skips most validation.
|
| 71 |
+
*
|
| 72 |
+
* @param string - The bech32/bech32m encoded string
|
| 73 |
+
* @param limit - Maximum length of the input string (default: 90)
|
| 74 |
+
* @returns The lowercase prefix
|
| 75 |
+
*/
|
| 76 |
+
export function getPrefix(string: string, limit?: number): string;
|
node_modules/@exodus/bytes/bech32.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { assertU8, E_STRING } from './fallback/_utils.js'
|
| 2 |
+
import { nativeEncoder } from './fallback/platform.js'
|
| 3 |
+
import { decodeAscii, encodeAscii, encodeLatin1 } from './fallback/latin1.js'
|
| 4 |
+
|
| 5 |
+
const alphabet = [...'qpzry9x8gf2tvdw0s3jn54khce6mua7l']
|
| 6 |
+
const BECH32 = 1
|
| 7 |
+
const BECH32M = 0x2b_c8_30_a3
|
| 8 |
+
|
| 9 |
+
const E_SIZE = 'Input length is out of range'
|
| 10 |
+
const E_PREFIX = 'Missing or invalid prefix'
|
| 11 |
+
const E_MIXED = 'Mixed-case string'
|
| 12 |
+
const E_PADDING = 'Padding is invalid'
|
| 13 |
+
const E_CHECKSUM = 'Invalid checksum'
|
| 14 |
+
const E_CHARACTER = 'Non-bech32 character'
|
| 15 |
+
|
| 16 |
+
// nativeEncoder path uses encodeAscii which asserts ascii, otherwise we have 0-255 bytes from encodeLatin1
|
| 17 |
+
const c2x = new Int8Array(nativeEncoder ? 128 : 256).fill(-1)
|
| 18 |
+
const x2c = new Uint8Array(32)
|
| 19 |
+
for (let i = 0; i < alphabet.length; i++) {
|
| 20 |
+
const c = alphabet[i].charCodeAt(0)
|
| 21 |
+
c2x[c] = i
|
| 22 |
+
x2c[i] = c
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// checksum size is 30 bits, 0x3f_ff_ff_ff
|
| 26 |
+
// The good thing about the checksum is that it's linear over every bit
|
| 27 |
+
const poly0 = new Uint32Array(32) // just precache all possible ones, it's only 1 KiB
|
| 28 |
+
const p = (x) => ((x & 0x1_ff_ff_ff) << 5) ^ poly0[x >> 25]
|
| 29 |
+
for (let i = 0; i < 32; i++) {
|
| 30 |
+
poly0[i] =
|
| 31 |
+
(i & 0b0_0001 ? 0x3b_6a_57_b2 : 0) ^
|
| 32 |
+
(i & 0b0_0010 ? 0x26_50_8e_6d : 0) ^
|
| 33 |
+
(i & 0b0_0100 ? 0x1e_a1_19_fa : 0) ^
|
| 34 |
+
(i & 0b0_1000 ? 0x3d_42_33_dd : 0) ^
|
| 35 |
+
(i & 0b1_0000 ? 0x2a_14_62_b3 : 0)
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// 7 KiB more for faster p6/p8
|
| 39 |
+
const poly1 = new Uint32Array(32)
|
| 40 |
+
const poly2 = new Uint32Array(32)
|
| 41 |
+
const poly3 = new Uint32Array(32)
|
| 42 |
+
const poly4 = new Uint32Array(32)
|
| 43 |
+
const poly5 = new Uint32Array(32)
|
| 44 |
+
const poly6 = new Uint32Array(32)
|
| 45 |
+
const poly7 = new Uint32Array(32)
|
| 46 |
+
for (let i = 0; i < 32; i++) {
|
| 47 |
+
// poly0[i] === p(p(p(p(p(p(i))))))
|
| 48 |
+
poly1[i] = p(poly0[i]) // aka p(p(p(p(p(p(i << 5))))))
|
| 49 |
+
poly2[i] = p(poly1[i]) // aka p(p(p(p(p(p(i << 10))))))
|
| 50 |
+
poly3[i] = p(poly2[i]) // aka p(p(p(p(p(p(i << 15))))))
|
| 51 |
+
poly4[i] = p(poly3[i]) // aka p(p(p(p(p(p(i << 20))))))
|
| 52 |
+
poly5[i] = p(poly4[i]) // aka p(p(p(p(p(p(i << 25))))))
|
| 53 |
+
poly6[i] = p(poly5[i])
|
| 54 |
+
poly7[i] = p(poly6[i])
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
function p6(x) {
|
| 58 |
+
// Same as: return p(p(p(p(p(p(x))))))
|
| 59 |
+
const x0 = x & 0x1f
|
| 60 |
+
const x1 = (x >> 5) & 0x1f
|
| 61 |
+
const x2 = (x >> 10) & 0x1f
|
| 62 |
+
const x3 = (x >> 15) & 0x1f
|
| 63 |
+
const x4 = (x >> 20) & 0x1f
|
| 64 |
+
const x5 = (x >> 25) & 0x1f
|
| 65 |
+
return poly0[x0] ^ poly1[x1] ^ poly2[x2] ^ poly3[x3] ^ poly4[x4] ^ poly5[x5]
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
function p8(x) {
|
| 69 |
+
// Same as: return p(p(p(p(p(p(p(p(x))))))))
|
| 70 |
+
const x0 = x & 0x1f
|
| 71 |
+
const x1 = (x >> 5) & 0x1f
|
| 72 |
+
const x2 = (x >> 10) & 0x1f
|
| 73 |
+
const x3 = (x >> 15) & 0x1f
|
| 74 |
+
const x4 = (x >> 20) & 0x1f
|
| 75 |
+
const x5 = (x >> 25) & 0x1f
|
| 76 |
+
return poly2[x0] ^ poly3[x1] ^ poly4[x2] ^ poly5[x3] ^ poly6[x4] ^ poly7[x5]
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// p(p(p(p(p(p(chk) ^ x0) ^ x1) ^ x2) ^ x3) ^ x4) ^ x5 === p6(chk) ^ merge(x0, x1, x2, x3, x4, x5)
|
| 80 |
+
const merge = (a, b, c, d, e, f) => f ^ (e << 5) ^ (d << 10) ^ (c << 15) ^ (b << 20) ^ (a << 25)
|
| 81 |
+
|
| 82 |
+
const prefixCache = new Map() // Cache 10 of them
|
| 83 |
+
|
| 84 |
+
function pPrefix(prefix) {
|
| 85 |
+
if (prefix === 'bc') return 0x2_31_80_43 // perf
|
| 86 |
+
const cached = prefixCache.get(prefix)
|
| 87 |
+
if (cached !== undefined) return cached
|
| 88 |
+
|
| 89 |
+
// bech32_hrp_expand(s): [ord(x) >> 5 for x in s] + [0] + [ord(x) & 31 for x in s]
|
| 90 |
+
// We can do this in a single scan due to linearity, but it's not very beneficial
|
| 91 |
+
let chk = 1 // it starts with one (see def bech32_polymod in BIP_0173)
|
| 92 |
+
const length = prefix.length
|
| 93 |
+
for (let i = 0; i < length; i++) {
|
| 94 |
+
const c = prefix.charCodeAt(i)
|
| 95 |
+
if (c < 33 || c > 126) throw new Error(E_PREFIX) // each character having a value in the range [33-126]
|
| 96 |
+
chk = p(chk) ^ (c >> 5)
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
chk = p(chk) // <= for + [0]
|
| 100 |
+
for (let i = 0; i < length; i++) {
|
| 101 |
+
const c = prefix.charCodeAt(i)
|
| 102 |
+
chk = p(chk) ^ (c & 0x1f)
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
if (prefixCache.size < 10) prefixCache.set(prefix, chk)
|
| 106 |
+
return chk
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
function toBech32enc(prefix, bytes, limit, encoding) {
|
| 110 |
+
if (typeof prefix !== 'string' || !prefix) throw new TypeError(E_PREFIX)
|
| 111 |
+
if (typeof limit !== 'number') throw new TypeError(E_SIZE)
|
| 112 |
+
assertU8(bytes)
|
| 113 |
+
const bytesLength = bytes.length
|
| 114 |
+
const wordsLength = Math.ceil((bytesLength * 8) / 5)
|
| 115 |
+
if (!(prefix.length + 7 + wordsLength <= limit)) throw new TypeError(E_SIZE)
|
| 116 |
+
prefix = prefix.toLowerCase()
|
| 117 |
+
const out = new Uint8Array(wordsLength + 6)
|
| 118 |
+
|
| 119 |
+
let chk = pPrefix(prefix)
|
| 120 |
+
let i = 0, j = 0 // prettier-ignore
|
| 121 |
+
|
| 122 |
+
// This loop is just an optimization of the next one
|
| 123 |
+
for (const length4 = bytesLength - 4; i < length4; i += 5, j += 8) {
|
| 124 |
+
const b0 = bytes[i], b1 = bytes[i + 1], b2 = bytes[i + 2], b3 = bytes[i + 3], b4 = bytes[i + 4] // prettier-ignore
|
| 125 |
+
const x0 = b0 >> 3
|
| 126 |
+
const x1 = ((b0 << 2) & 0x1f) | (b1 >> 6)
|
| 127 |
+
const x2 = (b1 >> 1) & 0x1f
|
| 128 |
+
const x3 = ((b1 << 4) & 0x1f) | (b2 >> 4)
|
| 129 |
+
const x4 = ((b2 << 1) & 0x1f) | (b3 >> 7)
|
| 130 |
+
const x5 = (b3 >> 2) & 0x1f
|
| 131 |
+
const x6 = ((b3 << 3) & 0x1f) | (b4 >> 5)
|
| 132 |
+
const x7 = b4 & 0x1f
|
| 133 |
+
chk = merge(x2, x3, x4, x5, x6, x7) ^ poly0[x1] ^ poly1[x0] ^ p8(chk)
|
| 134 |
+
out[j] = x2c[x0]
|
| 135 |
+
out[j + 1] = x2c[x1]
|
| 136 |
+
out[j + 2] = x2c[x2]
|
| 137 |
+
out[j + 3] = x2c[x3]
|
| 138 |
+
out[j + 4] = x2c[x4]
|
| 139 |
+
out[j + 5] = x2c[x5]
|
| 140 |
+
out[j + 6] = x2c[x6]
|
| 141 |
+
out[j + 7] = x2c[x7]
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
let value = 0, bits = 0 // prettier-ignore
|
| 145 |
+
for (; i < bytesLength; i++) {
|
| 146 |
+
value = ((value & 0xf) << 8) | bytes[i]
|
| 147 |
+
bits += 3
|
| 148 |
+
const x = (value >> bits) & 0x1f
|
| 149 |
+
chk = p(chk) ^ x
|
| 150 |
+
out[j++] = x2c[x]
|
| 151 |
+
if (bits >= 5) {
|
| 152 |
+
bits -= 5
|
| 153 |
+
const x = (value >> bits) & 0x1f
|
| 154 |
+
chk = p(chk) ^ x
|
| 155 |
+
out[j++] = x2c[x]
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
if (bits > 0) {
|
| 160 |
+
const x = (value << (5 - bits)) & 0x1f
|
| 161 |
+
chk = p(chk) ^ x
|
| 162 |
+
out[j++] = x2c[x]
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
chk = encoding ^ p6(chk)
|
| 166 |
+
out[j++] = x2c[(chk >> 25) & 0x1f]
|
| 167 |
+
out[j++] = x2c[(chk >> 20) & 0x1f]
|
| 168 |
+
out[j++] = x2c[(chk >> 15) & 0x1f]
|
| 169 |
+
out[j++] = x2c[(chk >> 10) & 0x1f]
|
| 170 |
+
out[j++] = x2c[(chk >> 5) & 0x1f]
|
| 171 |
+
out[j++] = x2c[(chk >> 0) & 0x1f]
|
| 172 |
+
|
| 173 |
+
return prefix + '1' + decodeAscii(out) // suboptimal in barebones, but actually ok in Hermes for not to care atm
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
function assertDecodeArgs(str, limit) {
|
| 177 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 178 |
+
if (typeof limit !== 'number' || str.length < 8 || !(str.length <= limit)) throw new Error(E_SIZE)
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
// this is instant on 8-bit strings
|
| 182 |
+
const NON_LATIN = /[^\x00-\xFF]/ // eslint-disable-line no-control-regex
|
| 183 |
+
|
| 184 |
+
function fromBech32enc(str, limit, encoding) {
|
| 185 |
+
assertDecodeArgs(str, limit)
|
| 186 |
+
const lower = str.toLowerCase()
|
| 187 |
+
if (str !== lower) {
|
| 188 |
+
if (str !== str.toUpperCase()) throw new Error(E_MIXED)
|
| 189 |
+
str = lower
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
const split = str.lastIndexOf('1')
|
| 193 |
+
if (split <= 0) throw new Error(E_PREFIX)
|
| 194 |
+
const prefix = str.slice(0, split)
|
| 195 |
+
const charsLength = str.length - split - 1
|
| 196 |
+
const wordsLength = charsLength - 6
|
| 197 |
+
if (wordsLength < 0) throw new Error(E_SIZE)
|
| 198 |
+
const bytesLength = (wordsLength * 5) >> 3
|
| 199 |
+
const slice = str.slice(split + 1)
|
| 200 |
+
if (!nativeEncoder && NON_LATIN.test(slice)) throw new SyntaxError(E_CHARACTER) // otherwise can't use encodeLatin1
|
| 201 |
+
const c = nativeEncoder ? encodeAscii(slice, E_CHARACTER) : encodeLatin1(slice) // suboptimal, but only affects non-Hermes barebones
|
| 202 |
+
const bytes = new Uint8Array(bytesLength)
|
| 203 |
+
|
| 204 |
+
let chk = pPrefix(prefix)
|
| 205 |
+
let i = 0, j = 0 // prettier-ignore
|
| 206 |
+
|
| 207 |
+
// This loop is just an optimization of the next one
|
| 208 |
+
for (const length7 = wordsLength - 7; i < length7; i += 8, j += 5) {
|
| 209 |
+
const c0 = c[i], c1 = c[i + 1], c2 = c[i + 2], c3 = c[i + 3], c4 = c[i + 4], c5 = c[i + 5], c6 = c[i + 6], c7 = c[i + 7] // prettier-ignore
|
| 210 |
+
const x0 = c2x[c0], x1 = c2x[c1], x2 = c2x[c2], x3 = c2x[c3], x4 = c2x[c4], x5 = c2x[c5], x6 = c2x[c6], x7 = c2x[c7] // prettier-ignore
|
| 211 |
+
if (x0 < 0 || x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0 || x5 < 0 || x6 < 0 || x7 < 0) throw new SyntaxError(E_CHARACTER) // prettier-ignore
|
| 212 |
+
chk = merge(x2, x3, x4, x5, x6, x7) ^ poly0[x1] ^ poly1[x0] ^ p8(chk)
|
| 213 |
+
bytes[j] = (x0 << 3) | (x1 >> 2)
|
| 214 |
+
bytes[j + 1] = (((x1 << 6) | (x2 << 1)) & 0xff) | (x3 >> 4)
|
| 215 |
+
bytes[j + 2] = ((x3 << 4) & 0xff) | (x4 >> 1)
|
| 216 |
+
bytes[j + 3] = ((((x4 << 5) | x5) << 2) & 0xff) | (x6 >> 3)
|
| 217 |
+
bytes[j + 4] = ((x6 << 5) & 0xff) | x7
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
let value = 0, bits = 0 // prettier-ignore
|
| 221 |
+
for (; i < wordsLength; i++) {
|
| 222 |
+
const x = c2x[c[i]]
|
| 223 |
+
if (x < 0) throw new SyntaxError(E_CHARACTER)
|
| 224 |
+
chk = p(chk) ^ x
|
| 225 |
+
value = (value << 5) | x
|
| 226 |
+
bits += 5
|
| 227 |
+
if (bits >= 8) {
|
| 228 |
+
bits -= 8
|
| 229 |
+
bytes[j++] = (value >> bits) & 0xff
|
| 230 |
+
}
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
if (bits >= 5 || (value << (8 - bits)) & 0xff) throw new Error(E_PADDING)
|
| 234 |
+
|
| 235 |
+
// Checksum
|
| 236 |
+
{
|
| 237 |
+
const c0 = c[i], c1 = c[i + 1], c2 = c[i + 2], c3 = c[i + 3], c4 = c[i + 4], c5 = c[i + 5] // prettier-ignore
|
| 238 |
+
const x0 = c2x[c0], x1 = c2x[c1], x2 = c2x[c2], x3 = c2x[c3], x4 = c2x[c4], x5 = c2x[c5] // prettier-ignore
|
| 239 |
+
if (x0 < 0 || x1 < 0 || x2 < 0 || x3 < 0 || x4 < 0 || x5 < 0) throw new SyntaxError(E_CHARACTER)
|
| 240 |
+
if ((merge(x0, x1, x2, x3, x4, x5) ^ p6(chk)) !== encoding) throw new Error(E_CHECKSUM)
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
return { prefix, bytes }
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
// This is designed to be a very quick check, skipping all other validation
|
| 247 |
+
export function getPrefix(str, limit = 90) {
|
| 248 |
+
assertDecodeArgs(str, limit)
|
| 249 |
+
const split = str.lastIndexOf('1')
|
| 250 |
+
if (split <= 0) throw new Error(E_PREFIX)
|
| 251 |
+
return str.slice(0, split).toLowerCase()
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
export const toBech32 = (prefix, bytes, limit = 90) => toBech32enc(prefix, bytes, limit, BECH32)
|
| 255 |
+
export const fromBech32 = (str, limit = 90) => fromBech32enc(str, limit, BECH32)
|
| 256 |
+
export const toBech32m = (prefix, bytes, limit = 90) => toBech32enc(prefix, bytes, limit, BECH32M)
|
| 257 |
+
export const fromBech32m = (str, limit = 90) => fromBech32enc(str, limit, BECH32M)
|
node_modules/@exodus/bytes/bigint.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Convert between BigInt and Uint8Array
|
| 3 |
+
*
|
| 4 |
+
* ```js
|
| 5 |
+
* import { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'
|
| 6 |
+
* ```
|
| 7 |
+
*
|
| 8 |
+
* @module @exodus/bytes/bigint.js
|
| 9 |
+
*/
|
| 10 |
+
|
| 11 |
+
/// <reference types="node" />
|
| 12 |
+
|
| 13 |
+
import type { OutputFormat, Uint8ArrayBuffer } from './array.js';
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* Options for converting BigInt to bytes
|
| 17 |
+
*/
|
| 18 |
+
export interface FromBigIntOptions {
|
| 19 |
+
/** The length in bytes of the output array */
|
| 20 |
+
length: number;
|
| 21 |
+
/** Output format (default: 'uint8') */
|
| 22 |
+
format?: OutputFormat;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/**
|
| 26 |
+
* Convert a BigInt to a Uint8Array or Buffer
|
| 27 |
+
*
|
| 28 |
+
* The output bytes are in big-endian format.
|
| 29 |
+
*
|
| 30 |
+
* Throws if the BigInt is negative or cannot fit into the specified length.
|
| 31 |
+
*
|
| 32 |
+
* @param bigint - The BigInt to convert (must be non-negative)
|
| 33 |
+
* @param options - Conversion options
|
| 34 |
+
* @returns The converted bytes in big-endian format
|
| 35 |
+
*/
|
| 36 |
+
export function fromBigInt(bigint: bigint, options: { length: number; format?: 'uint8' }): Uint8ArrayBuffer;
|
| 37 |
+
export function fromBigInt(bigint: bigint, options: { length: number; format: 'arraybuffer' }): ArrayBuffer;
|
| 38 |
+
export function fromBigInt(bigint: bigint, options: { length: number; format: 'buffer' }): Buffer;
|
| 39 |
+
export function fromBigInt(bigint: bigint, options: FromBigIntOptions): Uint8ArrayBuffer | ArrayBuffer | Buffer;
|
| 40 |
+
|
| 41 |
+
/**
|
| 42 |
+
* Convert a Uint8Array or Buffer to a BigInt
|
| 43 |
+
*
|
| 44 |
+
* The bytes are interpreted as a big-endian unsigned integer.
|
| 45 |
+
*
|
| 46 |
+
* @param arr - The bytes to convert
|
| 47 |
+
* @returns The BigInt representation
|
| 48 |
+
*/
|
| 49 |
+
export function toBigInt(arr: Uint8Array): bigint;
|
node_modules/@exodus/bytes/bigint.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { toHex, fromHex } from '@exodus/bytes/hex.js'
|
| 2 |
+
import { assert } from './fallback/_utils.js'
|
| 3 |
+
|
| 4 |
+
const _0n = BigInt(0)
|
| 5 |
+
|
| 6 |
+
export function fromBigInt(x, { length, format } = {}) {
|
| 7 |
+
assert(Number.isSafeInteger(length) && length > 0, 'Expected length arg to be a positive integer')
|
| 8 |
+
assert(typeof x === 'bigint' && x >= _0n, 'Expected a non-negative bigint')
|
| 9 |
+
const hex = x.toString(16)
|
| 10 |
+
assert(length * 2 >= hex.length, `Can not fit supplied number into ${length} bytes`)
|
| 11 |
+
return fromHex(hex.padStart(length * 2, '0'), format)
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export const toBigInt = (a) => BigInt('0x' + (toHex(a) || '0'))
|
node_modules/@exodus/bytes/encoding-browser.browser.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getBOMEncoding } from './fallback/encoding.api.js'
|
| 2 |
+
|
| 3 |
+
// Lite-weight version which re-exports existing implementations on browsers,
|
| 4 |
+
// while still being aliased to the full impl in RN and Node.js
|
| 5 |
+
|
| 6 |
+
// WARNING: Note that browsers have bugs (which hopefully will get fixed soon)
|
| 7 |
+
|
| 8 |
+
const { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream } = globalThis
|
| 9 |
+
|
| 10 |
+
export { getBOMEncoding } from './fallback/encoding.api.js'
|
| 11 |
+
export { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream }
|
| 12 |
+
|
| 13 |
+
export function normalizeEncoding(label) {
|
| 14 |
+
if (label === 'utf-8' || label === 'utf8' || label === 'UTF-8' || label === 'UTF8') return 'utf-8'
|
| 15 |
+
if (label === 'windows-1252' || label === 'ascii' || label === 'latin1') return 'windows-1252'
|
| 16 |
+
if (/[^\w\t\n\f\r .:-]/i.test(label)) return null
|
| 17 |
+
const l = `${label}`.trim().toLowerCase()
|
| 18 |
+
try {
|
| 19 |
+
return new TextDecoder(l).encoding
|
| 20 |
+
} catch {}
|
| 21 |
+
|
| 22 |
+
if (l === 'x-user-defined') return l
|
| 23 |
+
if (
|
| 24 |
+
l === 'replacement' ||
|
| 25 |
+
l === 'csiso2022kr' ||
|
| 26 |
+
l === 'hz-gb-2312' ||
|
| 27 |
+
l === 'iso-2022-cn' ||
|
| 28 |
+
l === 'iso-2022-cn-ext' ||
|
| 29 |
+
l === 'iso-2022-kr'
|
| 30 |
+
) {
|
| 31 |
+
return 'replacement'
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
return null
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
export function legacyHookDecode(input, fallbackEncoding = 'utf-8') {
|
| 38 |
+
const enc = getBOMEncoding(input) ?? normalizeEncoding(fallbackEncoding)
|
| 39 |
+
if (enc === 'replacement') return input.byteLength > 0 ? '\uFFFD' : ''
|
| 40 |
+
return new TextDecoder(enc).decode(input)
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
export function labelToName(label) {
|
| 44 |
+
const enc = normalizeEncoding(label)
|
| 45 |
+
if (enc === 'utf-8') return 'UTF-8'
|
| 46 |
+
if (!enc) return enc
|
| 47 |
+
const p = enc.slice(0, 3)
|
| 48 |
+
if (p === 'utf' || p === 'iso' || p === 'koi' || p === 'euc' || p === 'ibm' || p === 'gbk') {
|
| 49 |
+
return enc.toUpperCase()
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
if (enc === 'big5') return 'Big5'
|
| 53 |
+
if (enc === 'shift_jis') return 'Shift_JIS'
|
| 54 |
+
return enc
|
| 55 |
+
}
|
node_modules/@exodus/bytes/encoding-browser.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Same as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the
|
| 3 |
+
* browser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).
|
| 4 |
+
*
|
| 5 |
+
* Does not provide `isomorphicDecode` and `isomorphicEncode` exports.
|
| 6 |
+
*
|
| 7 |
+
* ```js
|
| 8 |
+
* import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'
|
| 9 |
+
* import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams
|
| 10 |
+
*
|
| 11 |
+
* // Hooks for standards
|
| 12 |
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'
|
| 13 |
+
* ```
|
| 14 |
+
*
|
| 15 |
+
* Under non-browser engines (Node.js, React Native, etc.) a full polyfill is used as those platforms
|
| 16 |
+
* do not provide sufficiently complete / non-buggy `TextDecoder` APIs.
|
| 17 |
+
*
|
| 18 |
+
* > [!NOTE]
|
| 19 |
+
* > Implementations in browsers [have bugs](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit),
|
| 20 |
+
* > but they are fixing them and the expected update window is short.\
|
| 21 |
+
* > If you want to circumvent browser bugs, use full `@exodus/bytes/encoding.js` import.
|
| 22 |
+
*
|
| 23 |
+
* @module @exodus/bytes/encoding-browser.js
|
| 24 |
+
*/
|
| 25 |
+
|
| 26 |
+
export {
|
| 27 |
+
TextDecoder,
|
| 28 |
+
TextEncoder,
|
| 29 |
+
TextDecoderStream,
|
| 30 |
+
TextEncoderStream,
|
| 31 |
+
normalizeEncoding,
|
| 32 |
+
getBOMEncoding,
|
| 33 |
+
labelToName,
|
| 34 |
+
legacyHookDecode,
|
| 35 |
+
} from './encoding.js'
|
node_modules/@exodus/bytes/encoding-browser.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
export * from './encoding-browser.native.js'
|
node_modules/@exodus/bytes/encoding-browser.native.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export {
|
| 2 |
+
TextDecoder,
|
| 3 |
+
TextEncoder,
|
| 4 |
+
TextDecoderStream,
|
| 5 |
+
TextEncoderStream,
|
| 6 |
+
normalizeEncoding,
|
| 7 |
+
getBOMEncoding,
|
| 8 |
+
labelToName,
|
| 9 |
+
legacyHookDecode,
|
| 10 |
+
} from './encoding.js'
|
node_modules/@exodus/bytes/encoding-lite.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* The exact same exports as `@exodus/bytes/encoding.js` are also exported as
|
| 3 |
+
* `@exodus/bytes/encoding-lite.js`, with the difference that the lite version does not load
|
| 4 |
+
* multi-byte `TextDecoder` encodings by default to reduce bundle size ~12x.
|
| 5 |
+
*
|
| 6 |
+
* ```js
|
| 7 |
+
* import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'
|
| 8 |
+
* import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js' // Requires Streams
|
| 9 |
+
* import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding-lite.js'
|
| 10 |
+
*
|
| 11 |
+
* // Hooks for standards
|
| 12 |
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'
|
| 13 |
+
* ```
|
| 14 |
+
*
|
| 15 |
+
* The only affected encodings are: `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`
|
| 16 |
+
* and their [labels](https://encoding.spec.whatwg.org/#names-and-labels) when used with `TextDecoder`.
|
| 17 |
+
*
|
| 18 |
+
* Legacy single-byte encodingds are loaded by default in both cases.
|
| 19 |
+
*
|
| 20 |
+
* `TextEncoder` and hooks for standards (including `labelToName` / `normalizeEncoding`) do not have any behavior
|
| 21 |
+
* differences in the lite version and support full range if inputs.
|
| 22 |
+
*
|
| 23 |
+
* To avoid inconsistencies, the exported classes and methods are exactly the same objects.
|
| 24 |
+
*
|
| 25 |
+
* ```console
|
| 26 |
+
* > lite = require('@exodus/bytes/encoding-lite.js')
|
| 27 |
+
* [Module: null prototype] {
|
| 28 |
+
* TextDecoder: [class TextDecoder],
|
| 29 |
+
* TextDecoderStream: [class TextDecoderStream],
|
| 30 |
+
* TextEncoder: [class TextEncoder],
|
| 31 |
+
* TextEncoderStream: [class TextEncoderStream],
|
| 32 |
+
* getBOMEncoding: [Function: getBOMEncoding],
|
| 33 |
+
* labelToName: [Function: labelToName],
|
| 34 |
+
* legacyHookDecode: [Function: legacyHookDecode],
|
| 35 |
+
* normalizeEncoding: [Function: normalizeEncoding]
|
| 36 |
+
* }
|
| 37 |
+
* > new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
| 38 |
+
* Uncaught:
|
| 39 |
+
* Error: Legacy multi-byte encodings are disabled in /encoding-lite.js, use /encoding.js for full encodings range support
|
| 40 |
+
*
|
| 41 |
+
* > full = require('@exodus/bytes/encoding.js')
|
| 42 |
+
* [Module: null prototype] {
|
| 43 |
+
* TextDecoder: [class TextDecoder],
|
| 44 |
+
* TextDecoderStream: [class TextDecoderStream],
|
| 45 |
+
* TextEncoder: [class TextEncoder],
|
| 46 |
+
* TextEncoderStream: [class TextEncoderStream],
|
| 47 |
+
* getBOMEncoding: [Function: getBOMEncoding],
|
| 48 |
+
* labelToName: [Function: labelToName],
|
| 49 |
+
* legacyHookDecode: [Function: legacyHookDecode],
|
| 50 |
+
* normalizeEncoding: [Function: normalizeEncoding]
|
| 51 |
+
* }
|
| 52 |
+
* > full.TextDecoder === lite.TextDecoder
|
| 53 |
+
* true
|
| 54 |
+
* > new full.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
| 55 |
+
* '%'
|
| 56 |
+
* > new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))
|
| 57 |
+
* '%'
|
| 58 |
+
* ```
|
| 59 |
+
*
|
| 60 |
+
* @module @exodus/bytes/encoding-lite.js
|
| 61 |
+
*/
|
| 62 |
+
|
| 63 |
+
export * from './encoding.js'
|
node_modules/@exodus/bytes/encoding-lite.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export {
|
| 2 |
+
TextDecoder,
|
| 3 |
+
TextEncoder,
|
| 4 |
+
TextDecoderStream,
|
| 5 |
+
TextEncoderStream,
|
| 6 |
+
normalizeEncoding,
|
| 7 |
+
getBOMEncoding,
|
| 8 |
+
labelToName,
|
| 9 |
+
legacyHookDecode,
|
| 10 |
+
isomorphicDecode,
|
| 11 |
+
isomorphicEncode,
|
| 12 |
+
} from './fallback/encoding.js'
|
node_modules/@exodus/bytes/encoding.d.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
|
| 3 |
+
* [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
|
| 4 |
+
* [TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder),
|
| 5 |
+
* [TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream),
|
| 6 |
+
* [TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream),
|
| 7 |
+
* some [hooks](https://encoding.spec.whatwg.org/#specification-hooks).
|
| 8 |
+
*
|
| 9 |
+
* ```js
|
| 10 |
+
* import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
|
| 11 |
+
* import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
|
| 12 |
+
* import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding.js'
|
| 13 |
+
*
|
| 14 |
+
* // Hooks for standards
|
| 15 |
+
* import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
|
| 16 |
+
* ```
|
| 17 |
+
*
|
| 18 |
+
* @module @exodus/bytes/encoding.js
|
| 19 |
+
*/
|
| 20 |
+
|
| 21 |
+
/// <reference types="node" />
|
| 22 |
+
|
| 23 |
+
/**
|
| 24 |
+
* Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
|
| 25 |
+
* as an ASCII-lowercased string.
|
| 26 |
+
*
|
| 27 |
+
* If an encoding with that label does not exist, returns `null`.
|
| 28 |
+
*
|
| 29 |
+
* This is the same as [`decoder.encoding` getter](https://encoding.spec.whatwg.org/#dom-textdecoder-encoding),
|
| 30 |
+
* except that it:
|
| 31 |
+
* 1. Supports [`replacement` encoding](https://encoding.spec.whatwg.org/#replacement) and its
|
| 32 |
+
* [labels](https://encoding.spec.whatwg.org/#ref-for-replacement%E2%91%A1)
|
| 33 |
+
* 2. Does not throw for invalid labels and instead returns `null`
|
| 34 |
+
*
|
| 35 |
+
* It is identical to:
|
| 36 |
+
* ```js
|
| 37 |
+
* labelToName(label)?.toLowerCase() ?? null
|
| 38 |
+
* ```
|
| 39 |
+
*
|
| 40 |
+
* All encoding names are also valid labels for corresponding encodings.
|
| 41 |
+
*
|
| 42 |
+
* @param label - The encoding label to normalize
|
| 43 |
+
* @returns The normalized encoding name, or null if invalid
|
| 44 |
+
*/
|
| 45 |
+
export function normalizeEncoding(label: string): string | null;
|
| 46 |
+
|
| 47 |
+
/**
|
| 48 |
+
* Implements [BOM sniff](https://encoding.spec.whatwg.org/#bom-sniff) legacy hook.
|
| 49 |
+
*
|
| 50 |
+
* Given a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:
|
| 51 |
+
* - `'utf-8'`, if `input` starts with UTF-8 byte order mark.
|
| 52 |
+
* - `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
|
| 53 |
+
* - `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
|
| 54 |
+
* - `null` otherwise.
|
| 55 |
+
*
|
| 56 |
+
* @param input - The bytes to check for BOM
|
| 57 |
+
* @returns The encoding ('utf-8', 'utf-16le', 'utf-16be'), or null if no BOM found
|
| 58 |
+
*/
|
| 59 |
+
export function getBOMEncoding(
|
| 60 |
+
input: ArrayBufferLike | ArrayBufferView
|
| 61 |
+
): 'utf-8' | 'utf-16le' | 'utf-16be' | null;
|
| 62 |
+
|
| 63 |
+
/**
|
| 64 |
+
* Implements [decode](https://encoding.spec.whatwg.org/#decode) legacy hook.
|
| 65 |
+
*
|
| 66 |
+
* Given a `TypedArray` or an `ArrayBuffer` instance `input` and an optional `fallbackEncoding`
|
| 67 |
+
* encoding [label](https://encoding.spec.whatwg.org/#names-and-labels),
|
| 68 |
+
* sniffs encoding from BOM with `fallbackEncoding` fallback and then
|
| 69 |
+
* decodes the `input` using that encoding, skipping BOM if it was present.
|
| 70 |
+
*
|
| 71 |
+
* Notes:
|
| 72 |
+
*
|
| 73 |
+
* - BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
|
| 74 |
+
* Use with care.
|
| 75 |
+
* - Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
|
| 76 |
+
* aka replacement. It can convert different byte sequences to equal strings.
|
| 77 |
+
*
|
| 78 |
+
* This method is similar to the following code, except that it doesn't support encoding labels and
|
| 79 |
+
* only expects lowercased encoding name:
|
| 80 |
+
*
|
| 81 |
+
* ```js
|
| 82 |
+
* new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)
|
| 83 |
+
* ```
|
| 84 |
+
*
|
| 85 |
+
* @param input - The bytes to decode
|
| 86 |
+
* @param fallbackEncoding - The encoding to use if no BOM detected (default: 'utf-8')
|
| 87 |
+
* @returns The decoded string
|
| 88 |
+
*/
|
| 89 |
+
export function legacyHookDecode(
|
| 90 |
+
input: ArrayBufferLike | ArrayBufferView,
|
| 91 |
+
fallbackEncoding?: string
|
| 92 |
+
): string;
|
| 93 |
+
|
| 94 |
+
/**
|
| 95 |
+
* Implements [isomorphic decode](https://infra.spec.whatwg.org/#isomorphic-decode).
|
| 96 |
+
*
|
| 97 |
+
* Given a `TypedArray` or an `ArrayBuffer` instance `input`, creates a string of the same length
|
| 98 |
+
* as input byteLength, using bytes from input as codepoints.
|
| 99 |
+
*
|
| 100 |
+
* E.g. for `Uint8Array` input, this is similar to `String.fromCodePoint(...input)`.
|
| 101 |
+
*
|
| 102 |
+
* Wider `TypedArray` inputs, e.g. `Uint16Array`, are interpreted as underlying _bytes_.
|
| 103 |
+
*
|
| 104 |
+
* @param input - The bytes to decode
|
| 105 |
+
* @returns The decoded string
|
| 106 |
+
*/
|
| 107 |
+
export function isomorphicDecode(input: ArrayBufferLike | ArrayBufferView): string;
|
| 108 |
+
|
| 109 |
+
/**
|
| 110 |
+
* Implements [isomorphic encode](https://infra.spec.whatwg.org/#isomorphic-encode).
|
| 111 |
+
*
|
| 112 |
+
* Given a string, creates an `Uint8Array` of the same length with the string codepoints as byte values.
|
| 113 |
+
*
|
| 114 |
+
* Accepts only [isomorphic string](https://infra.spec.whatwg.org/#isomorphic-string) input
|
| 115 |
+
* and asserts that, throwing on any strings containing codepoints higher than `U+00FF`.
|
| 116 |
+
*
|
| 117 |
+
* @param input - The bytes to decode
|
| 118 |
+
* @returns An Uint8Array containing the input bytes.
|
| 119 |
+
*/
|
| 120 |
+
export function isomorphicEncode(str: string): Uint8Array;
|
| 121 |
+
|
| 122 |
+
/**
|
| 123 |
+
* Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
|
| 124 |
+
*
|
| 125 |
+
* Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
|
| 126 |
+
* as a case-sensitive string.
|
| 127 |
+
*
|
| 128 |
+
* If an encoding with that label does not exist, returns `null`.
|
| 129 |
+
*
|
| 130 |
+
* All encoding names are also valid labels for corresponding encodings.
|
| 131 |
+
*
|
| 132 |
+
* @param label - The encoding label
|
| 133 |
+
* @returns The proper case encoding name, or null if invalid
|
| 134 |
+
*/
|
| 135 |
+
export function labelToName(label: string): string | null;
|
| 136 |
+
|
| 137 |
+
/**
|
| 138 |
+
* [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
|
| 139 |
+
*
|
| 140 |
+
* Decode bytes to strings according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
|
| 141 |
+
*/
|
| 142 |
+
export const TextDecoder: typeof globalThis.TextDecoder;
|
| 143 |
+
|
| 144 |
+
/**
|
| 145 |
+
* [TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) implementation/polyfill.
|
| 146 |
+
*
|
| 147 |
+
* Encode strings to UTF-8 bytes according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
|
| 148 |
+
*/
|
| 149 |
+
export const TextEncoder: typeof globalThis.TextEncoder;
|
| 150 |
+
|
| 151 |
+
/**
|
| 152 |
+
* [TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream) implementation/polyfill.
|
| 153 |
+
*
|
| 154 |
+
* A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextDecoder`.
|
| 155 |
+
*
|
| 156 |
+
* Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
|
| 157 |
+
* [polyfilled](https://npmjs.com/package/web-streams-polyfill).
|
| 158 |
+
*/
|
| 159 |
+
export const TextDecoderStream: typeof globalThis.TextDecoderStream;
|
| 160 |
+
|
| 161 |
+
/**
|
| 162 |
+
* [TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream) implementation/polyfill.
|
| 163 |
+
*
|
| 164 |
+
* A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.
|
| 165 |
+
*
|
| 166 |
+
* Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
|
| 167 |
+
* [polyfilled](https://npmjs.com/package/web-streams-polyfill).
|
| 168 |
+
*/
|
| 169 |
+
export const TextEncoderStream: typeof globalThis.TextEncoderStream;
|
node_modules/@exodus/bytes/encoding.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createMultibyteDecoder } from '@exodus/bytes/multi-byte.js'
|
| 2 |
+
import { multibyteEncoder } from './fallback/multi-byte.js'
|
| 3 |
+
import { setMultibyte } from './fallback/encoding.js'
|
| 4 |
+
|
| 5 |
+
setMultibyte(createMultibyteDecoder, multibyteEncoder)
|
| 6 |
+
|
| 7 |
+
export {
|
| 8 |
+
TextDecoder,
|
| 9 |
+
TextEncoder,
|
| 10 |
+
TextDecoderStream,
|
| 11 |
+
TextEncoderStream,
|
| 12 |
+
normalizeEncoding,
|
| 13 |
+
getBOMEncoding,
|
| 14 |
+
labelToName,
|
| 15 |
+
legacyHookDecode,
|
| 16 |
+
isomorphicDecode,
|
| 17 |
+
isomorphicEncode,
|
| 18 |
+
} from './fallback/encoding.js'
|
node_modules/@exodus/bytes/fallback/_utils.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export * from './platform.js'
|
| 2 |
+
|
| 3 |
+
const Buffer = /* @__PURE__ */ (() => globalThis.Buffer)()
|
| 4 |
+
|
| 5 |
+
export function assert(condition, msg) {
|
| 6 |
+
if (!condition) throw new Error(msg)
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export function assertU8(arg) {
|
| 10 |
+
if (arg && arg instanceof Uint8Array) return
|
| 11 |
+
throw new TypeError('Expected an Uint8Array')
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
// On arrays in heap (<= 64) it's cheaper to copy into a pooled buffer than lazy-create the ArrayBuffer storage
|
| 15 |
+
export const toBuf = (x) =>
|
| 16 |
+
x.byteLength <= 64 && x.BYTES_PER_ELEMENT === 1
|
| 17 |
+
? Buffer.from(x)
|
| 18 |
+
: Buffer.from(x.buffer, x.byteOffset, x.byteLength)
|
| 19 |
+
|
| 20 |
+
export const E_STRING = 'Input is not a string'
|
| 21 |
+
export const E_STRICT_UNICODE = 'Input is not well-formed Unicode'
|
| 22 |
+
|
| 23 |
+
// Input is never pooled
|
| 24 |
+
export function fromUint8(arr, format) {
|
| 25 |
+
switch (format) {
|
| 26 |
+
case 'uint8':
|
| 27 |
+
if (arr.constructor !== Uint8Array) throw new Error('Unexpected')
|
| 28 |
+
return arr
|
| 29 |
+
case 'arraybuffer':
|
| 30 |
+
if (arr.byteLength !== arr.buffer.byteLength) throw new Error('Unexpected')
|
| 31 |
+
return arr.buffer
|
| 32 |
+
case 'buffer':
|
| 33 |
+
if (arr.length <= 64) return Buffer.from(arr)
|
| 34 |
+
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
throw new TypeError('Unexpected format')
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Input can be pooled
|
| 41 |
+
export function fromBuffer(arr, format) {
|
| 42 |
+
switch (format) {
|
| 43 |
+
case 'uint8':
|
| 44 |
+
// byteOffset check is slightly faster and covers most pooling, so it comes first
|
| 45 |
+
if (arr.length <= 64 || arr.byteOffset !== 0 || arr.byteLength !== arr.buffer.byteLength) {
|
| 46 |
+
return new Uint8Array(arr)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)
|
| 50 |
+
case 'arraybuffer':
|
| 51 |
+
return arr.buffer.byteLength === arr.byteLength
|
| 52 |
+
? arr.buffer
|
| 53 |
+
: arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength)
|
| 54 |
+
case 'buffer':
|
| 55 |
+
if (arr.constructor !== Buffer) throw new Error('Unexpected')
|
| 56 |
+
return arr
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
throw new TypeError('Unexpected format')
|
| 60 |
+
}
|
node_modules/@exodus/bytes/fallback/base32.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { assertU8 } from './_utils.js'
|
| 2 |
+
import { nativeEncoder, nativeDecoder, isHermes } from './platform.js'
|
| 3 |
+
import { encodeAscii, decodeAscii } from './latin1.js'
|
| 4 |
+
|
| 5 |
+
// See https://datatracker.ietf.org/doc/html/rfc4648
|
| 6 |
+
|
| 7 |
+
const BASE32_HELPERS = [{}, {}, {}]
|
| 8 |
+
const BASE32_ALPHABETS = [
|
| 9 |
+
[...'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'], // RFC 4648, #6
|
| 10 |
+
[...'0123456789ABCDEFGHIJKLMNOPQRSTUV'], // RFC 4648, #7
|
| 11 |
+
[...'0123456789ABCDEFGHJKMNPQRSTVWXYZ'], // Crockford, base (see extra below in fromMap)
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
export const E_CHAR = 'Invalid character in base32 input'
|
| 15 |
+
export const E_PADDING = 'Invalid base32 padding'
|
| 16 |
+
export const E_LENGTH = 'Invalid base32 length'
|
| 17 |
+
export const E_LAST = 'Invalid last chunk'
|
| 18 |
+
|
| 19 |
+
const useTemplates = isHermes // Faster on Hermes and JSC, but we use it only on Hermes
|
| 20 |
+
|
| 21 |
+
// We construct output by concatenating chars, this seems to be fine enough on modern JS engines
|
| 22 |
+
export function toBase32(arr, mode, padding) {
|
| 23 |
+
assertU8(arr)
|
| 24 |
+
const fullChunks = Math.floor(arr.length / 5)
|
| 25 |
+
const fullChunksBytes = fullChunks * 5
|
| 26 |
+
let o = ''
|
| 27 |
+
let i = 0
|
| 28 |
+
|
| 29 |
+
const alphabet = BASE32_ALPHABETS[mode]
|
| 30 |
+
const helpers = BASE32_HELPERS[mode]
|
| 31 |
+
if (!helpers.pairs) {
|
| 32 |
+
helpers.pairs = []
|
| 33 |
+
if (nativeDecoder) {
|
| 34 |
+
// Lazy to save memory in case if this is not needed
|
| 35 |
+
helpers.codepairs = new Uint16Array(32 * 32)
|
| 36 |
+
const u16 = helpers.codepairs
|
| 37 |
+
const u8 = new Uint8Array(u16.buffer, u16.byteOffset, u16.byteLength) // write as 1-byte to ignore BE/LE difference
|
| 38 |
+
for (let i = 0; i < 32; i++) {
|
| 39 |
+
const ic = alphabet[i].charCodeAt(0)
|
| 40 |
+
for (let j = 0; j < 32; j++) u8[(i << 6) | (j << 1)] = u8[(j << 6) | ((i << 1) + 1)] = ic
|
| 41 |
+
}
|
| 42 |
+
} else {
|
| 43 |
+
const p = helpers.pairs
|
| 44 |
+
for (let i = 0; i < 32; i++) {
|
| 45 |
+
for (let j = 0; j < 32; j++) p.push(`${alphabet[i]}${alphabet[j]}`)
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
const { pairs, codepairs } = helpers
|
| 51 |
+
|
| 52 |
+
// Fast path for complete blocks
|
| 53 |
+
// This whole loop can be commented out, the algorithm won't change, it's just an optimization of the next loop
|
| 54 |
+
if (nativeDecoder) {
|
| 55 |
+
const oa = new Uint16Array(fullChunks * 4)
|
| 56 |
+
for (let j = 0; i < fullChunksBytes; i += 5) {
|
| 57 |
+
const a = arr[i]
|
| 58 |
+
const b = arr[i + 1]
|
| 59 |
+
const c = arr[i + 2]
|
| 60 |
+
const d = arr[i + 3]
|
| 61 |
+
const e = arr[i + 4]
|
| 62 |
+
const x0 = (a << 2) | (b >> 6) // 8 + 8 - 5 - 5 = 6 left
|
| 63 |
+
const x1 = ((b & 0x3f) << 4) | (c >> 4) // 6 + 8 - 5 - 5 = 4 left
|
| 64 |
+
const x2 = ((c & 0xf) << 6) | (d >> 2) // 4 + 8 - 5 - 5 = 2 left
|
| 65 |
+
const x3 = ((d & 0x3) << 8) | e // 2 + 8 - 5 - 5 = 0 left
|
| 66 |
+
oa[j] = codepairs[x0]
|
| 67 |
+
oa[j + 1] = codepairs[x1]
|
| 68 |
+
oa[j + 2] = codepairs[x2]
|
| 69 |
+
oa[j + 3] = codepairs[x3]
|
| 70 |
+
j += 4
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
o = decodeAscii(oa)
|
| 74 |
+
} else if (useTemplates) {
|
| 75 |
+
// Templates are faster only on Hermes and JSC. Browsers have TextDecoder anyway
|
| 76 |
+
for (; i < fullChunksBytes; i += 5) {
|
| 77 |
+
const a = arr[i]
|
| 78 |
+
const b = arr[i + 1]
|
| 79 |
+
const c = arr[i + 2]
|
| 80 |
+
const d = arr[i + 3]
|
| 81 |
+
const e = arr[i + 4]
|
| 82 |
+
const x0 = (a << 2) | (b >> 6) // 8 + 8 - 5 - 5 = 6 left
|
| 83 |
+
const x1 = ((b & 0x3f) << 4) | (c >> 4) // 6 + 8 - 5 - 5 = 4 left
|
| 84 |
+
const x2 = ((c & 0xf) << 6) | (d >> 2) // 4 + 8 - 5 - 5 = 2 left
|
| 85 |
+
const x3 = ((d & 0x3) << 8) | e // 2 + 8 - 5 - 5 = 0 left
|
| 86 |
+
o += `${pairs[x0]}${pairs[x1]}${pairs[x2]}${pairs[x3]}`
|
| 87 |
+
}
|
| 88 |
+
} else {
|
| 89 |
+
for (; i < fullChunksBytes; i += 5) {
|
| 90 |
+
const a = arr[i]
|
| 91 |
+
const b = arr[i + 1]
|
| 92 |
+
const c = arr[i + 2]
|
| 93 |
+
const d = arr[i + 3]
|
| 94 |
+
const e = arr[i + 4]
|
| 95 |
+
const x0 = (a << 2) | (b >> 6) // 8 + 8 - 5 - 5 = 6 left
|
| 96 |
+
const x1 = ((b & 0x3f) << 4) | (c >> 4) // 6 + 8 - 5 - 5 = 4 left
|
| 97 |
+
const x2 = ((c & 0xf) << 6) | (d >> 2) // 4 + 8 - 5 - 5 = 2 left
|
| 98 |
+
const x3 = ((d & 0x3) << 8) | e // 2 + 8 - 5 - 5 = 0 left
|
| 99 |
+
o += pairs[x0]
|
| 100 |
+
o += pairs[x1]
|
| 101 |
+
o += pairs[x2]
|
| 102 |
+
o += pairs[x3]
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// If we have something left, process it with a full algo
|
| 107 |
+
let carry = 0
|
| 108 |
+
let shift = 3 // First byte needs to be shifted by 3 to get 5 bits
|
| 109 |
+
for (; i < arr.length; i++) {
|
| 110 |
+
const x = arr[i]
|
| 111 |
+
o += alphabet[carry | (x >> shift)] // shift >= 3, so this fits
|
| 112 |
+
if (shift >= 5) {
|
| 113 |
+
shift -= 5
|
| 114 |
+
o += alphabet[(x >> shift) & 0x1f]
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
carry = (x << (5 - shift)) & 0x1f
|
| 118 |
+
shift += 3 // Each byte prints 5 bits and leaves 3 bits
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
if (shift !== 3) o += alphabet[carry] // shift 3 means we have no carry left
|
| 122 |
+
if (padding) o += ['', '======', '====', '===', '='][arr.length - fullChunksBytes]
|
| 123 |
+
|
| 124 |
+
return o
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// TODO: can this be optimized? This only affects non-Hermes barebone engines though
|
| 128 |
+
const mapSize = nativeEncoder ? 128 : 65_536 // we have to store 64 KiB map or recheck everything if we can't decode to byte array
|
| 129 |
+
|
| 130 |
+
export function fromBase32(str, mode) {
|
| 131 |
+
let inputLength = str.length
|
| 132 |
+
while (str[inputLength - 1] === '=') inputLength--
|
| 133 |
+
const paddingLength = str.length - inputLength
|
| 134 |
+
const tailLength = inputLength % 8
|
| 135 |
+
const mainLength = inputLength - tailLength // multiples of 8
|
| 136 |
+
if (![0, 2, 4, 5, 7].includes(tailLength)) throw new SyntaxError(E_LENGTH) // fast verification
|
| 137 |
+
if (paddingLength > 7 || (paddingLength !== 0 && str.length % 8 !== 0)) {
|
| 138 |
+
throw new SyntaxError(E_PADDING)
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
const alphabet = BASE32_ALPHABETS[mode]
|
| 142 |
+
const helpers = BASE32_HELPERS[mode]
|
| 143 |
+
|
| 144 |
+
if (!helpers.fromMap) {
|
| 145 |
+
helpers.fromMap = new Int8Array(mapSize).fill(-1) // no regex input validation here, so we map all other bytes to -1 and recheck sign
|
| 146 |
+
const m = helpers.fromMap
|
| 147 |
+
alphabet.forEach((c, i) => {
|
| 148 |
+
m[c.charCodeAt(0)] = m[c.toLowerCase().charCodeAt(0)] = i
|
| 149 |
+
})
|
| 150 |
+
|
| 151 |
+
if (mode === 2) {
|
| 152 |
+
// Extra Crockford mapping
|
| 153 |
+
m[73] = m[76] = m[105] = m[108] = m[49] // ILil -> 1
|
| 154 |
+
m[79] = m[111] = m[48] // Oo -> 0
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
const m = helpers.fromMap
|
| 159 |
+
|
| 160 |
+
const arr = new Uint8Array(Math.floor((inputLength * 5) / 8))
|
| 161 |
+
let at = 0
|
| 162 |
+
let i = 0
|
| 163 |
+
|
| 164 |
+
if (nativeEncoder) {
|
| 165 |
+
const codes = encodeAscii(str, E_CHAR)
|
| 166 |
+
for (; i < mainLength; i += 8) {
|
| 167 |
+
// each 5 bits, grouped 5 * 4 = 20
|
| 168 |
+
const x0 = codes[i]
|
| 169 |
+
const x1 = codes[i + 1]
|
| 170 |
+
const x2 = codes[i + 2]
|
| 171 |
+
const x3 = codes[i + 3]
|
| 172 |
+
const x4 = codes[i + 4]
|
| 173 |
+
const x5 = codes[i + 5]
|
| 174 |
+
const x6 = codes[i + 6]
|
| 175 |
+
const x7 = codes[i + 7]
|
| 176 |
+
const a = (m[x0] << 15) | (m[x1] << 10) | (m[x2] << 5) | m[x3]
|
| 177 |
+
const b = (m[x4] << 15) | (m[x5] << 10) | (m[x6] << 5) | m[x7]
|
| 178 |
+
if (a < 0 || b < 0) throw new SyntaxError(E_CHAR)
|
| 179 |
+
arr[at] = a >> 12
|
| 180 |
+
arr[at + 1] = (a >> 4) & 0xff
|
| 181 |
+
arr[at + 2] = ((a << 4) & 0xff) | (b >> 16)
|
| 182 |
+
arr[at + 3] = (b >> 8) & 0xff
|
| 183 |
+
arr[at + 4] = b & 0xff
|
| 184 |
+
at += 5
|
| 185 |
+
}
|
| 186 |
+
} else {
|
| 187 |
+
for (; i < mainLength; i += 8) {
|
| 188 |
+
// each 5 bits, grouped 5 * 4 = 20
|
| 189 |
+
const x0 = str.charCodeAt(i)
|
| 190 |
+
const x1 = str.charCodeAt(i + 1)
|
| 191 |
+
const x2 = str.charCodeAt(i + 2)
|
| 192 |
+
const x3 = str.charCodeAt(i + 3)
|
| 193 |
+
const x4 = str.charCodeAt(i + 4)
|
| 194 |
+
const x5 = str.charCodeAt(i + 5)
|
| 195 |
+
const x6 = str.charCodeAt(i + 6)
|
| 196 |
+
const x7 = str.charCodeAt(i + 7)
|
| 197 |
+
const a = (m[x0] << 15) | (m[x1] << 10) | (m[x2] << 5) | m[x3]
|
| 198 |
+
const b = (m[x4] << 15) | (m[x5] << 10) | (m[x6] << 5) | m[x7]
|
| 199 |
+
if (a < 0 || b < 0) throw new SyntaxError(E_CHAR)
|
| 200 |
+
arr[at] = a >> 12
|
| 201 |
+
arr[at + 1] = (a >> 4) & 0xff
|
| 202 |
+
arr[at + 2] = ((a << 4) & 0xff) | (b >> 16)
|
| 203 |
+
arr[at + 3] = (b >> 8) & 0xff
|
| 204 |
+
arr[at + 4] = b & 0xff
|
| 205 |
+
at += 5
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
// Last block, valid tailLength: 0 2 4 5 7, checked already
|
| 210 |
+
// We check last chunk to be strict
|
| 211 |
+
if (tailLength < 2) return arr
|
| 212 |
+
const ab = (m[str.charCodeAt(i++)] << 5) | m[str.charCodeAt(i++)]
|
| 213 |
+
if (ab < 0) throw new SyntaxError(E_CHAR)
|
| 214 |
+
arr[at++] = ab >> 2
|
| 215 |
+
if (tailLength < 4) {
|
| 216 |
+
if (ab & 0x3) throw new SyntaxError(E_LAST)
|
| 217 |
+
return arr
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
const cd = (m[str.charCodeAt(i++)] << 5) | m[str.charCodeAt(i++)]
|
| 221 |
+
if (cd < 0) throw new SyntaxError(E_CHAR)
|
| 222 |
+
arr[at++] = ((ab << 6) & 0xff) | (cd >> 4)
|
| 223 |
+
if (tailLength < 5) {
|
| 224 |
+
if (cd & 0xf) throw new SyntaxError(E_LAST)
|
| 225 |
+
return arr
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
const e = m[str.charCodeAt(i++)]
|
| 229 |
+
if (e < 0) throw new SyntaxError(E_CHAR)
|
| 230 |
+
arr[at++] = ((cd << 4) & 0xff) | (e >> 1) // 4 + 4
|
| 231 |
+
if (tailLength < 7) {
|
| 232 |
+
if (e & 0x1) throw new SyntaxError(E_LAST)
|
| 233 |
+
return arr
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
const fg = (m[str.charCodeAt(i++)] << 5) | m[str.charCodeAt(i++)]
|
| 237 |
+
if (fg < 0) throw new SyntaxError(E_CHAR)
|
| 238 |
+
arr[at++] = ((e << 7) & 0xff) | (fg >> 3) // 1 + 5 + 2
|
| 239 |
+
// Can't be 8, so no h
|
| 240 |
+
if (fg & 0x7) throw new SyntaxError(E_LAST)
|
| 241 |
+
return arr
|
| 242 |
+
}
|
node_modules/@exodus/bytes/fallback/base58check.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { toBase58, fromBase58 } from '@exodus/bytes/base58.js'
|
| 2 |
+
import { assertU8, fromUint8 } from './_utils.js'
|
| 3 |
+
|
| 4 |
+
const E_CHECKSUM = 'Invalid checksum'
|
| 5 |
+
|
| 6 |
+
// checksum length is 4, i.e. only the first 4 bytes of the hash are used
|
| 7 |
+
|
| 8 |
+
function encodeWithChecksum(arr, checksum) {
|
| 9 |
+
// arr type in already validated in input
|
| 10 |
+
const res = new Uint8Array(arr.length + 4)
|
| 11 |
+
res.set(arr, 0)
|
| 12 |
+
res.set(checksum.slice(0, 4), arr.length)
|
| 13 |
+
return toBase58(res)
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
function decodeWithChecksum(str) {
|
| 17 |
+
const arr = fromBase58(str) // checks input
|
| 18 |
+
const payloadSize = arr.length - 4
|
| 19 |
+
if (payloadSize < 0) throw new Error(E_CHECKSUM)
|
| 20 |
+
return [arr.slice(0, payloadSize), arr.slice(payloadSize)]
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
function assertChecksum(c, r) {
|
| 24 |
+
if ((c[0] ^ r[0]) | (c[1] ^ r[1]) | (c[2] ^ r[2]) | (c[3] ^ r[3])) throw new Error(E_CHECKSUM)
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
export const makeBase58check = (hashAlgo, hashAlgoSync) => {
|
| 28 |
+
const apis = {
|
| 29 |
+
async encode(arr) {
|
| 30 |
+
assertU8(arr)
|
| 31 |
+
return encodeWithChecksum(arr, await hashAlgo(arr))
|
| 32 |
+
},
|
| 33 |
+
async decode(str, format = 'uint8') {
|
| 34 |
+
const [payload, checksum] = decodeWithChecksum(str)
|
| 35 |
+
assertChecksum(checksum, await hashAlgo(payload))
|
| 36 |
+
return fromUint8(payload, format)
|
| 37 |
+
},
|
| 38 |
+
}
|
| 39 |
+
if (!hashAlgoSync) return apis
|
| 40 |
+
return {
|
| 41 |
+
...apis,
|
| 42 |
+
encodeSync(arr) {
|
| 43 |
+
assertU8(arr)
|
| 44 |
+
return encodeWithChecksum(arr, hashAlgoSync(arr))
|
| 45 |
+
},
|
| 46 |
+
decodeSync(str, format = 'uint8') {
|
| 47 |
+
const [payload, checksum] = decodeWithChecksum(str)
|
| 48 |
+
assertChecksum(checksum, hashAlgoSync(payload))
|
| 49 |
+
return fromUint8(payload, format)
|
| 50 |
+
},
|
| 51 |
+
}
|
| 52 |
+
}
|
node_modules/@exodus/bytes/fallback/base64.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { nativeEncoder, nativeDecoder } from './platform.js'
|
| 2 |
+
import { encodeAscii, decodeAscii } from './latin1.js'
|
| 3 |
+
|
| 4 |
+
// See https://datatracker.ietf.org/doc/html/rfc4648
|
| 5 |
+
|
| 6 |
+
const BASE64 = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/']
|
| 7 |
+
const BASE64URL = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_']
|
| 8 |
+
const BASE64_HELPERS = {}
|
| 9 |
+
const BASE64URL_HELPERS = {}
|
| 10 |
+
|
| 11 |
+
export const E_CHAR = 'Invalid character in base64 input'
|
| 12 |
+
export const E_PADDING = 'Invalid base64 padding'
|
| 13 |
+
export const E_LENGTH = 'Invalid base64 length'
|
| 14 |
+
export const E_LAST = 'Invalid last chunk'
|
| 15 |
+
|
| 16 |
+
// We construct output by concatenating chars, this seems to be fine enough on modern JS engines
|
| 17 |
+
// Expects a checked Uint8Array
|
| 18 |
+
export function toBase64(arr, isURL, padding) {
|
| 19 |
+
const fullChunks = (arr.length / 3) | 0
|
| 20 |
+
const fullChunksBytes = fullChunks * 3
|
| 21 |
+
let o = ''
|
| 22 |
+
let i = 0
|
| 23 |
+
|
| 24 |
+
const alphabet = isURL ? BASE64URL : BASE64
|
| 25 |
+
const helpers = isURL ? BASE64URL_HELPERS : BASE64_HELPERS
|
| 26 |
+
if (!helpers.pairs) {
|
| 27 |
+
helpers.pairs = []
|
| 28 |
+
if (nativeDecoder) {
|
| 29 |
+
// Lazy to save memory in case if this is not needed
|
| 30 |
+
helpers.codepairs = new Uint16Array(64 * 64)
|
| 31 |
+
const u16 = helpers.codepairs
|
| 32 |
+
const u8 = new Uint8Array(u16.buffer, u16.byteOffset, u16.byteLength) // write as 1-byte to ignore BE/LE difference
|
| 33 |
+
for (let i = 0; i < 64; i++) {
|
| 34 |
+
const ic = alphabet[i].charCodeAt(0)
|
| 35 |
+
for (let j = 0; j < 64; j++) u8[(i << 7) | (j << 1)] = u8[(j << 7) | ((i << 1) + 1)] = ic
|
| 36 |
+
}
|
| 37 |
+
} else {
|
| 38 |
+
const p = helpers.pairs
|
| 39 |
+
for (let i = 0; i < 64; i++) {
|
| 40 |
+
for (let j = 0; j < 64; j++) p.push(`${alphabet[i]}${alphabet[j]}`)
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const { pairs, codepairs } = helpers
|
| 46 |
+
|
| 47 |
+
// Fast path for complete blocks
|
| 48 |
+
// This whole loop can be commented out, the algorithm won't change, it's just an optimization of the next loop
|
| 49 |
+
if (nativeDecoder) {
|
| 50 |
+
const oa = new Uint16Array(fullChunks * 2)
|
| 51 |
+
let j = 0
|
| 52 |
+
for (const last = arr.length - 11; i < last; i += 12, j += 8) {
|
| 53 |
+
const x0 = arr[i]
|
| 54 |
+
const x1 = arr[i + 1]
|
| 55 |
+
const x2 = arr[i + 2]
|
| 56 |
+
const x3 = arr[i + 3]
|
| 57 |
+
const x4 = arr[i + 4]
|
| 58 |
+
const x5 = arr[i + 5]
|
| 59 |
+
const x6 = arr[i + 6]
|
| 60 |
+
const x7 = arr[i + 7]
|
| 61 |
+
const x8 = arr[i + 8]
|
| 62 |
+
const x9 = arr[i + 9]
|
| 63 |
+
const x10 = arr[i + 10]
|
| 64 |
+
const x11 = arr[i + 11]
|
| 65 |
+
oa[j] = codepairs[(x0 << 4) | (x1 >> 4)]
|
| 66 |
+
oa[j + 1] = codepairs[((x1 & 0x0f) << 8) | x2]
|
| 67 |
+
oa[j + 2] = codepairs[(x3 << 4) | (x4 >> 4)]
|
| 68 |
+
oa[j + 3] = codepairs[((x4 & 0x0f) << 8) | x5]
|
| 69 |
+
oa[j + 4] = codepairs[(x6 << 4) | (x7 >> 4)]
|
| 70 |
+
oa[j + 5] = codepairs[((x7 & 0x0f) << 8) | x8]
|
| 71 |
+
oa[j + 6] = codepairs[(x9 << 4) | (x10 >> 4)]
|
| 72 |
+
oa[j + 7] = codepairs[((x10 & 0x0f) << 8) | x11]
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// i < last here is equivalent to i < fullChunksBytes
|
| 76 |
+
for (const last = arr.length - 2; i < last; i += 3, j += 2) {
|
| 77 |
+
const a = arr[i]
|
| 78 |
+
const b = arr[i + 1]
|
| 79 |
+
const c = arr[i + 2]
|
| 80 |
+
oa[j] = codepairs[(a << 4) | (b >> 4)]
|
| 81 |
+
oa[j + 1] = codepairs[((b & 0x0f) << 8) | c]
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
o = decodeAscii(oa)
|
| 85 |
+
} else {
|
| 86 |
+
// This can be optimized by ~25% with templates on Hermes, but this codepath is not called on Hermes, it uses btoa
|
| 87 |
+
// Check git history for templates version
|
| 88 |
+
for (; i < fullChunksBytes; i += 3) {
|
| 89 |
+
const a = arr[i]
|
| 90 |
+
const b = arr[i + 1]
|
| 91 |
+
const c = arr[i + 2]
|
| 92 |
+
o += pairs[(a << 4) | (b >> 4)]
|
| 93 |
+
o += pairs[((b & 0x0f) << 8) | c]
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// If we have something left, process it with a full algo
|
| 98 |
+
let carry = 0
|
| 99 |
+
let shift = 2 // First byte needs to be shifted by 2 to get 6 bits
|
| 100 |
+
const length = arr.length
|
| 101 |
+
for (; i < length; i++) {
|
| 102 |
+
const x = arr[i]
|
| 103 |
+
o += alphabet[carry | (x >> shift)] // shift >= 2, so this fits
|
| 104 |
+
if (shift === 6) {
|
| 105 |
+
shift = 0
|
| 106 |
+
o += alphabet[x & 0x3f]
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
carry = (x << (6 - shift)) & 0x3f
|
| 110 |
+
shift += 2 // Each byte prints 6 bits and leaves 2 bits
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
if (shift !== 2) o += alphabet[carry] // shift 2 means we have no carry left
|
| 114 |
+
if (padding) o += ['', '==', '='][length - fullChunksBytes]
|
| 115 |
+
|
| 116 |
+
return o
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
// TODO: can this be optimized? This only affects non-Hermes barebone engines though
|
| 120 |
+
const mapSize = nativeEncoder ? 128 : 65_536 // we have to store 64 KiB map or recheck everything if we can't decode to byte array
|
| 121 |
+
|
| 122 |
+
export function fromBase64(str, isURL) {
|
| 123 |
+
let inputLength = str.length
|
| 124 |
+
while (str[inputLength - 1] === '=') inputLength--
|
| 125 |
+
const paddingLength = str.length - inputLength
|
| 126 |
+
const tailLength = inputLength % 4
|
| 127 |
+
const mainLength = inputLength - tailLength // multiples of 4
|
| 128 |
+
if (tailLength === 1) throw new SyntaxError(E_LENGTH)
|
| 129 |
+
if (paddingLength > 3 || (paddingLength !== 0 && str.length % 4 !== 0)) {
|
| 130 |
+
throw new SyntaxError(E_PADDING)
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
const alphabet = isURL ? BASE64URL : BASE64
|
| 134 |
+
const helpers = isURL ? BASE64URL_HELPERS : BASE64_HELPERS
|
| 135 |
+
|
| 136 |
+
if (!helpers.fromMap) {
|
| 137 |
+
helpers.fromMap = new Int8Array(mapSize).fill(-1) // no regex input validation here, so we map all other bytes to -1 and recheck sign
|
| 138 |
+
alphabet.forEach((c, i) => (helpers.fromMap[c.charCodeAt(0)] = i))
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
const m = helpers.fromMap
|
| 142 |
+
|
| 143 |
+
const arr = new Uint8Array(Math.floor((inputLength * 3) / 4))
|
| 144 |
+
let at = 0
|
| 145 |
+
let i = 0
|
| 146 |
+
|
| 147 |
+
if (nativeEncoder) {
|
| 148 |
+
const codes = encodeAscii(str, E_CHAR)
|
| 149 |
+
for (; i < mainLength; i += 4) {
|
| 150 |
+
const c0 = codes[i]
|
| 151 |
+
const c1 = codes[i + 1]
|
| 152 |
+
const c2 = codes[i + 2]
|
| 153 |
+
const c3 = codes[i + 3]
|
| 154 |
+
const a = (m[c0] << 18) | (m[c1] << 12) | (m[c2] << 6) | m[c3]
|
| 155 |
+
if (a < 0) throw new SyntaxError(E_CHAR)
|
| 156 |
+
arr[at] = a >> 16
|
| 157 |
+
arr[at + 1] = (a >> 8) & 0xff
|
| 158 |
+
arr[at + 2] = a & 0xff
|
| 159 |
+
at += 3
|
| 160 |
+
}
|
| 161 |
+
} else {
|
| 162 |
+
for (; i < mainLength; i += 4) {
|
| 163 |
+
const c0 = str.charCodeAt(i)
|
| 164 |
+
const c1 = str.charCodeAt(i + 1)
|
| 165 |
+
const c2 = str.charCodeAt(i + 2)
|
| 166 |
+
const c3 = str.charCodeAt(i + 3)
|
| 167 |
+
const a = (m[c0] << 18) | (m[c1] << 12) | (m[c2] << 6) | m[c3]
|
| 168 |
+
if (a < 0) throw new SyntaxError(E_CHAR)
|
| 169 |
+
arr[at] = a >> 16
|
| 170 |
+
arr[at + 1] = (a >> 8) & 0xff
|
| 171 |
+
arr[at + 2] = a & 0xff
|
| 172 |
+
at += 3
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// Can be 0, 2 or 3, verified by padding checks already
|
| 177 |
+
if (tailLength < 2) return arr // 0
|
| 178 |
+
const ab = (m[str.charCodeAt(i++)] << 6) | m[str.charCodeAt(i++)]
|
| 179 |
+
if (ab < 0) throw new SyntaxError(E_CHAR)
|
| 180 |
+
arr[at++] = ab >> 4
|
| 181 |
+
if (tailLength < 3) {
|
| 182 |
+
if (ab & 0xf) throw new SyntaxError(E_LAST)
|
| 183 |
+
return arr // 2
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
const c = m[str.charCodeAt(i++)]
|
| 187 |
+
if (c < 0) throw new SyntaxError(E_CHAR)
|
| 188 |
+
arr[at++] = ((ab << 4) & 0xff) | (c >> 2)
|
| 189 |
+
if (c & 0x3) throw new SyntaxError(E_LAST)
|
| 190 |
+
return arr // 3
|
| 191 |
+
}
|
node_modules/@exodus/bytes/fallback/encoding.api.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// TODO: make this more strict against Symbol.toStringTag
|
| 2 |
+
// Is not very significant though, anything faking Symbol.toStringTag could as well override
|
| 3 |
+
// prototypes, which is not something we protect against
|
| 4 |
+
|
| 5 |
+
function isAnyArrayBuffer(x) {
|
| 6 |
+
if (x instanceof ArrayBuffer) return true
|
| 7 |
+
if (globalThis.SharedArrayBuffer && x instanceof SharedArrayBuffer) return true
|
| 8 |
+
if (!x || typeof x.byteLength !== 'number') return false
|
| 9 |
+
const s = Object.prototype.toString.call(x)
|
| 10 |
+
return s === '[object ArrayBuffer]' || s === '[object SharedArrayBuffer]'
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
export function fromSource(x) {
|
| 14 |
+
if (x instanceof Uint8Array) return x
|
| 15 |
+
if (ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength)
|
| 16 |
+
if (isAnyArrayBuffer(x)) {
|
| 17 |
+
if ('detached' in x) return x.detached === true ? new Uint8Array() : new Uint8Array(x)
|
| 18 |
+
// Old engines without .detached, try-catch
|
| 19 |
+
try {
|
| 20 |
+
return new Uint8Array(x)
|
| 21 |
+
} catch {
|
| 22 |
+
return new Uint8Array()
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
throw new TypeError('Argument must be a SharedArrayBuffer, ArrayBuffer or ArrayBufferView')
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// Warning: unlike whatwg-encoding, returns lowercased labels
|
| 30 |
+
// Those are case-insensitive and that's how TextDecoder encoding getter normalizes them
|
| 31 |
+
export function getBOMEncoding(input) {
|
| 32 |
+
const u8 = fromSource(input) // asserts
|
| 33 |
+
if (u8.length >= 3 && u8[0] === 0xef && u8[1] === 0xbb && u8[2] === 0xbf) return 'utf-8'
|
| 34 |
+
if (u8.length < 2) return null
|
| 35 |
+
if (u8[0] === 0xff && u8[1] === 0xfe) return 'utf-16le'
|
| 36 |
+
if (u8[0] === 0xfe && u8[1] === 0xff) return 'utf-16be'
|
| 37 |
+
return null
|
| 38 |
+
}
|
node_modules/@exodus/bytes/fallback/encoding.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// We can't return native TextDecoder if it's present, as Node.js one is broken on windows-1252 and we fix that
|
| 2 |
+
// We are also faster than Node.js built-in on both TextEncoder and TextDecoder
|
| 3 |
+
|
| 4 |
+
import { utf16toString, utf16toStringLoose } from '@exodus/bytes/utf16.js'
|
| 5 |
+
import { utf8fromStringLoose, utf8toString, utf8toStringLoose } from '@exodus/bytes/utf8.js'
|
| 6 |
+
import {
|
| 7 |
+
createSinglebyteDecoder,
|
| 8 |
+
latin1toString,
|
| 9 |
+
latin1fromString,
|
| 10 |
+
} from '@exodus/bytes/single-byte.js'
|
| 11 |
+
import labels from './encoding.labels.js'
|
| 12 |
+
import { fromSource, getBOMEncoding } from './encoding.api.js'
|
| 13 |
+
import { unfinishedBytes, mergePrefix } from './encoding.util.js'
|
| 14 |
+
|
| 15 |
+
export { getBOMEncoding } from './encoding.api.js'
|
| 16 |
+
|
| 17 |
+
export const E_ENCODING = 'Unknown encoding'
|
| 18 |
+
const E_MULTI = "import '@exodus/bytes/encoding.js' for legacy multi-byte encodings support"
|
| 19 |
+
const E_OPTIONS = 'The "options" argument must be of type object'
|
| 20 |
+
const replacementChar = '\uFFFD'
|
| 21 |
+
const multibyteSet = new Set(['big5', 'euc-kr', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'gbk', 'gb18030']) // prettier-ignore
|
| 22 |
+
let createMultibyteDecoder, multibyteEncoder
|
| 23 |
+
|
| 24 |
+
let labelsMap
|
| 25 |
+
// Warning: unlike whatwg-encoding, returns lowercased labels
|
| 26 |
+
// Those are case-insensitive and that's how TextDecoder encoding getter normalizes them
|
| 27 |
+
// https://encoding.spec.whatwg.org/#names-and-labels
|
| 28 |
+
export function normalizeEncoding(label) {
|
| 29 |
+
// fast path
|
| 30 |
+
if (label === 'utf-8' || label === 'utf8' || label === 'UTF-8' || label === 'UTF8') return 'utf-8'
|
| 31 |
+
if (label === 'windows-1252' || label === 'ascii' || label === 'latin1') return 'windows-1252'
|
| 32 |
+
// full map
|
| 33 |
+
if (/[^\w\t\n\f\r .:-]/i.test(label)) return null // must be ASCII (with ASCII whitespace)
|
| 34 |
+
const low = `${label}`.trim().toLowerCase()
|
| 35 |
+
if (Object.hasOwn(labels, low)) return low
|
| 36 |
+
if (!labelsMap) {
|
| 37 |
+
labelsMap = new Map()
|
| 38 |
+
for (const [name, aliases] of Object.entries(labels)) {
|
| 39 |
+
for (const alias of aliases) labelsMap.set(alias, name)
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
const mapped = labelsMap.get(low)
|
| 44 |
+
if (mapped) return mapped
|
| 45 |
+
return null
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
const uppercasePrefixes = new Set(['utf', 'iso', 'koi', 'euc', 'ibm', 'gbk'])
|
| 49 |
+
|
| 50 |
+
// Unlike normalizeEncoding, case-sensitive
|
| 51 |
+
// https://encoding.spec.whatwg.org/#names-and-labels
|
| 52 |
+
export function labelToName(label) {
|
| 53 |
+
const enc = normalizeEncoding(label)
|
| 54 |
+
if (enc === 'utf-8') return 'UTF-8' // fast path
|
| 55 |
+
if (!enc) return enc
|
| 56 |
+
if (uppercasePrefixes.has(enc.slice(0, 3))) return enc.toUpperCase()
|
| 57 |
+
if (enc === 'big5') return 'Big5'
|
| 58 |
+
if (enc === 'shift_jis') return 'Shift_JIS'
|
| 59 |
+
return enc
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
export const isMultibyte = (enc) => multibyteSet.has(enc)
|
| 63 |
+
export function setMultibyte(createDecoder, createEncoder) {
|
| 64 |
+
createMultibyteDecoder = createDecoder
|
| 65 |
+
multibyteEncoder = createEncoder
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
export function getMultibyteEncoder() {
|
| 69 |
+
if (!multibyteEncoder) throw new Error(E_MULTI)
|
| 70 |
+
return multibyteEncoder
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
const define = (obj, key, value) => Object.defineProperty(obj, key, { value, writable: false })
|
| 74 |
+
|
| 75 |
+
function isAnyUint8Array(x) {
|
| 76 |
+
if (x instanceof Uint8Array) return true
|
| 77 |
+
if (!x || !ArrayBuffer.isView(x) || x.BYTES_PER_ELEMENT !== 1) return false
|
| 78 |
+
return Object.prototype.toString.call(x) === '[object Uint8Array]'
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
function unicodeDecoder(encoding, loose) {
|
| 82 |
+
if (encoding === 'utf-8') return loose ? utf8toStringLoose : utf8toString // likely
|
| 83 |
+
const form = encoding === 'utf-16le' ? 'uint8-le' : 'uint8-be'
|
| 84 |
+
return loose ? (u) => utf16toStringLoose(u, form) : (u) => utf16toString(u, form)
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
export class TextDecoder {
|
| 88 |
+
#decode
|
| 89 |
+
#unicode
|
| 90 |
+
#multibyte
|
| 91 |
+
#chunk
|
| 92 |
+
#canBOM
|
| 93 |
+
|
| 94 |
+
constructor(encoding = 'utf-8', options = {}) {
|
| 95 |
+
if (typeof options !== 'object') throw new TypeError(E_OPTIONS)
|
| 96 |
+
const enc = normalizeEncoding(encoding)
|
| 97 |
+
if (!enc || enc === 'replacement') throw new RangeError(E_ENCODING)
|
| 98 |
+
define(this, 'encoding', enc)
|
| 99 |
+
define(this, 'fatal', !!options.fatal)
|
| 100 |
+
define(this, 'ignoreBOM', !!options.ignoreBOM)
|
| 101 |
+
this.#unicode = enc === 'utf-8' || enc === 'utf-16le' || enc === 'utf-16be'
|
| 102 |
+
this.#multibyte = !this.#unicode && isMultibyte(enc)
|
| 103 |
+
this.#canBOM = this.#unicode && !this.ignoreBOM
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
get [Symbol.toStringTag]() {
|
| 107 |
+
return 'TextDecoder'
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
decode(input, options = {}) {
|
| 111 |
+
if (typeof options !== 'object') throw new TypeError(E_OPTIONS)
|
| 112 |
+
const stream = !!options.stream
|
| 113 |
+
let u = input === undefined ? new Uint8Array() : fromSource(input)
|
| 114 |
+
const empty = u.length === 0 // also can't be streaming after next line
|
| 115 |
+
if (empty && stream) return '' // no state change
|
| 116 |
+
|
| 117 |
+
if (this.#unicode) {
|
| 118 |
+
let prefix
|
| 119 |
+
if (this.#chunk) {
|
| 120 |
+
const merged = mergePrefix(u, this.#chunk, this.encoding)
|
| 121 |
+
if (u.length < 3) {
|
| 122 |
+
u = merged // might be unfinished, but fully consumed old u
|
| 123 |
+
} else {
|
| 124 |
+
prefix = merged // stops at complete chunk
|
| 125 |
+
const add = prefix.length - this.#chunk.length
|
| 126 |
+
if (add > 0) u = u.subarray(add)
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
this.#chunk = null
|
| 130 |
+
} else if (empty) {
|
| 131 |
+
this.#canBOM = !this.ignoreBOM // not streaming
|
| 132 |
+
return ''
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// For non-stream utf-8 we don't have to do this as it matches utf8toStringLoose already
|
| 136 |
+
// For non-stream loose utf-16 we still have to do this as this API supports uneven byteLength unlike utf16toStringLoose
|
| 137 |
+
let suffix = ''
|
| 138 |
+
if (stream || (!this.fatal && this.encoding !== 'utf-8')) {
|
| 139 |
+
const trail = unfinishedBytes(u, u.byteLength, this.encoding)
|
| 140 |
+
if (trail > 0) {
|
| 141 |
+
if (stream) {
|
| 142 |
+
this.#chunk = Uint8Array.from(u.subarray(-trail)) // copy
|
| 143 |
+
} else {
|
| 144 |
+
// non-fatal mode as already checked
|
| 145 |
+
suffix = replacementChar
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
u = u.subarray(0, -trail)
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
let seenBOM = false
|
| 153 |
+
if (this.#canBOM) {
|
| 154 |
+
const bom = this.#findBom(prefix ?? u)
|
| 155 |
+
if (bom) {
|
| 156 |
+
seenBOM = true
|
| 157 |
+
if (prefix) {
|
| 158 |
+
prefix = prefix.subarray(bom)
|
| 159 |
+
} else {
|
| 160 |
+
u = u.subarray(bom)
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
} else if (!stream && !this.ignoreBOM) {
|
| 164 |
+
this.#canBOM = true
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
if (!this.#decode) this.#decode = unicodeDecoder(this.encoding, !this.fatal)
|
| 168 |
+
try {
|
| 169 |
+
const res = (prefix ? this.#decode(prefix) : '') + this.#decode(u) + suffix
|
| 170 |
+
// "BOM seen" is set on the current decode call only if it did not error, in "serialize I/O queue" after decoding
|
| 171 |
+
if (stream && (seenBOM || res.length > 0)) this.#canBOM = false
|
| 172 |
+
return res
|
| 173 |
+
} catch (err) {
|
| 174 |
+
this.#chunk = null // reset unfinished chunk on errors
|
| 175 |
+
// The correct way per spec seems to be not destroying the decoder state (aka BOM here) in stream mode
|
| 176 |
+
// See also multi-byte.js
|
| 177 |
+
throw err
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
// eslint-disable-next-line no-else-return
|
| 181 |
+
} else if (this.#multibyte) {
|
| 182 |
+
if (!createMultibyteDecoder) throw new Error(E_MULTI)
|
| 183 |
+
if (!this.#decode) this.#decode = createMultibyteDecoder(this.encoding, !this.fatal) // can contain state!
|
| 184 |
+
return this.#decode(u, stream)
|
| 185 |
+
} else {
|
| 186 |
+
if (!this.#decode) this.#decode = createSinglebyteDecoder(this.encoding, !this.fatal)
|
| 187 |
+
return this.#decode(u)
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
#findBom(u) {
|
| 192 |
+
switch (this.encoding) {
|
| 193 |
+
case 'utf-8':
|
| 194 |
+
return u.byteLength >= 3 && u[0] === 0xef && u[1] === 0xbb && u[2] === 0xbf ? 3 : 0
|
| 195 |
+
case 'utf-16le':
|
| 196 |
+
return u.byteLength >= 2 && u[0] === 0xff && u[1] === 0xfe ? 2 : 0
|
| 197 |
+
case 'utf-16be':
|
| 198 |
+
return u.byteLength >= 2 && u[0] === 0xfe && u[1] === 0xff ? 2 : 0
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/* c8 ignore next */
|
| 202 |
+
throw new Error('Unreachable')
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
export class TextEncoder {
|
| 207 |
+
constructor() {
|
| 208 |
+
define(this, 'encoding', 'utf-8')
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
get [Symbol.toStringTag]() {
|
| 212 |
+
return 'TextEncoder'
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
encode(str = '') {
|
| 216 |
+
if (typeof str !== 'string') str = `${str}`
|
| 217 |
+
return utf8fromStringLoose(str) // non-pooled
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
encodeInto(str, target) {
|
| 221 |
+
if (typeof str !== 'string') str = `${str}`
|
| 222 |
+
if (!isAnyUint8Array(target)) throw new TypeError('Target must be an Uint8Array')
|
| 223 |
+
if (target.buffer.detached) return { read: 0, written: 0 } // Until https://github.com/whatwg/encoding/issues/324 is resolved
|
| 224 |
+
|
| 225 |
+
const tlen = target.length
|
| 226 |
+
if (tlen < str.length) str = str.slice(0, tlen)
|
| 227 |
+
let u8 = utf8fromStringLoose(str)
|
| 228 |
+
let read
|
| 229 |
+
if (tlen >= u8.length) {
|
| 230 |
+
read = str.length
|
| 231 |
+
} else if (u8.length === str.length) {
|
| 232 |
+
if (u8.length > tlen) u8 = u8.subarray(0, tlen) // ascii can be truncated
|
| 233 |
+
read = u8.length
|
| 234 |
+
} else {
|
| 235 |
+
u8 = u8.subarray(0, tlen)
|
| 236 |
+
const unfinished = unfinishedBytes(u8, u8.length, 'utf-8')
|
| 237 |
+
if (unfinished > 0) u8 = u8.subarray(0, u8.length - unfinished)
|
| 238 |
+
|
| 239 |
+
// We can do this because loose str -> u8 -> str preserves length, unlike loose u8 -> str -> u8
|
| 240 |
+
// Each unpaired surrogate (1 charcode) is replaced with a single charcode
|
| 241 |
+
read = utf8toStringLoose(u8).length // FIXME: Converting back is very inefficient
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
try {
|
| 245 |
+
target.set(u8)
|
| 246 |
+
} catch {
|
| 247 |
+
return { read: 0, written: 0 } // see above, likely detached but no .detached property support
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
return { read, written: u8.length }
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
const E_NO_STREAMS = 'TransformStream global not present in the environment'
|
| 255 |
+
|
| 256 |
+
// https://encoding.spec.whatwg.org/#interface-textdecoderstream
|
| 257 |
+
export class TextDecoderStream {
|
| 258 |
+
constructor(encoding = 'utf-8', options = {}) {
|
| 259 |
+
if (!globalThis.TransformStream) throw new Error(E_NO_STREAMS)
|
| 260 |
+
const decoder = new TextDecoder(encoding, options)
|
| 261 |
+
const transform = new TransformStream({
|
| 262 |
+
transform: (chunk, controller) => {
|
| 263 |
+
const value = decoder.decode(fromSource(chunk), { stream: true })
|
| 264 |
+
if (value) controller.enqueue(value)
|
| 265 |
+
},
|
| 266 |
+
flush: (controller) => {
|
| 267 |
+
// https://streams.spec.whatwg.org/#dom-transformer-flush
|
| 268 |
+
const value = decoder.decode()
|
| 269 |
+
if (value) controller.enqueue(value)
|
| 270 |
+
// No need to call .terminate() (Node.js is wrong)
|
| 271 |
+
},
|
| 272 |
+
})
|
| 273 |
+
|
| 274 |
+
define(this, 'encoding', decoder.encoding)
|
| 275 |
+
define(this, 'fatal', decoder.fatal)
|
| 276 |
+
define(this, 'ignoreBOM', decoder.ignoreBOM)
|
| 277 |
+
define(this, 'readable', transform.readable)
|
| 278 |
+
define(this, 'writable', transform.writable)
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
get [Symbol.toStringTag]() {
|
| 282 |
+
return 'TextDecoderStream'
|
| 283 |
+
}
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
// https://encoding.spec.whatwg.org/#interface-textencoderstream
|
| 287 |
+
// Only UTF-8 per spec
|
| 288 |
+
export class TextEncoderStream {
|
| 289 |
+
constructor() {
|
| 290 |
+
if (!globalThis.TransformStream) throw new Error(E_NO_STREAMS)
|
| 291 |
+
let lead
|
| 292 |
+
const transform = new TransformStream({
|
| 293 |
+
// https://encoding.spec.whatwg.org/#encode-and-enqueue-a-chunk
|
| 294 |
+
// Not identical in code, but reuses loose mode to have identical behavior
|
| 295 |
+
transform: (chunk, controller) => {
|
| 296 |
+
let s = String(chunk) // DOMString, might contain unpaired surrogates
|
| 297 |
+
if (s.length === 0) return
|
| 298 |
+
if (lead) {
|
| 299 |
+
s = lead + s
|
| 300 |
+
lead = null
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
const last = s.charCodeAt(s.length - 1) // Can't come from previous lead due to length check
|
| 304 |
+
if ((last & 0xfc_00) === 0xd8_00) {
|
| 305 |
+
lead = s[s.length - 1]
|
| 306 |
+
s = s.slice(0, -1)
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
if (s) controller.enqueue(utf8fromStringLoose(s))
|
| 310 |
+
},
|
| 311 |
+
// https://encoding.spec.whatwg.org/#encode-and-flush
|
| 312 |
+
flush: (controller) => {
|
| 313 |
+
if (lead) controller.enqueue(Uint8Array.of(0xef, 0xbf, 0xbd))
|
| 314 |
+
},
|
| 315 |
+
})
|
| 316 |
+
|
| 317 |
+
define(this, 'encoding', 'utf-8')
|
| 318 |
+
define(this, 'readable', transform.readable)
|
| 319 |
+
define(this, 'writable', transform.writable)
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
get [Symbol.toStringTag]() {
|
| 323 |
+
return 'TextEncoderStream'
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// https://encoding.spec.whatwg.org/#decode
|
| 328 |
+
// Warning: encoding sniffed from BOM takes preference over the supplied one
|
| 329 |
+
// Warning: lossy, performs replacement, no option of throwing
|
| 330 |
+
// Completely ignores encoding and even skips validation when BOM is found
|
| 331 |
+
// Unlike TextDecoder public API, additionally supports 'replacement' encoding
|
| 332 |
+
export function legacyHookDecode(input, fallbackEncoding = 'utf-8') {
|
| 333 |
+
let u8 = fromSource(input)
|
| 334 |
+
const bomEncoding = getBOMEncoding(u8)
|
| 335 |
+
if (bomEncoding) u8 = u8.subarray(bomEncoding === 'utf-8' ? 3 : 2)
|
| 336 |
+
const enc = bomEncoding ?? normalizeEncoding(fallbackEncoding) // "the byte order mark is more authoritative than anything else"
|
| 337 |
+
|
| 338 |
+
if (enc === 'utf-8') return utf8toStringLoose(u8)
|
| 339 |
+
if (enc === 'utf-16le' || enc === 'utf-16be') {
|
| 340 |
+
let suffix = ''
|
| 341 |
+
if (u8.byteLength % 2 !== 0) {
|
| 342 |
+
suffix = replacementChar
|
| 343 |
+
u8 = u8.subarray(0, -unfinishedBytes(u8, u8.byteLength, enc))
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
return utf16toStringLoose(u8, enc === 'utf-16le' ? 'uint8-le' : 'uint8-be') + suffix
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
if (!Object.hasOwn(labels, enc)) throw new RangeError(E_ENCODING)
|
| 350 |
+
|
| 351 |
+
if (isMultibyte(enc)) {
|
| 352 |
+
if (!createMultibyteDecoder) throw new Error(E_MULTI)
|
| 353 |
+
return createMultibyteDecoder(enc, true)(u8)
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
// https://encoding.spec.whatwg.org/#replacement-decoder
|
| 357 |
+
// On non-streaming non-fatal case, it just replaces any non-empty input with a single replacement char
|
| 358 |
+
if (enc === 'replacement') return input.byteLength > 0 ? replacementChar : ''
|
| 359 |
+
|
| 360 |
+
return createSinglebyteDecoder(enc, true)(u8)
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
export function isomorphicDecode(input) {
|
| 364 |
+
return latin1toString(fromSource(input))
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
export function isomorphicEncode(str) {
|
| 368 |
+
return latin1fromString(str)
|
| 369 |
+
}
|
node_modules/@exodus/bytes/fallback/encoding.labels.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// See https://encoding.spec.whatwg.org/#names-and-labels
|
| 2 |
+
|
| 3 |
+
/* eslint-disable @exodus/export-default/named */
|
| 4 |
+
// prettier-ignore
|
| 5 |
+
const labels = {
|
| 6 |
+
'utf-8': ['unicode-1-1-utf-8', 'unicode11utf8', 'unicode20utf8', 'utf8', 'x-unicode20utf8'],
|
| 7 |
+
'utf-16be': ['unicodefffe'],
|
| 8 |
+
'utf-16le': ['csunicode', 'iso-10646-ucs-2', 'ucs-2', 'unicode', 'unicodefeff', 'utf-16'],
|
| 9 |
+
'iso-8859-2': ['iso-ir-101'],
|
| 10 |
+
'iso-8859-3': ['iso-ir-109'],
|
| 11 |
+
'iso-8859-4': ['iso-ir-110'],
|
| 12 |
+
'iso-8859-5': ['csisolatincyrillic', 'cyrillic', 'iso-ir-144'],
|
| 13 |
+
'iso-8859-6': ['arabic', 'asmo-708', 'csiso88596e', 'csiso88596i', 'csisolatinarabic', 'ecma-114', 'iso-8859-6-e', 'iso-8859-6-i', 'iso-ir-127'],
|
| 14 |
+
'iso-8859-7': ['csisolatingreek', 'ecma-118', 'elot_928', 'greek', 'greek8', 'iso-ir-126', 'sun_eu_greek'],
|
| 15 |
+
'iso-8859-8': ['csiso88598e', 'csisolatinhebrew', 'hebrew', 'iso-8859-8-e', 'iso-ir-138', 'visual'],
|
| 16 |
+
'iso-8859-8-i': ['csiso88598i', 'logical'],
|
| 17 |
+
'iso-8859-16': [],
|
| 18 |
+
'koi8-r': ['cskoi8r', 'koi', 'koi8', 'koi8_r'],
|
| 19 |
+
'koi8-u': ['koi8-ru'],
|
| 20 |
+
'windows-874': ['dos-874', 'iso-8859-11', 'iso8859-11', 'iso885911', 'tis-620'],
|
| 21 |
+
ibm866: ['866', 'cp866', 'csibm866'],
|
| 22 |
+
'x-mac-cyrillic': ['x-mac-ukrainian'],
|
| 23 |
+
macintosh: ['csmacintosh', 'mac', 'x-mac-roman'],
|
| 24 |
+
gbk: ['chinese', 'csgb2312', 'csiso58gb231280', 'gb2312', 'gb_2312', 'gb_2312-80', 'iso-ir-58', 'x-gbk'],
|
| 25 |
+
gb18030: [],
|
| 26 |
+
big5: ['big5-hkscs', 'cn-big5', 'csbig5', 'x-x-big5'],
|
| 27 |
+
'euc-jp': ['cseucpkdfmtjapanese', 'x-euc-jp'],
|
| 28 |
+
shift_jis: ['csshiftjis', 'ms932', 'ms_kanji', 'shift-jis', 'sjis', 'windows-31j', 'x-sjis'],
|
| 29 |
+
'euc-kr': ['cseuckr', 'csksc56011987', 'iso-ir-149', 'korean', 'ks_c_5601-1987', 'ks_c_5601-1989', 'ksc5601', 'ksc_5601', 'windows-949'],
|
| 30 |
+
'iso-2022-jp': ['csiso2022jp'],
|
| 31 |
+
replacement: ['csiso2022kr', 'hz-gb-2312', 'iso-2022-cn', 'iso-2022-cn-ext', 'iso-2022-kr'],
|
| 32 |
+
'x-user-defined': [],
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
for (const i of [10, 13, 14, 15]) labels[`iso-8859-${i}`] = [`iso8859-${i}`, `iso8859${i}`]
|
| 36 |
+
for (const i of [2, 6, 7]) labels[`iso-8859-${i}`].push(`iso_8859-${i}:1987`)
|
| 37 |
+
for (const i of [3, 4, 5, 8]) labels[`iso-8859-${i}`].push(`iso_8859-${i}:1988`)
|
| 38 |
+
// prettier-ignore
|
| 39 |
+
for (let i = 2; i < 9; i++) labels[`iso-8859-${i}`].push(`iso8859-${i}`, `iso8859${i}`, `iso_8859-${i}`)
|
| 40 |
+
for (let i = 2; i < 5; i++) labels[`iso-8859-${i}`].push(`csisolatin${i}`, `l${i}`, `latin${i}`)
|
| 41 |
+
for (let i = 0; i < 9; i++) labels[`windows-125${i}`] = [`cp125${i}`, `x-cp125${i}`]
|
| 42 |
+
|
| 43 |
+
// prettier-ignore
|
| 44 |
+
labels['windows-1252'].push('ansi_x3.4-1968', 'ascii', 'cp819', 'csisolatin1', 'ibm819', 'iso-8859-1', 'iso-ir-100', 'iso8859-1', 'iso88591', 'iso_8859-1', 'iso_8859-1:1987', 'l1', 'latin1', 'us-ascii')
|
| 45 |
+
// prettier-ignore
|
| 46 |
+
labels['windows-1254'].push('csisolatin5', 'iso-8859-9', 'iso-ir-148', 'iso8859-9', 'iso88599', 'iso_8859-9', 'iso_8859-9:1989', 'l5', 'latin5')
|
| 47 |
+
labels['iso-8859-10'].push('csisolatin6', 'iso-ir-157', 'l6', 'latin6')
|
| 48 |
+
labels['iso-8859-15'].push('csisolatin9', 'iso_8859-15', 'l9')
|
| 49 |
+
|
| 50 |
+
export default labels
|
node_modules/@exodus/bytes/fallback/encoding.util.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Get a number of last bytes in an Uint8Array `u` ending at `len` that don't
|
| 2 |
+
// form a codepoint yet, but can be a part of a single codepoint on more data
|
| 3 |
+
export function unfinishedBytes(u, len, enc) {
|
| 4 |
+
switch (enc) {
|
| 5 |
+
case 'utf-8': {
|
| 6 |
+
// 0-3
|
| 7 |
+
let p = 0
|
| 8 |
+
while (p < 2 && p < len && (u[len - p - 1] & 0xc0) === 0x80) p++ // go back 0-2 trailing bytes
|
| 9 |
+
if (p === len) return 0 // no space for lead
|
| 10 |
+
const l = u[len - p - 1]
|
| 11 |
+
if (l < 0xc2 || l > 0xf4) return 0 // not a lead
|
| 12 |
+
if (p === 0) return 1 // nothing to recheck, we have only lead, return it. 2-byte must return here
|
| 13 |
+
if (l < 0xe0 || (l < 0xf0 && p >= 2)) return 0 // 2-byte, or 3-byte or less and we already have 2 trailing
|
| 14 |
+
const lower = l === 0xf0 ? 0x90 : l === 0xe0 ? 0xa0 : 0x80
|
| 15 |
+
const upper = l === 0xf4 ? 0x8f : l === 0xed ? 0x9f : 0xbf
|
| 16 |
+
const n = u[len - p]
|
| 17 |
+
return n >= lower && n <= upper ? p + 1 : 0
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
case 'utf-16le':
|
| 21 |
+
case 'utf-16be': {
|
| 22 |
+
// 0-3
|
| 23 |
+
const p = len % 2 // uneven byte length adds 1
|
| 24 |
+
if (len < 2) return p
|
| 25 |
+
const l = len - p - 1
|
| 26 |
+
const last = enc === 'utf-16le' ? (u[l] << 8) ^ u[l - 1] : (u[l - 1] << 8) ^ u[l]
|
| 27 |
+
return last >= 0xd8_00 && last < 0xdc_00 ? p + 2 : p // lone lead adds 2
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
throw new Error('Unsupported encoding')
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// Merge prefix `chunk` with `u` and return new combined prefix
|
| 35 |
+
// For u.length < 3, fully consumes u and can return unfinished data,
|
| 36 |
+
// otherwise returns a prefix with no unfinished bytes
|
| 37 |
+
export function mergePrefix(u, chunk, enc) {
|
| 38 |
+
if (u.length === 0) return chunk
|
| 39 |
+
const cl = chunk.length
|
| 40 |
+
if (u.length < 3) {
|
| 41 |
+
// No reason to bruteforce offsets, also it's possible this doesn't yet end the sequence
|
| 42 |
+
const a = new Uint8Array(cl + u.length)
|
| 43 |
+
a.set(chunk)
|
| 44 |
+
a.set(u, cl)
|
| 45 |
+
return a
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Slice off a small portion of u into prefix chunk so we can decode them separately without extending array size
|
| 49 |
+
const t = new Uint8Array(cl + 3) // We have 1-3 bytes and need 1-3 more bytes
|
| 50 |
+
t.set(chunk)
|
| 51 |
+
t.set(u.subarray(0, 3), cl)
|
| 52 |
+
|
| 53 |
+
// Stop at the first offset where unfinished bytes reaches 0 or fits into u
|
| 54 |
+
// If that doesn't happen (u too short), just concat chunk and u completely (above)
|
| 55 |
+
for (let i = 1; i <= 3; i++) {
|
| 56 |
+
const unfinished = unfinishedBytes(t, cl + i, enc) // 0-3
|
| 57 |
+
if (unfinished <= i) {
|
| 58 |
+
// Always reachable at 3, but we still need 'unfinished' value for it
|
| 59 |
+
const add = i - unfinished // 0-3
|
| 60 |
+
return add > 0 ? t.subarray(0, cl + add) : chunk
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// Unreachable
|
| 65 |
+
}
|
node_modules/@exodus/bytes/fallback/hex.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { E_STRING } from './_utils.js'
|
| 2 |
+
import { nativeDecoder, nativeEncoder, decode2string } from './platform.js'
|
| 3 |
+
import { encodeAscii, decodeAscii } from './latin1.js'
|
| 4 |
+
|
| 5 |
+
let hexArray // array of 256 bytes converted to two-char hex strings
|
| 6 |
+
let hexCodes // hexArray converted to u16 code pairs
|
| 7 |
+
let dehexArray
|
| 8 |
+
const _00 = 0x30_30 // '00' string in hex, the only allowed char pair to generate 0 byte
|
| 9 |
+
const _ff = 0x66_66 // 'ff' string in hex, max allowed char pair (larger than 'FF' string)
|
| 10 |
+
const allowed = '0123456789ABCDEFabcdef'
|
| 11 |
+
|
| 12 |
+
export const E_HEX = 'Input is not a hex string'
|
| 13 |
+
|
| 14 |
+
// Expects a checked Uint8Array
|
| 15 |
+
export function toHex(arr) {
|
| 16 |
+
if (!hexArray) hexArray = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'))
|
| 17 |
+
const length = arr.length // this helps Hermes
|
| 18 |
+
|
| 19 |
+
// Only old browsers use this, barebone engines don't have TextDecoder
|
| 20 |
+
// But Hermes can use this when it (hopefully) implements TextDecoder
|
| 21 |
+
if (nativeDecoder) {
|
| 22 |
+
if (!hexCodes) {
|
| 23 |
+
hexCodes = new Uint16Array(256)
|
| 24 |
+
const u8 = new Uint8Array(hexCodes.buffer, hexCodes.byteOffset, hexCodes.byteLength)
|
| 25 |
+
for (let i = 0; i < 256; i++) {
|
| 26 |
+
const pair = hexArray[i]
|
| 27 |
+
u8[2 * i] = pair.charCodeAt(0)
|
| 28 |
+
u8[2 * i + 1] = pair.charCodeAt(1)
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
const oa = new Uint16Array(length)
|
| 33 |
+
let i = 0
|
| 34 |
+
for (const last3 = arr.length - 3; ; i += 4) {
|
| 35 |
+
if (i >= last3) break // loop is fast enough for moving this here to be useful on JSC
|
| 36 |
+
const x0 = arr[i]
|
| 37 |
+
const x1 = arr[i + 1]
|
| 38 |
+
const x2 = arr[i + 2]
|
| 39 |
+
const x3 = arr[i + 3]
|
| 40 |
+
oa[i] = hexCodes[x0]
|
| 41 |
+
oa[i + 1] = hexCodes[x1]
|
| 42 |
+
oa[i + 2] = hexCodes[x2]
|
| 43 |
+
oa[i + 3] = hexCodes[x3]
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
for (; i < length; i++) oa[i] = hexCodes[arr[i]]
|
| 47 |
+
return decodeAscii(oa)
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
return decode2string(arr, 0, length, hexArray)
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
export function fromHex(str) {
|
| 54 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 55 |
+
if (str.length % 2 !== 0) throw new SyntaxError(E_HEX)
|
| 56 |
+
|
| 57 |
+
const length = str.length / 2 // this helps Hermes in loops
|
| 58 |
+
const arr = new Uint8Array(length)
|
| 59 |
+
|
| 60 |
+
// Native encoder path is beneficial even for small arrays in Hermes
|
| 61 |
+
if (nativeEncoder) {
|
| 62 |
+
if (!dehexArray) {
|
| 63 |
+
dehexArray = new Uint8Array(_ff + 1) // 26 KiB cache, >2x perf improvement on Hermes
|
| 64 |
+
const u8 = new Uint8Array(2)
|
| 65 |
+
const u16 = new Uint16Array(u8.buffer, u8.byteOffset, 1) // for endianess-agnostic transform
|
| 66 |
+
const map = [...allowed].map((c) => [c.charCodeAt(0), parseInt(c, 16)])
|
| 67 |
+
for (const [ch, vh] of map) {
|
| 68 |
+
u8[0] = ch // first we read high hex char
|
| 69 |
+
for (const [cl, vl] of map) {
|
| 70 |
+
u8[1] = cl // then we read low hex char
|
| 71 |
+
dehexArray[u16[0]] = (vh << 4) | vl
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
const codes = encodeAscii(str, E_HEX) // aligned
|
| 77 |
+
const codes16 = new Uint16Array(codes.buffer, codes.byteOffset, codes.byteLength / 2)
|
| 78 |
+
let i = 0
|
| 79 |
+
for (const last3 = length - 3; i < last3; i += 4) {
|
| 80 |
+
const ai = codes16[i]
|
| 81 |
+
const bi = codes16[i + 1]
|
| 82 |
+
const ci = codes16[i + 2]
|
| 83 |
+
const di = codes16[i + 3]
|
| 84 |
+
const a = dehexArray[ai]
|
| 85 |
+
const b = dehexArray[bi]
|
| 86 |
+
const c = dehexArray[ci]
|
| 87 |
+
const d = dehexArray[di]
|
| 88 |
+
if ((!a && ai !== _00) || (!b && bi !== _00) || (!c && ci !== _00) || (!d && di !== _00)) {
|
| 89 |
+
throw new SyntaxError(E_HEX)
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
arr[i] = a
|
| 93 |
+
arr[i + 1] = b
|
| 94 |
+
arr[i + 2] = c
|
| 95 |
+
arr[i + 3] = d
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
while (i < length) {
|
| 99 |
+
const ai = codes16[i]
|
| 100 |
+
const a = dehexArray[ai]
|
| 101 |
+
if (!a && ai !== _00) throw new SyntaxError(E_HEX)
|
| 102 |
+
arr[i++] = a
|
| 103 |
+
}
|
| 104 |
+
} else {
|
| 105 |
+
if (!dehexArray) {
|
| 106 |
+
// no regex input validation here, so we map all other bytes to -1 and recheck sign
|
| 107 |
+
// non-ASCII chars throw already though, so we should process only 0-127
|
| 108 |
+
dehexArray = new Int8Array(128).fill(-1)
|
| 109 |
+
for (let i = 0; i < 16; i++) {
|
| 110 |
+
const s = i.toString(16)
|
| 111 |
+
dehexArray[s.charCodeAt(0)] = dehexArray[s.toUpperCase().charCodeAt(0)] = i
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
let j = 0
|
| 116 |
+
for (let i = 0; i < length; i++) {
|
| 117 |
+
const a = str.charCodeAt(j++)
|
| 118 |
+
const b = str.charCodeAt(j++)
|
| 119 |
+
const res = (dehexArray[a] << 4) | dehexArray[b]
|
| 120 |
+
if (res < 0 || (0x7f | a | b) !== 0x7f) throw new SyntaxError(E_HEX) // 0-127
|
| 121 |
+
arr[i] = res
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
return arr
|
| 126 |
+
}
|
node_modules/@exodus/bytes/fallback/latin1.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
nativeEncoder,
|
| 3 |
+
nativeDecoder,
|
| 4 |
+
nativeDecoderLatin1,
|
| 5 |
+
nativeBuffer,
|
| 6 |
+
encodeCharcodes,
|
| 7 |
+
isHermes,
|
| 8 |
+
isDeno,
|
| 9 |
+
isLE,
|
| 10 |
+
} from './platform.js'
|
| 11 |
+
|
| 12 |
+
const atob = /* @__PURE__ */ (() => globalThis.atob)()
|
| 13 |
+
const web64 = /* @__PURE__ */ (() => Uint8Array.prototype.toBase64)()
|
| 14 |
+
|
| 15 |
+
// See http://stackoverflow.com/a/22747272/680742, which says that lowest limit is in Chrome, with 0xffff args
|
| 16 |
+
// On Hermes, actual max is 0x20_000 minus current stack depth, 1/16 of that should be safe
|
| 17 |
+
const maxFunctionArgs = 0x20_00
|
| 18 |
+
|
| 19 |
+
// toBase64+atob path is faster on everything where fromBase64 is fast
|
| 20 |
+
const useLatin1atob = web64 && atob
|
| 21 |
+
|
| 22 |
+
export function asciiPrefix(arr) {
|
| 23 |
+
let p = 0 // verified ascii bytes
|
| 24 |
+
const length = arr.length
|
| 25 |
+
// Threshold tested on Hermes (worse on <=48, better on >=52)
|
| 26 |
+
// Also on v8 arrs of size <=64 might be on heap and using Uint32Array on them is unoptimal
|
| 27 |
+
if (length > 64) {
|
| 28 |
+
// Speedup with u32
|
| 29 |
+
const u32start = (4 - (arr.byteOffset & 3)) % 4 // offset start by this many bytes for alignment
|
| 30 |
+
for (; p < u32start; p++) if (arr[p] >= 0x80) return p
|
| 31 |
+
const u32length = ((arr.byteLength - u32start) / 4) | 0
|
| 32 |
+
const u32 = new Uint32Array(arr.buffer, arr.byteOffset + u32start, u32length)
|
| 33 |
+
let i = 0
|
| 34 |
+
for (const last3 = u32length - 3; ; p += 16, i += 4) {
|
| 35 |
+
if (i >= last3) break // loop is fast enough for moving this here to be _very_ useful, likely due to array access checks
|
| 36 |
+
const a = u32[i]
|
| 37 |
+
const b = u32[i + 1]
|
| 38 |
+
const c = u32[i + 2]
|
| 39 |
+
const d = u32[i + 3]
|
| 40 |
+
// "(a | b | c | d) & mask" is slower on Hermes though faster on v8
|
| 41 |
+
if (a & 0x80_80_80_80 || b & 0x80_80_80_80 || c & 0x80_80_80_80 || d & 0x80_80_80_80) break
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
for (; i < u32length; p += 4, i++) if (u32[i] & 0x80_80_80_80) break
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
for (; p < length; p++) if (arr[p] >= 0x80) return p
|
| 48 |
+
return length
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Capable of decoding Uint16Array to UTF-16 as well as Uint8Array to Latin-1
|
| 52 |
+
export function decodeLatin1(arr, start = 0, stop = arr.length) {
|
| 53 |
+
start |= 0
|
| 54 |
+
stop |= 0
|
| 55 |
+
const total = stop - start
|
| 56 |
+
if (total === 0) return ''
|
| 57 |
+
|
| 58 |
+
if (
|
| 59 |
+
useLatin1atob &&
|
| 60 |
+
total >= 256 &&
|
| 61 |
+
total < 1e8 &&
|
| 62 |
+
arr.toBase64 === web64 &&
|
| 63 |
+
arr.BYTES_PER_ELEMENT === 1
|
| 64 |
+
) {
|
| 65 |
+
const sliced = start === 0 && stop === arr.length ? arr : arr.subarray(start, stop)
|
| 66 |
+
return atob(sliced.toBase64())
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
if (total > maxFunctionArgs) {
|
| 70 |
+
let prefix = ''
|
| 71 |
+
for (let i = start; i < stop; ) {
|
| 72 |
+
const i1 = Math.min(stop, i + maxFunctionArgs)
|
| 73 |
+
prefix += String.fromCharCode.apply(String, arr.subarray(i, i1))
|
| 74 |
+
i = i1
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
return prefix
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
const sliced = start === 0 && stop === arr.length ? arr : arr.subarray(start, stop)
|
| 81 |
+
return String.fromCharCode.apply(String, sliced)
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// Unchecked for well-formedness, raw. Expects Uint16Array input
|
| 85 |
+
export const decodeUCS2 =
|
| 86 |
+
nativeBuffer && isLE && !isDeno
|
| 87 |
+
? (u16, stop = u16.length) => {
|
| 88 |
+
// TODO: fast path for BE, perhaps faster path for Deno. Note that decoder replaces, this function doesn't
|
| 89 |
+
if (stop > 32) return nativeBuffer.from(u16.buffer, u16.byteOffset, stop * 2).ucs2Slice() // from 64 bytes, below are in heap
|
| 90 |
+
return decodeLatin1(u16, 0, stop)
|
| 91 |
+
}
|
| 92 |
+
: (u16, stop = u16.length) => decodeLatin1(u16, 0, stop)
|
| 93 |
+
|
| 94 |
+
// Does not check input, uses best available method
|
| 95 |
+
// Building an array for this is only faster than proper string concatenation when TextDecoder or native Buffer are available
|
| 96 |
+
export const decodeAscii = nativeBuffer
|
| 97 |
+
? (a) =>
|
| 98 |
+
// Buffer is faster on Node.js (but only for long enough data), if we know that output is ascii
|
| 99 |
+
a.byteLength >= 0x3_00 && !isDeno
|
| 100 |
+
? nativeBuffer.from(a.buffer, a.byteOffset, a.byteLength).latin1Slice(0, a.byteLength) // .latin1Slice is faster than .asciiSlice
|
| 101 |
+
: nativeDecoder.decode(a) // On Node.js, utf8 decoder is faster than latin1
|
| 102 |
+
: nativeDecoderLatin1
|
| 103 |
+
? (a) => nativeDecoderLatin1.decode(a) // On browsers (specifically WebKit), latin1 decoder is faster than utf8
|
| 104 |
+
: (a) =>
|
| 105 |
+
decodeLatin1(
|
| 106 |
+
a instanceof Uint8Array ? a : new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
/* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
|
| 110 |
+
|
| 111 |
+
export function encodeAsciiPrefix(x, s) {
|
| 112 |
+
let i = 0
|
| 113 |
+
for (const len3 = s.length - 3; i < len3; i += 4) {
|
| 114 |
+
const x0 = s.charCodeAt(i), x1 = s.charCodeAt(i + 1), x2 = s.charCodeAt(i + 2), x3 = s.charCodeAt(i + 3) // prettier-ignore
|
| 115 |
+
if ((x0 | x1 | x2 | x3) >= 128) break
|
| 116 |
+
x[i] = x0
|
| 117 |
+
x[i + 1] = x1
|
| 118 |
+
x[i + 2] = x2
|
| 119 |
+
x[i + 3] = x3
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
return i
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/* eslint-enable @exodus/mutable/no-param-reassign-prop-only */
|
| 126 |
+
|
| 127 |
+
// Warning: can be used only on checked strings, converts strings to 8-bit
|
| 128 |
+
export const encodeLatin1 = (str) => encodeCharcodes(str, new Uint8Array(str.length))
|
| 129 |
+
|
| 130 |
+
// Expects nativeEncoder to be present
|
| 131 |
+
const useEncodeInto = /* @__PURE__ */ (() => isHermes && nativeEncoder?.encodeInto)()
|
| 132 |
+
export const encodeAscii = useEncodeInto
|
| 133 |
+
? (str, ERR) => {
|
| 134 |
+
// Much faster in Hermes
|
| 135 |
+
const codes = new Uint8Array(str.length + 4) // overshoot by a full utf8 char
|
| 136 |
+
const info = nativeEncoder.encodeInto(str, codes)
|
| 137 |
+
if (info.read !== str.length || info.written !== str.length) throw new SyntaxError(ERR) // non-ascii
|
| 138 |
+
return codes.subarray(0, str.length)
|
| 139 |
+
}
|
| 140 |
+
: nativeBuffer
|
| 141 |
+
? (str, ERR) => {
|
| 142 |
+
// TextEncoder is slow on Node.js 24 / 25 (was ok on 22)
|
| 143 |
+
// Node.js Buffer.from always returns 8-byte aligned allocations, this is safe for e.g. conversion to Uint32Array
|
| 144 |
+
const codes = nativeBuffer.from(str, 'utf8') // ascii/latin1 coerces, we need to check
|
| 145 |
+
if (codes.length !== str.length) throw new SyntaxError(ERR) // non-ascii
|
| 146 |
+
return new Uint8Array(codes.buffer, codes.byteOffset, codes.byteLength)
|
| 147 |
+
}
|
| 148 |
+
: (str, ERR) => {
|
| 149 |
+
const codes = nativeEncoder.encode(str)
|
| 150 |
+
if (codes.length !== str.length) throw new SyntaxError(ERR) // non-ascii
|
| 151 |
+
return codes
|
| 152 |
+
}
|
node_modules/@exodus/bytes/fallback/multi-byte.encodings.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
module.exports = () => require('./multi-byte.encodings.json') // lazy-load
|
node_modules/@exodus/bytes/fallback/multi-byte.encodings.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
node_modules/@exodus/bytes/fallback/multi-byte.js
ADDED
|
@@ -0,0 +1,962 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { E_STRING } from './_utils.js'
|
| 2 |
+
import { nativeEncoder } from './platform.js'
|
| 3 |
+
import { asciiPrefix, decodeAscii, decodeLatin1, decodeUCS2 } from './latin1.js'
|
| 4 |
+
import { getTable } from './multi-byte.table.js'
|
| 5 |
+
|
| 6 |
+
export const E_STRICT = 'Input is not well-formed for this encoding'
|
| 7 |
+
|
| 8 |
+
/* Decoders */
|
| 9 |
+
|
| 10 |
+
// If the decoder is not cleared properly, state can be preserved between non-streaming calls!
|
| 11 |
+
// See comment about fatal stream
|
| 12 |
+
|
| 13 |
+
// All except iso-2022-jp are ASCII supersets
|
| 14 |
+
// When adding something that is not an ASCII superset, ajust the ASCII fast path
|
| 15 |
+
const mappers = {
|
| 16 |
+
// https://encoding.spec.whatwg.org/#euc-kr-decoder
|
| 17 |
+
'euc-kr': (err) => {
|
| 18 |
+
const euc = getTable('euc-kr')
|
| 19 |
+
let lead = 0
|
| 20 |
+
let oi = 0
|
| 21 |
+
let o16
|
| 22 |
+
|
| 23 |
+
const decodeLead = (b) => {
|
| 24 |
+
if (b < 0x41 || b > 0xfe) {
|
| 25 |
+
lead = 0
|
| 26 |
+
o16[oi++] = err()
|
| 27 |
+
if (b < 128) o16[oi++] = b
|
| 28 |
+
} else {
|
| 29 |
+
const p = euc[(lead - 0x81) * 190 + b - 0x41]
|
| 30 |
+
lead = 0
|
| 31 |
+
if (p) {
|
| 32 |
+
o16[oi++] = p
|
| 33 |
+
} else {
|
| 34 |
+
o16[oi++] = err()
|
| 35 |
+
if (b < 128) o16[oi++] = b
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
const decode = (arr, start, end, stream) => {
|
| 41 |
+
let i = start
|
| 42 |
+
o16 = new Uint16Array(end - start + (lead ? 1 : 0)) // there are pairs but they consume more than one byte
|
| 43 |
+
oi = 0
|
| 44 |
+
|
| 45 |
+
// Fast path
|
| 46 |
+
if (!lead) {
|
| 47 |
+
for (const last1 = end - 1; i < last1; ) {
|
| 48 |
+
const l = arr[i]
|
| 49 |
+
if (l < 128) {
|
| 50 |
+
o16[oi++] = l
|
| 51 |
+
i++
|
| 52 |
+
} else {
|
| 53 |
+
if (l === 0x80 || l === 0xff) break
|
| 54 |
+
const b = arr[i + 1]
|
| 55 |
+
if (b < 0x41 || b === 0xff) break
|
| 56 |
+
const p = euc[(l - 0x81) * 190 + b - 0x41]
|
| 57 |
+
if (!p) break
|
| 58 |
+
o16[oi++] = p
|
| 59 |
+
i += 2
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
if (lead && i < end) decodeLead(arr[i++])
|
| 65 |
+
while (i < end) {
|
| 66 |
+
const b = arr[i++]
|
| 67 |
+
if (b < 128) {
|
| 68 |
+
o16[oi++] = b
|
| 69 |
+
} else if (b === 0x80 || b === 0xff) {
|
| 70 |
+
o16[oi++] = err()
|
| 71 |
+
} else {
|
| 72 |
+
lead = b
|
| 73 |
+
if (i < end) decodeLead(arr[i++])
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
if (lead && !stream) {
|
| 78 |
+
lead = 0
|
| 79 |
+
o16[oi++] = err()
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
const res = decodeUCS2(o16, oi)
|
| 83 |
+
o16 = null
|
| 84 |
+
return res
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
return { decode, isAscii: () => lead === 0 }
|
| 88 |
+
},
|
| 89 |
+
// https://encoding.spec.whatwg.org/#euc-jp-decoder
|
| 90 |
+
'euc-jp': (err) => {
|
| 91 |
+
const jis0208 = getTable('jis0208')
|
| 92 |
+
const jis0212 = getTable('jis0212')
|
| 93 |
+
let j12 = false
|
| 94 |
+
let lead = 0
|
| 95 |
+
let oi = 0
|
| 96 |
+
let o16
|
| 97 |
+
|
| 98 |
+
const decodeLead = (b) => {
|
| 99 |
+
if (lead === 0x8e && b >= 0xa1 && b <= 0xdf) {
|
| 100 |
+
lead = 0
|
| 101 |
+
o16[oi++] = 0xfe_c0 + b
|
| 102 |
+
} else if (lead === 0x8f && b >= 0xa1 && b <= 0xfe) {
|
| 103 |
+
j12 = true
|
| 104 |
+
lead = b
|
| 105 |
+
} else {
|
| 106 |
+
let cp
|
| 107 |
+
if (lead >= 0xa1 && lead <= 0xfe && b >= 0xa1 && b <= 0xfe) {
|
| 108 |
+
cp = (j12 ? jis0212 : jis0208)[(lead - 0xa1) * 94 + b - 0xa1]
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
lead = 0
|
| 112 |
+
j12 = false
|
| 113 |
+
if (cp) {
|
| 114 |
+
o16[oi++] = cp
|
| 115 |
+
} else {
|
| 116 |
+
o16[oi++] = err()
|
| 117 |
+
if (b < 128) o16[oi++] = b
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
const decode = (arr, start, end, stream) => {
|
| 123 |
+
let i = start
|
| 124 |
+
o16 = new Uint16Array(end - start + (lead ? 1 : 0))
|
| 125 |
+
oi = 0
|
| 126 |
+
|
| 127 |
+
// Fast path, non-j12
|
| 128 |
+
// lead = 0 means j12 = 0
|
| 129 |
+
if (!lead) {
|
| 130 |
+
for (const last1 = end - 1; i < last1; ) {
|
| 131 |
+
const l = arr[i]
|
| 132 |
+
if (l < 128) {
|
| 133 |
+
o16[oi++] = l
|
| 134 |
+
i++
|
| 135 |
+
} else {
|
| 136 |
+
const b = arr[i + 1]
|
| 137 |
+
if (l === 0x8e && b >= 0xa1 && b <= 0xdf) {
|
| 138 |
+
o16[oi++] = 0xfe_c0 + b
|
| 139 |
+
i += 2
|
| 140 |
+
} else {
|
| 141 |
+
if (l < 0xa1 || l === 0xff || b < 0xa1 || b === 0xff) break
|
| 142 |
+
const cp = jis0208[(l - 0xa1) * 94 + b - 0xa1]
|
| 143 |
+
if (!cp) break
|
| 144 |
+
o16[oi++] = cp
|
| 145 |
+
i += 2
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
if (lead && i < end) decodeLead(arr[i++])
|
| 152 |
+
if (lead && i < end) decodeLead(arr[i++]) // could be two leads, but no more
|
| 153 |
+
while (i < end) {
|
| 154 |
+
const b = arr[i++]
|
| 155 |
+
if (b < 128) {
|
| 156 |
+
o16[oi++] = b
|
| 157 |
+
} else if ((b < 0xa1 && b !== 0x8e && b !== 0x8f) || b === 0xff) {
|
| 158 |
+
o16[oi++] = err()
|
| 159 |
+
} else {
|
| 160 |
+
lead = b
|
| 161 |
+
if (i < end) decodeLead(arr[i++])
|
| 162 |
+
if (lead && i < end) decodeLead(arr[i++]) // could be two leads
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
if (lead && !stream) {
|
| 167 |
+
lead = 0
|
| 168 |
+
j12 = false // can be true only when lead is non-zero
|
| 169 |
+
o16[oi++] = err()
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
const res = decodeUCS2(o16, oi)
|
| 173 |
+
o16 = null
|
| 174 |
+
return res
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
return { decode, isAscii: () => lead === 0 } // j12 can be true only when lead is non-zero
|
| 178 |
+
},
|
| 179 |
+
// https://encoding.spec.whatwg.org/#iso-2022-jp-decoder
|
| 180 |
+
'iso-2022-jp': (err) => {
|
| 181 |
+
const jis0208 = getTable('jis0208')
|
| 182 |
+
let dState = 1
|
| 183 |
+
let oState = 1
|
| 184 |
+
let lead = 0 // 0 or 0x21-0x7e
|
| 185 |
+
let out = false
|
| 186 |
+
|
| 187 |
+
const bytes = (pushback, b) => {
|
| 188 |
+
if (dState < 5 && b === 0x1b) {
|
| 189 |
+
dState = 6 // escape start
|
| 190 |
+
return
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
switch (dState) {
|
| 194 |
+
case 1:
|
| 195 |
+
case 2:
|
| 196 |
+
// ASCII, Roman (common)
|
| 197 |
+
out = false
|
| 198 |
+
if (dState === 2) {
|
| 199 |
+
if (b === 0x5c) return 0xa5
|
| 200 |
+
if (b === 0x7e) return 0x20_3e
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
if (b <= 0x7f && b !== 0x0e && b !== 0x0f) return b
|
| 204 |
+
return err()
|
| 205 |
+
case 3:
|
| 206 |
+
// Katakana
|
| 207 |
+
out = false
|
| 208 |
+
if (b >= 0x21 && b <= 0x5f) return 0xff_40 + b
|
| 209 |
+
return err()
|
| 210 |
+
case 4:
|
| 211 |
+
// Leading byte
|
| 212 |
+
out = false
|
| 213 |
+
if (b < 0x21 || b > 0x7e) return err()
|
| 214 |
+
lead = b
|
| 215 |
+
dState = 5
|
| 216 |
+
return
|
| 217 |
+
case 5:
|
| 218 |
+
// Trailing byte
|
| 219 |
+
out = false
|
| 220 |
+
if (b === 0x1b) {
|
| 221 |
+
dState = 6 // escape start
|
| 222 |
+
return err()
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
dState = 4
|
| 226 |
+
if (b >= 0x21 && b <= 0x7e) {
|
| 227 |
+
const cp = jis0208[(lead - 0x21) * 94 + b - 0x21]
|
| 228 |
+
if (cp) return cp
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
return err()
|
| 232 |
+
case 6:
|
| 233 |
+
// Escape start
|
| 234 |
+
if (b === 0x24 || b === 0x28) {
|
| 235 |
+
lead = b
|
| 236 |
+
dState = 7
|
| 237 |
+
return
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
out = false
|
| 241 |
+
dState = oState
|
| 242 |
+
pushback.push(b)
|
| 243 |
+
return err()
|
| 244 |
+
case 7: {
|
| 245 |
+
// Escape
|
| 246 |
+
const l = lead
|
| 247 |
+
lead = 0
|
| 248 |
+
let s
|
| 249 |
+
if (l === 0x28) {
|
| 250 |
+
// eslint-disable-next-line unicorn/prefer-switch
|
| 251 |
+
if (b === 0x42) {
|
| 252 |
+
s = 1
|
| 253 |
+
} else if (b === 0x4a) {
|
| 254 |
+
s = 2
|
| 255 |
+
} else if (b === 0x49) {
|
| 256 |
+
s = 3
|
| 257 |
+
}
|
| 258 |
+
} else if (l === 0x24 && (b === 0x40 || b === 0x42)) {
|
| 259 |
+
s = 4
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
if (s) {
|
| 263 |
+
dState = oState = s
|
| 264 |
+
const output = out
|
| 265 |
+
out = true
|
| 266 |
+
return output ? err() : undefined
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
out = false
|
| 270 |
+
dState = oState
|
| 271 |
+
pushback.push(b, l)
|
| 272 |
+
return err()
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
const eof = (pushback) => {
|
| 278 |
+
if (dState < 5) return null
|
| 279 |
+
out = false
|
| 280 |
+
switch (dState) {
|
| 281 |
+
case 5:
|
| 282 |
+
dState = 4
|
| 283 |
+
return err()
|
| 284 |
+
case 6:
|
| 285 |
+
dState = oState
|
| 286 |
+
return err()
|
| 287 |
+
case 7: {
|
| 288 |
+
dState = oState
|
| 289 |
+
pushback.push(lead)
|
| 290 |
+
lead = 0
|
| 291 |
+
return err()
|
| 292 |
+
}
|
| 293 |
+
}
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
const decode = (arr, start, end, stream) => {
|
| 297 |
+
const o16 = new Uint16Array(end - start + 2) // err in eof + lead from state
|
| 298 |
+
let oi = 0
|
| 299 |
+
let i = start
|
| 300 |
+
const pushback = [] // local and auto-cleared
|
| 301 |
+
|
| 302 |
+
// First, dump everything until EOF
|
| 303 |
+
// Same as the full loop, but without EOF handling
|
| 304 |
+
while (i < end || pushback.length > 0) {
|
| 305 |
+
const c = bytes(pushback, pushback.length > 0 ? pushback.pop() : arr[i++])
|
| 306 |
+
if (c !== undefined) o16[oi++] = c // 16-bit
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
// Then, dump EOF. This needs the same loop as the characters can be pushed back
|
| 310 |
+
if (!stream) {
|
| 311 |
+
while (i <= end || pushback.length > 0) {
|
| 312 |
+
if (i < end || pushback.length > 0) {
|
| 313 |
+
const c = bytes(pushback, pushback.length > 0 ? pushback.pop() : arr[i++])
|
| 314 |
+
if (c !== undefined) o16[oi++] = c // 16-bit
|
| 315 |
+
} else {
|
| 316 |
+
const c = eof(pushback)
|
| 317 |
+
if (c === null) break // clean exit
|
| 318 |
+
o16[oi++] = c
|
| 319 |
+
}
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
// Chrome and WebKit fail on this, we don't: completely destroy the old decoder state when finished streaming
|
| 324 |
+
// > If this’s do not flush is false, then set this’s decoder to a new instance of this’s encoding’s decoder,
|
| 325 |
+
// > Set this’s do not flush to options["stream"]
|
| 326 |
+
if (!stream) {
|
| 327 |
+
dState = oState = 1
|
| 328 |
+
lead = 0
|
| 329 |
+
out = false
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
return decodeUCS2(o16, oi)
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
return { decode, isAscii: () => false }
|
| 336 |
+
},
|
| 337 |
+
// https://encoding.spec.whatwg.org/#shift_jis-decoder
|
| 338 |
+
shift_jis: (err) => {
|
| 339 |
+
const jis0208 = getTable('jis0208')
|
| 340 |
+
let lead = 0
|
| 341 |
+
let oi = 0
|
| 342 |
+
let o16
|
| 343 |
+
|
| 344 |
+
const decodeLead = (b) => {
|
| 345 |
+
const l = lead
|
| 346 |
+
lead = 0
|
| 347 |
+
if (b >= 0x40 && b <= 0xfc && b !== 0x7f) {
|
| 348 |
+
const p = (l - (l < 0xa0 ? 0x81 : 0xc1)) * 188 + b - (b < 0x7f ? 0x40 : 0x41)
|
| 349 |
+
if (p >= 8836 && p <= 10_715) {
|
| 350 |
+
o16[oi++] = 0xe0_00 - 8836 + p
|
| 351 |
+
return
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
const cp = jis0208[p]
|
| 355 |
+
if (cp) {
|
| 356 |
+
o16[oi++] = cp
|
| 357 |
+
return
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
o16[oi++] = err()
|
| 362 |
+
if (b < 128) o16[oi++] = b
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
const decode = (arr, start, end, stream) => {
|
| 366 |
+
o16 = new Uint16Array(end - start + (lead ? 1 : 0))
|
| 367 |
+
oi = 0
|
| 368 |
+
let i = start
|
| 369 |
+
|
| 370 |
+
// Fast path
|
| 371 |
+
if (!lead) {
|
| 372 |
+
for (const last1 = end - 1; i < last1; ) {
|
| 373 |
+
const l = arr[i]
|
| 374 |
+
if (l <= 0x80) {
|
| 375 |
+
o16[oi++] = l
|
| 376 |
+
i++
|
| 377 |
+
} else if (l >= 0xa1 && l <= 0xdf) {
|
| 378 |
+
o16[oi++] = 0xfe_c0 + l
|
| 379 |
+
i++
|
| 380 |
+
} else {
|
| 381 |
+
if (l === 0xa0 || l > 0xfc) break
|
| 382 |
+
const b = arr[i + 1]
|
| 383 |
+
if (b < 0x40 || b > 0xfc || b === 0x7f) break
|
| 384 |
+
const p = (l - (l < 0xa0 ? 0x81 : 0xc1)) * 188 + b - (b < 0x7f ? 0x40 : 0x41)
|
| 385 |
+
if (p >= 8836 && p <= 10_715) {
|
| 386 |
+
o16[oi++] = 0xe0_00 - 8836 + p
|
| 387 |
+
i += 2
|
| 388 |
+
} else {
|
| 389 |
+
const cp = jis0208[p]
|
| 390 |
+
if (!cp) break
|
| 391 |
+
o16[oi++] = cp
|
| 392 |
+
i += 2
|
| 393 |
+
}
|
| 394 |
+
}
|
| 395 |
+
}
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
if (lead && i < end) decodeLead(arr[i++])
|
| 399 |
+
while (i < end) {
|
| 400 |
+
const b = arr[i++]
|
| 401 |
+
if (b <= 0x80) {
|
| 402 |
+
o16[oi++] = b // 0x80 is allowed
|
| 403 |
+
} else if (b >= 0xa1 && b <= 0xdf) {
|
| 404 |
+
o16[oi++] = 0xfe_c0 + b
|
| 405 |
+
} else if (b === 0xa0 || b > 0xfc) {
|
| 406 |
+
o16[oi++] = err()
|
| 407 |
+
} else {
|
| 408 |
+
lead = b
|
| 409 |
+
if (i < end) decodeLead(arr[i++])
|
| 410 |
+
}
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
if (lead && !stream) {
|
| 414 |
+
lead = 0
|
| 415 |
+
o16[oi++] = err()
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
const res = decodeUCS2(o16, oi)
|
| 419 |
+
o16 = null
|
| 420 |
+
return res
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
return { decode, isAscii: () => lead === 0 }
|
| 424 |
+
},
|
| 425 |
+
// https://encoding.spec.whatwg.org/#gbk-decoder
|
| 426 |
+
gbk: (err) => mappers.gb18030(err), // 10.1.1. GBK’s decoder is gb18030’s decoder
|
| 427 |
+
// https://encoding.spec.whatwg.org/#gb18030-decoder
|
| 428 |
+
gb18030: (err) => {
|
| 429 |
+
const gb18030 = getTable('gb18030')
|
| 430 |
+
const gb18030r = getTable('gb18030-ranges')
|
| 431 |
+
let g1 = 0, g2 = 0, g3 = 0 // prettier-ignore
|
| 432 |
+
const index = (p) => {
|
| 433 |
+
if ((p > 39_419 && p < 189_000) || p > 1_237_575) return
|
| 434 |
+
if (p === 7457) return 0xe7_c7
|
| 435 |
+
let a = 0, b = 0 // prettier-ignore
|
| 436 |
+
for (const [c, d] of gb18030r) {
|
| 437 |
+
if (c > p) break
|
| 438 |
+
a = c
|
| 439 |
+
b = d
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
return b + p - a
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
// g1 is 0 or 0x81-0xfe
|
| 446 |
+
// g2 is 0 or 0x30-0x39
|
| 447 |
+
// g3 is 0 or 0x81-0xfe
|
| 448 |
+
|
| 449 |
+
const decode = (arr, start, end, stream) => {
|
| 450 |
+
const o16 = new Uint16Array(end - start + (g1 ? 3 : 0)) // even with pushback it's at most 1 char per byte
|
| 451 |
+
let oi = 0
|
| 452 |
+
let i = start
|
| 453 |
+
const pushback = [] // local and auto-cleared
|
| 454 |
+
|
| 455 |
+
// Fast path for 2-byte only
|
| 456 |
+
// pushback is always empty ad start, and g1 = 0 means g2 = g3 = 0
|
| 457 |
+
if (g1 === 0) {
|
| 458 |
+
for (const last1 = end - 1; i < last1; ) {
|
| 459 |
+
const b = arr[i]
|
| 460 |
+
if (b < 128) {
|
| 461 |
+
o16[oi++] = b
|
| 462 |
+
i++
|
| 463 |
+
} else if (b === 0x80) {
|
| 464 |
+
o16[oi++] = 0x20_ac
|
| 465 |
+
i++
|
| 466 |
+
} else {
|
| 467 |
+
if (b === 0xff) break
|
| 468 |
+
const n = arr[i + 1]
|
| 469 |
+
let cp
|
| 470 |
+
if (n < 0x7f) {
|
| 471 |
+
if (n < 0x40) break
|
| 472 |
+
cp = gb18030[(b - 0x81) * 190 + n - 0x40]
|
| 473 |
+
} else {
|
| 474 |
+
if (n === 0xff || n === 0x7f) break
|
| 475 |
+
cp = gb18030[(b - 0x81) * 190 + n - 0x41]
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
if (!cp) break
|
| 479 |
+
o16[oi++] = cp // 16-bit
|
| 480 |
+
i += 2
|
| 481 |
+
}
|
| 482 |
+
}
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
// First, dump everything until EOF
|
| 486 |
+
// Same as the full loop, but without EOF handling
|
| 487 |
+
while (i < end || pushback.length > 0) {
|
| 488 |
+
const b = pushback.length > 0 ? pushback.pop() : arr[i++]
|
| 489 |
+
if (g1) {
|
| 490 |
+
// g2 can be set only when g1 is set, g3 can be set only when g2 is set
|
| 491 |
+
// hence, 3 checks for g3 is faster than 3 checks for g1
|
| 492 |
+
if (g2) {
|
| 493 |
+
if (g3) {
|
| 494 |
+
if (b <= 0x39 && b >= 0x30) {
|
| 495 |
+
const p = index(
|
| 496 |
+
(g1 - 0x81) * 12_600 + (g2 - 0x30) * 1260 + (g3 - 0x81) * 10 + b - 0x30
|
| 497 |
+
)
|
| 498 |
+
g1 = g2 = g3 = 0
|
| 499 |
+
if (p === undefined) {
|
| 500 |
+
o16[oi++] = err()
|
| 501 |
+
} else if (p <= 0xff_ff) {
|
| 502 |
+
o16[oi++] = p // Can validly return replacement
|
| 503 |
+
} else {
|
| 504 |
+
const d = p - 0x1_00_00
|
| 505 |
+
o16[oi++] = 0xd8_00 | (d >> 10)
|
| 506 |
+
o16[oi++] = 0xdc_00 | (d & 0x3_ff)
|
| 507 |
+
}
|
| 508 |
+
} else {
|
| 509 |
+
pushback.push(b, g3, g2)
|
| 510 |
+
g1 = g2 = g3 = 0
|
| 511 |
+
o16[oi++] = err()
|
| 512 |
+
}
|
| 513 |
+
} else if (b >= 0x81 && b <= 0xfe) {
|
| 514 |
+
g3 = b
|
| 515 |
+
} else {
|
| 516 |
+
pushback.push(b, g2)
|
| 517 |
+
g1 = g2 = 0
|
| 518 |
+
o16[oi++] = err()
|
| 519 |
+
}
|
| 520 |
+
} else if (b <= 0x39 && b >= 0x30) {
|
| 521 |
+
g2 = b
|
| 522 |
+
} else {
|
| 523 |
+
let cp
|
| 524 |
+
if (b >= 0x40 && b <= 0xfe && b !== 0x7f) {
|
| 525 |
+
cp = gb18030[(g1 - 0x81) * 190 + b - (b < 0x7f ? 0x40 : 0x41)]
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
g1 = 0
|
| 529 |
+
if (cp) {
|
| 530 |
+
o16[oi++] = cp // 16-bit
|
| 531 |
+
} else {
|
| 532 |
+
o16[oi++] = err()
|
| 533 |
+
if (b < 128) o16[oi++] = b // can be processed immediately
|
| 534 |
+
}
|
| 535 |
+
}
|
| 536 |
+
} else if (b < 128) {
|
| 537 |
+
o16[oi++] = b
|
| 538 |
+
} else if (b === 0x80) {
|
| 539 |
+
o16[oi++] = 0x20_ac
|
| 540 |
+
} else if (b === 0xff) {
|
| 541 |
+
o16[oi++] = err()
|
| 542 |
+
} else {
|
| 543 |
+
g1 = b
|
| 544 |
+
}
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
// if g1 = 0 then g2 = g3 = 0
|
| 548 |
+
if (g1 && !stream) {
|
| 549 |
+
g1 = g2 = g3 = 0
|
| 550 |
+
o16[oi++] = err()
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
return decodeUCS2(o16, oi)
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
return { decode, isAscii: () => g1 === 0 } // if g1 = 0 then g2 = g3 = 0
|
| 557 |
+
},
|
| 558 |
+
// https://encoding.spec.whatwg.org/#big5
|
| 559 |
+
big5: (err) => {
|
| 560 |
+
// The only decoder which returns multiple codepoints per byte, also has non-charcode codepoints
|
| 561 |
+
// We store that as strings
|
| 562 |
+
const big5 = getTable('big5')
|
| 563 |
+
let lead = 0
|
| 564 |
+
let oi = 0
|
| 565 |
+
let o16
|
| 566 |
+
|
| 567 |
+
const decodeLead = (b) => {
|
| 568 |
+
if (b < 0x40 || (b > 0x7e && b < 0xa1) || b === 0xff) {
|
| 569 |
+
lead = 0
|
| 570 |
+
o16[oi++] = err()
|
| 571 |
+
if (b < 128) o16[oi++] = b
|
| 572 |
+
} else {
|
| 573 |
+
const p = big5[(lead - 0x81) * 157 + b - (b < 0x7f ? 0x40 : 0x62)]
|
| 574 |
+
lead = 0
|
| 575 |
+
if (p > 0x1_00_00) {
|
| 576 |
+
o16[oi++] = p >> 16
|
| 577 |
+
o16[oi++] = p & 0xff_ff
|
| 578 |
+
} else if (p) {
|
| 579 |
+
o16[oi++] = p
|
| 580 |
+
} else {
|
| 581 |
+
o16[oi++] = err()
|
| 582 |
+
if (b < 128) o16[oi++] = b
|
| 583 |
+
}
|
| 584 |
+
}
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
// eslint-disable-next-line sonarjs/no-identical-functions
|
| 588 |
+
const decode = (arr, start, end, stream) => {
|
| 589 |
+
let i = start
|
| 590 |
+
o16 = new Uint16Array(end - start + (lead ? 1 : 0)) // there are pairs but they consume more than one byte
|
| 591 |
+
oi = 0
|
| 592 |
+
|
| 593 |
+
// Fast path
|
| 594 |
+
if (!lead) {
|
| 595 |
+
for (const last1 = end - 1; i < last1; ) {
|
| 596 |
+
const l = arr[i]
|
| 597 |
+
if (l < 128) {
|
| 598 |
+
o16[oi++] = l
|
| 599 |
+
i++
|
| 600 |
+
} else {
|
| 601 |
+
if (l === 0x80 || l === 0xff) break
|
| 602 |
+
const b = arr[i + 1]
|
| 603 |
+
if (b < 0x40 || (b > 0x7e && b < 0xa1) || b === 0xff) break
|
| 604 |
+
const p = big5[(l - 0x81) * 157 + b - (b < 0x7f ? 0x40 : 0x62)]
|
| 605 |
+
if (p > 0x1_00_00) {
|
| 606 |
+
o16[oi++] = p >> 16
|
| 607 |
+
o16[oi++] = p & 0xff_ff
|
| 608 |
+
} else {
|
| 609 |
+
if (!p) break
|
| 610 |
+
o16[oi++] = p
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
i += 2
|
| 614 |
+
}
|
| 615 |
+
}
|
| 616 |
+
}
|
| 617 |
+
|
| 618 |
+
if (lead && i < end) decodeLead(arr[i++])
|
| 619 |
+
while (i < end) {
|
| 620 |
+
const b = arr[i++]
|
| 621 |
+
if (b < 128) {
|
| 622 |
+
o16[oi++] = b
|
| 623 |
+
} else if (b === 0x80 || b === 0xff) {
|
| 624 |
+
o16[oi++] = err()
|
| 625 |
+
} else {
|
| 626 |
+
lead = b
|
| 627 |
+
if (i < end) decodeLead(arr[i++])
|
| 628 |
+
}
|
| 629 |
+
}
|
| 630 |
+
|
| 631 |
+
if (lead && !stream) {
|
| 632 |
+
lead = 0
|
| 633 |
+
o16[oi++] = err()
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
const res = decodeUCS2(o16, oi)
|
| 637 |
+
o16 = null
|
| 638 |
+
return res
|
| 639 |
+
}
|
| 640 |
+
|
| 641 |
+
return { decode, isAscii: () => lead === 0 }
|
| 642 |
+
},
|
| 643 |
+
}
|
| 644 |
+
|
| 645 |
+
export const isAsciiSuperset = (enc) => enc !== 'iso-2022-jp' // all others are ASCII supersets and can use fast path
|
| 646 |
+
|
| 647 |
+
export function multibyteDecoder(enc, loose = false) {
|
| 648 |
+
if (typeof loose !== 'boolean') throw new TypeError('loose option should be boolean')
|
| 649 |
+
if (!Object.hasOwn(mappers, enc)) throw new RangeError('Unsupported encoding')
|
| 650 |
+
|
| 651 |
+
// Input is assumed to be typechecked already
|
| 652 |
+
let mapper
|
| 653 |
+
const asciiSuperset = isAsciiSuperset(enc)
|
| 654 |
+
let streaming // because onErr is cached in mapper
|
| 655 |
+
const onErr = loose
|
| 656 |
+
? () => 0xff_fd
|
| 657 |
+
: () => {
|
| 658 |
+
// The correct way per spec seems to be not destoying the decoder state in stream mode, even when fatal
|
| 659 |
+
// Decoders big5, euc-jp, euc-kr, shift_jis, gb18030 / gbk - all clear state before throwing unless EOF, so not affected
|
| 660 |
+
// iso-2022-jp is the only tricky one one where this !stream check matters in non-stream mode
|
| 661 |
+
if (!streaming) mapper = null // destroy state, effectively the same as 'do not flush' = false, but early
|
| 662 |
+
throw new TypeError(E_STRICT)
|
| 663 |
+
}
|
| 664 |
+
|
| 665 |
+
return (arr, stream = false) => {
|
| 666 |
+
let res = ''
|
| 667 |
+
if (asciiSuperset && (!mapper || mapper.isAscii?.())) {
|
| 668 |
+
const prefixLen = asciiPrefix(arr)
|
| 669 |
+
if (prefixLen === arr.length) return decodeAscii(arr) // ascii
|
| 670 |
+
res = decodeLatin1(arr, 0, prefixLen) // TODO: check if decodeAscii with subarray is faster for small prefixes too
|
| 671 |
+
}
|
| 672 |
+
|
| 673 |
+
streaming = stream // affects onErr
|
| 674 |
+
if (!mapper) mapper = mappers[enc](onErr)
|
| 675 |
+
return res + mapper.decode(arr, res.length, arr.length, stream)
|
| 676 |
+
}
|
| 677 |
+
}
|
| 678 |
+
|
| 679 |
+
/* Encoders */
|
| 680 |
+
|
| 681 |
+
const maps = new Map()
|
| 682 |
+
const e7 = [[148, 236], [149, 237], [150, 243]] // prettier-ignore
|
| 683 |
+
const e8 = [[30, 89], [38, 97], [43, 102], [44, 103], [50, 109], [67, 126], [84, 144], [100, 160]] // prettier-ignore
|
| 684 |
+
const preencoders = {
|
| 685 |
+
__proto__: null,
|
| 686 |
+
big5: (p) => ((((p / 157) | 0) + 0x81) << 8) | ((p % 157 < 0x3f ? 0x40 : 0x62) + (p % 157)),
|
| 687 |
+
shift_jis: (p) => {
|
| 688 |
+
const l = (p / 188) | 0
|
| 689 |
+
const t = p % 188
|
| 690 |
+
return ((l + (l < 0x1f ? 0x81 : 0xc1)) << 8) | ((t < 0x3f ? 0x40 : 0x41) + t)
|
| 691 |
+
},
|
| 692 |
+
'iso-2022-jp': (p) => ((((p / 94) | 0) + 0x21) << 8) | ((p % 94) + 0x21),
|
| 693 |
+
'euc-jp': (p) => ((((p / 94) | 0) + 0xa1) << 8) | ((p % 94) + 0xa1),
|
| 694 |
+
'euc-kr': (p) => ((((p / 190) | 0) + 0x81) << 8) | ((p % 190) + 0x41),
|
| 695 |
+
gb18030: (p) => ((((p / 190) | 0) + 0x81) << 8) | ((p % 190 < 0x3f ? 0x40 : 0x41) + (p % 190)),
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
preencoders.gbk = preencoders.gb18030
|
| 699 |
+
|
| 700 |
+
// We accept that encoders use non-trivial amount of mem, for perf
|
| 701 |
+
// most are are 128 KiB mem, big5 is 380 KiB, lazy-loaded at first use
|
| 702 |
+
function getMap(id, size, ascii) {
|
| 703 |
+
const cached = maps.get(id)
|
| 704 |
+
if (cached) return cached
|
| 705 |
+
let tname = id
|
| 706 |
+
const sjis = id === 'shift_jis'
|
| 707 |
+
const iso2022jp = id === 'iso-2022-jp'
|
| 708 |
+
if (iso2022jp) tname = 'jis0208'
|
| 709 |
+
if (id === 'gbk') tname = 'gb18030'
|
| 710 |
+
if (id === 'euc-jp' || sjis) tname = 'jis0208'
|
| 711 |
+
const table = getTable(tname)
|
| 712 |
+
const map = new Uint16Array(size)
|
| 713 |
+
const enc = preencoders[id] || ((p) => p + 1)
|
| 714 |
+
for (let i = 0; i < table.length; i++) {
|
| 715 |
+
const c = table[i]
|
| 716 |
+
if (!c) continue
|
| 717 |
+
if (id === 'big5') {
|
| 718 |
+
if (i < 5024) continue // this also skips multi-codepoint strings
|
| 719 |
+
// In big5, all return first entries except for these
|
| 720 |
+
if (
|
| 721 |
+
map[c] &&
|
| 722 |
+
c !== 0x25_50 &&
|
| 723 |
+
c !== 0x25_5e &&
|
| 724 |
+
c !== 0x25_61 &&
|
| 725 |
+
c !== 0x25_6a &&
|
| 726 |
+
c !== 0x53_41 &&
|
| 727 |
+
c !== 0x53_45
|
| 728 |
+
) {
|
| 729 |
+
continue
|
| 730 |
+
}
|
| 731 |
+
} else {
|
| 732 |
+
if (sjis && i >= 8272 && i <= 8835) continue
|
| 733 |
+
if (map[c]) continue
|
| 734 |
+
}
|
| 735 |
+
|
| 736 |
+
if (c > 0xff_ff) {
|
| 737 |
+
// always a single codepoint here
|
| 738 |
+
const s = String.fromCharCode(c >> 16, c & 0xff_ff)
|
| 739 |
+
map[s.codePointAt(0)] = enc(i)
|
| 740 |
+
} else {
|
| 741 |
+
map[c] = enc(i)
|
| 742 |
+
}
|
| 743 |
+
}
|
| 744 |
+
|
| 745 |
+
if (ascii) for (let i = 0; i < 0x80; i++) map[i] = i
|
| 746 |
+
if (sjis || id === 'euc-jp') {
|
| 747 |
+
if (sjis) map[0x80] = 0x80
|
| 748 |
+
const d = sjis ? 0xfe_c0 : 0x70_c0
|
| 749 |
+
for (let i = 0xff_61; i <= 0xff_9f; i++) map[i] = i - d
|
| 750 |
+
map[0x22_12] = map[0xff_0d]
|
| 751 |
+
map[0xa5] = 0x5c
|
| 752 |
+
map[0x20_3e] = 0x7e
|
| 753 |
+
} else if (tname === 'gb18030') {
|
| 754 |
+
if (id === 'gbk') map[0x20_ac] = 0x80
|
| 755 |
+
for (let i = 0xe7_8d; i <= 0xe7_93; i++) map[i] = i - 0x40_b4
|
| 756 |
+
for (const [a, b] of e7) map[0xe7_00 | a] = 0xa6_00 | b
|
| 757 |
+
for (const [a, b] of e8) map[0xe8_00 | a] = 0xfe_00 | b
|
| 758 |
+
}
|
| 759 |
+
|
| 760 |
+
maps.set(id, map)
|
| 761 |
+
return map
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
+
const NON_LATIN = /[^\x00-\xFF]/ // eslint-disable-line no-control-regex
|
| 765 |
+
let gb18030r, katakana
|
| 766 |
+
|
| 767 |
+
export function multibyteEncoder(enc, onError) {
|
| 768 |
+
if (!Object.hasOwn(mappers, enc)) throw new RangeError('Unsupported encoding')
|
| 769 |
+
const size = enc === 'big5' ? 0x2_f8_a7 : 0x1_00_00 // for big5, max codepoint in table + 1
|
| 770 |
+
const iso2022jp = enc === 'iso-2022-jp'
|
| 771 |
+
const gb18030 = enc === 'gb18030'
|
| 772 |
+
const ascii = isAsciiSuperset(enc)
|
| 773 |
+
const width = iso2022jp ? 5 : gb18030 ? 4 : 2
|
| 774 |
+
const tailsize = iso2022jp ? 3 : 0
|
| 775 |
+
const map = getMap(enc, size, ascii)
|
| 776 |
+
if (gb18030 && !gb18030r) gb18030r = getTable('gb18030-ranges')
|
| 777 |
+
if (iso2022jp && !katakana) katakana = getTable('iso-2022-jp-katakana')
|
| 778 |
+
return (str) => {
|
| 779 |
+
if (typeof str !== 'string') throw new TypeError(E_STRING)
|
| 780 |
+
if (ascii && nativeEncoder && !NON_LATIN.test(str)) {
|
| 781 |
+
const u8 = nativeEncoder.encode(str)
|
| 782 |
+
if (u8.length === str.length) return u8
|
| 783 |
+
}
|
| 784 |
+
|
| 785 |
+
const length = str.length
|
| 786 |
+
const u8 = new Uint8Array(length * width + tailsize)
|
| 787 |
+
let i = 0
|
| 788 |
+
|
| 789 |
+
if (ascii) {
|
| 790 |
+
while (i < length) {
|
| 791 |
+
const x = str.charCodeAt(i)
|
| 792 |
+
if (x >= 128) break
|
| 793 |
+
u8[i++] = x
|
| 794 |
+
}
|
| 795 |
+
}
|
| 796 |
+
|
| 797 |
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
| 798 |
+
const err = (code) => {
|
| 799 |
+
if (onError) return onError(code, u8, i)
|
| 800 |
+
throw new TypeError(E_STRICT)
|
| 801 |
+
}
|
| 802 |
+
|
| 803 |
+
if (!map || map.length < size) /* c8 ignore next */ throw new Error('Unreachable') // Important for perf
|
| 804 |
+
|
| 805 |
+
if (iso2022jp) {
|
| 806 |
+
let state = 0 // 0 = ASCII, 1 = Roman, 2 = jis0208
|
| 807 |
+
const restore = () => {
|
| 808 |
+
state = 0
|
| 809 |
+
u8[i++] = 0x1b
|
| 810 |
+
u8[i++] = 0x28
|
| 811 |
+
u8[i++] = 0x42
|
| 812 |
+
}
|
| 813 |
+
|
| 814 |
+
for (let j = 0; j < length; j++) {
|
| 815 |
+
let x = str.charCodeAt(j)
|
| 816 |
+
if (x >= 0xd8_00 && x < 0xe0_00) {
|
| 817 |
+
if (state === 2) restore()
|
| 818 |
+
if (x >= 0xdc_00 || j + 1 === length) {
|
| 819 |
+
i += err(x) // lone
|
| 820 |
+
} else {
|
| 821 |
+
const x1 = str.charCodeAt(j + 1)
|
| 822 |
+
if (x1 < 0xdc_00 || x1 >= 0xe0_00) {
|
| 823 |
+
i += err(x) // lone
|
| 824 |
+
} else {
|
| 825 |
+
j++ // consume x1
|
| 826 |
+
i += err(0x1_00_00 + ((x1 & 0x3_ff) | ((x & 0x3_ff) << 10)))
|
| 827 |
+
}
|
| 828 |
+
}
|
| 829 |
+
} else if (x < 0x80) {
|
| 830 |
+
if (state === 2 || (state === 1 && (x === 0x5c || x === 0x7e))) restore()
|
| 831 |
+
if (x === 0xe || x === 0xf || x === 0x1b) {
|
| 832 |
+
i += err(0xff_fd) // 12.2.2. step 3: This returns U+FFFD rather than codePoint to prevent attacks
|
| 833 |
+
} else {
|
| 834 |
+
u8[i++] = x
|
| 835 |
+
}
|
| 836 |
+
} else if (x === 0xa5 || x === 0x20_3e) {
|
| 837 |
+
if (state !== 1) {
|
| 838 |
+
state = 1
|
| 839 |
+
u8[i++] = 0x1b
|
| 840 |
+
u8[i++] = 0x28
|
| 841 |
+
u8[i++] = 0x4a
|
| 842 |
+
}
|
| 843 |
+
|
| 844 |
+
u8[i++] = x === 0xa5 ? 0x5c : 0x7e
|
| 845 |
+
} else {
|
| 846 |
+
if (x === 0x22_12) x = 0xff_0d
|
| 847 |
+
if (x >= 0xff_61 && x <= 0xff_9f) x = katakana[x - 0xff_61]
|
| 848 |
+
const e = map[x]
|
| 849 |
+
if (e) {
|
| 850 |
+
if (state !== 2) {
|
| 851 |
+
state = 2
|
| 852 |
+
u8[i++] = 0x1b
|
| 853 |
+
u8[i++] = 0x24
|
| 854 |
+
u8[i++] = 0x42
|
| 855 |
+
}
|
| 856 |
+
|
| 857 |
+
u8[i++] = e >> 8
|
| 858 |
+
u8[i++] = e & 0xff
|
| 859 |
+
} else {
|
| 860 |
+
if (state === 2) restore()
|
| 861 |
+
i += err(x)
|
| 862 |
+
}
|
| 863 |
+
}
|
| 864 |
+
}
|
| 865 |
+
|
| 866 |
+
if (state) restore()
|
| 867 |
+
} else if (gb18030) {
|
| 868 |
+
// Deduping this branch hurts other encoders perf
|
| 869 |
+
const encode = (cp) => {
|
| 870 |
+
let a = 0, b = 0 // prettier-ignore
|
| 871 |
+
for (const [c, d] of gb18030r) {
|
| 872 |
+
if (d > cp) break
|
| 873 |
+
a = c
|
| 874 |
+
b = d
|
| 875 |
+
}
|
| 876 |
+
|
| 877 |
+
let rp = cp === 0xe7_c7 ? 7457 : a + cp - b
|
| 878 |
+
u8[i++] = 0x81 + ((rp / 12_600) | 0)
|
| 879 |
+
rp %= 12_600
|
| 880 |
+
u8[i++] = 0x30 + ((rp / 1260) | 0)
|
| 881 |
+
rp %= 1260
|
| 882 |
+
u8[i++] = 0x81 + ((rp / 10) | 0)
|
| 883 |
+
u8[i++] = 0x30 + (rp % 10)
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
+
for (let j = i; j < length; j++) {
|
| 887 |
+
const x = str.charCodeAt(j)
|
| 888 |
+
if (x >= 0xd8_00 && x < 0xe0_00) {
|
| 889 |
+
if (x >= 0xdc_00 || j + 1 === length) {
|
| 890 |
+
i += err(x) // lone
|
| 891 |
+
} else {
|
| 892 |
+
const x1 = str.charCodeAt(j + 1)
|
| 893 |
+
if (x1 < 0xdc_00 || x1 >= 0xe0_00) {
|
| 894 |
+
i += err(x) // lone
|
| 895 |
+
} else {
|
| 896 |
+
j++ // consume x1
|
| 897 |
+
encode(0x1_00_00 + ((x1 & 0x3_ff) | ((x & 0x3_ff) << 10)))
|
| 898 |
+
}
|
| 899 |
+
}
|
| 900 |
+
} else {
|
| 901 |
+
const e = map[x]
|
| 902 |
+
if (e & 0xff_00) {
|
| 903 |
+
u8[i++] = e >> 8
|
| 904 |
+
u8[i++] = e & 0xff
|
| 905 |
+
} else if (e || x === 0) {
|
| 906 |
+
u8[i++] = e
|
| 907 |
+
} else if (x === 0xe5_e5) {
|
| 908 |
+
i += err(x)
|
| 909 |
+
} else {
|
| 910 |
+
encode(x)
|
| 911 |
+
}
|
| 912 |
+
}
|
| 913 |
+
}
|
| 914 |
+
} else {
|
| 915 |
+
const long =
|
| 916 |
+
enc === 'big5'
|
| 917 |
+
? (x) => {
|
| 918 |
+
const e = map[x]
|
| 919 |
+
if (e & 0xff_00) {
|
| 920 |
+
u8[i++] = e >> 8
|
| 921 |
+
u8[i++] = e & 0xff
|
| 922 |
+
} else if (e || x === 0) {
|
| 923 |
+
u8[i++] = e
|
| 924 |
+
} else {
|
| 925 |
+
i += err(x)
|
| 926 |
+
}
|
| 927 |
+
}
|
| 928 |
+
: (x) => {
|
| 929 |
+
i += err(x)
|
| 930 |
+
}
|
| 931 |
+
|
| 932 |
+
for (let j = i; j < length; j++) {
|
| 933 |
+
const x = str.charCodeAt(j)
|
| 934 |
+
if (x >= 0xd8_00 && x < 0xe0_00) {
|
| 935 |
+
if (x >= 0xdc_00 || j + 1 === length) {
|
| 936 |
+
i += err(x) // lone
|
| 937 |
+
} else {
|
| 938 |
+
const x1 = str.charCodeAt(j + 1)
|
| 939 |
+
if (x1 < 0xdc_00 || x1 >= 0xe0_00) {
|
| 940 |
+
i += err(x) // lone
|
| 941 |
+
} else {
|
| 942 |
+
j++ // consume x1
|
| 943 |
+
long(0x1_00_00 + ((x1 & 0x3_ff) | ((x & 0x3_ff) << 10)))
|
| 944 |
+
}
|
| 945 |
+
}
|
| 946 |
+
} else {
|
| 947 |
+
const e = map[x]
|
| 948 |
+
if (e & 0xff_00) {
|
| 949 |
+
u8[i++] = e >> 8
|
| 950 |
+
u8[i++] = e & 0xff
|
| 951 |
+
} else if (e || x === 0) {
|
| 952 |
+
u8[i++] = e
|
| 953 |
+
} else {
|
| 954 |
+
i += err(x)
|
| 955 |
+
}
|
| 956 |
+
}
|
| 957 |
+
}
|
| 958 |
+
}
|
| 959 |
+
|
| 960 |
+
return i === u8.length ? u8 : u8.slice(0, i)
|
| 961 |
+
}
|
| 962 |
+
}
|
node_modules/@exodus/bytes/fallback/multi-byte.table.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { fromBase64url } from '@exodus/bytes/base64.js'
|
| 2 |
+
import { utf16toString } from '@exodus/bytes/utf16.js'
|
| 3 |
+
import loadEncodings from './multi-byte.encodings.cjs'
|
| 4 |
+
|
| 5 |
+
export const sizes = {
|
| 6 |
+
jis0208: 11_104,
|
| 7 |
+
jis0212: 7211,
|
| 8 |
+
'euc-kr': 23_750,
|
| 9 |
+
gb18030: 23_940,
|
| 10 |
+
big5: 19_782,
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
// This is huge. It's _much_ smaller than https://npmjs.com/text-encoding though
|
| 14 |
+
// Exactly as mapped by the index table
|
| 15 |
+
// 0,x - hole of x empty elements
|
| 16 |
+
// n,c - continious [c, ...] of length n
|
| 17 |
+
// $.. - references to common chunks
|
| 18 |
+
// -{x} - same as 1,{x}
|
| 19 |
+
|
| 20 |
+
// See tests/multi-byte.test.js to verify that this data decodes exactly into the encoding spec tables
|
| 21 |
+
|
| 22 |
+
let indices
|
| 23 |
+
const tables = new Map()
|
| 24 |
+
/* eslint-disable @exodus/mutable/no-param-reassign-prop-only */
|
| 25 |
+
|
| 26 |
+
function loadBase64(str) {
|
| 27 |
+
const x = fromBase64url(str)
|
| 28 |
+
const len = x.length
|
| 29 |
+
const len2 = len >> 1
|
| 30 |
+
const y = new Uint8Array(len)
|
| 31 |
+
let a = -1, b = 0 // prettier-ignore
|
| 32 |
+
for (let i = 0, j = 0; i < len; i += 2, j++) {
|
| 33 |
+
a = (a + x[j] + 1) & 0xff
|
| 34 |
+
b = (b + x[len2 + j]) & 0xff
|
| 35 |
+
y[i] = a
|
| 36 |
+
y[i + 1] = b
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return y
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
function unwrap(res, t, pos) {
|
| 43 |
+
let code = 0
|
| 44 |
+
for (let i = 0; i < t.length; i++) {
|
| 45 |
+
let x = t[i]
|
| 46 |
+
if (typeof x === 'number') {
|
| 47 |
+
if (x === 0) {
|
| 48 |
+
pos += t[++i]
|
| 49 |
+
} else {
|
| 50 |
+
if (x < 0) {
|
| 51 |
+
code -= x
|
| 52 |
+
x = 1
|
| 53 |
+
} else {
|
| 54 |
+
code += t[++i]
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
for (let k = 0; k < x; k++, pos++, code++) {
|
| 58 |
+
if (code <= 0xff_ff) {
|
| 59 |
+
res[pos] = code
|
| 60 |
+
} else {
|
| 61 |
+
const c = String.fromCodePoint(code)
|
| 62 |
+
res[pos] = (c.charCodeAt(0) << 16) | c.charCodeAt(1)
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
} else if (x[0] === '$' && Object.hasOwn(indices, x)) {
|
| 67 |
+
pos = unwrap(res, indices[x], pos) // self-reference using shared chunks
|
| 68 |
+
} else {
|
| 69 |
+
let last
|
| 70 |
+
// splits by codepoints
|
| 71 |
+
for (const c of utf16toString(loadBase64(x), 'uint8-le')) {
|
| 72 |
+
last = c
|
| 73 |
+
res[pos++] = c.length === 1 ? c.charCodeAt(0) : (c.charCodeAt(0) << 16) | c.charCodeAt(1)
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
code = last.codePointAt(0) + 1
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
return pos
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
export function getTable(id) {
|
| 84 |
+
const cached = tables.get(id)
|
| 85 |
+
if (cached) return cached
|
| 86 |
+
|
| 87 |
+
if (!indices) indices = loadEncodings() // lazy-load
|
| 88 |
+
if (!Object.hasOwn(indices, id)) throw new Error('Unknown encoding')
|
| 89 |
+
if (!indices[id]) throw new Error('Table already used (likely incorrect bundler dedupe)')
|
| 90 |
+
|
| 91 |
+
let res
|
| 92 |
+
if (id.endsWith('-ranges')) {
|
| 93 |
+
res = []
|
| 94 |
+
let a = 0, b = 0 // prettier-ignore
|
| 95 |
+
const idx = indices[id]
|
| 96 |
+
while (idx.length > 0) res.push([(a += idx.shift()), (b += idx.shift())]) // destroying, we remove it later anyway
|
| 97 |
+
} else if (id.endsWith('-katakana')) {
|
| 98 |
+
let a = -1
|
| 99 |
+
res = new Uint16Array(indices[id].map((x) => (a += x + 1)))
|
| 100 |
+
} else if (id === 'big5') {
|
| 101 |
+
res = new Uint32Array(sizes[id]) // single or double charcodes
|
| 102 |
+
unwrap(res, indices[id], 0)
|
| 103 |
+
// Pointer code updates are embedded into the table
|
| 104 |
+
// These are skipped in encoder as encoder uses only pointers >= (0xA1 - 0x81) * 157
|
| 105 |
+
res[1133] = 0xca_03_04
|
| 106 |
+
res[1135] = 0xca_03_0c
|
| 107 |
+
res[1164] = 0xea_03_04
|
| 108 |
+
res[1166] = 0xea_03_0c
|
| 109 |
+
} else {
|
| 110 |
+
if (!Object.hasOwn(sizes, id)) throw new Error('Unknown encoding')
|
| 111 |
+
res = new Uint16Array(sizes[id])
|
| 112 |
+
unwrap(res, indices[id], 0)
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
indices[id] = null // gc
|
| 116 |
+
tables.set(id, res)
|
| 117 |
+
return res
|
| 118 |
+
}
|