@leonidstern/labit 0.6.3-main.8 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,74 +0,0 @@
1
- // lib/proxy.ts
2
- import http from "node:http";
3
- import net from "node:net";
4
- function proxy({ PROXY, origin: originNow, widget, keyFor, isLoopback, nameFor, dress }) {
5
- const pageKey = (pathname) => keyFor(pathname.replace(/^\/+|\/+$/g, "").replace(/\.html?$/i, "") || "index");
6
- function proxyTo(req, res, url) {
7
- const origin = originNow();
8
- const page = req.method === "GET" && (req.headers["sec-fetch-dest"] === "document" || /text\/html/.test(req.headers.accept || ""));
9
- const headers = { ...req.headers, host: PROXY.host, ...page ? { "accept-encoding": "identity" } : {} };
10
- if (headers.origin) headers.origin = PROXY.origin;
11
- if (headers.referer) headers.referer = PROXY.origin + new URL(String(headers.referer), origin).pathname;
12
- const up = http.request({ hostname: PROXY.hostname, port: PROXY.port || 80, path: url.pathname + url.search, method: req.method, headers }, (r) => {
13
- const encoded = r.headers["content-encoding"] && r.headers["content-encoding"] !== "identity";
14
- const html = /text\/html/i.test(r.headers["content-type"] || "") && req.method === "GET" && !encoded;
15
- if (!html) {
16
- res.writeHead(r.statusCode || 502, r.headers);
17
- return void r.pipe(res);
18
- }
19
- const out = { ...r.headers, "cache-control": "no-store" };
20
- delete out["content-length"];
21
- delete out["content-encoding"];
22
- delete out["content-security-policy"];
23
- res.writeHead(r.statusCode || 502, out);
24
- const strip = (t) => t.replace(/<script\b[^>]*\bsrc=["'][^"']*\/_lab\/comments\.js["'][^>]*>\s*<\/script>\s*/gi, "");
25
- const decoder = new TextDecoder();
26
- let held = "";
27
- let first = true;
28
- r.on("data", (chunk) => {
29
- let t = held + decoder.decode(chunk, { stream: true });
30
- if (first) {
31
- first = false;
32
- t = dress(t);
33
- }
34
- const lower = t.toLowerCase();
35
- const script = lower.lastIndexOf("<script");
36
- const tag = t.lastIndexOf("<");
37
- const cut = script !== -1 && lower.indexOf("</script>", script) === -1 ? script : tag > t.lastIndexOf(">") ? tag : t.length;
38
- res.write(strip(t.slice(0, cut)));
39
- held = t.slice(cut);
40
- });
41
- r.on("end", () => {
42
- const tail = strip(held + decoder.decode()) + widget(pageKey(url.pathname), isLoopback(req), nameFor(req));
43
- res.end(first ? dress(tail) : tail);
44
- });
45
- r.on("error", () => res.destroy());
46
- });
47
- up.on("error", (e) => {
48
- if (res.headersSent) return void res.destroy();
49
- res.writeHead(502, { "content-type": "text/plain; charset=utf-8" }).end(`[lab] no answer from the dev server at ${PROXY.origin} (${e.code}). Start it, then reload.`);
50
- });
51
- req.pipe(up);
52
- }
53
- function upgrade(req, socket, head) {
54
- const up = net.connect(Number(PROXY.port) || 80, PROXY.hostname, () => {
55
- const lines = [];
56
- for (let i = 0; i < req.rawHeaders.length; i += 2) {
57
- const [k, v] = [req.rawHeaders[i], req.rawHeaders[i + 1]];
58
- lines.push(`${k}: ${/^host$/i.test(k) ? PROXY.host : /^origin$/i.test(k) ? PROXY.origin : v}`);
59
- }
60
- up.write(`${req.method} ${req.url} HTTP/1.1\r
61
- ${lines.join("\r\n")}\r
62
- \r
63
- `);
64
- if (head?.length) up.write(head);
65
- socket.pipe(up).pipe(socket);
66
- });
67
- up.on("error", () => socket.destroy());
68
- socket.on("error", () => up.destroy());
69
- }
70
- return { page: proxyTo, upgrade };
71
- }
72
- export {
73
- proxy
74
- };
@@ -1,168 +0,0 @@
1
- import {
2
- FLAGS,
3
- flagDefaults
4
- } from "./chunk-DIR2O7SK.mjs";
5
-
6
- // lib/settings.ts
7
- import fs from "node:fs";
8
- import os from "node:os";
9
- import path from "node:path";
10
- var SHORTCUTS = { toggle: "m", comment: "1", go: "2", pointer: "3", chat: "c", next: "w", pause: "p", done: "d", reply: "r", resolve: "e" };
11
- var SPEC = {
12
- port: { type: "number", def: 4400, min: 1 },
13
- proxy: { type: "string", def: "", only: "project" },
14
- folder: { type: "string", def: ".", only: "project" },
15
- open: { type: "boolean", def: true },
16
- wifi: { type: "boolean", def: false, only: "personal" },
17
- batchSeconds: { type: "number", def: 2, min: 0 },
18
- stopListeningAfterMinutes: { type: "number", def: 60, min: 0 },
19
- remindAfterMinutes: { type: "number", def: 3, min: 0 },
20
- archiveAfterDays: { type: "number", def: 30, min: 1 },
21
- updateCheck: { type: "boolean", def: true },
22
- name: { type: "string", def: "", only: "personal" },
23
- model: { type: "string", def: "", only: "personal" },
24
- shortcuts: { type: "shortcuts", def: SHORTCUTS },
25
- // What is being tried here (lib/flags.ts). Off unless a file or the ⋯ menu turns one on.
26
- flags: { type: "flags", def: flagDefaults() }
27
- };
28
- var ENV = {
29
- port: ["PORT", Number],
30
- batchSeconds: ["LAB_SETTLE_MS", (v) => Number(v) / 1e3],
31
- stopListeningAfterMinutes: ["LAB_CLOSE_GRACE", (v) => Number(v) / 60],
32
- remindAfterMinutes: ["LAB_REMIND_MS", (v) => Number(v) / 6e4],
33
- updateCheck: ["LABIT_NO_UPDATE_CHECK", () => false],
34
- model: ["LAB_MODEL", String]
35
- };
36
- var personalFile = () => path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "labit", "settings.json");
37
- var projectFile = (from) => {
38
- for (let dir = path.resolve(from); ; dir = path.dirname(dir)) {
39
- if (fs.existsSync(path.join(dir, "labit.json"))) return path.join(dir, "labit.json");
40
- if (path.dirname(dir) === dir) return null;
41
- }
42
- };
43
- var read = (file, problems) => {
44
- if (!file || !fs.existsSync(file)) return {};
45
- try {
46
- const v = JSON.parse(fs.readFileSync(file, "utf8"));
47
- if (v && typeof v === "object" && !Array.isArray(v)) return v;
48
- problems.push(`${file}: not an object of settings; ignored`);
49
- } catch (e) {
50
- problems.push(`${file}: not valid JSON (${e.message}); ignored`);
51
- }
52
- return {};
53
- };
54
- var check = (key, v, file, problems) => {
55
- const s = SPEC[key];
56
- const bad = (why) => void problems.push(`${file}: ${key} ${why}; ignored`);
57
- if (s.type === "shortcuts") {
58
- if (!v || typeof v !== "object" || Array.isArray(v)) return bad("must be an object of action \u2192 key");
59
- const out = {};
60
- for (const [action, key1] of Object.entries(v)) {
61
- if (!(action in SHORTCUTS)) bad(`.${action} is not an action (${Object.keys(SHORTCUTS).join(", ")})`);
62
- else if (typeof key1 !== "string" || !/^[a-z0-9]$/i.test(key1)) bad(`.${action} must be one letter or digit`);
63
- else out[action] = key1.toLowerCase();
64
- }
65
- return out;
66
- }
67
- if (s.type === "flags") {
68
- if (!v || typeof v !== "object" || Array.isArray(v)) return bad("must be an object of flag \u2192 true or false");
69
- const out = {};
70
- for (const [flag, on] of Object.entries(v)) {
71
- if (!(flag in FLAGS)) bad(`.${flag} is not something being tried now (${Object.keys(FLAGS).join(", ") || "nothing at the moment"})`);
72
- else if (typeof on !== "boolean") bad(`.${flag} must be true or false`);
73
- else out[flag] = on;
74
- }
75
- return out;
76
- }
77
- if (typeof v !== s.type) return bad(`must be a ${s.type}`);
78
- if (s.type === "number" && (!Number.isFinite(v) || v < (s.min ?? -Infinity))) return bad(`must be a number of at least ${s.min}`);
79
- if (s.type === "string" && key === "folder") return path.resolve(path.dirname(file), v);
80
- return s.type === "string" ? v.trim() : v;
81
- };
82
- function loadSettings(here, flags = {}, env = process.env) {
83
- const problems = [];
84
- const files = { project: projectFile(here), personal: personalFile() };
85
- const layers = [
86
- [files.project || "", read(files.project, problems), "project"],
87
- [files.personal, read(files.personal, problems), "personal"]
88
- ];
89
- const values = {};
90
- const from = {};
91
- for (const [file, data] of layers)
92
- for (const key of Object.keys(data)) if (!(key in SPEC) && key !== "$schema") problems.push(`${file}: ${key} is not a setting; ignored`);
93
- for (const key of Object.keys(SPEC)) {
94
- const s = SPEC[key];
95
- if (flags[key] !== void 0) {
96
- values[key] = flags[key];
97
- from[key] = "flag";
98
- continue;
99
- }
100
- const e = ENV[key];
101
- if (e && env[e[0]] !== void 0 && env[e[0]] !== "") {
102
- values[key] = e[1](env[e[0]]);
103
- from[key] = `env ${e[0]}`;
104
- continue;
105
- }
106
- for (const [file, data, where] of layers) {
107
- if (!(key in data)) continue;
108
- if (s.only && s.only !== where) {
109
- problems.push(`${file}: ${key} can only be set in ${s.only === "personal" ? `your own settings (${files.personal})` : "the project's labit.json"}; ignored`);
110
- continue;
111
- }
112
- const v = check(key, data[key], file, problems);
113
- if (v === void 0) continue;
114
- if (s.type === "shortcuts" || s.type === "flags") {
115
- values[key] = { ...v, ...values[key] };
116
- from[key] = from[key] ? `${from[key]} + ${file}` : file;
117
- continue;
118
- }
119
- values[key] = v;
120
- from[key] = file;
121
- break;
122
- }
123
- if (s.type === "flags" && values[key]) values[key] = { ...flagDefaults(), ...values[key] };
124
- if (s.type === "shortcuts" && values[key]) {
125
- values[key] = { ...SHORTCUTS, ...values[key] };
126
- const taken = Object.entries(values[key]).filter(([, k], i, all) => all.findIndex(([, k2]) => k2 === k) !== i);
127
- for (const [action, k] of taken) problems.push(`shortcuts: "${k}" is used twice (${action} too); the first action keeps it`);
128
- }
129
- if (!(key in values)) {
130
- values[key] = s.def;
131
- from[key] = "default";
132
- }
133
- }
134
- return { values, from, files, problems };
135
- }
136
- var changedShortcuts = (keys) => Object.fromEntries(Object.entries(keys).filter(([a, k]) => SHORTCUTS[a] !== k));
137
- function saveFlag(here, flag, on) {
138
- const file = projectFile(here) || path.join(path.resolve(here), "labit.json");
139
- let data = {};
140
- try {
141
- data = JSON.parse(fs.readFileSync(file, "utf8"));
142
- } catch {
143
- }
144
- const flags = data.flags && typeof data.flags === "object" && !Array.isArray(data.flags) ? data.flags : {};
145
- if (on) flags[flag] = true;
146
- else delete flags[flag];
147
- if (Object.keys(flags).length) data.flags = flags;
148
- else delete data.flags;
149
- fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n");
150
- return file;
151
- }
152
- function savePersonal(key, value) {
153
- const file = personalFile();
154
- let data = {};
155
- try {
156
- data = JSON.parse(fs.readFileSync(file, "utf8"));
157
- } catch {
158
- }
159
- data[key] = value;
160
- fs.mkdirSync(path.dirname(file), { recursive: true });
161
- fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n");
162
- }
163
- export {
164
- changedShortcuts,
165
- loadSettings,
166
- saveFlag,
167
- savePersonal
168
- };