@ryusenkei/mail 0.0.0-stage → 0.2.1
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 +35 -2
- package/dist/index.d.ts +28 -0
- package/dist/index.js +18 -0
- package/dist/testing.d.ts +5 -0
- package/dist/testing.js +16 -0
- package/package.json +40 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RYUSENKEI
|
|
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
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ryusenkei/mail
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The shared shape of an email (`Mail`, `MailAddress`), the `Mailer` interface, and the checks every sender runs before sending. Senders for a provider live elsewhere (for example, `cloudflareMailer` in `@ryusenkei/adapter-cloudflare`); `@ryusenkei/form` uses the interface for its mail destination.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @ryusenkei/mail
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Interface
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
type Mail = { from: MailAddress; to: MailAddress[]; replyTo?: MailAddress; subject: string; text: string; html?: string };
|
|
15
|
+
interface Mailer { send(mail: Mail): Promise<Result<{ id: string | undefined }, 'invalid_input' | 'rejected'>> }
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- `invalid_input`: a malformed address, no recipient, or a line break in a header field (blocks header injection).
|
|
19
|
+
- `rejected`: the provider would not deliver it (a suppressed recipient, a delivery failure).
|
|
20
|
+
- Infrastructure failures are thrown as `TransientError` or `ConfigError` from `@ryusenkei/core`.
|
|
21
|
+
|
|
22
|
+
## Checks
|
|
23
|
+
|
|
24
|
+
- `validateMail(mail)` is what every sender runs before sending. It rejects:
|
|
25
|
+
- CR, LF, NUL, U+0085, U+2028, and U+2029 in the subject or a display name
|
|
26
|
+
- addresses that fail `isEmailAddress`
|
|
27
|
+
- an empty `to`
|
|
28
|
+
- `isEmailAddress(value)` is deliberately loose (not full RFC 5322). It rejects whitespace and `< > ( ) " , ; :`. `@ryusenkei/form` uses the same check, so an address that passes the form is never rejected at send time.
|
|
29
|
+
|
|
30
|
+
## Testing
|
|
31
|
+
|
|
32
|
+
`memoryMailer()` from `@ryusenkei/mail/testing` validates, then keeps sent mail in `outbox`.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Result } from '@ryusenkei/core';
|
|
2
|
+
export type MailAddress = {
|
|
3
|
+
email: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
};
|
|
6
|
+
export type Mail = {
|
|
7
|
+
from: MailAddress;
|
|
8
|
+
to: MailAddress[];
|
|
9
|
+
replyTo?: MailAddress;
|
|
10
|
+
subject: string;
|
|
11
|
+
text: string;
|
|
12
|
+
html?: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* メール送信の業務の失敗
|
|
16
|
+
* - 基盤の失敗(送信の API の障害)は `TransientError`、送信元の未認証・binding の不足は `ConfigError` を投げる
|
|
17
|
+
*/
|
|
18
|
+
export type MailError = 'invalid_input' | 'rejected';
|
|
19
|
+
/** メールの送信 */
|
|
20
|
+
export interface Mailer {
|
|
21
|
+
send(mail: Mail): Promise<Result<{
|
|
22
|
+
id: string | undefined;
|
|
23
|
+
}, MailError>>;
|
|
24
|
+
}
|
|
25
|
+
/** メールアドレスとして送れる形か(フォームの検査もこれを使い、送信の時に弾かれないようにする) */
|
|
26
|
+
export declare const isEmailAddress: (value: string) => boolean;
|
|
27
|
+
/** 送信の前の入力の検査(実装は送る前にこれを通す) */
|
|
28
|
+
export declare function validateMail(mail: Mail): Result<void, 'invalid_input'>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { err, ok } from '@ryusenkei/core';
|
|
2
|
+
// `RFC 5322` の厳密な検査ではなく、明らかな誤りと改行・空白の混入だけを弾く
|
|
3
|
+
const EMAIL = /^[^\s@<>()",;:]+@[^\s@<>()",;:]+\.[^\s@<>()",;:]+$/;
|
|
4
|
+
// `U+0085`・`U+2028`・`U+2029` も、改行として扱うメールの処理系がある
|
|
5
|
+
const HEADER_BREAK = new RegExp(`[\\r\\n\\u0000${String.fromCharCode(0x85, 0x2028, 0x2029)}]`);
|
|
6
|
+
/** メールアドレスとして送れる形か(フォームの検査もこれを使い、送信の時に弾かれないようにする) */
|
|
7
|
+
export const isEmailAddress = (value) => EMAIL.test(value);
|
|
8
|
+
/** 送信の前の入力の検査(実装は送る前にこれを通す) */
|
|
9
|
+
export function validateMail(mail) {
|
|
10
|
+
const addresses = [mail.from, ...mail.to, ...(mail.replyTo ? [mail.replyTo] : [])];
|
|
11
|
+
if (!mail.to.length)
|
|
12
|
+
return err('invalid_input');
|
|
13
|
+
if (addresses.some((a) => !isEmailAddress(a.email) || (a.name !== undefined && HEADER_BREAK.test(a.name))))
|
|
14
|
+
return err('invalid_input');
|
|
15
|
+
if (HEADER_BREAK.test(mail.subject))
|
|
16
|
+
return err('invalid_input');
|
|
17
|
+
return ok();
|
|
18
|
+
}
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ok } from '@ryusenkei/core';
|
|
2
|
+
import { validateMail } from "./index.js";
|
|
3
|
+
/** 送ったメールを `outbox` に溜めるメモリの実装(テストと dev 用) */
|
|
4
|
+
export function memoryMailer() {
|
|
5
|
+
const outbox = [];
|
|
6
|
+
return {
|
|
7
|
+
outbox,
|
|
8
|
+
async send(mail) {
|
|
9
|
+
const valid = validateMail(mail);
|
|
10
|
+
if (!valid.ok)
|
|
11
|
+
return valid;
|
|
12
|
+
outbox.push(mail);
|
|
13
|
+
return ok({ id: `memory-${outbox.length}` });
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryusenkei/mail",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "メール送信の型と、宛先・ヘッダーの検査(メモリの実装付き)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "RYUSENKEI",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/ryusenkei-inc/toolkit.git",
|
|
10
|
+
"directory": "packages/runtime/mail"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/ryusenkei-inc/toolkit/tree/main/packages/runtime/mail",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./testing": {
|
|
22
|
+
"types": "./dist/testing.d.ts",
|
|
23
|
+
"default": "./dist/testing.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public",
|
|
31
|
+
"provenance": true
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@ryusenkei/core": "^0.2.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "^5.9.3"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.build.json",
|
|
41
|
+
"typecheck": "tsc --noEmit"
|
|
42
|
+
}
|
|
6
43
|
}
|