Skip to content

Commit

Permalink
feat: upgrade Deno to 2023.03.22 (#130)
Browse files Browse the repository at this point in the history
Relevant changes:
- feat(core): deno_core::extension! macro to simplify extension registration
- feat(ext/url): URLSearchParams.size
- feat(serde_v8): support BigInt serialization
- fix(runtime): Extract error code for all OS error variants
- perf(core) Reduce copying and cloning in extension initialization
- perf(core) Reduce script name and script code copies
- perf(core): preserve ops between snapshots
- perf(core): use static specifier in ExtensionFileSource
- perf: disable WAL for transpiled source cache
- perf: disable runtime snapshot compression

Full changelog:
https://github.com/denoland/deno/blob/v1.32.0/Releases.md#1320--20230322

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos authored Mar 23, 2023
1 parent bb0159d commit dae941c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 94 deletions.
49 changes: 25 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repository = "https://github.com/filecoin-station/zinnia"

[workspace.dependencies]
assert_fs = "1.0.12"
deno_core = "0.175.0"
deno_core = "0.176.0"
log = "0.4.17"
pretty_assertions = "1.3.0"
env_logger = "0.10.0"
Expand Down
44 changes: 23 additions & 21 deletions ext/libp2p/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Rc;

use deno_core::anyhow::{anyhow, Context, Result};
use deno_core::error::AnyError;
use deno_core::{include_js_files, op, Extension, OpState, ZeroCopyBuf};
use deno_core::{op, OpState, ZeroCopyBuf};
use libp2p::identity::PeerId;
use libp2p::multiaddr::Protocol;
use libp2p::Multiaddr;
Expand All @@ -22,26 +22,28 @@ pub struct Options {
#[derive(Debug, Clone, Copy)]
struct DefaultNodeResourceId(deno_core::ResourceId);

pub fn init(options: Options) -> Extension {
Extension::builder(env!("CARGO_PKG_NAME"))
.esm(include_js_files!(
dir "js",
"01_peer.js",
))
.ops(vec![
op_p2p_get_peer_id::decl(),
op_p2p_request_protocol::decl(),
])
.state(move |state| {
let default_node = PeerNode::spawn(options.default_peer.clone())
// FIXME: map errors to AnyError instead of panicking
// We need to convert `Box<dyn Error + Send>` to `anyhow::Error`
.unwrap();
let rid = state.resource_table.add(default_node);
state.put::<DefaultNodeResourceId>(DefaultNodeResourceId(rid));
})
.build()
}
deno_core::extension!(
zinnia_libp2p,
ops = [
op_p2p_get_peer_id,
op_p2p_request_protocol,
],
esm = [
dir "js",
"01_peer.js",
],
options = {
default_peer: PeerNodeConfig,
},
state = |state, options| {
let default_node = PeerNode::spawn(options.default_peer)
// FIXME: map errors to AnyError instead of panicking
// We need to convert `Box<dyn Error + Send>` to `anyhow::Error`
.unwrap();
let rid = state.resource_table.add(default_node);
state.put::<DefaultNodeResourceId>(DefaultNodeResourceId(rid));
},
);

#[op]
pub fn op_p2p_get_peer_id(state: &mut OpState) -> Result<String> {
Expand Down
12 changes: 6 additions & 6 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ path = "lib.rs"
[dependencies]
atty = "0.2.14"
chrono = { version= "0.4.24", default-features = false, features = [ "clock", "std" ] }
deno_console = "0.93.0"
deno_console = "0.94.0"
deno_core.workspace = true
deno_crypto = "0.107.0"
deno_fetch = "0.117.0"
deno_url = "0.93.0"
deno_web = "0.124.0"
deno_webidl = "0.93.0"
deno_crypto = "0.108.0"
deno_fetch = "0.118.0"
deno_url = "0.94.0"
deno_web = "0.125.0"
deno_webidl = "0.94.0"
log.workspace = true
once_cell = "1.17.1"
serde.workspace = true
Expand Down
50 changes: 24 additions & 26 deletions runtime/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ use std::rc::Rc;

use deno_core::anyhow::Result;
use deno_core::url::Url;
use deno_core::{include_js_files, op, Extension, OpState};
use deno_core::{op, OpState};
use deno_fetch::FetchPermissions;
use deno_web::TimersPermission;

use crate::{LogLevel, Reporter};

pub struct Options {
pub reporter: Rc<dyn Reporter>,
}

/// Hard-coded permissions
pub struct ZinniaPermissions;

Expand All @@ -39,27 +35,29 @@ impl FetchPermissions for ZinniaPermissions {
}
}

pub fn init(options: Options) -> Extension {
Extension::builder("zinnia_runtime")
.esm(include_js_files!(
dir "js",
"06_util.js",
"90_zinnia_apis.js",
"98_global_scope.js",
"99_main.js",
))
.ops(vec![
op_job_completed::decl(),
op_info_activity::decl(),
op_error_activity::decl(),
op_zinnia_log::decl(),
])
.state(move |state| {
state.put(ZinniaPermissions {});
state.put(Rc::clone(&options.reporter));
})
.build()
}
deno_core::extension!(
zinnia_runtime,
ops = [
op_job_completed,
op_info_activity,
op_error_activity,
op_zinnia_log,
],
esm = [
dir "js",
"06_util.js",
"90_zinnia_apis.js",
"98_global_scope.js",
"99_main.js",
],
options = {
reporter: Rc<dyn Reporter>,
},
state = |state, options| {
state.put(ZinniaPermissions {});
state.put(Rc::clone(&options.reporter));
}
);

type StoredReporter = Rc<dyn Reporter>;

Expand Down
29 changes: 13 additions & 16 deletions runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use deno_web::BlobStore;

use crate::{colors, ConsoleReporter, Reporter};

use crate::ext as runtime_ext;
use runtime_ext::ZinniaPermissions;
use crate::ext::ZinniaPermissions;

use tokio::fs::File;
use tokio::io::AsyncReadExt;
Expand Down Expand Up @@ -83,23 +82,21 @@ pub async fn run_js_module(
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![
// Web Platform APIs implemented by Deno
deno_console::init_esm(),
deno_webidl::init_esm(),
deno_url::init_ops_and_esm(),
deno_web::init_ops_and_esm::<ZinniaPermissions>(
deno_console::deno_console::init_ops_and_esm(),
deno_webidl::deno_webidl::init_ops_and_esm(),
deno_url::deno_url::init_ops_and_esm(),
deno_web::deno_web::init_ops_and_esm::<ZinniaPermissions>(
blob_store,
Some(module_specifier.clone()),
),
deno_fetch::init_ops_and_esm::<ZinniaPermissions>(Default::default()),
deno_crypto::init_ops_and_esm(bootstrap_options.rng_seed),
deno_fetch::deno_fetch::init_ops_and_esm::<ZinniaPermissions>(Default::default()),
deno_crypto::deno_crypto::init_ops_and_esm(bootstrap_options.rng_seed),
// Zinnia-specific APIs
zinnia_libp2p::init(zinnia_libp2p::Options {
default_peer: zinnia_libp2p::PeerNodeConfig {
agent_version: bootstrap_options.agent_version.clone(),
..Default::default()
},
zinnia_libp2p::zinnia_libp2p::init_ops_and_esm(zinnia_libp2p::PeerNodeConfig {
agent_version: bootstrap_options.agent_version.clone(),
..Default::default()
}),
runtime_ext::init(runtime_ext::Options { reporter }),
crate::ext::zinnia_runtime::init_ops_and_esm(reporter),
],
will_snapshot: false,
inspector: false,
Expand All @@ -110,7 +107,7 @@ pub async fn run_js_module(
});

let script = format!("bootstrap.mainRuntime({})", bootstrap_options.as_json());
runtime.execute_script(&located_script_name!(), &script)?;
runtime.execute_script(located_script_name!(), script)?;

// Load and run the module
let main_module_id = runtime.load_main_module(module_specifier, None).await?;
Expand Down Expand Up @@ -179,7 +176,7 @@ impl ModuleLoader for ZinniaModuleLoader {
};

let module = ModuleSource {
code: Box::from(code.as_bytes()),
code: code.into(),
module_type: ModuleType::JavaScript,
module_url_specified: specifier.to_string(),
module_url_found: specifier.to_string(),
Expand Down

0 comments on commit dae941c

Please sign in to comment.