summaryrefslogtreecommitdiff
path: root/ext/flash/lib.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-03-17 12:22:15 -0600
committerGitHub <noreply@github.com>2023-03-17 18:22:15 +0000
commite55b448730160a6e4df9815a268d4049ac89deab (patch)
tree35d80fd60f2f1d1d06903caff256484a7d703d76 /ext/flash/lib.rs
parent0bc6bf5d33b8198253954d7f04558270de45c925 (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/flash/lib.rs')
-rw-r--r--ext/flash/lib.rs111
1 files changed, 46 insertions, 65 deletions
diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs
index 77b8b6791..6f11e14af 100644
--- a/ext/flash/lib.rs
+++ b/ext/flash/lib.rs
@@ -16,8 +16,6 @@ use deno_core::v8::fast_api;
use deno_core::ByteString;
use deno_core::CancelFuture;
use deno_core::CancelHandle;
-use deno_core::Extension;
-use deno_core::ExtensionBuilder;
use deno_core::OpState;
use deno_core::StringOrBuffer;
use deno_core::ZeroCopyBuf;
@@ -1527,66 +1525,49 @@ pub trait FlashPermissions {
) -> Result<(), AnyError>;
}
-fn ext() -> ExtensionBuilder {
- Extension::builder_with_deps(
- env!("CARGO_PKG_NAME"),
- &[
- "deno_web",
- "deno_net",
- "deno_fetch",
- "deno_websocket",
- "deno_http",
- ],
- )
-}
-
-fn ops<P: FlashPermissions + 'static>(
- ext: &mut ExtensionBuilder,
- unstable: bool,
-) -> &mut ExtensionBuilder {
- ext
- .ops(vec![
- op_flash_serve::decl::<P>(),
- op_node_unstable_flash_serve::decl::<P>(),
- op_flash_respond::decl(),
- op_flash_respond_async::decl(),
- op_flash_respond_chunked::decl(),
- op_flash_method::decl(),
- op_flash_path::decl(),
- op_flash_headers::decl(),
- op_flash_addr::decl(),
- op_flash_next::decl(),
- op_flash_next_server::decl(),
- op_flash_next_async::decl(),
- op_flash_read_body::decl(),
- op_flash_upgrade_websocket::decl(),
- op_flash_drive_server::decl(),
- op_flash_wait_for_listening::decl(),
- op_flash_first_packet::decl(),
- op_flash_has_body_stream::decl(),
- op_flash_close_server::decl(),
- op_flash_make_request::decl(),
- op_flash_write_resource::decl(),
- op_try_flash_respond_chunked::decl(),
- ])
- .state(move |op_state| {
- op_state.put(Unstable(unstable));
- op_state.put(FlashContext {
- next_server_id: 0,
- join_handles: HashMap::default(),
- servers: HashMap::default(),
- });
- })
-}
-
-pub fn init_ops_and_esm<P: FlashPermissions + 'static>(
- unstable: bool,
-) -> Extension {
- ops::<P>(&mut ext(), unstable)
- .esm(deno_core::include_js_files!("01_http.js",))
- .build()
-}
-
-pub fn init_ops<P: FlashPermissions + 'static>(unstable: bool) -> Extension {
- ops::<P>(&mut ext(), unstable).build()
-}
+deno_core::extension!(deno_flash,
+ deps = [
+ deno_web,
+ deno_net,
+ deno_fetch,
+ deno_websocket,
+ deno_http
+ ],
+ parameters = [P: FlashPermissions],
+ ops = [
+ op_flash_serve<P>,
+ op_node_unstable_flash_serve<P>,
+ op_flash_respond,
+ op_flash_respond_async,
+ op_flash_respond_chunked,
+ op_flash_method,
+ op_flash_path,
+ op_flash_headers,
+ op_flash_addr,
+ op_flash_next,
+ op_flash_next_server,
+ op_flash_next_async,
+ op_flash_read_body,
+ op_flash_upgrade_websocket,
+ op_flash_drive_server,
+ op_flash_wait_for_listening,
+ op_flash_first_packet,
+ op_flash_has_body_stream,
+ op_flash_close_server,
+ op_flash_make_request,
+ op_flash_write_resource,
+ op_try_flash_respond_chunked,
+ ],
+ esm = [ "01_http.js" ],
+ config = {
+ unstable: bool,
+ },
+ state = |state, unstable| {
+ state.put(Unstable(unstable));
+ state.put(FlashContext {
+ next_server_id: 0,
+ join_handles: HashMap::default(),
+ servers: HashMap::default(),
+ });
+ },
+);