@limno/core 0.1.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.
- package/LICENSE.md +110 -0
- package/dist/index.cjs +543 -0
- package/dist/index.d.cts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +494 -0
- package/dist/loader.cjs +133 -0
- package/package.json +54 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, MIT Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-MIT
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Damilola Adewale
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the MIT license that is effective on the second anniversary of the date we make
|
|
91
|
+
the Software available. On or after that date, you may use the Software under
|
|
92
|
+
the MIT license, in which case the following will apply:
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
95
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
96
|
+
the Software without restriction, including without limitation the rights to
|
|
97
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
98
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
99
|
+
so, subject to the following conditions:
|
|
100
|
+
|
|
101
|
+
The above copyright notice and this permission notice shall be included in all
|
|
102
|
+
copies or substantial portions of the Software.
|
|
103
|
+
|
|
104
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
105
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
106
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
107
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
108
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
109
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
110
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ROUTE: () => ROUTE,
|
|
34
|
+
SOURCE_FILES: () => SOURCE_FILES,
|
|
35
|
+
TEMPLATE_FILES: () => TEMPLATE_FILES,
|
|
36
|
+
createLimnoService: () => createLimnoService,
|
|
37
|
+
overlayFile: () => overlayFile,
|
|
38
|
+
shouldStamp: () => shouldStamp,
|
|
39
|
+
stampSource: () => stampSource,
|
|
40
|
+
stampTemplate: () => stampTemplate,
|
|
41
|
+
startServer: () => startServer
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(index_exports);
|
|
44
|
+
|
|
45
|
+
// ../../node_modules/tsup/assets/cjs_shims.js
|
|
46
|
+
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
|
|
47
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
48
|
+
|
|
49
|
+
// src/transform.ts
|
|
50
|
+
var import_parser = require("@babel/parser");
|
|
51
|
+
var import_traverse = __toESM(require("@babel/traverse"), 1);
|
|
52
|
+
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
53
|
+
var traverse = import_traverse.default.default ?? import_traverse.default;
|
|
54
|
+
var SKIP_COMPOSITES = /* @__PURE__ */ new Set(["Fragment", "React.Fragment", "Suspense", "React.Suspense", "StrictMode", "React.StrictMode", "Profiler", "React.Profiler", "Activity"]);
|
|
55
|
+
function jsxName(name) {
|
|
56
|
+
if (name.type === "JSXIdentifier") return name.name;
|
|
57
|
+
if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
|
|
58
|
+
return `${jsxName(name.object)}.${name.property.name}`;
|
|
59
|
+
}
|
|
60
|
+
var isCapitalized = (s) => /^[A-Z]/.test(s);
|
|
61
|
+
function componentName(path3, fileBase) {
|
|
62
|
+
let p = path3.getFunctionParent();
|
|
63
|
+
while (p) {
|
|
64
|
+
const node = p.node;
|
|
65
|
+
let name;
|
|
66
|
+
if ((node.type === "FunctionDeclaration" || node.type === "FunctionExpression") && node.id) name = node.id.name;
|
|
67
|
+
if (!name) {
|
|
68
|
+
let parent = p.parentPath;
|
|
69
|
+
while (parent && parent.node.type === "CallExpression") parent = parent.parentPath;
|
|
70
|
+
if (parent?.node.type === "VariableDeclarator" && parent.node.id.type === "Identifier") name = parent.node.id.name;
|
|
71
|
+
else if (parent?.node.type === "ExportDefaultDeclaration") name = fileBase;
|
|
72
|
+
else if (parent?.node.type === "ClassMethod" || parent?.node.type === "ObjectMethod") name = void 0;
|
|
73
|
+
}
|
|
74
|
+
if (!name && p.node.type === "ClassMethod") {
|
|
75
|
+
const cls = p.findParent((q) => q.isClassDeclaration());
|
|
76
|
+
name = cls?.node.id?.name;
|
|
77
|
+
}
|
|
78
|
+
if (name && isCapitalized(name)) return name;
|
|
79
|
+
p = p.parentPath ? p.parentPath.getFunctionParent() : null;
|
|
80
|
+
}
|
|
81
|
+
return void 0;
|
|
82
|
+
}
|
|
83
|
+
function hasAttr(node, name) {
|
|
84
|
+
return node.attributes.some((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === name);
|
|
85
|
+
}
|
|
86
|
+
var escapeAttr = (s) => s.replace(/&/g, "&").replace(/"/g, """);
|
|
87
|
+
function stampSource(code, id, relFile) {
|
|
88
|
+
if (!code.includes("<")) return null;
|
|
89
|
+
const isTs = /\.tsx$/.test(id);
|
|
90
|
+
let ast;
|
|
91
|
+
try {
|
|
92
|
+
ast = (0, import_parser.parse)(code, {
|
|
93
|
+
sourceType: "module",
|
|
94
|
+
plugins: isTs ? ["jsx", "typescript"] : ["jsx"],
|
|
95
|
+
errorRecovery: true
|
|
96
|
+
});
|
|
97
|
+
} catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
const s = new import_magic_string.default(code);
|
|
101
|
+
const fileBase = relFile.split("/").pop().replace(/\.[jt]sx?$/, "").replace(/^./, (c) => c.toUpperCase());
|
|
102
|
+
let touched = false;
|
|
103
|
+
traverse(ast, {
|
|
104
|
+
JSXOpeningElement(path3) {
|
|
105
|
+
const node = path3.node;
|
|
106
|
+
const loc = node.loc;
|
|
107
|
+
if (!loc || node.name.end == null) return;
|
|
108
|
+
const name = jsxName(node.name);
|
|
109
|
+
const where = `${relFile}:${loc.start.line}:${loc.start.column + 1}`;
|
|
110
|
+
const host = node.name.type === "JSXIdentifier" && !isCapitalized(name);
|
|
111
|
+
if (host) {
|
|
112
|
+
if (hasAttr(node, "data-limno-src")) return;
|
|
113
|
+
const comp = componentName(path3, fileBase);
|
|
114
|
+
s.appendLeft(node.name.end, ` data-limno-src="${escapeAttr(where)}"${comp ? ` data-limno-comp="${escapeAttr(comp)}"` : ""}`);
|
|
115
|
+
touched = true;
|
|
116
|
+
} else {
|
|
117
|
+
if (SKIP_COMPOSITES.has(name) || /\.(Provider|Consumer)$/.test(name) || hasAttr(node, "data-limno-callsite")) return;
|
|
118
|
+
s.appendLeft(node.name.end, ` data-limno-callsite="${escapeAttr(where)}"`);
|
|
119
|
+
touched = true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
if (!touched) return null;
|
|
124
|
+
return { code: s.toString(), map: s.generateMap({ hires: "boundary", source: id, includeContent: true }) };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/templates.ts
|
|
128
|
+
var import_node_module = require("module");
|
|
129
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
130
|
+
var import_node_url = require("url");
|
|
131
|
+
var import_magic_string2 = __toESM(require("magic-string"), 1);
|
|
132
|
+
var TEMPLATE_FILES = /\.(vue|svelte)$/;
|
|
133
|
+
var SKIP = /* @__PURE__ */ new Set(["script", "style", "template", "slot", "noscript"]);
|
|
134
|
+
var escapeAttr2 = (s) => s.replace(/&/g, "&").replace(/"/g, """);
|
|
135
|
+
function lineStarts(code) {
|
|
136
|
+
const starts = [0];
|
|
137
|
+
for (let i = 0; i < code.length; i++) if (code.charCodeAt(i) === 10) starts.push(i + 1);
|
|
138
|
+
return starts;
|
|
139
|
+
}
|
|
140
|
+
function position(starts, offset) {
|
|
141
|
+
let lo = 0;
|
|
142
|
+
let hi = starts.length - 1;
|
|
143
|
+
while (lo < hi) {
|
|
144
|
+
const mid = lo + hi + 1 >> 1;
|
|
145
|
+
if (starts[mid] <= offset) lo = mid;
|
|
146
|
+
else hi = mid - 1;
|
|
147
|
+
}
|
|
148
|
+
return { line: lo + 1, column: offset - starts[lo] + 1 };
|
|
149
|
+
}
|
|
150
|
+
async function load(root, specifiers) {
|
|
151
|
+
for (const from of [import_node_path.default.join(root, "package.json"), importMetaUrl]) {
|
|
152
|
+
const req = (0, import_node_module.createRequire)(from);
|
|
153
|
+
for (const spec of specifiers) {
|
|
154
|
+
try {
|
|
155
|
+
const mod = await import((0, import_node_url.pathToFileURL)(req.resolve(spec)).href);
|
|
156
|
+
return mod.default && typeof mod.default === "object" ? { ...mod.default, ...mod } : mod;
|
|
157
|
+
} catch {
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
async function vueTags(code, root) {
|
|
164
|
+
const sfc = await load(root, ["vue/compiler-sfc", "@vue/compiler-sfc"]);
|
|
165
|
+
if (!sfc) return null;
|
|
166
|
+
const template = sfc.parse(code).descriptor.template;
|
|
167
|
+
if (!template?.ast || template.lang && template.lang !== "html") return null;
|
|
168
|
+
const tags = [];
|
|
169
|
+
const walk = (n) => {
|
|
170
|
+
if (n.type === 1 && n.tagType === 0 && n.tag && !SKIP.has(n.tag)) {
|
|
171
|
+
tags.push({ start: n.loc.start.offset, at: n.loc.start.offset + 1 + n.tag.length });
|
|
172
|
+
}
|
|
173
|
+
n.children?.forEach(walk);
|
|
174
|
+
n.branches?.forEach(walk);
|
|
175
|
+
};
|
|
176
|
+
template.ast.children?.forEach(walk);
|
|
177
|
+
return tags;
|
|
178
|
+
}
|
|
179
|
+
async function svelteTags(code, root) {
|
|
180
|
+
const compiler = await load(root, ["svelte/compiler"]);
|
|
181
|
+
if (!compiler?.parse) return null;
|
|
182
|
+
let ast;
|
|
183
|
+
try {
|
|
184
|
+
ast = compiler.parse(code, { modern: true });
|
|
185
|
+
} catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const tags = [];
|
|
189
|
+
const seen = /* @__PURE__ */ new Set();
|
|
190
|
+
const walk = (n) => {
|
|
191
|
+
if (!n || typeof n !== "object" || seen.has(n)) return;
|
|
192
|
+
seen.add(n);
|
|
193
|
+
const node = n;
|
|
194
|
+
if ((node.type === "RegularElement" || node.type === "Element") && node.name && node.start != null && !SKIP.has(node.name) && !node.name.includes(":")) {
|
|
195
|
+
tags.push({ start: node.start, at: node.start + 1 + node.name.length });
|
|
196
|
+
}
|
|
197
|
+
for (const [k, v] of Object.entries(node)) if (k !== "parent" && k !== "metadata") walk(v);
|
|
198
|
+
};
|
|
199
|
+
walk(ast.fragment ?? ast.html);
|
|
200
|
+
return tags;
|
|
201
|
+
}
|
|
202
|
+
async function stampTemplate(code, id, relFile, root) {
|
|
203
|
+
const ext = import_node_path.default.extname(id);
|
|
204
|
+
let tags = null;
|
|
205
|
+
try {
|
|
206
|
+
if (ext === ".vue") tags = await vueTags(code, root);
|
|
207
|
+
else if (ext === ".svelte") tags = await svelteTags(code, root);
|
|
208
|
+
} catch {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
if (!tags?.length) return null;
|
|
212
|
+
const component = escapeAttr2(import_node_path.default.basename(id, ext).replace(/^./, (c) => c.toUpperCase()));
|
|
213
|
+
const starts = lineStarts(code);
|
|
214
|
+
const s = new import_magic_string2.default(code);
|
|
215
|
+
for (const t of tags) {
|
|
216
|
+
const { line, column } = position(starts, t.start);
|
|
217
|
+
if (code.slice(t.at, t.at + 200).split(">")[0].includes("data-limno-src")) continue;
|
|
218
|
+
s.appendLeft(t.at, ` data-limno-src="${escapeAttr2(`${relFile}:${line}:${column}`)}" data-limno-comp="${component}"`);
|
|
219
|
+
}
|
|
220
|
+
return { code: s.toString(), map: s.generateMap({ hires: "boundary", source: id, includeContent: true }) };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/service.ts
|
|
224
|
+
var import_node_crypto = require("crypto");
|
|
225
|
+
var import_node_fs = require("fs");
|
|
226
|
+
var import_node_module2 = require("module");
|
|
227
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
228
|
+
var import_spec = require("@limno/spec");
|
|
229
|
+
var ROUTE = "/__limno";
|
|
230
|
+
var require2 = (0, import_node_module2.createRequire)(importMetaUrl);
|
|
231
|
+
var STALE_MS = 3e4;
|
|
232
|
+
function readJson(file) {
|
|
233
|
+
try {
|
|
234
|
+
return JSON.parse((0, import_node_fs.readFileSync)(file, "utf8"));
|
|
235
|
+
} catch {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function writeAtomic(file, data) {
|
|
240
|
+
(0, import_node_fs.mkdirSync)(import_node_path2.default.dirname(file), { recursive: true });
|
|
241
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
242
|
+
(0, import_node_fs.writeFileSync)(tmp, data);
|
|
243
|
+
(0, import_node_fs.renameSync)(tmp, file);
|
|
244
|
+
}
|
|
245
|
+
function body(req) {
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
let data = "";
|
|
248
|
+
req.on("data", (c) => data += c);
|
|
249
|
+
req.on("end", () => resolve(data));
|
|
250
|
+
req.on("error", reject);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
function send(res, status, payload, type = "application/json") {
|
|
254
|
+
res.statusCode = status;
|
|
255
|
+
res.setHeader("Content-Type", type);
|
|
256
|
+
res.setHeader("Cache-Control", "no-store");
|
|
257
|
+
res.end(typeof payload === "string" ? payload : JSON.stringify(payload));
|
|
258
|
+
}
|
|
259
|
+
var hash = (s) => (0, import_node_crypto.createHash)("sha1").update(s).digest("hex");
|
|
260
|
+
function overlayFile() {
|
|
261
|
+
return require2.resolve("@limno/overlay/dist/overlay.js");
|
|
262
|
+
}
|
|
263
|
+
function createLimnoService({ root, dir = ".limno" }) {
|
|
264
|
+
const folder = import_node_path2.default.join(root, dir);
|
|
265
|
+
const file = import_node_path2.default.join(folder, "changeset.json");
|
|
266
|
+
const presenceFile = import_node_path2.default.join(folder, "agent.json");
|
|
267
|
+
let lastWrittenHash = "";
|
|
268
|
+
const clients = /* @__PURE__ */ new Set();
|
|
269
|
+
function broadcast(event, data) {
|
|
270
|
+
const msg = `event: ${event}
|
|
271
|
+
data: ${JSON.stringify(data)}
|
|
272
|
+
|
|
273
|
+
`;
|
|
274
|
+
for (const c of clients) c.write(msg);
|
|
275
|
+
}
|
|
276
|
+
function setupInfo() {
|
|
277
|
+
let cli = null;
|
|
278
|
+
for (const from of [import_node_path2.default.join(root, "package.json"), importMetaUrl]) {
|
|
279
|
+
try {
|
|
280
|
+
cli = import_node_path2.default.join(import_node_path2.default.dirname((0, import_node_module2.createRequire)(from).resolve("@limno/mcp/package.json")), "dist", "cli.js");
|
|
281
|
+
if ((0, import_node_fs.existsSync)(cli)) break;
|
|
282
|
+
cli = null;
|
|
283
|
+
} catch {
|
|
284
|
+
cli = null;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return cli ? { command: "node", args: [cli, "--root", root], root } : { command: "npx", args: ["-y", "@limno/mcp", "--root", root], root };
|
|
288
|
+
}
|
|
289
|
+
function agentStatus() {
|
|
290
|
+
const presence = readJson(presenceFile);
|
|
291
|
+
let alive = !!presence?.pid;
|
|
292
|
+
if (alive) {
|
|
293
|
+
try {
|
|
294
|
+
process.kill(presence.pid, 0);
|
|
295
|
+
} catch {
|
|
296
|
+
alive = false;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const connected = alive && Date.now() - (presence?.lastSeen ?? 0) < STALE_MS;
|
|
300
|
+
return { connected, client: connected ? presence?.client ?? null : null, lastSeen: presence?.lastSeen ?? null, setup: setupInfo() };
|
|
301
|
+
}
|
|
302
|
+
function installConfig(target) {
|
|
303
|
+
const dest = target === "claude" ? import_node_path2.default.join(root, ".mcp.json") : import_node_path2.default.join(root, ".cursor", "mcp.json");
|
|
304
|
+
let config = {};
|
|
305
|
+
if ((0, import_node_fs.existsSync)(dest)) {
|
|
306
|
+
try {
|
|
307
|
+
config = JSON.parse((0, import_node_fs.readFileSync)(dest, "utf8"));
|
|
308
|
+
} catch {
|
|
309
|
+
throw new Error(`${import_node_path2.default.relative(root, dest)} isn't valid JSON; fix or remove it first.`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const { command, args } = setupInfo();
|
|
313
|
+
const portable = args.map((a, i) => {
|
|
314
|
+
if (args[i - 1] === "--root") return ".";
|
|
315
|
+
return import_node_path2.default.isAbsolute(a) ? import_node_path2.default.relative(root, a).split(import_node_path2.default.sep).join("/") : a;
|
|
316
|
+
});
|
|
317
|
+
config.mcpServers = { ...config.mcpServers ?? {}, limno: { command, args: portable } };
|
|
318
|
+
writeAtomic(dest, JSON.stringify(config, null, 2) + "\n");
|
|
319
|
+
return import_node_path2.default.relative(root, dest);
|
|
320
|
+
}
|
|
321
|
+
let lastAgent = "";
|
|
322
|
+
function pushAgent() {
|
|
323
|
+
const status = agentStatus();
|
|
324
|
+
const key = JSON.stringify([status.connected, status.client]);
|
|
325
|
+
if (key === lastAgent) return;
|
|
326
|
+
lastAgent = key;
|
|
327
|
+
broadcast("agent", status);
|
|
328
|
+
}
|
|
329
|
+
function onChangeSetFile() {
|
|
330
|
+
const raw = (0, import_node_fs.existsSync)(file) ? (0, import_node_fs.readFileSync)(file, "utf8") : "";
|
|
331
|
+
if (!raw || hash(raw) === lastWrittenHash) return;
|
|
332
|
+
const set = readJson(file);
|
|
333
|
+
if (set) broadcast("changeset", set);
|
|
334
|
+
}
|
|
335
|
+
(0, import_node_fs.mkdirSync)(folder, { recursive: true });
|
|
336
|
+
let watcher = null;
|
|
337
|
+
let debounce;
|
|
338
|
+
try {
|
|
339
|
+
watcher = (0, import_node_fs.watch)(folder, (_event, name) => {
|
|
340
|
+
if (!name || name.endsWith(".tmp")) return;
|
|
341
|
+
clearTimeout(debounce);
|
|
342
|
+
debounce = setTimeout(() => {
|
|
343
|
+
if (name === "changeset.json") onChangeSetFile();
|
|
344
|
+
if (name === "agent.json") pushAgent();
|
|
345
|
+
}, 40);
|
|
346
|
+
});
|
|
347
|
+
watcher.unref?.();
|
|
348
|
+
} catch {
|
|
349
|
+
}
|
|
350
|
+
const presenceTimer = setInterval(pushAgent, 1e4);
|
|
351
|
+
presenceTimer.unref();
|
|
352
|
+
const keepAlive = setInterval(() => {
|
|
353
|
+
for (const c of clients) c.write(": ping\n\n");
|
|
354
|
+
}, 25e3);
|
|
355
|
+
keepAlive.unref();
|
|
356
|
+
async function openInEditor(spec) {
|
|
357
|
+
const m = /^(.*?)(?::(\d+))?(?::(\d+))?$/.exec(spec);
|
|
358
|
+
if (!m) return false;
|
|
359
|
+
const target = import_node_path2.default.resolve(root, m[1]);
|
|
360
|
+
if (!target.startsWith(root + import_node_path2.default.sep) || !(0, import_node_fs.existsSync)(target)) return false;
|
|
361
|
+
const { default: launch } = await import("launch-editor");
|
|
362
|
+
launch(`${target}${m[2] ? `:${m[2]}` : ""}${m[3] ? `:${m[3]}` : ""}`);
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
async function handle(req, res, next) {
|
|
366
|
+
const [url, query = ""] = (req.url ?? "").split("?");
|
|
367
|
+
if (!url.startsWith(ROUTE)) return next ? next() : send(res, 404, { error: "not found" });
|
|
368
|
+
try {
|
|
369
|
+
if (url === `${ROUTE}/overlay.js`) {
|
|
370
|
+
return send(res, 200, (0, import_node_fs.readFileSync)(overlayFile(), "utf8"), "text/javascript");
|
|
371
|
+
}
|
|
372
|
+
if (url === `${ROUTE}/events`) {
|
|
373
|
+
res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-store", Connection: "keep-alive" });
|
|
374
|
+
res.write(": connected\n\n");
|
|
375
|
+
clients.add(res);
|
|
376
|
+
req.on("close", () => clients.delete(res));
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (url === `${ROUTE}/changeset` && req.method === "GET") {
|
|
380
|
+
return send(res, 200, readJson(file) ?? (0, import_spec.emptyChangeSet)());
|
|
381
|
+
}
|
|
382
|
+
if (url === `${ROUTE}/changeset` && req.method === "PUT") {
|
|
383
|
+
const incoming = JSON.parse(await body(req));
|
|
384
|
+
const merged = (0, import_spec.mergeStatuses)(incoming, readJson(file));
|
|
385
|
+
merged.updatedAt = Date.now();
|
|
386
|
+
const raw = JSON.stringify(merged, null, 2);
|
|
387
|
+
lastWrittenHash = hash(raw);
|
|
388
|
+
writeAtomic(file, raw);
|
|
389
|
+
return send(res, 200, merged);
|
|
390
|
+
}
|
|
391
|
+
if (url === `${ROUTE}/prompt` && req.method === "GET") {
|
|
392
|
+
const set = readJson(file) ?? (0, import_spec.emptyChangeSet)();
|
|
393
|
+
return send(res, 200, (0, import_spec.renderPrompt)(set, { mcp: false, statuses: ["draft", "queued", "in_progress", "differs", "failed"] }), "text/markdown; charset=utf-8");
|
|
394
|
+
}
|
|
395
|
+
if (url === `${ROUTE}/agent` && req.method === "GET") {
|
|
396
|
+
return send(res, 200, agentStatus());
|
|
397
|
+
}
|
|
398
|
+
if (url === `${ROUTE}/agent/install` && req.method === "POST") {
|
|
399
|
+
const { target } = JSON.parse(await body(req));
|
|
400
|
+
if (target !== "claude" && target !== "cursor") return send(res, 400, { error: "unknown target" });
|
|
401
|
+
try {
|
|
402
|
+
return send(res, 200, { written: installConfig(target) });
|
|
403
|
+
} catch (e) {
|
|
404
|
+
return send(res, 409, { error: e instanceof Error ? e.message : String(e) });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (url === `${ROUTE}/open`) {
|
|
408
|
+
const spec = new URLSearchParams(query).get("file") ?? "";
|
|
409
|
+
return send(res, await openInEditor(spec) ? 200 : 404, { ok: true });
|
|
410
|
+
}
|
|
411
|
+
if (url === `${ROUTE}/info`) {
|
|
412
|
+
return send(res, 200, { name: "limno", root, file: import_node_path2.default.relative(root, file) });
|
|
413
|
+
}
|
|
414
|
+
send(res, 404, { error: "not found" });
|
|
415
|
+
} catch (err) {
|
|
416
|
+
send(res, 500, { error: String(err) });
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return {
|
|
420
|
+
root,
|
|
421
|
+
handle: (req, res, next) => void handle(req, res, next),
|
|
422
|
+
close() {
|
|
423
|
+
watcher?.close();
|
|
424
|
+
clearInterval(presenceTimer);
|
|
425
|
+
clearInterval(keepAlive);
|
|
426
|
+
for (const c of clients) c.end();
|
|
427
|
+
clients.clear();
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/server.ts
|
|
433
|
+
var import_node_crypto2 = require("crypto");
|
|
434
|
+
var import_node_http = __toESM(require("http"), 1);
|
|
435
|
+
var LOOPBACK = /^(localhost|127\.0\.0\.1|\[::1\]|[\w-]+\.localhost)(:\d+)?$/i;
|
|
436
|
+
function allowedOrigin(origin) {
|
|
437
|
+
if (!origin) return null;
|
|
438
|
+
try {
|
|
439
|
+
const u = new URL(origin);
|
|
440
|
+
return (u.protocol === "http:" || u.protocol === "https:") && LOOPBACK.test(u.host) ? origin : null;
|
|
441
|
+
} catch {
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
function preferredPort(root) {
|
|
446
|
+
return 41e3 + parseInt((0, import_node_crypto2.createHash)("sha1").update(root).digest("hex").slice(0, 6), 16) % 900;
|
|
447
|
+
}
|
|
448
|
+
async function ours(port, root) {
|
|
449
|
+
try {
|
|
450
|
+
const res = await fetch(`http://127.0.0.1:${port}${ROUTE}/info`, { signal: AbortSignal.timeout(800) });
|
|
451
|
+
const info = await res.json();
|
|
452
|
+
return info.name === "limno" && info.root === root;
|
|
453
|
+
} catch {
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
var running = /* @__PURE__ */ new Map();
|
|
458
|
+
function startServer(options) {
|
|
459
|
+
const key = `${options.root}|${options.dir ?? ""}`;
|
|
460
|
+
let p = running.get(key);
|
|
461
|
+
if (!p) {
|
|
462
|
+
p = listen(options);
|
|
463
|
+
running.set(key, p);
|
|
464
|
+
}
|
|
465
|
+
return p;
|
|
466
|
+
}
|
|
467
|
+
async function listen(options) {
|
|
468
|
+
const service = createLimnoService(options);
|
|
469
|
+
const server = import_node_http.default.createServer((req, res) => {
|
|
470
|
+
if (!LOOPBACK.test(req.headers.host ?? "")) {
|
|
471
|
+
res.statusCode = 403;
|
|
472
|
+
return res.end();
|
|
473
|
+
}
|
|
474
|
+
const origin = allowedOrigin(req.headers.origin);
|
|
475
|
+
if (req.headers.origin && !origin) {
|
|
476
|
+
res.statusCode = 403;
|
|
477
|
+
return res.end();
|
|
478
|
+
}
|
|
479
|
+
if (origin) {
|
|
480
|
+
res.setHeader("Access-Control-Allow-Origin", origin);
|
|
481
|
+
res.setHeader("Vary", "Origin");
|
|
482
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");
|
|
483
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
484
|
+
if (req.headers["access-control-request-private-network"]) res.setHeader("Access-Control-Allow-Private-Network", "true");
|
|
485
|
+
}
|
|
486
|
+
if (req.method === "OPTIONS") {
|
|
487
|
+
res.statusCode = 204;
|
|
488
|
+
return res.end();
|
|
489
|
+
}
|
|
490
|
+
service.handle(req, res);
|
|
491
|
+
});
|
|
492
|
+
server.unref();
|
|
493
|
+
const start = preferredPort(options.root);
|
|
494
|
+
for (let port = start; port < start + 40; port++) {
|
|
495
|
+
const outcome = await new Promise((resolve, reject) => {
|
|
496
|
+
const onError = (err) => {
|
|
497
|
+
server.off("listening", onListening);
|
|
498
|
+
if (err.code === "EADDRINUSE") resolve("busy");
|
|
499
|
+
else reject(err);
|
|
500
|
+
};
|
|
501
|
+
const onListening = () => {
|
|
502
|
+
server.off("error", onError);
|
|
503
|
+
resolve("ok");
|
|
504
|
+
};
|
|
505
|
+
server.once("error", onError);
|
|
506
|
+
server.once("listening", onListening);
|
|
507
|
+
server.listen(port, "127.0.0.1");
|
|
508
|
+
});
|
|
509
|
+
if (outcome === "ok") {
|
|
510
|
+
return {
|
|
511
|
+
url: `http://127.0.0.1:${port}`,
|
|
512
|
+
close: () => {
|
|
513
|
+
service.close();
|
|
514
|
+
server.close();
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
if (await ours(port, options.root)) {
|
|
519
|
+
service.close();
|
|
520
|
+
return { url: `http://127.0.0.1:${port}`, close: () => void 0 };
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
service.close();
|
|
524
|
+
throw new Error("Limno: no free local port for its dev server.");
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// src/files.ts
|
|
528
|
+
var SOURCE_FILES = /\.(jsx|tsx|js|mjs)$/;
|
|
529
|
+
function shouldStamp(file, include = SOURCE_FILES) {
|
|
530
|
+
return include.test(file) && !/[\\/]node_modules[\\/]/.test(file) && !file.startsWith("\0");
|
|
531
|
+
}
|
|
532
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
533
|
+
0 && (module.exports = {
|
|
534
|
+
ROUTE,
|
|
535
|
+
SOURCE_FILES,
|
|
536
|
+
TEMPLATE_FILES,
|
|
537
|
+
createLimnoService,
|
|
538
|
+
overlayFile,
|
|
539
|
+
shouldStamp,
|
|
540
|
+
stampSource,
|
|
541
|
+
stampTemplate,
|
|
542
|
+
startServer
|
|
543
|
+
});
|