Skip to content

Commit 9b4dd8b

Browse files
committed
We don't store remote functions anymore. And rpcFilter/createRemoteFnFilter works as encoder
1 parent a1b559b commit 9b4dd8b

File tree

5 files changed

+53
-134
lines changed

5 files changed

+53
-134
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"main": "dist/dop.js",
55
"browser": "dist/dop.umd.js",
66
"module": "src/",

src/api/createNodeFactory.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ export default function createNodeFactory({ encode, decode }) {
77
return function createNode({
88
serialize = JSON.stringify,
99
deserialize = JSON.parse,
10-
createRemoteFunctionFilter = () => true,
10+
rpcFilter = (props) => props.rpc,
1111
errorInstances = [Error],
1212
} = {}) {
1313
const requests = {}
1414
const local_functions_id = {}
1515
const local_functions = new Map()
16-
const remote_functions_id = {}
17-
const remote_functions = new Set()
1816
let local_function_index = 0
19-
let remote_function_index = 0
2017
let request_id_index = 0
2118

2219
const encode_params = {
23-
remote_functions,
2420
local_functions,
2521
registerLocalFunctionFromEncode,
2622
}
@@ -38,25 +34,14 @@ export default function createNodeFactory({ encode, decode }) {
3834
caller,
3935
function_creator,
4036
}) {
41-
if (
42-
!createRemoteFunctionFilter({
43-
node: api,
44-
function_id,
45-
target,
46-
path,
47-
caller,
48-
function_creator,
49-
})
50-
) {
51-
return null
52-
}
5337
const makeCall = (request_id, args) => {
5438
const data = [request_id, function_id]
5539
if (args.length > 0) data.push(args)
5640
api.send(encode(data, encode_params))
5741
return data
5842
}
59-
const fn = (...args) => {
43+
44+
const rpc = (...args) => {
6045
const request_id = ++request_id_index
6146
const req = createRequest()
6247
const { resolve, reject } = req
@@ -73,23 +58,27 @@ export default function createNodeFactory({ encode, decode }) {
7358
requests[request_id] = req
7459
return req
7560
}
76-
fn.stub = (...args) => {
61+
62+
rpc.push = (...args) => {
7763
makeCall(0, args)
7864
}
7965

80-
remote_functions.add(fn)
81-
remote_functions.add(fn.stub)
82-
remote_functions_id[function_id] = fn
83-
return fn
66+
return rpcFilter({
67+
rpc,
68+
node: api,
69+
function_id,
70+
function_creator,
71+
caller,
72+
path,
73+
})
8474
}
8575

8676
function open(send, fn) {
87-
const remote_function_id = remote_function_index++
8877
const local_function_id = local_function_index++
8978
if (isFunction(fn)) registerLocalFunction(local_function_id, fn)
9079
api.send = (msg) => send(serialize(msg))
9180
return createRemoteFunction({
92-
function_id: remote_function_id,
81+
function_id: 0,
9382
function_creator: FUNCTION_CREATOR.ENTRY,
9483
})
9584
}
@@ -106,7 +95,6 @@ export default function createNodeFactory({ encode, decode }) {
10695

10796
msg = decode(msg, {
10897
createRemoteFunction,
109-
remote_functions_id,
11098
caller: fn,
11199
function_creator: is_request
112100
? FUNCTION_CREATOR.REQUEST
@@ -166,7 +154,6 @@ export default function createNodeFactory({ encode, decode }) {
166154
open,
167155
message,
168156
requests,
169-
remote_functions, // Exposing this can be used to know if a function is a remote function
170157
}
171158

172159
return api

src/types/Function.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ export default function Function() {}
77

88
Function.encode = function ({
99
value,
10-
remote_functions,
1110
local_functions,
1211
registerLocalFunctionFromEncode,
1312
}) {
1413
if (isFunction(value)) {
15-
if (remote_functions.has(value)) return null
1614
const function_id = local_functions.has(value)
1715
? local_functions.get(value)
1816
: registerLocalFunctionFromEncode(value)
@@ -25,7 +23,6 @@ Function.encode = function ({
2523

2624
Function.decode = function ({
2725
value,
28-
remote_functions_id,
2926
createRemoteFunction,
3027
path,
3128
caller,
@@ -35,16 +32,12 @@ Function.decode = function ({
3532
getUniqueKey(value) === FUNCTION_KEY &&
3633
isInteger(value[FUNCTION_KEY])
3734
) {
38-
const function_id = value[FUNCTION_KEY]
39-
const fn = remote_functions_id[function_id]
40-
return isFunction(fn)
41-
? fn
42-
: createRemoteFunction({
43-
function_id,
44-
function_creator,
45-
caller,
46-
path: path.slice(2),
47-
})
35+
return createRemoteFunction({
36+
function_id: value[FUNCTION_KEY],
37+
function_creator,
38+
caller,
39+
path: path.slice(2),
40+
})
4841
} else if (
4942
isValidToEscape({ value }) &&
5043
isValidToDecode({ value: value[ESCAPE_KEY], key: FUNCTION_KEY })

test/api.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,21 @@ const exported = [
1212
'createNode',
1313
'createStore',
1414
'addType',
15-
'TYPE'
15+
'TYPE',
1616
]
1717

18-
test('Checking exported params', function(t) {
18+
test('Checking exported params', function (t) {
1919
t.deepEqual(Object.keys(dop), exported)
2020
})
2121

22-
test('Checking factory', function(t) {
22+
test('Checking factory', function (t) {
2323
const dopcopy = dop.factory()
2424
t.deepEqual(Object.keys(dop), Object.keys(dopcopy))
2525
t.is(dop.factory, dopcopy.factory)
2626
t.not(dop.createNode, dopcopy.createNode)
2727
})
2828

29-
test('isRemoteFunction', async t => {
30-
const node = createNode()
31-
const { remote_functions } = node
32-
const isRemoteFunction = f => remote_functions.has(f)
33-
const callClient = node.open()
34-
t.true(isRemoteFunction(callClient))
35-
t.false(isRemoteFunction(() => {}))
36-
t.false(isRemoteFunction('other'))
37-
})
38-
39-
test('Types must be same instance when creating new dop', function(t) {
29+
test('Types must be same instance when creating new dop', function (t) {
4030
const dopcopy = dop.factory()
4131
t.is(dop.TYPE.Inner, dopcopy.TYPE.Inner)
4232
t.is(dop.TYPE.Replace, dopcopy.TYPE.Replace)

test/createNode.js

Lines changed: 28 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('Api', async (t) => {
1818
const callClient = server.open(client.message)
1919
const promise = callClient(2, 5)
2020

21-
t.deepEqual(Object.keys(server).length, 5)
21+
t.deepEqual(Object.keys(server).length, 4)
2222

2323
t.deepEqual(Object.keys(promise).length, 5)
2424
t.true(promise instanceof Promise)
@@ -89,8 +89,20 @@ test('Response (reject or resolve must delete request)', async (t) => {
8989
})
9090

9191
test('Passing same functions should not create a new one', async (t) => {
92+
function rpcFilter() {
93+
const rpcs = {}
94+
return ({ rpc, function_id }) => {
95+
if (rpcs.hasOwnProperty(function_id)) {
96+
return rpcs[function_id]
97+
} else {
98+
rpcs[function_id] = rpc
99+
return rpc
100+
}
101+
}
102+
}
103+
92104
const server = createNode()
93-
const client = createNode()
105+
const client = createNode({ rpcFilter: rpcFilter() })
94106

95107
// Client side
96108
const callServer = client.open(
@@ -108,25 +120,6 @@ test('Passing same functions should not create a new one', async (t) => {
108120
callClient(repeated, repeated, receiveFromClient)
109121
})
110122

111-
test('Sending remote functions that is from the same node must be replaced as null', async (t) => {
112-
const server = createNode()
113-
const client = createNode()
114-
115-
// server side
116-
server.open(client.message, (...args) => {
117-
t.is(args.length, 3)
118-
t.is(args[0], null)
119-
t.is(typeof args[1], 'function')
120-
t.is(typeof args[2], 'object')
121-
return args[1]
122-
})
123-
124-
// client side
125-
const callServer = client.open(server.message)
126-
const resu = await callServer(callServer, () => {})
127-
t.is(resu, null)
128-
})
129-
130123
test('Testing messages', async (t) => {
131124
const serdes = { serialize: (m) => m, deserialize: (m) => m }
132125
const server = createNode(serdes)
@@ -203,7 +196,7 @@ test('Using reject', async (t) => {
203196
t.is(Object.keys(client.requests).length, 0)
204197
})
205198

206-
test('Using stub', async (t) => {
199+
test('Using push', async (t) => {
207200
const server = createNode()
208201
const client = createNode()
209202
server.open(client.message, (...args) => {
@@ -212,29 +205,10 @@ test('Using stub', async (t) => {
212205
})
213206
const callServer = client.open(server.message)
214207
t.is(Object.keys(client.requests).length, 0)
215-
t.is(callServer.stub(), undefined)
208+
t.is(callServer.push(), undefined)
216209
t.is(Object.keys(client.requests).length, 0)
217210
})
218211

219-
test('Sending remote stub functions that is from the same node must be replaced as null', async (t) => {
220-
const server = createNode()
221-
const client = createNode()
222-
223-
// server side
224-
server.open(client.message, (...args) => {
225-
t.is(args.length, 3)
226-
t.is(args[0], null)
227-
t.is(typeof args[1], 'function')
228-
t.is(typeof args[2], 'object')
229-
return args[1]
230-
})
231-
232-
// client side
233-
const callServer = client.open(server.message)
234-
const resu = await callServer(callServer.stub, () => {})
235-
t.is(resu, null)
236-
})
237-
238212
test('Calling functions from client to server with another node in the middle', async (t) => {
239213
// connection 1
240214
const server = createNode()
@@ -267,45 +241,19 @@ test('Calling functions from client to server with another node in the middle',
267241
t.is(result, 10)
268242
})
269243

270-
test('Limiting remote functions', async (t) => {
271-
const server = createNode({
272-
createRemoteFunctionFilter: () => server.remote_functions.size < 6,
273-
})
274-
t.is(server.remote_functions.size, 0)
244+
test('rpc is created if rpcFilter returns the rpc itself', async (t) => {
245+
const n = Math.random()
246+
const server = createNode({ rpcFilter: () => n })
275247
const client = createNode()
276-
server.open(client.message, (fn, isLast) => {
277-
if (isLast) t.is(fn, null)
278-
else t.is(typeof fn, 'function')
248+
const callClient = server.open(client.message, (fn) => {
249+
t.is(fn, n)
279250
})
251+
t.is(callClient, n)
280252
const callServer = client.open(server.message)
281-
t.is(server.remote_functions.size, 2)
282-
await callServer(() => {}, false)
283-
t.is(server.remote_functions.size, 4)
284-
await callServer(() => {}, false)
285-
t.is(server.remote_functions.size, 6)
286-
await callServer(() => {}, true)
287-
t.is(server.remote_functions.size, 6)
288-
})
289-
290-
test('Limiting remote functions to 0', async (t) => {
291-
const server = createNode({ createRemoteFunctionFilter: () => false })
292-
const client = createNode()
293-
const callClient = server.open(client.message, (fn) => fn)
294-
t.is(callClient, null)
295-
const callServer = client.open(server.message)
296-
await callServer(() => {})
297-
t.is(server.remote_functions.size, 0)
298-
t.is(client.remote_functions.size, 2)
299-
await callServer(() => {})
300-
t.is(client.remote_functions.size, 2)
301-
302-
// faking calls
303-
t.is(server.message(JSON.stringify([111, 0])), true)
304-
t.is(server.message(JSON.stringify([222, 1])), false)
305-
t.is(client.message(JSON.stringify([222, 0])), false)
253+
callServer(() => {})
306254
})
307255

308-
test('createRemoteFunctionFilter API', async (t) => {
256+
test('rpcFilter API', async (t) => {
309257
function login({ value, fn }) {
310258
t.is(typeof fn, 'function')
311259
}
@@ -317,7 +265,8 @@ test('createRemoteFunctionFilter API', async (t) => {
317265
}
318266
}
319267

320-
const createRemoteFunctionFilter = ({
268+
const rpcFilter = ({
269+
rpc,
321270
node,
322271
function_id,
323272
function_creator,
@@ -346,9 +295,9 @@ test('createRemoteFunctionFilter API', async (t) => {
346295
t.is(caller, login)
347296
t.deepEqual(path, [0, 'fn'])
348297
}
349-
return true
298+
return rpc
350299
}
351-
const server = createNode({ createRemoteFunctionFilter })
300+
const server = createNode({ rpcFilter })
352301
const client = createNode()
353302
server.ENV = 'SERVER'
354303
client.ENV = 'CLIENT'

0 commit comments

Comments
 (0)