first commit
This commit is contained in:
75
grafana/dashboards/requests/node_modules/sync-rpc/lib/__tests__/index.test.js
generated
vendored
Normal file
75
grafana/dashboards/requests/node_modules/sync-rpc/lib/__tests__/index.test.js
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
const rpc = require('../');
|
||||
|
||||
const client = rpc(__dirname + '/../test-worker.js', 'My Server');
|
||||
test('it should work', () => {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(client('My Message')).toBe('sent My Message to My Server');
|
||||
}
|
||||
});
|
||||
|
||||
let nativeNCFails = false;
|
||||
|
||||
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
||||
test('profile ' + fn.name, () => {
|
||||
try {
|
||||
rpc.configuration.fastestFunction = fn;
|
||||
const start = Date.now();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
expect(client('My Message')).toBe('sent My Message to My Server');
|
||||
}
|
||||
const end = Date.now();
|
||||
console.log(fn.name + ': ' + (end - start));
|
||||
} catch (ex) {
|
||||
if (fn.name === 'nativeNC') {
|
||||
console.log(fn.name + ' fails');
|
||||
nativeNCFails = true;
|
||||
return;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
||||
test('test 30MB ' + fn.name, () => {
|
||||
let result;
|
||||
try {
|
||||
rpc.configuration.fastestFunction = fn;
|
||||
result = client('big');
|
||||
} catch (ex) {
|
||||
if (fn.name === 'nativeNC' && nativeNCFails) {
|
||||
console.log(fn.name + ' fails');
|
||||
return;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
expect(result.length).toBe(30 * 1024 * 1024, 42);
|
||||
// for (let i = 0; i < 30 * 1024 * 1024, 42; i++) {
|
||||
// expect(result[i]).toBe(42);
|
||||
// }
|
||||
});
|
||||
});
|
||||
|
||||
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
|
||||
let longMessage = '';
|
||||
for (let i = 0; i < 100000; i++) {
|
||||
longMessage += 'My Long Message Content';
|
||||
}
|
||||
test('profile large ' + fn.name, () => {
|
||||
try {
|
||||
rpc.configuration.fastestFunction = fn;
|
||||
const start = Date.now();
|
||||
for (let i = 0; i < 10; i++) {
|
||||
expect(client(longMessage)).toBe(`sent ${longMessage} to My Server`);
|
||||
}
|
||||
const end = Date.now();
|
||||
console.log('large ' + fn.name + ': ' + (end - start));
|
||||
} catch (ex) {
|
||||
if (fn.name === 'nativeNC' && nativeNCFails) {
|
||||
console.log('large ' + fn.name + ' fails');
|
||||
return;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
});
|
||||
11
grafana/dashboards/requests/node_modules/sync-rpc/lib/find-port.js
generated
vendored
Normal file
11
grafana/dashboards/requests/node_modules/sync-rpc/lib/find-port.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const getPort = require('get-port');
|
||||
|
||||
getPort()
|
||||
.then(port => process.stdout.write('' + port))
|
||||
.catch(err =>
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
}, 0)
|
||||
);
|
||||
181
grafana/dashboards/requests/node_modules/sync-rpc/lib/index.js
generated
vendored
Normal file
181
grafana/dashboards/requests/node_modules/sync-rpc/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,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;
|
||||
22
grafana/dashboards/requests/node_modules/sync-rpc/lib/json-buffer/LICENSE
generated
vendored
Normal file
22
grafana/dashboards/requests/node_modules/sync-rpc/lib/json-buffer/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
Copyright (c) 2013 Dominic Tarr
|
||||
|
||||
Permission is hereby granted, free of charge,
|
||||
to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify,
|
||||
merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom
|
||||
the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||||
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
1
grafana/dashboards/requests/node_modules/sync-rpc/lib/json-buffer/README.md
generated
vendored
Normal file
1
grafana/dashboards/requests/node_modules/sync-rpc/lib/json-buffer/README.md
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Code based on https://github.com/dominictarr/json-buffer but adapted to be simpler to run in a purely node.js environment
|
||||
52
grafana/dashboards/requests/node_modules/sync-rpc/lib/json-buffer/index.js
generated
vendored
Normal file
52
grafana/dashboards/requests/node_modules/sync-rpc/lib/json-buffer/index.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
//TODO: handle reviver/dehydrate function like normal
|
||||
//and handle indentation, like normal.
|
||||
//if anyone needs this... please send pull request.
|
||||
|
||||
exports.stringify = function stringify(o) {
|
||||
if (o && Buffer.isBuffer(o))
|
||||
return JSON.stringify(':base64:' + o.toString('base64'));
|
||||
|
||||
if (o && o.toJSON) o = o.toJSON();
|
||||
|
||||
if (o && 'object' === typeof o) {
|
||||
var s = '';
|
||||
var array = Array.isArray(o);
|
||||
s = array ? '[' : '{';
|
||||
var first = true;
|
||||
|
||||
for (var k in o) {
|
||||
var ignore =
|
||||
'function' == typeof o[k] || (!array && 'undefined' === typeof o[k]);
|
||||
if (Object.hasOwnProperty.call(o, k) && !ignore) {
|
||||
if (!first) s += ',';
|
||||
first = false;
|
||||
if (array) {
|
||||
s += stringify(o[k]);
|
||||
} else if (o[k] !== void 0) {
|
||||
s += stringify(k) + ':' + stringify(o[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s += array ? ']' : '}';
|
||||
|
||||
return s;
|
||||
} else if ('string' === typeof o) {
|
||||
return JSON.stringify(/^:/.test(o) ? ':' + o : o);
|
||||
} else if ('undefined' === typeof o) {
|
||||
return 'null';
|
||||
} else return JSON.stringify(o);
|
||||
};
|
||||
|
||||
exports.parse = function(s) {
|
||||
return JSON.parse(s, function(key, value) {
|
||||
if ('string' === typeof value) {
|
||||
if (/^:base64:/.test(value))
|
||||
return new Buffer(value.substring(8), 'base64');
|
||||
else return /^:/.test(value) ? value.substring(1) : value;
|
||||
}
|
||||
return value;
|
||||
});
|
||||
};
|
||||
9
grafana/dashboards/requests/node_modules/sync-rpc/lib/test-worker.js
generated
vendored
Normal file
9
grafana/dashboards/requests/node_modules/sync-rpc/lib/test-worker.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
function init(connection) {
|
||||
return function(message) {
|
||||
if (message === 'big') {
|
||||
return Promise.resolve(Buffer.alloc(30 * 1024 * 1024, 42));
|
||||
}
|
||||
return Promise.resolve('sent ' + message + ' to ' + connection);
|
||||
};
|
||||
}
|
||||
module.exports = init;
|
||||
68
grafana/dashboards/requests/node_modules/sync-rpc/lib/worker.js
generated
vendored
Normal file
68
grafana/dashboards/requests/node_modules/sync-rpc/lib/worker.js
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
|
||||
const net = require('net');
|
||||
const JSON = require('./json-buffer');
|
||||
|
||||
const INIT = 1;
|
||||
const CALL = 0;
|
||||
const modules = [];
|
||||
|
||||
const NULL_PROMISE = Promise.resolve(null);
|
||||
const server = net.createServer({allowHalfOpen: true}, c => {
|
||||
let responded = false;
|
||||
function respond(data) {
|
||||
if (responded) return;
|
||||
responded = true;
|
||||
c.end(JSON.stringify(data));
|
||||
}
|
||||
|
||||
let buffer = '';
|
||||
c.on('error', function(err) {
|
||||
respond({s: false, v: {code: err.code, message: err.message}});
|
||||
});
|
||||
c.on('data', function(data) {
|
||||
buffer += data.toString('utf8');
|
||||
if (/\r\n/.test(buffer)) {
|
||||
onMessage(buffer.trim());
|
||||
}
|
||||
});
|
||||
function onMessage(str) {
|
||||
if (str === 'ping') {
|
||||
c.end('pong');
|
||||
return;
|
||||
}
|
||||
NULL_PROMISE.then(function() {
|
||||
const req = JSON.parse(str);
|
||||
if (req.t === INIT) {
|
||||
return init(req.f, req.a);
|
||||
}
|
||||
return modules[req.i](req.a);
|
||||
}).then(
|
||||
function(response) {
|
||||
respond({s: true, v: response});
|
||||
},
|
||||
function(err) {
|
||||
respond({s: false, v: {code: err.code, message: err.message}});
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
function init(filename, arg) {
|
||||
let m = require(filename);
|
||||
if (m && typeof m === 'object' && typeof m.default === 'function') {
|
||||
m = m.default;
|
||||
}
|
||||
if (typeof m !== 'function') {
|
||||
throw new Error(filename + ' did not export a function.');
|
||||
}
|
||||
return NULL_PROMISE.then(function() {
|
||||
return m(arg);
|
||||
}).then(function(fn) {
|
||||
const i = modules.length;
|
||||
modules[i] = fn;
|
||||
return i;
|
||||
});
|
||||
}
|
||||
|
||||
server.listen(+process.argv[2]);
|
||||
Reference in New Issue
Block a user