Name: js-handler/node_modules/restify/lib/plugins/pre/pause.js
| 1: | // Copyright 2012 Mark Cavage, Inc. All rights reserved. |
| 2: | |
| 3: | |
| 4: | |
| 5: | ///--- Helpers |
| 6: | |
| 7: | function pauseStream(stream) { |
| 8: | function _buffer(chunk) { |
| 9: | stream.__buffered.push(chunk); |
| 10: | } |
| 11: | |
| 12: | function _catchEnd(chunk) { |
| 13: | stream.__rstfy_ended = true; |
| 14: | } |
| 15: | |
| 16: | stream.__rstfy_ended = false; |
| 17: | stream.__rstfy_paused = true; |
| 18: | stream.__buffered = []; |
| 19: | stream.on('data', _buffer); |
| 20: | stream.once('end', _catchEnd); |
| 21: | stream.pause(); |
| 22: | |
| 23: | stream._resume = stream.resume; |
| 24: | stream.resume = function _rstfy_resume() { |
| 25: | if (!stream.__rstfy_paused) |
| 26: | return; |
| 27: | |
| 28: | stream.removeListener('data', _buffer); |
| 29: | stream.removeListener('end', _catchEnd); |
| 30: | |
| 31: | stream.__buffered.forEach(stream.emit.bind(stream, 'data')); |
| 32: | stream.__buffered.length = 0; |
| 33: | |
| 34: | stream._resume(); |
| 35: | stream.resume = stream._resume; |
| 36: | |
| 37: | if (stream.__rstfy_ended) |
| 38: | stream.emit('end'); |
| 39: | }; |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | ///--- Exports |
| 45: | |
| 46: | module.exports = function pause() { |
| 47: | |
| 48: | function prePause(req, res, next) { |
| 49: | pauseStream(req); |
| 50: | next(); |
| 51: | } |
| 52: | |
| 53: | return (prePause); |
| 54: | }; |
