appmesh 1.2.3 → 1.3.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 CHANGED
@@ -92,25 +92,35 @@ npm install msgpack-lite uuid
92
92
  #### Basic HTTP Server
93
93
 
94
94
  ```javascript
95
- import { AppMeshServer } from "appmesh/server";
95
+ import { AppMeshWorker, ProcessSupersededError } from "appmesh/server";
96
96
 
97
- const server = new AppMeshServer();
97
+ const server = new AppMeshWorker();
98
98
 
99
- // Fetch task from client
100
- const payload = await server.task_fetch();
101
- console.log("Received:", payload);
99
+ try {
100
+ // Fetch task from client
101
+ const payload = await server.fetch_task();
102
+ console.log("Received:", payload);
102
103
 
103
- // Process the task
104
- const result = processData(payload);
104
+ // Process the task
105
+ const result = processData(payload);
105
106
 
106
- // Return result to client
107
- await server.task_return(result);
107
+ // Return result to client
108
+ await server.send_task_result(result);
109
+ } catch (error) {
110
+ if (error instanceof ProcessSupersededError) {
111
+ // This process instance was replaced by the daemon (HTTP 412) — stop serving.
112
+ // Exiting is an app entry-point decision; the SDK only surfaces the error.
113
+ console.error(error.message);
114
+ process.exit(1);
115
+ }
116
+ throw error;
117
+ }
108
118
  ```
109
119
 
110
120
  #### With SSL Configuration
111
121
 
112
122
  ```javascript
113
- import { AppMeshServer } from "appmesh/server";
123
+ import { AppMeshWorker } from "appmesh/server";
114
124
  import fs from "fs";
115
125
 
116
126
  const sslConfig = {
@@ -120,7 +130,7 @@ const sslConfig = {
120
130
  rejectUnauthorized: true,
121
131
  };
122
132
 
123
- const server = new AppMeshServer("https://127.0.0.1:6060", sslConfig);
133
+ const server = new AppMeshWorker("https://127.0.0.1:6060", sslConfig);
124
134
  ```
125
135
 
126
136
  #### TCP Server (Better Performance)
@@ -128,7 +138,7 @@ const server = new AppMeshServer("https://127.0.0.1:6060", sslConfig);
128
138
  **Dependencies:** `npm install msgpack-lite uuid`
129
139
 
130
140
  ```javascript
131
- import { AppMeshServerTCP, withServer } from "appmesh/server";
141
+ import { AppMeshWorkerTCP, withServer } from "appmesh/server";
132
142
  import fs from "fs";
133
143
 
134
144
  const sslConfig = {
@@ -139,11 +149,11 @@ const sslConfig = {
139
149
 
140
150
  // Auto-cleanup with context manager
141
151
  await withServer(
142
- () => new AppMeshServerTCP(sslConfig, ["127.0.0.1", 6059]),
152
+ () => new AppMeshWorkerTCP(sslConfig, ["127.0.0.1", 6059]),
143
153
  async (server) => {
144
- const payload = await server.task_fetch();
154
+ const payload = await server.fetch_task();
145
155
  const result = processPayload(payload);
146
- await server.task_return(result);
156
+ await server.send_task_result(result);
147
157
  }
148
158
  );
149
159
  // Connection automatically closed
@@ -160,12 +170,12 @@ These are **required** and set automatically by the App Mesh service when runnin
160
170
 
161
171
  ### API Reference
162
172
 
163
- #### AppMeshServer
173
+ #### AppMeshWorker
164
174
 
165
175
  ##### Constructor
166
176
 
167
177
  ```javascript
168
- new AppMeshServer(baseURL, sslConfig, options);
178
+ new AppMeshWorker(baseURL, sslConfig, options);
169
179
  ```
170
180
 
171
181
  **Parameters:**
@@ -181,20 +191,22 @@ new AppMeshServer(baseURL, sslConfig, options);
181
191
 
182
192
  ##### Methods
183
193
 
184
- ###### `task_fetch()`
194
+ ###### `fetch_task()`
185
195
 
186
196
  Fetch task payload from the App Mesh service. Automatically retries on failure.
187
197
 
188
198
  **Returns:** `Promise<string|Buffer>` - The payload sent by the client
189
199
 
200
+ **Throws:** `ProcessSupersededError` if the daemon reports HTTP 412 (this process was superseded by a newer instance) - the caller should stop serving; exit at the app entry point if appropriate
201
+
190
202
  **Example:**
191
203
 
192
204
  ```javascript
193
- const payload = await server.task_fetch();
205
+ const payload = await server.fetch_task();
194
206
  const data = JSON.parse(payload);
195
207
  ```
196
208
 
197
- ###### `task_return(result)`
209
+ ###### `send_task_result(result)`
198
210
 
199
211
  Return processed result back to the client.
200
212
 
@@ -210,28 +222,28 @@ Return processed result back to the client.
210
222
 
211
223
  ```javascript
212
224
  const result = { status: "success", data: processedData };
213
- await server.task_return(JSON.stringify(result));
225
+ await server.send_task_result(JSON.stringify(result));
214
226
  ```
215
227
 
216
- #### AppMeshServerTCP
228
+ #### AppMeshWorkerTCP
217
229
 
218
230
  TCP-based server for better performance with large data transfers.
219
231
 
220
232
  ##### Constructor
221
233
 
222
234
  ```javascript
223
- new AppMeshServerTCP(sslConfig, tcpAddress, options);
235
+ new AppMeshWorkerTCP(sslConfig, tcpAddress, options);
224
236
  ```
225
237
 
226
238
  **Parameters:**
227
239
 
228
- - `sslConfig` (object, optional) - SSL configuration (same as AppMeshServer)
229
- - `tcpAddress` (array, optional) - `[host, port]`. Default: `['127.0.0.1', 6059]`
230
- - `options` (object, optional) - Same as AppMeshServer
240
+ - `sslConfig` (object, optional) - SSL configuration (same as AppMeshWorker)
241
+ - `tcpAddress` (array or object, optional) - `[host, port]` (legacy) or `{host, port}`. Default: `['127.0.0.1', 6059]`. The `{host, port}` object form is also accepted as the first argument: `new AppMeshWorkerTCP({host, port}, sslConfig)`
242
+ - `options` (object, optional) - Same as AppMeshWorker
231
243
 
232
244
  ##### Methods
233
245
 
234
- Same as `AppMeshServer`, plus:
246
+ Same as `AppMeshWorker`, plus:
235
247
 
236
248
  ###### `close()`
237
249
 
@@ -240,7 +252,7 @@ Close the TCP connection and release resources.
240
252
  **Example:**
241
253
 
242
254
  ```javascript
243
- const server = new AppMeshServerTCP();
255
+ const server = new AppMeshWorkerTCP();
244
256
  try {
245
257
  // Use server...
246
258
  } finally {
@@ -265,10 +277,10 @@ Context manager for automatic resource cleanup.
265
277
 
266
278
  ```javascript
267
279
  await withServer(
268
- () => new AppMeshServerTCP(),
280
+ () => new AppMeshWorkerTCP(),
269
281
  async (server) => {
270
- const payload = await server.task_fetch();
271
- await server.task_return(processData(payload));
282
+ const payload = await server.fetch_task();
283
+ await server.send_task_result(processData(payload));
272
284
  }
273
285
  );
274
286
  ```
@@ -278,12 +290,12 @@ await withServer(
278
290
  #### JSON Processing
279
291
 
280
292
  ```javascript
281
- import { AppMeshServer } from "appmesh/server";
293
+ import { AppMeshWorker } from "appmesh/server";
282
294
 
283
- const server = new AppMeshServer();
295
+ const server = new AppMeshWorker();
284
296
 
285
297
  try {
286
- const payload = await server.task_fetch();
298
+ const payload = await server.fetch_task();
287
299
  const data = JSON.parse(payload);
288
300
 
289
301
  // Process JSON data
@@ -293,9 +305,9 @@ try {
293
305
  timestamp: new Date().toISOString(),
294
306
  };
295
307
 
296
- await server.task_return(JSON.stringify(result));
308
+ await server.send_task_result(JSON.stringify(result));
297
309
  } catch (error) {
298
- await server.task_return(
310
+ await server.send_task_result(
299
311
  JSON.stringify({
300
312
  status: "error",
301
313
  message: error.message,
@@ -307,12 +319,12 @@ try {
307
319
  #### Binary Data Processing
308
320
 
309
321
  ```javascript
310
- import { AppMeshServer } from "appmesh/server";
322
+ import { AppMeshWorker } from "appmesh/server";
311
323
  import crypto from "crypto";
312
324
 
313
- const server = new AppMeshServer();
325
+ const server = new AppMeshWorker();
314
326
 
315
- const binaryPayload = await server.task_fetch();
327
+ const binaryPayload = await server.fetch_task();
316
328
  const hash = crypto.createHash("sha256");
317
329
  hash.update(binaryPayload);
318
330
 
@@ -321,17 +333,17 @@ const result = {
321
333
  checksum: hash.digest("hex"),
322
334
  };
323
335
 
324
- await server.task_return(JSON.stringify(result));
336
+ await server.send_task_result(JSON.stringify(result));
325
337
  ```
326
338
 
327
339
  #### Long-Running Task
328
340
 
329
341
  ```javascript
330
- import { AppMeshServer } from "appmesh/server";
342
+ import { AppMeshWorker } from "appmesh/server";
331
343
 
332
- const server = new AppMeshServer();
344
+ const server = new AppMeshWorker();
333
345
 
334
- const config = JSON.parse(await server.task_fetch());
346
+ const config = JSON.parse(await server.fetch_task());
335
347
  const results = [];
336
348
 
337
349
  for (let i = 0; i < config.steps; i++) {
@@ -341,7 +353,7 @@ for (let i = 0; i < config.steps; i++) {
341
353
  console.log(`Step ${i + 1}/${config.steps} completed`);
342
354
  }
343
355
 
344
- await server.task_return(
356
+ await server.send_task_result(
345
357
  JSON.stringify({
346
358
  status: "success",
347
359
  results: results,
@@ -375,11 +387,11 @@ console.log("Result:", result);
375
387
 
376
388
  ```javascript
377
389
  // Server side (running as App Mesh application)
378
- import { AppMeshServer } from "appmesh/server";
390
+ import { AppMeshWorker } from "appmesh/server";
379
391
 
380
- const server = new AppMeshServer();
392
+ const server = new AppMeshWorker();
381
393
 
382
- const payload = await server.task_fetch();
394
+ const payload = await server.fetch_task();
383
395
  const request = JSON.parse(payload);
384
396
 
385
397
  const result = {
@@ -387,5 +399,9 @@ const result = {
387
399
  sum: request.data.reduce((a, b) => a + b, 0),
388
400
  };
389
401
 
390
- await server.task_return(JSON.stringify(result));
402
+ await server.send_task_result(JSON.stringify(result));
391
403
  ```
404
+
405
+ ## Behavioral contract
406
+
407
+ Cross-SDK client guarantees (event demuxer ordering, `__disconnected__` event, buffering caps, timeout/cleanup policy, auth-token sync) are defined in [SDKContract.md](https://github.com/laoshanxi/app-mesh/blob/main/docs/source/SDKContract.md).