@simplysm/service-common 13.0.78 → 13.0.80

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/package.json +4 -3
  2. package/README.md +0 -534
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/service-common",
3
- "version": "13.0.78",
3
+ "version": "13.0.80",
4
4
  "description": "Simplysm package - Service module (common)",
5
5
  "author": "simplysm",
6
6
  "license": "Apache-2.0",
@@ -14,12 +14,13 @@
14
14
  "types": "./dist/index.d.ts",
15
15
  "files": [
16
16
  "dist",
17
+ "docs",
17
18
  "src",
18
19
  "tests"
19
20
  ],
20
21
  "sideEffects": false,
21
22
  "dependencies": {
22
- "@simplysm/core-common": "13.0.78",
23
- "@simplysm/orm-common": "13.0.78"
23
+ "@simplysm/core-common": "13.0.80",
24
+ "@simplysm/orm-common": "13.0.80"
24
25
  }
25
26
  }
package/README.md DELETED
@@ -1,534 +0,0 @@
1
- # @simplysm/service-common
2
-
3
- Simplysm package - Service module (common)
4
-
5
- Shared types, protocol definitions, and service interfaces used by both `@simplysm/service-client` and `@simplysm/service-server`.
6
-
7
- ## Installation
8
-
9
- ```bash
10
- pnpm add @simplysm/service-common
11
- ```
12
-
13
- ## Protocol
14
-
15
- ### `PROTOCOL_CONFIG`
16
-
17
- Constants that control the binary wire protocol.
18
-
19
- ```ts
20
- import { PROTOCOL_CONFIG } from "@simplysm/service-common";
21
-
22
- console.log(PROTOCOL_CONFIG.MAX_TOTAL_SIZE); // 104857600 (100 MB)
23
- console.log(PROTOCOL_CONFIG.SPLIT_MESSAGE_SIZE); // 3145728 (3 MB)
24
- console.log(PROTOCOL_CONFIG.CHUNK_SIZE); // 307200 (300 KB)
25
- console.log(PROTOCOL_CONFIG.GC_INTERVAL); // 10000 (10 s)
26
- console.log(PROTOCOL_CONFIG.EXPIRE_TIME); // 60000 (60 s)
27
- ```
28
-
29
- | Field | Value | Description |
30
- |---|---|---|
31
- | `MAX_TOTAL_SIZE` | 100 MB | Maximum allowed message size |
32
- | `SPLIT_MESSAGE_SIZE` | 3 MB | Messages larger than this are split into chunks |
33
- | `CHUNK_SIZE` | 300 KB | Size of each chunk |
34
- | `GC_INTERVAL` | 10 s | Interval for GC of incomplete messages |
35
- | `EXPIRE_TIME` | 60 s | Expiry time for incomplete chunked messages |
36
-
37
- ---
38
-
39
- ### Message Types
40
-
41
- Type union aliases for all WebSocket messages exchanged between client and server.
42
-
43
- ```ts
44
- import type {
45
- ServiceMessage,
46
- ServiceServerMessage,
47
- ServiceServerRawMessage,
48
- ServiceClientMessage,
49
- } from "@simplysm/service-common";
50
- ```
51
-
52
- | Type | Direction | Description |
53
- |---|---|---|
54
- | `ServiceMessage` | both | Union of all possible messages |
55
- | `ServiceServerMessage` | server → client | Messages the server sends to the client |
56
- | `ServiceServerRawMessage` | server → client | `ServiceProgressMessage` plus `ServiceServerMessage` |
57
- | `ServiceClientMessage` | client → server | Messages the client sends to the server |
58
-
59
- ---
60
-
61
- ### Individual Message Interfaces
62
-
63
- #### `ServiceReloadMessage`
64
-
65
- Server-sent notification asking the client to reload.
66
-
67
- ```ts
68
- import type { ServiceReloadMessage } from "@simplysm/service-common";
69
-
70
- const msg: ServiceReloadMessage = {
71
- name: "reload",
72
- body: {
73
- clientName: "my-app",
74
- changedFileSet: new Set(["main.js"]),
75
- },
76
- };
77
- ```
78
-
79
- #### `ServiceProgressMessage`
80
-
81
- Server-sent progress notification while a large chunked message is being received.
82
-
83
- ```ts
84
- import type { ServiceProgressMessage } from "@simplysm/service-common";
85
-
86
- const msg: ServiceProgressMessage = {
87
- name: "progress",
88
- body: { totalSize: 10_000_000, completedSize: 3_000_000 },
89
- };
90
- ```
91
-
92
- #### `ServiceErrorMessage`
93
-
94
- Server-sent error notification.
95
-
96
- ```ts
97
- import type { ServiceErrorMessage } from "@simplysm/service-common";
98
-
99
- const msg: ServiceErrorMessage = {
100
- name: "error",
101
- body: {
102
- name: "NotFoundError",
103
- message: "Record not found",
104
- code: "NOT_FOUND",
105
- stack: "...",
106
- detail: { id: 42 },
107
- cause: undefined,
108
- },
109
- };
110
- ```
111
-
112
- | Field | Required | Description |
113
- |---|---|---|
114
- | `name` | yes | Error class name |
115
- | `message` | yes | Error message |
116
- | `code` | yes | Error code string |
117
- | `stack` | no | Stack trace |
118
- | `detail` | no | Additional context |
119
- | `cause` | no | Underlying cause |
120
-
121
- #### `ServiceAuthMessage`
122
-
123
- Client-sent authentication message carrying a token.
124
-
125
- ```ts
126
- import type { ServiceAuthMessage } from "@simplysm/service-common";
127
-
128
- const msg: ServiceAuthMessage = { name: "auth", body: "<jwt-token>" };
129
- ```
130
-
131
- #### `ServiceRequestMessage`
132
-
133
- Client-sent message to invoke a service method.
134
-
135
- ```ts
136
- import type { ServiceRequestMessage } from "@simplysm/service-common";
137
-
138
- const msg: ServiceRequestMessage = {
139
- name: "UserService.getUser", // "${service}.${method}"
140
- body: [42], // method arguments
141
- };
142
- ```
143
-
144
- #### `ServiceResponseMessage`
145
-
146
- Server-sent response to a `ServiceRequestMessage`.
147
-
148
- ```ts
149
- import type { ServiceResponseMessage } from "@simplysm/service-common";
150
-
151
- const msg: ServiceResponseMessage = {
152
- name: "response",
153
- body: { id: 42, name: "Alice" },
154
- };
155
- ```
156
-
157
- #### `ServiceAddEventListenerMessage`
158
-
159
- Client-sent message to subscribe to a named event.
160
-
161
- ```ts
162
- import type { ServiceAddEventListenerMessage } from "@simplysm/service-common";
163
-
164
- const msg: ServiceAddEventListenerMessage = {
165
- name: "evt:add",
166
- body: {
167
- key: "<uuid>", // unique listener key for later removal
168
- name: "OrderUpdated", // event name
169
- info: { orderId: 1 }, // filter info passed to the server
170
- },
171
- };
172
- ```
173
-
174
- #### `ServiceRemoveEventListenerMessage`
175
-
176
- Client-sent message to unsubscribe from an event.
177
-
178
- ```ts
179
- import type { ServiceRemoveEventListenerMessage } from "@simplysm/service-common";
180
-
181
- const msg: ServiceRemoveEventListenerMessage = {
182
- name: "evt:remove",
183
- body: { key: "<uuid>" },
184
- };
185
- ```
186
-
187
- #### `ServiceGetEventListenerInfosMessage`
188
-
189
- Client-sent message to retrieve listener info objects for a named event.
190
-
191
- ```ts
192
- import type { ServiceGetEventListenerInfosMessage } from "@simplysm/service-common";
193
-
194
- const msg: ServiceGetEventListenerInfosMessage = {
195
- name: "evt:gets",
196
- body: { name: "OrderUpdated" },
197
- };
198
- ```
199
-
200
- #### `ServiceEmitEventMessage`
201
-
202
- Client-sent message to emit an event to specific listeners.
203
-
204
- ```ts
205
- import type { ServiceEmitEventMessage } from "@simplysm/service-common";
206
-
207
- const msg: ServiceEmitEventMessage = {
208
- name: "evt:emit",
209
- body: {
210
- keys: ["<uuid1>", "<uuid2>"],
211
- data: { status: "shipped" },
212
- },
213
- };
214
- ```
215
-
216
- #### `ServiceEventMessage`
217
-
218
- Server-sent event notification delivered to subscribed clients.
219
-
220
- ```ts
221
- import type { ServiceEventMessage } from "@simplysm/service-common";
222
-
223
- const msg: ServiceEventMessage = {
224
- name: "evt:on",
225
- body: {
226
- keys: ["<uuid1>"],
227
- data: { status: "shipped" },
228
- },
229
- };
230
- ```
231
-
232
- ---
233
-
234
- ### `createServiceProtocol`
235
-
236
- Factory function that creates a `ServiceProtocol` encoder/decoder.
237
-
238
- Implements Binary Protocol V2:
239
- - Header: 28 bytes (UUID 16 + TotalSize 8 + Index 4), Big Endian
240
- - Body: UTF-8 JSON
241
- - Automatically splits messages larger than 3 MB into 300 KB chunks
242
- - Automatically reassembles chunked packets on decode
243
-
244
- ```ts
245
- import { createServiceProtocol } from "@simplysm/service-common";
246
-
247
- const protocol = createServiceProtocol();
248
-
249
- // Encode
250
- const uuid = crypto.randomUUID();
251
- const { chunks, totalSize } = protocol.encode(uuid, {
252
- name: "UserService.getUser",
253
- body: [42],
254
- });
255
- // Send each chunk over a WebSocket
256
-
257
- // Decode (call for every incoming binary frame)
258
- const result = protocol.decode(incomingBytes);
259
- if (result.type === "complete") {
260
- console.log(result.uuid, result.message);
261
- } else {
262
- // result.type === "progress"
263
- console.log(`${result.completedSize} / ${result.totalSize} bytes received`);
264
- }
265
-
266
- // Release internal GC timer when done
267
- protocol.dispose();
268
- ```
269
-
270
- ---
271
-
272
- ### `ServiceProtocol`
273
-
274
- Interface returned by `createServiceProtocol`.
275
-
276
- ```ts
277
- import type { ServiceProtocol } from "@simplysm/service-common";
278
-
279
- interface ServiceProtocol {
280
- encode(uuid: string, message: ServiceMessage): { chunks: Bytes[]; totalSize: number };
281
- decode<T extends ServiceMessage>(bytes: Bytes): ServiceMessageDecodeResult<T>;
282
- dispose(): void;
283
- }
284
- ```
285
-
286
- ---
287
-
288
- ### `ServiceMessageDecodeResult`
289
-
290
- Union type returned by `ServiceProtocol.decode`.
291
-
292
- ```ts
293
- import type { ServiceMessageDecodeResult } from "@simplysm/service-common";
294
-
295
- type ServiceMessageDecodeResult<TMessage extends ServiceMessage> =
296
- | { type: "complete"; uuid: string; message: TMessage }
297
- | { type: "progress"; uuid: string; totalSize: number; completedSize: number };
298
- ```
299
-
300
- ## Service Types
301
-
302
- Pre-defined service interfaces for common backend services. Implement these interfaces on the server side and consume them on the client side.
303
-
304
- ### `OrmService`
305
-
306
- Interface for ORM database access over the service layer.
307
-
308
- ```ts
309
- import type { OrmService } from "@simplysm/service-common";
310
- ```
311
-
312
- | Method | Description |
313
- |---|---|
314
- | `getInfo(opt)` | Returns dialect, database, and schema for a named connection config |
315
- | `connect(opt)` | Opens a connection using an inline config object and returns a connection ID |
316
- | `close(connId)` | Closes the connection |
317
- | `beginTransaction(connId, isolationLevel?)` | Begins a transaction |
318
- | `commitTransaction(connId)` | Commits the current transaction |
319
- | `rollbackTransaction(connId)` | Rolls back the current transaction |
320
- | `executeParametrized(connId, query, params?)` | Executes a parameterized SQL query |
321
- | `executeDefs(connId, defs, options?)` | Executes a list of `QueryDef` objects |
322
- | `bulkInsert(connId, tableName, columnDefs, records)` | Performs a bulk insert |
323
-
324
- **Signatures**
325
-
326
- ```ts
327
- getInfo(opt: DbConnOptions & { configName: string }): Promise<{
328
- dialect: Dialect;
329
- database?: string;
330
- schema?: string;
331
- }>;
332
-
333
- connect(opt: Record<string, unknown>): Promise<number>;
334
-
335
- executeParametrized(connId: number, query: string, params?: unknown[]): Promise<unknown[][]>;
336
-
337
- executeDefs(
338
- connId: number,
339
- defs: QueryDef[],
340
- options?: (ResultMeta | undefined)[],
341
- ): Promise<unknown[][]>;
342
-
343
- bulkInsert(
344
- connId: number,
345
- tableName: string,
346
- columnDefs: Record<string, ColumnMeta>,
347
- records: Record<string, unknown>[],
348
- ): Promise<void>;
349
- ```
350
-
351
- ---
352
-
353
- ### `DbConnOptions`
354
-
355
- Options for selecting a database connection configuration.
356
-
357
- ```ts
358
- import type { DbConnOptions } from "@simplysm/service-common";
359
-
360
- type DbConnOptions = {
361
- configName?: string; // Named config key
362
- config?: Record<string, unknown>; // Inline config object
363
- };
364
- ```
365
-
366
- ---
367
-
368
- ### `AutoUpdateService`
369
-
370
- Interface for retrieving the latest client application version.
371
-
372
- ```ts
373
- import type { AutoUpdateService } from "@simplysm/service-common";
374
-
375
- // Server implementation example
376
- class MyAutoUpdateService implements AutoUpdateService {
377
- async getLastVersion(platform: string) {
378
- if (platform === "win32") {
379
- return { version: "1.2.3", downloadPath: "/updates/app-1.2.3.exe" };
380
- }
381
- return undefined;
382
- }
383
- }
384
- ```
385
-
386
- | Method | Parameters | Returns | Description |
387
- |---|---|---|---|
388
- | `getLastVersion` | `platform: string` | `Promise<{ version: string; downloadPath: string } \| undefined>` | Returns the latest version info for the given platform, or `undefined` if none exists |
389
-
390
- ---
391
-
392
- ### `SmtpClientSendAttachment`
393
-
394
- Attachment descriptor for outgoing emails.
395
-
396
- ```ts
397
- import type { SmtpClientSendAttachment } from "@simplysm/service-common";
398
-
399
- interface SmtpClientSendAttachment {
400
- filename: string;
401
- content?: string | Uint8Array; // Inline content
402
- path?: any; // File path (node)
403
- contentType?: string;
404
- }
405
- ```
406
-
407
- ---
408
-
409
- ### `SmtpClientSendByDefaultOption`
410
-
411
- Email send options when using a pre-configured SMTP default.
412
-
413
- ```ts
414
- import type { SmtpClientSendByDefaultOption } from "@simplysm/service-common";
415
-
416
- interface SmtpClientSendByDefaultOption {
417
- to: string;
418
- cc?: string;
419
- bcc?: string;
420
- subject: string;
421
- html: string;
422
- attachments?: SmtpClientSendAttachment[];
423
- }
424
- ```
425
-
426
- ---
427
-
428
- ### `SmtpClientSendOption`
429
-
430
- Full email send options including explicit SMTP connection details.
431
-
432
- ```ts
433
- import type { SmtpClientSendOption } from "@simplysm/service-common";
434
-
435
- interface SmtpClientSendOption {
436
- host: string;
437
- port?: number;
438
- secure?: boolean;
439
- user?: string;
440
- pass?: string;
441
-
442
- from: string;
443
- to: string;
444
- cc?: string;
445
- bcc?: string;
446
- subject: string;
447
- html: string;
448
- attachments?: SmtpClientSendAttachment[];
449
- }
450
- ```
451
-
452
- ---
453
-
454
- ### `SmtpClientDefaultOptions`
455
-
456
- Configuration for a default SMTP sender used server-side.
457
-
458
- ```ts
459
- import type { SmtpClientDefaultOptions } from "@simplysm/service-common";
460
-
461
- interface SmtpClientDefaultOptions {
462
- senderName: string;
463
- senderEmail?: string;
464
- user?: string;
465
- pass?: string;
466
- host: string;
467
- port?: number;
468
- secure?: boolean;
469
- }
470
- ```
471
-
472
- ## Types
473
-
474
- ### `ServiceUploadResult`
475
-
476
- Describes a file that has been uploaded to the server.
477
-
478
- ```ts
479
- import type { ServiceUploadResult } from "@simplysm/service-common";
480
-
481
- interface ServiceUploadResult {
482
- path: string; // Storage path on the server
483
- filename: string; // Original filename
484
- size: number; // File size in bytes
485
- }
486
- ```
487
-
488
- ## Define
489
-
490
- ### `defineEvent`
491
-
492
- Creates a type-safe event definition object used to subscribe and emit events through the service layer.
493
-
494
- ```ts
495
- import { defineEvent } from "@simplysm/service-common";
496
-
497
- // Define a typed event
498
- const OrderUpdated = defineEvent<
499
- { orderId: number }, // TInfo — filter info sent with addEventListener
500
- { status: string } // TData — data payload delivered to the listener
501
- >("OrderUpdated");
502
-
503
- // Server: emit the event
504
- ctx.socket?.emitEvent(OrderUpdated, { orderId: 123 }, { status: "shipped" });
505
-
506
- // Client: subscribe to the event
507
- await client.addEventListener(OrderUpdated, { orderId: 123 }, (data) => {
508
- console.log(data.status); // typed as string
509
- });
510
- ```
511
-
512
- **Signature**
513
-
514
- ```ts
515
- function defineEvent<TInfo = unknown, TData = unknown>(
516
- eventName: string,
517
- ): ServiceEventDef<TInfo, TData>
518
- ```
519
-
520
- ---
521
-
522
- ### `ServiceEventDef`
523
-
524
- The object type returned by `defineEvent`. The `$info` and `$data` fields are compile-time type markers only and hold no runtime value.
525
-
526
- ```ts
527
- import type { ServiceEventDef } from "@simplysm/service-common";
528
-
529
- interface ServiceEventDef<TInfo = unknown, TData = unknown> {
530
- eventName: string;
531
- readonly $info: TInfo; // type extraction only
532
- readonly $data: TData; // type extraction only
533
- }
534
- ```