fix_assert_module_in_the_renderer_process.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Shelley Vohr <[email protected]>
  3. Date: Wed, 16 Aug 2023 19:15:29 +0200
  4. Subject: fix: assert module in the renderer process
  5. When creating a Node.js Environment, embedders have the option to disable Node.js'
  6. default overriding of Error.prepareStackTrace. However, the assert module depends on
  7. a WeakMap that is populated with the error stacktraces in the overridden function.
  8. This adds handling to fall back to the default implementation if Error.prepareStackTrace
  9. if the override has been disabled.
  10. This will be upstreamed.
  11. diff --git a/lib/internal/assert/utils.js b/lib/internal/assert/utils.js
  12. index 59b5a16f1309a5e4055bccfdb7a529045ad30402..bfdaf6211466a01b64b7942f7b16c480283278ff 100644
  13. --- a/lib/internal/assert/utils.js
  14. +++ b/lib/internal/assert/utils.js
  15. @@ -25,6 +25,7 @@ const AssertionError = require('internal/assert/assertion_error');
  16. const { openSync, closeSync, readSync } = require('fs');
  17. const { EOL } = require('internal/constants');
  18. const { BuiltinModule } = require('internal/bootstrap/realm');
  19. +const { getEmbedderOptions } = require('internal/options');
  20. const { isError } = require('internal/util');
  21. const errorCache = new SafeMap();
  22. @@ -167,8 +168,16 @@ function getErrMessage(message, fn) {
  23. ErrorCaptureStackTrace(err, fn);
  24. if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = tmpLimit;
  25. - overrideStackTrace.set(err, (_, stack) => stack);
  26. - const call = err.stack[0];
  27. + let call;
  28. + if (getEmbedderOptions().hasPrepareStackTraceCallback) {
  29. + overrideStackTrace.set(err, (_, stack) => stack);
  30. + call = err.stack[0];
  31. + } else {
  32. + const tmpPrepare = Error.prepareStackTrace;
  33. + Error.prepareStackTrace = (_, stack) => stack;
  34. + call = err.stack[0];
  35. + Error.prepareStackTrace = tmpPrepare;
  36. + }
  37. let filename = call.getFileName();
  38. const line = call.getLineNumber() - 1;
  39. diff --git a/src/api/environment.cc b/src/api/environment.cc
  40. index b9098d102b40adad7fafcc331ac62870617019b9..cb9269a31e073caf86164aa39c0640370ade60fd 100644
  41. --- a/src/api/environment.cc
  42. +++ b/src/api/environment.cc
  43. @@ -244,6 +244,9 @@ void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
  44. auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ?
  45. s.prepare_stack_trace_callback : PrepareStackTraceCallback;
  46. isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb);
  47. + } else {
  48. + auto env = Environment::GetCurrent(isolate);
  49. + env->set_prepare_stack_trace_callback(Local<Function>());
  50. }
  51. }
  52. diff --git a/src/node_options.cc b/src/node_options.cc
  53. index 818baf611fcab7838a339f3ea137467653e270d0..4e3c82e9528b04fd1a0cc99d30fb885e4b224bc9 100644
  54. --- a/src/node_options.cc
  55. +++ b/src/node_options.cc
  56. @@ -1405,14 +1405,16 @@ void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) {
  57. }
  58. Isolate* isolate = args.GetIsolate();
  59. - constexpr size_t kOptionsSize = 4;
  60. + constexpr size_t kOptionsSize = 5;
  61. std::array<Local<Name>, kOptionsSize> names = {
  62. + FIXED_ONE_BYTE_STRING(env->isolate(), "hasPrepareStackTraceCallback"),
  63. FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
  64. FIXED_ONE_BYTE_STRING(env->isolate(), "noGlobalSearchPaths"),
  65. FIXED_ONE_BYTE_STRING(env->isolate(), "noBrowserGlobals"),
  66. FIXED_ONE_BYTE_STRING(env->isolate(), "hasEmbedderPreload")};
  67. std::array<Local<Value>, kOptionsSize> values = {
  68. + Boolean::New(isolate, env->prepare_stack_trace_callback().IsEmpty()),
  69. Boolean::New(isolate, env->should_not_register_esm_loader()),
  70. Boolean::New(isolate, env->no_global_search_paths()),
  71. Boolean::New(isolate, env->no_browser_globals()),