bytex-sdk 1.9.0 → 2.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/index.js +73 -70
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -25,97 +25,100 @@ export class BytexCloud {
|
|
|
25
25
|
this._listeners = {};
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
//
|
|
28
|
+
// --- AUTO X-RAY WRAPPER ---
|
|
29
|
+
async _execute(fn, actionName) {
|
|
30
|
+
try {
|
|
31
|
+
return await fn();
|
|
32
|
+
} catch (e) {
|
|
33
|
+
console.warn(`[ByteX SDK] Error in ${actionName}. Running Auto X-RAY...`);
|
|
34
|
+
const diagnostic = await this.xray();
|
|
35
|
+
const advice = diagnostic.advice.length > 0 ? `\n[X-RAY Advice]: ${diagnostic.advice.join(' ')}` : '';
|
|
36
|
+
throw new Error(`${actionName} failed: ${e.message}${advice}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// --- CORE METHODS (ENHANCED WITH AUTO X-RAY) ---
|
|
29
41
|
async login(email, password) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
return this._execute(async () => {
|
|
43
|
+
const { data, error } = await this.supabase.auth.signInWithPassword({ email, password });
|
|
44
|
+
if (error) throw error;
|
|
45
|
+
return data.user;
|
|
46
|
+
}, 'Login');
|
|
33
47
|
}
|
|
34
48
|
|
|
35
49
|
async upload(name, data) {
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
return this._execute(async () => {
|
|
51
|
+
this._requireKey();
|
|
52
|
+
const fd = new FormData();
|
|
53
|
+
fd.append('file', data instanceof Blob ? data : new Blob([data]), name);
|
|
54
|
+
const res = await fetch(`${this.workerUrl}?action=upload&key=${this.apiKey}`, { method: 'POST', body: fd });
|
|
55
|
+
if (!res.ok) throw new Error(await res.text() || res.statusText);
|
|
56
|
+
return await res.json();
|
|
57
|
+
}, 'Upload');
|
|
41
58
|
}
|
|
42
59
|
|
|
43
60
|
async downloadData(name) {
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
return this._execute(async () => {
|
|
62
|
+
this._requireKey();
|
|
63
|
+
const streamName = name.endsWith('.btx') ? name : `${name}.stream.btx`;
|
|
64
|
+
const url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}&_cb=${Date.now()}`;
|
|
65
|
+
const { data: { session } } = await this.supabase.auth.getSession();
|
|
66
|
+
const res = await fetch(url, { headers: { 'Authorization': `Bearer ${session?.access_token || ''}` } });
|
|
67
|
+
if (!res.ok) throw new Error(`Status ${res.status}`);
|
|
68
|
+
const text = await res.text();
|
|
69
|
+
try { return JSON.parse(text); } catch (e) { return text.split('\n').filter(Boolean).map(line => JSON.parse(line)); }
|
|
70
|
+
}, 'Download');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async delete(name) {
|
|
74
|
+
return this._execute(async () => {
|
|
75
|
+
this._requireKey();
|
|
76
|
+
const streamName = name.endsWith('.btx') ? name : `${name}.stream.btx`;
|
|
77
|
+
const { data: { session } } = await this.supabase.auth.getSession();
|
|
78
|
+
const res = await fetch(`${this.workerUrl}?action=delete&key=${this.apiKey}&file=${encodeURIComponent(streamName)}`, {
|
|
79
|
+
method: 'DELETE',
|
|
80
|
+
headers: { 'Authorization': `Bearer ${session?.access_token || ''}` }
|
|
81
|
+
});
|
|
82
|
+
if (!res.ok) throw new Error(`Status ${res.status}`);
|
|
83
|
+
return true;
|
|
84
|
+
}, 'Delete');
|
|
50
85
|
}
|
|
51
86
|
|
|
52
87
|
/**
|
|
53
|
-
* ByteX X-RAY v2.
|
|
54
|
-
* Advanced diagnostics & code implementation audit.
|
|
88
|
+
* ByteX X-RAY v2.5 (Self-Healing Diagnostic)
|
|
55
89
|
*/
|
|
56
90
|
async xray() {
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
health_score: 100,
|
|
62
|
-
diagnostics: {},
|
|
63
|
-
advice: []
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// 1. Implementation Audit (Code Surgery)
|
|
67
|
-
report.diagnostics.implementation = { status: 'OK' };
|
|
68
|
-
if (!this.config.storage && typeof navigator !== 'undefined' && /React|Expo/i.test(navigator.userAgent || '')) {
|
|
69
|
-
report.diagnostics.implementation = { status: 'WARN', issue: 'Missing Storage Engine' };
|
|
70
|
-
report.advice.push('React Native detected but no storage engine (AsyncStorage) provided. Sessions will not persist.');
|
|
71
|
-
report.health_score -= 20;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// 2. API Key Diagnostic
|
|
75
|
-
try {
|
|
76
|
-
this._requireKey();
|
|
77
|
-
report.diagnostics.api_key = { status: 'OK', prefix: this.apiKey.substring(0, 8) };
|
|
78
|
-
} catch (e) {
|
|
79
|
-
report.diagnostics.api_key = { status: 'CRITICAL', issue: 'Missing Key' };
|
|
80
|
-
report.advice.push('No API Key found. Call setApiKey() or pass it in constructor.');
|
|
81
|
-
report.health_score -= 40;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 3. Worker Connectivity & Speed Probe
|
|
91
|
+
const report = { timestamp: new Date().toISOString(), sdk_version: '2.0.0', checks: {}, advice: [] };
|
|
92
|
+
const { data: { session } } = await this.supabase.auth.getSession();
|
|
93
|
+
|
|
94
|
+
// 1. Connectivity Check
|
|
85
95
|
const start = Date.now();
|
|
86
96
|
try {
|
|
87
97
|
const res = await fetch(`${this.workerUrl}?action=list&key=${this.apiKey}`);
|
|
88
|
-
|
|
89
|
-
report.
|
|
90
|
-
status: res.ok ? 'ONLINE' : 'DEGRADED',
|
|
91
|
-
latency_ms: latency
|
|
92
|
-
};
|
|
93
|
-
if (latency > 500) {
|
|
94
|
-
report.advice.push('High latency detected. This may cause delays in audio/video streaming.');
|
|
95
|
-
}
|
|
98
|
+
report.checks.connectivity = res.ok ? 'OK' : 'ERROR';
|
|
99
|
+
report.latency = Date.now() - start;
|
|
96
100
|
} catch (e) {
|
|
97
|
-
report.
|
|
98
|
-
report.advice.push('ByteX Cloud is unreachable. Check your internet
|
|
99
|
-
report.health_score -= 30;
|
|
101
|
+
report.checks.connectivity = 'OFFLINE';
|
|
102
|
+
report.advice.push('ByteX Cloud is unreachable. Check your internet.');
|
|
100
103
|
}
|
|
101
104
|
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
105
|
+
// 2. API Key Check
|
|
106
|
+
if (!this.apiKey) {
|
|
107
|
+
report.checks.api_key = 'MISSING';
|
|
108
|
+
report.advice.push('No API Key detected. Please provide an apiKey in the constructor.');
|
|
109
|
+
} else { report.checks.api_key = 'OK'; }
|
|
110
|
+
|
|
111
|
+
// 3. Auth Check
|
|
112
|
+
report.checks.auth = session ? 'AUTHENTICATED' : 'GUEST';
|
|
113
|
+
if (!session) report.advice.push('User is not logged in. Some operations may fail.');
|
|
114
|
+
|
|
115
|
+
// 4. Platform Audit (Mobile/Web)
|
|
116
|
+
if (!this.config.storage && typeof navigator !== 'undefined') {
|
|
117
|
+
report.advice.push('Storage engine (AsyncStorage) is missing. Sessions will not persist.');
|
|
118
|
+
}
|
|
114
119
|
|
|
115
|
-
report.final_status = report.health_score > 80 ? 'HEALTHY' : (report.health_score > 50 ? 'WARNING' : 'CRITICAL');
|
|
116
120
|
return report;
|
|
117
121
|
}
|
|
118
122
|
|
|
119
123
|
_requireKey() { if (!this.apiKey) throw new Error('API Key required!'); }
|
|
120
|
-
_emit(event, data) { if (this._listeners[event]) this._listeners[event].forEach(cb => cb(data)); }
|
|
121
124
|
}
|