@forge/cli

14.2.015.0.0-next.23
out/service/skills-download.js
+out/service/skills-download.jsNew file
+92
Index: package/out/service/skills-download.js
===================================================================
--- package/out/service/skills-download.js
+++ package/out/service/skills-download.js
@@ -0,0 +1,92 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.SkillsDownloader = exports.skillOf = exports.SKILL_NAME = exports.FORGE_MCP_SERVER_NAMES = void 0;
+const tslib_1 = require("tslib");
+const adm_zip_1 = tslib_1.__importDefault(require("adm-zip"));
+const cli_shared_1 = require("@forge/cli-shared");
+exports.FORGE_MCP_SERVER_NAMES = ['forge', 'ads-mcp'];
+exports.SKILL_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
+const MAX_ARCHIVE_BYTES = 50 * 1024 * 1024;
+const MAX_UNPACKED_BYTES = 100 * 1024 * 1024;
+const skillOf = (path) => path.split('/')[0];
+exports.skillOf = skillOf;
+class SkillsDownloader {
+    send;
+    constructor(send = cli_shared_1.sendWithNodeRequest) {
+        this.send = send;
+    }
+    async fetchLatest() {
+        let archive;
+        try {
+            const base = process.env.FORGE_SKILLS_DOWNLOAD_BASE_URL ?? 'https://codeload.github.com';
+            const url = new URL('atlassian/forge-skills/zip/main', base.endsWith('/') ? base : `${base}/`);
+            const response = await this.send({ method: 'GET', url: url.href });
+            if (!response.ok)
+                throw new Error(`HTTP ${response.status}`);
+            archive = Buffer.from(await response.arrayBuffer());
+        }
+        catch {
+            throw new cli_shared_1.UserError(cli_shared_1.Text.skills.errors.downloadFailed);
+        }
+        try {
+            return readRelease(archive);
+        }
+        catch {
+            throw new cli_shared_1.UserError(cli_shared_1.Text.skills.errors.invalidRelease);
+        }
+    }
+}
+exports.SkillsDownloader = SkillsDownloader;
+function readRelease(archive) {
+    if (archive.length > MAX_ARCHIVE_BYTES)
+        throw new Error('Archive too large');
+    const zip = new adm_zip_1.default(archive);
+    const entries = zip.getEntries();
+    const root = entries[0]?.entryName.split('/')[0];
+    let unpacked = 0;
+    for (const entry of entries) {
+        const parts = entry.entryName.split('/');
+        const symlink = ((entry.attr >>> 16) & 0o170000) === 0o120000;
+        unpacked += entry.header.size;
+        if (!root || parts[0] !== root || parts.includes('..') || entry.entryName.includes('\\') || symlink) {
+            throw new Error('Unsafe archive entry');
+        }
+        if (unpacked > MAX_UNPACKED_BYTES)
+            throw new Error('Archive too large');
+    }
+    const prefix = `${root}/skills/`;
+    const files = entries
+        .filter((entry) => !entry.isDirectory && entry.entryName.startsWith(prefix))
+        .map((entry) => ({
+        path: entry.entryName.slice(prefix.length),
+        data: entry.getData(),
+        mode: (entry.attr >>> 16) & 0o777 || 0o644
+    }));
+    const skills = files
+        .filter((file) => file.path === `${(0, exports.skillOf)(file.path)}/SKILL.md` && exports.SKILL_NAME.test((0, exports.skillOf)(file.path)))
+        .map((file) => (0, exports.skillOf)(file.path))
+        .sort();
+    if (!skills.length)
+        throw new Error('No skills');
+    return {
+        skills,
+        files: files.filter((file) => skills.includes((0, exports.skillOf)(file.path))),
+        servers: readServers(JSON.parse(zip.readAsText(`${root}/mcp.json`)))
+    };
+}
+function readServers(config) {
+    const servers = {};
+    for (const name of exports.FORGE_MCP_SERVER_NAMES) {
+        const { type, url, headers } = config.mcpServers?.[name] ?? {};
+        const validHeaders = headers === undefined ||
+            (typeof headers === 'object' &&
+                headers !== null &&
+                Object.values(headers).every((value) => typeof value === 'string'));
+        if (type !== 'streamable-http' || typeof url !== 'string' || !/^https?:\/\//.test(url) || !validHeaders) {
+            throw new Error(`Invalid ${name} server`);
+        }
+        servers[name] = headers ? { type, url, headers: { ...headers } } : { type, url };
+    }
+    return servers;
+}
+//# sourceMappingURL=skills-download.js.map
\ No newline at end of file