Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix tests on custom registry #445

Merged
merged 3 commits into from Apr 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 61 additions & 70 deletions tests/_registryServer.mjs
Expand Up @@ -36,87 +36,78 @@ const mockPackageTarGz = gzipSync(Buffer.concat([
Buffer.alloc(1024),
]));
const shasum = createHash(`sha1`).update(mockPackageTarGz).digest(`hex`);

const integrity = `sha512-${createHash(`sha512`).update(mockPackageTarGz).digest(`base64`)}`;

const registry = {
__proto__: null,
yarn: [`1.9998.9999`],
pnpm: [`1.9998.9999`],
// eslint-disable-next-line @typescript-eslint/naming-convention
'@yarnpkg/cli-dist': [`5.9999.9999`],
customPkgManager: [`1.0.0`],
};

function getVersionMetadata(packageName, version) {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
return {
name: packageName,
version,
bin: {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
[packageName]: `./bin/${packageName}.js`,
},
dist: {
integrity,
shasum,
size: mockPackageTarGz.length,
noattachment: false,
tarball: `${process.env.COREPACK_NPM_REGISTRY}/${packageName}/-/${packageName}-${version}.tgz`,
},
};
}

const server = createServer((req, res) => {
const auth = req.headers.authorization;
if (!auth?.startsWith(`Bearer `) || Buffer.from(auth.slice(`Bearer `.length), `base64`).toString() !== `user:pass`) {
res.statusCode = 401;
res.end(`Unauthorized`);
res.writeHead(401).end(`Unauthorized`);
return;
}
switch (req.url) {
case `/yarn`: {
res.end(JSON.stringify({"dist-tags": {
latest: `1.9998.9999`,
}, versions: {'1.9998.9999': {
dist: {
shasum,
size: mockPackageTarGz.length,
noattachment: false,
tarball: `${process.env.COREPACK_NPM_REGISTRY}/yarn.tgz`,
},
}}}));
break;
}

case `/pnpm`: {
res.end(JSON.stringify({"dist-tags": {
latest: `1.9998.9999`,
}, versions: {'1.9998.9999': {
dist: {
shasum,
size: mockPackageTarGz.length,
noattachment: false,
tarball: `${process.env.COREPACK_NPM_REGISTRY}/pnpm/-/pnpm-1.9998.9999.tgz`,
},
}}}));
break;
}
let slashPosition = req.url.indexOf(`/`, 1);
if (req.url.charAt(1) === `@`) slashPosition = req.url.indexOf(`/`, slashPosition + 1);

case `/@yarnpkg/cli-dist`: {
const packageName = req.url.slice(1, slashPosition === -1 ? undefined : slashPosition);
if (packageName in registry) {
if (req.url === `/${packageName}`) {
// eslint-disable-next-line @typescript-eslint/naming-convention
res.end(JSON.stringify({"dist-tags": {
latest: `5.9999.9999`,
}, versions: {'5.9999.9999': {
bin: {
yarn: `./bin/yarn.js`,
yarnpkg: `./bin/yarn.js`,
},
dist: {
shasum,
size: mockPackageTarGz.length,
noattachment: false,
tarball: `${process.env.COREPACK_NPM_REGISTRY}/yarn.tgz`,
},
}}}));
break;
latest: registry[packageName].at(-1),
}, versions: Object.fromEntries(registry[packageName].map(version => [version, getVersionMetadata(packageName, version)])),
}));
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

case `/customPkgManager`: {
res.end(JSON.stringify({"dist-tags": {
latest: `1.0.0`,
}, versions: {'1.0.0': {
bin: {
customPkgManager: `./bin/customPkgManager.js`,
},
dist: {
shasum,
size: mockPackageTarGz.length,
noattachment: false,
tarball: `${process.env.COREPACK_NPM_REGISTRY}/customPkgManager/-/customPkgManager-1.0.0.tgz`,
},
}}}));
break;
const isDownloadingRequest = req.url.slice(packageName.length + 1, packageName.length + 4) === `/-/`;
let version;
if (isDownloadingRequest) {
const match = /^(.+)-(.+)\.tgz$/.exec(req.url.slice(packageName.length + 4));
if (match?.[1] === packageName) {
version = match[2];
}
} else {
version = req.url.slice(packageName.length + 2);
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

case `/pnpm/-/pnpm-1.9998.9999.tgz`:
case `/yarn.tgz`:
case `/customPkgManager/-/customPkgManager-1.0.0.tgz`:
res.end(mockPackageTarGz);
break;

default:
throw new Error(`unsupported request`, {cause: req.url});
if (version === `latest`) version = registry[packageName].at(-1);
if (registry[packageName].includes(version)) {
res.end(
isDownloadingRequest ?
mockPackageTarGz :
JSON.stringify(getVersionMetadata(packageName, version)),
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
);
} else {
res.writeHead(404).end(`Not Found`);
throw new Error(`unsupported request`, {cause: {url: req.url, packageName, version, isDownloadingRequest}});
}
} else {
res.writeHead(500).end(`Internal Error`);
throw new Error(`unsupported request`, {cause: {url: req.url, packageName}});
}
}).listen(0, `localhost`);

Expand Down