This commit is contained in:
Pan Qiancheng 2024-11-08 16:26:28 +08:00
commit 5edfcfc3c3
9 changed files with 53 additions and 38 deletions

View File

@ -42,10 +42,10 @@ export type AccessConfiguration = {
routerPrefixes?: {
aspect?: string;
endpoint?: string;
subscribe?: string;
getSubscribePoint?: string;
bridge?: string;
};
socketPath?: string;
http: {
hostname: string;
port?: number;

View File

@ -26,10 +26,11 @@ export interface Connector<ED extends EntityDict & BaseEntityDict, FrontCxt exte
};
headers?: Record<string, any>;
};
getSubscribeRouter: () => string;
getSubscribePointRouter: () => string;
getSubscribePoint: () => Promise<{
url: string;
getSocketPath: () => string;
getSocketPointRouter: () => string;
getSocketPoint: () => Promise<{
socketUrl: string;
subscribeUrl: string;
path: string;
}>;
getBridgeRouter: () => string;

View File

@ -17,6 +17,7 @@ export interface WBWatcher<ED extends EntityDict & BaseEntityDict, T extends key
filter: ED[T]['Selection']['filter'] | (() => Promise<ED[T]['Selection']['filter']>);
projection: ED[T]['Selection']['data'] | (() => Promise<ED[T]['Selection']['data']>);
fn: (context: Cxt, data: Partial<ED[T]['Schema']>[]) => Promise<OperationResult<ED>>;
forUpdate?: true;
singleton?: true;
}
export type Watcher<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>> = BBWatcher<ED, T> | WBWatcher<ED, T, Cxt>;

View File

@ -7,8 +7,9 @@ import { AccessConfiguration } from '../types/Configuration';
export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, FrontCxt extends SyncContext<ED>> implements Connector<ED, FrontCxt> {
static ASPECT_ROUTER: string;
static BRIDGE_ROUTER: string;
static SUBSCRIBE_ROUTER: string;
static SUBSCRIBE_POINT_ROUTER: string;
static SUBSCRIBE_PATH: string;
static SOCKET_PATH: string;
static SOCKET_POINT_ROUTER: string;
static ENDPOINT_ROUTER: string;
private serverUrl;
private serverAspectUrl;
@ -47,11 +48,12 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
opRecords?: undefined;
}>;
getRouter(): string;
getSubscribeRouter(): string;
getSubscribePointRouter(): string;
getSubscribePoint(): Promise<{
url: any;
getSocketPath(): string;
getSocketPointRouter(): string;
getSocketPoint(): Promise<{
path: any;
subscribeUrl: any;
socketUrl: any;
}>;
getEndpointRouter(): string;
parseRequest(headers: IncomingHttpHeaders, body?: any, files?: any): {

View File

@ -8,8 +8,9 @@ const types_1 = require("../types");
class SimpleConnector {
static ASPECT_ROUTER = '/aspect';
static BRIDGE_ROUTER = '/bridge';
static SUBSCRIBE_ROUTER = process.env.OAK_SUBSCRIBE_ROUTER || '/subscribe';
static SUBSCRIBE_POINT_ROUTER = '/subscribePoint';
static SUBSCRIBE_PATH = process.env.OAK_SUBSCRIBE_PATH || '/subscribe';
static SOCKET_PATH = process.env.OAK_SOCKET_PATH || '/socket';
static SOCKET_POINT_ROUTER = '/socketPoint';
static ENDPOINT_ROUTER = '/endpoint';
serverUrl;
serverAspectUrl;
@ -38,7 +39,7 @@ class SimpleConnector {
this.serverAspectUrl = `${serverUrl}${routerPrefixes?.aspect || SimpleConnector.ASPECT_ROUTER}`;
this.serverBridgeUrl = `${serverUrl}${routerPrefixes?.bridge || SimpleConnector.BRIDGE_ROUTER}`;
this.serverSubscribePointUrl = `${serverUrl}${routerPrefixes?.getSubscribePoint ||
SimpleConnector.SUBSCRIBE_POINT_ROUTER}`;
SimpleConnector.SOCKET_POINT_ROUTER}`;
this.makeException = makeException;
}
getCorsHeader() {
@ -122,13 +123,16 @@ class SimpleConnector {
getRouter() {
return this.configuration.routerPrefixes?.aspect || SimpleConnector.ASPECT_ROUTER;
}
getSubscribeRouter() {
return this.configuration.routerPrefixes?.subscribe || SimpleConnector.SUBSCRIBE_ROUTER;
/* getSubscribePath(): string {
return this.configuration.socketPath?.subscribe || SimpleConnector.SUBSCRIBE_PATH;
} */
getSocketPath() {
return this.configuration.socketPath || SimpleConnector.SOCKET_PATH;
}
getSubscribePointRouter() {
return this.configuration.routerPrefixes?.getSubscribePoint || SimpleConnector.SUBSCRIBE_POINT_ROUTER;
getSocketPointRouter() {
return this.configuration.routerPrefixes?.getSubscribePoint || SimpleConnector.SOCKET_POINT_ROUTER;
}
async getSubscribePoint() {
async getSocketPoint() {
let response;
try {
response = await global.fetch(this.serverSubscribePointUrl);
@ -144,10 +148,11 @@ class SimpleConnector {
const responseType = response.headers.get('Content-Type') ||
response.headers.get('content-type');
if (responseType?.toLocaleLowerCase().match(/application\/json/i)) {
const { url, path } = await response.json();
const { socketUrl, subscribeUrl, path } = await response.json();
return {
url,
path,
subscribeUrl,
socketUrl,
};
}
else {

View File

@ -50,15 +50,13 @@ export type AccessConfiguration = {
// 默认endpoint
endpoint?: string;
// 默认subscribe
subscribe?: string;
// 默认getSubscribePoint
getSubscribePoint?: string;
// 默认bridge
bridge?: string;
},
socketPath?: string,
http: {
// 后台所在域名
hostname: string;

View File

@ -45,12 +45,13 @@ export interface Connector<ED extends EntityDict & BaseEntityDict, FrontCxt exte
headers?: Record<string, any>;
};
getSubscribeRouter: () => string;
getSocketPath: () => string;
getSubscribePointRouter: () => string;
getSocketPointRouter: () => string;
getSubscribePoint: () => Promise<{
url: string;
getSocketPoint: () => Promise<{
socketUrl: string;
subscribeUrl: string;
path: string;
}>;

View File

@ -21,6 +21,7 @@ export interface WBWatcher<ED extends EntityDict & BaseEntityDict, T extends key
filter: ED[T]['Selection']['filter'] | (() => Promise<ED[T]['Selection']['filter']>);
projection: ED[T]['Selection']['data'] | (() => Promise<ED[T]['Selection']['data']>);
fn: (context: Cxt, data: Partial<ED[T]['Schema']>[]) => Promise<OperationResult<ED>>;
forUpdate?: true;
singleton?: true; // 置singleton意味着在集群环境中只有一个进程会去执行
};

View File

@ -12,8 +12,9 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
{
static ASPECT_ROUTER = '/aspect';
static BRIDGE_ROUTER = '/bridge';
static SUBSCRIBE_ROUTER = process.env.OAK_SUBSCRIBE_ROUTER || '/subscribe';
static SUBSCRIBE_POINT_ROUTER = '/subscribePoint';
static SUBSCRIBE_PATH = process.env.OAK_SUBSCRIBE_PATH || '/subscribe';
static SOCKET_PATH = process.env.OAK_SOCKET_PATH || '/socket';
static SOCKET_POINT_ROUTER = '/socketPoint';
static ENDPOINT_ROUTER = '/endpoint';
private serverUrl: string;
private serverAspectUrl: string;
@ -46,7 +47,7 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
this.serverAspectUrl = `${serverUrl}${routerPrefixes?.aspect || SimpleConnector.ASPECT_ROUTER}`;
this.serverBridgeUrl = `${serverUrl}${routerPrefixes?.bridge || SimpleConnector.BRIDGE_ROUTER}`;
this.serverSubscribePointUrl = `${serverUrl}${routerPrefixes?.getSubscribePoint ||
SimpleConnector.SUBSCRIBE_POINT_ROUTER}`;
SimpleConnector.SOCKET_POINT_ROUTER}`;
this.makeException = makeException;
}
getCorsHeader() {
@ -141,15 +142,19 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
return this.configuration.routerPrefixes?.aspect || SimpleConnector.ASPECT_ROUTER;
}
getSubscribeRouter(): string {
return this.configuration.routerPrefixes?.subscribe || SimpleConnector.SUBSCRIBE_ROUTER;
/* getSubscribePath(): string {
return this.configuration.socketPath?.subscribe || SimpleConnector.SUBSCRIBE_PATH;
} */
getSocketPath() {
return this.configuration.socketPath || SimpleConnector.SOCKET_PATH;
}
getSubscribePointRouter(): string {
return this.configuration.routerPrefixes?.getSubscribePoint || SimpleConnector.SUBSCRIBE_POINT_ROUTER;
getSocketPointRouter(): string {
return this.configuration.routerPrefixes?.getSubscribePoint || SimpleConnector.SOCKET_POINT_ROUTER;
}
async getSubscribePoint() {
async getSocketPoint() {
let response: Response;
try {
response = await global.fetch(this.serverSubscribePointUrl);
@ -169,11 +174,12 @@ export default class SimpleConnector<ED extends EntityDict & BaseEntityDict, Fro
response.headers.get('Content-Type') ||
response.headers.get('content-type');
if (responseType?.toLocaleLowerCase().match(/application\/json/i)) {
const { url, path } = await response.json();
const { socketUrl, subscribeUrl, path } = await response.json();
return {
url,
path,
subscribeUrl,
socketUrl,
};
} else {
throw new Error(`尚不支持的content-type类型${responseType}`);