@steve02081504/virtual-console 0.1.6 → 0.1.7

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/browser.d.mts ADDED
@@ -0,0 +1,110 @@
1
+ import type { AsyncLocalStorage } from 'node:async_hooks'
2
+ import type { Console } from 'node:console'
3
+ import type { Writable } from 'node:stream'
4
+
5
+ /**
6
+ * 虚拟控制台配置选项
7
+ */
8
+ export interface VirtualConsoleOptions {
9
+ /** 如果为 true,则在捕获输出的同时,也调用底层控制台进行实际输出 */
10
+ realConsoleOutput?: boolean
11
+ /** 如果为 true,则捕获输出并保存在 outputs 属性中 */
12
+ recordOutput?: boolean
13
+ /** 专门处理单个 Error 对象的错误处理器 */
14
+ error_handler?: ((error: Error) => void) | null
15
+ /** 用于 realConsoleOutput 的底层控制台实例 */
16
+ base_console?: Console
17
+ }
18
+
19
+ /**
20
+ * 控制台反射逻辑
21
+ */
22
+ export interface ConsoleReflect {
23
+ /** 从默认控制台获取当前控制台对象 */
24
+ Reflect: () => Console
25
+ /** 设置当前控制台对象的函数 */
26
+ ReflectSet: (value: VirtualConsole) => void
27
+ /** 在新的异步上下文中执行函数的函数 */
28
+ ReflectRun: <T>(value: VirtualConsole, fn: () => T | Promise<T>) => Promise<T>
29
+ }
30
+
31
+ /**
32
+ * 虚拟流,包装 Node.js 可写流
33
+ */
34
+ interface VirtualStream extends Writable {
35
+ /** 是否为 TTY */
36
+ readonly isTTY: boolean
37
+ /** 列数 */
38
+ readonly columns: number
39
+ /** 行数 */
40
+ readonly rows: number
41
+ /** 获取底层目标流 */
42
+ readonly targetStream: NodeJS.WritableStream
43
+ /** 获取颜色深度 */
44
+ getColorDepth(): number
45
+ /** 判断是否支持颜色 */
46
+ hasColors(): boolean
47
+ }
48
+
49
+ /**
50
+ * 虚拟控制台,用于捕获输出,同时可以选择性地将输出传递给真实的控制台
51
+ */
52
+ export class VirtualConsole extends Console {
53
+ /** 捕获的所有输出 */
54
+ outputs: string
55
+ /** 捕获的所有输出 (HTML) */
56
+ outputsHtml: string
57
+ /** 最终合并后的配置项 */
58
+ options: Required<Omit<VirtualConsoleOptions, 'base_console'>> & {
59
+ base_console?: Console
60
+ }
61
+ /** 用于 realConsoleOutput 的底层控制台实例 */
62
+ base_console: Console
63
+
64
+ constructor(options?: VirtualConsoleOptions)
65
+
66
+ /**
67
+ * 若提供 fn,则在新的异步上下文中执行 fn,并将 fn 上下文的控制台替换为此对象。
68
+ * 否则,将当前异步上下文中的控制台替换为此对象。
69
+ * @param fn 在新的异步上下文中执行的函数
70
+ * @returns 若提供 fn,则返回 fn 的 Promise 结果;否则返回 void
71
+ */
72
+ hookAsyncContext<T>(fn?: () => T | Promise<T>): Promise<T> | void
73
+
74
+ /**
75
+ * 在终端中打印一行,等同于 log。
76
+ * @param id 用于标识可覆盖行的唯一 ID
77
+ * @param args 要打印的内容
78
+ */
79
+ freshLine(id: string, ...args: unknown[]): void
80
+
81
+ /** 清空捕获的输出,并选择性地清空真实控制台 */
82
+ clear(): void
83
+ }
84
+
85
+ /** 全局异步存储,浏览器环境下不存在 */
86
+ export const consoleAsyncStorage: undefined
87
+
88
+ /** 默认的虚拟控制台实例 */
89
+ export const defaultConsole: VirtualConsole
90
+
91
+ /** 全局控制台的附加属性 */
92
+ export const globalConsoleAdditionalProperties: Record<string, unknown>
93
+
94
+ /**
95
+ * 设置全局控制台反射逻辑
96
+ * @param Reflect 从默认控制台映射到新的控制台对象的函数
97
+ * @param ReflectSet 设置当前控制台对象的函数
98
+ * @param ReflectRun 在新的异步上下文中执行函数的函数
99
+ */
100
+ export function setGlobalConsoleReflect(
101
+ Reflect: (defaultConsole: VirtualConsole) => Console,
102
+ ReflectSet: (value: VirtualConsole) => void,
103
+ ReflectRun: <T>(value: VirtualConsole, fn: () => T | Promise<T>) => Promise<T>
104
+ ): void
105
+
106
+ /** 获取全局控制台反射逻辑 */
107
+ export function getGlobalConsoleReflect(): ConsoleReflect
108
+
109
+ /** 全局控制台实例(代理对象,委托给当前活动的虚拟控制台) */
110
+ export const console: Console
package/main.d.mts ADDED
@@ -0,0 +1,113 @@
1
+ import type { AsyncLocalStorage } from 'node:async_hooks'
2
+ import type { Console } from 'node:console'
3
+ import type { Writable } from 'node:stream'
4
+
5
+ /**
6
+ * 虚拟控制台配置选项
7
+ */
8
+ export interface VirtualConsoleOptions {
9
+ /** 如果为 true,则在捕获输出的同时,也调用底层控制台进行实际输出 */
10
+ realConsoleOutput?: boolean
11
+ /** 如果为 true,则捕获输出并保存在 outputs 属性中 */
12
+ recordOutput?: boolean
13
+ /** 如果为 true,则启用 ANSI 转义序列支持(Node.js 环境) */
14
+ supportsAnsi?: boolean
15
+ /** 专门处理单个 Error 对象的错误处理器 */
16
+ error_handler?: ((error: Error) => void) | null
17
+ /** 用于 realConsoleOutput 的底层控制台实例 */
18
+ base_console?: Console
19
+ }
20
+
21
+ /**
22
+ * 控制台反射逻辑
23
+ */
24
+ export interface ConsoleReflect {
25
+ /** 从默认控制台获取当前控制台对象 */
26
+ Reflect: () => Console
27
+ /** 设置当前控制台对象的函数 */
28
+ ReflectSet: (value: VirtualConsole) => void
29
+ /** 在新的异步上下文中执行函数的函数 */
30
+ ReflectRun: <T>(value: VirtualConsole, fn: () => T | Promise<T>) => Promise<T>
31
+ }
32
+
33
+ /**
34
+ * 虚拟流,包装 Node.js 可写流
35
+ */
36
+ interface VirtualStream extends Writable {
37
+ /** 是否为 TTY */
38
+ readonly isTTY: boolean
39
+ /** 列数 */
40
+ readonly columns: number
41
+ /** 行数 */
42
+ readonly rows: number
43
+ /** 获取底层目标流 */
44
+ readonly targetStream: NodeJS.WritableStream
45
+ /** 获取颜色深度 */
46
+ getColorDepth(): number
47
+ /** 判断是否支持颜色 */
48
+ hasColors(): boolean
49
+ }
50
+
51
+ /**
52
+ * 虚拟控制台,用于捕获输出,同时可以选择性地将输出传递给真实的控制台
53
+ */
54
+ export class VirtualConsole extends Console {
55
+ /** 捕获的所有输出 */
56
+ outputs: string
57
+ /** 捕获的所有输出 (HTML) */
58
+ outputsHtml: string
59
+ /** 最终合并后的配置项 */
60
+ options: Required<Omit<VirtualConsoleOptions, 'base_console'>> & {
61
+ base_console?: Console
62
+ }
63
+ /** 用于 realConsoleOutput 的底层控制台实例 */
64
+ base_console: Console
65
+
66
+ constructor(options?: VirtualConsoleOptions)
67
+
68
+ /**
69
+ * 若提供 fn,则在新的异步上下文中执行 fn,并将 fn 上下文的控制台替换为此对象。
70
+ * 否则,将当前异步上下文中的控制台替换为此对象。
71
+ * @param fn 在新的异步上下文中执行的函数
72
+ * @returns 若提供 fn,则返回 fn 的 Promise 结果;否则返回 void
73
+ */
74
+ hookAsyncContext<T>(fn?: () => T | Promise<T>): Promise<T> | void
75
+
76
+ /**
77
+ * 在终端中打印一行,如果前一次调用也是具有相同 ID 的 freshLine,
78
+ * 则会覆盖上一行而不是打印新行(Node.js 环境)。浏览器环境等同于 log。
79
+ * @param id 用于标识可覆盖行的唯一 ID
80
+ * @param args 要打印的内容
81
+ */
82
+ freshLine(id: string, ...args: unknown[]): void
83
+
84
+ /** 清空捕获的输出,并选择性地清空真实控制台 */
85
+ clear(): void
86
+ }
87
+
88
+ /** 全局异步存储,用于管理控制台上下文(Node.js 环境) */
89
+ export const consoleAsyncStorage: AsyncLocalStorage<VirtualConsole>
90
+
91
+ /** 默认的虚拟控制台实例 */
92
+ export const defaultConsole: VirtualConsole
93
+
94
+ /** 全局控制台的附加属性 */
95
+ export const globalConsoleAdditionalProperties: Record<string, unknown>
96
+
97
+ /**
98
+ * 设置全局控制台反射逻辑
99
+ * @param Reflect 从默认控制台映射到新的控制台对象的函数
100
+ * @param ReflectSet 设置当前控制台对象的函数
101
+ * @param ReflectRun 在新的异步上下文中执行函数的函数
102
+ */
103
+ export function setGlobalConsoleReflect(
104
+ Reflect: (defaultConsole: VirtualConsole) => Console,
105
+ ReflectSet: (value: VirtualConsole) => void,
106
+ ReflectRun: <T>(value: VirtualConsole, fn: () => T | Promise<T>) => Promise<T>
107
+ ): void
108
+
109
+ /** 获取全局控制台反射逻辑 */
110
+ export function getGlobalConsoleReflect(): ConsoleReflect
111
+
112
+ /** 全局控制台实例(代理对象,委托给当前活动的虚拟控制台) */
113
+ export const console: Console
package/node.d.mts ADDED
@@ -0,0 +1,113 @@
1
+ import type { AsyncLocalStorage } from 'node:async_hooks'
2
+ import type { Console } from 'node:console'
3
+ import type { Writable } from 'node:stream'
4
+
5
+ /**
6
+ * 虚拟控制台配置选项
7
+ */
8
+ export interface VirtualConsoleOptions {
9
+ /** 如果为 true,则在捕获输出的同时,也调用底层控制台进行实际输出 */
10
+ realConsoleOutput?: boolean
11
+ /** 如果为 true,则捕获输出并保存在 outputs 属性中 */
12
+ recordOutput?: boolean
13
+ /** 如果为 true,则启用 ANSI 转义序列支持 */
14
+ supportsAnsi?: boolean
15
+ /** 专门处理单个 Error 对象的错误处理器 */
16
+ error_handler?: ((error: Error) => void) | null
17
+ /** 用于 realConsoleOutput 的底层控制台实例 */
18
+ base_console?: Console
19
+ }
20
+
21
+ /**
22
+ * 控制台反射逻辑
23
+ */
24
+ export interface ConsoleReflect {
25
+ /** 从默认控制台获取当前控制台对象 */
26
+ Reflect: () => Console
27
+ /** 设置当前控制台对象的函数 */
28
+ ReflectSet: (value: VirtualConsole) => void
29
+ /** 在新的异步上下文中执行函数的函数 */
30
+ ReflectRun: <T>(value: VirtualConsole, fn: () => T | Promise<T>) => Promise<T>
31
+ }
32
+
33
+ /**
34
+ * 虚拟流,包装 Node.js 可写流
35
+ */
36
+ interface VirtualStream extends Writable {
37
+ /** 是否为 TTY */
38
+ readonly isTTY: boolean
39
+ /** 列数 */
40
+ readonly columns: number
41
+ /** 行数 */
42
+ readonly rows: number
43
+ /** 获取底层目标流 */
44
+ readonly targetStream: NodeJS.WritableStream
45
+ /** 获取颜色深度 */
46
+ getColorDepth(): number
47
+ /** 判断是否支持颜色 */
48
+ hasColors(): boolean
49
+ }
50
+
51
+ /**
52
+ * 虚拟控制台,用于捕获输出,同时可以选择性地将输出传递给真实的控制台
53
+ */
54
+ export class VirtualConsole extends Console {
55
+ /** 捕获的所有输出 */
56
+ outputs: string
57
+ /** 捕获的所有输出 (HTML) */
58
+ outputsHtml: string
59
+ /** 最终合并后的配置项 */
60
+ options: Required<Omit<VirtualConsoleOptions, 'base_console'>> & {
61
+ base_console?: Console
62
+ }
63
+ /** 用于 realConsoleOutput 的底层控制台实例 */
64
+ base_console: Console
65
+
66
+ constructor(options?: VirtualConsoleOptions)
67
+
68
+ /**
69
+ * 若提供 fn,则在新的异步上下文中执行 fn,并将 fn 上下文的控制台替换为此对象。
70
+ * 否则,将当前异步上下文中的控制台替换为此对象。
71
+ * @param fn 在新的异步上下文中执行的函数
72
+ * @returns 若提供 fn,则返回 fn 的 Promise 结果;否则返回 void
73
+ */
74
+ hookAsyncContext<T>(fn?: () => T | Promise<T>): Promise<T> | void
75
+
76
+ /**
77
+ * 在终端中打印一行,如果前一次调用也是具有相同 ID 的 freshLine,
78
+ * 则会覆盖上一行而不是打印新行。
79
+ * @param id 用于标识可覆盖行的唯一 ID
80
+ * @param args 要打印的内容
81
+ */
82
+ freshLine(id: string, ...args: unknown[]): void
83
+
84
+ /** 清空捕获的输出,并选择性地清空真实控制台 */
85
+ clear(): void
86
+ }
87
+
88
+ /** 全局异步存储,用于管理控制台上下文 */
89
+ export const consoleAsyncStorage: AsyncLocalStorage<VirtualConsole>
90
+
91
+ /** 默认的虚拟控制台实例 */
92
+ export const defaultConsole: VirtualConsole
93
+
94
+ /** 全局控制台的附加属性 */
95
+ export const globalConsoleAdditionalProperties: Record<string, unknown>
96
+
97
+ /**
98
+ * 设置全局控制台反射逻辑
99
+ * @param Reflect 从默认控制台映射到新的控制台对象的函数
100
+ * @param ReflectSet 设置当前控制台对象的函数
101
+ * @param ReflectRun 在新的异步上下文中执行函数的函数
102
+ */
103
+ export function setGlobalConsoleReflect(
104
+ Reflect: (defaultConsole: VirtualConsole) => Console,
105
+ ReflectSet: (value: VirtualConsole) => void,
106
+ ReflectRun: <T>(value: VirtualConsole, fn: () => T | Promise<T>) => Promise<T>
107
+ ): void
108
+
109
+ /** 获取全局控制台反射逻辑 */
110
+ export function getGlobalConsoleReflect(): ConsoleReflect
111
+
112
+ /** 全局控制台实例(代理对象,委托给当前活动的虚拟控制台) */
113
+ export const console: Console
package/node.mjs CHANGED
@@ -152,6 +152,14 @@ class VirtualStream extends Writable {
152
152
  hasColors() {
153
153
  return this.#targetStream.hasColors()
154
154
  }
155
+
156
+ /**
157
+ * 获取底层目标流。
158
+ * @returns {NodeJS.WritableStream} 底层目标流。
159
+ */
160
+ get targetStream() {
161
+ return this.#targetStream
162
+ }
155
163
  }
156
164
 
157
165
  /**
@@ -194,6 +202,12 @@ export class VirtualConsole extends Console {
194
202
  /** @type {Console} - 用于 realConsoleOutput 的底层控制台实例 */
195
203
  #base_console
196
204
 
205
+ /** @private @type {VirtualStream} - 标准输出流 */
206
+ #_stdout
207
+
208
+ /** @private @type {VirtualStream} - 标准错误流 */
209
+ #_stderr
210
+
197
211
  /** @private @type {string | null} - 用于 freshLine 功能,记录上一次 freshLine 的 ID */
198
212
  #loggedFreshLineId = null
199
213
 
@@ -207,6 +221,8 @@ export class VirtualConsole extends Console {
207
221
  */
208
222
  constructor(options = {}) {
209
223
  super(new Writable({ /** 啥也不干 */ write: () => { } }), new Writable({ /** 啥也不干 */ write: () => { } }))
224
+ for (const property of ['_stdout', '_stderr'])
225
+ delete this[property] // 因为父类的实例属性会遮蔽子类的getter/setter,所以需要删除这些字段
210
226
 
211
227
  const base_console = options.base_console || consoleReflect()
212
228
  delete options.base_console
@@ -239,22 +255,11 @@ export class VirtualConsole extends Console {
239
255
  }
240
256
 
241
257
  /**
242
- * 获取用于 realConsoleOutput 的底层控制台实例。
243
- * @returns {Console} 底层控制台实例。
244
- */
245
- get base_console() {
246
- return this.#base_console
247
- }
248
-
249
- /**
250
- * 设置用于 realConsoleOutput 的底层控制台实例。
251
- * @param {Console} value - 底层控制台实例。
252
- * @returns {void}
258
+ * 获取虚拟控制台的上下文对象,用于创建 VirtualStream。
259
+ * @returns {object} 上下文对象。
253
260
  */
254
- set base_console(value) {
255
- this.#base_console = value
256
-
257
- const context = {
261
+ #getStreamContext() {
262
+ return {
258
263
  /**
259
264
  * 写入完成时的回调函数,用于重置 loggedFreshLineId。
260
265
  * @returns {void}
@@ -265,9 +270,63 @@ export class VirtualConsole extends Console {
265
270
  options: this.options,
266
271
  state: this
267
272
  }
273
+ }
274
+
275
+ /**
276
+ * 获取标准输出流。
277
+ * @returns {VirtualStream} 标准输出流。
278
+ */
279
+ get _stdout() {
280
+ return this.#_stdout
281
+ }
282
+
283
+ /**
284
+ * 设置标准输出流,自动将其包装为 VirtualStream。
285
+ * @param {NodeJS.WritableStream | VirtualStream} value - 要设置的流。
286
+ * @returns {void}
287
+ */
288
+ set _stdout(value) {
289
+ const context = this.#getStreamContext()
290
+ const targetStream = value?.targetStream || value || process.stdout
291
+ this.#_stdout = new VirtualStream(targetStream, context)
292
+ }
293
+
294
+ /**
295
+ * 获取标准错误流。
296
+ * @returns {VirtualStream} 标准错误流。
297
+ */
298
+ get _stderr() {
299
+ return this.#_stderr
300
+ }
301
+
302
+ /**
303
+ * 设置标准错误流,自动将其包装为 VirtualStream。
304
+ * @param {NodeJS.WritableStream | VirtualStream} value - 要设置的流。
305
+ * @returns {void}
306
+ */
307
+ set _stderr(value) {
308
+ const context = this.#getStreamContext()
309
+ const targetStream = value?.targetStream || value || process.stderr
310
+ this.#_stderr = new VirtualStream(targetStream, context)
311
+ }
312
+
313
+ /**
314
+ * 设置用于 realConsoleOutput 的底层控制台实例。
315
+ * @param {Console} value - 底层控制台实例。
316
+ * @returns {void}
317
+ */
318
+ set base_console(value) {
319
+ this.#base_console = value || globalThis.console
320
+ this._stdout = this.#base_console?._stdout
321
+ this._stderr = this.#base_console?._stderr
322
+ }
268
323
 
269
- this._stdout = new VirtualStream(this.#base_console?._stdout || process.stdout, context)
270
- this._stderr = new VirtualStream(this.#base_console?._stderr || process.stderr, context)
324
+ /**
325
+ * 获取用于 realConsoleOutput 的底层控制台实例。
326
+ * @returns {Console} 底层控制台实例。
327
+ */
328
+ get base_console() {
329
+ return this.#base_console
271
330
  }
272
331
 
273
332
  /**
package/package.json CHANGED
@@ -1,9 +1,35 @@
1
1
  {
2
2
  "name": "@steve02081504/virtual-console",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "A virtual console for capturing and manipulating terminal output.",
5
5
  "main": "main.mjs",
6
6
  "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "browser": {
10
+ "types": "./browser.d.mts",
11
+ "default": "./browser.mjs"
12
+ },
13
+ "node": {
14
+ "types": "./node.d.mts",
15
+ "default": "./node.mjs"
16
+ },
17
+ "types": "./main.d.mts",
18
+ "default": "./main.mjs"
19
+ },
20
+ "./node": {
21
+ "types": "./node.d.mts",
22
+ "default": "./node.mjs"
23
+ },
24
+ "./browser": {
25
+ "types": "./browser.d.mts",
26
+ "default": "./browser.mjs"
27
+ }
28
+ },
29
+ "types": "./main.d.mts",
30
+ "scripts": {
31
+ "test": "node test.mjs"
32
+ },
7
33
  "keywords": [
8
34
  "console",
9
35
  "virtual",
@@ -17,9 +43,9 @@
17
43
  },
18
44
  "homepage": "https://github.com/steve02081504/virtual-console#readme",
19
45
  "dependencies": {
46
+ "ansi_up": "latest",
20
47
  "ansi-escapes": "latest",
21
48
  "full-proxy": "latest",
22
- "supports-ansi": "latest",
23
- "ansi_up": "latest"
49
+ "supports-ansi": "latest"
24
50
  }
25
51
  }