-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
327 lines (287 loc) · 11.8 KB
/
index.js
File metadata and controls
327 lines (287 loc) · 11.8 KB
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
// @flow
import jsonStream from "json-lpduplex-stream"
import traverse from "traverse"
import { memoize as _memoize, set as _set } from "lodash"
import Backoff from "backo"
import uuidv4 from "uuid/v4"
import serializeError from 'serialize-error'
import type { Duplex } from "stream"
import type { CallPayload, Payload, RejectPayload, RemoteDescriptor, ResolvePayload, SerialMethod, SerialProp } from "./types"
import { RemoteError } from './utils'
import { TYPE_CALL, TYPE_RESOLVE, TYPE_REJECT, TYPE_DESCRIPTOR } from './constants'
export const SIGN_TYPE_NONCE = "nonce"
const NONCE_NOT_INITIALIZED = 'NONCE_NOT_INITIALIZED'
type PromisyGetterThing = string | (() => string) | (() => Promise<string>)
type Authenticator = PromisyGetterThing
type Signer = (string) => Promise<string>
type SignerOptions = {
type: typeof SIGN_TYPE_NONCE,
}
export type Remote<Face> = {
(): Promise<Face>,
auth: (authenticator: Authenticator) => void,
forceConnect: () => any,
sign: (signer: Signer, options: SignerOptions) => void
}
const sleepReject = async (timeout: number, label?: string) =>
new Promise((resolve, reject) => setTimeout(() => reject(new Error(`edonode: ${label || ''} connection timed out after ${timeout}ms`)), timeout))
export let connectionRegistry: Map<string, any> = new Map()
type BaseStream = () => Duplex | Object
type VerifyTypeNonce = (nonce: string, signature: any) => (void | Promise<void>)
type Options = {|
autoReconnect?: boolean,
debug?: boolean,
duplexSerializer?: Function,
name?: string,
onStreamEvents?: {
onOpen?: (Object) => any,
onClose?: (Object) => any,
onError?: (Object) => any,
},
sessionId?: PromisyGetterThing,
timeout?: number,
verify?: VerifyTypeNonce,
|}
type ContextMethod<F, O> = {
v?: F,
options?: O,
}
type Context = { auth: ContextMethod<Authenticator, void>, sign: ContextMethod<Signer, SignerOptions>, remoteNonce: string, }
// @NOTE return type any, not sure how to proxy the Face type through
function edonode(baseStream: BaseStream, rpc: Object | void, options: Options): any {
// #validations
if (options.autoReconnect && typeof baseStream !== "function")
throw new Error("edonode: autoReconnect requires stream be a factory")
// #private variables
const backoff = new Backoff({ min: 1000, max: 600000 })
let _stream
let _rpc
let _rpcPromise
let _connectTimeout
// @NOTE default name to the baseStream name
options.name = options.name || baseStream.name
let _context: Context = {
auth: { },
sign: { },
remoteNonce: NONCE_NOT_INITIALIZED,
}
// #plumbing
const onConnect = ({ rpc, nonce }) => {
_rpc = rpc
_context.remoteNonce = nonce
backoff.reset()
return rpc
}
const requestReconnect = error => {
if (options.debug && error) console.error("edonode: requestReconnect, error: ", error)
// if reconnect alreay pending, noop
if (_connectTimeout) return
_rpc = null
_rpcPromise = null
const wait = backoff.duration()
if (options.debug) console.log("edonode: requestReconnect, connecting in", wait, _connectTimeout)
_connectTimeout = setTimeout(connect, wait)
}
const connect = () => {
// clear any scheduled connect
_connectTimeout !== null && clearTimeout(_connectTimeout)
_connectTimeout = null
_stream = typeof baseStream === "function" ? baseStream() : baseStream
monitorStream(_stream)
_rpcPromise = connectRpc(_stream, _context, rpc, options)
.then(onConnect)
.catch(requestReconnect)
return _rpcPromise
}
function monitorStream(stream) {
// #stream monitoring
let onStreamEvents = options.onStreamEvents || {}
stream.on("open", e => {
if (onStreamEvents.onOpen) onStreamEvents.onOpen(e)
if (options.debug) console.log("edonode: stream open", e)
})
stream.on("error", e => {
if (onStreamEvents.onError) onStreamEvents.onError(e)
if (options.debug) console.log("edonode: stream err", e)
if (options.autoReconnect) requestReconnect()
})
stream.on("close", e => {
if (onStreamEvents.onClose) onStreamEvents.onClose(e)
if (options.debug) console.log("edonode: stream close", e)
if (options.autoReconnect) requestReconnect()
// else: stream closed now, is cleanup required?
})
}
// #init @TODO should we remove this and have it be lazy?
connect()
function remote(timeout: number = options.timeout || 500) {
if (_rpc) return _rpc
// @TODO implement reconnect logic
return Promise.race([
// @NOTE if there is no rpcPromise, connect immediately. Should this automatic behavior be replaced by an explicit control?
_rpcPromise || connect(),
sleepReject(timeout, options.name)
])
}
remote.auth = (authenticator: Authenticator) => {
if (_context.auth.v) throw new Error('edonode: hot swapping auth is not currently supported')
_context.auth = { v: authenticator }
}
remote.sign = async (signer, options = { type: SIGN_TYPE_NONCE }) => {
if (_context.sign.v) throw new Error('edonode: hot swapping sign is not currently supported')
// @NOTE this is memoized which means signer need to be deterministic and pure. @TODO anyway to enforce this?
_context.sign = { v: _memoize(signer), options }
}
remote.forceConnect = connect
return remote
}
async function prepareRPC(rpc, options: Options): Promise<[Map<string, { method: Function, path: string }>, RemoteDescriptor]> {
let methods = []
let props = []
let registry = new Map()
traverse(rpc).forEach(function(node) {
if (typeof node === "function") {
let key = Math.random().toString()
methods.push({ path: this.path, key })
registry.set(key, { method: node, path: this.path })
} else {
props.push({ path: this.path, node })
}
})
let sessionId = typeof options.sessionId === 'function' ? await options.sessionId() : options.sessionId
let remoteDescriptor: RemoteDescriptor = { type: TYPE_DESCRIPTOR, methods, props, nonce: uuidv4(), sessionId }
return [registry, remoteDescriptor]
}
// @NOTE flow stream type does not understand object mode streams
async function connectRpc(
_stream: any,
_context: Context,
rpc: ?Object,
options: Options
): Promise<Object> {
let _lastSessionId = null
let duplexSerializer = options.duplexSerializer || jsonStream
let stream = duplexSerializer(_stream)
let remotes = {}
let remoteMethodNames = {}
const [localRegistry, remoteDescriptor] = await prepareRPC(rpc, options)
const callPromises: Map<string, { resolve: Function, reject: Function, methodKey: string }> = new Map()
// send the description of our available rpc immediately
stream.write(remoteDescriptor)
const callRemote = async (methodKey, ...args) => {
// @TODO more efficient way than attaching to every call? it can already be closed over in the stream
let sessionId = typeof options.sessionId === 'function' ? await options.sessionId() : options.sessionId
return new Promise(async (resolve, reject) => {
let callId = Math.random().toString()
callPromises.set(callId, { resolve, reject, methodKey })
let call: CallPayload = {
type: TYPE_CALL,
callId,
methodKey,
args,
sessionId,
authentication: typeof _context.auth.v === 'function' ? await _context.auth.v() : _context.auth.v,
signature: typeof _context.sign.v === 'function' ? await _context.sign.v(_context.remoteNonce) : _context.sign.v,
}
stream.write(call)
})
}
function parseRPC (data: RemoteDescriptor): { rpc: Object, nonce: string } {
let mkmethod = methodKey => {
return async (...args) => callRemote(methodKey, ...args)
}
data.methods.forEach(m => {
remoteMethodNames[m.key] = m.path
_set(remotes, m.path, mkmethod(m.key))
})
data.props.forEach(p => {
_set(remotes, p.path, p.node)
})
// store connection in registry
data.sessionId && connectionRegistry.set(data.sessionId, remotes)
_lastSessionId = data.sessionId
return { rpc: remotes, nonce: data.nonce }
}
return new Promise((resolveConnect, rejectConnect) => {
stream.on("error", (err) => {
// @TODO should we expose this to the consumer via a onError callback or similar?
// @TODO hide this behind options.debug in the future
console.error("Edonode - Stream Error", err)
})
stream.on("data", async (payload: Payload) => {
if (payload.type === TYPE_DESCRIPTOR) {
resolveConnect(parseRPC(payload))
} else if (payload.type === TYPE_CALL) {
let { method, path } = localRegistry.get(payload.methodKey) || {}
if (options.debug) console.log(`Edonode - Call - ${path}`, payload)
// @NOTE we allow sessionId to change on any call. The alternative is we could require the connection be reset completely on sessionId change
// @TODO cleanup connectionRegistry after disconnect
if (payload.sessionId !== _lastSessionId) {
_lastSessionId && connectionRegistry.delete(_lastSessionId)
_lastSessionId = payload.sessionId
payload.sessionId && connectionRegistry.set(payload.sessionId, remotes)
}
if (!method) {
console.error("Missing required method", payload)
return
}
try {
if (payload.signature) {
// do not allow signature to be provided if the counterparty does not have a verify implemented
if (!options.verify) throw new Error('edonode: signature was provided but no verify method exists')
// verifier should confirm the nonce is signed
// also need to memoize either here or in verifier
// @TODO implement other verification types. for now only can verify nonce
else await options.verify(remoteDescriptor.nonce, payload.signature)
}
let value = await method.apply(payload, payload.args)
let resolvePayload: ResolvePayload = {
type: TYPE_RESOLVE,
callId: payload.callId,
value
}
stream.write(resolvePayload)
} catch (err) {
let rejectPayload: RejectPayload = {
type: TYPE_REJECT,
callId: payload.callId,
error: serializeError(err)
}
stream.write(rejectPayload)
}
} else if (payload.type === TYPE_RESOLVE) {
let { resolve, methodKey } = callPromises.get(payload.callId) || {}
if (options.debug) console.log(`Edonode - Resolve - ${remoteMethodNames[methodKey]}`, payload)
if (!resolve) {
console.error("Missing required callPromise", payload)
return
}
resolve(payload.value)
callPromises.delete(payload.callId)
} else if (payload.type === TYPE_REJECT) {
let { reject, methodKey } = callPromises.get(payload.callId) || {}
let { error } = payload
if (options.debug) console.log(`Edonode - Reject - ${remoteMethodNames[methodKey]}`, payload)
if (!reject) {
console.error("Missing required callPromise", payload)
return
}
let e = new RemoteError(`remote: ${options.name || ''} - method: ${remoteMethodNames[methodKey]} - message: ${error && error.message}`)
if (process.env.NODE_ENV !== 'production' && error && error.sourceMessage) console.error('Edonode - sourceMessage already exists in the rejected error, this value will be overwritten.')
if (error) Object.keys(payload.error).forEach(k => {
// @NOTE we map error.message to error.sourceMessage
// $FlowFixMe
if (k === 'message') e.sourceMessage = error[k]
// $FlowFixMe
else e[k] = error[k]
})
reject(e)
callPromises.delete(payload.callId)
}
})
})
}
export default edonode
export function getLocalRemote(sessionId: string): any {
return connectionRegistry.get(sessionId)
}