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

Runner #26

Merged
merged 10 commits into from
May 27, 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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"type": "module",
"dependencies": {
"@blockly/field-grid-dropdown": "^4.0.11",
"@webcontainer/api": "^1.1.9",
"blockly": "^10.4.3",
"dexie": "^4.0.1"
}
Expand Down
80 changes: 80 additions & 0 deletions src/lib/utils/Runner/Common/WebSocketManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
export enum WSOPCodes {
CommitFile = 0,
TerminalUpdate = 1,
InstallationComplete = 2,
BotStarted = 3
}

export interface WebSocketData<T> {
op: WSOPCodes;
d: T;
}

export const WEBSOCKET_ERRORS = {
NotConnected: "WEBSOCKET ERROR: NOT CONNECTED"
}

export interface WebSocketInitOptions {
onMessage?: <T>(eventName: WSOPCodes, data: T) => void
}

export class WebSocketManager {
ws: WebSocket
// eslint-disable-next-line
files: Record<string, any> = {}
terminal = ""

constructor(wsAddress: string, options: WebSocketInitOptions) {
this.ws = new WebSocket(wsAddress)

this.ws.addEventListener("message", ({ data }) => {
const trueData = JSON.parse(data.toString()) as WebSocketData<unknown>

if(trueData.op === WSOPCodes.TerminalUpdate) {
this.terminal += (trueData as WebSocketData<string>).d
return
}

if(options.onMessage) options.onMessage<unknown>(trueData.op, (trueData as WebSocketData<unknown>).d)
})
}

pushFile(name: string, content: string) {
if(!name.includes("/")) {
this.files[name] = content
return
}

const fileSystemRoutes = name.split("/")

let obj = this.files

fileSystemRoutes.forEach((val, i) => {
if(!this.#isDirectory(fileSystemRoutes, i)) {
obj[val] = content

return
}

if(!obj[val]) obj[val] = {}

obj = obj[val]
})
}

sendFiles(appId: string) {
if(this.ws.readyState !== 1) throw new Error(WEBSOCKET_ERRORS.NotConnected)

this.ws.send(JSON.stringify({
op: WSOPCodes.CommitFile,
d: {
app: appId,
files: this.files
}
}))
}

#isDirectory(array: string[], index: number) {
return index !== array.length - 1
}
}
54 changes: 0 additions & 54 deletions src/lib/utils/Runner/ContainerBuilder.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/lib/utils/Runner/Local/LocalWS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { WebSocketManager, type WebSocketInitOptions } from "../Common/WebSocketManager";

// TODO: IMPLEMENT MORE FUNCTIONS FOR LOCAL WS

export class LocalWSManager extends WebSocketManager {
constructor(wsAddress: string, options: WebSocketInitOptions = {}) {
super(wsAddress, options)
}
}
57 changes: 0 additions & 57 deletions src/lib/utils/Runner/NodeFileSystem.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/lib/utils/Runner/Remote/RemoteWS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { WebSocketManager, type WebSocketInitOptions } from "../Common/WebSocketManager";

// TODO: IMPLEMENT MORE FUNCTIONS FOR REMOTE WS

export class RemoteWSManager extends WebSocketManager {
constructor(wsAddress: string, options: WebSocketInitOptions = {}) {
super(wsAddress, options)
}
}
2 changes: 2 additions & 0 deletions src/lib/utils/ToolboxGen/Toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default class Toolbox {
fields: {}
}
};
// @ts-expect-error Accessing unknown type
inputs[data.argName]["block"]["fields"][key] = value;
}
}
Expand Down Expand Up @@ -115,6 +116,7 @@ export default class Toolbox {

await this.setStruct(
path,
// @ts-expect-error Unknown type
(contents[contents.length - 1] as unknown[])["contents"],
filesInDir,
topPath
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,6 @@
loupe "^2.3.7"
pretty-format "^29.7.0"

"@webcontainer/api@^1.1.9":
version "1.1.9"
resolved "https://registry.npmjs.org/@webcontainer/api/-/api-1.1.9.tgz#b546bc71f06740975d15c8dbf9b8287f51f92a9b"
integrity sha512-Sp6PV0K9D/3f8fSbCubqhfmBFH8XbngZCBOCF+aExyGqnz2etmw+KYvbQ/JxYvYX5KPaSxM+asFQwoP2RHl5cg==

abab@^2.0.6:
version "2.0.6"
resolved "https://registry.npmjs.org/abab/-/abab-2.0.6.tgz#41b80f2c871d19686216b82309231cfd3cb3d291"
Expand Down
Loading