Skip to content

Commit

Permalink
feat(cdn): add config for gem, npm and pip (#2767)
Browse files Browse the repository at this point in the history
This allows to enable the cdn for those managers selectively.

- #972
  • Loading branch information
viceice authored Jun 6, 2024
1 parent ff6f57b commit 9e99e54
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/cli/install-tool/install-legacy-tool.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { EnvService } from '../services';
Expand All @@ -13,7 +14,10 @@ export class InstallLegacyToolService {
logger.debug(`Installing legacy tool ${tool} v${version} ...`);
const env: NodeJS.ProcessEnv = {};

const pipIndex = this.envSvc.replaceUrl(defaultPipRegistry);
const pipIndex = this.envSvc.replaceUrl(
defaultPipRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_PIP),
);
if (pipIndex !== defaultPipRegistry) {
env.PIP_INDEX_URL = pipIndex;
}
Expand Down
3 changes: 3 additions & 0 deletions src/cli/services/env.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,8 @@ describe('env.service', () => {
expect(e.replaceUrl('https://registry.npmjs.org')).toBe(
'https://npm.example.test',
);
expect(e.replaceUrl('https://registry.npmjs.org', false)).toBe(
'https://registry.npmjs.org',
);
});
});
10 changes: 8 additions & 2 deletions src/cli/services/env.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,16 @@ export class EnvService {
return this.ignoredTools.has(tool.toUpperCase());
}

public replaceUrl(src: string): string {
/**
* Replace the source url with the optional cdn and replacement urls
* @param src the source url
* @param cdn should the cdn url be used
* @returns the replaced url
*/
public replaceUrl(src: string, cdn = true): string {
let tgt = src;

if (env.CONTAINERBASE_CDN) {
if (env.CONTAINERBASE_CDN && cdn) {
tgt = src.replace(/^https:\//, env.CONTAINERBASE_CDN.replace(/\/$/, ''));
}

Expand Down
9 changes: 6 additions & 3 deletions src/cli/tools/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs, { appendFile, chmod, mkdir, readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { env as penv } from 'node:process';
import is from '@sindresorhus/is';
import { isNonEmptyStringAndNotWhitespace, isString } from '@sindresorhus/is';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import type { PackageJson } from 'type-fest';
Expand Down Expand Up @@ -32,7 +32,10 @@ export abstract class InstallNodeBaseService extends InstallToolBaseService {
env.npm_config_cache = tmp;
}

const registry = this.envSvc.replaceUrl(defaultRegistry);
const registry = this.envSvc.replaceUrl(
defaultRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_NPM),
);
if (registry !== defaultRegistry) {
env.npm_config_registry = registry;
}
Expand Down Expand Up @@ -171,7 +174,7 @@ export abstract class InstallNpmBaseService extends InstallNodeBaseService {
return;
}

if (is.string(pkg.bin)) {
if (isString(pkg.bin)) {
await this.shellwrapper({
srcDir: src,
name: pkg.name ?? this.tool(version),
Expand Down
6 changes: 5 additions & 1 deletion src/cli/tools/ruby/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chmod, mkdir, readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { InstallToolBaseService } from '../../install-tool/install-tool-base.service';
Expand All @@ -22,7 +23,10 @@ export abstract class InstallRubyBaseService extends InstallToolBaseService {
const env: NodeJS.ProcessEnv = {};
const args: string[] = [];

const registry = this.envSvc.replaceUrl(defaultRegistry);
const registry = this.envSvc.replaceUrl(
defaultRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_GEM),
);
if (registry !== defaultRegistry) {
args.push('--clear-sources', '--source', registry);
}
Expand Down

0 comments on commit 9e99e54

Please sign in to comment.