@astrasyncai/verification-gateway 5.10.0 → 5.12.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/README.md +4 -0
- package/dist/adapters/express.d.mts +1 -1
- package/dist/adapters/express.d.ts +1 -1
- package/dist/adapters/express.js +359 -30
- package/dist/adapters/express.js.map +1 -1
- package/dist/adapters/express.mjs +359 -30
- package/dist/adapters/express.mjs.map +1 -1
- package/dist/adapters/mcp.d.mts +5 -1
- package/dist/adapters/mcp.d.ts +5 -1
- package/dist/adapters/mcp.js +168 -41
- package/dist/adapters/mcp.js.map +1 -1
- package/dist/adapters/mcp.mjs +168 -41
- package/dist/adapters/mcp.mjs.map +1 -1
- package/dist/adapters/nextjs.d.mts +1 -1
- package/dist/adapters/nextjs.d.ts +1 -1
- package/dist/adapters/nextjs.js +352 -13
- package/dist/adapters/nextjs.js.map +1 -1
- package/dist/adapters/nextjs.mjs +352 -13
- package/dist/adapters/nextjs.mjs.map +1 -1
- package/dist/adapters/sdk.d.mts +9 -1
- package/dist/adapters/sdk.d.ts +9 -1
- package/dist/adapters/sdk.js +100 -8
- package/dist/adapters/sdk.js.map +1 -1
- package/dist/adapters/sdk.mjs +100 -8
- package/dist/adapters/sdk.mjs.map +1 -1
- package/dist/agent/index.js +1 -1
- package/dist/agent/index.js.map +1 -1
- package/dist/agent/index.mjs +1 -1
- package/dist/agent/index.mjs.map +1 -1
- package/dist/bin/astrasync-claude-hook.js +805 -16
- package/dist/bin/astrasync-codex-hook.js +805 -16
- package/dist/bin/astrasync-guard.js +894 -71
- package/dist/bin/astrasync.js +951 -74
- package/dist/browser/background.js +284 -7
- package/dist/browser/background.js.map +1 -1
- package/dist/browser/background.mjs +284 -7
- package/dist/browser/background.mjs.map +1 -1
- package/dist/cli/index.d.mts +8 -8
- package/dist/cli/index.d.ts +8 -8
- package/dist/cli/index.js +22 -64
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +22 -64
- package/dist/cli/index.mjs.map +1 -1
- package/dist/codex/index.d.mts +5 -0
- package/dist/codex/index.d.ts +5 -0
- package/dist/codex/index.js +821 -24
- package/dist/codex/index.js.map +1 -1
- package/dist/codex/index.mjs +823 -26
- package/dist/codex/index.mjs.map +1 -1
- package/dist/cursor/extension.d.mts +9 -2
- package/dist/cursor/extension.d.ts +9 -2
- package/dist/cursor/extension.js +787 -11
- package/dist/cursor/extension.js.map +1 -1
- package/dist/cursor/extension.mjs +789 -11
- package/dist/cursor/extension.mjs.map +1 -1
- package/dist/edge-config.d.mts +1 -1
- package/dist/edge-config.d.ts +1 -1
- package/dist/edge-config.js +1 -1
- package/dist/edge-config.js.map +1 -1
- package/dist/edge-config.mjs +1 -1
- package/dist/edge-config.mjs.map +1 -1
- package/dist/edge-core/index.d.mts +128 -13
- package/dist/edge-core/index.d.ts +128 -13
- package/dist/edge-core/index.js +337 -55
- package/dist/edge-core/index.js.map +1 -1
- package/dist/edge-core/index.mjs +334 -55
- package/dist/edge-core/index.mjs.map +1 -1
- package/dist/enrolment-store-8DyaIOLe.d.mts +26 -0
- package/dist/enrolment-store-8DyaIOLe.d.ts +26 -0
- package/dist/gateway/gateway.js +284 -7
- package/dist/gateway/gateway.js.map +1 -1
- package/dist/gateway/gateway.mjs +284 -7
- package/dist/gateway/gateway.mjs.map +1 -1
- package/dist/git-trigger/git-hooks.d.mts +1 -1
- package/dist/git-trigger/git-hooks.d.ts +1 -1
- package/dist/index.d.mts +215 -5
- package/dist/index.d.ts +215 -5
- package/dist/index.js +3756 -80
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3748 -80
- package/dist/index.mjs.map +1 -1
- package/dist/platform-signatures.js +4 -1
- package/dist/platform-signatures.js.map +1 -1
- package/dist/platform-signatures.mjs +4 -1
- package/dist/platform-signatures.mjs.map +1 -1
- package/dist/registration/index.js +1 -1
- package/dist/registration/index.js.map +1 -1
- package/dist/registration/index.mjs +1 -1
- package/dist/registration/index.mjs.map +1 -1
- package/dist/transport/index.js +1 -1
- package/dist/transport/index.js.map +1 -1
- package/dist/transport/index.mjs +1 -1
- package/dist/transport/index.mjs.map +1 -1
- package/dist/{types-CTmUpNpS.d.mts → types-BCkwBA84.d.mts} +12 -0
- package/dist/{types-CTmUpNpS.d.ts → types-BCkwBA84.d.ts} +12 -0
- package/dist/{types-CtFXmDzm.d.ts → types-C_b4QTyO.d.ts} +18 -0
- package/dist/{types-DQrn_kei.d.mts → types-DlshIIR-.d.mts} +18 -0
- package/dist/ui/index.d.mts +1 -1
- package/dist/ui/index.d.ts +1 -1
- package/dist/verify.d.mts +35 -4
- package/dist/verify.d.ts +35 -4
- package/dist/verify.js +130 -16
- package/dist/verify.js.map +1 -1
- package/dist/verify.mjs +129 -16
- package/dist/verify.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where fleet enrolment and the cached signed policy live.
|
|
3
|
+
*
|
|
4
|
+
* Machine scope (written by `astrasync guard enroll --system` / MDM) WINS
|
|
5
|
+
* over user scope. The machine-scope fleet.json is world-readable (0644) —
|
|
6
|
+
* the hook runs as the developer and must read the device credential; that
|
|
7
|
+
* credential can only heartbeat and fetch this device's signed policy, never
|
|
8
|
+
* alter it. User scope lives under ~/.astrasync/fleet/ at 0600.
|
|
9
|
+
*
|
|
10
|
+
* The policy cache + runtime state are user-scope (the hook must be able to
|
|
11
|
+
* refresh them); tampering is caught by the signature check against the JWKS
|
|
12
|
+
* pinned in the enrolment, and a missing cache is re-fetched or fails closed.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
interface FleetPaths {
|
|
16
|
+
machineDevice: string;
|
|
17
|
+
machinePolicy: string;
|
|
18
|
+
userDir: string;
|
|
19
|
+
userDevice: string;
|
|
20
|
+
userPolicy: string;
|
|
21
|
+
/** Signed key-rotation chain (untrusted storage: each link is verified from the pin). */
|
|
22
|
+
userKeyChain: string;
|
|
23
|
+
state: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type { FleetPaths as F };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where fleet enrolment and the cached signed policy live.
|
|
3
|
+
*
|
|
4
|
+
* Machine scope (written by `astrasync guard enroll --system` / MDM) WINS
|
|
5
|
+
* over user scope. The machine-scope fleet.json is world-readable (0644) —
|
|
6
|
+
* the hook runs as the developer and must read the device credential; that
|
|
7
|
+
* credential can only heartbeat and fetch this device's signed policy, never
|
|
8
|
+
* alter it. User scope lives under ~/.astrasync/fleet/ at 0600.
|
|
9
|
+
*
|
|
10
|
+
* The policy cache + runtime state are user-scope (the hook must be able to
|
|
11
|
+
* refresh them); tampering is caught by the signature check against the JWKS
|
|
12
|
+
* pinned in the enrolment, and a missing cache is re-fetched or fails closed.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
interface FleetPaths {
|
|
16
|
+
machineDevice: string;
|
|
17
|
+
machinePolicy: string;
|
|
18
|
+
userDir: string;
|
|
19
|
+
userDevice: string;
|
|
20
|
+
userPolicy: string;
|
|
21
|
+
/** Signed key-rotation chain (untrusted storage: each link is verified from the pin). */
|
|
22
|
+
userKeyChain: string;
|
|
23
|
+
state: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type { FleetPaths as F };
|
package/dist/gateway/gateway.js
CHANGED
|
@@ -3748,7 +3748,7 @@ function getTrustLevel(score) {
|
|
|
3748
3748
|
}
|
|
3749
3749
|
|
|
3750
3750
|
// src/version.ts
|
|
3751
|
-
var SDK_VERSION = "5.
|
|
3751
|
+
var SDK_VERSION = "5.12.0";
|
|
3752
3752
|
|
|
3753
3753
|
// src/http.ts
|
|
3754
3754
|
var SDK_USER_AGENT = `astrasync-sdk/${SDK_VERSION}`;
|
|
@@ -3775,6 +3775,14 @@ function getCachedWellKnownUrls(apiBaseUrl) {
|
|
|
3775
3775
|
return cache.get(apiBaseUrl)?.data;
|
|
3776
3776
|
}
|
|
3777
3777
|
|
|
3778
|
+
// src/failure-posture.ts
|
|
3779
|
+
var REJECTED_IDENTITY_DIMENSIONS = /* @__PURE__ */ new Set([
|
|
3780
|
+
"identity.invalid_agent_id",
|
|
3781
|
+
"identity.unknown_agent",
|
|
3782
|
+
"agent.lookup"
|
|
3783
|
+
]);
|
|
3784
|
+
var UNKNOWN_AGENT_GUIDANCE = "Register your agent with AstraSync (or, inside an MCP host, add the AstraSync MCP connector for keyless, human-approved registration), then present the canonical ASTRA-* id issued after owner approval.";
|
|
3785
|
+
|
|
3778
3786
|
// src/verify.ts
|
|
3779
3787
|
var DEFAULT_CONFIG = {
|
|
3780
3788
|
apiBaseUrl: "https://astrasync.ai/api",
|
|
@@ -3986,11 +3994,44 @@ function createGuidanceResponse(_config, reason, options = {}) {
|
|
|
3986
3994
|
// coercing the absent recommendation to a policy 'deny'. Misconfig is
|
|
3987
3995
|
// deterministic and intentionally NOT marked retryable.
|
|
3988
3996
|
...isApiError && { retryable: true },
|
|
3997
|
+
...options.httpStatus !== void 0 && { httpStatus: options.httpStatus },
|
|
3998
|
+
...options.retryAfterMs !== void 0 && { retryAfterMs: options.retryAfterMs },
|
|
3989
3999
|
correlationId: options.correlationId,
|
|
3990
4000
|
attemptId: options.attemptId,
|
|
3991
4001
|
verifiedAt: /* @__PURE__ */ new Date()
|
|
3992
4002
|
};
|
|
3993
4003
|
}
|
|
4004
|
+
async function readJsonSafe(response) {
|
|
4005
|
+
try {
|
|
4006
|
+
const parsed = await response.json();
|
|
4007
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
4008
|
+
} catch {
|
|
4009
|
+
return {};
|
|
4010
|
+
}
|
|
4011
|
+
}
|
|
4012
|
+
function rejectedIdentityGuidance(failures) {
|
|
4013
|
+
const f = failures?.find((x) => REJECTED_IDENTITY_DIMENSIONS.has(x.dimension));
|
|
4014
|
+
if (!f) return void 0;
|
|
4015
|
+
if (f.dimension === "agent.lookup") {
|
|
4016
|
+
return `The presented agent identifier is not registered with AstraSync. ${UNKNOWN_AGENT_GUIDANCE}`;
|
|
4017
|
+
}
|
|
4018
|
+
return f.guidance ? `${f.message} ${f.guidance}` : f.message;
|
|
4019
|
+
}
|
|
4020
|
+
var MAX_RETRY_AFTER_MS = 15 * 60 * 1e3;
|
|
4021
|
+
function parseRetryAfterMs(response) {
|
|
4022
|
+
const raw = response.headers?.get?.("retry-after");
|
|
4023
|
+
if (!raw) return void 0;
|
|
4024
|
+
const trimmed = raw.trim();
|
|
4025
|
+
let ms;
|
|
4026
|
+
if (/^\d+(\.\d+)?$/.test(trimmed)) {
|
|
4027
|
+
ms = Number(trimmed) * 1e3;
|
|
4028
|
+
} else {
|
|
4029
|
+
const at = Date.parse(trimmed);
|
|
4030
|
+
if (Number.isNaN(at)) return void 0;
|
|
4031
|
+
ms = at - Date.now();
|
|
4032
|
+
}
|
|
4033
|
+
return Math.min(MAX_RETRY_AFTER_MS, Math.max(0, Math.round(ms)));
|
|
4034
|
+
}
|
|
3994
4035
|
async function callVerifyAccessAPI(config, request) {
|
|
3995
4036
|
const { credentials, ...requestData } = request;
|
|
3996
4037
|
const body = {
|
|
@@ -4047,7 +4088,8 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4047
4088
|
body: JSON.stringify(body),
|
|
4048
4089
|
signal: AbortSignal.timeout(config.verifyTimeoutMs ?? 1e4)
|
|
4049
4090
|
});
|
|
4050
|
-
const data = await response
|
|
4091
|
+
const data = await readJsonSafe(response);
|
|
4092
|
+
const retryAfterMs = parseRetryAfterMs(response);
|
|
4051
4093
|
if (response.status === 410) {
|
|
4052
4094
|
return {
|
|
4053
4095
|
success: true,
|
|
@@ -4085,6 +4127,23 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4085
4127
|
}
|
|
4086
4128
|
};
|
|
4087
4129
|
}
|
|
4130
|
+
if (response.status === 404 && credentials.astraId && (data?.error === "AGENT_NOT_FOUND" || typeof data?.message === "string" && /agent/i.test(data.message))) {
|
|
4131
|
+
return {
|
|
4132
|
+
success: true,
|
|
4133
|
+
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0,
|
|
4134
|
+
access: {
|
|
4135
|
+
allowed: false,
|
|
4136
|
+
reason: "unknown_agent",
|
|
4137
|
+
failures: [
|
|
4138
|
+
{
|
|
4139
|
+
dimension: "identity.unknown_agent",
|
|
4140
|
+
message: "The presented agent identifier is not registered with AstraSync.",
|
|
4141
|
+
guidance: UNKNOWN_AGENT_GUIDANCE
|
|
4142
|
+
}
|
|
4143
|
+
]
|
|
4144
|
+
}
|
|
4145
|
+
};
|
|
4146
|
+
}
|
|
4088
4147
|
if (!response.ok) {
|
|
4089
4148
|
if (response.status === 401) {
|
|
4090
4149
|
return {
|
|
@@ -4099,9 +4158,11 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4099
4158
|
}
|
|
4100
4159
|
return {
|
|
4101
4160
|
success: false,
|
|
4102
|
-
error: data.message || data.error || `API returned ${response.status}`,
|
|
4161
|
+
error: typeof data?.message === "string" && data.message || typeof data?.error === "string" && data.error || `API returned ${response.status}`,
|
|
4103
4162
|
correlationId: typeof data?.correlationId === "string" ? data.correlationId : void 0,
|
|
4104
|
-
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0
|
|
4163
|
+
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0,
|
|
4164
|
+
httpStatus: response.status,
|
|
4165
|
+
...retryAfterMs !== void 0 && { retryAfterMs }
|
|
4105
4166
|
};
|
|
4106
4167
|
}
|
|
4107
4168
|
return data;
|
|
@@ -4173,12 +4234,14 @@ async function verify(config, request, options) {
|
|
|
4173
4234
|
correlationId: apiResponse.correlationId,
|
|
4174
4235
|
attemptId: apiResponse.attemptId,
|
|
4175
4236
|
remediation: apiResponse.remediation,
|
|
4237
|
+
httpStatus: apiResponse.httpStatus,
|
|
4238
|
+
retryAfterMs: apiResponse.retryAfterMs,
|
|
4176
4239
|
urls
|
|
4177
4240
|
});
|
|
4178
4241
|
}
|
|
4179
4242
|
if (!apiResponse.access?.allowed) {
|
|
4180
4243
|
const aggregatedFailures = apiResponse.access?.failures;
|
|
4181
|
-
const idVerifiedFromBackend = apiResponse.verificationContext?.idVerified === true;
|
|
4244
|
+
const idVerifiedFromBackend = apiResponse.verificationContext?.idVerified === true && !aggregatedFailures?.some((f) => REJECTED_IDENTITY_DIMENSIONS.has(f.dimension));
|
|
4182
4245
|
const result2 = {
|
|
4183
4246
|
identityVerified: idVerifiedFromBackend,
|
|
4184
4247
|
policyAllowed: false,
|
|
@@ -4190,7 +4253,10 @@ async function verify(config, request, options) {
|
|
|
4190
4253
|
requiresStepUp: apiResponse.access?.requiresStepUp,
|
|
4191
4254
|
requiresApproval: apiResponse.access?.requiresApproval,
|
|
4192
4255
|
guidance: {
|
|
4193
|
-
|
|
4256
|
+
// 5.12.0: a presented-but-rejected id (malformed / unknown) carries
|
|
4257
|
+
// actionable registration guidance on its failure — surface it
|
|
4258
|
+
// instead of the bare machine reason ('unknown_agent').
|
|
4259
|
+
message: rejectedIdentityGuidance(aggregatedFailures) ?? (apiResponse.access?.reason || "Access denied by PDLSS policy"),
|
|
4194
4260
|
registrationUrl: urls?.registrationUrl ?? "",
|
|
4195
4261
|
documentationUrl: urls?.documentationUrl ?? "",
|
|
4196
4262
|
// 5.1.0: anonymous callers land on this branch — steer MCP-hosted
|
|
@@ -4290,7 +4356,7 @@ async function verify(config, request, options) {
|
|
|
4290
4356
|
registrationUrl: urls?.registrationUrl ?? "",
|
|
4291
4357
|
documentationUrl: urls?.documentationUrl ?? ""
|
|
4292
4358
|
};
|
|
4293
|
-
} else if (result.recommendation === "step_up_required") {
|
|
4359
|
+
} else if (result.recommendation === "step_up_required" && result.identityVerified) {
|
|
4294
4360
|
result.requiresStepUp = true;
|
|
4295
4361
|
result.denialReasons = serverDenialReasons ?? result.recommendationReasons ?? ["Step-up verification required"];
|
|
4296
4362
|
}
|
|
@@ -4319,6 +4385,217 @@ var DEFAULT_EDGE_CONFIG = Object.freeze({
|
|
|
4319
4385
|
var DEFAULT_TTL_MS = 60 * 1e3;
|
|
4320
4386
|
var DEFAULT_MAX_STALE_MS = 24 * 60 * 60 * 1e3;
|
|
4321
4387
|
|
|
4388
|
+
// src/edge-core/events.ts
|
|
4389
|
+
async function withBudget(promise, ms) {
|
|
4390
|
+
let timer;
|
|
4391
|
+
try {
|
|
4392
|
+
return await Promise.race([
|
|
4393
|
+
promise,
|
|
4394
|
+
new Promise((resolve) => {
|
|
4395
|
+
timer = setTimeout(() => resolve(void 0), ms);
|
|
4396
|
+
})
|
|
4397
|
+
]);
|
|
4398
|
+
} catch {
|
|
4399
|
+
return void 0;
|
|
4400
|
+
} finally {
|
|
4401
|
+
if (timer) clearTimeout(timer);
|
|
4402
|
+
}
|
|
4403
|
+
}
|
|
4404
|
+
var destKeyOf = (d) => `${d.apiBaseUrl}\0${d.apiKey ?? ""}\0${d.counterparty}\0${d.adapter ?? ""}`;
|
|
4405
|
+
var BeaconBuffer = class {
|
|
4406
|
+
constructor(options = {}) {
|
|
4407
|
+
this.queue = [];
|
|
4408
|
+
this.droppedSinceLast = 0;
|
|
4409
|
+
this.droppedTotal = 0;
|
|
4410
|
+
this.deliveredTotal = 0;
|
|
4411
|
+
this.backoffUntil = 0;
|
|
4412
|
+
this.collapse = /* @__PURE__ */ new Map();
|
|
4413
|
+
/** Keys the backend answered 401 for — later batches go unauthenticated. */
|
|
4414
|
+
this.rejectedKeys = /* @__PURE__ */ new Set();
|
|
4415
|
+
this.opts = {
|
|
4416
|
+
maxItems: options.maxItems ?? 1e3,
|
|
4417
|
+
maxBatch: options.maxBatch ?? 100,
|
|
4418
|
+
maxAttempts: options.maxAttempts ?? 3,
|
|
4419
|
+
collapseWindowMs: options.collapseWindowMs ?? 6e4,
|
|
4420
|
+
collapseMaxKeys: options.collapseMaxKeys ?? 1e3,
|
|
4421
|
+
requestTimeoutMs: options.requestTimeoutMs ?? 1e4,
|
|
4422
|
+
retryBackoffMs: options.retryBackoffMs ?? 1e3,
|
|
4423
|
+
now: options.now ?? Date.now
|
|
4424
|
+
};
|
|
4425
|
+
}
|
|
4426
|
+
/**
|
|
4427
|
+
* Queue one beacon. With a `collapseKey`, a repeat inside the window is
|
|
4428
|
+
* merged into a held aggregate (sent when the window closes) instead of
|
|
4429
|
+
* becoming its own item.
|
|
4430
|
+
*/
|
|
4431
|
+
enqueue(dest, item, collapseKey) {
|
|
4432
|
+
const now = this.opts.now();
|
|
4433
|
+
if (collapseKey) {
|
|
4434
|
+
const entry = this.collapse.get(collapseKey);
|
|
4435
|
+
if (entry && now - entry.firstSeenAt < this.opts.collapseWindowMs) {
|
|
4436
|
+
for (const target of [entry.first, entry.held]) {
|
|
4437
|
+
if (target && !target.inFlight && this.queue.includes(target)) {
|
|
4438
|
+
target.item.repeatCount = (target.item.repeatCount ?? 1) + 1;
|
|
4439
|
+
return;
|
|
4440
|
+
}
|
|
4441
|
+
}
|
|
4442
|
+
const held = this.push(
|
|
4443
|
+
dest,
|
|
4444
|
+
{ ...item, repeatCount: 1 },
|
|
4445
|
+
entry.firstSeenAt + this.opts.collapseWindowMs
|
|
4446
|
+
);
|
|
4447
|
+
entry.held = held;
|
|
4448
|
+
return;
|
|
4449
|
+
}
|
|
4450
|
+
this.collapse.delete(collapseKey);
|
|
4451
|
+
if (this.collapse.size >= this.opts.collapseMaxKeys) {
|
|
4452
|
+
const oldest = this.collapse.keys().next().value;
|
|
4453
|
+
if (oldest !== void 0) this.collapse.delete(oldest);
|
|
4454
|
+
}
|
|
4455
|
+
const fresh = { firstSeenAt: now };
|
|
4456
|
+
this.collapse.set(collapseKey, fresh);
|
|
4457
|
+
fresh.first = this.push(dest, item, now);
|
|
4458
|
+
return;
|
|
4459
|
+
}
|
|
4460
|
+
this.push(dest, item, now);
|
|
4461
|
+
}
|
|
4462
|
+
/**
|
|
4463
|
+
* Deliver every eligible queued item. Concurrent callers share ONE
|
|
4464
|
+
* in-flight drain (it survives the caller's budget and a Lambda freeze),
|
|
4465
|
+
* so nothing in flight is ever sent twice; items queued while it runs are
|
|
4466
|
+
* picked up by the same drain.
|
|
4467
|
+
*/
|
|
4468
|
+
flush() {
|
|
4469
|
+
if (!this.inFlight) {
|
|
4470
|
+
this.inFlight = this.drain().finally(() => {
|
|
4471
|
+
this.inFlight = void 0;
|
|
4472
|
+
});
|
|
4473
|
+
}
|
|
4474
|
+
return this.inFlight;
|
|
4475
|
+
}
|
|
4476
|
+
/** Counters for tests and diagnostics. */
|
|
4477
|
+
stats() {
|
|
4478
|
+
return {
|
|
4479
|
+
queueDepth: this.queue.length,
|
|
4480
|
+
droppedSinceLast: this.droppedSinceLast,
|
|
4481
|
+
droppedTotal: this.droppedTotal,
|
|
4482
|
+
deliveredTotal: this.deliveredTotal,
|
|
4483
|
+
flushing: this.inFlight !== void 0
|
|
4484
|
+
};
|
|
4485
|
+
}
|
|
4486
|
+
push(dest, item, eligibleAt) {
|
|
4487
|
+
while (this.queue.length >= this.opts.maxItems) this.dropOldest();
|
|
4488
|
+
const entry = {
|
|
4489
|
+
dest,
|
|
4490
|
+
destKey: destKeyOf(dest),
|
|
4491
|
+
item,
|
|
4492
|
+
attempts: 0,
|
|
4493
|
+
eligibleAt,
|
|
4494
|
+
inFlight: false
|
|
4495
|
+
};
|
|
4496
|
+
this.queue.push(entry);
|
|
4497
|
+
return entry;
|
|
4498
|
+
}
|
|
4499
|
+
dropOldest() {
|
|
4500
|
+
if (this.queue.shift()) this.countDropped(1);
|
|
4501
|
+
}
|
|
4502
|
+
countDropped(n) {
|
|
4503
|
+
this.droppedSinceLast += n;
|
|
4504
|
+
this.droppedTotal += n;
|
|
4505
|
+
}
|
|
4506
|
+
async drain() {
|
|
4507
|
+
for (; ; ) {
|
|
4508
|
+
const now = this.opts.now();
|
|
4509
|
+
if (now < this.backoffUntil) return;
|
|
4510
|
+
const batch = this.takeBatch(now);
|
|
4511
|
+
if (batch.length === 0) return;
|
|
4512
|
+
const ok = await this.send(batch);
|
|
4513
|
+
if (!ok) return;
|
|
4514
|
+
}
|
|
4515
|
+
}
|
|
4516
|
+
/** Oldest eligible item + up to maxBatch-1 more for the same destination. */
|
|
4517
|
+
takeBatch(now) {
|
|
4518
|
+
const first = this.queue.find((q) => q.eligibleAt <= now);
|
|
4519
|
+
if (!first) return [];
|
|
4520
|
+
const batch = [];
|
|
4521
|
+
const rest = [];
|
|
4522
|
+
for (const q of this.queue) {
|
|
4523
|
+
if (batch.length < this.opts.maxBatch && q.destKey === first.destKey && q.eligibleAt <= now) {
|
|
4524
|
+
q.inFlight = true;
|
|
4525
|
+
batch.push(q);
|
|
4526
|
+
} else {
|
|
4527
|
+
rest.push(q);
|
|
4528
|
+
}
|
|
4529
|
+
}
|
|
4530
|
+
this.queue = rest;
|
|
4531
|
+
return batch;
|
|
4532
|
+
}
|
|
4533
|
+
/** POST one batch. Returns false when the drain should pause (backoff). */
|
|
4534
|
+
async send(batch) {
|
|
4535
|
+
const dest = batch[0].dest;
|
|
4536
|
+
const useKey = dest.apiKey && !this.rejectedKeys.has(dest.apiKey) ? dest.apiKey : void 0;
|
|
4537
|
+
const reportedDropped = this.droppedSinceLast;
|
|
4538
|
+
const meta = {
|
|
4539
|
+
droppedSinceLast: reportedDropped,
|
|
4540
|
+
queueDepth: this.queue.length,
|
|
4541
|
+
adapter: dest.adapter ?? "edge-core",
|
|
4542
|
+
version: SDK_VERSION
|
|
4543
|
+
};
|
|
4544
|
+
const controller = typeof AbortController !== "undefined" ? new AbortController() : void 0;
|
|
4545
|
+
const response = await withBudget(
|
|
4546
|
+
sdkFetch(`${dest.apiBaseUrl}/verification-activity/edge-events`, {
|
|
4547
|
+
method: "POST",
|
|
4548
|
+
headers: {
|
|
4549
|
+
"Content-Type": "application/json",
|
|
4550
|
+
...useKey && { Authorization: `Bearer ${useKey}` }
|
|
4551
|
+
},
|
|
4552
|
+
body: JSON.stringify({ events: batch.map((q) => q.item), meta }),
|
|
4553
|
+
...controller && { signal: controller.signal }
|
|
4554
|
+
}),
|
|
4555
|
+
this.opts.requestTimeoutMs
|
|
4556
|
+
);
|
|
4557
|
+
if (!response) controller?.abort();
|
|
4558
|
+
const status = response?.status;
|
|
4559
|
+
for (const q of batch) q.inFlight = false;
|
|
4560
|
+
if (status !== void 0 && status >= 200 && status < 300) {
|
|
4561
|
+
this.droppedSinceLast = Math.max(0, this.droppedSinceLast - reportedDropped);
|
|
4562
|
+
this.deliveredTotal += batch.length;
|
|
4563
|
+
return true;
|
|
4564
|
+
}
|
|
4565
|
+
if (status === 401 && useKey) {
|
|
4566
|
+
this.rejectedKeys.add(useKey);
|
|
4567
|
+
this.requeue(batch, false);
|
|
4568
|
+
return true;
|
|
4569
|
+
}
|
|
4570
|
+
if (status === 429) {
|
|
4571
|
+
const retryAfter = Number(response?.headers.get("retry-after"));
|
|
4572
|
+
const waitMs = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter, 120) * 1e3 : 3e4;
|
|
4573
|
+
this.backoffUntil = this.opts.now() + waitMs;
|
|
4574
|
+
this.requeue(batch, false);
|
|
4575
|
+
return false;
|
|
4576
|
+
}
|
|
4577
|
+
if (status !== void 0 && status >= 400 && status < 500) {
|
|
4578
|
+
this.countDropped(batch.length);
|
|
4579
|
+
return true;
|
|
4580
|
+
}
|
|
4581
|
+
this.backoffUntil = this.opts.now() + this.opts.retryBackoffMs;
|
|
4582
|
+
this.requeue(batch, true);
|
|
4583
|
+
return false;
|
|
4584
|
+
}
|
|
4585
|
+
/** Put a failed batch back at the head of the queue (order preserved). */
|
|
4586
|
+
requeue(batch, countAttempt) {
|
|
4587
|
+
const keep = [];
|
|
4588
|
+
for (const q of batch) {
|
|
4589
|
+
if (countAttempt) q.attempts += 1;
|
|
4590
|
+
if (q.attempts >= this.opts.maxAttempts) this.countDropped(1);
|
|
4591
|
+
else keep.push(q);
|
|
4592
|
+
}
|
|
4593
|
+
this.queue = [...keep, ...this.queue];
|
|
4594
|
+
while (this.queue.length > this.opts.maxItems) this.dropOldest();
|
|
4595
|
+
}
|
|
4596
|
+
};
|
|
4597
|
+
var beaconBuffer = new BeaconBuffer();
|
|
4598
|
+
|
|
4322
4599
|
// src/adapters/express.ts
|
|
4323
4600
|
var DEFAULT_ROUTES_REFRESH_MS = 5 * 60 * 1e3;
|
|
4324
4601
|
|