Skip to content

Commit

Permalink
Merge branch 'master' into luda-editor/audio-s3-key
Browse files Browse the repository at this point in the history
  • Loading branch information
namse authored Sep 24, 2024
2 parents 6c9ef0e + 4dd7d39 commit 2cbbe79
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 54 deletions.
36 changes: 33 additions & 3 deletions namui/namui-cli/webCode/src/httpFetch/yesStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ export class YesStreamHttpFetchHandle implements HttpFetchHandle {

(async () => {
try {
const requestBody = ["GET", "HEAD", "OPTIONS"].includes(
request.method,
)
? undefined
: request.url.startsWith("http://")
? await fullRead(readable)
: readable;

const response = await fetch(request, {
body: ["GET", "HEAD"].includes(request.method)
? undefined
: readable,
body: requestBody,
signal: abortController.signal,
// @ts-ignore
duplex: "half",
Expand Down Expand Up @@ -170,6 +176,8 @@ export class YesStreamHttpFetchHandle implements HttpFetchHandle {

if (requestBody.isOver) {
writer.close();
} else {
writer.releaseLock();
}

requestBody.isSomeoneWriting = false;
Expand Down Expand Up @@ -217,3 +225,25 @@ export class YesStreamHttpFetchHandle implements HttpFetchHandle {
this.cleanUpFetch(fetchId);
}
}
async function fullRead(
readable: ReadableStream<ArrayBuffer>,
): Promise<Uint8Array> {
const reader = readable.getReader();
const chunks: Uint8Array[] = [];
let totalLength = 0;
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
chunks.push(new Uint8Array(value));
totalLength += value.byteLength;
}
const result = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
result.set(chunk, offset);
offset += chunk.length;
}
return result;
}
Loading

0 comments on commit 2cbbe79

Please sign in to comment.