isomorphic.node.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.addInspectMethod = exports.format = void 0;
  4. const util = require("util");
  5. const to_json_1 = require("./to-json");
  6. // The `inspect()` method is actually a Symbol, not a string key.
  7. // https://nodejs.org/api/util.html#util_util_inspect_custom
  8. const inspectMethod = util.inspect.custom || Symbol.for("nodejs.util.inspect.custom");
  9. /**
  10. * Ono supports Node's `util.format()` formatting for error messages.
  11. *
  12. * @see https://nodejs.org/api/util.html#util_util_format_format_args
  13. */
  14. exports.format = util.format;
  15. /**
  16. * Adds an `inspect()` method to support Node's `util.inspect()` function.
  17. *
  18. * @see https://nodejs.org/api/util.html#util_util_inspect_custom
  19. */
  20. function addInspectMethod(newError) {
  21. // @ts-expect-error - TypeScript doesn't support symbol indexers
  22. newError[inspectMethod] = inspect;
  23. }
  24. exports.addInspectMethod = addInspectMethod;
  25. /**
  26. * Returns a representation of the error for Node's `util.inspect()` method.
  27. *
  28. * @see https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
  29. */
  30. function inspect() {
  31. // HACK: We have to cast the objects to `any` so we can use symbol indexers.
  32. // see https://github.com/Microsoft/TypeScript/issues/1863
  33. let pojo = {};
  34. let error = this;
  35. for (let key of to_json_1.getDeepKeys(error)) {
  36. let value = error[key];
  37. pojo[key] = value;
  38. }
  39. // Don't include the `inspect()` method on the output object,
  40. // otherwise it will cause `util.inspect()` to go into an infinite loop
  41. // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
  42. delete pojo[inspectMethod];
  43. return pojo;
  44. }
  45. //# sourceMappingURL=isomorphic.node.js.map