LogBoxSymbolication.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow strict-local
  8. * @format
  9. */
  10. 'use strict';
  11. import symbolicateStackTrace from '../../Core/Devtools/symbolicateStackTrace';
  12. import type {StackFrame} from '../../Core/NativeExceptionsManager';
  13. import type {SymbolicatedStackTrace} from '../../Core/Devtools/symbolicateStackTrace';
  14. export type Stack = Array<StackFrame>;
  15. const cache: Map<Stack, Promise<SymbolicatedStackTrace>> = new Map();
  16. /**
  17. * Sanitize because sometimes, `symbolicateStackTrace` gives us invalid values.
  18. */
  19. const sanitize = ({
  20. stack: maybeStack,
  21. codeFrame,
  22. }: SymbolicatedStackTrace): SymbolicatedStackTrace => {
  23. if (!Array.isArray(maybeStack)) {
  24. throw new Error('Expected stack to be an array.');
  25. }
  26. const stack = [];
  27. for (const maybeFrame of maybeStack) {
  28. let collapse = false;
  29. if ('collapse' in maybeFrame) {
  30. if (typeof maybeFrame.collapse !== 'boolean') {
  31. throw new Error('Expected stack frame `collapse` to be a boolean.');
  32. }
  33. collapse = maybeFrame.collapse;
  34. }
  35. stack.push({
  36. column: maybeFrame.column,
  37. file: maybeFrame.file,
  38. lineNumber: maybeFrame.lineNumber,
  39. methodName: maybeFrame.methodName,
  40. collapse,
  41. });
  42. }
  43. return {stack, codeFrame};
  44. };
  45. export function deleteStack(stack: Stack): void {
  46. cache.delete(stack);
  47. }
  48. export function symbolicate(stack: Stack): Promise<SymbolicatedStackTrace> {
  49. let promise = cache.get(stack);
  50. if (promise == null) {
  51. promise = symbolicateStackTrace(stack).then(sanitize);
  52. cache.set(stack, promise);
  53. }
  54. return promise;
  55. }