@capture.dev/event-logger 0.0.1

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.
Files changed (92) hide show
  1. package/LICENSE.txt +7 -0
  2. package/dist/event-logger.d.ts +25 -0
  3. package/dist/event-logger.d.ts.map +1 -0
  4. package/dist/event-logger.js +42 -0
  5. package/dist/event-sink.d.ts +11 -0
  6. package/dist/event-sink.d.ts.map +1 -0
  7. package/dist/event-sink.js +32 -0
  8. package/dist/global-state.d.ts +14 -0
  9. package/dist/global-state.d.ts.map +1 -0
  10. package/dist/global-state.js +28 -0
  11. package/dist/index-internal.d.ts +20 -0
  12. package/dist/index-internal.d.ts.map +1 -0
  13. package/dist/index-internal.js +18 -0
  14. package/dist/index.d.ts +5 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -0
  17. package/dist/register-logger.d.ts +8 -0
  18. package/dist/register-logger.d.ts.map +1 -0
  19. package/dist/register-logger.js +24 -0
  20. package/dist/register-state-logger.d.ts +5 -0
  21. package/dist/register-state-logger.d.ts.map +1 -0
  22. package/dist/register-state-logger.js +5 -0
  23. package/dist/snapshot-event-to-operation.d.ts +5 -0
  24. package/dist/snapshot-event-to-operation.d.ts.map +1 -0
  25. package/dist/snapshot-event-to-operation.js +7 -0
  26. package/dist/state-logger.d.ts +37 -0
  27. package/dist/state-logger.d.ts.map +1 -0
  28. package/dist/state-logger.js +90 -0
  29. package/dist/to-json/__tests__/serialize-class.test.d.ts +2 -0
  30. package/dist/to-json/__tests__/serialize-class.test.d.ts.map +1 -0
  31. package/dist/to-json/__tests__/serialize-class.test.js +79 -0
  32. package/dist/to-json/__tests__/serialize-date.test.d.ts +2 -0
  33. package/dist/to-json/__tests__/serialize-date.test.d.ts.map +1 -0
  34. package/dist/to-json/__tests__/serialize-date.test.js +20 -0
  35. package/dist/to-json/__tests__/serialize-error.test.d.ts +2 -0
  36. package/dist/to-json/__tests__/serialize-error.test.d.ts.map +1 -0
  37. package/dist/to-json/__tests__/serialize-error.test.js +71 -0
  38. package/dist/to-json/__tests__/serialize-function.test.d.ts +2 -0
  39. package/dist/to-json/__tests__/serialize-function.test.d.ts.map +1 -0
  40. package/dist/to-json/__tests__/serialize-function.test.js +84 -0
  41. package/dist/to-json/__tests__/serialize-map.test.d.ts +2 -0
  42. package/dist/to-json/__tests__/serialize-map.test.d.ts.map +1 -0
  43. package/dist/to-json/__tests__/serialize-map.test.js +90 -0
  44. package/dist/to-json/__tests__/serialize-object.test.d.ts +2 -0
  45. package/dist/to-json/__tests__/serialize-object.test.d.ts.map +1 -0
  46. package/dist/to-json/__tests__/serialize-object.test.js +138 -0
  47. package/dist/to-json/__tests__/serialize-regex.test.d.ts +2 -0
  48. package/dist/to-json/__tests__/serialize-regex.test.d.ts.map +1 -0
  49. package/dist/to-json/__tests__/serialize-regex.test.js +64 -0
  50. package/dist/to-json/__tests__/serialize-set.test.d.ts +2 -0
  51. package/dist/to-json/__tests__/serialize-set.test.d.ts.map +1 -0
  52. package/dist/to-json/__tests__/serialize-set.test.js +74 -0
  53. package/dist/to-json/__tests__/to-json.test.d.ts +2 -0
  54. package/dist/to-json/__tests__/to-json.test.d.ts.map +1 -0
  55. package/dist/to-json/__tests__/to-json.test.js +356 -0
  56. package/dist/to-json/serialize-class.d.ts +4 -0
  57. package/dist/to-json/serialize-class.d.ts.map +1 -0
  58. package/dist/to-json/serialize-class.js +14 -0
  59. package/dist/to-json/serialize-date.d.ts +3 -0
  60. package/dist/to-json/serialize-date.d.ts.map +1 -0
  61. package/dist/to-json/serialize-date.js +8 -0
  62. package/dist/to-json/serialize-error.d.ts +3 -0
  63. package/dist/to-json/serialize-error.d.ts.map +1 -0
  64. package/dist/to-json/serialize-error.js +22 -0
  65. package/dist/to-json/serialize-function.d.ts +3 -0
  66. package/dist/to-json/serialize-function.d.ts.map +1 -0
  67. package/dist/to-json/serialize-function.js +7 -0
  68. package/dist/to-json/serialize-map.d.ts +4 -0
  69. package/dist/to-json/serialize-map.d.ts.map +1 -0
  70. package/dist/to-json/serialize-map.js +10 -0
  71. package/dist/to-json/serialize-object.d.ts +4 -0
  72. package/dist/to-json/serialize-object.d.ts.map +1 -0
  73. package/dist/to-json/serialize-object.js +17 -0
  74. package/dist/to-json/serialize-regex.d.ts +3 -0
  75. package/dist/to-json/serialize-regex.d.ts.map +1 -0
  76. package/dist/to-json/serialize-regex.js +6 -0
  77. package/dist/to-json/serialize-set.d.ts +4 -0
  78. package/dist/to-json/serialize-set.d.ts.map +1 -0
  79. package/dist/to-json/serialize-set.js +10 -0
  80. package/dist/to-json/serialized-markers.d.ts +23 -0
  81. package/dist/to-json/serialized-markers.d.ts.map +1 -0
  82. package/dist/to-json/serialized-markers.js +22 -0
  83. package/dist/to-json/serialized-types.d.ts +55 -0
  84. package/dist/to-json/serialized-types.d.ts.map +1 -0
  85. package/dist/to-json/serialized-types.js +39 -0
  86. package/dist/to-json/to-json.constants.d.ts +11 -0
  87. package/dist/to-json/to-json.constants.d.ts.map +1 -0
  88. package/dist/to-json/to-json.constants.js +10 -0
  89. package/dist/to-json/to-json.d.ts +14 -0
  90. package/dist/to-json/to-json.d.ts.map +1 -0
  91. package/dist/to-json/to-json.js +141 -0
  92. package/package.json +51 -0
@@ -0,0 +1,39 @@
1
+ const isSerializedWithType = (type, value) => {
2
+ return (typeof value === "object" &&
3
+ value !== null &&
4
+ "__CAPTURE_TYPE__" in value &&
5
+ value.__CAPTURE_TYPE__ === type);
6
+ };
7
+ export const isSerializedPlainObject = (value) => {
8
+ return (typeof value === "object" &&
9
+ value !== null &&
10
+ !Array.isArray(value) &&
11
+ !("__CAPTURE_TYPE__" in value));
12
+ };
13
+ export const isSerializedArray = (value) => {
14
+ return Array.isArray(value);
15
+ };
16
+ export const isSerializedError = (value) => {
17
+ return isSerializedWithType("error", value);
18
+ };
19
+ export const isSerializedMap = (value) => {
20
+ return isSerializedWithType("map", value);
21
+ };
22
+ export const isSerializedSet = (value) => {
23
+ return isSerializedWithType("set", value);
24
+ };
25
+ export const isSerializedRegex = (value) => {
26
+ return isSerializedWithType("regex", value);
27
+ };
28
+ export const isSerializedFunction = (value) => {
29
+ return isSerializedWithType("function", value);
30
+ };
31
+ export const isSerializedClassConstructor = (value) => {
32
+ return isSerializedWithType("classConstructor", value);
33
+ };
34
+ export const isSerializedObject = (value) => {
35
+ return isSerializedWithType("object", value);
36
+ };
37
+ export const isSerializedDate = (value) => {
38
+ return isSerializedWithType("date", value);
39
+ };
@@ -0,0 +1,11 @@
1
+ export declare const REDACTED = "$REDACTED$";
2
+ export declare const UNDEFINED = "$UNDEFINED$";
3
+ export declare const BIG_INT = "$BIGINT$";
4
+ export declare const FUNCTION = "$FUNCTION$";
5
+ export declare const PROMISE = "$PROMISE$";
6
+ export declare const CIRCULAR_REFERENCE = "$CIRCULAR_REFERENCE$";
7
+ export declare const UNKNOWN = "$UNKNOWN$";
8
+ export declare const TOO_LARGE = "$TOO_LARGE$";
9
+ export declare const MAX_DEPTH_REACHED = "$MAX_DEPTH_REACHED$";
10
+ export declare const SERIALIZATION_ERROR = "$SERIALIZATION_ERROR$";
11
+ //# sourceMappingURL=to-json.constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-json.constants.d.ts","sourceRoot":"","sources":["../../src/to-json/to-json.constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,eAAe,CAAC;AACrC,eAAO,MAAM,SAAS,gBAAgB,CAAC;AACvC,eAAO,MAAM,OAAO,aAAa,CAAC;AAClC,eAAO,MAAM,QAAQ,eAAe,CAAC;AACrC,eAAO,MAAM,OAAO,cAAc,CAAC;AACnC,eAAO,MAAM,kBAAkB,yBAAyB,CAAC;AACzD,eAAO,MAAM,OAAO,cAAc,CAAC;AACnC,eAAO,MAAM,SAAS,gBAAgB,CAAC;AACvC,eAAO,MAAM,iBAAiB,wBAAwB,CAAC;AACvD,eAAO,MAAM,mBAAmB,0BAA0B,CAAC"}
@@ -0,0 +1,10 @@
1
+ export const REDACTED = "$REDACTED$";
2
+ export const UNDEFINED = "$UNDEFINED$";
3
+ export const BIG_INT = "$BIGINT$";
4
+ export const FUNCTION = "$FUNCTION$";
5
+ export const PROMISE = "$PROMISE$";
6
+ export const CIRCULAR_REFERENCE = "$CIRCULAR_REFERENCE$";
7
+ export const UNKNOWN = "$UNKNOWN$";
8
+ export const TOO_LARGE = "$TOO_LARGE$";
9
+ export const MAX_DEPTH_REACHED = "$MAX_DEPTH_REACHED$";
10
+ export const SERIALIZATION_ERROR = "$SERIALIZATION_ERROR$";
@@ -0,0 +1,14 @@
1
+ import type { SerializedValue } from "./serialized-types.js";
2
+ export type VisitorFn = (value: unknown) => SerializedValue;
3
+ interface ToJSONOpts {
4
+ maxDepth?: number;
5
+ maxStringLength?: number;
6
+ maxSize?: number;
7
+ onError?: (error: unknown) => void;
8
+ }
9
+ /**
10
+ * Serializes a JS value to a JSON-compatible structure.
11
+ */
12
+ export declare function toJSON<T>(obj: T, opts?: ToJSONOpts): SerializedValue;
13
+ export {};
14
+ //# sourceMappingURL=to-json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-json.d.ts","sourceRoot":"","sources":["../../src/to-json/to-json.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,eAAe,CAAC;AAoB5D,UAAU,UAAU;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACpC;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,GAAE,UAAe,GAAG,eAAe,CAwIxE"}
@@ -0,0 +1,141 @@
1
+ import { isClass, serializeClass } from "./serialize-class.js";
2
+ import { serializeDate } from "./serialize-date.js";
3
+ import { serializeError } from "./serialize-error.js";
4
+ import { serializeFunction } from "./serialize-function.js";
5
+ import { serializeMap } from "./serialize-map.js";
6
+ import { serializeObject } from "./serialize-object.js";
7
+ import { serializeRegex } from "./serialize-regex.js";
8
+ import { serializeSet } from "./serialize-set.js";
9
+ import { BIG_INT, CIRCULAR_REFERENCE, MAX_DEPTH_REACHED, PROMISE, TOO_LARGE, UNDEFINED, UNKNOWN, } from "./to-json.constants.js";
10
+ import { SERIALIZATION_ERROR } from "./to-json.constants.js";
11
+ const hasProp = Object.prototype.hasOwnProperty;
12
+ function throwsMessage(error) {
13
+ return "[Throws: " + (error instanceof Error ? error.message : "?") + "]";
14
+ }
15
+ function safeGetValue(obj, property) {
16
+ if (hasProp.call(obj, property)) {
17
+ try {
18
+ return obj[property];
19
+ }
20
+ catch (err) {
21
+ return throwsMessage(err);
22
+ }
23
+ }
24
+ return obj[property];
25
+ }
26
+ /**
27
+ * Serializes a JS value to a JSON-compatible structure.
28
+ */
29
+ export function toJSON(obj, opts = {}) {
30
+ const seen = [];
31
+ const maxStringLength = opts.maxStringLength ?? 100000;
32
+ const maxSize = opts.maxSize ?? 500000;
33
+ const maxDepth = opts.maxDepth ?? 20;
34
+ let currentSize = 0;
35
+ function visit(obj, depth) {
36
+ try {
37
+ if (depth > maxDepth) {
38
+ return MAX_DEPTH_REACHED;
39
+ }
40
+ if (currentSize > maxSize) {
41
+ return TOO_LARGE;
42
+ }
43
+ if (obj === null ||
44
+ typeof obj === "boolean" ||
45
+ typeof obj === "number" ||
46
+ typeof obj === "string") {
47
+ const size = JSON.stringify(obj).length;
48
+ currentSize += size;
49
+ if (currentSize > maxSize) {
50
+ return TOO_LARGE;
51
+ }
52
+ if (typeof obj === "string" && size > maxStringLength) {
53
+ return (obj.substring(0, maxStringLength) +
54
+ " [Output truncated due to length]");
55
+ }
56
+ return obj;
57
+ }
58
+ else if (typeof obj === "undefined") {
59
+ return UNDEFINED;
60
+ }
61
+ else if (typeof obj === "bigint") {
62
+ if (obj <= Number.MAX_SAFE_INTEGER && obj >= Number.MIN_SAFE_INTEGER) {
63
+ return Number(obj);
64
+ }
65
+ return BIG_INT;
66
+ }
67
+ if (typeof obj === "symbol") {
68
+ return obj.toString();
69
+ }
70
+ if (typeof obj === "function") {
71
+ return isClass(obj) ? serializeClass(obj) : serializeFunction(obj);
72
+ }
73
+ if (obj instanceof Date) {
74
+ return serializeDate(obj);
75
+ }
76
+ if (obj instanceof Error) {
77
+ return serializeError(obj);
78
+ }
79
+ if (obj instanceof RegExp) {
80
+ return serializeRegex(obj);
81
+ }
82
+ if (obj instanceof Promise) {
83
+ return PROMISE;
84
+ }
85
+ if (seen.indexOf(obj) !== -1) {
86
+ return CIRCULAR_REFERENCE;
87
+ }
88
+ seen.push(obj);
89
+ if (obj instanceof Map) {
90
+ const result = serializeMap(obj, (value) => visit(value, depth + 1));
91
+ seen.pop();
92
+ return result;
93
+ }
94
+ if (obj instanceof Set) {
95
+ const result = serializeSet(obj, (value) => visit(value, depth + 1));
96
+ seen.pop();
97
+ return result;
98
+ }
99
+ if (Array.isArray(obj)) {
100
+ const aResult = obj.map((item) => visit(item, depth + 1));
101
+ seen.pop();
102
+ return aResult;
103
+ }
104
+ if (typeof obj === "object") {
105
+ if ("toJSON" in obj && typeof obj.toJSON === "function") {
106
+ try {
107
+ const fResult = visit(obj.toJSON(), depth + 1);
108
+ seen.pop();
109
+ return fResult;
110
+ }
111
+ catch (err) {
112
+ return throwsMessage(err);
113
+ }
114
+ }
115
+ const isPlainObject = Object.getPrototypeOf(obj) === Object.prototype;
116
+ if (!isPlainObject) {
117
+ const result = serializeObject(obj, (value) => visit(value, depth + 1));
118
+ seen.pop();
119
+ return result;
120
+ }
121
+ const result = {};
122
+ for (const prop of Object.keys(obj)) {
123
+ result[prop] = visit(safeGetValue(obj, prop), depth + 1);
124
+ }
125
+ seen.pop();
126
+ return result;
127
+ }
128
+ return UNKNOWN;
129
+ }
130
+ catch (error) {
131
+ try {
132
+ opts.onError?.(error);
133
+ }
134
+ catch {
135
+ // ignore
136
+ }
137
+ return SERIALIZATION_ERROR;
138
+ }
139
+ }
140
+ return visit(obj, 0);
141
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@capture.dev/event-logger",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "sideEffects": false,
7
+ "license": "MIT",
8
+ "homepage": "https://capture.dev",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ },
14
+ "./internal": {
15
+ "import": "./dist/index-internal.js",
16
+ "require": "./dist/index-internal.cjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist/**"
21
+ ],
22
+ "scripts": {
23
+ "lint": "eslint src/",
24
+ "lint:fix": "npm run lint -- --fix",
25
+ "build": "tsc",
26
+ "test": "vitest",
27
+ "clean": "rimraf dist"
28
+ },
29
+ "devDependencies": {
30
+ "@capture/vitest-config": "*",
31
+ "clean-publish": "^6.0.1",
32
+ "eslint": "^9.29.0",
33
+ "tsup": "^8.5.1",
34
+ "typescript": "^5.9.3",
35
+ "vitest": "^3.0.9"
36
+ },
37
+ "volta": {
38
+ "extends": "../../package.json"
39
+ },
40
+ "lint-staged": {
41
+ "**/*.{ts,tsx,js}": [
42
+ "eslint --fix",
43
+ "prettier --write"
44
+ ],
45
+ "**/*.{json,md,css,html}": "prettier --write"
46
+ },
47
+ "tscLib": "true",
48
+ "dependencies": {
49
+ "@capture.dev/fast-json-patch": "^3.2.1"
50
+ }
51
+ }