@simplysm/service-common 13.0.82 → 13.0.84
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 +237 -0
- package/package.json +3 -4
package/README.md
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# @simplysm/service-common
|
|
2
|
+
|
|
3
|
+
> Simplysm package - Service module (common)
|
|
4
|
+
|
|
5
|
+
Shared types and utilities for the simplysm service layer. Provides the binary protocol for client-server communication, type-safe event definitions, and interface contracts for built-in services (ORM, auto-update, SMTP).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @simplysm/service-common
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Protocol
|
|
14
|
+
|
|
15
|
+
The service protocol handles binary encoding/decoding of messages between client and server over WebSocket. Large messages are automatically chunked and reassembled.
|
|
16
|
+
|
|
17
|
+
### `createServiceProtocol()`
|
|
18
|
+
|
|
19
|
+
Creates a `ServiceProtocol` encoder/decoder instance.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createServiceProtocol } from "@simplysm/service-common";
|
|
23
|
+
|
|
24
|
+
const protocol = createServiceProtocol();
|
|
25
|
+
|
|
26
|
+
// Encode a request message
|
|
27
|
+
const { chunks, totalSize } = protocol.encode(uuid, {
|
|
28
|
+
name: "MyService.getData",
|
|
29
|
+
body: [arg1, arg2],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Decode incoming bytes
|
|
33
|
+
const result = protocol.decode(bytes);
|
|
34
|
+
if (result.type === "complete") {
|
|
35
|
+
console.log(result.message); // fully reassembled message
|
|
36
|
+
} else {
|
|
37
|
+
console.log(`${result.completedSize}/${result.totalSize} bytes received`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Cleanup when done
|
|
41
|
+
protocol.dispose();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**`ServiceProtocol` methods:**
|
|
45
|
+
|
|
46
|
+
| Method | Signature | Description |
|
|
47
|
+
|---|---|---|
|
|
48
|
+
| `encode` | `(uuid: string, message: ServiceMessage) => { chunks: Bytes[]; totalSize: number }` | Encode a message, auto-splitting into chunks if needed |
|
|
49
|
+
| `decode` | `(bytes: Bytes) => ServiceMessageDecodeResult<T>` | Decode bytes, auto-reassembling chunked packets |
|
|
50
|
+
| `dispose` | `() => void` | Release the internal chunk accumulator GC timer |
|
|
51
|
+
|
|
52
|
+
**Binary Protocol V2 header (28 bytes, Big Endian):**
|
|
53
|
+
|
|
54
|
+
| Offset | Size | Field |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| 0 | 16 | UUID (binary) |
|
|
57
|
+
| 16 | 8 | TotalSize (uint64) |
|
|
58
|
+
| 24 | 4 | Index (uint32) |
|
|
59
|
+
|
|
60
|
+
### `PROTOCOL_CONFIG`
|
|
61
|
+
|
|
62
|
+
Protocol configuration constants.
|
|
63
|
+
|
|
64
|
+
| Constant | Value | Description |
|
|
65
|
+
|---|---|---|
|
|
66
|
+
| `MAX_TOTAL_SIZE` | 100 MB | Maximum message size |
|
|
67
|
+
| `SPLIT_MESSAGE_SIZE` | 3 MB | Chunking threshold |
|
|
68
|
+
| `CHUNK_SIZE` | 300 KB | Individual chunk size |
|
|
69
|
+
| `GC_INTERVAL` | 10 s | Garbage collection interval for incomplete messages |
|
|
70
|
+
| `EXPIRE_TIME` | 60 s | Expiry time for incomplete messages |
|
|
71
|
+
|
|
72
|
+
### `ServiceMessageDecodeResult<T>`
|
|
73
|
+
|
|
74
|
+
Union type returned by `decode()`:
|
|
75
|
+
|
|
76
|
+
- `{ type: "complete"; uuid: string; message: T }` -- all chunks received, message fully reassembled
|
|
77
|
+
- `{ type: "progress"; uuid: string; totalSize: number; completedSize: number }` -- chunked message still in progress
|
|
78
|
+
|
|
79
|
+
### Message Types
|
|
80
|
+
|
|
81
|
+
All messages exchanged between client and server are typed as `ServiceMessage`, which is a union of the following:
|
|
82
|
+
|
|
83
|
+
**Client messages (`ServiceClientMessage`):**
|
|
84
|
+
|
|
85
|
+
| Interface | `name` | Description |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| `ServiceAuthMessage` | `"auth"` | Authentication with token |
|
|
88
|
+
| `ServiceRequestMessage` | `` `${service}.${method}` `` | Service method invocation |
|
|
89
|
+
| `ServiceAddEventListenerMessage` | `"evt:add"` | Subscribe to an event |
|
|
90
|
+
| `ServiceRemoveEventListenerMessage` | `"evt:remove"` | Unsubscribe from an event |
|
|
91
|
+
| `ServiceGetEventListenerInfosMessage` | `"evt:gets"` | Query event listener info |
|
|
92
|
+
| `ServiceEmitEventMessage` | `"evt:emit"` | Emit an event to listeners |
|
|
93
|
+
|
|
94
|
+
**Server messages (`ServiceServerMessage`):**
|
|
95
|
+
|
|
96
|
+
| Interface | `name` | Description |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| `ServiceReloadMessage` | `"reload"` | Hot-reload command to client |
|
|
99
|
+
| `ServiceResponseMessage` | `"response"` | Response to a service method request |
|
|
100
|
+
| `ServiceErrorMessage` | `"error"` | Error notification |
|
|
101
|
+
| `ServiceEventMessage` | `"evt:on"` | Event notification to subscribers |
|
|
102
|
+
|
|
103
|
+
**Internal server messages (`ServiceServerRawMessage`):**
|
|
104
|
+
|
|
105
|
+
| Interface | `name` | Description |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| `ServiceProgressMessage` | `"progress"` | Chunk upload progress notification |
|
|
108
|
+
|
|
109
|
+
## Events
|
|
110
|
+
|
|
111
|
+
### `defineEvent<TInfo, TData>(eventName)`
|
|
112
|
+
|
|
113
|
+
Define a type-safe service event. Returns a `ServiceEventDef` descriptor used by the client and server to subscribe, emit, and handle events with full type inference.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { defineEvent } from "@simplysm/service-common";
|
|
117
|
+
|
|
118
|
+
// Define an event with typed info (filter) and data (payload)
|
|
119
|
+
const OrderUpdated = defineEvent<{ orderId: number }, { status: string }>("OrderUpdated");
|
|
120
|
+
|
|
121
|
+
// Server emit
|
|
122
|
+
ctx.socket?.emitEvent(OrderUpdated, { orderId: 123 }, { status: "shipped" });
|
|
123
|
+
|
|
124
|
+
// Client subscribe
|
|
125
|
+
await client.addEventListener(OrderUpdated, { orderId: 123 }, (data) => {
|
|
126
|
+
console.log(data.status); // typed as string
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### `ServiceEventDef<TInfo, TData>`
|
|
131
|
+
|
|
132
|
+
Event definition object returned by `defineEvent()`.
|
|
133
|
+
|
|
134
|
+
| Property | Type | Description |
|
|
135
|
+
|---|---|---|
|
|
136
|
+
| `eventName` | `string` | Event name identifier |
|
|
137
|
+
| `$info` | `TInfo` | Type-only marker for filter info (not used at runtime) |
|
|
138
|
+
| `$data` | `TData` | Type-only marker for event data (not used at runtime) |
|
|
139
|
+
|
|
140
|
+
## Service Interfaces
|
|
141
|
+
|
|
142
|
+
Contracts for built-in services. These interfaces are implemented on the server and consumed by the client.
|
|
143
|
+
|
|
144
|
+
### `OrmService`
|
|
145
|
+
|
|
146
|
+
Database connection, transaction management, and query execution. Supports MySQL, MSSQL, and PostgreSQL.
|
|
147
|
+
|
|
148
|
+
| Method | Signature | Description |
|
|
149
|
+
|---|---|---|
|
|
150
|
+
| `getInfo` | `(opt: DbConnOptions & { configName: string }) => Promise<{ dialect; database?; schema? }>` | Get database connection info |
|
|
151
|
+
| `connect` | `(opt: Record<string, unknown>) => Promise<number>` | Open a connection, returns connection ID |
|
|
152
|
+
| `close` | `(connId: number) => Promise<void>` | Close a connection |
|
|
153
|
+
| `beginTransaction` | `(connId: number, isolationLevel?: IsolationLevel) => Promise<void>` | Begin a transaction |
|
|
154
|
+
| `commitTransaction` | `(connId: number) => Promise<void>` | Commit a transaction |
|
|
155
|
+
| `rollbackTransaction` | `(connId: number) => Promise<void>` | Rollback a transaction |
|
|
156
|
+
| `executeParametrized` | `(connId: number, query: string, params?: unknown[]) => Promise<unknown[][]>` | Execute a parameterized query |
|
|
157
|
+
| `executeDefs` | `(connId: number, defs: QueryDef[], options?: (ResultMeta \| undefined)[]) => Promise<unknown[][]>` | Execute query definitions |
|
|
158
|
+
| `bulkInsert` | `(connId: number, tableName: string, columnDefs: Record<string, ColumnMeta>, records: Record<string, unknown>[]) => Promise<void>` | Bulk insert records |
|
|
159
|
+
|
|
160
|
+
### `DbConnOptions`
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
type DbConnOptions = { configName?: string; config?: Record<string, unknown> };
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### `AutoUpdateService`
|
|
167
|
+
|
|
168
|
+
Retrieves the latest version info for client applications.
|
|
169
|
+
|
|
170
|
+
| Method | Signature | Description |
|
|
171
|
+
|---|---|---|
|
|
172
|
+
| `getLastVersion` | `(platform: string) => Promise<{ version: string; downloadPath: string } \| undefined>` | Get latest version info for a platform (e.g., `"win32"`, `"darwin"`, `"linux"`) |
|
|
173
|
+
|
|
174
|
+
### SMTP Types
|
|
175
|
+
|
|
176
|
+
Types for the SMTP email sending service.
|
|
177
|
+
|
|
178
|
+
**`SmtpClientSendOption`** -- Full SMTP send configuration:
|
|
179
|
+
|
|
180
|
+
| Property | Type | Required | Description |
|
|
181
|
+
|---|---|---|---|
|
|
182
|
+
| `host` | `string` | Yes | SMTP server host |
|
|
183
|
+
| `port` | `number` | No | SMTP server port |
|
|
184
|
+
| `secure` | `boolean` | No | Use TLS |
|
|
185
|
+
| `user` | `string` | No | Authentication username |
|
|
186
|
+
| `pass` | `string` | No | Authentication password |
|
|
187
|
+
| `from` | `string` | Yes | Sender address |
|
|
188
|
+
| `to` | `string` | Yes | Recipient address |
|
|
189
|
+
| `cc` | `string` | No | CC address |
|
|
190
|
+
| `bcc` | `string` | No | BCC address |
|
|
191
|
+
| `subject` | `string` | Yes | Email subject |
|
|
192
|
+
| `html` | `string` | Yes | Email body (HTML) |
|
|
193
|
+
| `attachments` | `SmtpClientSendAttachment[]` | No | File attachments |
|
|
194
|
+
|
|
195
|
+
**`SmtpClientSendByDefaultOption`** -- Send using pre-configured defaults (omits host/auth/from):
|
|
196
|
+
|
|
197
|
+
| Property | Type | Required | Description |
|
|
198
|
+
|---|---|---|---|
|
|
199
|
+
| `to` | `string` | Yes | Recipient address |
|
|
200
|
+
| `cc` | `string` | No | CC address |
|
|
201
|
+
| `bcc` | `string` | No | BCC address |
|
|
202
|
+
| `subject` | `string` | Yes | Email subject |
|
|
203
|
+
| `html` | `string` | Yes | Email body (HTML) |
|
|
204
|
+
| `attachments` | `SmtpClientSendAttachment[]` | No | File attachments |
|
|
205
|
+
|
|
206
|
+
**`SmtpClientDefaultOptions`** -- Default SMTP configuration:
|
|
207
|
+
|
|
208
|
+
| Property | Type | Required | Description |
|
|
209
|
+
|---|---|---|---|
|
|
210
|
+
| `senderName` | `string` | Yes | Display name for sender |
|
|
211
|
+
| `senderEmail` | `string` | No | Sender email address |
|
|
212
|
+
| `user` | `string` | No | Authentication username |
|
|
213
|
+
| `pass` | `string` | No | Authentication password |
|
|
214
|
+
| `host` | `string` | Yes | SMTP server host |
|
|
215
|
+
| `port` | `number` | No | SMTP server port |
|
|
216
|
+
| `secure` | `boolean` | No | Use TLS |
|
|
217
|
+
|
|
218
|
+
**`SmtpClientSendAttachment`** -- Email attachment:
|
|
219
|
+
|
|
220
|
+
| Property | Type | Required | Description |
|
|
221
|
+
|---|---|---|---|
|
|
222
|
+
| `filename` | `string` | Yes | Attachment filename |
|
|
223
|
+
| `content` | `string \| Uint8Array` | No | Inline content |
|
|
224
|
+
| `path` | `any` | No | File path |
|
|
225
|
+
| `contentType` | `string` | No | MIME type |
|
|
226
|
+
|
|
227
|
+
## Common Types
|
|
228
|
+
|
|
229
|
+
### `ServiceUploadResult`
|
|
230
|
+
|
|
231
|
+
File upload result returned by the server.
|
|
232
|
+
|
|
233
|
+
| Property | Type | Description |
|
|
234
|
+
|---|---|---|
|
|
235
|
+
| `path` | `string` | Storage path on the server |
|
|
236
|
+
| `filename` | `string` | Original filename |
|
|
237
|
+
| `size` | `number` | File size in bytes |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/service-common",
|
|
3
|
-
"version": "13.0.
|
|
3
|
+
"version": "13.0.84",
|
|
4
4
|
"description": "Simplysm package - Service module (common)",
|
|
5
5
|
"author": "simplysm",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -14,13 +14,12 @@
|
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"dist",
|
|
17
|
-
"docs",
|
|
18
17
|
"src",
|
|
19
18
|
"tests"
|
|
20
19
|
],
|
|
21
20
|
"sideEffects": false,
|
|
22
21
|
"dependencies": {
|
|
23
|
-
"@simplysm/core-common": "13.0.
|
|
24
|
-
"@simplysm/orm-common": "13.0.
|
|
22
|
+
"@simplysm/core-common": "13.0.84",
|
|
23
|
+
"@simplysm/orm-common": "13.0.84"
|
|
25
24
|
}
|
|
26
25
|
}
|