Skip to content

Commit

Permalink
feat: run Go programs in separate worker (#412)
Browse files Browse the repository at this point in the history
* feat: move Go executor into worker

* feat: refactor build dispatcher

* feat: display download progress

* feat: format duration

* feat: decouple nodejs stubs for go
  • Loading branch information
x1unix authored Sep 15, 2024
1 parent 4545bb9 commit cd059da
Show file tree
Hide file tree
Showing 21 changed files with 566 additions and 431 deletions.
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@xterm/xterm": "^5.4.0-beta.1",
"axios": "^1.7.4",
"clsx": "^1.1.1",
"comlink": "^4.4.1",
"connected-react-router": "^6.9.2",
"copy-to-clipboard": "^3.3.1",
"core-js": "^3.35.1",
Expand Down
9 changes: 1 addition & 8 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
import { Switch, Route } from 'react-router-dom'

import { configureStore, createGoConsoleAdapter, createGoLifecycleAdapter } from './store'
import { configureStore } from './store'
import { history } from '~/store/configure'
import { bootstrapGo } from '~/services/go'
import config from './services/config'
import { PlaygroundPage } from '~/components/pages/PlaygroundPage'
import { NotFoundPage } from '~/components/pages/NotFoundPage'
Expand All @@ -18,12 +17,6 @@ import './App.css'
const store = configureStore()
config.sync()

// Bootstrap Go and storage bridge
bootstrapGo(
createGoConsoleAdapter((a) => store.dispatch(a)),
createGoLifecycleAdapter((a) => store.dispatch(a)),
)

export const App = () => {
return (
<Provider store={store}>
Expand Down
7 changes: 7 additions & 0 deletions web/src/lib/go/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export interface DebugOptions {
debug?: boolean
}

export const validateResponse = async (resp: Response | PromiseLike<Response>) => {
const r: Response = resp instanceof Promise ? await resp : resp
if (r.status !== 200) {
throw new Error(`Invalid HTTP response: '${r.status} ${r.statusText}' (URL: ${r.url})`)
}
}

export const instantiateStreaming = async (resp: Response | PromiseLike<Response>, importObject) => {
const r: Response = resp instanceof Promise ? await resp : resp
if (r.status !== 200) {
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export * from './stack'
export * from './types'
export * from './memory'
export { GoWrapper, wrapGlobal } from './wrapper/wrapper'
export { instantiateStreaming } from './common'
export { instantiateStreaming, validateResponse } from './common'
export type { GoWebAssemblyInstance } from './wrapper/instance'

export * as js from './pkg/syscall/js'
File renamed without changes.
4 changes: 2 additions & 2 deletions web/src/services/go/fs.ts → web/src/lib/go/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface IFileSystem {
*/
export interface IWriter {
// write writes data and returns written bytes count
write: (data: Uint8Array) => number
write: (data: ArrayBuffer) => number
}

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ export class FileSystemWrapper {
throw enosys()
}

return writer.write(buf)
return writer.write(buf.buffer)
}

write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { enosys } from './foundation'
const PROCID_STUB = -1
const CWD_STUB = '/'

export type Process = Pick<
NodeJS.Process,
'getuid' | 'getgid' | 'geteuid' | 'getegid' | 'getgroups' | 'pid' | 'ppid' | 'umask' | 'cwd' | 'chdir'
>

/**
* Minimal NodeJS.Process implementation for wasm_exec.js
*
* Source: wasm_exec.js:87 in Go 1.17
*/
const ProcessStub = {
export const processStub: Process = {
getuid() {
return PROCID_STUB
},
Expand All @@ -35,6 +40,4 @@ const ProcessStub = {
chdir() {
throw enosys()
},
} as any

export default ProcessStub as NodeJS.Process
}
2 changes: 2 additions & 0 deletions web/src/lib/go/wrapper/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface ImportObject {
* Introduced in Go 1.21.x.
*/
_gotest: Record<string, ImportFunction>

[k: string]: any
}

export interface GoInstance {
Expand Down
59 changes: 0 additions & 59 deletions web/src/services/go/index.ts

This file was deleted.

35 changes: 0 additions & 35 deletions web/src/services/go/stdio.ts

This file was deleted.

Loading

0 comments on commit cd059da

Please sign in to comment.