diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-03-17 12:22:15 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-17 18:22:15 +0000 |
commit | e55b448730160a6e4df9815a268d4049ac89deab (patch) | |
tree | 35d80fd60f2f1d1d06903caff256484a7d703d76 /ext/http/lib.rs | |
parent | 0bc6bf5d33b8198253954d7f04558270de45c925 (diff) |
feat(core) deno_core::extension! macro to simplify extension registration (#18210)
This implements two macros to simplify extension registration and centralize a lot of the boilerplate as a base for future improvements:
* `deno_core::ops!` registers a block of `#[op]`s, optionally with type
parameters, useful for places where we share lists of ops
* `deno_core::extension!` is used to register an extension, and creates
two methods that can be used at runtime/snapshot generation time:
`init_ops` and `init_ops_and_esm`.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/http/lib.rs')
-rw-r--r-- | ext/http/lib.rs | 47 |
1 files changed, 15 insertions, 32 deletions
diff --git a/ext/http/lib.rs b/ext/http/lib.rs index 11bd91aa4..20436a82d 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -19,7 +19,6 @@ use deno_core::futures::stream::Peekable; use deno_core::futures::FutureExt; use deno_core::futures::StreamExt; use deno_core::futures::TryFutureExt; -use deno_core::include_js_files; use deno_core::op; use deno_core::AsyncRefCell; use deno_core::AsyncResult; @@ -28,8 +27,6 @@ use deno_core::ByteString; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::CancelTryFuture; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -78,35 +75,21 @@ use crate::reader_stream::ShutdownHandle; pub mod compressible; mod reader_stream; -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps( - env!("CARGO_PKG_NAME"), - &["deno_web", "deno_net", "deno_fetch", "deno_websocket"], - ) -} - -fn ops(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder { - ext.ops(vec![ - op_http_accept::decl(), - op_http_write_headers::decl(), - op_http_headers::decl(), - op_http_write::decl(), - op_http_write_resource::decl(), - op_http_shutdown::decl(), - op_http_websocket_accept_header::decl(), - op_http_upgrade_websocket::decl(), - ]) -} - -pub fn init_ops_and_esm() -> Extension { - ops(&mut ext()) - .esm(include_js_files!("01_http.js",)) - .build() -} - -pub fn init_ops() -> Extension { - ops(&mut ext()).build() -} +deno_core::extension!( + deno_http, + deps = [deno_web, deno_net, deno_fetch, deno_websocket], + ops = [ + op_http_accept, + op_http_write_headers, + op_http_headers, + op_http_write, + op_http_write_resource, + op_http_shutdown, + op_http_websocket_accept_header, + op_http_upgrade_websocket, + ], + esm = ["01_http.js"], +); pub enum HttpSocketAddr { IpSocket(std::net::SocketAddr), |