@ryusenkei/adapter-cloudflare 0.1.0 → 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/README.md +21 -24
- package/dist/index.d.ts +1 -2
- package/dist/index.js +0 -1
- package/dist/rate-limit.d.ts +5 -1
- package/package.json +6 -7
- package/dist/r2.d.ts +0 -33
- package/dist/r2.js +0 -41
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ryusenkei/adapter-cloudflare
|
|
2
2
|
|
|
3
|
-
|
|
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
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -11,33 +11,30 @@ pnpm add @ryusenkei/adapter-cloudflare
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { cloudflareMailer, cloudflareRateLimiter
|
|
14
|
+
import { cloudflareMailer, cloudflareRateLimiter } from '@ryusenkei/adapter-cloudflare';
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
mailer: cloudflareMailer(env.EMAIL),
|
|
19
|
-
blob: r2BlobStore(env.UPLOADS),
|
|
20
|
-
});
|
|
16
|
+
const rateLimiter = cloudflareRateLimiter(env.FORM_LIMITER);
|
|
17
|
+
const mailer = cloudflareMailer(env.EMAIL);
|
|
21
18
|
```
|
|
22
19
|
|
|
23
|
-
| Function |
|
|
20
|
+
| Function | Returns | Binding |
|
|
24
21
|
| --- | --- | --- |
|
|
25
|
-
| `cloudflareRateLimiter` |
|
|
26
|
-
| `cloudflareMailer` |
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
- A
|
|
32
|
-
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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 |
|
|
41
38
|
|
|
42
39
|
## License
|
|
43
40
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
export { cloudflareMailer, type SendEmailBinding } from './mail.ts';
|
|
2
|
-
export {
|
|
3
|
-
export { cloudflareRateLimiter, type RateLimitBinding } from './rate-limit.ts';
|
|
2
|
+
export { cloudflareRateLimiter, type RateLimitBinding, type RateLimiter } from './rate-limit.ts';
|
package/dist/index.js
CHANGED
package/dist/rate-limit.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type
|
|
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
|
+
}>;
|
|
2
6
|
/** Workers の Rate Limiting の binding(使う部分だけの型。`@cloudflare/workers-types` に依存しない) */
|
|
3
7
|
export type RateLimitBinding = {
|
|
4
8
|
limit(options: {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryusenkei/adapter-cloudflare",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Cloudflare Workers の binding による実装(回数の制限、メール送信)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "RYUSENKEI",
|
|
7
7
|
"repository": {
|
|
@@ -23,13 +23,12 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"publishConfig": {
|
|
26
|
-
"access": "public"
|
|
26
|
+
"access": "public",
|
|
27
|
+
"provenance": true
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@ryusenkei/core": "^0.
|
|
30
|
-
"@ryusenkei/
|
|
31
|
-
"@ryusenkei/mail": "^0.1.0",
|
|
32
|
-
"@ryusenkei/blob": "^0.1.0"
|
|
30
|
+
"@ryusenkei/core": "^0.2.0",
|
|
31
|
+
"@ryusenkei/mail": "^0.2.0"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"typescript": "^5.9.3"
|
package/dist/r2.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { BlobBody, BlobStore } from '@ryusenkei/blob';
|
|
2
|
-
type R2Object = {
|
|
3
|
-
size: number;
|
|
4
|
-
httpMetadata?: {
|
|
5
|
-
contentType?: string;
|
|
6
|
-
};
|
|
7
|
-
body: ReadableStream<Uint8Array>;
|
|
8
|
-
arrayBuffer(): Promise<ArrayBuffer>;
|
|
9
|
-
text(): Promise<string>;
|
|
10
|
-
};
|
|
11
|
-
/** R2 の bucket の binding(使う部分だけの型) */
|
|
12
|
-
export type R2BucketBinding = {
|
|
13
|
-
put(key: string, value: BlobBody, options?: {
|
|
14
|
-
httpMetadata?: {
|
|
15
|
-
contentType?: string;
|
|
16
|
-
};
|
|
17
|
-
}): Promise<unknown>;
|
|
18
|
-
get(key: string): Promise<R2Object | null>;
|
|
19
|
-
delete(key: string): Promise<void>;
|
|
20
|
-
list(options?: {
|
|
21
|
-
prefix?: string;
|
|
22
|
-
cursor?: string;
|
|
23
|
-
}): Promise<{
|
|
24
|
-
objects: {
|
|
25
|
-
key: string;
|
|
26
|
-
}[];
|
|
27
|
-
truncated: boolean;
|
|
28
|
-
cursor?: string;
|
|
29
|
-
}>;
|
|
30
|
-
};
|
|
31
|
-
/** R2 の bucket によるファイルの保存 */
|
|
32
|
-
export declare function r2BlobStore(bucket: R2BucketBinding | undefined): BlobStore;
|
|
33
|
-
export {};
|
package/dist/r2.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { ConfigError, err, ok, TransientError } from '@ryusenkei/core';
|
|
2
|
-
/** R2 の bucket によるファイルの保存 */
|
|
3
|
-
export function r2BlobStore(bucket) {
|
|
4
|
-
// R2 の失敗は再試行で回復しうるものとして扱う(binding の操作の例外は理由を区別できない)
|
|
5
|
-
const transient = async (op, run) => {
|
|
6
|
-
if (!bucket)
|
|
7
|
-
throw new ConfigError('R2 の binding が未設定');
|
|
8
|
-
try {
|
|
9
|
-
return await run(bucket);
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
throw new TransientError(`R2 の ${op} の失敗`, { cause: e });
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
return {
|
|
16
|
-
async put(key, body, options = {}) {
|
|
17
|
-
await transient('put', (b) => b.put(key, body, options.contentType ? { httpMetadata: { contentType: options.contentType } } : {}));
|
|
18
|
-
},
|
|
19
|
-
async get(key) {
|
|
20
|
-
const object = await transient('get', (b) => b.get(key));
|
|
21
|
-
if (!object)
|
|
22
|
-
return err('not_found');
|
|
23
|
-
const blob = { size: object.size, contentType: object.httpMetadata?.contentType, body: object.body, arrayBuffer: () => object.arrayBuffer(), text: () => object.text() };
|
|
24
|
-
return ok(blob);
|
|
25
|
-
},
|
|
26
|
-
async delete(key) {
|
|
27
|
-
await transient('delete', (b) => b.delete(key));
|
|
28
|
-
},
|
|
29
|
-
// 1回の応答は最大1,000件なので、cursor で続きを取得
|
|
30
|
-
async list(prefix) {
|
|
31
|
-
const keys = [];
|
|
32
|
-
let cursor;
|
|
33
|
-
do {
|
|
34
|
-
const page = await transient('list', (b) => b.list({ ...(prefix ? { prefix } : {}), ...(cursor ? { cursor } : {}) }));
|
|
35
|
-
keys.push(...page.objects.map((o) => o.key));
|
|
36
|
-
cursor = page.truncated ? page.cursor : undefined;
|
|
37
|
-
} while (cursor);
|
|
38
|
-
return keys.sort();
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
}
|