sync-rpc 1.3.5 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/__tests__/index.test.js +34 -2
- package/lib/index.js +20 -5
- package/lib/test-worker.js +3 -0
- package/lib/worker.js +16 -9
- package/package.json +1 -1
- package/HISTORY.md +0 -5
|
@@ -7,6 +7,8 @@ test('it should work', () => {
|
|
|
7
7
|
}
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
+
let nativeNCFails = false;
|
|
11
|
+
|
|
10
12
|
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
|
11
13
|
test('profile ' + fn.name, () => {
|
|
12
14
|
try {
|
|
@@ -18,10 +20,36 @@ rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
|
|
18
20
|
const end = Date.now();
|
|
19
21
|
console.log(fn.name + ': ' + (end - start));
|
|
20
22
|
} catch (ex) {
|
|
21
|
-
|
|
23
|
+
if (fn.name === 'nativeNC') {
|
|
24
|
+
console.log(fn.name + ' fails');
|
|
25
|
+
nativeNCFails = true;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
throw ex;
|
|
22
29
|
}
|
|
23
30
|
});
|
|
24
31
|
});
|
|
32
|
+
|
|
33
|
+
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
|
34
|
+
test('test 30MB ' + fn.name, () => {
|
|
35
|
+
let result;
|
|
36
|
+
try {
|
|
37
|
+
rpc.configuration.fastestFunction = fn;
|
|
38
|
+
result = client('big');
|
|
39
|
+
} catch (ex) {
|
|
40
|
+
if (fn.name === 'nativeNC' && nativeNCFails) {
|
|
41
|
+
console.log(fn.name + ' fails');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
throw ex;
|
|
45
|
+
}
|
|
46
|
+
expect(result.length).toBe(30 * 1024 * 1024, 42);
|
|
47
|
+
// for (let i = 0; i < 30 * 1024 * 1024, 42; i++) {
|
|
48
|
+
// expect(result[i]).toBe(42);
|
|
49
|
+
// }
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
25
53
|
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
|
26
54
|
let longMessage = '';
|
|
27
55
|
for (let i = 0; i < 100000; i++) {
|
|
@@ -37,7 +65,11 @@ rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
|
|
37
65
|
const end = Date.now();
|
|
38
66
|
console.log('large ' + fn.name + ': ' + (end - start));
|
|
39
67
|
} catch (ex) {
|
|
40
|
-
|
|
68
|
+
if (fn.name === 'nativeNC' && nativeNCFails) {
|
|
69
|
+
console.log('large ' + fn.name + ' fails');
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
throw ex;
|
|
41
73
|
}
|
|
42
74
|
});
|
|
43
75
|
});
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const spawn = require('child_process').spawn;
|
|
5
5
|
const spawnSync = require('child_process').spawnSync;
|
|
6
|
+
const crypto = require('crypto');
|
|
6
7
|
const JSON = require('./json-buffer');
|
|
7
8
|
|
|
8
9
|
const host = '127.0.0.1';
|
|
@@ -21,6 +22,7 @@ function nodeNetCatSrc(port, input) {
|
|
|
21
22
|
const FUNCTION_PRIORITY = [nativeNC, nodeNC];
|
|
22
23
|
|
|
23
24
|
let started = false;
|
|
25
|
+
let key;
|
|
24
26
|
const configuration = {port: null, fastestFunction: null};
|
|
25
27
|
function start() {
|
|
26
28
|
if (!spawnSync) {
|
|
@@ -29,8 +31,9 @@ function start() {
|
|
|
29
31
|
'you can `npm install sync-request@2.2.0`, which was the last version to support older versions of node.'
|
|
30
32
|
);
|
|
31
33
|
}
|
|
34
|
+
key = crypto.randomBytes(32).toString('hex');
|
|
32
35
|
const port = findPort();
|
|
33
|
-
const p = spawn(process.execPath, [require.resolve('./worker'), port], {
|
|
36
|
+
const p = spawn(process.execPath, [require.resolve('./worker'), port, key], {
|
|
34
37
|
stdio: 'inherit',
|
|
35
38
|
windowsHide: true,
|
|
36
39
|
});
|
|
@@ -92,15 +95,26 @@ function waitForAlive(port) {
|
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
function nativeNC(port, input) {
|
|
95
|
-
return spawnSync('nc', [host, port], {
|
|
98
|
+
return spawnSync('nc', [host, port], {
|
|
99
|
+
input: input,
|
|
100
|
+
windowsHide: true,
|
|
101
|
+
maxBuffer: Infinity,
|
|
102
|
+
});
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
function nodeNC(port, input) {
|
|
99
106
|
const src = nodeNetCatSrc(port, input);
|
|
100
107
|
if (src.length < 1000) {
|
|
101
|
-
return spawnSync(process.execPath, ['-e', src], {
|
|
108
|
+
return spawnSync(process.execPath, ['-e', src], {
|
|
109
|
+
windowsHide: true,
|
|
110
|
+
maxBuffer: Infinity,
|
|
111
|
+
});
|
|
102
112
|
} else {
|
|
103
|
-
return spawnSync(process.execPath, [], {
|
|
113
|
+
return spawnSync(process.execPath, [], {
|
|
114
|
+
input: src,
|
|
115
|
+
windowsHide: true,
|
|
116
|
+
maxBuffer: Infinity,
|
|
117
|
+
});
|
|
104
118
|
}
|
|
105
119
|
}
|
|
106
120
|
|
|
@@ -120,6 +134,7 @@ function getFastestFunction(port) {
|
|
|
120
134
|
|
|
121
135
|
function sendMessage(input) {
|
|
122
136
|
if (!started) start();
|
|
137
|
+
input.k = key;
|
|
123
138
|
const res = configuration.fastestFunction(
|
|
124
139
|
configuration.port,
|
|
125
140
|
JSON.stringify(input) + '\r\n'
|
|
@@ -160,7 +175,7 @@ function extractValue(msg) {
|
|
|
160
175
|
|
|
161
176
|
function createClient(filename, args) {
|
|
162
177
|
const id = extractValue(sendMessage({t: 1, f: filename, a: args}));
|
|
163
|
-
return function(args) {
|
|
178
|
+
return function (args) {
|
|
164
179
|
return extractValue(sendMessage({t: 0, i: id, a: args}));
|
|
165
180
|
};
|
|
166
181
|
}
|
package/lib/test-worker.js
CHANGED
package/lib/worker.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const crypto = require('crypto');
|
|
3
4
|
const net = require('net');
|
|
4
5
|
const JSON = require('./json-buffer');
|
|
5
6
|
|
|
7
|
+
const key = Buffer.from(process.argv[3], 'utf8');
|
|
8
|
+
|
|
6
9
|
const INIT = 1;
|
|
7
10
|
const CALL = 0;
|
|
8
11
|
const modules = [];
|
|
9
12
|
|
|
10
13
|
const NULL_PROMISE = Promise.resolve(null);
|
|
11
|
-
const server = net.createServer({allowHalfOpen: true}, c => {
|
|
14
|
+
const server = net.createServer({allowHalfOpen: true}, (c) => {
|
|
12
15
|
let responded = false;
|
|
13
16
|
function respond(data) {
|
|
14
17
|
if (responded) return;
|
|
@@ -17,10 +20,10 @@ const server = net.createServer({allowHalfOpen: true}, c => {
|
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
let buffer = '';
|
|
20
|
-
c.on('error', function(err) {
|
|
23
|
+
c.on('error', function (err) {
|
|
21
24
|
respond({s: false, v: {code: err.code, message: err.message}});
|
|
22
25
|
});
|
|
23
|
-
c.on('data', function(data) {
|
|
26
|
+
c.on('data', function (data) {
|
|
24
27
|
buffer += data.toString('utf8');
|
|
25
28
|
if (/\r\n/.test(buffer)) {
|
|
26
29
|
onMessage(buffer.trim());
|
|
@@ -31,17 +34,21 @@ const server = net.createServer({allowHalfOpen: true}, c => {
|
|
|
31
34
|
c.end('pong');
|
|
32
35
|
return;
|
|
33
36
|
}
|
|
34
|
-
NULL_PROMISE.then(function() {
|
|
37
|
+
NULL_PROMISE.then(function () {
|
|
35
38
|
const req = JSON.parse(str);
|
|
39
|
+
const k = Buffer.from(req.k, 'utf8');
|
|
40
|
+
if (k.byteLength !== key.byteLength || !crypto.timingSafeEqual(k, key)) {
|
|
41
|
+
throw new Error('Invalid key');
|
|
42
|
+
}
|
|
36
43
|
if (req.t === INIT) {
|
|
37
44
|
return init(req.f, req.a);
|
|
38
45
|
}
|
|
39
46
|
return modules[req.i](req.a);
|
|
40
47
|
}).then(
|
|
41
|
-
function(response) {
|
|
48
|
+
function (response) {
|
|
42
49
|
respond({s: true, v: response});
|
|
43
50
|
},
|
|
44
|
-
function(err) {
|
|
51
|
+
function (err) {
|
|
45
52
|
respond({s: false, v: {code: err.code, message: err.message}});
|
|
46
53
|
}
|
|
47
54
|
);
|
|
@@ -56,13 +63,13 @@ function init(filename, arg) {
|
|
|
56
63
|
if (typeof m !== 'function') {
|
|
57
64
|
throw new Error(filename + ' did not export a function.');
|
|
58
65
|
}
|
|
59
|
-
return NULL_PROMISE.then(function() {
|
|
66
|
+
return NULL_PROMISE.then(function () {
|
|
60
67
|
return m(arg);
|
|
61
|
-
}).then(function(fn) {
|
|
68
|
+
}).then(function (fn) {
|
|
62
69
|
const i = modules.length;
|
|
63
70
|
modules[i] = fn;
|
|
64
71
|
return i;
|
|
65
72
|
});
|
|
66
73
|
}
|
|
67
74
|
|
|
68
|
-
server.listen(+process.argv[2]);
|
|
75
|
+
server.listen(+process.argv[2], '127.0.0.1');
|
package/package.json
CHANGED