Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix MSI default stock id value
- Add outputFormatter to response from cache - @gibkigonzo (#428)
- disable showing stack for invalid requests - @gibkigonzo (#431)

- Improve `_outputFormatter` on cache catalog-response to prevent exception - @cewald (#432)

## [1.11.1] - 2020.03.17

Expand Down
34 changes: 18 additions & 16 deletions src/api/catalog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jwt from 'jwt-simple';
import request from 'request';
import ProcessorFactory from '../processor/factory';
import jwt from 'jwt-simple'
import request from 'request'
import ProcessorFactory from '../processor/factory'
import { adjustBackendProxyUrl } from '../lib/elastic'
import cache from '../lib/cache-instance'
import { sha3_224 } from 'js-sha3'
Expand All @@ -10,9 +10,9 @@ import loadCustomFilters from '../helpers/loadCustomFilters'
import { elasticsearch, SearchQuery } from 'storefront-query-builder'
import { apiError } from '../lib/util'

function _cacheStorageHandler (config, result, hash, tags) {
async function _cacheStorageHandler (config, result, hash, tags) {
if (config.server.useOutputCache && cache) {
cache.set(
return cache.set(
'api:' + hash,
result,
tags
Expand Down Expand Up @@ -61,12 +61,12 @@ export default ({config, db}) => async function (req, res, body) {
}
if (req.query.response_format) responseFormat = req.query.response_format

const urlSegments = req.url.split('/');
const urlSegments = req.url.split('/')

let indexName = ''
let entityType = ''
if (urlSegments.length < 2) { throw new Error('No index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/<entity_type>_search') } else {
indexName = urlSegments[1];
indexName = urlSegments[1]

if (urlSegments.length > 2) { entityType = urlSegments[2] }

Expand Down Expand Up @@ -94,14 +94,14 @@ export default ({config, db}) => async function (req, res, body) {
delete requestBody.groupToken
delete requestBody.groupId

let auth = null;
let auth = null

// Only pass auth if configured
if (config.elasticsearch.user || config.elasticsearch.password) {
auth = {
user: config.elasticsearch.user,
pass: config.elasticsearch.password
};
}
}
const s = Date.now()
const reqHash = sha3_224(`${JSON.stringify(requestBody)}${req.url}`)
Expand All @@ -114,7 +114,7 @@ export default ({config, db}) => async function (req, res, body) {
auth: auth
}, async (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
if (_err || _resBody.error) {
apiError(res, _err || _resBody.error);
apiError(res, _err || _resBody.error)
return
}
try {
Expand Down Expand Up @@ -146,6 +146,9 @@ export default ({config, db}) => async function (req, res, body) {
const attributeList = await AttributeService.list(attributeListParam, config, indexName)
_resBody.attribute_metadata = attributeList.map(AttributeService.transformToMetadata)
}

_resBody = _outputFormatter(_resBody, responseFormat)

if (config.get('varnish.enabled')) {
// Add tags to cache, so we can display them in response headers then
_cacheStorageHandler(config, {
Expand All @@ -155,14 +158,13 @@ export default ({config, db}) => async function (req, res, body) {
} else {
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
}
res.json(_outputFormatter(_resBody, responseFormat));
} else { // no cache storage if no results from Elastic
res.json(_resBody);
}

res.json(_resBody)
} catch (err) {
apiError(res, err);
apiError(res, err)
}
});
})
}

if (config.server.useOutputCache && cache) {
Expand All @@ -176,7 +178,7 @@ export default ({config, db}) => async function (req, res, body) {
res.setHeader('X-VS-Cache-Tag', tagsHeader)
delete output.tags
}
res.json(_outputFormatter(output, responseFormat));
res.json(output)
console.log(`cache hit [${req.url}], cached request: ${Date.now() - s}ms`)
} else {
res.setHeader('X-VS-Cache', 'Miss')
Expand Down