forked from bnoordhuis/node-buffertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffertools.js
121 lines (104 loc) · 3.18 KB
/
buffertools.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
/* Copyright (c) 2010, Ben Noordhuis <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
var SlowBuffer = require('buffer').SlowBuffer;
var Buffer = require('buffer').Buffer;
// requires node 3.1
var events = require('events');
var util = require('util');
try {
var buffertools = require('./build/Release/buffertools.node');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e;
var buffertools = require('./build/Debug/buffertools.node');
}
exports.extend = function() {
var receivers;
if (arguments.length > 0) {
receivers = Array.prototype.slice.call(arguments);
} else if (typeof SlowBuffer === 'function') {
receivers = [Buffer.prototype, SlowBuffer.prototype];
} else {
receivers = [Buffer.prototype];
}
for (var i = 0, n = receivers.length; i < n; i += 1) {
var receiver = receivers[i];
for (var key in buffertools) {
receiver[key] = buffertools[key];
}
if (receiver !== exports) {
receiver.concat = function() {
var args = [this].concat(Array.prototype.slice.call(arguments));
return buffertools.concat.apply(buffertools, args);
};
}
}
};
exports.extend(exports);
//
// WritableBufferStream
//
// - never emits 'error'
// - never emits 'drain'
//
function WritableBufferStream() {
this.writable = true;
this.buffer = null;
}
util.inherits(WritableBufferStream, events.EventEmitter);
WritableBufferStream.prototype._append = function(buffer, encoding) {
if (!this.writable) {
throw new Error('Stream is not writable.');
}
if (Buffer.isBuffer(buffer)) {
// no action required
}
else if (typeof buffer == 'string') {
// TODO optimize
buffer = new Buffer(buffer, encoding || 'utf8');
}
else {
throw new Error('Argument should be either a buffer or a string.');
}
// FIXME optimize!
if (this.buffer) {
this.buffer = buffertools.concat(this.buffer, buffer);
}
else {
this.buffer = new Buffer(buffer.length);
buffer.copy(this.buffer);
}
};
WritableBufferStream.prototype.write = function(buffer, encoding) {
this._append(buffer, encoding);
// signal that it's safe to immediately write again
return true;
};
WritableBufferStream.prototype.end = function(buffer, encoding) {
if (buffer) {
this._append(buffer, encoding);
}
this.emit('close');
this.writable = false;
};
WritableBufferStream.prototype.getBuffer = function() {
if (this.buffer) {
return this.buffer;
}
return new Buffer(0);
};
WritableBufferStream.prototype.toString = function() {
return this.getBuffer().toString();
};
exports.WritableBufferStream = WritableBufferStream;