@metamask/utils 1.0.0 → 3.0.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 +44 -1
- package/README.md +1 -1
- package/dist/__fixtures__/index.d.ts +1 -0
- package/dist/__fixtures__/index.js +18 -0
- package/dist/__fixtures__/index.js.map +1 -0
- package/dist/__fixtures__/json.d.ts +825 -0
- package/dist/__fixtures__/json.js +797 -0
- package/dist/__fixtures__/json.js.map +1 -0
- package/dist/collections.js +31 -33
- package/dist/collections.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +187 -40
- package/dist/json.js +336 -12
- package/dist/json.js.map +1 -1
- package/dist/logging.d.ts +29 -0
- package/dist/logging.js +43 -0
- package/dist/logging.js.map +1 -0
- package/dist/misc.d.ts +47 -0
- package/dist/misc.js +80 -1
- package/dist/misc.js.map +1 -1
- package/dist/time.d.ts +37 -16
- package/dist/time.js +50 -17
- package/dist/time.js.map +1 -1
- package/package.json +11 -7
package/dist/json.js
CHANGED
|
@@ -3,9 +3,29 @@ 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.isJsonRpcFailure = exports.isJsonRpcSuccess = exports.jsonrpc2 = exports.isValidJson = void 0;
|
|
6
|
+
exports.validateJsonAndGetSize = exports.getJsonRpcIdValidator = exports.assertIsJsonRpcFailure = exports.isJsonRpcFailure = exports.assertIsJsonRpcSuccess = exports.isJsonRpcSuccess = exports.assertIsJsonRpcResponse = exports.isJsonRpcResponse = exports.JsonRpcResponseStruct = exports.JsonRpcFailureStruct = exports.JsonRpcSuccessStruct = exports.assertIsJsonRpcRequest = exports.isJsonRpcRequest = exports.assertIsJsonRpcNotification = exports.isJsonRpcNotification = exports.JsonRpcNotificationStruct = exports.JsonRpcRequestStruct = exports.JsonRpcParamsStruct = exports.JsonRpcErrorStruct = exports.JsonRpcIdStruct = exports.JsonRpcVersionStruct = exports.jsonrpc2 = exports.isValidJson = exports.JsonStruct = void 0;
|
|
7
7
|
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
8
|
+
const superstruct_1 = require("superstruct");
|
|
8
9
|
const misc_1 = require("./misc");
|
|
10
|
+
/**
|
|
11
|
+
* Type guard for determining whether the given value is an error object with a
|
|
12
|
+
* `message` property, such as an instance of Error.
|
|
13
|
+
*
|
|
14
|
+
* @param error - The object to check.
|
|
15
|
+
* @returns True or false, depending on the result.
|
|
16
|
+
*/
|
|
17
|
+
function isErrorWithMessage(error) {
|
|
18
|
+
return typeof error === 'object' && error !== null && 'message' in error;
|
|
19
|
+
}
|
|
20
|
+
// Note: This struct references itself, so TypeScript cannot infer the type.
|
|
21
|
+
exports.JsonStruct = (0, superstruct_1.union)([
|
|
22
|
+
(0, superstruct_1.literal)(null),
|
|
23
|
+
(0, superstruct_1.boolean)(),
|
|
24
|
+
(0, superstruct_1.number)(),
|
|
25
|
+
(0, superstruct_1.string)(),
|
|
26
|
+
(0, superstruct_1.lazy)(() => (0, superstruct_1.array)(exports.JsonStruct)),
|
|
27
|
+
(0, superstruct_1.lazy)(() => (0, superstruct_1.record)((0, superstruct_1.string)(), exports.JsonStruct)),
|
|
28
|
+
]);
|
|
9
29
|
/**
|
|
10
30
|
* Type guard for {@link Json}.
|
|
11
31
|
*
|
|
@@ -14,7 +34,7 @@ const misc_1 = require("./misc");
|
|
|
14
34
|
*/
|
|
15
35
|
function isValidJson(value) {
|
|
16
36
|
try {
|
|
17
|
-
return fast_deep_equal_1.default(value, JSON.parse(JSON.stringify(value)));
|
|
37
|
+
return (0, fast_deep_equal_1.default)(value, JSON.parse(JSON.stringify(value)));
|
|
18
38
|
}
|
|
19
39
|
catch (_) {
|
|
20
40
|
return false;
|
|
@@ -25,26 +45,140 @@ exports.isValidJson = isValidJson;
|
|
|
25
45
|
* The string '2.0'.
|
|
26
46
|
*/
|
|
27
47
|
exports.jsonrpc2 = '2.0';
|
|
48
|
+
exports.JsonRpcVersionStruct = (0, superstruct_1.literal)(exports.jsonrpc2);
|
|
49
|
+
exports.JsonRpcIdStruct = (0, superstruct_1.nullable)((0, superstruct_1.union)([(0, superstruct_1.number)(), (0, superstruct_1.string)()]));
|
|
50
|
+
exports.JsonRpcErrorStruct = (0, superstruct_1.object)({
|
|
51
|
+
code: (0, superstruct_1.number)(),
|
|
52
|
+
message: (0, superstruct_1.string)(),
|
|
53
|
+
data: (0, superstruct_1.optional)((0, superstruct_1.unknown)()),
|
|
54
|
+
stack: (0, superstruct_1.optional)((0, superstruct_1.string)()),
|
|
55
|
+
});
|
|
56
|
+
exports.JsonRpcParamsStruct = (0, superstruct_1.optional)((0, superstruct_1.union)([(0, superstruct_1.object)(), (0, superstruct_1.array)()]));
|
|
57
|
+
exports.JsonRpcRequestStruct = (0, superstruct_1.object)({
|
|
58
|
+
id: exports.JsonRpcIdStruct,
|
|
59
|
+
jsonrpc: exports.JsonRpcVersionStruct,
|
|
60
|
+
method: (0, superstruct_1.string)(),
|
|
61
|
+
params: exports.JsonRpcParamsStruct,
|
|
62
|
+
});
|
|
63
|
+
exports.JsonRpcNotificationStruct = (0, superstruct_1.omit)(exports.JsonRpcRequestStruct, ['id']);
|
|
64
|
+
/**
|
|
65
|
+
* Type guard to narrow a JSON-RPC request or notification object to a
|
|
66
|
+
* notification.
|
|
67
|
+
*
|
|
68
|
+
* @param requestOrNotification - The JSON-RPC request or notification to check.
|
|
69
|
+
* @returns Whether the specified JSON-RPC message is a notification.
|
|
70
|
+
*/
|
|
71
|
+
function isJsonRpcNotification(requestOrNotification) {
|
|
72
|
+
return (0, superstruct_1.is)(requestOrNotification, exports.JsonRpcNotificationStruct);
|
|
73
|
+
}
|
|
74
|
+
exports.isJsonRpcNotification = isJsonRpcNotification;
|
|
75
|
+
/**
|
|
76
|
+
* Assertion type guard to narrow a JSON-RPC request or notification object to a
|
|
77
|
+
* notification.
|
|
78
|
+
*
|
|
79
|
+
* @param requestOrNotification - The JSON-RPC request or notification to check.
|
|
80
|
+
*/
|
|
81
|
+
function assertIsJsonRpcNotification(requestOrNotification) {
|
|
82
|
+
try {
|
|
83
|
+
(0, superstruct_1.assert)(requestOrNotification, exports.JsonRpcNotificationStruct);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
const message = isErrorWithMessage(error) ? error.message : error;
|
|
87
|
+
throw new Error(`Not a JSON-RPC notification: ${message}.`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.assertIsJsonRpcNotification = assertIsJsonRpcNotification;
|
|
91
|
+
/**
|
|
92
|
+
* Type guard to narrow a JSON-RPC request or notification object to a request.
|
|
93
|
+
*
|
|
94
|
+
* @param requestOrNotification - The JSON-RPC request or notification to check.
|
|
95
|
+
* @returns Whether the specified JSON-RPC message is a request.
|
|
96
|
+
*/
|
|
97
|
+
function isJsonRpcRequest(requestOrNotification) {
|
|
98
|
+
return (0, superstruct_1.is)(requestOrNotification, exports.JsonRpcRequestStruct);
|
|
99
|
+
}
|
|
100
|
+
exports.isJsonRpcRequest = isJsonRpcRequest;
|
|
101
|
+
/**
|
|
102
|
+
* Assertion type guard to narrow a JSON-RPC request or notification object to a
|
|
103
|
+
* request.
|
|
104
|
+
*
|
|
105
|
+
* @param requestOrNotification - The JSON-RPC request or notification to check.
|
|
106
|
+
*/
|
|
107
|
+
function assertIsJsonRpcRequest(requestOrNotification) {
|
|
108
|
+
try {
|
|
109
|
+
(0, superstruct_1.assert)(requestOrNotification, exports.JsonRpcRequestStruct);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
const message = isErrorWithMessage(error) ? error.message : error;
|
|
113
|
+
throw new Error(`Not a JSON-RPC request: ${message}.`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.assertIsJsonRpcRequest = assertIsJsonRpcRequest;
|
|
117
|
+
exports.JsonRpcSuccessStruct = (0, superstruct_1.object)({
|
|
118
|
+
id: exports.JsonRpcIdStruct,
|
|
119
|
+
jsonrpc: exports.JsonRpcVersionStruct,
|
|
120
|
+
result: exports.JsonStruct,
|
|
121
|
+
});
|
|
122
|
+
exports.JsonRpcFailureStruct = (0, superstruct_1.object)({
|
|
123
|
+
id: exports.JsonRpcIdStruct,
|
|
124
|
+
jsonrpc: exports.JsonRpcVersionStruct,
|
|
125
|
+
error: exports.JsonRpcErrorStruct,
|
|
126
|
+
});
|
|
127
|
+
exports.JsonRpcResponseStruct = (0, superstruct_1.union)([
|
|
128
|
+
exports.JsonRpcSuccessStruct,
|
|
129
|
+
exports.JsonRpcFailureStruct,
|
|
130
|
+
]);
|
|
131
|
+
/**
|
|
132
|
+
* Type guard to check if a value is a JsonRpcResponse.
|
|
133
|
+
*
|
|
134
|
+
* @param response - The object to check.
|
|
135
|
+
* @returns Whether the object is a JsonRpcResponse.
|
|
136
|
+
*/
|
|
137
|
+
function isJsonRpcResponse(response) {
|
|
138
|
+
return (0, superstruct_1.is)(response, exports.JsonRpcResponseStruct);
|
|
139
|
+
}
|
|
140
|
+
exports.isJsonRpcResponse = isJsonRpcResponse;
|
|
28
141
|
/**
|
|
29
|
-
*
|
|
30
|
-
* present on the `response`, as guaranteed by e.g.
|
|
31
|
-
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
|
|
142
|
+
* Type assertion to check if a value is a JsonRpcResponse.
|
|
32
143
|
*
|
|
144
|
+
* @param response - The response to check.
|
|
145
|
+
*/
|
|
146
|
+
function assertIsJsonRpcResponse(response) {
|
|
147
|
+
try {
|
|
148
|
+
(0, superstruct_1.assert)(response, exports.JsonRpcResponseStruct);
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
const message = isErrorWithMessage(error) ? error.message : error;
|
|
152
|
+
throw new Error(`Not a JSON-RPC response: ${message}.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.assertIsJsonRpcResponse = assertIsJsonRpcResponse;
|
|
156
|
+
/**
|
|
33
157
|
* Type guard to narrow a JsonRpcResponse object to a success (or failure).
|
|
34
158
|
*
|
|
35
159
|
* @param response - The response object to check.
|
|
36
|
-
* @returns Whether the response object is a success
|
|
37
|
-
* property.
|
|
160
|
+
* @returns Whether the response object is a success.
|
|
38
161
|
*/
|
|
39
162
|
function isJsonRpcSuccess(response) {
|
|
40
|
-
return
|
|
163
|
+
return (0, superstruct_1.is)(response, exports.JsonRpcSuccessStruct);
|
|
41
164
|
}
|
|
42
165
|
exports.isJsonRpcSuccess = isJsonRpcSuccess;
|
|
43
166
|
/**
|
|
44
|
-
*
|
|
45
|
-
* present on the `response`, as guaranteed by e.g.
|
|
46
|
-
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
|
|
167
|
+
* Type assertion to narrow a JsonRpcResponse object to a success (or failure).
|
|
47
168
|
*
|
|
169
|
+
* @param response - The response object to check.
|
|
170
|
+
*/
|
|
171
|
+
function assertIsJsonRpcSuccess(response) {
|
|
172
|
+
try {
|
|
173
|
+
(0, superstruct_1.assert)(response, exports.JsonRpcSuccessStruct);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
const message = isErrorWithMessage(error) ? error.message : error;
|
|
177
|
+
throw new Error(`Not a successful JSON-RPC response: ${message}.`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.assertIsJsonRpcSuccess = assertIsJsonRpcSuccess;
|
|
181
|
+
/**
|
|
48
182
|
* Type guard to narrow a JsonRpcResponse object to a failure (or success).
|
|
49
183
|
*
|
|
50
184
|
* @param response - The response object to check.
|
|
@@ -52,7 +186,197 @@ exports.isJsonRpcSuccess = isJsonRpcSuccess;
|
|
|
52
186
|
* property.
|
|
53
187
|
*/
|
|
54
188
|
function isJsonRpcFailure(response) {
|
|
55
|
-
return
|
|
189
|
+
return (0, superstruct_1.is)(response, exports.JsonRpcFailureStruct);
|
|
56
190
|
}
|
|
57
191
|
exports.isJsonRpcFailure = isJsonRpcFailure;
|
|
192
|
+
/**
|
|
193
|
+
* Type assertion to narrow a JsonRpcResponse object to a failure (or success).
|
|
194
|
+
*
|
|
195
|
+
* @param response - The response object to check.
|
|
196
|
+
*/
|
|
197
|
+
function assertIsJsonRpcFailure(response) {
|
|
198
|
+
try {
|
|
199
|
+
(0, superstruct_1.assert)(response, exports.JsonRpcFailureStruct);
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
const message = isErrorWithMessage(error) ? error.message : error;
|
|
203
|
+
throw new Error(`Not a failed JSON-RPC response: ${message}.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
exports.assertIsJsonRpcFailure = assertIsJsonRpcFailure;
|
|
207
|
+
/**
|
|
208
|
+
* Gets a function for validating JSON-RPC request / response `id` values.
|
|
209
|
+
*
|
|
210
|
+
* By manipulating the options of this factory, you can control the behavior
|
|
211
|
+
* of the resulting validator for some edge cases. This is useful because e.g.
|
|
212
|
+
* `null` should sometimes but not always be permitted.
|
|
213
|
+
*
|
|
214
|
+
* Note that the empty string (`''`) is always permitted by the JSON-RPC
|
|
215
|
+
* specification, but that kind of sucks and you may want to forbid it in some
|
|
216
|
+
* instances anyway.
|
|
217
|
+
*
|
|
218
|
+
* For more details, see the
|
|
219
|
+
* [JSON-RPC Specification](https://www.jsonrpc.org/specification).
|
|
220
|
+
*
|
|
221
|
+
* @param options - An options object.
|
|
222
|
+
* @param options.permitEmptyString - Whether the empty string (i.e. `''`)
|
|
223
|
+
* should be treated as a valid ID. Default: `true`
|
|
224
|
+
* @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)
|
|
225
|
+
* should be treated as valid IDs. Default: `false`
|
|
226
|
+
* @param options.permitNull - Whether `null` should be treated as a valid ID.
|
|
227
|
+
* Default: `true`
|
|
228
|
+
* @returns The JSON-RPC ID validator function.
|
|
229
|
+
*/
|
|
230
|
+
function getJsonRpcIdValidator(options) {
|
|
231
|
+
const { permitEmptyString, permitFractions, permitNull } = Object.assign({ permitEmptyString: true, permitFractions: false, permitNull: true }, options);
|
|
232
|
+
/**
|
|
233
|
+
* Type guard for {@link JsonRpcId}.
|
|
234
|
+
*
|
|
235
|
+
* @param id - The JSON-RPC ID value to check.
|
|
236
|
+
* @returns Whether the given ID is valid per the options given to the
|
|
237
|
+
* factory.
|
|
238
|
+
*/
|
|
239
|
+
const isValidJsonRpcId = (id) => {
|
|
240
|
+
return Boolean((typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||
|
|
241
|
+
(typeof id === 'string' && (permitEmptyString || id.length > 0)) ||
|
|
242
|
+
(permitNull && id === null));
|
|
243
|
+
};
|
|
244
|
+
return isValidJsonRpcId;
|
|
245
|
+
}
|
|
246
|
+
exports.getJsonRpcIdValidator = getJsonRpcIdValidator;
|
|
247
|
+
/**
|
|
248
|
+
* Checks whether a value is JSON serializable and counts the total number
|
|
249
|
+
* of bytes needed to store the serialized version of the value.
|
|
250
|
+
*
|
|
251
|
+
* @param jsObject - Potential JSON serializable object.
|
|
252
|
+
* @param skipSizingProcess - Skip JSON size calculation (default: false).
|
|
253
|
+
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
|
|
254
|
+
* the value was serializable and a number of bytes that it will use when serialized to JSON.
|
|
255
|
+
*/
|
|
256
|
+
function validateJsonAndGetSize(jsObject, skipSizingProcess = false) {
|
|
257
|
+
const seenObjects = new Set();
|
|
258
|
+
/**
|
|
259
|
+
* Checks whether a value is JSON serializable and counts the total number
|
|
260
|
+
* of bytes needed to store the serialized version of the value.
|
|
261
|
+
*
|
|
262
|
+
* This function assumes the encoding of the JSON is done in UTF-8.
|
|
263
|
+
*
|
|
264
|
+
* @param value - Potential JSON serializable value.
|
|
265
|
+
* @param skipSizing - Skip JSON size calculation (default: false).
|
|
266
|
+
* @returns Tuple [isValid, plainTextSizeInBytes] containing a boolean that signals whether
|
|
267
|
+
* the value was serializable and a number of bytes that it will use when serialized to JSON.
|
|
268
|
+
*/
|
|
269
|
+
function getJsonSerializableInfo(value, skipSizing) {
|
|
270
|
+
if (value === undefined) {
|
|
271
|
+
// Return zero for undefined, since these are omitted from JSON serialization
|
|
272
|
+
return [true, 0];
|
|
273
|
+
}
|
|
274
|
+
else if (value === null) {
|
|
275
|
+
// Return already specified constant size for null (special object)
|
|
276
|
+
return [true, skipSizing ? 0 : misc_1.JsonSize.Null];
|
|
277
|
+
}
|
|
278
|
+
// Check and calculate sizes for basic (and some special) types
|
|
279
|
+
const typeOfValue = typeof value;
|
|
280
|
+
try {
|
|
281
|
+
if (typeOfValue === 'function') {
|
|
282
|
+
return [false, 0];
|
|
283
|
+
}
|
|
284
|
+
else if (typeOfValue === 'string' || value instanceof String) {
|
|
285
|
+
return [
|
|
286
|
+
true,
|
|
287
|
+
skipSizing
|
|
288
|
+
? 0
|
|
289
|
+
: (0, misc_1.calculateStringSize)(value) + misc_1.JsonSize.Quote * 2,
|
|
290
|
+
];
|
|
291
|
+
}
|
|
292
|
+
else if (typeOfValue === 'boolean' || value instanceof Boolean) {
|
|
293
|
+
if (skipSizing) {
|
|
294
|
+
return [true, 0];
|
|
295
|
+
}
|
|
296
|
+
// eslint-disable-next-line eqeqeq
|
|
297
|
+
return [true, value == true ? misc_1.JsonSize.True : misc_1.JsonSize.False];
|
|
298
|
+
}
|
|
299
|
+
else if (typeOfValue === 'number' || value instanceof Number) {
|
|
300
|
+
if (skipSizing) {
|
|
301
|
+
return [true, 0];
|
|
302
|
+
}
|
|
303
|
+
return [true, (0, misc_1.calculateNumberSize)(value)];
|
|
304
|
+
}
|
|
305
|
+
else if (value instanceof Date) {
|
|
306
|
+
if (skipSizing) {
|
|
307
|
+
return [true, 0];
|
|
308
|
+
}
|
|
309
|
+
return [
|
|
310
|
+
true,
|
|
311
|
+
// Note: Invalid dates will serialize to null
|
|
312
|
+
isNaN(value.getDate())
|
|
313
|
+
? misc_1.JsonSize.Null
|
|
314
|
+
: misc_1.JsonSize.Date + misc_1.JsonSize.Quote * 2,
|
|
315
|
+
];
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch (_) {
|
|
319
|
+
return [false, 0];
|
|
320
|
+
}
|
|
321
|
+
// If object is not plain and cannot be serialized properly,
|
|
322
|
+
// stop here and return false for serialization
|
|
323
|
+
if (!(0, misc_1.isPlainObject)(value) && !Array.isArray(value)) {
|
|
324
|
+
return [false, 0];
|
|
325
|
+
}
|
|
326
|
+
// Circular object detection (handling)
|
|
327
|
+
// Check if the same object already exists
|
|
328
|
+
if (seenObjects.has(value)) {
|
|
329
|
+
return [false, 0];
|
|
330
|
+
}
|
|
331
|
+
// Add new object to the seen objects set
|
|
332
|
+
// Only the plain objects should be added (Primitive types are skipped)
|
|
333
|
+
seenObjects.add(value);
|
|
334
|
+
// Continue object decomposition
|
|
335
|
+
try {
|
|
336
|
+
return [
|
|
337
|
+
true,
|
|
338
|
+
Object.entries(value).reduce((sum, [key, nestedValue], idx, arr) => {
|
|
339
|
+
// Recursively process next nested object or primitive type
|
|
340
|
+
// eslint-disable-next-line prefer-const
|
|
341
|
+
let [valid, size] = getJsonSerializableInfo(nestedValue, skipSizing);
|
|
342
|
+
if (!valid) {
|
|
343
|
+
throw new Error('JSON validation did not pass. Validation process stopped.');
|
|
344
|
+
}
|
|
345
|
+
// Circular object detection
|
|
346
|
+
// Once a child node is visited and processed remove it from the set.
|
|
347
|
+
// This will prevent false positives with the same adjacent objects.
|
|
348
|
+
seenObjects.delete(value);
|
|
349
|
+
if (skipSizing) {
|
|
350
|
+
return 0;
|
|
351
|
+
}
|
|
352
|
+
// If the size is 0, the value is undefined and undefined in an array
|
|
353
|
+
// when serialized will be replaced with null
|
|
354
|
+
if (size === 0 && Array.isArray(value)) {
|
|
355
|
+
size = misc_1.JsonSize.Null;
|
|
356
|
+
}
|
|
357
|
+
// If the size is 0, that means the object is undefined and
|
|
358
|
+
// the rest of the object structure will be omitted
|
|
359
|
+
if (size === 0) {
|
|
360
|
+
return sum;
|
|
361
|
+
}
|
|
362
|
+
// Objects will have be serialized with "key": value,
|
|
363
|
+
// therefore we include the key in the calculation here
|
|
364
|
+
const keySize = Array.isArray(value)
|
|
365
|
+
? 0
|
|
366
|
+
: key.length + misc_1.JsonSize.Comma + misc_1.JsonSize.Colon * 2;
|
|
367
|
+
const separator = idx < arr.length - 1 ? misc_1.JsonSize.Comma : 0;
|
|
368
|
+
return sum + keySize + size + separator;
|
|
369
|
+
},
|
|
370
|
+
// Starts at 2 because the serialized JSON string data (plain text)
|
|
371
|
+
// will minimally contain {}/[]
|
|
372
|
+
skipSizing ? 0 : misc_1.JsonSize.Wrapper * 2),
|
|
373
|
+
];
|
|
374
|
+
}
|
|
375
|
+
catch (_) {
|
|
376
|
+
return [false, 0];
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return getJsonSerializableInfo(jsObject, skipSizingProcess);
|
|
380
|
+
}
|
|
381
|
+
exports.validateJsonAndGetSize = validateJsonAndGetSize;
|
|
58
382
|
//# 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;AA+EvC;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAC9B,QAAiC;IAEjC,OAAO,kBAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACzC,CAAC;AAJD,4CAIC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAC9B,QAAkC;IAElC,OAAO,kBAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC;AAJD,4CAIC","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 * 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 * ATTN: Assumes that only one of the `result` and `error` properties is\n * present on the `response`, as guaranteed by e.g.\n * [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).\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 * ATTN: Assumes that only one of the `result` and `error` properties is\n * present on the `response`, as guaranteed by e.g.\n * [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).\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"]}
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,6CAkBqB;AACrB,iCAKgB;AAEhB;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,KAAc;IACxC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,IAAI,KAAK,CAAC;AAC3E,CAAC;AAED,4EAA4E;AAC/D,QAAA,UAAU,GA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AC,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 array,\n assert,\n boolean,\n Infer,\n is,\n lazy,\n literal,\n nullable,\n number,\n object,\n omit,\n optional,\n record,\n string,\n Struct,\n union,\n unknown,\n} from 'superstruct';\nimport {\n calculateNumberSize,\n calculateStringSize,\n isPlainObject,\n JsonSize,\n} from './misc';\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns True or false, depending on the result.\n */\nfunction isErrorWithMessage(error: unknown): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\n\n// Note: This struct references itself, so TypeScript cannot infer the type.\nexport const JsonStruct: Struct<Json> = union([\n literal(null),\n boolean(),\n number(),\n string(),\n lazy(() => array(JsonStruct)),\n lazy(() => record(string(), JsonStruct)),\n]);\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;\nexport const JsonRpcVersionStruct = literal(jsonrpc2);\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\nexport const JsonRpcIdStruct = nullable(union([number(), string()]));\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 = Infer<typeof JsonRpcIdStruct>;\n\nexport const JsonRpcErrorStruct = object({\n code: number(),\n message: string(),\n data: optional(unknown()),\n stack: optional(string()),\n});\n\n/**\n * A JSON-RPC error object.\n */\nexport type JsonRpcError = Infer<typeof JsonRpcErrorStruct>;\n\nexport const JsonRpcParamsStruct = optional(union([object(), array()]));\n\nexport type JsonRpcParams = Infer<typeof JsonRpcParamsStruct>;\n\nexport const JsonRpcRequestStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: JsonRpcParamsStruct,\n});\n\nexport type InferWithParams<\n Type extends Struct<any, unknown>,\n Params extends JsonRpcParams\n> = Omit<Infer<Type>, 'params'> & {\n params: Params;\n};\n\n/**\n * A JSON-RPC request object.\n */\nexport type JsonRpcRequest<Params extends JsonRpcParams> = InferWithParams<\n typeof JsonRpcRequestStruct,\n Params\n>;\n\nexport const JsonRpcNotificationStruct = omit(JsonRpcRequestStruct, ['id']);\n\n/**\n * A JSON-RPC notification object.\n */\nexport type JsonRpcNotification<Params extends JsonRpcParams> = InferWithParams<\n typeof JsonRpcNotificationStruct,\n 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(\n requestOrNotification: unknown,\n): requestOrNotification is JsonRpcNotification<JsonRpcParams> {\n return is(requestOrNotification, JsonRpcNotificationStruct);\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(\n requestOrNotification: unknown,\n): asserts requestOrNotification is JsonRpcNotification<JsonRpcParams> {\n try {\n assert(requestOrNotification, JsonRpcNotificationStruct);\n } catch (error) {\n const message = isErrorWithMessage(error) ? error.message : error;\n throw new Error(`Not a JSON-RPC notification: ${message}.`);\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(\n requestOrNotification: unknown,\n): requestOrNotification is JsonRpcRequest<JsonRpcParams> {\n return is(requestOrNotification, JsonRpcRequestStruct);\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(\n requestOrNotification: unknown,\n): asserts requestOrNotification is JsonRpcRequest<JsonRpcParams> {\n try {\n assert(requestOrNotification, JsonRpcRequestStruct);\n } catch (error) {\n const message = isErrorWithMessage(error) ? error.message : error;\n throw new Error(`Not a JSON-RPC request: ${message}.`);\n }\n}\n\nexport const JsonRpcSuccessStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: JsonStruct,\n});\n\n/**\n * A successful JSON-RPC response object.\n */\nexport type JsonRpcSuccess<Result extends Json> = Omit<\n Infer<typeof JsonRpcSuccessStruct>,\n 'result'\n> & {\n result: Result;\n};\n\nexport const JsonRpcFailureStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n error: JsonRpcErrorStruct,\n});\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = Infer<typeof JsonRpcFailureStruct>;\n\nexport const JsonRpcResponseStruct = union([\n JsonRpcSuccessStruct,\n JsonRpcFailureStruct,\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 extends Json> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to check if a value is a JsonRpcResponse.\n *\n * @param response - The object to check.\n * @returns Whether the object is a JsonRpcResponse.\n */\nexport function isJsonRpcResponse(\n response: unknown,\n): response is JsonRpcResponse<Json> {\n return is(response, JsonRpcResponseStruct);\n}\n\n/**\n * Type assertion to check if a value is a JsonRpcResponse.\n *\n * @param response - The response to check.\n */\nexport function assertIsJsonRpcResponse(\n response: unknown,\n): asserts response is JsonRpcResponse<Json> {\n try {\n assert(response, JsonRpcResponseStruct);\n } catch (error) {\n const message = isErrorWithMessage(error) ? error.message : error;\n throw new Error(`Not a JSON-RPC response: ${message}.`);\n }\n}\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.\n */\nexport function isJsonRpcSuccess(\n response: unknown,\n): response is JsonRpcSuccess<Json> {\n return is(response, JsonRpcSuccessStruct);\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(\n response: unknown,\n): asserts response is JsonRpcSuccess<Json> {\n try {\n assert(response, JsonRpcSuccessStruct);\n } catch (error) {\n const message = isErrorWithMessage(error) ? error.message : error;\n throw new Error(`Not a successful JSON-RPC response: ${message}.`);\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: unknown,\n): response is JsonRpcFailure {\n return is(response, JsonRpcFailureStruct);\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: unknown,\n): asserts response is JsonRpcFailure {\n try {\n assert(response, JsonRpcFailureStruct);\n } catch (error) {\n const message = isErrorWithMessage(error) ? error.message : error;\n throw new Error(`Not a failed JSON-RPC response: ${message}.`);\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\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"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Debugger } from 'debug';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a logger via the `debug` library whose log messages will be tagged
|
|
4
|
+
* using the name of your project. By default, such messages will be
|
|
5
|
+
* suppressed, but you can reveal them by setting the `DEBUG` environment
|
|
6
|
+
* variable to `metamask:<projectName>`. You can also set this variable to
|
|
7
|
+
* `metamask:*` if you want to see log messages from all MetaMask projects that
|
|
8
|
+
* are also using this function to create their loggers.
|
|
9
|
+
*
|
|
10
|
+
* @param projectName - The name of your project. This should be the name of
|
|
11
|
+
* your NPM package if you're developing one.
|
|
12
|
+
* @returns An instance of `debug`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createProjectLogger(projectName: string): Debugger;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a logger via the `debug` library which is derived from the logger for
|
|
17
|
+
* the whole project whose log messages will be tagged using the name of your
|
|
18
|
+
* module. By default, such messages will be suppressed, but you can reveal them
|
|
19
|
+
* by setting the `DEBUG` environment variable to
|
|
20
|
+
* `metamask:<projectName>:<moduleName>`. You can also set this variable to
|
|
21
|
+
* `metamask:<projectName>:*` if you want to see log messages from the project,
|
|
22
|
+
* or `metamask:*` if you want to see log messages from all MetaMask projects.
|
|
23
|
+
*
|
|
24
|
+
* @param projectLogger - The logger created via {@link createProjectLogger}.
|
|
25
|
+
* @param moduleName - The name of your module. You could use the name of the
|
|
26
|
+
* file where you're using this logger or some other name.
|
|
27
|
+
* @returns An instance of `debug`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createModuleLogger(projectLogger: Debugger, moduleName: string): Debugger;
|
package/dist/logging.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createModuleLogger = exports.createProjectLogger = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const globalLogger = (0, debug_1.default)('metamask');
|
|
9
|
+
/**
|
|
10
|
+
* Creates a logger via the `debug` library whose log messages will be tagged
|
|
11
|
+
* using the name of your project. By default, such messages will be
|
|
12
|
+
* suppressed, but you can reveal them by setting the `DEBUG` environment
|
|
13
|
+
* variable to `metamask:<projectName>`. You can also set this variable to
|
|
14
|
+
* `metamask:*` if you want to see log messages from all MetaMask projects that
|
|
15
|
+
* are also using this function to create their loggers.
|
|
16
|
+
*
|
|
17
|
+
* @param projectName - The name of your project. This should be the name of
|
|
18
|
+
* your NPM package if you're developing one.
|
|
19
|
+
* @returns An instance of `debug`.
|
|
20
|
+
*/
|
|
21
|
+
function createProjectLogger(projectName) {
|
|
22
|
+
return globalLogger.extend(projectName);
|
|
23
|
+
}
|
|
24
|
+
exports.createProjectLogger = createProjectLogger;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a logger via the `debug` library which is derived from the logger for
|
|
27
|
+
* the whole project whose log messages will be tagged using the name of your
|
|
28
|
+
* module. By default, such messages will be suppressed, but you can reveal them
|
|
29
|
+
* by setting the `DEBUG` environment variable to
|
|
30
|
+
* `metamask:<projectName>:<moduleName>`. You can also set this variable to
|
|
31
|
+
* `metamask:<projectName>:*` if you want to see log messages from the project,
|
|
32
|
+
* or `metamask:*` if you want to see log messages from all MetaMask projects.
|
|
33
|
+
*
|
|
34
|
+
* @param projectLogger - The logger created via {@link createProjectLogger}.
|
|
35
|
+
* @param moduleName - The name of your module. You could use the name of the
|
|
36
|
+
* file where you're using this logger or some other name.
|
|
37
|
+
* @returns An instance of `debug`.
|
|
38
|
+
*/
|
|
39
|
+
function createModuleLogger(projectLogger, moduleName) {
|
|
40
|
+
return projectLogger.extend(moduleName);
|
|
41
|
+
}
|
|
42
|
+
exports.createModuleLogger = createModuleLogger;
|
|
43
|
+
//# sourceMappingURL=logging.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAwC;AAExC,MAAM,YAAY,GAAG,IAAA,eAAK,EAAC,UAAU,CAAC,CAAC;AAEvC;;;;;;;;;;;GAWG;AACH,SAAgB,mBAAmB,CAAC,WAAmB;IACrD,OAAO,YAAY,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAC1C,CAAC;AAFD,kDAEC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,kBAAkB,CAChC,aAAuB,EACvB,UAAkB;IAElB,OAAO,aAAa,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;AAC1C,CAAC;AALD,gDAKC","sourcesContent":["import debug, { Debugger } from 'debug';\n\nconst globalLogger = debug('metamask');\n\n/**\n * Creates a logger via the `debug` library whose log messages will be tagged\n * using the name of your project. By default, such messages will be\n * suppressed, but you can reveal them by setting the `DEBUG` environment\n * variable to `metamask:<projectName>`. You can also set this variable to\n * `metamask:*` if you want to see log messages from all MetaMask projects that\n * are also using this function to create their loggers.\n *\n * @param projectName - The name of your project. This should be the name of\n * your NPM package if you're developing one.\n * @returns An instance of `debug`.\n */\nexport function createProjectLogger(projectName: string): Debugger {\n return globalLogger.extend(projectName);\n}\n\n/**\n * Creates a logger via the `debug` library which is derived from the logger for\n * the whole project whose log messages will be tagged using the name of your\n * module. By default, such messages will be suppressed, but you can reveal them\n * by setting the `DEBUG` environment variable to\n * `metamask:<projectName>:<moduleName>`. You can also set this variable to\n * `metamask:<projectName>:*` if you want to see log messages from the project,\n * or `metamask:*` if you want to see log messages from all MetaMask projects.\n *\n * @param projectLogger - The logger created via {@link createProjectLogger}.\n * @param moduleName - The name of your module. You could use the name of the\n * file where you're using this logger or some other name.\n * @returns An instance of `debug`.\n */\nexport function createModuleLogger(\n projectLogger: Debugger,\n moduleName: string,\n): Debugger {\n return projectLogger.extend(moduleName);\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;
|