-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
75 lines (69 loc) · 3.12 KB
/
core.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
// Copyright (c) 2018 MyGnar, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { rpc } from 'protobufjs/minimal'
// TODO: Rename generator to factory throughout?
// TwirpJS transports override protobuf.js's implementation of "rpcCall", the original implementation
// is saved as the transport type 'ORIGINAL' and requires use of the createRpcImpl option
const ORIGINAL = 'ORIGINAL'
export const TransportGenerators = {
[ORIGINAL]: (origRpcCall => {
return (twirpURL, opts) => origRpcCall
})(rpc.Service.prototype.rpcCall), // capture rpcCall in case it gets altered
}
export const TRANSPORT_TYPES = { ORIGINAL }
TRANSPORT_TYPES.DEFAULT = ORIGINAL // can be overridden by RegisterTransportGenerator()
// Register a TransportGenerator
// Generator signature: (twirpURL, options) => (method, requestCtor, responseCtor, request, callback)
// * "request" and "callback" are the first and second arguments passed to a client rpc method
// Note that the callback argument does not need to actually be a callback -- a transport could
// define an alternate use for the callback argument where it is an options object or anything else
// * "method", "requestCtor", and "responseCtor" are provided by protobuf.js internals
export function registerTransportGenerator({ name, generator, setAsDefault }) {
if (TRANSPORT_TYPES[name] === name) {
throw new Error(`A twirpjs transport generator has already been registered for the type ${name}`)
}
TRANSPORT_TYPES[name] = name
if (setAsDefault) {
TRANSPORT_TYPES.DEFAULT = name
}
TransportGenerators[name] = generator
}
export default function newTwirpClient(hostURL, options = {}) {
const {
serviceRoute, // required
serviceClass, // required
transportType, // (optional) one of TRANSPORT_TYPES
createRpcImpl, // required when using TRANSPORT_TYPE.ORIGINAL (protobuf.js-style transport)
} = options
const twirpURL = `${hostURL}/twirp/${serviceRoute}`
const rpcImpl =
typeof createRpcImpl === 'function'
? createRpcImpl(twirpURL, options)
: () => {
throw new Error(
`Current transport type ${transportType || 'DEFAULT'} requires the createRpcImpl option`,
)
}
const client = serviceClass.create(rpcImpl, /* req delimited */ false, /* resp delimited */ false)
client.rpcCall = getRpcCall(twirpURL, options) // Override protobuf.js's implementation of rpcCall
return client
}
function getRpcCall(twirpURL, opts = {}) {
const { transportType } = opts
const tt = transportType || TRANSPORT_TYPES.DEFAULT
if (typeof TransportGenerators[tt] !== 'function') {
throw new Error(`Unsupported rpc transport type: ${tt}`)
}
return TransportGenerators[tt](twirpURL, opts)
}