Skip to content

Commit d9023d7

Browse files
authored
feat: intro Andromeda.serve (#137)
1 parent 6c9b463 commit d9023d7

File tree

7 files changed

+74
-10
lines changed

7 files changed

+74
-10
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ andromeda-core = { path = "core" }
1414
andromeda-runtime = { path = "runtime", features = [
1515
"canvas",
1616
"crypto",
17-
"storage"
17+
"storage",
18+
"serve"
1819
] }
1920
anyhow = "1.0.99"
2021
anymap = "0.12.1"

examples/serve/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Andromeda.serve();

namespace/mod.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,12 @@ const Andromeda = {
438438
* console.log(entries);
439439
* ```
440440
*/
441-
readDirSync(
442-
path: string,
443-
): Array<
444-
{ name: string; isFile: boolean; isDirectory: boolean; isSymlink: boolean; }
445-
> {
441+
readDirSync(path: string): Array<{
442+
name: string;
443+
isFile: boolean;
444+
isDirectory: boolean;
445+
isSymlink: boolean;
446+
}> {
446447
return __andromeda__.internal_read_dir(path);
447448
},
448449

@@ -736,6 +737,16 @@ const Andromeda = {
736737
}
737738
}
738739
},
740+
741+
/**
742+
* @example
743+
* ```ts
744+
* Andromeda.serve();
745+
* ```
746+
*/
747+
serve(): string[] {
748+
return __andromeda__.internal_serve();
749+
},
739750
};
740751

741752
/**
@@ -806,9 +817,13 @@ function encodeURIComponent(input: string): string {
806817
for (const b of bytes) {
807818
// unreserved characters A-Z a-z 0-9 - _ . ~
808819
if (
809-
(b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5A) ||
810-
(b >= 0x61 && b <= 0x7A) || b === 0x2D || b === 0x5F || b === 0x2E ||
811-
b === 0x7E
820+
(b >= 0x30 && b <= 0x39) ||
821+
(b >= 0x41 && b <= 0x5a) ||
822+
(b >= 0x61 && b <= 0x7a) ||
823+
b === 0x2d ||
824+
b === 0x5f ||
825+
b === 0x2e ||
826+
b === 0x7e
812827
) {
813828
out += String.fromCharCode(b);
814829
} else {

runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ default = []
1111
canvas = ["dep:wgpu", "dep:image"]
1212
crypto = ["dep:ring", "dep:rand"]
1313
storage = ["dep:rusqlite"]
14+
serve = []
1415

1516
[dependencies]
1617
andromeda-core.workspace = true

runtime/src/ext/http/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use andromeda_core::{Extension, ExtensionOp};
2+
use nova_vm::{
3+
ecmascript::{
4+
builtins::ArgumentsList,
5+
execution::{Agent, JsResult},
6+
types::Value,
7+
},
8+
engine::context::GcScope,
9+
};
10+
11+
#[derive(Default)]
12+
pub struct ServeExt;
13+
14+
impl ServeExt {
15+
pub fn new_extension() -> Extension {
16+
Extension {
17+
name: "serve",
18+
ops: vec![ExtensionOp::new(
19+
"internal_serve",
20+
Self::internal_serve,
21+
0,
22+
false,
23+
)],
24+
storage: None,
25+
files: vec![],
26+
}
27+
}
28+
29+
fn internal_serve<'gc>(
30+
agent: &mut Agent,
31+
_this: Value,
32+
_args: ArgumentsList,
33+
gc: GcScope<'gc, '_>,
34+
) -> JsResult<'gc, Value<'gc>> {
35+
println!("hello world");
36+
Ok(Value::from_string(
37+
agent,
38+
"Success".to_string(),
39+
gc.into_nogc(),
40+
))
41+
}
42+
}

runtime/src/ext/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod fetch;
1515
mod ffi;
1616
mod file;
1717
mod fs;
18+
mod http;
1819
#[cfg(feature = "storage")]
1920
mod local_storage;
2021
mod net;
@@ -40,6 +41,7 @@ pub use fetch::*;
4041
pub use ffi::*;
4142
pub use file::*;
4243
pub use fs::*;
44+
pub use http::*;
4345
#[cfg(feature = "storage")]
4446
pub use local_storage::*;
4547
pub use net::*;

runtime/src/recommended.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use nova_vm::{
1414

1515
use crate::{
1616
BroadcastChannelExt, ConsoleExt, CronExt, FetchExt, FfiExt, FileExt, FsExt, NetExt, ProcessExt,
17-
RuntimeMacroTask, StreamsExt, TimeExt, TlsExt, URLExt, WebExt,
17+
RuntimeMacroTask, ServeExt, StreamsExt, TimeExt, TlsExt, URLExt, WebExt,
1818
};
1919

2020
pub fn recommended_extensions() -> Vec<Extension> {
@@ -33,6 +33,8 @@ pub fn recommended_extensions() -> Vec<Extension> {
3333
StreamsExt::new_extension(),
3434
TlsExt::new_extension(),
3535
FfiExt::new_extension(),
36+
#[cfg(feature = "serve")]
37+
ServeExt::new_extension(),
3638
#[cfg(feature = "canvas")]
3739
crate::CanvasExt::new_extension(),
3840
#[cfg(feature = "crypto")]

0 commit comments

Comments
 (0)