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 +64 -48
- package/dist/appmesh.esm.js +1 -1
- package/package.json +6 -1
- package/src/appmesh.js +310 -153
- package/src/appmesh_tcp.js +408 -272
- package/src/server.js +175 -81
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 {
|
|
95
|
+
import { AppMeshWorker, ProcessSupersededError } from "appmesh/server";
|
|
96
96
|
|
|
97
|
-
const server = new
|
|
97
|
+
const server = new AppMeshWorker();
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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.
|
|
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 {
|
|
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
|
|
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 {
|
|
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
|
|
152
|
+
() => new AppMeshWorkerTCP(sslConfig, ["127.0.0.1", 6059]),
|
|
143
153
|
async (server) => {
|
|
144
|
-
const payload = await server.
|
|
154
|
+
const payload = await server.fetch_task();
|
|
145
155
|
const result = processPayload(payload);
|
|
146
|
-
await server.
|
|
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
|
-
####
|
|
173
|
+
#### AppMeshWorker
|
|
164
174
|
|
|
165
175
|
##### Constructor
|
|
166
176
|
|
|
167
177
|
```javascript
|
|
168
|
-
new
|
|
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
|
-
###### `
|
|
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.
|
|
205
|
+
const payload = await server.fetch_task();
|
|
194
206
|
const data = JSON.parse(payload);
|
|
195
207
|
```
|
|
196
208
|
|
|
197
|
-
###### `
|
|
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.
|
|
225
|
+
await server.send_task_result(JSON.stringify(result));
|
|
214
226
|
```
|
|
215
227
|
|
|
216
|
-
####
|
|
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
|
|
235
|
+
new AppMeshWorkerTCP(sslConfig, tcpAddress, options);
|
|
224
236
|
```
|
|
225
237
|
|
|
226
238
|
**Parameters:**
|
|
227
239
|
|
|
228
|
-
- `sslConfig` (object, optional) - SSL configuration (same as
|
|
229
|
-
- `tcpAddress` (array, optional) - `[host, port]`. Default: `['127.0.0.1', 6059]`
|
|
230
|
-
- `options` (object, optional) - Same as
|
|
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 `
|
|
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
|
|
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
|
|
280
|
+
() => new AppMeshWorkerTCP(),
|
|
269
281
|
async (server) => {
|
|
270
|
-
const payload = await server.
|
|
271
|
-
await server.
|
|
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 {
|
|
293
|
+
import { AppMeshWorker } from "appmesh/server";
|
|
282
294
|
|
|
283
|
-
const server = new
|
|
295
|
+
const server = new AppMeshWorker();
|
|
284
296
|
|
|
285
297
|
try {
|
|
286
|
-
const payload = await server.
|
|
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.
|
|
308
|
+
await server.send_task_result(JSON.stringify(result));
|
|
297
309
|
} catch (error) {
|
|
298
|
-
await server.
|
|
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 {
|
|
322
|
+
import { AppMeshWorker } from "appmesh/server";
|
|
311
323
|
import crypto from "crypto";
|
|
312
324
|
|
|
313
|
-
const server = new
|
|
325
|
+
const server = new AppMeshWorker();
|
|
314
326
|
|
|
315
|
-
const binaryPayload = await server.
|
|
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.
|
|
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 {
|
|
342
|
+
import { AppMeshWorker } from "appmesh/server";
|
|
331
343
|
|
|
332
|
-
const server = new
|
|
344
|
+
const server = new AppMeshWorker();
|
|
333
345
|
|
|
334
|
-
const config = JSON.parse(await server.
|
|
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.
|
|
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 {
|
|
390
|
+
import { AppMeshWorker } from "appmesh/server";
|
|
379
391
|
|
|
380
|
-
const server = new
|
|
392
|
+
const server = new AppMeshWorker();
|
|
381
393
|
|
|
382
|
-
const payload = await server.
|
|
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.
|
|
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).
|