Skip to content

Commit

Permalink
Fix ESLint issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
nasa42 committed Dec 24, 2024
1 parent 90a036e commit d8b0e45
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 69 deletions.
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
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
12 changes: 6 additions & 6 deletions frontend/src/scripts/client/pipeline/processA2f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export const processA2f = async (agentRoot: A2fRoot, send: SendPayload) => {
const processPlain = async (payload: A2fRoot, send: SendPayload) => {
switch (payload.plainMessageType()) {
case A2fPlainMessage.AuthPreamble:
let preamble = payload.plainMessage(new A2fPlainAuthPreamble()) as A2fPlainAuthPreamble | null;
const preamble = payload.plainMessage(new A2fPlainAuthPreamble()) as A2fPlainAuthPreamble | null;
const salt = preamble?.salt();
if (!preamble || !preamble.pbkdf2Iterations() || !salt) {
alert("Invalid preamble");
return;
}
console.info("received preamble");
let preambleResp = F2aBuilder.new();
const preambleResp = F2aBuilder.new();
await send.runner.initCryptographer({
iterations: preamble.pbkdf2Iterations(),
salt: Bits256Array.fromFbBits256(salt),
Expand All @@ -46,11 +46,11 @@ const processPlain = async (payload: A2fRoot, send: SendPayload) => {
send.toAgentPlain = preambleResp.buildAuthRequestVerification(iv, ciphertext, 0n);
return;
case A2fPlainMessage.AuthResult:
let result = payload.plainMessage(new A2fPlainAuthResult()) as A2fPlainAuthResult | null;
const result = payload.plainMessage(new A2fPlainAuthResult()) as A2fPlainAuthResult | null;
if (!result) return;
console.info(`received auth result: ${result.successAuth()}`);
activeNotification.clear();
let resp = F2aBuilder.new();
const resp = F2aBuilder.new();
send.toAgentEncrypted = resp.buildActivityCreateTerminal();
return;
default:
Expand All @@ -71,15 +71,15 @@ const processEncrypted = async (agentRoot: A2fRoot, send: SendPayload) => {

const compressed = agentRoot.format() === A2fMessageFormat.Aes256GcmDeflateRaw;

let plaintext = await send.runner.cryptographer().decrypt(ciphertext, Bits96Array.fromFbBits96(iv), compressed);
const plaintext = await send.runner.cryptographer().decrypt(ciphertext, Bits96Array.fromFbBits96(iv), compressed);

if (!plaintext) {
// TODO: Return an error to agent
console.error("No decrypted payload");
return;
}

let message = readA2fEncryptedRoot(plaintext);
const message = readA2fEncryptedRoot(plaintext);

switch (message.messageType()) {
case A2fMessage.ActivityOutput:
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/scripts/client/pipeline/processR2f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { readA2fRoot } from "../parsers/readA2fRoot.ts";
export const processR2f = async (relayRoot: R2fRoot, send: SendPayload) => {
switch (relayRoot.rootPayloadType()) {
case R2fRootPayload.FromAgent:
let fromAgent = relayRoot.rootPayload(new R2fFromAgent()) as R2fFromAgent | null;
let data = fromAgent?.payloadArray();
const fromAgent = relayRoot.rootPayload(new R2fFromAgent()) as R2fFromAgent | null;
const data = fromAgent?.payloadArray();
if (!data) return;
let payload = readA2fRoot(data);
const payload = readA2fRoot(data);
await processA2f(payload, send);
return;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { VectorTable } from "../../../generated/flatbuffers_schema/talk_v1/vecto
export const processTerminalOutput = (root: PtyOutputRoot, send: SendPayload) => {
switch (root.payloadType()) {
case PtyOutput.Output:
let payload = root.payload(new VectorTable()) as VectorTable | null;
const payload = root.payload(new VectorTable()) as VectorTable | null;
if (!payload) return;
send.toTerminal = payload.dataArray();
return;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/scripts/client/serialisers/F2aBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { VERSION } from "../config.ts";
import { Cryptographer } from "../cryptography/Cryptographer.ts";
import { type Bits96Array } from "../types/BitsArray.ts";

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface BuilderState {}

class Initial implements BuilderState {
Expand Down Expand Up @@ -102,7 +103,7 @@ export class F2aBuilder<State extends BuilderState> {

const builder = new flatbuffers.Builder();

let encryptedPayloadOffset = builder.createByteVector(ciphertext);
const encryptedPayloadOffset = builder.createByteVector(ciphertext);
F2aRoot.startF2aRoot(builder);
F2aRoot.addFormat(builder, format);
F2aRoot.addIv(builder, iv.toFbBits96(builder));
Expand Down
1 change: 1 addition & 0 deletions frontend/src/scripts/client/serialisers/F2rBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../../generated/flatbuffers_schema/talk_v1/talk_v1.ts";
import { type F2aRootBlob, F2rRootBlob } from "../types/BinaryBlob.ts";

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface BuilderState {}

class Initial implements BuilderState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { VectorTable } from "../../../generated/flatbuffers_schema/talk_v1/talk_
import { ActivityInputBlob } from "../types/BinaryBlob.ts";
import { PtyInput, PtyInputRoot, PtyResize } from "../../../generated/flatbuffers_schema/talk_v1/activity.ts";

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface BuilderState {}

class Initial implements BuilderState {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scripts/client/types/BinaryLike.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type BinaryLike = string | Uint8Array;
export type BinaryLike = string | Uint8Array;
Loading

0 comments on commit d8b0e45

Please sign in to comment.