@tinycloud/sdk-services 2.4.0-beta.15 → 2.4.0-beta.19
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/index.cjs +285 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +74 -25
- package/dist/index.d.ts +74 -25
- package/dist/index.js +278 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -67,6 +67,202 @@ function serviceError(code, message, service, options) {
|
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
// src/debug.ts
|
|
71
|
+
var DEBUG_FLAG = "TinyCloud_debug";
|
|
72
|
+
var MAX_EVENTS = 1e3;
|
|
73
|
+
function getGlobal() {
|
|
74
|
+
return globalThis;
|
|
75
|
+
}
|
|
76
|
+
function nowMs() {
|
|
77
|
+
const performanceNow = globalThis.performance?.now?.bind(globalThis.performance);
|
|
78
|
+
return typeof performanceNow === "function" ? performanceNow() : Date.now();
|
|
79
|
+
}
|
|
80
|
+
function isTrue(value) {
|
|
81
|
+
return value === true || value === "true" || value === "1";
|
|
82
|
+
}
|
|
83
|
+
function getProcessDebugFlag() {
|
|
84
|
+
const processLike = globalThis.process;
|
|
85
|
+
return processLike?.env?.[DEBUG_FLAG];
|
|
86
|
+
}
|
|
87
|
+
function isBrowserWindow() {
|
|
88
|
+
const global = getGlobal();
|
|
89
|
+
return global.window === globalThis;
|
|
90
|
+
}
|
|
91
|
+
function getLocalStorage() {
|
|
92
|
+
if (!isBrowserWindow()) {
|
|
93
|
+
return void 0;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
return globalThis.localStorage;
|
|
97
|
+
} catch {
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function getStoredDebugFlag() {
|
|
102
|
+
const storage = getLocalStorage();
|
|
103
|
+
if (!storage) {
|
|
104
|
+
return void 0;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
return storage.getItem(DEBUG_FLAG);
|
|
108
|
+
} catch {
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function setStoredDebugFlag(enabled) {
|
|
113
|
+
const storage = getLocalStorage();
|
|
114
|
+
if (!storage) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
if (enabled) {
|
|
119
|
+
storage.setItem(DEBUG_FLAG, "true");
|
|
120
|
+
} else {
|
|
121
|
+
storage.removeItem(DEBUG_FLAG);
|
|
122
|
+
}
|
|
123
|
+
} catch {
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function shouldStartEnabled() {
|
|
127
|
+
const global = getGlobal();
|
|
128
|
+
return isTrue(global[DEBUG_FLAG]) || isTrue(getStoredDebugFlag()) || isTrue(getProcessDebugFlag());
|
|
129
|
+
}
|
|
130
|
+
var TinyCloudDebugLogger = class {
|
|
131
|
+
constructor() {
|
|
132
|
+
this.enabled = shouldStartEnabled();
|
|
133
|
+
this.sequence = 0;
|
|
134
|
+
this.events = [];
|
|
135
|
+
this.maxEvents = MAX_EVENTS;
|
|
136
|
+
}
|
|
137
|
+
isEnabled() {
|
|
138
|
+
return this.enabled || isTrue(getGlobal()[DEBUG_FLAG]) || isTrue(getProcessDebugFlag());
|
|
139
|
+
}
|
|
140
|
+
enable(options = {}) {
|
|
141
|
+
this.enabled = true;
|
|
142
|
+
getGlobal()[DEBUG_FLAG] = true;
|
|
143
|
+
if (options.persist !== false) {
|
|
144
|
+
setStoredDebugFlag(true);
|
|
145
|
+
}
|
|
146
|
+
this.log("debug.enabled", { persisted: options.persist !== false });
|
|
147
|
+
}
|
|
148
|
+
disable(options = {}) {
|
|
149
|
+
this.log("debug.disabled", { persisted: options.persist !== false });
|
|
150
|
+
this.enabled = false;
|
|
151
|
+
getGlobal()[DEBUG_FLAG] = false;
|
|
152
|
+
if (options.persist !== false) {
|
|
153
|
+
setStoredDebugFlag(false);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
clear() {
|
|
157
|
+
this.events = [];
|
|
158
|
+
}
|
|
159
|
+
getLogs() {
|
|
160
|
+
return [...this.events];
|
|
161
|
+
}
|
|
162
|
+
log(event, data, message) {
|
|
163
|
+
if (!this.isEnabled()) {
|
|
164
|
+
return void 0;
|
|
165
|
+
}
|
|
166
|
+
return this.record({
|
|
167
|
+
event,
|
|
168
|
+
data,
|
|
169
|
+
message,
|
|
170
|
+
level: "debug",
|
|
171
|
+
timestamp: Date.now(),
|
|
172
|
+
timestampIso: (/* @__PURE__ */ new Date()).toISOString()
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
startTimer(event, data) {
|
|
176
|
+
if (!this.isEnabled()) {
|
|
177
|
+
return { stop: () => void 0 };
|
|
178
|
+
}
|
|
179
|
+
const startedAt = nowMs();
|
|
180
|
+
this.log(`${event}.start`, data);
|
|
181
|
+
return {
|
|
182
|
+
stop: (finishData) => {
|
|
183
|
+
const endedAt = nowMs();
|
|
184
|
+
return this.record({
|
|
185
|
+
event: `${event}.end`,
|
|
186
|
+
data: finishData,
|
|
187
|
+
durationMs: endedAt - startedAt,
|
|
188
|
+
startedAt,
|
|
189
|
+
endedAt,
|
|
190
|
+
level: "debug",
|
|
191
|
+
timestamp: Date.now(),
|
|
192
|
+
timestampIso: (/* @__PURE__ */ new Date()).toISOString()
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async timeAsync(event, operation, data) {
|
|
198
|
+
if (!this.isEnabled()) {
|
|
199
|
+
return operation();
|
|
200
|
+
}
|
|
201
|
+
const timer = this.startTimer(event, data);
|
|
202
|
+
try {
|
|
203
|
+
const result = await operation();
|
|
204
|
+
timer.stop({ ok: true });
|
|
205
|
+
return result;
|
|
206
|
+
} catch (error) {
|
|
207
|
+
timer.stop({ ok: false, error });
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
time(event, operation, data) {
|
|
212
|
+
if (!this.isEnabled()) {
|
|
213
|
+
return operation();
|
|
214
|
+
}
|
|
215
|
+
const timer = this.startTimer(event, data);
|
|
216
|
+
try {
|
|
217
|
+
const result = operation();
|
|
218
|
+
timer.stop({ ok: true });
|
|
219
|
+
return result;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
timer.stop({ ok: false, error });
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
record(event) {
|
|
226
|
+
const debugEvent = {
|
|
227
|
+
...event,
|
|
228
|
+
sequence: ++this.sequence
|
|
229
|
+
};
|
|
230
|
+
this.events.push(debugEvent);
|
|
231
|
+
if (this.events.length > this.maxEvents) {
|
|
232
|
+
this.events.splice(0, this.events.length - this.maxEvents);
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
globalThis.console?.debug?.("[TinyCloud]", debugEvent.event, debugEvent);
|
|
236
|
+
} catch {
|
|
237
|
+
}
|
|
238
|
+
return debugEvent;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
var tinyCloudDebugLogger = new TinyCloudDebugLogger();
|
|
242
|
+
function enableTinyCloudDebug(options) {
|
|
243
|
+
tinyCloudDebugLogger.enable(options);
|
|
244
|
+
return tinyCloudDebugLogger;
|
|
245
|
+
}
|
|
246
|
+
function disableTinyCloudDebug(options) {
|
|
247
|
+
tinyCloudDebugLogger.disable(options);
|
|
248
|
+
return tinyCloudDebugLogger;
|
|
249
|
+
}
|
|
250
|
+
function getTinyCloudDebugLogs() {
|
|
251
|
+
return tinyCloudDebugLogger.getLogs();
|
|
252
|
+
}
|
|
253
|
+
function clearTinyCloudDebugLogs() {
|
|
254
|
+
tinyCloudDebugLogger.clear();
|
|
255
|
+
}
|
|
256
|
+
function installTinyCloudDebugGlobals() {
|
|
257
|
+
const global = getGlobal();
|
|
258
|
+
global.TinyCloudDebug = tinyCloudDebugLogger;
|
|
259
|
+
global.enableTinyCloudDebug = enableTinyCloudDebug;
|
|
260
|
+
global.disableTinyCloudDebug = disableTinyCloudDebug;
|
|
261
|
+
global.getTinyCloudDebugLogs = getTinyCloudDebugLogs;
|
|
262
|
+
global.clearTinyCloudDebugLogs = clearTinyCloudDebugLogs;
|
|
263
|
+
}
|
|
264
|
+
installTinyCloudDebugGlobals();
|
|
265
|
+
|
|
70
266
|
// src/types.schema.ts
|
|
71
267
|
import { z } from "zod";
|
|
72
268
|
var ServiceErrorSchema = z.object({
|
|
@@ -308,9 +504,9 @@ var ServiceContext = class {
|
|
|
308
504
|
this._services = /* @__PURE__ */ new Map();
|
|
309
505
|
this._eventHandlers = /* @__PURE__ */ new Map();
|
|
310
506
|
this._abortController = new AbortController();
|
|
311
|
-
this._invoke = config.invoke;
|
|
312
|
-
this._invokeAny = config.invokeAny;
|
|
313
|
-
this._fetch = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
507
|
+
this._invoke = this.wrapInvoke(config.invoke);
|
|
508
|
+
this._invokeAny = config.invokeAny ? this.wrapInvokeAny(config.invokeAny) : void 0;
|
|
509
|
+
this._fetch = this.wrapFetch(config.fetch ?? globalThis.fetch.bind(globalThis));
|
|
314
510
|
this._hosts = config.hosts;
|
|
315
511
|
this._session = config.session ?? null;
|
|
316
512
|
this._retryPolicy = {
|
|
@@ -413,6 +609,7 @@ var ServiceContext = class {
|
|
|
413
609
|
* @param data - Event data
|
|
414
610
|
*/
|
|
415
611
|
emit(event, data) {
|
|
612
|
+
tinyCloudDebugLogger.log(event, data);
|
|
416
613
|
if (this._telemetryEnabled && this._telemetryHandler) {
|
|
417
614
|
try {
|
|
418
615
|
this._telemetryHandler(event, data);
|
|
@@ -502,6 +699,77 @@ var ServiceContext = class {
|
|
|
502
699
|
get retryPolicy() {
|
|
503
700
|
return this._retryPolicy;
|
|
504
701
|
}
|
|
702
|
+
wrapInvoke(invoke) {
|
|
703
|
+
return (session, service, path, action, facts) => {
|
|
704
|
+
if (!tinyCloudDebugLogger.isEnabled()) {
|
|
705
|
+
return invoke(session, service, path, action, facts);
|
|
706
|
+
}
|
|
707
|
+
const timer = tinyCloudDebugLogger.startTimer("sdk.invoke", {
|
|
708
|
+
service,
|
|
709
|
+
path,
|
|
710
|
+
action,
|
|
711
|
+
facts
|
|
712
|
+
});
|
|
713
|
+
try {
|
|
714
|
+
const result = invoke(session, service, path, action, facts);
|
|
715
|
+
timer.stop({ ok: true, service, path, action });
|
|
716
|
+
return result;
|
|
717
|
+
} catch (error) {
|
|
718
|
+
timer.stop({ ok: false, service, path, action, error });
|
|
719
|
+
throw error;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
wrapInvokeAny(invokeAny) {
|
|
724
|
+
return (session, entries, facts) => {
|
|
725
|
+
if (!tinyCloudDebugLogger.isEnabled()) {
|
|
726
|
+
return invokeAny(session, entries, facts);
|
|
727
|
+
}
|
|
728
|
+
const timer = tinyCloudDebugLogger.startTimer("sdk.invokeAny", {
|
|
729
|
+
entries,
|
|
730
|
+
facts
|
|
731
|
+
});
|
|
732
|
+
try {
|
|
733
|
+
const result = invokeAny(session, entries, facts);
|
|
734
|
+
timer.stop({ ok: true, entries });
|
|
735
|
+
return result;
|
|
736
|
+
} catch (error) {
|
|
737
|
+
timer.stop({ ok: false, entries, error });
|
|
738
|
+
throw error;
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
wrapFetch(fetchFn) {
|
|
743
|
+
return async (url, init) => {
|
|
744
|
+
if (!tinyCloudDebugLogger.isEnabled()) {
|
|
745
|
+
return fetchFn(url, init);
|
|
746
|
+
}
|
|
747
|
+
const timer = tinyCloudDebugLogger.startTimer("sdk.fetch", {
|
|
748
|
+
url,
|
|
749
|
+
method: init?.method ?? "GET",
|
|
750
|
+
init
|
|
751
|
+
});
|
|
752
|
+
try {
|
|
753
|
+
const response = await fetchFn(url, init);
|
|
754
|
+
timer.stop({
|
|
755
|
+
ok: response.ok,
|
|
756
|
+
url,
|
|
757
|
+
method: init?.method ?? "GET",
|
|
758
|
+
status: response.status,
|
|
759
|
+
statusText: response.statusText
|
|
760
|
+
});
|
|
761
|
+
return response;
|
|
762
|
+
} catch (error) {
|
|
763
|
+
timer.stop({
|
|
764
|
+
ok: false,
|
|
765
|
+
url,
|
|
766
|
+
method: init?.method ?? "GET",
|
|
767
|
+
error
|
|
768
|
+
});
|
|
769
|
+
throw error;
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
}
|
|
505
773
|
};
|
|
506
774
|
|
|
507
775
|
// src/errors.ts
|
|
@@ -5891,6 +6159,7 @@ export {
|
|
|
5891
6159
|
ServiceSessionSchema,
|
|
5892
6160
|
TelemetryEvents,
|
|
5893
6161
|
TelemetrySpanEventSchema,
|
|
6162
|
+
TinyCloudDebugLogger,
|
|
5894
6163
|
TinyCloudQuota,
|
|
5895
6164
|
VaultHeaders,
|
|
5896
6165
|
VaultPublicSpaceKVActions,
|
|
@@ -5910,21 +6179,26 @@ export {
|
|
|
5910
6179
|
canonicalize as canonicalizeEncryptionJson,
|
|
5911
6180
|
canonicalizeSecretScope,
|
|
5912
6181
|
checkDecryptInvocationInput,
|
|
6182
|
+
clearTinyCloudDebugLogs,
|
|
5913
6183
|
createKVResponseSchema,
|
|
5914
6184
|
createResultSchema,
|
|
5915
6185
|
createVaultCrypto,
|
|
5916
6186
|
decryptEnvelopeWithKey,
|
|
5917
6187
|
defaultRetryPolicy,
|
|
5918
6188
|
deriveSignedReceiverKey,
|
|
6189
|
+
disableTinyCloudDebug,
|
|
5919
6190
|
discoverNetwork,
|
|
6191
|
+
enableTinyCloudDebug,
|
|
5920
6192
|
encryptToNetwork,
|
|
5921
6193
|
encryptionError,
|
|
5922
6194
|
ensureNetworkUsableForDecrypt,
|
|
5923
6195
|
err,
|
|
5924
6196
|
errorResult,
|
|
5925
6197
|
generateRandomReceiverKey,
|
|
6198
|
+
getTinyCloudDebugLogs,
|
|
5926
6199
|
hexDecode,
|
|
5927
6200
|
hexEncode2 as hexEncode,
|
|
6201
|
+
installTinyCloudDebugGlobals,
|
|
5928
6202
|
isNetworkId,
|
|
5929
6203
|
networkDiscoveryKey,
|
|
5930
6204
|
networkError,
|
|
@@ -5940,6 +6214,7 @@ export {
|
|
|
5940
6214
|
storageLimitReachedError,
|
|
5941
6215
|
storageQuotaExceededError,
|
|
5942
6216
|
timeoutError,
|
|
6217
|
+
tinyCloudDebugLogger,
|
|
5943
6218
|
utf8Decode,
|
|
5944
6219
|
utf8Encode,
|
|
5945
6220
|
validateEnvelope,
|