diff --git a/examples/wasi-browser/htdocs/worker-util.js b/examples/wasi-browser/htdocs/worker-util.js index 9fddd28a..59f86454 100644 --- a/examples/wasi-browser/htdocs/worker-util.js +++ b/examples/wasi-browser/htdocs/worker-util.js @@ -184,7 +184,7 @@ function wasiHackSocket(wasi, listenfd, connfd) { } var _fd_prestat_get = wasi.wasiImport.fd_prestat_get; wasi.wasiImport.fd_prestat_get = (fd, prestat_ptr) => { - if ((fd == listenfd) || (fd <= connfd)){ // reserve socket-related fds + if ((fd == listenfd) || (fd == connfd)){ // reserve socket-related fds let buffer = new DataView(wasi.inst.exports.memory.buffer); buffer.setUint8(prestat_ptr, 1); return 0; diff --git a/extras/runcontainerjs/src/web/runcontainer.js b/extras/runcontainerjs/src/web/runcontainer.js index fc9acab3..79ab7469 100644 --- a/extras/runcontainerjs/src/web/runcontainer.js +++ b/extras/runcontainerjs/src/web/runcontainer.js @@ -132,9 +132,6 @@ function connect(name, shared, toNet) { if (timeoutHandler) { clearTimeout(timeoutHandler); timeoutHandler = null; - } else { - Atomics.store(streamCtrl, 0, 1); - Atomics.notify(streamCtrl, 0); } break; case "recv-is-readable": @@ -166,17 +163,14 @@ function connect(name, shared, toNet) { Atomics.notify(toNetNotify, 0); clearTimeout(timeoutHandler); timeoutHandler = null; - // streamStatus[0] = 0; - Atomics.store(streamCtrl, 0, 1); - Atomics.notify(streamCtrl, 0); }, 0.01); } pollBuf(); - return; + } else { + streamStatus[0] = 0; // timeout + Atomics.store(toNetNotify, 0, -1); + Atomics.notify(toNetNotify, 0); } - streamStatus[0] = 0; // timeout - Atomics.store(toNetNotify, 0, -1); - Atomics.notify(toNetNotify, 0); } break; case "http_send": @@ -223,31 +217,66 @@ function connect(name, shared, toNet) { connObj.request.body = connObj.reqBodybuf; } fetch(connObj.address, connObj.request).then((resp) => { - var headers = {}; - for (const key of resp.headers.keys()) { - headers[key] = resp.headers.get(key); - } - connObj.response = new TextEncoder().encode(JSON.stringify({ - bodyUsed: resp.bodyUsed, - headers: headers, - redirected: resp.redirected, - status: resp.status, - statusText: resp.statusText, - type: resp.type, - url: resp.url - })), connObj.done = false; connObj.respBodybuf = new Uint8Array(0); if (resp.ok) { resp.arrayBuffer().then((data) => { + var headers = {}; + for (const key of resp.headers.keys()) { + if (data.byteLength > 0) { + if (key == "content-encoding") { + continue; + } + if (key == "content-length") { + headers[key] = data.byteLength.toString(); + continue; + } + } + headers[key] = resp.headers.get(key); + } + connObj.response = new TextEncoder().encode(JSON.stringify({ + bodyUsed: resp.bodyUsed, + headers: headers, + redirected: resp.redirected, + status: resp.status, + statusText: resp.statusText, + type: resp.type, + url: resp.url + })); connObj.respBodybuf = new Uint8Array(data); connObj.done = true; }).catch((error) => { + var headers = {}; + for (const key of resp.headers.keys()) { + headers[key] = resp.headers.get(key); + } + connObj.response = new TextEncoder().encode(JSON.stringify({ + bodyUsed: resp.bodyUsed, + headers: headers, + redirected: resp.redirected, + status: resp.status, + statusText: resp.statusText, + type: resp.type, + url: resp.url + })); connObj.respBodybuf = new Uint8Array(0); connObj.done = true; console.log("failed to fetch body: " + error); }); } else { + var headers = {}; + for (const key of resp.headers.keys()) { + headers[key] = resp.headers.get(key); + } + connObj.response = new TextEncoder().encode(JSON.stringify({ + bodyUsed: resp.bodyUsed, + headers: headers, + redirected: resp.redirected, + status: resp.status, + statusText: resp.statusText, + type: resp.type, + url: resp.url + })); connObj.done = true; } }).catch((error) => { diff --git a/extras/runcontainerjs/src/web/stack-worker.js b/extras/runcontainerjs/src/web/stack-worker.js index 88ebcaf3..490a3692 100644 --- a/extras/runcontainerjs/src/web/stack-worker.js +++ b/extras/runcontainerjs/src/web/stack-worker.js @@ -592,9 +592,11 @@ function sockWaitForReadable(timeout){ streamCtrl[0] = 0; Atomics.store(toNetNotify, 0, 0); postMessage({type: "recv-is-readable", timeout: timeout}); + Atomics.wait(streamCtrl, 0, 0); Atomics.wait(toNetNotify, 0, 0); var res = Atomics.load(toNetNotify, 0); + streamCtrl[0] = 0; postMessage({type: "recv-is-readable-cancel"}); Atomics.wait(streamCtrl, 0, 0); diff --git a/extras/runcontainerjs/src/web/wasi-util.js b/extras/runcontainerjs/src/web/wasi-util.js index cbf83c0e..28597617 100644 --- a/extras/runcontainerjs/src/web/wasi-util.js +++ b/extras/runcontainerjs/src/web/wasi-util.js @@ -168,7 +168,7 @@ export function wasiHackSocket(wasi, listenfd, connfd, sockAccept, sockSend, soc } var _fd_prestat_get = wasi.wasiImport.fd_prestat_get; wasi.wasiImport.fd_prestat_get = (fd, prestat_ptr) => { - if ((fd == listenfd) || (fd <= connfd)){ // reserve socket-related fds + if ((fd == listenfd) || (fd == connfd)){ // reserve socket-related fds let buffer = new DataView(wasi.inst.exports.memory.buffer); buffer.setUint8(prestat_ptr, 1); return 0; diff --git a/extras/runcontainerjs/src/web/worker-util.js b/extras/runcontainerjs/src/web/worker-util.js index 6dbad8f3..0e8e7d93 100644 --- a/extras/runcontainerjs/src/web/worker-util.js +++ b/extras/runcontainerjs/src/web/worker-util.js @@ -1,5 +1,5 @@ import { Event, EventType, Subscription, SubscriptionClock, SubscriptionFdReadWrite, SubscriptionU, wasiHackSocket } from './wasi-util'; -import { WASI, PreopenDirectory } from "@bjorn3/browser_wasi_shim"; +import { WASI, PreopenDirectory, File } from "@bjorn3/browser_wasi_shim"; import * as wasitype from "@bjorn3/browser_wasi_shim"; export function startContainer(info, cargs, ttyClient) { @@ -423,12 +423,12 @@ function getCertDir(cert) { var o = ret.fd_obj; ret.fd_obj.fd_pread = (view8, iovs, offset) => { var old_offset = o.file_pos; - var r = o.fd_seek(offset, WHENCE_SET); + var r = o.fd_seek(offset, wasitype.wasi.WHENCE_SET); if (r.ret != 0) { return { ret: -1, nread: 0 }; } var read_ret = o.fd_read(view8, iovs); - r = o.fd_seek(old_offset, WHENCE_SET); + r = o.fd_seek(old_offset, wasitype.wasi.WHENCE_SET); if (r.ret != 0) { return { ret: -1, nread: 0 }; }