AsyncFileStreamer example is unclear #1780
Replies: 5 comments 5 replies
-
That example is unfinished |
Beta Was this translation helpful? Give feedback.
-
The return value of |
Beta Was this translation helpful? Give feedback.
-
If it helps here is example in JS: import { open } from 'node:fs/promises';
const streamFile = async (res, path, headers) => {
res.onAborted(() => res.aborted = true);
const fh = await open(path), size = (await fh.stat()).size;
if (res.aborted) return fh.close();
if (headers) res.cork(() => { for (const h in headers) res.writeHeader(h, headers[h]); });
for await (const chunk of fh.createReadStream()) {
if (res.aborted) break;
let ok, done;
const lastOffset = res.getWriteOffset();
res.cork(() => [ ok, done ] = res.tryEnd(chunk, size));
if (!ok) done = await new Promise(resolve => {
res.onAborted(() => resolve(true));
res.onWritable(offset => {
res.cork(() => [ ok, done ] = res.tryEnd(chunk.subarray(offset - lastOffset), size));
if (ok) resolve(done);
});
});
if (done) break;
}
return fh.close();
};
app.get('/', r => streamFile(r, path)) |
Beta Was this translation helpful? Give feedback.
-
The comment left in that code (at the time of the commit: If write was
never called, the developer should still return true so that we may drain.
Yes it would be nice if documentation were updated.
…On Tue, 3 Sept 2024, 10:21 am e3dio, ***@***.***> wrote:
// todo: I don't really know what this is supposed to mean?
The return value of onWritable is not relevant, has no effect since this
commit 5528e6f
<5528e6f>
Could probably update the docs to clarify that
—
Reply to this email directly, view it on GitHub
<#1780 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAMSASZCDOROPQZLO6J3XG3ZUUMJTAVCNFSM6AAAAABNRB2R5OVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANJSGYZTQMA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
Thanks, would much prefer a C++ example that was maintained with the
code...
The existing example doesn't even do the cork stuff?
…On Wed, 4 Sept 2024, 2:14 am e3dio, ***@***.***> wrote:
If it helps here is example in JS:
import { open } from 'node:fs/promises';
const streamFile = async (res, path, headers) => {
res.onAborted(() => res.aborted = true);
const fh = await open(path), size = (await fh.stat()).size;
if (res.aborted) return fh.close();
if (headers) res.cork(() => { for (const h in headers) res.writeHeader(h, headers[h]); });
for await (const chunk of fh.createReadStream()) {
if (res.aborted) break;
let ok, done;
const lastOffset = res.getWriteOffset();
res.cork(() => [ ok, done ] = res.tryEnd(chunk, size));
if (!ok) done = await new Promise(resolve => res.onWritable(offset => {
if (res.aborted) return resolve(true);
res.cork(() => [ ok, done ] = res.tryEnd(chunk.subarray(offset - lastOffset), size));
if (ok) resolve(done);
}));
if (done) break;
}
return fh.close();};
app.get('/', r => streamFile(r, path))
—
Reply to this email directly, view it on GitHub
<#1780 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAMSASZ6MKDKV4JFJYYURZDZUX37FAVCNFSM6AAAAABNRB2R5OVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANJTGUZTOMY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
Hi,
Getting started with uWebSockets is made more difficult because the AsyncFileStreamer.h (in particular) file seems incomplete and written by someone who doesn't know how uWebSockets works.
Example:
uWebSockets/examples/helpers/AsyncFileStreamer.h
Line 78 in 388e2b3
This in particular is in
streamFile()
, which is part of the lambda created and passed tores->onWritable()
.The documentation states that
You MUST return true for success, false for failure.
but in this case, the lambda seems to be simply scheduling a future attempt to send some more of the file in the future.There is more confusion in the example... can we see a complete example written with best practices please?
Beta Was this translation helpful? Give feedback.
All reactions