Skip to content

Commit 6a77884

Browse files
committed
reject an rpc as 0 is converted to null
1 parent df1e1d7 commit 6a77884

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"main": "dist/dop.js",
55
"browser": "dist/dop.umd.js",
66
"license": "MIT",

src/api/createNodeFactory.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ export default function createNodeFactory({ encoders, decoders }) {
7272

7373
function message(msg) {
7474
msg = deserialize(msg)
75-
try {
76-
msg = decode(msg)
77-
} catch (e) {
78-
// Invalid array to decode
79-
return false
80-
}
75+
if (!isArray(msg)) return false
76+
msg = decode(msg)
8177

8278
let [id, function_id, args] = msg
8379
const response_id = -id
@@ -105,7 +101,7 @@ export default function createNodeFactory({ encoders, decoders }) {
105101
if (value !== undefined) response.push(value)
106102
api.send(encode(response))
107103
}).catch(error => {
108-
response.push(error) // error
104+
response.push(error === 0 ? null : error)
109105
api.send(encode(response))
110106
})
111107
args.push(req)

test/rpc.js

+71-14
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ const localFunctions = {
109109

110110
throwError: function() {
111111
throw new Error('whatever')
112+
},
113+
114+
throwUndefined: function() {
115+
throw undefined
116+
},
117+
118+
throwNull: function() {
119+
throw null
120+
},
121+
122+
throwZero: function() {
123+
throw 0
112124
}
113125
}
114126

@@ -302,17 +314,62 @@ test('ReferenceError error', async function(t) {
302314
}
303315
})
304316

305-
// test('throw new Error()', async function(t) {
306-
// try {
307-
// remoteFunctions.throwError()
308-
// t.true(false)
309-
// } catch (e) {
310-
// t.true(e instanceof Error)
311-
// }
312-
// try {
313-
// await remoteFunctions.throwError()
314-
// t.true(false)
315-
// } catch (e) {
316-
// t.true(e instanceof Error)
317-
// }
318-
// })
317+
test('throw new Error()', async function(t) {
318+
try {
319+
localFunctions.throwError()
320+
t.true(false)
321+
} catch (e) {
322+
t.true(e instanceof Error)
323+
}
324+
try {
325+
await remoteFunctions.throwError()
326+
t.true(false)
327+
} catch (e) {
328+
t.true(e instanceof Error)
329+
}
330+
})
331+
332+
test('throwUndefined', async function(t) {
333+
try {
334+
localFunctions.throwUndefined()
335+
t.true(false)
336+
} catch (e) {
337+
t.is(e, undefined)
338+
}
339+
try {
340+
await remoteFunctions.throwUndefined()
341+
t.true(false)
342+
} catch (e) {
343+
t.is(e, null)
344+
}
345+
})
346+
347+
test('throwNull', async function(t) {
348+
try {
349+
localFunctions.throwNull()
350+
t.true(false)
351+
} catch (e) {
352+
t.is(e, null)
353+
}
354+
try {
355+
await remoteFunctions.throwNull()
356+
t.true(false)
357+
} catch (e) {
358+
t.is(e, null)
359+
}
360+
})
361+
362+
test('throwZero', async function(t) {
363+
try {
364+
localFunctions.throwZero()
365+
t.true(false)
366+
} catch (e) {
367+
t.is(e, 0)
368+
}
369+
try {
370+
await remoteFunctions.throwZero('lel')
371+
t.true(false)
372+
} catch (e) {
373+
t.is(e, null)
374+
}
375+
})

0 commit comments

Comments
 (0)