Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. * statuses
  3. * Copyright(c) 2014 Jonathan Ong
  4. * Copyright(c) 2016 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. 'use strict'
  8. /**
  9. * Module dependencies.
  10. * @private
  11. */
  12. var codes = require('./codes.json')
  13. /**
  14. * Module exports.
  15. * @public
  16. */
  17. module.exports = status
  18. // status code to message map
  19. status.message = codes
  20. // status message (lower-case) to code map
  21. status.code = createMessageToStatusCodeMap(codes)
  22. // array of status codes
  23. status.codes = createStatusCodeList(codes)
  24. // status codes for redirects
  25. status.redirect = {
  26. 300: true,
  27. 301: true,
  28. 302: true,
  29. 303: true,
  30. 305: true,
  31. 307: true,
  32. 308: true
  33. }
  34. // status codes for empty bodies
  35. status.empty = {
  36. 204: true,
  37. 205: true,
  38. 304: true
  39. }
  40. // status codes for when you should retry the request
  41. status.retry = {
  42. 502: true,
  43. 503: true,
  44. 504: true
  45. }
  46. /**
  47. * Create a map of message to status code.
  48. * @private
  49. */
  50. function createMessageToStatusCodeMap (codes) {
  51. var map = {}
  52. Object.keys(codes).forEach(function forEachCode (code) {
  53. var message = codes[code]
  54. var status = Number(code)
  55. // populate map
  56. map[message.toLowerCase()] = status
  57. })
  58. return map
  59. }
  60. /**
  61. * Create a list of all status codes.
  62. * @private
  63. */
  64. function createStatusCodeList (codes) {
  65. return Object.keys(codes).map(function mapCode (code) {
  66. return Number(code)
  67. })
  68. }
  69. /**
  70. * Get the status code for given message.
  71. * @private
  72. */
  73. function getStatusCode (message) {
  74. var msg = message.toLowerCase()
  75. if (!Object.prototype.hasOwnProperty.call(status.code, msg)) {
  76. throw new Error('invalid status message: "' + message + '"')
  77. }
  78. return status.code[msg]
  79. }
  80. /**
  81. * Get the status message for given code.
  82. * @private
  83. */
  84. function getStatusMessage (code) {
  85. if (!Object.prototype.hasOwnProperty.call(status.message, code)) {
  86. throw new Error('invalid status code: ' + code)
  87. }
  88. return status.message[code]
  89. }
  90. /**
  91. * Get the status code.
  92. *
  93. * Given a number, this will throw if it is not a known status
  94. * code, otherwise the code will be returned. Given a string,
  95. * the string will be parsed for a number and return the code
  96. * if valid, otherwise will lookup the code assuming this is
  97. * the status message.
  98. *
  99. * @param {string|number} code
  100. * @returns {number}
  101. * @public
  102. */
  103. function status (code) {
  104. if (typeof code === 'number') {
  105. return getStatusMessage(code)
  106. }
  107. if (typeof code !== 'string') {
  108. throw new TypeError('code must be a number or string')
  109. }
  110. // '403'
  111. var n = parseInt(code, 10)
  112. if (!isNaN(n)) {
  113. return getStatusMessage(n)
  114. }
  115. return getStatusCode(code)
  116. }