-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathdeserializer.js
324 lines (288 loc) · 7.65 KB
/
deserializer.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
var sax = require('sax')
, dateFormatter = require('./date_formatter')
var Deserializer = function(encoding) {
this.type = null
this.responseType = null
this.stack = []
this.marks = []
this.data = []
this.methodname = null
this.encoding = encoding || 'utf8'
this.value = false
this.callback = null
this.error = null
this.parser = sax.createStream()
this.parser.on('opentag', this.onOpentag.bind(this))
this.parser.on('closetag', this.onClosetag.bind(this))
this.parser.on('text', this.onText.bind(this))
this.parser.on('cdata', this.onCDATA.bind(this))
this.parser.on('end', this.onDone.bind(this))
this.parser.on('error', this.onError.bind(this))
}
Deserializer.prototype.deserializeMethodResponse = function(stream, callback) {
var that = this
this.callback = function(error, result) {
if (error) {
callback(error)
}
else if (result.length > 1) {
callback(new Error('Response has more than one param'))
}
else if (that.type !== 'methodresponse') {
callback(new Error('Not a method response'))
}
else if (!that.responseType) {
callback(new Error('Invalid method response'))
}
else {
callback(null, result[0])
}
}
stream.setEncoding(this.encoding)
stream.on('error', this.onError.bind(this))
stream.pipe(this.parser)
}
Deserializer.prototype.deserializeMethodCall = function(stream, callback) {
var that = this
this.callback = function(error, result) {
if (error) {
callback(error)
}
else if (that.type !== 'methodcall') {
callback(new Error('Not a method call'))
}
else if (!that.methodname) {
callback(new Error('Method call did not contain a method name'))
}
else {
callback(null, that.methodname, result)
}
}
stream.setEncoding(this.encoding)
stream.on('error', this.onError.bind(this))
stream.pipe(this.parser)
}
Deserializer.prototype.onDone = function() {
var that = this
if (!this.error) {
if (this.type === null || this.marks.length) {
this.callback(new Error('Invalid XML-RPC message'))
}
else if (this.responseType === 'fault') {
var createFault = function(fault) {
var error = new Error('XML-RPC fault' + (fault.faultString ? ': ' + fault.faultString : ''))
error.code = fault.faultCode
error.faultCode = fault.faultCode
error.faultString = fault.faultString
return error
}
this.callback(createFault(this.stack[0]))
}
else {
this.callback(undefined, this.stack)
}
}
}
// TODO:
// Error handling needs a little thinking. There are two different kinds of
// errors:
// 1. Low level errors like network, stream or xml errors. These don't
// require special treatment. They only need to be forwarded. The IO
// is already stopped in these cases.
// 2. Protocol errors: Invalid tags, invalid values &c. These happen in
// our code and we should tear down the IO and stop parsing.
// Currently all errors end here. Guess I'll split it up.
Deserializer.prototype.onError = function(msg) {
if (!this.error) {
if (typeof msg === 'string') {
this.error = new Error(msg)
}
else {
this.error = msg
}
this.callback(this.error)
}
}
Deserializer.prototype.push = function(value) {
this.stack.push(value)
}
//==============================================================================
// SAX Handlers
//==============================================================================
Deserializer.prototype.onOpentag = function(node) {
if (node.name === 'ARRAY' || node.name === 'STRUCT') {
this.marks.push(this.stack.length)
}
this.data = []
this.value = (node.name === 'VALUE')
}
Deserializer.prototype.onText = function(text) {
this.data.push(text)
}
Deserializer.prototype.onCDATA = function(cdata) {
this.data.push(cdata)
}
Deserializer.prototype.onClosetag = function(el) {
var data = this.data.join('')
try {
switch(el) {
case 'BOOLEAN':
this.endBoolean(data)
break
case 'INT':
case 'I4':
this.endInt(data)
break
case 'I8':
this.endI8(data)
break
case 'DOUBLE':
this.endDouble(data)
break
case 'STRING':
case 'NAME':
this.endString(data)
break
case 'ARRAY':
this.endArray(data)
break
case 'STRUCT':
this.endStruct(data)
break
case 'BASE64':
this.endBase64(data)
break
case 'DATETIME.ISO8601':
this.endDateTime(data)
break
case 'VALUE':
this.endValue(data)
break
case 'PARAMS':
this.endParams(data)
break
case 'FAULT':
this.endFault(data)
break
case 'METHODRESPONSE':
this.endMethodResponse(data)
break
case 'METHODNAME':
this.endMethodName(data)
break
case 'METHODCALL':
this.endMethodCall(data)
break
case 'NIL':
this.endNil(data)
break
case 'DATA':
case 'PARAM':
case 'MEMBER':
// Ignored by design
break
default:
this.onError('Unknown XML-RPC tag \'' + el + '\'')
break
}
}
catch (e) {
this.onError(e)
}
}
Deserializer.prototype.endNil = function(data) {
this.push(null)
this.value = false
}
Deserializer.prototype.endBoolean = function(data) {
if (data === '1') {
this.push(true)
}
else if (data === '0') {
this.push(false)
}
else {
throw new Error('Illegal boolean value \'' + data + '\'')
}
this.value = false
}
Deserializer.prototype.endInt = function(data) {
var value = parseInt(data, 10)
if (isNaN(value)) {
throw new Error('Expected an integer but got \'' + data + '\'')
}
else {
this.push(value)
this.value = false
}
}
Deserializer.prototype.endDouble = function(data) {
var value = parseFloat(data)
if (isNaN(value)) {
throw new Error('Expected a double but got \'' + data + '\'')
}
else {
this.push(value)
this.value = false
}
}
Deserializer.prototype.endString = function(data) {
this.push(data)
this.value = false
}
Deserializer.prototype.endArray = function(data) {
var mark = this.marks.pop()
this.stack.splice(mark, this.stack.length - mark, this.stack.slice(mark))
this.value = false
}
Deserializer.prototype.endStruct = function(data) {
var mark = this.marks.pop()
, struct = {}
, items = this.stack.slice(mark)
, i = 0
for (; i < items.length; i += 2) {
struct[items[i]] = items[i + 1]
}
this.stack.splice(mark, this.stack.length - mark, struct)
this.value = false
}
Deserializer.prototype.endBase64 = function(data) {
var buffer = new Buffer(data, 'base64')
this.push(buffer)
this.value = false
}
Deserializer.prototype.endDateTime = function(data) {
var date = dateFormatter.decodeIso8601(data)
this.push(date)
this.value = false
}
var isInteger = /^-?\d+$/
Deserializer.prototype.endI8 = function(data) {
if (!isInteger.test(data)) {
throw new Error('Expected integer (I8) value but got \'' + data + '\'')
}
else {
this.endString(data)
}
}
Deserializer.prototype.endValue = function(data) {
if (this.value) {
this.endString(data)
}
}
Deserializer.prototype.endParams = function(data) {
this.responseType = 'params'
}
Deserializer.prototype.endFault = function(data) {
this.responseType = 'fault'
}
Deserializer.prototype.endMethodResponse = function(data) {
this.type = 'methodresponse'
}
Deserializer.prototype.endMethodName = function(data) {
this.methodname = data
}
Deserializer.prototype.endMethodCall = function(data) {
this.type = 'methodcall'
}
module.exports = Deserializer