Skip to content

Commit 74112d7

Browse files
committed
simplify worker logic
1 parent c4dc9b5 commit 74112d7

File tree

4 files changed

+177
-146
lines changed

4 files changed

+177
-146
lines changed

internal/cmd/run.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"os/exec"
78
"path/filepath"
89

10+
"github.com/mattn/go-isatty"
911
"github.com/pomdtr/smallweb/internal/api"
1012
"github.com/pomdtr/smallweb/internal/app"
1113
"github.com/pomdtr/smallweb/internal/worker"
@@ -26,7 +28,6 @@ func NewCmdRun() *cobra.Command {
2628
return nil
2729
},
2830
RunE: func(cmd *cobra.Command, args []string) error {
29-
3031
var appConfig app.Config
3132
if err := conf.Unmarshal(fmt.Sprintf("apps.%s", args[0]), &appConfig); err != nil {
3233
fmt.Fprintf(cmd.ErrOrStderr(), "failed to get app config: %v\n", err)
@@ -42,11 +43,17 @@ func NewCmdRun() *cobra.Command {
4243

4344
cmd.SilenceErrors = true
4445

45-
if err := wk.Run(cmd.Context(), worker.RunParams{
46+
params := worker.RunParams{
4647
Args: args[1:],
4748
Stdout: cmd.OutOrStdout(),
4849
Stderr: cmd.ErrOrStderr(),
49-
}); err != nil {
50+
}
51+
52+
if !isatty.IsTerminal(os.Stdin.Fd()) {
53+
params.Stdin = cmd.InOrStdin()
54+
}
55+
56+
if err := wk.Run(cmd.Context(), params); err != nil {
5057
var exitErr *exec.ExitError
5158
if errors.As(err, &exitErr) {
5259
return ExitError{exitErr.ExitCode()}

internal/cmd/up.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/tls"
67
"errors"
@@ -240,7 +241,7 @@ func NewCmdUp() *cobra.Command {
240241
}
241242

242243
worker := worker.NewWorker(a, api.NewHandler(conf))
243-
if err := worker.SendEmail(context.Background(), data); err != nil {
244+
if err := worker.SendEmail(context.Background(), bytes.NewReader(data)); err != nil {
244245
logger.Error("failed to send email", "error", err)
245246
continue
246247
}

internal/worker/sandbox.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { accepts } from "jsr:@std/http@1.0.12/negotiation"
22
import { escape } from "jsr:@std/html@1.0.3"
3-
import { decodeBase64 } from "jsr:@std/encoding@1.0.8/base64"
43

54
function cleanStack(str?: string) {
65
if (!str) return undefined;
@@ -78,7 +77,27 @@ function respondWithError(request: Request, error: Error) {
7877
);
7978
}
8079

81-
const payload = JSON.parse(Deno.args[0]);
80+
const { SMALLWEB_SOCKET_PATH } = Deno.env.toObject();
81+
82+
if (!SMALLWEB_SOCKET_PATH) {
83+
console.error("SMALLWEB_SOCKET is not set.");
84+
Deno.exit(1);
85+
}
86+
87+
const client = Deno.createHttpClient({
88+
proxy: {
89+
transport: "unix",
90+
path: SMALLWEB_SOCKET_PATH,
91+
}
92+
});
93+
94+
const resp = await fetch("http://worker.localhost/payload", { client })
95+
if (!resp.ok) {
96+
console.error("Could not get worker payload:", resp.statusText);
97+
Deno.exit(1);
98+
}
99+
100+
const payload = await resp.json();
82101

83102
if (!payload || !payload.command) {
84103
console.error("Invalid input.");
@@ -89,10 +108,9 @@ if (payload.command === "fetch") {
89108
Deno.serve(
90109
{
91110
port: parseInt(payload.port),
92-
onListen: () => {
93-
// This line will signal that the server is ready to the go
94-
console.error("READY");
95-
},
111+
onListen: async () => {
112+
await fetch("http://worker.localhost/ready", { client });
113+
}
96114
},
97115
async (req) => {
98116
try {
@@ -162,7 +180,13 @@ if (payload.command === "fetch") {
162180
Deno.exit(1);
163181
}
164182

165-
await handler.run(payload.args);
183+
const resp = await fetch("http://worker.localhost/command/stdin", { client });
184+
if (!resp.ok) {
185+
console.error("Could not get command input:", resp.statusText);
186+
Deno.exit(1);
187+
}
188+
189+
await handler.run(payload.args, await resp.body);
166190
} else if (payload.command === "email") {
167191
const mod = await import(payload.entrypoint);
168192
if (!mod.default || typeof mod.default !== "object") {
@@ -178,9 +202,13 @@ if (payload.command === "fetch") {
178202
Deno.exit(1);
179203
}
180204

181-
const data = decodeBase64(payload.msg)
182-
const blob = new Blob([data]);
183-
await handler.email(blob.stream());
205+
const resp = await fetch("http://worker.localhost/email/msg", { client });
206+
if (!resp.ok) {
207+
console.error("Could not get email message:", resp.statusText);
208+
Deno.exit(1);
209+
}
210+
211+
await handler.email(await resp.body);
184212
} else {
185213
console.error("Unknown command: ", payload.command);
186214
Deno.exit(1);

0 commit comments

Comments
 (0)