Skip to content

Commit

Permalink
feat: upgrade Deno to v1.38.2 / 2023.11.17 (#417)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos authored Nov 20, 2023
1 parent c4b8767 commit 50cacc1
Show file tree
Hide file tree
Showing 11 changed files with 587 additions and 379 deletions.
806 changes: 472 additions & 334 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ repository = "https://github.com/filecoin-station/zinnia"
[workspace.dependencies]
assert_cmd = "2.0.12"
assert_fs = "1.0.13"
deno_core = "0.204.0"
console_static_text = "=0.8.1"
deno_core = "0.230.0"
log = "0.4.20"
pretty_assertions = "1.4.0"
env_logger = "0.10.1"
Expand Down
14 changes: 8 additions & 6 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::{op, JsBuffer, OpState};
use deno_core::{op2, JsBuffer, OpState};
use libp2p::identity::PeerId;
use libp2p::multiaddr::Protocol;
use libp2p::Multiaddr;
Expand Down Expand Up @@ -45,20 +45,22 @@ deno_core::extension!(
},
);

#[op]
#[op2]
#[string]
pub fn op_p2p_get_peer_id(state: &mut OpState) -> Result<String> {
let rid = state.borrow::<DefaultNodeResourceId>().0;
let node = state.resource_table.get::<PeerNode>(rid)?;
let id = node.peer_id();
Ok(id.to_string())
}

#[op]
#[op2(async)]
#[buffer]
pub async fn op_p2p_request_protocol(
state: Rc<RefCell<OpState>>,
remote_address: String,
protocol_name: String,
request_payload: JsBuffer,
#[string] remote_address: String,
#[string] protocol_name: String,
#[buffer] request_payload: JsBuffer,
) -> Result<Vec<u8>> {
let mut peer_addr: Multiaddr = remote_address
.parse()
Expand Down
14 changes: 7 additions & 7 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ name = "zinnia_runtime"
path = "lib.rs"

[dependencies]
atty = "0.2.14"
console_static_text.workspace = true
chrono = { version= "0.4.31", default-features = false, features = [ "clock", "std" ] }
deno_console = "0.118.0"
deno_console = "0.124.0"
deno_core.workspace = true
deno_crypto = "0.132.0"
deno_fetch = "0.142.0"
deno_url = "0.118.0"
deno_web = "0.149.0"
deno_webidl = "0.118.0"
deno_crypto = "0.138.0"
deno_fetch = "0.148.0"
deno_url = "0.124.0"
deno_web = "0.155.0"
deno_webidl = "0.124.0"
lassie = "0.7.0"
# lassie = { git = "https://github.com/filecoin-station/rusty-lassie.git" }
log.workspace = true
Expand Down
26 changes: 13 additions & 13 deletions runtime/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::rc::Rc;
use deno_core::anyhow::Result;
use deno_core::error::JsError;
use deno_core::url::Url;
use deno_core::{op, OpState};
use deno_core::{op2, OpState};
use deno_fetch::FetchPermissions;
use deno_web::TimersPermission;

use crate::{LogLevel, Reporter};
use crate::Reporter;

/// Hard-coded permissions
pub struct ZinniaPermissions;
Expand All @@ -24,7 +24,6 @@ impl TimersPermission for ZinniaPermissions {
// > adding jitter to them.
false
}
fn check_unstable(&self, _state: &deno_core::OpState, _api_name: &'static str) {}
}

impl FetchPermissions for ZinniaPermissions {
Expand Down Expand Up @@ -68,31 +67,32 @@ deno_core::extension!(

type StoredReporter = Rc<dyn Reporter>;

#[op]
#[op2(fast)]
fn op_job_completed(state: &mut OpState) {
let reporter = state.borrow::<StoredReporter>();
reporter.job_completed();
}

#[op]
fn op_info_activity(state: &mut OpState, msg: &str) {
#[op2(fast)]
fn op_info_activity(state: &mut OpState, #[string] msg: &str) {
let reporter = state.borrow::<StoredReporter>();
reporter.info_activity(msg);
}

#[op]
fn op_error_activity(state: &mut OpState, msg: &str) {
#[op2(fast)]
fn op_error_activity(state: &mut OpState, #[string] msg: &str) {
let reporter = state.borrow::<StoredReporter>();
reporter.error_activity(msg);
}

#[op]
fn op_zinnia_log(state: &mut OpState, msg: &str, level: LogLevel) {
#[op2(fast)]
fn op_zinnia_log(state: &mut OpState, #[string] msg: &str, #[smi] level: i32) {
let reporter = state.borrow::<StoredReporter>();
reporter.log(level, msg);
reporter.log(level.into(), msg);
}

#[op]
fn op_format_test_error(error: JsError) -> String {
#[op2]
#[string]
fn op_format_test_error(#[serde] error: JsError) -> String {
crate::vendored::cli_tools::format_test_error(&error)
}
2 changes: 2 additions & 0 deletions runtime/js/98_global_scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as fileReader from "ext:deno_web/10_filereader.js";
import * as formData from "ext:deno_fetch/21_formdata.js";
import * as request from "ext:deno_fetch/23_request.js";
import * as response from "ext:deno_fetch/23_response.js";
import * as eventSource from "ext:deno_fetch/27_eventsource.js";
import * as fetch from "ext:zinnia_runtime/fetch.js";
import * as messagePort from "ext:deno_web/13_message_port.js";
import * as webidl from "ext:deno_webidl/00_webidl.js";
Expand Down Expand Up @@ -103,6 +104,7 @@ const windowOrWorkerGlobalScope = {
Crypto: util.nonEnumerable(crypto.Crypto),
SubtleCrypto: util.nonEnumerable(crypto.SubtleCrypto),
fetch: util.writable(fetch.fetch),
EventSource: util.writable(eventSource.EventSource),
performance: util.writable(performance.performance),
reportError: util.writable(event.reportError),
setInterval: util.writable(timers.setInterval),
Expand Down
9 changes: 4 additions & 5 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
getNoColor,
inspectArgs,
quoteString,
setNoColor,
setNoColorFn,
wrapConsole,
} from "ext:deno_console/01_console.js";
import * as performance from "ext:deno_web/15_performance.js";
Expand Down Expand Up @@ -64,10 +64,9 @@ function runtimeStart(runtimeOptions) {
// );
// build.setBuildInfo(runtimeOptions.target);
// util.setLogDebug(runtimeOptions.debugFlag, source);
setNoColor(runtimeOptions.noColor || !runtimeOptions.isTty);

// deno-lint-ignore prefer-primordials
Error.prepareStackTrace = core.prepareStackTrace;
// FIXME: rework to lazy load, see
// https://github.com/denoland/deno/commit/1ef617e8f3d48098e69e222b6eb6fe981aeca1c3
setNoColorFn(() => runtimeOptions.noColor || !runtimeOptions.isTty);

setLassieConfig(runtimeOptions.lassieUrl, runtimeOptions.lassieAuth);
setVersions(runtimeOptions.zinniaVersion, runtimeOptions.v8Version);
Expand Down
12 changes: 12 additions & 0 deletions runtime/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ impl From<LogLevel> for log::Level {
}
}

impl From<i32> for LogLevel {
fn from(value: i32) -> Self {
match value {
0 => LogLevel::Debug,
1 => LogLevel::Info,
2 => LogLevel::Warn,
3 => LogLevel::Error,
_ => panic!("Invalid LogLevel value {}", value),
}
}
}

// Report events, activities and messages from the running module
pub trait Reporter {
/// Print a debug log message. This is typically triggered by Console APIs like `console.log`.
Expand Down
20 changes: 18 additions & 2 deletions runtime/vendored/cli_tools.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
// https://github.com/denoland/deno/blob/v1.33.3/cli/tools/test.rs
// https://github.com/denoland/deno/blob/v1.38.2/cli/tools/test.rs
//
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use crate::fmt_errors::format_js_error;
use deno_core::error::JsError;
use deno_core::{error::JsError, url::Url};

#[allow(dead_code)]
pub fn to_relative_path_or_remote_url(cwd: &Url, path_or_url: &str) -> String {
let Ok(url) = Url::parse(path_or_url) else {
return "<anonymous>".to_string();
};
if url.scheme() == "file" {
if let Some(mut r) = cwd.make_relative(&url) {
if !r.starts_with("../") {
r = format!("./{r}");
}
return r;
}
}
path_or_url.to_string()
}

fn abbreviate_test_error(js_error: &JsError) -> JsError {
let mut js_error = js_error.clone();
Expand Down
18 changes: 15 additions & 3 deletions runtime/vendored/colors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// https://github.com/denoland/deno/blob/v1.31.2/runtime/colors.rs
// https://github.com/denoland/deno/blob/v1.38.2/runtime/colors.rs
//
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use atty;
use once_cell::sync::Lazy;
use std::fmt;
use std::io::IsTerminal;
use std::io::Write;
use termcolor::Ansi;
use termcolor::Color::Ansi256;
use termcolor::Color::Black;
use termcolor::Color::Blue;
use termcolor::Color::Cyan;
use termcolor::Color::Green;
use termcolor::Color::Magenta;
use termcolor::Color::Red;
use termcolor::Color::White;
use termcolor::Color::Yellow;
Expand All @@ -25,7 +26,7 @@ use termcolor::ColorChoice;

static NO_COLOR: Lazy<bool> = Lazy::new(|| std::env::var_os("NO_COLOR").is_some());

static IS_TTY: Lazy<bool> = Lazy::new(|| atty::is(atty::Stream::Stdout));
static IS_TTY: Lazy<bool> = Lazy::new(|| std::io::stdout().is_terminal());

pub fn is_tty() -> bool {
*IS_TTY
Expand Down Expand Up @@ -105,6 +106,17 @@ pub fn cyan<S: AsRef<str>>(s: S) -> impl fmt::Display {
style_spec.set_fg(Some(Cyan));
style(s, style_spec)
}
pub fn cyan_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Cyan)).set_bold(true);
style(s, style_spec)
}

pub fn magenta<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Magenta));
style(s, style_spec)
}

pub fn red<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
Expand Down
42 changes: 34 additions & 8 deletions runtime/vendored/fmt_errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/denoland/deno/blob/v1.31.2/runtime/fmt_errors.rs
// https://github.com/denoland/deno/blob/v1.38.2/runtime/fmt_errors.rs
//
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
//! This mod provides DenoError to unify errors across Deno.
Expand All @@ -14,7 +14,7 @@ use std::fmt::Write as _;
/// Compares all properties of JsError, except for JsError::cause.
/// This function is used to detect that 2 JsError objects in a JsError::cause
/// chain are identical, ie. there is a recursive cause.
/// 02_console.js, which also detects recursive causes, can use JS object
/// 01_console.js, which also detects recursive causes, can use JS object
/// comparisons to compare errors. We don't have access to JS object identity in
/// format_js_error().
fn errors_are_equal_without_cause(a: &JsError, b: &JsError) -> bool {
Expand Down Expand Up @@ -47,7 +47,8 @@ pub fn format_location(frame: &JsStackFrame) -> String {
let _internal = frame
.file_name
.as_ref()
.map_or(false, |f| f.starts_with("ext:"));
.map(|f| f.starts_with("ext:"))
.unwrap_or(false);
if frame.is_native {
return cyan("native").to_string();
}
Expand All @@ -74,7 +75,8 @@ fn format_frame(frame: &JsStackFrame) -> String {
let _internal = frame
.file_name
.as_ref()
.map_or(false, |f| f.starts_with("ext:"));
.map(|f| f.starts_with("ext:"))
.unwrap_or(false);
let is_method_call = !(frame.is_top_level.unwrap_or_default() || frame.is_constructor);
let mut result = String::new();
if frame.is_async {
Expand Down Expand Up @@ -252,7 +254,10 @@ fn format_js_error_inner(
if let Some(aggregated) = &js_error.aggregated {
let aggregated_message = format_aggregated_error(
aggregated,
circular.as_ref().map_or(0, |circular| circular.index),
circular
.as_ref()
.map(|circular| circular.index)
.unwrap_or(0),
);
s.push_str(&aggregated_message);
}
Expand All @@ -274,9 +279,10 @@ fn format_js_error_inner(
write!(s, "\n at {}", format_frame(frame)).unwrap();
}
if let Some(cause) = &js_error.cause {
let is_caused_by_circular = circular.as_ref().map_or(false, |circular| {
errors_are_equal_without_cause(circular.reference.from, js_error)
});
let is_caused_by_circular = circular
.as_ref()
.map(|circular| errors_are_equal_without_cause(circular.reference.from, js_error))
.unwrap_or(false);

let error_string = if is_caused_by_circular {
cyan(format!("[Circular *{}]", circular.unwrap().index)).to_string()
Expand All @@ -303,3 +309,23 @@ pub fn format_js_error(js_error: &JsError) -> String {

format_js_error_inner(js_error, circular, true)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_format_none_source_line() {
let actual = format_maybe_source_line(None, None, false, 0);
assert_eq!(actual, "");
}

#[test]
fn test_format_some_source_line() {
let actual = format_maybe_source_line(Some("console.log('foo');"), Some(9), true, 0);
assert_eq!(
console_static_text::ansi::strip_ansi_codes(&actual),
"\nconsole.log(\'foo\');\n ^"
);
}
}

0 comments on commit 50cacc1

Please sign in to comment.