Node-Red configuration
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*!
  2. * cookie
  3. * Copyright(c) 2012-2014 Roman Shtylman
  4. * Copyright(c) 2015 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. 'use strict';
  8. /**
  9. * Module exports.
  10. * @public
  11. */
  12. exports.parse = parse;
  13. exports.serialize = serialize;
  14. /**
  15. * Module variables.
  16. * @private
  17. */
  18. var __toString = Object.prototype.toString
  19. /**
  20. * RegExp to match field-content in RFC 7230 sec 3.2
  21. *
  22. * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  23. * field-vchar = VCHAR / obs-text
  24. * obs-text = %x80-FF
  25. */
  26. var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
  27. /**
  28. * Parse a cookie header.
  29. *
  30. * Parse the given cookie header string into an object
  31. * The object has the various cookies as keys(names) => values
  32. *
  33. * @param {string} str
  34. * @param {object} [options]
  35. * @return {object}
  36. * @public
  37. */
  38. function parse(str, options) {
  39. if (typeof str !== 'string') {
  40. throw new TypeError('argument str must be a string');
  41. }
  42. var obj = {}
  43. var opt = options || {};
  44. var dec = opt.decode || decode;
  45. var index = 0
  46. while (index < str.length) {
  47. var eqIdx = str.indexOf('=', index)
  48. // no more cookie pairs
  49. if (eqIdx === -1) {
  50. break
  51. }
  52. var endIdx = str.indexOf(';', index)
  53. if (endIdx === -1) {
  54. endIdx = str.length
  55. } else if (endIdx < eqIdx) {
  56. // backtrack on prior semicolon
  57. index = str.lastIndexOf(';', eqIdx - 1) + 1
  58. continue
  59. }
  60. var key = str.slice(index, eqIdx).trim()
  61. // only assign once
  62. if (undefined === obj[key]) {
  63. var val = str.slice(eqIdx + 1, endIdx).trim()
  64. // quoted values
  65. if (val.charCodeAt(0) === 0x22) {
  66. val = val.slice(1, -1)
  67. }
  68. obj[key] = tryDecode(val, dec);
  69. }
  70. index = endIdx + 1
  71. }
  72. return obj;
  73. }
  74. /**
  75. * Serialize data into a cookie header.
  76. *
  77. * Serialize the a name value pair into a cookie string suitable for
  78. * http headers. An optional options object specified cookie parameters.
  79. *
  80. * serialize('foo', 'bar', { httpOnly: true })
  81. * => "foo=bar; httpOnly"
  82. *
  83. * @param {string} name
  84. * @param {string} val
  85. * @param {object} [options]
  86. * @return {string}
  87. * @public
  88. */
  89. function serialize(name, val, options) {
  90. var opt = options || {};
  91. var enc = opt.encode || encode;
  92. if (typeof enc !== 'function') {
  93. throw new TypeError('option encode is invalid');
  94. }
  95. if (!fieldContentRegExp.test(name)) {
  96. throw new TypeError('argument name is invalid');
  97. }
  98. var value = enc(val);
  99. if (value && !fieldContentRegExp.test(value)) {
  100. throw new TypeError('argument val is invalid');
  101. }
  102. var str = name + '=' + value;
  103. if (null != opt.maxAge) {
  104. var maxAge = opt.maxAge - 0;
  105. if (isNaN(maxAge) || !isFinite(maxAge)) {
  106. throw new TypeError('option maxAge is invalid')
  107. }
  108. str += '; Max-Age=' + Math.floor(maxAge);
  109. }
  110. if (opt.domain) {
  111. if (!fieldContentRegExp.test(opt.domain)) {
  112. throw new TypeError('option domain is invalid');
  113. }
  114. str += '; Domain=' + opt.domain;
  115. }
  116. if (opt.path) {
  117. if (!fieldContentRegExp.test(opt.path)) {
  118. throw new TypeError('option path is invalid');
  119. }
  120. str += '; Path=' + opt.path;
  121. }
  122. if (opt.expires) {
  123. var expires = opt.expires
  124. if (!isDate(expires) || isNaN(expires.valueOf())) {
  125. throw new TypeError('option expires is invalid');
  126. }
  127. str += '; Expires=' + expires.toUTCString()
  128. }
  129. if (opt.httpOnly) {
  130. str += '; HttpOnly';
  131. }
  132. if (opt.secure) {
  133. str += '; Secure';
  134. }
  135. if (opt.partitioned) {
  136. str += '; Partitioned'
  137. }
  138. if (opt.priority) {
  139. var priority = typeof opt.priority === 'string'
  140. ? opt.priority.toLowerCase()
  141. : opt.priority
  142. switch (priority) {
  143. case 'low':
  144. str += '; Priority=Low'
  145. break
  146. case 'medium':
  147. str += '; Priority=Medium'
  148. break
  149. case 'high':
  150. str += '; Priority=High'
  151. break
  152. default:
  153. throw new TypeError('option priority is invalid')
  154. }
  155. }
  156. if (opt.sameSite) {
  157. var sameSite = typeof opt.sameSite === 'string'
  158. ? opt.sameSite.toLowerCase() : opt.sameSite;
  159. switch (sameSite) {
  160. case true:
  161. str += '; SameSite=Strict';
  162. break;
  163. case 'lax':
  164. str += '; SameSite=Lax';
  165. break;
  166. case 'strict':
  167. str += '; SameSite=Strict';
  168. break;
  169. case 'none':
  170. str += '; SameSite=None';
  171. break;
  172. default:
  173. throw new TypeError('option sameSite is invalid');
  174. }
  175. }
  176. return str;
  177. }
  178. /**
  179. * URL-decode string value. Optimized to skip native call when no %.
  180. *
  181. * @param {string} str
  182. * @returns {string}
  183. */
  184. function decode (str) {
  185. return str.indexOf('%') !== -1
  186. ? decodeURIComponent(str)
  187. : str
  188. }
  189. /**
  190. * URL-encode value.
  191. *
  192. * @param {string} val
  193. * @returns {string}
  194. */
  195. function encode (val) {
  196. return encodeURIComponent(val)
  197. }
  198. /**
  199. * Determine if value is a Date.
  200. *
  201. * @param {*} val
  202. * @private
  203. */
  204. function isDate (val) {
  205. return __toString.call(val) === '[object Date]' ||
  206. val instanceof Date
  207. }
  208. /**
  209. * Try decoding a string using a decoding function.
  210. *
  211. * @param {string} str
  212. * @param {function} decode
  213. * @private
  214. */
  215. function tryDecode(str, decode) {
  216. try {
  217. return decode(str);
  218. } catch (e) {
  219. return str;
  220. }
  221. }