dillonkearns / elm-pages

Hybrid Elm framework with full-stack and static routes.
https://elm-pages.com
BSD 3-Clause "New" or "Revised" License
658 stars 97 forks source link

INTERNAL ERROR when calling custom task. #463

Open miniBill opened 8 months ago

miniBill commented 8 months ago

I'm trying to define a custom task.

// custom-backend-task.js
import child_process from "node:child_process";

/**
 * @param { string[] } args
 * @returns { Promise<number> }
 */
export async function spawn(args) {
    const child = child_process.spawn(args[0], args.splice(1));
    child.on("error", (err) => {
        console.log("Error", err);
    });
    console.log("Pid:", child.pid);
    return child.pid;
}
-- Main.elm
run = [...] (spawn "socat" [...])

spawn : String -> List String -> BackendTask FatalError Int
spawn command args =
    BackendTask.Custom.run "spawn"
        (Json.Encode.list Json.Encode.string (command :: args))
        Json.Decode.int
        |> BackendTask.allowFatal

The output is

Pid: 1462229
BackendTask.Custom.run "spawn": 2.992ms
-- HTTP ERROR --------------- 
BadBody: INTERNAL ERROR - expected requestelm-pages-internal://port

I can see socat running, and I don't know what is wrong that causes the internal error.

miniBill commented 7 months ago

The issue is the splice(1) which mutates args and causes the request to change hash and not be matched when returning.