@primitivedotdev/sdk 0.7.0 → 0.9.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.
@@ -0,0 +1,13 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return target;
11
+ };
12
+ //#endregion
13
+ export { __exportAll as t };
@@ -1,9 +1,7 @@
1
- import { EmailAddress, EmailAnalysis, EmailAuth, EmailReceivedEvent, ParsedDataComplete, ParsedDataFailed, ParsedError, RawContentDownloadOnly, RawContentInline, WebhookAttachment } from "../types-CKFmgitP.js";
2
- import { SignResult, StandardWebhooksSignResult, WEBHOOK_VERSION$1 as WEBHOOK_VERSION, signStandardWebhooksPayload$1 as signStandardWebhooksPayload, signWebhookPayload$1 as signWebhookPayload } from "../index-DLmAI4UQ.js";
1
+ import { C as ParsedDataFailed, D as RawContentDownloadOnly, M as WebhookAttachment, O as RawContentInline, S as ParsedDataComplete, c as EmailAnalysis, l as EmailAuth, s as EmailAddress, u as EmailReceivedEvent, w as ParsedError } from "../types-9vXGZjPd.js";
2
+ import { C as signStandardWebhooksPayload, h as WEBHOOK_VERSION, j as signWebhookPayload, k as SignResult, x as StandardWebhooksSignResult } from "../index-CbEivn3S.js";
3
3
 
4
4
  //#region src/contract/contract.d.ts
5
- /** Maximum raw email size for inline inclusion (256 KB). */
6
-
7
5
  /** Maximum raw email size for inline inclusion (256 KB). */
8
6
  declare const RAW_EMAIL_INLINE_THRESHOLD = 262144;
9
7
  /** Parsed content input when parsing succeeded. */
@@ -142,8 +140,7 @@ declare function generateEventId(endpoint_id: string, email_id: string): string;
142
140
  * ```
143
141
  */
144
142
  declare function buildEmailReceivedEvent(input: EmailReceivedEventInput, options?: {
145
- /** Override the attempted-at timestamp, typically for tests. */
146
- attempted_at?: string;
143
+ /** Override the attempted-at timestamp, typically for tests. */attempted_at?: string;
147
144
  }): EmailReceivedEvent;
148
145
  /**
149
146
  * Input for building an `EmailReceivedEvent` directly from parser output.
@@ -221,5 +218,6 @@ interface BuildEventFromParsedDataOptions {
221
218
  * @throws Error if `smtpRcptTo` is empty.
222
219
  * @throws WebhookValidationError if the assembled event fails schema validation.
223
220
  */
224
- declare function buildEventFromParsedData(params: BuildEventFromParsedDataOptions): EmailReceivedEvent; //#endregion
225
- export { BuildEventFromParsedDataOptions, EmailAddress, EmailAnalysis, EmailAuth, EmailReceivedEvent, EmailReceivedEventInput, ParsedDataComplete, ParsedDataFailed, ParsedError, ParsedInput, ParsedInputComplete, ParsedInputFailed, RAW_EMAIL_INLINE_THRESHOLD, RawContentDownloadOnly, RawContentInline, SignResult, StandardWebhooksSignResult, WEBHOOK_VERSION, WebhookAttachment, buildEmailReceivedEvent, buildEventFromParsedData, generateEventId, signStandardWebhooksPayload, signWebhookPayload };
221
+ declare function buildEventFromParsedData(params: BuildEventFromParsedDataOptions): EmailReceivedEvent;
222
+ //#endregion
223
+ export { BuildEventFromParsedDataOptions, type EmailAddress, type EmailAnalysis, type EmailAuth, type EmailReceivedEvent, EmailReceivedEventInput, type ParsedDataComplete, type ParsedDataFailed, type ParsedError, ParsedInput, ParsedInputComplete, ParsedInputFailed, RAW_EMAIL_INLINE_THRESHOLD, type RawContentDownloadOnly, type RawContentInline, type SignResult, type StandardWebhooksSignResult, WEBHOOK_VERSION, type WebhookAttachment, buildEmailReceivedEvent, buildEventFromParsedData, generateEventId, signStandardWebhooksPayload, signWebhookPayload };
@@ -1,7 +1,25 @@
1
- import { WEBHOOK_VERSION, signStandardWebhooksPayload, signWebhookPayload, validateEmailReceivedEvent } from "../webhook-COe5N_Uj.js";
1
+ import { E as signStandardWebhooksPayload, L as validateEmailReceivedEvent, M as signWebhookPayload, d as WEBHOOK_VERSION } from "../webhook-zkN4wUTs.js";
2
2
  import { createHash } from "node:crypto";
3
-
4
3
  //#region src/contract/contract.ts
4
+ /**
5
+ * Primitive Contract
6
+ *
7
+ * This module exposes the exact functions used by Primitive servers to construct
8
+ * webhook payloads. It serves two purposes:
9
+ *
10
+ * 1. **Build-time type safety**: Primitive producer code can import these types
11
+ * and functions so schema changes surface as TypeScript errors instead of
12
+ * runtime contract drift.
13
+ *
14
+ * 2. **Debugging transparency**: If you receive a malformed payload, you can
15
+ * compare it against these exact types and builder functions to identify
16
+ * which side broke the contract.
17
+ *
18
+ * This package is also useful for building test fixtures that match real
19
+ * payloads exactly.
20
+ *
21
+ * @module @primitivedotdev/sdk/contract
22
+ */
5
23
  /** Maximum raw email size for inline inclusion (256 KB). */
6
24
  const RAW_EMAIL_INLINE_THRESHOLD = 262144;
7
25
  /**
@@ -40,8 +58,7 @@ function validateTimestamp(timestamp, fieldName) {
40
58
  */
41
59
  function generateEventId(endpoint_id, email_id) {
42
60
  const hashInput = `email.received:${WEBHOOK_VERSION}:${endpoint_id}:${email_id}`;
43
- const hash = createHash("sha256").update(hashInput).digest("hex");
44
- return `evt_${hash}`;
61
+ return `evt_${createHash("sha256").update(hashInput).digest("hex")}`;
45
62
  }
46
63
  /**
47
64
  * Build an `EmailReceivedEvent` from producer-side input.
@@ -92,13 +109,12 @@ function generateEventId(endpoint_id, email_id) {
92
109
  */
93
110
  function buildEmailReceivedEvent(input, options) {
94
111
  const event_id = generateEventId(input.endpoint_id, input.email_id);
95
- const attempted_at = options?.attempted_at ? validateTimestamp(options.attempted_at, "attempted_at") : new Date().toISOString();
112
+ const attempted_at = options?.attempted_at ? validateTimestamp(options.attempted_at, "attempted_at") : (/* @__PURE__ */ new Date()).toISOString();
96
113
  const raw_size_bytes = input.raw_bytes.length;
97
114
  if (input.raw_size_bytes !== raw_size_bytes) throw new Error(`[@primitivedotdev/sdk/contract] Invalid raw_size_bytes: ${input.raw_size_bytes}. Expected ${raw_size_bytes} based on raw_bytes length`);
98
115
  const raw_sha256 = createHash("sha256").update(input.raw_bytes).digest("hex");
99
116
  if (input.raw_sha256 !== raw_sha256) throw new Error(`[@primitivedotdev/sdk/contract] Invalid raw_sha256: "${input.raw_sha256}". Expected ${raw_sha256} based on raw_bytes`);
100
- const shouldInline = raw_size_bytes <= RAW_EMAIL_INLINE_THRESHOLD;
101
- const rawContent = shouldInline ? {
117
+ const rawContent = raw_size_bytes <= 262144 ? {
102
118
  included: true,
103
119
  encoding: "base64",
104
120
  max_inline_bytes: RAW_EMAIL_INLINE_THRESHOLD,
@@ -156,7 +172,7 @@ function buildEmailReceivedEvent(input, options) {
156
172
  attachments: [],
157
173
  attachments_download_url: null
158
174
  };
159
- const event = {
175
+ return validateEmailReceivedEvent({
160
176
  id: event_id,
161
177
  event: "email.received",
162
178
  version: WEBHOOK_VERSION,
@@ -191,8 +207,7 @@ function buildEmailReceivedEvent(input, options) {
191
207
  analysis: input.analysis,
192
208
  auth: input.auth
193
209
  }
194
- };
195
- return validateEmailReceivedEvent(event);
210
+ });
196
211
  }
197
212
  /**
198
213
  * Build an `EmailReceivedEvent` from parsed email data plus delivery metadata.
@@ -239,7 +254,7 @@ function buildEventFromParsedData(params) {
239
254
  references: parsed.references,
240
255
  attachments: parsed.attachments
241
256
  };
242
- const input = {
257
+ return buildEmailReceivedEvent({
243
258
  email_id: params.emailId,
244
259
  endpoint_id: params.endpointId,
245
260
  message_id: params.messageId,
@@ -261,9 +276,7 @@ function buildEventFromParsedData(params) {
261
276
  parsed: parsedInput,
262
277
  auth: params.auth,
263
278
  analysis: params.analysis
264
- };
265
- return buildEmailReceivedEvent(input, params.buildOptions);
279
+ }, params.buildOptions);
266
280
  }
267
-
268
281
  //#endregion
269
- export { RAW_EMAIL_INLINE_THRESHOLD, WEBHOOK_VERSION, buildEmailReceivedEvent, buildEventFromParsedData, generateEventId, signStandardWebhooksPayload, signWebhookPayload };
282
+ export { RAW_EMAIL_INLINE_THRESHOLD, WEBHOOK_VERSION, buildEmailReceivedEvent, buildEventFromParsedData, generateEventId, signStandardWebhooksPayload, signWebhookPayload };
@@ -1,11 +1,8 @@
1
- import { EmailAuth, EmailReceivedEvent, ValidateEmailAuthResult, WebhookEvent } from "./types-CKFmgitP.js";
1
+ import { N as WebhookEvent, j as ValidateEmailAuthResult, l as EmailAuth, u as EmailReceivedEvent } from "./types-9vXGZjPd.js";
2
+ import { t as ReceivedEmail } from "./received-email-DNjpq_Wt.js";
2
3
  import { ErrorObject } from "ajv";
3
4
 
4
5
  //#region src/webhook/errors.d.ts
5
- /**
6
- * Verification error definitions.
7
- * Use these for documentation, dashboards, and i18n.
8
- */
9
6
  /**
10
7
  * Verification error definitions.
11
8
  * Use these for documentation, dashboards, and i18n.
@@ -210,7 +207,8 @@ declare class RawEmailDecodeError extends PrimitiveWebhookError {
210
207
  readonly code: RawEmailDecodeErrorCode;
211
208
  readonly suggestion: string;
212
209
  constructor(code: RawEmailDecodeErrorCode, message?: string);
213
- } //#endregion
210
+ }
211
+ //#endregion
214
212
  //#region src/validation.d.ts
215
213
  interface ValidationSuccess<T> {
216
214
  success: true;
@@ -223,7 +221,6 @@ interface ValidationFailure {
223
221
  type ValidationResult<T> = ValidationSuccess<T> | ValidationFailure;
224
222
  declare function validateEmailReceivedEvent(input: unknown): EmailReceivedEvent;
225
223
  declare function safeValidateEmailReceivedEvent(input: unknown): ValidationResult<EmailReceivedEvent>;
226
-
227
224
  //#endregion
228
225
  //#region src/webhook/download-tokens.d.ts
229
226
  /**
@@ -312,7 +309,6 @@ type VerifyDownloadTokenResult = {
312
309
  * @returns Whether the token is valid, plus a reason on failure.
313
310
  */
314
311
  declare function verifyDownloadToken(params: VerifyDownloadTokenOptions): VerifyDownloadTokenResult;
315
-
316
312
  //#endregion
317
313
  //#region src/webhook/signing.d.ts
318
314
  /**
@@ -405,7 +401,6 @@ declare function signWebhookPayload(rawBody: string | Buffer, secret: string | B
405
401
  * ```
406
402
  */
407
403
  declare function verifyWebhookSignature(opts: VerifyOptions): true;
408
-
409
404
  //#endregion
410
405
  //#region src/webhook/standard-webhooks.d.ts
411
406
  /**
@@ -457,15 +452,6 @@ interface StandardWebhooksVerifyOptions {
457
452
  /** Override current time for testing (unix seconds) */
458
453
  nowSeconds?: number;
459
454
  }
460
- /**
461
- * Prepare a Standard Webhooks secret for HMAC computation.
462
- *
463
- * Strips the "whsec_" prefix if present, then base64-decodes the remainder
464
- * to produce the raw HMAC key bytes.
465
- *
466
- * @internal
467
- */
468
-
469
455
  /**
470
456
  * Sign a webhook payload using the Standard Webhooks format.
471
457
  *
@@ -481,7 +467,6 @@ declare function signStandardWebhooksPayload(rawBody: string | Buffer, secret: s
481
467
  * Throws `WebhookVerificationError` on failure with a specific error code.
482
468
  */
483
469
  declare function verifyStandardWebhooksSignature(opts: StandardWebhooksVerifyOptions): true;
484
-
485
470
  //#endregion
486
471
  //#region src/schema.generated.d.ts
487
472
  /**
@@ -1273,7 +1258,8 @@ declare const emailReceivedEventJsonSchema: {
1273
1258
  readonly description: "DKIM signature verification result for a single signature.";
1274
1259
  };
1275
1260
  };
1276
- }; //#endregion
1261
+ };
1262
+ //#endregion
1277
1263
  //#region src/webhook/auth.d.ts
1278
1264
  /**
1279
1265
  * Validate email authentication and compute a verdict.
@@ -1335,7 +1321,6 @@ declare const emailReceivedEventJsonSchema: {
1335
1321
  * ```
1336
1322
  */
1337
1323
  declare function validateEmailAuth(auth: EmailAuth): ValidateEmailAuthResult;
1338
-
1339
1324
  //#endregion
1340
1325
  //#region src/webhook/version.d.ts
1341
1326
  /**
@@ -1351,15 +1336,8 @@ declare function validateEmailAuth(auth: EmailAuth): ValidateEmailAuthResult;
1351
1336
  * need to handle version-specific behavior.
1352
1337
  */
1353
1338
  declare const WEBHOOK_VERSION = "2025-12-14";
1354
-
1355
1339
  //#endregion
1356
1340
  //#region src/webhook/index.d.ts
1357
- /**
1358
- * Valid webhook version format (YYYY-MM-DD date string).
1359
- * The SDK accepts any valid date-formatted version, not just the current one,
1360
- * for forward and backward compatibility.
1361
- */
1362
-
1363
1341
  /**
1364
1342
  * Parse a webhook payload, returning typed events for known types
1365
1343
  * and UnknownEvent for future event types.
@@ -1484,6 +1462,12 @@ interface HandleWebhookOptions {
1484
1462
  * ```
1485
1463
  */
1486
1464
  declare function handleWebhook(options: HandleWebhookOptions): EmailReceivedEvent;
1465
+ interface ReceiveRequestOptions {
1466
+ secret: string;
1467
+ toleranceSeconds?: number;
1468
+ }
1469
+ declare function receive(options: HandleWebhookOptions): ReceivedEmail;
1470
+ declare function receive(request: Request, options: ReceiveRequestOptions): Promise<ReceivedEmail>;
1487
1471
  /**
1488
1472
  * Returns headers for the optional "content discard" feature.
1489
1473
  *
@@ -1639,6 +1623,5 @@ declare function decodeRawEmail(event: EmailReceivedEvent, options?: DecodeRawEm
1639
1623
  * ```
1640
1624
  */
1641
1625
  declare function verifyRawEmailDownload(downloaded: Buffer | ArrayBuffer | Uint8Array, event: EmailReceivedEvent): Buffer;
1642
-
1643
1626
  //#endregion
1644
- export { DecodeRawEmailOptions, GenerateDownloadTokenOptions, HandleWebhookOptions, LEGACY_CONFIRMED_HEADER as LEGACY_CONFIRMED_HEADER$1, LEGACY_SIGNATURE_HEADER as LEGACY_SIGNATURE_HEADER$1, PAYLOAD_ERRORS as PAYLOAD_ERRORS$1, PRIMITIVE_CONFIRMED_HEADER as PRIMITIVE_CONFIRMED_HEADER$1, PRIMITIVE_SIGNATURE_HEADER as PRIMITIVE_SIGNATURE_HEADER$1, PrimitiveWebhookError as PrimitiveWebhookError$1, RAW_EMAIL_ERRORS as RAW_EMAIL_ERRORS$1, RawEmailDecodeError as RawEmailDecodeError$1, RawEmailDecodeErrorCode, STANDARD_WEBHOOK_ID_HEADER as STANDARD_WEBHOOK_ID_HEADER$1, STANDARD_WEBHOOK_SIGNATURE_HEADER as STANDARD_WEBHOOK_SIGNATURE_HEADER$1, STANDARD_WEBHOOK_TIMESTAMP_HEADER as STANDARD_WEBHOOK_TIMESTAMP_HEADER$1, SignResult, StandardWebhooksSignResult, StandardWebhooksVerifyOptions, VERIFICATION_ERRORS as VERIFICATION_ERRORS$1, VerifyDownloadTokenOptions, VerifyDownloadTokenResult, VerifyOptions, WEBHOOK_VERSION as WEBHOOK_VERSION$1, WebhookErrorCode, WebhookHeaders, WebhookPayloadError as WebhookPayloadError$1, WebhookPayloadErrorCode, WebhookValidationError as WebhookValidationError$1, WebhookValidationErrorCode, WebhookVerificationError as WebhookVerificationError$1, WebhookVerificationErrorCode, confirmedHeaders as confirmedHeaders$1, decodeRawEmail as decodeRawEmail$1, emailReceivedEventJsonSchema as emailReceivedEventJsonSchema$1, generateDownloadToken as generateDownloadToken$1, getDownloadTimeRemaining as getDownloadTimeRemaining$1, handleWebhook as handleWebhook$1, isDownloadExpired as isDownloadExpired$1, isEmailReceivedEvent as isEmailReceivedEvent$1, isRawIncluded as isRawIncluded$1, parseWebhookEvent as parseWebhookEvent$1, safeValidateEmailReceivedEvent as safeValidateEmailReceivedEvent$1, signStandardWebhooksPayload as signStandardWebhooksPayload$1, signWebhookPayload as signWebhookPayload$1, validateEmailAuth as validateEmailAuth$1, validateEmailReceivedEvent as validateEmailReceivedEvent$1, verifyDownloadToken as verifyDownloadToken$1, verifyRawEmailDownload as verifyRawEmailDownload$1, verifyStandardWebhooksSignature as verifyStandardWebhooksSignature$1, verifyWebhookSignature as verifyWebhookSignature$1 };
1627
+ export { VerifyOptions as A, PAYLOAD_ERRORS as B, signStandardWebhooksPayload as C, PRIMITIVE_CONFIRMED_HEADER as D, LEGACY_SIGNATURE_HEADER as E, VerifyDownloadTokenResult as F, VERIFICATION_ERRORS as G, RAW_EMAIL_ERRORS as H, generateDownloadToken as I, WebhookPayloadErrorCode as J, WebhookErrorCode as K, verifyDownloadToken as L, verifyWebhookSignature as M, GenerateDownloadTokenOptions as N, PRIMITIVE_SIGNATURE_HEADER as O, VerifyDownloadTokenOptions as P, WebhookVerificationErrorCode as Q, safeValidateEmailReceivedEvent as R, StandardWebhooksVerifyOptions as S, LEGACY_CONFIRMED_HEADER as T, RawEmailDecodeError as U, PrimitiveWebhookError as V, RawEmailDecodeErrorCode as W, WebhookValidationErrorCode as X, WebhookValidationError as Y, WebhookVerificationError as Z, emailReceivedEventJsonSchema as _, confirmedHeaders as a, STANDARD_WEBHOOK_TIMESTAMP_HEADER as b, handleWebhook as c, isRawIncluded as d, parseWebhookEvent as f, validateEmailAuth as g, WEBHOOK_VERSION as h, WebhookHeaders as i, signWebhookPayload as j, SignResult as k, isDownloadExpired as l, verifyRawEmailDownload as m, HandleWebhookOptions as n, decodeRawEmail as o, receive as p, WebhookPayloadError as q, ReceiveRequestOptions as r, getDownloadTimeRemaining as s, DecodeRawEmailOptions as t, isEmailReceivedEvent as u, STANDARD_WEBHOOK_ID_HEADER as v, verifyStandardWebhooksSignature as w, StandardWebhooksSignResult as x, STANDARD_WEBHOOK_SIGNATURE_HEADER as y, validateEmailReceivedEvent as z };