UNPKG

1.21 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14
15import File = require('vinyl');
16
17import {AsyncTransformStream} from './streams';
18
19export function forkStream(stream: NodeJS.ReadableStream):
20 NodeJS.ReadableStream {
21 const fork = new ForkedVinylStream();
22 stream.pipe(fork);
23 return fork;
24}
25
26/**
27 * Forks a stream of Vinyl files, cloning each file before emitting on the fork.
28 */
29export class ForkedVinylStream extends AsyncTransformStream<File, File> {
30 constructor() {
31 super({objectMode: true});
32 }
33
34 protected async *
35 _transformIter(files: AsyncIterable<File>): AsyncIterable<File> {
36 for await (const file of files) {
37 yield file.clone({deep: true, contents: true});
38 }
39 }
40}