fix try gzip
This commit is contained in:
parent
b0d6362af0
commit
bac25c0790
|
|
@ -9,6 +9,7 @@ import {
|
||||||
WS_MSG_SV_LOCAL,
|
WS_MSG_SV_LOCAL,
|
||||||
} from "../../../../web/src/utils/types/ws";
|
} from "../../../../web/src/utils/types/ws";
|
||||||
import { SingleComp, eg } from "../edit-global";
|
import { SingleComp, eg } from "../edit-global";
|
||||||
|
import { sendWS } from "../send";
|
||||||
export const getComp = async (
|
export const getComp = async (
|
||||||
ws: ServerWebSocket<WSData>,
|
ws: ServerWebSocket<WSData>,
|
||||||
msg: WS_MSG_GET_COMP
|
msg: WS_MSG_GET_COMP
|
||||||
|
|
@ -39,7 +40,7 @@ export const getComp = async (
|
||||||
comp_id: comp_id,
|
comp_id: comp_id,
|
||||||
changes: "",
|
changes: "",
|
||||||
};
|
};
|
||||||
ws.send(JSON.stringify(sent));
|
sendWS(ws, JSON.stringify(sent));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,6 +81,6 @@ export const getComp = async (
|
||||||
comp_id: comp_id,
|
comp_id: comp_id,
|
||||||
changes: Y.encodeStateAsUpdate(comp.doc as any).toString(),
|
changes: Y.encodeStateAsUpdate(comp.doc as any).toString(),
|
||||||
};
|
};
|
||||||
ws.send(JSON.stringify(sent));
|
sendWS(ws, JSON.stringify(sent));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import {
|
||||||
} from "../../../../web/src/utils/types/ws";
|
} from "../../../../web/src/utils/types/ws";
|
||||||
import { eg } from "../edit-global";
|
import { eg } from "../edit-global";
|
||||||
import { loadPage } from "../tools/load-page";
|
import { loadPage } from "../tools/load-page";
|
||||||
|
import { sendWS } from "../send";
|
||||||
|
|
||||||
export const getPage = async (
|
export const getPage = async (
|
||||||
ws: ServerWebSocket<WSData>,
|
ws: ServerWebSocket<WSData>,
|
||||||
|
|
@ -56,5 +57,5 @@ export const getPage = async (
|
||||||
type: "set_page",
|
type: "set_page",
|
||||||
changes: Y.encodeStateAsUpdate(page.doc as any).toString(),
|
changes: Y.encodeStateAsUpdate(page.doc as any).toString(),
|
||||||
};
|
};
|
||||||
ws.send(JSON.stringify(sent));
|
sendWS(ws,JSON.stringify(sent));
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import {
|
||||||
import { eg } from "../edit-global";
|
import { eg } from "../edit-global";
|
||||||
import { getComp } from "./get-comp";
|
import { getComp } from "./get-comp";
|
||||||
import { getPage } from "./get-page";
|
import { getPage } from "./get-page";
|
||||||
|
import { sendWS } from "../send";
|
||||||
|
|
||||||
export const svLocal = async (ws: any, msg: WS_MSG_SV_LOCAL) => {
|
export const svLocal = async (ws: any, msg: WS_MSG_SV_LOCAL) => {
|
||||||
const changes = Uint8Array.from(
|
const changes = Uint8Array.from(
|
||||||
|
|
@ -39,6 +40,6 @@ export const svLocal = async (ws: any, msg: WS_MSG_SV_LOCAL) => {
|
||||||
mode: msg.mode,
|
mode: msg.mode,
|
||||||
type: "svd_remote",
|
type: "svd_remote",
|
||||||
};
|
};
|
||||||
ws.send(JSON.stringify(sendmsg));
|
sendWS(ws,JSON.stringify(sendmsg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import {
|
||||||
WS_MSG_SVDIFF_REMOTE,
|
WS_MSG_SVDIFF_REMOTE,
|
||||||
} from "../../../../web/src/utils/types/ws";
|
} from "../../../../web/src/utils/types/ws";
|
||||||
import { eg } from "../edit-global";
|
import { eg } from "../edit-global";
|
||||||
|
import { sendWS } from "../send";
|
||||||
|
|
||||||
export const svdiffRemote = async (ws: any, msg: WS_MSG_SVDIFF_REMOTE) => {
|
export const svdiffRemote = async (ws: any, msg: WS_MSG_SVDIFF_REMOTE) => {
|
||||||
const sv_remote = Uint8Array.from(
|
const sv_remote = Uint8Array.from(
|
||||||
|
|
@ -32,6 +33,6 @@ export const svdiffRemote = async (ws: any, msg: WS_MSG_SVDIFF_REMOTE) => {
|
||||||
id: msg.id,
|
id: msg.id,
|
||||||
diff_local: diff_local.toString(),
|
diff_local: diff_local.toString(),
|
||||||
};
|
};
|
||||||
ws.send(JSON.stringify(sendmsg));
|
sendWS(ws,JSON.stringify(sendmsg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { ServerWebSocket } from "bun";
|
||||||
|
import { gzip } from "zlib";
|
||||||
|
import { WSData } from "../../../../pkgs/core/server/create";
|
||||||
|
|
||||||
|
export const sendWS = async (ws: ServerWebSocket<WSData>, msg: string) => {
|
||||||
|
gzip(Buffer.from(msg), (err, data) => {
|
||||||
|
if (!err) {
|
||||||
|
ws.sendBinary(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
@ -9,7 +9,7 @@ import { svLocal } from "./edit/action/sv-local";
|
||||||
import { svdiffRemote } from "./edit/action/svdiff-remote";
|
import { svdiffRemote } from "./edit/action/svdiff-remote";
|
||||||
import { redo, undo } from "./edit/action/undo-redo";
|
import { redo, undo } from "./edit/action/undo-redo";
|
||||||
import { eg } from "./edit/edit-global";
|
import { eg } from "./edit/edit-global";
|
||||||
import { syncHandler } from "./sync/sync-handler";
|
import { sendWS } from "./edit/send";import { syncHandler } from "./sync/sync-handler";
|
||||||
|
|
||||||
eg.edit = {
|
eg.edit = {
|
||||||
site: {},
|
site: {},
|
||||||
|
|
@ -35,7 +35,7 @@ export const wsHandler: Record<string, WebSocketHandler<WSData>> = {
|
||||||
const msg = JSON.parse(raw) as WS_MSG;
|
const msg = JSON.parse(raw) as WS_MSG;
|
||||||
|
|
||||||
if (msg.type === "ping") {
|
if (msg.type === "ping") {
|
||||||
ws.send(JSON.stringify({ type: "pong" }));
|
sendWS(ws, JSON.stringify({ type: "pong" }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@
|
||||||
"tinycolor2": "^1.6.0",
|
"tinycolor2": "^1.6.0",
|
||||||
"ua-parser-js": "^1.0.36",
|
"ua-parser-js": "^1.0.36",
|
||||||
"uuid": "9.0.1",
|
"uuid": "9.0.1",
|
||||||
|
"wasm-gzip": "^2.0.3",
|
||||||
"web-utils": "workspace:*",
|
"web-utils": "workspace:*",
|
||||||
"y-pojo": "^0.0.8",
|
"y-pojo": "^0.0.8",
|
||||||
"yjs": "^13.6.8",
|
"yjs": "^13.6.8",
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ export default page({
|
||||||
if (rto) {
|
if (rto) {
|
||||||
navigate(rto);
|
navigate(rto);
|
||||||
} else {
|
} else {
|
||||||
console.log("navigate to");
|
|
||||||
localStorage.setItem("prasi-session", JSON.stringify(s));
|
localStorage.setItem("prasi-session", JSON.stringify(s));
|
||||||
navigate("/editor/");
|
navigate("/editor/");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import {
|
||||||
} from "../../../utils/types/ws";
|
} from "../../../utils/types/ws";
|
||||||
import { scanComponent } from "./comp";
|
import { scanComponent } from "./comp";
|
||||||
import { rebuildTree } from "./tree-logic";
|
import { rebuildTree } from "./tree-logic";
|
||||||
|
import init, { compress, decompress } from "wasm-gzip";
|
||||||
const timeout = {
|
const timeout = {
|
||||||
reconnect: null as any,
|
reconnect: null as any,
|
||||||
svd: null as any,
|
svd: null as any,
|
||||||
|
|
@ -25,6 +25,7 @@ export const editorWS = async (p: PG) => {
|
||||||
if (p.ws && p.ws.readyState === p.ws.OPEN) {
|
if (p.ws && p.ws.readyState === p.ws.OPEN) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
await init();
|
||||||
|
|
||||||
const render = () => {
|
const render = () => {
|
||||||
if (!p.focused && !p.script.active) {
|
if (!p.focused && !p.script.active) {
|
||||||
|
|
@ -98,8 +99,10 @@ export const editorWS = async (p: PG) => {
|
||||||
p.softRender.topR();
|
p.softRender.topR();
|
||||||
}
|
}
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
const decoder = new TextDecoder();
|
||||||
ws.addEventListener("message", async (e) => {
|
ws.addEventListener("message", async (e) => {
|
||||||
const msg = JSON.parse(e.data) as WS_MSG;
|
const raw = decoder.decode(decompress(e.data));
|
||||||
|
const msg = JSON.parse(raw) as WS_MSG;
|
||||||
|
|
||||||
if (msg.type === "pong") {
|
if (msg.type === "pong") {
|
||||||
p.wsPing = Date.now() - p.wsPingTs;
|
p.wsPing = Date.now() - p.wsPingTs;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ g.api = {};
|
||||||
g.mode = process.argv.includes("dev") ? "dev" : "prod";
|
g.mode = process.argv.includes("dev") ? "dev" : "prod";
|
||||||
g.datadir = g.mode == "prod" ? "../data" : "data";
|
g.datadir = g.mode == "prod" ? "../data" : "data";
|
||||||
g.port = parseInt(process.env.PORT || "4550");
|
g.port = parseInt(process.env.PORT || "4550");
|
||||||
|
|
||||||
|
|
||||||
g.log.info(g.mode === "dev" ? "DEVELOPMENT" : "PRODUCTION");
|
g.log.info(g.mode === "dev" ? "DEVELOPMENT" : "PRODUCTION");
|
||||||
if (g.mode === "dev") {
|
if (g.mode === "dev") {
|
||||||
await startDevWatcher();
|
await startDevWatcher();
|
||||||
|
|
@ -30,9 +32,18 @@ if (g.db) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
await parcelBuild();
|
await parcelBuild();
|
||||||
|
|
||||||
await generateAPIFrm();
|
await generateAPIFrm();
|
||||||
await prepareApiRoutes();
|
await prepareApiRoutes();
|
||||||
|
|
||||||
|
// Bun.serve({
|
||||||
|
// port: g.port,
|
||||||
|
// async fetch(req, server) {
|
||||||
|
// return new Response("test. sabar. ya....");
|
||||||
|
// },
|
||||||
|
// });
|
||||||
await createServer();
|
await createServer();
|
||||||
await prepareAPITypes();
|
await prepareAPITypes();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,9 @@ export const createServer = async () => {
|
||||||
websocket: {
|
websocket: {
|
||||||
maxPayloadLength: 9999999,
|
maxPayloadLength: 9999999,
|
||||||
closeOnBackpressureLimit: true,
|
closeOnBackpressureLimit: true,
|
||||||
|
drain(ws) {
|
||||||
|
console.log("Backpressure relieved...");
|
||||||
|
},
|
||||||
close(ws, code, reason) {
|
close(ws, code, reason) {
|
||||||
const pathname = ws.data.url.pathname;
|
const pathname = ws.data.url.pathname;
|
||||||
if (wsHandler[pathname]) {
|
if (wsHandler[pathname]) {
|
||||||
|
|
@ -61,63 +64,75 @@ export const createServer = async () => {
|
||||||
async fetch(req, server) {
|
async fetch(req, server) {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
|
|
||||||
if (wsHandler[url.pathname]) {
|
const response = async () => {
|
||||||
if (
|
if (wsHandler[url.pathname]) {
|
||||||
server.upgrade(req, {
|
if (
|
||||||
data: {
|
server.upgrade(req, {
|
||||||
url: new URL(req.url),
|
data: {
|
||||||
},
|
url: new URL(req.url),
|
||||||
})
|
},
|
||||||
) {
|
})
|
||||||
return;
|
) {
|
||||||
}
|
return;
|
||||||
return new Response("Upgrade failed :(", { status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const api = await serveAPI(url, req);
|
|
||||||
if (api) {
|
|
||||||
return api;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
g.log.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
const webPath = "app/static";
|
|
||||||
try {
|
|
||||||
const found = cache.static[url.pathname];
|
|
||||||
if (found && g.mode === "prod") {
|
|
||||||
return responseCached(req, found);
|
|
||||||
}
|
|
||||||
|
|
||||||
const file = Bun.file(dir.path(`${webPath}${url.pathname}`));
|
|
||||||
if ((await file.exists()) && file.type !== "application/octet-stream") {
|
|
||||||
if (g.mode === "dev") {
|
|
||||||
return new Response(file as any);
|
|
||||||
}
|
}
|
||||||
|
return new Response("Upgrade failed :(", { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
if (!cache.static[url.pathname]) {
|
try {
|
||||||
cache.static[url.pathname] = {
|
const api = await serveAPI(url, req);
|
||||||
type: lookup(url.pathname) || "text/plain",
|
if (api) {
|
||||||
content: await file.arrayBuffer(),
|
return api;
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
g.log.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
const webPath = "app/static";
|
||||||
|
try {
|
||||||
const found = cache.static[url.pathname];
|
const found = cache.static[url.pathname];
|
||||||
if (found) {
|
if (found && g.mode === "prod") {
|
||||||
return responseCached(req, found);
|
return responseCached(req, found);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const file = Bun.file(dir.path(`${webPath}${url.pathname}`));
|
||||||
|
if (
|
||||||
|
(await file.exists()) &&
|
||||||
|
file.type !== "application/octet-stream"
|
||||||
|
) {
|
||||||
|
if (g.mode === "dev") {
|
||||||
|
return new Response(file as any);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cache.static[url.pathname]) {
|
||||||
|
cache.static[url.pathname] = {
|
||||||
|
type: lookup(url.pathname) || "text/plain",
|
||||||
|
content: await file.arrayBuffer(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const found = cache.static[url.pathname];
|
||||||
|
if (found) {
|
||||||
|
return responseCached(req, found);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
g.log.error(e);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
g.log.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new Response(Bun.file(dir.path(`${webPath}/index.html`)) as any);
|
return new Response(
|
||||||
} catch (e) {
|
Bun.file(dir.path(`${webPath}/index.html`)) as any
|
||||||
g.log.error(e);
|
);
|
||||||
|
} catch (e) {
|
||||||
|
g.log.error(e);
|
||||||
|
|
||||||
return new Response("Loading...");
|
return new Response("Loading...");
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
console.log("[START]", req.url);
|
||||||
|
console.time(req.url);
|
||||||
|
const res = await response();
|
||||||
|
console.timeEnd(req.url);
|
||||||
|
return res;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue