Spaces:
Running
Running
File size: 1,442 Bytes
b39afbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
import { Socket } from 'rete';
import { type WorkerContext, type ICustomSocketOpts } from '../openapi/types';
abstract class CustomSocket extends Socket {
public type: string;
protected opts: ICustomSocketOpts;
protected customActions = new Map<string, Function>();
constructor(name: string, type: string, opts?: ICustomSocketOpts) {
super(name, opts);
this.opts = opts || {};
this.type = type;
}
abstract handleInput(ctx: WorkerContext, data: any): Promise<any | null>;
abstract handleOutput(ctx: WorkerContext, data: any): Promise<any | null>;
get format() {
return this.opts.format;
}
get array() {
return this.opts.array || false;
}
get customAction() {
return this.opts.customAction;
}
get customSettings() {
return this.opts.customSettings;
}
compatibleWith(socket: Socket, noReverse = false) {
if (noReverse) return super.compatibleWith(socket);
// Flip this to have input check compatibility
//@ts-ignore
return socket.compatibleWith(this, true);
}
isValidUrl(str: string): boolean {
let url;
if (!(typeof str === 'string' && str.length > 0)) {
return false;
}
try {
url = new URL(str);
} catch (e) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
}
}
export default CustomSocket;
|