4
4
import { Buffer } from "buffer" ;
5
5
import * as msgpack5 from "msgpack5" ;
6
6
7
- import { CompletionMessage , HubMessage , IHubProtocol , ILogger , InvocationMessage , LogLevel , MessageHeaders , MessageType , NullLogger , StreamCompleteMessage , StreamDataMessage , StreamInvocationMessage , StreamItemMessage , TransferFormat } from "@aspnet/signalr" ;
7
+ import { CompletionMessage , HubMessage , IHubProtocol , ILogger , InvocationMessage , LogLevel , MessageHeaders , MessageType , NullLogger , StreamInvocationMessage , StreamItemMessage , TransferFormat } from "@aspnet/signalr" ;
8
8
9
9
import { BinaryMessageFormat } from "./BinaryMessageFormat" ;
10
10
import { isArrayBuffer } from "./Utils" ;
@@ -25,6 +25,10 @@ export class MessagePackHubProtocol implements IHubProtocol {
25
25
/** The TransferFormat of the protocol. */
26
26
public readonly transferFormat : TransferFormat = TransferFormat . Binary ;
27
27
28
+ private readonly errorResult = 1 ;
29
+ private readonly voidResult = 2 ;
30
+ private readonly nonVoidResult = 3 ;
31
+
28
32
/** Creates an array of HubMessage objects from the specified serialized representation.
29
33
*
30
34
* @param {ArrayBuffer | Buffer } input An ArrayBuffer containing the serialized representation.
@@ -65,15 +69,12 @@ export class MessagePackHubProtocol implements IHubProtocol {
65
69
return this . writeInvocation ( message as InvocationMessage ) ;
66
70
case MessageType . StreamInvocation :
67
71
return this . writeStreamInvocation ( message as StreamInvocationMessage ) ;
68
- case MessageType . StreamData :
69
- return this . writeStreamData ( message as StreamDataMessage ) ;
70
72
case MessageType . StreamItem :
73
+ return this . writeStreamItem ( message as StreamItemMessage ) ;
71
74
case MessageType . Completion :
72
- throw new Error ( `Writing messages of type ' ${ message . type } ' is not supported.` ) ;
75
+ return this . writeCompletion ( message as CompletionMessage ) ;
73
76
case MessageType . Ping :
74
77
return BinaryMessageFormat . write ( SERIALIZED_PING_MESSAGE ) ;
75
- case MessageType . StreamComplete :
76
- return this . writeStreamComplete ( message as StreamCompleteMessage ) ;
77
78
default :
78
79
throw new Error ( "Invalid message type." ) ;
79
80
}
@@ -147,13 +148,15 @@ export class MessagePackHubProtocol implements IHubProtocol {
147
148
arguments : properties [ 4 ] ,
148
149
headers,
149
150
invocationId,
151
+ streamIds : [ ] ,
150
152
target : properties [ 3 ] as string ,
151
153
type : MessageType . Invocation ,
152
154
} ;
153
155
} else {
154
156
return {
155
157
arguments : properties [ 4 ] ,
156
158
headers,
159
+ streamIds : [ ] ,
157
160
target : properties [ 3 ] ,
158
161
type : MessageType . Invocation ,
159
162
} ;
@@ -181,24 +184,20 @@ export class MessagePackHubProtocol implements IHubProtocol {
181
184
throw new Error ( "Invalid payload for Completion message." ) ;
182
185
}
183
186
184
- const errorResult = 1 ;
185
- const voidResult = 2 ;
186
- const nonVoidResult = 3 ;
187
-
188
187
const resultKind = properties [ 3 ] ;
189
188
190
- if ( resultKind !== voidResult && properties . length < 5 ) {
189
+ if ( resultKind !== this . voidResult && properties . length < 5 ) {
191
190
throw new Error ( "Invalid payload for Completion message." ) ;
192
191
}
193
192
194
193
let error : string | undefined ;
195
194
let result : any ;
196
195
197
196
switch ( resultKind ) {
198
- case errorResult :
197
+ case this . errorResult :
199
198
error = properties [ 4 ] ;
200
199
break ;
201
- case nonVoidResult :
200
+ case this . nonVoidResult :
202
201
result = properties [ 4 ] ;
203
202
break ;
204
203
}
@@ -217,31 +216,43 @@ export class MessagePackHubProtocol implements IHubProtocol {
217
216
private writeInvocation ( invocationMessage : InvocationMessage ) : ArrayBuffer {
218
217
const msgpack = msgpack5 ( ) ;
219
218
const payload = msgpack . encode ( [ MessageType . Invocation , invocationMessage . headers || { } , invocationMessage . invocationId || null ,
220
- invocationMessage . target , invocationMessage . arguments ] ) ;
219
+ invocationMessage . target , invocationMessage . arguments , invocationMessage . streamIds ] ) ;
221
220
222
221
return BinaryMessageFormat . write ( payload . slice ( ) ) ;
223
222
}
224
223
225
224
private writeStreamInvocation ( streamInvocationMessage : StreamInvocationMessage ) : ArrayBuffer {
226
225
const msgpack = msgpack5 ( ) ;
227
226
const payload = msgpack . encode ( [ MessageType . StreamInvocation , streamInvocationMessage . headers || { } , streamInvocationMessage . invocationId ,
228
- streamInvocationMessage . target , streamInvocationMessage . arguments ] ) ;
227
+ streamInvocationMessage . target , streamInvocationMessage . arguments , streamInvocationMessage . streamIds ] ) ;
229
228
230
229
return BinaryMessageFormat . write ( payload . slice ( ) ) ;
231
230
}
232
231
233
- private writeStreamData ( streamDataMessage : StreamDataMessage ) : ArrayBuffer {
232
+ private writeStreamItem ( streamItemMessage : StreamItemMessage ) : ArrayBuffer {
234
233
const msgpack = msgpack5 ( ) ;
235
- const payload = msgpack . encode ( [ MessageType . StreamData , streamDataMessage . streamId ,
236
- streamDataMessage . item ] ) ;
234
+ const payload = msgpack . encode ( [ MessageType . StreamItem , streamItemMessage . headers || { } , streamItemMessage . invocationId ,
235
+ streamItemMessage . item ] ) ;
237
236
238
237
return BinaryMessageFormat . write ( payload . slice ( ) ) ;
239
238
}
240
239
241
- private writeStreamComplete ( streamCompleteMessage : StreamCompleteMessage ) : ArrayBuffer {
240
+ private writeCompletion ( completionMessage : CompletionMessage ) : ArrayBuffer {
242
241
const msgpack = msgpack5 ( ) ;
243
- const payload = msgpack . encode ( [ MessageType . StreamComplete , streamCompleteMessage . streamId ,
244
- streamCompleteMessage . error || null ] ) ;
242
+ const resultKind = completionMessage . error ? this . errorResult : completionMessage . result ? this . nonVoidResult : this . voidResult ;
243
+
244
+ let payload : any ;
245
+ switch ( resultKind ) {
246
+ case this . errorResult :
247
+ payload = msgpack . encode ( [ MessageType . Completion , completionMessage . headers || { } , completionMessage . invocationId , resultKind , completionMessage . error ] ) ;
248
+ break ;
249
+ case this . voidResult :
250
+ payload = msgpack . encode ( [ MessageType . Completion , completionMessage . headers || { } , completionMessage . invocationId , resultKind ] ) ;
251
+ break ;
252
+ case this . nonVoidResult :
253
+ payload = msgpack . encode ( [ MessageType . Completion , completionMessage . headers || { } , completionMessage . invocationId , resultKind , completionMessage . result ] ) ;
254
+ break ;
255
+ }
245
256
246
257
return BinaryMessageFormat . write ( payload . slice ( ) ) ;
247
258
}
0 commit comments