Compare commits
4 commits
main
...
manual-pus
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3298542143 | ||
![]() |
9b255532af | ||
![]() |
443589bfce | ||
![]() |
d342929284 |
18 changed files with 252 additions and 2210 deletions
|
@ -1,2 +0,0 @@
|
|||
node_modules/
|
||||
dist/
|
10
.eslintrc
10
.eslintrc
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"root": true,
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended"
|
||||
]
|
||||
}
|
26
.gitattributes
vendored
26
.gitattributes
vendored
|
@ -1,26 +0,0 @@
|
|||
# js / ts files
|
||||
*.js text
|
||||
*.ts text
|
||||
|
||||
# Config text files
|
||||
*.jsonc text
|
||||
*.json text
|
||||
*.yml text
|
||||
.eslintrc text
|
||||
.eslintignore text
|
||||
.gitignore text
|
||||
.gitattributes text
|
||||
.prettierignore text
|
||||
.prettierrc text
|
||||
|
||||
# Standard text files
|
||||
*.txt text
|
||||
*.md text
|
||||
*.sh text eol=lf
|
||||
*.bat text eol=crlf
|
||||
LICENSE text
|
||||
|
||||
# Binary files
|
||||
*.gif binary
|
||||
*.jpg binary
|
||||
*.png binary
|
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"extends": "@istanbuljs/nyc-config-typescript",
|
||||
"all": true
|
||||
"all": true,
|
||||
"include": ["src/**"]
|
||||
}
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
#!/usr/bin/env -S node
|
||||
import { start } from "../src/index.js";
|
||||
#!/usr/bin/env -S node --experimental-specifier-resolution=node
|
||||
import { start, push, pull } from "../src/index";
|
||||
import parser from "yargs-parser";
|
||||
|
||||
await start();
|
||||
const argv = parser(process.argv.slice(2));
|
||||
|
||||
if (argv.push == true) await push();
|
||||
else if (argv.pull == true) await pull();
|
||||
else await start();
|
||||
|
|
2152
package-lock.json
generated
2152
package-lock.json
generated
File diff suppressed because it is too large
Load diff
18
package.json
18
package.json
|
@ -6,12 +6,10 @@
|
|||
"bin": "./dist/npx/bitburner-filesync.js",
|
||||
"main": "./dist/npx/bitburner-filesync.js",
|
||||
"scripts": {
|
||||
"prepare": "npm run build",
|
||||
"build": "tsc --build --verbose --pretty",
|
||||
"clean": "tsc --build --clean",
|
||||
"format": "prettier -w .",
|
||||
"format:report": "prettier -c .",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"test:all": "tsc && nyc mocha --recursive test",
|
||||
"test:single": "mocha",
|
||||
"start": "ts-node npx/bitburner-filesync.ts"
|
||||
|
@ -33,10 +31,11 @@
|
|||
"homepage": "https://github.com/bitburner-official/bitburner-filesync#readme",
|
||||
"dependencies": {
|
||||
"cheap-watch": "^1.0.4",
|
||||
"convict": "^6.2.4",
|
||||
"convict": "^6.2.3",
|
||||
"signal-js": "^3.0.1",
|
||||
"typescript": "^4.8.4",
|
||||
"ws": "^8.8.1"
|
||||
"ws": "^8.8.1",
|
||||
"yargs-parser": "^21.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@istanbuljs/nyc-config-typescript": "^1.0.2",
|
||||
|
@ -48,17 +47,8 @@
|
|||
"@types/sinon": "^10.0.13",
|
||||
"@types/sinon-chai": "^3.2.8",
|
||||
"@types/ws": "^8.5.3",
|
||||
"@typescript-eslint/eslint-plugin": "^5.40.0",
|
||||
"@typescript-eslint/parser": "^5.40.0",
|
||||
"@types/parse-json": "^4.0.0",
|
||||
"@types/react": "^18.0.34",
|
||||
"@types/react-is": "^17.0.3",
|
||||
"@types/react-transition-group": "^4.4.5",
|
||||
"@types/prop-types": "^15.7.5",
|
||||
"@types/scheduler": "^0.16.3",
|
||||
"chai": "^4.3.6",
|
||||
"eslint": "^8.25.0",
|
||||
"mocha": "^10.1.0",
|
||||
"mocha": "^10.0.0",
|
||||
"nyc": "^15.1.0",
|
||||
"prettier": "^2.7.1",
|
||||
"sinon": "^14.0.0",
|
||||
|
|
|
@ -2,7 +2,7 @@ import convict from "convict";
|
|||
import { existsSync } from "fs";
|
||||
|
||||
// Define a schema
|
||||
export const config = convict({
|
||||
export let config = convict({
|
||||
allowedFiletypes: {
|
||||
doc: "Filetypes that are synchronized to the game.",
|
||||
format: "Array",
|
||||
|
@ -28,11 +28,6 @@ export const config = convict({
|
|||
env: "BB_SCRIPTFOLDER",
|
||||
arg: "folder",
|
||||
},
|
||||
exclude: {
|
||||
doc: "A list of folders or files to exclude from the sync.",
|
||||
format: "Array",
|
||||
default: [".vscode", ".idea", ".github"],
|
||||
},
|
||||
quiet: {
|
||||
doc: "Log less internal events to stdout.",
|
||||
format: "Boolean",
|
||||
|
@ -68,6 +63,13 @@ export const config = convict({
|
|||
default: false,
|
||||
arg: "pushAllOnConnection",
|
||||
},
|
||||
disableWatch: {
|
||||
doc: "Disable everything besides push/pulling through the external calls.",
|
||||
format: "Boolean",
|
||||
env: "BB_MANUAL",
|
||||
default: false,
|
||||
arg: "manual",
|
||||
},
|
||||
});
|
||||
|
||||
export function loadConfig() {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
export class EventType {
|
||||
static ConnectionMade = "ConnectionMade";
|
||||
static FileChanged = "FileChanged";
|
||||
static FileDeleted = "FileDeleted";
|
||||
static MessageReceived = "MessageReceived";
|
||||
static MessageSend = "MessageSend";
|
||||
export enum EventType {
|
||||
ConnectionMade,
|
||||
FileChanged,
|
||||
FileDeleted,
|
||||
MessageReceived,
|
||||
MessageSend,
|
||||
}
|
||||
|
|
|
@ -1,45 +1,21 @@
|
|||
import CheapWatch from "cheap-watch";
|
||||
import { config } from "./config.js";
|
||||
import { EventType } from "./eventTypes.js";
|
||||
import { config } from "./config";
|
||||
import { EventType } from "./eventTypes";
|
||||
import { mkdir } from "fs/promises";
|
||||
import { resolve, relative, isAbsolute } from "path";
|
||||
import { resolve } from "path";
|
||||
import type { Signal } from "signal-js";
|
||||
import type { File } from "./interfaces.js";
|
||||
import type { File } from "./interfaces";
|
||||
|
||||
/**
|
||||
* Returns true if a directory is a subdirectory of a parent directory (not necessarily strict).
|
||||
* @param dir The directory to perform the check on.
|
||||
* @param parent The parent directory.
|
||||
*/
|
||||
function isSubDirOf(dir: string, parent: string) {
|
||||
const relPath = relative(resolve(parent), resolve(dir));
|
||||
return !!relPath && !relPath.startsWith("..") && !isAbsolute(relPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given file should be watched.
|
||||
* @param file The provided file.
|
||||
*/
|
||||
function fileFilter(file: File) {
|
||||
// If the file is excluded, skip all other checks and ignore it.
|
||||
if (config.get("exclude").some((x) => isSubDirOf(file.path, x))) return false;
|
||||
if (config.get("allowedFiletypes").some((extension) => file.path.endsWith(extension))) return true;
|
||||
if (file.stats.isDirectory()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard for {@code NodeJS.ErrnoException}.
|
||||
* @param err
|
||||
*/
|
||||
function isError(err: unknown): err is NodeJS.ErrnoException {
|
||||
return (err as NodeJS.ErrnoException).code !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the file watch.
|
||||
* @param signaller The signal event emitter.
|
||||
*/
|
||||
export async function setupWatch(signaller: Signal) {
|
||||
try {
|
||||
await mkdir(resolve(config.get("scriptsFolder")));
|
||||
|
|
64
src/index.ts
64
src/index.ts
|
@ -1,6 +1,7 @@
|
|||
import { setupWatch } from "./fileWatch.js";
|
||||
import { config, loadConfig } from "./config.js";
|
||||
import { setupSocket } from "./networking/webSocket.js";
|
||||
import { setupWatch } from "./fileWatch";
|
||||
import { config, loadConfig } from "./config";
|
||||
import { setupSocket } from "./networking/webSocket";
|
||||
import { WebSocket } from "ws";
|
||||
import signal from "signal-js";
|
||||
import { RawData } from "ws";
|
||||
import {
|
||||
|
@ -8,14 +9,13 @@ import {
|
|||
fileRemovalEventToMsg,
|
||||
requestFilenames,
|
||||
requestDefinitionFile,
|
||||
} from "./networking/messageGenerators.js";
|
||||
import { EventType } from "./eventTypes.js";
|
||||
import { messageHandler } from "./networking/messageHandler.js";
|
||||
import { FileEvent } from "./interfaces.js";
|
||||
} from "./networking/messageGenerators";
|
||||
import { EventType } from "./eventTypes";
|
||||
import { messageHandler } from "./networking/messageHandler";
|
||||
import { FileEvent } from "./interfaces";
|
||||
import { exit } from "process";
|
||||
import CheapWatch from "cheap-watch";
|
||||
|
||||
/**
|
||||
* Starts the file watcher.
|
||||
*/
|
||||
export async function start() {
|
||||
loadConfig();
|
||||
const watch = await setupWatch(signal);
|
||||
|
@ -24,6 +24,18 @@ export async function start() {
|
|||
// Add a handler for received messages.
|
||||
signal.on(EventType.MessageReceived, (msg: RawData) => messageHandler(signal, msg, watch.paths));
|
||||
|
||||
console.log(`Server is ready, running on ${config.get("port")}!`);
|
||||
|
||||
process.on("SIGINT", function () {
|
||||
console.log("Shutting down!");
|
||||
|
||||
watch.close();
|
||||
socket.close();
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
|
||||
async function watchMode(watch: CheapWatch) {
|
||||
// Add a handler for when a connection to a game is made.
|
||||
signal.on(EventType.ConnectionMade, () => {
|
||||
console.log("Connection made!");
|
||||
|
@ -55,14 +67,26 @@ export async function start() {
|
|||
signal.on(EventType.FileDeleted, (fileEvent: FileEvent) =>
|
||||
signal.emit(EventType.MessageSend, fileRemovalEventToMsg(fileEvent)),
|
||||
);
|
||||
|
||||
console.log(`Server is ready, running on ${config.get("port")}!`);
|
||||
|
||||
process.on("SIGINT", function () {
|
||||
console.log("Shutting down!");
|
||||
|
||||
watch.close();
|
||||
socket.close();
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
|
||||
export async function pull() {
|
||||
loadConfig();
|
||||
const ws = new WebSocket(`ws://localhost:${config.get("port")}`);
|
||||
|
||||
ws.onopen = () => {
|
||||
ws.send(JSON.stringify({ jsonrpc: "2.0", method: "manualPullFromGame", id: Number.MAX_SAFE_INTEGER }));
|
||||
ws.close();
|
||||
exit(0);
|
||||
};
|
||||
}
|
||||
|
||||
export async function push() {
|
||||
loadConfig();
|
||||
const ws = new WebSocket(`ws://localhost:${config.get("port")}`);
|
||||
|
||||
ws.onopen = () => {
|
||||
ws.send(JSON.stringify({ jsonrpc: "2.0", method: "manualPushToGame", id: Number.MAX_SAFE_INTEGER }));
|
||||
ws.close();
|
||||
exit(0);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { readFileSync } from "fs";
|
||||
import { config } from "../config.js";
|
||||
import { config } from "../config";
|
||||
import { join } from "path";
|
||||
import type { FileEvent, Message } from "../interfaces.js";
|
||||
import type { FileEvent, Message } from "../interfaces";
|
||||
|
||||
let messageCounter = 0;
|
||||
|
||||
|
@ -49,6 +49,17 @@ export function requestFilenames(): Message {
|
|||
};
|
||||
}
|
||||
|
||||
export function requestAllFiles(): Message {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
method: "getAllFiles",
|
||||
params: {
|
||||
server: "home",
|
||||
},
|
||||
id: messageCounter++,
|
||||
};
|
||||
}
|
||||
|
||||
function addLeadingSlash(path: string): string {
|
||||
const slashes = path.match("/");
|
||||
if (slashes) return `/${path}`;
|
||||
|
|
|
@ -1,11 +1,32 @@
|
|||
import { messageTracker } from "./messageTracker.js";
|
||||
import { messageTracker } from "./messageTracker";
|
||||
import { Stats, writeFile } from "fs";
|
||||
import { RawData } from "ws";
|
||||
import { config } from "../config.js";
|
||||
import { EventType } from "../eventTypes.js";
|
||||
import { fileChangeEventToMsg } from "./messageGenerators.js";
|
||||
import { config } from "../config";
|
||||
import { EventType } from "../eventTypes";
|
||||
import { fileChangeEventToMsg, requestAllFiles } from "./messageGenerators";
|
||||
import type { Signal } from "signal-js";
|
||||
import { Message } from "../interfaces.js";
|
||||
import { FileContent, Message } from "../interfaces";
|
||||
|
||||
/*function deserialize(data: RawData): Message {
|
||||
const msg = JSON.parse(data.toString());
|
||||
|
||||
if (typeof msg.jsonrpc !== "string" || msg.jsonrpc !== "2.0" || typeof msg.id !== "number") {
|
||||
throw Error("Malformed data received.");
|
||||
}
|
||||
|
||||
const id: number = msg.id;
|
||||
let request = messageTracker.get(id);
|
||||
|
||||
console.log(msg);
|
||||
|
||||
if (msg.error != null) {
|
||||
throw Error(msg.error);
|
||||
} else if (msg.result == null) {
|
||||
throw Error("Malformed JSON received.");
|
||||
}
|
||||
|
||||
return { jsonrpc: "2.0", method: !request? msg.method : request.method, result: msg.result, id };
|
||||
}*/
|
||||
|
||||
function deserialize(data: RawData): Message {
|
||||
const msg = JSON.parse(data.toString());
|
||||
|
@ -15,23 +36,26 @@ function deserialize(data: RawData): Message {
|
|||
}
|
||||
|
||||
const id: number = msg.id;
|
||||
const request = messageTracker.get(id);
|
||||
const request = id === Number.MAX_SAFE_INTEGER ? { method: msg.method } : messageTracker.get(id);
|
||||
|
||||
if (typeof request?.method !== "string") {
|
||||
throw Error("Malformed JSON received.");
|
||||
} else if (msg.error != null) {
|
||||
console.log("M:", msg);
|
||||
console.log("R:", request);
|
||||
|
||||
if (msg.error != null) {
|
||||
throw Error(msg.error);
|
||||
} else if (msg.result == null) {
|
||||
throw Error("Malformed JSON received.");
|
||||
}
|
||||
|
||||
return { jsonrpc: "2.0", method: request.method, result: msg.result, id };
|
||||
return { jsonrpc: "2.0", method: request?.method, result: msg.result, id };
|
||||
}
|
||||
|
||||
export function isStringArray(s: Array<unknown>): s is string[] {
|
||||
return s.every((s) => typeof s === "string");
|
||||
}
|
||||
|
||||
export function isFileContentArray(a: Array<unknown>): a is FileContent[] {
|
||||
return a.every((f) => !!f && Object.hasOwn(f, "filename") && Object.hasOwn(f, "content"));
|
||||
}
|
||||
|
||||
export function messageHandler(signaller: Signal, data: RawData, paths: Map<string, Stats>) {
|
||||
let incoming;
|
||||
|
||||
|
@ -49,9 +73,9 @@ export function messageHandler(signaller: Signal, data: RawData, paths: Map<stri
|
|||
writeFile(config.get("definitionFile").location, incoming.result, (err) => {
|
||||
if (err) return console.log(err);
|
||||
});
|
||||
|
||||
break;
|
||||
case "getFileNames": {
|
||||
|
||||
case "getFileNames":
|
||||
if (!Array.isArray(incoming.result) || !isStringArray(incoming.result))
|
||||
return console.log("Malformed data received.");
|
||||
|
||||
|
@ -61,7 +85,30 @@ export function messageHandler(signaller: Signal, data: RawData, paths: Map<stri
|
|||
if (!stats.isDirectory() && !gameFiles.includes(fileName))
|
||||
signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName }));
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "getAllFiles":
|
||||
if (!Array.isArray(incoming.result) || !isFileContentArray(incoming.result))
|
||||
return console.log("Malformed data received.");
|
||||
|
||||
const gameFiles2 = incoming.result.map((f: FileContent) => {
|
||||
return { filename: removeLeadingSlash(f.filename), content: f.content };
|
||||
});
|
||||
|
||||
gameFiles2.forEach((f) => console.log(f));
|
||||
break;
|
||||
|
||||
case "manualPullFromGame":
|
||||
console.log("PULL");
|
||||
signaller.emit(EventType.MessageSend, requestAllFiles());
|
||||
break;
|
||||
|
||||
case "manualPushToGame":
|
||||
console.log("PUSH");
|
||||
paths.forEach((stats, fileName) => {
|
||||
if (!stats.isDirectory()) signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName }));
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import type { Message } from "../interfaces.js";
|
||||
import type { Message } from "../interfaces";
|
||||
|
||||
class MessageTracker {
|
||||
data = new Map<number, Message>();
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import type { Signal } from "signal-js";
|
||||
import { WebSocketServer } from "ws";
|
||||
import { config } from "../config.js";
|
||||
import { EventType } from "../eventTypes.js";
|
||||
import { Message } from "../interfaces.js";
|
||||
import { messageTracker } from "./messageTracker.js";
|
||||
import { config } from "../config";
|
||||
import { EventType } from "../eventTypes";
|
||||
import { Message } from "../interfaces";
|
||||
import { messageTracker } from "./messageTracker";
|
||||
|
||||
export function setupSocket(signaller: Signal) {
|
||||
const wss = new WebSocketServer({ port: config.get("port") });
|
||||
|
|
6
src/signals.d.ts
vendored
6
src/signals.d.ts
vendored
|
@ -1,8 +1,8 @@
|
|||
declare module "signal-js" {
|
||||
export type Signal = typeof signal;
|
||||
export default class signal {
|
||||
static on<T>(event: string, callback: (data: T) => void): void;
|
||||
static emit<T>(event: string, data: T): void;
|
||||
static trigger(event: string): void;
|
||||
static on<T>(event: number, callback: (data: T) => void): void;
|
||||
static emit<T>(event: number, data: T): void;
|
||||
static trigger(event: number): void;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ describe("messageGenerators", () => {
|
|||
const msg = fileRemovalEventToMsg({ path: "sub/test.js" });
|
||||
const result = (msg?.params as FileData).filename;
|
||||
|
||||
if (result && Object.prototype.hasOwnProperty.call(result, "filename")) expect(result).to.eq("/sub/test.js");
|
||||
if (result && result.hasOwnProperty("filename")) expect(result).to.eq("/sub/test.js");
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -46,7 +46,7 @@ describe("messageGenerators", () => {
|
|||
const msg = fileRemovalEventToMsg({ path: "test.js" });
|
||||
const result = (msg?.params as FileData).filename;
|
||||
|
||||
if (result && Object.prototype.hasOwnProperty.call(result, "filename")) expect(result).to.eq("test.js");
|
||||
if (result && result.hasOwnProperty("filename")) expect(result).to.eq("test.js");
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -55,7 +55,7 @@ describe("messageGenerators", () => {
|
|||
const msg = fileRemovalEventToMsg({ path: "/sub/test.js" });
|
||||
const result = (msg?.params as FileData).filename;
|
||||
|
||||
if (result && Object.prototype.hasOwnProperty.call(result, "filename")) expect(result).to.eq("/sub/test.js");
|
||||
if (result && result.hasOwnProperty("filename")) expect(result).to.eq("/sub/test.js");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import chai from "chai";
|
||||
import { createSandbox } from "sinon";
|
||||
import { createSandbox, stub } from "sinon";
|
||||
import sinonChai from "sinon-chai";
|
||||
|
||||
export async function mochaGlobalSetup() {
|
||||
|
|
Loading…
Add table
Reference in a new issue