build.js 746 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const qs = require('querystring');
  2. /**
  3. * @typedef ParsedURL
  4. * @type {import('.').ParsedURL}
  5. */
  6. /**
  7. * @typedef Request
  8. * @property {string} url
  9. * @property {ParsedURL} _parsedUrl
  10. */
  11. /**
  12. * @param {Request} req
  13. * @returns {ParsedURL|void}
  14. */
  15. function parse(req) {
  16. let raw = req.url;
  17. if (raw == null) return;
  18. let prev = req._parsedUrl;
  19. if (prev && prev.raw === raw) return prev;
  20. let pathname=raw, search='', query;
  21. if (raw.length > 1) {
  22. let idx = raw.indexOf('?', 1);
  23. if (idx !== -1) {
  24. search = raw.substring(idx);
  25. pathname = raw.substring(0, idx);
  26. if (search.length > 1) {
  27. query = qs.parse(search.substring(1));
  28. }
  29. }
  30. }
  31. return req._parsedUrl = { pathname, search, query, raw };
  32. }
  33. exports.parse = parse;