-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
181 lines (168 loc) · 4.61 KB
/
index.js
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
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
const spawnSync = require('child_process').spawnSync;
const JSON = require('./json-buffer');
const host = '127.0.0.1';
function nodeNetCatSrc(port, input) {
return (
"var c=require('net').connect(" +
port +
",'127.0.0.1',()=>{c.pipe(process.stdout);c.end(" +
JSON.stringify(input)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') +
')})'
);
}
const FUNCTION_PRIORITY = [nativeNC, nodeNC];
let started = false;
const configuration = {port: null, fastestFunction: null};
function start() {
if (!spawnSync) {
throw new Error(
'Sync-request requires node version 0.12 or later. If you need to use it with an older version of node\n' +
'you can `npm install sync-request@2.2.0`, which was the last version to support older versions of node.'
);
}
const port = findPort();
const p = spawn(process.execPath, [require.resolve('./worker'), port], {
stdio: 'inherit',
windowsHide: true,
});
p.unref();
process.on('exit', () => {
p.kill();
});
waitForAlive(port);
const fastestFunction = getFastestFunction(port);
configuration.port = port;
configuration.fastestFunction = fastestFunction;
started = true;
}
function findPort() {
const findPortResult = spawnSync(
process.execPath,
[require.resolve('./find-port')],
{
windowsHide: true,
}
);
if (findPortResult.error) {
if (typeof findPortResult.error === 'string') {
throw new Error(findPortResult.error);
}
throw findPortResult.error;
}
if (findPortResult.status !== 0) {
throw new Error(
findPortResult.stderr.toString() ||
'find port exited with code ' + findPortResult.status
);
}
const portString = findPortResult.stdout.toString('utf8').trim();
if (!/^[0-9]+$/.test(portString)) {
throw new Error('Invalid port number string returned: ' + portString);
}
return +portString;
}
function waitForAlive(port) {
let response = null;
let err = null;
let timeout = Date.now() + 10000;
while (response !== 'pong' && Date.now() < timeout) {
const result = nodeNC(port, 'ping\r\n');
response = result.stdout && result.stdout.toString();
err = result.stderr && result.stderr.toString();
}
if (response !== 'pong') {
throw new Error(
'Timed out waiting for sync-rpc server to start (it should respond with "pong" when sent "ping"):\n\n' +
err +
'\n' +
response
);
}
}
function nativeNC(port, input) {
return spawnSync('nc', [host, port], {
input: input,
windowsHide: true,
maxBuffer: Infinity,
});
}
function nodeNC(port, input) {
const src = nodeNetCatSrc(port, input);
if (src.length < 1000) {
return spawnSync(process.execPath, ['-e', src], {
windowsHide: true,
maxBuffer: Infinity,
});
} else {
return spawnSync(process.execPath, [], {
input: src,
windowsHide: true,
maxBuffer: Infinity,
});
}
}
function test(fn, port) {
const result = fn(port, 'ping\r\n');
const response = result.stdout && result.stdout.toString();
return response === 'pong';
}
function getFastestFunction(port) {
for (let i = 0; i < FUNCTION_PRIORITY.length; i++) {
if (test(FUNCTION_PRIORITY[i], port)) {
return FUNCTION_PRIORITY[i];
}
}
}
function sendMessage(input) {
if (!started) start();
const res = configuration.fastestFunction(
configuration.port,
JSON.stringify(input) + '\r\n'
);
try {
return JSON.parse(res.stdout.toString('utf8'));
} catch (ex) {
if (res.error) {
if (typeof res.error === 'string') res.error = new Error(res.error);
throw res.error;
}
if (res.status !== 0) {
throw new Error(
configuration.fastestFunction.name +
' failed:\n' +
(res.stdout && res.stdout.toString()) +
'\n' +
(res.stderr && res.stderr.toString())
);
}
throw new Error(
configuration.fastestFunction.name +
' failed:\n' +
(res.stdout && res.stdout).toString() +
'\n' +
(res.stderr && res.stderr).toString()
);
}
}
function extractValue(msg) {
if (!msg.s) {
const error = new Error(msg.v.message);
error.code = msg.v.code;
throw error;
}
return msg.v;
}
function createClient(filename, args) {
const id = extractValue(sendMessage({t: 1, f: filename, a: args}));
return function(args) {
return extractValue(sendMessage({t: 0, i: id, a: args}));
};
}
createClient.FUNCTION_PRIORITY = FUNCTION_PRIORITY;
createClient.configuration = configuration;
module.exports = createClient;