From 7354176c487c1a19c896d72ce15a26098851d01d Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 16 Oct 2023 06:42:32 +0700 Subject: [PATCH] remove internal api server --- app/srv/api/local-ip.ts | 28 +++++++++++++++++++ app/srv/exports.d.ts | 13 +++++++++ app/srv/exports.ts | 7 +++++ app/web/src/index.tsx | 1 + .../panel/toolbar/center/api/APIConfig.tsx | 4 +-- 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 app/srv/api/local-ip.ts diff --git a/app/srv/api/local-ip.ts b/app/srv/api/local-ip.ts new file mode 100644 index 00000000..8baf9dc5 --- /dev/null +++ b/app/srv/api/local-ip.ts @@ -0,0 +1,28 @@ +import { apiContext } from "service-srv"; +export const _ = { + url: "/local-ip", + async api() { + const { req, res } = apiContext(this); + + const { networkInterfaces } = require("os"); + + const nets = networkInterfaces(); + const results = Object.create(null); // Or just '{}', an empty object + const all = []; + for (const name of Object.keys(nets)) { + for (const net of nets[name]) { + // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses + // 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6 + const familyV4Value = typeof net.family === "string" ? "IPv4" : 4; + if (net.family === familyV4Value && !net.internal) { + if (!results[name]) { + results[name] = []; + } + results[name].push(net.address); + all.push(net.address); + } + } + } + return all as string[]; + }, +}; diff --git a/app/srv/exports.d.ts b/app/srv/exports.d.ts index 9591e1a7..ae3260bb 100644 --- a/app/srv/exports.d.ts +++ b/app/srv/exports.d.ts @@ -57,6 +57,12 @@ declare module "app/srv/api/npm" { api(mode: "site" | "page", id: string): Promise; }; } +declare module "app/srv/api/local-ip" { + export const _: { + url: string; + api(): Promise; + }; +} declare module "app/web/src/utils/types/ws" { export type WS_MSG = WS_MSG_GET_COMP | WS_MSG_SET_COMP | WS_MSG_GET_PAGE | WS_MSG_SET_PAGE | WS_MSG_SV_LOCAL | WS_MSG_SVDIFF_REMOTE | WS_MSG_DIFF_LOCAL | WS_MSG_UNDO | WS_MSG_REDO | WS_MSG_NEW_COMP | WS_SITE_JS | { type: "ping"; @@ -634,6 +640,13 @@ declare module "app/srv/exports" { args: string[]; handler: Promise; }; + export const local_ip: { + name: string; + url: string; + path: string; + args: any[]; + handler: Promise; + }; export const npm_bundle: { name: string; url: string; diff --git a/app/srv/exports.ts b/app/srv/exports.ts index f8c5153d..547cdadc 100644 --- a/app/srv/exports.ts +++ b/app/srv/exports.ts @@ -26,6 +26,13 @@ export const npm = { args: ["mode","id"], handler: import("./api/npm") } +export const local_ip = { + name: "local_ip", + url: "/local-ip", + path: "app/srv/api/local-ip.ts", + args: [], + handler: import("./api/local-ip") +} export const npm_bundle = { name: "npm_bundle", url: "/npm-bundle/:mode/:id", diff --git a/app/web/src/index.tsx b/app/web/src/index.tsx index e069c362..db3018d7 100644 --- a/app/web/src/index.tsx +++ b/app/web/src/index.tsx @@ -17,6 +17,7 @@ const start = async () => { w.db = createDB(base); navigator.serviceWorker.addEventListener("message", (e) => { + console.log("[SW]", e.data); if (e.data.type === "activated") { if (e.data.shouldRefresh && sw) { sw.unregister().then(() => { diff --git a/app/web/src/render/editor/panel/toolbar/center/api/APIConfig.tsx b/app/web/src/render/editor/panel/toolbar/center/api/APIConfig.tsx index c94325b2..f3b85019 100644 --- a/app/web/src/render/editor/panel/toolbar/center/api/APIConfig.tsx +++ b/app/web/src/render/editor/panel/toolbar/center/api/APIConfig.tsx @@ -45,7 +45,7 @@ export const APIConfig: FC<{ > Use Existing API Server -
— OR —
+ {/*
— OR —
{ @@ -69,7 +69,7 @@ export const APIConfig: FC<{ }} > Create New API Server -
+ */} )} {local.creating && (