Skip to content

Commit

Permalink
fix: use hasProxyTo to find correct uplink for tarballs (#1644)
Browse files Browse the repository at this point in the history
* test: different uplinks with the same URL

This test reproduces #1642

* fix: use hasProxyTo to find correct uplink for tarballs

Fixes #1642

Co-authored-by: Juan Picado @jotadeveloper <juanpicado19@gmail.com>
  • Loading branch information
Beanow and juanpicado committed Jan 11, 2020
1 parent aac6709 commit 19d9fc2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Storage implements IStorageHandler {
let uplink: any = null;

for (const uplinkId in self.uplinks) {
if (self.uplinks[uplinkId].isUplinkValid(file.url)) {
if (hasProxyTo(name, uplinkId, self.config.packages)) {
uplink = self.uplinks[uplinkId];
}
}
Expand Down
84 changes: 84 additions & 0 deletions test/unit/modules/storage/store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import fs from 'fs';
import rimraf from 'rimraf';
import {Writable} from 'stream';
import configExample from '../../partials/config';
import AppConfig from '../../../../src/lib/config';
import Storage from '../../../../src/lib/storage';
Expand Down Expand Up @@ -34,6 +35,47 @@ const generateStorage = async function(port = mockServerPort) {
return store;
}

const generateSameUplinkStorage = async function(port = mockServerPort) {
const storageConfig = configExample({
self_path: __dirname,
storage: storagePath,
packages: {
jquery: {
access: ['$all'],
publish: ['$all'],
proxy: ['cached'],
},
'@jquery/*': {
access: ['$all'],
publish: ['$all'],
proxy: ['notcached'],
}
},
uplinks: {
cached: {
url: `http://${DOMAIN_SERVERS}:${port}`,
cache: true,
},
notcached: {
url: `http://${DOMAIN_SERVERS}:${port}`,
cache: false,
}
}
}, 'store.spec.yaml');

const config: Config = new AppConfig(storageConfig);
const store: IStorageHandler = new Storage(config);
await store.init(config, []);

return store;
}

const createNullStream = () => new Writable({
write: function(chunk, encoding, next) {
next();
}
});

describe('StorageTest', () => {
let mockRegistry;

Expand All @@ -55,6 +97,48 @@ describe('StorageTest', () => {
expect(storage).toBeDefined();
});

describe('test getTarball', () => {
test('should select right uplink given package.proxy for upstream tarballs', async (done) => {
const storage: IStorageHandler = await generateSameUplinkStorage();
const notcachedSpy = jest.spyOn(storage.uplinks.notcached, 'fetchTarball');
const cachedSpy = jest.spyOn(storage.uplinks.cached, 'fetchTarball');

await new Promise((res, rej) => {
const reader = storage.getTarball('jquery', 'jquery-1.5.1.tgz');
reader.on('end', () => {
expect(notcachedSpy).toHaveBeenCalledTimes(0);
expect(cachedSpy).toHaveBeenCalledTimes(1);
expect(cachedSpy).toHaveBeenCalledWith('http://0.0.0.0:55548/jquery/-/jquery-1.5.1.tgz');
res();
});
reader.on('error', (err) => {
rej(err);
});
reader.pipe(createNullStream());
});

// Reset counters.
cachedSpy.mockClear();
notcachedSpy.mockClear();

await new Promise((res, rej) => {
const reader = storage.getTarball('@jquery/jquery', 'jquery-1.5.1.tgz');
reader.on('end', () => {
expect(cachedSpy).toHaveBeenCalledTimes(0);
expect(notcachedSpy).toHaveBeenCalledTimes(1);
expect(notcachedSpy).toHaveBeenCalledWith('http://0.0.0.0:55548/@jquery%2fjquery/-/jquery-1.5.1.tgz');
res();
});
reader.on('error', (err) => {
rej(err);
});
reader.pipe(createNullStream());
});

done();
});
});

describe('test _syncUplinksMetadata', () => {
test('should fetch from uplink jquery metadata from registry', async (done) => {
const storage: IStorageHandler = await generateStorage();
Expand Down

0 comments on commit 19d9fc2

Please sign in to comment.