@simplysm/service-common 13.0.98 → 13.0.99

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
@@ -1,6 +1,8 @@
1
1
  # @simplysm/service-common
2
2
 
3
- Service module (common) -- shared protocol and types used by both `@simplysm/service-client` and `@simplysm/service-server`.
3
+ Simplysm package - Service module (common)
4
+
5
+ Shared protocol and types used by both `@simplysm/service-client` and `@simplysm/service-server`. Provides binary message encoding/decoding, service interface definitions, and event type system.
4
6
 
5
7
  ## Installation
6
8
 
@@ -8,51 +10,102 @@ Service module (common) -- shared protocol and types used by both `@simplysm/ser
8
10
  npm install @simplysm/service-common
9
11
  ```
10
12
 
11
- ## Exports
13
+ ## API Overview
14
+
15
+ ### Protocol
16
+ | API | Type | Description |
17
+ |-----|------|-------------|
18
+ | `PROTOCOL_CONFIG` | const | Protocol configuration constants (sizes, timeouts) |
19
+ | `ServiceProtocol` | interface | Binary protocol encoder/decoder interface |
20
+ | `createServiceProtocol` | function | Create a protocol instance |
21
+ | `ServiceMessageDecodeResult` | type | Decode result (complete or progress) |
22
+ | `ServiceMessage` | type | Union of all message types |
23
+ | `ServiceServerMessage` | type | Server-to-client message union |
24
+ | `ServiceServerRawMessage` | type | Server messages including progress |
25
+ | `ServiceClientMessage` | type | Client-to-server message union |
26
+ | `ServiceReloadMessage` | interface | Server reload command |
27
+ | `ServiceProgressMessage` | interface | Chunked message progress notification |
28
+ | `ServiceErrorMessage` | interface | Server error notification |
29
+ | `ServiceAuthMessage` | interface | Client authentication message |
30
+ | `ServiceRequestMessage` | interface | Client service method request |
31
+ | `ServiceResponseMessage` | interface | Server method response |
32
+ | `ServiceAddEventListenerMessage` | interface | Client add event listener |
33
+ | `ServiceRemoveEventListenerMessage` | interface | Client remove event listener |
34
+ | `ServiceGetEventListenerInfosMessage` | interface | Client request event listener infos |
35
+ | `ServiceEmitEventMessage` | interface | Client emit event |
36
+ | `ServiceEventMessage` | interface | Server event notification |
37
+
38
+ -> See [docs/protocol.md](./docs/protocol.md) for details.
39
+
40
+ ### Service Types
41
+ | API | Type | Description |
42
+ |-----|------|-------------|
43
+ | `OrmService` | interface | ORM service interface (connect, query, transaction) |
44
+ | `DbConnOptions` | type | Database connection options |
45
+ | `AutoUpdateService` | interface | Auto-update service interface |
46
+ | `SmtpClientSendOption` | interface | Full SMTP send options |
47
+ | `SmtpClientSendByDefaultOption` | interface | SMTP send with server defaults |
48
+ | `SmtpClientSendAttachment` | interface | Email attachment definition |
49
+ | `SmtpClientDefaultOptions` | interface | Default SMTP client config |
50
+
51
+ -> See [docs/service-types.md](./docs/service-types.md) for details.
52
+
53
+ ### Types
54
+ | API | Type | Description |
55
+ |-----|------|-------------|
56
+ | `ServiceUploadResult` | interface | File upload result (path, filename, size) |
57
+
58
+ -> See [docs/events.md](./docs/events.md) for details.
59
+
60
+ ### Event Definition
61
+ | API | Type | Description |
62
+ |-----|------|-------------|
63
+ | `ServiceEventDef` | interface | Type-safe event definition |
64
+ | `defineEvent` | function | Define a service event with typed info and data |
65
+
66
+ -> See [docs/events.md](./docs/events.md) for details.
67
+
68
+ ## Usage Examples
69
+
70
+ ### Define and Use Events
12
71
 
13
72
  ```typescript
14
- import {
15
- // Protocol
16
- PROTOCOL_CONFIG,
17
- type ServiceMessage,
18
- type ServiceServerMessage,
19
- type ServiceServerRawMessage,
20
- type ServiceClientMessage,
21
- type ServiceProtocol,
22
- type ServiceMessageDecodeResult,
23
- createServiceProtocol,
24
- // Service Types
25
- type OrmService,
26
- type DbConnOptions,
27
- type AutoUpdateService,
28
- type SmtpClientSendAttachment,
29
- type SmtpClientSendByDefaultOption,
30
- type SmtpClientSendOption,
31
- type SmtpClientDefaultOptions,
32
- // Events + Types
33
- type ServiceEventDef,
34
- defineEvent,
35
- type ServiceUploadResult,
36
- } from "@simplysm/service-common";
73
+ import { defineEvent } from "@simplysm/service-common";
74
+
75
+ // Define a typed event
76
+ const OrderUpdated = defineEvent<
77
+ { orderId: number }, // TInfo: filter info
78
+ { status: string } // TData: event data
79
+ >("OrderUpdated");
80
+
81
+ // Server: emit event
82
+ ctx.socket?.emitEvent(OrderUpdated, { orderId: 123 }, { status: "shipped" });
83
+
84
+ // Client: subscribe to event
85
+ await client.addEventListener(OrderUpdated, { orderId: 123 }, (data) => {
86
+ console.log(data.status); // typed as string
87
+ });
37
88
  ```
38
89
 
39
- ## Quick Start
90
+ ### Use Protocol
40
91
 
41
92
  ```typescript
42
- import { createServiceProtocol, defineEvent } from "@simplysm/service-common";
93
+ import { createServiceProtocol } from "@simplysm/service-common";
43
94
 
44
- // Create protocol encoder/decoder
45
95
  const protocol = createServiceProtocol();
46
- const { chunks, totalSize } = protocol.encode(uuid, { name: "auth", body: token });
47
- const result = protocol.decode(chunks[0]);
48
- protocol.dispose();
49
96
 
50
- // Define a typed event
51
- const OrderUpdated = defineEvent<{ orderId: number }, { status: string }>("OrderUpdated");
52
- ```
97
+ // Encode
98
+ const { chunks, totalSize } = protocol.encode(uuid, {
99
+ name: "MyService.myMethod",
100
+ body: [param1, param2],
101
+ });
53
102
 
54
- ## Documentation
103
+ // Decode
104
+ const result = protocol.decode(receivedBytes);
105
+ if (result.type === "complete") {
106
+ handleMessage(result.message);
107
+ }
55
108
 
56
- - [Protocol](docs/protocol.md)
57
- - [Service Types](docs/service-types.md)
58
- - [Events and Types](docs/events.md)
109
+ // Cleanup
110
+ protocol.dispose();
111
+ ```
package/docs/events.md ADDED
@@ -0,0 +1,51 @@
1
+ # Event Definition
2
+
3
+ ## `ServiceEventDef`
4
+
5
+ Event definition created by `defineEvent()`. `$info` and `$data` are type-only markers (not used at runtime).
6
+
7
+ ```typescript
8
+ interface ServiceEventDef<TInfo = unknown, TData = unknown> {
9
+ eventName: string;
10
+ readonly $info: TInfo;
11
+ readonly $data: TData;
12
+ }
13
+ ```
14
+
15
+ | Field | Type | Description |
16
+ |-------|------|-------------|
17
+ | `eventName` | `string` | Event name |
18
+ | `$info` | `TInfo` | Type extraction only (listener filter info type) |
19
+ | `$data` | `TData` | Type extraction only (event data type) |
20
+
21
+ ## `defineEvent`
22
+
23
+ Define a service event with type-safe info and data.
24
+
25
+ ```typescript
26
+ function defineEvent<TInfo = unknown, TData = unknown>(
27
+ eventName: string,
28
+ ): ServiceEventDef<TInfo, TData>;
29
+ ```
30
+
31
+ | Parameter | Type | Description |
32
+ |-----------|------|-------------|
33
+ | `eventName` | `string` | Event name |
34
+
35
+ ## `ServiceUploadResult`
36
+
37
+ File upload result. Contains information about a file uploaded to the server.
38
+
39
+ ```typescript
40
+ interface ServiceUploadResult {
41
+ path: string;
42
+ filename: string;
43
+ size: number;
44
+ }
45
+ ```
46
+
47
+ | Field | Type | Description |
48
+ |-------|------|-------------|
49
+ | `path` | `string` | Storage path on the server |
50
+ | `filename` | `string` | Original filename |
51
+ | `size` | `number` | File size (bytes) |
@@ -0,0 +1,252 @@
1
+ # Protocol
2
+
3
+ ## `PROTOCOL_CONFIG`
4
+
5
+ Service protocol configuration constants.
6
+
7
+ ```typescript
8
+ const PROTOCOL_CONFIG = {
9
+ MAX_TOTAL_SIZE: 100 * 1024 * 1024, // Max message size (100MB)
10
+ SPLIT_MESSAGE_SIZE: 3 * 1024 * 1024, // Chunking threshold (3MB)
11
+ CHUNK_SIZE: 300 * 1024, // Chunk size (300KB)
12
+ GC_INTERVAL: 10 * 1000, // GC interval (10s)
13
+ EXPIRE_TIME: 60 * 1000, // Incomplete message expiry (60s)
14
+ } as const;
15
+ ```
16
+
17
+ ## `ServiceProtocol`
18
+
19
+ Service protocol interface for encoding/decoding binary messages.
20
+
21
+ ```typescript
22
+ interface ServiceProtocol {
23
+ encode(uuid: string, message: ServiceMessage): { chunks: Bytes[]; totalSize: number };
24
+ decode<T extends ServiceMessage>(bytes: Bytes): ServiceMessageDecodeResult<T>;
25
+ dispose(): void;
26
+ }
27
+ ```
28
+
29
+ | Method | Description |
30
+ |--------|-------------|
31
+ | `encode()` | Encode a message (auto-split if exceeding 3MB) |
32
+ | `decode()` | Decode a message (auto-reassemble chunked packets) |
33
+ | `dispose()` | Release GC timer and free memory |
34
+
35
+ ## `ServiceMessageDecodeResult`
36
+
37
+ Message decode result type (union).
38
+
39
+ ```typescript
40
+ type ServiceMessageDecodeResult<TMessage extends ServiceMessage> =
41
+ | { type: "complete"; uuid: string; message: TMessage }
42
+ | { type: "progress"; uuid: string; totalSize: number; completedSize: number };
43
+ ```
44
+
45
+ ## `createServiceProtocol`
46
+
47
+ Create a service protocol encoder/decoder. Binary Protocol V2: Header 28 bytes (UUID 16 + TotalSize 8 + Index 4) + JSON body. Auto chunking at 300KB when exceeding 3MB. Max 100MB.
48
+
49
+ ```typescript
50
+ function createServiceProtocol(): ServiceProtocol;
51
+ ```
52
+
53
+ ## `ServiceMessage`
54
+
55
+ Union type of all service messages.
56
+
57
+ ```typescript
58
+ type ServiceMessage =
59
+ | ServiceReloadMessage
60
+ | ServiceRequestMessage
61
+ | ServiceAuthMessage
62
+ | ServiceProgressMessage
63
+ | ServiceResponseMessage
64
+ | ServiceErrorMessage
65
+ | ServiceAddEventListenerMessage
66
+ | ServiceRemoveEventListenerMessage
67
+ | ServiceGetEventListenerInfosMessage
68
+ | ServiceEmitEventMessage
69
+ | ServiceEventMessage;
70
+ ```
71
+
72
+ ## `ServiceServerMessage`
73
+
74
+ Messages sent from server to client.
75
+
76
+ ```typescript
77
+ type ServiceServerMessage =
78
+ | ServiceReloadMessage
79
+ | ServiceResponseMessage
80
+ | ServiceErrorMessage
81
+ | ServiceEventMessage;
82
+ ```
83
+
84
+ ## `ServiceServerRawMessage`
85
+
86
+ Server messages including progress notifications.
87
+
88
+ ```typescript
89
+ type ServiceServerRawMessage = ServiceProgressMessage | ServiceServerMessage;
90
+ ```
91
+
92
+ ## `ServiceClientMessage`
93
+
94
+ Messages sent from client to server.
95
+
96
+ ```typescript
97
+ type ServiceClientMessage =
98
+ | ServiceRequestMessage
99
+ | ServiceAuthMessage
100
+ | ServiceAddEventListenerMessage
101
+ | ServiceRemoveEventListenerMessage
102
+ | ServiceGetEventListenerInfosMessage
103
+ | ServiceEmitEventMessage;
104
+ ```
105
+
106
+ ## `ServiceReloadMessage`
107
+
108
+ Server reload command to client.
109
+
110
+ ```typescript
111
+ interface ServiceReloadMessage {
112
+ name: "reload";
113
+ body: {
114
+ clientName: string | undefined;
115
+ changedFileSet: Set<string>;
116
+ };
117
+ }
118
+ ```
119
+
120
+ ## `ServiceProgressMessage`
121
+
122
+ Server progress notification for chunked messages.
123
+
124
+ ```typescript
125
+ interface ServiceProgressMessage {
126
+ name: "progress";
127
+ body: {
128
+ totalSize: number;
129
+ completedSize: number;
130
+ };
131
+ }
132
+ ```
133
+
134
+ ## `ServiceErrorMessage`
135
+
136
+ Server error notification.
137
+
138
+ ```typescript
139
+ interface ServiceErrorMessage {
140
+ name: "error";
141
+ body: {
142
+ name: string;
143
+ message: string;
144
+ code: string;
145
+ stack?: string;
146
+ detail?: unknown;
147
+ cause?: unknown;
148
+ };
149
+ }
150
+ ```
151
+
152
+ ## `ServiceAuthMessage`
153
+
154
+ Client authentication message.
155
+
156
+ ```typescript
157
+ interface ServiceAuthMessage {
158
+ name: "auth";
159
+ body: string;
160
+ }
161
+ ```
162
+
163
+ ## `ServiceRequestMessage`
164
+
165
+ Client service method request.
166
+
167
+ ```typescript
168
+ interface ServiceRequestMessage {
169
+ name: `${string}.${string}`;
170
+ body: unknown[];
171
+ }
172
+ ```
173
+
174
+ ## `ServiceResponseMessage`
175
+
176
+ Server service method response.
177
+
178
+ ```typescript
179
+ interface ServiceResponseMessage {
180
+ name: "response";
181
+ body?: unknown;
182
+ }
183
+ ```
184
+
185
+ ## `ServiceAddEventListenerMessage`
186
+
187
+ Client add event listener request.
188
+
189
+ ```typescript
190
+ interface ServiceAddEventListenerMessage {
191
+ name: "evt:add";
192
+ body: {
193
+ key: string;
194
+ name: string;
195
+ info: unknown;
196
+ };
197
+ }
198
+ ```
199
+
200
+ ## `ServiceRemoveEventListenerMessage`
201
+
202
+ Client remove event listener request.
203
+
204
+ ```typescript
205
+ interface ServiceRemoveEventListenerMessage {
206
+ name: "evt:remove";
207
+ body: {
208
+ key: string;
209
+ };
210
+ }
211
+ ```
212
+
213
+ ## `ServiceGetEventListenerInfosMessage`
214
+
215
+ Client request for event listener info list.
216
+
217
+ ```typescript
218
+ interface ServiceGetEventListenerInfosMessage {
219
+ name: "evt:gets";
220
+ body: {
221
+ name: string;
222
+ };
223
+ }
224
+ ```
225
+
226
+ ## `ServiceEmitEventMessage`
227
+
228
+ Client emit event request.
229
+
230
+ ```typescript
231
+ interface ServiceEmitEventMessage {
232
+ name: "evt:emit";
233
+ body: {
234
+ keys: string[];
235
+ data: unknown;
236
+ };
237
+ }
238
+ ```
239
+
240
+ ## `ServiceEventMessage`
241
+
242
+ Server event notification.
243
+
244
+ ```typescript
245
+ interface ServiceEventMessage {
246
+ name: "evt:on";
247
+ body: {
248
+ keys: string[];
249
+ data: unknown;
250
+ };
251
+ }
252
+ ```
@@ -0,0 +1,162 @@
1
+ # Service Types
2
+
3
+ ## `OrmService`
4
+
5
+ ORM service interface. Provides database connection, transaction management, and query execution over the service protocol. Supports MySQL, MSSQL, and PostgreSQL.
6
+
7
+ ```typescript
8
+ interface OrmService {
9
+ getInfo(opt: DbConnOptions & { configName: string }): Promise<{
10
+ dialect: Dialect;
11
+ database?: string;
12
+ schema?: string;
13
+ }>;
14
+ connect(opt: Record<string, unknown>): Promise<number>;
15
+ close(connId: number): Promise<void>;
16
+ beginTransaction(connId: number, isolationLevel?: IsolationLevel): Promise<void>;
17
+ commitTransaction(connId: number): Promise<void>;
18
+ rollbackTransaction(connId: number): Promise<void>;
19
+ executeParametrized(connId: number, query: string, params?: unknown[]): Promise<unknown[][]>;
20
+ executeDefs(connId: number, defs: QueryDef[], options?: (ResultMeta | undefined)[]): Promise<unknown[][]>;
21
+ bulkInsert(connId: number, tableName: string, columnDefs: Record<string, ColumnMeta>, records: Record<string, unknown>[]): Promise<void>;
22
+ }
23
+ ```
24
+
25
+ | Method | Description |
26
+ |--------|-------------|
27
+ | `getInfo()` | Get database dialect and connection info |
28
+ | `connect()` | Open a new database connection, returns connection ID |
29
+ | `close()` | Close a database connection |
30
+ | `beginTransaction()` | Begin transaction with optional isolation level |
31
+ | `commitTransaction()` | Commit transaction |
32
+ | `rollbackTransaction()` | Rollback transaction |
33
+ | `executeParametrized()` | Execute a parameterized SQL query |
34
+ | `executeDefs()` | Execute QueryDef array |
35
+ | `bulkInsert()` | Bulk insert records |
36
+
37
+ ## `DbConnOptions`
38
+
39
+ Database connection options.
40
+
41
+ ```typescript
42
+ type DbConnOptions = { configName?: string; config?: Record<string, unknown> };
43
+ ```
44
+
45
+ | Field | Type | Description |
46
+ |-------|------|-------------|
47
+ | `configName` | `string` | Configuration name to look up from server config |
48
+ | `config` | `Record<string, unknown>` | Direct connection config object |
49
+
50
+ ## `AutoUpdateService`
51
+
52
+ Auto-update service interface. Retrieves the latest version info for client applications.
53
+
54
+ ```typescript
55
+ interface AutoUpdateService {
56
+ getLastVersion(platform: string): Promise<
57
+ | { version: string; downloadPath: string }
58
+ | undefined
59
+ >;
60
+ }
61
+ ```
62
+
63
+ | Method | Description |
64
+ |--------|-------------|
65
+ | `getLastVersion()` | Retrieve latest version info for a platform (e.g., "win32", "darwin", "android") |
66
+
67
+ ## `SmtpClientSendOption`
68
+
69
+ Full SMTP send options.
70
+
71
+ ```typescript
72
+ interface SmtpClientSendOption {
73
+ host: string;
74
+ port?: number;
75
+ secure?: boolean;
76
+ user?: string;
77
+ pass?: string;
78
+ from: string;
79
+ to: string;
80
+ cc?: string;
81
+ bcc?: string;
82
+ subject: string;
83
+ html: string;
84
+ attachments?: SmtpClientSendAttachment[];
85
+ }
86
+ ```
87
+
88
+ | Field | Type | Description |
89
+ |-------|------|-------------|
90
+ | `host` | `string` | SMTP server hostname |
91
+ | `port` | `number` | SMTP server port |
92
+ | `secure` | `boolean` | Use TLS |
93
+ | `user` | `string` | SMTP auth username |
94
+ | `pass` | `string` | SMTP auth password |
95
+ | `from` | `string` | Sender email |
96
+ | `to` | `string` | Recipient email |
97
+ | `cc` | `string` | CC recipients |
98
+ | `bcc` | `string` | BCC recipients |
99
+ | `subject` | `string` | Email subject |
100
+ | `html` | `string` | Email body (HTML) |
101
+ | `attachments` | `SmtpClientSendAttachment[]` | File attachments |
102
+
103
+ ## `SmtpClientSendByDefaultOption`
104
+
105
+ SMTP send options using default server configuration.
106
+
107
+ ```typescript
108
+ interface SmtpClientSendByDefaultOption {
109
+ to: string;
110
+ cc?: string;
111
+ bcc?: string;
112
+ subject: string;
113
+ html: string;
114
+ attachments?: SmtpClientSendAttachment[];
115
+ }
116
+ ```
117
+
118
+ ## `SmtpClientSendAttachment`
119
+
120
+ Email attachment definition.
121
+
122
+ ```typescript
123
+ interface SmtpClientSendAttachment {
124
+ filename: string;
125
+ content?: string | Uint8Array;
126
+ path?: any;
127
+ contentType?: string;
128
+ }
129
+ ```
130
+
131
+ | Field | Type | Description |
132
+ |-------|------|-------------|
133
+ | `filename` | `string` | Attachment filename |
134
+ | `content` | `string \| Uint8Array` | Inline content |
135
+ | `path` | `any` | File path |
136
+ | `contentType` | `string` | MIME type |
137
+
138
+ ## `SmtpClientDefaultOptions`
139
+
140
+ Default SMTP client configuration.
141
+
142
+ ```typescript
143
+ interface SmtpClientDefaultOptions {
144
+ senderName: string;
145
+ senderEmail?: string;
146
+ user?: string;
147
+ pass?: string;
148
+ host: string;
149
+ port?: number;
150
+ secure?: boolean;
151
+ }
152
+ ```
153
+
154
+ | Field | Type | Description |
155
+ |-------|------|-------------|
156
+ | `senderName` | `string` | Default sender display name |
157
+ | `senderEmail` | `string` | Default sender email |
158
+ | `user` | `string` | SMTP auth username |
159
+ | `pass` | `string` | SMTP auth password |
160
+ | `host` | `string` | SMTP server hostname |
161
+ | `port` | `number` | SMTP server port |
162
+ | `secure` | `boolean` | Use TLS |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/service-common",
3
- "version": "13.0.98",
3
+ "version": "13.0.99",
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.98",
23
- "@simplysm/orm-common": "13.0.98"
23
+ "@simplysm/core-common": "13.0.99",
24
+ "@simplysm/orm-common": "13.0.99"
24
25
  }
25
26
  }