Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1c5506

Browse files
committed
refactor(.): Fix regressions
1 parented5c4b1 commitd1c5506

File tree

6 files changed

+31
-22
lines changed

6 files changed

+31
-22
lines changed

‎command.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ class Command {
2929
constructor(encoding,name,args,callback){
3030
this.id=Command.id()
3131
this.name=name
32+
this.encoding=encoding
3233
this.callback=callback
3334
this.arguments=args
35+
}
3436

35-
if(Array.isArray(this.arguments)&&encoding){
36-
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))
3742
}
3843
}
3944

4045
toBuffer(){
41-
returnmessages.Command.encode(this)
46+
returnmessages.Command.encode(this.toJSON())
4247
}
4348

4449
pack(){

‎encoding.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ function decode(buffer, offset) {
5656
offset=0
5757
}
5858

59-
constdecoded=map(parse(buffer.slice(offset).toString('utf8')))
59+
try{
60+
returnmap(parse(buffer.slice(offset).toString('utf8')))
61+
}catch(err){
62+
returnmap(buffer.slice(offset).toString('utf8'))
63+
}
6064

6165
returndecoded
6266

‎example-client.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ socket.on('connect', () => {
1010
if(error){
1111
console.log('got error',error)
1212
}else{
13-
console.log('got response',unserialize(results))
13+
console.log('got response',results)
1414
}
1515
})
1616

1717
alice.call('yo',[],(error,results)=>{
1818
if(error){
1919
console.log('got error',error)
2020
}else{
21-
console.log('got response',unserialize(results))
21+
console.log('got response',results)
2222
}
2323
})
24-
})
24+
})

‎example-server.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ server.on('connection', (socket) => {
77
global.bob=newProtocol({connect:()=>socket})
88

99
bob.command('hey joe!',(command,reply)=>{
10-
console.log('got command',command.name,unserialize(command.arguments))
10+
console.log('got command',command)
1111
reply(null,['result',1000])
1212
})
1313

1414
bob.command('yo',(command,reply)=>{
15-
console.log('got command',command.name,unserialize(command.arguments))
15+
console.log('got command',command)
1616
reply({name:'BigError',code:'4000',message:'you dun goofed'})
1717
})
18-
})
18+
})

‎protocol.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ class Protocol extends Duplex {
249249
}
250250

251251
oncommand(command){
252+
this.emit('command',command)
252253
this.emit(`command:${command.name}`,command,(err,results)=>{
253254
if(results&&!Array.isArray(results)){
254255
results=[results]
@@ -260,8 +261,8 @@ class Protocol extends Duplex {
260261
})
261262
}
262263

263-
onresponse(response){
264-
constid=response.id.toString('hex')
264+
onresponse(res){
265+
constid=res.id.toString('hex')
265266
constrequest=this.pending.get(id)
266267
const{ callback}=request
267268

@@ -270,7 +271,8 @@ class Protocol extends Duplex {
270271
}
271272

272273
if('function'===typeofcallback){
273-
callback(response.error,response.results)
274+
constresults=res.results&&res.results.map((result)=>encoding.decode(result))
275+
callback(res.error,results)
274276
}
275277
}
276278
}

‎response.js‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,23 @@ class Response {
3232
this.name=req.name
3333
this.error=toMaybeError(error)
3434
this.results=results
35-
36-
if(Array.isArray(this.results)&&encoding){
37-
this.results=this.results.map((result)=>encoding.decode(result))
38-
}
39-
}
40-
41-
toBuffer(){
42-
returnmessages.Response.encode(this)
35+
this.encoding=encoding
4336
}
4437

4538
toJSON(){
39+
const{ encoding}=this
4640
return{
4741
id:this.id,
4842
name:this.name,
4943
error:this.error||null,
50-
results:this.results||[],
44+
results:this.results&&this.results.map((result)=>encoding.encode(result)),
5145
}
5246
}
5347

48+
toBuffer(){
49+
returnmessages.Response.encode(this.toJSON())
50+
}
51+
5452
pack(){
5553
returnpack(Response.WIRE_TYPE,this.toBuffer())
5654
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp