@objectstack/core 12.5.0 → 12.6.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.
- package/dist/index.cjs +89 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +78 -1
- package/dist/index.d.ts +78 -1
- package/dist/index.js +85 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1862,6 +1862,83 @@ declare function calendarPartsInTz(d: Date, tz: string): CalendarParts;
|
|
|
1862
1862
|
*/
|
|
1863
1863
|
declare function calendarPartsInTzOrUtc(d: Date, tz?: string): CalendarParts;
|
|
1864
1864
|
|
|
1865
|
+
/**
|
|
1866
|
+
* `bulkWrite` — the shared batched-write helper used by BOTH the seed loader
|
|
1867
|
+
* (`@objectstack/metadata-protocol`) and the data-import runner
|
|
1868
|
+
* (`@objectstack/rest`), so neither reimplements batching, transient-error
|
|
1869
|
+
* retry, or per-row degradation. See framework#2678.
|
|
1870
|
+
*
|
|
1871
|
+
* ObjectQL's engine already does the efficient thing when handed an ARRAY —
|
|
1872
|
+
* one `driver.bulkCreate` round-trip plus parent-deduplicated summary
|
|
1873
|
+
* recompute (`engine.insert(object, rows[])`) — but seed/import fed it one
|
|
1874
|
+
* record at a time, so neither got the benefit. This module re-chunks rows
|
|
1875
|
+
* into batches and drives them through a caller-supplied batch-write
|
|
1876
|
+
* function, adding:
|
|
1877
|
+
*
|
|
1878
|
+
* - transient-error retry (network blip / timeout) with exponential
|
|
1879
|
+
* backoff, so a dropped connection doesn't silently drop the row (the
|
|
1880
|
+
* 2026-07-06 HotCRM incident: a turso `fetch failed` mid-seed dropped rows
|
|
1881
|
+
* silently because nothing retried);
|
|
1882
|
+
* - per-row degradation when a batch fails for a non-transient (logical /
|
|
1883
|
+
* validation) reason, so one bad row can't fail the other N-1 — needed
|
|
1884
|
+
* because `driver.bulkCreate` is a single multi-row statement/`Promise.all`
|
|
1885
|
+
* on every driver in this repo (sql, memory, mongodb): one bad row fails
|
|
1886
|
+
* the whole call;
|
|
1887
|
+
* - a stable per-row result keyed by the row's original index, so callers
|
|
1888
|
+
* can reassemble output in input order even though rows are processed in
|
|
1889
|
+
* batches (and a batch's flush may be interleaved with other, immediate,
|
|
1890
|
+
* per-row work such as updates).
|
|
1891
|
+
*/
|
|
1892
|
+
interface BulkWriteRowResult<TRecord = any> {
|
|
1893
|
+
/** Index into the original `rows` array passed to {@link bulkWrite}. */
|
|
1894
|
+
index: number;
|
|
1895
|
+
ok: boolean;
|
|
1896
|
+
record?: TRecord;
|
|
1897
|
+
error?: unknown;
|
|
1898
|
+
}
|
|
1899
|
+
interface RetryOptions {
|
|
1900
|
+
/** Max attempts for one write (batch or single-row), including the first. Default 3. */
|
|
1901
|
+
maxRetries?: number;
|
|
1902
|
+
/** Base backoff in ms; doubled each retry, plus jitter. Default 200. */
|
|
1903
|
+
backoffBaseMs?: number;
|
|
1904
|
+
/** Classifies an error as transient (worth retrying) vs logical (the row/batch is just bad). */
|
|
1905
|
+
isTransientError?: (err: unknown) => boolean;
|
|
1906
|
+
/** Injectable sleep, for deterministic tests. */
|
|
1907
|
+
sleep?: (ms: number) => Promise<void>;
|
|
1908
|
+
}
|
|
1909
|
+
interface BulkWriteOptions<TRow, TRecord = any> extends RetryOptions {
|
|
1910
|
+
/** Rows per batch. Default 200 (framework#2678 suggests 100-500). */
|
|
1911
|
+
batchSize?: number;
|
|
1912
|
+
/**
|
|
1913
|
+
* Write one batch. MUST resolve to one record per input row, in the SAME
|
|
1914
|
+
* order as `batch` — {@link bulkWrite} correlates `records[i]` back to
|
|
1915
|
+
* `batch[i]` positionally (this is how every `bulkCreate` implementation in
|
|
1916
|
+
* this repo already behaves: sql's single `INSERT ... VALUES (...), (...)
|
|
1917
|
+
* RETURNING *`, memory's `Promise.all`, mongodb's ordered `insertMany`).
|
|
1918
|
+
*/
|
|
1919
|
+
writeBatch: (batch: TRow[]) => Promise<TRecord[]>;
|
|
1920
|
+
/** Write a single row — used only to degrade a failed batch. */
|
|
1921
|
+
writeOne: (row: TRow) => Promise<TRecord>;
|
|
1922
|
+
}
|
|
1923
|
+
declare function defaultIsTransientError(err: unknown): boolean;
|
|
1924
|
+
/**
|
|
1925
|
+
* Retry a single write (e.g. an `engine.update()` call the seed loader or
|
|
1926
|
+
* import runner makes outside the batched-insert path) with the same
|
|
1927
|
+
* transient-error backoff {@link bulkWrite} applies to batches — so a
|
|
1928
|
+
* network blip doesn't drop an update the way it used to drop an insert.
|
|
1929
|
+
*/
|
|
1930
|
+
declare function withTransientRetry<T>(fn: () => Promise<T>, opts?: RetryOptions): Promise<T>;
|
|
1931
|
+
/**
|
|
1932
|
+
* Write `rows` through `opts.writeBatch` in chunks of `opts.batchSize`,
|
|
1933
|
+
* retrying a whole-batch transient failure with backoff, and degrading to
|
|
1934
|
+
* per-row `opts.writeOne` calls (each itself retried) when a batch fails for
|
|
1935
|
+
* a non-transient reason — so one bad row can't drop the rest of the batch.
|
|
1936
|
+
*
|
|
1937
|
+
* Returns one {@link BulkWriteRowResult} per input row, indexed to match
|
|
1938
|
+
* `rows`' original order.
|
|
1939
|
+
*/
|
|
1940
|
+
declare function bulkWrite<TRow, TRecord = any>(rows: TRow[], opts: BulkWriteOptions<TRow, TRecord>): Promise<BulkWriteRowResult<TRecord>[]>;
|
|
1941
|
+
|
|
1865
1942
|
/**
|
|
1866
1943
|
* In-memory Map-backed cache fallback.
|
|
1867
1944
|
*
|
|
@@ -2298,4 +2375,4 @@ declare class NamespaceResolver {
|
|
|
2298
2375
|
private suggestAlternative;
|
|
2299
2376
|
}
|
|
2300
2377
|
|
|
2301
|
-
export { API_KEY_PREFIX, type ApiKeyPrincipal, ApiRegistry, type ApiRegistryPluginConfig, type AuthGate, CORE_FALLBACK_FACTORIES, type CalendarParts, DependencyResolver, type GeneratedApiKey, HotReloadManager, type KernelState, type KeyInput, LiteKernel, type NamespaceCheckResult, type NamespaceConflict, type NamespaceEntry, NamespaceResolver, ObjectKernel, ObjectKernelBase, type ObjectKernelConfig, ObjectLogger, type ParsedSignature, type PermissionCheckResult$1 as PermissionCheckResult, type PermissionGrant, type Plugin, type PluginArtifactVerifyResult, PluginConfigValidator, type PluginContext, PluginHealthMonitor, type PluginHealthStatus, type PluginLoadResult, PluginLoader, type PluginMetadata, type PermissionCheckResult as PluginPermissionCheckResult, PluginPermissionEnforcer, PluginPermissionManager, type PluginPermissions, PluginSandboxRuntime, PluginSecurityScanner, type PluginSignatureConfig, PluginSignatureVerifier, type PluginStartupResult, type PublisherVerifyResult, index as QA, type ResolveAuthzInput, type ResolveLocalizationInput, type ResolvedAuthzContext, type ResourceUsage, SIGNATURE_ALG, type SandboxContext, type ScanTarget, SecurePluginContext, type SecurityIssue, SemanticVersionManager, type ServiceFactory, ServiceLifecycle, type ServiceRegistration, type SignatureVerificationResult, type VersionCompatibility, buildPermissionsFromGrants, calendarPartsInTz, calendarPartsInTzOrUtc, counterSignPayload, createApiRegistryPlugin, createMemoryCache, createMemoryI18n, createMemoryJob, createMemoryMetadata, createMemoryQueue, createPluginConfigValidator, createPluginPermissionEnforcer, deepMerge, evaluateAuthGate, extractApiKey, generateApiKey, generateEd25519KeyPair, getEnv, getMemoryUsage, hashApiKey, isAuthGateAllowlisted, isExpired, isNode, parseScopes, parseSignature, readAuthoredTranslationLayer, resolveApiKeyPrincipal, resolveAuthzContext, resolveLocale, resolveLocalizationContext, safeExit, signPayload, verifyPayload, verifyPlatformSignature, verifyPluginArtifact, verifyPublisherSignature, wireAuthoredTranslationSync };
|
|
2378
|
+
export { API_KEY_PREFIX, type ApiKeyPrincipal, ApiRegistry, type ApiRegistryPluginConfig, type AuthGate, type BulkWriteOptions, type BulkWriteRowResult, CORE_FALLBACK_FACTORIES, type CalendarParts, DependencyResolver, type GeneratedApiKey, HotReloadManager, type KernelState, type KeyInput, LiteKernel, type NamespaceCheckResult, type NamespaceConflict, type NamespaceEntry, NamespaceResolver, ObjectKernel, ObjectKernelBase, type ObjectKernelConfig, ObjectLogger, type ParsedSignature, type PermissionCheckResult$1 as PermissionCheckResult, type PermissionGrant, type Plugin, type PluginArtifactVerifyResult, PluginConfigValidator, type PluginContext, PluginHealthMonitor, type PluginHealthStatus, type PluginLoadResult, PluginLoader, type PluginMetadata, type PermissionCheckResult as PluginPermissionCheckResult, PluginPermissionEnforcer, PluginPermissionManager, type PluginPermissions, PluginSandboxRuntime, PluginSecurityScanner, type PluginSignatureConfig, PluginSignatureVerifier, type PluginStartupResult, type PublisherVerifyResult, index as QA, type ResolveAuthzInput, type ResolveLocalizationInput, type ResolvedAuthzContext, type ResourceUsage, type RetryOptions, SIGNATURE_ALG, type SandboxContext, type ScanTarget, SecurePluginContext, type SecurityIssue, SemanticVersionManager, type ServiceFactory, ServiceLifecycle, type ServiceRegistration, type SignatureVerificationResult, type VersionCompatibility, buildPermissionsFromGrants, bulkWrite, calendarPartsInTz, calendarPartsInTzOrUtc, counterSignPayload, createApiRegistryPlugin, createMemoryCache, createMemoryI18n, createMemoryJob, createMemoryMetadata, createMemoryQueue, createPluginConfigValidator, createPluginPermissionEnforcer, deepMerge, defaultIsTransientError, evaluateAuthGate, extractApiKey, generateApiKey, generateEd25519KeyPair, getEnv, getMemoryUsage, hashApiKey, isAuthGateAllowlisted, isExpired, isNode, parseScopes, parseSignature, readAuthoredTranslationLayer, resolveApiKeyPrincipal, resolveAuthzContext, resolveLocale, resolveLocalizationContext, safeExit, signPayload, verifyPayload, verifyPlatformSignature, verifyPluginArtifact, verifyPublisherSignature, wireAuthoredTranslationSync, withTransientRetry };
|
package/dist/index.d.ts
CHANGED
|
@@ -1862,6 +1862,83 @@ declare function calendarPartsInTz(d: Date, tz: string): CalendarParts;
|
|
|
1862
1862
|
*/
|
|
1863
1863
|
declare function calendarPartsInTzOrUtc(d: Date, tz?: string): CalendarParts;
|
|
1864
1864
|
|
|
1865
|
+
/**
|
|
1866
|
+
* `bulkWrite` — the shared batched-write helper used by BOTH the seed loader
|
|
1867
|
+
* (`@objectstack/metadata-protocol`) and the data-import runner
|
|
1868
|
+
* (`@objectstack/rest`), so neither reimplements batching, transient-error
|
|
1869
|
+
* retry, or per-row degradation. See framework#2678.
|
|
1870
|
+
*
|
|
1871
|
+
* ObjectQL's engine already does the efficient thing when handed an ARRAY —
|
|
1872
|
+
* one `driver.bulkCreate` round-trip plus parent-deduplicated summary
|
|
1873
|
+
* recompute (`engine.insert(object, rows[])`) — but seed/import fed it one
|
|
1874
|
+
* record at a time, so neither got the benefit. This module re-chunks rows
|
|
1875
|
+
* into batches and drives them through a caller-supplied batch-write
|
|
1876
|
+
* function, adding:
|
|
1877
|
+
*
|
|
1878
|
+
* - transient-error retry (network blip / timeout) with exponential
|
|
1879
|
+
* backoff, so a dropped connection doesn't silently drop the row (the
|
|
1880
|
+
* 2026-07-06 HotCRM incident: a turso `fetch failed` mid-seed dropped rows
|
|
1881
|
+
* silently because nothing retried);
|
|
1882
|
+
* - per-row degradation when a batch fails for a non-transient (logical /
|
|
1883
|
+
* validation) reason, so one bad row can't fail the other N-1 — needed
|
|
1884
|
+
* because `driver.bulkCreate` is a single multi-row statement/`Promise.all`
|
|
1885
|
+
* on every driver in this repo (sql, memory, mongodb): one bad row fails
|
|
1886
|
+
* the whole call;
|
|
1887
|
+
* - a stable per-row result keyed by the row's original index, so callers
|
|
1888
|
+
* can reassemble output in input order even though rows are processed in
|
|
1889
|
+
* batches (and a batch's flush may be interleaved with other, immediate,
|
|
1890
|
+
* per-row work such as updates).
|
|
1891
|
+
*/
|
|
1892
|
+
interface BulkWriteRowResult<TRecord = any> {
|
|
1893
|
+
/** Index into the original `rows` array passed to {@link bulkWrite}. */
|
|
1894
|
+
index: number;
|
|
1895
|
+
ok: boolean;
|
|
1896
|
+
record?: TRecord;
|
|
1897
|
+
error?: unknown;
|
|
1898
|
+
}
|
|
1899
|
+
interface RetryOptions {
|
|
1900
|
+
/** Max attempts for one write (batch or single-row), including the first. Default 3. */
|
|
1901
|
+
maxRetries?: number;
|
|
1902
|
+
/** Base backoff in ms; doubled each retry, plus jitter. Default 200. */
|
|
1903
|
+
backoffBaseMs?: number;
|
|
1904
|
+
/** Classifies an error as transient (worth retrying) vs logical (the row/batch is just bad). */
|
|
1905
|
+
isTransientError?: (err: unknown) => boolean;
|
|
1906
|
+
/** Injectable sleep, for deterministic tests. */
|
|
1907
|
+
sleep?: (ms: number) => Promise<void>;
|
|
1908
|
+
}
|
|
1909
|
+
interface BulkWriteOptions<TRow, TRecord = any> extends RetryOptions {
|
|
1910
|
+
/** Rows per batch. Default 200 (framework#2678 suggests 100-500). */
|
|
1911
|
+
batchSize?: number;
|
|
1912
|
+
/**
|
|
1913
|
+
* Write one batch. MUST resolve to one record per input row, in the SAME
|
|
1914
|
+
* order as `batch` — {@link bulkWrite} correlates `records[i]` back to
|
|
1915
|
+
* `batch[i]` positionally (this is how every `bulkCreate` implementation in
|
|
1916
|
+
* this repo already behaves: sql's single `INSERT ... VALUES (...), (...)
|
|
1917
|
+
* RETURNING *`, memory's `Promise.all`, mongodb's ordered `insertMany`).
|
|
1918
|
+
*/
|
|
1919
|
+
writeBatch: (batch: TRow[]) => Promise<TRecord[]>;
|
|
1920
|
+
/** Write a single row — used only to degrade a failed batch. */
|
|
1921
|
+
writeOne: (row: TRow) => Promise<TRecord>;
|
|
1922
|
+
}
|
|
1923
|
+
declare function defaultIsTransientError(err: unknown): boolean;
|
|
1924
|
+
/**
|
|
1925
|
+
* Retry a single write (e.g. an `engine.update()` call the seed loader or
|
|
1926
|
+
* import runner makes outside the batched-insert path) with the same
|
|
1927
|
+
* transient-error backoff {@link bulkWrite} applies to batches — so a
|
|
1928
|
+
* network blip doesn't drop an update the way it used to drop an insert.
|
|
1929
|
+
*/
|
|
1930
|
+
declare function withTransientRetry<T>(fn: () => Promise<T>, opts?: RetryOptions): Promise<T>;
|
|
1931
|
+
/**
|
|
1932
|
+
* Write `rows` through `opts.writeBatch` in chunks of `opts.batchSize`,
|
|
1933
|
+
* retrying a whole-batch transient failure with backoff, and degrading to
|
|
1934
|
+
* per-row `opts.writeOne` calls (each itself retried) when a batch fails for
|
|
1935
|
+
* a non-transient reason — so one bad row can't drop the rest of the batch.
|
|
1936
|
+
*
|
|
1937
|
+
* Returns one {@link BulkWriteRowResult} per input row, indexed to match
|
|
1938
|
+
* `rows`' original order.
|
|
1939
|
+
*/
|
|
1940
|
+
declare function bulkWrite<TRow, TRecord = any>(rows: TRow[], opts: BulkWriteOptions<TRow, TRecord>): Promise<BulkWriteRowResult<TRecord>[]>;
|
|
1941
|
+
|
|
1865
1942
|
/**
|
|
1866
1943
|
* In-memory Map-backed cache fallback.
|
|
1867
1944
|
*
|
|
@@ -2298,4 +2375,4 @@ declare class NamespaceResolver {
|
|
|
2298
2375
|
private suggestAlternative;
|
|
2299
2376
|
}
|
|
2300
2377
|
|
|
2301
|
-
export { API_KEY_PREFIX, type ApiKeyPrincipal, ApiRegistry, type ApiRegistryPluginConfig, type AuthGate, CORE_FALLBACK_FACTORIES, type CalendarParts, DependencyResolver, type GeneratedApiKey, HotReloadManager, type KernelState, type KeyInput, LiteKernel, type NamespaceCheckResult, type NamespaceConflict, type NamespaceEntry, NamespaceResolver, ObjectKernel, ObjectKernelBase, type ObjectKernelConfig, ObjectLogger, type ParsedSignature, type PermissionCheckResult$1 as PermissionCheckResult, type PermissionGrant, type Plugin, type PluginArtifactVerifyResult, PluginConfigValidator, type PluginContext, PluginHealthMonitor, type PluginHealthStatus, type PluginLoadResult, PluginLoader, type PluginMetadata, type PermissionCheckResult as PluginPermissionCheckResult, PluginPermissionEnforcer, PluginPermissionManager, type PluginPermissions, PluginSandboxRuntime, PluginSecurityScanner, type PluginSignatureConfig, PluginSignatureVerifier, type PluginStartupResult, type PublisherVerifyResult, index as QA, type ResolveAuthzInput, type ResolveLocalizationInput, type ResolvedAuthzContext, type ResourceUsage, SIGNATURE_ALG, type SandboxContext, type ScanTarget, SecurePluginContext, type SecurityIssue, SemanticVersionManager, type ServiceFactory, ServiceLifecycle, type ServiceRegistration, type SignatureVerificationResult, type VersionCompatibility, buildPermissionsFromGrants, calendarPartsInTz, calendarPartsInTzOrUtc, counterSignPayload, createApiRegistryPlugin, createMemoryCache, createMemoryI18n, createMemoryJob, createMemoryMetadata, createMemoryQueue, createPluginConfigValidator, createPluginPermissionEnforcer, deepMerge, evaluateAuthGate, extractApiKey, generateApiKey, generateEd25519KeyPair, getEnv, getMemoryUsage, hashApiKey, isAuthGateAllowlisted, isExpired, isNode, parseScopes, parseSignature, readAuthoredTranslationLayer, resolveApiKeyPrincipal, resolveAuthzContext, resolveLocale, resolveLocalizationContext, safeExit, signPayload, verifyPayload, verifyPlatformSignature, verifyPluginArtifact, verifyPublisherSignature, wireAuthoredTranslationSync };
|
|
2378
|
+
export { API_KEY_PREFIX, type ApiKeyPrincipal, ApiRegistry, type ApiRegistryPluginConfig, type AuthGate, type BulkWriteOptions, type BulkWriteRowResult, CORE_FALLBACK_FACTORIES, type CalendarParts, DependencyResolver, type GeneratedApiKey, HotReloadManager, type KernelState, type KeyInput, LiteKernel, type NamespaceCheckResult, type NamespaceConflict, type NamespaceEntry, NamespaceResolver, ObjectKernel, ObjectKernelBase, type ObjectKernelConfig, ObjectLogger, type ParsedSignature, type PermissionCheckResult$1 as PermissionCheckResult, type PermissionGrant, type Plugin, type PluginArtifactVerifyResult, PluginConfigValidator, type PluginContext, PluginHealthMonitor, type PluginHealthStatus, type PluginLoadResult, PluginLoader, type PluginMetadata, type PermissionCheckResult as PluginPermissionCheckResult, PluginPermissionEnforcer, PluginPermissionManager, type PluginPermissions, PluginSandboxRuntime, PluginSecurityScanner, type PluginSignatureConfig, PluginSignatureVerifier, type PluginStartupResult, type PublisherVerifyResult, index as QA, type ResolveAuthzInput, type ResolveLocalizationInput, type ResolvedAuthzContext, type ResourceUsage, type RetryOptions, SIGNATURE_ALG, type SandboxContext, type ScanTarget, SecurePluginContext, type SecurityIssue, SemanticVersionManager, type ServiceFactory, ServiceLifecycle, type ServiceRegistration, type SignatureVerificationResult, type VersionCompatibility, buildPermissionsFromGrants, bulkWrite, calendarPartsInTz, calendarPartsInTzOrUtc, counterSignPayload, createApiRegistryPlugin, createMemoryCache, createMemoryI18n, createMemoryJob, createMemoryMetadata, createMemoryQueue, createPluginConfigValidator, createPluginPermissionEnforcer, deepMerge, defaultIsTransientError, evaluateAuthGate, extractApiKey, generateApiKey, generateEd25519KeyPair, getEnv, getMemoryUsage, hashApiKey, isAuthGateAllowlisted, isExpired, isNode, parseScopes, parseSignature, readAuthoredTranslationLayer, resolveApiKeyPrincipal, resolveAuthzContext, resolveLocale, resolveLocalizationContext, safeExit, signPayload, verifyPayload, verifyPlatformSignature, verifyPluginArtifact, verifyPublisherSignature, wireAuthoredTranslationSync, withTransientRetry };
|
package/dist/index.js
CHANGED
|
@@ -4389,6 +4389,87 @@ function calendarPartsInTzOrUtc(d, tz) {
|
|
|
4389
4389
|
};
|
|
4390
4390
|
}
|
|
4391
4391
|
|
|
4392
|
+
// src/utils/bulk-write.ts
|
|
4393
|
+
var DEFAULT_BATCH_SIZE = 200;
|
|
4394
|
+
var DEFAULT_MAX_RETRIES = 3;
|
|
4395
|
+
var DEFAULT_BACKOFF_BASE_MS = 200;
|
|
4396
|
+
var TRANSIENT_PATTERNS = [
|
|
4397
|
+
/fetch failed/i,
|
|
4398
|
+
/network/i,
|
|
4399
|
+
/timed?\s*out/i,
|
|
4400
|
+
/timeout/i,
|
|
4401
|
+
/socket hang ?up/i,
|
|
4402
|
+
/connection.*(closed|reset|refused|terminated|aborted)/i,
|
|
4403
|
+
/\b(502|503|504)\b/,
|
|
4404
|
+
/server.*unavailable/i,
|
|
4405
|
+
/too many connections/i
|
|
4406
|
+
];
|
|
4407
|
+
var TRANSIENT_CODES = /^(ECONNRESET|ECONNREFUSED|ECONNABORTED|EPIPE|EAI_AGAIN|ETIMEDOUT|EHOSTUNREACH|ENETUNREACH|ENOTFOUND)$/i;
|
|
4408
|
+
function defaultIsTransientError(err) {
|
|
4409
|
+
const code = err?.code;
|
|
4410
|
+
if (typeof code === "string" && TRANSIENT_CODES.test(code)) return true;
|
|
4411
|
+
const message = err?.message;
|
|
4412
|
+
const text = typeof message === "string" ? message : String(err ?? "");
|
|
4413
|
+
return TRANSIENT_PATTERNS.some((re) => re.test(text));
|
|
4414
|
+
}
|
|
4415
|
+
var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
4416
|
+
async function withRetry(fn, opts) {
|
|
4417
|
+
let lastError;
|
|
4418
|
+
for (let attempt = 1; attempt <= opts.maxRetries; attempt++) {
|
|
4419
|
+
try {
|
|
4420
|
+
return await fn();
|
|
4421
|
+
} catch (err) {
|
|
4422
|
+
lastError = err;
|
|
4423
|
+
if (attempt >= opts.maxRetries || !opts.isTransientError(err)) throw err;
|
|
4424
|
+
const jitter = Math.floor(Math.random() * 50);
|
|
4425
|
+
await opts.sleep(opts.backoffBaseMs * 2 ** (attempt - 1) + jitter);
|
|
4426
|
+
}
|
|
4427
|
+
}
|
|
4428
|
+
throw lastError;
|
|
4429
|
+
}
|
|
4430
|
+
async function withTransientRetry(fn, opts = {}) {
|
|
4431
|
+
return withRetry(fn, {
|
|
4432
|
+
maxRetries: Math.max(1, opts.maxRetries ?? DEFAULT_MAX_RETRIES),
|
|
4433
|
+
backoffBaseMs: opts.backoffBaseMs ?? DEFAULT_BACKOFF_BASE_MS,
|
|
4434
|
+
isTransientError: opts.isTransientError ?? defaultIsTransientError,
|
|
4435
|
+
sleep: opts.sleep ?? defaultSleep
|
|
4436
|
+
});
|
|
4437
|
+
}
|
|
4438
|
+
async function bulkWrite(rows, opts) {
|
|
4439
|
+
const batchSize = Math.max(1, opts.batchSize ?? DEFAULT_BATCH_SIZE);
|
|
4440
|
+
const retryOpts = {
|
|
4441
|
+
maxRetries: Math.max(1, opts.maxRetries ?? DEFAULT_MAX_RETRIES),
|
|
4442
|
+
backoffBaseMs: opts.backoffBaseMs ?? DEFAULT_BACKOFF_BASE_MS,
|
|
4443
|
+
isTransientError: opts.isTransientError ?? defaultIsTransientError,
|
|
4444
|
+
sleep: opts.sleep ?? defaultSleep
|
|
4445
|
+
};
|
|
4446
|
+
const results = new Array(rows.length);
|
|
4447
|
+
for (let start = 0; start < rows.length; start += batchSize) {
|
|
4448
|
+
const batch = rows.slice(start, start + batchSize);
|
|
4449
|
+
try {
|
|
4450
|
+
const records = await withRetry(() => opts.writeBatch(batch), retryOpts);
|
|
4451
|
+
for (let i = 0; i < batch.length; i++) {
|
|
4452
|
+
results[start + i] = { index: start + i, ok: true, record: records[i] };
|
|
4453
|
+
}
|
|
4454
|
+
} catch (batchErr) {
|
|
4455
|
+
if (batch.length === 1) {
|
|
4456
|
+
results[start] = { index: start, ok: false, error: batchErr };
|
|
4457
|
+
continue;
|
|
4458
|
+
}
|
|
4459
|
+
for (let i = 0; i < batch.length; i++) {
|
|
4460
|
+
const idx = start + i;
|
|
4461
|
+
try {
|
|
4462
|
+
const record = await withRetry(() => opts.writeOne(batch[i]), retryOpts);
|
|
4463
|
+
results[idx] = { index: idx, ok: true, record };
|
|
4464
|
+
} catch (err) {
|
|
4465
|
+
results[idx] = { index: idx, ok: false, error: err };
|
|
4466
|
+
}
|
|
4467
|
+
}
|
|
4468
|
+
}
|
|
4469
|
+
}
|
|
4470
|
+
return results;
|
|
4471
|
+
}
|
|
4472
|
+
|
|
4392
4473
|
// src/health-monitor.ts
|
|
4393
4474
|
var PluginHealthMonitor = class {
|
|
4394
4475
|
constructor(logger) {
|
|
@@ -5327,6 +5408,7 @@ export {
|
|
|
5327
5408
|
SemanticVersionManager,
|
|
5328
5409
|
ServiceLifecycle,
|
|
5329
5410
|
buildPermissionsFromGrants,
|
|
5411
|
+
bulkWrite,
|
|
5330
5412
|
calendarPartsInTz,
|
|
5331
5413
|
calendarPartsInTzOrUtc,
|
|
5332
5414
|
counterSignPayload,
|
|
@@ -5340,6 +5422,7 @@ export {
|
|
|
5340
5422
|
createPluginConfigValidator,
|
|
5341
5423
|
createPluginPermissionEnforcer,
|
|
5342
5424
|
deepMerge,
|
|
5425
|
+
defaultIsTransientError,
|
|
5343
5426
|
evaluateAuthGate,
|
|
5344
5427
|
extractApiKey,
|
|
5345
5428
|
generateApiKey,
|
|
@@ -5363,6 +5446,7 @@ export {
|
|
|
5363
5446
|
verifyPlatformSignature,
|
|
5364
5447
|
verifyPluginArtifact,
|
|
5365
5448
|
verifyPublisherSignature,
|
|
5366
|
-
wireAuthoredTranslationSync
|
|
5449
|
+
wireAuthoredTranslationSync,
|
|
5450
|
+
withTransientRetry
|
|
5367
5451
|
};
|
|
5368
5452
|
//# sourceMappingURL=index.js.map
|