Skip to content
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
43 changes: 36 additions & 7 deletions src/registry/routes/helpers/get-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

const acceptLanguageParser = require('accept-language-parser');
const Cache = require('nice-cache');
const Client = require('oc-client');
const Domain = require('domain');
const format = require('stringformat');
const vm = require('vm');
const _ = require('lodash');

const applyDefaultValues = require('./apply-default-values');
const Client = require('oc-client');
const eventsHandler = require('../../domain/events-handler');
const GetComponentRetrievingInfo = require('./get-component-retrieving-info');
const getComponentFallback = require('./get-component-fallback');
Expand Down Expand Up @@ -147,10 +147,27 @@ module.exports = function(conf, repository) {

// Support legacy templates
let templateType = component.oc.files.template.type;
if (templateType === 'jade' || templateType === 'handlebars') {
const isLegacyTemplate =
templateType === 'jade' || templateType === 'handlebars';
if (isLegacyTemplate) {
templateType = `oc-template-${templateType}`;
}

const supportedTemplates = repository.getTemplates().map(t => t.type);

if (!_.includes(supportedTemplates, templateType)) {
return callback({
status: 400,
response: {
code: 'TEMPLATE_NOT_SUPPORTED',
error: format(
strings.errors.registry.TEMPLATE_NOT_SUPPORTED,
templateType
)
}
});
}

const filterCustomHeaders = (
headers,
requestedVersion,
Expand Down Expand Up @@ -188,18 +205,30 @@ module.exports = function(conf, repository) {
options.headers['user-agent'] &&
!!options.headers['user-agent'].match('oc-client-');

const parseTemplatesHeader = t =>
t.split(';').map(t => t.split(',')[0]);
const supportedTemplates = options.headers.templates
? parseTemplatesHeader(options.headers.templates)
: [];

const isTemplateSupportedByClient = Boolean(
options.headers['user-agent'] &&
!!options.headers['user-agent'].match('oc-client-') &&
isValidClientRequest &&
options.headers.templates &&
(options.headers.templates[component.oc.files.template.type] ||
options.headers.templates[templateType])
(_.includes(
supportedTemplates,
component.oc.files.template.type
) ||
_.includes(supportedTemplates, templateType))
);

let renderMode = 'rendered';
if (isUnrendered) {
renderMode = 'unrendered';
if (isValidClientRequest && !isTemplateSupportedByClient) {
if (
isValidClientRequest &&
!isTemplateSupportedByClient &&
!isLegacyTemplate
) {
renderMode = 'rendered';
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ module.exports = {
PLUGIN_NOT_VALID: 'Plugin {0} is not valid',
RESOLVING_ERROR: 'component resolving error',
TEMPLATE_NOT_FOUND: 'Template {0} not found',
TEMPLATE_NOT_VALID: '{0} is not a valid oc-template'
TEMPLATE_NOT_VALID: '{0} is not a valid oc-template',
TEMPLATE_NOT_SUPPORTED: '{0} is not a supported oc-template'
},
cli: {
scaffoldError: (url, error) =>
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/mocked-components/async-error3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

module.exports = {
package: {
name: 'async-error3-component',
version: '1.0.0',
oc: {
container: false,
renderInfo: false,
files: {
template: {
type: 'oc-template-supported',
hashKey: '8c1fbd954f2b0d8cd5cf11c885fed4805225749f',
src: 'template.js'
},
dataProvider: {
type: 'node.js',
hashKey: 'bf6318cf5d5f2e7654a750c574fd0db9fb493432',
src: 'server.js'
}
}
}
},
data: '"use strict";module.exports.data=function(r,t){r.params.error?setTimeout(function(){thisDoesnotExist()},1e3):t(null,{error:!!r.params.error})};',
view: 'var oc=oc||{};oc.components=oc.components||{},oc.components["8c1fbd954f2b0d8cd5cf11c885fed4805225749f"]' +
'=function(){var o=[];return o.push("<div>hello</div>"),o.join("")};'
};
27 changes: 27 additions & 0 deletions test/fixtures/mocked-components/async-error4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

module.exports = {
package: {
name: 'async-error4-component',
version: '1.0.0',
oc: {
container: false,
renderInfo: false,
files: {
template: {
type: 'oc-template-notsupported',
hashKey: '8c1fbd954f2b0d8cd5cf11c885fed4805225749f',
src: 'template.js'
},
dataProvider: {
type: 'node.js',
hashKey: 'bf6318cf5d5f2e7654a750c574fd0db9fb493432',
src: 'server.js'
}
}
}
},
data: '"use strict";module.exports.data=function(r,t){r.params.error?setTimeout(function(){thisDoesnotExist()},1e3):t(null,{error:!!r.params.error})};',
view: 'var oc=oc||{};oc.components=oc.components||{},oc.components["8c1fbd954f2b0d8cd5cf11c885fed4805225749f"]' +
'=function(){var o=[];return o.push("<div>hello</div>"),o.join("")};'
};
2 changes: 2 additions & 0 deletions test/fixtures/mocked-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module.exports = {
'async-error-component': require('./async-error'),
'async-error2-component': require('./async-error2'),
'async-error3-component': require('./async-error3'),
'async-error4-component': require('./async-error4'),
'error-component': require('./error'),
'npm-component': require('./npm'),
'plugin-component': require('./plugin'),
Expand Down
26 changes: 24 additions & 2 deletions test/unit/registry-routes-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ describe('registry : routes : component', () => {
getCompiledView: sinon.stub().yields(null, params.view),
getComponent: sinon.stub().yields(null, params.package),
getDataProvider: sinon.stub().yields(null, params.data),
getTemplates: sinon.stub(),
getTemplates: sinon.stub().returns([
{
type: 'oc-template-jade',
version: '6.0.1',
externals: []
},
{
type: 'oc-template-handlebars',
version: '6.0.2',
externals: []
}
]),
getStaticFilePath: sinon.stub().returns('//my-cdn.com/files/')
};
};
Expand Down Expand Up @@ -568,7 +579,18 @@ describe('registry : routes : component', () => {
getCompiledView: sinon.stub(),
getComponent: sinon.stub(),
getDataProvider: sinon.stub(),
getTemplates: sinon.stub(),
getTemplates: sinon.stub().returns([
{
type: 'oc-template-jade',
version: '6.0.1',
externals: []
},
{
type: 'oc-template-handlebars',
version: '6.0.2',
externals: []
}
]),
getStaticFilePath: sinon.stub().returns('//my-cdn.com/files/')
};

Expand Down
Loading