Skip to content

Commit

Permalink
Merge pull request #5186 from systeminit/deno-lang-js
Browse files Browse the repository at this point in the history
feat: replace vm2 with deno and web workers
  • Loading branch information
stack72 authored Jan 17, 2025
2 parents 11e78ba + cf99411 commit 1df917b
Show file tree
Hide file tree
Showing 72 changed files with 6,148 additions and 788 deletions.
4 changes: 4 additions & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export_file(
name = "package.json",
)

export_file(
name = "deno.json",
)

pnpm_workspace(
name = "pnpm-workspace.yaml",
packages = [
Expand Down
4 changes: 2 additions & 2 deletions bin/lang-js/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = {
},
],
parserOptions: {
parser: '@typescript-eslint/parser',
project: './tsconfig.json',
parser: "@typescript-eslint/parser",
project: "./tsconfig.json",
tsconfigRootDir: __dirname,
},
};
133 changes: 52 additions & 81 deletions bin/lang-js/BUCK
Original file line number Diff line number Diff line change
@@ -1,113 +1,84 @@
load(
"@prelude-si//:macros.bzl",
"eslint",
"alias",
"export_file",
"ts_test",
"nix_omnibus_pkg",
"node_pkg_bin",
"npm_bin",
"package_node_modules",
"prettier_check",
"typescript_check",
"typescript_dist",
"typescript_runnable_dist",
"typescript_runnable_dist_bin",
)
load(
"@prelude-si//:pnpm.bzl",
"pnpm_task_library",
"pnpm_task_binary",
"pnpm_task_test",
"@prelude-si//:deno.bzl",
"deno_compile",
"deno_format",
"deno_test",
)

export_file(
name = "package.json",
)

package_node_modules(
name = "node_modules",
)

filegroup(
name = "src",
srcs = glob([
"src/**/*.ts",
"tsconfig.json",
]),
)

filegroup(
name = "test_src",
srcs = glob([
"tests/**/*",
]),
)

prod_deps_srcs = {
"lib/ts-lib": "//lib/ts-lib:src",
}

dev_deps_srcs = {
"lib/eslint-config": "//lib/eslint-config:src",
"lib/tsconfig": "//lib/tsconfig:src",
}

npm_bin(
name = "tsup",
)

typescript_dist(
name = "dist",
srcs = [":src"],
tsup = ":tsup",
prod_deps_srcs = prod_deps_srcs,
dev_deps_srcs = dev_deps_srcs,
export_file(
name = "deno.json",
)

typescript_runnable_dist(
alias(
name = "lang-js",
actual = ":bin"
)

typescript_runnable_dist_bin(
deno_compile(
name = "bin",
typescript_runnable_dist = ":lang-js",
main = "src/index.ts",
out = "lang-js",
srcs = glob([
"src/**/*.ts",
"src/**/*.js",
]) +
["//lib/ts-lib-deno:ts-lib-deno"],
permissions = [
"allow-all",
],
unstable_flags = [
"worker-options",
],
visibility = ["PUBLIC"],
)

eslint(
name = "check-lint",
directories = ["src", "tests"],
srcs = [":src", ":test_src"] + glob([".eslint*"]),
prod_deps_srcs = prod_deps_srcs,
dev_deps_srcs = dev_deps_srcs,
deno_format(
name = "fix-format",
srcs = glob(["**/*.ts", "**/*.js"]),
ignore = ["node_modules"],
)

typescript_check(
name = "check-type",
srcs = [":src"],
prod_deps_srcs = prod_deps_srcs,
dev_deps_srcs = dev_deps_srcs,
deno_format(
name = "check-format",
srcs = glob(["**/*.ts", "**/*.js"]),
check = True,
)

ts_test(
deno_test(
name = "test-unit",
srcs = [":src", ":test_src"],
prod_deps_srcs = prod_deps_srcs,
dev_deps_srcs = dev_deps_srcs,
srcs = glob(["**/tests/*.spec.ts"]),
ignore = ["node_modules"],
permissions = [
"allow-all",
],
unstable_flags = [
"worker-options",
],
)

dev_deps_srcs = {
"lib/eslint-config": "//lib/eslint-config:src",
"lib/tsconfig": "//lib/tsconfig:src",
}

nix_omnibus_pkg(
name = "omnibus",
pkg_name = "lang-js",
build_dep = "//bin/lang-js:bin",
)

pnpm_task_binary(
name = "docs",
command = "docs",
srcs = glob(["src/**/*"]),
path = "bin/lang-js",
deps = [
"//:node_modules",
],
visibility = ["PUBLIC"],
srcs = {
"//:deno.json": ".",
"//:flake.nix": ".",
"//:flake.lock": ".",
"//:rust-toolchain": ".",
}
)
35 changes: 21 additions & 14 deletions bin/lang-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ This directory contains `lang-js`, the primary executor for functions in SI.

## Local Testing Guide

This is a guide for testing `lang-js` locally by using functions in the [examples](exmaples) directory.
This is a guide for testing `lang-js` locally by using functions in the
[examples](exmaples) directory.

> [!NOTE]
> While [dal integration tests](../../lib/dal/tests/integration.rs) are useful for testing new functions and workflows that leverage `lang-js`, it can be helpful to run `lang-js` directly for an efficient developer feedback loop.
> While [dal integration tests](../../lib/dal/tests/integration.rs) are useful
> for testing new functions and workflows that leverage `lang-js`, it can be
> helpful to run `lang-js` directly for an efficient developer feedback loop.
### 1) Writing the Function

Before writing a payload file, we will want to write a function to be executed.
We can do this anywhere, but for this guide, we will write the file to the [examples](examples) directory.
We can do this anywhere, but for this guide, we will write the file to the
[examples](examples) directory.

> [!TIP]
> You can write the function in JavaScript (`.js`) or TypeScript (`.ts`).
> You can also write an `async` function or a regular, synchronous one.
> You can write the function in JavaScript (`.js`) or TypeScript (`.ts`). You
> can also write an `async` function or a regular, synchronous one.
Here is an example function:

Expand All @@ -28,31 +32,34 @@ function fail() {

### 2) Encoding the Function

With our new function written, we need to "base64 encode" it for the payload file.
You can do that by executing the following:
With our new function written, we need to "base64 encode" it for the payload
file. You can do that by executing the following:

```bash
cat examples/<code-file>.<js-or-ts> | base64 | tr -d '\n'
```

You'll want to copy the result to your clipboard.
On macOS, you can execute the following to do it in one step:
You'll want to copy the result to your clipboard. On macOS, you can execute the
following to do it in one step:

```bash
cat examples/<code-file>.<js-or-ts> | base64 | tr -d '\n' | pbcopy
```

### 3) Preparing the Payload File

With the encoded function in our clipboard, we can create a payload file to send
to `lang-js`. The payload file will be a `json` file in the same directory.

With the encoded function in our clipboard, we can create a payload file to send to `lang-js`.
The payload file will be a `json` file in the same directory.

At the time of writing the guide ([PR #4259](https://github.com/systeminit/si/pull/4259)), the shape the the `json` file has drifted from what it used to be, so developers will need to read the source code to learn its shape.
At the time of writing the guide
([PR #4259](https://github.com/systeminit/si/pull/4259)), the shape the the
`json` file has drifted from what it used to be, so developers will need to read
the source code to learn its shape.

### 4) Running the Function via the Payload File

When we run our function in `lang-js`, let's set the debug flag to see what's going on!
When we run our function in `lang-js`, let's set the debug flag to see what's
going on!

```bash
cat examples/<payload-file>.json | SI_LANG_JS_LOG=* buck2 run :lang-js -- <function-kind>
Expand Down
6 changes: 6 additions & 0 deletions bin/lang-js/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": {
"@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.7",
"@deno/emit": "jsr:@deno/emit@^0.46.0"
}
}
Loading

0 comments on commit 1df917b

Please sign in to comment.