this repo has no description smallweb.run
smallweb
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

add sendEmail function in worker in sandbox

pomdtr 554b1237 953b3e68

+73 -6
+8
examples/email/main.ts
··· 1 + import PostalMime from 'npm:postal-mime'; 2 + 3 + export default { 4 + async email(input: ReadableStream<Uint8Array>) { 5 + const email = await PostalMime.parse(input); 6 + await Deno.writeTextFile("data/email.json", JSON.stringify(email, null, 4)); 7 + } 8 + }
+26 -6
worker/sandbox.ts
··· 80 80 const input = JSON.parse(Deno.args[0]); 81 81 82 82 if (input.command === "fetch") { 83 - const { entrypoint, port } = input; 84 83 Deno.serve( 85 84 { 86 - port: parseInt(port), 85 + port: parseInt(input.port), 87 86 onListen: () => { 88 87 // This line will signal that the server is ready to the go 89 88 console.error("READY"); ··· 91 90 }, 92 91 async (req) => { 93 92 try { 94 - const mod = await import(entrypoint); 93 + const mod = await import(input.entrypoint); 95 94 if (!mod.default) { 96 95 return new Response("The app does not provide a default export.", { status: 500 }); 97 96 } ··· 136 135 }, 137 136 ); 138 137 } else if (input.command === "run") { 139 - const { entrypoint, args } = input; 140 - const mod = await import(entrypoint); 138 + const mod = await import(input.entrypoint); 141 139 if (!mod.default || typeof mod.default !== "object") { 142 140 console.error( 143 141 "The mod does not provide an object as it's default export.", ··· 156 154 Deno.exit(1); 157 155 } 158 156 159 - await handler.run(args); 157 + await handler.run(input.args); 158 + } else if (input.command == "email") { 159 + const { entrypoint } = input; 160 + const mod = await import(entrypoint); 161 + if (!mod.default || typeof mod.default !== "object") { 162 + console.error( 163 + "The mod does not provide an object as it's default export.", 164 + ); 165 + Deno.exit(1); 166 + } 167 + 168 + const handler = mod.default; 169 + if (!("email" in handler)) { 170 + console.error("The mod default export does not have a email function."); 171 + Deno.exit(1); 172 + } 173 + 174 + if (!(typeof handler.email === "function")) { 175 + console.error("The mod default export email property must be a function."); 176 + Deno.exit(1); 177 + } 178 + 179 + await handler.email(Deno.stdin.readable); 160 180 } else { 161 181 console.error("Unknown command"); 162 182 Deno.exit(1);
+39
worker/worker.go
··· 512 512 return command, nil 513 513 } 514 514 515 + func (me *Worker) SendEmail(ctx context.Context, message io.Reader) error { 516 + deno, err := DenoExecutable() 517 + if err != nil { 518 + return fmt.Errorf("could not find deno executable") 519 + } 520 + 521 + denoArgs := []string{"run"} 522 + denoArgs = append(denoArgs, me.DenoArgs(me.App, deno)...) 523 + 524 + input := strings.Builder{} 525 + encoder := json.NewEncoder(&input) 526 + encoder.SetEscapeHTML(false) 527 + if err := encoder.Encode(map[string]any{ 528 + "command": "email", 529 + "entrypoint": me.App.Entrypoint(), 530 + }); err != nil { 531 + return fmt.Errorf("could not encode input: %w", err) 532 + } 533 + 534 + denoArgs = append(denoArgs, sandboxPath, input.String()) 535 + 536 + command := exec.CommandContext(ctx, deno, denoArgs...) 537 + command.Dir = me.App.Root() 538 + command.Stderr = os.Stderr 539 + command.Env = commandEnv(me.App, me.RootDir, me.Domain) 540 + 541 + stdin, err := command.StdinPipe() 542 + if err != nil { 543 + return fmt.Errorf("could not get stdin pipe: %w", err) 544 + } 545 + 546 + go func() { 547 + defer stdin.Close() 548 + io.Copy(stdin, message) 549 + }() 550 + 551 + return command.Run() 552 + } 553 + 515 554 // GetFreePort asks the kernel for a free open port that is ready to use. 516 555 func GetFreePort() (int, error) { 517 556 addr, err := net.ResolveTCPAddr("tcp", "localhost:0")