-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeno-bridge-echo.ts
68 lines (60 loc) · 1.58 KB
/
deno-bridge-echo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { DenoBridge } from "https://deno.land/x/[email protected]/mod.ts";
import { serve } from "https://deno.land/std/http/server.ts";
const bridge = new DenoBridge(
Deno.args[0],
Deno.args[1],
Deno.args[2],
messageDispatcher
);
// 存放数据用的
const data: {
currentTab: {
url?: string;
title?: string;
};
} = { currentTab: {} };
async function messageDispatcher(message: string) {
const [funcName, funcArgs] = JSON.parse(message)[1];
if (funcName === "getCurrentTab") {
const { currentTab } = data;
if (!currentTab.url) {
bridge.messageToEmacs("No Valid CurrentTab");
return;
}
bridge.evalInEmacs(
`(insert "[[${data.currentTab.url}][ ${currentTab.title}]]")`
);
}
}
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
if (url.pathname === "/v1/echo") {
const payload = await req.json();
data.currentTab = {
url: payload.url,
title: payload.title,
};
return new Response(JSON.stringify({ message: "already memorize" }), {
status: 200,
headers: {
"content-type": "application/json; charset=utf-8",
},
});
}
if (url.pathname === "/v1/show") {
return new Response(JSON.stringify(data.currentTab), {
status: 200,
headers: {
"content-type": "application/json; charset=utf-8",
},
});
}
const body = JSON.stringify({ message: "NOT FOUND" });
return new Response(body, {
status: 404,
headers: {
"content-type": "application/json; charset=utf-8",
},
});
}
serve(handler, { port: 8000 });