@tinycloud/sdk-services 2.4.0-beta.16 → 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.cjs
CHANGED
|
@@ -64,6 +64,7 @@ __export(index_exports, {
|
|
|
64
64
|
ServiceSessionSchema: () => ServiceSessionSchema,
|
|
65
65
|
TelemetryEvents: () => TelemetryEvents,
|
|
66
66
|
TelemetrySpanEventSchema: () => TelemetrySpanEventSchema,
|
|
67
|
+
TinyCloudDebugLogger: () => TinyCloudDebugLogger,
|
|
67
68
|
TinyCloudQuota: () => TinyCloudQuota,
|
|
68
69
|
VaultHeaders: () => VaultHeaders,
|
|
69
70
|
VaultPublicSpaceKVActions: () => VaultPublicSpaceKVActions,
|
|
@@ -83,21 +84,26 @@ __export(index_exports, {
|
|
|
83
84
|
canonicalizeEncryptionJson: () => canonicalize,
|
|
84
85
|
canonicalizeSecretScope: () => canonicalizeSecretScope,
|
|
85
86
|
checkDecryptInvocationInput: () => checkDecryptInvocationInput,
|
|
87
|
+
clearTinyCloudDebugLogs: () => clearTinyCloudDebugLogs,
|
|
86
88
|
createKVResponseSchema: () => createKVResponseSchema,
|
|
87
89
|
createResultSchema: () => createResultSchema,
|
|
88
90
|
createVaultCrypto: () => createVaultCrypto,
|
|
89
91
|
decryptEnvelopeWithKey: () => decryptEnvelopeWithKey,
|
|
90
92
|
defaultRetryPolicy: () => defaultRetryPolicy,
|
|
91
93
|
deriveSignedReceiverKey: () => deriveSignedReceiverKey,
|
|
94
|
+
disableTinyCloudDebug: () => disableTinyCloudDebug,
|
|
92
95
|
discoverNetwork: () => discoverNetwork,
|
|
96
|
+
enableTinyCloudDebug: () => enableTinyCloudDebug,
|
|
93
97
|
encryptToNetwork: () => encryptToNetwork,
|
|
94
98
|
encryptionError: () => encryptionError,
|
|
95
99
|
ensureNetworkUsableForDecrypt: () => ensureNetworkUsableForDecrypt,
|
|
96
100
|
err: () => err,
|
|
97
101
|
errorResult: () => errorResult,
|
|
98
102
|
generateRandomReceiverKey: () => generateRandomReceiverKey,
|
|
103
|
+
getTinyCloudDebugLogs: () => getTinyCloudDebugLogs,
|
|
99
104
|
hexDecode: () => hexDecode,
|
|
100
105
|
hexEncode: () => hexEncode2,
|
|
106
|
+
installTinyCloudDebugGlobals: () => installTinyCloudDebugGlobals,
|
|
101
107
|
isNetworkId: () => isNetworkId,
|
|
102
108
|
networkDiscoveryKey: () => networkDiscoveryKey,
|
|
103
109
|
networkError: () => networkError,
|
|
@@ -113,6 +119,7 @@ __export(index_exports, {
|
|
|
113
119
|
storageLimitReachedError: () => storageLimitReachedError,
|
|
114
120
|
storageQuotaExceededError: () => storageQuotaExceededError,
|
|
115
121
|
timeoutError: () => timeoutError,
|
|
122
|
+
tinyCloudDebugLogger: () => tinyCloudDebugLogger,
|
|
116
123
|
utf8Decode: () => utf8Decode,
|
|
117
124
|
utf8Encode: () => utf8Encode,
|
|
118
125
|
validateEnvelope: () => validateEnvelope,
|
|
@@ -198,6 +205,202 @@ function serviceError(code, message, service, options) {
|
|
|
198
205
|
};
|
|
199
206
|
}
|
|
200
207
|
|
|
208
|
+
// src/debug.ts
|
|
209
|
+
var DEBUG_FLAG = "TinyCloud_debug";
|
|
210
|
+
var MAX_EVENTS = 1e3;
|
|
211
|
+
function getGlobal() {
|
|
212
|
+
return globalThis;
|
|
213
|
+
}
|
|
214
|
+
function nowMs() {
|
|
215
|
+
const performanceNow = globalThis.performance?.now?.bind(globalThis.performance);
|
|
216
|
+
return typeof performanceNow === "function" ? performanceNow() : Date.now();
|
|
217
|
+
}
|
|
218
|
+
function isTrue(value) {
|
|
219
|
+
return value === true || value === "true" || value === "1";
|
|
220
|
+
}
|
|
221
|
+
function getProcessDebugFlag() {
|
|
222
|
+
const processLike = globalThis.process;
|
|
223
|
+
return processLike?.env?.[DEBUG_FLAG];
|
|
224
|
+
}
|
|
225
|
+
function isBrowserWindow() {
|
|
226
|
+
const global = getGlobal();
|
|
227
|
+
return global.window === globalThis;
|
|
228
|
+
}
|
|
229
|
+
function getLocalStorage() {
|
|
230
|
+
if (!isBrowserWindow()) {
|
|
231
|
+
return void 0;
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
return globalThis.localStorage;
|
|
235
|
+
} catch {
|
|
236
|
+
return void 0;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function getStoredDebugFlag() {
|
|
240
|
+
const storage = getLocalStorage();
|
|
241
|
+
if (!storage) {
|
|
242
|
+
return void 0;
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
return storage.getItem(DEBUG_FLAG);
|
|
246
|
+
} catch {
|
|
247
|
+
return void 0;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function setStoredDebugFlag(enabled) {
|
|
251
|
+
const storage = getLocalStorage();
|
|
252
|
+
if (!storage) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
if (enabled) {
|
|
257
|
+
storage.setItem(DEBUG_FLAG, "true");
|
|
258
|
+
} else {
|
|
259
|
+
storage.removeItem(DEBUG_FLAG);
|
|
260
|
+
}
|
|
261
|
+
} catch {
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function shouldStartEnabled() {
|
|
265
|
+
const global = getGlobal();
|
|
266
|
+
return isTrue(global[DEBUG_FLAG]) || isTrue(getStoredDebugFlag()) || isTrue(getProcessDebugFlag());
|
|
267
|
+
}
|
|
268
|
+
var TinyCloudDebugLogger = class {
|
|
269
|
+
constructor() {
|
|
270
|
+
this.enabled = shouldStartEnabled();
|
|
271
|
+
this.sequence = 0;
|
|
272
|
+
this.events = [];
|
|
273
|
+
this.maxEvents = MAX_EVENTS;
|
|
274
|
+
}
|
|
275
|
+
isEnabled() {
|
|
276
|
+
return this.enabled || isTrue(getGlobal()[DEBUG_FLAG]) || isTrue(getProcessDebugFlag());
|
|
277
|
+
}
|
|
278
|
+
enable(options = {}) {
|
|
279
|
+
this.enabled = true;
|
|
280
|
+
getGlobal()[DEBUG_FLAG] = true;
|
|
281
|
+
if (options.persist !== false) {
|
|
282
|
+
setStoredDebugFlag(true);
|
|
283
|
+
}
|
|
284
|
+
this.log("debug.enabled", { persisted: options.persist !== false });
|
|
285
|
+
}
|
|
286
|
+
disable(options = {}) {
|
|
287
|
+
this.log("debug.disabled", { persisted: options.persist !== false });
|
|
288
|
+
this.enabled = false;
|
|
289
|
+
getGlobal()[DEBUG_FLAG] = false;
|
|
290
|
+
if (options.persist !== false) {
|
|
291
|
+
setStoredDebugFlag(false);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
clear() {
|
|
295
|
+
this.events = [];
|
|
296
|
+
}
|
|
297
|
+
getLogs() {
|
|
298
|
+
return [...this.events];
|
|
299
|
+
}
|
|
300
|
+
log(event, data, message) {
|
|
301
|
+
if (!this.isEnabled()) {
|
|
302
|
+
return void 0;
|
|
303
|
+
}
|
|
304
|
+
return this.record({
|
|
305
|
+
event,
|
|
306
|
+
data,
|
|
307
|
+
message,
|
|
308
|
+
level: "debug",
|
|
309
|
+
timestamp: Date.now(),
|
|
310
|
+
timestampIso: (/* @__PURE__ */ new Date()).toISOString()
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
startTimer(event, data) {
|
|
314
|
+
if (!this.isEnabled()) {
|
|
315
|
+
return { stop: () => void 0 };
|
|
316
|
+
}
|
|
317
|
+
const startedAt = nowMs();
|
|
318
|
+
this.log(`${event}.start`, data);
|
|
319
|
+
return {
|
|
320
|
+
stop: (finishData) => {
|
|
321
|
+
const endedAt = nowMs();
|
|
322
|
+
return this.record({
|
|
323
|
+
event: `${event}.end`,
|
|
324
|
+
data: finishData,
|
|
325
|
+
durationMs: endedAt - startedAt,
|
|
326
|
+
startedAt,
|
|
327
|
+
endedAt,
|
|
328
|
+
level: "debug",
|
|
329
|
+
timestamp: Date.now(),
|
|
330
|
+
timestampIso: (/* @__PURE__ */ new Date()).toISOString()
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
async timeAsync(event, operation, data) {
|
|
336
|
+
if (!this.isEnabled()) {
|
|
337
|
+
return operation();
|
|
338
|
+
}
|
|
339
|
+
const timer = this.startTimer(event, data);
|
|
340
|
+
try {
|
|
341
|
+
const result = await operation();
|
|
342
|
+
timer.stop({ ok: true });
|
|
343
|
+
return result;
|
|
344
|
+
} catch (error) {
|
|
345
|
+
timer.stop({ ok: false, error });
|
|
346
|
+
throw error;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
time(event, operation, data) {
|
|
350
|
+
if (!this.isEnabled()) {
|
|
351
|
+
return operation();
|
|
352
|
+
}
|
|
353
|
+
const timer = this.startTimer(event, data);
|
|
354
|
+
try {
|
|
355
|
+
const result = operation();
|
|
356
|
+
timer.stop({ ok: true });
|
|
357
|
+
return result;
|
|
358
|
+
} catch (error) {
|
|
359
|
+
timer.stop({ ok: false, error });
|
|
360
|
+
throw error;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
record(event) {
|
|
364
|
+
const debugEvent = {
|
|
365
|
+
...event,
|
|
366
|
+
sequence: ++this.sequence
|
|
367
|
+
};
|
|
368
|
+
this.events.push(debugEvent);
|
|
369
|
+
if (this.events.length > this.maxEvents) {
|
|
370
|
+
this.events.splice(0, this.events.length - this.maxEvents);
|
|
371
|
+
}
|
|
372
|
+
try {
|
|
373
|
+
globalThis.console?.debug?.("[TinyCloud]", debugEvent.event, debugEvent);
|
|
374
|
+
} catch {
|
|
375
|
+
}
|
|
376
|
+
return debugEvent;
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
var tinyCloudDebugLogger = new TinyCloudDebugLogger();
|
|
380
|
+
function enableTinyCloudDebug(options) {
|
|
381
|
+
tinyCloudDebugLogger.enable(options);
|
|
382
|
+
return tinyCloudDebugLogger;
|
|
383
|
+
}
|
|
384
|
+
function disableTinyCloudDebug(options) {
|
|
385
|
+
tinyCloudDebugLogger.disable(options);
|
|
386
|
+
return tinyCloudDebugLogger;
|
|
387
|
+
}
|
|
388
|
+
function getTinyCloudDebugLogs() {
|
|
389
|
+
return tinyCloudDebugLogger.getLogs();
|
|
390
|
+
}
|
|
391
|
+
function clearTinyCloudDebugLogs() {
|
|
392
|
+
tinyCloudDebugLogger.clear();
|
|
393
|
+
}
|
|
394
|
+
function installTinyCloudDebugGlobals() {
|
|
395
|
+
const global = getGlobal();
|
|
396
|
+
global.TinyCloudDebug = tinyCloudDebugLogger;
|
|
397
|
+
global.enableTinyCloudDebug = enableTinyCloudDebug;
|
|
398
|
+
global.disableTinyCloudDebug = disableTinyCloudDebug;
|
|
399
|
+
global.getTinyCloudDebugLogs = getTinyCloudDebugLogs;
|
|
400
|
+
global.clearTinyCloudDebugLogs = clearTinyCloudDebugLogs;
|
|
401
|
+
}
|
|
402
|
+
installTinyCloudDebugGlobals();
|
|
403
|
+
|
|
201
404
|
// src/types.schema.ts
|
|
202
405
|
var import_zod = require("zod");
|
|
203
406
|
var ServiceErrorSchema = import_zod.z.object({
|
|
@@ -439,9 +642,9 @@ var ServiceContext = class {
|
|
|
439
642
|
this._services = /* @__PURE__ */ new Map();
|
|
440
643
|
this._eventHandlers = /* @__PURE__ */ new Map();
|
|
441
644
|
this._abortController = new AbortController();
|
|
442
|
-
this._invoke = config.invoke;
|
|
443
|
-
this._invokeAny = config.invokeAny;
|
|
444
|
-
this._fetch = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
645
|
+
this._invoke = this.wrapInvoke(config.invoke);
|
|
646
|
+
this._invokeAny = config.invokeAny ? this.wrapInvokeAny(config.invokeAny) : void 0;
|
|
647
|
+
this._fetch = this.wrapFetch(config.fetch ?? globalThis.fetch.bind(globalThis));
|
|
445
648
|
this._hosts = config.hosts;
|
|
446
649
|
this._session = config.session ?? null;
|
|
447
650
|
this._retryPolicy = {
|
|
@@ -544,6 +747,7 @@ var ServiceContext = class {
|
|
|
544
747
|
* @param data - Event data
|
|
545
748
|
*/
|
|
546
749
|
emit(event, data) {
|
|
750
|
+
tinyCloudDebugLogger.log(event, data);
|
|
547
751
|
if (this._telemetryEnabled && this._telemetryHandler) {
|
|
548
752
|
try {
|
|
549
753
|
this._telemetryHandler(event, data);
|
|
@@ -633,6 +837,77 @@ var ServiceContext = class {
|
|
|
633
837
|
get retryPolicy() {
|
|
634
838
|
return this._retryPolicy;
|
|
635
839
|
}
|
|
840
|
+
wrapInvoke(invoke) {
|
|
841
|
+
return (session, service, path, action, facts) => {
|
|
842
|
+
if (!tinyCloudDebugLogger.isEnabled()) {
|
|
843
|
+
return invoke(session, service, path, action, facts);
|
|
844
|
+
}
|
|
845
|
+
const timer = tinyCloudDebugLogger.startTimer("sdk.invoke", {
|
|
846
|
+
service,
|
|
847
|
+
path,
|
|
848
|
+
action,
|
|
849
|
+
facts
|
|
850
|
+
});
|
|
851
|
+
try {
|
|
852
|
+
const result = invoke(session, service, path, action, facts);
|
|
853
|
+
timer.stop({ ok: true, service, path, action });
|
|
854
|
+
return result;
|
|
855
|
+
} catch (error) {
|
|
856
|
+
timer.stop({ ok: false, service, path, action, error });
|
|
857
|
+
throw error;
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
wrapInvokeAny(invokeAny) {
|
|
862
|
+
return (session, entries, facts) => {
|
|
863
|
+
if (!tinyCloudDebugLogger.isEnabled()) {
|
|
864
|
+
return invokeAny(session, entries, facts);
|
|
865
|
+
}
|
|
866
|
+
const timer = tinyCloudDebugLogger.startTimer("sdk.invokeAny", {
|
|
867
|
+
entries,
|
|
868
|
+
facts
|
|
869
|
+
});
|
|
870
|
+
try {
|
|
871
|
+
const result = invokeAny(session, entries, facts);
|
|
872
|
+
timer.stop({ ok: true, entries });
|
|
873
|
+
return result;
|
|
874
|
+
} catch (error) {
|
|
875
|
+
timer.stop({ ok: false, entries, error });
|
|
876
|
+
throw error;
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
wrapFetch(fetchFn) {
|
|
881
|
+
return async (url, init) => {
|
|
882
|
+
if (!tinyCloudDebugLogger.isEnabled()) {
|
|
883
|
+
return fetchFn(url, init);
|
|
884
|
+
}
|
|
885
|
+
const timer = tinyCloudDebugLogger.startTimer("sdk.fetch", {
|
|
886
|
+
url,
|
|
887
|
+
method: init?.method ?? "GET",
|
|
888
|
+
init
|
|
889
|
+
});
|
|
890
|
+
try {
|
|
891
|
+
const response = await fetchFn(url, init);
|
|
892
|
+
timer.stop({
|
|
893
|
+
ok: response.ok,
|
|
894
|
+
url,
|
|
895
|
+
method: init?.method ?? "GET",
|
|
896
|
+
status: response.status,
|
|
897
|
+
statusText: response.statusText
|
|
898
|
+
});
|
|
899
|
+
return response;
|
|
900
|
+
} catch (error) {
|
|
901
|
+
timer.stop({
|
|
902
|
+
ok: false,
|
|
903
|
+
url,
|
|
904
|
+
method: init?.method ?? "GET",
|
|
905
|
+
error
|
|
906
|
+
});
|
|
907
|
+
throw error;
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
}
|
|
636
911
|
};
|
|
637
912
|
|
|
638
913
|
// src/errors.ts
|
|
@@ -6023,6 +6298,7 @@ EncryptionService.serviceName = "encryption";
|
|
|
6023
6298
|
ServiceSessionSchema,
|
|
6024
6299
|
TelemetryEvents,
|
|
6025
6300
|
TelemetrySpanEventSchema,
|
|
6301
|
+
TinyCloudDebugLogger,
|
|
6026
6302
|
TinyCloudQuota,
|
|
6027
6303
|
VaultHeaders,
|
|
6028
6304
|
VaultPublicSpaceKVActions,
|
|
@@ -6042,21 +6318,26 @@ EncryptionService.serviceName = "encryption";
|
|
|
6042
6318
|
canonicalizeEncryptionJson,
|
|
6043
6319
|
canonicalizeSecretScope,
|
|
6044
6320
|
checkDecryptInvocationInput,
|
|
6321
|
+
clearTinyCloudDebugLogs,
|
|
6045
6322
|
createKVResponseSchema,
|
|
6046
6323
|
createResultSchema,
|
|
6047
6324
|
createVaultCrypto,
|
|
6048
6325
|
decryptEnvelopeWithKey,
|
|
6049
6326
|
defaultRetryPolicy,
|
|
6050
6327
|
deriveSignedReceiverKey,
|
|
6328
|
+
disableTinyCloudDebug,
|
|
6051
6329
|
discoverNetwork,
|
|
6330
|
+
enableTinyCloudDebug,
|
|
6052
6331
|
encryptToNetwork,
|
|
6053
6332
|
encryptionError,
|
|
6054
6333
|
ensureNetworkUsableForDecrypt,
|
|
6055
6334
|
err,
|
|
6056
6335
|
errorResult,
|
|
6057
6336
|
generateRandomReceiverKey,
|
|
6337
|
+
getTinyCloudDebugLogs,
|
|
6058
6338
|
hexDecode,
|
|
6059
6339
|
hexEncode,
|
|
6340
|
+
installTinyCloudDebugGlobals,
|
|
6060
6341
|
isNetworkId,
|
|
6061
6342
|
networkDiscoveryKey,
|
|
6062
6343
|
networkError,
|
|
@@ -6072,6 +6353,7 @@ EncryptionService.serviceName = "encryption";
|
|
|
6072
6353
|
storageLimitReachedError,
|
|
6073
6354
|
storageQuotaExceededError,
|
|
6074
6355
|
timeoutError,
|
|
6356
|
+
tinyCloudDebugLogger,
|
|
6075
6357
|
utf8Decode,
|
|
6076
6358
|
utf8Encode,
|
|
6077
6359
|
validateEnvelope,
|