File: /var/www/quadcode.com/node_modules/@playwright/test/lib/runner/processHost.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ProcessHost = void 0;
var _child_process = _interopRequireDefault(require("child_process"));
var _events = require("events");
var _utilsBundle = require("playwright-core/lib/utilsBundle");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Copyright Microsoft Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class ProcessHost extends _events.EventEmitter {
constructor(runnerScript, processName, env) {
super();
this.process = void 0;
this._didSendStop = false;
this._didFail = false;
this.didExit = false;
this._runnerScript = void 0;
this._lastMessageId = 0;
this._callbacks = new Map();
this._processName = void 0;
this._producedEnv = {};
this._extraEnv = void 0;
this._runnerScript = runnerScript;
this._processName = processName;
this._extraEnv = env;
}
async startRunner(runnerParams, inheritStdio) {
var _process$stdout$getCo, _process$stdout, _process$stderr$getCo, _process$stderr;
this.process = _child_process.default.fork(require.resolve('../common/process'), {
detached: false,
env: {
...process.env,
...this._extraEnv
},
stdio: inheritStdio ? ['ignore', 'inherit', 'inherit', 'ipc'] : ['ignore', 'ignore', process.env.PW_RUNNER_DEBUG ? 'inherit' : 'ignore', 'ipc']
});
this.process.on('exit', (code, signal) => {
this.didExit = true;
this.emit('exit', {
unexpectedly: !this._didSendStop,
code,
signal
});
});
this.process.on('error', e => {}); // do not yell at a send to dead process.
this.process.on('message', message => {
if (_utilsBundle.debug.enabled('pw:test:protocol')) (0, _utilsBundle.debug)('pw:test:protocol')('◀ RECV ' + JSON.stringify(message));
if (message.method === '__env_produced__') {
const producedEnv = message.params;
this._producedEnv = Object.fromEntries(producedEnv.map(e => {
var _e$;
return [e[0], (_e$ = e[1]) !== null && _e$ !== void 0 ? _e$ : undefined];
}));
} else if (message.method === '__dispatch__') {
const {
id,
error,
method,
params,
result
} = message.params;
if (id && this._callbacks.has(id)) {
const {
resolve,
reject
} = this._callbacks.get(id);
this._callbacks.delete(id);
if (error) {
const errorObject = new Error(error.message);
errorObject.stack = error.stack;
reject(errorObject);
} else {
resolve(result);
}
} else {
this.emit(method, params);
}
} else {
this.emit(message.method, message.params);
}
});
await new Promise((resolve, reject) => {
this.process.once('exit', (code, signal) => reject(new Error(`process exited with code "${code}" and signal "${signal}" before it became ready`)));
this.once('ready', () => resolve());
});
const processParams = {
stdoutParams: {
rows: process.stdout.rows,
columns: process.stdout.columns,
colorDepth: ((_process$stdout$getCo = (_process$stdout = process.stdout).getColorDepth) === null || _process$stdout$getCo === void 0 ? void 0 : _process$stdout$getCo.call(_process$stdout)) || 8
},
stderrParams: {
rows: process.stderr.rows,
columns: process.stderr.columns,
colorDepth: ((_process$stderr$getCo = (_process$stderr = process.stderr).getColorDepth) === null || _process$stderr$getCo === void 0 ? void 0 : _process$stderr$getCo.call(_process$stderr)) || 8
},
processName: this._processName
};
this.send({
method: '__init__',
params: {
processParams,
runnerScript: this._runnerScript,
runnerParams
}
});
}
sendMessage(message) {
const id = ++this._lastMessageId;
this.send({
method: '__dispatch__',
params: {
id,
...message
}
});
return new Promise((resolve, reject) => {
this._callbacks.set(id, {
resolve,
reject
});
});
}
sendMessageNoReply(message) {
this.sendMessage(message).catch(() => {});
}
async stop(didFail) {
if (didFail) this._didFail = true;
if (this.didExit) return;
if (!this._didSendStop) {
this.send({
method: '__stop__'
});
this._didSendStop = true;
}
await new Promise(f => this.once('exit', f));
}
didFail() {
return this._didFail;
}
didSendStop() {
return this._didSendStop;
}
producedEnv() {
return this._producedEnv;
}
send(message) {
if (_utilsBundle.debug.enabled('pw:test:protocol')) (0, _utilsBundle.debug)('pw:test:protocol')('SEND ► ' + JSON.stringify(message));
this.process.send(message);
}
}
exports.ProcessHost = ProcessHost;