-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
247 lines (211 loc) · 6.55 KB
/
index.js
File metadata and controls
247 lines (211 loc) · 6.55 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use strict'
/*
* RESP.js
* https://github.com/zensh/resp.js
*
* Copyright (c) 2014-2018 Yan Qing
* Licensed under the MIT license.
*/
const EventEmitter = require('events')
const CRLF = '\r\n'
class Resp extends EventEmitter {
static encodeNull () {
return Buffer.from('$-1\r\n')
}
static encodeNullArray () {
return Buffer.from('*-1\r\n')
}
static encodeString (str) {
if (typeof str !== 'string') throw new TypeError(String(str) + ' must be string')
return Buffer.from('+' + str + CRLF)
}
static encodeError (err) {
if (!(err instanceof Error)) throw new TypeError(String(err) + ' must be Error object')
return Buffer.from('-' + err.name + ' ' + err.message + CRLF)
}
static encodeInteger (num) {
if (!Number.isInteger(num)) throw new TypeError(String(num) + ' must be Integer')
return Buffer.from(':' + num + CRLF)
}
static encodeBulk (str) {
if (!arguments.length) throw new Error('no value to encode')
str = String(str)
return Buffer.from('$' + Buffer.byteLength(str, 'utf8') + CRLF + str + CRLF)
}
static encodeBufBulk (buf) {
if (!Buffer.isBuffer(buf)) throw new TypeError(String(buf) + ' must be Buffer object')
const prefix = '$' + buf.length + CRLF
const buffer = Buffer.allocUnsafe(prefix.length + buf.length + 2)
buffer.write(prefix)
buf.copy(buffer, prefix.length)
buffer.write(CRLF, prefix.length + buf.length)
return buffer
}
static encodeArray (arr) {
if (!Array.isArray(arr)) throw new Error(String(arr) + ' must be Array object')
const prefix = '*' + arr.length + CRLF
let length = prefix.length
const bufs = [Buffer.from(prefix)]
for (let buf, i = 0, len = arr.length; i < len; i++) {
buf = arr[i]
if (Array.isArray(buf)) buf = Resp.encodeArray(buf)
else if (!Buffer.isBuffer(buf)) throw new TypeError(String(buf) + ' must be RESP Buffer value')
bufs.push(buf)
length += buf.length
}
return Buffer.concat(bufs, length)
}
static encodeRequest (arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error(String(arr) + ' must be array of value')
}
const bulks = Array(arr.length)
for (let i = 0, len = arr.length; i < len; i++) {
bulks[i] = Buffer.isBuffer(arr[i]) ? Resp.encodeBufBulk(arr[i]) : Resp.encodeBulk(arr[i])
}
return Resp.encodeArray(bulks)
}
// Decode a RESP buffer to RESP value
static decode (buf, bufBulk) {
const res = parseBuffer(buf, 0, bufBulk)
if (!res || res.index < buf.length) throw new Error('Parse "' + buf + '" failed')
if (res instanceof Error) throw res
return res.content
}
constructor (options) {
super()
options = options || {}
this._bufBulk = !!options.bufBulk
// legacy from old stream.
this.writable = true
clearState(this)
}
write (buf) {
if (!Buffer.isBuffer(buf)) {
this.emit('error', new Error('Invalid buffer chunk'))
return true
}
if (!this._buf) this._buf = buf
else {
const ret = this._buf.length - this._pos
const _buf = Buffer.allocUnsafe(buf.length + ret)
this._buf.copy(_buf, 0, this._pos)
buf.copy(_buf, ret)
this._buf = _buf
this._pos = 0
}
while (this._pos < this._buf.length) {
const result = parseBuffer(this._buf, this._pos, this._bufBulk)
if (result == null) {
this.emit('drain')
return true
}
if (result instanceof Error) {
clearState(this)
this.emit('error', result)
return false
}
this._pos = result.index
this.emit('data', result.content)
}
clearState(this).emit('drain')
return true
}
end (buf) {
if (buf != null) this.write(buf)
this.emit('finish')
}
}
function clearState (ctx) {
ctx._pos = 0
ctx._buf = null
return ctx
}
class ReadRes {
constructor (content, index) {
this.content = content
this.index = index
}
}
function readBuffer (buf, i) {
const start = i
const len = buf.length
while (i < len && !isCRLF(buf, i)) i++
return i >= len ? null : new ReadRes(buf.utf8Slice(start, i), i + 2)
}
function parseBuffer (buf, index, bufBulk) {
let result = null
let num = NaN
if (index >= buf.length) return null
switch (buf[index]) {
case 43: { // '+'
return readBuffer(buf, index + 1)
}
case 45: { // '-'
result = readBuffer(buf, index + 1)
if (result == null) return result
let fragment = result.content.match(/^(\S+) ([\s\S]+)$/)
if (!fragment) fragment = [null, 'Error', result.content]
result.content = new Error(fragment[2])
result.content.name = result.content.code = fragment[1]
return result
}
case 58: { // ':'
result = readBuffer(buf, index + 1)
if (result == null) return result
num = parseInteger(result.content)
if (num == null) return new Error('Parse ":" failed')
result.content = num
return result
}
case 36: { // '$'
result = readBuffer(buf, index + 1)
if (result == null) return result
num = parseInteger(result.content)
if (num == null || num < -1) return new Error('Parse "$" failed, invalid length')
const endIndex = result.index + num
if (num === -1) {
// Null Bulk
result.content = null
} else if (buf.length < endIndex + 2) {
return null
} else if (!isCRLF(buf, endIndex)) {
return new Error('Parse "$" failed, invalid CRLF')
} else {
result.content = buf[bufBulk ? 'slice' : 'utf8Slice'](result.index, endIndex)
result.index = endIndex + 2
}
return result
}
case 42: { // '*'
result = readBuffer(buf, index + 1)
if (result == null) return result
num = parseInteger(result.content)
if (num == null || num < -1) return new Error('Parse "*" failed, invalid length')
if (num === -1) {
// Null Array
result.content = null
} else if (num === 0) {
result.content = []
} else {
result.content = Array(num)
for (let _result, i = 0; i < num; i++) {
_result = parseBuffer(buf, result.index, bufBulk)
if (_result == null || _result instanceof Error) return _result
result.content[i] = _result.content
result.index = _result.index
}
}
return result
}
}
return new Error('Invalid Chunk: parse failed')
}
function parseInteger (str) {
const num = +str
return (str && Number.isInteger(num)) ? num : null
}
function isCRLF (buf, i) {
return buf[i] === 13 && buf[i + 1] === 10
}
module.exports = Resp.Resp = Resp