Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { existsSync, readFileSync } from 'node:fs'; |
| 2 | import { join } from 'node:path'; |
| 3 | import { INSTALL_E2E_LOCAL_RELEASE_TAG } from '@e2e-tests/utils/const'; |
| 4 | |
| 5 | const ARCHIVE_BY_ARCH = { |
| 6 | arm64: 'composio-linux-aarch64.zip', |
| 7 | x64: 'composio-linux-x64.zip', |
| 8 | } as const; |
| 9 | |
| 10 | /** |
| 11 | * Body served in place of the release archive under the failure-mode prefixes. |
| 12 | * |
| 13 | * - `/corrupt/...` also serves a checksums.txt that matches this body, so the |
| 14 | * installer passes checksum verification and fails at extraction. |
| 15 | * - `/checksum-mismatch/...` keeps the genuine checksums.txt, so the installer |
| 16 | * fails at checksum verification before extraction. |
| 17 | * - `/missing-asset/...` answers 404 for the archive, as GitHub does for a |
| 18 | * release that has no build for the platform. |
| 19 | * - `/truncated/...` starts sending the genuine archive and drops the |
| 20 | * connection partway, as an interrupted download does. |
| 21 | */ |
| 22 | const CORRUPT_ARCHIVE_BODY = 'not a zip archive\n'; |
| 23 | |
| 24 | /** The first bytes of the archive at `path`, then a dropped connection. */ |
| 25 | const truncatedResponse = (path: string): Response => { |
| 26 | const head = readFileSync(path).subarray(0, 64 * 1024); |
| 27 | return new Response( |
| 28 | new ReadableStream<Uint8Array>({ |
| 29 | async pull(controller) { |
| 30 | controller.enqueue(head); |
| 31 | // Let the chunk reach curl before the stream fails. |
| 32 | await Bun.sleep(100); |
| 33 | // Failing the stream is how Bun.serve drops a connection mid-body. Closing |
| 34 | // it early instead ends the response cleanly and curl accepts the short file. |
| 35 | controller.error('install e2e: download dropped on purpose'); |
| 36 | }, |
| 37 | }), |
| 38 | { headers: { 'content-type': 'application/zip' } } |
| 39 | ); |
| 40 | }; |
| 41 | |
| 42 | export interface InstallReleaseServer { |
| 43 | baseUrl: string; |
| 44 | platform: 'linux/amd64' | 'linux/arm64'; |
| 45 | stop: () => void; |
| 46 | } |
| 47 | |
| 48 | export function startInstallReleaseServer(options: { |
| 49 | repoRoot: string; |
| 50 | releaseDir: string; |
| 51 | }): InstallReleaseServer { |
| 52 | const preferredArch = process.arch === 'arm64' ? 'arm64' : 'x64'; |
| 53 | const archiveName = ARCHIVE_BY_ARCH[preferredArch]; |
| 54 | const archivePath = join(options.releaseDir, archiveName); |
| 55 | const checksumsPath = join(options.releaseDir, 'checksums.txt'); |
| 56 | if (!existsSync(archivePath) || !existsSync(checksumsPath)) { |
| 57 | throw new Error( |
| 58 | `Local install fixture must contain ${archiveName} and checksums.txt: ${options.releaseDir}` |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | const scripts = new Map([ |
| 63 | ['/install', join(options.repoRoot, 'install.sh')], |
| 64 | ['/install/bash', join(options.repoRoot, 'install/bash.sh')], |
| 65 | ['/install/zsh', join(options.repoRoot, 'install/zsh.sh')], |
| 66 | ['/install/fish', join(options.repoRoot, 'install/fish.sh')], |
| 67 | ]); |
| 68 | |
| 69 | let dockerBaseUrl = ''; |
| 70 | const server = Bun.serve({ |
| 71 | hostname: '0.0.0.0', |
| 72 | port: 0, |
| 73 | fetch(request) { |
| 74 | const url = new URL(request.url); |
| 75 | const scriptPath = scripts.get(url.pathname); |
| 76 | if (scriptPath) { |
| 77 | return new Response(Bun.file(scriptPath), { |
| 78 | headers: { 'content-type': 'text/x-shellscript; charset=utf-8' }, |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | if (/^\/repos\/[^/]+\/[^/]+\/releases$/.test(url.pathname)) { |
| 83 | return Response.json([ |
| 84 | { |
| 85 | tag_name: INSTALL_E2E_LOCAL_RELEASE_TAG, |
| 86 | draft: false, |
| 87 | prerelease: false, |
| 88 | assets: [ |
| 89 | { |
| 90 | name: archiveName, |
| 91 | browser_download_url: `${dockerBaseUrl}/ComposioHQ/composio/releases/download/${encodeURIComponent(INSTALL_E2E_LOCAL_RELEASE_TAG)}/${archiveName}`, |
| 92 | }, |
| 93 | ], |
| 94 | }, |
| 95 | ]); |
| 96 | } |
| 97 | |
| 98 | if (url.pathname.includes('/releases/download/') && url.pathname.endsWith(archiveName)) { |
| 99 | if (url.pathname.startsWith('/missing-asset/')) { |
| 100 | return new Response('Not Found\n', { status: 404 }); |
| 101 | } |
| 102 | if (url.pathname.startsWith('/truncated/')) { |
| 103 | return truncatedResponse(archivePath); |
| 104 | } |
| 105 | if ( |
| 106 | url.pathname.startsWith('/corrupt/') || |
| 107 | url.pathname.startsWith('/checksum-mismatch/') |
| 108 | ) { |
| 109 | return new Response(CORRUPT_ARCHIVE_BODY, { |
| 110 | headers: { 'content-type': 'application/zip' }, |
| 111 | }); |
| 112 | } |
| 113 | return new Response(Bun.file(archivePath), { |
| 114 | headers: { 'content-type': 'application/zip' }, |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | if (url.pathname.includes('/releases/download/') && url.pathname.endsWith('checksums.txt')) { |
| 119 | if (url.pathname.startsWith('/corrupt/')) { |
| 120 | const corruptDigest = new Bun.CryptoHasher('sha256') |
| 121 | .update(CORRUPT_ARCHIVE_BODY) |
| 122 | .digest('hex'); |
| 123 | return new Response(`${corruptDigest} ${archiveName}\n`, { |
| 124 | headers: { 'content-type': 'text/plain; charset=utf-8' }, |
| 125 | }); |
| 126 | } |
| 127 | return new Response(readFileSync(checksumsPath), { |
| 128 | headers: { 'content-type': 'text/plain; charset=utf-8' }, |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | return new Response('not found\n', { status: 404 }); |
| 133 | }, |
| 134 | }); |
| 135 | |
| 136 | dockerBaseUrl = `http://host.docker.internal:${server.port}`; |
| 137 | return { |
| 138 | baseUrl: dockerBaseUrl, |
| 139 | platform: preferredArch === 'arm64' ? 'linux/arm64' : 'linux/amd64', |
| 140 | stop: () => server.stop(true), |
| 141 | }; |
| 142 | } |