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

Feature/relative discovery links #177

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions registry/server/common/services/AppAssetsDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'lodash';
import urljoin from 'url-join';

import knex from '../../db';
import manifestProcessor from './assetsManifestProcessor';

export default class AppAssetsDiscovery {
private timerId?: NodeJS.Timeout;
Expand Down Expand Up @@ -53,10 +54,7 @@ export default class AppAssetsDiscovery {
continue;
}

let data = _.pick(res.data, ['spaBundle', 'cssBundle', 'dependencies']);
if (data.dependencies !== undefined) {
data.dependencies = JSON.stringify(data.dependencies);
}
let data = manifestProcessor(reqUrl, res.data);

await knex('apps').where('name', app.name).update(Object.assign({}, data, {
assetsDiscoveryUpdatedAt: now,
Expand Down
23 changes: 23 additions & 0 deletions registry/server/common/services/assetsManifestProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import _ from 'lodash';
import url from 'url';


export default function processManifest(baseUrl: string, manifest: any) {
let data = _.pick(manifest, ['spaBundle', 'cssBundle', 'dependencies']);

if (data.spaBundle) {
data.spaBundle = url.resolve(baseUrl, data.spaBundle);
}
if (data.cssBundle) {
data.cssBundle = url.resolve(baseUrl, data.cssBundle);
}

if (data.dependencies !== undefined && _.isPlainObject(data.dependencies)) {
data.dependencies = _.mapValues(data.dependencies, v => url.resolve(baseUrl, v));
data.dependencies = JSON.stringify(data.dependencies);
} else {
delete data.dependencies;
}

return data;
}
59 changes: 59 additions & 0 deletions registry/tests/assetsManifestProcessor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from './common';

import processor from '../server/common/services/assetsManifestProcessor'

describe('assetsManifestProcessor', () => {
it('perform basic link resolution & cleanup of extra props', () => {
expect(processor('https://example.com/folder/assets-discovery.js', {
a: 'tst',
spaBundle: 'https://example.com/aa.js',
cssBundle: './tst/aa.js',
dependencies: {
a1: 'https://example.com/aa.js',
a2: './tst/aa.js',
}
})).to.eql({
spaBundle: 'https://example.com/aa.js',
cssBundle: 'https://example.com/folder/tst/aa.js',
dependencies: JSON.stringify({
a1: 'https://example.com/aa.js',
a2: 'https://example.com/folder/tst/aa.js',
})
});
});

it('handles absence of dependencies object or invalid type correctly', () => {
expect(processor('https://example.com/folder/assets-discovery.js', {
spaBundle: 'https://example.com/aa.js',
cssBundle: './tst/aa.js',
})).to.eql({
spaBundle: 'https://example.com/aa.js',
cssBundle: 'https://example.com/folder/tst/aa.js',
});

expect(processor('https://example.com/folder/assets-discovery.js', {
spaBundle: 'https://example.com/aa.js',
cssBundle: './tst/aa.js',
dependencies: ['aaa']
})).to.eql({
spaBundle: 'https://example.com/aa.js',
cssBundle: 'https://example.com/folder/tst/aa.js',
});
});

it('handles absence of cssBundle correctly', () => {
expect(processor('https://example.com/folder/assets-discovery.js', {
spaBundle: 'https://example.com/aa.js',
})).to.eql({
spaBundle: 'https://example.com/aa.js',
});
});

it('handles absence of spaBundle correctly', () => {
expect(processor('https://example.com/folder/assets-discovery.js', {
cssBundle: 'https://example.com/aa.css',
})).to.eql({
cssBundle: 'https://example.com/aa.css',
});
});
});