wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
 
'use strict';
 
///--- Helpers
 
/**
 * @private
 * @function pauseStream
 * @param    {Stream} stream - the stream to pause
 * @returns  {undefined} no return value
 */
function pauseStream(stream) {
    function _buffer(chunk) {
        stream.__buffered.push(chunk);
    }
 
    function _catchEnd(chunk) {
        stream.__rstfyEnded = true;
    }
 
    stream.__rstfyEnded = false;
    stream.__rstfyPaused = true;
    stream.__buffered = [];
    stream.on('data', _buffer);
    stream.once('end', _catchEnd);
    stream.pause();
 
    stream._resume = stream.resume;
    stream.resume = function _rstfy_resume() {
        if (!stream.__rstfyPaused) {
            return;
        }
 
        stream.removeListener('data', _buffer);
        stream.removeListener('end', _catchEnd);
 
        stream.__buffered.forEach(stream.emit.bind(stream, 'data'));
        stream.__buffered.length = 0;
 
        stream._resume();
        stream.resume = stream._resume;
 
        if (stream.__rstfyEnded) {
            stream.emit('end');
        }
    };
}
 
/**
 * This pre handler fixes issues with node hanging when an `asyncHandler` is
 * used prior to `bodyParser`.
 * https://github.com/restify/node-restify/issues/287
 * https://github.com/restify/node-restify/issues/409
 * https://github.com/restify/node-restify/wiki/1.4-to-2.0-Migration-Tips
 *
 * @public
 * @function pause
 * @returns  {Function} Handler
 */
function pause() {
    function prePause(req, res, next) {
        pauseStream(req);
        next();
    }
 
    return prePause;
}
 
///--- Exports
 
module.exports = pause;