cookie-parser.js 784 B

1234567891011121314151617181920212223242526272829303132
  1. import {Service} from '../service.js';
  2. import {parseCookie} from '../utils/index.js';
  3. import {getRequestPathname} from '../utils/index.js';
  4. /**
  5. * Cookie parser.
  6. */
  7. export class CookieParser extends Service {
  8. /**
  9. * Parse
  10. *
  11. * @param {import('http').IncomingMessage} req
  12. * @returns {object}
  13. */
  14. parse(req) {
  15. const cookieString = req.headers['cookie'] || '';
  16. const cookie = parseCookie(cookieString);
  17. const cookieKeys = Object.keys(cookie);
  18. if (cookieKeys.length) {
  19. cookieKeys.forEach(key => {
  20. this.debug('The cookie %v has the value %v.', key, cookie[key]);
  21. });
  22. } else {
  23. this.debug(
  24. 'The request %s %v has no cookie.',
  25. req.method,
  26. getRequestPathname(req),
  27. );
  28. }
  29. return cookie;
  30. }
  31. }