@ricsam/isolate-console 0.1.1 → 0.1.2

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/src/index.ts DELETED
@@ -1,215 +0,0 @@
1
- import ivm from "isolated-vm";
2
-
3
- export interface ConsoleOptions {
4
- onLog?: (level: string, ...args: unknown[]) => void;
5
- onTime?: (label: string, duration: number) => void;
6
- onTimeLog?: (label: string, duration: number, ...args: unknown[]) => void;
7
- onCount?: (label: string, count: number) => void;
8
- onCountReset?: (label: string) => void;
9
- onGroup?: (label: string, collapsed: boolean) => void;
10
- onGroupEnd?: () => void;
11
- onClear?: () => void;
12
- onAssert?: (condition: boolean, ...args: unknown[]) => void;
13
- }
14
-
15
- export interface ConsoleHandle {
16
- dispose(): void;
17
- reset(): void;
18
- getTimers(): Map<string, number>;
19
- getCounters(): Map<string, number>;
20
- getGroupDepth(): number;
21
- }
22
-
23
- /**
24
- * Setup console API in an isolated-vm context
25
- *
26
- * Injects console.log, console.warn, console.error, console.info, console.debug,
27
- * console.trace, console.dir, console.table, console.time, console.timeEnd,
28
- * console.timeLog, console.count, console.countReset, console.group,
29
- * console.groupCollapsed, console.groupEnd, console.clear, console.assert
30
- *
31
- * @example
32
- * const handle = await setupConsole(context, {
33
- * onLog: (level, ...args) => console.log(`[${level}]`, ...args)
34
- * });
35
- */
36
- export async function setupConsole(
37
- context: ivm.Context,
38
- options?: ConsoleOptions
39
- ): Promise<ConsoleHandle> {
40
- const opts = options ?? {};
41
-
42
- // State management
43
- const timers = new Map<string, number>();
44
- const counters = new Map<string, number>();
45
- let groupDepth = 0;
46
-
47
- const global = context.global;
48
-
49
- // Log-level methods
50
- const logLevels = [
51
- "log",
52
- "warn",
53
- "error",
54
- "debug",
55
- "info",
56
- "trace",
57
- "dir",
58
- "table",
59
- ];
60
-
61
- for (const level of logLevels) {
62
- global.setSync(
63
- `__console_${level}`,
64
- new ivm.Callback((...args: unknown[]) => {
65
- opts.onLog?.(level, ...args);
66
- })
67
- );
68
- }
69
-
70
- // Timing methods
71
- global.setSync(
72
- "__console_time",
73
- new ivm.Callback((label?: string) => {
74
- const l = label ?? "default";
75
- timers.set(l, performance.now());
76
- })
77
- );
78
-
79
- global.setSync(
80
- "__console_timeEnd",
81
- new ivm.Callback((label?: string) => {
82
- const l = label ?? "default";
83
- const start = timers.get(l);
84
- if (start !== undefined) {
85
- const duration = performance.now() - start;
86
- timers.delete(l);
87
- opts.onTime?.(l, duration);
88
- }
89
- })
90
- );
91
-
92
- global.setSync(
93
- "__console_timeLog",
94
- new ivm.Callback((label?: string, ...args: unknown[]) => {
95
- const l = label ?? "default";
96
- const start = timers.get(l);
97
- if (start !== undefined) {
98
- const duration = performance.now() - start;
99
- opts.onTimeLog?.(l, duration, ...args);
100
- }
101
- })
102
- );
103
-
104
- // Counting methods
105
- global.setSync(
106
- "__console_count",
107
- new ivm.Callback((label?: string) => {
108
- const l = label ?? "default";
109
- const count = (counters.get(l) ?? 0) + 1;
110
- counters.set(l, count);
111
- opts.onCount?.(l, count);
112
- })
113
- );
114
-
115
- global.setSync(
116
- "__console_countReset",
117
- new ivm.Callback((label?: string) => {
118
- const l = label ?? "default";
119
- counters.delete(l);
120
- opts.onCountReset?.(l);
121
- })
122
- );
123
-
124
- // Grouping methods
125
- global.setSync(
126
- "__console_group",
127
- new ivm.Callback((label?: string) => {
128
- const l = label ?? "default";
129
- groupDepth++;
130
- opts.onGroup?.(l, false);
131
- })
132
- );
133
-
134
- global.setSync(
135
- "__console_groupCollapsed",
136
- new ivm.Callback((label?: string) => {
137
- const l = label ?? "default";
138
- groupDepth++;
139
- opts.onGroup?.(l, true);
140
- })
141
- );
142
-
143
- global.setSync(
144
- "__console_groupEnd",
145
- new ivm.Callback(() => {
146
- if (groupDepth > 0) {
147
- groupDepth--;
148
- }
149
- opts.onGroupEnd?.();
150
- })
151
- );
152
-
153
- // Other methods
154
- global.setSync(
155
- "__console_clear",
156
- new ivm.Callback(() => {
157
- opts.onClear?.();
158
- })
159
- );
160
-
161
- global.setSync(
162
- "__console_assert",
163
- new ivm.Callback((condition: boolean, ...args: unknown[]) => {
164
- if (!condition) {
165
- opts.onAssert?.(condition, ...args);
166
- }
167
- })
168
- );
169
-
170
- // Inject console object
171
- context.evalSync(`
172
- globalThis.console = {
173
- log: __console_log,
174
- warn: __console_warn,
175
- error: __console_error,
176
- debug: __console_debug,
177
- info: __console_info,
178
- trace: __console_trace,
179
- dir: __console_dir,
180
- table: __console_table,
181
- time: __console_time,
182
- timeEnd: __console_timeEnd,
183
- timeLog: __console_timeLog,
184
- count: __console_count,
185
- countReset: __console_countReset,
186
- group: __console_group,
187
- groupCollapsed: __console_groupCollapsed,
188
- groupEnd: __console_groupEnd,
189
- clear: __console_clear,
190
- assert: __console_assert,
191
- };
192
- `);
193
-
194
- return {
195
- dispose() {
196
- timers.clear();
197
- counters.clear();
198
- groupDepth = 0;
199
- },
200
- reset() {
201
- timers.clear();
202
- counters.clear();
203
- groupDepth = 0;
204
- },
205
- getTimers() {
206
- return new Map(timers);
207
- },
208
- getCounters() {
209
- return new Map(counters);
210
- },
211
- getGroupDepth() {
212
- return groupDepth;
213
- },
214
- };
215
- }