diff options
| author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-11-09 19:26:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-09 19:26:17 +0100 |
| commit | 375ce63c6390cf7710210ce22f14a2b5a02cbfc3 (patch) | |
| tree | 85100876e5e0b50514385ae3c7ce08493c82b38b /ext/fetch | |
| parent | 1eae6c139ee1dac28df57d67d993792b773fa1ff (diff) | |
feat(core): streams (#12596)
This allows resources to be "streams" by implementing read/write/shutdown. These streams are implicit since their nature (read/write/duplex) isn't known until called, but we could easily add another method to explicitly tag resources as streams.
`op_read/op_write/op_shutdown` are now builtin ops provided by `deno_core`
Note: this current implementation is simple & straightforward but it results in an additional alloc per read/write call
Closes #12556
Diffstat (limited to 'ext/fetch')
| -rw-r--r-- | ext/fetch/26_fetch.js | 23 | ||||
| -rw-r--r-- | ext/fetch/lib.rs | 62 |
2 files changed, 27 insertions, 58 deletions
diff --git a/ext/fetch/26_fetch.js b/ext/fetch/26_fetch.js index 22baaf5c2..f15e7f6b9 100644 --- a/ext/fetch/26_fetch.js +++ b/ext/fetch/26_fetch.js @@ -73,24 +73,6 @@ return core.opAsync("op_fetch_send", rid); } - /** - * @param {number} rid - * @param {Uint8Array} body - * @returns {Promise<void>} - */ - function opFetchRequestWrite(rid, body) { - return core.opAsync("op_fetch_request_write", rid, body); - } - - /** - * @param {number} rid - * @param {Uint8Array} body - * @returns {Promise<number>} - */ - function opFetchResponseRead(rid, body) { - return core.opAsync("op_fetch_response_read", rid, body); - } - // A finalization registry to clean up underlying fetch resources that are GC'ed. const RESOURCE_REGISTRY = new FinalizationRegistry((rid) => { core.tryClose(rid); @@ -120,7 +102,8 @@ // This is the largest possible size for a single packet on a TLS // stream. const chunk = new Uint8Array(16 * 1024 + 256); - const read = await opFetchResponseRead( + // TODO(@AaronO): switch to handle nulls if that's moved to core + const read = await core.read( responseBodyRid, chunk, ); @@ -260,7 +243,7 @@ } try { await PromisePrototypeCatch( - opFetchRequestWrite(requestBodyRid, value), + core.write(requestBodyRid, value), (err) => { if (terminator.aborted) return; throw err; diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 4bd62cd7c..5bae92c8e 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -13,6 +13,7 @@ use deno_core::op_async; use deno_core::op_sync; use deno_core::url::Url; use deno_core::AsyncRefCell; +use deno_core::AsyncResult; use deno_core::ByteString; use deno_core::CancelFuture; use deno_core::CancelHandle; @@ -84,8 +85,6 @@ where .ops(vec