@wwchen/stack-notify 0.0.0 → 0.7.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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +92 -0
- package/package.json +34 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Will Chen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @wwchen/stack-notify
|
|
2
|
+
|
|
3
|
+
The one way an app sends mail. Production posts to Resend's HTTP API with `fetch`; everywhere else nothing is sent and the first URL in the text is appended to a sink file, so development and tests need no mail account and no network.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { mailerFromEnv } from "@wwchen/stack-notify";
|
|
7
|
+
|
|
8
|
+
const mailer = mailerFromEnv(env);
|
|
9
|
+
await mailer.send({ to, subject, text });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
| Export | Purpose |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `createMailer(options)` | a `Mailer` in `resend` or `sink` mode |
|
|
15
|
+
| `mailerFromEnv(env)` | Resend in production, the sink elsewhere |
|
|
16
|
+
| `countSinkLines(path)` | how many links the sink holds now |
|
|
17
|
+
| `waitForLink(path, afterLine, timeoutMs?)` | the first link after line `afterLine`, polling until the timeout |
|
|
18
|
+
|
|
19
|
+
`@wwchen/stack/mail` re-exports this package.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one way an app sends mail: every message goes through `Mailer`, so one switch decides whether it is delivered or only recorded.
|
|
3
|
+
*
|
|
4
|
+
* Production posts to Resend's HTTP API with `fetch` — no SDK, one request. Everywhere else nothing is sent: the first URL in the text is appended, one per line, to the sink file, which is what lets development, the tests and e2e sign in with no mail account and no network. The sink never holds the body or the address.
|
|
5
|
+
*/
|
|
6
|
+
export type Mail = {
|
|
7
|
+
to: string;
|
|
8
|
+
subject: string;
|
|
9
|
+
text: string;
|
|
10
|
+
};
|
|
11
|
+
export type Mailer = {
|
|
12
|
+
send(m: Mail): Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
export type MailerOptions = {
|
|
15
|
+
mode: "resend";
|
|
16
|
+
apiKey: string;
|
|
17
|
+
from: string;
|
|
18
|
+
fetch?: typeof fetch;
|
|
19
|
+
} | {
|
|
20
|
+
mode: "sink";
|
|
21
|
+
sinkPath: string;
|
|
22
|
+
};
|
|
23
|
+
export declare function createMailer(opts: MailerOptions): Mailer;
|
|
24
|
+
/**
|
|
25
|
+
* Production sends through Resend; every other environment uses the sink.
|
|
26
|
+
*
|
|
27
|
+
* A production run without `RESEND_API_KEY` or `EMAIL_FROM` gets a mailer that refuses every send, naming the missing variable, rather than a throw here: `next build` evaluates the app's mail module with `NODE_ENV=production` and no secrets (a build skips env validation), and must not fail on it. A booted production app never gets this far without them: the stack's env requires both in production.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mailerFromEnv(env: {
|
|
30
|
+
NODE_ENV: string;
|
|
31
|
+
RESEND_API_KEY?: string | undefined;
|
|
32
|
+
EMAIL_FROM?: string | undefined;
|
|
33
|
+
MAGIC_LINK_SINK_PATH: string;
|
|
34
|
+
}): Mailer;
|
|
35
|
+
/** How many links the sink holds now; take it before the request that should add one. */
|
|
36
|
+
export declare function countSinkLines(sinkPath: string): number;
|
|
37
|
+
/** The first link after line `afterLine` (a count from `countSinkLines`), polling until `timeoutMs`. */
|
|
38
|
+
export declare function waitForLink(sinkPath: string, afterLine: number, timeoutMs?: number): Promise<string>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, readFileSync } from "node:fs";
|
|
2
|
+
const RESEND_ENDPOINT = "https://api.resend.com/emails";
|
|
3
|
+
export function createMailer(opts) {
|
|
4
|
+
if (opts.mode === "sink") {
|
|
5
|
+
const { sinkPath } = opts;
|
|
6
|
+
return {
|
|
7
|
+
async send(m) {
|
|
8
|
+
const url = m.text.match(/https?:\/\/\S+/)?.[0];
|
|
9
|
+
if (url)
|
|
10
|
+
appendFileSync(sinkPath, `${url}\n`, "utf8");
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const { apiKey, from } = opts;
|
|
15
|
+
const doFetch = opts.fetch ?? fetch;
|
|
16
|
+
return {
|
|
17
|
+
async send(m) {
|
|
18
|
+
const response = await doFetch(RESEND_ENDPOINT, {
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: {
|
|
21
|
+
authorization: `Bearer ${apiKey}`,
|
|
22
|
+
"content-type": "application/json",
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify({ from, to: m.to, subject: m.subject, text: m.text }),
|
|
25
|
+
// A hung Resend must not hold the sign-in request open.
|
|
26
|
+
signal: AbortSignal.timeout(10_000),
|
|
27
|
+
});
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new Error(`Resend refused the message: ${response.status}${await refusalReason(response)}`);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Production sends through Resend; every other environment uses the sink.
|
|
36
|
+
*
|
|
37
|
+
* A production run without `RESEND_API_KEY` or `EMAIL_FROM` gets a mailer that refuses every send, naming the missing variable, rather than a throw here: `next build` evaluates the app's mail module with `NODE_ENV=production` and no secrets (a build skips env validation), and must not fail on it. A booted production app never gets this far without them: the stack's env requires both in production.
|
|
38
|
+
*/
|
|
39
|
+
export function mailerFromEnv(env) {
|
|
40
|
+
if (env.NODE_ENV !== "production") {
|
|
41
|
+
return createMailer({ mode: "sink", sinkPath: env.MAGIC_LINK_SINK_PATH });
|
|
42
|
+
}
|
|
43
|
+
const { RESEND_API_KEY: apiKey, EMAIL_FROM: from } = env;
|
|
44
|
+
if (!apiKey || !from) {
|
|
45
|
+
const missing = apiKey ? "EMAIL_FROM" : "RESEND_API_KEY";
|
|
46
|
+
return {
|
|
47
|
+
async send() {
|
|
48
|
+
throw new Error(`${missing} is required in production`);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return createMailer({ mode: "resend", apiKey, from });
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Resend's `{ name, message }` as " name: message", or "" for any other body. The caller logs it, and Resend's message can echo the recipient, so every email address in it is redacted.
|
|
56
|
+
*/
|
|
57
|
+
async function refusalReason(response) {
|
|
58
|
+
try {
|
|
59
|
+
const body = await response.json();
|
|
60
|
+
if (typeof body !== "object" || body === null)
|
|
61
|
+
return "";
|
|
62
|
+
const { name, message } = body;
|
|
63
|
+
if (typeof name !== "string" || typeof message !== "string")
|
|
64
|
+
return "";
|
|
65
|
+
const redacted = message.replace(/[^\s()<>@,;:"]+@[^\s()<>@,;:"]+/g, "[email]").slice(0, 300);
|
|
66
|
+
return ` ${name}: ${redacted}`;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return "";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function sinkLines(sinkPath) {
|
|
73
|
+
if (!existsSync(sinkPath))
|
|
74
|
+
return [];
|
|
75
|
+
return readFileSync(sinkPath, "utf8").split("\n").filter(Boolean);
|
|
76
|
+
}
|
|
77
|
+
/** How many links the sink holds now; take it before the request that should add one. */
|
|
78
|
+
export function countSinkLines(sinkPath) {
|
|
79
|
+
return sinkLines(sinkPath).length;
|
|
80
|
+
}
|
|
81
|
+
/** The first link after line `afterLine` (a count from `countSinkLines`), polling until `timeoutMs`. */
|
|
82
|
+
export async function waitForLink(sinkPath, afterLine, timeoutMs = 10_000) {
|
|
83
|
+
const deadline = Date.now() + timeoutMs;
|
|
84
|
+
for (;;) {
|
|
85
|
+
const line = sinkLines(sinkPath)[afterLine];
|
|
86
|
+
if (line)
|
|
87
|
+
return line;
|
|
88
|
+
if (Date.now() >= deadline)
|
|
89
|
+
throw new Error(`No link reached ${sinkPath}`);
|
|
90
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
91
|
+
}
|
|
92
|
+
}
|
package/package.json
CHANGED
|
@@ -1 +1,34 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@wwchen/stack-notify",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Send mail through Resend, or to a sink file in development and tests.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/wwchen/bootstrap-stack.git",
|
|
9
|
+
"directory": "packages/notify"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": "24.x"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^24.19.2",
|
|
26
|
+
"typescript": "~6.0.3",
|
|
27
|
+
"vitest": "5.0.5"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"typecheck": "tsc --noEmit"
|
|
33
|
+
}
|
|
34
|
+
}
|