@stacksjs/email 0.75.600 → 0.75.602

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,31 @@
1
+ /**
2
+ * What a database's error means to the suppression list, on every dialect.
3
+ *
4
+ * Kept apart from `suppression.ts` so these can be read, and tested, without
5
+ * a database behind them: they decide whether a send proceeds, and each one
6
+ * was wrong on Postgres once.
7
+ */
8
+ /**
9
+ * Does this error mean the backing table simply hasn't been migrated yet?
10
+ * Exported for direct unit coverage across dialects.
11
+ *
12
+ * Each supported database phrases "missing table" differently, and the
13
+ * suppression layer is fail-open — so a matcher that only knows sqlite/mysql
14
+ * would let Postgres's wording slip through and hard-fail every send on an
15
+ * un-migrated Postgres DB (stacksjs/stacks#1976). We scope the Postgres check
16
+ * to `undefined_table` specifically (SQLSTATE 42P01 / `relation "..." does not
17
+ * exist`) rather than a bare `does not exist`, so a genuine `column ... does
18
+ * not exist` schema bug still surfaces instead of being silently swallowed.
19
+ */
20
+ export declare function isMissingTableError(err: unknown): boolean;
21
+ /**
22
+ * Does this error mean the (email, type) pair is already on the list?
23
+ * Exported for direct unit coverage across dialects.
24
+ *
25
+ * Suppressing is idempotent, and each database says "already there" in its
26
+ * own words. Matching only SQLite's and MySQL's made a second bounce for the
27
+ * same address throw on Postgres (`duplicate key value violates unique
28
+ * constraint`, SQLSTATE 23505), out of a webhook handler that had every
29
+ * reason to expect a no-op.
30
+ */
31
+ export declare function isDuplicateSuppressionError(err: unknown): boolean;
@@ -0,0 +1 @@
1
+ export function isMissingTableError(err){const e=err;const msg=e?.message??"";return e?.code==="42P01"||msg.includes("no such table")||msg.includes("doesn't exist")||/relation "[^"]*" does not exist/i.test(msg)}export function isDuplicateSuppressionError(err){const e=err;const msg=e?.message??"";return e?.code==="23505"||e?.errno===1062||msg.includes("UNIQUE constraint")||msg.includes("Duplicate entry")||msg.includes("duplicate key value violates unique constraint")}
@@ -1,16 +1,4 @@
1
- /**
2
- * Does this error mean the backing table simply hasn't been migrated yet?
3
- * Exported for direct unit coverage across dialects.
4
- *
5
- * Each supported database phrases "missing table" differently, and the
6
- * suppression layer is fail-open — so a matcher that only knows sqlite/mysql
7
- * would let Postgres's wording slip through and hard-fail every send on an
8
- * un-migrated Postgres DB (stacksjs/stacks#1976). We scope the Postgres check
9
- * to `undefined_table` specifically (SQLSTATE 42P01 / `relation "..." does not
10
- * exist`) rather than a bare `does not exist`, so a genuine `column ... does
11
- * not exist` schema bug still surfaces instead of being silently swallowed.
12
- */
13
- export declare function isMissingTableError(err: unknown): boolean;
1
+ import { isDuplicateSuppressionError, isMissingTableError } from './suppression-errors';
14
2
  /**
15
3
  * Is the address suppressed? Pass a specific `type` to check only
16
4
  * one kind (e.g. unsubscribe-only — bounces from the same address
@@ -80,3 +68,4 @@ export type SuppressionType = 'bounce' | 'complaint' | 'unsubscribe' | 'manual';
80
68
  * missing.
81
69
  */
82
70
  export type SuppressionPolicy = 'strict' | 'transactional-allowed' | 'off';
71
+ export { isDuplicateSuppressionError, isMissingTableError };
@@ -1 +1 @@
1
- import{db}from"@stacksjs/database/runtime";let warnedAboutMissingTable=false;function warnOnceAboutMissingTable(){if(warnedAboutMissingTable)return;warnedAboutMissingTable=true;console.warn("[email/suppression] email_suppressions table missing - suppression checks accepted but NOT enforced. "+"Run migrations to enable enforcement.")}export function isMissingTableError(err){const e=err;const msg=e?.message??"";return e?.code==="42P01"||msg.includes("no such table")||msg.includes("doesn't exist")||/relation "[^"]*" does not exist/i.test(msg)}function isSuppressionStoreUnavailable(err){if(isMissingTableError(err))return true;const msg=(err?.message??"").toLowerCase();return msg.includes("connection closed")||msg.includes("unable to open database")||msg.includes("econnrefused")||msg.includes("connection terminated")||msg.includes("no database")||msg.includes("database connection")}function canonicalize(email){return String(email).trim().toLowerCase()}export async function isSuppressed(email,type){const canon=canonicalize(email);try{let query=db.selectFrom("email_suppressions").where("email","=",canon).select(["email"]);if(type)query=query.where("type","=",type);const row=await query.executeTakeFirst();return Boolean(row)}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return false}throw err}}export async function getSuppressions(email){const canon=canonicalize(email);try{const rows=await db.selectFrom("email_suppressions").where("email","=",canon).selectAll().execute();return rows??[]}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return[]}throw err}}export async function suppress(email,type,reason){const canon=canonicalize(email);const createdAt=new Date().toISOString().slice(0,19).replace("T"," ");try{await db.insertInto("email_suppressions").values({email:canon,type,reason:reason??null,created_at:createdAt}).execute()}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return}const msg=err?.message??"";if(msg.includes("UNIQUE constraint")||msg.includes("Duplicate entry"))return;throw err}}export async function unsuppress(email,type){const canon=canonicalize(email);try{await db.deleteFrom("email_suppressions").where("email","=",canon).where("type","=",type).execute()}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return}throw err}}export async function getSuppressionPolicy(){try{const{config}=await import("@stacksjs/config");const policy=config?.email?.suppressionPolicy;if(policy==="strict"||policy==="transactional-allowed"||policy==="off")return policy}catch{}return"strict"}export async function checkSuppressionFor(email,tag){const policy=await getSuppressionPolicy();if(policy==="off")return null;if(policy==="transactional-allowed"&&tag==="transactional")return null;const suppressions=await getSuppressions(email);if(suppressions.length===0)return null;const priority=["unsubscribe","complaint","bounce","manual"];for(const p of priority){if(suppressions.some((s)=>s.type===p))return p}return suppressions[0].type}
1
+ import{db}from"@stacksjs/database/runtime";import{isDuplicateSuppressionError,isMissingTableError}from"./suppression-errors";export{isDuplicateSuppressionError,isMissingTableError};let warnedAboutMissingTable=false;function warnOnceAboutMissingTable(){if(warnedAboutMissingTable)return;warnedAboutMissingTable=true;console.warn("[email/suppression] The email_suppressions table is missing, so bounces, complaints and unsubscribes are NOT honoured: every address is sent to. "+"Publish the model (`buddy publish model EmailSuppression`) and run `buddy migrate`, or add its table in a migration of your own "+"(email, type, reason, created_at, updated_at, unique on email and type).")}function isSuppressionStoreUnavailable(err){if(isMissingTableError(err))return true;const msg=(err?.message??"").toLowerCase();return msg.includes("connection closed")||msg.includes("unable to open database")||msg.includes("econnrefused")||msg.includes("connection terminated")||msg.includes("no database")||msg.includes("database connection")}function canonicalize(email){return String(email).trim().toLowerCase()}export async function isSuppressed(email,type){const canon=canonicalize(email);try{let query=db.selectFrom("email_suppressions").where("email","=",canon).select(["email"]);if(type)query=query.where("type","=",type);const row=await query.executeTakeFirst();return Boolean(row)}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return false}throw err}}export async function getSuppressions(email){const canon=canonicalize(email);try{const rows=await db.selectFrom("email_suppressions").where("email","=",canon).selectAll().execute();return rows??[]}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return[]}throw err}}export async function suppress(email,type,reason){const canon=canonicalize(email);const createdAt=new Date().toISOString().slice(0,19).replace("T"," ");try{await db.insertInto("email_suppressions").values({email:canon,type,reason:reason??null,created_at:createdAt}).execute()}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return}if(isDuplicateSuppressionError(err))return;throw err}}export async function unsuppress(email,type){const canon=canonicalize(email);try{await db.deleteFrom("email_suppressions").where("email","=",canon).where("type","=",type).execute()}catch(err){if(isSuppressionStoreUnavailable(err)){warnOnceAboutMissingTable();return}throw err}}export async function getSuppressionPolicy(){try{const{config}=await import("@stacksjs/config");const policy=config?.email?.suppressionPolicy;if(policy==="strict"||policy==="transactional-allowed"||policy==="off")return policy}catch{}return"strict"}export async function checkSuppressionFor(email,tag){const policy=await getSuppressionPolicy();if(policy==="off")return null;if(policy==="transactional-allowed"&&tag==="transactional")return null;const suppressions=await getSuppressions(email);if(suppressions.length===0)return null;const priority=["unsubscribe","complaint","bounce","manual"];for(const p of priority){if(suppressions.some((s)=>s.type===p))return p}return suppressions[0].type}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/email",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.600",
5
+ "version": "0.75.602",
6
6
  "description": "The Stacks Email integration. Painlessly create & manage your inboxes, templates, and send emails.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -61,26 +61,26 @@
61
61
  "prepublishOnly": "bun run build"
62
62
  },
63
63
  "dependencies": {
64
- "@stacksjs/config": "0.75.600",
65
- "@stacksjs/database": "0.75.600",
66
- "@stacksjs/events": "0.75.600",
67
- "@stacksjs/logging": "0.75.600",
64
+ "@stacksjs/config": "0.75.602",
65
+ "@stacksjs/database": "0.75.602",
66
+ "@stacksjs/events": "0.75.602",
67
+ "@stacksjs/logging": "0.75.602",
68
68
  "@stacksjs/mail": "^0.3.11",
69
- "@stacksjs/path": "0.75.600",
70
- "@stacksjs/storage": "0.75.600",
71
- "@stacksjs/strings": "0.75.600",
69
+ "@stacksjs/path": "0.75.602",
70
+ "@stacksjs/storage": "0.75.602",
71
+ "@stacksjs/strings": "0.75.602",
72
72
  "@stacksjs/stx": "^0.2.889",
73
73
  "@stacksjs/ts-cloud": "^0.16.135",
74
- "@stacksjs/utils": "0.75.600"
74
+ "@stacksjs/utils": "0.75.602"
75
75
  },
76
76
  "devDependencies": {
77
- "@stacksjs/cli": "0.75.600",
78
- "@stacksjs/error-handling": "0.75.600",
79
- "@stacksjs/types": "0.75.600",
77
+ "@stacksjs/cli": "0.75.602",
78
+ "@stacksjs/error-handling": "0.75.602",
79
+ "@stacksjs/types": "0.75.602",
80
80
  "better-dx": "^0.2.39"
81
81
  },
82
82
  "peerDependencies": {
83
- "@stacksjs/queue": "0.75.600"
83
+ "@stacksjs/queue": "0.75.602"
84
84
  },
85
85
  "peerDependenciesMeta": {
86
86
  "@stacksjs/queue": {