Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ class Server {
if (req.url === '/api') {
if (req.method !== 'POST') transport.error(403);
else this.message(client, data);
return;
}
this.request(client, transport, data);
});

if (balancer) return;
Expand Down Expand Up @@ -322,47 +320,6 @@ class Server {
}
}

request(client, transport, data) {
const { application } = this;
const { headers, url, method: verb } = transport.req;
const pathname = url.slice('/api/'.length);
const [path, params] = metautil.split(pathname, '?');
const parameters = metautil.parseParams(params);
const [unit, method] = metautil.split(path, '/');
const body = metautil.jsonParse(data) || {};
const args = { ...parameters, ...body };
const packet = { id: this.generateId(), method: unit + '/' + method, args };
const hook = application.getHook(unit);
if (hook) this.hook(client, hook, packet, verb, headers);
else this.rpc(client, packet);
}

async hook(client, proc, packet, verb, headers) {
const { id, method, args } = packet;
if (!proc) return void client.error(404, { id });
const context = client.createContext();
try {
await proc.enter();
} catch {
return void client.error(503, { id });
}
let result = null;
try {
const par = { verb, method, args, headers };
result = await proc.invoke(context, par);
} catch (error) {
client.error(500, { id, error });
} finally {
proc.leave();
}
if (metautil.isError(result)) {
const { code, httpCode = 200 } = result;
return void client.error(code, { id, error: result, httpCode });
}
client.send(result);
this.console.log(`${client.ip}\t${method}`);
}

balancing(transport) {
const host = metautil.parseHost(transport.req.headers.host);
const { protocol, ports } = this.options;
Expand Down
16 changes: 1 addition & 15 deletions metacom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,7 @@ export class Server {
message(client: Client, data: string): void;
rpc(client: Client, packet: CallPacket): Promise<void>;
binary(client: Client, data: Buffer): void;
handleRpcPacket(client: Client, packet: CallPacket): void;
handleStreamPacket(client: Client, packet: StreamPacket): Promise<void>;
handleRequest(
client: Client,
transport: Transport,
data: Buffer,
application: object,
): void;
hook(
client: Client,
proc: object,
packet: CallPacket,
verb: string,
headers: object,
): Promise<void>;
stream(client: Client, packet: StreamPacket): Promise<void>;
balancing(transport: Transport): void;
closeClients(): void;
close(): Promise<void>;
Expand Down