We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
There was an error while loading.Please reload this page.
1 parented5c4b1 commitd1c5506Copy full SHA for d1c5506
command.js
@@ -29,16 +29,21 @@ class Command {
29
constructor(encoding,name,args,callback){
30
this.id=Command.id()
31
this.name=name
32
+this.encoding=encoding
33
this.callback=callback
34
this.arguments=args
35
+}
36
-if(Array.isArray(this.arguments)&&encoding){
-this.arguments=this.arguments.map((arg)=>encoding.encode(arg))
37
+toJSON(){
38
+return{
39
+id:this.id,
40
+name:this.name,
41
+arguments:this.arguments.map((arg)=>this.encoding.encode(arg))
42
}
43
44
45
toBuffer(){
-returnmessages.Command.encode(this)
46
+returnmessages.Command.encode(this.toJSON())
47
48
49
pack(){
encoding.js
@@ -56,7 +56,11 @@ function decode(buffer, offset) {
56
offset=0
57
58
59
-constdecoded=map(parse(buffer.slice(offset).toString('utf8')))
+try{
60
+returnmap(parse(buffer.slice(offset).toString('utf8')))
61
+}catch(err){
62
+returnmap(buffer.slice(offset).toString('utf8'))
63
64
65
returndecoded
66
example-client.js
@@ -10,15 +10,15 @@ socket.on('connect', () => {
10
if(error){
11
console.log('got error',error)
12
}else{
13
-console.log('got response',unserialize(results))
+console.log('got response',results)
14
15
})
16
17
alice.call('yo',[],(error,results)=>{
18
19
20
21
22
23
24
-})
+})
example-server.js
@@ -7,12 +7,12 @@ server.on('connection', (socket) => {
7
global.bob=newProtocol({connect:()=>socket})
8
9
bob.command('hey joe!',(command,reply)=>{
-console.log('got command',command.name,unserialize(command.arguments))
+console.log('got command',command)
reply(null,['result',1000])
bob.command('yo',(command,reply)=>{
reply({name:'BigError',code:'4000',message:'you dun goofed'})
protocol.js
@@ -249,6 +249,7 @@ class Protocol extends Duplex {
249
250
251
oncommand(command){
252
+this.emit('command',command)
253
this.emit(`command:${command.name}`,command,(err,results)=>{
254
if(results&&!Array.isArray(results)){
255
results=[results]
@@ -260,8 +261,8 @@ class Protocol extends Duplex {
260
261
262
263
-onresponse(response){
264
-constid=response.id.toString('hex')
+onresponse(res){
265
+constid=res.id.toString('hex')
266
constrequest=this.pending.get(id)
267
const{ callback}=request
268
@@ -270,7 +271,8 @@ class Protocol extends Duplex {
270
271
272
273
if('function'===typeofcallback){
-callback(response.error,response.results)
274
+constresults=res.results&&res.results.map((result)=>encoding.decode(result))
275
+callback(res.error,results)
276
277
278
response.js
@@ -32,25 +32,23 @@ class Response {
this.name=req.name
this.error=toMaybeError(error)
this.results=results
-
-if(Array.isArray(this.results)&&encoding){
-this.results=this.results.map((result)=>encoding.decode(result))
-}
-toBuffer(){
-returnmessages.Response.encode(this)
toJSON(){
+const{ encoding}=this
return{
id:this.id,
name:this.name,
error:this.error||null,
50
-results:this.results||[],
+results:this.results&&this.results.map((result)=>encoding.encode(result)),
51
52
53
+toBuffer(){
+returnmessages.Response.encode(this.toJSON())
+
54
55
returnpack(Response.WIRE_TYPE,this.toBuffer())