Flatten lib namespace

Move all functions and classes currently available through lib/name and
libName to be under a single flat namespace in lib.  This make
reorganising the lib namespace much simpler as the location of symbols
can move without breaking the consumers of lib.

The multi-entrypoint design was already problematic for the web plugin
system which couldn't effectively use it due to how webpack's shared
module system is awkward to set up with multiple entrypoints per package.
This commit is contained in:
Hornwitser committed 2023-07-28 16:03:29 +02:00
1 parent bb58670597
commit bfe119eaa3
143 files changed
+1798 -1918

No files matched your search

+14 -15
View File
@@ -3,13 +3,12 @@ const assert = require("assert").strict;
const events = require("events");
const mock = require("../../mock");
const libData = require("@clusterio/lib/data");
const libLink = require("@clusterio/lib/link");
const lib = require("@clusterio/lib");
describe("lib/link/connectors", function() {
describe("class WebSocketBaseConnector", function() {
let testConnector = new libLink.WebSocketBaseConnector();
let testConnector = new lib.WebSocketBaseConnector();
describe("._dropSendBufferSeq()", function() {
beforeEach(function() {
@@ -35,7 +34,7 @@ describe("lib/link/connectors", function() {
});
describe("class WebSocketClientConnector", function() {
let testConnector = new libLink.WebSocketClientConnector("url", 1);
let testConnector = new lib.WebSocketClientConnector("url", 1);
testConnector._socket = new mock.MockSocket();
describe(".register()", function() {
@@ -51,7 +50,7 @@ describe("lib/link/connectors", function() {
it("calls send on the socket", function() {
testConnector._state = "connecting";
testConnector._socket.sentMessages = [];
testConnector.sendHandshake(new libData.MessageInvalidate());
testConnector.sendHandshake(new lib.MessageInvalidate());
assert.deepEqual(
testConnector._socket.sentMessages.map(JSON.parse),
[{ type: "invalidate" }]
@@ -63,7 +62,7 @@ describe("lib/link/connectors", function() {
it("calls send on the socket", function() {
testConnector._state = "connected";
testConnector._socket.sentMessages = [];
testConnector.send(new libData.MessageHeartbeat(1));
testConnector.send(new lib.MessageHeartbeat(1));
assert.deepEqual(
testConnector._socket.sentMessages.map(JSON.parse),
[{ seq: 1, type: "heartbeat"}]
@@ -96,8 +95,8 @@ describe("lib/link/connectors", function() {
let called = false;
testConnector.register = () => { called = true; };
testConnector._processHandshake(
new libData.MessageHello(
new libData.HelloData("test", {})
new lib.MessageHello(
new lib.HelloData("test", {})
)
);
assert(called, "register was not called");
@@ -105,9 +104,9 @@ describe("lib/link/connectors", function() {
it("should emit connect on ready", async function() {
let result = events.once(testConnector, "connect");
testConnector._processHandshake(
new libData.MessageReady(
new libData.ReadyData(
new libData.Address(libData.Address.control, 1),
new lib.MessageReady(
new lib.ReadyData(
new lib.Address(lib.Address.control, 1),
"x",
20,
10,
@@ -128,7 +127,7 @@ describe("lib/link/connectors", function() {
it("should throw on message received in invalid state", function() {
testConnector._state = "closed";
assert.throws(
() => testConnector._socket.events.get("message")(JSON.stringify(new libData.MessageInvalidate())),
() => testConnector._socket.events.get("message")(JSON.stringify(new lib.MessageInvalidate())),
new Error("Received message in unexpected state closed")
);
});
@@ -136,15 +135,15 @@ describe("lib/link/connectors", function() {
testConnector._state = "connecting";
let called = false;
testConnector._processHandshake = () => { called = true; };
testConnector._socket.events.get("message")(JSON.stringify(new libData.MessageInvalidate()));
testConnector._socket.events.get("message")(JSON.stringify(new lib.MessageInvalidate()));
assert(called, "_processHandshake was not called");
});
it("should emit message on message in connected state", async function() {
testConnector._socket.sentMessages = [];
testConnector._state = "connected";
let result = events.once(testConnector, "message");
let addr = new libData.Address(libData.Address.control, 1);
let event = new libData.MessageEvent(1, addr, addr, "TestEvent");
let addr = new lib.Address(lib.Address.control, 1);
let event = new lib.MessageEvent(1, addr, addr, "TestEvent");
testConnector._socket.events.get("message")(JSON.stringify(event));
assert.deepEqual((await result)[0], event);
});
+76 -78
View File
@@ -2,12 +2,10 @@
const assert = require("assert").strict;
const events = require("events");
const libData = require("@clusterio/lib/data");
const libLink = require("@clusterio/lib/link");
const libErrors = require("@clusterio/lib/errors");
const lib = require("@clusterio/lib");
const mock = require("../../mock");
const addr = libData.Address.fromShorthand;
const addr = lib.Address.fromShorthand;
describe("lib/link/link", function() {
function throwSimple(message) {
@@ -28,7 +26,7 @@ describe("lib/link/link", function() {
static dst = ["controller", "control"];
static permission = null;
}
libLink.Link.register(SimpleRequest);
lib.Link.register(SimpleRequest);
class NumberRequest {
static type = "request";
static src = ["controller", "control"];
@@ -45,14 +43,14 @@ describe("lib/link/link", function() {
toJSON() { return this.value; }
static fromJSON(json) { return new this(json); }
};
libLink.Link.register(NumberRequest);
lib.Link.register(NumberRequest);
class SimpleEvent {
static type = "event";
static src = ["controller", "control"];
static dst = ["controller", "control"];
static permission = null;
}
libLink.Link.register(SimpleEvent);
lib.Link.register(SimpleEvent);
class NumberEvent {
static type = "event";
static src = ["controller", "control"];
@@ -63,11 +61,11 @@ describe("lib/link/link", function() {
toJSON() { return this.value; }
static fromJSON(json) { return new this(json); }
}
libLink.Link.register(NumberEvent);
lib.Link.register(NumberEvent);
beforeEach(function() {
testConnector = new mock.MockConnector(src, dst);
testLink = new libLink.Link(testConnector);
testLink = new lib.Link(testConnector);
});
it("should handle unknown message", async function() {
@@ -76,13 +74,13 @@ describe("lib/link/link", function() {
describe("Request handling", function() {
it("should give an error response back on unrecognized request", function() {
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "UnhandledRequest"));
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "UnhandledRequest"));
assert.deepEqual(testConnector.sentMessages, [
new libData.MessageResponseError(
new lib.MessageResponseError(
testConnector._seq - 1,
src,
dst,
new libData.ResponseError("Unrecognized request UnhandledRequest")
new lib.ResponseError("Unrecognized request UnhandledRequest")
),
]);
});
@@ -92,14 +90,14 @@ describe("lib/link/link", function() {
static src = "controller";
static dst = "control";
}
libLink.Link.register(UnhandledRequest);
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "UnhandledRequest"));
lib.Link.register(UnhandledRequest);
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "UnhandledRequest"));
assert.deepEqual(testConnector.sentMessages, [
new libData.MessageResponseError(
new lib.MessageResponseError(
testConnector._seq - 1,
src,
dst,
new libData.ResponseError("No handler for UnhandledRequest")
new lib.ResponseError("No handler for UnhandledRequest")
),
]);
});
@@ -109,14 +107,14 @@ describe("lib/link/link", function() {
static src = "host";
static dst = "control";
}
libLink.Link.register(InvalidSrcRequest);
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "InvalidSrcRequest"));
lib.Link.register(InvalidSrcRequest);
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "InvalidSrcRequest"));
assert.deepEqual(testConnector.sentMessages, [
new libData.MessageResponseError(
new lib.MessageResponseError(
testConnector._seq - 1,
src,
dst,
new libData.ResponseError("Source [Address controller:0] is not allowed for InvalidSrcRequest")
new lib.ResponseError("Source [Address controller:0] is not allowed for InvalidSrcRequest")
),
]);
});
@@ -126,38 +124,38 @@ describe("lib/link/link", function() {
static src = "controller";
static dst = "host";
}
libLink.Link.register(InvalidDstRequest);
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "InvalidDstRequest"));
lib.Link.register(InvalidDstRequest);
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "InvalidDstRequest"));
assert.deepEqual(testConnector.sentMessages, [
new libData.MessageResponseError(
new lib.MessageResponseError(
testConnector._seq - 1,
src,
dst,
new libData.ResponseError(
new lib.ResponseError(
"Destination [Address control:1] is not allowed for InvalidDstRequest"
)
),
]);
});
it("should give an error response back on broadcast src", function() {
testConnector.emit("message", new libData.MessageRequest(1, addr("allControls"), src, "SimpleRequest"));
testConnector.emit("message", new lib.MessageRequest(1, addr("allControls"), src, "SimpleRequest"));
assert.deepEqual(testConnector.sentMessages, [
new libData.MessageResponseError(
new lib.MessageResponseError(
testConnector._seq - 1,
src,
addr("allControls"),
new libData.ResponseError("Message src may not be broadcast")
new lib.ResponseError("Message src may not be broadcast")
),
]);
});
it("should give an error response back on requst with broadcast dst", function() {
testConnector.emit("message", new libData.MessageRequest(1, dst, addr("allControls"), "SimpleRequest"));
testConnector.emit("message", new lib.MessageRequest(1, dst, addr("allControls"), "SimpleRequest"));
assert.deepEqual(testConnector.sentMessages, [
new libData.MessageResponseError(
new lib.MessageResponseError(
testConnector._seq - 1,
src,
dst,
new libData.ResponseError("Destination [Address 4:control] is not allowed for SimpleRequest")
new lib.ResponseError("Destination [Address 4:control] is not allowed for SimpleRequest")
),
]);
});
@@ -165,7 +163,7 @@ describe("lib/link/link", function() {
describe("Event handling", function() {
it("should ignore unrecognized event", function() {
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "UnhandledEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "UnhandledEvent"));
assert.deepEqual(testConnector.sentMessages, []);
});
it("should ignore unhandled event", function() {
@@ -174,8 +172,8 @@ describe("lib/link/link", function() {
static src = "controller";
static dst = "control";
}
libLink.Link.register(UnhandledEvent);
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "UnhandledEvent"));
lib.Link.register(UnhandledEvent);
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "UnhandledEvent"));
assert.deepEqual(testConnector.sentMessages, []);
});
it("should ignore invalid src", function() {
@@ -185,9 +183,9 @@ describe("lib/link/link", function() {
static dst = "control";
}
let handled = false;
libLink.Link.register(InvalidSrcEvent);
lib.Link.register(InvalidSrcEvent);
testLink.snoopEvent(InvalidSrcEvent, async () => { handled = true; });
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "InvalidSrcEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "InvalidSrcEvent"));
assert(!handled, "event was not ignored");
assert.deepEqual(testConnector.sentMessages, []);
});
@@ -197,25 +195,25 @@ describe("lib/link/link", function() {
static src = "controller";
static dst = "host";
}
libLink.Link.register(InvalidDstEvent);
lib.Link.register(InvalidDstEvent);
let handled = false;
testLink.snoopEvent(InvalidDstEvent, async () => { handled = true; });
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "InvalidDstEvent"));
testConnector.emit("message", new libData.MessageEvent(1, dst, addr("allControls"), "InvalidDstEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "InvalidDstEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, addr("allControls"), "InvalidDstEvent"));
assert(!handled, "event was not ignored");
assert.deepEqual(testConnector.sentMessages, []);
});
it("should ignore broadcast src", function() {
let handled = false;
testLink.snoopEvent(SimpleEvent, async () => { handled = true; });
testConnector.emit("message", new libData.MessageEvent(1, addr("allControls"), src, "SimpleEvent"));
testConnector.emit("message", new lib.MessageEvent(1, addr("allControls"), src, "SimpleEvent"));
assert(!handled, "event was not ignored");
assert.deepEqual(testConnector.sentMessages, []);
});
it("should handle event with broadcast dst", function() {
let handled = false;
testLink.snoopEvent(SimpleEvent, async () => { handled = true; });
testConnector.emit("message", new libData.MessageEvent(1, dst, addr("allControls"), "SimpleEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, addr("allControls"), "SimpleEvent"));
assert(handled, "event was ignored");
assert.deepEqual(testConnector.sentMessages, []);
});
@@ -225,14 +223,14 @@ describe("lib/link/link", function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testConnector.emit("disconnectPrepare");
assert.deepEqual(await message, [new libData.MessageDisconnect("ready")]);
assert.deepEqual(await message, [new lib.MessageDisconnect("ready")]);
});
it("should send ready on connector prepareDisconnect if an error occurs", async function() {
testLink.prepareDisconnect = async () => { throwSimple("Error occured"); };
let message = events.once(testConnector, "send");
message.catch(() => {});
testConnector.emit("disconnectPrepare");
assert.deepEqual(await message, [new libData.MessageDisconnect("ready")]);
assert.deepEqual(await message, [new lib.MessageDisconnect("ready")]);
});
it("should reject pending requests on close", async function() {
@@ -253,39 +251,39 @@ describe("lib/link/link", function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testLink.send(new SimpleRequest());
let srcReq = new libData.Address(libData.Address.control, 1, 1);
let srcReq = new lib.Address(lib.Address.control, 1, 1);
assert.deepEqual(
await message,
[new libData.MessageRequest(1, srcReq, dst, "SimpleRequest", undefined)]
[new lib.MessageRequest(1, srcReq, dst, "SimpleRequest", undefined)]
);
});
it("should send request with data to the other side of the link", async function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testLink.send(new NumberRequest(22));
let srcReq = new libData.Address(libData.Address.control, 1, 1);
let srcReq = new lib.Address(lib.Address.control, 1, 1);
assert.deepEqual(
await message,
[new libData.MessageRequest(1, srcReq, dst, "NumberRequest", new NumberRequest(22))]
[new lib.MessageRequest(1, srcReq, dst, "NumberRequest", new NumberRequest(22))]
);
});
it("should send request and resolve when response is received", async function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
let request = testLink.send(new SimpleRequest());
let srcReq = new libData.Address(libData.Address.control, 1, 1);
testConnector.emit("message", new libData.MessageResponse(1, dst, srcReq));
let srcReq = new lib.Address(lib.Address.control, 1, 1);
testConnector.emit("message", new lib.MessageResponse(1, dst, srcReq));
await request;
});
it("should send request and reject with error when error response is received", async function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
let request = testLink.send(new SimpleRequest());
let srcReq = new libData.Address(libData.Address.control, 1, 1);
let srcReq = new lib.Address(lib.Address.control, 1, 1);
testConnector.emit(
"message",
new libData.MessageResponseError(
1, dst, srcReq, new libData.ResponseError("Error")
new lib.MessageResponseError(
1, dst, srcReq, new lib.ResponseError("Error")
)
);
await assert.rejects(
@@ -297,8 +295,8 @@ describe("lib/link/link", function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
let request = testLink.send(new NumberRequest(22));
let srcReq = new libData.Address(libData.Address.control, 1, 1);
testConnector.emit("message", new libData.MessageResponse(1, dst, srcReq, 44));
let srcReq = new lib.Address(lib.Address.control, 1, 1);
testConnector.emit("message", new lib.MessageResponse(1, dst, srcReq, 44));
assert.deepEqual(await request, new NumberRequest.Response(44));
});
it("should send event to the other side of the link", async function() {
@@ -307,7 +305,7 @@ describe("lib/link/link", function() {
testLink.send(new SimpleEvent());
assert.deepEqual(
await message,
[new libData.MessageEvent(1, src, dst, "SimpleEvent", undefined)]
[new lib.MessageEvent(1, src, dst, "SimpleEvent", undefined)]
);
});
it("should send event with data to the other side of the link", async function() {
@@ -316,7 +314,7 @@ describe("lib/link/link", function() {
testLink.send(new NumberEvent(22));
assert.deepEqual(
await message,
[new libData.MessageEvent(1, src, dst, "NumberEvent", new NumberEvent(22))]
[new lib.MessageEvent(1, src, dst, "NumberEvent", new NumberEvent(22))]
);
});
});
@@ -325,18 +323,18 @@ describe("lib/link/link", function() {
let handled = false;
testLink.handle(SimpleRequest, async () => { handled = true; });
assert(testLink._requestHandlers.has(SimpleRequest), "request handler was not registered");
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "SimpleRequest"));
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "SimpleRequest"));
assert(handled, "request was not handled");
});
it("should send response error from request handler throwing", async function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testLink.handle(SimpleRequest, async () => { throwSimple("Error"); });
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "SimpleRequest"));
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "SimpleRequest"));
assert.deepEqual(
await message,
[new libData.MessageResponseError(
1, src, dst, new libData.ResponseError("Error", undefined, "Error")
[new lib.MessageResponseError(
1, src, dst, new lib.ResponseError("Error", undefined, "Error")
)]
);
});
@@ -344,13 +342,13 @@ describe("lib/link/link", function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testLink.handle(NumberRequest, async () => 1);
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "NumberRequest", "not a number"));
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "NumberRequest", "not a number"));
let response = (await message)[0];
assert.deepEqual(
response,
new libData.MessageResponseError(
new lib.MessageResponseError(
1, src, dst,
new libData.ResponseError(
new lib.ResponseError(
"Request NumberRequest failed validation",
response.data.code,
response.data.stack,
@@ -362,13 +360,13 @@ describe("lib/link/link", function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testLink.handle(NumberRequest, async () => "not a number");
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "NumberRequest", 1));
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "NumberRequest", 1));
let response = (await message)[0];
assert.deepEqual(
response,
new libData.MessageResponseError(
new lib.MessageResponseError(
1, src, dst,
new libData.ResponseError(
new lib.ResponseError(
"Response for request NumberRequest failed validation",
response.data.code,
response.data.stack,
@@ -380,29 +378,29 @@ describe("lib/link/link", function() {
let message = events.once(testConnector, "send");
message.catch(() => {});
testLink.handle(NumberRequest, async (request) => request.value + 4);
testConnector.emit("message", new libData.MessageRequest(1, dst, src, "NumberRequest", 1));
testConnector.emit("message", new lib.MessageRequest(1, dst, src, "NumberRequest", 1));
let response = (await message)[0];
assert.deepEqual(
response,
new libData.MessageResponse(1, src, dst, 5)
new lib.MessageResponse(1, src, dst, 5)
);
});
it("should register an event handler", function() {
let handled = false;
testLink.handle(SimpleEvent, async () => { handled = true; });
assert(testLink._eventHandlers.has(SimpleEvent), "event handler was not registered");
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "SimpleEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "SimpleEvent"));
assert(handled, "event was not handled");
});
it("should pass value to event handler", async function() {
let value;
testLink.handle(NumberEvent, async (event) => { value = event.value; });
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "NumberEvent", 9));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "NumberEvent", 9));
assert.deepEqual(value, 9);
});
it("should log errors from event handler", function() {
testLink.handle(SimpleEvent, async () => { throwSimple("Error"); });
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "SimpleEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "SimpleEvent"));
});
it("should throw on unknown type", function() {
assert.throws(
@@ -432,7 +430,7 @@ describe("lib/link/link", function() {
static jsonSchema = {};
}
assert.throws(
() => libLink.Link.register(BadRequest1),
() => lib.Link.register(BadRequest1),
new Error("Request BadRequest1 has static jsonSchema but is missing static fromJSON")
);
class BadRequest2 {
@@ -442,7 +440,7 @@ describe("lib/link/link", function() {
static fromJSON() {};
}
assert.throws(
() => libLink.Link.register(BadRequest2),
() => lib.Link.register(BadRequest2),
new Error("Request BadRequest2 has static fromJSON but is missing static jsonSchema")
);
});
@@ -454,7 +452,7 @@ describe("lib/link/link", function() {
static jsonSchema = {};
}
assert.throws(
() => libLink.Link.register(BadEvent1),
() => lib.Link.register(BadEvent1),
new Error("Event BadEvent1 has static jsonSchema but is missing static fromJSON")
);
class BadEvent2 {
@@ -464,7 +462,7 @@ describe("lib/link/link", function() {
static fromJSON() {};
}
assert.throws(
() => libLink.Link.register(BadEvent2),
() => lib.Link.register(BadEvent2),
new Error("Event BadEvent2 has static fromJSON but is missing static jsonSchema")
);
});
@@ -486,10 +484,10 @@ describe("lib/link/link", function() {
static jsonSchema = { type: "string" };
static fromJSON(json) { return new this(json); };
}
libLink.Link.register(StringEvent);
lib.Link.register(StringEvent);
testLink.handle(StringEvent, () => {});
assert.throws(
() => testLink._processMessage(new libData.MessageEvent(1, dst, src, "StringEvent", 99)),
() => testLink._processMessage(new lib.MessageEvent(1, dst, src, "StringEvent", 99)),
{ message: "Event StringEvent failed validation" }
);
});
@@ -500,12 +498,12 @@ describe("lib/link/link", function() {
let handled = false;
testLink.snoopEvent(SimpleEvent, async () => { handled = true; });
assert(testLink._eventSnoopers.has(SimpleEvent), "event was not snooped");
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "SimpleEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "SimpleEvent"));
assert(handled, "event was not handled");
});
it("should log errors from snoop handler", function() {
testLink.snoopEvent(SimpleEvent, async () => { throwSimple("Error"); });
testConnector.emit("message", new libData.MessageEvent(1, dst, src, "SimpleEvent"));
testConnector.emit("message", new lib.MessageEvent(1, dst, src, "SimpleEvent"));
});
it("should throw on double registration", function() {
testLink.snoopEvent(SimpleEvent);