@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.
Files changed (57) hide show
  1. package/dist/base/BaseService.d.ts +151 -0
  2. package/dist/base/BaseService.d.ts.map +1 -0
  3. package/dist/base/BaseService.js +221 -0
  4. package/dist/base/BaseService.js.map +1 -0
  5. package/dist/base/index.d.ts +6 -0
  6. package/dist/base/index.d.ts.map +1 -0
  7. package/dist/base/index.js +6 -0
  8. package/dist/base/index.js.map +1 -0
  9. package/dist/base/types.d.ts +36 -0
  10. package/dist/base/types.d.ts.map +1 -0
  11. package/dist/base/types.js +7 -0
  12. package/dist/base/types.js.map +1 -0
  13. package/dist/context.d.ts +142 -0
  14. package/dist/context.d.ts.map +1 -0
  15. package/dist/context.js +218 -0
  16. package/dist/context.js.map +1 -0
  17. package/dist/errors.d.ts +43 -0
  18. package/dist/errors.d.ts.map +1 -0
  19. package/dist/errors.js +111 -0
  20. package/dist/errors.js.map +1 -0
  21. package/dist/index.d.ts +47 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +53 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/kv/IKVService.d.ts +148 -0
  26. package/dist/kv/IKVService.d.ts.map +1 -0
  27. package/dist/kv/IKVService.js +8 -0
  28. package/dist/kv/IKVService.js.map +1 -0
  29. package/dist/kv/KVService.d.ts +153 -0
  30. package/dist/kv/KVService.d.ts.map +1 -0
  31. package/dist/kv/KVService.js +337 -0
  32. package/dist/kv/KVService.js.map +1 -0
  33. package/dist/kv/PrefixedKVService.d.ts +246 -0
  34. package/dist/kv/PrefixedKVService.d.ts.map +1 -0
  35. package/dist/kv/PrefixedKVService.js +145 -0
  36. package/dist/kv/PrefixedKVService.js.map +1 -0
  37. package/dist/kv/index.d.ts +10 -0
  38. package/dist/kv/index.d.ts.map +1 -0
  39. package/dist/kv/index.js +12 -0
  40. package/dist/kv/index.js.map +1 -0
  41. package/dist/kv/types.d.ts +204 -0
  42. package/dist/kv/types.d.ts.map +1 -0
  43. package/dist/kv/types.js +16 -0
  44. package/dist/kv/types.js.map +1 -0
  45. package/dist/types.d.ts +259 -0
  46. package/dist/types.d.ts.map +1 -0
  47. package/dist/types.js +72 -0
  48. package/dist/types.js.map +1 -0
  49. package/dist/types.schema.d.ts +652 -0
  50. package/dist/types.schema.d.ts.map +1 -0
  51. package/dist/types.schema.js +342 -0
  52. package/dist/types.schema.js.map +1 -0
  53. package/dist/types.schema.test.d.ts +5 -0
  54. package/dist/types.schema.test.d.ts.map +1 -0
  55. package/dist/types.schema.test.js +677 -0
  56. package/dist/types.schema.test.js.map +1 -0
  57. package/package.json +42 -0
@@ -0,0 +1,218 @@
1
+ /**
2
+ * ServiceContext implementation for TinyCloud SDK Services
3
+ * @module @tinycloudlabs/sdk-services
4
+ */
5
+ import { defaultRetryPolicy, } from "./types";
6
+ /**
7
+ * ServiceContext provides platform dependencies and cross-service access to services.
8
+ * This is the primary interface services use to interact with the SDK runtime.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const context = new ServiceContext({
13
+ * invoke: wasmInvoke,
14
+ * hosts: ['https://node.tinycloud.xyz'],
15
+ * retryPolicy: { maxAttempts: 5 },
16
+ * });
17
+ *
18
+ * // Register a service
19
+ * const kvService = new KVService({});
20
+ * context.registerService('kv', kvService);
21
+ * kvService.initialize(context);
22
+ *
23
+ * // Update session when user signs in
24
+ * context.setSession(userSession);
25
+ * ```
26
+ */
27
+ export class ServiceContext {
28
+ constructor(config) {
29
+ this._session = null;
30
+ this._services = new Map();
31
+ this._eventHandlers = new Map();
32
+ this._abortController = new AbortController();
33
+ this._invoke = config.invoke;
34
+ this._fetch = config.fetch ?? globalThis.fetch.bind(globalThis);
35
+ this._hosts = config.hosts;
36
+ this._session = config.session ?? null;
37
+ this._retryPolicy = {
38
+ ...defaultRetryPolicy,
39
+ ...config.retryPolicy,
40
+ };
41
+ }
42
+ // ============================================================
43
+ // Session Management
44
+ // ============================================================
45
+ /**
46
+ * Get the current session.
47
+ */
48
+ get session() {
49
+ return this._session;
50
+ }
51
+ /**
52
+ * Check if the context has an authenticated session.
53
+ */
54
+ get isAuthenticated() {
55
+ return this._session !== null;
56
+ }
57
+ /**
58
+ * Update the session and notify all registered services.
59
+ *
60
+ * @param session - New session or null to clear
61
+ */
62
+ setSession(session) {
63
+ this._session = session;
64
+ this.emit('session.changed', { authenticated: session !== null });
65
+ // Notify all services of session change
66
+ for (const service of this._services.values()) {
67
+ service.onSessionChange(session);
68
+ }
69
+ }
70
+ // ============================================================
71
+ // Platform Dependencies
72
+ // ============================================================
73
+ /**
74
+ * Get the invoke function for WASM operations.
75
+ */
76
+ get invoke() {
77
+ return this._invoke;
78
+ }
79
+ /**
80
+ * Get the fetch function for HTTP requests.
81
+ */
82
+ get fetch() {
83
+ return this._fetch;
84
+ }
85
+ /**
86
+ * Get the list of TinyCloud host URLs.
87
+ */
88
+ get hosts() {
89
+ return this._hosts;
90
+ }
91
+ // ============================================================
92
+ // Service Registry
93
+ // ============================================================
94
+ /**
95
+ * Register a service with the context.
96
+ *
97
+ * @param name - Service name (e.g., 'kv')
98
+ * @param service - Service instance
99
+ */
100
+ registerService(name, service) {
101
+ this._services.set(name, service);
102
+ }
103
+ /**
104
+ * Unregister a service from the context.
105
+ *
106
+ * @param name - Service name to remove
107
+ */
108
+ unregisterService(name) {
109
+ this._services.delete(name);
110
+ }
111
+ /**
112
+ * Get a registered service by name.
113
+ *
114
+ * @param name - Service name
115
+ * @returns The service instance or undefined if not registered
116
+ */
117
+ getService(name) {
118
+ return this._services.get(name);
119
+ }
120
+ // ============================================================
121
+ // Event System (Telemetry)
122
+ // ============================================================
123
+ /**
124
+ * Emit a telemetry event.
125
+ *
126
+ * @param event - Event name
127
+ * @param data - Event data
128
+ */
129
+ emit(event, data) {
130
+ const handlers = this._eventHandlers.get(event);
131
+ if (handlers) {
132
+ for (const handler of handlers) {
133
+ try {
134
+ handler(data);
135
+ }
136
+ catch (error) {
137
+ // Don't let event handler errors break the flow
138
+ console.error(`Error in event handler for "${event}":`, error);
139
+ }
140
+ }
141
+ }
142
+ }
143
+ /**
144
+ * Subscribe to telemetry events.
145
+ *
146
+ * @param event - Event name to subscribe to
147
+ * @param handler - Handler function
148
+ * @returns Unsubscribe function
149
+ */
150
+ on(event, handler) {
151
+ if (!this._eventHandlers.has(event)) {
152
+ this._eventHandlers.set(event, new Set());
153
+ }
154
+ this._eventHandlers.get(event).add(handler);
155
+ // Return unsubscribe function
156
+ return () => {
157
+ const handlers = this._eventHandlers.get(event);
158
+ if (handlers) {
159
+ handlers.delete(handler);
160
+ if (handlers.size === 0) {
161
+ this._eventHandlers.delete(event);
162
+ }
163
+ }
164
+ };
165
+ }
166
+ /**
167
+ * Remove all event handlers for an event.
168
+ *
169
+ * @param event - Event name (if omitted, clears all events)
170
+ */
171
+ clearEventHandlers(event) {
172
+ if (event) {
173
+ this._eventHandlers.delete(event);
174
+ }
175
+ else {
176
+ this._eventHandlers.clear();
177
+ }
178
+ }
179
+ // ============================================================
180
+ // Lifecycle
181
+ // ============================================================
182
+ /**
183
+ * Get the abort signal for cancelling operations.
184
+ */
185
+ get abortSignal() {
186
+ return this._abortController.signal;
187
+ }
188
+ /**
189
+ * Abort all pending operations and notify services.
190
+ * Creates a new AbortController for future operations.
191
+ */
192
+ abort() {
193
+ this._abortController.abort();
194
+ this._abortController = new AbortController();
195
+ // Notify all services
196
+ for (const service of this._services.values()) {
197
+ service.onSignOut();
198
+ }
199
+ }
200
+ /**
201
+ * Sign out - abort operations and clear session.
202
+ */
203
+ signOut() {
204
+ this.abort();
205
+ this.setSession(null);
206
+ this.emit('session.expired', {});
207
+ }
208
+ // ============================================================
209
+ // Retry Policy
210
+ // ============================================================
211
+ /**
212
+ * Get the retry policy configuration.
213
+ */
214
+ get retryPolicy() {
215
+ return this._retryPolicy;
216
+ }
217
+ }
218
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"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,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,CAAC;YAC9C,OAAO,CAAC,SAAS,EAAE,CAAC;QACtB,CAAC;IACH,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,KAAK,EAAE,CAAC;QACb,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,+DAA+D;IAC/D,eAAe;IACf,+DAA+D;IAE/D;;OAEG;IACH,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * SDK Services - Error Utilities
3
+ *
4
+ * Utilities for creating and handling service errors.
5
+ */
6
+ import { ServiceError } from "./types";
7
+ /**
8
+ * Create a service error for authentication required.
9
+ */
10
+ export declare function authRequiredError(service: string): ServiceError;
11
+ /**
12
+ * Create a service error for expired authentication.
13
+ */
14
+ export declare function authExpiredError(service: string): ServiceError;
15
+ /**
16
+ * Create a service error for network issues.
17
+ */
18
+ export declare function networkError(service: string, message: string, cause?: Error): ServiceError;
19
+ /**
20
+ * Create a service error for timeouts.
21
+ */
22
+ export declare function timeoutError(service: string): ServiceError;
23
+ /**
24
+ * Create a service error for aborted requests.
25
+ */
26
+ export declare function abortedError(service: string): ServiceError;
27
+ /**
28
+ * Create a service error for not found resources.
29
+ */
30
+ export declare function notFoundError(service: string, resource: string): ServiceError;
31
+ /**
32
+ * Create a service error for permission denied.
33
+ */
34
+ export declare function permissionDeniedError(service: string, action: string): ServiceError;
35
+ /**
36
+ * Wrap an unknown error in a ServiceError.
37
+ */
38
+ export declare function wrapError(service: string, error: unknown, defaultCode?: string): ServiceError;
39
+ /**
40
+ * Create an error Result from a ServiceError.
41
+ */
42
+ export declare function errorResult(error: ServiceError): import("./types").Result<never, ServiceError>;
43
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAmB,MAAM,SAAS,CAAC;AAExD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAM/D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAM9D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,KAAK,GACZ,YAAY,CAOd;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAM1D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAM1D;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GACf,YAAY,CAMd;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,YAAY,CAMd;AAED;;GAEG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,OAAO,EACd,WAAW,GAAE,MAAiC,GAC7C,YAAY,CA4Bd;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,iDAE9C"}
package/dist/errors.js ADDED
@@ -0,0 +1,111 @@
1
+ /**
2
+ * SDK Services - Error Utilities
3
+ *
4
+ * Utilities for creating and handling service errors.
5
+ */
6
+ import { ErrorCodes, err } from "./types";
7
+ /**
8
+ * Create a service error for authentication required.
9
+ */
10
+ export function authRequiredError(service) {
11
+ return {
12
+ code: ErrorCodes.AUTH_REQUIRED,
13
+ message: "Authentication required. Please sign in first.",
14
+ service,
15
+ };
16
+ }
17
+ /**
18
+ * Create a service error for expired authentication.
19
+ */
20
+ export function authExpiredError(service) {
21
+ return {
22
+ code: ErrorCodes.AUTH_EXPIRED,
23
+ message: "Session has expired. Please sign in again.",
24
+ service,
25
+ };
26
+ }
27
+ /**
28
+ * Create a service error for network issues.
29
+ */
30
+ export function networkError(service, message, cause) {
31
+ return {
32
+ code: ErrorCodes.NETWORK_ERROR,
33
+ message,
34
+ service,
35
+ cause,
36
+ };
37
+ }
38
+ /**
39
+ * Create a service error for timeouts.
40
+ */
41
+ export function timeoutError(service) {
42
+ return {
43
+ code: ErrorCodes.TIMEOUT,
44
+ message: "Request timed out.",
45
+ service,
46
+ };
47
+ }
48
+ /**
49
+ * Create a service error for aborted requests.
50
+ */
51
+ export function abortedError(service) {
52
+ return {
53
+ code: ErrorCodes.ABORTED,
54
+ message: "Request was aborted.",
55
+ service,
56
+ };
57
+ }
58
+ /**
59
+ * Create a service error for not found resources.
60
+ */
61
+ export function notFoundError(service, resource) {
62
+ return {
63
+ code: ErrorCodes.NOT_FOUND,
64
+ message: `Resource not found: ${resource}`,
65
+ service,
66
+ };
67
+ }
68
+ /**
69
+ * Create a service error for permission denied.
70
+ */
71
+ export function permissionDeniedError(service, action) {
72
+ return {
73
+ code: ErrorCodes.PERMISSION_DENIED,
74
+ message: `Permission denied for action: ${action}`,
75
+ service,
76
+ };
77
+ }
78
+ /**
79
+ * Wrap an unknown error in a ServiceError.
80
+ */
81
+ export function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
82
+ if (error instanceof Error) {
83
+ // Check for abort errors
84
+ if (error.name === "AbortError") {
85
+ return abortedError(service);
86
+ }
87
+ // Check for timeout errors (varies by platform)
88
+ if (error.name === "TimeoutError" ||
89
+ error.message.toLowerCase().includes("timeout")) {
90
+ return timeoutError(service);
91
+ }
92
+ return {
93
+ code: defaultCode,
94
+ message: error.message,
95
+ service,
96
+ cause: error,
97
+ };
98
+ }
99
+ return {
100
+ code: defaultCode,
101
+ message: String(error),
102
+ service,
103
+ };
104
+ }
105
+ /**
106
+ * Create an error Result from a ServiceError.
107
+ */
108
+ export function errorResult(error) {
109
+ return err(error);
110
+ }
111
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAgB,UAAU,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAExD;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAe;IAC/C,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,aAAa;QAC9B,OAAO,EAAE,gDAAgD;QACzD,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC9C,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,YAAY;QAC7B,OAAO,EAAE,4CAA4C;QACrD,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAC1B,OAAe,EACf,OAAe,EACf,KAAa;IAEb,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,aAAa;QAC9B,OAAO;QACP,OAAO;QACP,KAAK;KACN,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,OAAe;IAC1C,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,OAAO;QACxB,OAAO,EAAE,oBAAoB;QAC7B,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,OAAe;IAC1C,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,OAAO;QACxB,OAAO,EAAE,sBAAsB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAC3B,OAAe,EACf,QAAgB;IAEhB,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,SAAS;QAC1B,OAAO,EAAE,uBAAuB,QAAQ,EAAE;QAC1C,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CACnC,OAAe,EACf,MAAc;IAEd,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,iBAAiB;QAClC,OAAO,EAAE,iCAAiC,MAAM,EAAE;QAClD,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CACvB,OAAe,EACf,KAAc,EACd,cAAsB,UAAU,CAAC,aAAa;IAE9C,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,yBAAyB;QACzB,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YAChC,OAAO,YAAY,CAAC,OAAO,CAAC,CAAC;QAC/B,CAAC;QAED,gDAAgD;QAChD,IACE,KAAK,CAAC,IAAI,KAAK,cAAc;YAC7B,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,EAC/C,CAAC;YACD,OAAO,YAAY,CAAC,OAAO,CAAC,CAAC;QAC/B,CAAC;QAED,OAAO;YACL,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,OAAO;YACP,KAAK,EAAE,KAAK;SACb,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC;QACtB,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,KAAmB;IAC7C,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;AACpB,CAAC"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * TinyCloud SDK Services
3
+ *
4
+ * Platform-agnostic services with plugin architecture for TinyCloud.
5
+ *
6
+ * @packageDocumentation
7
+ * @module @tinycloudlabs/sdk-services
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import {
12
+ * ServiceContext,
13
+ * BaseService,
14
+ * Result,
15
+ * ErrorCodes,
16
+ * } from '@tinycloudlabs/sdk-services';
17
+ *
18
+ * // Create a context
19
+ * const context = new ServiceContext({
20
+ * invoke: wasmInvoke,
21
+ * hosts: ['https://node.tinycloud.xyz'],
22
+ * });
23
+ *
24
+ * // Create and register a service
25
+ * const kv = new KVService({ prefix: 'myapp' });
26
+ * context.registerService('kv', kv);
27
+ * kv.initialize(context);
28
+ *
29
+ * // Use the service
30
+ * const result = await kv.get('key');
31
+ * if (result.ok) {
32
+ * console.log(result.data);
33
+ * }
34
+ * ```
35
+ */
36
+ export type { Result, ServiceError, ErrorCode, IServiceContext, IService, ServiceSession, RetryPolicy, InvokeFunction, InvocationFact, InvocationFacts, FetchFunction, FetchRequestInit, FetchResponse, ServiceHeaders, EventHandler, ServiceRequestEvent, ServiceResponseEvent, ServiceErrorEvent, ServiceRetryEvent, } from "./types";
37
+ export { ErrorCodes, defaultRetryPolicy, TelemetryEvents, ok, err, serviceError, } from "./types";
38
+ export { ServiceErrorSchema, KVResponseHeadersSchema, KVListResponseSchema, ServiceRequestEventSchema, ServiceResponseEventSchema, ServiceErrorEventSchema, ServiceRetryEventSchema, RetryPolicySchema, ServiceSessionSchema, GenericResultSchema, GenericKVResponseSchema, KVListResultSchema, createResultSchema, createKVResponseSchema, validateServiceError, validateKVListResponse, validateKVResponseHeaders, validateServiceSession, validateRetryPolicy, validateServiceRequestEvent, validateServiceResponseEvent, } from "./types.schema";
39
+ export type { ValidationError, ServiceErrorType, KVResponseHeadersType, KVListResponseType, GenericKVResponseType, KVListResultType, ServiceRequestEventType, ServiceResponseEventType, ServiceErrorEventType, ServiceRetryEventType, RetryPolicyType, ServiceSessionType, } from "./types.schema";
40
+ export { ServiceContext } from "./context";
41
+ export type { ServiceContextConfig } from "./context";
42
+ export { authRequiredError, authExpiredError, networkError, timeoutError, abortedError, notFoundError, permissionDeniedError, wrapError, errorResult, } from "./errors";
43
+ export { BaseService } from "./base/index";
44
+ export type { ServiceConstructor, ServiceRegistration, BaseServiceOptions, } from "./base/index";
45
+ export { KVService, PrefixedKVService, IKVService, KVAction } from "./kv";
46
+ export type { IPrefixedKVService, KVServiceConfig, KVGetOptions, KVPutOptions, KVListOptions, KVDeleteOptions, KVHeadOptions, KVResponse, KVListResponse, KVResponseHeaders, KVActionType, } from "./kv";
47
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAGH,YAAY,EACV,MAAM,EACN,YAAY,EACZ,SAAS,EACT,eAAe,EACf,QAAQ,EACR,cAAc,EACd,WAAW,EACX,cAAc,EACd,cAAc,EACd,eAAe,EACf,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,eAAe,EACf,EAAE,EACF,GAAG,EACH,YAAY,GACb,MAAM,SAAS,CAAC;AAGjB,OAAO,EAEL,kBAAkB,EAClB,uBAAuB,EACvB,oBAAoB,EACpB,yBAAyB,EACzB,0BAA0B,EAC1B,uBAAuB,EACvB,uBAAuB,EACvB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,EACvB,kBAAkB,EAElB,kBAAkB,EAClB,sBAAsB,EAEtB,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,EACzB,sBAAsB,EACtB,mBAAmB,EACnB,2BAA2B,EAC3B,4BAA4B,GAC7B,MAAM,gBAAgB,CAAC;AAExB,YAAY,EAEV,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,qBAAqB,EACrB,gBAAgB,EAChB,uBAAuB,EACvB,wBAAwB,EACxB,qBAAqB,EACrB,qBAAqB,EACrB,eAAe,EACf,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,YAAY,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAGtD,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,qBAAqB,EACrB,SAAS,EACT,WAAW,GACZ,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,YAAY,EACV,kBAAkB,EAClB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAC1E,YAAY,EACV,kBAAkB,EAClB,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,eAAe,EACf,aAAa,EACb,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,YAAY,GACb,MAAM,MAAM,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * TinyCloud SDK Services
3
+ *
4
+ * Platform-agnostic services with plugin architecture for TinyCloud.
5
+ *
6
+ * @packageDocumentation
7
+ * @module @tinycloudlabs/sdk-services
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import {
12
+ * ServiceContext,
13
+ * BaseService,
14
+ * Result,
15
+ * ErrorCodes,
16
+ * } from '@tinycloudlabs/sdk-services';
17
+ *
18
+ * // Create a context
19
+ * const context = new ServiceContext({
20
+ * invoke: wasmInvoke,
21
+ * hosts: ['https://node.tinycloud.xyz'],
22
+ * });
23
+ *
24
+ * // Create and register a service
25
+ * const kv = new KVService({ prefix: 'myapp' });
26
+ * context.registerService('kv', kv);
27
+ * kv.initialize(context);
28
+ *
29
+ * // Use the service
30
+ * const result = await kv.get('key');
31
+ * if (result.ok) {
32
+ * console.log(result.data);
33
+ * }
34
+ * ```
35
+ */
36
+ export { ErrorCodes, defaultRetryPolicy, TelemetryEvents, ok, err, serviceError, } from "./types";
37
+ // Zod schemas and validation
38
+ export {
39
+ // Schemas
40
+ ServiceErrorSchema, KVResponseHeadersSchema, KVListResponseSchema, ServiceRequestEventSchema, ServiceResponseEventSchema, ServiceErrorEventSchema, ServiceRetryEventSchema, RetryPolicySchema, ServiceSessionSchema, GenericResultSchema, GenericKVResponseSchema, KVListResultSchema,
41
+ // Schema factories
42
+ createResultSchema, createKVResponseSchema,
43
+ // Validation functions
44
+ validateServiceError, validateKVListResponse, validateKVResponseHeaders, validateServiceSession, validateRetryPolicy, validateServiceRequestEvent, validateServiceResponseEvent, } from "./types.schema";
45
+ // Context
46
+ export { ServiceContext } from "./context";
47
+ // Errors
48
+ export { authRequiredError, authExpiredError, networkError, timeoutError, abortedError, notFoundError, permissionDeniedError, wrapError, errorResult, } from "./errors";
49
+ // Base service
50
+ export { BaseService } from "./base/index";
51
+ // KV service
52
+ export { KVService, PrefixedKVService, KVAction } from "./kv";
53
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAyBH,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,eAAe,EACf,EAAE,EACF,GAAG,EACH,YAAY,GACb,MAAM,SAAS,CAAC;AAEjB,6BAA6B;AAC7B,OAAO;AACL,UAAU;AACV,kBAAkB,EAClB,uBAAuB,EACvB,oBAAoB,EACpB,yBAAyB,EACzB,0BAA0B,EAC1B,uBAAuB,EACvB,uBAAuB,EACvB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,EACvB,kBAAkB;AAClB,mBAAmB;AACnB,kBAAkB,EAClB,sBAAsB;AACtB,uBAAuB;AACvB,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,EACzB,sBAAsB,EACtB,mBAAmB,EACnB,2BAA2B,EAC3B,4BAA4B,GAC7B,MAAM,gBAAgB,CAAC;AAkBxB,UAAU;AACV,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,SAAS;AACT,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,qBAAqB,EACrB,SAAS,EACT,WAAW,GACZ,MAAM,UAAU,CAAC;AAElB,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAO3C,aAAa;AACb,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAc,QAAQ,EAAE,MAAM,MAAM,CAAC"}
@@ -0,0 +1,148 @@
1
+ /**
2
+ * IKVService - Interface for KV (Key-Value) service.
3
+ *
4
+ * Platform-agnostic interface for key-value storage operations.
5
+ * Implementations use dependency injection via IServiceContext.
6
+ */
7
+ import { IService, Result } from "../types";
8
+ import type { IPrefixedKVService } from "./PrefixedKVService";
9
+ import { KVServiceConfig, KVGetOptions, KVPutOptions, KVListOptions, KVDeleteOptions, KVHeadOptions, KVResponse, KVListResponse } from "./types";
10
+ /**
11
+ * KV service interface.
12
+ *
13
+ * Provides key-value storage operations with:
14
+ * - Result type pattern (no throwing)
15
+ * - Optional prefix namespacing
16
+ * - Configurable timeouts
17
+ * - Abort signal support
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const result = await kv.get('user/settings');
22
+ * if (result.ok) {
23
+ * console.log('Settings:', result.data.data);
24
+ * } else {
25
+ * console.error('Error:', result.error.code);
26
+ * }
27
+ * ```
28
+ */
29
+ export interface IKVService extends IService {
30
+ /**
31
+ * Get a value by key.
32
+ *
33
+ * @param key - The key to retrieve
34
+ * @param options - Optional get configuration
35
+ * @returns Result with the stored value and headers
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const result = await kv.get<UserSettings>('settings');
40
+ * if (result.ok) {
41
+ * const settings = result.data.data;
42
+ * const etag = result.data.headers.etag;
43
+ * }
44
+ * ```
45
+ */
46
+ get<T = unknown>(key: string, options?: KVGetOptions): Promise<Result<KVResponse<T>>>;
47
+ /**
48
+ * Store a value at a key.
49
+ *
50
+ * Objects are automatically JSON stringified.
51
+ * Strings are stored as-is.
52
+ *
53
+ * @param key - The key to store under
54
+ * @param value - The value to store
55
+ * @param options - Optional put configuration
56
+ * @returns Result indicating success/failure
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Store an object (auto-stringified)
61
+ * const result = await kv.put('settings', { theme: 'dark' });
62
+ *
63
+ * // Store a string
64
+ * const result = await kv.put('name', 'Alice');
65
+ * ```
66
+ */
67
+ put(key: string, value: unknown, options?: KVPutOptions): Promise<Result<KVResponse<void>>>;
68
+ /**
69
+ * List keys with optional prefix filtering.
70
+ *
71
+ * @param options - Optional list configuration
72
+ * @returns Result with array of matching keys
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * // List all keys
77
+ * const result = await kv.list();
78
+ *
79
+ * // List keys with a specific prefix
80
+ * const result = await kv.list({ prefix: 'users/' });
81
+ * ```
82
+ */
83
+ list(options?: KVListOptions): Promise<Result<KVListResponse>>;
84
+ /**
85
+ * Delete a key.
86
+ *
87
+ * @param key - The key to delete
88
+ * @param options - Optional delete configuration
89
+ * @returns Result indicating success/failure
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const result = await kv.delete('old-key');
94
+ * if (!result.ok && result.error.code === 'KV_NOT_FOUND') {
95
+ * console.log('Key already deleted');
96
+ * }
97
+ * ```
98
+ */
99
+ delete(key: string, options?: KVDeleteOptions): Promise<Result<void>>;
100
+ /**
101
+ * Get metadata for a key without retrieving the value.
102
+ *
103
+ * Useful for checking if a key exists or getting headers
104
+ * without downloading the full value.
105
+ *
106
+ * @param key - The key to check
107
+ * @param options - Optional head configuration
108
+ * @returns Result with headers only
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const result = await kv.head('large-file');
113
+ * if (result.ok) {
114
+ * console.log('Size:', result.data.headers.contentLength);
115
+ * }
116
+ * ```
117
+ */
118
+ head(key: string, options?: KVHeadOptions): Promise<Result<KVResponse<void>>>;
119
+ /**
120
+ * Create a prefix-scoped view of this KV service.
121
+ *
122
+ * Returns a PrefixedKVService that automatically prefixes all
123
+ * key operations with the specified prefix. This enables apps
124
+ * to isolate their data within a shared space.
125
+ *
126
+ * @param prefix - The prefix to apply to all operations
127
+ * @returns A PrefixedKVService scoped to the prefix
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * const myApp = kv.withPrefix('/app.myapp.com');
132
+ *
133
+ * // Operations are automatically prefixed
134
+ * await myApp.put('settings.json', { theme: 'dark' });
135
+ * // -> Actually writes to: /app.myapp.com/settings.json
136
+ *
137
+ * // Nested prefixes
138
+ * const settings = myApp.withPrefix('/settings');
139
+ * await settings.get('theme.json'); // -> /app.myapp.com/settings/theme.json
140
+ * ```
141
+ */
142
+ withPrefix(prefix: string): IPrefixedKVService;
143
+ /**
144
+ * Service configuration.
145
+ */
146
+ readonly config: KVServiceConfig;
147
+ }
148
+ //# sourceMappingURL=IKVService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IKVService.d.ts","sourceRoot":"","sources":["../../src/kv/IKVService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EACL,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,eAAe,EACf,aAAa,EACb,UAAU,EACV,cAAc,EACf,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,UAAW,SAAQ,QAAQ;IAC1C;;;;;;;;;;;;;;;OAeG;IACH,GAAG,CAAC,CAAC,GAAG,OAAO,EACb,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,YAAY,GACrB,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAElC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CACD,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,YAAY,GACrB,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAErC;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC;IAE/D;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAEtE;;;;;;;;;;;;;;;;;OAiBG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAE9E;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAAC;IAE/C;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;CAClC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * IKVService - Interface for KV (Key-Value) service.
3
+ *
4
+ * Platform-agnostic interface for key-value storage operations.
5
+ * Implementations use dependency injection via IServiceContext.
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=IKVService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IKVService.js","sourceRoot":"","sources":["../../src/kv/IKVService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}