@metamask/utils 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -1
- package/dist/json.d.ts +10 -0
- package/dist/json.js +136 -1
- package/dist/json.js.map +1 -1
- package/dist/misc.d.ts +47 -0
- package/dist/misc.js +80 -1
- package/dist/misc.js.map +1 -1
- package/dist/test.data.d.ts +85 -0
- package/dist/test.data.js +130 -0
- package/dist/test.data.js.map +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [2.1.0]
|
|
10
|
+
### Added
|
|
11
|
+
- Add JSON storage validation and limit utilities ([#14](https://github.com/MetaMask/utils/pull/14))
|
|
12
|
+
- Adds a new function `validateJsonAndGetSize`.
|
|
13
|
+
|
|
9
14
|
## [2.0.0]
|
|
10
15
|
### Added
|
|
11
16
|
- Add more JSON utils ([#8](https://github.com/MetaMask/utils/pull/8))
|
|
@@ -18,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
18
23
|
### Added
|
|
19
24
|
- Initial release
|
|
20
25
|
|
|
21
|
-
[Unreleased]: https://github.com/MetaMask/utils/compare/v2.
|
|
26
|
+
[Unreleased]: https://github.com/MetaMask/utils/compare/v2.1.0...HEAD
|
|
27
|
+
[2.1.0]: https://github.com/MetaMask/utils/compare/v2.0.0...v2.1.0
|
|
22
28
|
[2.0.0]: https://github.com/MetaMask/utils/compare/v1.0.0...v2.0.0
|
|
23
29
|
[1.0.0]: https://github.com/MetaMask/utils/releases/tag/v1.0.0
|
package/dist/json.d.ts
CHANGED
|
@@ -168,4 +168,14 @@ declare type JsonRpcValidatorOptions = {
|
|
|
168
168
|
* @returns The JSON-RPC ID validator function.
|
|
169
169
|
*/
|
|
170
170
|
export declare function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions): (id: unknown) => id is JsonRpcId;
|
|
171
|
+
/**
|
|
172
|
+
* Checks whether a value is JSON serializable and counts the total number
|
|
173
|
+
* of bytes needed to store the serialized version of the value.
|
|
174
|
+
*
|
|
175
|
+
* @param jsObject - Potential JSON serializable object.
|
|
176
|
+
* @param skipSizingProcess - Skip JSON size calculation (default: false).
|
|
177
|
+
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
|
|
178
|
+
* the value was serializable and a number of bytes that it will use when serialized to JSON.
|
|
179
|
+
*/
|
|
180
|
+
export declare function validateJsonAndGetSize(jsObject: unknown, skipSizingProcess?: boolean): [isValid: boolean, plainTextSizeInBytes: number];
|
|
171
181
|
export {};
|
package/dist/json.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getJsonRpcIdValidator = exports.assertIsJsonRpcFailure = exports.isJsonRpcFailure = exports.assertIsJsonRpcSuccess = exports.isJsonRpcSuccess = exports.assertIsJsonRpcRequest = exports.isJsonRpcRequest = exports.assertIsJsonRpcNotification = exports.isJsonRpcNotification = exports.jsonrpc2 = exports.isValidJson = void 0;
|
|
6
|
+
exports.validateJsonAndGetSize = exports.getJsonRpcIdValidator = exports.assertIsJsonRpcFailure = exports.isJsonRpcFailure = exports.assertIsJsonRpcSuccess = exports.isJsonRpcSuccess = exports.assertIsJsonRpcRequest = exports.isJsonRpcRequest = exports.assertIsJsonRpcNotification = exports.isJsonRpcNotification = exports.jsonrpc2 = exports.isValidJson = void 0;
|
|
7
7
|
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
8
8
|
const misc_1 = require("./misc");
|
|
9
9
|
/**
|
|
@@ -154,4 +154,139 @@ function getJsonRpcIdValidator(options) {
|
|
|
154
154
|
return isValidJsonRpcId;
|
|
155
155
|
}
|
|
156
156
|
exports.getJsonRpcIdValidator = getJsonRpcIdValidator;
|
|
157
|
+
/**
|
|
158
|
+
* Checks whether a value is JSON serializable and counts the total number
|
|
159
|
+
* of bytes needed to store the serialized version of the value.
|
|
160
|
+
*
|
|
161
|
+
* @param jsObject - Potential JSON serializable object.
|
|
162
|
+
* @param skipSizingProcess - Skip JSON size calculation (default: false).
|
|
163
|
+
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
|
|
164
|
+
* the value was serializable and a number of bytes that it will use when serialized to JSON.
|
|
165
|
+
*/
|
|
166
|
+
function validateJsonAndGetSize(jsObject, skipSizingProcess = false) {
|
|
167
|
+
const seenObjects = new Set();
|
|
168
|
+
/**
|
|
169
|
+
* Checks whether a value is JSON serializable and counts the total number
|
|
170
|
+
* of bytes needed to store the serialized version of the value.
|
|
171
|
+
*
|
|
172
|
+
* This function assumes the encoding of the JSON is done in UTF-8.
|
|
173
|
+
*
|
|
174
|
+
* @param value - Potential JSON serializable value.
|
|
175
|
+
* @param skipSizing - Skip JSON size calculation (default: false).
|
|
176
|
+
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
|
|
177
|
+
* the value was serializable and a number of bytes that it will use when serialized to JSON.
|
|
178
|
+
*/
|
|
179
|
+
function getJsonSerializableInfo(value, skipSizing) {
|
|
180
|
+
if (value === undefined) {
|
|
181
|
+
// Return zero for undefined, since these are omitted from JSON serialization
|
|
182
|
+
return [true, 0];
|
|
183
|
+
}
|
|
184
|
+
else if (value === null) {
|
|
185
|
+
// Return already specified constant size for null (special object)
|
|
186
|
+
return [true, skipSizing ? 0 : misc_1.JsonSize.Null];
|
|
187
|
+
}
|
|
188
|
+
// Check and calculate sizes for basic (and some special) types
|
|
189
|
+
const typeOfValue = typeof value;
|
|
190
|
+
try {
|
|
191
|
+
if (typeOfValue === 'function') {
|
|
192
|
+
return [false, 0];
|
|
193
|
+
}
|
|
194
|
+
else if (typeOfValue === 'string' || value instanceof String) {
|
|
195
|
+
return [
|
|
196
|
+
true,
|
|
197
|
+
skipSizing
|
|
198
|
+
? 0
|
|
199
|
+
: misc_1.calculateStringSize(value) + misc_1.JsonSize.Quote * 2,
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
else if (typeOfValue === 'boolean' || value instanceof Boolean) {
|
|
203
|
+
if (skipSizing) {
|
|
204
|
+
return [true, 0];
|
|
205
|
+
}
|
|
206
|
+
// eslint-disable-next-line eqeqeq
|
|
207
|
+
return [true, value == true ? misc_1.JsonSize.True : misc_1.JsonSize.False];
|
|
208
|
+
}
|
|
209
|
+
else if (typeOfValue === 'number' || value instanceof Number) {
|
|
210
|
+
if (skipSizing) {
|
|
211
|
+
return [true, 0];
|
|
212
|
+
}
|
|
213
|
+
return [true, misc_1.calculateNumberSize(value)];
|
|
214
|
+
}
|
|
215
|
+
else if (value instanceof Date) {
|
|
216
|
+
if (skipSizing) {
|
|
217
|
+
return [true, 0];
|
|
218
|
+
}
|
|
219
|
+
return [
|
|
220
|
+
true,
|
|
221
|
+
// Note: Invalid dates will serialize to null
|
|
222
|
+
isNaN(value.getDate())
|
|
223
|
+
? misc_1.JsonSize.Null
|
|
224
|
+
: misc_1.JsonSize.Date + misc_1.JsonSize.Quote * 2,
|
|
225
|
+
];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
catch (_) {
|
|
229
|
+
return [false, 0];
|
|
230
|
+
}
|
|
231
|
+
// If object is not plain and cannot be serialized properly,
|
|
232
|
+
// stop here and return false for serialization
|
|
233
|
+
if (!misc_1.isPlainObject(value) && !Array.isArray(value)) {
|
|
234
|
+
return [false, 0];
|
|
235
|
+
}
|
|
236
|
+
// Circular object detection (handling)
|
|
237
|
+
// Check if the same object already exists
|
|
238
|
+
if (seenObjects.has(value)) {
|
|
239
|
+
return [false, 0];
|
|
240
|
+
}
|
|
241
|
+
// Add new object to the seen objects set
|
|
242
|
+
// Only the plain objects should be added (Primitive types are skipped)
|
|
243
|
+
seenObjects.add(value);
|
|
244
|
+
// Continue object decomposition
|
|
245
|
+
try {
|
|
246
|
+
return [
|
|
247
|
+
true,
|
|
248
|
+
Object.entries(value).reduce((sum, [key, nestedValue], idx, arr) => {
|
|
249
|
+
// Recursively process next nested object or primitive type
|
|
250
|
+
// eslint-disable-next-line prefer-const
|
|
251
|
+
let [valid, size] = getJsonSerializableInfo(nestedValue, skipSizing);
|
|
252
|
+
if (!valid) {
|
|
253
|
+
throw new Error('JSON validation did not pass. Validation process stopped.');
|
|
254
|
+
}
|
|
255
|
+
// Circular object detection
|
|
256
|
+
// Once a child node is visited and processed remove it from the set.
|
|
257
|
+
// This will prevent false positives with the same adjacent objects.
|
|
258
|
+
seenObjects.delete(value);
|
|
259
|
+
if (skipSizing) {
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
// If the size is 0, the value is undefined and undefined in an array
|
|
263
|
+
// when serialized will be replaced with null
|
|
264
|
+
if (size === 0 && Array.isArray(value)) {
|
|
265
|
+
size = misc_1.JsonSize.Null;
|
|
266
|
+
}
|
|
267
|
+
// If the size is 0, that means the object is undefined and
|
|
268
|
+
// the rest of the object structure will be omitted
|
|
269
|
+
if (size === 0) {
|
|
270
|
+
return sum;
|
|
271
|
+
}
|
|
272
|
+
// Objects will have be serialized with "key": value,
|
|
273
|
+
// therefore we include the key in the calculation here
|
|
274
|
+
const keySize = Array.isArray(value)
|
|
275
|
+
? 0
|
|
276
|
+
: key.length + misc_1.JsonSize.Comma + misc_1.JsonSize.Colon * 2;
|
|
277
|
+
const separator = idx < arr.length - 1 ? misc_1.JsonSize.Comma : 0;
|
|
278
|
+
return sum + keySize + size + separator;
|
|
279
|
+
},
|
|
280
|
+
// Starts at 2 because the serialized JSON string data (plain text)
|
|
281
|
+
// will minimally contain {}/[]
|
|
282
|
+
skipSizing ? 0 : misc_1.JsonSize.Wrapper * 2),
|
|
283
|
+
];
|
|
284
|
+
}
|
|
285
|
+
catch (_) {
|
|
286
|
+
return [false, 0];
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return getJsonSerializableInfo(jsObject, skipSizingProcess);
|
|
290
|
+
}
|
|
291
|
+
exports.validateJsonAndGetSize = validateJsonAndGetSize;
|
|
157
292
|
//# sourceMappingURL=json.js.map
|
package/dist/json.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,iCAAqC;AAarC;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,IAAI;QACF,OAAO,yBAAS,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5D;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAND,kCAMC;AAED;;GAEG;AACU,QAAA,QAAQ,GAAG,KAAc,CAAC;AAiDvC;;;;;;GAMG;AACH,SAAgB,qBAAqB,CACnC,qBAAiE;IAEjE,OAAO,CAAC,kBAAW,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAC;AACnD,CAAC;AAJD,sDAIC;AAED;;;;;GAKG;AACH,SAAgB,2BAA2B,CACzC,qBAAiE;IAEjE,IAAI,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,EAAE;QACjD,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;AACH,CAAC;AAND,kEAMC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAC9B,qBAAiE;IAEjE,OAAO,kBAAW,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAC;AAClD,CAAC;AAJD,4CAIC;AAED;;;;;GAKG;AACH,SAAgB,sBAAsB,CACpC,qBAAiE;IAEjE,IAAI,CAAC,gBAAgB,CAAC,qBAAqB,CAAC,EAAE;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CAAC,CAAC;YAChE,CAAC,UAAU,IAAI,EAAE,KAAK,IAAI,CAAC,CAC9B,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAvBD,sDAuBC","sourcesContent":["import deepEqual from 'fast-deep-equal';\nimport { hasProperty } from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * Type guard for {@link Json}.\n *\n * @param value - The value to check.\n * @returns Whether the value is valid JSON.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n return deepEqual(value, JSON.parse(JSON.stringify(value)));\n } catch (_) {\n return false;\n }\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = number | string | null;\n\n/**\n * A JSON-RPC error object.\n */\nexport type JsonRpcError = {\n code: number;\n message: string;\n data?: unknown;\n stack?: string;\n};\n\n/**\n * A JSON-RPC request object.\n *\n * @template Params - The type of the params.\n */\nexport type JsonRpcRequest<Params> = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * A JSON-RPC notification object.\n *\n * @template Params - The type of the params.\n */\nexport type JsonRpcNotification<Params> = {\n jsonrpc: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * Type guard to narrow a JSON-RPC request or notification object to a\n * notification.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n * @returns Whether the specified JSON-RPC message is a notification.\n */\nexport function isJsonRpcNotification<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): requestOrNotification is JsonRpcNotification<T> {\n return !hasProperty(requestOrNotification, 'id');\n}\n\n/**\n * Assertion type guard to narrow a JSON-RPC request or notification object to a\n * notification.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n */\nexport function assertIsJsonRpcNotification<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): asserts requestOrNotification is JsonRpcNotification<T> {\n if (!isJsonRpcNotification(requestOrNotification)) {\n throw new Error('Not a JSON-RPC notification.');\n }\n}\n\n/**\n * Type guard to narrow a JSON-RPC request or notification object to a request.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n * @returns Whether the specified JSON-RPC message is a request.\n */\nexport function isJsonRpcRequest<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): requestOrNotification is JsonRpcRequest<T> {\n return hasProperty(requestOrNotification, 'id');\n}\n\n/**\n * Assertion type guard to narrow a JSON-RPC request or notification object to a\n * request.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n */\nexport function assertIsJsonRpcRequest<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): asserts requestOrNotification is JsonRpcRequest<T> {\n if (!isJsonRpcRequest(requestOrNotification)) {\n throw new Error('Not a JSON-RPC request.');\n }\n}\n\n/**\n * A successful JSON-RPC response object.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcSuccess<Result = unknown> = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n result: Result;\n};\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n error: JsonRpcError;\n};\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result = unknown> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to narrow a JsonRpcResponse object to a success (or failure).\n *\n * @param response - The response object to check.\n * @returns Whether the response object is a success, i.e. has a `result`\n * property.\n */\nexport function isJsonRpcSuccess<Result>(\n response: JsonRpcResponse<Result>,\n): response is JsonRpcSuccess<Result> {\n return hasProperty(response, 'result');\n}\n\n/**\n * Type assertion to narrow a JsonRpcResponse object to a success (or failure).\n *\n * @param response - The response object to check.\n */\nexport function assertIsJsonRpcSuccess<T>(\n response: JsonRpcResponse<T>,\n): asserts response is JsonRpcSuccess<T> {\n if (!isJsonRpcSuccess(response)) {\n throw new Error('Not a successful JSON-RPC response.');\n }\n}\n\n/**\n * Type guard to narrow a JsonRpcResponse object to a failure (or success).\n *\n * @param response - The response object to check.\n * @returns Whether the response object is a failure, i.e. has an `error`\n * property.\n */\nexport function isJsonRpcFailure(\n response: JsonRpcResponse<unknown>,\n): response is JsonRpcFailure {\n return hasProperty(response, 'error');\n}\n\n/**\n * Type assertion to narrow a JsonRpcResponse object to a failure (or success).\n *\n * @param response - The response object to check.\n */\nexport function assertIsJsonRpcFailure(\n response: JsonRpcResponse<unknown>,\n): asserts response is JsonRpcFailure {\n if (!isJsonRpcFailure(response)) {\n throw new Error('Not a failed JSON-RPC response.');\n }\n}\n\ntype JsonRpcValidatorOptions = {\n permitEmptyString?: boolean;\n permitFractions?: boolean;\n permitNull?: boolean;\n};\n\n/**\n * Gets a function for validating JSON-RPC request / response `id` values.\n *\n * By manipulating the options of this factory, you can control the behavior\n * of the resulting validator for some edge cases. This is useful because e.g.\n * `null` should sometimes but not always be permitted.\n *\n * Note that the empty string (`''`) is always permitted by the JSON-RPC\n * specification, but that kind of sucks and you may want to forbid it in some\n * instances anyway.\n *\n * For more details, see the\n * [JSON-RPC Specification](https://www.jsonrpc.org/specification).\n *\n * @param options - An options object.\n * @param options.permitEmptyString - Whether the empty string (i.e. `''`)\n * should be treated as a valid ID. Default: `true`\n * @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)\n * should be treated as valid IDs. Default: `false`\n * @param options.permitNull - Whether `null` should be treated as a valid ID.\n * Default: `true`\n * @returns The JSON-RPC ID validator function.\n */\nexport function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {\n const { permitEmptyString, permitFractions, permitNull } = {\n permitEmptyString: true,\n permitFractions: false,\n permitNull: true,\n ...options,\n };\n\n /**\n * Type guard for {@link JsonRpcId}.\n *\n * @param id - The JSON-RPC ID value to check.\n * @returns Whether the given ID is valid per the options given to the\n * factory.\n */\n const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {\n return Boolean(\n (typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||\n (typeof id === 'string' && (permitEmptyString || id.length > 0)) ||\n (permitNull && id === null),\n );\n };\n return isValidJsonRpcId;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,iCAMgB;AAahB;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,IAAI;QACF,OAAO,yBAAS,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5D;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAND,kCAMC;AAED;;GAEG;AACU,QAAA,QAAQ,GAAG,KAAc,CAAC;AAiDvC;;;;;;GAMG;AACH,SAAgB,qBAAqB,CACnC,qBAAiE;IAEjE,OAAO,CAAC,kBAAW,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAC;AACnD,CAAC;AAJD,sDAIC;AAED;;;;;GAKG;AACH,SAAgB,2BAA2B,CACzC,qBAAiE;IAEjE,IAAI,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,EAAE;QACjD,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;AACH,CAAC;AAND,kEAMC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAC9B,qBAAiE;IA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AC,CAAC;oBAE5D,OAAO,GAAG,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;gBAC1C,CAAC;gBACD,mEAAmE;gBACnE,+BAA+B;gBAC/B,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,eAAQ,CAAC,OAAO,GAAG,CAAC,CACtC;aACF,CAAC;SACH;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;SACnB;IACH,CAAC;IAED,OAAO,uBAAuB,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC;AAC9D,CAAC;AA9ID,wDA8IC","sourcesContent":["import deepEqual from 'fast-deep-equal';\nimport {\n calculateNumberSize,\n calculateStringSize,\n hasProperty,\n isPlainObject,\n JsonSize,\n} from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * Type guard for {@link Json}.\n *\n * @param value - The value to check.\n * @returns Whether the value is valid JSON.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n return deepEqual(value, JSON.parse(JSON.stringify(value)));\n } catch (_) {\n return false;\n }\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = number | string | null;\n\n/**\n * A JSON-RPC error object.\n */\nexport type JsonRpcError = {\n code: number;\n message: string;\n data?: unknown;\n stack?: string;\n};\n\n/**\n * A JSON-RPC request object.\n *\n * @template Params - The type of the params.\n */\nexport type JsonRpcRequest<Params> = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * A JSON-RPC notification object.\n *\n * @template Params - The type of the params.\n */\nexport type JsonRpcNotification<Params> = {\n jsonrpc: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * Type guard to narrow a JSON-RPC request or notification object to a\n * notification.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n * @returns Whether the specified JSON-RPC message is a notification.\n */\nexport function isJsonRpcNotification<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): requestOrNotification is JsonRpcNotification<T> {\n return !hasProperty(requestOrNotification, 'id');\n}\n\n/**\n * Assertion type guard to narrow a JSON-RPC request or notification object to a\n * notification.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n */\nexport function assertIsJsonRpcNotification<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): asserts requestOrNotification is JsonRpcNotification<T> {\n if (!isJsonRpcNotification(requestOrNotification)) {\n throw new Error('Not a JSON-RPC notification.');\n }\n}\n\n/**\n * Type guard to narrow a JSON-RPC request or notification object to a request.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n * @returns Whether the specified JSON-RPC message is a request.\n */\nexport function isJsonRpcRequest<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): requestOrNotification is JsonRpcRequest<T> {\n return hasProperty(requestOrNotification, 'id');\n}\n\n/**\n * Assertion type guard to narrow a JSON-RPC request or notification object to a\n * request.\n *\n * @param requestOrNotification - The JSON-RPC request or notification to check.\n */\nexport function assertIsJsonRpcRequest<T>(\n requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>,\n): asserts requestOrNotification is JsonRpcRequest<T> {\n if (!isJsonRpcRequest(requestOrNotification)) {\n throw new Error('Not a JSON-RPC request.');\n }\n}\n\n/**\n * A successful JSON-RPC response object.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcSuccess<Result = unknown> = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n result: Result;\n};\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n error: JsonRpcError;\n};\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result = unknown> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to narrow a JsonRpcResponse object to a success (or failure).\n *\n * @param response - The response object to check.\n * @returns Whether the response object is a success, i.e. has a `result`\n * property.\n */\nexport function isJsonRpcSuccess<Result>(\n response: JsonRpcResponse<Result>,\n): response is JsonRpcSuccess<Result> {\n return hasProperty(response, 'result');\n}\n\n/**\n * Type assertion to narrow a JsonRpcResponse object to a success (or failure).\n *\n * @param response - The response object to check.\n */\nexport function assertIsJsonRpcSuccess<T>(\n response: JsonRpcResponse<T>,\n): asserts response is JsonRpcSuccess<T> {\n if (!isJsonRpcSuccess(response)) {\n throw new Error('Not a successful JSON-RPC response.');\n }\n}\n\n/**\n * Type guard to narrow a JsonRpcResponse object to a failure (or success).\n *\n * @param response - The response object to check.\n * @returns Whether the response object is a failure, i.e. has an `error`\n * property.\n */\nexport function isJsonRpcFailure(\n response: JsonRpcResponse<unknown>,\n): response is JsonRpcFailure {\n return hasProperty(response, 'error');\n}\n\n/**\n * Type assertion to narrow a JsonRpcResponse object to a failure (or success).\n *\n * @param response - The response object to check.\n */\nexport function assertIsJsonRpcFailure(\n response: JsonRpcResponse<unknown>,\n): asserts response is JsonRpcFailure {\n if (!isJsonRpcFailure(response)) {\n throw new Error('Not a failed JSON-RPC response.');\n }\n}\n\ntype JsonRpcValidatorOptions = {\n permitEmptyString?: boolean;\n permitFractions?: boolean;\n permitNull?: boolean;\n};\n\n/**\n * Gets a function for validating JSON-RPC request / response `id` values.\n *\n * By manipulating the options of this factory, you can control the behavior\n * of the resulting validator for some edge cases. This is useful because e.g.\n * `null` should sometimes but not always be permitted.\n *\n * Note that the empty string (`''`) is always permitted by the JSON-RPC\n * specification, but that kind of sucks and you may want to forbid it in some\n * instances anyway.\n *\n * For more details, see the\n * [JSON-RPC Specification](https://www.jsonrpc.org/specification).\n *\n * @param options - An options object.\n * @param options.permitEmptyString - Whether the empty string (i.e. `''`)\n * should be treated as a valid ID. Default: `true`\n * @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)\n * should be treated as valid IDs. Default: `false`\n * @param options.permitNull - Whether `null` should be treated as a valid ID.\n * Default: `true`\n * @returns The JSON-RPC ID validator function.\n */\nexport function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {\n const { permitEmptyString, permitFractions, permitNull } = {\n permitEmptyString: true,\n permitFractions: false,\n permitNull: true,\n ...options,\n };\n\n /**\n * Type guard for {@link JsonRpcId}.\n *\n * @param id - The JSON-RPC ID value to check.\n * @returns Whether the given ID is valid per the options given to the\n * factory.\n */\n const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {\n return Boolean(\n (typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||\n (typeof id === 'string' && (permitEmptyString || id.length > 0)) ||\n (permitNull && id === null),\n );\n };\n return isValidJsonRpcId;\n}\n\n/**\n * Checks whether a value is JSON serializable and counts the total number\n * of bytes needed to store the serialized version of the value.\n *\n * @param jsObject - Potential JSON serializable object.\n * @param skipSizingProcess - Skip JSON size calculation (default: false).\n * @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether\n * the value was serializable and a number of bytes that it will use when serialized to JSON.\n */\nexport function validateJsonAndGetSize(\n jsObject: unknown,\n skipSizingProcess = false,\n): [isValid: boolean, plainTextSizeInBytes: number] {\n const seenObjects = new Set();\n /**\n * Checks whether a value is JSON serializable and counts the total number\n * of bytes needed to store the serialized version of the value.\n *\n * This function assumes the encoding of the JSON is done in UTF-8.\n *\n * @param value - Potential JSON serializable value.\n * @param skipSizing - Skip JSON size calculation (default: false).\n * @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether\n * the value was serializable and a number of bytes that it will use when serialized to JSON.\n */\n function getJsonSerializableInfo(\n value: unknown,\n skipSizing: boolean,\n ): [isValid: boolean, plainTextSizeInBytes: number] {\n if (value === undefined) {\n // Return zero for undefined, since these are omitted from JSON serialization\n return [true, 0];\n } else if (value === null) {\n // Return already specified constant size for null (special object)\n return [true, skipSizing ? 0 : JsonSize.Null];\n }\n\n // Check and calculate sizes for basic (and some special) types\n const typeOfValue = typeof value;\n try {\n if (typeOfValue === 'function') {\n return [false, 0];\n } else if (typeOfValue === 'string' || value instanceof String) {\n return [\n true,\n skipSizing\n ? 0\n : calculateStringSize(value as string) + JsonSize.Quote * 2,\n ];\n } else if (typeOfValue === 'boolean' || value instanceof Boolean) {\n if (skipSizing) {\n return [true, 0];\n }\n // eslint-disable-next-line eqeqeq\n return [true, value == true ? JsonSize.True : JsonSize.False];\n } else if (typeOfValue === 'number' || value instanceof Number) {\n if (skipSizing) {\n return [true, 0];\n }\n return [true, calculateNumberSize(value as number)];\n } else if (value instanceof Date) {\n if (skipSizing) {\n return [true, 0];\n }\n return [\n true,\n // Note: Invalid dates will serialize to null\n isNaN(value.getDate())\n ? JsonSize.Null\n : JsonSize.Date + JsonSize.Quote * 2,\n ];\n }\n } catch (_) {\n return [false, 0];\n }\n\n // If object is not plain and cannot be serialized properly,\n // stop here and return false for serialization\n if (!isPlainObject(value) && !Array.isArray(value)) {\n return [false, 0];\n }\n\n // Circular object detection (handling)\n // Check if the same object already exists\n if (seenObjects.has(value)) {\n return [false, 0];\n }\n // Add new object to the seen objects set\n // Only the plain objects should be added (Primitive types are skipped)\n seenObjects.add(value);\n\n // Continue object decomposition\n try {\n return [\n true,\n Object.entries(value).reduce(\n (sum, [key, nestedValue], idx, arr) => {\n // Recursively process next nested object or primitive type\n // eslint-disable-next-line prefer-const\n let [valid, size] = getJsonSerializableInfo(\n nestedValue,\n skipSizing,\n );\n if (!valid) {\n throw new Error(\n 'JSON validation did not pass. Validation process stopped.',\n );\n }\n\n // Circular object detection\n // Once a child node is visited and processed remove it from the set.\n // This will prevent false positives with the same adjacent objects.\n seenObjects.delete(value);\n\n if (skipSizing) {\n return 0;\n }\n\n // If the size is 0, the value is undefined and undefined in an array\n // when serialized will be replaced with null\n if (size === 0 && Array.isArray(value)) {\n size = JsonSize.Null;\n }\n\n // If the size is 0, that means the object is undefined and\n // the rest of the object structure will be omitted\n if (size === 0) {\n return sum;\n }\n\n // Objects will have be serialized with \"key\": value,\n // therefore we include the key in the calculation here\n const keySize = Array.isArray(value)\n ? 0\n : key.length + JsonSize.Comma + JsonSize.Colon * 2;\n\n const separator = idx < arr.length - 1 ? JsonSize.Comma : 0;\n\n return sum + keySize + size + separator;\n },\n // Starts at 2 because the serialized JSON string data (plain text)\n // will minimally contain {}/[]\n skipSizing ? 0 : JsonSize.Wrapper * 2,\n ),\n ];\n } catch (_) {\n return [false, 0];\n }\n }\n\n return getJsonSerializableInfo(jsObject, skipSizingProcess);\n}\n"]}
|
package/dist/misc.d.ts
CHANGED
|
@@ -58,3 +58,50 @@ export declare function isObject(value: unknown): value is RuntimeObject;
|
|
|
58
58
|
* name, regardless of whether it is enumerable or not.
|
|
59
59
|
*/
|
|
60
60
|
export declare const hasProperty: (object: RuntimeObject, name: string | number | symbol) => boolean;
|
|
61
|
+
export declare type PlainObject = Record<number | string | symbol, unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* Predefined sizes (in Bytes) of specific parts of JSON structure.
|
|
64
|
+
*/
|
|
65
|
+
export declare enum JsonSize {
|
|
66
|
+
Null = 4,
|
|
67
|
+
Comma = 1,
|
|
68
|
+
Wrapper = 1,
|
|
69
|
+
True = 4,
|
|
70
|
+
False = 5,
|
|
71
|
+
Quote = 1,
|
|
72
|
+
Colon = 1,
|
|
73
|
+
Date = 24
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Regular expression with pattern matching for (special) escaped characters.
|
|
77
|
+
*/
|
|
78
|
+
export declare const ESCAPE_CHARACTERS_REGEXP: RegExp;
|
|
79
|
+
/**
|
|
80
|
+
* Check if the value is plain object.
|
|
81
|
+
*
|
|
82
|
+
* @param value - Value to be checked.
|
|
83
|
+
* @returns True if an object is the plain JavaScript object,
|
|
84
|
+
* false if the object is not plain (e.g. function).
|
|
85
|
+
*/
|
|
86
|
+
export declare function isPlainObject(value: unknown): value is PlainObject;
|
|
87
|
+
/**
|
|
88
|
+
* Check if character is ASCII.
|
|
89
|
+
*
|
|
90
|
+
* @param character - Character.
|
|
91
|
+
* @returns True if a character code is ASCII, false if not.
|
|
92
|
+
*/
|
|
93
|
+
export declare function isASCII(character: string): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Calculate string size.
|
|
96
|
+
*
|
|
97
|
+
* @param value - String value to calculate size.
|
|
98
|
+
* @returns Number of bytes used to store whole string value.
|
|
99
|
+
*/
|
|
100
|
+
export declare function calculateStringSize(value: string): number;
|
|
101
|
+
/**
|
|
102
|
+
* Calculate size of a number ofter JSON serialization.
|
|
103
|
+
*
|
|
104
|
+
* @param value - Number value to calculate size.
|
|
105
|
+
* @returns Number of bytes used to store whole number in JSON.
|
|
106
|
+
*/
|
|
107
|
+
export declare function calculateNumberSize(value: number): number;
|
package/dist/misc.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Types
|
|
4
4
|
//
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.hasProperty = exports.isObject = exports.isNullOrUndefined = exports.isNonEmptyArray = void 0;
|
|
6
|
+
exports.calculateNumberSize = exports.calculateStringSize = exports.isASCII = exports.isPlainObject = exports.ESCAPE_CHARACTERS_REGEXP = exports.JsonSize = exports.hasProperty = exports.isObject = exports.isNullOrUndefined = exports.isNonEmptyArray = void 0;
|
|
7
7
|
//
|
|
8
8
|
// Type Guards
|
|
9
9
|
//
|
|
@@ -52,4 +52,83 @@ exports.isObject = isObject;
|
|
|
52
52
|
*/
|
|
53
53
|
const hasProperty = (object, name) => Object.hasOwnProperty.call(object, name);
|
|
54
54
|
exports.hasProperty = hasProperty;
|
|
55
|
+
/**
|
|
56
|
+
* Predefined sizes (in Bytes) of specific parts of JSON structure.
|
|
57
|
+
*/
|
|
58
|
+
var JsonSize;
|
|
59
|
+
(function (JsonSize) {
|
|
60
|
+
JsonSize[JsonSize["Null"] = 4] = "Null";
|
|
61
|
+
JsonSize[JsonSize["Comma"] = 1] = "Comma";
|
|
62
|
+
JsonSize[JsonSize["Wrapper"] = 1] = "Wrapper";
|
|
63
|
+
JsonSize[JsonSize["True"] = 4] = "True";
|
|
64
|
+
JsonSize[JsonSize["False"] = 5] = "False";
|
|
65
|
+
JsonSize[JsonSize["Quote"] = 1] = "Quote";
|
|
66
|
+
JsonSize[JsonSize["Colon"] = 1] = "Colon";
|
|
67
|
+
JsonSize[JsonSize["Date"] = 24] = "Date";
|
|
68
|
+
})(JsonSize = exports.JsonSize || (exports.JsonSize = {}));
|
|
69
|
+
/**
|
|
70
|
+
* Regular expression with pattern matching for (special) escaped characters.
|
|
71
|
+
*/
|
|
72
|
+
exports.ESCAPE_CHARACTERS_REGEXP = /"|\\|\n|\r|\t/gu;
|
|
73
|
+
/**
|
|
74
|
+
* Check if the value is plain object.
|
|
75
|
+
*
|
|
76
|
+
* @param value - Value to be checked.
|
|
77
|
+
* @returns True if an object is the plain JavaScript object,
|
|
78
|
+
* false if the object is not plain (e.g. function).
|
|
79
|
+
*/
|
|
80
|
+
function isPlainObject(value) {
|
|
81
|
+
if (typeof value !== 'object' || value === null) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
let proto = value;
|
|
86
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
87
|
+
proto = Object.getPrototypeOf(proto);
|
|
88
|
+
}
|
|
89
|
+
return Object.getPrototypeOf(value) === proto;
|
|
90
|
+
}
|
|
91
|
+
catch (_) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.isPlainObject = isPlainObject;
|
|
96
|
+
/**
|
|
97
|
+
* Check if character is ASCII.
|
|
98
|
+
*
|
|
99
|
+
* @param character - Character.
|
|
100
|
+
* @returns True if a character code is ASCII, false if not.
|
|
101
|
+
*/
|
|
102
|
+
function isASCII(character) {
|
|
103
|
+
return character.charCodeAt(0) <= 127;
|
|
104
|
+
}
|
|
105
|
+
exports.isASCII = isASCII;
|
|
106
|
+
/**
|
|
107
|
+
* Calculate string size.
|
|
108
|
+
*
|
|
109
|
+
* @param value - String value to calculate size.
|
|
110
|
+
* @returns Number of bytes used to store whole string value.
|
|
111
|
+
*/
|
|
112
|
+
function calculateStringSize(value) {
|
|
113
|
+
var _a;
|
|
114
|
+
const size = value.split('').reduce((total, character) => {
|
|
115
|
+
if (isASCII(character)) {
|
|
116
|
+
return total + 1;
|
|
117
|
+
}
|
|
118
|
+
return total + 2;
|
|
119
|
+
}, 0);
|
|
120
|
+
// Also detect characters that need backslash escape
|
|
121
|
+
return size + ((_a = value.match(exports.ESCAPE_CHARACTERS_REGEXP)) !== null && _a !== void 0 ? _a : []).length;
|
|
122
|
+
}
|
|
123
|
+
exports.calculateStringSize = calculateStringSize;
|
|
124
|
+
/**
|
|
125
|
+
* Calculate size of a number ofter JSON serialization.
|
|
126
|
+
*
|
|
127
|
+
* @param value - Number value to calculate size.
|
|
128
|
+
* @returns Number of bytes used to store whole number in JSON.
|
|
129
|
+
*/
|
|
130
|
+
function calculateNumberSize(value) {
|
|
131
|
+
return value.toString().length;
|
|
132
|
+
}
|
|
133
|
+
exports.calculateNumberSize = calculateNumberSize;
|
|
55
134
|
//# sourceMappingURL=misc.js.map
|
package/dist/misc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,QAAQ;AACR,EAAE;;;AAsCF,EAAE;AACF,cAAc;AACd,EAAE;AAEF;;;;;;GAMG;AACH,SAAgB,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC;AAJD,0CAIC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAFD,4BAEC;AAED,EAAE;AACF,0BAA0B;AAC1B,EAAE;AAEF;;;;;;;GAOG;AACI,MAAM,WAAW,GAAG,CACzB,MAAqB,EACrB,IAA8B,EACrB,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAH1C,QAAA,WAAW,eAG+B","sourcesContent":["//\n// Types\n//\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} &\n {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n };\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A JavaScript object that is not `null`, a function, or an array. The object\n * can still be an instance of a class.\n */\nexport type RuntimeObject = Record<number | string | symbol, unknown>;\n\n//\n// Type Guards\n//\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n//\n// Other utility functions\n//\n\n/**\n * An alias for {@link Object.hasOwnProperty}.\n *\n * @param object - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = (\n object: RuntimeObject,\n name: string | number | symbol,\n): boolean => Object.hasOwnProperty.call(object, name);\n"]}
|
|
1
|
+
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,QAAQ;AACR,EAAE;;;AAsCF,EAAE;AACF,cAAc;AACd,EAAE;AAEF;;;;;;GAMG;AACH,SAAgB,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC;AAJD,0CAIC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,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AC,UAAU,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;AACxC,CAAC;AAFD,0BAEC;AAED;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,KAAa;;IAC/C,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;QACvD,IAAI,OAAO,CAAC,SAAS,CAAC,EAAE;YACtB,OAAO,KAAK,GAAG,CAAC,CAAC;SAClB;QACD,OAAO,KAAK,GAAG,CAAC,CAAC;IACnB,CAAC,EAAE,CAAC,CAAC,CAAC;IAEN,oDAAoD;IACpD,OAAO,IAAI,GAAG,CAAC,MAAA,KAAK,CAAC,KAAK,CAAC,gCAAwB,CAAC,mCAAI,EAAE,CAAC,CAAC,MAAM,CAAC;AACrE,CAAC;AAVD,kDAUC;AAED;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,KAAa;IAC/C,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC;AACjC,CAAC;AAFD,kDAEC","sourcesContent":["//\n// Types\n//\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} &\n {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n };\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A JavaScript object that is not `null`, a function, or an array. The object\n * can still be an instance of a class.\n */\nexport type RuntimeObject = Record<number | string | symbol, unknown>;\n\n//\n// Type Guards\n//\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n//\n// Other utility functions\n//\n\n/**\n * An alias for {@link Object.hasOwnProperty}.\n *\n * @param object - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = (\n object: RuntimeObject,\n name: string | number | symbol,\n): boolean => Object.hasOwnProperty.call(object, name);\n\nexport type PlainObject = Record<number | string | symbol, unknown>;\n\n/**\n * Predefined sizes (in Bytes) of specific parts of JSON structure.\n */\nexport enum JsonSize {\n Null = 4,\n Comma = 1,\n Wrapper = 1,\n True = 4,\n False = 5,\n Quote = 1,\n Colon = 1,\n Date = 24,\n}\n\n/**\n * Regular expression with pattern matching for (special) escaped characters.\n */\nexport const ESCAPE_CHARACTERS_REGEXP = /\"|\\\\|\\n|\\r|\\t/gu;\n\n/**\n * Check if the value is plain object.\n *\n * @param value - Value to be checked.\n * @returns True if an object is the plain JavaScript object,\n * false if the object is not plain (e.g. function).\n */\nexport function isPlainObject(value: unknown): value is PlainObject {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n try {\n let proto = value;\n while (Object.getPrototypeOf(proto) !== null) {\n proto = Object.getPrototypeOf(proto);\n }\n\n return Object.getPrototypeOf(value) === proto;\n } catch (_) {\n return false;\n }\n}\n\n/**\n * Check if character is ASCII.\n *\n * @param character - Character.\n * @returns True if a character code is ASCII, false if not.\n */\nexport function isASCII(character: string) {\n return character.charCodeAt(0) <= 127;\n}\n\n/**\n * Calculate string size.\n *\n * @param value - String value to calculate size.\n * @returns Number of bytes used to store whole string value.\n */\nexport function calculateStringSize(value: string): number {\n const size = value.split('').reduce((total, character) => {\n if (isASCII(character)) {\n return total + 1;\n }\n return total + 2;\n }, 0);\n\n // Also detect characters that need backslash escape\n return size + (value.match(ESCAPE_CHARACTERS_REGEXP) ?? []).length;\n}\n\n/**\n * Calculate size of a number ofter JSON serialization.\n *\n * @param value - Number value to calculate size.\n * @returns Number of bytes used to store whole number in JSON.\n */\nexport function calculateNumberSize(value: number): number {\n return value.toString().length;\n}\n"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export declare const complexObject: {
|
|
2
|
+
data: {
|
|
3
|
+
account: {
|
|
4
|
+
__typename: string;
|
|
5
|
+
registrations: {
|
|
6
|
+
__typename: string;
|
|
7
|
+
domain: {
|
|
8
|
+
__typename: string;
|
|
9
|
+
isMigrated: boolean;
|
|
10
|
+
labelName: string;
|
|
11
|
+
labelhash: string;
|
|
12
|
+
name: string;
|
|
13
|
+
parent: {
|
|
14
|
+
__typename: string;
|
|
15
|
+
name: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
expiryDate: string;
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
moreComplexity: {
|
|
22
|
+
numbers: number[];
|
|
23
|
+
moreNestedObjects: {
|
|
24
|
+
nestedAgain: {
|
|
25
|
+
nestedAgain: {
|
|
26
|
+
andAgain: {
|
|
27
|
+
andAgain: {
|
|
28
|
+
value: boolean;
|
|
29
|
+
again: {
|
|
30
|
+
value: boolean;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
differentEncodings: {
|
|
38
|
+
ascii: string;
|
|
39
|
+
utf8: string;
|
|
40
|
+
mixed: string;
|
|
41
|
+
specialCharacters: string;
|
|
42
|
+
stringWithSpecialEscapedCharacters: string;
|
|
43
|
+
};
|
|
44
|
+
specialObjectsTypesAndValues: {
|
|
45
|
+
t: boolean[];
|
|
46
|
+
f: boolean[];
|
|
47
|
+
nulls: null[];
|
|
48
|
+
undef: undefined;
|
|
49
|
+
mixed: (boolean | null | undefined)[];
|
|
50
|
+
inObject: {
|
|
51
|
+
valueOne: null;
|
|
52
|
+
valueTwo: undefined;
|
|
53
|
+
t: boolean;
|
|
54
|
+
f: boolean;
|
|
55
|
+
};
|
|
56
|
+
dates: {
|
|
57
|
+
someDate: Date;
|
|
58
|
+
someOther: Date;
|
|
59
|
+
invalidDate: Date;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export declare const nonSerializableNestedObject: {
|
|
66
|
+
levelOne: {
|
|
67
|
+
levelTwo: {
|
|
68
|
+
levelThree: {
|
|
69
|
+
levelFour: {
|
|
70
|
+
levelFive: () => string;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
export declare const arrayOfDifferentKindsOfNumbers: number[];
|
|
77
|
+
export declare const arrayOfMixedSpecialObjects: (null | undefined)[];
|
|
78
|
+
export declare const objectMixedWithUndefinedValues: {
|
|
79
|
+
a: undefined;
|
|
80
|
+
b: string;
|
|
81
|
+
c: undefined;
|
|
82
|
+
d: string;
|
|
83
|
+
e: undefined;
|
|
84
|
+
f: string;
|
|
85
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.objectMixedWithUndefinedValues = exports.arrayOfMixedSpecialObjects = exports.arrayOfDifferentKindsOfNumbers = exports.nonSerializableNestedObject = exports.complexObject = void 0;
|
|
4
|
+
exports.complexObject = {
|
|
5
|
+
data: {
|
|
6
|
+
account: {
|
|
7
|
+
__typename: 'Account',
|
|
8
|
+
registrations: [
|
|
9
|
+
{
|
|
10
|
+
__typename: 'Registration',
|
|
11
|
+
domain: {
|
|
12
|
+
__typename: 'Domain',
|
|
13
|
+
isMigrated: true,
|
|
14
|
+
labelName: 'mycrypto',
|
|
15
|
+
labelhash: '0x9a781ca0d227debc3ee76d547c960b0803a6c9f58c6d3b4722f12ede7e6ef7c9',
|
|
16
|
+
name: 'mycrypto.eth',
|
|
17
|
+
parent: { __typename: 'Domain', name: 'eth' },
|
|
18
|
+
},
|
|
19
|
+
expiryDate: '1754111315',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
moreComplexity: {
|
|
24
|
+
numbers: [
|
|
25
|
+
-5e-11,
|
|
26
|
+
5e-9,
|
|
27
|
+
0.000000000001,
|
|
28
|
+
-0.00000000009,
|
|
29
|
+
100000.00000008,
|
|
30
|
+
-100.88888,
|
|
31
|
+
0.333,
|
|
32
|
+
1000000000000,
|
|
33
|
+
],
|
|
34
|
+
moreNestedObjects: {
|
|
35
|
+
nestedAgain: {
|
|
36
|
+
nestedAgain: {
|
|
37
|
+
andAgain: {
|
|
38
|
+
andAgain: {
|
|
39
|
+
value: true,
|
|
40
|
+
again: {
|
|
41
|
+
value: false,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
differentEncodings: {
|
|
49
|
+
ascii: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
|
|
50
|
+
utf8: 'šđćčžЀЂЇЄЖФћΣΩδλ',
|
|
51
|
+
mixed: 'ABCDEFGHIJ KLMNOPQRST UVWXYZšđćč žЀЂЇЄЖФћΣΩδλ',
|
|
52
|
+
specialCharacters: '"\\\n\r\t',
|
|
53
|
+
stringWithSpecialEscapedCharacters: "this\nis\nsome\"'string\r\nwith\tspecial\\escaped\tcharacters'",
|
|
54
|
+
},
|
|
55
|
+
specialObjectsTypesAndValues: {
|
|
56
|
+
t: [true, true, true],
|
|
57
|
+
f: [false, false, false],
|
|
58
|
+
nulls: [null, null, null],
|
|
59
|
+
undef: undefined,
|
|
60
|
+
mixed: [
|
|
61
|
+
null,
|
|
62
|
+
undefined,
|
|
63
|
+
null,
|
|
64
|
+
undefined,
|
|
65
|
+
null,
|
|
66
|
+
true,
|
|
67
|
+
null,
|
|
68
|
+
false,
|
|
69
|
+
null,
|
|
70
|
+
undefined,
|
|
71
|
+
],
|
|
72
|
+
inObject: {
|
|
73
|
+
valueOne: null,
|
|
74
|
+
valueTwo: undefined,
|
|
75
|
+
t: true,
|
|
76
|
+
f: false,
|
|
77
|
+
},
|
|
78
|
+
dates: {
|
|
79
|
+
someDate: new Date(),
|
|
80
|
+
someOther: new Date(2022, 0, 2, 15, 4, 5),
|
|
81
|
+
invalidDate: new Date('bad-date-format'),
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
exports.nonSerializableNestedObject = {
|
|
88
|
+
levelOne: {
|
|
89
|
+
levelTwo: {
|
|
90
|
+
levelThree: {
|
|
91
|
+
levelFour: {
|
|
92
|
+
levelFive: () => {
|
|
93
|
+
return 'anything';
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
exports.arrayOfDifferentKindsOfNumbers = [
|
|
101
|
+
-5e-11,
|
|
102
|
+
5e-9,
|
|
103
|
+
0.000000000001,
|
|
104
|
+
-0.00000000009,
|
|
105
|
+
100000.00000008,
|
|
106
|
+
-100.88888,
|
|
107
|
+
0.333,
|
|
108
|
+
1000000000000,
|
|
109
|
+
];
|
|
110
|
+
exports.arrayOfMixedSpecialObjects = [
|
|
111
|
+
null,
|
|
112
|
+
undefined,
|
|
113
|
+
null,
|
|
114
|
+
undefined,
|
|
115
|
+
undefined,
|
|
116
|
+
undefined,
|
|
117
|
+
null,
|
|
118
|
+
null,
|
|
119
|
+
null,
|
|
120
|
+
undefined,
|
|
121
|
+
];
|
|
122
|
+
exports.objectMixedWithUndefinedValues = {
|
|
123
|
+
a: undefined,
|
|
124
|
+
b: 'b',
|
|
125
|
+
c: undefined,
|
|
126
|
+
d: 'd',
|
|
127
|
+
e: undefined,
|
|
128
|
+
f: 'f',
|
|
129
|
+
};
|
|
130
|
+
//# sourceMappingURL=test.data.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.data.js","sourceRoot":"","sources":["../src/test.data.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG;IAC3B,IAAI,EAAE;QACJ,OAAO,EAAE;YACP,UAAU,EAAE,SAAS;YACrB,aAAa,EAAE;gBACb;oBACE,UAAU,EAAE,cAAc;oBAC1B,MAAM,EAAE;wBACN,UAAU,EAAE,QAAQ;wBACpB,UAAU,EAAE,IAAI;wBAChB,SAAS,EAAE,UAAU;wBACrB,SAAS,EACP,oEAAoE;wBACtE,IAAI,EAAE,cAAc;wBACpB,MAAM,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE;qBAC9C;oBACD,UAAU,EAAE,YAAY;iBACzB;aACF;SACF;QACD,cAAc,EAAE;YACd,OAAO,EAAE;gBACP,CAAC,KAAK;gBACN,IAAI;gBACJ,cAAc;gBACd,CAAC,aAAa;gBACd,eAAe;gBACf,CAAC,SAAS;gBACV,KAAK;gBACL,aAAa;aACd;YACD,iBAAiB,EAAE;gBACjB,WAAW,EAAE;oBACX,WAAW,EAAE;wBACX,QAAQ,EAAE;4BACR,QAAQ,EAAE;gCACR,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8B,GAAG;IAC5C,CAAC,KAAK;IACN,IAAI;IACJ,cAAc;IACd,CAAC,aAAa;IACd,eAAe;IACf,CAAC,SAAS;IACV,KAAK;IACL,aAAa;CACd,CAAC;AAEW,QAAA,0BAA0B,GAAG;IACxC,IAAI;IACJ,SAAS;IACT,IAAI;IACJ,SAAS;IACT,SAAS;IACT,SAAS;IACT,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,SAAS;CACV,CAAC;AAEW,QAAA,8BAA8B,GAAG;IAC5C,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,GAAG;IACN,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,GAAG;IACN,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,GAAG;CACP,CAAC","sourcesContent":["export const complexObject = {\n data: {\n account: {\n __typename: 'Account',\n registrations: [\n {\n __typename: 'Registration',\n domain: {\n __typename: 'Domain',\n isMigrated: true,\n labelName: 'mycrypto',\n labelhash:\n '0x9a781ca0d227debc3ee76d547c960b0803a6c9f58c6d3b4722f12ede7e6ef7c9',\n name: 'mycrypto.eth',\n parent: { __typename: 'Domain', name: 'eth' },\n },\n expiryDate: '1754111315',\n },\n ],\n },\n moreComplexity: {\n numbers: [\n -5e-11,\n 5e-9,\n 0.000000000001,\n -0.00000000009,\n 100000.00000008,\n -100.88888,\n 0.333,\n 1000000000000,\n ],\n moreNestedObjects: {\n nestedAgain: {\n nestedAgain: {\n andAgain: {\n andAgain: {\n value: true,\n again: {\n value: false,\n },\n },\n },\n },\n },\n },\n differentEncodings: {\n ascii:\n '!\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',\n utf8: 'šđćčžЀЂЇЄЖФћΣΩδλ',\n mixed: 'ABCDEFGHIJ KLMNOPQRST UVWXYZšđćč žЀЂЇЄЖФћΣΩδλ',\n specialCharacters: '\"\\\\\\n\\r\\t',\n stringWithSpecialEscapedCharacters:\n \"this\\nis\\nsome\\\"'string\\r\\nwith\\tspecial\\\\escaped\\tcharacters'\",\n },\n specialObjectsTypesAndValues: {\n t: [true, true, true],\n f: [false, false, false],\n nulls: [null, null, null],\n undef: undefined,\n mixed: [\n null,\n undefined,\n null,\n undefined,\n null,\n true,\n null,\n false,\n null,\n undefined,\n ],\n inObject: {\n valueOne: null,\n valueTwo: undefined,\n t: true,\n f: false,\n },\n dates: {\n someDate: new Date(),\n someOther: new Date(2022, 0, 2, 15, 4, 5),\n invalidDate: new Date('bad-date-format'),\n },\n },\n },\n },\n};\n\nexport const nonSerializableNestedObject = {\n levelOne: {\n levelTwo: {\n levelThree: {\n levelFour: {\n levelFive: () => {\n return 'anything';\n },\n },\n },\n },\n },\n};\n\nexport const arrayOfDifferentKindsOfNumbers = [\n -5e-11,\n 5e-9,\n 0.000000000001,\n -0.00000000009,\n 100000.00000008,\n -100.88888,\n 0.333,\n 1000000000000,\n];\n\nexport const arrayOfMixedSpecialObjects = [\n null,\n undefined,\n null,\n undefined,\n undefined,\n undefined,\n null,\n null,\n null,\n undefined,\n];\n\nexport const objectMixedWithUndefinedValues = {\n a: undefined,\n b: 'b',\n c: undefined,\n d: 'd',\n e: undefined,\n f: 'f',\n};\n"]}
|