@evumi/berth 0.2.0 → 0.2.2-rc.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 +11 -0
- package/dist/cjs/client.d.ts +34 -8
- package/dist/cjs/client.js +22 -2
- package/dist/cjs/index.d.ts +1 -1
- package/dist/esm/client.d.ts +34 -8
- package/dist/esm/client.js +22 -2
- package/dist/esm/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,6 +23,17 @@ const { ids } = await EMAILS.sendBatch([{ body: { n: 1 } }, { body: { n: 2 }, de
|
|
|
23
23
|
|
|
24
24
|
`contentType` 是 `json`(默认,任意 JSON 值)、`text`(字符串)或 `bytes`(`Uint8Array` / `ArrayBuffer`)。`send` 返回 `{ id, deduplicated }`。
|
|
25
25
|
|
|
26
|
+
项目的 KV 是 `regional`(每个地域一个 Valkey)时,消息默认存进**本实例所在地域**的单元,本地域的消费者收到。可以显式指定:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
await EMAILS.send(job, { region: 'sgp' }); // 发到新加坡的单元,由那里的消费者收到;地域名不在项目的单元里是 invalid_region
|
|
30
|
+
await EMAILS.send(job, { spill: true }); // 本地域单元不可达时改发下一个地域(默认关:消息会离开本地域,可能重复投递)
|
|
31
|
+
await EMAILS.sendBatch(jobs, { region: 'sgp' }); // 整批
|
|
32
|
+
const { messages } = await JOBS.pull({ region: 'sgp' }); // 从另一个单元拉取;leaseId 原样交给 ack
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`send` 的结果在地域化的项目里还有 `region`(实际存进的单元)和 `spilledFrom`。`dedupId` 只在同一个单元内去重。
|
|
36
|
+
|
|
26
37
|
自己建客户端:
|
|
27
38
|
|
|
28
39
|
```ts
|
package/dist/cjs/client.d.ts
CHANGED
|
@@ -1,22 +1,50 @@
|
|
|
1
1
|
import { type ContentType } from './body.js';
|
|
2
2
|
export type { ContentType } from './body.js';
|
|
3
|
-
|
|
3
|
+
/** What one message carries. */
|
|
4
|
+
export interface MessageOptions {
|
|
4
5
|
/** json (default; any JSON value), text (a string) or bytes (Uint8Array/ArrayBuffer). Bytes bodies pick bytes themselves. */
|
|
5
6
|
contentType?: ContentType;
|
|
6
7
|
/** Delay before the first delivery, 0–604800. */
|
|
7
8
|
delaySeconds?: number;
|
|
8
|
-
/** The same dedupId on the same queue within 24 h stores nothing and returns the first id. */
|
|
9
|
+
/** The same dedupId on the same queue within 24 h in the same region stores nothing and returns the first id. */
|
|
9
10
|
dedupId?: string;
|
|
10
11
|
}
|
|
12
|
+
/** Where a send goes, for a project whose KV store is regional (one Valkey per region). */
|
|
13
|
+
export interface RegionOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Store the message in this region's cell instead of the region this instance runs in. Only the
|
|
16
|
+
* regions of the project's KV store are accepted ("invalid_region" otherwise). Its consumers
|
|
17
|
+
* are the ones running in that region.
|
|
18
|
+
*/
|
|
19
|
+
region?: string;
|
|
20
|
+
/**
|
|
21
|
+
* When the instance's own region cannot take the message (its Valkey does not answer), store it
|
|
22
|
+
* in the next region instead. Off by default: the message leaves its region. Ignored when
|
|
23
|
+
* region is given. A message may then be delivered twice; consumers must be idempotent.
|
|
24
|
+
*/
|
|
25
|
+
spill?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface SendOptions extends MessageOptions, RegionOptions {
|
|
28
|
+
}
|
|
11
29
|
export interface SendResult {
|
|
12
30
|
id: string;
|
|
13
|
-
/** The dedupId was seen within 24 h: id is the first message's and nothing was stored. */
|
|
31
|
+
/** The dedupId was seen within 24 h in this region: id is the first message's and nothing was stored. */
|
|
14
32
|
deduplicated: boolean;
|
|
33
|
+
/** The region the message was stored in (regional projects only). */
|
|
34
|
+
region?: string;
|
|
35
|
+
/** Set when spill stored the message in region because this instance's own region could not. */
|
|
36
|
+
spilledFrom?: string;
|
|
15
37
|
}
|
|
16
|
-
export interface BatchMessage extends
|
|
38
|
+
export interface BatchMessage extends MessageOptions {
|
|
17
39
|
body: unknown;
|
|
18
40
|
}
|
|
41
|
+
export interface BatchOptions extends RegionOptions {
|
|
42
|
+
/** For messages without their own delaySeconds. */
|
|
43
|
+
delaySeconds?: number;
|
|
44
|
+
}
|
|
19
45
|
export interface PullOptions {
|
|
46
|
+
/** Take messages from this region's cell instead of the instance's own (regional projects only). Ack as usual: the leaseId remembers the region. */
|
|
47
|
+
region?: string;
|
|
20
48
|
/** 1–100, default 10. */
|
|
21
49
|
batchSize?: number;
|
|
22
50
|
/** A message not acknowledged within this is handed out again with a new leaseId. 1000–43200000, default 30000. */
|
|
@@ -52,10 +80,8 @@ export interface AckResult {
|
|
|
52
80
|
export interface Queue {
|
|
53
81
|
readonly name: string;
|
|
54
82
|
send(body: unknown, opts?: SendOptions): Promise<SendResult>;
|
|
55
|
-
/** Up to 100 messages; validation is all or nothing. opts.delaySeconds applies to messages without their own. */
|
|
56
|
-
sendBatch(messages: BatchMessage[], opts?: {
|
|
57
|
-
delaySeconds?: number;
|
|
58
|
-
}): Promise<{
|
|
83
|
+
/** Up to 100 messages to one region; validation is all or nothing. opts.delaySeconds applies to messages without their own. */
|
|
84
|
+
sendBatch(messages: BatchMessage[], opts?: BatchOptions): Promise<{
|
|
59
85
|
ids: string[];
|
|
60
86
|
items: SendResult[];
|
|
61
87
|
}>;
|
package/dist/cjs/client.js
CHANGED
|
@@ -13,6 +13,13 @@ function wireMessage(body, o) {
|
|
|
13
13
|
out.dedup_id = o.dedupId;
|
|
14
14
|
return out;
|
|
15
15
|
}
|
|
16
|
+
function wireRegion(out, o) {
|
|
17
|
+
if (o.region !== undefined && o.region !== '')
|
|
18
|
+
out.region = o.region;
|
|
19
|
+
if (o.spill)
|
|
20
|
+
out.spill = true;
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
16
23
|
/** Scheduled time from the wire: Unix ms, or an RFC 3339 string. */
|
|
17
24
|
function toMs(v) {
|
|
18
25
|
if (typeof v === 'number')
|
|
@@ -23,6 +30,15 @@ function toMs(v) {
|
|
|
23
30
|
return t;
|
|
24
31
|
}
|
|
25
32
|
const seg = (s) => encodeURIComponent(s);
|
|
33
|
+
/** A send answer from the wire: region and spilledFrom only when the project is regional. */
|
|
34
|
+
function sendResult(r) {
|
|
35
|
+
const out = { id: String(r.id), deduplicated: r.deduplicated === true };
|
|
36
|
+
if (typeof r.region === 'string' && r.region !== '')
|
|
37
|
+
out.region = r.region;
|
|
38
|
+
if (typeof r.spilled_from === 'string' && r.spilled_from !== '')
|
|
39
|
+
out.spilledFrom = r.spilled_from;
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
26
42
|
/** A client for queues and alarms. URL and token default to the environment, read lazily. */
|
|
27
43
|
function createClient(opts = {}) {
|
|
28
44
|
const timeoutMs = opts.timeoutMs ?? 10_000;
|
|
@@ -75,7 +91,8 @@ function createClient(opts = {}) {
|
|
|
75
91
|
return {
|
|
76
92
|
name,
|
|
77
93
|
async send(body, o = {}) {
|
|
78
|
-
|
|
94
|
+
const r = await request('POST', p, wireRegion(wireMessage(body, o), o));
|
|
95
|
+
return sendResult(r);
|
|
79
96
|
},
|
|
80
97
|
async sendBatch(messages, o = {}) {
|
|
81
98
|
const req = {
|
|
@@ -87,8 +104,9 @@ function createClient(opts = {}) {
|
|
|
87
104
|
};
|
|
88
105
|
if (o.delaySeconds !== undefined)
|
|
89
106
|
req.delay_seconds = o.delaySeconds;
|
|
107
|
+
wireRegion(req, o);
|
|
90
108
|
const r = await request('POST', `${p}/batch`, req);
|
|
91
|
-
const items = r?.items ?? [];
|
|
109
|
+
const items = (r?.items ?? []).map(sendResult);
|
|
92
110
|
return { ids: items.map((i) => i.id), items };
|
|
93
111
|
},
|
|
94
112
|
async pull(o = {}) {
|
|
@@ -97,6 +115,8 @@ function createClient(opts = {}) {
|
|
|
97
115
|
req.batch_size = o.batchSize;
|
|
98
116
|
if (o.visibilityTimeoutMs !== undefined)
|
|
99
117
|
req.visibility_timeout_ms = o.visibilityTimeoutMs;
|
|
118
|
+
if (o.region !== undefined && o.region !== '')
|
|
119
|
+
req.region = o.region;
|
|
100
120
|
const r = await request('POST', `${p}/pull`, req);
|
|
101
121
|
return {
|
|
102
122
|
messages: (r?.messages ?? []).map((m) => {
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { BerthError, type ErrorCode } from './errors.js';
|
|
2
|
-
export { type AckRequest, type AckResult, type Alarm, type BatchMessage, type Client, type ClientOptions, type ContentType, type FetchLike, type PulledMessage, type PullOptions, type Queue, type SendOptions, type SendResult, alarm, createClient, queue, } from './client.js';
|
|
2
|
+
export { type AckRequest, type AckResult, type Alarm, type BatchMessage, type BatchOptions, type Client, type ClientOptions, type ContentType, type FetchLike, type MessageOptions, type PulledMessage, type PullOptions, type Queue, type RegionOptions, type SendOptions, type SendResult, alarm, createClient, queue, } from './client.js';
|
|
3
3
|
export { type AlarmInfo, type Env, type ExecutionContext, type FetchHandler, type Handlers, type Message, type MessageBatch, type NodeRequest, type NodeResponse, type ScheduledController, createHandler, toNodeHandler, } from './handler.js';
|
|
4
4
|
export { type RawBody, type SignatureError, type VerifyOptions, checkSignature, verifySignature } from './signature.js';
|
package/dist/esm/client.d.ts
CHANGED
|
@@ -1,22 +1,50 @@
|
|
|
1
1
|
import { type ContentType } from './body.js';
|
|
2
2
|
export type { ContentType } from './body.js';
|
|
3
|
-
|
|
3
|
+
/** What one message carries. */
|
|
4
|
+
export interface MessageOptions {
|
|
4
5
|
/** json (default; any JSON value), text (a string) or bytes (Uint8Array/ArrayBuffer). Bytes bodies pick bytes themselves. */
|
|
5
6
|
contentType?: ContentType;
|
|
6
7
|
/** Delay before the first delivery, 0–604800. */
|
|
7
8
|
delaySeconds?: number;
|
|
8
|
-
/** The same dedupId on the same queue within 24 h stores nothing and returns the first id. */
|
|
9
|
+
/** The same dedupId on the same queue within 24 h in the same region stores nothing and returns the first id. */
|
|
9
10
|
dedupId?: string;
|
|
10
11
|
}
|
|
12
|
+
/** Where a send goes, for a project whose KV store is regional (one Valkey per region). */
|
|
13
|
+
export interface RegionOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Store the message in this region's cell instead of the region this instance runs in. Only the
|
|
16
|
+
* regions of the project's KV store are accepted ("invalid_region" otherwise). Its consumers
|
|
17
|
+
* are the ones running in that region.
|
|
18
|
+
*/
|
|
19
|
+
region?: string;
|
|
20
|
+
/**
|
|
21
|
+
* When the instance's own region cannot take the message (its Valkey does not answer), store it
|
|
22
|
+
* in the next region instead. Off by default: the message leaves its region. Ignored when
|
|
23
|
+
* region is given. A message may then be delivered twice; consumers must be idempotent.
|
|
24
|
+
*/
|
|
25
|
+
spill?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface SendOptions extends MessageOptions, RegionOptions {
|
|
28
|
+
}
|
|
11
29
|
export interface SendResult {
|
|
12
30
|
id: string;
|
|
13
|
-
/** The dedupId was seen within 24 h: id is the first message's and nothing was stored. */
|
|
31
|
+
/** The dedupId was seen within 24 h in this region: id is the first message's and nothing was stored. */
|
|
14
32
|
deduplicated: boolean;
|
|
33
|
+
/** The region the message was stored in (regional projects only). */
|
|
34
|
+
region?: string;
|
|
35
|
+
/** Set when spill stored the message in region because this instance's own region could not. */
|
|
36
|
+
spilledFrom?: string;
|
|
15
37
|
}
|
|
16
|
-
export interface BatchMessage extends
|
|
38
|
+
export interface BatchMessage extends MessageOptions {
|
|
17
39
|
body: unknown;
|
|
18
40
|
}
|
|
41
|
+
export interface BatchOptions extends RegionOptions {
|
|
42
|
+
/** For messages without their own delaySeconds. */
|
|
43
|
+
delaySeconds?: number;
|
|
44
|
+
}
|
|
19
45
|
export interface PullOptions {
|
|
46
|
+
/** Take messages from this region's cell instead of the instance's own (regional projects only). Ack as usual: the leaseId remembers the region. */
|
|
47
|
+
region?: string;
|
|
20
48
|
/** 1–100, default 10. */
|
|
21
49
|
batchSize?: number;
|
|
22
50
|
/** A message not acknowledged within this is handed out again with a new leaseId. 1000–43200000, default 30000. */
|
|
@@ -52,10 +80,8 @@ export interface AckResult {
|
|
|
52
80
|
export interface Queue {
|
|
53
81
|
readonly name: string;
|
|
54
82
|
send(body: unknown, opts?: SendOptions): Promise<SendResult>;
|
|
55
|
-
/** Up to 100 messages; validation is all or nothing. opts.delaySeconds applies to messages without their own. */
|
|
56
|
-
sendBatch(messages: BatchMessage[], opts?: {
|
|
57
|
-
delaySeconds?: number;
|
|
58
|
-
}): Promise<{
|
|
83
|
+
/** Up to 100 messages to one region; validation is all or nothing. opts.delaySeconds applies to messages without their own. */
|
|
84
|
+
sendBatch(messages: BatchMessage[], opts?: BatchOptions): Promise<{
|
|
59
85
|
ids: string[];
|
|
60
86
|
items: SendResult[];
|
|
61
87
|
}>;
|
package/dist/esm/client.js
CHANGED
|
@@ -9,6 +9,13 @@ function wireMessage(body, o) {
|
|
|
9
9
|
out.dedup_id = o.dedupId;
|
|
10
10
|
return out;
|
|
11
11
|
}
|
|
12
|
+
function wireRegion(out, o) {
|
|
13
|
+
if (o.region !== undefined && o.region !== '')
|
|
14
|
+
out.region = o.region;
|
|
15
|
+
if (o.spill)
|
|
16
|
+
out.spill = true;
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
12
19
|
/** Scheduled time from the wire: Unix ms, or an RFC 3339 string. */
|
|
13
20
|
function toMs(v) {
|
|
14
21
|
if (typeof v === 'number')
|
|
@@ -19,6 +26,15 @@ function toMs(v) {
|
|
|
19
26
|
return t;
|
|
20
27
|
}
|
|
21
28
|
const seg = (s) => encodeURIComponent(s);
|
|
29
|
+
/** A send answer from the wire: region and spilledFrom only when the project is regional. */
|
|
30
|
+
function sendResult(r) {
|
|
31
|
+
const out = { id: String(r.id), deduplicated: r.deduplicated === true };
|
|
32
|
+
if (typeof r.region === 'string' && r.region !== '')
|
|
33
|
+
out.region = r.region;
|
|
34
|
+
if (typeof r.spilled_from === 'string' && r.spilled_from !== '')
|
|
35
|
+
out.spilledFrom = r.spilled_from;
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
22
38
|
/** A client for queues and alarms. URL and token default to the environment, read lazily. */
|
|
23
39
|
export function createClient(opts = {}) {
|
|
24
40
|
const timeoutMs = opts.timeoutMs ?? 10_000;
|
|
@@ -71,7 +87,8 @@ export function createClient(opts = {}) {
|
|
|
71
87
|
return {
|
|
72
88
|
name,
|
|
73
89
|
async send(body, o = {}) {
|
|
74
|
-
|
|
90
|
+
const r = await request('POST', p, wireRegion(wireMessage(body, o), o));
|
|
91
|
+
return sendResult(r);
|
|
75
92
|
},
|
|
76
93
|
async sendBatch(messages, o = {}) {
|
|
77
94
|
const req = {
|
|
@@ -83,8 +100,9 @@ export function createClient(opts = {}) {
|
|
|
83
100
|
};
|
|
84
101
|
if (o.delaySeconds !== undefined)
|
|
85
102
|
req.delay_seconds = o.delaySeconds;
|
|
103
|
+
wireRegion(req, o);
|
|
86
104
|
const r = await request('POST', `${p}/batch`, req);
|
|
87
|
-
const items = r?.items ?? [];
|
|
105
|
+
const items = (r?.items ?? []).map(sendResult);
|
|
88
106
|
return { ids: items.map((i) => i.id), items };
|
|
89
107
|
},
|
|
90
108
|
async pull(o = {}) {
|
|
@@ -93,6 +111,8 @@ export function createClient(opts = {}) {
|
|
|
93
111
|
req.batch_size = o.batchSize;
|
|
94
112
|
if (o.visibilityTimeoutMs !== undefined)
|
|
95
113
|
req.visibility_timeout_ms = o.visibilityTimeoutMs;
|
|
114
|
+
if (o.region !== undefined && o.region !== '')
|
|
115
|
+
req.region = o.region;
|
|
96
116
|
const r = await request('POST', `${p}/pull`, req);
|
|
97
117
|
return {
|
|
98
118
|
messages: (r?.messages ?? []).map((m) => {
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { BerthError, type ErrorCode } from './errors.js';
|
|
2
|
-
export { type AckRequest, type AckResult, type Alarm, type BatchMessage, type Client, type ClientOptions, type ContentType, type FetchLike, type PulledMessage, type PullOptions, type Queue, type SendOptions, type SendResult, alarm, createClient, queue, } from './client.js';
|
|
2
|
+
export { type AckRequest, type AckResult, type Alarm, type BatchMessage, type BatchOptions, type Client, type ClientOptions, type ContentType, type FetchLike, type MessageOptions, type PulledMessage, type PullOptions, type Queue, type RegionOptions, type SendOptions, type SendResult, alarm, createClient, queue, } from './client.js';
|
|
3
3
|
export { type AlarmInfo, type Env, type ExecutionContext, type FetchHandler, type Handlers, type Message, type MessageBatch, type NodeRequest, type NodeResponse, type ScheduledController, createHandler, toNodeHandler, } from './handler.js';
|
|
4
4
|
export { type RawBody, type SignatureError, type VerifyOptions, checkSignature, verifySignature } from './signature.js';
|
package/package.json
CHANGED