puppeteer/phantom_shim/System.js

119 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-05-11 07:06:41 +00:00
/**
* Copyright 2017 Google Inc. 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.
*/
2017-08-02 19:06:47 +00:00
const readline = require('readline');
const await = require('./utilities').await;
const os = require('os');
2017-05-11 07:06:41 +00:00
class System {
2017-06-21 20:51:06 +00:00
/**
* @param {!Array<string>} args
*/
2017-06-21 20:51:06 +00:00
constructor(args) {
this.args = args;
this.env = {};
Object.assign(this.env, process.env);
this.stdin = new StandardInput(process.stdin);
this.stdout = new StandardOutput(process.stdout);
this.stderr = new StandardOutput(process.stderr);
this.platform = 'phantomjs';
this.pid = process.pid;
this.isSSLSupported = false;
this.os = {
architecture: os.arch(),
name: os.type(),
version: os.release()
};
}
2017-05-11 07:06:41 +00:00
}
class StandardInput {
2017-06-21 20:51:06 +00:00
/**
* @param {!Readable} readableStream
*/
2017-06-21 20:51:06 +00:00
constructor(readableStream) {
this._readline = readline.createInterface({
input: readableStream
});
this._lines = [];
this._closed = false;
this._readline.on('line', line => this._lines.push(line));
this._readline.on('close', () => this._closed = true);
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
readLine() {
if (this._closed && !this._lines.length)
return '';
if (!this._lines.length) {
let linePromise = new Promise(fulfill => this._readline.once('line', fulfill));
2017-06-21 20:51:06 +00:00
await(linePromise);
2017-05-11 07:06:41 +00:00
}
2017-06-21 20:51:06 +00:00
return this._lines.shift();
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
read() {
if (!this._closed) {
let closePromise = new Promise(fulfill => this._readline.once('close', fulfill));
2017-06-21 20:51:06 +00:00
await(closePromise);
2017-05-11 07:06:41 +00:00
}
let text = this._lines.join('\n');
2017-06-21 20:51:06 +00:00
this._lines = [];
return text;
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
close() {
this._readline.close();
}
2017-05-11 07:06:41 +00:00
}
class StandardOutput {
2017-06-21 20:51:06 +00:00
/**
* @param {!Writable} writableStream
*/
2017-06-21 20:51:06 +00:00
constructor(writableStream) {
this._stream = writableStream;
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} data
*/
2017-06-21 20:51:06 +00:00
write(data) {
this._stream.write(data);
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
/**
* @param {string} data
*/
2017-06-21 20:51:06 +00:00
writeLine(data) {
this._stream.write(data + '\n');
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
flush() {
}
2017-05-11 07:06:41 +00:00
2017-06-21 20:51:06 +00:00
close() {
this._stream.end();
}
2017-05-11 07:06:41 +00:00
}
module.exports = System;