RequestShortener.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { contextify } = require("./util/identifier");
  7. /** @typedef {import("./util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
  8. /**
  9. * Shortens absolute or verbose request strings so diagnostics and stats output
  10. * can be rendered relative to a chosen base directory.
  11. */
  12. class RequestShortener {
  13. /**
  14. * Binds a context-aware shortening function to the provided directory and
  15. * optional cache owner.
  16. * @param {string} dir the directory
  17. * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
  18. */
  19. constructor(dir, associatedObjectForCache) {
  20. this.contextify = contextify.bindContextCache(
  21. dir,
  22. associatedObjectForCache
  23. );
  24. }
  25. /**
  26. * Returns a request string rewritten relative to the configured directory
  27. * when one is provided.
  28. * @param {string | undefined | null} request the request to shorten
  29. * @returns {string | undefined | null} the shortened request
  30. */
  31. shorten(request) {
  32. if (!request) {
  33. return request;
  34. }
  35. return this.contextify(request);
  36. }
  37. }
  38. module.exports = RequestShortener;