Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
chore: add support for tar images for js (#582)
Browse files Browse the repository at this point in the history
Fixes RVT-4170
  • Loading branch information
NathanFlurry committed Nov 23, 2024
1 parent f5c588b commit 5930aa1
Show file tree
Hide file tree
Showing 20 changed files with 262 additions and 212 deletions.
40 changes: 40 additions & 0 deletions ACTOR_CRASH_COURSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Actor Crash Course

## Prerequisites

- Must have Rivet docker compose running
- Must have hub running just for the device link

## Actor test

```bash
cargo build
cd examples/js-deno
../../target/debug/rivet login --api-endpoint http://localhost:8080
# copy the path to localhost:5080/device/link/... and finish linking
../../target/debug/rivet deploy default
# copy build id
../../target/debug/rivet actor create default -t name=rng --build MY_BUILD_ID --region local --network-mode host --port protocol=tcp,name=http,host
# copy public_hostname and public_port
curl 127.0.0.1:20081
../../target/debug/rivet actor destroy default --id MY_ACTOR_ID
```

## Reference

- See `rivet --help` for more commands
- rivet.jsonc config spec at `packages/toolchain/src/config/mod.rs`
- WIP typedefs for actors [here](https://github.com/rivet-gg/rivet/blob/925b9b5c2f024f40615e910e9670655249eb2bc7/sdks/actor-types/src/internal/90_rivet_ns.ts)

## Known issues

- Only supports host networking
- Networking is not working (afaik)
- LZ4 compression for JS tars don't work (defaults to correct compression, no action needed)

## Troubleshooting

### `Error getting bootstrap: get project failed`

Make sure to run `rivet logout` if you reset the core cluster.

44 changes: 44 additions & 0 deletions examples/actor-layer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Without Rivet actor class

const portStr = Deno.env.get("PORT_http") ?? Deno.env.get("HTTP_PORT");
if (!portStr) throw "Missing port";
const port = parseInt(portStr);
if (!isFinite(port)) throw "Invalid port";

const server = Deno.serve({
handler,
port,
hostname: "0.0.0.0",
});

await server.finished;

async function handler(req: Request) {
console.log("Received request");

let newCount = (await Rivet.kv.get("count") ?? 0) + 1;
await Rivet.kv.put("count", newCount);

return new Response(newCount.toString());
}

// With Rivet actor class

import { Actor } from "@rivet-gg/rivet";

interface State {
count: number;
}

class Counter extends Actor<State> {
initialize(): State {
return { count: 0 };
}

async onRequest(req) {
this.count += 1;
return new Response(this.count.toString());
}
}

Rivet.run(Counter);
2 changes: 1 addition & 1 deletion examples/js-deno/rivet.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "2.0",
"builds": [
{
"tags": { "name": "rng-http" },
"tags": { "name": "rng" },
"runtime": "javascript",
"script": "rng_http.ts"
}
Expand Down
3 changes: 2 additions & 1 deletion examples/js-deno/rng_http.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { randomIntegerBetween, randomSeeded } from "@std/random";

console.log("Hello, world!");
console.log(Deno.env.toObject());

const portStr = Deno.env.get("PORT_ds_http") ?? Deno.env.get("HTTP_PORT");
const portStr = Deno.env.get("PORT_http") ?? Deno.env.get("HTTP_PORT");
if (!portStr) throw "Missing port";
const port = parseInt(portStr);
if (!isFinite(port)) throw "Invalid port";
Expand Down
8 changes: 8 additions & 0 deletions examples/js-kv/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
await Rivet.kv.put("count", 0);

console.log('Started', Deno.env.toObject());
setInterval(async () => {
let x = await Rivet.kv.get("count");
await Rivet.kv.put("count", x + 1);
console.log('Count', x);
}, 1000);
11 changes: 11 additions & 0 deletions examples/js-kv/rivet.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0",
"builds": [
{
"tags": { "name": "kv" },
"runtime": "javascript",
"bundler": "none",
"script": "index.js"
}
]
}
1 change: 1 addition & 0 deletions packages/toolchain/src/config/build/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub enum BuildMethod {
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize, strum::AsRefStr)]
#[serde(rename_all = "snake_case")]
pub enum BundleKind {
/// Legacy option. Docker image archive output from `docker save`. Slower lobby start
/// times.
Expand Down
7 changes: 3 additions & 4 deletions packages/toolchain/src/config/build/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub struct Unstable {
pub minify: Option<bool>,
pub analyze_result: Option<bool>,
pub esbuild_log_level: Option<String>,
// TODO(RVT-4127): Add compression support
// pub compression: Option<Compression>,
pub compression: Option<Compression>,
}

impl Unstable {
Expand All @@ -60,8 +59,8 @@ impl Unstable {
.unwrap_or_else(|| "error".to_string())
}

// TODO(RVT-4127): Add compression support
pub fn compression(&self) -> Compression {
Compression::None
// TODO: Change back to Lz4 default
self.compression.unwrap_or(Compression::None)
}
}
1 change: 1 addition & 0 deletions packages/toolchain/src/config/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum Runtime {
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize, strum::AsRefStr)]
#[serde(rename_all = "snake_case")]
pub enum Compression {
/// No compression.
#[strum(serialize = "none")]
Expand Down
11 changes: 1 addition & 10 deletions packages/toolchain/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,4 @@ pub struct Build {

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Unstable {
#[serde(skip_serializing_if = "Option::is_none")]
pub multipart_enabled: Option<bool>,
}

impl Unstable {
pub fn multipart_enabled(&self) -> bool {
self.multipart_enabled.unwrap_or(true)
}
}
pub struct Unstable {}
Loading

0 comments on commit 5930aa1

Please sign in to comment.