Skip to content

Commit

Permalink
Fix curl fails on browser
Browse files Browse the repository at this point in the history
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
  • Loading branch information
ktock committed Apr 9, 2024
1 parent c2d228e commit e659fd9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/wasi-browser/htdocs/worker-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 48 additions & 13 deletions extras/runcontainerjs/src/web/runcontainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,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) => {
Expand Down
2 changes: 1 addition & 1 deletion extras/runcontainerjs/src/web/wasi-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions extras/runcontainerjs/src/web/worker-util.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 };
}
Expand Down

0 comments on commit e659fd9

Please sign in to comment.