switching networks works, established server functions
This commit is contained in:
38
ui/_/code/bridge/bridge.js
Normal file
38
ui/_/code/bridge/bridge.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const IS_NODE =
|
||||
typeof process !== "undefined" &&
|
||||
process.versions?.node != null
|
||||
|
||||
async function bridgeSend(name, args) {
|
||||
// Example browser implementation: send function call to server
|
||||
const res = await global.Socket.send({
|
||||
name: name,
|
||||
args: args
|
||||
})
|
||||
|
||||
const json = await res.json()
|
||||
if (!res.ok) throw new Error(json.error)
|
||||
return json.result
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps an object of functions so that:
|
||||
* - Node calls the real function
|
||||
* - Browser calls bridgeSend
|
||||
*/
|
||||
export function createBridge(funcs) {
|
||||
return new Proxy(funcs, {
|
||||
get(target, prop) {
|
||||
const orig = target[prop]
|
||||
|
||||
if (typeof orig !== "function") return orig
|
||||
|
||||
return function (...args) {
|
||||
if (IS_NODE) {
|
||||
return orig(...args)
|
||||
} else {
|
||||
return bridgeSend(prop, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
11
ui/_/code/bridge/serverFunctions.js
Normal file
11
ui/_/code/bridge/serverFunctions.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import fs from "fs"
|
||||
import { createBridge } from "./bridge.js"
|
||||
|
||||
const handlers = {
|
||||
getProfile(one, two) {
|
||||
fs.writeFileSync("output.txt", `${one} ${two}`)
|
||||
return "written to disk"
|
||||
},
|
||||
}
|
||||
|
||||
export const { getProfile } = createBridge(handlers)
|
||||
Reference in New Issue
Block a user