@tinycloudlabs/sdk-services 1.0.0
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/dist/base/BaseService.d.ts +151 -0
- package/dist/base/BaseService.d.ts.map +1 -0
- package/dist/base/BaseService.js +221 -0
- package/dist/base/BaseService.js.map +1 -0
- package/dist/base/index.d.ts +6 -0
- package/dist/base/index.d.ts.map +1 -0
- package/dist/base/index.js +6 -0
- package/dist/base/index.js.map +1 -0
- package/dist/base/types.d.ts +36 -0
- package/dist/base/types.d.ts.map +1 -0
- package/dist/base/types.js +7 -0
- package/dist/base/types.js.map +1 -0
- package/dist/context.d.ts +142 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +218 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +43 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +111 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/dist/kv/IKVService.d.ts +148 -0
- package/dist/kv/IKVService.d.ts.map +1 -0
- package/dist/kv/IKVService.js +8 -0
- package/dist/kv/IKVService.js.map +1 -0
- package/dist/kv/KVService.d.ts +153 -0
- package/dist/kv/KVService.d.ts.map +1 -0
- package/dist/kv/KVService.js +337 -0
- package/dist/kv/KVService.js.map +1 -0
- package/dist/kv/PrefixedKVService.d.ts +246 -0
- package/dist/kv/PrefixedKVService.d.ts.map +1 -0
- package/dist/kv/PrefixedKVService.js +145 -0
- package/dist/kv/PrefixedKVService.js.map +1 -0
- package/dist/kv/index.d.ts +10 -0
- package/dist/kv/index.d.ts.map +1 -0
- package/dist/kv/index.js +12 -0
- package/dist/kv/index.js.map +1 -0
- package/dist/kv/types.d.ts +204 -0
- package/dist/kv/types.d.ts.map +1 -0
- package/dist/kv/types.js +16 -0
- package/dist/kv/types.js.map +1 -0
- package/dist/types.d.ts +259 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +72 -0
- package/dist/types.js.map +1 -0
- package/dist/types.schema.d.ts +652 -0
- package/dist/types.schema.d.ts.map +1 -0
- package/dist/types.schema.js +342 -0
- package/dist/types.schema.js.map +1 -0
- package/dist/types.schema.test.d.ts +5 -0
- package/dist/types.schema.test.d.ts.map +1 -0
- package/dist/types.schema.test.js +677 -0
- package/dist/types.schema.test.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BaseService - Abstract base class for all TinyCloud services.
|
|
3
|
+
*
|
|
4
|
+
* Provides common functionality:
|
|
5
|
+
* - Context management
|
|
6
|
+
* - Session lifecycle hooks
|
|
7
|
+
* - Abort signal handling
|
|
8
|
+
* - Telemetry emission
|
|
9
|
+
*/
|
|
10
|
+
import { IService, IServiceContext, ServiceSession, ServiceError, Result } from "../types";
|
|
11
|
+
/**
|
|
12
|
+
* Abstract base class for TinyCloud services.
|
|
13
|
+
*
|
|
14
|
+
* Services extend this class to get common functionality like
|
|
15
|
+
* context management, session lifecycle, and abort handling.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* class MyService extends BaseService implements IMyService {
|
|
20
|
+
* static readonly serviceName = 'myservice';
|
|
21
|
+
*
|
|
22
|
+
* constructor(config: MyServiceConfig = {}) {
|
|
23
|
+
* super();
|
|
24
|
+
* this._config = config;
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* async doSomething(): Promise<Result<Data>> {
|
|
28
|
+
* if (!this.requireAuth()) {
|
|
29
|
+
* return err(authRequiredError('myservice'));
|
|
30
|
+
* }
|
|
31
|
+
* // ... implementation
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare abstract class BaseService implements IService {
|
|
37
|
+
/**
|
|
38
|
+
* Service identifier used for registration.
|
|
39
|
+
* Must be overridden by subclasses.
|
|
40
|
+
*/
|
|
41
|
+
static readonly serviceName: string;
|
|
42
|
+
/**
|
|
43
|
+
* Service context providing access to platform dependencies.
|
|
44
|
+
* Set during initialize().
|
|
45
|
+
*/
|
|
46
|
+
protected context: IServiceContext;
|
|
47
|
+
/**
|
|
48
|
+
* Abort controller for this service's operations.
|
|
49
|
+
* Reset on sign-out.
|
|
50
|
+
*/
|
|
51
|
+
protected abortController: AbortController;
|
|
52
|
+
/**
|
|
53
|
+
* Service-specific configuration.
|
|
54
|
+
*/
|
|
55
|
+
protected _config: Record<string, unknown>;
|
|
56
|
+
/**
|
|
57
|
+
* Get the service configuration.
|
|
58
|
+
*/
|
|
59
|
+
get config(): Record<string, unknown>;
|
|
60
|
+
/**
|
|
61
|
+
* Initialize the service with context.
|
|
62
|
+
* Called by the SDK after instantiation.
|
|
63
|
+
*
|
|
64
|
+
* @param context - The service context
|
|
65
|
+
*/
|
|
66
|
+
initialize(context: IServiceContext): void;
|
|
67
|
+
/**
|
|
68
|
+
* Called when session changes (sign-in, sign-out, refresh).
|
|
69
|
+
* Override in subclasses to handle session changes.
|
|
70
|
+
*
|
|
71
|
+
* @param session - The new session, or null if signed out
|
|
72
|
+
*/
|
|
73
|
+
onSessionChange(session: ServiceSession | null): void;
|
|
74
|
+
/**
|
|
75
|
+
* Called when SDK signs out.
|
|
76
|
+
* Aborts all pending operations.
|
|
77
|
+
*/
|
|
78
|
+
onSignOut(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Get the abort signal for this service.
|
|
81
|
+
* Combines the service-level abort with context-level abort.
|
|
82
|
+
*/
|
|
83
|
+
protected get abortSignal(): AbortSignal;
|
|
84
|
+
/**
|
|
85
|
+
* Check if the service is authenticated.
|
|
86
|
+
*/
|
|
87
|
+
protected get isAuthenticated(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Get the current session.
|
|
90
|
+
* Throws if not authenticated.
|
|
91
|
+
*/
|
|
92
|
+
protected get session(): ServiceSession;
|
|
93
|
+
/**
|
|
94
|
+
* Check authentication and return error result if not authenticated.
|
|
95
|
+
* Use this at the start of methods that require authentication.
|
|
96
|
+
*
|
|
97
|
+
* @returns true if authenticated, false otherwise
|
|
98
|
+
*/
|
|
99
|
+
protected requireAuth(): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Emit a telemetry event.
|
|
102
|
+
*
|
|
103
|
+
* @param event - Event name
|
|
104
|
+
* @param data - Event data
|
|
105
|
+
*/
|
|
106
|
+
protected emit(event: string, data: unknown): void;
|
|
107
|
+
/**
|
|
108
|
+
* Emit a service request event.
|
|
109
|
+
*
|
|
110
|
+
* @param action - The action being performed
|
|
111
|
+
* @param key - Optional key/path being accessed
|
|
112
|
+
*/
|
|
113
|
+
protected emitRequest(action: string, key?: string): void;
|
|
114
|
+
/**
|
|
115
|
+
* Emit a service response event.
|
|
116
|
+
*
|
|
117
|
+
* @param action - The action that was performed
|
|
118
|
+
* @param ok - Whether the request was successful
|
|
119
|
+
* @param startTime - Start time for duration calculation
|
|
120
|
+
* @param status - Optional HTTP status code
|
|
121
|
+
*/
|
|
122
|
+
protected emitResponse(action: string, ok: boolean, startTime: number, status?: number): void;
|
|
123
|
+
/**
|
|
124
|
+
* Emit a service error event.
|
|
125
|
+
*
|
|
126
|
+
* @param error - The service error
|
|
127
|
+
*/
|
|
128
|
+
protected emitError(error: ServiceError): void;
|
|
129
|
+
/**
|
|
130
|
+
* Get the service name from the static property.
|
|
131
|
+
* Subclasses must define static serviceName.
|
|
132
|
+
*/
|
|
133
|
+
protected getServiceName(): string;
|
|
134
|
+
/**
|
|
135
|
+
* Create a combined abort signal from multiple sources.
|
|
136
|
+
*
|
|
137
|
+
* @param signals - Additional abort signals to combine
|
|
138
|
+
* @returns A combined abort signal
|
|
139
|
+
*/
|
|
140
|
+
protected combineSignals(...signals: (AbortSignal | undefined)[]): AbortSignal;
|
|
141
|
+
/**
|
|
142
|
+
* Wrap an operation with error handling and telemetry.
|
|
143
|
+
*
|
|
144
|
+
* @param action - The action name for telemetry
|
|
145
|
+
* @param key - Optional key for telemetry
|
|
146
|
+
* @param operation - The operation to execute
|
|
147
|
+
* @returns Result of the operation
|
|
148
|
+
*/
|
|
149
|
+
protected withTelemetry<T>(action: string, key: string | undefined, operation: () => Promise<Result<T>>): Promise<Result<T>>;
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=BaseService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseService.d.ts","sourceRoot":"","sources":["../../src/base/BaseService.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EACL,QAAQ,EACR,eAAe,EACf,cAAc,EACd,YAAY,EAGZ,MAAM,EACP,MAAM,UAAU,CAAC;AAGlB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,8BAAsB,WAAY,YAAW,QAAQ;IACnD;;;OAGG;IACH,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAEpC;;;OAGG;IACH,SAAS,CAAC,OAAO,EAAG,eAAe,CAAC;IAEpC;;;OAGG;IACH,SAAS,CAAC,eAAe,EAAE,eAAe,CAAyB;IAEnE;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM;IAEhD;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEpC;IAED;;;;;OAKG;IACH,UAAU,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;IAI1C;;;;;OAKG;IACH,eAAe,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,GAAG,IAAI;IAIrD;;;OAGG;IACH,SAAS,IAAI,IAAI;IAKjB;;;OAGG;IACH,SAAS,KAAK,WAAW,IAAI,WAAW,CAEvC;IAED;;OAEG;IACH,SAAS,KAAK,eAAe,IAAI,OAAO,CAEvC;IAED;;;OAGG;IACH,SAAS,KAAK,OAAO,IAAI,cAAc,CAKtC;IAED;;;;;OAKG;IACH,SAAS,CAAC,WAAW,IAAI,OAAO;IAIhC;;;;;OAKG;IACH,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI;IAIlD;;;;;OAKG;IACH,SAAS,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IASzD;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CACpB,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,OAAO,EACX,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,GACd,IAAI;IAUP;;;;OAIG;IACH,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAO9C;;;OAGG;IACH,SAAS,CAAC,cAAc,IAAI,MAAM;IAIlC;;;;;OAKG;IACH,SAAS,CAAC,cAAc,CAAC,GAAG,OAAO,EAAE,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,GAAG,WAAW;IAiB9E;;;;;;;OAOG;cACa,aAAa,CAAC,CAAC,EAC7B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,SAAS,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAClC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAsBtB"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BaseService - Abstract base class for all TinyCloud services.
|
|
3
|
+
*
|
|
4
|
+
* Provides common functionality:
|
|
5
|
+
* - Context management
|
|
6
|
+
* - Session lifecycle hooks
|
|
7
|
+
* - Abort signal handling
|
|
8
|
+
* - Telemetry emission
|
|
9
|
+
*/
|
|
10
|
+
import { TelemetryEvents, err, } from "../types";
|
|
11
|
+
import { wrapError } from "../errors";
|
|
12
|
+
/**
|
|
13
|
+
* Abstract base class for TinyCloud services.
|
|
14
|
+
*
|
|
15
|
+
* Services extend this class to get common functionality like
|
|
16
|
+
* context management, session lifecycle, and abort handling.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* class MyService extends BaseService implements IMyService {
|
|
21
|
+
* static readonly serviceName = 'myservice';
|
|
22
|
+
*
|
|
23
|
+
* constructor(config: MyServiceConfig = {}) {
|
|
24
|
+
* super();
|
|
25
|
+
* this._config = config;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* async doSomething(): Promise<Result<Data>> {
|
|
29
|
+
* if (!this.requireAuth()) {
|
|
30
|
+
* return err(authRequiredError('myservice'));
|
|
31
|
+
* }
|
|
32
|
+
* // ... implementation
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class BaseService {
|
|
38
|
+
constructor() {
|
|
39
|
+
/**
|
|
40
|
+
* Abort controller for this service's operations.
|
|
41
|
+
* Reset on sign-out.
|
|
42
|
+
*/
|
|
43
|
+
this.abortController = new AbortController();
|
|
44
|
+
/**
|
|
45
|
+
* Service-specific configuration.
|
|
46
|
+
*/
|
|
47
|
+
this._config = {};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get the service configuration.
|
|
51
|
+
*/
|
|
52
|
+
get config() {
|
|
53
|
+
return this._config;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Initialize the service with context.
|
|
57
|
+
* Called by the SDK after instantiation.
|
|
58
|
+
*
|
|
59
|
+
* @param context - The service context
|
|
60
|
+
*/
|
|
61
|
+
initialize(context) {
|
|
62
|
+
this.context = context;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Called when session changes (sign-in, sign-out, refresh).
|
|
66
|
+
* Override in subclasses to handle session changes.
|
|
67
|
+
*
|
|
68
|
+
* @param session - The new session, or null if signed out
|
|
69
|
+
*/
|
|
70
|
+
onSessionChange(session) {
|
|
71
|
+
// Override in subclass if needed
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Called when SDK signs out.
|
|
75
|
+
* Aborts all pending operations.
|
|
76
|
+
*/
|
|
77
|
+
onSignOut() {
|
|
78
|
+
this.abortController.abort();
|
|
79
|
+
this.abortController = new AbortController();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the abort signal for this service.
|
|
83
|
+
* Combines the service-level abort with context-level abort.
|
|
84
|
+
*/
|
|
85
|
+
get abortSignal() {
|
|
86
|
+
return this.abortController.signal;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Check if the service is authenticated.
|
|
90
|
+
*/
|
|
91
|
+
get isAuthenticated() {
|
|
92
|
+
return this.context?.isAuthenticated ?? false;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the current session.
|
|
96
|
+
* Throws if not authenticated.
|
|
97
|
+
*/
|
|
98
|
+
get session() {
|
|
99
|
+
if (!this.context?.session) {
|
|
100
|
+
throw new Error("Not authenticated");
|
|
101
|
+
}
|
|
102
|
+
return this.context.session;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Check authentication and return error result if not authenticated.
|
|
106
|
+
* Use this at the start of methods that require authentication.
|
|
107
|
+
*
|
|
108
|
+
* @returns true if authenticated, false otherwise
|
|
109
|
+
*/
|
|
110
|
+
requireAuth() {
|
|
111
|
+
return this.isAuthenticated;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Emit a telemetry event.
|
|
115
|
+
*
|
|
116
|
+
* @param event - Event name
|
|
117
|
+
* @param data - Event data
|
|
118
|
+
*/
|
|
119
|
+
emit(event, data) {
|
|
120
|
+
this.context?.emit(event, data);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Emit a service request event.
|
|
124
|
+
*
|
|
125
|
+
* @param action - The action being performed
|
|
126
|
+
* @param key - Optional key/path being accessed
|
|
127
|
+
*/
|
|
128
|
+
emitRequest(action, key) {
|
|
129
|
+
this.emit(TelemetryEvents.SERVICE_REQUEST, {
|
|
130
|
+
service: this.getServiceName(),
|
|
131
|
+
action,
|
|
132
|
+
key,
|
|
133
|
+
timestamp: Date.now(),
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Emit a service response event.
|
|
138
|
+
*
|
|
139
|
+
* @param action - The action that was performed
|
|
140
|
+
* @param ok - Whether the request was successful
|
|
141
|
+
* @param startTime - Start time for duration calculation
|
|
142
|
+
* @param status - Optional HTTP status code
|
|
143
|
+
*/
|
|
144
|
+
emitResponse(action, ok, startTime, status) {
|
|
145
|
+
this.emit(TelemetryEvents.SERVICE_RESPONSE, {
|
|
146
|
+
service: this.getServiceName(),
|
|
147
|
+
action,
|
|
148
|
+
ok,
|
|
149
|
+
duration: Date.now() - startTime,
|
|
150
|
+
status,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Emit a service error event.
|
|
155
|
+
*
|
|
156
|
+
* @param error - The service error
|
|
157
|
+
*/
|
|
158
|
+
emitError(error) {
|
|
159
|
+
this.emit(TelemetryEvents.SERVICE_ERROR, {
|
|
160
|
+
service: this.getServiceName(),
|
|
161
|
+
error,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get the service name from the static property.
|
|
166
|
+
* Subclasses must define static serviceName.
|
|
167
|
+
*/
|
|
168
|
+
getServiceName() {
|
|
169
|
+
return this.constructor.serviceName;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create a combined abort signal from multiple sources.
|
|
173
|
+
*
|
|
174
|
+
* @param signals - Additional abort signals to combine
|
|
175
|
+
* @returns A combined abort signal
|
|
176
|
+
*/
|
|
177
|
+
combineSignals(...signals) {
|
|
178
|
+
const controller = new AbortController();
|
|
179
|
+
const allSignals = [this.abortSignal, ...signals.filter(Boolean)];
|
|
180
|
+
for (const signal of allSignals) {
|
|
181
|
+
if (signal.aborted) {
|
|
182
|
+
controller.abort(signal.reason);
|
|
183
|
+
return controller.signal;
|
|
184
|
+
}
|
|
185
|
+
signal.addEventListener("abort", () => controller.abort(signal.reason), {
|
|
186
|
+
once: true,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return controller.signal;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Wrap an operation with error handling and telemetry.
|
|
193
|
+
*
|
|
194
|
+
* @param action - The action name for telemetry
|
|
195
|
+
* @param key - Optional key for telemetry
|
|
196
|
+
* @param operation - The operation to execute
|
|
197
|
+
* @returns Result of the operation
|
|
198
|
+
*/
|
|
199
|
+
async withTelemetry(action, key, operation) {
|
|
200
|
+
const startTime = Date.now();
|
|
201
|
+
this.emitRequest(action, key);
|
|
202
|
+
try {
|
|
203
|
+
const result = await operation();
|
|
204
|
+
if (result.ok) {
|
|
205
|
+
this.emitResponse(action, true, startTime);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
this.emitResponse(action, false, startTime);
|
|
209
|
+
this.emitError(result.error);
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
const serviceError = wrapError(this.getServiceName(), error);
|
|
215
|
+
this.emitResponse(action, false, startTime);
|
|
216
|
+
this.emitError(serviceError);
|
|
217
|
+
return err(serviceError);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=BaseService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseService.js","sourceRoot":"","sources":["../../src/base/BaseService.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAKL,eAAe,EACf,GAAG,GAEJ,MAAM,UAAU,CAAC;AAClB,OAAO,EAAqB,SAAS,EAAE,MAAM,WAAW,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAgB,WAAW;IAAjC;QAaE;;;WA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QACL,CAAC;QAED,OAAO,UAAU,CAAC,MAAM,CAAC;IAC3B,CAAC;IAED;;;;;;;OAOG;IACO,KAAK,CAAC,aAAa,CAC3B,MAAc,EACd,GAAuB,EACvB,SAAmC;QAEnC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC7B,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAE9B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAC;YAEjC,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;gBACd,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC;YAC7C,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;gBAC5C,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;YAED,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,KAAK,CAAC,CAAC;YAC7D,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;YAC5C,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;YAC7B,OAAO,GAAG,CAAC,YAAY,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/base/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Service Types
|
|
3
|
+
*
|
|
4
|
+
* Types specific to the base service infrastructure.
|
|
5
|
+
*/
|
|
6
|
+
import { IService, IServiceContext, ServiceSession } from "../types";
|
|
7
|
+
/**
|
|
8
|
+
* Service constructor type for registration.
|
|
9
|
+
* Used by the SDK to instantiate services.
|
|
10
|
+
*/
|
|
11
|
+
export interface ServiceConstructor<TConfig = Record<string, unknown>, TService extends IService = IService> {
|
|
12
|
+
/** Service identifier used for registration */
|
|
13
|
+
readonly serviceName: string;
|
|
14
|
+
/** Create a new instance of the service */
|
|
15
|
+
new (config?: TConfig): TService;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Service registration entry.
|
|
19
|
+
*/
|
|
20
|
+
export interface ServiceRegistration {
|
|
21
|
+
/** The service class constructor */
|
|
22
|
+
constructor: ServiceConstructor;
|
|
23
|
+
/** Configuration for this service instance */
|
|
24
|
+
config?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Options for base service operations.
|
|
28
|
+
*/
|
|
29
|
+
export interface BaseServiceOptions {
|
|
30
|
+
/** Override the default timeout for this operation */
|
|
31
|
+
timeout?: number;
|
|
32
|
+
/** Custom abort signal for this operation */
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
}
|
|
35
|
+
export type { IService, IServiceContext, ServiceSession };
|
|
36
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/base/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,QAAQ,EACR,eAAe,EACf,cAAc,EACf,MAAM,UAAU,CAAC;AAElB;;;GAGG;AACH,MAAM,WAAW,kBAAkB,CACjC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,QAAQ,SAAS,QAAQ,GAAG,QAAQ;IAEpC,+CAA+C;IAC/C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,2CAA2C;IAC3C,KAAK,MAAM,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,oCAAoC;IACpC,WAAW,EAAE,kBAAkB,CAAC;IAChC,8CAA8C;IAC9C,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6CAA6C;IAC7C,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAGD,YAAY,EAAE,QAAQ,EAAE,eAAe,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/base/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ServiceContext implementation for TinyCloud SDK Services
|
|
3
|
+
* @module @tinycloudlabs/sdk-services
|
|
4
|
+
*/
|
|
5
|
+
import { IServiceContext, IService, ServiceSession, RetryPolicy, InvokeFunction, FetchFunction } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* Event handler type for telemetry events.
|
|
8
|
+
*/
|
|
9
|
+
type EventHandler = (data: unknown) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Configuration options for ServiceContext.
|
|
12
|
+
*/
|
|
13
|
+
export interface ServiceContextConfig {
|
|
14
|
+
/** Function to invoke WASM operations */
|
|
15
|
+
invoke: InvokeFunction;
|
|
16
|
+
/** Function to make HTTP requests (defaults to globalThis.fetch) */
|
|
17
|
+
fetch?: FetchFunction;
|
|
18
|
+
/** List of TinyCloud host URLs */
|
|
19
|
+
hosts: string[];
|
|
20
|
+
/** Initial session (optional) */
|
|
21
|
+
session?: ServiceSession | null;
|
|
22
|
+
/** Retry policy configuration */
|
|
23
|
+
retryPolicy?: Partial<RetryPolicy>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* ServiceContext provides platform dependencies and cross-service access to services.
|
|
27
|
+
* This is the primary interface services use to interact with the SDK runtime.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const context = new ServiceContext({
|
|
32
|
+
* invoke: wasmInvoke,
|
|
33
|
+
* hosts: ['https://node.tinycloud.xyz'],
|
|
34
|
+
* retryPolicy: { maxAttempts: 5 },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Register a service
|
|
38
|
+
* const kvService = new KVService({});
|
|
39
|
+
* context.registerService('kv', kvService);
|
|
40
|
+
* kvService.initialize(context);
|
|
41
|
+
*
|
|
42
|
+
* // Update session when user signs in
|
|
43
|
+
* context.setSession(userSession);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class ServiceContext implements IServiceContext {
|
|
47
|
+
private _session;
|
|
48
|
+
private _services;
|
|
49
|
+
private _eventHandlers;
|
|
50
|
+
private _abortController;
|
|
51
|
+
private readonly _invoke;
|
|
52
|
+
private readonly _fetch;
|
|
53
|
+
private readonly _hosts;
|
|
54
|
+
private readonly _retryPolicy;
|
|
55
|
+
constructor(config: ServiceContextConfig);
|
|
56
|
+
/**
|
|
57
|
+
* Get the current session.
|
|
58
|
+
*/
|
|
59
|
+
get session(): ServiceSession | null;
|
|
60
|
+
/**
|
|
61
|
+
* Check if the context has an authenticated session.
|
|
62
|
+
*/
|
|
63
|
+
get isAuthenticated(): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Update the session and notify all registered services.
|
|
66
|
+
*
|
|
67
|
+
* @param session - New session or null to clear
|
|
68
|
+
*/
|
|
69
|
+
setSession(session: ServiceSession | null): void;
|
|
70
|
+
/**
|
|
71
|
+
* Get the invoke function for WASM operations.
|
|
72
|
+
*/
|
|
73
|
+
get invoke(): InvokeFunction;
|
|
74
|
+
/**
|
|
75
|
+
* Get the fetch function for HTTP requests.
|
|
76
|
+
*/
|
|
77
|
+
get fetch(): FetchFunction;
|
|
78
|
+
/**
|
|
79
|
+
* Get the list of TinyCloud host URLs.
|
|
80
|
+
*/
|
|
81
|
+
get hosts(): string[];
|
|
82
|
+
/**
|
|
83
|
+
* Register a service with the context.
|
|
84
|
+
*
|
|
85
|
+
* @param name - Service name (e.g., 'kv')
|
|
86
|
+
* @param service - Service instance
|
|
87
|
+
*/
|
|
88
|
+
registerService(name: string, service: IService): void;
|
|
89
|
+
/**
|
|
90
|
+
* Unregister a service from the context.
|
|
91
|
+
*
|
|
92
|
+
* @param name - Service name to remove
|
|
93
|
+
*/
|
|
94
|
+
unregisterService(name: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* Get a registered service by name.
|
|
97
|
+
*
|
|
98
|
+
* @param name - Service name
|
|
99
|
+
* @returns The service instance or undefined if not registered
|
|
100
|
+
*/
|
|
101
|
+
getService<T extends IService>(name: string): T | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Emit a telemetry event.
|
|
104
|
+
*
|
|
105
|
+
* @param event - Event name
|
|
106
|
+
* @param data - Event data
|
|
107
|
+
*/
|
|
108
|
+
emit(event: string, data: unknown): void;
|
|
109
|
+
/**
|
|
110
|
+
* Subscribe to telemetry events.
|
|
111
|
+
*
|
|
112
|
+
* @param event - Event name to subscribe to
|
|
113
|
+
* @param handler - Handler function
|
|
114
|
+
* @returns Unsubscribe function
|
|
115
|
+
*/
|
|
116
|
+
on(event: string, handler: EventHandler): () => void;
|
|
117
|
+
/**
|
|
118
|
+
* Remove all event handlers for an event.
|
|
119
|
+
*
|
|
120
|
+
* @param event - Event name (if omitted, clears all events)
|
|
121
|
+
*/
|
|
122
|
+
clearEventHandlers(event?: string): void;
|
|
123
|
+
/**
|
|
124
|
+
* Get the abort signal for cancelling operations.
|
|
125
|
+
*/
|
|
126
|
+
get abortSignal(): AbortSignal;
|
|
127
|
+
/**
|
|
128
|
+
* Abort all pending operations and notify services.
|
|
129
|
+
* Creates a new AbortController for future operations.
|
|
130
|
+
*/
|
|
131
|
+
abort(): void;
|
|
132
|
+
/**
|
|
133
|
+
* Sign out - abort operations and clear session.
|
|
134
|
+
*/
|
|
135
|
+
signOut(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Get the retry policy configuration.
|
|
138
|
+
*/
|
|
139
|
+
get retryPolicy(): RetryPolicy;
|
|
140
|
+
}
|
|
141
|
+
export {};
|
|
142
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,eAAe,EACf,QAAQ,EACR,cAAc,EACd,WAAW,EACX,cAAc,EACd,aAAa,EAEd,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,KAAK,YAAY,GAAG,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,yCAAyC;IACzC,MAAM,EAAE,cAAc,CAAC;IACvB,oEAAoE;IACpE,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,kCAAkC;IAClC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,iCAAiC;IACjC,OAAO,CAAC,EAAE,cAAc,GAAG,IAAI,CAAC;IAChC,iCAAiC;IACjC,WAAW,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,cAAe,YAAW,eAAe;IACpD,OAAO,CAAC,QAAQ,CAA+B;IAC/C,OAAO,CAAC,SAAS,CAAoC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