豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 71f4f78

Browse files
committed
refactor(ng-dev): introduce utility for reading buffer from stdin
Introduces an utility for reading buffer/text from process stdin. This will be useful for delivering larger data from the release tool to external commands. Command line arguments are not sufficient-enough sine they suffer from escaping problems and max-length limits.
1 parent 7fe2e6c commit 71f4f78

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/** Unique error class for failures when reading from the stdin. */
10+
export class ReadBufferFromStdinError extends Error {}
11+
12+
/**
13+
* Reads a `Buffer` from `stdin` until the stream is closed.
14+
*
15+
* @returns a Promise resolving with the `Buffer`. Rejects with `ReadBufferFromStdinError`
16+
* on unexpected read errors.
17+
*/
18+
export function readBufferFromStdinUntilClosed(
19+
input: NodeJS.ReadStream = process.stdin,
20+
): Promise<Buffer> {
21+
return new Promise((resolve, reject) => {
22+
const data: Buffer[] = [];
23+
24+
input.on('data', (chunk) => data.push(chunk));
25+
input.on('end', () => resolve(Buffer.concat(data)));
26+
input.on('error', () => reject(new ReadBufferFromStdinError()));
27+
input.on('timeout', () => reject(new ReadBufferFromStdinError('Unexpected timeout')));
28+
});
29+
}

0 commit comments

Comments
 (0)