@getstrata/core 2.2.48 → 2.2.49

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.
@@ -85,30 +85,7 @@ function sdkClientClassName() {
85
85
  return fromName ? `${fromName}Client` : "AppClient";
86
86
  }
87
87
 
88
- // ../../src/core/mail/mailer.ts
89
- function resolveSmtpConfig() {
90
- const host = process.env.MAIL_HOST?.trim();
91
- if (!host) {
92
- throw new Error('MAIL_DRIVER="smtp" requires MAIL_HOST to be set.');
93
- }
94
- const from = process.env.MAIL_FROM?.trim();
95
- if (!from) {
96
- throw new Error('MAIL_DRIVER="smtp" requires MAIL_FROM to be set.');
97
- }
98
- const port = Number(process.env.MAIL_PORT ?? "587");
99
- if (!Number.isInteger(port) || port <= 0 || port > 65535) {
100
- throw new Error('Environment variable "MAIL_PORT" must be a positive integer.');
101
- }
102
- return {
103
- host,
104
- port,
105
- from,
106
- secure: (process.env.MAIL_SECURE ?? "false") === "true",
107
- ...process.env.MAIL_TIMEOUT_MS !== undefined ? { timeoutMs: smtpTimeout(Number(process.env.MAIL_TIMEOUT_MS)) } : {},
108
- ...process.env.MAIL_USERNAME?.trim() ? { username: process.env.MAIL_USERNAME.trim() } : {},
109
- ...process.env.MAIL_PASSWORD?.trim() ? { password: process.env.MAIL_PASSWORD.trim() } : {}
110
- };
111
- }
88
+ // ../../src/core/mail/smtpMessage.ts
112
89
  var EMAIL_SHAPE = /^[^\s@\r\n]+@[^\s@\r\n]+\.[^\s@\r\n]+$/u;
113
90
  function assertSafeSmtpField(value, field) {
114
91
  if (/[\r\n]/u.test(value)) {
@@ -134,12 +111,111 @@ function assertSafeSmtpAddress(value, field) {
134
111
  smtpEnvelopeAddress(value, field);
135
112
  return assertSafeSmtpField(value, field).trim();
136
113
  }
137
- function redactMailBody(body) {
138
- return body.replace(/https?:\/\/[^\s]+/giu, "[redacted-url]").replace(/[?&](token|signature|expires|code)=[^&\s]+/giu, "[redacted-query]");
139
- }
140
114
  function encodeBase64(value) {
141
115
  return Buffer.from(value, "utf8").toString("base64");
142
116
  }
117
+ function encodeSmtpHeader(value) {
118
+ if (/^[\x20-\x7e]{0,60}$/u.test(value))
119
+ return value;
120
+ const words = [];
121
+ let part = "";
122
+ for (const character of value) {
123
+ if (Buffer.byteLength(part + character, "utf8") > 45) {
124
+ words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
125
+ part = "";
126
+ }
127
+ part += character;
128
+ }
129
+ if (part)
130
+ words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
131
+ return words.join(`\r
132
+ `);
133
+ }
134
+ function smtpAddressHeader(value) {
135
+ if (!value.includes("<"))
136
+ return value;
137
+ const name = value.slice(0, value.lastIndexOf("<")).trim();
138
+ const address = smtpEnvelopeAddress(value, "address");
139
+ if (!name)
140
+ return address;
141
+ const encoded = encodeSmtpHeader(name);
142
+ const display = encoded.startsWith("=?UTF-8?B?") ? encoded : `"${encoded.replace(/[\\"]/gu, "\\$&")}"`;
143
+ return `${display} <${address}>`;
144
+ }
145
+ function smtpMimePart(type, value) {
146
+ const normalized = value.replace(/\r\n|\r|\n/gu, `\r
147
+ `);
148
+ const base64 = /[^\t\r\n\x20-\x7e]/u.test(normalized) || normalized.split(`\r
149
+ `).some((line) => line.length > 998);
150
+ return [
151
+ `Content-Type: ${type}; charset=utf-8`,
152
+ `Content-Transfer-Encoding: ${base64 ? "base64" : "7bit"}`,
153
+ "",
154
+ base64 ? (encodeBase64(normalized).match(/.{1,76}/gu) ?? []).join(`\r
155
+ `) : normalized
156
+ ];
157
+ }
158
+ function buildSmtpPayload(from, message) {
159
+ const headers = [
160
+ `From: ${smtpAddressHeader(assertSafeSmtpAddress(from, "from"))}`,
161
+ `To: ${smtpAddressHeader(assertSafeSmtpAddress(message.to, "to"))}`,
162
+ `Subject: ${encodeSmtpHeader(assertSafeSmtpField(message.subject, "subject"))}`,
163
+ "MIME-Version: 1.0"
164
+ ];
165
+ let lines;
166
+ if (message.html) {
167
+ let boundary;
168
+ do {
169
+ boundary = `strata-${crypto.randomUUID()}`;
170
+ } while (message.body.includes(boundary) || message.html.includes(boundary));
171
+ lines = [
172
+ ...headers,
173
+ `Content-Type: multipart/alternative; boundary="${boundary}"`,
174
+ "",
175
+ `--${boundary}`,
176
+ ...smtpMimePart("text/plain", message.body),
177
+ `--${boundary}`,
178
+ ...smtpMimePart("text/html", message.html),
179
+ `--${boundary}--`,
180
+ ""
181
+ ];
182
+ } else
183
+ lines = [...headers, ...smtpMimePart("text/plain", message.body)];
184
+ const content = lines.join(`\r
185
+ `).replace(/(^|\r\n)\./gu, "$1..");
186
+ return `${content}${content.endsWith(`\r
187
+ `) ? "" : `\r
188
+ `}.\r
189
+ `;
190
+ }
191
+
192
+ // ../../src/core/mail/mailer.ts
193
+ function resolveSmtpConfig() {
194
+ const host = process.env.MAIL_HOST?.trim();
195
+ if (!host) {
196
+ throw new Error('MAIL_DRIVER="smtp" requires MAIL_HOST to be set.');
197
+ }
198
+ const from = process.env.MAIL_FROM?.trim();
199
+ if (!from) {
200
+ throw new Error('MAIL_DRIVER="smtp" requires MAIL_FROM to be set.');
201
+ }
202
+ const port = Number(process.env.MAIL_PORT ?? "587");
203
+ if (!Number.isInteger(port) || port <= 0 || port > 65535) {
204
+ throw new Error('Environment variable "MAIL_PORT" must be a positive integer.');
205
+ }
206
+ return {
207
+ host,
208
+ port,
209
+ from,
210
+ secure: (process.env.MAIL_SECURE ?? "false") === "true",
211
+ ...process.env.MAIL_TIMEOUT_MS !== undefined ? { timeoutMs: smtpTimeout(Number(process.env.MAIL_TIMEOUT_MS)) } : {},
212
+ ...process.env.MAIL_USERNAME?.trim() ? { username: process.env.MAIL_USERNAME.trim() } : {},
213
+ ...process.env.MAIL_PASSWORD?.trim() ? { password: process.env.MAIL_PASSWORD.trim() } : {}
214
+ };
215
+ }
216
+ function redactMailBody(body) {
217
+ return body.replace(/https?:\/\/[^\s]+/giu, "[redacted-url]").replace(/[?&](token|signature|expires|code)=[^&\s]+/giu, "[redacted-query]");
218
+ }
143
219
  function smtpTimeout(value = 1e4) {
144
220
  if (!Number.isSafeInteger(value) || value < 1 || value > 2147483647) {
145
221
  throw new RangeError("SMTP timeoutMs must be a positive integer within the timer range.");
@@ -368,80 +444,6 @@ async function defaultSmtpTransport(config, message) {
368
444
  socket?.end();
369
445
  }
370
446
  }
371
- function encodeSmtpHeader(value) {
372
- if (/^[\x20-\x7e]{0,60}$/u.test(value))
373
- return value;
374
- const words = [];
375
- let part = "";
376
- for (const character of value) {
377
- if (Buffer.byteLength(part + character, "utf8") > 45) {
378
- words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
379
- part = "";
380
- }
381
- part += character;
382
- }
383
- if (part)
384
- words.push(`=?UTF-8?B?${encodeBase64(part)}?=`);
385
- return words.join(`\r
386
- `);
387
- }
388
- function smtpAddressHeader(value) {
389
- if (!value.includes("<"))
390
- return value;
391
- const name = value.slice(0, value.lastIndexOf("<")).trim();
392
- const address = smtpEnvelopeAddress(value, "address");
393
- if (!name)
394
- return address;
395
- const encoded = encodeSmtpHeader(name);
396
- const display = encoded.startsWith("=?UTF-8?B?") ? encoded : `"${encoded.replace(/[\\"]/gu, "\\$&")}"`;
397
- return `${display} <${address}>`;
398
- }
399
- function smtpMimePart(type, value) {
400
- const normalized = value.replace(/\r\n|\r|\n/gu, `\r
401
- `);
402
- const base64 = /[^\t\r\n\x20-\x7e]/u.test(normalized) || normalized.split(`\r
403
- `).some((line) => line.length > 998);
404
- return [
405
- `Content-Type: ${type}; charset=utf-8`,
406
- `Content-Transfer-Encoding: ${base64 ? "base64" : "7bit"}`,
407
- "",
408
- base64 ? (encodeBase64(normalized).match(/.{1,76}/gu) ?? []).join(`\r
409
- `) : normalized
410
- ];
411
- }
412
- function buildSmtpPayload(from, message) {
413
- const headers = [
414
- `From: ${smtpAddressHeader(assertSafeSmtpAddress(from, "from"))}`,
415
- `To: ${smtpAddressHeader(assertSafeSmtpAddress(message.to, "to"))}`,
416
- `Subject: ${encodeSmtpHeader(assertSafeSmtpField(message.subject, "subject"))}`,
417
- "MIME-Version: 1.0"
418
- ];
419
- let lines;
420
- if (message.html) {
421
- let boundary;
422
- do {
423
- boundary = `strata-${crypto.randomUUID()}`;
424
- } while (message.body.includes(boundary) || message.html.includes(boundary));
425
- lines = [
426
- ...headers,
427
- `Content-Type: multipart/alternative; boundary="${boundary}"`,
428
- "",
429
- `--${boundary}`,
430
- ...smtpMimePart("text/plain", message.body),
431
- `--${boundary}`,
432
- ...smtpMimePart("text/html", message.html),
433
- `--${boundary}--`,
434
- ""
435
- ];
436
- } else
437
- lines = [...headers, ...smtpMimePart("text/plain", message.body)];
438
- const content = lines.join(`\r
439
- `).replace(/(^|\r\n)\./gu, "$1..");
440
- return `${content}${content.endsWith(`\r
441
- `) ? "" : `\r
442
- `}.\r
443
- `;
444
- }
445
447
 
446
448
  class LogMailDriver {
447
449
  async send(message) {