@centia-io/sdk 0.0.52 → 0.0.54

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.
@@ -735,35 +735,94 @@ var Users = class {
735
735
  */
736
736
  var Ws = class {
737
737
  constructor(options) {
738
- this.options = options;
739
- this.options.wsClient = this.options?.wsClient ?? WebSocket;
738
+ this.ws = null;
739
+ this.listeners = {};
740
+ this.closed = false;
741
+ this.options = {
742
+ reconnect: true,
743
+ reconnectInterval: 3e3,
744
+ ...options
745
+ };
746
+ this.options.wsClient = this.options.wsClient ?? WebSocket;
740
747
  }
741
748
  connect() {
742
- const me = this;
749
+ this.closed = false;
750
+ this.doConnect();
751
+ }
752
+ disconnect() {
753
+ this.closed = true;
754
+ this.ws?.close();
755
+ this.ws = null;
756
+ }
757
+ subscribe(sub) {
758
+ this.send({
759
+ type: "subscription",
760
+ ...sub
761
+ });
762
+ }
763
+ send(data) {
764
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error("WebSocket is not connected");
765
+ this.ws.send(typeof data === "string" ? data : JSON.stringify(data));
766
+ }
767
+ on(event, listener) {
768
+ if (!this.listeners[event]) this.listeners[event] = [];
769
+ this.listeners[event].push(listener);
770
+ return () => this.off(event, listener);
771
+ }
772
+ off(event, listener) {
773
+ const arr = this.listeners[event];
774
+ if (!arr) return;
775
+ const idx = arr.indexOf(listener);
776
+ if (idx !== -1) arr.splice(idx, 1);
777
+ }
778
+ get connected() {
779
+ return this.ws?.readyState === WebSocket.OPEN;
780
+ }
781
+ emit(event, data) {
782
+ const arr = this.listeners[event];
783
+ if (!arr) return;
784
+ for (const fn of arr) fn(data);
785
+ }
786
+ doConnect() {
743
787
  const { accessToken } = getTokens();
744
- const connect = () => {
745
- let queryString = `?token=` + accessToken;
746
- if (this.options?.rel) queryString = queryString + `&rel=` + this.options.rel;
747
- const WSClass = this.options.wsClient;
748
- const ws = new WSClass(this.options.host + `/` + queryString);
749
- ws.onopen = function() {
750
- console.log("WebSocket connected!");
751
- };
752
- ws.onmessage = function(event) {
753
- me.options.callBack(event.data);
754
- };
755
- ws.onclose = function(event) {
756
- if (accessToken !== "") {
757
- console.log("WebSocket closed, reconnecting in 3 seconds...", event.reason);
758
- setTimeout(connect, 3e3);
759
- }
760
- };
761
- ws.onerror = function(err) {
762
- console.error("WebSocket error observed:", err);
763
- ws.close();
764
- };
788
+ if (!accessToken) return;
789
+ let url = this.options.host + "/?token=" + encodeURIComponent(accessToken);
790
+ if (this.options.rels) url += "&rels=" + encodeURIComponent(this.options.rels);
791
+ const WSClass = this.options.wsClient;
792
+ const ws = new WSClass(url);
793
+ this.ws = ws;
794
+ ws.onopen = () => {
795
+ this.emit("open", void 0);
796
+ };
797
+ ws.onmessage = (event) => {
798
+ let msg;
799
+ try {
800
+ msg = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
801
+ } catch {
802
+ return;
803
+ }
804
+ switch (msg.type) {
805
+ case "batch":
806
+ this.emit("batch", msg);
807
+ break;
808
+ case "subscription_ack":
809
+ this.emit("subscription_ack", msg);
810
+ break;
811
+ case "error":
812
+ this.emit("error", msg);
813
+ break;
814
+ }
815
+ };
816
+ ws.onclose = (event) => {
817
+ this.emit("close", {
818
+ code: event.code,
819
+ reason: event.reason
820
+ });
821
+ if (!this.closed && this.options.reconnect) setTimeout(() => this.doConnect(), this.options.reconnectInterval);
822
+ };
823
+ ws.onerror = () => {
824
+ ws.close();
765
825
  };
766
- if (accessToken !== "") connect();
767
826
  }
768
827
  };
769
828
 
@@ -2125,16 +2184,41 @@ var FileImport = class {
2125
2184
  }
2126
2185
  /**
2127
2186
  * Upload a file via multipart/form-data.
2128
- * In Node.js, pass a FormData instance. In browsers, pass a native FormData.
2187
+ * When `options.chunkSize` is set, the file is split into chunks and uploaded
2188
+ * sequentially. The server reassembles the file from the chunks.
2129
2189
  */
2130
- async postFileUpload(formData) {
2131
- return this.client.request({
2190
+ async postFileUpload(formData, options) {
2191
+ if (!options?.chunkSize) return this.client.request({
2132
2192
  path: "api/v4/file/upload",
2133
2193
  method: "POST",
2134
2194
  body: formData,
2135
2195
  contentType: null,
2136
2196
  expectedStatus: 201
2137
2197
  });
2198
+ const file = formData.get("filename");
2199
+ if (!file) throw new Error("FormData must contain a \"filename\" entry for chunked upload.");
2200
+ const fileName = file instanceof File ? file.name : "upload";
2201
+ const totalChunks = Math.ceil(file.size / options.chunkSize);
2202
+ let result = { filename: "" };
2203
+ for (let i = 0; i < totalChunks; i++) {
2204
+ const start = i * options.chunkSize;
2205
+ const end = Math.min(start + options.chunkSize, file.size);
2206
+ const chunk = file.slice(start, end);
2207
+ const chunkForm = new FormData();
2208
+ chunkForm.append("filename", chunk, fileName);
2209
+ result = await this.client.request({
2210
+ path: "api/v4/file/upload",
2211
+ method: "POST",
2212
+ body: chunkForm,
2213
+ contentType: null,
2214
+ query: {
2215
+ chunk: String(i),
2216
+ chunks: String(totalChunks)
2217
+ },
2218
+ expectedStatus: 201
2219
+ });
2220
+ }
2221
+ return result;
2138
2222
  }
2139
2223
  async postFileProcess(body) {
2140
2224
  return this.client.request({