@infosupport/huddle-cli 0.0.0-experiment-147.95.1 → 0.0.0-experiment-160.97.1
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/README.md +14 -32
- package/dist/api.js +5 -11
- package/dist/config.js +2 -5
- package/dist/index.js +12 -69
- package/dist/init.js +20 -23
- package/dist/pipe/command.js +186 -0
- package/dist/pipe/fs-list.js +157 -0
- package/dist/pipe/protocol.js +66 -0
- package/dist/pipe/serve.js +210 -0
- package/dist/pipe/tools.js +229 -0
- package/dist/start.js +8 -9
- package/package.json +3 -7
- package/dist/extension.js +0 -170
- package/dist/host-logs.js +0 -60
- package/dist/indexfolder.js +0 -167
- package/dist/zip.js +0 -74
package/dist/start.js
CHANGED
|
@@ -80,19 +80,18 @@ async function runStart(opts) {
|
|
|
80
80
|
console.log(`Open in VS Code: ${(0, utils_1.cyan)('Dev Containers: Attach to Running Container')} -> ${(0, utils_1.bold)(result.containerName)}`);
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
|
-
|
|
83
|
+
console.log(`Open in JetBrains Gateway: ${(0, utils_1.cyan)('Remote Development > Dev Containers')} -> ${(0, utils_1.bold)(result.containerName)}`);
|
|
84
|
+
await tryPrintIdeLink(result.containerName);
|
|
84
85
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// in Settings, or on an older image, the portal button explains what is missing.
|
|
88
|
-
async function tryPrintSshLink(containerName) {
|
|
86
|
+
async function tryPrintIdeLink(containerName) {
|
|
87
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
89
88
|
try {
|
|
90
|
-
const res = await (0, api_1.get)(`/api/docker/containers/${encodeURIComponent(containerName)}/
|
|
89
|
+
const res = await (0, api_1.get)(`/api/docker/containers/${encodeURIComponent(containerName)}/ide-link`);
|
|
91
90
|
if (res?.link)
|
|
92
|
-
console.log(
|
|
91
|
+
console.log((0, utils_1.dim)(`Gateway-link: ${res.link}`));
|
|
93
92
|
}
|
|
94
|
-
catch
|
|
95
|
-
|
|
93
|
+
catch {
|
|
94
|
+
// The IDE link is best-effort; JetBrains may still be starting the backend.
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
function resolveWorkspace(workspace) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infosupport/huddle-cli",
|
|
3
|
-
"version": "0.0.0-experiment-
|
|
3
|
+
"version": "0.0.0-experiment-160.97.1",
|
|
4
4
|
"description": "Huddle CLI — start devcontainers and manage firewall rules",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"author": "Info Support B.V.",
|
|
@@ -20,15 +20,11 @@
|
|
|
20
20
|
"build": "tsc",
|
|
21
21
|
"dev": "tsc --watch",
|
|
22
22
|
"install-global": "npm run build && npm install -g .",
|
|
23
|
-
"typecheck": "tsc --noEmit"
|
|
24
|
-
"test": "vitest run"
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"@types/adm-zip": "^0.5.8",
|
|
28
26
|
"@types/node": "^22.0.0",
|
|
29
|
-
"
|
|
30
|
-
"typescript": "^5.0.0",
|
|
31
|
-
"vitest": "^4.1.11"
|
|
27
|
+
"typescript": "^5.0.0"
|
|
32
28
|
},
|
|
33
29
|
"engines": {
|
|
34
30
|
"node": ">=18"
|
package/dist/extension.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isExtensionId = exports.MAX_FOLDER_DEPTH = void 0;
|
|
7
|
-
exports.insideFolder = insideFolder;
|
|
8
|
-
exports.collectExtensionFiles = collectExtensionFiles;
|
|
9
|
-
exports.planInstall = planInstall;
|
|
10
|
-
exports.asExtensionList = asExtensionList;
|
|
11
|
-
exports.runExtensionList = runExtensionList;
|
|
12
|
-
exports.restartRuntime = restartRuntime;
|
|
13
|
-
exports.runExtensionInstall = runExtensionInstall;
|
|
14
|
-
exports.runExtensionRemove = runExtensionRemove;
|
|
15
|
-
const fs_1 = __importDefault(require("fs"));
|
|
16
|
-
const path_1 = __importDefault(require("path"));
|
|
17
|
-
const child_process_1 = require("child_process");
|
|
18
|
-
const api_1 = require("./api");
|
|
19
|
-
const zip_1 = require("./zip");
|
|
20
|
-
const init_1 = require("./init");
|
|
21
|
-
const runtime_1 = require("./runtime");
|
|
22
|
-
const utils_1 = require("./utils");
|
|
23
|
-
const SKIPPED = new Set(['node_modules']);
|
|
24
|
-
exports.MAX_FOLDER_DEPTH = 32;
|
|
25
|
-
/** The gateway's own id rule: one lowercase path component, so it can never point outside the extensions folder. */
|
|
26
|
-
const isExtensionId = (id) => typeof id === 'string' && /^[a-z0-9-]+$/.test(id);
|
|
27
|
-
exports.isExtensionId = isExtensionId;
|
|
28
|
-
/** Resolves `rel` within `root` and refuses anything that lands outside it (`..`, absolute paths). */
|
|
29
|
-
function insideFolder(root, rel) {
|
|
30
|
-
const base = path_1.default.resolve(root);
|
|
31
|
-
const abs = path_1.default.resolve(base, rel);
|
|
32
|
-
if (abs !== base && !abs.startsWith(base + path_1.default.sep))
|
|
33
|
-
throw new Error(`${rel} is outside the extension folder`);
|
|
34
|
-
return abs;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Reads a regular file of the folder; a symlink is refused, so the zip can never carry a file from outside it.
|
|
38
|
-
* @param root the resolved extension folder @param rel a path relative to it, from readdir
|
|
39
|
-
*/
|
|
40
|
-
function readPlainFile(root, rel) {
|
|
41
|
-
const abs = insideFolder(root, rel);
|
|
42
|
-
const stat = fs_1.default.lstatSync(abs);
|
|
43
|
-
if (stat.isSymbolicLink())
|
|
44
|
-
throw new Error(`${rel} is a symlink; extensions are packaged without symlinks`);
|
|
45
|
-
if (!stat.isFile())
|
|
46
|
-
throw new Error(`${rel} is not a regular file`);
|
|
47
|
-
return fs_1.default.readFileSync(abs);
|
|
48
|
-
}
|
|
49
|
-
/** Reads an extension folder into zip entries; the manifest must pass the gateway's own id rule. */
|
|
50
|
-
function collectExtensionFiles(dir) {
|
|
51
|
-
const root = path_1.default.resolve(dir);
|
|
52
|
-
for (const required of ['manifest.json', 'index.js']) {
|
|
53
|
-
if (!fs_1.default.lstatSync(insideFolder(root, required), { throwIfNoEntry: false }))
|
|
54
|
-
throw new Error(`${required} missing in ${root}`);
|
|
55
|
-
}
|
|
56
|
-
const manifest = JSON.parse(readPlainFile(root, 'manifest.json').toString('utf8'));
|
|
57
|
-
if (!(0, exports.isExtensionId)(manifest.id)) {
|
|
58
|
-
throw new Error(`manifest id ${JSON.stringify(manifest.id)} must be lowercase letters, digits and -`);
|
|
59
|
-
}
|
|
60
|
-
if (typeof manifest.name !== 'string' || !manifest.name)
|
|
61
|
-
throw new Error('manifest name is required');
|
|
62
|
-
readPlainFile(root, 'index.js');
|
|
63
|
-
const entries = [];
|
|
64
|
-
const walk = (rel, depth) => {
|
|
65
|
-
if (depth > exports.MAX_FOLDER_DEPTH)
|
|
66
|
-
throw new Error(`${rel} is nested deeper than ${exports.MAX_FOLDER_DEPTH} folders`);
|
|
67
|
-
for (const item of fs_1.default.readdirSync(insideFolder(root, rel), { withFileTypes: true })) {
|
|
68
|
-
if (item.name.startsWith('.') || SKIPPED.has(item.name))
|
|
69
|
-
continue;
|
|
70
|
-
const childRel = rel ? `${rel}/${item.name}` : item.name;
|
|
71
|
-
if (item.isSymbolicLink())
|
|
72
|
-
throw new Error(`${childRel} is a symlink; extensions are packaged without symlinks`);
|
|
73
|
-
if (item.isDirectory())
|
|
74
|
-
walk(childRel, depth + 1);
|
|
75
|
-
else if (item.isFile())
|
|
76
|
-
entries.push({ name: childRel, data: readPlainFile(root, childRel) });
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
walk('', 0);
|
|
80
|
-
return { manifest, entries };
|
|
81
|
-
}
|
|
82
|
-
/** An id already taken by an extension with another name is someone else's; only --force replaces it. */
|
|
83
|
-
function planInstall(incoming, installed, force) {
|
|
84
|
-
const existing = installed.find((e) => e.id === incoming.id);
|
|
85
|
-
if (!existing)
|
|
86
|
-
return { action: 'install' };
|
|
87
|
-
const from = existing.version ?? 'unknown';
|
|
88
|
-
if (existing.name !== incoming.name && !force) {
|
|
89
|
-
return {
|
|
90
|
-
action: 'refuse',
|
|
91
|
-
reason: `id '${incoming.id}' belongs to "${existing.name}" (v${from}), not "${incoming.name}". Use --force to replace it.`,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
return { action: 'update', from };
|
|
95
|
-
}
|
|
96
|
-
/** The answer of /api/extensions, or an error when another service holds Huddle's address. */
|
|
97
|
-
function asExtensionList(value) {
|
|
98
|
-
if (!Array.isArray(value))
|
|
99
|
-
throw new Error('could not reach Huddle: the address answered, but not with its extensions. Check HUDDLE_URL.');
|
|
100
|
-
return value;
|
|
101
|
-
}
|
|
102
|
-
async function runExtensionList() {
|
|
103
|
-
const installed = asExtensionList(await (0, api_1.get)('/api/extensions'));
|
|
104
|
-
if (!installed.length) {
|
|
105
|
-
console.log('No extensions installed.');
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
(0, utils_1.printTable)(['ID', 'NAME', 'VERSION', 'ENABLED'], installed.map((e) => [e.id, e.name, e.version ?? '-', e.enabled === false ? 'no' : 'yes']));
|
|
109
|
-
}
|
|
110
|
-
function hasHuddleContainer(rt) {
|
|
111
|
-
try {
|
|
112
|
-
(0, child_process_1.execFileSync)(rt, ['container', 'inspect', init_1.CONTAINER], { stdio: 'ignore' });
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
catch {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* The engine whose huddle container gets restarted after an install. An explicit
|
|
121
|
-
* --runtime / HUDDLE_RUNTIME wins; otherwise the engine that actually runs the
|
|
122
|
-
* container. Plain auto-detection prefers Docker, so with Podman owning Huddle
|
|
123
|
-
* it would restart an unrelated `huddle` container (or none) and leave the new
|
|
124
|
-
* extension inactive.
|
|
125
|
-
*/
|
|
126
|
-
function restartRuntime(explicit, hasContainer = hasHuddleContainer, detect = () => (0, runtime_1.resolveRuntime)().name) {
|
|
127
|
-
const requested = explicit ?? process.env.HUDDLE_RUNTIME;
|
|
128
|
-
if (requested !== undefined) {
|
|
129
|
-
// An empty value is a typo, not a request to auto-detect: probing could pick another engine's container.
|
|
130
|
-
if (!requested)
|
|
131
|
-
throw new Error('--runtime must not be empty; choose docker or podman.');
|
|
132
|
-
return (0, runtime_1.resolveRuntime)(requested).name;
|
|
133
|
-
}
|
|
134
|
-
const owners = ['docker', 'podman'].filter((rt) => hasContainer(rt));
|
|
135
|
-
if (owners.length === 1)
|
|
136
|
-
return owners[0];
|
|
137
|
-
if (owners.length === 0) {
|
|
138
|
-
throw new Error(`No ${init_1.CONTAINER} container found under docker or podman. Run huddle init first.`);
|
|
139
|
-
}
|
|
140
|
-
// Both commands see a huddle container: either `docker` is a Podman shim
|
|
141
|
-
// (one engine, detection reports podman) or two engines each run one.
|
|
142
|
-
if (detect() === 'podman')
|
|
143
|
-
return 'podman';
|
|
144
|
-
throw new Error(`Both docker and podman run a ${init_1.CONTAINER} container; pick one with --runtime <docker|podman>.`);
|
|
145
|
-
}
|
|
146
|
-
async function runExtensionInstall(dir, opts) {
|
|
147
|
-
const { manifest, entries } = collectExtensionFiles(dir);
|
|
148
|
-
const plan = planInstall(manifest, asExtensionList(await (0, api_1.get)('/api/extensions')), opts.force);
|
|
149
|
-
if (plan.action === 'refuse')
|
|
150
|
-
throw new Error(plan.reason);
|
|
151
|
-
const result = await (0, api_1.uploadFile)('/api/extensions/upload', `${manifest.id}.zip`, (0, zip_1.createZip)(entries));
|
|
152
|
-
const verb = plan.action === 'update' ? `Updated ${manifest.id} v${plan.from} → v${manifest.version ?? '?'}` : `Installed ${manifest.id} v${manifest.version ?? '?'}`;
|
|
153
|
-
console.log((0, utils_1.green)(`[OK] ${verb}`));
|
|
154
|
-
if (!result.restartRequired)
|
|
155
|
-
return;
|
|
156
|
-
if (!opts.restart) {
|
|
157
|
-
console.log((0, utils_1.yellow)('The gateway keeps running the old code until it restarts. Re-run with --restart, or restart the huddle container.'));
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
const rt = restartRuntime(opts.runtime);
|
|
161
|
-
console.log((0, utils_1.dim)(`Restarting the gateway process (${rt} restart ${init_1.CONTAINER}); the container itself is kept.`));
|
|
162
|
-
(0, child_process_1.execFileSync)(rt, ['restart', init_1.CONTAINER], { stdio: 'ignore' });
|
|
163
|
-
console.log((0, utils_1.green)('[OK] Gateway restarted with the new extension code.'));
|
|
164
|
-
}
|
|
165
|
-
async function runExtensionRemove(id) {
|
|
166
|
-
if (!(0, exports.isExtensionId)(id))
|
|
167
|
-
throw new Error(`invalid extension id: ${id}`);
|
|
168
|
-
await (0, api_1.del)(`/api/extensions/${id}`);
|
|
169
|
-
console.log((0, utils_1.green)(`[OK] Removed ${id}`));
|
|
170
|
-
}
|
package/dist/host-logs.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.HOST_CODEX_INDEX_MOUNT = exports.HOST_CODEX_LOGS_MOUNT = exports.HOST_AGENT_LOGS_MOUNT = void 0;
|
|
7
|
-
exports.hostAgentLogsMountArgs = hostAgentLogsMountArgs;
|
|
8
|
-
exports.hostCodexLogsMountArgs = hostCodexLogsMountArgs;
|
|
9
|
-
exports.hostCodexIndexMountArgs = hostCodexIndexMountArgs;
|
|
10
|
-
const fs_1 = __importDefault(require("fs"));
|
|
11
|
-
const path_1 = __importDefault(require("path"));
|
|
12
|
-
/** Must match HOST_AGENT_LOGS_MOUNT in gateway/src/host-config.ts. */
|
|
13
|
-
exports.HOST_AGENT_LOGS_MOUNT = '/host-logs/claude/projects';
|
|
14
|
-
/** Must match HOST_CODEX_LOGS_MOUNT in gateway/src/host-config.ts. */
|
|
15
|
-
exports.HOST_CODEX_LOGS_MOUNT = '/host-logs/codex/sessions';
|
|
16
|
-
/** Extensions read the Codex index here; the gateway itself never opens it. */
|
|
17
|
-
exports.HOST_CODEX_INDEX_MOUNT = '/host-logs/codex/session_index.jsonl';
|
|
18
|
-
/** A real directory only: Docker follows a symlinked source, which could point the mount at ~/.claude itself. */
|
|
19
|
-
const isPlainDir = (p) => {
|
|
20
|
-
try {
|
|
21
|
-
return fs_1.default.lstatSync(p).isDirectory();
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* `docker run` args that expose the host's Claude session logs to extensions:
|
|
29
|
-
* only ~/.claude/projects, read-only — never ~/.claude itself, which holds credentials.
|
|
30
|
-
*/
|
|
31
|
-
function hostAgentLogsMountArgs(cfg, homeDir, exists = isPlainDir) {
|
|
32
|
-
if (cfg.hostAgentLogs !== true)
|
|
33
|
-
return [];
|
|
34
|
-
const projects = path_1.default.join(homeDir, '.claude', 'projects');
|
|
35
|
-
return exists(projects) ? ['-v', `${projects}:${exports.HOST_AGENT_LOGS_MOUNT}:ro`] : [];
|
|
36
|
-
}
|
|
37
|
-
/** The same for Codex: only ~/.codex/sessions, read-only — never ~/.codex itself, which holds auth.json. */
|
|
38
|
-
function hostCodexLogsMountArgs(cfg, homeDir, exists = isPlainDir) {
|
|
39
|
-
if (cfg.hostAgentLogs !== true)
|
|
40
|
-
return [];
|
|
41
|
-
const sessions = path_1.default.join(homeDir, '.codex', 'sessions');
|
|
42
|
-
return exists(sessions) ? ['-v', `${sessions}:${exports.HOST_CODEX_LOGS_MOUNT}:ro`] : [];
|
|
43
|
-
}
|
|
44
|
-
/** A plain file only: a symlink or hard link here could point the mount at auth.json. */
|
|
45
|
-
const isPlainFile = (p) => {
|
|
46
|
-
try {
|
|
47
|
-
const stat = fs_1.default.lstatSync(p);
|
|
48
|
-
return stat.isFile() && stat.nlink === 1;
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
/** Codex keeps thread names in ~/.codex/session_index.jsonl; only that file is mounted, read-only. */
|
|
55
|
-
function hostCodexIndexMountArgs(cfg, homeDir, exists = isPlainFile) {
|
|
56
|
-
if (cfg.hostAgentLogs !== true)
|
|
57
|
-
return [];
|
|
58
|
-
const index = path_1.default.join(homeDir, '.codex', 'session_index.jsonl');
|
|
59
|
-
return exists(index) ? ['-v', `${index}:${exports.HOST_CODEX_INDEX_MOUNT}:ro`] : [];
|
|
60
|
-
}
|
package/dist/indexfolder.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.runIndexFolder = runIndexFolder;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const api_1 = require("./api");
|
|
10
|
-
const utils_1 = require("./utils");
|
|
11
|
-
// `huddle indexfolder` — make host folders selectable in the portal.
|
|
12
|
-
//
|
|
13
|
-
// The portal runs inside a container: it cannot open a file dialog on the host,
|
|
14
|
-
// so every host path had to be typed from memory. This command walks the host
|
|
15
|
-
// filesystem where the operator already is (their projects folder) and posts the
|
|
16
|
-
// folders it finds to the gateway, which stores them as an index. The portal then
|
|
17
|
-
// offers that index wherever a host path is needed.
|
|
18
|
-
//
|
|
19
|
-
// Deliberately a snapshot, not a live view: the gateway has no way to read the
|
|
20
|
-
// host filesystem, and re-running the command is cheap.
|
|
21
|
-
// Folders that are never a workspace choice but do contain thousands of
|
|
22
|
-
// subfolders. Skipping them is what keeps a scan of a projects folder in the
|
|
23
|
-
// hundreds rather than the tens of thousands. `--all` turns this off; hidden
|
|
24
|
-
// (dot) folders stay skipped either way.
|
|
25
|
-
const NOISE = new Set([
|
|
26
|
-
'node_modules', 'dist', 'build', 'out', 'bin', 'obj', 'target', 'vendor',
|
|
27
|
-
'venv', '__pycache__', 'coverage', 'packages', 'AppData', 'Library',
|
|
28
|
-
'$RECYCLE.BIN', 'System Volume Information', 'Windows', 'Program Files',
|
|
29
|
-
'Program Files (x86)', 'ProgramData',
|
|
30
|
-
]);
|
|
31
|
-
// Hard stop on discovery, well under the gateway's own cap. Pointing the command
|
|
32
|
-
// at a drive root should end in a clear warning, not a ten-minute walk.
|
|
33
|
-
const MAX_FOLDERS = 1500;
|
|
34
|
-
// Host paths travel to the gateway with forward slashes: `path.resolve` hands
|
|
35
|
-
// back `T:\projects\app` on Windows, and the whole stack (the portal, the index,
|
|
36
|
-
// the Docker mount translation) speaks the forward-slash form. The gateway
|
|
37
|
-
// normalizes again on ingest — this is only so what we PRINT matches what gets
|
|
38
|
-
// stored.
|
|
39
|
-
function toPosix(p) {
|
|
40
|
-
return p.replace(/\\/g, '/').replace(/^([a-z]):/, (_m, d) => `${d.toUpperCase()}:`);
|
|
41
|
-
}
|
|
42
|
-
function parseDepth(raw) {
|
|
43
|
-
if (raw === undefined)
|
|
44
|
-
return 2;
|
|
45
|
-
const n = Number(raw);
|
|
46
|
-
if (!Number.isInteger(n) || n < 0 || n > 8) {
|
|
47
|
-
throw new Error(`Invalid --depth "${raw}": use a whole number between 0 and 8.`);
|
|
48
|
-
}
|
|
49
|
-
return n;
|
|
50
|
-
}
|
|
51
|
-
// The folder to walk comes from argv, or from wherever the operator's shell is.
|
|
52
|
-
// Resolving it here — once, before anything touches the filesystem — is what lets
|
|
53
|
-
// the rest of the command treat "the root" as one absolute, `..`-free path: the
|
|
54
|
-
// walk below, the containment check, and the paths posted to the gateway all
|
|
55
|
-
// derive from this value.
|
|
56
|
-
function resolveScanRoot(raw) {
|
|
57
|
-
const input = raw ?? process.cwd();
|
|
58
|
-
// A null byte makes every fs call throw ERR_INVALID_ARG_VALUE with a stack
|
|
59
|
-
// trace; say what is wrong instead.
|
|
60
|
-
if (input.includes('\0'))
|
|
61
|
-
throw new Error('Invalid folder: the path contains a null byte.');
|
|
62
|
-
return path_1.default.resolve(input);
|
|
63
|
-
}
|
|
64
|
-
// Whether `candidate` is the root or sits underneath it.
|
|
65
|
-
function contains(root, candidate) {
|
|
66
|
-
const rel = path_1.default.relative(root, candidate);
|
|
67
|
-
return rel === '' || (!rel.startsWith('..') && !path_1.default.isAbsolute(rel));
|
|
68
|
-
}
|
|
69
|
-
// Breadth-first so a depth cut-off keeps the folders nearest the root — those are
|
|
70
|
-
// the ones an operator actually mounts. Symlinks are not followed: `isDirectory()`
|
|
71
|
-
// is false for a symlink entry, which also rules out a link loop.
|
|
72
|
-
function scan(root, depth, all) {
|
|
73
|
-
const folders = [root];
|
|
74
|
-
let level = [root];
|
|
75
|
-
for (let d = 0; d < depth; d++) {
|
|
76
|
-
const next = [];
|
|
77
|
-
for (const dir of level) {
|
|
78
|
-
let entries;
|
|
79
|
-
try {
|
|
80
|
-
entries = fs_1.default.readdirSync(dir, { withFileTypes: true });
|
|
81
|
-
}
|
|
82
|
-
catch {
|
|
83
|
-
continue; // unreadable folder (permissions, a vanished dir) — skip it
|
|
84
|
-
}
|
|
85
|
-
for (const e of entries) {
|
|
86
|
-
if (!e.isDirectory())
|
|
87
|
-
continue;
|
|
88
|
-
if (e.name.startsWith('.'))
|
|
89
|
-
continue;
|
|
90
|
-
if (!all && NOISE.has(e.name))
|
|
91
|
-
continue;
|
|
92
|
-
const full = path_1.default.join(dir, e.name);
|
|
93
|
-
// Nothing outside the folder the operator named ever enters the index or
|
|
94
|
-
// gets opened by the next round of readdirSync. Entries come from
|
|
95
|
-
// readdirSync and symlinks are already skipped, so this should never
|
|
96
|
-
// fire — it is here so a scan can never walk out of its root by accident.
|
|
97
|
-
if (!contains(root, full))
|
|
98
|
-
continue;
|
|
99
|
-
folders.push(full);
|
|
100
|
-
next.push(full);
|
|
101
|
-
if (folders.length >= MAX_FOLDERS)
|
|
102
|
-
return { folders, truncated: true };
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
level = next;
|
|
106
|
-
if (level.length === 0)
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
return { folders, truncated: false };
|
|
110
|
-
}
|
|
111
|
-
async function runIndexFolder(opts) {
|
|
112
|
-
if (opts.list) {
|
|
113
|
-
const res = await (0, api_1.get)('/api/indexed-folders');
|
|
114
|
-
if (res.folders.length === 0) {
|
|
115
|
-
console.log((0, utils_1.dim)('No folders indexed yet. Run `huddle indexfolder` in your projects folder.'));
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
(0, utils_1.printTable)(['ID', 'PATH', 'SOURCE'], res.folders.map((f) => [String(f.id), f.path, f.source]));
|
|
119
|
-
console.log((0, utils_1.dim)(`\n${res.folders.length} folder(s) indexed (max ${res.max}).`));
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
if (opts.clear) {
|
|
123
|
-
// Scoped when a folder is given, so clearing one project's entries does not
|
|
124
|
-
// throw away everything else that was indexed.
|
|
125
|
-
const root = opts.path ? toPosix(resolveScanRoot(opts.path)) : undefined;
|
|
126
|
-
const query = root ? `?root=${encodeURIComponent(root)}` : '';
|
|
127
|
-
const res = await (0, api_1.del)(`/api/indexed-folders${query}`);
|
|
128
|
-
console.log((0, utils_1.green)(`[OK] Removed ${res.removed} folder(s) from the index${root ? ` under ${(0, utils_1.cyan)(root)}` : ''}.`));
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
const depth = parseDepth(opts.depth);
|
|
132
|
-
const root = resolveScanRoot(opts.path);
|
|
133
|
-
let stat;
|
|
134
|
-
try {
|
|
135
|
-
stat = fs_1.default.statSync(root);
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
throw new Error(`Folder does not exist: ${root}`);
|
|
139
|
-
}
|
|
140
|
-
if (!stat.isDirectory())
|
|
141
|
-
throw new Error(`Not a folder: ${root}`);
|
|
142
|
-
const posixRoot = toPosix(root);
|
|
143
|
-
console.log(`Indexing ${(0, utils_1.bold)(posixRoot)} ${(0, utils_1.dim)(`(depth ${depth}${opts.all ? ', including build folders' : ''})`)}`);
|
|
144
|
-
const { folders, truncated } = scan(root, depth, opts.all === true);
|
|
145
|
-
if (truncated) {
|
|
146
|
-
console.log((0, utils_1.yellow)(`[!] Stopped at ${MAX_FOLDERS} folders. Index a more specific folder, or lower --depth.`));
|
|
147
|
-
}
|
|
148
|
-
const res = await (0, api_1.post)('/api/indexed-folders', {
|
|
149
|
-
paths: folders.map(toPosix),
|
|
150
|
-
root: posixRoot,
|
|
151
|
-
replace: opts.replace === true,
|
|
152
|
-
});
|
|
153
|
-
if (res.removed)
|
|
154
|
-
console.log((0, utils_1.dim)(` Replaced: removed ${res.removed} earlier entr(y|ies) under this folder.`));
|
|
155
|
-
console.log((0, utils_1.green)(`[OK] ${res.added} added, ${res.updated} already known — ${res.total} folder(s) in the index.`));
|
|
156
|
-
if (res.skipped) {
|
|
157
|
-
console.log((0, utils_1.yellow)(`[!] ${res.skipped} folder(s) skipped (index limit ${res.max} reached, or a duplicate spelling).`));
|
|
158
|
-
}
|
|
159
|
-
for (const bad of res.invalid.slice(0, 5)) {
|
|
160
|
-
console.log((0, utils_1.yellow)(`[!] Refused ${bad.path}: ${bad.error}`));
|
|
161
|
-
}
|
|
162
|
-
if (res.invalid.length > 5)
|
|
163
|
-
console.log((0, utils_1.yellow)(`[!] ...and ${res.invalid.length - 5} more refused.`));
|
|
164
|
-
console.log();
|
|
165
|
-
console.log('Pick these folders in the portal when starting a devcontainer,');
|
|
166
|
-
console.log(`or manage the index under ${(0, utils_1.cyan)('Settings > Indexed folders')}.`);
|
|
167
|
-
}
|
package/dist/zip.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createZip = createZip;
|
|
7
|
-
const zlib_1 = __importDefault(require("zlib"));
|
|
8
|
-
const CRC_TABLE = (() => {
|
|
9
|
-
const table = new Uint32Array(256);
|
|
10
|
-
for (let n = 0; n < 256; n++) {
|
|
11
|
-
let c = n;
|
|
12
|
-
for (let k = 0; k < 8; k++)
|
|
13
|
-
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
14
|
-
table[n] = c >>> 0;
|
|
15
|
-
}
|
|
16
|
-
return table;
|
|
17
|
-
})();
|
|
18
|
-
function crc32(data) {
|
|
19
|
-
let crc = 0xffffffff;
|
|
20
|
-
for (const byte of data)
|
|
21
|
-
crc = CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);
|
|
22
|
-
return (crc ^ 0xffffffff) >>> 0;
|
|
23
|
-
}
|
|
24
|
-
function assertSafeName(name) {
|
|
25
|
-
const segments = name.split('/');
|
|
26
|
-
if (!name || name.startsWith('/') || name.includes('\\') || segments.some((s) => s === '..' || s === '')) {
|
|
27
|
-
throw new Error(`invalid zip entry name: ${JSON.stringify(name)}`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/** Builds a deflated zip archive with UTF-8 entry names, in the order given. */
|
|
31
|
-
function createZip(entries) {
|
|
32
|
-
const UTF8_NAMES = 0x0800;
|
|
33
|
-
const DEFLATE = 8;
|
|
34
|
-
const locals = [];
|
|
35
|
-
const centrals = [];
|
|
36
|
-
let offset = 0;
|
|
37
|
-
for (const entry of entries) {
|
|
38
|
-
assertSafeName(entry.name);
|
|
39
|
-
const name = Buffer.from(entry.name, 'utf8');
|
|
40
|
-
const packed = zlib_1.default.deflateRawSync(entry.data);
|
|
41
|
-
const crc = crc32(entry.data);
|
|
42
|
-
const local = Buffer.alloc(30);
|
|
43
|
-
local.writeUInt32LE(0x04034b50, 0);
|
|
44
|
-
local.writeUInt16LE(20, 4);
|
|
45
|
-
local.writeUInt16LE(UTF8_NAMES, 6);
|
|
46
|
-
local.writeUInt16LE(DEFLATE, 8);
|
|
47
|
-
local.writeUInt32LE(crc, 14);
|
|
48
|
-
local.writeUInt32LE(packed.length, 18);
|
|
49
|
-
local.writeUInt32LE(entry.data.length, 22);
|
|
50
|
-
local.writeUInt16LE(name.length, 26);
|
|
51
|
-
locals.push(local, name, packed);
|
|
52
|
-
const central = Buffer.alloc(46);
|
|
53
|
-
central.writeUInt32LE(0x02014b50, 0);
|
|
54
|
-
central.writeUInt16LE(20, 4);
|
|
55
|
-
central.writeUInt16LE(20, 6);
|
|
56
|
-
central.writeUInt16LE(UTF8_NAMES, 8);
|
|
57
|
-
central.writeUInt16LE(DEFLATE, 10);
|
|
58
|
-
central.writeUInt32LE(crc, 16);
|
|
59
|
-
central.writeUInt32LE(packed.length, 20);
|
|
60
|
-
central.writeUInt32LE(entry.data.length, 24);
|
|
61
|
-
central.writeUInt16LE(name.length, 28);
|
|
62
|
-
central.writeUInt32LE(offset, 42);
|
|
63
|
-
centrals.push(central, name);
|
|
64
|
-
offset += local.length + name.length + packed.length;
|
|
65
|
-
}
|
|
66
|
-
const centralDir = Buffer.concat(centrals);
|
|
67
|
-
const end = Buffer.alloc(22);
|
|
68
|
-
end.writeUInt32LE(0x06054b50, 0);
|
|
69
|
-
end.writeUInt16LE(entries.length, 8);
|
|
70
|
-
end.writeUInt16LE(entries.length, 10);
|
|
71
|
-
end.writeUInt32LE(centralDir.length, 12);
|
|
72
|
-
end.writeUInt32LE(offset, 16);
|
|
73
|
-
return Buffer.concat([...locals, centralDir, end]);
|
|
74
|
-
}
|