Skip to content

Commit c29ab31

Browse files
committed
Implemented tests
1 parent 8db166f commit c29ab31

File tree

10 files changed

+103
-41
lines changed

10 files changed

+103
-41
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules/
2+
coverage/
3+
.nyc_output/
24
diff
5+
npm-debug.log

dist/web-rpc.cjs.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ Emitter.prototype.emit = function emit (event, data) {
3636
}
3737
};
3838

39-
var crypto = window.crypto || window.msCrypto;
40-
var arr = new Uint8Array(1);
41-
4239
function isObject (object) {
4340
return Object(object) === object
4441
}
@@ -61,10 +58,8 @@ function peekTransferables (data) {
6158
return result
6259
}
6360

64-
// https://github.com/Chalarangelo/30-seconds-of-code#uuidgeneratorbrowser
6561
function uuid () {
66-
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function (c) { return (c ^ (crypto.getRandomValues(arr)[0] & (15 >> (c / 4)))).toString(16); }
67-
)
62+
return Math.floor((1 + Math.random()) * 1e10).toString(16)
6863
}
6964

7065
var RpcClient = (function (EventEmitter$$1) {
@@ -135,12 +130,12 @@ var RpcClient = (function (EventEmitter$$1) {
135130

136131
var uid = uuid();
137132
var transferables = peekTransferables(data);
138-
this.workers[this.idx].postMessage({ method: method, uid: uid, data: data }, transferables);
139-
this.idx = ++this.idx % this.workers.length; // round robin
140133
return new Promise(function (resolve, reject) {
141134
this$1.timeouts[uid] = setTimeout(function () { return this$1.reject(("RPC timeout exceeded for '" + method + "' call")); }, timeout);
142135
this$1.calls[uid] = resolve;
143136
this$1.errors[uid] = reject;
137+
this$1.workers[this$1.idx].postMessage({ method: method, uid: uid, data: data }, transferables);
138+
this$1.idx = ++this$1.idx % this$1.workers.length; // round robin
144139
})
145140
};
146141

@@ -165,7 +160,7 @@ RpcServer.prototype.handler = function handler (e) {
165160
var uid = ref.uid;
166161
var data = ref.data;
167162
if (this.methods[method]) {
168-
Promise.all([this.methods[method](data)]).then(
163+
Promise.resolve(data).then(this.methods[method]).then(
169164
function (data) { return this$1.reply(uid, method, data); },
170165
function (error) { return this$1.throw(uid, error); }
171166
);

dist/web-rpc.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ Emitter.prototype.emit = function emit (event, data) {
3434
}
3535
};
3636

37-
var crypto = window.crypto || window.msCrypto;
38-
var arr = new Uint8Array(1);
39-
4037
function isObject (object) {
4138
return Object(object) === object
4239
}
@@ -59,10 +56,8 @@ function peekTransferables (data) {
5956
return result
6057
}
6158

62-
// https://github.com/Chalarangelo/30-seconds-of-code#uuidgeneratorbrowser
6359
function uuid () {
64-
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function (c) { return (c ^ (crypto.getRandomValues(arr)[0] & (15 >> (c / 4)))).toString(16); }
65-
)
60+
return Math.floor((1 + Math.random()) * 1e10).toString(16)
6661
}
6762

6863
var RpcClient = (function (EventEmitter$$1) {
@@ -133,12 +128,12 @@ var RpcClient = (function (EventEmitter$$1) {
133128

134129
var uid = uuid();
135130
var transferables = peekTransferables(data);
136-
this.workers[this.idx].postMessage({ method: method, uid: uid, data: data }, transferables);
137-
this.idx = ++this.idx % this.workers.length; // round robin
138131
return new Promise(function (resolve, reject) {
139132
this$1.timeouts[uid] = setTimeout(function () { return this$1.reject(("RPC timeout exceeded for '" + method + "' call")); }, timeout);
140133
this$1.calls[uid] = resolve;
141134
this$1.errors[uid] = reject;
135+
this$1.workers[this$1.idx].postMessage({ method: method, uid: uid, data: data }, transferables);
136+
this$1.idx = ++this$1.idx % this$1.workers.length; // round robin
142137
})
143138
};
144139

@@ -163,7 +158,7 @@ RpcServer.prototype.handler = function handler (e) {
163158
var uid = ref.uid;
164159
var data = ref.data;
165160
if (this.methods[method]) {
166-
Promise.all([this.methods[method](data)]).then(
161+
Promise.resolve(data).then(this.methods[method]).then(
167162
function (data) { return this$1.reply(uid, method, data); },
168163
function (error) { return this$1.throw(uid, error); }
169164
);

dist/web-rpc.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web-rpc.umd.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ Emitter.prototype.emit = function emit (event, data) {
4040
}
4141
};
4242

43-
var crypto = window.crypto || window.msCrypto;
44-
var arr = new Uint8Array(1);
45-
4643
function isObject (object) {
4744
return Object(object) === object
4845
}
@@ -65,10 +62,8 @@ function peekTransferables (data) {
6562
return result
6663
}
6764

68-
// https://github.com/Chalarangelo/30-seconds-of-code#uuidgeneratorbrowser
6965
function uuid () {
70-
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function (c) { return (c ^ (crypto.getRandomValues(arr)[0] & (15 >> (c / 4)))).toString(16); }
71-
)
66+
return Math.floor((1 + Math.random()) * 1e10).toString(16)
7267
}
7368

7469
var RpcClient = (function (EventEmitter$$1) {
@@ -139,12 +134,12 @@ var RpcClient = (function (EventEmitter$$1) {
139134

140135
var uid = uuid();
141136
var transferables = peekTransferables(data);
142-
this.workers[this.idx].postMessage({ method: method, uid: uid, data: data }, transferables);
143-
this.idx = ++this.idx % this.workers.length; // round robin
144137
return new Promise(function (resolve, reject) {
145138
this$1.timeouts[uid] = setTimeout(function () { return this$1.reject(("RPC timeout exceeded for '" + method + "' call")); }, timeout);
146139
this$1.calls[uid] = resolve;
147140
this$1.errors[uid] = reject;
141+
this$1.workers[this$1.idx].postMessage({ method: method, uid: uid, data: data }, transferables);
142+
this$1.idx = ++this$1.idx % this$1.workers.length; // round robin
148143
})
149144
};
150145

@@ -169,7 +164,7 @@ RpcServer.prototype.handler = function handler (e) {
169164
var uid = ref.uid;
170165
var data = ref.data;
171166
if (this.methods[method]) {
172-
Promise.all([this.methods[method](data)]).then(
167+
Promise.resolve(data).then(this.methods[method]).then(
173168
function (data) { return this$1.reply(uid, method, data); },
174169
function (error) { return this$1.throw(uid, error); }
175170
);

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@
3131
"main": "dist/web-rpc.cjs.js",
3232
"module": "dist/web-rpc.js",
3333
"devDependencies": {
34+
"nyc": "^11.4.1",
3435
"rollup": "0.49.2",
3536
"rollup-plugin-buble": "0.15.0",
36-
"rollup-plugin-commonjs": "^8.2.6",
37-
"rollup-plugin-node-resolve": "^3.0.0",
37+
"rollup-plugin-commonjs": "8.2.6",
38+
"rollup-plugin-node-resolve": "3.0.0",
3839
"snazzy": "7.0.0",
39-
"standard": "^10.0.3",
40+
"standard": "10.0.3",
41+
"tap-diff": "0.1.1",
42+
"tape": "4.8.0",
4043
"uglify-js": "3.0.28"
4144
},
4245
"scripts": {
4346
"check": "standard src/*.js | snazzy",
4447
"build": "rollup -c",
48+
"test": "nyc --reporter=html --reporter=lcov --reporter=text tape test/index.test.js",
4549
"min": "uglifyjs dist/web-rpc.umd.js -c -m > dist/web-rpc.min.js"
4650
},
4751
"dependencies": {

src/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ class RpcClient extends EventEmitter {
5252
call (method, data, { timeout = 2000 } = {}) {
5353
var uid = uuid()
5454
var transferables = peekTransferables(data)
55-
this.workers[this.idx].postMessage({ method, uid, data }, transferables)
56-
this.idx = ++this.idx % this.workers.length // round robin
5755
return new Promise((resolve, reject) => {
5856
this.timeouts[uid] = setTimeout(() => this.reject(`RPC timeout exceeded for '${method}' call`), timeout)
5957
this.calls[uid] = resolve
6058
this.errors[uid] = reject
59+
this.workers[this.idx].postMessage({ method, uid, data }, transferables)
60+
this.idx = ++this.idx % this.workers.length // round robin
6161
})
6262
}
6363
}

src/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RpcServer {
1414
handler (e) {
1515
var { method, uid, data } = e.data
1616
if (this.methods[method]) {
17-
Promise.all([this.methods[method](data)]).then(
17+
Promise.resolve(data).then(this.methods[method]).then(
1818
data => this.reply(uid, method, data),
1919
error => this.throw(uid, error)
2020
)

src/utils.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var crypto = window.crypto || window.msCrypto
2-
var arr = new Uint8Array(1)
3-
41
export function isObject (object) {
52
return Object(object) === object
63
}
@@ -23,9 +20,6 @@ export function peekTransferables (data) {
2320
return result
2421
}
2522

26-
// https://github.com/Chalarangelo/30-seconds-of-code#uuidgeneratorbrowser
2723
export function uuid () {
28-
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
29-
(c ^ (crypto.getRandomValues(arr)[0] & (15 >> (c / 4)))).toString(16)
30-
)
24+
return Math.floor((1 + Math.random()) * 1e10).toString(16)
3125
}

test/index.test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var test = require('tape')
2+
var tapDiff = require('tap-diff')
3+
var EventEmitter = require('events')
4+
5+
test.createStream()
6+
.pipe(tapDiff())
7+
.pipe(process.stdout)
8+
9+
var WebRPC = require('../dist/web-rpc.cjs.js')
10+
11+
class EventTarget extends EventEmitter {
12+
addEventListener (event, listener) {
13+
this.on(event, listener)
14+
}
15+
}
16+
17+
global.self = new EventTarget()
18+
global.self.postMessage = function (data) {
19+
global.worker.emit('message', { data })
20+
}
21+
22+
global.worker = new EventTarget()
23+
global.worker.postMessage = function (data) {
24+
global.self.emit('message', { data })
25+
}
26+
27+
function wait (time) {
28+
return new Promise(resolve => setTimeout(resolve, time))
29+
}
30+
31+
var server = new WebRPC.Server({
32+
add ({ x, y }) { return x + y },
33+
pow ({ x, y }) { return x ** y },
34+
task () { return wait(1000) }
35+
})
36+
37+
var client = new WebRPC.Client({
38+
workers: [worker, worker]
39+
})
40+
41+
test('RpcServer.constructor()', t => {
42+
t.ok(server instanceof WebRPC.Server, 'should create new RPC server')
43+
t.deepEqual(Object.keys(server.methods), ['add', 'pow', 'task'], 'should have passed methods')
44+
t.deepEqual(self.eventNames(), ['message'], 'should listen "message" event')
45+
t.end()
46+
})
47+
48+
test('RpcServer.emit()', t => {
49+
t.plan(1)
50+
function listener (data) {
51+
t.deepEqual(data, { foo: 'bar' }, 'should trigger RPC server event')
52+
}
53+
54+
client.on('event', listener)
55+
server.emit('event', { foo: 'bar' })
56+
client.off('event', listener)
57+
server.emit('event', { foo: 'bar' })
58+
})
59+
60+
test('RpcClient.constructor() should create new RPC client', t => {
61+
t.ok(client instanceof WebRPC.Client, 'should create new RPC client')
62+
t.equal(client.workers.length, 2, 'should have passed workers')
63+
t.deepEqual(worker.eventNames(), ['message'], 'should listen "message" event')
64+
t.end()
65+
})
66+
67+
test('RpcClient.call()', t => {
68+
t.plan(4)
69+
var result = client.call('add', { x: 1, y: 1})
70+
t.ok(result.then, 'should return Promise')
71+
t.equal(client.idx, 1, 'should round robbin')
72+
result.then(res => t.equal(res, 2, 'should call RPC server method'))
73+
client.call('length').catch(err => {
74+
t.ok(err instanceof Error, 'should throw error for unknown methods')
75+
})
76+
})

0 commit comments

Comments
 (0)