├── .github └── workflows │ └── ci.yml ├── LICENSE ├── README.md ├── mod.ts ├── test.ts └── test_server.ts /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v1 11 | - uses: denolib/setup-deno@v1.1.0 12 | with: 13 | deno-version: 0.x 14 | - run: deno fetch test_server.ts 15 | - run: deno run -A test.ts 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 EnokMan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deno-deamon 2 | 3 | [![Build Status](https://github.com/manyuanrong/deno-deamon/workflows/CI/badge.svg)](https://github.com/manyuanrong/deno-deamon/actions) 4 | 5 | Make the Deno program run in the background 6 | 7 | Example 8 | 9 | ```ts 10 | import deamon from "https://raw.githubusercontent.com/manyuanrong/deno-deamon/master/mod.ts" 11 | 12 | // before your program 13 | deamon() // run in background 14 | 15 | // other program... 16 | ``` -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | export default function deamon(): number { 2 | if (!Deno.env("__deamon")) { 3 | const args = [...Deno.args]; 4 | const script = "./" + args.shift(); 5 | const env = { ...Deno.env(), __deamon: "true" }; 6 | Deno.run({ 7 | args: [Deno.execPath(), "run", "--allow-all", script, ...args], 8 | env, 9 | stdout: "null", 10 | stdin: "null", 11 | stderr: "null" 12 | }); 13 | Deno.exit(); 14 | } else { 15 | return Deno.pid; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test.ts: -------------------------------------------------------------------------------- 1 | import { assertEquals } from "https://deno.land/std@v0.24.0/testing/asserts.ts"; 2 | import { runTests, test } from "https://deno.land/std@v0.24.0/testing/mod.ts"; 3 | 4 | test("http server run in background", async function() { 5 | Deno.run({ 6 | args: ["deno", "run", "-A", "test_server.ts"] 7 | }); 8 | 9 | await new Promise(resolve => setTimeout(resolve, 2000)); 10 | 11 | const res = await fetch("http://127.0.0.1:8000"); 12 | const { message, pid } = await res.json(); 13 | assertEquals(message, "Hello World"); 14 | 15 | Deno.kill(pid, 9); 16 | }); 17 | 18 | runTests(); 19 | -------------------------------------------------------------------------------- /test_server.ts: -------------------------------------------------------------------------------- 1 | import { serve } from "https://deno.land/std@v0.24.0/http/server.ts"; 2 | import deamon from "./mod.ts"; 3 | 4 | const pid = deamon(); 5 | 6 | const body = new TextEncoder().encode( 7 | `${JSON.stringify({ 8 | message: "Hello World", 9 | pid 10 | })}` 11 | ); 12 | const s = serve({ port: 8000 }); 13 | 14 | for await (const req of s) { 15 | req.respond({ body }); 16 | } 17 | --------------------------------------------------------------------------------