|
| 1 | +import {createHash} from 'node:crypto'; |
| 2 | +import {once} from 'node:events'; |
| 3 | +import {createServer} from 'node:http'; |
| 4 | +import {gzipSync} from 'node:zlib'; |
| 5 | + |
| 6 | +function createSimpleTarArchive(fileName, fileContent, mode = 0o644) { |
| 7 | + const contentBuffer = Buffer.from(fileContent); |
| 8 | + |
| 9 | + const header = Buffer.alloc(512); // TAR headers are 512 bytes |
| 10 | + header.write(fileName); |
| 11 | + header.write(`100${mode.toString(8)} `, 100, 7, `utf-8`); // File mode (octal) followed by a space |
| 12 | + header.write(`0001750 `, 108, 8, `utf-8`); // Owner's numeric user ID (octal) followed by a space |
| 13 | + header.write(`0001750 `, 116, 8, `utf-8`); // Group's numeric user ID (octal) followed by a space |
| 14 | + header.write(`${contentBuffer.length.toString(8)} `, 124, 12, `utf-8`); // File size in bytes (octal) followed by a space |
| 15 | + header.write(`${Math.floor(Date.now() / 1000).toString(8)} `, 136, 12, `utf-8`); // Last modification time in numeric Unix time format (octal) followed by a space |
| 16 | + header.fill(` `, 148, 156); // Fill checksum area with spaces for calculation |
| 17 | + header.write(`ustar `, 257, 8, `utf-8`); // UStar indicator |
| 18 | + |
| 19 | + // Calculate and write the checksum. Note: This is a simplified calculation not recommended for production |
| 20 | + const checksum = header.reduce((sum, value) => sum + value, 0); |
| 21 | + header.write(`${checksum.toString(8)}\0 `, 148, 8, `utf-8`); // Write checksum in octal followed by null and space |
| 22 | + |
| 23 | + |
| 24 | + return Buffer.concat([ |
| 25 | + header, |
| 26 | + contentBuffer, |
| 27 | + Buffer.alloc(512 - (contentBuffer.length % 512)), |
| 28 | + ]); |
| 29 | +} |
| 30 | + |
| 31 | +const mockPackageTarGz = gzipSync(Buffer.concat([ |
| 32 | + createSimpleTarArchive(`package/bin/customPkgManager.js`, `#!/usr/bin/env node\nconsole.log("customPkgManager: Hello from custom registry");\n`, 0o755), |
| 33 | + createSimpleTarArchive(`package/bin/pnpm.js`, `#!/usr/bin/env node\nconsole.log("pnpm: Hello from custom registry");\n`, 0o755), |
| 34 | + createSimpleTarArchive(`package/bin/yarn.js`, `#!/usr/bin/env node\nconsole.log("yarn: Hello from custom registry");\n`, 0o755), |
| 35 | + createSimpleTarArchive(`package/package.json`, JSON.stringify({bin: {yarn: `bin/yarn.js`, pnpm: `bin/pnpm.js`, customPkgManager: `bin/customPkgManager.js`}})), |
| 36 | + Buffer.alloc(1024), |
| 37 | +])); |
| 38 | +const shasum = createHash(`sha1`).update(mockPackageTarGz).digest(`hex`); |
| 39 | + |
| 40 | + |
| 41 | +const server = createServer((req, res) => { |
| 42 | + const auth = req.headers.authorization; |
| 43 | + if (!auth?.startsWith(`Bearer `) || Buffer.from(auth.slice(`Bearer `.length), `base64`).toString() !== `user:pass`) { |
| 44 | + res.statusCode = 401; |
| 45 | + res.end(`Unauthorized`); |
| 46 | + return; |
| 47 | + } |
| 48 | + switch (req.url) { |
| 49 | + case `/yarn`: { |
| 50 | + res.end(JSON.stringify({"dist-tags": { |
| 51 | + latest: `1.9998.9999`, |
| 52 | + }, versions: {'1.9998.9999': { |
| 53 | + dist: { |
| 54 | + shasum, |
| 55 | + size: mockPackageTarGz.length, |
| 56 | + noattachment: false, |
| 57 | + tarball: `${process.env.COREPACK_NPM_REGISTRY}/yarn.tgz`, |
| 58 | + }, |
| 59 | + }}})); |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + case `/pnpm`: { |
| 64 | + res.end(JSON.stringify({"dist-tags": { |
| 65 | + latest: `1.9998.9999`, |
| 66 | + }, versions: {'1.9998.9999': { |
| 67 | + dist: { |
| 68 | + shasum, |
| 69 | + size: mockPackageTarGz.length, |
| 70 | + noattachment: false, |
| 71 | + tarball: `${process.env.COREPACK_NPM_REGISTRY}/pnpm/-/pnpm-1.9998.9999.tgz`, |
| 72 | + }, |
| 73 | + }}})); |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + case `/@yarnpkg/cli-dist`: { |
| 78 | + res.end(JSON.stringify({"dist-tags": { |
| 79 | + latest: `5.9999.9999`, |
| 80 | + }, versions: {'5.9999.9999': { |
| 81 | + bin: { |
| 82 | + yarn: `./bin/yarn.js`, |
| 83 | + yarnpkg: `./bin/yarn.js`, |
| 84 | + }, |
| 85 | + dist: { |
| 86 | + shasum, |
| 87 | + size: mockPackageTarGz.length, |
| 88 | + noattachment: false, |
| 89 | + tarball: `${process.env.COREPACK_NPM_REGISTRY}/yarn.tgz`, |
| 90 | + }, |
| 91 | + }}})); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + case `/customPkgManager`: { |
| 96 | + res.end(JSON.stringify({"dist-tags": { |
| 97 | + latest: `1.0.0`, |
| 98 | + }, versions: {'1.0.0': { |
| 99 | + bin: { |
| 100 | + customPkgManager: `./bin/customPkgManager.js`, |
| 101 | + }, |
| 102 | + dist: { |
| 103 | + shasum, |
| 104 | + size: mockPackageTarGz.length, |
| 105 | + noattachment: false, |
| 106 | + tarball: `${process.env.COREPACK_NPM_REGISTRY}/customPkgManager/-/customPkgManager-1.0.0.tgz`, |
| 107 | + }, |
| 108 | + }}})); |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + case `/pnpm/-/pnpm-1.9998.9999.tgz`: |
| 113 | + case `/yarn.tgz`: |
| 114 | + case `/customPkgManager/-/customPkgManager-1.0.0.tgz`: |
| 115 | + res.end(mockPackageTarGz); |
| 116 | + break; |
| 117 | + |
| 118 | + default: |
| 119 | + throw new Error(`unsupported request`, {cause: req.url}); |
| 120 | + } |
| 121 | +}).listen(0, `localhost`); |
| 122 | + |
| 123 | +await once(server, `listening`); |
| 124 | + |
| 125 | +const {address, port} = server.address(); |
| 126 | +switch (process.env.AUTH_TYPE) { |
| 127 | + case `COREPACK_NPM_REGISTRY`: |
| 128 | + process.env.COREPACK_NPM_REGISTRY = `http://user:pass@${address.includes(`:`) ? `[${address}]` : address}:${port}`; |
| 129 | + break; |
| 130 | + |
| 131 | + case `COREPACK_NPM_TOKEN`: |
| 132 | + process.env.COREPACK_NPM_REGISTRY = `http://${address.includes(`:`) ? `[${address}]` : address}:${port}`; |
| 133 | + process.env.COREPACK_NPM_TOKEN = Buffer.from(`user:pass`).toString(`base64`); |
| 134 | + break; |
| 135 | + |
| 136 | + case `COREPACK_NPM_PASSWORD`: |
| 137 | + process.env.COREPACK_NPM_REGISTRY = `http://${address.includes(`:`) ? `[${address}]` : address}:${port}`; |
| 138 | + process.env.COREPACK_NPM_USER = `user`; |
| 139 | + process.env.COREPACK_NPM_PASSWORD = `pass`; |
| 140 | + break; |
| 141 | + |
| 142 | + default: throw new Error(`Invalid AUTH_TYPE in env`, {cause: process.env.AUTH_TYPE}); |
| 143 | +} |
| 144 | + |
| 145 | +if (process.env.NOCK_ENV === `replay`) { |
| 146 | + const originalFetch = globalThis.fetch; |
| 147 | + globalThis.fetch = function fetch(i) { |
| 148 | + if (!`${i}`.startsWith(`http://${address.includes(`:`) ? `[${address}]` : address}:${port}`)) |
| 149 | + throw new Error; |
| 150 | + |
| 151 | + return Reflect.apply(originalFetch, this, arguments); |
| 152 | + }; |
| 153 | +} |
| 154 | + |
| 155 | +server.unref(); |
0 commit comments