feat: initial commit
This commit is contained in:
255
pkg/sdk/client/src/client.ts
Normal file
255
pkg/sdk/client/src/client.ts
Normal file
@ -0,0 +1,255 @@
|
||||
import { EventTarget } from "./event-target";
|
||||
import { messageFrom,Message, TypeMessage } from "./message";
|
||||
import { RPCError } from "./rpc-error";
|
||||
import SockJS from 'sockjs-client';
|
||||
|
||||
const EventTypeMessage = "message";
|
||||
|
||||
export class Client extends EventTarget {
|
||||
|
||||
_conn: any
|
||||
_rpcID: number
|
||||
_pendingRPC: {}
|
||||
_queue: Message[]
|
||||
_reconnectionDelay: number
|
||||
_autoReconnect: boolean
|
||||
debug: boolean
|
||||
|
||||
constructor(autoReconnect = true) {
|
||||
super();
|
||||
this._conn = null;
|
||||
this._onConnectionClose = this._onConnectionClose.bind(this);
|
||||
this._onConnectionMessage = this._onConnectionMessage.bind(this);
|
||||
this._handleRPCResponse = this._handleRPCResponse.bind(this);
|
||||
this._rpcID = 0;
|
||||
this._pendingRPC = {};
|
||||
this._queue = [];
|
||||
this._reconnectionDelay = 250;
|
||||
this._autoReconnect = autoReconnect;
|
||||
|
||||
this.debug = false;
|
||||
|
||||
this.connect = this.connect.bind(this);
|
||||
this.disconnect = this.disconnect.bind(this);
|
||||
this.rpc = this.rpc.bind(this);
|
||||
this.send = this.send.bind(this);
|
||||
this.upload = this.upload.bind(this);
|
||||
|
||||
this.addEventListener("message", this._handleRPCResponse);
|
||||
}
|
||||
|
||||
connect(token = "") {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = `//${document.location.host}/edge/sock?token=${token}`;
|
||||
this._log("opening connection to", url);
|
||||
const conn: any = new SockJS(url);
|
||||
|
||||
const onOpen = () => {
|
||||
this._log('client connected');
|
||||
resetHandlers();
|
||||
conn.onclose = this._onConnectionClose;
|
||||
conn.onmessage = this._onConnectionMessage;
|
||||
this._conn = conn;
|
||||
this._sendQueued();
|
||||
setTimeout(() => {
|
||||
this._dispatchConnect();
|
||||
}, 0);
|
||||
return resolve(this);
|
||||
};
|
||||
|
||||
const onError = (evt) => {
|
||||
resetHandlers();
|
||||
this._scheduleReconnection();
|
||||
return reject(evt);
|
||||
};
|
||||
|
||||
const resetHandlers = () => {
|
||||
conn.removeEventListener('open', onOpen);
|
||||
conn.removeEventListener('close', onError);
|
||||
conn.removeEventListener('error', onError);
|
||||
};
|
||||
|
||||
conn.addEventListener('open', onOpen);
|
||||
conn.addEventListener('error', onError);
|
||||
conn.addEventListener('close', onError);
|
||||
});
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this._cleanupConnection();
|
||||
}
|
||||
|
||||
_onConnectionMessage(evt) {
|
||||
const rawMessage = JSON.parse(evt.data);
|
||||
const message = messageFrom(rawMessage);
|
||||
const event = new CustomEvent(message.getType(), {
|
||||
cancelable: true,
|
||||
detail: message.getPayload()
|
||||
});
|
||||
this.dispatchEvent(event);
|
||||
}
|
||||
|
||||
_handleRPCResponse(evt) {
|
||||
console.log(evt);
|
||||
|
||||
const { jsonrpc, id, error, result } = evt.detail;
|
||||
|
||||
if (jsonrpc !== '2.0' || id === undefined) return;
|
||||
|
||||
// Prevent additional handlers to catch this event
|
||||
evt.stopImmediatePropagation();
|
||||
|
||||
const pending = this._pendingRPC[id];
|
||||
if (!pending) return;
|
||||
|
||||
delete this._pendingRPC[id];
|
||||
|
||||
if (error) {
|
||||
pending.reject(new RPCError(error.code, error.message, error.data));
|
||||
return;
|
||||
}
|
||||
|
||||
pending.resolve(result);
|
||||
}
|
||||
|
||||
_onConnectionClose(evt) {
|
||||
this._log('client disconnected');
|
||||
this._dispatchDisconnect();
|
||||
this._cleanupConnection();
|
||||
this._scheduleReconnection();
|
||||
}
|
||||
|
||||
_dispatchDisconnect() {
|
||||
const event = new CustomEvent('disconnect');
|
||||
this.dispatchEvent(event);
|
||||
}
|
||||
|
||||
_dispatchConnect() {
|
||||
const event = new CustomEvent('connect');
|
||||
this.dispatchEvent(event);
|
||||
}
|
||||
|
||||
_scheduleReconnection() {
|
||||
if (!this._autoReconnect) return;
|
||||
|
||||
this._reconnectionDelay = this._reconnectionDelay * 2 + Math.random();
|
||||
this._log('client will try to reconnect in %dms', this._reconnectionDelay);
|
||||
setTimeout(this.connect.bind(this), this._reconnectionDelay);
|
||||
}
|
||||
|
||||
_cleanupConnection() {
|
||||
if (!this._conn) return;
|
||||
this._conn.onopen = null;
|
||||
this._conn.onerror = null;
|
||||
this._conn.onclose = null;
|
||||
this._conn.onmessage = null;
|
||||
this._conn.close();
|
||||
this._conn = null;
|
||||
}
|
||||
|
||||
_send(message) {
|
||||
if (!this._conn) return false;
|
||||
this._log('sending message', message);
|
||||
this._conn.send(JSON.stringify(message));
|
||||
return true;
|
||||
}
|
||||
|
||||
_sendQueued() {
|
||||
this._log("sending queued messages", this._queue.length);
|
||||
let msg = this._queue.shift();
|
||||
while (msg) {
|
||||
const sent = this._send(msg);
|
||||
if (!sent) return;
|
||||
msg = this._queue.shift();
|
||||
}
|
||||
}
|
||||
|
||||
_log(...args) {
|
||||
if (!this.debug) return;
|
||||
console.log(...args);
|
||||
}
|
||||
|
||||
_sendOrQueue(msg) {
|
||||
if (this.isConnected()) {
|
||||
this._sendQueued();
|
||||
this._send(msg);
|
||||
} else {
|
||||
this._log('queuing message', msg);
|
||||
this._queue.push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
send(data) {
|
||||
const msg = new Message("message", data);
|
||||
this._sendOrQueue(msg);
|
||||
}
|
||||
|
||||
rpc(method, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = this._rpcID++;
|
||||
const rpc = new Message(TypeMessage, {
|
||||
jsonrpc: '2.0',
|
||||
id, method, params
|
||||
});
|
||||
this._sendOrQueue(rpc);
|
||||
this._pendingRPC[id.toString()] = { resolve, reject };
|
||||
});
|
||||
}
|
||||
|
||||
isConnected() {
|
||||
return this._conn !== null;
|
||||
}
|
||||
|
||||
upload(blob: string|Blob, metadata: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const formData = new FormData();
|
||||
formData.set("file", blob);
|
||||
|
||||
if (metadata) {
|
||||
try {
|
||||
formData.set("metadata", JSON.stringify(metadata));
|
||||
} catch(err) {
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
const result = {
|
||||
onProgress: null,
|
||||
abort: () => xhr.abort(),
|
||||
result: () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
xhr.onload = () => {
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(xhr.responseText);
|
||||
} catch(err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(data);
|
||||
};
|
||||
xhr.onerror = reject;
|
||||
xhr.onabort = reject;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
xhr.upload.onprogress = evt => {
|
||||
if (typeof result.onProgress !== 'function') return;
|
||||
(result as any).onProgress(evt.loaded, evt.total);
|
||||
};
|
||||
xhr.onabort = reject;
|
||||
xhr.onerror = reject;
|
||||
|
||||
xhr.open('POST', `/edge/api/v1/upload`);
|
||||
xhr.send(formData);
|
||||
|
||||
resolve(result);
|
||||
});
|
||||
}
|
||||
|
||||
blobUrl(bucket: string, blobId: string) {
|
||||
return `/edge/api/v1/download/${bucket}/${blobId}`;
|
||||
}
|
||||
}
|
44
pkg/sdk/client/src/event-target.ts
Normal file
44
pkg/sdk/client/src/event-target.ts
Normal file
@ -0,0 +1,44 @@
|
||||
export class EventTarget {
|
||||
listeners: {
|
||||
[type: string]: Function[]
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.listeners = {};
|
||||
}
|
||||
|
||||
addEventListener(type: string, callback: Function) {
|
||||
if (!(type in this.listeners)) {
|
||||
this.listeners[type] = [];
|
||||
}
|
||||
this.listeners[type].push(callback);
|
||||
};
|
||||
|
||||
removeEventListener(type: string, callback: Function) {
|
||||
if (!(type in this.listeners)) {
|
||||
return;
|
||||
}
|
||||
const stack = this.listeners[type];
|
||||
for (var i = 0, l = stack.length; i < l; i++) {
|
||||
if (stack[i] === callback){
|
||||
stack.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
dispatchEvent(event: Event) {
|
||||
if (!(event.type in this.listeners)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const stack = this.listeners[event.type].slice();
|
||||
|
||||
for (let i = 0, l = stack.length; i < l; i++) {
|
||||
stack[i].call(this, event);
|
||||
if (event.cancelBubble) return;
|
||||
}
|
||||
return !event.defaultPrevented;
|
||||
};
|
||||
|
||||
}
|
3
pkg/sdk/client/src/index.ts
Normal file
3
pkg/sdk/client/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { Client } from './client.js';
|
||||
|
||||
export default new Client();
|
32
pkg/sdk/client/src/message.ts
Normal file
32
pkg/sdk/client/src/message.ts
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
export const TypeMessage = "message"
|
||||
|
||||
export class Message {
|
||||
_type: string
|
||||
_payload: any
|
||||
|
||||
constructor(type, payload) {
|
||||
this._type = type;
|
||||
this._payload = payload;
|
||||
}
|
||||
|
||||
getType() {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
getPayload() {
|
||||
return this._payload;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
t: this._type,
|
||||
p: this._payload
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export function messageFrom(raw) {
|
||||
return new Message(raw.t, raw.p);
|
||||
}
|
11
pkg/sdk/client/src/rpc-error.ts
Normal file
11
pkg/sdk/client/src/rpc-error.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export class RPCError extends Error {
|
||||
code: string
|
||||
data: any
|
||||
|
||||
constructor(code, message, data) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
if((Error as any).captureStackTrace) (Error as any).captureStackTrace(this, RPCError);
|
||||
}
|
||||
}
|
3
pkg/sdk/client/src/sock.ts
Normal file
3
pkg/sdk/client/src/sock.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import SockJS from 'sockjs-client';
|
||||
|
||||
window.SockJS = SockJS;
|
6
pkg/sdk/sdk.go
Normal file
6
pkg/sdk/sdk.go
Normal file
@ -0,0 +1,6 @@
|
||||
package sdk
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed client/dist/*.js client/dist/*.js.map
|
||||
var FS embed.FS
|
Reference in New Issue
Block a user