Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on any ESLint failed check. #43

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/frontend-eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ jobs:
with:
workdir: ./frontend
reporter: github-pr-check
fail_on_error: true
fail_level: any
filter_mode: nofilter
12 changes: 11 additions & 1 deletion frontend/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import tsEsLint from "typescript-eslint";

export default [
{
ignores: ["src/env.d.ts", "src/generated/flatbuffers_schema/"],
ignores: [".astro/", "src/env.d.ts", "src/generated/flatbuffers_schema/"],
},
...tsEsLint.configs.recommended,
...eslintPluginAstro.configs["flat/recommended"],
{
rules: {
// override/add rules settings here, such as:
// "astro/no-set-html-directive": "error"
"@typescript-eslint/no-unused-vars": [
"error",
{
args: "after-used",
argsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
},
},
];
33 changes: 33 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/node": "^22.10.1",
"eslint": "^9.17.0",
"eslint-plugin-astro": "^1.3.1",
"happy-dom": "^15.11.7",
"prettier": "^3.4.2",
"prettier-plugin-astro": "^0.14.1",
"sass": "^1.81.0",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/scripts/client/functions/ensureBinary.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { BinaryLike } from "../types/BinaryLike.ts";

const textEncoder = new TextEncoder();

export const ensureBinary = (data: BinaryLike): Uint8Array => {
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/scripts/client/functions/jsonUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ describe("JSON Utils", () => {

testCases.forEach((testCase) => {
const serialised = jsonStringify(testCase);
const deserialised = jsonParse(serialised);
const deserialised: typeof testCase = jsonParse(serialised);
expect(deserialised).toEqual(testCase);
});
});

it("should handle Uint8Array", () => {
const original = new Uint8Array([1, 2, 3, 4, 5]);
const serialised = jsonStringify(original);
const deserialised = jsonParse(serialised);
const deserialised: typeof original = jsonParse(serialised);

expect(deserialised).toBeInstanceOf(Uint8Array);
expect(Array.from(deserialised)).toEqual(Array.from(original));
Expand All @@ -31,7 +31,7 @@ describe("JSON Utils", () => {
};

const serialised = jsonStringify(original);
const deserialised = jsonParse(serialised);
const deserialised: typeof original = jsonParse(serialised);

expect(deserialised.data).toBeInstanceOf(Uint8Array);
expect(deserialised.nested.buffer).toBeInstanceOf(Uint8Array);
Expand All @@ -43,7 +43,7 @@ describe("JSON Utils", () => {
const original = [new Uint8Array([1, 2]), new Uint8Array([3, 4]), new Uint8Array([5, 6])];

const serialised = jsonStringify(original);
const deserialised = jsonParse(serialised);
const deserialised: typeof original = jsonParse(serialised);

deserialised.forEach((arr: Uint8Array, index: number) => {
expect(arr).toBeInstanceOf(Uint8Array);
Expand All @@ -54,7 +54,7 @@ describe("JSON Utils", () => {
it("should handle empty Uint8Array", () => {
const original = new Uint8Array();
const serialised = jsonStringify(original);
const deserialised = jsonParse(serialised);
const deserialised: typeof original = jsonParse(serialised);

expect(deserialised).toBeInstanceOf(Uint8Array);
expect(deserialised.length).toBe(0);
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/scripts/client/functions/jsonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const TYPES = {
// Add more special types here as needed
};

function customReplacer(_key: string, value: any): any {
function customReplacer(_key: string, value: unknown): unknown {
if (value instanceof Uint8Array) {
return {
[TYPE_IDENTIFIER]: TYPES.UINT8ARRAY,
Expand All @@ -14,6 +14,7 @@ function customReplacer(_key: string, value: any): any {
return value;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function customReviver(_key: string, value: any): any {
if (value && typeof value === "object" && value[TYPE_IDENTIFIER]) {
switch (value[TYPE_IDENTIFIER]) {
Expand All @@ -26,10 +27,10 @@ function customReviver(_key: string, value: any): any {
return value;
}

export function jsonStringify(obj: any): string {
export function jsonStringify(obj: unknown): string {
return JSON.stringify(obj, customReplacer);
}

export function jsonParse<T = any>(text: string): T {
export function jsonParse<T = unknown>(text: string): T {
return JSON.parse(text, customReviver);
}
25 changes: 13 additions & 12 deletions frontend/src/scripts/client/models/RelayConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ensureBinary } from "../functions/ensureBinary.ts";
import type { ActivityId } from "../types/BigIntLike.ts";
import { TerminalInputBuilder } from "../serialisers/TerminalInputBuilder.ts";
import type { Runner } from "./Runner.ts";
import type { BinaryLike } from "../types/BinaryLike.ts";

export class RelayConnection {
constructor(
Expand All @@ -23,7 +24,7 @@ export class RelayConnection {
this.socket.addEventListener("error", (error) => this.onError(error));
}

private onOpen(event: Event) {
private onOpen(_event: Event) {
console.log("Connected to WebSocket connection...");
this.initiateAuthentication();
}
Expand All @@ -37,13 +38,13 @@ export class RelayConnection {
}

private dispatchToRelay(f2r: F2rBuilder<Built>) {
let payload = f2r.toFlatbuffers();
const payload = f2r.toFlatbuffers();
this.socket.send(payload.data());
}

async dispatchToAgentEncrypted(payload: F2aBuilder<EncryptionReady>) {
console.log("Dispatching message to agent:");
let f2a = await payload.toFlatbuffersEncrypted(this.runner.cryptographer());
const f2a = await payload.toFlatbuffersEncrypted(this.runner.cryptographer());
this.dispatchToRelay(F2rBuilder.new().buildToAgent(f2a));
}

Expand All @@ -53,26 +54,26 @@ export class RelayConnection {
}

async dispatchTerminalUserInput(activityId: ActivityId, raw_data: BinaryLike) {
let tb = TerminalInputBuilder.new();
let input_ = tb.buildUserInput(ensureBinary(raw_data)).toFlatbuffers();
let f2a = F2aBuilder.new();
let payload = f2a.buildActivityInputMessage(activityId, input_);
const tb = TerminalInputBuilder.new();
const input_ = tb.buildUserInput(ensureBinary(raw_data)).toFlatbuffers();
const f2a = F2aBuilder.new();
const payload = f2a.buildActivityInputMessage(activityId, input_);

await this.dispatchToAgentEncrypted(payload);
}

async dispatchResize(activityId: ActivityId, cols: number, rows: number) {
let tb = TerminalInputBuilder.new();
let input_ = tb.buildResize(cols, rows).toFlatbuffers();
let f2a = F2aBuilder.new();
let payload = f2a.buildActivityInputMessage(activityId, input_);
const tb = TerminalInputBuilder.new();
const input_ = tb.buildResize(cols, rows).toFlatbuffers();
const f2a = F2aBuilder.new();
const payload = f2a.buildActivityInputMessage(activityId, input_);

await this.dispatchToAgentEncrypted(payload);
}

initiateAuthentication() {
console.debug("Initiating authentication...");
let f2a = F2aBuilder.new();
const f2a = F2aBuilder.new();
this.dispatchToAgentPlain(f2a.buildAuthRequestPreamble());
}
}
2 changes: 1 addition & 1 deletion frontend/src/scripts/client/models/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Runner {

private async onWebsocketMessage(event: MessageEvent) {
const r2fRoot = readR2fRoot(new Uint8Array(event.data));
let send = new SendPayload(this);
const send = new SendPayload(this);

try {
await processR2f(r2fRoot, send);
Expand Down
53 changes: 15 additions & 38 deletions frontend/src/scripts/client/models/StoredCredential.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
const mockStorage: { [key: string]: string } = {};

global.sessionStorage = {
getItem(key: string): string | null {
return mockStorage[key] || null;
},
setItem(key: string, value: string): void {
mockStorage[key] = value;
},
removeItem(key: string): void {
delete mockStorage[key];
},
clear(): void {
Object.keys(mockStorage).forEach(key => delete mockStorage[key]);
},
key(index: number): string | null {
return Object.keys(mockStorage)[index] || null;
},
get length(): number {
return Object.keys(mockStorage).length;
}
} as Storage;

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { StoredCredential } from './StoredCredential';
import { sessionStore } from './SessionStore';

describe('StoredCredential', () => {
const testServerId = 'test-server';
const testPassword = 'test-password';
import { beforeEach, describe, expect, it } from "vitest";
import { StoredCredential } from "./StoredCredential";
import { sessionStore } from "./SessionStore";

describe("StoredCredential", () => {
const testServerId = "test-server";
const testPassword = "test-password";

beforeEach(() => {
sessionStore.clear();
});

describe('store and retrieve', () => {
it('should store and retrieve credentials correctly', async () => {
describe("store and retrieve", () => {
it("should store and retrieve credentials correctly", async () => {
const { index, secretKey } = await StoredCredential.store(testServerId, testPassword);

const credential = await StoredCredential.retrieve(index, secretKey);
Expand All @@ -43,16 +20,16 @@ describe('StoredCredential', () => {
expect(credential.serverPassword).toBe(testPassword);
});

it('should throw error when no credentials found', async () => {
await expect(StoredCredential.retrieve(999, 'invalid-key'))
.rejects.toThrow('No stored credentials for index 999');
it("should throw error when no credentials found", async () => {
await expect(StoredCredential.retrieve(999, "invalid-key")).rejects.toThrow(
"No stored credentials for index 999",
);
});

it('should throw error with invalid secret key', async () => {
it("should throw error with invalid secret key", async () => {
const { index } = await StoredCredential.store(testServerId, testPassword);

await expect(StoredCredential.retrieve(index, 'wrong-secret-key'))
.rejects.toThrow();
await expect(StoredCredential.retrieve(index, "wrong-secret-key")).rejects.toThrow();
});
});
});
4 changes: 2 additions & 2 deletions frontend/src/scripts/client/models/StoredCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Bits256Array, Bits96Array } from "../types/BitsArray.ts";

export class StoredCredential {
static async store(serverId: string, serverPassword: string): Promise<{ index: number; secretKey: string }> {
let secretKey = crypto.randomUUID();
const secretKey = crypto.randomUUID();

const encrypted = await Cryptographer.quickEncrypt({
secretKey,
Expand Down Expand Up @@ -36,7 +36,7 @@ export class StoredCredential {
ciphertext,
});

const { serverId, serverPassword } = jsonParse(decrypted);
const { serverId, serverPassword } = jsonParse(decrypted) as { serverId: string; serverPassword: string };

return new StoredCredential(serverId, serverPassword);
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/scripts/client/models/TerminalConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import { formatPtyOutput } from "../functions/formatPtyOutput.ts";
import { ensureBinary } from "../functions/ensureBinary.ts";
import type { BinaryLike } from "../types/BinaryLike.ts";

export class TerminalConnection {
private readonly terminal: Terminal;
Expand Down
Loading
Loading