| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,14 +31,24 @@ process.removeListener('removeListener', stopListeningIfSignal); | |||
| 31 | 31 | ||
| 32 | 32 | const { | |
| 33 | 33 | createWorkerStdio, | |
| 34 | + kStdioWantsMoreDataCallback, | ||
| 34 | 35 | } = require('internal/worker/io'); | |
| 35 | 36 | ||
| 36 | 37 | let workerStdio; | |
| 37 | 38 | function lazyWorkerStdio() { | |
| 38 | - if (!workerStdio) workerStdio = createWorkerStdio(); | ||
| 39 | + if (workerStdio === undefined) { | ||
| 40 | + workerStdio = createWorkerStdio(); | ||
| 41 | + process.on('exit', flushSync); | ||
| 42 | + } | ||
| 43 | + | ||
| 39 | 44 | return workerStdio; | |
| 40 | 45 | } | |
| 41 | 46 | ||
| 47 | + function flushSync() { | ||
| 48 | + workerStdio.stdout[kStdioWantsMoreDataCallback](); | ||
| 49 | + workerStdio.stderr[kStdioWantsMoreDataCallback](); | ||
| 50 | + } | ||
| 51 | + | ||
| 42 | 52 | function getStdout() { return lazyWorkerStdio().stdout; } | |
| 43 | 53 | ||
| 44 | 54 | function getStderr() { return lazyWorkerStdio().stderr; } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -373,9 +373,13 @@ class WritableWorkerStdio extends Writable { | |||
| 373 | 373 | chunks: ArrayPrototypeMap(chunks, | |
| 374 | 374 | ({ chunk, encoding }) => ({ chunk, encoding })), | |
| 375 | 375 | }); | |
| 376 | - ArrayPrototypePush(this[kWritableCallbacks], cb); | ||
| 377 | - if (this[kPort][kWaitingStreams]++ === 0) | ||
| 378 | - this[kPort].ref(); | ||
| 376 | + if (process._exiting) { | ||
| 377 | + cb(); | ||
| 378 | + } else { | ||
| 379 | + ArrayPrototypePush(this[kWritableCallbacks], cb); | ||
| 380 | + if (this[kPort][kWaitingStreams]++ === 0) | ||
| 381 | + this[kPort].ref(); | ||
| 382 | + } | ||
| 379 | 383 | } | |
| 380 | 384 | ||
| 381 | 385 | _final(cb) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,24 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { Worker, isMainThread } = require('worker_threads'); | ||
| 5 | + | ||
| 6 | + if (isMainThread) { | ||
| 7 | + const w = new Worker(__filename, { stdout: true }); | ||
| 8 | + const expected = 'hello world'; | ||
| 9 | + | ||
| 10 | + let data = ''; | ||
| 11 | + w.stdout.setEncoding('utf8'); | ||
| 12 | + w.stdout.on('data', (chunk) => { | ||
| 13 | + data += chunk; | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + w.on('exit', common.mustCall(() => { | ||
| 17 | + assert.strictEqual(data, expected); | ||
| 18 | + })); | ||
| 19 | + } else { | ||
| 20 | + process.stdout.write('hello'); | ||
| 21 | + process.stdout.write(' '); | ||
| 22 | + process.stdout.write('world'); | ||
| 23 | + process.exit(0); | ||
| 24 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,25 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { Worker, isMainThread } = require('worker_threads'); | ||
| 5 | + | ||
| 6 | + if (isMainThread) { | ||
| 7 | + const w = new Worker(__filename, { stdout: true }); | ||
| 8 | + const expected = 'hello world'; | ||
| 9 | + | ||
| 10 | + let data = ''; | ||
| 11 | + w.stdout.setEncoding('utf8'); | ||
| 12 | + w.stdout.on('data', (chunk) => { | ||
| 13 | + data += chunk; | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + w.on('exit', common.mustCall(() => { | ||
| 17 | + assert.strictEqual(data, expected); | ||
| 18 | + })); | ||
| 19 | + } else { | ||
| 20 | + process.on('exit', () => { | ||
| 21 | + process.stdout.write(' '); | ||
| 22 | + process.stdout.write('world'); | ||
| 23 | + }); | ||
| 24 | + process.stdout.write('hello'); | ||
| 25 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments