|
7 | 7 | // - Transform - transform message objects to reply objects |
8 | 8 | // - Debug - transform JavaScript objects to lines of JSON (for debugging, obviously) |
9 | 9 |
|
10 | | -var stream = require('stream'); |
11 | | -var util = require('util'); |
| 10 | +const stream = require('stream'); |
| 11 | +const util = require('util'); |
12 | 12 |
|
13 | 13 | function Input() { |
14 | | - stream.Transform.call(this); |
| 14 | + stream.Transform.call(this); |
15 | 15 |
|
16 | | - // Transform bytes... |
17 | | - this._writableState.objectMode = false; |
18 | | - // ...into objects. |
19 | | - this._readableState.objectMode = true; |
| 16 | + // Transform bytes... |
| 17 | + this._writableState.objectMode = false; |
| 18 | + // ...into objects. |
| 19 | + this._readableState.objectMode = true; |
20 | 20 |
|
21 | | - // Unparsed data. |
22 | | - this.buf = new Buffer(0); |
23 | | - // Parsed length. |
24 | | - this.len = null; |
| 21 | + // Unparsed data. |
| 22 | + this.buf = Buffer.alloc(0); |
| 23 | + // Parsed length. |
| 24 | + this.len = null; |
25 | 25 | } |
26 | 26 |
|
27 | 27 | util.inherits(Input, stream.Transform); |
28 | 28 |
|
29 | 29 | Input.prototype._transform = function(chunk, encoding, done) { |
30 | | - // Save this chunk. |
31 | | - this.buf = Buffer.concat([ this.buf, chunk ]); |
32 | | - |
33 | | - var self = this; |
34 | | - |
35 | | - function parseBuf() { |
36 | | - // Do we have a length yet? |
37 | | - if (typeof self.len !== 'number') { |
38 | | - // Nope. Do we have enough bytes for the length? |
39 | | - if (self.buf.length >= 4) { |
40 | | - // Yep. Parse the bytes. |
41 | | - self.len = self.buf.readUInt32LE(0); |
42 | | - // Remove the length bytes from the buffer. |
43 | | - self.buf = self.buf.slice(4); |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - // Do we have a length yet? (We may have just parsed it.) |
48 | | - if (typeof self.len === 'number') { |
49 | | - // Yep. Do we have enough bytes for the message? |
50 | | - if (self.buf.length >= self.len) { |
51 | | - // Yep. Slice off the bytes we need. |
52 | | - var message = self.buf.slice(0, self.len); |
53 | | - // Remove the bytes for the message from the buffer. |
54 | | - self.buf = self.buf.slice(self.len); |
55 | | - // Clear the length so we know we need to parse it again. |
56 | | - self.len = null; |
57 | | - // Parse the message bytes. |
58 | | - var obj = JSON.parse(message.toString()); |
59 | | - // Enqueue it for reading. |
60 | | - self.push(obj); |
61 | | - // We could have more messages in the buffer so check again. |
62 | | - parseBuf(); |
63 | | - } |
64 | | - } |
| 30 | + // Save this chunk. |
| 31 | + this.buf = Buffer.concat([this.buf, chunk]); |
| 32 | + |
| 33 | + const self = this; |
| 34 | + |
| 35 | + function parseBuf() { |
| 36 | + // Do we have a length yet? |
| 37 | + if (typeof self.len !== 'number') { |
| 38 | + // Nope. Do we have enough bytes for the length? |
| 39 | + if (self.buf.length >= 4) { |
| 40 | + // Yep. Parse the bytes. |
| 41 | + self.len = self.buf.readUInt32LE(0); |
| 42 | + // Remove the length bytes from the buffer. |
| 43 | + self.buf = self.buf.slice(4); |
| 44 | + } |
65 | 45 | } |
66 | 46 |
|
67 | | - // Check for a parsable buffer (both length and message). |
68 | | - parseBuf(); |
| 47 | + // Do we have a length yet? (We may have just parsed it.) |
| 48 | + if (typeof self.len === 'number') { |
| 49 | + // Yep. Do we have enough bytes for the message? |
| 50 | + if (self.buf.length >= self.len) { |
| 51 | + // Yep. Slice off the bytes we need. |
| 52 | + const message = self.buf.slice(0, self.len); |
| 53 | + // Remove the bytes for the message from the buffer. |
| 54 | + self.buf = self.buf.slice(self.len); |
| 55 | + // Clear the length so we know we need to parse it again. |
| 56 | + self.len = null; |
| 57 | + // Parse the message bytes. |
| 58 | + const obj = JSON.parse(message.toString()); |
| 59 | + // Enqueue it for reading. |
| 60 | + self.push(obj); |
| 61 | + // We could have more messages in the buffer so check again. |
| 62 | + parseBuf(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Check for a parsable buffer (both length and message). |
| 68 | + parseBuf(); |
69 | 69 |
|
70 | | - // We're done. |
71 | | - done(); |
| 70 | + // We're done. |
| 71 | + done(); |
72 | 72 | }; |
73 | 73 |
|
74 | 74 | function Output() { |
75 | | - stream.Transform.call(this); |
| 75 | + stream.Transform.call(this); |
76 | 76 |
|
77 | | - this._writableState.objectMode = true; |
78 | | - this._readableState.objectMode = false; |
| 77 | + this._writableState.objectMode = true; |
| 78 | + this._readableState.objectMode = false; |
79 | 79 | } |
80 | 80 |
|
81 | 81 | util.inherits(Output, stream.Transform); |
82 | 82 |
|
83 | 83 | Output.prototype._transform = function(chunk, encoding, done) { |
84 | | - var len = new Buffer(4); |
85 | | - var buf = new Buffer(JSON.stringify(chunk)); |
| 84 | + const len = Buffer.alloc(4); |
| 85 | + const buf = new Buffer(JSON.stringify(chunk)); |
86 | 86 |
|
87 | | - len.writeUInt32LE(buf.length, 0); |
| 87 | + len.writeUInt32LE(buf.length, 0); |
88 | 88 |
|
89 | | - this.push(len); |
90 | | - this.push(buf); |
| 89 | + this.push(len); |
| 90 | + this.push(buf); |
91 | 91 |
|
92 | | - done(); |
| 92 | + done(); |
93 | 93 | }; |
94 | 94 |
|
95 | 95 | function Transform(handler) { |
96 | | - stream.Transform.call(this); |
| 96 | + stream.Transform.call(this); |
97 | 97 |
|
98 | | - this._writableState.objectMode = true; |
99 | | - this._readableState.objectMode = true; |
| 98 | + this._writableState.objectMode = true; |
| 99 | + this._readableState.objectMode = true; |
100 | 100 |
|
101 | | - this.handler = handler; |
| 101 | + this.handler = handler; |
102 | 102 | } |
103 | 103 |
|
104 | 104 | util.inherits(Transform, stream.Transform); |
105 | 105 |
|
106 | 106 | Transform.prototype._transform = function(msg, encoding, done) { |
107 | | - this.handler(msg, this.push.bind(this), done); |
| 107 | + this.handler(msg, this.push.bind(this), done); |
108 | 108 | }; |
109 | 109 |
|
110 | 110 | function Debug() { |
111 | | - stream.Transform.call(this); |
| 111 | + stream.Transform.call(this); |
112 | 112 |
|
113 | | - this._writableState.objectMode = true; |
114 | | - this._readableState.objectMode = false; |
| 113 | + this._writableState.objectMode = true; |
| 114 | + this._readableState.objectMode = false; |
115 | 115 | } |
116 | 116 |
|
117 | 117 | util.inherits(Debug, stream.Transform); |
118 | 118 |
|
119 | 119 | Debug.prototype._transform = function(chunk, encoding, done) { |
120 | | - this.push(JSON.stringify(chunk) + '\n'); |
| 120 | + this.push(JSON.stringify(chunk) + '\n'); |
121 | 121 |
|
122 | | - done(); |
| 122 | + done(); |
123 | 123 | }; |
124 | 124 |
|
125 | 125 | exports.Input = Input; |
|
0 commit comments