fix: no more need to hijack process.stdout on Win32 (#25765)

This commit is contained in:
Cheng Zhao 2020-10-06 02:10:38 +09:00 коммит произвёл GitHub
Родитель 9717dff4fa
Коммит 57dc170e81
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 9 добавлений и 63 удалений

Просмотреть файл

@ -1149,6 +1149,7 @@ if (is_mac) {
"wtsapi32.lib",
]
configs -= [ "//build/config/win:console" ]
configs += [
"//build/config/win:windowed",
"//build/config/win:delayloads",

Просмотреть файл

@ -1,9 +1,6 @@
import { Buffer } from 'buffer';
import { EventEmitter } from 'events';
import * as fs from 'fs';
import { Socket } from 'net';
import * as path from 'path';
import * as util from 'util';
const Module = require('module');
@ -19,28 +16,6 @@ require('@electron/internal/common/init');
process._linkedBinding('electron_browser_event_emitter').setEventEmitterPrototype(EventEmitter.prototype);
if (process.platform === 'win32') {
// Redirect node's console to use our own implementations, since node can not
// handle console output when running as GUI program.
const consoleLog = (...args: any[]) => {
// @ts-ignore this typing is incorrect; 'format' is an optional parameter
// See https://nodejs.org/api/util.html#util_util_format_format_args
return process.log(util.format(...args) + '\n');
};
const streamWrite: Socket['write'] = function (chunk: Buffer | string, encoding?: any, callback?: Function) {
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString(encoding);
}
process.log(chunk);
if (callback) {
callback();
}
return true;
};
console.log = console.error = console.warn = consoleLog;
process.stdout.write = process.stderr.write = streamWrite;
}
// Don't quit on fatal error.
process.on('uncaughtException', function (error) {
// Do nothing if the user has a custom uncaught exception handler.

Просмотреть файл

@ -11,7 +11,6 @@ feat_add_new_built_with_electron_variable_to_config_gypi.patch
feat_add_flags_for_low-level_hooks_and_exceptions.patch
fix_expose_tracing_agent_and_use_tracing_tracingcontroller_instead.patch
pass_all_globals_through_require.patch
call_process_log_from_fallback_stream_on_windows.patch
fixme_remove_async_id_assertion_check.patch
fixme_comment_trace_event_macro.patch
fix_key_gen_apis_are_not_available_in_boringssl.patch

Просмотреть файл

@ -1,23 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Kevin Sawicki <kevinsawicki@gmail.com>
Date: Wed, 12 Oct 2016 09:43:26 -0700
Subject: Call process.log from fallback stream on Windows
(cherry picked from commit d31e629b4f2daf3500a485caab2b2990a41e3ad4)
diff --git a/lib/internal/bootstrap/switches/is_main_thread.js b/lib/internal/bootstrap/switches/is_main_thread.js
index 08623898edafacfa8cee47ab35bd75887f9d3e2a..828589d4047ac49d16e9080ad1f364484941aa6e 100644
--- a/lib/internal/bootstrap/switches/is_main_thread.js
+++ b/lib/internal/bootstrap/switches/is_main_thread.js
@@ -85,6 +85,11 @@ function createWritableStdioStream(fd) {
const { Writable } = require('stream');
stream = new Writable({
write(buf, enc, cb) {
+ if (process.platform === 'win32' &&
+ process.env.ELECTRON_RUN_AS_NODE &&
+ !process.env.ELECTRON_NO_ATTACH_CONSOLE) {
+ process.log(buf.toString());
+ }
cb();
}
});

Просмотреть файл

@ -212,9 +212,6 @@ int NodeMain(int argc, char* argv[]) {
node::SetIsolateUpForNode(isolate, is);
gin_helper::Dictionary process(isolate, env->process_object());
#if defined(OS_WIN)
process.SetMethod("log", &ElectronBindings::Log);
#endif
process.SetMethod("crash", &ElectronBindings::Crash);
// Setup process.crashReporter in child node processes

Просмотреть файл

@ -5,7 +5,6 @@
#include "shell/common/api/electron_bindings.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
@ -48,7 +47,6 @@ void ElectronBindings::BindProcess(v8::Isolate* isolate,
// These bindings are shared between sandboxed & unsandboxed renderers
process->SetMethod("crash", &Crash);
process->SetMethod("hang", &Hang);
process->SetMethod("log", &Log);
process->SetMethod("getCreationTime", &GetCreationTime);
process->SetMethod("getHeapStatistics", &GetHeapStatistics);
process->SetMethod("getBlinkMemoryInfo", &GetBlinkMemoryInfo);
@ -126,11 +124,6 @@ void ElectronBindings::OnCallNextTick(uv_async_t* handle) {
self->pending_next_ticks_.clear();
}
// static
void ElectronBindings::Log(const base::string16& message) {
std::cout << message << std::flush;
}
// static
void ElectronBindings::Crash() {
volatile int* zero = nullptr;

Просмотреть файл

@ -12,7 +12,6 @@
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/process/process_metrics.h"
#include "base/strings/string16.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/node_bindings.h"
#include "uv.h" // NOLINT(build/include_directory)
@ -48,7 +47,6 @@ class ElectronBindings {
gin_helper::Dictionary* process,
base::ProcessMetrics* metrics);
static void Log(const base::string16& message);
static void Crash();
private:

Просмотреть файл

@ -159,6 +159,12 @@ describe('node feature', () => {
});
});
describe('process.stdout', () => {
it('is a real Node stream', () => {
expect((process.stdout as any)._type).to.not.be.undefined();
});
});
ifdescribe(features.isRunAsNodeEnabled())('inspector', () => {
let child: childProcess.ChildProcessWithoutNullStreams;
let exitPromise: Promise<any[]>;

2
spec/fixtures/module/run-as-node.js поставляемый
Просмотреть файл

@ -1,5 +1,5 @@
console.log(JSON.stringify({
processLog: typeof process.log,
stdoutType: process.stdout._type,
processType: typeof process.type,
window: typeof window
}));

Просмотреть файл

@ -122,7 +122,7 @@ describe('node feature', () => {
});
await emittedOnce(child.stdout, 'close');
expect(JSON.parse(output)).to.deep.equal({
processLog: process.platform === 'win32' ? 'function' : 'undefined',
stdoutType: 'pipe',
processType: 'undefined',
window: 'undefined'
});