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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
1'use strict';
2
3// undocumented cb() API, needed for core, not for public API
4function destroy(err, cb) {
5 var _this = this;
6 var readableDestroyed = this._readableState && this._readableState.destroyed;
7 var writableDestroyed = this._writableState && this._writableState.destroyed;
8 if (readableDestroyed || writableDestroyed) {
9 if (cb) {
10 cb(err);
11 } else if (err) {
12 if (!this._writableState) {
13 process.nextTick(emitErrorNT, this, err);
14 } else if (!this._writableState.errorEmitted) {
15 this._writableState.errorEmitted = true;
16 process.nextTick(emitErrorNT, this, err);
17 }
18 }
19 return this;
20 }
21
22 // we set destroyed to true before firing error callbacks in order
23 // to make it re-entrance safe in case destroy() is called within callbacks
24
25 if (this._readableState) {
26 this._readableState.destroyed = true;
27 }
28
29 // if this is a duplex stream mark the writable part as destroyed as well
30 if (this._writableState) {
31 this._writableState.destroyed = true;
32 }
33 this._destroy(err || null, function (err) {
34 if (!cb && err) {
35 if (!_this._writableState) {
36 process.nextTick(emitErrorAndCloseNT, _this, err);
37 } else if (!_this._writableState.errorEmitted) {
38 _this._writableState.errorEmitted = true;
39 process.nextTick(emitErrorAndCloseNT, _this, err);
40 } else {
41 process.nextTick(emitCloseNT, _this);
42 }
43 } else if (cb) {
44 process.nextTick(emitCloseNT, _this);
45 cb(err);
46 } else {
47 process.nextTick(emitCloseNT, _this);
48 }
49 });
50 return this;
51}
52function emitErrorAndCloseNT(self, err) {
53 emitErrorNT(self, err);
54 emitCloseNT(self);
55}
56function emitCloseNT(self) {
57 if (self._writableState && !self._writableState.emitClose) return;
58 if (self._readableState && !self._readableState.emitClose) return;
59 self.emit('close');
60}
61function undestroy() {
62 if (this._readableState) {
63 this._readableState.destroyed = false;
64 this._readableState.reading = false;
65 this._readableState.ended = false;
66 this._readableState.endEmitted = false;
67 }
68 if (this._writableState) {
69 this._writableState.destroyed = false;
70 this._writableState.ended = false;
71 this._writableState.ending = false;
72 this._writableState.finalCalled = false;
73 this._writableState.prefinished = false;
74 this._writableState.finished = false;
75 this._writableState.errorEmitted = false;
76 }
77}
78function emitErrorNT(self, err) {
79 self.emit('error', err);
80}
81function errorOrDestroy(stream, err) {
82 // We have tests that rely on errors being emitted
83 // in the same tick, so changing this is semver major.
84 // For now when you opt-in to autoDestroy we allow
85 // the error to be emitted nextTick. In a future
86 // semver major update we should change the default to this.
87
88 var rState = stream._readableState;
89 var wState = stream._writableState;
90 if (rState && rState.autoDestroy || wState && wState.autoDestroy) stream.destroy(err);else stream.emit('error', err);
91}
92module.exports = {
93 destroy: destroy,
94 undestroy: undestroy,
95 errorOrDestroy: errorOrDestroy
96};
Note: See TracBrowser for help on using the repository browser.