Skip to content

Commit

Permalink
Upgrade Deno (#496)
Browse files Browse the repository at this point in the history
* chore(deps): upgrade deno-runtime, use re-exported deno-core

* feat(auraescript): use snapshots when bootstrapping deno runtime

* feat(auraescript): remove use of `opAsync`

* chore(auraescript): bootstrap options to original values

* chore(ci): limit cargo-deny to x86/aarch64 linux-musl targets

---------

Co-authored-by: dominic <[email protected]>
  • Loading branch information
mccormickt and dmah42 authored Jan 21, 2024
1 parent d633ce3 commit f616e44
Show file tree
Hide file tree
Showing 17 changed files with 1,770 additions and 1,164 deletions.
2,699 changes: 1,668 additions & 1,031 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions auraescript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ path = "src/bin/main.rs"
[dependencies]
anyhow = { workspace = true }
client = { workspace = true }
deno_ast = { version = "0.29.5", features = ["transpiling"] }
deno_core = "0.222.0"
deno_runtime = "0.129.0"
deno_ast = { version = "1.0.1", features = ["transpiling"] }
deno_runtime = { version = "0.140.0" }
macros = { package = "auraescript_macros", path = "./macros" }
proto = { workspace = true }
tokio = { workspace = true, features = ["fs", "rt-multi-thread"] }

[build-dependencies]
deno_runtime = "0.140.0"
2 changes: 1 addition & 1 deletion auraescript/aurae.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ export function createClient(opts: CreateClient = { kind: "default" }): Promise<
}
}
// @ts-ignore
return Deno[Deno.internal].core.opAsync("as__client_new", config);
return Deno[Deno.internal].core.ops.as__client_new(config);
}
7 changes: 7 additions & 0 deletions auraescript/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use deno_runtime::snapshot;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;

fn main() {
generate_aurae_ts();

// Create a runtime snapshot for Deno to be used on startup
snapshot::create_runtime_snapshot(
"gen/runtime.bin".into(),
Default::default(),
)
}

fn generate_aurae_ts() {
Expand Down
17 changes: 9 additions & 8 deletions auraescript/macros/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,25 @@ pub(crate) fn ops_generator(input: TokenStream) -> TokenStream {

// Magic OpState from deno (https://github.com/denoland/deno/blob/b6ac54815c1bcfa44a45b3f2c1c982829482477f/ops/lib.rs#L295)
quote! {
#[::deno_core::op]
#[::deno_runtime::deno_core::op2(async)]
#[serde]
pub(crate) async fn #op_ident(
op_state: Rc<RefCell<OpState>>, // Auto filled by deno macro, call from typescript ignoring this parameter
client_rid: Option<::deno_core::ResourceId>,
req: ::proto::#module::#input_type,
#[smi] client_rid: Option<::deno_runtime::deno_core::ResourceId>,
#[serde] req: ::proto::#module::#input_type,
) -> std::result::Result<
::proto::#module::#output_type,
::anyhow::Error
> {
let client = match client_rid {
None => ::deno_core::RcRef::new(::client::Client::default().await?),
None => ::deno_runtime::deno_core::RcRef::new(::client::Client::default().await?),
Some(client_rid) => {
let as_client = {
let op_state = &op_state.borrow();
let rt = &op_state.resource_table; // get `ResourceTable` from JsRuntime `OpState`
rt.get::<crate::builtin::auraescript_client::AuraeScriptClient>(client_rid)?.clone() // get `Client` from its rid
};
::deno_core::RcRef::map(as_client, |v| &v.0)
::deno_runtime::deno_core::RcRef::map(as_client, |v| &v.0)
}
};
let res = ::client::#module::#service_name_in_snake_case::#client_ident::#name(
Expand Down Expand Up @@ -127,11 +128,11 @@ pub(crate) fn ops_generator(input: TokenStream) -> TokenStream {

let expanded = quote! {
use ::std::{rc::Rc, cell::RefCell};
use ::deno_core::OpState;
use ::deno_runtime::deno_core::{self, Op, OpState};

#(#(#op_functions)*)*

pub(crate) fn op_decls() -> Vec<::deno_core::OpDecl> {
pub(crate) fn op_decls() -> Vec<::deno_runtime::deno_core::OpDecl> {
vec![#(#(#op_decls,)*)*]
}
};
Expand Down Expand Up @@ -248,7 +249,7 @@ export class {service_name}Client implements {service_name} {{
r#"
{fn_name}(request: {input_type}): Promise<{output_type}> {{
// @ts-ignore
return Deno[Deno.internal].core.opAsync("{op_name}", this.client, request);
return Deno[Deno.internal].core.ops.{op_name}(this.client, request);
}}
"#
));
Expand Down
4 changes: 2 additions & 2 deletions auraescript/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
)]
#![warn(clippy::unwrap_used)]

use auraescript::*;
use deno_core::resolve_path;
use auraescript::init;
use deno_runtime::deno_core::resolve_path;
use std::env::current_dir;

#[tokio::main(flavor = "current_thread")]
Expand Down
31 changes: 17 additions & 14 deletions auraescript/src/builtin/auraescript_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Result;
use client::{AuraeConfig, Client};
use deno_core::{Op, OpState, Resource, ResourceId};
use deno_runtime::deno_core::{self, op2, Op, OpState, Resource, ResourceId};
use std::{cell::RefCell, rc::Rc};

// `AuraeConfig` `try_default`
#[deno_core::op]
#[op2(fast)]
#[smi]
pub(crate) fn as__aurae_config__try_default(
op_state: &mut OpState,
) -> Result<ResourceId> {
Expand All @@ -14,41 +15,43 @@ pub(crate) fn as__aurae_config__try_default(
}

// `AuraeConfig` `from_options`
#[deno_core::op]
#[op2(fast)]
#[smi]
pub(crate) fn as__aurae_config__from_options(
op_state: &mut OpState,
ca_crt: String,
client_crt: String,
client_key: String,
socket: String,
#[string] ca_crt: String,
#[string] client_crt: String,
#[string] client_key: String,
#[string] socket: String,
) -> ResourceId {
let config =
AuraeConfig::from_options(ca_crt, client_crt, client_key, socket);
op_state.resource_table.add(AuraeScriptConfig(config))
}

// `AuraeConfig` `parse_from_file`
#[deno_core::op]
#[op2(fast)]
#[smi]
pub(crate) fn as__aurae_config__parse_from_file(
op_state: &mut OpState,
path: String,
#[string] path: String,
) -> Result<ResourceId> {
let config = AuraeConfig::parse_from_toml_file(path)?;
let rid = op_state.resource_table.add(AuraeScriptConfig(config));
Ok(rid)
}

// Re export AuraeConfig in auraescript to be able
// to impl Resource on it
// Re export AuraeConfig in auraescript to be able to impl Resource on it
pub(crate) struct AuraeScriptConfig(pub AuraeConfig);

impl Resource for AuraeScriptConfig {} // Blank impl

// Create a `Client` with given `AuraeConfig`
#[deno_core::op]
#[op2(async)]
#[smi]
pub(crate) async fn as__client_new(
op_state: Rc<RefCell<OpState>>,
config: ResourceId,
#[smi] config: ResourceId,
) -> Result<ResourceId> {
let config = {
let op_state = &op_state.borrow();
Expand All @@ -61,7 +64,7 @@ pub(crate) async fn as__client_new(
Ok(rid)
}

pub(crate) fn op_decls() -> Vec<::deno_core::OpDecl> {
pub(crate) fn op_decls() -> Vec<::deno_runtime::deno_core::OpDecl> {
vec![
as__aurae_config__try_default::DECL,
as__aurae_config__from_options::DECL,
Expand Down
4 changes: 1 addition & 3 deletions auraescript/src/cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@
* *
\* -------------------------------------------------------------------------- */

use deno_core::Op;

macros::ops_generator!("../api/v0/cells/cells.proto", cells, CellService,);
macros::ops_generator!("../api/v0/cells/cells.proto", cells, CellService);
2 changes: 0 additions & 2 deletions auraescript/src/cri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* *
\* -------------------------------------------------------------------------- */

use deno_core::Op;

// TODO: macro doesn't support streaming. Does deno?
macros::ops_generator!(
"../api/cri/v1/release-1.26.proto",
Expand Down
2 changes: 0 additions & 2 deletions auraescript/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* *
\* -------------------------------------------------------------------------- */

use deno_core::Op;

macros::ops_generator!(
"../api/v0/discovery/discovery.proto",
discovery,
Expand Down
2 changes: 0 additions & 2 deletions auraescript/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* *
\* -------------------------------------------------------------------------- */

use deno_core::Op;

// TODO: macro doesn't support streaming. Does deno?
macros::ops_generator!(
"../api/grpc/health/v1/health.proto",
Expand Down
114 changes: 57 additions & 57 deletions auraescript/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
/* -------------------------------------------------------------------------- *\
* Apache 2.0 License Copyright © 2022-2023 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */
* Apache 2.0 License Copyright © 2022-2023 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */
//! # AuraeScript
//!
//! AuraeScript is a turing complete language for platform teams built on [Deno](https://deno.land).
Expand Down Expand Up @@ -66,20 +66,18 @@

use anyhow::{anyhow, bail, Error};
use deno_ast::{MediaType, ParseParams, SourceTextInfo};
use deno_core::error::AnyError;
use deno_core::futures::FutureExt;
use deno_core::url::Url;
use deno_core::{
resolve_import, ModuleCode, ModuleLoader, ModuleSource,
ModuleSourceFuture, ModuleSpecifier, ModuleType, ResolutionKind,
use deno_runtime::{
deno_core::{
self, error::AnyError, futures::FutureExt, resolve_import, url::Url,
FastString, ModuleLoader, ModuleSource, ModuleSourceCode,
ModuleSourceFuture, ModuleSpecifier, ModuleType, ResolutionKind,
Snapshot,
},
permissions::PermissionsContainer,
worker::{MainWorker, WorkerOptions},
BootstrapOptions, WorkerLogLevel,
};

use deno_runtime::deno_core::{Extension, OpDecl};
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::worker::{MainWorker, WorkerOptions};
use deno_runtime::{BootstrapOptions, WorkerLogLevel};

use std::borrow::Cow;
use std::pin::Pin;
use std::rc::Rc;

Expand All @@ -90,22 +88,24 @@ mod discovery;
mod health;
mod observe;

// Load the snapshot of the Deno javascript runtime
static RUNTIME_SNAPSHOT: &[u8] = include_bytes!("../gen/runtime.bin");

fn get_error_class_name(e: &AnyError) -> &'static str {
deno_runtime::errors::get_error_class_name(e).unwrap_or("Error")
}

pub fn init(main_module: Url) -> MainWorker {
let extension =
//Extension::builder("").ops(stdlib()).build();
Extension{name: "", ops: Cow::from(stdlib()), ..Default::default()};
deno_core::extension!(auraescript, ops_fn = stdlib);

pub fn init(main_module: Url) -> MainWorker {
MainWorker::bootstrap_from_options(
main_module,
PermissionsContainer::allow_all(),
WorkerOptions {
extensions: vec![extension],
extensions: vec![auraescript::init_ops()],
module_loader: Rc::new(TypescriptModuleLoader),
get_error_class_fn: Some(&get_error_class_name),
startup_snapshot: Some(Snapshot::Static(RUNTIME_SNAPSHOT)),
bootstrap: BootstrapOptions {
args: vec![],
cpu_count: 1,
Expand All @@ -115,16 +115,14 @@ pub fn init(main_module: Url) -> MainWorker {
log_level: WorkerLogLevel::Info,
no_color: false,
is_tty: false,
runtime_version: "".to_string(),
ts_version: "".to_string(),
unstable: true,
user_agent: "".to_string(),
inspect: false,
..Default::default()
},
..Default::default()
},
)
)
}

/// Standard Library Autogeneration Code
Expand All @@ -137,7 +135,7 @@ pub fn init(main_module: Url) -> MainWorker {
///
/// ops.extend(my_package::op_decls());
///
fn stdlib() -> Vec<OpDecl> {
fn stdlib() -> Vec<deno_core::OpDecl> {
let mut ops = vec![];
ops.extend(builtin::auraescript_client::op_decls());
ops.extend(cells::op_decls());
Expand Down Expand Up @@ -181,12 +179,12 @@ impl ModuleLoader for TypescriptModuleLoader {
}
MediaType::Jsx => (ModuleType::JavaScript, true),
MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
| MediaType::Dts
| MediaType::Dmts
| MediaType::Dcts
| MediaType::Tsx => (ModuleType::JavaScript, true),
| MediaType::Mts
| MediaType::Cts
| MediaType::Dts
| MediaType::Dmts
| MediaType::Dcts
| MediaType::Tsx => (ModuleType::JavaScript, true),
MediaType::Json => (ModuleType::Json, false),
_ => bail!("Unknown extension {:?}", path.extension()),
};
Expand All @@ -206,9 +204,11 @@ impl ModuleLoader for TypescriptModuleLoader {
code
};
let module = ModuleSource::new_with_redirect(
module_type, ModuleCode::from(code),
module_type,
ModuleSourceCode::String(FastString::Owned(code.into())),
&module_specifier,
&module_specifier,
&module_specifier);
);
Ok(module)
}
.boxed_local()
Expand Down
Loading

0 comments on commit f616e44

Please sign in to comment.