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
fix: re-add support for custom registries with auth #397
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e30bec8
test: add tests for custom registries with auth
aduh95 aa25d62
add support for custom URLs
aduh95 0733180
add more tests
aduh95 f4a5d93
Merge branch 'main' of https://github.com/nodejs/corepack
aduh95 e993c58
fix merge conflicts and remove debug only changes
aduh95 af26938
fix failing tests
aduh95 ad9fe3f
remove merge residus
aduh95 8cb977f
Ensure no internet access during tests
aduh95 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import {createHash} from 'node:crypto'; | ||
import {once} from 'node:events'; | ||
import {createServer} from 'node:http'; | ||
import {gzipSync} from 'node:zlib'; | ||
|
||
function createSimpleTarArchive(fileName, fileContent, mode = 0o644) { | ||
const contentBuffer = Buffer.from(fileContent); | ||
|
||
const header = Buffer.alloc(512); // TAR headers are 512 bytes | ||
header.write(fileName); | ||
header.write(`100${mode.toString(8)} `, 100, 7, `utf-8`); // File mode (octal) followed by a space | ||
header.write(`0001750 `, 108, 8, `utf-8`); // Owner's numeric user ID (octal) followed by a space | ||
header.write(`0001750 `, 116, 8, `utf-8`); // Group's numeric user ID (octal) followed by a space | ||
header.write(`${contentBuffer.length.toString(8)} `, 124, 12, `utf-8`); // File size in bytes (octal) followed by a space | ||
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 | ||
header.fill(` `, 148, 156); // Fill checksum area with spaces for calculation | ||
header.write(`ustar `, 257, 8, `utf-8`); // UStar indicator | ||
|
||
// Calculate and write the checksum. Note: This is a simplified calculation not recommended for production | ||
const checksum = header.reduce((sum, value) => sum + value, 0); | ||
header.write(`${checksum.toString(8)}\0 `, 148, 8, `utf-8`); // Write checksum in octal followed by null and space | ||
|
||
|
||
return Buffer.concat([ | ||
header, | ||
contentBuffer, | ||
Buffer.alloc(512 - (contentBuffer.length % 512)), | ||
]); | ||
} | ||
|
||
const mockPackageTarGz = gzipSync(Buffer.concat([ | ||
createSimpleTarArchive(`package/bin/customPkgManager.js`, `#!/usr/bin/env node\nconsole.log("customPkgManager: Hello from custom registry");\n`, 0o755), | ||
createSimpleTarArchive(`package/bin/pnpm.js`, `#!/usr/bin/env node\nconsole.log("pnpm: Hello from custom registry");\n`, 0o755), | ||
createSimpleTarArchive(`package/bin/yarn.js`, `#!/usr/bin/env node\nconsole.log("yarn: Hello from custom registry");\n`, 0o755), | ||
createSimpleTarArchive(`package/package.json`, JSON.stringify({bin: {yarn: `bin/yarn.js`, pnpm: `bin/pnpm.js`, customPkgManager: `bin/customPkgManager.js`}})), | ||
Buffer.alloc(1024), | ||
])); | ||
const shasum = createHash(`sha1`).update(mockPackageTarGz).digest(`hex`); | ||
|
||
|
||
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`); | ||
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; | ||
} | ||
|
||
case `/@yarnpkg/cli-dist`: { | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
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}); | ||
} | ||
}).listen(0, `localhost`); | ||
|
||
await once(server, `listening`); | ||
|
||
const {address, port} = server.address(); | ||
switch (process.env.AUTH_TYPE) { | ||
case `COREPACK_NPM_REGISTRY`: | ||
process.env.COREPACK_NPM_REGISTRY = `http://user:pass@${address.includes(`:`) ? `[${address}]` : address}:${port}`; | ||
break; | ||
|
||
case `COREPACK_NPM_TOKEN`: | ||
process.env.COREPACK_NPM_REGISTRY = `http://${address.includes(`:`) ? `[${address}]` : address}:${port}`; | ||
process.env.COREPACK_NPM_TOKEN = Buffer.from(`user:pass`).toString(`base64`); | ||
break; | ||
|
||
case `COREPACK_NPM_PASSWORD`: | ||
process.env.COREPACK_NPM_REGISTRY = `http://${address.includes(`:`) ? `[${address}]` : address}:${port}`; | ||
process.env.COREPACK_NPM_USER = `user`; | ||
process.env.COREPACK_NPM_PASSWORD = `pass`; | ||
break; | ||
|
||
default: throw new Error(`Invalid AUTH_TYPE in env`, {cause: process.env.AUTH_TYPE}); | ||
} | ||
|
||
if (process.env.NOCK_ENV === `replay`) { | ||
const originalFetch = globalThis.fetch; | ||
globalThis.fetch = function fetch(i) { | ||
if (!`${i}`.startsWith(`http://${address.includes(`:`) ? `[${address}]` : address}:${port}`)) | ||
throw new Error; | ||
|
||
return Reflect.apply(originalFetch, this, arguments); | ||
}; | ||
} | ||
|
||
server.unref(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without testing it but I think that should be
Basic
instead ofBearer
when using username and password. 🤔@aduh95
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thankfully it's already fixed on
main
: #454There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry. Okay the fix seems not the be released yet, that might explain why it's not working. Sorry for bothering you.