source: frontend/node_modules/readable-stream/lib/internal/streams/pipeline.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
Line 
1// Ported from https://github.com/mafintosh/pump with
2// permission from the author, Mathias Buus (@mafintosh).
3
4'use strict';
5
6var eos;
7function once(callback) {
8 var called = false;
9 return function () {
10 if (called) return;
11 called = true;
12 callback.apply(void 0, arguments);
13 };
14}
15var _require$codes = require('../../../errors').codes,
16 ERR_MISSING_ARGS = _require$codes.ERR_MISSING_ARGS,
17 ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED;
18function noop(err) {
19 // Rethrow the error if it exists to avoid swallowing it
20 if (err) throw err;
21}
22function isRequest(stream) {
23 return stream.setHeader && typeof stream.abort === 'function';
24}
25function destroyer(stream, reading, writing, callback) {
26 callback = once(callback);
27 var closed = false;
28 stream.on('close', function () {
29 closed = true;
30 });
31 if (eos === undefined) eos = require('./end-of-stream');
32 eos(stream, {
33 readable: reading,
34 writable: writing
35 }, function (err) {
36 if (err) return callback(err);
37 closed = true;
38 callback();
39 });
40 var destroyed = false;
41 return function (err) {
42 if (closed) return;
43 if (destroyed) return;
44 destroyed = true;
45
46 // request.destroy just do .end - .abort is what we want
47 if (isRequest(stream)) return stream.abort();
48 if (typeof stream.destroy === 'function') return stream.destroy();
49 callback(err || new ERR_STREAM_DESTROYED('pipe'));
50 };
51}
52function call(fn) {
53 fn();
54}
55function pipe(from, to) {
56 return from.pipe(to);
57}
58function popCallback(streams) {
59 if (!streams.length) return noop;
60 if (typeof streams[streams.length - 1] !== 'function') return noop;
61 return streams.pop();
62}
63function pipeline() {
64 for (var _len = arguments.length, streams = new Array(_len), _key = 0; _key < _len; _key++) {
65 streams[_key] = arguments[_key];
66 }
67 var callback = popCallback(streams);
68 if (Array.isArray(streams[0])) streams = streams[0];
69 if (streams.length < 2) {
70 throw new ERR_MISSING_ARGS('streams');
71 }
72 var error;
73 var destroys = streams.map(function (stream, i) {
74 var reading = i < streams.length - 1;
75 var writing = i > 0;
76 return destroyer(stream, reading, writing, function (err) {
77 if (!error) error = err;
78 if (err) destroys.forEach(call);
79 if (reading) return;
80 destroys.forEach(call);
81 callback(error);
82 });
83 });
84 return streams.reduce(pipe);
85}
86module.exports = pipeline;
Note: See TracBrowser for help on using the repository browser.