-
Notifications
You must be signed in to change notification settings - Fork 16
/
grpc-node.node.txt
449 lines (349 loc) · 22.9 KB
/
grpc-node.node.txt
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
GRPC-NODE
/=+===============================+=\
/ : : \
)==: NODE :==(
\ :_______________________________: /
\=+===============================+=/
require('gprc') #Version 1.24.11
require('@gprc/grpc-js') #Version 1.6.7
#Same interface as grpc.
#Has a few more features:
# - unix|ipv4|ipv6:* support
# - no native addon
#Misses a few features:
# - compress only response, not request
# - no automatic retries
# - no stats|tracing|monitoring
# - load balancing: only pick first and round robin
HEALTH-CHECKING ==> #See gprc-health-checking doc
/=+===============================+=\
/ : : \
)==: PROTOBUF :==(
\ :_______________________________: /
\=+===============================+=/
require('@gprc/proto-loader') #Version 0.6.13
#Thin wrapper above protobufjs:
# - use DEF instead of ROOT, which is similar but simpler
# - provides with default serialize|deserialize() for RPC.*
DEF #Similar to protobufjs ROOT, but different format
load('PATH.proto'[_ARR][, LOPTS]) #LOPTS:
->PROMISE_DEF # - includeDirs STR_ARR: like protoc --proto_path
# - any from protobufjs toObject() (for MESSAGE parsing)
# - any from protobufjs load()
loadSync(...)->DEF #
loadFileDescriptorSetFromObject
(FILE_DESCRIPTOR[, LOPTS])->DEF #Same from a FILE_DESCRIPTOR (see protobufjs)
loadFileDescriptorSetFromBuffer
(BUFFER[, LOPTS])->DEF #Same from a file descriptor protobuf BUFFER
fromJSON(ROOT)->DEF #Using protobuf.js ROOT
DEF.'[PACKAGE_VARR.]SERVICE_NAME' #SERVICE
SERVICE.RPC_NAME #RPC
RPC.path #'/[PACKAGE_VARR.]SERVICE_NAME/RPC_NAME'
RPC.originalName #'RPC_NAME' (camelCased)
RPC.request|responseStream #BOOL
RPC.request|responseType #MESSAGE_TYPE
RPC.request|responseSerialize #E.g. Buffer.from(MESSAGE.serializeBinary())
(MESSAGE)->BUFFER #Should validate MESSAGE instanceof Message
RPC.request|responseDeserialize
(BUFFER)->MESSAGE #E.g. MESSAGE.deserializeBinary(new Uint8Array(BUFFER))
DEF.
'[PACKAGE_VARR.]MESSAGE_TYP_NAME'#MESSAGE_TYPE
DEF.
'[PACKAGE_VARR.]ENUM_TYPE_NAME' #ENUM_TYPE
MESSAGE_TYPE|ENUM_TYPE.type #OBJ (protobufjs-specific reflection)
MESSAGE_TYPE|ENUM_TYPE.
fileDescriptorProtos #BUFFER_ARR of Protobuf file descriptors sets
/=+===============================+=\
/ : : \
)==: SERVER :==(
\ :_______________________________: /
\=+===============================+=/
new Server([SOPTS]) #SERVER
SERVER.addService(SERVICE, OBJ) #Define RPC endpoints in server.
#Must define OBJ.RPC_NAME(SCALL[, RES_FUNC])
SCALL.request #REQ (non-streaming request)
RES_FUNC(SERROR, RES, METADATA #Non-streaming response
[, WRITE_FLAGS]) #METADATA is trailing metadata (see below).
ISTREAM ==> #SCALL is ISTREAM of REQ when receiving streaming.
#Should handle errors
OSTREAM ==> #SCALL is OSTREAM of RES when sending streaming.
#Should handle errors
MESSAGE #Either OBJ or google-protobuf MESSAGE
REQ #Request MESSAGE
RES #Response MESSAGE
SERVER.bindAsync
('ORIGIN', SCREDENTIALS,
FUNC(ERROR, PORT_NUM)) #Start HTTP2 server
SERVER.start() #Starts accepting requests
#Must be done after SERVER.bindAsync() completed
SERVER.tryShutdown(FUNC()) #Shutdown, waiting for ongoing requests to end
SERVER.forceShutdown() #Shutdown, without waiting for ongoing requests to end
CHOPTS.default_authority #
/=+===============================+=\
/ : : \
)==: CLIENT :==(
\ :_______________________________: /
\=+===============================+=/
loadPackageDefinition(DEF)
->SERVICES #
SERVICES.PACKAGE_VARR.SERVICE_VAR #ABSERVICE
makeClientConstructor(SERVICE)
->ABSERVICE #Same as loadPackageDefinition() but from SERVICE
ABSERVICE.service #SERVICE
ABSERVICE.serviceName #STR
new ABSERVICE('ORIGIN',
CCREDENTIALS[, NOPTS]) #CLIENT
CLIENT.waitForReady
(DEADLINE, FUNC([ERROR])) #Calls FUNC() once connected
MOPTS.waitForReady #BOOL (def: false). Do not send request until connection is ready.
COPTS.host #STR
CLIENT.close() #
CLIENT.RPC_NAME([REQ, ]
[METADATA, ][COPTS, ][REQ_FUNC]) #Call RPC endpoints from client.
->CCALL #REQ is non-streaming request
REQ_FUNC(SERROR, RES) #Non-streaming response
STREAMING ==> #Like SCALL, but inverting REQ|RES
#Different signature: OSTREAM.write(REQ[, WRITE_FLAGS][, FUNC()])
/=+===============================+=\
/ : : \
)==: CLIENT GENERATION :==(
\ :_______________________________: /
\=+===============================+=/
grpc-tools #Version 1.11.2
#Only contains the following binary
gprc_tools_node_protoc #Like protoc, but with additional following flag
--grpc_out=
[LANG_OPT[=STR]:]OUT_DIR #Creates FILE_grpc_pb.js which:
# - imports FILE_pb.js as CommonJS (i.e. --js_out must be used) to get MESSAGE_TYPE
# - export SERVICE_NAMEService: SERVICE from *.proto
# - export ABSERVICEClient from makeClientConstructor(SERVICE)
LANG_OPT.grpc_js #Use @grpc/grpc-js instead of gprc
LANG_OPT.
generate_package_definition #Export DEF instead of ABSERVICE
/=+===============================+=\
/ : : \
)==: STATUS :==(
\ :_______________________________: /
\=+===============================+=/
CCALL.on('status', FUNC(STATUS)) #
STATUS.code #STATUS_CODE
status.VAR #STATUS_CODE (e.g. status.OK)
STATUS.details #STR
STATUS.metadata #METADATA (trailing)
SERROR #ERROR with code STATUS_CODE
/=+===============================+=\
/ : : \
)==: METADATA :==(
\ :_______________________________: /
\=+===============================+=/
new Metadata([MOPTS]) #METADATA
METADATA.setOptions(MOPTS) #
METADATA.getOptions()->MOPTS #
METADATA.get('KEY')
->STR|BUFFER_ARR #
METADATA.set('KEY', STR|BUFFER) #Set single value
METADATA.add('KEY', STR|BUFFER) #Add to ARR value
METADATA.remove('KEY') #
METADATA.getMap()->MAP #Clone
METADATA.clone()->METADATA2 #
METADATA.merge(METADATA2) #
METADATA.toHttp2Headers()->OBJ
Metadata.fromHttp2Headers(OBJ)
->METADATA #Converts from|to plain OBJ
SCALL.metadata #Request METADATA
SCALL.sendMetadata(METADATA)
CCALL.on
('metadata', FUNC(METADATA)) #Non-trailing response METADATA
TRAILING RESPONSE METADATA ==> #Use RES_FUNC to send, and CCALL.on('status') to receive
/=+===============================+=\
/ : : \
)==: TIMEOUT/CANCEL :==(
\ :_______________________________: /
\=+===============================+=/
CCALL.cancel() #Cancel request
SCALL.on('cancelled', FUNC()) #
SCALL.cancelled #BOOL
DEADLINE #DATE or UNIX_TIMESTAMP_NUM (in ms) or Infinity (i.e. none)
COPTS.deadline #DEADLINE (def: none)
/=+===============================+=\
/ : : \
)==: AUTHENTICATION :==(
\ :_______________________________: /
\=+===============================+=/
SCREDENTIALS|CCREDENTIALS #Server|client authentication for a whole connection
CREDENTIALS #Request|response authentication for a single request
ServerCredentials.createInsecure()
->SCREDENTIALS #No authentication
ServerCredentials.createSsl #SSL authentication
(BUFFER|null, KEY_ARR[, BOOL]) #BUFFER (def: null): certificate
->SCREDENTIALS #KEY_ARR: private_key BUFFER2, cert_chain BUFFER3
#BOOL (def: false): check client certificate
credentials.createInsecure()
->CCREDENTIALS #No authentication
credentials.createSsl #SSL authentication
(BUFFER|null, BUFFER2|null, #BUFFER2|3 are private_key|cert_chain
BUFFER3|null[, OPTS]) #OPTS:
->CCREDENTIALS # - checkServerIdentity('ORIGIN', CERTIFICATE_OBJ)[->ERROR] (def: none)
credentials.
createFromSecureContext #Same but using TSL.createSecureContext(OBJ)
(OBJ[, OPTS])->CCREDENTIALS #Same OPTS
credentials.createEmpty()
->CREDENTIALS #No authentication
credentials.
createFromMetadataGenerator
(FUNC(OBJ, FUNC(ERROR,METADATA)))#OBJ: service_url STR
->CREDENTIALS #Custom header authentication
credentials.combineCallCredentials
(CREDENTIALS_ARR)->CREDENTIALS #
credentials.
combineChannelCredentials
(CCREDENTIALS, CREDENTIALS_ARR)
->CCREDENTIALS #When using both channel and request-specific authentication
COPTS.credentials #CREDENTIALS
/=+===============================+=\
/ : : \
)==: CHANNEL :==(
\ :_______________________________: /
\=+===============================+=/
CHANNEL #Underlying transport. By def, HTTP/2 logic
#Automatically reconnects
NOPTS.channelOverride #CHANNEL. Customize
NOPTS.channelFactoryOverride
('ORIGIN', CCREDENTIALS, COPTS)
->CHANNEL #Same but as a method
CLIENT.getChannel()->CHANNEL #
CHANNEL.getTarget()->'ORIGIN' #
CHANNEL.getConnectivityState
([BOOL])->CONNECTIVITY_STATE #If BOOL true (def: false), tries to connect if not connected
CHANNEL.watchConnectivityState
(CONNECTIVITY_STATE, DEADLINE,
FUNC(ERROR)) #Call FUNC() when CONNECTIVITY_STATE, or after DEADLINE timeout
connectivityState.ENUM #Among: IDLE, CONNECTING, READY, TRANSIENT_FAILURE, SHUTDOWN
CCALL|SCALL.getPeer()->'ORIGIN' #
Channelz #Endpoints to query runtime|debugging information about channels and connections.
#Not documented here yet
CHOPTS.enable_channelz #BOOL (def: true)
addAdminServicesToServer(...) #Add admin endpoints like Channelz
#Not documented here yet
/=+===============================+=\
/ : : \
)==: CHANNEL OPTIONS :==(
\ :_______________________________: /
\=+===============================+=/
SOPTS|NOPTS.'grpc.PROP' #Documented as [S|N]CHOPTS.PROP
CHOPTS.max_send_message_length #NUM (in bytes, def: -1)
CHOPTS.max_receive_message_length #NUM (in bytes, def: 4MB)
SCHOPTS.max_concurrent_streams #NUM (def: unlim). Max number of requests at once.
CHOPTS.max_session_memory #NUM (in bytes, def: -1). Max memory per HTTP/2 session
NCHOPTS.
primary|secondary_user_agent #STR. Prepended|appended to user-agent METADATA
CHOPTS.initial| #
max_reconnect_backoff_ms #NUM (in ms) (def: 1s initial, 2m max)
CHOPTS.keepalive_time_ms #NUM (in ms) (def|max: 1 month in client, 2 hours in server).
CHOPTS.keepalive_timeout_ms #NUM (in ms) (def: 20s).
CHOPTS.
keepalive_permit_without_calls #0|1 (def: 0).
CHOPTS.use_local_subchannel_pool #BOOL. Make the CHANNELs|sockets pool be local to this server, instead of being global.
CHOPTS.dns_min_time_between_
resolutions_ms #NUM (def: 3e4, in ms)
/=+===============================+=\
/ : : \
)==: COMPRESSION :==(
\ :_______________________________: /
\=+===============================+=/
CHOPTS. #NUM
default_compression_algorithm #Compression of REQ
#Can be:
# - 0: none
# - 1: DEFLATE (non-streaming)
# - 2: GZIP (non-streaming)
# - 3: GZIP (streaming)
#Does not seem to be supported yet
CHOPTS.default_compression_level #NUM
#Compression of RES
#Can be: 0 (none), 1 (low), 2 (medium), 3 (high)
writeFlags.ENUM #WRITE_FLAGS:
# - NO_COMPRESS: force no compression on a specific REQ|RES
/=+===============================+=\
/ : : \
)==: CUSTOM CLIENT :==(
\ :_______________________________: /
\=+===============================+=/
NOPTS.callInvocationTransformer #Modify each request before sending
(OBJ)->OBJ #OBJ:
# - argument REQ
# - metadata METADATA
# - callOptions COPTS
# - callback REQ_FUNC
# - methodDefinition RPC
# - call CCALL
# - channel CHANNEL
COPTS|NOPTS.interceptors #ARR of FUNC(COPTS, FUNC2)->new InterceptingCall(FUNC2(COPTS, OBJ))
#Customizes some event handling using OBJ.FUNC(...):
# - start(METADATA, LISTENER, FUNC4([ERROR]))
# - sendMessage(MESSAGE, FUNC4([ERROR]))
# - halfClose(FUNC4([ERROR]))
# - cancel(MESSAGE, FUNC4([ERROR]))
#LISTENER is OBJ with:
# - onReceiveMetadata(METADATA, FUNC5([ERROR]))
# - onReceiveMessage(MESSAGE, FUNC5([ERROR]))
# - onReceiveStatus(STATUS, FUNC5([ERROR]))
#COPTS also has method_definition RPC
COPTS|NOPTS.interceptor_providers #FUNC3_ARR: same using FUNC3(RPC)->FUNC
/=+===============================+=\
/ : : \
)==: PROXY :==(
\ :_______________________________: /
\=+===============================+=/
CHOPTS.enable_http_proxy #0|1 (def: 1). Allow client HTTP proxy (if configured with ENVVAR GRPC_PROXY)
ENVVAR GRPC_PROXY="URL" #Proxies all response from server to client at URL
ENVVAR NO_GRPC_PROXY="HOST,..." #
COPTS.parent #CCALL. Indicates current client is a proxy
COPTS.propagate_flags #NUM
#With COPTS.parent, proxies some information
#Bitwise-flag of propagate.VAR among:
# - DEADLINE: timeout
# - CANCELLATION: request cancellation
# - CENSUS_STATS_CONTEXT
# - CENSUS_TRACING_CONTEXT
# - DEFAULTS (default values, i.e. all of them)
/=+===============================+=\
/ : : \
)==: LOAD BALANCING :==(
\ :_______________________________: /
\=+===============================+=/
CHOPTS.service_config #'JSON'. Load balancing configuration.
#Is OBJ:
# - loadBalancingPolicy STR
# - loadBalancingConfig OBJ_ARR:
# - pick_first|round_robin OBJ
# - methodConfig OBJ_ARR:
# - name OBJ: service STR, method STR
# - waitForReady BOOL
# - timeout 'NUM[.NUM]s'
# - maxRequestBytes|maxResponseBytes NUM
/=+===============================+=\
/ : : \
)==: LOGGING :==(
\ :_______________________________: /
\=+===============================+=/
ENVVAR GRPC_TRACE=STR #Debug mode (def: none). STR can be 'all' or specific category.
ENVVAR GRPC_VERBOSITY=STR #Can be 'NONE|DEBUG|INFO|ERROR|NONE' (def: 'INFO')
setLoggerVerbosity(NUM) #Same, using logVerbositoy.DEBUG|INFO|ERROR
setLogger(CONSOLE) #Customize logging
/=+===============================+=\
/ : : \
)==: TYPESCRIPT :==(
\ :_______________________________: /
\=+===============================+=/
proto-loader-gen-types
FILE.proto... #Transform to *.ts, with associated *.proto files
--outDir|-O DIR #DIR where to output
--grpcLib STR #'@grpc/grpc-js' or 'grpc'
--includeDirs|-I #Like protoc -I
--keepCase #Like protobuf.js load()
--longs|enums|bytes|defaults|
arrays|objects|oneofs|json #Like protobuf.js toObject()
--includeComments #