@posthog/core 1.70.0 → 1.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/capture-v1/package.json +6 -0
  2. package/dist/capture-v1/compression.d.ts +18 -0
  3. package/dist/capture-v1/compression.d.ts.map +1 -0
  4. package/dist/capture-v1/compression.js +70 -0
  5. package/dist/capture-v1/compression.mjs +26 -0
  6. package/dist/capture-v1/errors.d.ts +48 -0
  7. package/dist/capture-v1/errors.d.ts.map +1 -0
  8. package/dist/capture-v1/errors.js +82 -0
  9. package/dist/capture-v1/errors.mjs +38 -0
  10. package/dist/capture-v1/index.d.ts +15 -0
  11. package/dist/capture-v1/index.d.ts.map +1 -0
  12. package/dist/capture-v1/index.js +88 -0
  13. package/dist/capture-v1/index.mjs +7 -0
  14. package/dist/capture-v1/sender.d.ts +150 -0
  15. package/dist/capture-v1/sender.d.ts.map +1 -0
  16. package/dist/capture-v1/sender.js +554 -0
  17. package/dist/capture-v1/sender.mjs +507 -0
  18. package/dist/capture-v1/serialize.d.ts +30 -0
  19. package/dist/capture-v1/serialize.d.ts.map +1 -0
  20. package/dist/capture-v1/serialize.js +106 -0
  21. package/dist/capture-v1/serialize.mjs +59 -0
  22. package/dist/capture-v1/transform.d.ts +19 -0
  23. package/dist/capture-v1/transform.d.ts.map +1 -0
  24. package/dist/capture-v1/transform.js +118 -0
  25. package/dist/capture-v1/transform.mjs +77 -0
  26. package/dist/capture-v1/types.d.ts +52 -0
  27. package/dist/capture-v1/types.d.ts.map +1 -0
  28. package/dist/capture-v1/types.js +44 -0
  29. package/dist/capture-v1/types.mjs +3 -0
  30. package/dist/capture-v1/uuid.d.ts +9 -0
  31. package/dist/capture-v1/uuid.d.ts.map +1 -0
  32. package/dist/capture-v1/uuid.js +75 -0
  33. package/dist/capture-v1/uuid.mjs +34 -0
  34. package/dist/featureFlagUtils.d.ts +7 -5
  35. package/dist/featureFlagUtils.d.ts.map +1 -1
  36. package/dist/featureFlagUtils.js +17 -11
  37. package/dist/featureFlagUtils.mjs +14 -11
  38. package/dist/index.d.ts +1 -1
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +27 -16
  41. package/dist/index.mjs +1 -1
  42. package/dist/posthog-core-stateless.d.ts +40 -3
  43. package/dist/posthog-core-stateless.d.ts.map +1 -1
  44. package/dist/posthog-core-stateless.js +87 -19
  45. package/dist/posthog-core-stateless.mjs +87 -19
  46. package/dist/posthog-core.d.ts +138 -53
  47. package/dist/posthog-core.d.ts.map +1 -1
  48. package/dist/posthog-core.js +341 -115
  49. package/dist/posthog-core.mjs +338 -115
  50. package/dist/types.d.ts +24 -1
  51. package/dist/types.d.ts.map +1 -1
  52. package/dist/utils/index.d.ts +1 -1
  53. package/dist/utils/index.d.ts.map +1 -1
  54. package/dist/utils/index.js +25 -12
  55. package/dist/utils/index.mjs +1 -1
  56. package/dist/utils/json-utils.d.ts +11 -1
  57. package/dist/utils/json-utils.d.ts.map +1 -1
  58. package/dist/utils/json-utils.js +50 -2
  59. package/dist/utils/json-utils.mjs +46 -1
  60. package/package.json +12 -2
  61. package/src/capture-v1/compression.ts +50 -0
  62. package/src/capture-v1/errors.ts +91 -0
  63. package/src/capture-v1/index.ts +34 -0
  64. package/src/capture-v1/sender.ts +779 -0
  65. package/src/capture-v1/serialize.ts +113 -0
  66. package/src/capture-v1/transform.ts +135 -0
  67. package/src/capture-v1/types.ts +59 -0
  68. package/src/capture-v1/uuid.ts +53 -0
  69. package/src/featureFlagUtils.ts +22 -10
  70. package/src/index.ts +1 -0
  71. package/src/posthog-core-stateless.ts +166 -30
  72. package/src/posthog-core.ts +615 -178
  73. package/src/types.ts +21 -1
  74. package/src/utils/index.ts +1 -1
  75. package/src/utils/json-utils.ts +64 -1
  76. package/src/error-tracking/chunk-ids.spec.ts +0 -77
  77. package/src/error-tracking/coercers/error-event-coercer.spec.ts +0 -142
  78. package/src/error-tracking/coercers/promise-rejection-event.spec.ts +0 -77
  79. package/src/error-tracking/coercers/string-coercer.spec.ts +0 -26
  80. package/src/error-tracking/error-properties-builder.aggregate-errors.spec.ts +0 -412
  81. package/src/error-tracking/error-properties-builder.coerce.spec.ts +0 -337
  82. package/src/error-tracking/error-properties-builder.metadata.spec.ts +0 -318
  83. package/src/error-tracking/error-properties-builder.parse.spec.ts +0 -37
  84. package/src/error-tracking/exception-steps.spec.ts +0 -115
  85. package/src/error-tracking/parsers/base.spec.ts +0 -156
  86. package/src/error-tracking/parsers/index.spec.ts +0 -270
  87. package/src/error-tracking/parsers/node.spec.ts +0 -34
  88. package/src/error-tracking/release.spec.ts +0 -34
  89. package/src/logs/index.spec.ts +0 -2421
  90. package/src/logs/logs-utils.spec.ts +0 -266
  91. package/src/metrics/index.spec.ts +0 -1249
  92. package/src/surveys/activation.spec.ts +0 -61
  93. package/src/surveys/events.spec.ts +0 -183
  94. package/src/surveys/keys.spec.ts +0 -29
  95. package/src/surveys/property-matching.spec.ts +0 -539
  96. package/src/surveys/shuffling.spec.ts +0 -51
  97. package/src/surveys/translations.spec.ts +0 -274
  98. package/src/surveys/validation.spec.ts +0 -168
  99. package/src/traces/config.spec.ts +0 -271
  100. package/src/traces/ids.spec.ts +0 -115
  101. package/src/traces/index.spec.ts +0 -3287
  102. package/src/traces/live-spans.spec.ts +0 -69
  103. package/src/traces/otlp.spec.ts +0 -368
  104. package/src/traces/span.spec.ts +0 -1137
  105. package/src/traces/traceparent.spec.ts +0 -184
  106. package/src/utils/bot-detection.spec.ts +0 -107
  107. package/src/utils/bucketed-rate-limiter.spec.ts +0 -327
  108. package/src/utils/json-utils.spec.ts +0 -115
  109. package/src/utils/named-error.spec.ts +0 -36
  110. package/src/utils/number-utils.spec.ts +0 -212
  111. package/src/utils/otlp-any-value.spec.ts +0 -376
  112. package/src/utils/otlp-resource.spec.ts +0 -171
  113. package/src/utils/promise-queue.spec.ts +0 -55
  114. package/src/utils/string-utils.spec.ts +0 -158
  115. package/src/utils/type-utils.spec.ts +0 -38
  116. package/src/utils/webview-app-utils.spec.ts +0 -118
  117. package/src/vendor/uuidv7.spec.ts +0 -26
@@ -2,6 +2,7 @@ import type { OtlpLogsPayload, OtlpMetricsPayload, OtlpTracesPayload } from '@po
2
2
  import type { SendMetricsBatchOutcome } from './metrics/types'
3
3
  import type { SendTracesBatchOutcome } from './traces/types'
4
4
  import { SimpleEventEmitter } from './eventemitter'
5
+ import { EVENT_OPTION_LEGACY_PROPERTIES } from './event-options'
5
6
  import { getFeatureFlagValue, minimizeFlagCalledEventProperties, normalizeFlagsResponse } from './featureFlagUtils'
6
7
  import { gzipCompress, isGzipSupported } from './gzip'
7
8
  import {
@@ -158,6 +159,9 @@ type SuccessfulWriteResponseHandling = {
158
159
  type: 'successful-write'
159
160
  }
160
161
 
162
+ /** Properties that set a per-event option, kept on a minimal `$feature_flag_called` event so it keeps the option. */
163
+ const FLAG_CALLED_OPTION_PROPERTIES: readonly string[] = Object.values(EVENT_OPTION_LEGACY_PROPERTIES)
164
+
161
165
  export const maybeAdd = (key: string, value: JsonType | undefined): Record<string, JsonType> =>
162
166
  value !== undefined ? { [key]: value } : {}
163
167
 
@@ -286,6 +290,15 @@ function isWellFormedQueueItem(item: unknown): item is PostHogQueueItem {
286
290
  return isPostHogEventProperties((item as PostHogQueueItem).message)
287
291
  }
288
292
 
293
+ /**
294
+ * What a `sendBatch()` override can resolve with when the transport keeps some events for a later
295
+ * flush. The flush removes the rest of the batch from the queue, keeps the events at `keepIndices`
296
+ * (positions in the batch passed to `sendBatch()`), and stops draining that route until the next
297
+ * flush. `tooLarge` halves the batch size and resends the kept events instead. `timedOut` keeps the
298
+ * events the same way and halves the batch size for the next flush.
299
+ */
300
+ export type SendBatchOutcome = { keepIndices: readonly number[]; tooLarge?: boolean; timedOut?: boolean }
301
+
289
302
  /**
290
303
  * Outcome of a logs batch send. Keeps HTTP error classification inside core so
291
304
  * PostHogLogs doesn't need to know about specific error types.
@@ -349,6 +362,8 @@ export abstract class PostHogCoreStateless {
349
362
  readonly preloadFeatureFlags: boolean
350
363
  readonly disableSurveys: boolean
351
364
  private maxBatchSize: number
365
+ // The batch size before a timeout lowered it; each delivered batch doubles `maxBatchSize` back towards it.
366
+ private batchSizeBeforeTimeout: number | undefined
352
367
  private maxQueueSize: number
353
368
  private flushInterval: number
354
369
  private flushPromise: Promise<any> | null = null
@@ -491,7 +506,7 @@ export abstract class PostHogCoreStateless {
491
506
  'evaluationEnvironments is deprecated. Use evaluationContexts instead. This property will be removed in a future version.'
492
507
  )
493
508
  }
494
- this.disableCompression = !isGzipSupported() || (options?.disableCompression ?? false)
509
+ this.disableCompression = !this.isCompressionAvailable() || (options?.disableCompression ?? false)
495
510
  }
496
511
 
497
512
  protected logMsgIfDebug(fn: () => void): void {
@@ -619,7 +634,7 @@ export abstract class PostHogCoreStateless {
619
634
  properties.$feature_flag_has_experiment === false &&
620
635
  this.isMinimalFlagCalledEventsEnabled()
621
636
  ) {
622
- properties = minimizeFlagCalledEventProperties(properties)
637
+ properties = minimizeFlagCalledEventProperties(properties, FLAG_CALLED_OPTION_PROPERTIES)
623
638
  }
624
639
  return {
625
640
  distinct_id: payload.distinct_id,
@@ -1415,6 +1430,42 @@ export abstract class PostHogCoreStateless {
1415
1430
  }
1416
1431
  }
1417
1432
 
1433
+ /** The most event bytes one batch may carry, or undefined to batch by count only. */
1434
+ protected batchByteTarget(): number | undefined {
1435
+ return undefined
1436
+ }
1437
+
1438
+ /** The bytes an event adds to a batch, used when `batchByteTarget()` is set. */
1439
+ protected batchEventBytes(_message: PostHogEventProperties): number {
1440
+ return 0
1441
+ }
1442
+
1443
+ /**
1444
+ * When the leading `messages` are a resend that has to go out on its own, how many of them;
1445
+ * otherwise undefined.
1446
+ */
1447
+ protected resendBatchSize(_messages: (PostHogEventProperties | undefined)[]): number | undefined {
1448
+ return undefined
1449
+ }
1450
+
1451
+ /** How many leading messages fit the byte target; the first always does, so an oversize event goes alone. */
1452
+ private countWithinBatchByteTarget(messages: (PostHogEventProperties | undefined)[]): number {
1453
+ const target = this.batchByteTarget()
1454
+ if (target === undefined) {
1455
+ return messages.length
1456
+ }
1457
+ let total = 0
1458
+ for (let index = 0; index < messages.length; index++) {
1459
+ const message = messages[index]
1460
+ const bytes = message === undefined ? 0 : this.batchEventBytes(message)
1461
+ if (index > 0 && total + bytes > target) {
1462
+ return index
1463
+ }
1464
+ total += bytes
1465
+ }
1466
+ return messages.length
1467
+ }
1468
+
1418
1469
  protected normalizeMessage(message: PostHogEventProperties): PostHogEventProperties {
1419
1470
  // Top-level `type` is deprecated and a no-op for batch ingestion.
1420
1471
  // Top-level `library` and `library_version` are deprecated; `$lib` and
@@ -1435,7 +1486,7 @@ export abstract class PostHogCoreStateless {
1435
1486
  sanitizedMessage.properties = properties
1436
1487
  }
1437
1488
 
1438
- sanitizedMessage.uuid = getEventUuid(sanitizedMessage.uuid, uuidv7)
1489
+ sanitizedMessage.uuid = getEventUuid(sanitizedMessage.uuid, () => this.generateEventUuid())
1439
1490
 
1440
1491
  return sanitizedMessage
1441
1492
  }
@@ -1455,11 +1506,16 @@ export abstract class PostHogCoreStateless {
1455
1506
  : normalized
1456
1507
  }
1457
1508
 
1509
+ /** A new event uuid. */
1510
+ protected generateEventUuid(): string {
1511
+ return uuidv7()
1512
+ }
1513
+
1458
1514
  protected prepareMessage(_message: any, options?: PostHogCaptureOptions): PostHogEventProperties {
1459
1515
  const message = {
1460
1516
  ..._message,
1461
1517
  timestamp: options?.timestamp ? options?.timestamp : currentISOTime(),
1462
- uuid: getEventUuid(options?.uuid, uuidv7),
1518
+ uuid: getEventUuid(options?.uuid, () => this.generateEventUuid()),
1463
1519
  }
1464
1520
 
1465
1521
  const addGeoipDisableProperty = options?.disableGeoip ?? this.disableGeoip
@@ -1500,13 +1556,22 @@ export abstract class PostHogCoreStateless {
1500
1556
 
1501
1557
  /**
1502
1558
  * The flush the SDK runs on its own, from the interval timer or the `flushAt`
1503
- * threshold. Separate from `flush()` so a host can hold back work that an
1504
- * endpoint has asked it to wait on, which an explicit flush overrides.
1559
+ * threshold. Hosts override it when an automatic flush covers less than
1560
+ * `flush()` does.
1505
1561
  */
1506
1562
  protected flushAutomatic(): Promise<void> {
1507
1563
  return this.flush()
1508
1564
  }
1509
1565
 
1566
+ /**
1567
+ * How long flushes wait before sending, in milliseconds, or 0 when they can send. Checked right
1568
+ * before a flush sends, so a flush queued behind one that got told to wait also waits. A held
1569
+ * flush resolves without sending; `shutdown()` waits the hold out when it fits in its timeout.
1570
+ */
1571
+ protected flushHoldMs(): number {
1572
+ return 0
1573
+ }
1574
+
1510
1575
  private async waitForPendingPromises(
1511
1576
  maxPromiseId: number,
1512
1577
  ignoredPromises: (Promise<any> | null | undefined)[] = []
@@ -1593,6 +1658,9 @@ export abstract class PostHogCoreStateless {
1593
1658
  if (this.pendingFlushPromise === nextFlushPromise) {
1594
1659
  this.pendingFlushPromise = null
1595
1660
  }
1661
+ if (this.flushHoldMs() > 0) {
1662
+ return
1663
+ }
1596
1664
  return this._flush()
1597
1665
  })
1598
1666
 
@@ -1629,6 +1697,14 @@ export abstract class PostHogCoreStateless {
1629
1697
  return headers
1630
1698
  }
1631
1699
 
1700
+ /**
1701
+ * Whether `compressPayload` can gzip in this runtime. Called from the constructor, so overrides must not read
1702
+ * instance fields. Clients that override `compressPayload` with another compressor override this too.
1703
+ */
1704
+ protected isCompressionAvailable(): boolean {
1705
+ return isGzipSupported()
1706
+ }
1707
+
1632
1708
  /**
1633
1709
  * Compresses an outgoing payload. Runtime-specific clients can override this
1634
1710
  * to avoid using the Web Streams compression implementation.
@@ -1657,7 +1733,7 @@ export abstract class PostHogCoreStateless {
1657
1733
  batchMessages: (PostHogEventProperties | undefined)[],
1658
1734
  retryOptions?: Partial<RetriableOptions>,
1659
1735
  route: string = DEFAULT_QUEUE_ROUTE
1660
- ): Promise<void> {
1736
+ ): Promise<void | SendBatchOutcome> {
1661
1737
  const data: Record<string, any> = {
1662
1738
  api_key: this.projectToken,
1663
1739
  batch: batchMessages.map((message) => {
@@ -1764,35 +1840,44 @@ export abstract class PostHogCoreStateless {
1764
1840
  let sentFromRoute = 0
1765
1841
 
1766
1842
  while (queue.length > 0 && sentFromRoute < originalQueueLength) {
1767
- const batchItems = queue.slice(0, this.maxBatchSize)
1768
- const batchMessages = batchItems.map((item) =>
1843
+ const countItems = queue.slice(0, this.maxBatchSize)
1844
+ const countMessages = countItems.map((item) =>
1769
1845
  item.message === undefined ? item.message : this.normalizeMessage(item.message)
1770
1846
  )
1771
-
1772
- const persistQueueChange = async (): Promise<void> => {
1847
+ const batchSize = Math.min(
1848
+ this.countWithinBatchByteTarget(countMessages),
1849
+ this.resendBatchSize(countMessages) ?? countMessages.length
1850
+ )
1851
+ const batchItems = countItems.slice(0, batchSize)
1852
+ const batchMessages = countMessages.slice(0, batchSize)
1853
+
1854
+ const persistQueueChange = async (keepIndices?: ReadonlySet<number>): Promise<void> => {
1855
+ const removedBatchItems = keepIndices ? batchItems.filter((_, index) => !keepIndices.has(index)) : batchItems
1856
+ if (removedBatchItems.length === 0) {
1857
+ await this.flushStorage()
1858
+ return
1859
+ }
1773
1860
  const refreshedQueue = this.getPersistedProperty<PostHogQueueItem[]>(queueKey) || []
1774
1861
  // The live queue may have overflowed while this batch was in flight. Remove only
1775
- // snapshotted items: UUID survives persistence, while reference identity supports
1776
- // legacy queue items without a UUID.
1777
- const remainingBatchItems = [...batchItems]
1778
- const newQueue = refreshedQueue.filter((item) => {
1779
- const itemUuid = item.message?.uuid
1780
- const batchItemIndex = remainingBatchItems.findIndex(
1781
- (batchItem) =>
1782
- batchItem === item ||
1783
- (typeof itemUuid === 'string' && itemUuid.length > 0 && batchItem.message?.uuid === itemUuid)
1784
- )
1785
-
1786
- if (batchItemIndex === -1) {
1787
- return true
1862
+ // snapshotted items, by reference first so events sharing a UUID keep their own copy,
1863
+ // then by UUID, which survives persistence.
1864
+ const removedQueueIndices = new Set<number>()
1865
+ const findQueueIndex = (matches: (item: PostHogQueueItem) => boolean): number =>
1866
+ refreshedQueue.findIndex((item, index) => !removedQueueIndices.has(index) && matches(item))
1867
+ for (const batchItem of removedBatchItems) {
1868
+ let queueIndex = findQueueIndex((item) => item === batchItem)
1869
+ const uuid = batchItem.message?.uuid
1870
+ if (queueIndex === -1 && typeof uuid === 'string' && uuid.length > 0) {
1871
+ queueIndex = findQueueIndex((item) => item.message?.uuid === uuid)
1788
1872
  }
1789
-
1790
- remainingBatchItems.splice(batchItemIndex, 1)
1791
- return false
1792
- })
1873
+ if (queueIndex !== -1) {
1874
+ removedQueueIndices.add(queueIndex)
1875
+ }
1876
+ }
1877
+ const newQueue = refreshedQueue.filter((_, index) => !removedQueueIndices.has(index))
1793
1878
  this.setPersistedProperty<PostHogQueueItem[]>(queueKey, newQueue)
1794
1879
  queue = newQueue
1795
- this._dequeuedMessagesCount += batchItems.length
1880
+ this._dequeuedMessagesCount += removedBatchItems.length
1796
1881
  // Wait for storage to complete to prevent duplicate events on app crash
1797
1882
  await this.flushStorage()
1798
1883
  }
@@ -1808,12 +1893,14 @@ export abstract class PostHogCoreStateless {
1808
1893
  },
1809
1894
  }
1810
1895
 
1896
+ let outcome: void | SendBatchOutcome
1811
1897
  try {
1812
- await this.sendBatch(batchMessages, retryOptions, route)
1898
+ outcome = await this.sendBatch(batchMessages, retryOptions, route)
1813
1899
  } catch (err) {
1814
1900
  if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
1815
1901
  // if we get a 413 error, we want to reduce the batch size and try again
1816
1902
  this.maxBatchSize = Math.max(1, Math.floor(batchMessages.length / 2))
1903
+ this.batchSizeBeforeTimeout = undefined
1817
1904
  this._logger.warn(
1818
1905
  `Received 413 when sending batch of size ${batchMessages.length}, reducing batch size to ${this.maxBatchSize}`
1819
1906
  )
@@ -1831,10 +1918,45 @@ export abstract class PostHogCoreStateless {
1831
1918
  throw err
1832
1919
  }
1833
1920
 
1921
+ const keepIndices = outcome ? new Set(outcome.keepIndices) : undefined
1922
+
1923
+ if (outcome?.tooLarge && keepIndices && keepIndices.size > 1) {
1924
+ if (keepIndices.size < batchMessages.length) {
1925
+ // Remove events the transport already dropped so the smaller batches don't report them again.
1926
+ await persistQueueChange(keepIndices)
1927
+ }
1928
+ this.maxBatchSize = Math.max(1, Math.floor(keepIndices.size / 2))
1929
+ this.batchSizeBeforeTimeout = undefined
1930
+ this._logger.warn(
1931
+ `Received 413 when sending batch of size ${keepIndices.size}, reducing batch size to ${this.maxBatchSize}`
1932
+ )
1933
+ continue
1934
+ }
1935
+
1936
+ if (keepIndices && keepIndices.size > 0) {
1937
+ if (outcome?.timedOut && keepIndices.size > 1) {
1938
+ this.batchSizeBeforeTimeout ??= this.maxBatchSize
1939
+ this.maxBatchSize = Math.max(1, Math.floor(keepIndices.size / 2))
1940
+ this._logger.warn(
1941
+ `Request timed out when sending batch of size ${keepIndices.size}, reducing batch size to ${this.maxBatchSize}`
1942
+ )
1943
+ }
1944
+ await persistQueueChange(keepIndices)
1945
+ sentMessages.push(...batchMessages.filter((_, index) => !keepIndices.has(index)))
1946
+ // The transport asked to wait before sending the kept events again.
1947
+ return
1948
+ }
1949
+
1834
1950
  await persistQueueChange()
1835
1951
 
1836
1952
  sentMessages.push(...batchMessages)
1837
1953
  sentFromRoute += batchMessages.length
1954
+ if (this.batchSizeBeforeTimeout !== undefined) {
1955
+ this.maxBatchSize = Math.min(this.batchSizeBeforeTimeout, this.maxBatchSize * 2)
1956
+ if (this.maxBatchSize >= this.batchSizeBeforeTimeout) {
1957
+ this.batchSizeBeforeTimeout = undefined
1958
+ }
1959
+ }
1838
1960
  }
1839
1961
  }
1840
1962
 
@@ -2105,6 +2227,7 @@ export abstract class PostHogCoreStateless {
2105
2227
 
2106
2228
  await this._initPromise
2107
2229
  let hasTimedOut = false
2230
+ const deadline = Date.now() + shutdownTimeoutMs
2108
2231
  this.clearFlushTimer()
2109
2232
 
2110
2233
  if (this.disabled) {
@@ -2122,6 +2245,19 @@ export abstract class PostHogCoreStateless {
2122
2245
  break
2123
2246
  }
2124
2247
 
2248
+ const holdMs = this.flushHoldMs()
2249
+ if (holdMs > 0) {
2250
+ if (Date.now() + holdMs >= deadline) {
2251
+ this._logger.warn('Shutdown stopped draining: the endpoint asked to wait past the shutdown timeout.')
2252
+ break
2253
+ }
2254
+ await new Promise<void>((resolve) => safeSetTimeout(resolve, holdMs))
2255
+ if (hasTimedOut) {
2256
+ break
2257
+ }
2258
+ continue
2259
+ }
2260
+
2125
2261
  const dequeuedBeforeFlush = this._dequeuedMessagesCount
2126
2262
 
2127
2263
  // flush again to make sure we send all events, some of which might've been added