{"_id":"timeout-middleware","_rev":"16-f6ead62ac04338dc9d16b89829222049","name":"timeout-middleware","time":{"modified":"2022-06-27T06:48:31.935Z","created":"2016-01-22T13:41:54.863Z","1.0.0":"2016-01-22T13:41:54.863Z","1.1.0":"2016-01-22T17:05:47.627Z","0.1.0":"2016-01-22T19:55:39.260Z","0.2.0":"2016-01-22T20:20:40.284Z","0.3.0":"2016-01-22T20:25:54.250Z","0.4.0":"2016-01-23T08:20:25.514Z","0.4.1":"2016-02-02T08:34:45.509Z","0.6.1":"2016-04-12T18:55:13.093Z"},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},{"name":"hugojosefson","email":"hugo@josefson.org"}],"dist-tags":{"latest":"0.6.1"},"description":"Express middleware for handling timeouts for slow requests","readme":"#timeout-middleware#\nExpress middleware that intercepts the `res.status`, `res.sendStatus` and `res.send` functions and checks for whether the response has timed out or not.\n\n##Why\nThe res object in Express is a subclass of Node.js's `http.ServerResponse`.\nIt is possible to call `res.setHeader(name, value)` as often as needed until `res.writeHead(statusCode)` is called.\nAfter `writeHead`, the headers are baked in and you can only call `res.write(data)`, and finally `res.end(data)`.\n\nBecause of the way middleware processing works, once a module passes the request to the next middleware, it can no longer stop the flow, so we need to check if the request has timed out before we can continue to act on the request.\n\nThe express module for request timeout `connect-timeout` does this with a middleware that checks for `req.timedout`. \nWhich would be used (as top-level middleware) like this:\n```\nvar express = require('express');\nvar timeout = require('connect-timeout');\n\n// example of using this top-level; note the use of haltOnTimedout\n// after every middleware; it will stop the request flow on a timeout\nvar app = express();\napp.use(timeout('5s'));\napp.use(bodyParser());\napp.use(haltOnTimedout);\napp.use(cookieParser());\napp.use(haltOnTimedout);\n\n// Add your routes here, etc.\n\nfunction haltOnTimedout(req, res, next){\n  if (!req.timedout) next();\n}\n\napp.listen(3000);\n```\nAbove codesnippet is copied from: https://github.com/expressjs/timeout  \n\nSince we want to set a timeout on the response and try to keep ourselves DRY:  \nWe intercept the usage of `res.status`, `res.sendStatus` and `res.send` with a wrapper that checks for the `res.timedout` bool that we put on the `res` object.  \nIf `res.timedout` is `true` we return `res`.  \nElse we simply return the res function that was called with its arguments:  \n```\nfunction wrap(res, fn) {\n  return function(...args) {\n    if (res.timedout) {\n      console.log({res: args, fn: fn.name});\n      return res;\n    } else {\n      return fn.apply(res, args);\n    }\n  };\n}\n\n```\nThis will then be used in the actual middleware function:  \n```\nexport default (timeoutValue) => {\n    return (req, res, next) => {\n      const {send, sendStatus, status} = res;\n      res.setTimeout(timeoutValue);\n      res.on('timeout', () => {\n        res.timedout = true;\n        if (!res.headersSent) {\n          console.log({req, message: 'Request Timeout'});\n          res.statusCode = 503;\n          res.type('txt');\n          send.apply(res, ['Request Timeout']);\n        }\n      });\n      res.on('error'), error => {\n        console.log(error);\n      });\n      res.send = wrap(res, send);\n      res.sendStatus = wrap(res, sendStatus);\n      res.status = wrap(res, status);\n      next();\n    };\n};\n```\n\nFirst we use destructuring to extract the `send`, `sendStatus` and `status` functions from `res`.\nThen we set a timeout on the res object (see https://nodejs.org/api/http.html#http_class_http_serverresponse).\n\nOn timeout we set a `boolean` attribute called `res.timedout` to true.\nThen we check for the `res.headersSent` and if it return `false` we set the `res.statusCode` to 503, `res.type` to `'txt'` and applies the `[Request Timeout]` to `send`.\n\nOn error we simply log the error to the console. This is needed since the request object is an EventEmitter and if you don't have a listener for the error event, an error will be thrown, which could crash your program. \n\nOutside of the `timeout` listener we let the `res.send` use the wrapper function to intercept any timeouts for that function. We do the same with `res.sendStatus` and `res.status`.\n\nFinally we proceed with `next()`.\n\nNow we can use it as a top-level middleware like this:\n```\nimport express from 'express';\nimport timeout from 'timeout-middleware';\nconst app = express();\n\napp.use(timeout(30000)); //timeout in milliseconds\napp.use(bodyParser());\napp.use(cookieParser());\n\napp.listen(3000);\n```\n","versions":{"0.1.0":{"name":"timeout-middleware","version":"0.1.0","description":"Express middleware for handling timeouts for slow requests","main":"lib/index.js","scripts":{"compile":"babel --presets es2015,stage-0 -d lib/ src/","prepublish":"npm run compile","test":"test"},"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"keywords":["node","express","timeout","request"],"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","devDependencies":{"babel-cli":"^6.4.5","babel-preset-es2015":"^6.3.13","babel-preset-stage-0":"^6.3.13"},"gitHead":"67149d990b4aa6e60872fab6d28aa0bda4c74378","_id":"timeout-middleware@0.1.0","_shasum":"7c3a73acf14017b7af8017d0b99a2643ae621c85","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.3.0","_npmUser":{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},"dist":{"shasum":"7c3a73acf14017b7af8017d0b99a2643ae621c85","tarball":"https://registry.npmjs.org/timeout-middleware/-/timeout-middleware-0.1.0.tgz","integrity":"sha512-nhNuLM7S5MzDuV0Bu+2KogXZMeV1zm8cT3Zip+29JcalWR8FX6Lfc/VEH/0DHWwU48MK2jjb4ySNVZLelB/Ylw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCmIFPgFvvwfFHOkN4SoUQ2+k4s0q1Z/wzmuy0V4y5HhwIgel06xR3FqBavSrnEcB36nbXhAgleq298RZ40LM7wKdM="}]},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"}]},"0.2.0":{"name":"timeout-middleware","version":"0.2.0","description":"Express middleware for handling timeouts for slow requests","main":"lib/index.js","scripts":{"compile":"babel --presets es2015,stage-0 -d lib/ src/","prepublish":"npm run compile","test":"test"},"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"keywords":["node","express","timeout","request"],"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","devDependencies":{"babel-cli":"^6.4.5","babel-preset-es2015":"^6.3.13","babel-preset-stage-0":"^6.3.13"},"gitHead":"8e013a3056138527e41d0cb2c5e2efa612bfefe8","_id":"timeout-middleware@0.2.0","_shasum":"d6d9488273da40179d7862e47f9fc77f2770a204","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.3.0","_npmUser":{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},"dist":{"shasum":"d6d9488273da40179d7862e47f9fc77f2770a204","tarball":"https://registry.npmjs.org/timeout-middleware/-/timeout-middleware-0.2.0.tgz","integrity":"sha512-LZjI/OIZHfGAngOi8JcsmtQVz3GUPpLyAqQPHjcr4BF8d185TLDBacJqaAi8SSEpN9Cs+rubfnMttPiPZTsJ3Q==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDatvqX/dM1Jy/69wjQWf8pA0/4M7mfAb/3KQiERNpUdgIhAKqHoilyg+jDDKTj581z1QKzTqDfYbppmwGeUtb6YQGS"}]},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"}]},"0.3.0":{"name":"timeout-middleware","version":"0.3.0","description":"Express middleware for handling timeouts for slow requests","main":"lib/index.js","scripts":{"compile":"babel --presets es2015,stage-0 -d lib/ src/","prepublish":"npm run compile","test":"test"},"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"keywords":["node","express","timeout","request"],"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","devDependencies":{"babel-cli":"^6.4.5","babel-preset-es2015":"^6.3.13","babel-preset-stage-0":"^6.3.13"},"gitHead":"7453234a6c627427dd644f772f5e9e2cb040affc","_id":"timeout-middleware@0.3.0","_shasum":"31f007aed0c761f1425002292b9754c05be46485","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.3.0","_npmUser":{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},"dist":{"shasum":"31f007aed0c761f1425002292b9754c05be46485","tarball":"https://registry.npmjs.org/timeout-middleware/-/timeout-middleware-0.3.0.tgz","integrity":"sha512-2LhnR0UGCicYw6EGoWPFBmB7hNixa0Oq6fPsRJFoJl+bWE5tF7/J7oS3rhsQBku3PhdZ1wpvWPSMDpROA4hxAQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDsoIJhs+EfKoyy3CKvC6bkT+s3Ekw2a66w4Vu+y3nI5AIhAJfCoh+zuJRes/b9Qjjp65kkFNSFrEE5tPAMl8uvLi/N"}]},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"}]},"0.4.0":{"name":"timeout-middleware","version":"0.4.0","description":"Express middleware for handling timeouts for slow requests","main":"lib/index.js","scripts":{"compile":"babel --presets es2015,stage-0 -d lib/ src/","prepublish":"npm run compile","test":"test"},"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"keywords":["node","express","timeout","request"],"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","devDependencies":{"babel-cli":"^6.4.5","babel-preset-es2015":"^6.3.13","babel-preset-stage-0":"^6.3.13"},"gitHead":"3d1402cb850f17eeec0f2fe683a6ca3f3ae56993","_id":"timeout-middleware@0.4.0","_shasum":"e9c4e62d8043d3809fa3fffebffeeeb526ec3e9e","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.3.0","_npmUser":{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},"dist":{"shasum":"e9c4e62d8043d3809fa3fffebffeeeb526ec3e9e","tarball":"https://registry.npmjs.org/timeout-middleware/-/timeout-middleware-0.4.0.tgz","integrity":"sha512-4g8e52XGqijPaWQapnCeBqcCaJaKX/85QXPewrl7lMNbNFXL4038QpPgKgDHb/JQg4tF1e1OhQc0xP9mSjKdQA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDp7Ev7iMlh1Cfg9rWCcn8+TBA+6PBmHcC/U/M+gIgQhAiEAtacD6LBNdBApzlDH2AceOxy3cH9y2QfhDuwoJjnWTIM="}]},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"}]},"0.4.1":{"name":"timeout-middleware","version":"0.4.1","description":"Express middleware for handling timeouts for slow requests","main":"lib/index.js","scripts":{"compile":"babel --presets es2015,stage-0 -d lib/ src/","prepublish":"npm run compile","test":"test"},"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"keywords":["node","express","timeout","request"],"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","devDependencies":{"babel-cli":"^6.4.5","babel-preset-es2015":"^6.3.13","babel-preset-stage-0":"^6.3.13"},"gitHead":"dd41779fd52c10280a0a7a4b0abb9695b345b73b","_id":"timeout-middleware@0.4.1","_shasum":"f7d4a789ec1b7b2a158a9fa18a09ea6a58710415","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.1.0","_npmUser":{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},"dist":{"shasum":"f7d4a789ec1b7b2a158a9fa18a09ea6a58710415","tarball":"https://registry.npmjs.org/timeout-middleware/-/timeout-middleware-0.4.1.tgz","integrity":"sha512-itoVsP9GtNngPXPlWUJY/UyvVJRz+W2JbAVqq6z7Szpv1d3gyPn1xLL3WgIW3KVfDBIEc1xIcEBxJbvzbXcspw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCKXN8ftF329571Se7ZjmlF1el/+Et/LG2W0GQGugCctwIgNZCuyTB62ebC3wwY7DQrP1vxpQdgJRxgP53UqcYoA9c="}]},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},{"name":"hugojosefson","email":"hugo@josefson.org"}],"_npmOperationalInternal":{"host":"packages-6-west.internal.npmjs.com","tmp":"tmp/timeout-middleware-0.4.1.tgz_1454402082900_0.33269048179499805"}},"0.6.1":{"name":"timeout-middleware","version":"0.6.1","description":"Express middleware for handling timeouts for slow requests","main":"lib/index.js","scripts":{"compile":"rm -rf lib; babel -d lib/ src/","prepublish":"npm run compile","test":"test"},"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"keywords":["node","express","timeout","request"],"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","devDependencies":{"babel-cli":"^6.4.5","babel-plugin-add-module-exports":"^0.1.2","babel-preset-es2015":"^6.3.13","babel-preset-stage-0":"^6.3.13"},"babel":{"presets":["es2015","stage-0"],"plugins":["add-module-exports"]},"gitHead":"3479fd6fcaa18b30c8efdab27092d49f5857a7a3","_id":"timeout-middleware@0.6.1","_shasum":"887bc139c7175a314739735a866a31445fd154e4","_from":".","_npmVersion":"3.6.0","_nodeVersion":"5.6.0","_npmUser":{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},"dist":{"shasum":"887bc139c7175a314739735a866a31445fd154e4","tarball":"https://registry.npmjs.org/timeout-middleware/-/timeout-middleware-0.6.1.tgz","integrity":"sha512-Rlvmzd0bxy2uRp6aWD2KWuVfN0zJzRDwoxAVF0f4s+6oqjxXWXE4escLDMIyiyQ+iptAU+ZPV6WOX+wBJFfR7g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDUVf3Gc+P8wBJqSXqdNfk18Q/d8xUSd2v0fuRZolUAgAiBeEhvbhiixmjtsS92I9FAh0YZdChmi5InBwSZfqwf6qg=="}]},"maintainers":[{"name":"fjodorekstrom","email":"fjodor.ekstrom@gmail.com"},{"name":"hugojosefson","email":"hugo@josefson.org"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/timeout-middleware-0.6.1.tgz_1460487310675_0.06991209811531007"}}},"homepage":"https://github.com/fjodorekstrom/timeout-middleware#readme","keywords":["node","express","timeout","request"],"repository":{"type":"git","url":"git+https://github.com/fjodorekstrom/timeout-middleware.git"},"author":{"name":"Fjodor Ekström","email":"fjodor.ekstrom@gmail.com"},"bugs":{"url":"https://github.com/fjodorekstrom/timeout-middleware/issues"},"license":"MIT","readmeFilename":"README.md","users":{"hugojosefson":true,"fjodorekstrom":true}}