How to use the @hapi/hoek.once function in @hapi/hoek

To help you get started, we’ve selected a few @hapi/hoek examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hapijs / wreck / lib / index.js View on Github external
req.abort();
            }

            req.abort = _abort;                             // Restore original function to release memory
            req.removeListener('response', onResponse);
            req.removeListener('error', onError);
            req.on('error', Hoek.ignore);

            clearTimeout(timeoutId);

            this._emit('response', err, { req, res, start, uri });

            return relay.callback(err, res);
        };

        const finishOnce = Hoek.once(finish);

        req.once('response', onResponse);

        if (options.timeout) {
            timeoutId = setTimeout(() => finishOnce(Boom.gatewayTimeout('Client request timeout')), options.timeout);
            delete options.timeout;
        }

        // Custom abort method to detect early aborts

        const _abort = req.abort;
        let aborted = false;
        req.abort = () => {

            if (!aborted && !req.res && !req.socket) {
                process.nextTick(() => {
github hapijs / wreck / lib / index.js View on Github external
const contentType = res.headers && internals.findHeader('content-type', res.headers) || '';
            const mime = contentType.split(';')[0].trim().toLowerCase();

            if (!internals.jsonRegex.test(mime)) {
                if (options.json === 'strict') {
                    return callback(Boom.notAcceptable('The content-type is not JSON compatible'));
                }

                return callback(null, buffer);
            }

            return internals.tryParseBuffer(buffer, callback);
        };

        const finishOnce = Hoek.once(finish);

        const clientTimeout = options.timeout;
        if (clientTimeout &&
            clientTimeout > 0) {

            clientTimeoutId = setTimeout(() => finishOnce(Boom.clientTimeout()), clientTimeout);
        }

        // Hander errors

        const onResError = (err) => {

            return finishOnce(err.isBoom ? err : Boom.internal('Payload stream error', err));
        };

        const onResAborted = () => {
github hapijs / pez / lib / index.js View on Github external
let finish = (err) => {

            if (piper) {
                piper.removeListener('data', onReqData);
                piper.removeListener('error', finish);
                piper.removeListener('aborted', onReqAborted);
            }

            if (err) {
                return this._abort(err);
            }

            this._emit('close');
        };

        finish = Hoek.once(finish);

        this._lines.once('close', () => {

            if (this._state === internals.state.epilogue) {
                if (this._held) {
                    this._emit('epilogue', this._held);
                    this._held = '';
                }
            }
            else if (this._state === internals.state.boundary) {
                if (!this._held) {
                    this._abort(Boom.badRequest('Missing end boundary'));
                }
                else if (this._held !== '--') {
                    this._abort(Boom.badRequest('Only white space allowed after boundary at end'));
                }