@steve02081504/virtual-console 0.0.1 → 0.0.3

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 (2) hide show
  1. package/main.mjs +21 -7
  2. package/package.json +1 -1
package/main.mjs CHANGED
@@ -6,6 +6,10 @@ import supportsAnsi from 'supports-ansi'
6
6
  import { FullProxy } from 'full-proxy'
7
7
 
8
8
  export const consoleAsyncStorage = new AsyncLocalStorage()
9
+ const cleanupRegistry = new FinalizationRegistry(cleanupToken => {
10
+ const { stream, listener } = cleanupToken
11
+ stream.off?.('resize', listener)
12
+ })
9
13
 
10
14
  /**
11
15
  * 创建一个虚拟控制台,用于捕获输出,同时可以选择性地将输出传递给真实的控制台。
@@ -86,8 +90,9 @@ export class VirtualConsole extends Console {
86
90
  if (this.options.recordOutput)
87
91
  this.outputs += chunk.toString()
88
92
  if (this.options.realConsoleOutput)
89
- targetStream.write(chunk, encoding)
90
- callback()
93
+ targetStream.write(chunk, encoding, callback)
94
+ else
95
+ callback()
91
96
  },
92
97
  })
93
98
 
@@ -100,16 +105,25 @@ export class VirtualConsole extends Console {
100
105
  hasColors: { get: () => targetStream.hasColors.bind(targetStream), configurable: true, enumerable: true },
101
106
  })
102
107
 
103
- targetStream.on?.('resize', () => {
104
- virtualStream.emit('resize')
105
- })
108
+ const virtualStreamRef = new WeakRef(virtualStream)
109
+
110
+ const resizeListener = () => {
111
+ virtualStreamRef.deref()?.emit('resize')
112
+ }
113
+
114
+ targetStream.on?.('resize', resizeListener)
115
+
116
+ cleanupRegistry.register(this, {
117
+ stream: targetStream,
118
+ listener: resizeListener,
119
+ }, this)
106
120
  }
107
121
 
108
122
  return virtualStream
109
123
  }
110
124
 
111
- this._stdout = createVirtualStream(this.#base_console._stdout || process.stdout)
112
- this._stderr = createVirtualStream(this.#base_console._stderr || process.stderr)
125
+ this._stdout = createVirtualStream(this.#base_console?._stdout || process.stdout)
126
+ this._stderr = createVirtualStream(this.#base_console?._stderr || process.stderr)
113
127
  }
114
128
 
115
129
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve02081504/virtual-console",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A virtual console for capturing and manipulating terminal output.",
5
5
  "main": "main.mjs",
6
6
  "type": "module",