This commit is contained in:
2025-08-18 23:06:34 +08:00
parent 0bc04fb659
commit ed18af0cad
1926 changed files with 275098 additions and 0 deletions

21
package/node_modules/@tufjs/canonical-json/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 GitHub and the TUF Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,64 @@
const COMMA = ',';
const COLON = ':';
const LEFT_SQUARE_BRACKET = '[';
const RIGHT_SQUARE_BRACKET = ']';
const LEFT_CURLY_BRACKET = '{';
const RIGHT_CURLY_BRACKET = '}';
// Recursively encodes the supplied object according to the canonical JSON form
// as specified at http://wiki.laptop.org/go/Canonical_JSON. It's a restricted
// dialect of JSON in which keys are lexically sorted, floats are not allowed,
// and only double quotes and backslashes are escaped.
function canonicalize(object) {
const buffer = [];
if (typeof object === 'string') {
buffer.push(canonicalizeString(object));
} else if (typeof object === 'boolean') {
buffer.push(JSON.stringify(object));
} else if (Number.isInteger(object)) {
buffer.push(JSON.stringify(object));
} else if (object === null) {
buffer.push(JSON.stringify(object));
} else if (Array.isArray(object)) {
buffer.push(LEFT_SQUARE_BRACKET);
let first = true;
object.forEach((element) => {
if (!first) {
buffer.push(COMMA);
}
first = false;
buffer.push(canonicalize(element));
});
buffer.push(RIGHT_SQUARE_BRACKET);
} else if (typeof object === 'object') {
buffer.push(LEFT_CURLY_BRACKET);
let first = true;
Object.keys(object)
.sort()
.forEach((property) => {
if (!first) {
buffer.push(COMMA);
}
first = false;
buffer.push(canonicalizeString(property));
buffer.push(COLON);
buffer.push(canonicalize(object[property]));
});
buffer.push(RIGHT_CURLY_BRACKET);
} else {
throw new TypeError('cannot encode ' + object.toString());
}
return buffer.join('');
}
// String canonicalization consists of escaping backslash (\) and double
// quote (") characters and wrapping the resulting string in double quotes.
function canonicalizeString(string) {
const escapedString = string.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
return '"' + escapedString + '"';
}
module.exports = {
canonicalize,
};

View File

@@ -0,0 +1,35 @@
{
"name": "@tufjs/canonical-json",
"version": "2.0.0",
"description": "OLPC JSON canonicalization",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"license": "MIT",
"keywords": [
"json",
"canonical",
"canonicalize",
"canonicalization",
"crypto",
"signature",
"olpc"
],
"author": "bdehamer@github.com",
"repository": {
"type": "git",
"url": "git+https://github.com/theupdateframework/tuf-js.git"
},
"homepage": "https://github.com/theupdateframework/tuf-js/tree/main/packages/canonical-json#readme",
"bugs": {
"url": "https://github.com/theupdateframework/tuf-js/issues"
},
"files": [
"lib/"
],
"scripts": {
"test": "jest"
},
"engines": {
"node": "^16.14.0 || >=18.0.0"
}
}