Skip to content

Commit

Permalink
Reintroduce padlock module
Browse files Browse the repository at this point in the history
  • Loading branch information
Zekiah-A committed Mar 3, 2024
1 parent ddea174 commit ed3f81a
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions padlock/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,22 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ServerWebSocket } from "bun"
import * as path from "path"
import { PublicPromise } from "../server-types.ts"
import { fileURLToPath } from "url"
import { solveWorker, solveReqs, makeSolveRequest } from "./solveWorker.js"

const __dirname = path.dirname(fileURLToPath(import.meta.url))
export const __dirname = path.dirname(fileURLToPath(import.meta.url))

// Pending serverside solutions
const pendingServerSolutions = new Map<ServerWebSocket<any>, number>()

// Challenge is global for now until we have more challenges we can dynamically switch per client
let sourcesData:string|null = null
let wasmPath = ""
export let wasmPath = ""
export async function useAlgo(name: string) {
wasmPath = path.join(__dirname, name, name + ".wasm")
const sourcesFile = Bun.file(path.join(__dirname, name, name + ".min.js"))
sourcesData = await sourcesFile.text()
}
await useAlgo("algo-1")

const solveReqs = new Map<number, PublicPromise<bigint>>()
const solveWorker = new Worker(path.join(__dirname, "solve-worker.ts"))
let solveReqId = 0

function makeSolveRequest(data:any) {
const handle = solveReqId++
const postMessage = { handle: handle, data: data, solver: wasmPath }
const promise = new PublicPromise<bigint>()
solveReqs.set(handle, promise)
solveWorker.postMessage(postMessage)
return handle
}
solveWorker.onmessage = ({data}) => {
solveReqs.get(data.handle)?.resolve(data.solution)
}
Expand Down

0 comments on commit ed3f81a

Please sign in to comment.