@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
package/dist/centia-io-sdk.js
CHANGED
|
@@ -735,35 +735,94 @@ var Users = class {
|
|
|
735
735
|
*/
|
|
736
736
|
var Ws = class {
|
|
737
737
|
constructor(options) {
|
|
738
|
-
this.
|
|
739
|
-
this.
|
|
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
|
-
|
|
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
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
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
|
-
*
|
|
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({
|