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

+18 -21
View File
@@ -2,19 +2,16 @@
const assert = require("assert").strict;
const fs = require("fs-extra");
const path = require("path");
const util = require("util");
const { testLines } = require("./factorio/lines");
const libLoggingUtils = require("@clusterio/lib/logging_utils");
const libStream = require("@clusterio/lib/stream");
const lib = require("@clusterio/lib");
describe("lib/logging_utils.js", function() {
describe("formatServerOutput", function() {
it("should pass the test lines", function() {
for (let [reference, output] of testLines) {
let line = libLoggingUtils._formatServerOutput(output);
let line = lib._formatServerOutput(output);
// Strip colours
line = line.replace(/\x1B\[\d+m/g, "");
assert.deepEqual(line, reference);
@@ -25,11 +22,11 @@ describe("lib/logging_utils.js", function() {
let logDir = path.join("test", "file", "log");
describe("class LogIndex", function() {
it("should create a new empty index if index.json does not exist", async function() {
let logIndex = await libLoggingUtils.LogIndex.load(logDir);
let logIndex = await lib.LogIndex.load(logDir);
assert.deepEqual(logIndex.index, new Map());
});
it("should build index of only past log files", async function() {
let logIndex = await libLoggingUtils.LogIndex.load(logDir);
let logIndex = await lib.LogIndex.load(logDir);
await logIndex.buildIndex();
assert.deepEqual(
new Set(logIndex.index.keys()),
@@ -37,12 +34,12 @@ describe("lib/logging_utils.js", function() {
);
});
it("should roundtrip index on save load cycle", async function() {
let logIndex = await libLoggingUtils.LogIndex.load(logDir);
let logIndex = await lib.LogIndex.load(logDir);
await logIndex.buildIndex();
let tempDir = path.join("temp", "test", "log");
logIndex.logDirectory = tempDir;
await logIndex.save();
let loadedIndex = await libLoggingUtils.LogIndex.load(tempDir);
let loadedIndex = await lib.LogIndex.load(tempDir);
assert.deepEqual(
loadedIndex,
logIndex
@@ -51,7 +48,7 @@ describe("lib/logging_utils.js", function() {
it("should handle loading broken index", async function() {
let tempDir = path.join("temp", "test", "log");
await fs.outputFile(path.join(tempDir, "index.json"), "Broken JSON");
let logIndex = await libLoggingUtils.LogIndex.load(tempDir);
let logIndex = await lib.LogIndex.load(tempDir);
assert.deepEqual(logIndex.index, new Map());
});
});
@@ -64,7 +61,7 @@ describe("lib/logging_utils.js", function() {
continue;
}
let fileStream = fs.createReadStream(path.join(logDir, file));
let lineStream = new libStream.LineSplitter({ readableObjectMode: true });
let lineStream = new lib.LineSplitter({ readableObjectMode: true });
fileStream.pipe(lineStream);
for await (let line of lineStream) {
try {
@@ -75,7 +72,7 @@ describe("lib/logging_utils.js", function() {
}
}
logIndex = await libLoggingUtils.LogIndex.load(logDir);
logIndex = await lib.LogIndex.load(logDir);
await logIndex.buildIndex();
});
@@ -83,45 +80,45 @@ describe("lib/logging_utils.js", function() {
for (let index of [() => undefined, () => logIndex]) {
describe(index() === undefined ? "without index" : "with index", function() {
it("returns nothing with an empty filter", async function() {
let log = await libLoggingUtils.queryLog(logDir, {}, index());
let log = await lib.queryLog(logDir, {}, index());
assert.deepEqual(log, []);
});
it("should return the whole log when querying all", async function() {
let log = await libLoggingUtils.queryLog(logDir, { all: true }, index());
let log = await lib.queryLog(logDir, { all: true }, index());
assert.deepEqual(log, allLines);
});
it("should return the whole log in reverse when querying all desc", async function() {
let log = await libLoggingUtils.queryLog(logDir, { all: true, order: "desc" }, index());
let log = await lib.queryLog(logDir, { all: true, order: "desc" }, index());
assert.deepEqual(log, [...allLines].reverse());
});
it("should limit by limit", async function() {
let log = await libLoggingUtils.queryLog(logDir, { limit: 2, all: true }, index());
let log = await lib.queryLog(logDir, { limit: 2, all: true }, index());
assert.deepEqual(log, allLines.slice(0, 2));
});
it("should filter by maxLevel", async function() {
let log = await libLoggingUtils.queryLog(logDir, { maxLevel: "info", all: true }, index());
let log = await lib.queryLog(logDir, { maxLevel: "info", all: true }, index());
assert.deepEqual(log, allLines.filter(
info => ["fatal", "error", "warn", "audit", "info"].includes(info.level)
));
log = await libLoggingUtils.queryLog(logDir, { maxLevel: "fatal", all: true }, index());
log = await lib.queryLog(logDir, { maxLevel: "fatal", all: true }, index());
assert.deepEqual(log, allLines.filter(
info => ["fatal"].includes(info.level)
));
});
it("should filter by controller", async function() {
let log = await libLoggingUtils.queryLog(logDir, { controller: true }, index());
let log = await lib.queryLog(logDir, { controller: true }, index());
assert.deepEqual(log, allLines.filter(
info => info.host_id === undefined && info.instance_id === undefined
));
});
it("should filter by hostIds", async function() {
let log = await libLoggingUtils.queryLog(logDir, { hostIds: [1, 2] }, index());
let log = await lib.queryLog(logDir, { hostIds: [1, 2] }, index());
assert.deepEqual(log, allLines.filter(
info => [1, 2].includes(info.host_id) && info.instance_id === undefined
));
});
it("should filter by instanceIds", async function() {
let log = await libLoggingUtils.queryLog(logDir, { instanceIds: [10, 11] }, index());
let log = await lib.queryLog(logDir, { instanceIds: [10, 11] }, index());
assert.deepEqual(log, allLines.filter(info => [10, 11].includes(info.instance_id)));
});
});