@dxos/invariant 0.1.56-main.bf228a7
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/LICENSE +8 -0
- package/dist/lib/browser/index.mjs +28 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +56 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/invariant.d.ts +12 -0
- package/dist/types/src/invariant.d.ts.map +1 -0
- package/dist/types/src/meta.d.ts +30 -0
- package/dist/types/src/meta.d.ts.map +1 -0
- package/package.json +29 -0
- package/src/index.ts +5 -0
- package/src/invariant.ts +46 -0
- package/src/meta.ts +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// packages/common/invariant/src/invariant.ts
|
|
2
|
+
var invariant = (condition, message, meta) => {
|
|
3
|
+
if (condition) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
let errorMessage = "invariant violation";
|
|
7
|
+
if (message) {
|
|
8
|
+
errorMessage += `: ${message}`;
|
|
9
|
+
}
|
|
10
|
+
if (meta == null ? void 0 : meta.A) {
|
|
11
|
+
errorMessage += ` [${meta.A[0]}]`;
|
|
12
|
+
}
|
|
13
|
+
if (meta == null ? void 0 : meta.F) {
|
|
14
|
+
errorMessage += ` at ${meta.F}:${meta.L}`;
|
|
15
|
+
}
|
|
16
|
+
throw new InvariantViolation(errorMessage);
|
|
17
|
+
};
|
|
18
|
+
var InvariantViolation = class extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
InvariantViolation,
|
|
26
|
+
invariant
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/invariant.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { CallMetadata } from './meta';\n\nexport type InvariantFn = (condition: unknown, message?: string, meta?: CallMetadata) => asserts condition;\n\n/**\n * Asserts that the condition is true.\n *\n * @param message Optional message.\n */\nexport const invariant: InvariantFn = (\n condition: unknown,\n message?: string,\n meta?: CallMetadata,\n): asserts condition => {\n if (condition) {\n return;\n }\n\n let errorMessage = 'invariant violation';\n\n if (message) {\n errorMessage += `: ${message}`;\n }\n\n if (meta?.A) {\n errorMessage += ` [${meta.A[0]}]`;\n }\n\n if (meta?.F) {\n errorMessage += ` at ${meta.F}:${meta.L}`;\n }\n\n throw new InvariantViolation(errorMessage);\n};\n\nexport class InvariantViolation extends Error {\n constructor(message: string) {\n super(message);\n // NOTE: Restores prototype chain (https://stackoverflow.com/a/48342359).\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAaO,IAAMA,YAAyB,CACpCC,WACAC,SACAC,SAAAA;AAEA,MAAIF,WAAW;AACb;EACF;AAEA,MAAIG,eAAe;AAEnB,MAAIF,SAAS;AACXE,oBAAgB,KAAKF;EACvB;AAEA,MAAIC,6BAAME,GAAG;AACXD,oBAAgB,KAAKD,KAAKE,EAAE,CAAA;EAC9B;AAEA,MAAIF,6BAAMG,GAAG;AACXF,oBAAgB,OAAOD,KAAKG,KAAKH,KAAKI;EACxC;AAEA,QAAM,IAAIC,mBAAmBJ,YAAAA;AAC/B;AAEO,IAAMI,qBAAN,cAAiCC,MAAAA;EACtCC,YAAYR,SAAiB;AAC3B,UAAMA,OAAAA;AAENS,WAAOC,eAAe,MAAM,WAAWC,SAAS;EAClD;AACF;",
|
|
6
|
+
"names": ["invariant", "condition", "message", "meta", "errorMessage", "A", "F", "L", "InvariantViolation", "Error", "constructor", "Object", "setPrototypeOf", "prototype"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/invariant/src/invariant.ts":{"bytes":3310,"imports":[]},"packages/common/invariant/src/index.ts":{"bytes":470,"imports":[{"path":"packages/common/invariant/src/invariant.ts","kind":"import-statement","original":"./invariant"}]}},"outputs":{"packages/common/invariant/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":1692},"packages/common/invariant/dist/lib/browser/index.mjs":{"imports":[],"exports":["InvariantViolation","invariant"],"entryPoint":"packages/common/invariant/src/index.ts","inputs":{"packages/common/invariant/src/invariant.ts":{"bytesInOutput":561},"packages/common/invariant/src/index.ts":{"bytesInOutput":0}},"bytes":688}}}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/common/invariant/src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
InvariantViolation: () => InvariantViolation,
|
|
24
|
+
invariant: () => invariant
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// packages/common/invariant/src/invariant.ts
|
|
29
|
+
var invariant = (condition, message, meta) => {
|
|
30
|
+
if (condition) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
let errorMessage = "invariant violation";
|
|
34
|
+
if (message) {
|
|
35
|
+
errorMessage += `: ${message}`;
|
|
36
|
+
}
|
|
37
|
+
if (meta == null ? void 0 : meta.A) {
|
|
38
|
+
errorMessage += ` [${meta.A[0]}]`;
|
|
39
|
+
}
|
|
40
|
+
if (meta == null ? void 0 : meta.F) {
|
|
41
|
+
errorMessage += ` at ${meta.F}:${meta.L}`;
|
|
42
|
+
}
|
|
43
|
+
throw new InvariantViolation(errorMessage);
|
|
44
|
+
};
|
|
45
|
+
var InvariantViolation = class extends Error {
|
|
46
|
+
constructor(message) {
|
|
47
|
+
super(message);
|
|
48
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
InvariantViolation,
|
|
54
|
+
invariant
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/invariant.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nexport * from './invariant';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { CallMetadata } from './meta';\n\nexport type InvariantFn = (condition: unknown, message?: string, meta?: CallMetadata) => asserts condition;\n\n/**\n * Asserts that the condition is true.\n *\n * @param message Optional message.\n */\nexport const invariant: InvariantFn = (\n condition: unknown,\n message?: string,\n meta?: CallMetadata,\n): asserts condition => {\n if (condition) {\n return;\n }\n\n let errorMessage = 'invariant violation';\n\n if (message) {\n errorMessage += `: ${message}`;\n }\n\n if (meta?.A) {\n errorMessage += ` [${meta.A[0]}]`;\n }\n\n if (meta?.F) {\n errorMessage += ` at ${meta.F}:${meta.L}`;\n }\n\n throw new InvariantViolation(errorMessage);\n};\n\nexport class InvariantViolation extends Error {\n constructor(message: string) {\n super(message);\n // NOTE: Restores prototype chain (https://stackoverflow.com/a/48342359).\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACaO,IAAMA,YAAyB,CACpCC,WACAC,SACAC,SAAAA;AAEA,MAAIF,WAAW;AACb;EACF;AAEA,MAAIG,eAAe;AAEnB,MAAIF,SAAS;AACXE,oBAAgB,KAAKF;EACvB;AAEA,MAAIC,6BAAME,GAAG;AACXD,oBAAgB,KAAKD,KAAKE,EAAE,CAAA;EAC9B;AAEA,MAAIF,6BAAMG,GAAG;AACXF,oBAAgB,OAAOD,KAAKG,KAAKH,KAAKI;EACxC;AAEA,QAAM,IAAIC,mBAAmBJ,YAAAA;AAC/B;AAEO,IAAMI,qBAAN,cAAiCC,MAAAA;EACtCC,YAAYR,SAAiB;AAC3B,UAAMA,OAAAA;AAENS,WAAOC,eAAe,MAAM,WAAWC,SAAS;EAClD;AACF;",
|
|
6
|
+
"names": ["invariant", "condition", "message", "meta", "errorMessage", "A", "F", "L", "InvariantViolation", "Error", "constructor", "Object", "setPrototypeOf", "prototype"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/invariant/src/invariant.ts":{"bytes":3310,"imports":[]},"packages/common/invariant/src/index.ts":{"bytes":470,"imports":[{"path":"packages/common/invariant/src/invariant.ts","kind":"import-statement","original":"./invariant"}]}},"outputs":{"packages/common/invariant/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":1820},"packages/common/invariant/dist/lib/node/index.cjs":{"imports":[],"exports":[],"entryPoint":"packages/common/invariant/src/index.ts","inputs":{"packages/common/invariant/src/index.ts":{"bytesInOutput":171},"packages/common/invariant/src/invariant.ts":{"bytesInOutput":561}},"bytes":1781}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CallMetadata } from './meta';
|
|
2
|
+
export type InvariantFn = (condition: unknown, message?: string, meta?: CallMetadata) => asserts condition;
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that the condition is true.
|
|
5
|
+
*
|
|
6
|
+
* @param message Optional message.
|
|
7
|
+
*/
|
|
8
|
+
export declare const invariant: InvariantFn;
|
|
9
|
+
export declare class InvariantViolation extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=invariant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariant.d.ts","sourceRoot":"","sources":["../../../src/invariant.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,MAAM,MAAM,WAAW,GAAG,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,SAAS,CAAC;AAE3G;;;;GAIG;AACH,eAAO,MAAM,SAAS,EAAE,WAwBvB,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,KAAK;gBAC/B,OAAO,EAAE,MAAM;CAK5B"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata injected b y the log transform plugin.
|
|
3
|
+
*
|
|
4
|
+
* Field names are intentionally short to reduce the size of the generated code.
|
|
5
|
+
*/
|
|
6
|
+
export interface CallMetadata {
|
|
7
|
+
/**
|
|
8
|
+
* File name.
|
|
9
|
+
*/
|
|
10
|
+
F: string;
|
|
11
|
+
/**
|
|
12
|
+
* Line number.
|
|
13
|
+
*/
|
|
14
|
+
L: number;
|
|
15
|
+
/**
|
|
16
|
+
* Value of `this` at the site of the log call.
|
|
17
|
+
* Will be set to the class instance if the call is inside a method, or to the `globalThis` (`window` or `global`) otherwise.
|
|
18
|
+
*/
|
|
19
|
+
S: any | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* A callback that will invoke the provided function with provided arguments.
|
|
22
|
+
* Useful in the browser to force a `console.log` call to have a certain stack-trace.
|
|
23
|
+
*/
|
|
24
|
+
C?: (fn: Function, args: any[]) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Source code of the argument list.
|
|
27
|
+
*/
|
|
28
|
+
A?: string[];
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meta.d.ts","sourceRoot":"","sources":["../../../src/meta.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,CAAC,EAAE,MAAM,CAAC;IAEV;;OAEG;IACH,CAAC,EAAE,MAAM,CAAC;IAEV;;;OAGG;IACH,CAAC,EAAE,GAAG,GAAG,SAAS,CAAC;IAEnB;;;OAGG;IACH,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAExC;;OAEG;IACH,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;CACd"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/invariant",
|
|
3
|
+
"version": "0.1.56-main.bf228a7",
|
|
4
|
+
"description": "Invariant assertion compatible with DXOS logger.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "DXOS.org",
|
|
9
|
+
"main": "dist/lib/node/index.cjs",
|
|
10
|
+
"browser": {
|
|
11
|
+
"./dist/lib/node/index.cjs": "./dist/lib/browser/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/types/src/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@dxos/node-std": "0.1.56-main.bf228a7"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@swc/core": "1.3.70",
|
|
23
|
+
"@types/node": "^18.11.9",
|
|
24
|
+
"typescript": "^5.0.4"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
package/src/invariant.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { CallMetadata } from './meta';
|
|
6
|
+
|
|
7
|
+
export type InvariantFn = (condition: unknown, message?: string, meta?: CallMetadata) => asserts condition;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Asserts that the condition is true.
|
|
11
|
+
*
|
|
12
|
+
* @param message Optional message.
|
|
13
|
+
*/
|
|
14
|
+
export const invariant: InvariantFn = (
|
|
15
|
+
condition: unknown,
|
|
16
|
+
message?: string,
|
|
17
|
+
meta?: CallMetadata,
|
|
18
|
+
): asserts condition => {
|
|
19
|
+
if (condition) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let errorMessage = 'invariant violation';
|
|
24
|
+
|
|
25
|
+
if (message) {
|
|
26
|
+
errorMessage += `: ${message}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (meta?.A) {
|
|
30
|
+
errorMessage += ` [${meta.A[0]}]`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (meta?.F) {
|
|
34
|
+
errorMessage += ` at ${meta.F}:${meta.L}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
throw new InvariantViolation(errorMessage);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export class InvariantViolation extends Error {
|
|
41
|
+
constructor(message: string) {
|
|
42
|
+
super(message);
|
|
43
|
+
// NOTE: Restores prototype chain (https://stackoverflow.com/a/48342359).
|
|
44
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/meta.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Metadata injected b y the log transform plugin.
|
|
7
|
+
*
|
|
8
|
+
* Field names are intentionally short to reduce the size of the generated code.
|
|
9
|
+
*/
|
|
10
|
+
export interface CallMetadata {
|
|
11
|
+
/**
|
|
12
|
+
* File name.
|
|
13
|
+
*/
|
|
14
|
+
F: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Line number.
|
|
18
|
+
*/
|
|
19
|
+
L: number;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Value of `this` at the site of the log call.
|
|
23
|
+
* Will be set to the class instance if the call is inside a method, or to the `globalThis` (`window` or `global`) otherwise.
|
|
24
|
+
*/
|
|
25
|
+
S: any | undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A callback that will invoke the provided function with provided arguments.
|
|
29
|
+
* Useful in the browser to force a `console.log` call to have a certain stack-trace.
|
|
30
|
+
*/
|
|
31
|
+
C?: (fn: Function, args: any[]) => void;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Source code of the argument list.
|
|
35
|
+
*/
|
|
36
|
+
A?: string[];
|
|
37
|
+
}
|