This commit is contained in:
Xu Chang 2025-04-15 11:12:15 +08:00
commit 30ba13e841
6 changed files with 28 additions and 61 deletions

2
lib/AppLoader.d.ts vendored
View File

@ -1,10 +1,10 @@
/// <reference types="node" />
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { AppLoader as GeneralAppLoader, Trigger, EntityDict, Watcher, OpRecord, FreeTimer, OperationResult } from "oak-domain/lib/types";
import { DbStore } from "./DbStore";
import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
import { IncomingHttpHeaders } from 'http';
import { Namespace } from 'socket.io';
import Koa from "koa";
import DataSubscriber from './cluster/DataSubscriber';
import Synchronizer from './Synchronizer';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';

View File

@ -274,42 +274,25 @@ class AppLoader extends types_1.AppLoader {
url += `/:${p}`;
}
}
if (type == "custom") {
endPointRouters.push([type, name, method, url, async (koaCtx) => {
const { request, res } = koaCtx;
const { headers } = request;
koaCtx.respond = false;
try {
await fn(() => this.makeContext(undefined, headers), koaCtx);
res.statusCode = 200;
}
catch (err) {
console.error(`endpoint「${key}」方法「${method}」出错`, err);
res.statusCode = 500;
throw err;
}
finally {
res.end();
}
}]);
}
else {
endPointRouters.push([type, name, method, url, async (koaCtx) => {
const { request, params } = koaCtx;
const { body, headers, files, req } = request;
endPointRouters.push([name, method, url, async (params, headers, req, body) => {
let result;
if (type == "custom") {
result = await fn(() => this.makeContext(undefined, headers), params, headers, req, body);
}
else {
const context = await this.makeContext(undefined, headers);
try {
const result = await fn(context, params, headers, req, files ? Object.assign({}, body, files) : body);
result = await fn(context, params, headers, req, body);
await context.commit();
request.body = result;
}
catch (err) {
await context.rollback();
console.error(`endpoint「${key}」方法「${method}」出错`, err);
throw err;
}
}]);
}
}
return result;
}]);
};
if (endPointMap[k]) {
if (process.env.NODE_ENV === 'development') {

View File

@ -27,7 +27,7 @@ export default class Synchronizer<ED extends EntityDict & BaseEntityDict, Cxt ex
* sync的定义 commit triggers
* @returns
*/
getSyncTriggers(): Array<VolatileTrigger<ED, keyof ED, Cxt>>;
getSyncTriggers(): VolatileTrigger<ED, keyof ED, Cxt>[];
getSelfEndpoint(): EndpointItem<ED, Cxt>;
tryCreateSyncProcess(): void;
}

View File

@ -114,7 +114,7 @@ class Synchronizer {
remoteEntityId: entityId,
data: queue.map((ele) => ({
entity: ele.oper.targetEntity,
rowIds: ele.oper.filter.id.$in, // 暂时应该没什么用
rowIds: ele.oper.filter.id.$in,
action: ele.oper.action,
data: ele.oper.data,
})),

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClusterInfo = getClusterInfo;
exports.getClusterInfo = void 0;
function getProcessEnvOption(option) {
if (process.env.hasOwnProperty(option)) {
return process.env[option];
@ -54,3 +54,4 @@ const MyClusterInfo = initialize();
function getClusterInfo() {
return MyClusterInfo;
}
exports.getClusterInfo = getClusterInfo;

View File

@ -342,45 +342,28 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Backe
url += `/:${p}`;
}
}
if (type == "custom") {
endPointRouters.push(
[type, name, method, url, async (koaCtx) => {
const { request, res } = koaCtx
const { headers } = request;
koaCtx.respond = false
try {
await fn(() => this.makeContext(undefined, headers), koaCtx);
res.statusCode = 200
}
catch (err) {
console.error(`endpoint「${key}」方法「${method}」出错`, err);
res.statusCode = 500
throw err;
} finally {
res.end()
}
}]
);
} else {
endPointRouters.push(
[type, name, method, url, async (koaCtx) => {
const { response, request, params } = koaCtx;
const { body, headers, files, req } = request;
const context = await this.makeContext(undefined, headers);
endPointRouters.push(
[name, method, url, async (params, headers, req, body) => {
let result: string | NodeJS.ReadableStream
if (type == "custom") {
result = await fn(() => this.makeContext(undefined, headers), params, headers, req, body);
} else {
const context = await this.makeContext(undefined, headers);
try {
const result = await fn(context, params, headers, req, files ? Object.assign({}, body, files) : body);
result = await fn(context, params, headers, req, body);
await context.commit();
response.body = result;
}
catch (err) {
await context.rollback();
console.error(`endpoint「${key}」方法「${method}」出错`, err);
throw err;
}
}]
);
}
}
return result;
}]
);
}
if (endPointMap[k]) {
if (process.env.NODE_ENV === 'development') {