本文整理汇总了TypeScript中stream.Readable.setEncoding方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Readable.setEncoding方法的具体用法?TypeScript Readable.setEncoding怎么用?TypeScript Readable.setEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Readable
的用法示例。
在下文中一共展示了Readable.setEncoding方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: run
run(): void {
let cmd = this.taskExecParams.cmd;
let stdin = this.taskExecParams.stdin;
let instream: stream.Readable = null;
let pid:number = null;
let stdout = '';
let stderr = '';
let raisedError = "";
if (stdin && stdin.length > 0) {
if (stdin.length >= 1 && stdin.substr(0,1) === '@') { // stdin string begins with '@' => a file path
let stdinFile = stdin.substr(1);
instream = fs.createReadStream(stdinFile, {encoding: 'utf8'});
} else {
instream = new stream.Readable();
instream.setEncoding("utf8");
instream.push(stdin);
instream.push(null);
}
}
if (instream) {
instream.on("error", (err: any) => { // stdin stream has some kind of error (maybe input file does not exist)
if (err.syscall && err.path)
raisedError = "error " + err.syscall + " " + err.path;
else
raisedError = JSON.stringify(err);
treeKill(pid, 'SIGKILL'); // kill the child process tree
});
}
let env: any;
if (this.taskExecParams.envJSON) {
try {env = JSON.parse(this.taskExecParams.envJSON);}catch(e) {}
}
let child = exec(cmd, {maxBuffer: 20000 * 1024, env});
if (instream && child.stdin) instream.pipe(child.stdin);
pid = child.pid;
this.emit('started', pid);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', (data:string) => {
stdout += data;
});
child.stderr.on('data', (data:string) => {
stderr += data;
});
child.on('close', (code: number, signal: string) => {
let result: ITaskExecResult = {
pid
,retCode: code
,stdout: (stdout.length > 0 ? stdout : null)
,stderr: (stderr.length > 0 ? stderr : (raisedError ? raisedError : null))
};
this.emit('finished', result);
});
}
示例2: test
test('rewrites /node_modules references, 1 dir nested', async (t) => {
const filePath = '/demo/index.html';
const beforeStream = new Readable();
beforeStream.push('<script src="../node_modules/other-package/file.html">');
beforeStream.push(null);
beforeStream.setEncoding('utf8');
const expected = '<script src="/other-package/file.html">';
const actualStream = beforeStream.pipe(new HTMLRewriter({}, filePath));
t.is(await getStream(actualStream), expected);
});
示例3: Promise
return new Promise(resolve => {
stdin
.setEncoding('utf8')
.on('readable', () => {
let current
while ((current = stdin.read())) {
out += current
}
})
.on('end', () => resolve(out.trim()))
setTimeout(() => {
if (!out.trim()) {
resolve(out.trim())
}
}, 1000)
})