| 1 | 'use strict'; |
| 2 | |
| 3 | var throttle = require('throttleit'); |
| 4 | |
| 5 | function onRequest(context) { |
| 6 | // Reset dynamic stuff |
| 7 | context.startedAt = null; |
| 8 | |
| 9 | context.state = context.request.progressState = null; |
| 10 | |
| 11 | context.delayTimer && clearTimeout(context.delayTimer); |
| 12 | context.delayTimer = null; |
| 13 | } |
| 14 | |
| 15 | function onResponse(context, response) { |
| 16 | // Mark start timestamp |
| 17 | context.startedAt = Date.now(); |
| 18 | |
| 19 | // Create state |
| 20 | // Also expose the state throught the request |
| 21 | // See https://github.com/IndigoUnited/node-request-progress/pull/2/files |
| 22 | context.state = context.request.progressState = { |
| 23 | time: { |
| 24 | elapsed: 0, |
| 25 | remaining: null |
| 26 | }, |
| 27 | speed: null, |
| 28 | percentage: null, |
| 29 | size: { |
| 30 | total: Number(response.headers[context.options.lengthHeader]) || null, |
| 31 | transferred: 0 |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | // Delay the progress report |
| 36 | context.delayTimer = setTimeout(function () { |
| 37 | context.delayTimer = null; |
| 38 | }, context.options.delay); |
| 39 | } |
| 40 | |
| 41 | function onData(context, data) { |
| 42 | context.state.size.transferred += data.length; |
| 43 | |
| 44 | !context.delayTimer && context.reportState(); |
| 45 | } |
| 46 | |
| 47 | function onEnd(context) { |
| 48 | /* istanbul ignore if */ |
| 49 | if (context.delayTimer) { |
| 50 | clearTimeout(context.delayTimer); |
| 51 | context.delayTimer = null; |
| 52 | } |
| 53 | |
| 54 | context.request.progressState = context.request.progressContext = null; |
| 55 | } |
| 56 | |
| 57 | function reportState(context) { |
| 58 | var state; |
| 59 | |
| 60 | // Do nothing if still within the initial delay or if already finished |
| 61 | if (context.delayTimer || !context.request.progressState) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | state = context.state; |
| 66 | state.time.elapsed = (Date.now() - context.startedAt) / 1000; |
| 67 | |
| 68 | // Calculate speed only if 1s has passed |
| 69 | if (state.time.elapsed >= 1) { |
| 70 | state.speed = state.size.transferred / state.time.elapsed; |
| 71 | } |
| 72 | |
| 73 | // Calculate percentage & remaining only if we know the total size |
| 74 | if (state.size.total != null) { |
| 75 | state.percentage = Math.min(state.size.transferred, state.size.total) / state.size.total; |
| 76 | |
| 77 | if (state.speed != null) { |
| 78 | state.time.remaining = state.percentage !== 1 ? (state.size.total / state.speed) - state.time.elapsed : 0; |
| 79 | state.time.remaining = Math.round(state.time.remaining * 1000) / 1000; // Round to 4 decimals |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | context.request.emit('progress', state); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | function requestProgress(request, options) { |
| 88 | var context; |
| 89 | |
| 90 | if (request.progressContext) { |
| 91 | return request; |
| 92 | } |
| 93 | |
| 94 | if (request.response) { |
| 95 | throw new Error('Already got response, it\'s too late to track progress'); |
| 96 | } |
| 97 | |
| 98 | // Parse options |
| 99 | options = options || {}; |
| 100 | options.throttle = options.throttle == null ? 1000 : options.throttle; |
| 101 | options.delay = options.delay || 0; |
| 102 | options.lengthHeader = options.lengthHeader || 'content-length'; |
| 103 | |
| 104 | // Create context |
| 105 | context = {}; |
| 106 | context.request = request; |
| 107 | context.options = options; |
| 108 | context.reportState = throttle(reportState.bind(null, context), options.throttle); |
| 109 | // context.startedAt = null; |
| 110 | // context.state = null; |
| 111 | // context.delayTimer = null; |
| 112 | |
| 113 | // Attach listeners |
| 114 | request |
| 115 | .on('request', onRequest.bind(null, context)) |
| 116 | .on('response', onResponse.bind(null, context)) |
| 117 | .on('data', onData.bind(null, context)) |
| 118 | .on('end', onEnd.bind(null, context)); |
| 119 | |
| 120 | request.progressContext = context; |
| 121 | |
| 122 | return request; |
| 123 | } |
| 124 | |
| 125 | module.exports = requestProgress; |