Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add APIs used by imagemounter #20

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading