/
sequence.js
145 lines (127 loc) · 3.13 KB
/
sequence.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var Stream = require("stream");
var TASK_TYPE = {
CHUNK: 0,
STREAM: 1,
EOF: 2,
ERROR: 3
};
function Task(type, data) {
this.type = type;
this.data = data;
}
function Sequence() {
var stream = new Stream();
stream.writable = true;
stream.readable = true;
this.stream = stream;
this._queue = [];
this._writing = false; //When this flag is set, another _next event is scheduled
this._closed = false; //If set, stream is dead. Don't accept new events
this._next = Sequence.prototype._nextWrite.bind(this);
this._error = Sequence.prototype.error.bind(this);
}
Sequence.prototype._nextWrite = function() {
if(queue.length === 0) {
this._writing = false;
return;
}
this._writing = true;
//Pop off the head of the queue and do it
var task = this._queue[0];
this._queue.shift();
switch(task.type) {
case TASK_TYPE.CHUNK:
if(this.stream.write(task.data)) {
while(this._queue.length > 0 &&
this._queue[0].type === TASK_TYPE.CHUNK) {
var data = this._queue[0].data;
this._queue.shift();
if(!this.stream.write(data)) {
this.stream.once("drain", this._next);
return;
}
}
if(this._queue.length > 0) {
process.nextTick(this._next);
} else {
this._writing = false;
}
} else {
this.stream.once("drain", this._next);
}
break;
case TASK_TYPE.STREAM:
task.data.resume();
task.data.on("end", this._next)
task.data.on("error", this._error);
task.data.pipe(this.stream, { end: false });
break;
case TASK_TYPE.EOF:
this.stream.end();
this._close();
break;
}
}
Sequence.prototype._addTask = function(task) {
this._queue.push(task);
if(!this._writing) {
this._writing = true;
process.nextTick(this._next);
}
}
Sequence.prototype._close = function() {
for(var i=0; i<this._queue.length; ++i) {
if(this._queue[i].type === TASK_TYPE.STREAM) {
this._queue[i].data.destroy();
}
}
this._queue.length = 0;
}
Sequence.prototype.write = function(buffer) {
if(this._closed) {
return false;
}
if(!this.writing) {
if(!this.stream.write(buffer)) {
this._writing = true;
this.stream.once("drain", this._next);
return false;
}
return true;
} else {
this._addTask(new Task(TASK_TYPE.CHUNK, buffer));
return false;
}
}
Sequence.prototype.insert = function(stream) {
if(this._closed) {
return null;
}
if(!this._writing) {
this._writing = true;
stream.resume();
stream.on("end", this._next);
stream.on("error", this._error);
stream.pipe(this.stream, { end: false });
} else {
stream.pause();
this._addTask(new Task(TASK_TYPE.STREAM, stream));
}
return this;
}
Sequence.prototype.end = function() {
if(this._closed) {
return;
}
this._closed = true;
this._addTask(new Task(TASK_TYPE.EOF));
}
Sequence.prototype.error = function(err) {
if(this._closed) {
return;
}
this._closed = true;
this.stream.emit("error", err);
this._close();
}
module.exports = Sequence;