aboutsummaryrefslogtreecommitdiff
path: root/examples/openInEditor.js
blob: 59282c098ba564d81b27767f131a194c5f90b7e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { resolve } from "path";
import { parse } from "querystring";

export default {
  fetch(req) {
    if (req.url === "/favicon.ico")
      return new Response("nooo dont open favicon in editor", { status: 404 });

    var pathname = req.url.substring(1);
    const q = pathname.indexOf("?");
    var { editor } = parse(pathname.substring(q + 1)) || {};

    if (q > 0) {
      pathname = pathname.substring(0, q);
    }

    Bun.openInEditor(resolve(pathname), {
      editor,
    });

    return new Response(`Opened ${req.url}`);
  },
};