{"version":3,"file":"async-stack.mjs","sources":["../../src/utils/async-stack.ts"],"sourcesContent":["/**\n * Lowest `Error.stackTraceLimit` used for the one capture below. Node keeps 10 frames by default,\n * which ends inside the document service middleware chain.\n */\nconst CALLER_STACK_LIMIT = 50;\n\n/**\n * knex starts a transaction through a promise chain that V8's async stack traces cannot follow, so\n * an error raised while the transaction waits for its connection carries knex frames only. Append\n * the frames of the code awaiting us, captured here, so the stack reaches the caller. The error\n * object itself (class, name, message) is unchanged.\n */\nexport const appendCallerStack = (error: unknown): void => {\n  if (\n    !(error instanceof Error) ||\n    typeof error.stack !== 'string' ||\n    !(Error.stackTraceLimit > 0)\n  ) {\n    return;\n  }\n\n  try {\n    const previousLimit = Error.stackTraceLimit;\n    Error.stackTraceLimit = Math.max(previousLimit, CALLER_STACK_LIMIT);\n\n    let callerStack: unknown;\n    try {\n      callerStack = new Error().stack;\n    } finally {\n      Error.stackTraceLimit = previousLimit;\n    }\n\n    if (typeof callerStack !== 'string') {\n      return;\n    }\n\n    // Drop the \"Error\" line and this function's own frame\n    const frames = callerStack.split('\\n').slice(2);\n    if (frames.length > 0) {\n      error.stack = `${error.stack}\\n${frames.join('\\n')}`;\n    }\n  } catch {\n    // Extra frames are a convenience: never let them replace the error being reported\n  }\n};\n"],"names":["CALLER_STACK_LIMIT","appendCallerStack","error","Error","stack","stackTraceLimit","previousLimit","Math","max","callerStack","frames","split","slice","length","join"],"mappings":"AAAA;;;AAGC,IACD,MAAMA,kBAAAA,GAAqB,EAAA;AAE3B;;;;;IAMO,MAAMC,iBAAAA,GAAoB,CAACC,KAAAA,GAAAA;AAChC,IAAA,IACE,EAAEA,KAAAA,YAAiBC,KAAI,CAAA,IACvB,OAAOD,KAAAA,CAAME,KAAK,KAAK,QAAA,IACvB,EAAED,KAAAA,CAAME,eAAe,GAAG,CAAA,CAAA,EAC1B;AACA,QAAA;AACF,IAAA;IAEA,IAAI;QACF,MAAMC,aAAAA,GAAgBH,MAAME,eAAe;AAC3CF,QAAAA,KAAAA,CAAME,eAAe,GAAGE,IAAAA,CAAKC,GAAG,CAACF,aAAAA,EAAeN,kBAAAA,CAAAA;QAEhD,IAAIS,WAAAA;QACJ,IAAI;YACFA,WAAAA,GAAc,IAAIN,QAAQC,KAAK;QACjC,CAAA,QAAU;AACRD,YAAAA,KAAAA,CAAME,eAAe,GAAGC,aAAAA;AAC1B,QAAA;QAEA,IAAI,OAAOG,gBAAgB,QAAA,EAAU;AACnC,YAAA;AACF,QAAA;;AAGA,QAAA,MAAMC,SAASD,WAAAA,CAAYE,KAAK,CAAC,IAAA,CAAA,CAAMC,KAAK,CAAC,CAAA,CAAA;QAC7C,IAAIF,MAAAA,CAAOG,MAAM,GAAG,CAAA,EAAG;YACrBX,KAAAA,CAAME,KAAK,GAAG,CAAA,EAAGF,KAAAA,CAAME,KAAK,CAAC,EAAE,EAAEM,MAAAA,CAAOI,IAAI,CAAC,IAAA,CAAA,CAAA,CAAO;AACtD,QAAA;AACF,IAAA,CAAA,CAAE,OAAM;;AAER,IAAA;AACF;;;;"}