Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// stringify(obj)
let a: any = {};
a.b = a;
Hoek.stringify(a); // Returns '[Cannot display object: Converting circular structure to JSON]'
// Timer
let timerObj = new Hoek.Timer();
console.log("Time is now: " + timerObj.ts);
console.log("Elapsed time from initialization: " + timerObj.elapsed() + 'milliseconds');
// Bench
let benchObj = new Hoek.Bench();
console.log("Elapsed time from initialization: " + benchObj.elapsed() + 'milliseconds');
// base64urlEncode(value)
Hoek.base64urlEncode("hoek");
// base64urlDecode(value)
Hoek.base64urlDecode("aG9law==");
// escapeHtml(string)
let string = ' hey ';
let escapedString = Hoek.escapeHtml(string); // returns <html> hey </html>
// escapeHeaderAttribute(attribute)
server.start(function () {
var timer = new Hoek.Bench();
var options = {
hostname: '127.0.0.1',
port: server.info.port,
path: '/fast',
method: 'POST'
};
var req = Http.request(options, function (res) {
expect(res.statusCode).to.equal(408);
expect(timer.elapsed()).to.be.at.least(45);
done();
});
req.on('error', function (err) { }); // Will error out, so don't allow error to escape test
server.start(function () {
var timer = new Hoek.Bench();
var options = {
hostname: '127.0.0.1',
port: server.info.port,
path: '/fast',
method: 'POST'
};
var req = Http.request(options, function (res) {
expect(res.statusCode).to.equal(408);
expect(timer.elapsed()).to.be.at.least(45);
done();
});
req.on('error', function (err) { }); // Will error out, so don't allow error to escape test
internals.Request = function (connection, req, res, options) {
var self = this;
Events.EventEmitter.call(this);
// Take measurement as soon as possible
this._bench = new Hoek.Bench();
var now = Date.now();
// Public members
this.connection = connection;
this.server = connection.server;
this.url = null;
this.query = null;
this.path = null;
this.method = null;
this.mime = null; // Set if payload is parsed
this.setUrl = this._setUrl; // Decoration removed after 'onRequest'
this.setMethod = this._setMethod;
it('immediately destroys idle keep-alive connections', async () => {
const server = new Hapi.Server();
server.route({ method: 'GET', path: '/', handler: () => null });
await server.start();
const socket = await internals.socket(server);
socket.write('GET / HTTP/1.1\nHost: test\nConnection: Keep-Alive\n\n\n');
await new Promise((resolve) => socket.on('data', resolve));
const count = await internals.countConnections(server);
expect(count).to.equal(1);
const timer = new Hoek.Bench();
await server.stop({ timeout: 20 });
expect(timer.elapsed()).to.be.at.most(20);
});
it('reconnects immediately', async () => {
const team = new Teamwork({ meetings: 2 });
const timer = new Hoek.Bench();
let orig = null;
let count = 0;
const onConnect = () => {
++count;
team.attend();
};
const onDisconnect = (willReconnect) => {
expect(willReconnect).to.equal(count !== 2);
};
let errors = 0;
const onError = (err) => {
it('returns server error message when server taking too long', function (done) {
var timeoutHandler = function (request, reply) { };
var server = new Hapi.Server();
server.connection({ routes: { timeout: { server: 50 } } });
server.route({ method: 'GET', path: '/timeout', config: { handler: timeoutHandler } });
var timer = new Hoek.Bench();
server.inject('/timeout', function (res) {
expect(res.statusCode).to.equal(503);
expect(timer.elapsed()).to.be.at.least(45);
done();
});
});
internals.delay = function (callback) {
var bench = new Hoek.Bench();
setImmediate(function () {
return callback(null, bench.elapsed());
});
};
exports = module.exports = internals.Heavy = function (options) {
options = options || {};
Joi.assert(options, internals.schema, 'Invalid load monitoring options');
this.settings = Hoek.applyToDefaults(internals.defaults, options);
Hoek.assert(this.settings.sampleInterval || (!this.settings.maxEventLoopDelay && !this.settings.maxHeapUsedBytes && !this.settings.maxRssBytes), 'Load sample interval must be set to enable load limits');
this._eventLoopTimer = null;
this._loadBench = new Hoek.Bench();
this.load = {
eventLoopDelay: 0,
heapUsed: 0,
rss: 0
};
};
setInterval(function () {
var memory = process.memoryUsage();
this.log.info('sample#node.rss=' + memory.rss, 'sample#node.heapTotal=' + memory.heapTotal, 'sample#node.heapUsed=' + memory.heapUsed);
var load = os.loadavg();
this.log.info('sample#os.load1=' + load[0], 'sample#os.load5=' + load[1], 'sample#os.load15=' + load[2]);
var bench = new Hoek.Bench();
setImmediate(function () {
self.log.info('sample#node.threadDelay=' + bench.elapsed());
});
}.bind(this), this.opts.statsInterval);
};