@mfdaves/okf-mcp 0.3.3-rc.1 → 0.5.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/.agents/skills/okf-v02-migration/SKILL.md +37 -0
- package/.agents/skills/okf-v02-migration/agents/openai.yaml +6 -0
- package/.agents/skills/okf-v02-migration/references/field-mapping.md +26 -0
- package/README.md +192 -78
- package/okf/bundles/okf-mcp/distribution/reference-bundle.md +2 -2
- package/okf/bundles/okf-mcp/index.md +7 -0
- package/okf/bundles/okf-mcp/interfaces/http-authoring-api.md +2 -2
- package/okf/bundles/okf-mcp/interfaces/mcp-tools.md +8 -4
- package/okf/bundles/okf-mcp/overview/okf-mcp.md +2 -2
- package/okf/bundles/okf-mcp/policies/authoring-safety.md +5 -3
- package/okf/bundles/okf-mcp/runtime/file-concept-store.md +7 -5
- package/okf/bundles/okf-mcp/runtime/indexer.md +6 -4
- package/okf/bundles/okf-mcp/runtime/mcp-server.md +6 -6
- package/okf/bundles/okf-mcp/specs/attested-computation.md +24 -0
- package/okf/bundles/okf-mcp/specs/concept-format.md +5 -3
- package/okf/bundles/okf-mcp/specs/git-sources.md +23 -0
- package/okf/bundles/okf-mcp/workflows/v02-migration.md +24 -0
- package/package.json +9 -3
- package/server.json +5 -5
- package/src/assets.js +469 -0
- package/src/authoring.js +492 -20
- package/src/cli.js +467 -37
- package/src/computation.js +366 -0
- package/src/git-source.js +551 -0
- package/src/graph.js +71 -18
- package/src/http-server.js +8 -1
- package/src/index.js +5 -0
- package/src/indexer.js +428 -24
- package/src/markdown.js +129 -0
- package/src/mcp-server.js +447 -313
- package/src/migration.js +687 -0
- package/src/parser.js +135 -54
- package/src/plugins.js +30 -50
- package/src/project.js +74 -17
- package/src/remote.js +232 -17
- package/src/search.js +69 -6
- package/src/store.js +421 -25
- package/src/v02.js +545 -0
- package/src/validation.js +13 -2
- package/src/mcp-protocol.js +0 -283
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { spawnSync: nodeSpawnSync } = require("node:child_process");
|
|
7
|
+
const { resolveConcept } = require("./indexer");
|
|
8
|
+
|
|
9
|
+
const DEFAULT_MAX_GIT_SOURCE_BYTES = 1024 * 1024;
|
|
10
|
+
const MAX_GIT_SOURCE_BYTES = 1024 * 1024;
|
|
11
|
+
const DEFAULT_GIT_TIMEOUT_MS = 10000;
|
|
12
|
+
const MAX_GIT_TIMEOUT_MS = 60000;
|
|
13
|
+
|
|
14
|
+
class GitSourceError extends Error {
|
|
15
|
+
constructor(code, message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "GitSourceError";
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isPlainObject(value) {
|
|
23
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const prototype = Object.getPrototypeOf(value);
|
|
27
|
+
return prototype === Object.prototype || prototype === null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function sourceError(code, message) {
|
|
31
|
+
return new GitSourceError(code, message);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function normalizeLines(value) {
|
|
35
|
+
if (value === undefined || value === null) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
let start;
|
|
39
|
+
let end;
|
|
40
|
+
if (Array.isArray(value) && value.length === 2) {
|
|
41
|
+
[start, end] = value;
|
|
42
|
+
} else if (isPlainObject(value)) {
|
|
43
|
+
start = value.from === undefined ? value.start : value.from;
|
|
44
|
+
end = value.to === undefined ? value.end : value.to;
|
|
45
|
+
} else {
|
|
46
|
+
throw sourceError(
|
|
47
|
+
"git_source_invalid_lines",
|
|
48
|
+
"git.lines must be a two-item [start, end] list or a mapping with start and end.",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 1 || end < start) {
|
|
52
|
+
throw sourceError(
|
|
53
|
+
"git_source_invalid_lines",
|
|
54
|
+
"git.lines uses inclusive, one-based safe integers with end not less than start.",
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return { start, end };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function safeGitPath(value) {
|
|
61
|
+
if (typeof value !== "string" || !value || value !== value.trim()) {
|
|
62
|
+
throw sourceError("git_source_invalid_path", "git.path must be a non-empty repository-relative POSIX path.");
|
|
63
|
+
}
|
|
64
|
+
if (value.includes("\\") || /[\x00-\x1f\x7f]/.test(value) || path.posix.isAbsolute(value)) {
|
|
65
|
+
throw sourceError("git_source_invalid_path", "git.path must be a safe repository-relative POSIX path.");
|
|
66
|
+
}
|
|
67
|
+
const segments = value.split("/");
|
|
68
|
+
if (segments.some((segment) => !segment || segment === "." || segment === "..")
|
|
69
|
+
|| path.posix.normalize(value) !== value) {
|
|
70
|
+
throw sourceError("git_source_invalid_path", "git.path cannot contain empty, dot, or parent components.");
|
|
71
|
+
}
|
|
72
|
+
if (Buffer.byteLength(value, "utf8") > 4096) {
|
|
73
|
+
throw sourceError("git_source_invalid_path", "git.path exceeds the 4096-byte safety limit.");
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function validateGitSource(source) {
|
|
79
|
+
if (!isPlainObject(source)) {
|
|
80
|
+
throw sourceError("git_source_invalid", "A Git source entry must be a mapping.");
|
|
81
|
+
}
|
|
82
|
+
if (typeof source.resource !== "string" || !source.resource.trim()) {
|
|
83
|
+
throw sourceError(
|
|
84
|
+
"git_source_repository_missing",
|
|
85
|
+
"A Git source entry requires resource to identify its repository concept.",
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (!isPlainObject(source.git)) {
|
|
89
|
+
throw sourceError("git_source_metadata_missing", "A Git source entry requires a git mapping.");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const requestedRevision = typeof source.git.revision === "string"
|
|
93
|
+
? source.git.revision.trim()
|
|
94
|
+
: "";
|
|
95
|
+
const pinned = /^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/.test(requestedRevision);
|
|
96
|
+
return {
|
|
97
|
+
repositoryConceptId: source.resource.trim(),
|
|
98
|
+
revision: pinned ? requestedRevision.toLowerCase() : null,
|
|
99
|
+
pinned,
|
|
100
|
+
path: safeGitPath(source.git.path),
|
|
101
|
+
lines: normalizeLines(source.git.lines),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function mappingFor(repositoryMappings, conceptId) {
|
|
106
|
+
if (repositoryMappings instanceof Map) {
|
|
107
|
+
return repositoryMappings.has(conceptId) ? repositoryMappings.get(conceptId) : undefined;
|
|
108
|
+
}
|
|
109
|
+
if (isPlainObject(repositoryMappings)
|
|
110
|
+
&& Object.prototype.hasOwnProperty.call(repositoryMappings, conceptId)) {
|
|
111
|
+
return repositoryMappings[conceptId];
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function pathFromMapping(mapping) {
|
|
117
|
+
if (typeof mapping === "string") {
|
|
118
|
+
return mapping;
|
|
119
|
+
}
|
|
120
|
+
if (!isPlainObject(mapping)) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
return mapping.path || mapping.checkout || mapping.repositoryPath || mapping.gitDir || null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function resolveRepositoryPath(repositoryMappings, conceptId) {
|
|
127
|
+
const mapping = mappingFor(repositoryMappings, conceptId);
|
|
128
|
+
if (mapping === undefined) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const configuredPath = pathFromMapping(mapping);
|
|
132
|
+
if (typeof configuredPath !== "string" || !configuredPath || !path.isAbsolute(configuredPath)) {
|
|
133
|
+
throw sourceError(
|
|
134
|
+
"git_source_invalid_repository_mapping",
|
|
135
|
+
`Repository mapping for ${conceptId} must contain an explicit absolute path.`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const resolved = fs.realpathSync(configuredPath);
|
|
140
|
+
if (!fs.statSync(resolved).isDirectory()) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
return resolved;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (error && ["ENOENT", "ENOTDIR"].includes(error.code)) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function boundedInteger(value, fallback, maximum, label) {
|
|
153
|
+
const normalized = value === undefined ? fallback : Number(value);
|
|
154
|
+
if (!Number.isSafeInteger(normalized) || normalized < 1 || normalized > maximum) {
|
|
155
|
+
throw new TypeError(`${label} must be a safe integer from 1 through ${maximum}.`);
|
|
156
|
+
}
|
|
157
|
+
return normalized;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function gitEnvironment(extra) {
|
|
161
|
+
const environment = Object.assign({}, process.env, extra || {});
|
|
162
|
+
[
|
|
163
|
+
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
|
|
164
|
+
"GIT_COMMON_DIR",
|
|
165
|
+
"GIT_CONFIG_COUNT",
|
|
166
|
+
"GIT_CONFIG_PARAMETERS",
|
|
167
|
+
"GIT_DIR",
|
|
168
|
+
"GIT_EXEC_PATH",
|
|
169
|
+
"GIT_INDEX_FILE",
|
|
170
|
+
"GIT_NAMESPACE",
|
|
171
|
+
"GIT_OBJECT_DIRECTORY",
|
|
172
|
+
"GIT_WORK_TREE",
|
|
173
|
+
].forEach((name) => {
|
|
174
|
+
delete environment[name];
|
|
175
|
+
});
|
|
176
|
+
Object.keys(environment).forEach((name) => {
|
|
177
|
+
if (/^GIT_CONFIG_(?:KEY|VALUE)_\d+$/.test(name)) {
|
|
178
|
+
delete environment[name];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
environment.GIT_ALLOW_PROTOCOL = "";
|
|
182
|
+
environment.GIT_LITERAL_PATHSPECS = "1";
|
|
183
|
+
environment.GIT_NO_LAZY_FETCH = "1";
|
|
184
|
+
environment.GIT_NO_REPLACE_OBJECTS = "1";
|
|
185
|
+
environment.GIT_OPTIONAL_LOCKS = "0";
|
|
186
|
+
environment.GIT_PAGER = "cat";
|
|
187
|
+
environment.GIT_PROTOCOL_FROM_USER = "0";
|
|
188
|
+
environment.GIT_TERMINAL_PROMPT = "0";
|
|
189
|
+
environment.LC_ALL = "C";
|
|
190
|
+
return environment;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function runGit(repositoryPath, args, options, maxBuffer) {
|
|
194
|
+
const config = options || {};
|
|
195
|
+
const spawnSync = config.spawnSync || nodeSpawnSync;
|
|
196
|
+
const result = spawnSync(
|
|
197
|
+
config.gitBinary || "git",
|
|
198
|
+
["--no-pager", "-C", repositoryPath].concat(args),
|
|
199
|
+
{
|
|
200
|
+
encoding: null,
|
|
201
|
+
env: gitEnvironment(config.env),
|
|
202
|
+
maxBuffer,
|
|
203
|
+
shell: false,
|
|
204
|
+
timeout: config.timeoutMs,
|
|
205
|
+
windowsHide: true,
|
|
206
|
+
},
|
|
207
|
+
);
|
|
208
|
+
if (!result || typeof result !== "object") {
|
|
209
|
+
throw sourceError("git_source_process_error", "Git returned no process result.");
|
|
210
|
+
}
|
|
211
|
+
if (result.error) {
|
|
212
|
+
const code = result.error.code === "ETIMEDOUT"
|
|
213
|
+
? "git_source_timeout"
|
|
214
|
+
: result.error.code === "ENOBUFS"
|
|
215
|
+
? "git_source_output_too_large"
|
|
216
|
+
: "git_source_process_error";
|
|
217
|
+
throw sourceError(code, `Git source inspection failed: ${result.error.message}`);
|
|
218
|
+
}
|
|
219
|
+
if (result.signal) {
|
|
220
|
+
throw sourceError("git_source_process_error", "Git source inspection was terminated.");
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
ok: result.status === 0,
|
|
224
|
+
stdout: Buffer.isBuffer(result.stdout) ? result.stdout : Buffer.from(result.stdout || ""),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function decodeAscii(buffer, code, label) {
|
|
229
|
+
if (!Array.from(buffer).every((byte) => (
|
|
230
|
+
byte === 9 || byte === 10 || byte === 13 || (byte >= 32 && byte <= 126)
|
|
231
|
+
))) {
|
|
232
|
+
throw sourceError(code, `${label} was not valid ASCII.`);
|
|
233
|
+
}
|
|
234
|
+
return buffer.toString("ascii");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function parseTreeEntry(buffer, expectedPath) {
|
|
238
|
+
if (!buffer.length) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
if (buffer[buffer.length - 1] !== 0 || buffer.subarray(0, -1).includes(0)) {
|
|
242
|
+
throw sourceError("git_source_invalid_tree_output", "Git returned malformed tree output.");
|
|
243
|
+
}
|
|
244
|
+
const record = buffer.subarray(0, -1);
|
|
245
|
+
const tab = record.indexOf(9);
|
|
246
|
+
if (tab < 0) {
|
|
247
|
+
throw sourceError("git_source_invalid_tree_output", "Git returned malformed tree output.");
|
|
248
|
+
}
|
|
249
|
+
const header = decodeAscii(
|
|
250
|
+
record.subarray(0, tab),
|
|
251
|
+
"git_source_invalid_tree_output",
|
|
252
|
+
"Git tree metadata",
|
|
253
|
+
);
|
|
254
|
+
const match = header.match(/^([0-7]{6}) ([a-z]+) ([0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/);
|
|
255
|
+
if (!match) {
|
|
256
|
+
throw sourceError("git_source_invalid_tree_output", "Git returned malformed tree metadata.");
|
|
257
|
+
}
|
|
258
|
+
let returnedPath;
|
|
259
|
+
try {
|
|
260
|
+
returnedPath = new TextDecoder("utf-8", { fatal: true }).decode(record.subarray(tab + 1));
|
|
261
|
+
} catch (error) {
|
|
262
|
+
throw sourceError("git_source_invalid_tree_path", "Git returned a tree path that is not valid UTF-8.");
|
|
263
|
+
}
|
|
264
|
+
if (returnedPath !== expectedPath) {
|
|
265
|
+
throw sourceError("git_source_tree_path_mismatch", "Git returned a different path than the requested source path.");
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
mode: match[1],
|
|
269
|
+
type: match[2],
|
|
270
|
+
oid: match[3].toLowerCase(),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function unavailable(source, reason) {
|
|
275
|
+
return {
|
|
276
|
+
available: false,
|
|
277
|
+
reason,
|
|
278
|
+
metadata: {
|
|
279
|
+
repositoryConceptId: source.repositoryConceptId,
|
|
280
|
+
revision: source.revision,
|
|
281
|
+
path: source.path,
|
|
282
|
+
lines: source.lines,
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function sliceInclusiveLines(text, lines) {
|
|
288
|
+
if (!lines) {
|
|
289
|
+
return text;
|
|
290
|
+
}
|
|
291
|
+
if (!text.length) {
|
|
292
|
+
throw sourceError("git_source_lines_out_of_range", "git.lines cannot select from an empty file.");
|
|
293
|
+
}
|
|
294
|
+
const starts = [0];
|
|
295
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
296
|
+
if (text[index] === "\n" && index + 1 < text.length) {
|
|
297
|
+
starts.push(index + 1);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (lines.end > starts.length) {
|
|
301
|
+
throw sourceError(
|
|
302
|
+
"git_source_lines_out_of_range",
|
|
303
|
+
`git.lines ends at ${lines.end}, but the source contains ${starts.length} lines.`,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const startOffset = starts[lines.start - 1];
|
|
307
|
+
const endOffset = lines.end < starts.length ? starts[lines.end] : text.length;
|
|
308
|
+
return text.slice(startOffset, endOffset);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function blobObjectId(content, algorithm) {
|
|
312
|
+
const header = Buffer.from(`blob ${content.length}\0`, "ascii");
|
|
313
|
+
return crypto.createHash(algorithm).update(header).update(content).digest("hex");
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function readGitSource(source, repositoryMappings, options) {
|
|
317
|
+
const normalized = validateGitSource(source);
|
|
318
|
+
if (!normalized.pinned) {
|
|
319
|
+
return unavailable(normalized, "revision_unpinned");
|
|
320
|
+
}
|
|
321
|
+
if (mappingFor(repositoryMappings, normalized.repositoryConceptId) === undefined) {
|
|
322
|
+
return unavailable(normalized, "repository_unmapped");
|
|
323
|
+
}
|
|
324
|
+
const repositoryPath = resolveRepositoryPath(repositoryMappings, normalized.repositoryConceptId);
|
|
325
|
+
if (!repositoryPath) {
|
|
326
|
+
return unavailable(normalized, "repository_unavailable");
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const config = Object.assign({}, options || {});
|
|
330
|
+
config.timeoutMs = boundedInteger(
|
|
331
|
+
config.timeoutMs,
|
|
332
|
+
DEFAULT_GIT_TIMEOUT_MS,
|
|
333
|
+
MAX_GIT_TIMEOUT_MS,
|
|
334
|
+
"timeoutMs",
|
|
335
|
+
);
|
|
336
|
+
const maxBytes = boundedInteger(
|
|
337
|
+
config.maxBytes,
|
|
338
|
+
DEFAULT_MAX_GIT_SOURCE_BYTES,
|
|
339
|
+
MAX_GIT_SOURCE_BYTES,
|
|
340
|
+
"maxBytes",
|
|
341
|
+
);
|
|
342
|
+
const metadataBuffer = 64 * 1024;
|
|
343
|
+
|
|
344
|
+
const revisionType = runGit(
|
|
345
|
+
repositoryPath,
|
|
346
|
+
["cat-file", "-t", normalized.revision],
|
|
347
|
+
config,
|
|
348
|
+
metadataBuffer,
|
|
349
|
+
);
|
|
350
|
+
if (!revisionType.ok) {
|
|
351
|
+
return unavailable(normalized, "revision_unavailable");
|
|
352
|
+
}
|
|
353
|
+
if (decodeAscii(revisionType.stdout, "git_source_invalid_object_type", "Git object type").trim() !== "commit") {
|
|
354
|
+
return unavailable(normalized, "revision_not_commit");
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const listed = runGit(
|
|
358
|
+
repositoryPath,
|
|
359
|
+
["ls-tree", "-z", "--full-tree", normalized.revision, "--", normalized.path],
|
|
360
|
+
config,
|
|
361
|
+
metadataBuffer,
|
|
362
|
+
);
|
|
363
|
+
if (!listed.ok) {
|
|
364
|
+
return unavailable(normalized, "path_unavailable");
|
|
365
|
+
}
|
|
366
|
+
const entry = parseTreeEntry(listed.stdout, normalized.path);
|
|
367
|
+
if (!entry) {
|
|
368
|
+
return unavailable(normalized, "path_unavailable");
|
|
369
|
+
}
|
|
370
|
+
if (entry.mode === "120000") {
|
|
371
|
+
throw sourceError("git_source_symlink", "Git source paths cannot resolve to symbolic links.");
|
|
372
|
+
}
|
|
373
|
+
if (entry.mode === "160000") {
|
|
374
|
+
throw sourceError("git_source_submodule", "Git source paths cannot resolve to submodules.");
|
|
375
|
+
}
|
|
376
|
+
if (!["100644", "100755"].includes(entry.mode) || entry.type !== "blob") {
|
|
377
|
+
throw sourceError("git_source_not_regular_file", "Git source paths must resolve to regular file blobs.");
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const sized = runGit(repositoryPath, ["cat-file", "-s", entry.oid], config, metadataBuffer);
|
|
381
|
+
if (!sized.ok) {
|
|
382
|
+
return unavailable(normalized, "blob_unavailable");
|
|
383
|
+
}
|
|
384
|
+
const sizeText = decodeAscii(sized.stdout, "git_source_invalid_size", "Git blob size").trim();
|
|
385
|
+
if (!/^(?:0|[1-9][0-9]*)$/.test(sizeText)) {
|
|
386
|
+
throw sourceError("git_source_invalid_size", "Git returned an invalid blob size.");
|
|
387
|
+
}
|
|
388
|
+
const size = Number(sizeText);
|
|
389
|
+
if (!Number.isSafeInteger(size)) {
|
|
390
|
+
throw sourceError("git_source_invalid_size", "Git returned an unsafe blob size.");
|
|
391
|
+
}
|
|
392
|
+
if (size > maxBytes) {
|
|
393
|
+
throw sourceError(
|
|
394
|
+
"git_source_too_large",
|
|
395
|
+
`Git source exceeds the configured ${maxBytes} byte limit.`,
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const fetched = runGit(repositoryPath, ["cat-file", "blob", entry.oid], config, maxBytes + metadataBuffer);
|
|
400
|
+
if (!fetched.ok) {
|
|
401
|
+
return unavailable(normalized, "blob_unavailable");
|
|
402
|
+
}
|
|
403
|
+
if (fetched.stdout.length !== size || fetched.stdout.length > maxBytes) {
|
|
404
|
+
throw sourceError("git_source_size_mismatch", "Git blob content does not match its inspected size.");
|
|
405
|
+
}
|
|
406
|
+
const objectAlgorithm = entry.oid.length === 64 ? "sha256" : "sha1";
|
|
407
|
+
if (blobObjectId(fetched.stdout, objectAlgorithm) !== entry.oid) {
|
|
408
|
+
throw sourceError("git_source_blob_mismatch", "Git blob content does not match its object ID.");
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
let text;
|
|
412
|
+
try {
|
|
413
|
+
text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(fetched.stdout);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
throw sourceError("git_source_invalid_utf8", "Git source content is not valid UTF-8.");
|
|
416
|
+
}
|
|
417
|
+
const content = sliceInclusiveLines(text, normalized.lines);
|
|
418
|
+
const sha256 = `sha256:${crypto.createHash("sha256").update(fetched.stdout).digest("hex")}`;
|
|
419
|
+
const metadata = {
|
|
420
|
+
repositoryConceptId: normalized.repositoryConceptId,
|
|
421
|
+
revision: normalized.revision,
|
|
422
|
+
revisionAlgorithm: normalized.revision.length === 64 ? "sha256" : "sha1",
|
|
423
|
+
path: normalized.path,
|
|
424
|
+
lines: normalized.lines,
|
|
425
|
+
mode: entry.mode,
|
|
426
|
+
objectType: entry.type,
|
|
427
|
+
blobOid: entry.oid,
|
|
428
|
+
objectAlgorithm,
|
|
429
|
+
size,
|
|
430
|
+
encoding: "utf-8",
|
|
431
|
+
sha256,
|
|
432
|
+
};
|
|
433
|
+
return {
|
|
434
|
+
available: true,
|
|
435
|
+
content,
|
|
436
|
+
blobOid: entry.oid,
|
|
437
|
+
sha256,
|
|
438
|
+
metadata,
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function readGitSources(sources, repositoryMappings, options) {
|
|
443
|
+
if (!Array.isArray(sources)) {
|
|
444
|
+
throw new TypeError("sources must be an array.");
|
|
445
|
+
}
|
|
446
|
+
return sources.reduce((results, source, index) => {
|
|
447
|
+
if (!source || source.git === undefined) {
|
|
448
|
+
return results;
|
|
449
|
+
}
|
|
450
|
+
results.push(Object.assign({ sourceIndex: index }, readGitSource(source, repositoryMappings, options)));
|
|
451
|
+
return results;
|
|
452
|
+
}, []);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function declaredSource(document, sourceId) {
|
|
456
|
+
const sources = document && document.frontmatter && Array.isArray(document.frontmatter.sources)
|
|
457
|
+
? document.frontmatter.sources
|
|
458
|
+
: [];
|
|
459
|
+
const source = sources.find((entry) => (
|
|
460
|
+
entry
|
|
461
|
+
&& typeof entry === "object"
|
|
462
|
+
&& !Array.isArray(entry)
|
|
463
|
+
&& String(entry.id || "") === String(sourceId || "")
|
|
464
|
+
));
|
|
465
|
+
if (!source) {
|
|
466
|
+
throw sourceError(
|
|
467
|
+
"git_source_not_declared",
|
|
468
|
+
`Concept ${document && document.conceptId || "<unknown>"} has no source with id ${sourceId || "<missing>"}.`,
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
if (!source.git) {
|
|
472
|
+
throw sourceError("git_source_not_git", `Source ${sourceId} does not declare Git metadata.`);
|
|
473
|
+
}
|
|
474
|
+
return source;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function repositoryForSource(index, source) {
|
|
478
|
+
const repository = resolveConcept(index, source && source.resource);
|
|
479
|
+
if (!repository || !repository.valid || repository.reserved || repository.type !== "Git Repository") {
|
|
480
|
+
throw sourceError(
|
|
481
|
+
"git_source_repository_invalid",
|
|
482
|
+
"A Git source resource must resolve to a valid concept with type Git Repository.",
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
return repository;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function readConceptGitSource(index, conceptLocator, sourceId, repositoryMappings, options) {
|
|
489
|
+
const document = resolveConcept(index, conceptLocator);
|
|
490
|
+
if (!document || !document.valid || document.reserved) {
|
|
491
|
+
throw sourceError("git_source_concept_invalid", `Unknown valid OKF concept: ${conceptLocator || "<missing>"}`);
|
|
492
|
+
}
|
|
493
|
+
const source = declaredSource(document, sourceId);
|
|
494
|
+
const repository = repositoryForSource(index, source);
|
|
495
|
+
const result = readGitSource(Object.assign({}, source, {
|
|
496
|
+
resource: repository.conceptId,
|
|
497
|
+
}), repositoryMappings, options);
|
|
498
|
+
return Object.assign({
|
|
499
|
+
conceptId: document.conceptId,
|
|
500
|
+
sourceId: String(sourceId),
|
|
501
|
+
repository: {
|
|
502
|
+
conceptId: repository.conceptId,
|
|
503
|
+
resource: repository.frontmatter.resource || null,
|
|
504
|
+
},
|
|
505
|
+
}, result);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function describeConceptGitSources(index, document, repositoryMappings) {
|
|
509
|
+
const sources = document && document.frontmatter && Array.isArray(document.frontmatter.sources)
|
|
510
|
+
? document.frontmatter.sources
|
|
511
|
+
: [];
|
|
512
|
+
return sources.reduce((output, source) => {
|
|
513
|
+
if (!source || typeof source !== "object" || Array.isArray(source) || !source.git) {
|
|
514
|
+
return output;
|
|
515
|
+
}
|
|
516
|
+
try {
|
|
517
|
+
const repository = repositoryForSource(index, source);
|
|
518
|
+
const normalized = validateGitSource(Object.assign({}, source, { resource: repository.conceptId }));
|
|
519
|
+
output.push({
|
|
520
|
+
id: source.id || null,
|
|
521
|
+
repositoryConceptId: repository.conceptId,
|
|
522
|
+
revision: normalized.revision,
|
|
523
|
+
path: normalized.path,
|
|
524
|
+
lines: normalized.lines,
|
|
525
|
+
pinned: normalized.pinned,
|
|
526
|
+
mapped: mappingFor(repositoryMappings, repository.conceptId) !== undefined,
|
|
527
|
+
});
|
|
528
|
+
} catch (error) {
|
|
529
|
+
output.push({
|
|
530
|
+
id: source.id || null,
|
|
531
|
+
available: false,
|
|
532
|
+
reason: error.code || "git_source_invalid",
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
return output;
|
|
536
|
+
}, []);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
module.exports = {
|
|
540
|
+
DEFAULT_GIT_TIMEOUT_MS,
|
|
541
|
+
DEFAULT_MAX_GIT_SOURCE_BYTES,
|
|
542
|
+
describeConceptGitSources,
|
|
543
|
+
GitSourceError,
|
|
544
|
+
MAX_GIT_SOURCE_BYTES,
|
|
545
|
+
readConceptGitSource,
|
|
546
|
+
readGitSource,
|
|
547
|
+
readGitSources,
|
|
548
|
+
resolveRepositoryPath,
|
|
549
|
+
safeGitPath,
|
|
550
|
+
validateGitSource,
|
|
551
|
+
};
|