Skip to content

Commit

Permalink
Merge pull request #193 from supabase/npm-implementation
Browse files Browse the repository at this point in the history
feat: Npm implementation
  • Loading branch information
andreespirela authored Nov 28, 2023
2 parents dd689ad + ebeea0e commit e3c9e9e
Show file tree
Hide file tree
Showing 48 changed files with 3,461 additions and 600 deletions.
92 changes: 92 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ members = [
"./crates/sb_os",
"./crates/cpu_timer",
"./crates/event_worker",
"./crates/sb_eszip"
"./crates/sb_eszip",
"./crates/npm"
]
resolver = "2"

Expand Down Expand Up @@ -48,6 +49,7 @@ bytes = { version = "1.4.0" }
once_cell = "1.17.1"
thiserror = "1.0.40"
deno_lockfile = "0.17.1"
async-trait = "0.1.73"
indexmap = { version = "2.0.0", features = ["serde"] }
flate2 = "=1.0.26"
tar = "=0.4.40"
Expand Down
8 changes: 8 additions & 0 deletions crates/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait.workspace = true
thiserror = "1.0.40"
monch = "=0.4.3"
once_cell.workspace = true
deno_semver.workspace = true
deno_npm.workspace = true
cpu_timer = { version = "0.1.0", path = "../cpu_timer" }
anyhow = { workspace = true }
bytes = { version = "1.2.1" }
Expand Down Expand Up @@ -41,11 +47,13 @@ sb_env = { version = "0.1.0", path = "../sb_env" }
sb_core = { version = "0.1.0", path = "../sb_core" }
sb_os = { version = "0.1.0", path = "../sb_os" }
sb_eszip = { version = "0.1.0", path = "../sb_eszip" }
sb_npm = { version = "0.1.0", path = "../npm" }
urlencoding = { version = "2.1.2" }
uuid = { workspace = true }
deno_broadcast_channel.workspace = true
sb_node = { version = "0.1.0", path = "../node" }
eszip.workspace = true
notify = { version = "6.1.1", default-features = false, features = ["macos_kqueue"] }

[dev-dependencies]
futures-util = { version = "0.3.28" }
Expand Down
47 changes: 4 additions & 43 deletions crates/base/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ use std::path::PathBuf;

mod supabase_startup_snapshot {
use super::*;
use deno_ast::MediaType;
use deno_ast::ParseParams;
use deno_ast::SourceTextInfo;
use deno_core::error::AnyError;
use deno_core::snapshot_util::*;
use deno_core::ExtensionFileSource;
use deno_core::{Extension, ExtensionFileSourceCode};
use deno_core::Extension;
use deno_fs::OpenOptions;
use deno_http::DefaultHttpPropertyExtractor;
use event_worker::js_interceptors::sb_events_js_interceptors;
Expand All @@ -20,49 +16,14 @@ mod supabase_startup_snapshot {
use sb_core::permissions::sb_core_permissions;
use sb_core::runtime::sb_core_runtime;
use sb_core::sb_core_main_js;
use sb_core::transpiler::maybe_transpile_source;
use sb_env::sb_env;
use sb_node::deno_node;
use sb_workers::sb_user_workers;
use std::path::Path;
use std::sync::Arc;
use url::Url;

fn maybe_transpile_source(source: &mut ExtensionFileSource) -> Result<(), AnyError> {
let media_type = if source.specifier.starts_with("node:") {
MediaType::TypeScript
} else {
MediaType::from_path(Path::new(&source.specifier))
};

match media_type {
MediaType::TypeScript => {}
MediaType::JavaScript => return Ok(()),
MediaType::Mjs => return Ok(()),
_ => panic!(
"Unsupported media type for snapshotting {media_type:?} for file {}",
source.specifier
),
}
let code = source.load()?;

let parsed = deno_ast::parse_module(ParseParams {
specifier: source.specifier.to_string(),
text_info: SourceTextInfo::from_string(code.as_str().to_owned()),
media_type,
capture_tokens: false,
scope_analysis: false,
maybe_syntax: None,
})?;
let transpiled_source = parsed.transpile(&deno_ast::EmitOptions {
imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove,
inline_source_map: false,
..Default::default()
})?;

source.code = ExtensionFileSourceCode::Computed(transpiled_source.text.into());
Ok(())
}

#[derive(Clone)]
pub struct Permissions;

Expand Down Expand Up @@ -231,10 +192,10 @@ mod supabase_startup_snapshot {

for extension in &mut extensions {
for source in extension.esm_files.to_mut() {
maybe_transpile_source(source).unwrap();
let _ = maybe_transpile_source(source).unwrap();
}
for source in extension.js_files.to_mut() {
maybe_transpile_source(source).unwrap();
let _ = maybe_transpile_source(source).unwrap();
}
}

Expand Down
Loading

0 comments on commit e3c9e9e

Please sign in to comment.