@simplysm/service-server 13.0.100 → 14.0.4
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 +79 -133
- package/dist/auth/auth-token-payload.js +2 -1
- package/dist/auth/auth-token-payload.js.map +1 -6
- package/dist/auth/jwt-manager.js +21 -21
- package/dist/auth/jwt-manager.js.map +1 -6
- package/dist/core/define-service.d.ts +12 -12
- package/dist/core/define-service.d.ts.map +1 -1
- package/dist/core/define-service.js +77 -63
- package/dist/core/define-service.js.map +1 -6
- package/dist/core/service-executor.d.ts.map +1 -1
- package/dist/core/service-executor.js +42 -32
- package/dist/core/service-executor.js.map +1 -6
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -6
- package/dist/legacy/v1-auto-update-handler.d.ts +2 -2
- package/dist/legacy/v1-auto-update-handler.js +42 -35
- package/dist/legacy/v1-auto-update-handler.js.map +1 -6
- package/dist/protocol/protocol-wrapper.d.ts +9 -9
- package/dist/protocol/protocol-wrapper.js +64 -46
- package/dist/protocol/protocol-wrapper.js.map +1 -6
- package/dist/service-server.d.ts +2 -1
- package/dist/service-server.d.ts.map +1 -1
- package/dist/service-server.js +183 -165
- package/dist/service-server.js.map +1 -6
- package/dist/services/auto-update-service.js +35 -34
- package/dist/services/auto-update-service.js.map +1 -6
- package/dist/services/orm-service.js +114 -120
- package/dist/services/orm-service.js.map +1 -6
- package/dist/transport/http/http-request-handler.d.ts.map +1 -1
- package/dist/transport/http/http-request-handler.js +58 -46
- package/dist/transport/http/http-request-handler.js.map +1 -6
- package/dist/transport/http/static-file-handler.js +42 -39
- package/dist/transport/http/static-file-handler.js.map +1 -6
- package/dist/transport/http/upload-handler.d.ts.map +1 -1
- package/dist/transport/http/upload-handler.js +60 -55
- package/dist/transport/http/upload-handler.js.map +1 -6
- package/dist/transport/socket/service-socket.d.ts +13 -13
- package/dist/transport/socket/service-socket.js +132 -108
- package/dist/transport/socket/service-socket.js.map +1 -6
- package/dist/transport/socket/websocket-handler.d.ts +9 -13
- package/dist/transport/socket/websocket-handler.d.ts.map +1 -1
- package/dist/transport/socket/websocket-handler.js +148 -139
- package/dist/transport/socket/websocket-handler.js.map +1 -6
- package/dist/types/server-options.d.ts +1 -1
- package/dist/types/server-options.d.ts.map +1 -1
- package/dist/types/server-options.js +2 -1
- package/dist/types/server-options.js.map +1 -6
- package/dist/utils/config-manager.js +48 -46
- package/dist/utils/config-manager.js.map +1 -6
- package/dist/workers/service-protocol.worker.js +8 -11
- package/dist/workers/service-protocol.worker.js.map +1 -6
- package/docs/auth.md +28 -16
- package/docs/core.md +113 -54
- package/docs/legacy.md +21 -0
- package/docs/main.md +81 -0
- package/docs/protocol.md +31 -0
- package/docs/services.md +49 -44
- package/docs/transport.md +81 -76
- package/docs/types.md +31 -0
- package/docs/utilities.md +27 -0
- package/package.json +12 -13
- package/src/auth/jwt-manager.ts +2 -2
- package/src/core/define-service.ts +19 -19
- package/src/core/service-executor.ts +23 -17
- package/src/index.ts +10 -12
- package/src/legacy/v1-auto-update-handler.ts +10 -10
- package/src/protocol/protocol-wrapper.ts +16 -16
- package/src/service-server.ts +51 -43
- package/src/services/auto-update-service.ts +1 -1
- package/src/services/orm-service.ts +7 -7
- package/src/transport/http/http-request-handler.ts +16 -10
- package/src/transport/http/static-file-handler.ts +8 -8
- package/src/transport/http/upload-handler.ts +16 -9
- package/src/transport/socket/service-socket.ts +22 -22
- package/src/transport/socket/websocket-handler.ts +50 -70
- package/src/types/server-options.ts +1 -1
- package/src/utils/config-manager.ts +11 -11
- package/dist/services/smtp-client-service.d.ts +0 -8
- package/dist/services/smtp-client-service.d.ts.map +0 -1
- package/dist/services/smtp-client-service.js +0 -46
- package/dist/services/smtp-client-service.js.map +0 -6
- package/docs/server.md +0 -126
- package/src/services/smtp-client-service.ts +0 -59
- package/tests/define-service.spec.ts +0 -66
- package/tests/orm-service.spec.ts +0 -83
- package/tests/service-executor.spec.ts +0 -114
package/docs/core.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
# Core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Service definition, context, authentication wrapper, and method execution.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## ServiceContext\<TAuthInfo\>
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Context object passed to service factory functions. Provides access to the server, socket, HTTP info, and authentication state.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
8
10
|
interface ServiceContext<TAuthInfo = unknown> {
|
|
9
11
|
server: ServiceServer<TAuthInfo>;
|
|
10
12
|
socket?: ServiceSocket;
|
|
@@ -15,7 +17,6 @@ interface ServiceContext<TAuthInfo = unknown> {
|
|
|
15
17
|
legacy?: {
|
|
16
18
|
clientName?: string;
|
|
17
19
|
};
|
|
18
|
-
|
|
19
20
|
get authInfo(): TAuthInfo | undefined;
|
|
20
21
|
get clientName(): string | undefined;
|
|
21
22
|
get clientPath(): string | undefined;
|
|
@@ -23,22 +24,24 @@ interface ServiceContext<TAuthInfo = unknown> {
|
|
|
23
24
|
}
|
|
24
25
|
```
|
|
25
26
|
|
|
26
|
-
|
|
|
27
|
-
|
|
28
|
-
| `server` | `ServiceServer<TAuthInfo>` |
|
|
29
|
-
| `socket` | `ServiceSocket
|
|
30
|
-
| `http` | `
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
34
|
-
| `
|
|
35
|
-
| `
|
|
27
|
+
| Member | Kind | Type | Description |
|
|
28
|
+
|--------|------|------|-------------|
|
|
29
|
+
| `server` | property | `ServiceServer<TAuthInfo>` | Reference to the server instance |
|
|
30
|
+
| `socket` | property | `ServiceSocket?` | WebSocket connection (present for WS calls) |
|
|
31
|
+
| `http` | property | `{ clientName; authTokenPayload? }?` | HTTP request info (present for HTTP calls) |
|
|
32
|
+
| `http.clientName` | field | `string` | Client identifier from HTTP header |
|
|
33
|
+
| `http.authTokenPayload` | field | `AuthTokenPayload<TAuthInfo>?` | Decoded auth token from HTTP request |
|
|
34
|
+
| `legacy` | property | `{ clientName? }?` | Legacy V1 connection info |
|
|
35
|
+
| `authInfo` | getter | `TAuthInfo \| undefined` | Authenticated user data (from socket or HTTP token) |
|
|
36
|
+
| `clientName` | getter | `string \| undefined` | Client name (from socket, HTTP, or legacy) |
|
|
37
|
+
| `clientPath` | getter | `string \| undefined` | Client path identifier |
|
|
38
|
+
| `getConfig` | method | `<T>(section: string) => Promise<T>` | Read a configuration section |
|
|
36
39
|
|
|
37
|
-
##
|
|
40
|
+
## createServiceContext
|
|
38
41
|
|
|
39
|
-
Create a
|
|
42
|
+
Create a `ServiceContext` instance.
|
|
40
43
|
|
|
41
|
-
```
|
|
44
|
+
```ts
|
|
42
45
|
function createServiceContext<TAuthInfo = unknown>(
|
|
43
46
|
server: ServiceServer<TAuthInfo>,
|
|
44
47
|
socket?: ServiceSocket,
|
|
@@ -47,11 +50,59 @@ function createServiceContext<TAuthInfo = unknown>(
|
|
|
47
50
|
): ServiceContext<TAuthInfo>;
|
|
48
51
|
```
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
| Parameter | Type | Description |
|
|
54
|
+
|-----------|------|-------------|
|
|
55
|
+
| `server` | `ServiceServer<TAuthInfo>` | Server instance |
|
|
56
|
+
| `socket` | `ServiceSocket?` | WebSocket connection |
|
|
57
|
+
| `http` | `{ clientName; authTokenPayload? }?` | HTTP request context |
|
|
58
|
+
| `legacy` | `{ clientName? }?` | Legacy V1 context |
|
|
59
|
+
|
|
60
|
+
## auth
|
|
51
61
|
|
|
52
|
-
|
|
62
|
+
Authentication wrapper for service factories and methods. Marks a function as requiring authentication. Optionally restricts access to specific roles.
|
|
53
63
|
|
|
54
|
-
```
|
|
64
|
+
```ts
|
|
65
|
+
// No role restriction -- just requires authentication
|
|
66
|
+
function auth<TFunction extends (...args: any[]) => any>(fn: TFunction): TFunction;
|
|
67
|
+
|
|
68
|
+
// With role restriction
|
|
69
|
+
function auth<TFunction extends (...args: any[]) => any>(permissions: string[], fn: TFunction): TFunction;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Can be applied at the service level (wrapping the factory) or at individual method level.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const MyService = defineService("My", (ctx) => ({
|
|
76
|
+
// Public method -- no auth required
|
|
77
|
+
publicMethod() { return "open"; },
|
|
78
|
+
|
|
79
|
+
// Requires authentication (any role)
|
|
80
|
+
protectedMethod: auth(() => "protected"),
|
|
81
|
+
|
|
82
|
+
// Requires "admin" role
|
|
83
|
+
adminMethod: auth(["admin"], () => "admin only"),
|
|
84
|
+
}));
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## getServiceAuthPermissions
|
|
88
|
+
|
|
89
|
+
Read the authentication permissions from a function wrapped with `auth()`. Returns `undefined` if the function is not wrapped.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
function getServiceAuthPermissions(fn: Function): string[] | undefined;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
| Parameter | Type | Description |
|
|
96
|
+
|-----------|------|-------------|
|
|
97
|
+
| `fn` | `Function` | A potentially auth-wrapped function |
|
|
98
|
+
|
|
99
|
+
Returns `string[]` (role list, possibly empty for auth-only) or `undefined` (not wrapped).
|
|
100
|
+
|
|
101
|
+
## ServiceDefinition\<TMethods\>
|
|
102
|
+
|
|
103
|
+
A named service with a factory function and optional auth permissions.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
55
106
|
interface ServiceDefinition<TMethods = Record<string, (...args: any[]) => any>> {
|
|
56
107
|
name: string;
|
|
57
108
|
factory: (ctx: ServiceContext) => TMethods;
|
|
@@ -61,15 +112,15 @@ interface ServiceDefinition<TMethods = Record<string, (...args: any[]) => any>>
|
|
|
61
112
|
|
|
62
113
|
| Field | Type | Description |
|
|
63
114
|
|-------|------|-------------|
|
|
64
|
-
| `name` | `string` | Service name |
|
|
65
|
-
| `factory` | `(ctx: ServiceContext) => TMethods` | Factory function that creates
|
|
66
|
-
| `authPermissions` | `string[]
|
|
115
|
+
| `name` | `string` | Service name used for RPC routing |
|
|
116
|
+
| `factory` | `(ctx: ServiceContext) => TMethods` | Factory function that creates service methods |
|
|
117
|
+
| `authPermissions` | `string[]?` | Service-level auth permissions (from `auth()` wrapper) |
|
|
67
118
|
|
|
68
|
-
##
|
|
119
|
+
## defineService
|
|
69
120
|
|
|
70
|
-
Define a service
|
|
121
|
+
Define a named service from a factory function.
|
|
71
122
|
|
|
72
|
-
```
|
|
123
|
+
```ts
|
|
73
124
|
function defineService<TMethods extends Record<string, (...args: any[]) => any>>(
|
|
74
125
|
name: string,
|
|
75
126
|
factory: (ctx: ServiceContext) => TMethods,
|
|
@@ -79,47 +130,44 @@ function defineService<TMethods extends Record<string, (...args: any[]) => any>>
|
|
|
79
130
|
| Parameter | Type | Description |
|
|
80
131
|
|-----------|------|-------------|
|
|
81
132
|
| `name` | `string` | Service name |
|
|
82
|
-
| `factory` | `(ctx: ServiceContext) => TMethods` | Factory
|
|
133
|
+
| `factory` | `(ctx: ServiceContext) => TMethods` | Factory that receives context and returns methods |
|
|
83
134
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
permissions: string[],
|
|
92
|
-
fn: TFunction,
|
|
93
|
-
): TFunction;
|
|
135
|
+
```ts
|
|
136
|
+
const UserService = defineService("User", (ctx) => ({
|
|
137
|
+
async getUser(id: number) {
|
|
138
|
+
// ctx.authInfo, ctx.server, ctx.getConfig, etc.
|
|
139
|
+
return { id, name: "Alice" };
|
|
140
|
+
},
|
|
141
|
+
}));
|
|
94
142
|
```
|
|
95
143
|
|
|
96
|
-
|
|
97
|
-
|----------|-------------|
|
|
98
|
-
| `auth(fn)` | Require login (any authenticated user) |
|
|
99
|
-
| `auth(["admin"], fn)` | Require specific roles |
|
|
100
|
-
|
|
101
|
-
## `getServiceAuthPermissions`
|
|
144
|
+
## ServiceMethods\<TDefinition\>
|
|
102
145
|
|
|
103
|
-
|
|
146
|
+
Utility type that extracts method signatures from a `ServiceDefinition`. Useful for sharing types with the client.
|
|
104
147
|
|
|
105
|
-
```
|
|
106
|
-
|
|
148
|
+
```ts
|
|
149
|
+
type ServiceMethods<TDefinition> = TDefinition extends ServiceDefinition<infer M> ? M : never;
|
|
107
150
|
```
|
|
108
151
|
|
|
109
|
-
|
|
152
|
+
```ts
|
|
153
|
+
// Server
|
|
154
|
+
const UserService = defineService("User", (ctx) => ({
|
|
155
|
+
getUser(id: number) { return { id, name: "Alice" }; },
|
|
156
|
+
}));
|
|
110
157
|
|
|
111
|
-
|
|
158
|
+
// Shared type for client
|
|
159
|
+
type UserServiceType = ServiceMethods<typeof UserService>;
|
|
112
160
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
161
|
+
// Client
|
|
162
|
+
const userSvc = client.getService<UserServiceType>("User");
|
|
163
|
+
const user = await userSvc.getUser(1); // typed as { id: number; name: string }
|
|
116
164
|
```
|
|
117
165
|
|
|
118
|
-
##
|
|
166
|
+
## executeServiceMethod
|
|
119
167
|
|
|
120
|
-
Execute a service method
|
|
168
|
+
Execute a service method by name. Used internally by the transport layer; can also be called directly for testing or custom routing.
|
|
121
169
|
|
|
122
|
-
```
|
|
170
|
+
```ts
|
|
123
171
|
async function executeServiceMethod(
|
|
124
172
|
server: ServiceServer,
|
|
125
173
|
def: {
|
|
@@ -131,3 +179,14 @@ async function executeServiceMethod(
|
|
|
131
179
|
},
|
|
132
180
|
): Promise<unknown>;
|
|
133
181
|
```
|
|
182
|
+
|
|
183
|
+
| Parameter | Type | Description |
|
|
184
|
+
|-----------|------|-------------|
|
|
185
|
+
| `server` | `ServiceServer` | Server instance containing registered services |
|
|
186
|
+
| `def.serviceName` | `string` | Name of the service to invoke |
|
|
187
|
+
| `def.methodName` | `string` | Method name within the service |
|
|
188
|
+
| `def.params` | `unknown[]` | Method arguments |
|
|
189
|
+
| `def.socket` | `ServiceSocket?` | WebSocket context (for WS calls) |
|
|
190
|
+
| `def.http` | `{ clientName; authTokenPayload? }?` | HTTP context (for HTTP calls) |
|
|
191
|
+
|
|
192
|
+
Returns the method return value.
|
package/docs/legacy.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Legacy
|
|
2
|
+
|
|
3
|
+
## handleV1Connection
|
|
4
|
+
|
|
5
|
+
V1 legacy client handler. Only supports auto-update requests; all other requests receive an upgrade-required error.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
function handleV1Connection(
|
|
9
|
+
socket: WebSocket,
|
|
10
|
+
autoUpdateMethods: { getLastVersion: (platform: string) => Promise<any> },
|
|
11
|
+
clientNameSetter?: (clientName: string | undefined) => void,
|
|
12
|
+
): void;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
| Parameter | Type | Description |
|
|
16
|
+
|-----------|------|-------------|
|
|
17
|
+
| `socket` | `WebSocket` | Raw WebSocket connection from the V1 client |
|
|
18
|
+
| `autoUpdateMethods` | `{ getLastVersion: (platform: string) => Promise<any> }` | Auto-update method implementation |
|
|
19
|
+
| `clientNameSetter` | `((clientName: string \| undefined) => void)?` | Optional callback invoked with the client name when connected (or `undefined` on disconnect) |
|
|
20
|
+
|
|
21
|
+
This handler is provided for backward compatibility with V1 protocol clients. New clients should use the V2 binary protocol.
|
package/docs/main.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Main
|
|
2
|
+
|
|
3
|
+
## ServiceServer\<TAuthInfo\>
|
|
4
|
+
|
|
5
|
+
Main server class. Extends `EventEmitter` with `ready` and `close` events. Manages Fastify, WebSocket connections, service routing, and event broadcasting.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
class ServiceServer<TAuthInfo = unknown> extends EventEmitter<{
|
|
9
|
+
ready: void;
|
|
10
|
+
close: void;
|
|
11
|
+
}> {
|
|
12
|
+
constructor(options: ServiceServerOptions);
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Events
|
|
17
|
+
|
|
18
|
+
| Event | Data Type | Description |
|
|
19
|
+
|-------|-----------|-------------|
|
|
20
|
+
| `ready` | `void` | Emitted when the server is listening and ready |
|
|
21
|
+
| `close` | `void` | Emitted when the server has shut down |
|
|
22
|
+
|
|
23
|
+
### Properties
|
|
24
|
+
|
|
25
|
+
| Property | Type | Description |
|
|
26
|
+
|----------|------|-------------|
|
|
27
|
+
| `isOpen` | `boolean` | Whether the server is currently listening |
|
|
28
|
+
| `fastify` | `FastifyInstance` | Underlying Fastify instance for advanced configuration |
|
|
29
|
+
| `options` | `ServiceServerOptions` | Server configuration options |
|
|
30
|
+
|
|
31
|
+
### Methods
|
|
32
|
+
|
|
33
|
+
| Method | Parameters | Return | Description |
|
|
34
|
+
|--------|-----------|--------|-------------|
|
|
35
|
+
| `listen` | -- | `Promise<void>` | Start the server and begin accepting connections |
|
|
36
|
+
| `close` | -- | `Promise<void>` | Gracefully shut down the server |
|
|
37
|
+
| `emitEvent` | `eventDef: ServiceEventDef<TInfo, TData>`, `infoSelector: (item: TInfo) => boolean`, `data: TData` | `Promise<void>` | Broadcast an event to matching listeners across all connected sockets |
|
|
38
|
+
| `signAuthToken` | `payload: AuthTokenPayload<TAuthInfo>` | `Promise<string>` | Sign a JWT token using the server's configured secret |
|
|
39
|
+
| `verifyAuthToken` | `token: string` | `Promise<AuthTokenPayload<TAuthInfo>>` | Verify and decode a JWT token |
|
|
40
|
+
|
|
41
|
+
### Usage
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { createServiceServer, defineService } from "@simplysm/service-server";
|
|
45
|
+
|
|
46
|
+
const server = createServiceServer({
|
|
47
|
+
rootPath: process.cwd(),
|
|
48
|
+
port: 3000,
|
|
49
|
+
auth: { jwtSecret: "secret" },
|
|
50
|
+
services: [
|
|
51
|
+
defineService("Hello", () => ({
|
|
52
|
+
greet(name: string) { return `Hello, ${name}!`; },
|
|
53
|
+
})),
|
|
54
|
+
],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
server.on("ready", () => {
|
|
58
|
+
// server is listening
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await server.listen();
|
|
62
|
+
|
|
63
|
+
// Later: shut down
|
|
64
|
+
await server.close();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## createServiceServer
|
|
68
|
+
|
|
69
|
+
Factory function to create a `ServiceServer` instance.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
function createServiceServer<TAuthInfo = unknown>(
|
|
73
|
+
options: ServiceServerOptions,
|
|
74
|
+
): ServiceServer<TAuthInfo>;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
| Parameter | Type | Description |
|
|
78
|
+
|-----------|------|-------------|
|
|
79
|
+
| `options` | `ServiceServerOptions` | Server configuration |
|
|
80
|
+
|
|
81
|
+
Returns a new `ServiceServer` instance. Call `listen()` to start accepting connections.
|
package/docs/protocol.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Protocol
|
|
2
|
+
|
|
3
|
+
Server-side protocol wrapper that automatically delegates heavy message encoding/decoding to worker threads.
|
|
4
|
+
|
|
5
|
+
## ServerProtocolWrapper
|
|
6
|
+
|
|
7
|
+
Protocol wrapper interface. Heavy messages are encoded/decoded in a worker thread; lightweight messages are processed on the main thread.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
interface ServerProtocolWrapper {
|
|
11
|
+
encode(uuid: string, message: ServiceMessage): Promise<{ chunks: Bytes[]; totalSize: number }>;
|
|
12
|
+
decode(bytes: Bytes): Promise<ServiceMessageDecodeResult<ServiceMessage>>;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
| Method | Parameters | Return | Description |
|
|
18
|
+
|--------|-----------|--------|-------------|
|
|
19
|
+
| `encode` | `uuid: string`, `message: ServiceMessage` | `Promise<{ chunks: Bytes[]; totalSize: number }>` | Encode a message into binary chunks |
|
|
20
|
+
| `decode` | `bytes: Bytes` | `Promise<ServiceMessageDecodeResult<ServiceMessage>>` | Decode received binary data |
|
|
21
|
+
| `dispose` | -- | `void` | Dispose the wrapper and terminate worker threads |
|
|
22
|
+
|
|
23
|
+
## createServerProtocolWrapper
|
|
24
|
+
|
|
25
|
+
Create a `ServerProtocolWrapper` instance.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
function createServerProtocolWrapper(): ServerProtocolWrapper;
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Returns a new protocol wrapper with automatic worker thread delegation for heavy payloads.
|
package/docs/services.md
CHANGED
|
@@ -1,58 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Services
|
|
2
|
+
|
|
3
|
+
Built-in service definitions that can be included in `ServiceServerOptions.services`.
|
|
4
|
+
|
|
5
|
+
## OrmService
|
|
6
|
+
|
|
7
|
+
Built-in ORM service definition. Provides database connection management, transaction control, and query execution over the RPC layer. Supports MySQL, MSSQL, and PostgreSQL.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const OrmService: ServiceDefinition<{
|
|
11
|
+
getInfo(opt: DbConnOptions & { configName: string }): Promise<{ dialect: Dialect; database?: string; schema?: string }>;
|
|
12
|
+
connect(opt: DbConnOptions & { configName: string }): Promise<number>;
|
|
13
|
+
close(connId: number): Promise<void>;
|
|
14
|
+
beginTransaction(connId: number, isolationLevel?: IsolationLevel): Promise<void>;
|
|
15
|
+
commitTransaction(connId: number): Promise<void>;
|
|
16
|
+
rollbackTransaction(connId: number): Promise<void>;
|
|
17
|
+
executeParametrized(connId: number, query: string, params?: unknown[]): Promise<unknown[][]>;
|
|
18
|
+
executeDefs(connId: number, defs: QueryDef[], options?: (ResultMeta | undefined)[]): Promise<unknown[][]>;
|
|
19
|
+
bulkInsert(connId: number, tableName: string, columnDefs: Record<string, ColumnMeta>, records: Record<string, unknown>[]): Promise<void>;
|
|
20
|
+
}>;
|
|
21
|
+
```
|
|
2
22
|
|
|
3
|
-
|
|
23
|
+
| Method | Parameters | Return | Description |
|
|
24
|
+
|--------|-----------|--------|-------------|
|
|
25
|
+
| `getInfo` | `opt` | `Promise<{ dialect; database?; schema? }>` | Get database dialect and schema info |
|
|
26
|
+
| `connect` | `opt` | `Promise<number>` | Open a connection; returns connection ID |
|
|
27
|
+
| `close` | `connId` | `Promise<void>` | Close a connection |
|
|
28
|
+
| `beginTransaction` | `connId`, `isolationLevel?` | `Promise<void>` | Begin a transaction |
|
|
29
|
+
| `commitTransaction` | `connId` | `Promise<void>` | Commit the current transaction |
|
|
30
|
+
| `rollbackTransaction` | `connId` | `Promise<void>` | Rollback the current transaction |
|
|
31
|
+
| `executeParametrized` | `connId`, `query`, `params?` | `Promise<unknown[][]>` | Execute a parameterized SQL query |
|
|
32
|
+
| `executeDefs` | `connId`, `defs`, `options?` | `Promise<unknown[][]>` | Execute query definitions |
|
|
33
|
+
| `bulkInsert` | `connId`, `tableName`, `columnDefs`, `records` | `Promise<void>` | Bulk insert records into a table |
|
|
4
34
|
|
|
5
|
-
|
|
35
|
+
### OrmServiceType
|
|
6
36
|
|
|
7
|
-
|
|
37
|
+
Utility type for extracting ORM service method signatures.
|
|
8
38
|
|
|
9
|
-
```
|
|
10
|
-
|
|
39
|
+
```ts
|
|
40
|
+
type OrmServiceType = ServiceMethods<typeof OrmService>;
|
|
11
41
|
```
|
|
12
42
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
| Method | Description |
|
|
16
|
-
|--------|-------------|
|
|
17
|
-
| `getInfo()` | Get database dialect and connection info from config |
|
|
18
|
-
| `connect()` | Open a new database connection, returns connection ID |
|
|
19
|
-
| `close()` | Close a database connection |
|
|
20
|
-
| `beginTransaction()` | Begin transaction |
|
|
21
|
-
| `commitTransaction()` | Commit transaction |
|
|
22
|
-
| `rollbackTransaction()` | Rollback transaction |
|
|
23
|
-
| `executeParametrized()` | Execute parameterized SQL |
|
|
24
|
-
| `executeDefs()` | Execute QueryDef array (builds SQL via QueryBuilder) |
|
|
25
|
-
| `bulkInsert()` | Bulk insert records |
|
|
26
|
-
|
|
27
|
-
**Note:** ORM service requires WebSocket connection (cannot be used over HTTP).
|
|
28
|
-
|
|
29
|
-
## `AutoUpdateService`
|
|
43
|
+
## AutoUpdateService
|
|
30
44
|
|
|
31
|
-
|
|
45
|
+
Built-in auto-update service definition. Retrieves the latest version information for client applications by platform.
|
|
32
46
|
|
|
33
|
-
```
|
|
34
|
-
const AutoUpdateService: ServiceDefinition
|
|
47
|
+
```ts
|
|
48
|
+
const AutoUpdateService: ServiceDefinition<{
|
|
49
|
+
getLastVersion(platform: string): Promise<{ version: string; downloadPath: string } | undefined>;
|
|
50
|
+
}>;
|
|
35
51
|
```
|
|
36
52
|
|
|
37
|
-
|
|
53
|
+
| Method | Parameters | Return | Description |
|
|
54
|
+
|--------|-----------|--------|-------------|
|
|
55
|
+
| `getLastVersion` | `platform: string` | `Promise<{ version; downloadPath } \| undefined>` | Get latest version info for a platform |
|
|
38
56
|
|
|
39
|
-
|
|
40
|
-
|--------|-------------|
|
|
41
|
-
| `getLastVersion(platform)` | Find latest version file for platform (win32, android, etc.) |
|
|
57
|
+
### AutoUpdateServiceType
|
|
42
58
|
|
|
43
|
-
|
|
44
|
-
- `android`: `.apk` files
|
|
45
|
-
- Other platforms: `.exe` files
|
|
59
|
+
Utility type for extracting auto-update service method signatures.
|
|
46
60
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
Server-side SMTP email sending service. Uses `nodemailer` under the hood.
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
const SmtpClientService: ServiceDefinition;
|
|
61
|
+
```ts
|
|
62
|
+
type AutoUpdateServiceType = ServiceMethods<typeof AutoUpdateService>;
|
|
53
63
|
```
|
|
54
|
-
|
|
55
|
-
| Method | Description |
|
|
56
|
-
|--------|-------------|
|
|
57
|
-
| `send(options)` | Send email with full SMTP options |
|
|
58
|
-
| `sendByDefault(options)` | Send email using server's default SMTP config |
|