bytex-sdk 1.7.5 → 1.8.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 (2) hide show
  1. package/index.js +34 -11
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -359,17 +359,17 @@ export class BytexCloud {
359
359
  */
360
360
  async download(name, password) {
361
361
  this._requireKey();
362
- // Only append .stream.btx if it's a media file or doesn't have an extension
363
- const isDataFile = name.endsWith('.json') || name.endsWith('.txt') || name.endsWith('.csv');
364
- const streamName = (isDataFile || name.endsWith('.stream.btx')) ? name : `${name}.stream.btx`;
365
-
366
- let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}`;
362
+ // ByteX Worker always expects .stream.btx for action=view
363
+ const streamName = name.endsWith('.stream.btx') ? name : `${name}.stream.btx`;
364
+ let url = `${this.workerUrl}?action=view&key=${this.apiKey}&file=${encodeURIComponent(streamName)}&_cb=${Date.now()}`;
367
365
  if (password) url += `&password=${encodeURIComponent(password)}`;
368
366
 
369
367
  const { data: { session } } = await this.supabase.auth.getSession();
370
368
  const res = await fetch(url, {
371
369
  headers: {
372
- 'Authorization': `Bearer ${session?.access_token || ''}`
370
+ 'Authorization': `Bearer ${session?.access_token || ''}`,
371
+ 'Cache-Control': 'no-cache',
372
+ 'Pragma': 'no-cache'
373
373
  }
374
374
  });
375
375
  if (!res.ok) throw new Error(`Download failed: ${res.status}`);
@@ -377,14 +377,33 @@ export class BytexCloud {
377
377
  }
378
378
 
379
379
  /**
380
- * Helper to download JSON data directly from cloud.
380
+ * Helper to download JSON or NDJSON data directly from cloud.
381
+ * Compatible with React Native (uses FileReader).
381
382
  * @param {string} name
382
- * @returns {object}
383
+ * @returns {object|Array}
383
384
  */
384
385
  async downloadData(name) {
385
386
  const blob = await this.download(name);
386
- const text = await blob.text();
387
- return JSON.parse(text);
387
+
388
+ return new Promise((resolve, reject) => {
389
+ const reader = new FileReader();
390
+ reader.onload = () => {
391
+ const text = reader.result;
392
+ try {
393
+ // Detect NDJSON (multiple lines of JSON)
394
+ if (text.includes('\n') && text.trim().startsWith('{')) {
395
+ const lines = text.trim().split('\n');
396
+ resolve(lines.map(l => JSON.parse(l)));
397
+ } else {
398
+ resolve(JSON.parse(text));
399
+ }
400
+ } catch (e) {
401
+ reject(new Error("Failed to parse Cloud Data (Invalid JSON/NDJSON)"));
402
+ }
403
+ };
404
+ reader.onerror = () => reject(new Error("Failed to read Cloud Blob"));
405
+ reader.readAsText(blob);
406
+ });
388
407
  }
389
408
 
390
409
  /**
@@ -717,9 +736,13 @@ export class BytexCloud {
717
736
  }
718
737
 
719
738
  async _workerPost(action, body) {
739
+ const { data: { session } } = await this.supabase.auth.getSession();
720
740
  const res = await fetch(`${this.workerUrl}?action=${action}&key=${this.apiKey}`, {
721
741
  method: 'POST',
722
- headers: { 'Content-Type': 'application/json' },
742
+ headers: {
743
+ 'Content-Type': 'application/json',
744
+ 'Authorization': `Bearer ${session?.access_token || ''}`
745
+ },
723
746
  body: JSON.stringify(body)
724
747
  });
725
748
  if (!res.ok) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bytex-sdk",
3
- "version": "1.7.5",
3
+ "version": "1.8.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {