@simplysm/service-common 13.0.96 → 13.0.98
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 +39 -273
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,292 +1,58 @@
|
|
|
1
1
|
# @simplysm/service-common
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Service module (common) -- shared protocol and types used by both `@simplysm/service-client` and `@simplysm/service-server`.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @simplysm/service-common
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
11
|
+
## Exports
|
|
12
|
+
|
|
13
|
+
```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";
|
|
29
37
|
```
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
## Quick Start
|
|
32
40
|
|
|
33
41
|
```typescript
|
|
34
|
-
import { createServiceProtocol } from "@simplysm/service-common";
|
|
35
|
-
import type { ServiceProtocol, ServiceMessageDecodeResult } from "@simplysm/service-common";
|
|
36
|
-
|
|
37
|
-
const protocol: ServiceProtocol = createServiceProtocol();
|
|
38
|
-
|
|
39
|
-
// 인코딩: 메시지를 바이너리 청크 배열로 변환
|
|
40
|
-
const { chunks, totalSize } = protocol.encode(uuid, message);
|
|
41
|
-
// chunks: Bytes[] - 전송할 바이너리 청크 배열
|
|
42
|
-
// totalSize: number - 전체 메시지 크기
|
|
42
|
+
import { createServiceProtocol, defineEvent } from "@simplysm/service-common";
|
|
43
43
|
|
|
44
|
-
//
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// 모든 청크 수신 완료
|
|
49
|
-
result.uuid; // string - 메시지 UUID
|
|
50
|
-
result.message; // ServiceMessage - 복원된 메시지
|
|
51
|
-
} else {
|
|
52
|
-
// result.type === "progress" - 청크 수신 중
|
|
53
|
-
result.uuid; // string
|
|
54
|
-
result.totalSize; // number - 전체 크기
|
|
55
|
-
result.completedSize; // number - 수신 완료된 크기
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 사용 완료 후 반드시 dispose 호출 (내부 GC 타이머 해제)
|
|
44
|
+
// Create protocol encoder/decoder
|
|
45
|
+
const protocol = createServiceProtocol();
|
|
46
|
+
const { chunks, totalSize } = protocol.encode(uuid, { name: "auth", body: token });
|
|
47
|
+
const result = protocol.decode(chunks[0]);
|
|
59
48
|
protocol.dispose();
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
#### 헤더 구조 (28바이트, Big Endian)
|
|
63
|
-
|
|
64
|
-
| 오프셋 | 크기 | 필드 |
|
|
65
|
-
|--------|------|------|
|
|
66
|
-
| 0 | 16 | UUID (binary) |
|
|
67
|
-
| 16 | 8 | TotalSize (uint64) |
|
|
68
|
-
| 24 | 4 | Index (uint32) |
|
|
69
|
-
|
|
70
|
-
### 이벤트 정의
|
|
71
|
-
|
|
72
|
-
타입 안전한 클라이언트-서버 이벤트 시스템.
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
import { defineEvent } from "@simplysm/service-common";
|
|
76
|
-
import type { ServiceEventDef } from "@simplysm/service-common";
|
|
77
49
|
|
|
78
|
-
//
|
|
50
|
+
// Define a typed event
|
|
79
51
|
const OrderUpdated = defineEvent<{ orderId: number }, { status: string }>("OrderUpdated");
|
|
80
|
-
|
|
81
|
-
// 서버에서 이벤트 발행
|
|
82
|
-
ctx.socket?.emitEvent(OrderUpdated, { orderId: 123 }, { status: "shipped" });
|
|
83
|
-
|
|
84
|
-
// 클라이언트에서 이벤트 구독
|
|
85
|
-
await client.addEventListener(OrderUpdated, { orderId: 123 }, (data) => {
|
|
86
|
-
// data.status는 string으로 타입 추론됨
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
#### `ServiceEventDef<TInfo, TData>`
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
interface ServiceEventDef<TInfo = unknown, TData = unknown> {
|
|
94
|
-
eventName: string;
|
|
95
|
-
readonly $info: TInfo; // 타입 추출 전용 (런타임 미사용)
|
|
96
|
-
readonly $data: TData; // 타입 추출 전용 (런타임 미사용)
|
|
97
|
-
}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
#### `defineEvent<TInfo, TData>(eventName: string): ServiceEventDef<TInfo, TData>`
|
|
101
|
-
|
|
102
|
-
이벤트 이름과 타입 파라미터로 타입 안전한 이벤트 정의 객체를 생성한다.
|
|
103
|
-
|
|
104
|
-
### 메시지 타입
|
|
105
|
-
|
|
106
|
-
모든 메시지는 `ServiceMessage` 유니온 타입에 포함된다.
|
|
107
|
-
|
|
108
|
-
#### 방향별 메시지 그룹
|
|
109
|
-
|
|
110
|
-
```typescript
|
|
111
|
-
// 서버 → 클라이언트 메시지
|
|
112
|
-
type ServiceServerMessage =
|
|
113
|
-
| ServiceReloadMessage
|
|
114
|
-
| ServiceResponseMessage
|
|
115
|
-
| ServiceErrorMessage
|
|
116
|
-
| ServiceEventMessage;
|
|
117
|
-
|
|
118
|
-
// 서버 → 클라이언트 (progress 포함)
|
|
119
|
-
type ServiceServerRawMessage = ServiceProgressMessage | ServiceServerMessage;
|
|
120
|
-
|
|
121
|
-
// 클라이언트 → 서버 메시지
|
|
122
|
-
type ServiceClientMessage =
|
|
123
|
-
| ServiceRequestMessage
|
|
124
|
-
| ServiceAuthMessage
|
|
125
|
-
| ServiceAddEventListenerMessage
|
|
126
|
-
| ServiceRemoveEventListenerMessage
|
|
127
|
-
| ServiceGetEventListenerInfosMessage
|
|
128
|
-
| ServiceEmitEventMessage;
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
#### 전체 메시지 타입 목록
|
|
132
|
-
|
|
133
|
-
| 타입 | name 필드 | 방향 | 설명 |
|
|
134
|
-
|------|-----------|------|------|
|
|
135
|
-
| `ServiceRequestMessage` | `` `${service}.${method}` `` | C->S | RPC 호출. body: `unknown[]` (파라미터 배열) |
|
|
136
|
-
| `ServiceResponseMessage` | `"response"` | S->C | RPC 응답. body?: `unknown` (결과값, 선택) |
|
|
137
|
-
| `ServiceErrorMessage` | `"error"` | S->C | 에러. body: `{ name, message, code, stack?, detail?, cause? }` |
|
|
138
|
-
| `ServiceAuthMessage` | `"auth"` | C->S | 인증. body: `string` (Bearer 토큰) |
|
|
139
|
-
| `ServiceProgressMessage` | `"progress"` | 양방향 | 청크 진행률. body: `{ totalSize, completedSize }` |
|
|
140
|
-
| `ServiceReloadMessage` | `"reload"` | S->C | 리로드 알림. body: `{ clientName, changedFileSet }` |
|
|
141
|
-
| `ServiceAddEventListenerMessage` | `"evt:add"` | C->S | 이벤트 리스너 등록. body: `{ key, name, info }` |
|
|
142
|
-
| `ServiceRemoveEventListenerMessage` | `"evt:remove"` | C->S | 이벤트 리스너 해제. body: `{ key }` |
|
|
143
|
-
| `ServiceGetEventListenerInfosMessage` | `"evt:gets"` | C->S | 이벤트 리스너 목록 조회. body: `{ name }` |
|
|
144
|
-
| `ServiceEmitEventMessage` | `"evt:emit"` | C->S | 이벤트 발행. body: `{ keys, data }` |
|
|
145
|
-
| `ServiceEventMessage` | `"evt:on"` | S->C | 이벤트 브로드캐스트. body: `{ keys, data }` |
|
|
146
|
-
|
|
147
|
-
### 서비스 인터페이스
|
|
148
|
-
|
|
149
|
-
#### ORM 서비스 (`OrmService`)
|
|
150
|
-
|
|
151
|
-
DB 연결, 트랜잭션, 쿼리 실행을 제공한다. MySQL, MSSQL, PostgreSQL 지원.
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
import type { OrmService, DbConnOptions } from "@simplysm/service-common";
|
|
155
|
-
|
|
156
|
-
interface OrmService {
|
|
157
|
-
getInfo(opt: DbConnOptions & { configName: string }): Promise<{
|
|
158
|
-
dialect: Dialect;
|
|
159
|
-
database?: string;
|
|
160
|
-
schema?: string;
|
|
161
|
-
}>;
|
|
162
|
-
|
|
163
|
-
connect(opt: Record<string, unknown>): Promise<number>;
|
|
164
|
-
close(connId: number): Promise<void>;
|
|
165
|
-
|
|
166
|
-
beginTransaction(connId: number, isolationLevel?: IsolationLevel): Promise<void>;
|
|
167
|
-
commitTransaction(connId: number): Promise<void>;
|
|
168
|
-
rollbackTransaction(connId: number): Promise<void>;
|
|
169
|
-
|
|
170
|
-
executeParametrized(connId: number, query: string, params?: unknown[]): Promise<unknown[][]>;
|
|
171
|
-
executeDefs(
|
|
172
|
-
connId: number,
|
|
173
|
-
defs: QueryDef[],
|
|
174
|
-
options?: (ResultMeta | undefined)[],
|
|
175
|
-
): Promise<unknown[][]>;
|
|
176
|
-
|
|
177
|
-
bulkInsert(
|
|
178
|
-
connId: number,
|
|
179
|
-
tableName: string,
|
|
180
|
-
columnDefs: Record<string, ColumnMeta>,
|
|
181
|
-
records: Record<string, unknown>[],
|
|
182
|
-
): Promise<void>;
|
|
183
|
-
}
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
##### `DbConnOptions`
|
|
187
|
-
|
|
188
|
-
```typescript
|
|
189
|
-
type DbConnOptions = {
|
|
190
|
-
configName?: string;
|
|
191
|
-
config?: Record<string, unknown>;
|
|
192
|
-
};
|
|
193
52
|
```
|
|
194
53
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
```typescript
|
|
198
|
-
import type { AutoUpdateService } from "@simplysm/service-common";
|
|
199
|
-
|
|
200
|
-
interface AutoUpdateService {
|
|
201
|
-
getLastVersion(platform: string): Promise<
|
|
202
|
-
| { version: string; downloadPath: string }
|
|
203
|
-
| undefined
|
|
204
|
-
>;
|
|
205
|
-
}
|
|
206
|
-
```
|
|
54
|
+
## Documentation
|
|
207
55
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
```typescript
|
|
213
|
-
import type {
|
|
214
|
-
SmtpClientSendOption,
|
|
215
|
-
SmtpClientSendByDefaultOption,
|
|
216
|
-
SmtpClientSendAttachment,
|
|
217
|
-
SmtpClientDefaultOptions,
|
|
218
|
-
} from "@simplysm/service-common";
|
|
219
|
-
|
|
220
|
-
// 직접 SMTP 설정 포함하여 메일 전송
|
|
221
|
-
interface SmtpClientSendOption {
|
|
222
|
-
host: string;
|
|
223
|
-
port?: number;
|
|
224
|
-
secure?: boolean;
|
|
225
|
-
user?: string;
|
|
226
|
-
pass?: string;
|
|
227
|
-
from: string;
|
|
228
|
-
to: string;
|
|
229
|
-
cc?: string;
|
|
230
|
-
bcc?: string;
|
|
231
|
-
subject: string;
|
|
232
|
-
html: string;
|
|
233
|
-
attachments?: SmtpClientSendAttachment[];
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// 기본 SMTP 설정 사용 시 (from/host/user/pass 등 생략)
|
|
237
|
-
interface SmtpClientSendByDefaultOption {
|
|
238
|
-
to: string;
|
|
239
|
-
cc?: string;
|
|
240
|
-
bcc?: string;
|
|
241
|
-
subject: string;
|
|
242
|
-
html: string;
|
|
243
|
-
attachments?: SmtpClientSendAttachment[];
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// 기본 SMTP 서버 설정
|
|
247
|
-
interface SmtpClientDefaultOptions {
|
|
248
|
-
senderName: string;
|
|
249
|
-
senderEmail?: string;
|
|
250
|
-
user?: string;
|
|
251
|
-
pass?: string;
|
|
252
|
-
host: string;
|
|
253
|
-
port?: number;
|
|
254
|
-
secure?: boolean;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// 첨부파일
|
|
258
|
-
interface SmtpClientSendAttachment {
|
|
259
|
-
filename: string;
|
|
260
|
-
content?: string | Uint8Array;
|
|
261
|
-
path?: any;
|
|
262
|
-
contentType?: string;
|
|
263
|
-
}
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
### 파일 업로드 결과
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
import type { ServiceUploadResult } from "@simplysm/service-common";
|
|
270
|
-
|
|
271
|
-
interface ServiceUploadResult {
|
|
272
|
-
path: string; // 서버 저장 경로
|
|
273
|
-
filename: string; // 원본 파일명
|
|
274
|
-
size: number; // 파일 크기 (bytes)
|
|
275
|
-
}
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
## 디렉토리 구조
|
|
279
|
-
|
|
280
|
-
```
|
|
281
|
-
src/
|
|
282
|
-
index.ts # 엔트리포인트 (모든 export 재수출)
|
|
283
|
-
define-event.ts # defineEvent(), ServiceEventDef
|
|
284
|
-
types.ts # ServiceUploadResult
|
|
285
|
-
protocol/
|
|
286
|
-
protocol.types.ts # PROTOCOL_CONFIG, 메시지 타입 정의
|
|
287
|
-
create-service-protocol.ts # createServiceProtocol(), ServiceProtocol
|
|
288
|
-
service-types/
|
|
289
|
-
orm-service.types.ts # OrmService, DbConnOptions
|
|
290
|
-
auto-update-service.types.ts # AutoUpdateService
|
|
291
|
-
smtp-client-service.types.ts # SmtpClientSend*, SmtpClientDefault*
|
|
292
|
-
```
|
|
56
|
+
- [Protocol](docs/protocol.md)
|
|
57
|
+
- [Service Types](docs/service-types.md)
|
|
58
|
+
- [Events and Types](docs/events.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/service-common",
|
|
3
|
-
"version": "13.0.
|
|
3
|
+
"version": "13.0.98",
|
|
4
4
|
"description": "Simplysm package - Service module (common)",
|
|
5
5
|
"author": "simplysm",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@simplysm/
|
|
23
|
-
"@simplysm/
|
|
22
|
+
"@simplysm/core-common": "13.0.98",
|
|
23
|
+
"@simplysm/orm-common": "13.0.98"
|
|
24
24
|
}
|
|
25
25
|
}
|