Skip to content

Commit

Permalink
📦 NEW: Support libc field on conditions install (#387)
Browse files Browse the repository at this point in the history
closes #386
  • Loading branch information
fengmk2 committed Mar 20, 2022
1 parent 6d32f06 commit a34f2b4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ async function download(pkg, options) {
err.name = 'UnSupportedPlatformError';
throw err;
}
// dont download pkg in not matched libc
if (Array.isArray(pkg.libc)) {
const currentLibc = utils.getLibc();
debug('currentLibc', currentLibc, pkg.libc);
if (!utils.matchPlatform(currentLibc, pkg.libc)) {
const errMsg = `[${pkg.name}@${pkg.version}] skip download for reason ${pkg.libc.join(', ')} dont includes your libc ${currentLibc}`;
const err = new Error(errMsg);
err.name = 'UnSupportedPlatformError';
throw err;
}
}

const ungzipDir = options.ungzipDir || utils.getPackageStorePath(options.storeDir, pkg);

Expand Down
21 changes: 20 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,26 @@ exports.getBugVersions = async (registry, globalOptions) => {
return pkg.config['bug-versions'];
};

// match platform or arch
// detect libc
// follow yarn impl: https://github.com/yarnpkg/berry/pull/3981/files#diff-f9ba955128e960171ac97ce06b0b0a940bb074af2794429a9f03c75f2a3bd459R10
exports.getLibc = () => {
if (process.platform === 'win32') return null;
const report = process.report && process.report.getReport() || {};
const sharedObjects = report.sharedObjects || [];

// Matches the first group if libc, second group if musl
const libcRE = /\/(?:(ld-linux-|[^/]+-linux-gnu\/)|(libc.musl-|ld-musl-))/i;
for (const sharedObject of sharedObjects) {
const m = sharedObject.match(libcRE);
if (m) {
if (m[1]) return 'libc';
if (m[2]) return 'musl';
}
}
return null;
};

// match platform, arch or libc
// see https://docs.npmjs.com/cli/v7/configuring-npm/package-json#os
exports.matchPlatform = (current, osNames) => {
if (!Array.isArray(osNames) || osNames.length === 0) {
Expand Down
23 changes: 23 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ describe('test/utils.test.js', () => {
assert(utils.matchPlatform('x64', [ 'x64', '!x64' ]));
});

it('should match libc names', () => {
assert(utils.matchPlatform('glibc', []));
assert(utils.matchPlatform('musl', []));
assert(utils.matchPlatform(null, []));
assert(utils.matchPlatform('glibc', [ 'glibc' ]));
assert(utils.matchPlatform('glibc', [ 'glibc', 'musl' ]));
assert(utils.matchPlatform('musl', [ 'glibc', 'musl' ]));
assert(utils.matchPlatform('musl', [ 'musl' ]));
assert(utils.matchPlatform('glibc', [ '!musl' ]));
assert(utils.matchPlatform('musl', [ '!glibc' ]));
assert(utils.matchPlatform('glibc', [ '!musl', 'glibc' ]));
assert(utils.matchPlatform('glibc', [ 'glibc', '!glibc' ]));
});

it('should not match os names', () => {
assert(!utils.matchPlatform('darwin', [ 'linux' ]));
assert(!utils.matchPlatform('darwin', [ 'win32' ]));
Expand All @@ -53,6 +67,15 @@ describe('test/utils.test.js', () => {
assert(!utils.matchPlatform('mips', [ '!x64', '!mips' ]));
assert(!utils.matchPlatform('x64', [ '!x64' ]));
});

it('should not match libc names', () => {
assert(!utils.matchPlatform(null, [ 'musl' ]));
assert(!utils.matchPlatform(null, [ 'glibc' ]));
assert(!utils.matchPlatform('glibc', [ 'musl' ]));
assert(!utils.matchPlatform('musl', [ 'glibc' ]));
assert(!utils.matchPlatform('glibc', [ '!glibc' ]));
assert(!utils.matchPlatform('musl', [ '!musl' ]));
});
});

describe('findMaxSatisfyingVersion()', () => {
Expand Down

0 comments on commit a34f2b4

Please sign in to comment.