Skip to content

Commit

Permalink
Merge pull request #20 from ktock/c2w-next
Browse files Browse the repository at this point in the history
Add APIs used by imagemounter
  • Loading branch information
ktock authored Mar 12, 2024
2 parents 6fc127f + 8b39185 commit a44e09a
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/web/net/stack-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,73 @@ function wasiHack(wasi, certfd, connfd) {

function envHack(wasi){
return {
decompress_init: function(idP) {
if (idP < 0) {
idP = idP >>> 0;
}
var buffer = new DataView(wasi.inst.exports.memory.buffer);
var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer);

streamCtrl[0] = 0;
postMessage({
type: "decompress_init",
});
Atomics.wait(streamCtrl, 0, 0);
if (streamStatus[0] < 0) {
return ERRNO_INVAL;
}
var id = streamStatus[0];
buffer.setUint32(idP, id, true);
return 0;
},
decompress_write: function(id, bufP, buflen, isEOF) {
if (bufP < 0) {
bufP = bufP >>> 0;
}
var buf = new Uint8Array(wasi.inst.exports.memory.buffer, bufP, buflen);
streamCtrl[0] = 0;
postMessage({
type: "decompress_write",
id: id,
chunk: buf.slice(0, buf.length),
isEOF: isEOF,
});
Atomics.wait(streamCtrl, 0, 0);
if (streamStatus[0] < 0) {
return ERRNO_INVAL;
}
return 0;
},
decompress_read: function(id, bufP, buflen, recvLenP, isEOFP) {
if (bufP < 0) {
bufP = bufP >>> 0;
}
if (recvLenP < 0) {
recvLenP = recvLenP >>> 0;
}
if (isEOFP < 0) {
isEOFP = isEOFP >>> 0;
}
var buffer = new DataView(wasi.inst.exports.memory.buffer);
var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer);

streamCtrl[0] = 0;
postMessage({type: "decompress_read", id: id, len: buflen});
Atomics.wait(streamCtrl, 0, 0);
if (streamStatus[0] < 0) {
return ERRNO_INVAL;
}
var ddlen = streamLen[0];
var res = streamData.subarray(0, ddlen);
buffer8.set(res, bufP);
buffer.setUint32(recvLenP, ddlen, true);
if (streamStatus[0] == 1) {
buffer.setUint32(isEOFP, 1, true);
} else {
buffer.setUint32(isEOFP, 0, true);
}
return 0;
},
http_send: function(addressP, addresslen, reqP, reqlen, idP){
var buffer = new DataView(wasi.inst.exports.memory.buffer);
var address = new Uint8Array(wasi.inst.exports.memory.buffer, addressP, addresslen);
Expand Down
104 changes: 103 additions & 1 deletion src/web/net/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,13 @@ function connect(name, shared, toNet) {
streamStatus[0] = reqID;
break;
case "http_writebody":
httpConnections[req_.id].reqBodybuf = appendData(httpConnections[req_.id].reqBodybuf, req_.body)
var s;
if (httpConnections[req_.id] == undefined) {
s = newUint8Array(0);
} else {
s = httpConnections[req_.id].reqBodybuf;
}
httpConnections[req_.id].reqBodybuf = appendData(s, req_.body)
httpConnections[req_.id].reqBodyEOF = req_.isEOF;
streamStatus[0] = 0;
if (req_.isEOF && !httpConnections[req_.id].requestSent) {
Expand Down Expand Up @@ -567,6 +573,99 @@ function connect(name, shared, toNet) {
serveDataOffset(httpConnections[req_.id].respBodybuf, req_.offset, req_.len);
streamStatus[0] = 0;
break;

case "decompress_init":
var ds = new DecompressionStream("gzip");
var r = ds.readable.getReader();
var w = ds.writable.getWriter();
decompressors[decompressID] = {
reader: r,
writer: w,
writerClosed: false,
closed: false,
reading: false,
resbuf: new Uint8Array(0),
done: false,
error: null,
}
var decompressIDA = decompressID;
r.closed.then(() => {
decompressors[decompressIDA].closed = true;
}).catch((e) => {
console.log("error on closing:", e);
})
streamStatus[0] = decompressIDA;
decompressID++;
break;
case "decompress_write":
if (decompressors[req_.id] == undefined) {
console.log("unknown id", req_.id);
} else {
if (req_.chunk.byteLength > 0) {
decompressors[req_.id].writer.write(req_.chunk);
}
if ((req_.isEOF == 1) && (!decompressors[req_.id].writerClosed)) {
try {
decompressors[req_.id].writer.ready.then(() => {
decompressors[req_.id].writer.close();
decompressors[req_.id].writerClosed = true;
});
} catch(e) {
console.log("error decompress_write:", e);
streamStatus[0] = -1;
}
}
}
break;
case "decompress_read":
if (decompressors[req_.id] == undefined) {
console.log("unknown id", req_.id);
} else {
var readnext = true;
if (decompressors[req_.id].error != null) {
streamStatus[0] = -1;
readnext = false;
} else {
decompressors[req_.id].resbuf = serveData(decompressors[req_.id].resbuf, req_.len);
streamStatus[0] = 0;
if (decompressors[req_.id].done) {
readnext = false;
if (decompressors[req_.id].resbuf.byteLength == 0) {
streamStatus[0] = 1; // isEOF
delete decompressors[req_.id]; // decompression done
}
}
}
if (readnext) {
var id = req_.id;
if (!decompressors[id].reading) {
decompressors[id].reader.read().then((o) => {
var rb;
if ((decompressors[id] == undefined) || (decompressors[id].resbuf == undefined)) {
rb = new Uint8Array(0);
} else {
rb = decompressors[id].resbuf;
}
var ov;
if ((o == undefined) || (o.value == undefined)) {
ov = new Uint8Array(0);
} else {
ov = o.value;
}
decompressors[id].resbuf = appendData(rb, ov);
decompressors[id].reading = false;
if ((decompressors[id].closed) && (o.done)) {
decompressors[id].done = true;
}
}).catch((e) => {
console.log("error decompress_read:", e);
decompressors[id].error = e;
})
decompressors[id].reading = true;
}
}
}
break;
default:
console.log(name + ":" + "unknown request: " + req_.type)
return;
Expand All @@ -579,6 +678,9 @@ function connect(name, shared, toNet) {
}
}

var decompressID = 0;
var decompressors = {};

function appendData(data1, data2) {
let buf2 = new Uint8Array(data1.byteLength + data2.byteLength);
buf2.set(new Uint8Array(data1), 0);
Expand Down

0 comments on commit a44e09a

Please sign in to comment.