Skip to content

Commit a32c427

Browse files
committed
API execution code unification
PR-URL: #105
1 parent af146b5 commit a32c427

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

lib/server.js

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,33 @@ class Client {
7070
this.res.end(`HTTP ${status}: ${http.STATUS_CODES[status]}`);
7171
}
7272

73-
async api() {
74-
const { semaphore } = this.application.server;
73+
async apihttp(method, args) {
74+
const { res, application } = this;
75+
const { semaphore } = application.server;
7576
try {
7677
await semaphore.enter();
7778
} catch {
7879
this.error(504);
7980
return;
8081
}
81-
const { req, res } = this;
82-
const { url } = req;
83-
const name = url.substring(METHOD_OFFSET);
8482
const session = await this.application.auth.restore(this);
85-
const args = await receiveArgs(req);
8683
const sandbox = session ? session.sandbox : undefined;
8784
const context = session ? session.context : {};
8885
try {
89-
const exp = this.application.runScript(name, sandbox);
90-
const { method, access } = exp(context);
91-
if (!session && access !== 'public') {
92-
this.application.logger.error(`Forbidden ${url}`);
86+
const exp = this.application.runScript(method, sandbox);
87+
const proc = exp(context);
88+
if (!session && proc.access !== 'public') {
89+
this.application.logger.error(`Forbidden ${method}`);
9390
this.error(403);
9491
semaphore.leave();
9592
return;
9693
}
97-
const result = await method(args);
94+
const result = await proc.method(args);
9895
if (res.finished) {
9996
semaphore.leave();
10097
return;
10198
}
102-
if (!session && access === 'public') {
99+
if (!session && proc.access === 'public') {
103100
this.application.auth.start(this, result.userId);
104101
}
105102
res.end(JSON.stringify(result));
@@ -109,6 +106,41 @@ class Client {
109106
}
110107
semaphore.leave();
111108
}
109+
110+
async apiws(method, args) {
111+
const { connection, application } = this;
112+
const { semaphore } = application.server;
113+
const send = obj => connection.send(JSON.stringify(obj));
114+
try {
115+
await semaphore.enter();
116+
} catch {
117+
send({ result: 'error', reason: 'timeout' });
118+
return;
119+
}
120+
try {
121+
const session = await application.auth.restore(this);
122+
const sandbox = session ? session.sandbox : undefined;
123+
const context = session ? session.context : {};
124+
const exp = application.runScript(method, sandbox);
125+
const proc = exp(context);
126+
if (!session && proc.access !== 'public') {
127+
application.logger.error(`Forbidden: ${method}`);
128+
send({ result: 'error', reason: 'forbidden' });
129+
semaphore.leave();
130+
return;
131+
}
132+
const result = await proc.method(args);
133+
if (!session && proc.access === 'public') {
134+
const session = application.auth.start(this, result.userId);
135+
result.token = session.token;
136+
}
137+
send(result);
138+
} catch (err) {
139+
application.logger.error(err.stack);
140+
send({ result: 'error' });
141+
}
142+
semaphore.leave();
143+
}
112144
}
113145

114146
const listener = application => (req, res) => {
@@ -133,48 +165,19 @@ const listener = application => (req, res) => {
133165

134166
application.logger.log(`${method}\t${url}`);
135167
if (url.startsWith('/api/')) {
136-
if (method === 'POST') client.api();
137-
else client.error(403);
168+
if (method !== 'POST') {
169+
client.error(403);
170+
return;
171+
}
172+
receiveArgs(req).then(args => {
173+
const method = url.substring(METHOD_OFFSET);
174+
client.apihttp(method, args);
175+
});
138176
} else {
139177
client.static();
140178
}
141179
};
142180

143-
const apiws = async (client, message) => {
144-
const { connection, application } = client;
145-
const { semaphore } = application.server;
146-
const send = obj => connection.send(JSON.stringify(obj));
147-
try {
148-
await semaphore.enter();
149-
} catch {
150-
send({ result: 'error', reason: 'timeout' });
151-
return;
152-
}
153-
try {
154-
const { method: name, args } = JSON.parse(message);
155-
const session = await application.auth.restore(client);
156-
const sandbox = session ? session.sandbox : undefined;
157-
const context = session ? session.context : {};
158-
const exp = application.runScript(name, sandbox);
159-
const { method, access } = exp(context);
160-
if (!session && access !== 'public') {
161-
application.logger.error(`Forbidden: ${name}`);
162-
send({ result: 'error', reason: 'forbidden' });
163-
semaphore.leave();
164-
return;
165-
}
166-
const result = await method(args);
167-
if (!session && access === 'public') {
168-
const session = application.auth.start(client, result.userId);
169-
result.token = session.token;
170-
}
171-
send(result);
172-
} catch (err) {
173-
application.logger.error(err.stack);
174-
send({ result: 'error' });
175-
}
176-
semaphore.leave();
177-
};
178181

179182
class Server {
180183
constructor(config, application) {
@@ -192,7 +195,8 @@ class Server {
192195
this.ws.on('connection', (connection, req) => {
193196
const client = new Client(req, null, application, connection);
194197
connection.on('message', message => {
195-
apiws(client, message);
198+
const { method, args } = JSON.parse(message);
199+
client.apiws(method, args);
196200
});
197201
});
198202
}

0 commit comments

Comments
 (0)