@fedify/cfworkers 2.4.0-dev.1666 → 2.4.0-dev.1727

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.
Files changed (2) hide show
  1. package/dist/mod.js +36 -7
  2. package/package.json +2 -2
package/dist/mod.js CHANGED
@@ -1,5 +1,11 @@
1
1
  //#region src/mod.ts
2
2
  /**
3
+ * enqueueMany Limit settings.
4
+ */
5
+ const MAX_BATCH_MESSAGES = 100;
6
+ const MAX_ESTIMATED_BATCH_BYTES = 24e4;
7
+ const ESTIMATED_METADATA_BYTES_PER_MESSAGE = 128;
8
+ /**
3
9
  * Implementation of the {@link KvStore} interface for Cloudflare Workers KV
4
10
  * binding. This class provides a wrapper around Cloudflare's KV namespace to
5
11
  * store and retrieve JSON-serializable values using structured keys.
@@ -124,14 +130,37 @@ var WorkersMessageQueue = class {
124
130
  });
125
131
  }
126
132
  async enqueueMany(messages, options) {
127
- const requests = messages.map((msg) => ({
128
- body: {
133
+ const utf8Encoder = new TextEncoder();
134
+ const estimateMessageBytes = (body) => {
135
+ const serialized = JSON.stringify(body);
136
+ if (serialized === void 0) throw new TypeError("Queue message must be JSON-serializable.");
137
+ return utf8Encoder.encode(serialized).byteLength + ESTIMATED_METADATA_BYTES_PER_MESSAGE;
138
+ };
139
+ const delaySeconds = options?.delay?.total("seconds") ?? 0;
140
+ let batch = [];
141
+ let estimatedBatchBytes = 0;
142
+ const flush = async () => {
143
+ if (batch.length === 0) return;
144
+ await this.#queue.sendBatch(batch, { delaySeconds });
145
+ batch = [];
146
+ estimatedBatchBytes = 0;
147
+ };
148
+ for (const message of messages) {
149
+ const body = {
129
150
  __fedify_ordering_key__: options?.orderingKey,
130
- __fedify_payload__: msg
131
- },
132
- contentType: "json"
133
- }));
134
- await this.#queue.sendBatch(requests, { delaySeconds: options?.delay?.total("seconds") ?? 0 });
151
+ __fedify_payload__: message
152
+ };
153
+ const request = {
154
+ body,
155
+ contentType: "json"
156
+ };
157
+ const messageBytes = estimateMessageBytes(body);
158
+ const exceedsBatchLimit = batch.length >= MAX_BATCH_MESSAGES || estimatedBatchBytes + messageBytes > MAX_ESTIMATED_BATCH_BYTES;
159
+ if (batch.length > 0 && exceedsBatchLimit) await flush();
160
+ batch.push(request);
161
+ estimatedBatchBytes += messageBytes;
162
+ }
163
+ await flush();
135
164
  }
136
165
  /**
137
166
  * Processes a message from the queue, handling ordering key locks.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/cfworkers",
3
- "version": "2.4.0-dev.1666+690d9b28",
3
+ "version": "2.4.0-dev.1727+1eadcb04",
4
4
  "description": "Adapt Fedify with Cloudflare Workers",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -49,7 +49,7 @@
49
49
  ],
50
50
  "peerDependencies": {
51
51
  "@cloudflare/workers-types": "^4.20260511.1",
52
- "@fedify/fedify": "^2.4.0-dev.1666+690d9b28"
52
+ "@fedify/fedify": "^2.4.0-dev.1727+1eadcb04"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@cloudflare/vitest-pool-workers": "^0.8.31",