@ryusenkei/adapter-cloudflare 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 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,41 @@
1
- # Temporary Holding Version
1
+ # @ryusenkei/adapter-cloudflare
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Cloudflare Workers bindings as the rate limiter and mail sender that `@ryusenkei/form` uses. The binding types are structural, so this package does not depend on `@cloudflare/workers-types`.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @ryusenkei/adapter-cloudflare
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { cloudflareMailer, cloudflareRateLimiter } from '@ryusenkei/adapter-cloudflare';
15
+
16
+ const rateLimiter = cloudflareRateLimiter(env.FORM_LIMITER);
17
+ const mailer = cloudflareMailer(env.EMAIL);
18
+ ```
19
+
20
+ | Function | Returns | Binding |
21
+ | --- | --- | --- |
22
+ | `cloudflareRateLimiter` | `limit(key)` → `rate_limited` over the limit | Rate Limiting (`ratelimits` in wrangler config; limit and period are set there). Counts are per Cloudflare location, not a strict global limit |
23
+ | `cloudflareMailer` | a `Mailer` from `@ryusenkei/mail` | Email Service (`send_email`). The sender domain must be onboarded to Email Sending |
24
+
25
+ ## Errors
26
+
27
+ - A missing binding throws `ConfigError`, so a misconfigured deploy answers 503 `not_configured`.
28
+ - A Rate Limiting binding failure throws `TransientError`.
29
+ - The mailer runs `validateMail` first, then maps Email Service error codes:
30
+
31
+ | Codes | Result |
32
+ | --- | --- |
33
+ | `E_RATE_LIMIT_EXCEEDED`, `E_DAILY_LIMIT_EXCEEDED`, `E_INTERNAL_SERVER_ERROR` | throws `TransientError` |
34
+ | `E_SENDER_NOT_VERIFIED`, `E_SENDER_DOMAIN_NOT_AVAILABLE`, `E_ACCOUNT_SUSPENDED`, `E_RECIPIENT_NOT_ALLOWED` | throws `ConfigError` |
35
+ | `E_RECIPIENT_SUPPRESSED`, `E_DELIVERY_FAILED` | `rejected` |
36
+ | validation and header errors | `invalid_input` |
37
+ | anything else | rethrown as is |
38
+
39
+ ## License
40
+
41
+ MIT
@@ -0,0 +1,2 @@
1
+ export { cloudflareMailer, type SendEmailBinding } from './mail.ts';
2
+ export { cloudflareRateLimiter, type RateLimitBinding, type RateLimiter } from './rate-limit.ts';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { cloudflareMailer } from "./mail.js";
2
+ export { cloudflareRateLimiter } from "./rate-limit.js";
package/dist/mail.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { type MailAddress, type Mailer } from '@ryusenkei/mail';
2
+ /** Email Service の `send_email` binding(使う部分だけの型) */
3
+ export type SendEmailBinding = {
4
+ send(message: {
5
+ from: MailAddress;
6
+ to: MailAddress[];
7
+ replyTo?: MailAddress;
8
+ subject: string;
9
+ text: string;
10
+ html?: string;
11
+ }): Promise<{
12
+ messageId: string;
13
+ }>;
14
+ };
15
+ /**
16
+ * Email Service(Email Sending)の binding によるメール送信
17
+ * - 送信元のドメインは Email Sending に登録済みであること(未登録は `ConfigError`)
18
+ * - 知らないエラーコードはそのまま投げる(想定外の失敗として扱う)
19
+ */
20
+ export declare function cloudflareMailer(binding: SendEmailBinding | undefined): Mailer;
package/dist/mail.js ADDED
@@ -0,0 +1,41 @@
1
+ import { ConfigError, err, ok, TransientError } from '@ryusenkei/core';
2
+ import { validateMail } from '@ryusenkei/mail';
3
+ // binding のエラーの `code` の分類(Email Service の Workers API の一覧)
4
+ const TRANSIENT = ['E_RATE_LIMIT_EXCEEDED', 'E_DAILY_LIMIT_EXCEEDED', 'E_INTERNAL_SERVER_ERROR'];
5
+ const CONFIG = ['E_SENDER_NOT_VERIFIED', 'E_SENDER_DOMAIN_NOT_AVAILABLE', 'E_ACCOUNT_SUSPENDED', 'E_RECIPIENT_NOT_ALLOWED'];
6
+ const REJECTED = ['E_RECIPIENT_SUPPRESSED', 'E_DELIVERY_FAILED'];
7
+ const INVALID = /^E_(VALIDATION_ERROR|FIELD_MISSING|TOO_MANY_RECIPIENTS|TOO_MANY_ATTACHMENTS|CONTENT_TOO_LARGE|HEADERS?_.+)$/;
8
+ /**
9
+ * Email Service(Email Sending)の binding によるメール送信
10
+ * - 送信元のドメインは Email Sending に登録済みであること(未登録は `ConfigError`)
11
+ * - 知らないエラーコードはそのまま投げる(想定外の失敗として扱う)
12
+ */
13
+ export function cloudflareMailer(binding) {
14
+ return {
15
+ async send(mail) {
16
+ if (!binding)
17
+ throw new ConfigError('send_email の binding が未設定');
18
+ const valid = validateMail(mail);
19
+ if (!valid.ok)
20
+ return valid;
21
+ try {
22
+ const { messageId } = await binding.send({ from: mail.from, to: mail.to, ...(mail.replyTo ? { replyTo: mail.replyTo } : {}), subject: mail.subject, text: mail.text, ...(mail.html ? { html: mail.html } : {}) });
23
+ return ok({ id: messageId });
24
+ }
25
+ catch (e) {
26
+ const code = e.code;
27
+ if (typeof code !== 'string')
28
+ throw e;
29
+ if (TRANSIENT.includes(code))
30
+ throw new TransientError(`メール送信の一時的な失敗: ${code}`, { cause: e });
31
+ if (CONFIG.includes(code))
32
+ throw new ConfigError(`メール送信の設定の誤り: ${code}`, { cause: e });
33
+ if (REJECTED.includes(code))
34
+ return err('rejected');
35
+ if (INVALID.test(code))
36
+ return err('invalid_input');
37
+ throw e;
38
+ }
39
+ },
40
+ };
41
+ }
@@ -0,0 +1,19 @@
1
+ import { type Result } from '@ryusenkei/core';
2
+ /** キー(IP アドレスなど)ごとの回数の制限(`@ryusenkei/form/server` の `rateLimiter` に渡せる) */
3
+ export type RateLimiter = Readonly<{
4
+ limit(key: string): Promise<Result<void, 'rate_limited'>>;
5
+ }>;
6
+ /** Workers の Rate Limiting の binding(使う部分だけの型。`@cloudflare/workers-types` に依存しない) */
7
+ export type RateLimitBinding = {
8
+ limit(options: {
9
+ key: string;
10
+ }): Promise<{
11
+ success: boolean;
12
+ }>;
13
+ };
14
+ /**
15
+ * Workers の Rate Limiting の binding による回数の制限
16
+ * - 上限と期間は wrangler の設定(`ratelimits`)で決める
17
+ * - 数え方は Cloudflare の拠点ごとで、厳密な全体の上限ではない
18
+ */
19
+ export declare function cloudflareRateLimiter(binding: RateLimitBinding | undefined): RateLimiter;
@@ -0,0 +1,22 @@
1
+ import { ConfigError, err, ok, TransientError } from '@ryusenkei/core';
2
+ /**
3
+ * Workers の Rate Limiting の binding による回数の制限
4
+ * - 上限と期間は wrangler の設定(`ratelimits`)で決める
5
+ * - 数え方は Cloudflare の拠点ごとで、厳密な全体の上限ではない
6
+ */
7
+ export function cloudflareRateLimiter(binding) {
8
+ return {
9
+ async limit(key) {
10
+ if (!binding)
11
+ throw new ConfigError('Rate Limiting の binding が未設定');
12
+ let result;
13
+ try {
14
+ result = await binding.limit({ key });
15
+ }
16
+ catch (e) {
17
+ throw new TransientError('Rate Limiting の binding の失敗', { cause: e });
18
+ }
19
+ return result.success ? ok() : err('rate_limited');
20
+ },
21
+ };
22
+ }
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@ryusenkei/adapter-cloudflare",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.1",
4
+ "description": "Cloudflare Workers の binding による実装(回数の制限、メール送信)",
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/adapter-cloudflare"
11
+ },
12
+ "homepage": "https://github.com/ryusenkei-inc/toolkit/tree/main/packages/runtime/adapter-cloudflare",
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
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "publishConfig": {
26
+ "access": "public",
27
+ "provenance": true
28
+ },
29
+ "dependencies": {
30
+ "@ryusenkei/core": "^0.2.0",
31
+ "@ryusenkei/mail": "^0.2.0"
32
+ },
33
+ "devDependencies": {
34
+ "typescript": "^5.9.3"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.build.json",
38
+ "typecheck": "tsc --noEmit"
39
+ }
6
40
  }