mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Reverse the direction of the API for sending messages from
messageDefinition.send(link, { content });
to
link.send(new MessageClass(content));
and convert all message definitions to use the JSON serialisable class
pattern and explicit registration of message handlers, along with a
end-to-end based routing of messages instead of link to link.
The experience I gained from the existing way messages were defined and
sent over links was that it was difficult to understand for people
unfamiliar with the system. The automatic registration of message
handlers ment that code was being called as if by magic from nowhere
with no easy way to discover it. The manual forwarding and rules for
what can be sent where and how obscured its working.
The use of pure JSON as both input and output from message handlers made
it tedious to work with. Data had to be manually converted between the
program representation and the over the wire format, which made handler
code unplesant to write. Particularly because the over the wire format
used snake_case while the program code uses camelCase for variables.
The idea behind the decision to do it this way was to make it more clear
which data came from the wire and which from the program, and while this
worked it didn't make message handlers any nicer to write.
The direction of the API and magic registration of message handlers also
meant that a future migration to TypeScript would be troublesome. While
it may be possible to implement, the degree of TypeScript magic required
would go starkly against the principle of keep it simple stupid.
By refactoring to using the JSON serialisable class pattern most of
these issues go away. There is no longer a need to distinguish the on
wire format of messages with the program representation because the only
place that deals with the wire format of messages are the toJSON and
fromJSON methods on the classes themselves. Message handlers will work
with proper instances of classes instead of raw data, something that
makes them easier to write, and trivial to type with TypeScript.
End-to-end routing also simplifies sending messages that need to do
mulitple hops to reach the destination. And removes the need for having
all messages be specified in plugin's info module, as they can be routed
without having to be parsed completely.
The explicit registration of message handlers is something that I
thought would be tedious boilerplate code that could be automated away.
But experience now shows it to be the opposite. Allthough dull
repetition, the purpose it serves is to link together the code in a way
that humans can easily find. With explicit registration searching the
name of the message handler in the editor will show the registration that
ties it together with the type of message it handles and the kind of
registration. With implicit registration searching the name of the
message handler turns up only the handler with no clue as to what calls
it.
356 lines
13 KiB
JavaScript
356 lines
13 KiB
JavaScript
"use strict";
|
|
const assert = require("assert").strict;
|
|
const events = require("events");
|
|
const FormData = require("form-data");
|
|
const jwt = require("jsonwebtoken");
|
|
const http = require("http");
|
|
const phin = require("phin");
|
|
|
|
const { wait } = require("@clusterio/lib/helpers");
|
|
const routes = require("@clusterio/controller/src/routes");
|
|
const mock = require("../mock");
|
|
|
|
|
|
describe("controller/src/routes", function() {
|
|
let controller;
|
|
let server;
|
|
let port;
|
|
beforeEach(async function() {
|
|
controller = new mock.MockController();
|
|
routes.addRouteHandlers(controller.app);
|
|
server = http.createServer(controller.app);
|
|
server.listen(0, "localhost");
|
|
await events.once(server, "listening");
|
|
port = server.address().port;
|
|
});
|
|
afterEach(function() {
|
|
server.close();
|
|
});
|
|
describe("/api/stream/:id", function() {
|
|
it("should respond with 404 if stream doesn't exist", async function() {
|
|
let response;
|
|
response = await phin(`http://localhost:${port}/api/stream/01020304`);
|
|
assert.equal(response.statusCode, 404);
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/stream/01020304`, method: "PUT",
|
|
data: "test stream",
|
|
});
|
|
assert.equal(response.statusCode, 404);
|
|
});
|
|
it("should respond with 500 if stream times out", async function() {
|
|
let stream = await routes.createProxyStream(controller.app);
|
|
let response = await phin(`http://localhost:${port}/api/stream/${stream.id}`);
|
|
assert.equal(response.statusCode, 500);
|
|
});
|
|
it("should passthrough a stream", async function() {
|
|
let stream;
|
|
let responses;
|
|
stream = await routes.createProxyStream(controller.app);
|
|
responses = await Promise.all([
|
|
(async function() {
|
|
await wait(100);
|
|
return await phin({
|
|
url: `http://localhost:${port}/api/stream/${stream.id}`, method: "PUT",
|
|
data: "test content",
|
|
});
|
|
}()),
|
|
phin(`http://localhost:${port}/api/stream/${stream.id}`),
|
|
]);
|
|
assert.equal(responses[0].statusCode, 200);
|
|
assert.equal(responses[1].statusCode, 200);
|
|
assert.equal(responses[1].body.toString(), "test content");
|
|
stream = await routes.createProxyStream(controller.app);
|
|
responses = await Promise.all([
|
|
phin({
|
|
url: `http://localhost:${port}/api/stream/${stream.id}`, method: "PUT",
|
|
data: "test content",
|
|
}),
|
|
(async function() {
|
|
await wait(100);
|
|
return await phin(`http://localhost:${port}/api/stream/${stream.id}`);
|
|
}()),
|
|
]);
|
|
assert.equal(responses[0].statusCode, 200);
|
|
assert.equal(responses[1].statusCode, 200);
|
|
assert.equal(responses[1].body.toString(), "test content");
|
|
});
|
|
});
|
|
describe("/api/upload-save", function() {
|
|
it("should respond with 401 with invalid token", async function() {
|
|
let response;
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: { "Content-Type": "application/zip" },
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 401);
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/zip",
|
|
"X-Access-Token": "invalid",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 401);
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/zip",
|
|
"X-Access-Token": jwt.sign(
|
|
{ aud: "user", user: "invalid" }, Buffer.from("TestSecretDoNotUse", "base64")
|
|
),
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 401);
|
|
controller.userManager.users.get("test").tokenValidAfter = Math.floor((Date.now() + 60e3) / 1000);
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/zip",
|
|
"X-Access-Token": jwt.sign(
|
|
{ aud: "user", user: "test" }, Buffer.from("TestSecretDoNotUse", "base64")
|
|
),
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 401);
|
|
});
|
|
|
|
it("should respond with 403 when user has insufficient permission", async function() {
|
|
let response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/zip",
|
|
"X-Access-Token": jwt.sign(
|
|
{ aud: "user", user: "player" }, Buffer.from("TestSecretDoNotUse", "base64")
|
|
),
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 403);
|
|
});
|
|
|
|
let testToken = jwt.sign({ aud: "user", user: "test" }, Buffer.from("TestSecretDoNotUse", "base64"));
|
|
it("should respond with 415 if content type is missing or invalid", async function() {
|
|
let response;
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 415);
|
|
assert.deepEqual(JSON.parse(response.body), { request_errors: ["invalid Content-Type"] });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "invalid",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 415);
|
|
assert.deepEqual(JSON.parse(response.body), { request_errors: ["invalid Content-Type"] });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=file.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "text/plain",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 415);
|
|
assert.deepEqual(JSON.parse(response.body), { request_errors: ["invalid Content-Type"] });
|
|
});
|
|
|
|
it("should respond with 400 if invalid multipart request", async function() {
|
|
let response;
|
|
let form;
|
|
form = new FormData();
|
|
form.append("instance_id", "123");
|
|
form.append("file", "totally a zip file", { filename: "test.zip", contentType: "text/plain" });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
...form.getHeaders(),
|
|
},
|
|
data: form.getBuffer(),
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(JSON.parse(response.body), { request_errors: ["invalid file Content-Type"] });
|
|
form = new FormData();
|
|
form.append("file", "totally a zip file", { filename: "test.zip", contentType: "application/zip" });
|
|
form.append("instance_id", "123");
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
...form.getHeaders(),
|
|
},
|
|
data: form.getBuffer(),
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(
|
|
JSON.parse(response.body), { request_errors: ["instance_id must come before files uploaded"] }
|
|
);
|
|
form = new FormData();
|
|
form.append("instance_id", "123");
|
|
form.append("file", "totally a zip file", { filename: "test.txt", contentType: "application/zip" });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
...form.getHeaders(),
|
|
},
|
|
data: form.getBuffer(),
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(
|
|
JSON.parse(response.body), { request_errors: ["filename must end with .zip"] }
|
|
);
|
|
form = new FormData();
|
|
form.append("instance_id", "invalid");
|
|
form.append("file", "totally a zip file", { filename: "test.zip", contentType: "application/zip" });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
...form.getHeaders(),
|
|
},
|
|
data: form.getBuffer(),
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(
|
|
JSON.parse(response.body), { request_errors: ["invalid instance_id"] }
|
|
);
|
|
});
|
|
|
|
it("should respond with 400 if invalid file request", async function() {
|
|
let response;
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/zip",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(JSON.parse(response.body), { request_errors: ["Missing or invalid filename parameter"] });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=test.txt`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/zip",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(JSON.parse(response.body), { request_errors: ["filename must end with .zip"] });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?filename=test.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/zip",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(
|
|
JSON.parse(response.body), { request_errors: ["Missing or invalid instance_id parameter"] }
|
|
);
|
|
});
|
|
|
|
it("should respond with 500 if the transfer failed", async function() {
|
|
controller.sendToHostByInstanceId = async (request) => {
|
|
throw new Error("Something went wrong");
|
|
};
|
|
let response;
|
|
let form;
|
|
form = new FormData();
|
|
form.append("instance_id", "123");
|
|
form.append("file", "totally a zip file", { filename: "test.zip", contentType: "application/zip" });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
...form.getHeaders(),
|
|
},
|
|
data: form.getBuffer(),
|
|
});
|
|
assert.equal(response.statusCode, 500);
|
|
assert.deepEqual(JSON.parse(response.body), { errors: ["Something went wrong"], request_errors: [] });
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=test.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/zip",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 500);
|
|
assert.deepEqual(JSON.parse(response.body), { errors: ["Something went wrong"], request_errors: [] });
|
|
controller.sendToHostByInstanceId = async (request) => {
|
|
await new Promise(() => {});
|
|
};
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=test.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/zip",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.equal(response.statusCode, 500);
|
|
assert.deepEqual(
|
|
JSON.parse(response.body), { errors: ["Timed out establishing stream to host"], request_errors: [] }
|
|
);
|
|
});
|
|
|
|
it("should complete a valid transfer", async function() {
|
|
controller.sendToHostByInstanceId = async (request) => {
|
|
let stream = await phin({
|
|
url: `http://localhost:${port}/api/stream/${request.streamId}`,
|
|
});
|
|
assert.equal(stream.body.toString(), "totally a zip file");
|
|
return request.name;
|
|
};
|
|
let response;
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=test.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/zip",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.deepEqual(JSON.parse(response.body), { saves: ["test.zip"] });
|
|
assert.equal(response.statusCode, 200);
|
|
});
|
|
it("should complete a valid transfer with non-standerd mime type", async function() {
|
|
controller.sendToHostByInstanceId = async (request) => {
|
|
let stream = await phin({
|
|
url: `http://localhost:${port}/api/stream/${request.streamId}`,
|
|
});
|
|
assert.equal(stream.body.toString(), "totally a zip file");
|
|
return request.name;
|
|
};
|
|
let response;
|
|
response = await phin({
|
|
url: `http://localhost:${port}/api/upload-save?instance_id=123&filename=test.zip`, method: "POST",
|
|
headers: {
|
|
"X-Access-Token": testToken,
|
|
"Content-Type": "application/x-zip-compressed",
|
|
},
|
|
data: "totally a zip file",
|
|
});
|
|
assert.deepEqual(JSON.parse(response.body), { saves: ["test.zip"] });
|
|
assert.equal(response.statusCode, 200);
|
|
});
|
|
});
|
|
});
|