-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrpc_request_update_as_event.js
131 lines (111 loc) · 3.27 KB
/
rpc_request_update_as_event.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
const bufferAsBase64 = n => !!n.length ? n.toString('base64') : undefined;
const {isBuffer} = Buffer;
const request = 'request';
const response = 'response';
const streamAuth = 'stream_auth';
/** Derive request details from an RPC request update
{
msg_id: <Message Id Number String>
request_id: <Request Id Number String>
raw_macaroon: <Raw Macaroon Buffer Object>
custom_caveat_condition: <Custom Caveat Condition String>
[request]: {
method_full_uri: <Method URI String>
stream_rpc: <Is String RPC Bool>
type_name: <RPC Message Type String>
serialized: <Raw Protobuf Request Buffer Object>
}
[response]: {
method_full_uri: <Method URI String>
stream_rpc: <Is String RPC Bool>
type_name: <RPC Message Type String>
serialized: <Raw Protobuf Response Buffer Object>
}
[stream_auth]: {
method_full_uri: <Method URI String>
}
intercept_type: <RPC Update Type String>
}
@throws
<Error>
@returns
{
call: <Call Id Number>
[event]: <Event Type String>
id: <Request Id Number>
[macaroon]: <Base64 Encoded Macaroon String>
[uri]: <RPC URI String>
}
*/
module.exports = args => {
if (!args) {
throw new Error('ExpectedRpcRequestUpdateDetailsToDeriveUpdateEvent');
}
if (args.custom_caveat_condition === undefined) {
throw new Error('ExpectedCustomCaveatConditionInRpcRequestUpdate');
}
if (args.intercept_type === undefined) {
throw new Error('ExpectedInterceptTypeInRpcRequestUpdate');
}
if (args.msg_id === undefined) {
throw new Error('ExpectedMessageIdInRpcRequestUpdate');
}
if (!isBuffer(args.raw_macaroon)) {
throw new Error('ExpectedCompleteMacaroonCredentialsInRequestUpdate');
}
if (args.request_id === undefined) {
throw new Error('ExpectedRequestIdInRpcRequestUpdate');
}
const call = Number(args.request_id);
const id = Number(args.msg_id);
switch (args.intercept_type) {
// New subscription
case streamAuth:
if (!args[streamAuth]) {
throw new Error('ExpectedStreamAuthDetailsInRpcRequestUpdate');
}
if (!args[streamAuth].method_full_uri) {
throw new Error('ExpectedFullUriForStreamAuthRpcRequestUpdate');
}
return {
call,
id,
event: request,
macaroon: bufferAsBase64(args.raw_macaroon),
uri: args[streamAuth].method_full_uri,
};
// New request
case request:
if (!args[request]) {
throw new Error('ExpectedRequestDetailsInRpcRequestUpdate');
}
if (!args[request].method_full_uri) {
throw new Error('ExpectedFullUriForRequestRpcRequestUpdate');
}
return {
call,
id,
event: request,
macaroon: bufferAsBase64(args.raw_macaroon),
uri: args[request].method_full_uri,
};
// Response to past request
case response:
if (!args[response]) {
throw new Error('ExpectedResponseDetailsInRpcRequestUpdate');
}
if (!args[response].method_full_uri) {
throw new Error('ExpectedFullUriForResponseRpcRequestUpdate');
}
return {
call,
id,
event: response,
macaroon: bufferAsBase64(args.raw_macaroon),
uri: args[response].method_full_uri,
};
// Unknown update
default:
return {call, id, macaroon: bufferAsBase64(args.raw_macaroon)};
}
};