@codua/codeoutline-win32-x64 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/DISTRIBUTION.md +98 -0
- package/LICENSE +24 -0
- package/README.md +132 -0
- package/README.zh-CN.md +132 -0
- package/THIRD_PARTY.md +32 -0
- package/build-info.json +1 -0
- package/codeoutline +4 -0
- package/codeoutline.cmd +3 -0
- package/licenses/libdeflate.txt +22 -0
- package/licenses/lua-cmsgpack.txt +22 -0
- package/licenses/luajit.txt +56 -0
- package/licenses/mbedtls.txt +553 -0
- package/licenses/rpmalloc.txt +12 -0
- package/licenses/xnet2lua.txt +24 -0
- package/licenses/yyjson.txt +21 -0
- package/package.json +1 -0
- package/scripts/codeoutline/cli.lua +32 -0
- package/scripts/codeoutline/command.lua +155 -0
- package/scripts/codeoutline/common.lua +124 -0
- package/scripts/codeoutline/control.lua +4 -0
- package/scripts/codeoutline/explore.lua +500 -0
- package/scripts/codeoutline/graph.lua +380 -0
- package/scripts/codeoutline/http.lua +156 -0
- package/scripts/codeoutline/index.lua +404 -0
- package/scripts/codeoutline/index_worker.lua +43 -0
- package/scripts/codeoutline/lang/c.lua +362 -0
- package/scripts/codeoutline/lang/cpp.lua +539 -0
- package/scripts/codeoutline/lang/csharp.lua +334 -0
- package/scripts/codeoutline/lang/go.lua +233 -0
- package/scripts/codeoutline/lang/java.lua +274 -0
- package/scripts/codeoutline/lang/js.lua +466 -0
- package/scripts/codeoutline/lang/lua.lua +218 -0
- package/scripts/codeoutline/lang/python.lua +184 -0
- package/scripts/codeoutline/lang/rust.lua +276 -0
- package/scripts/codeoutline/main.lua +104 -0
- package/scripts/codeoutline/mcp.lua +188 -0
- package/scripts/codeoutline/parse.lua +49 -0
- package/scripts/codeoutline/path.lua +36 -0
- package/scripts/codeoutline/service.lua +135 -0
- package/scripts/codeoutline/text.lua +43 -0
- package/scripts/codeoutline/version.lua +1 -0
- package/scripts/codeoutline/xscan.lua +625 -0
- package/xnet2lua/bin/xnet.exe +0 -0
- package/xnet2lua/scripts/core/share/xhttp_codec.lua +835 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
-- graph.lua — cross-file resolution over an index: global symbol tables,
|
|
2
|
+
-- file dependencies (#include / import), and call edges between nodes.
|
|
3
|
+
--
|
|
4
|
+
-- local G = graph.build(idx)
|
|
5
|
+
-- G.nodes[id] -> { f = file index, node = <node table>, id = id }
|
|
6
|
+
-- G.files[f] -> file record (path, language, nodes, refs, imports)
|
|
7
|
+
-- G.by_name[name] -> { id, ... }
|
|
8
|
+
-- G.out[id], G.inn[id] -> { {id = other, line = call line, kind = ref kind}, ... }
|
|
9
|
+
--
|
|
10
|
+
-- Resolution is name-based with locality scoring, the same model CodeGraph
|
|
11
|
+
-- uses for dynamic languages: a call binds to the best-placed definition of
|
|
12
|
+
-- that name (same file > included/imported file > paired source of an
|
|
13
|
+
-- included header > same directory), never across languages. Ties keep up
|
|
14
|
+
-- to MAX_TIES targets instead of guessing.
|
|
15
|
+
|
|
16
|
+
local control = require('codeoutline.control')
|
|
17
|
+
local M = {}
|
|
18
|
+
|
|
19
|
+
M.MAX_TIES = 3
|
|
20
|
+
M.MAX_CANDIDATES = 200
|
|
21
|
+
|
|
22
|
+
-- Node kinds a call / `new` / bare reference may bind to.
|
|
23
|
+
local CALLABLE = {
|
|
24
|
+
['function'] = true, method = true, macro = true, constructor = true, prototype = true,
|
|
25
|
+
class = true, record = true,
|
|
26
|
+
}
|
|
27
|
+
local NEWABLE = { class = true, record = true, constructor = true, enum = true }
|
|
28
|
+
local VALUE = { ['function'] = true, method = true }
|
|
29
|
+
|
|
30
|
+
-- C and C++ call each other (extern "C", shared headers); others don't mix.
|
|
31
|
+
local C_FAMILY = { c = true, cpp = true }
|
|
32
|
+
local HEADER_EXT = { h = true, hpp = true, hh = true, hxx = true }
|
|
33
|
+
local SOURCE_EXT = { c = true, cc = true, cpp = true, cxx = true }
|
|
34
|
+
local FAMILY = { c = 'c', cpp = 'c', javascript = 'js', typescript = 'js' }
|
|
35
|
+
local function family(lang) return FAMILY[lang] or lang end
|
|
36
|
+
|
|
37
|
+
local function dirname(p) return p:match('^(.*)/[^/]*$') or '' end
|
|
38
|
+
local function basename(p) return p:match('([^/]*)$') end
|
|
39
|
+
local function stem(p) return basename(p):match('^(.*)%.[^.]*$') or basename(p) end
|
|
40
|
+
|
|
41
|
+
local function push(t, k, v)
|
|
42
|
+
local list = t[k]
|
|
43
|
+
if not list then list = {}; t[k] = list end
|
|
44
|
+
list[#list + 1] = v
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
-- Files a file depends on, resolved from its #include / import records.
|
|
48
|
+
local function resolve_deps(G, f)
|
|
49
|
+
local rec = G.files[f]
|
|
50
|
+
local deps = {}
|
|
51
|
+
local function add(g) if g and g ~= f then deps[g] = true end end
|
|
52
|
+
local here = dirname(rec.path)
|
|
53
|
+
for _, imp in ipairs(rec.imports) do
|
|
54
|
+
local path = imp.path
|
|
55
|
+
if C_FAMILY[rec.language] then
|
|
56
|
+
-- "dir/x.h": files whose path ends with it, preferring the closest
|
|
57
|
+
for _, g in ipairs(G.by_base[basename(path)] or {}) do
|
|
58
|
+
local p = G.files[g].path
|
|
59
|
+
if p == path or p:sub(-(#path + 1)) == '/' .. path then add(g) end
|
|
60
|
+
end
|
|
61
|
+
elseif rec.language == 'python' then
|
|
62
|
+
local rel = path
|
|
63
|
+
local base = here
|
|
64
|
+
local dots = rel:match('^(%.+)')
|
|
65
|
+
if dots then
|
|
66
|
+
for _ = 2, #dots do base = dirname(base) end
|
|
67
|
+
rel = rel:sub(#dots + 1)
|
|
68
|
+
end
|
|
69
|
+
local sub = rel:gsub('%.', '/')
|
|
70
|
+
local cands = {}
|
|
71
|
+
if dots then
|
|
72
|
+
local b = base ~= '' and (base .. '/') or ''
|
|
73
|
+
cands = { b .. sub .. '.py', b .. sub .. '/__init__.py' }
|
|
74
|
+
-- `from . import x` / `from .pkg import x`: the names may be modules
|
|
75
|
+
for _, name in ipairs(imp.names or {}) do
|
|
76
|
+
local s2 = (sub ~= '' and (sub .. '/') or '') .. name
|
|
77
|
+
cands[#cands + 1] = b .. s2 .. '.py'
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
for _, c in ipairs(cands) do add(G.file_index[c]) end
|
|
81
|
+
if not dots and sub ~= '' then
|
|
82
|
+
-- absolute import: match by path suffix anywhere in the tree
|
|
83
|
+
for _, g in ipairs(G.by_base[basename(sub) .. '.py'] or {}) do
|
|
84
|
+
local p = G.files[g].path
|
|
85
|
+
if p == sub .. '.py' or p:sub(-(#sub + 4)) == '/' .. sub .. '.py' then add(g) end
|
|
86
|
+
end
|
|
87
|
+
for _, g in ipairs(G.by_base['__init__.py'] or {}) do
|
|
88
|
+
local p = G.files[g].path
|
|
89
|
+
local want = sub .. '/__init__.py'
|
|
90
|
+
if p == want or p:sub(-(#want + 1)) == '/' .. want then add(g) end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
elseif rec.language == 'lua' then
|
|
94
|
+
-- require 'a.b' -> a/b.lua or a/b/init.lua anywhere under the root
|
|
95
|
+
-- (package.path prefixes like scripts/?.lua vary per project);
|
|
96
|
+
-- dofile/loadfile take a path.
|
|
97
|
+
local wants
|
|
98
|
+
if imp.kind == 'require' then
|
|
99
|
+
local sub = path:gsub('%.', '/')
|
|
100
|
+
wants = { sub .. '.lua', sub .. '/init.lua' }
|
|
101
|
+
else
|
|
102
|
+
wants = { (path:gsub('\\', '/'):gsub('^%./', '')) }
|
|
103
|
+
end
|
|
104
|
+
for _, want in ipairs(wants) do
|
|
105
|
+
for _, g in ipairs(G.by_base[basename(want)] or {}) do
|
|
106
|
+
local p = G.files[g].path
|
|
107
|
+
if p == want or p:sub(-(#want + 1)) == '/' .. want then add(g) end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
elseif rec.language == 'go' then
|
|
111
|
+
-- import "github.com/org/repo/pkg/sub": the package is the directory
|
|
112
|
+
-- whose path ends with the longest matching suffix of the import
|
|
113
|
+
local segs = {}
|
|
114
|
+
for s in path:gmatch('[^/]+') do segs[#segs + 1] = s end
|
|
115
|
+
for k = 1, #segs do
|
|
116
|
+
local suffix = table.concat(segs, '/', k)
|
|
117
|
+
local hit = false
|
|
118
|
+
for _, d in ipairs(G.dir_by_base[segs[#segs]] or {}) do
|
|
119
|
+
if d == suffix or d:sub(-(#suffix + 1)) == '/' .. suffix then
|
|
120
|
+
for _, g in ipairs(G.dir_files[d]) do add(g) end
|
|
121
|
+
hit = true
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
if hit then break end
|
|
125
|
+
end
|
|
126
|
+
elseif rec.language == 'javascript' or rec.language == 'typescript' then
|
|
127
|
+
-- relative specifiers only; bare package names live in node_modules
|
|
128
|
+
if path:sub(1, 1) == '.' then
|
|
129
|
+
local base = here
|
|
130
|
+
local rel = path
|
|
131
|
+
while true do
|
|
132
|
+
if rel:sub(1, 2) == './' then rel = rel:sub(3)
|
|
133
|
+
elseif rel:sub(1, 3) == '../' then base = dirname(base); rel = rel:sub(4)
|
|
134
|
+
else break end
|
|
135
|
+
end
|
|
136
|
+
local stemp = (base ~= '' and (base .. '/') or '') .. rel
|
|
137
|
+
stemp = stemp:gsub('%.[mc]?js$', '') -- TS sources imported as ./x.js
|
|
138
|
+
for _, ext in ipairs({ '', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.mts',
|
|
139
|
+
'/index.ts', '/index.tsx', '/index.js', '/index.mjs' }) do
|
|
140
|
+
add(G.file_index[stemp .. ext])
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
elseif rec.language == 'csharp' then
|
|
144
|
+
for _, g in ipairs(G.ns_files[path] or {}) do add(g) end
|
|
145
|
+
elseif rec.language == 'rust' then
|
|
146
|
+
if imp.kind == 'mod' then
|
|
147
|
+
-- mod x; -> x.rs / x/mod.rs next to this file (or under its stem dir)
|
|
148
|
+
local st = stem(rec.path)
|
|
149
|
+
local bases = { here }
|
|
150
|
+
if st ~= 'mod' and st ~= 'lib' and st ~= 'main' then bases[2] = (here ~= '' and here .. '/' or '') .. st end
|
|
151
|
+
for _, b in ipairs(bases) do
|
|
152
|
+
local pre = b ~= '' and (b .. '/') or ''
|
|
153
|
+
add(G.file_index[pre .. path .. '.rs'])
|
|
154
|
+
add(G.file_index[pre .. path .. '/mod.rs'])
|
|
155
|
+
end
|
|
156
|
+
else
|
|
157
|
+
-- use crate::a::b::{C, D}: the module file for the longest path prefix
|
|
158
|
+
local segs = {}
|
|
159
|
+
for s in path:gsub('{.*$', ''):gmatch('[%w_]+') do
|
|
160
|
+
if s ~= 'crate' and s ~= 'self' and s ~= 'super' and s ~= 'pub' then segs[#segs + 1] = s end
|
|
161
|
+
end
|
|
162
|
+
for k = #segs, 1, -1 do
|
|
163
|
+
local sub = table.concat(segs, '/', 1, k)
|
|
164
|
+
local found = false
|
|
165
|
+
for _, want in ipairs({ sub .. '.rs', sub .. '/mod.rs' }) do
|
|
166
|
+
for _, g in ipairs(G.by_base[basename(want)] or {}) do
|
|
167
|
+
local p = G.files[g].path
|
|
168
|
+
if p == want or p:sub(-(#want + 1)) == '/' .. want then add(g); found = true end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
if found then break end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
elseif rec.language == 'java' then
|
|
175
|
+
if path:sub(-2) == '.*' then
|
|
176
|
+
for _, g in ipairs(G.pkg_files[path:sub(1, -3)] or {}) do add(g) end
|
|
177
|
+
else
|
|
178
|
+
for _, id in ipairs(G.by_qualified[path] or {}) do add(G.nodes[id].f) end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
-- Java: the whole package is visible without imports; Go: the whole
|
|
183
|
+
-- directory is one package; C#: the file's own namespaces.
|
|
184
|
+
if rec.language == 'java' and rec.package then
|
|
185
|
+
for _, g in ipairs(G.pkg_files[rec.package] or {}) do add(g) end
|
|
186
|
+
elseif rec.language == 'go' then
|
|
187
|
+
for _, g in ipairs(G.dir_files[here] or {}) do add(g) end
|
|
188
|
+
elseif rec.language == 'csharp' then
|
|
189
|
+
for _, node in ipairs(rec.nodes) do
|
|
190
|
+
if node.kind == 'namespace' then
|
|
191
|
+
for _, g in ipairs(G.ns_files[node.qualified] or {}) do add(g) end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
-- C: x.c usually implements what x.h declares, so a file that includes
|
|
196
|
+
-- x.h also "depends on" x.c for resolution purposes.
|
|
197
|
+
local paired = {}
|
|
198
|
+
if C_FAMILY[rec.language] then
|
|
199
|
+
for g in pairs(deps) do
|
|
200
|
+
local p = G.files[g].path
|
|
201
|
+
if HEADER_EXT[p:match('%.(%w+)$') or ''] then
|
|
202
|
+
for _, h in ipairs(G.by_stem[stem(p)] or {}) do
|
|
203
|
+
if h ~= f and SOURCE_EXT[G.files[h].path:match('%.(%w+)$') or ''] then paired[h] = true end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
return deps, paired
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
-- Innermost class-like container of a node (for self./this. calls).
|
|
212
|
+
local function owner_class(G, id)
|
|
213
|
+
local n = G.nodes[id]
|
|
214
|
+
local rec = G.files[n.f]
|
|
215
|
+
local p = n.node.parent
|
|
216
|
+
while p do
|
|
217
|
+
local pn = rec.nodes[p]
|
|
218
|
+
if pn.kind == 'class' or pn.kind == 'record' or pn.kind == 'interface' or pn.kind == 'enum'
|
|
219
|
+
or pn.kind == 'struct' or pn.kind == 'union' then
|
|
220
|
+
return G.id_of[n.f][p]
|
|
221
|
+
end
|
|
222
|
+
p = pn.parent
|
|
223
|
+
end
|
|
224
|
+
return nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
local function score(G, f, src_id, ref, cid, deps, paired)
|
|
228
|
+
local c = G.nodes[cid]
|
|
229
|
+
local cf = c.f
|
|
230
|
+
local crec, frec = G.files[cf], G.files[f]
|
|
231
|
+
if family(crec.language) ~= family(frec.language) then return nil end
|
|
232
|
+
local s = 0
|
|
233
|
+
if cf == f then
|
|
234
|
+
s = s + 100
|
|
235
|
+
elseif c.node.static then
|
|
236
|
+
return nil -- C static: invisible outside its file
|
|
237
|
+
elseif deps[cf] then
|
|
238
|
+
s = s + 50
|
|
239
|
+
elseif paired[cf] then
|
|
240
|
+
s = s + 40
|
|
241
|
+
elseif dirname(crec.path) == dirname(frec.path) then
|
|
242
|
+
s = s + 20
|
|
243
|
+
end
|
|
244
|
+
local kind = c.node.kind
|
|
245
|
+
if kind == 'prototype' then s = s - 3 end
|
|
246
|
+
if ref.kind == 'member_call' and (ref.recv == 'self' or ref.recv == 'this' or ref.recv == 'cls') and src_id then
|
|
247
|
+
local owner = owner_class(G, src_id)
|
|
248
|
+
if owner and G.nodes[cid].node.parent and G.id_of[cf][G.nodes[cid].node.parent] == owner then
|
|
249
|
+
s = s + 80
|
|
250
|
+
elseif kind ~= 'method' then
|
|
251
|
+
s = s - 20
|
|
252
|
+
end
|
|
253
|
+
elseif ref.kind == 'member_call' and s <= 0 then
|
|
254
|
+
-- obj.name() on an unknown receiver: binding it to any same-named
|
|
255
|
+
-- method project-wide (str.format -> some format()) is mostly wrong,
|
|
256
|
+
-- so only local candidates qualify.
|
|
257
|
+
return nil
|
|
258
|
+
elseif ref.kind == 'call' and frec.language == 'java' and src_id then
|
|
259
|
+
-- unqualified call inside a class: implicit this
|
|
260
|
+
local owner = owner_class(G, src_id)
|
|
261
|
+
if owner and c.node.parent and G.id_of[cf][c.node.parent] == owner then s = s + 30 end
|
|
262
|
+
end
|
|
263
|
+
return s
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
local function resolve_ref(G, f, src_id, ref, deps, paired)
|
|
267
|
+
if ref.kind == 'annotation' then return nil end
|
|
268
|
+
local accept = CALLABLE
|
|
269
|
+
if ref.kind == 'new' then accept = NEWABLE elseif ref.kind == 'ref' then accept = VALUE end
|
|
270
|
+
local cands = G.by_name[ref.name]
|
|
271
|
+
if not cands then return nil end
|
|
272
|
+
-- Score first: score() rejects other languages and statics of other
|
|
273
|
+
-- files, so "is there a real definition?" is only asked among the
|
|
274
|
+
-- candidates this call could actually reach. (Asking it over all
|
|
275
|
+
-- same-named nodes let a Python `def work` hide the C prototype of
|
|
276
|
+
-- `work`, after which the Python def was filtered out too.)
|
|
277
|
+
local function is_decl(node) return node.kind == 'prototype' or node.decl end
|
|
278
|
+
local scored, has_def = {}, false
|
|
279
|
+
for i = 1, math.min(#cands, M.MAX_CANDIDATES) do
|
|
280
|
+
local cid = cands[i]
|
|
281
|
+
local node = G.nodes[cid].node
|
|
282
|
+
if cid ~= src_id and accept[node.kind] then
|
|
283
|
+
local s = score(G, f, src_id, ref, cid, deps, paired)
|
|
284
|
+
if s then
|
|
285
|
+
scored[#scored + 1] = { cid, s }
|
|
286
|
+
if not is_decl(node) then has_def = true end
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
-- Prefer definitions: drop prototypes / in-class declarations when a
|
|
291
|
+
-- reachable definition exists.
|
|
292
|
+
local best, ties = nil, {}
|
|
293
|
+
for _, c in ipairs(scored) do
|
|
294
|
+
local cid, s = c[1], c[2]
|
|
295
|
+
if not (has_def and is_decl(G.nodes[cid].node)) then
|
|
296
|
+
if not best or s > best then
|
|
297
|
+
best, ties = s, { cid }
|
|
298
|
+
elseif s == best and #ties < M.MAX_TIES then
|
|
299
|
+
ties[#ties + 1] = cid
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
-- A lone candidate far away (score 0) is still a match for a unique
|
|
304
|
+
-- name; many far candidates with nothing to separate them is a guess.
|
|
305
|
+
if best and best <= 0 and #ties > 1 then return nil end
|
|
306
|
+
return ties
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
function M.build(idx)
|
|
310
|
+
local G = {
|
|
311
|
+
files = {}, file_index = {}, nodes = {}, id_of = {},
|
|
312
|
+
by_name = {}, by_lname = {}, by_qualified = {}, by_base = {}, by_stem = {},
|
|
313
|
+
pkg_files = {}, children = {}, out = {}, inn = {}, deps = {}, dir_files = {}, ns_files = {},
|
|
314
|
+
generation = idx.generation,
|
|
315
|
+
}
|
|
316
|
+
local paths = {}
|
|
317
|
+
for rel in pairs(idx.files) do paths[#paths + 1] = rel end
|
|
318
|
+
table.sort(paths)
|
|
319
|
+
-- Checkpoints per file: LuaJIT does not run count hooks inside compiled
|
|
320
|
+
-- loops, so cancellation relies on these (as in Index:refresh).
|
|
321
|
+
for f, rel in ipairs(paths) do
|
|
322
|
+
control.check()
|
|
323
|
+
local rec = idx.files[rel]
|
|
324
|
+
G.files[f] = rec
|
|
325
|
+
G.file_index[rel] = f
|
|
326
|
+
push(G.by_base, basename(rel), f)
|
|
327
|
+
push(G.by_stem, stem(rel), f)
|
|
328
|
+
push(G.dir_files, dirname(rel), f)
|
|
329
|
+
if rec.package then push(G.pkg_files, rec.package, f) end
|
|
330
|
+
local ids = {}
|
|
331
|
+
G.id_of[f] = ids
|
|
332
|
+
for n, node in ipairs(rec.nodes) do
|
|
333
|
+
local id = #G.nodes + 1
|
|
334
|
+
G.nodes[id] = { id = id, f = f, n = n, node = node }
|
|
335
|
+
ids[n] = id
|
|
336
|
+
push(G.by_name, node.name, id)
|
|
337
|
+
push(G.by_lname, node.name:lower(), id)
|
|
338
|
+
push(G.by_qualified, node.qualified, id)
|
|
339
|
+
if node.kind == 'namespace' and rec.language == 'csharp' then
|
|
340
|
+
local list = G.ns_files[node.qualified]
|
|
341
|
+
if not list or list[#list] ~= f then push(G.ns_files, node.qualified, f) end
|
|
342
|
+
end
|
|
343
|
+
if node.parent then push(G.children, ids[node.parent], id) end
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
-- Directories indexed by their last path segment (Go import lookups).
|
|
347
|
+
G.dir_by_base = {}
|
|
348
|
+
for d in pairs(G.dir_files) do push(G.dir_by_base, basename(d), d) end
|
|
349
|
+
for _, list in pairs(G.dir_by_base) do table.sort(list) end
|
|
350
|
+
-- Edges.
|
|
351
|
+
local edges = 0
|
|
352
|
+
for f, rec in ipairs(G.files) do
|
|
353
|
+
control.check()
|
|
354
|
+
local deps, paired = resolve_deps(G, f)
|
|
355
|
+
G.deps[f] = deps
|
|
356
|
+
for _, ref in ipairs(rec.refs) do
|
|
357
|
+
if ref.from and ref.from > 0 then
|
|
358
|
+
local src = G.id_of[f][ref.from]
|
|
359
|
+
local targets = resolve_ref(G, f, src, ref, deps, paired)
|
|
360
|
+
if targets then
|
|
361
|
+
for _, dst in ipairs(targets) do
|
|
362
|
+
push(G.out, src, { id = dst, line = ref.line, kind = ref.kind })
|
|
363
|
+
push(G.inn, dst, { id = src, line = ref.line, kind = ref.kind })
|
|
364
|
+
edges = edges + 1
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
G.edge_count = edges
|
|
371
|
+
return G
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
-- Location string for a node: "path:line".
|
|
375
|
+
function M.where(G, id)
|
|
376
|
+
local n = G.nodes[id]
|
|
377
|
+
return G.files[n.f].path .. ':' .. n.node.line
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
return M
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
local mcp = require('codeoutline.mcp')
|
|
2
|
+
local M = {}
|
|
3
|
+
local MAX_BODY, MAX_HEADER = 1048576, 16384
|
|
4
|
+
|
|
5
|
+
function M.start(config, submit, cancel, codec)
|
|
6
|
+
local host, port = config.host, config.port
|
|
7
|
+
local local_only = host == '127.0.0.1' or host == 'localhost' or host == '::1'
|
|
8
|
+
assert(local_only or (config.token and #config.token >= 16 and #config.hosts > 0),
|
|
9
|
+
'Remote HTTP requires CODEOUTLINE_TOKEN (at least 16 bytes) and ALLOW_HOST')
|
|
10
|
+
local hosts, origins = {}, {}
|
|
11
|
+
if local_only then hosts['127.0.0.1'], hosts.localhost, hosts['[::1]'] = true, true, true end
|
|
12
|
+
for _, h in ipairs(config.hosts) do hosts[h] = true end
|
|
13
|
+
for _, o in ipairs(config.origins) do origins[o] = true end
|
|
14
|
+
local sessions, connections = {}, {}
|
|
15
|
+
local session_count, connection_count = 0, 0
|
|
16
|
+
local handler = {}
|
|
17
|
+
|
|
18
|
+
local function respond(conn, req, status, body, headers)
|
|
19
|
+
if not connections[conn] then return end
|
|
20
|
+
headers = headers or {}
|
|
21
|
+
headers['Content-Type'] = 'application/json'
|
|
22
|
+
req.keep_alive = false
|
|
23
|
+
codec.send_response(conn, req, { status = status, body = body or '', headers = headers,
|
|
24
|
+
content_type = 'application/json' }, { server_name = 'CodeOutline' })
|
|
25
|
+
conn:close('response complete')
|
|
26
|
+
end
|
|
27
|
+
local function fail(conn, req, status, message, code)
|
|
28
|
+
respond(conn, req, status, xutils.json_pack(mcp.error(nil, code or -32000, message)))
|
|
29
|
+
end
|
|
30
|
+
local function authorized(header)
|
|
31
|
+
if not config.token then return true end
|
|
32
|
+
local expected = 'Bearer ' .. config.token
|
|
33
|
+
if type(header) ~= 'string' or #header ~= #expected then return false end
|
|
34
|
+
-- Constant time without bitwise operators, which LuaJIT cannot parse.
|
|
35
|
+
local diff = 0
|
|
36
|
+
for i = 1, #expected do
|
|
37
|
+
local d = header:byte(i) - expected:byte(i)
|
|
38
|
+
diff = diff + d * d
|
|
39
|
+
end
|
|
40
|
+
return diff == 0
|
|
41
|
+
end
|
|
42
|
+
local function remove_session(sid)
|
|
43
|
+
local session = sessions[sid]
|
|
44
|
+
if session then session:close(); sessions[sid] = nil; session_count = session_count - 1 end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
local function request(conn, req)
|
|
48
|
+
local h = req.headers
|
|
49
|
+
local hostname = (h.host or ''):match('^(%[[%x:]+%]):?%d*$') or (h.host or ''):match('^([%w.%-]+):?%d*$')
|
|
50
|
+
if not hosts[hostname] then fail(conn, req, 403, 'Host is not allowed'); return end
|
|
51
|
+
if h.origin and not origins[h.origin] then fail(conn, req, 403, 'Origin is not allowed'); return end
|
|
52
|
+
if not authorized(h.authorization) then fail(conn, req, 401, 'Bearer authentication required'); return end
|
|
53
|
+
if req.path ~= '/mcp' then fail(conn, req, 404, 'Not found'); return end
|
|
54
|
+
if h['mcp-protocol-version'] and h['mcp-protocol-version'] ~= mcp.VERSION then
|
|
55
|
+
fail(conn, req, 400, 'Unsupported MCP protocol version'); return
|
|
56
|
+
end
|
|
57
|
+
local sid = h['mcp-session-id']
|
|
58
|
+
local session = sid and sessions[sid]
|
|
59
|
+
if sid and not session then fail(conn, req, 404, 'Session not found'); return end
|
|
60
|
+
if req.method == 'GET' then
|
|
61
|
+
respond(conn, req, 405, '', { Allow = 'POST, DELETE' }); return
|
|
62
|
+
elseif req.method == 'DELETE' then
|
|
63
|
+
if not session then fail(conn, req, 400, 'Session required'); return end
|
|
64
|
+
remove_session(sid); respond(conn, req, 200); return
|
|
65
|
+
elseif req.method ~= 'POST' then respond(conn, req, 405, '', { Allow = 'POST, DELETE' }); return end
|
|
66
|
+
if not (h.accept or ''):find('application/json', 1, true) or not (h.accept or ''):find('text/event-stream', 1, true) then
|
|
67
|
+
fail(conn, req, 406, 'Accept must include application/json and text/event-stream'); return
|
|
68
|
+
end
|
|
69
|
+
local content_type = (h['content-type'] or ''):lower():match('^%s*([^;%s]+)')
|
|
70
|
+
if content_type ~= 'application/json' then fail(conn, req, 415, 'Content-Type must be application/json'); return end
|
|
71
|
+
local msg, decode_error = mcp.decode(req.body)
|
|
72
|
+
if not msg then respond(conn, req, 400, xutils.json_pack(decode_error)); return end
|
|
73
|
+
if not session then
|
|
74
|
+
if msg.method ~= 'initialize' or msg.id == nil then fail(conn, req, 400, 'Initialization required'); return end
|
|
75
|
+
if session_count >= 64 then fail(conn, req, 503, 'Session limit reached'); return end
|
|
76
|
+
sid = xutils.sha256_hex(xutils.random_bytes(32))
|
|
77
|
+
session = mcp.new(config, submit, cancel)
|
|
78
|
+
sessions[sid] = session
|
|
79
|
+
session_count = session_count + 1
|
|
80
|
+
end
|
|
81
|
+
local stream = false
|
|
82
|
+
session:dispatch(msg, function(response, notification)
|
|
83
|
+
if not connections[conn] then return end
|
|
84
|
+
if notification then
|
|
85
|
+
if not stream then
|
|
86
|
+
stream = true
|
|
87
|
+
conn:send('HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\nMcp-Session-Id: ' .. sid .. '\r\n\r\n')
|
|
88
|
+
end
|
|
89
|
+
conn:send('data: ' .. xutils.json_pack(response) .. '\n\n')
|
|
90
|
+
elseif stream then
|
|
91
|
+
if response then conn:send('data: ' .. xutils.json_pack(response) .. '\n\n') end
|
|
92
|
+
conn:close('stream complete')
|
|
93
|
+
else
|
|
94
|
+
local headers = session.initialized and { ['Mcp-Session-Id'] = sid } or {}
|
|
95
|
+
if not session.initialized then remove_session(sid) end
|
|
96
|
+
respond(conn, req, response and 200 or ((msg.id == nil or msg.method == nil) and 202 or 204),
|
|
97
|
+
response and xutils.json_pack(response) or '', headers)
|
|
98
|
+
end
|
|
99
|
+
end)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
function handler.on_connect(conn)
|
|
103
|
+
if connection_count >= 128 then conn:close('connection limit'); return end
|
|
104
|
+
conn:set_framing({ type = 'raw', max_packet = MAX_BODY + MAX_HEADER })
|
|
105
|
+
connections[conn] = { buffer = '', started = xtimer.now_ms() }
|
|
106
|
+
connection_count = connection_count + 1
|
|
107
|
+
end
|
|
108
|
+
function handler.on_packet(conn, data)
|
|
109
|
+
local state = connections[conn]
|
|
110
|
+
if not state then return #data end
|
|
111
|
+
if state.dispatched then conn:close('HTTP pipelining is not supported'); return #data end
|
|
112
|
+
state.buffer = state.buffer .. data
|
|
113
|
+
local header_end = state.buffer:find('\r\n\r\n', 1, true)
|
|
114
|
+
if (header_end or #state.buffer) > MAX_HEADER or #state.buffer > MAX_BODY + MAX_HEADER then
|
|
115
|
+
fail(conn, {}, 413, 'Request too large'); return #data
|
|
116
|
+
end
|
|
117
|
+
local req, next_pos, err = codec.parse_request(state.buffer, 1, { max_request_size = MAX_BODY + MAX_HEADER })
|
|
118
|
+
if not req then
|
|
119
|
+
if err ~= 'incomplete' then fail(conn, {}, err == 'request too large' and 413 or 400, err) end
|
|
120
|
+
return #data
|
|
121
|
+
end
|
|
122
|
+
if #req.body > MAX_BODY then fail(conn, req, 413, 'Request body too large'); return #data end
|
|
123
|
+
if next_pos <= #state.buffer then fail(conn, req, 400, 'HTTP pipelining is not supported'); return #data end
|
|
124
|
+
state.buffer, state.dispatched = '', true
|
|
125
|
+
local ok, request_error = pcall(request, conn, req)
|
|
126
|
+
if not ok then
|
|
127
|
+
io.stderr:write('[codeoutline] HTTP: ' .. tostring(request_error) .. '\n')
|
|
128
|
+
fail(conn, req, 500, 'Internal server error')
|
|
129
|
+
end
|
|
130
|
+
return #data
|
|
131
|
+
end
|
|
132
|
+
function handler.on_close(conn)
|
|
133
|
+
if connections[conn] then connection_count = connection_count - 1 end
|
|
134
|
+
connections[conn] = nil
|
|
135
|
+
end
|
|
136
|
+
local listener = assert(xnet.listen(host, port, handler))
|
|
137
|
+
return {
|
|
138
|
+
tick = function()
|
|
139
|
+
local now = xtimer.now_ms()
|
|
140
|
+
for sid, session in pairs(sessions) do
|
|
141
|
+
session:tick()
|
|
142
|
+
if not next(session.pending) and now - session.used > 900000 then remove_session(sid) end
|
|
143
|
+
end
|
|
144
|
+
for conn, state in pairs(connections) do
|
|
145
|
+
if now - state.started > (state.dispatched and 125000 or 15000) then conn:close('timeout') end
|
|
146
|
+
end
|
|
147
|
+
end,
|
|
148
|
+
close = function()
|
|
149
|
+
for sid in pairs(sessions) do remove_session(sid) end
|
|
150
|
+
for conn in pairs(connections) do conn:close('shutdown') end
|
|
151
|
+
listener:close('shutdown')
|
|
152
|
+
end,
|
|
153
|
+
}
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
return M
|