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