From 8e301315abb45ff4fccb00781b316b8898028542 Mon Sep 17 00:00:00 2001 From: Marquise Rosier Date: Wed, 6 May 2020 23:43:35 -0700 Subject: [PATCH 1/4] feat(run-query.js): adds helix-run-query support can now embed query results from bigquery fix #9 --- package-lock.json | 95 ++++++++++++++++++++++++++++++++++----- package.json | 1 + src/embed.js | 4 +- src/matchers/run-query.js | 50 +++++++++++++++++++++ test/index.test.js | 15 +++++++ test/run-query.test.js | 35 +++++++++++++++ 6 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 src/matchers/run-query.js create mode 100644 test/run-query.test.js diff --git a/package-lock.json b/package-lock.json index 58480790..e259b67b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,18 @@ "epsagon": "1.62.0" } }, + "@adobe/helix-fetch": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@adobe/helix-fetch/-/helix-fetch-1.6.0.tgz", + "integrity": "sha512-YdZSt9/HLWblfU9QS+e/mRhLvEDrlqApOLfuSBVbW372KR+L16kSuJ6niR6WahmE+yfhmiRZEtCS877PM4G1DQ==", + "requires": { + "fetch-h2": "^2.4.3", + "get-stream": "^5.1.0", + "http-cache-semantics": "^4.0.3", + "lru-cache": "^5.1.1", + "object-sizeof": "^1.5.3" + } + }, "@adobe/helix-log": { "version": "4.5.1", "resolved": "https://registry.npmjs.org/@adobe/helix-log/-/helix-log-4.5.1.tgz", @@ -1291,6 +1303,11 @@ "@types/node": "*" } }, + "@types/tough-cookie": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.7.tgz", + "integrity": "sha512-rMQbgMGxnLsdn8e9aPVyuN+zMQLrZ2QW8xlv7eWS1mydfGXN+tsTKffcIzd8rGCcLdmi3xvQw2MDaZI1bBNTaw==" + }, "@webassemblyjs/ast": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.9.0.tgz", @@ -1570,6 +1587,14 @@ "integrity": "sha512-RO1ibKvd27e6FEShVFfPALuHI3WjSVNeK5FIsmme/LYRNxjKuNj+Dt7bucLa6NdSv3JcVTyMlm9kGR84z1XpaQ==", "dev": true }, + "already": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/already/-/already-1.12.0.tgz", + "integrity": "sha512-JOq+vzTKA3qheL4rOcTYvdUwUhLFrUSiRshlDxxj+7CJTQlw9qHGveuMzEbyGRfoOscREA1gqJxxTnK7GDAfxQ==", + "requires": { + "throat": "^5.0.0" + } + }, "ansi-colors": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.3.tgz", @@ -2274,7 +2299,6 @@ "version": "5.6.0", "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz", "integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==", - "dev": true, "requires": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" @@ -2460,6 +2484,11 @@ "caller-callsite": "^2.0.0" } }, + "callguard": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/callguard/-/callguard-1.2.1.tgz", + "integrity": "sha512-QTxtuE+QrPA/ZStfYEf/IL1RqyOEpg3CMKp35oTs4n91WveYjbnbPJ0O2ob+RxhPzOuvb0KgQvYwJzhZ6nswXQ==" + }, "callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -3942,7 +3971,6 @@ "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", - "dev": true, "requires": { "once": "^1.4.0" } @@ -4883,6 +4911,40 @@ "lodash.isplainobject": "4.0.6" } }, + "fetch-h2": { + "version": "2.4.5", + "resolved": "https://registry.npmjs.org/fetch-h2/-/fetch-h2-2.4.5.tgz", + "integrity": "sha512-Nl6wvKqwHPBKlGbXQsnA55EgzI62p/pTb5ISrfxJEAb7ShwcrITEQHAVHKiuf9Z3ulMd64fkildG/bQB4J3qzw==", + "requires": { + "@types/tough-cookie": "^2.3.6", + "already": "^1.12.0", + "callguard": "^1.2.1", + "get-stream": "^5.1.0", + "through2": "^3.0.1", + "to-arraybuffer": "^1.0.1", + "tough-cookie": "^3.0.1" + }, + "dependencies": { + "through2": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz", + "integrity": "sha512-M96dvTalPT3YbYLaKaCuwu+j06D/8Jfib0o/PxbVt6Amhv3dUAtW6rTV1jPgJSBG83I/e04Y6xkVdVhSRhi0ww==", + "requires": { + "readable-stream": "2 || 3" + } + }, + "tough-cookie": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-3.0.1.tgz", + "integrity": "sha512-yQyJ0u4pZsv9D4clxO69OEjLWYw+jbgspjTue4lTQZLfV0c5l1VmK2y1JK8E9ahdpltPOaAThPcp5nKPUgSnsg==", + "requires": { + "ip-regex": "^2.1.0", + "psl": "^1.1.28", + "punycode": "^2.1.1" + } + } + } + }, "figgy-pudding": { "version": "3.5.2", "resolved": "https://registry.npmjs.org/figgy-pudding/-/figgy-pudding-3.5.2.tgz", @@ -5873,7 +5935,6 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.1.0.tgz", "integrity": "sha512-EXr1FOzrzTfGeL0gQdeFEvOMm2mzMOglyiOXSTpPC+iAjAKftbr3jpCMWynogwYnM+eSj9sHGc6wjIcDvYiygw==", - "dev": true, "requires": { "pump": "^3.0.0" } @@ -6340,6 +6401,11 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, + "http-cache-semantics": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz", + "integrity": "sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==" + }, "http-proxy-agent": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-4.0.0.tgz", @@ -6436,8 +6502,7 @@ "ieee754": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", - "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==", - "dev": true + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" }, "iferr": { "version": "0.1.5", @@ -6667,8 +6732,7 @@ "ip-regex": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/ip-regex/-/ip-regex-2.1.0.tgz", - "integrity": "sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk=", - "dev": true + "integrity": "sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk=" }, "is-accessor-descriptor": { "version": "0.1.6", @@ -12832,6 +12896,14 @@ "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", "dev": true }, + "object-sizeof": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/object-sizeof/-/object-sizeof-1.6.0.tgz", + "integrity": "sha512-Z+suoK94o8WjEMYItJjpPyG6I78STcI1CxM7rjxM8LhbgegAY+jhGSFY0c5Ez20LXgDGtyKzYZNXHpDyr6hj8Q==", + "requires": { + "buffer": "^5.5.0" + } + }, "object-to-spawn-args": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-to-spawn-args/-/object-to-spawn-args-1.1.1.tgz", @@ -13464,7 +13536,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", - "dev": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -15430,6 +15501,11 @@ "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, + "throat": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/throat/-/throat-5.0.0.tgz", + "integrity": "sha512-fcwX4mndzpLQKBS1DVYhGAcYaYt7vsHNIvQV+WXMvnow5cgjPphq5CaayLaGsjRdSCKZFNGt7/GYAuXaNOiYCA==" + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", @@ -15467,8 +15543,7 @@ "to-arraybuffer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/to-arraybuffer/-/to-arraybuffer-1.0.1.tgz", - "integrity": "sha1-fSKbH8xjfkZsoIEYCDanqr/4P0M=", - "dev": true + "integrity": "sha1-fSKbH8xjfkZsoIEYCDanqr/4P0M=" }, "to-fast-properties": { "version": "2.0.0", diff --git a/package.json b/package.json index c61cc191..fe82ef86 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "homepage": "https://github.com/adobe/helix-data-embed#readme", "dependencies": { "@adobe/helix-epsagon": "1.2.1", + "@adobe/helix-fetch": "^1.6.0", "@adobe/helix-onedrive-support": "2.3.0", "@adobe/helix-status": "7.1.3", "@adobe/openwhisk-action-logger": "2.2.0", diff --git a/src/embed.js b/src/embed.js index b1f42ac2..5281bd07 100644 --- a/src/embed.js +++ b/src/embed.js @@ -12,9 +12,11 @@ const feed = require('./matchers/feed'); const excel = require('./matchers/excel'); const google = require('./matchers/google'); +// eslint-disable-next-line camelcase +const runQuery = require('./matchers/run-query'); const matchers = [ - feed, excel, google, + feed, excel, google, runQuery, ]; function hasParams(list, params) { diff --git a/src/matchers/run-query.js b/src/matchers/run-query.js new file mode 100644 index 00000000..7ed02f15 --- /dev/null +++ b/src/matchers/run-query.js @@ -0,0 +1,50 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +/* eslint-disable camelcase */ +const { fetch } = require('@adobe/helix-fetch'); + +async function extract(url, params, log = console) { + const host = 'https://adobeioruntime.net'; + const path = '/api/v1/web/helix/helix-services/run-query@latest/'; + const query = url.split('/').pop(); + const resource = `${host}${path}${query}`; + + const results = await fetch(resource); + + if (results.ok) { + return { + statusCode: 200, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'max-age=600', + }, + body: (await results.json()).results, + }; + } else { + const errText = await results.text(); + log.error(`data request to ${resource} failed ${errText}`); + return { + statusCode: results.status, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'max-age=600', + }, + body: [], + }; + } +} + +module.exports = { + required: [], + pattern: (url) => /.*run_query.*/.test(url), + extract, +}; diff --git a/test/index.test.js b/test/index.test.js index 6033c1dc..cb4c5d30 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -21,6 +21,21 @@ describe('Integration Tests', () => { assert.equal(result.statusCode, 400); }); + it('tests index with run_query', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await main({ + __ow_path: '/run_query/error500', + __ow_query: 'fromMins=1000&toMins=0', + }); + + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 200); + }); + it('Rejects missing parameters', async () => { const result = await main(); assert.equal(result.statusCode, 400); diff --git a/test/run-query.test.js b/test/run-query.test.js new file mode 100644 index 00000000..4a8c056c --- /dev/null +++ b/test/run-query.test.js @@ -0,0 +1,35 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +/* eslint-env mocha */ +const assert = require('assert'); +// eslint-disable-next-line camelcase +const runQuery = require('../src/matchers/run-query.js'); + +describe('run query tests', () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + it('run query data embeds work', async () => { + const { body, headers, statusCode } = await runQuery.extract('/run_query/error500?fromMins=1000&toMins=0'); + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 200); + }); + + it('run query data embeds fail gracefully', async () => { + const { body, headers, statusCode } = await runQuery.extract('/run_query/fail'); + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 500); + }); +}); From 2b3a12f842032a6b131cc108cce815f5b386a4b5 Mon Sep 17 00:00:00 2001 From: Marquise Rosier Date: Fri, 8 May 2020 05:46:42 -0700 Subject: [PATCH 2/4] fix(run-query.js): pass rev use helix-shared for status code propagation fix #44 --- package-lock.json | 44 ++++++++++++++++++++++++++++----------- package.json | 3 ++- src/matchers/run-query.js | 21 ++++++++++++------- test/index.test.js | 20 +++++++++++++++--- test/run-query.test.js | 20 +++++++++++------- 5 files changed, 76 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index e259b67b..88bfb05c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -112,6 +112,29 @@ } } }, + "@adobe/helix-shared": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/@adobe/helix-shared/-/helix-shared-7.3.0.tgz", + "integrity": "sha512-trZqFEPEpLHnZ1GN9AcltjV2GDDxfVZQ7rUCSIS7DeITq9FKkwQ08o6raWziR6q01GX/7E5IEhCGWJ2z5li98Q==", + "requires": { + "@adobe/helix-log": "^4.4.1", + "ajv": "^6.10.2", + "ferrum": "^1.4.1", + "fs-extra": "^9.0.0", + "lodash": "^4.17.15", + "object-hash": "^2.0.0", + "uri-js": "^4.2.2", + "uuid": "^8.0.0", + "yaml": "1.9.2" + }, + "dependencies": { + "uuid": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.0.0.tgz", + "integrity": "sha512-jOXGuXZAWdsTH7eZLtyXMqUb9EcWMGZNbL9YcGBJl4MH4nrxHmZJhEHvyLFrkxo+28uLb/NYRcStH48fnD0Vzw==" + } + } + }, "@adobe/helix-status": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@adobe/helix-status/-/helix-status-7.1.3.tgz", @@ -463,7 +486,6 @@ "version": "7.9.2", "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.9.2.tgz", "integrity": "sha512-NE2DtOdufG7R5vnfQUTehdTfNycfUANEtCa9PssN9O/xmTzP4E08UI797ixaei6hBEVL9BI/PsdJS5x7mWoB9Q==", - "dev": true, "requires": { "regenerator-runtime": "^0.13.4" }, @@ -471,8 +493,7 @@ "regenerator-runtime": { "version": "0.13.5", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz", - "integrity": "sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==", - "dev": true + "integrity": "sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==" } } }, @@ -1971,8 +1992,7 @@ "at-least-node": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", - "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", - "dev": true + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==" }, "atob": { "version": "2.1.2", @@ -5259,7 +5279,6 @@ "version": "9.0.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.0.tgz", "integrity": "sha512-pmEYSk3vYsG/bF651KPUXZ+hvjpgWYw/Gc7W9NFUe3ZVLczKKWIij3IKpOrQcdw4TILtibFslZ0UmR8Vvzig4g==", - "dev": true, "requires": { "at-least-node": "^1.0.0", "graceful-fs": "^4.2.0", @@ -6179,8 +6198,7 @@ "graceful-fs": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.3.tgz", - "integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==", - "dev": true + "integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==" }, "growl": { "version": "1.10.5", @@ -7458,7 +7476,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.0.1.tgz", "integrity": "sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==", - "dev": true, "requires": { "graceful-fs": "^4.1.6", "universalify": "^1.0.0" @@ -12884,6 +12901,11 @@ "integrity": "sha1-ciu9tgA576R8rTxtws5RqFwCxa4=", "dev": true }, + "object-hash": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-2.0.3.tgz", + "integrity": "sha512-JPKn0GMu+Fa3zt3Bmr66JhokJU5BaNBIh4ZeTlaCBzrBsOeXzwcKKAK1tbLiPKgvwmPXsDvvLHoWh5Bm7ofIYg==" + }, "object-inspect": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz", @@ -15795,8 +15817,7 @@ "universalify": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", - "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==", - "dev": true + "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, "unset-value": { "version": "1.0.0", @@ -16371,7 +16392,6 @@ "version": "1.9.2", "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.9.2.tgz", "integrity": "sha512-HPT7cGGI0DuRcsO51qC1j9O16Dh1mZ2bnXwsi0jrSpsLz0WxOLSLXfkABVl6bZO629py3CU+OMJtpNHDLB97kg==", - "dev": true, "requires": { "@babel/runtime": "^7.9.2" } diff --git a/package.json b/package.json index fe82ef86..f03eb546 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,9 @@ "homepage": "https://github.com/adobe/helix-data-embed#readme", "dependencies": { "@adobe/helix-epsagon": "1.2.1", - "@adobe/helix-fetch": "^1.6.0", + "@adobe/helix-fetch": "1.6.0", "@adobe/helix-onedrive-support": "2.3.0", + "@adobe/helix-shared": "7.3.0", "@adobe/helix-status": "7.1.3", "@adobe/openwhisk-action-logger": "2.2.0", "@adobe/openwhisk-action-utils": "4.2.2", diff --git a/src/matchers/run-query.js b/src/matchers/run-query.js index 7ed02f15..43e320fc 100644 --- a/src/matchers/run-query.js +++ b/src/matchers/run-query.js @@ -11,32 +11,37 @@ */ /* eslint-disable camelcase */ const { fetch } = require('@adobe/helix-fetch'); +const { utils } = require('@adobe/helix-shared'); async function extract(url, params, log = console) { const host = 'https://adobeioruntime.net'; - const path = '/api/v1/web/helix/helix-services/run-query@latest/'; + const path = '/api/v1/web/helix/helix-services/run-query@2.4.11/'; const query = url.split('/').pop(); const resource = `${host}${path}${query}`; + const DEFAULT_CACHE = 'max-age=600'; - const results = await fetch(resource); + const results = await fetch(url.startsWith('http') ? url : resource); + const statusCode = utils.propagateStatusCode(results.status); + const logLevel = utils.logLevelForStatusCode(results.status); + const cacheControl = results.headers.get('cache-control'); if (results.ok) { return { - statusCode: 200, + statusCode, headers: { 'Content-Type': 'application/json', - 'Cache-Control': 'max-age=600', + 'Cache-Control': cacheControl || DEFAULT_CACHE, }, body: (await results.json()).results, }; } else { const errText = await results.text(); - log.error(`data request to ${resource} failed ${errText}`); + log[logLevel](`data request to ${resource} failed ${errText}`); return { - statusCode: results.status, + statusCode, headers: { 'Content-Type': 'application/json', - 'Cache-Control': 'max-age=600', + 'Cache-Control': 'no-cache', }, body: [], }; @@ -45,6 +50,6 @@ async function extract(url, params, log = console) { module.exports = { required: [], - pattern: (url) => /.*run_query.*/.test(url), + pattern: (url) => /(^https:\/\/adobeioruntime.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)|(^\/?run-query.*)/.test(url), extract, }; diff --git a/test/index.test.js b/test/index.test.js index cb4c5d30..f1fd340e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -21,20 +21,34 @@ describe('Integration Tests', () => { assert.equal(result.statusCode, 400); }); - it('tests index with run_query', async () => { + it('tests index with absolute run_query url', async () => { const EXPECTED_HEADERS = { 'Cache-Control': 'max-age=600', 'Content-Type': 'application/json', }; const { body, headers, statusCode } = await main({ - __ow_path: '/run_query/error500', + __ow_path: '/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@2.4.11/error500', + }); + + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 200); + }).timeout(6000); + + it('tests index with relative run_query url', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await main({ + __ow_path: '/run-query/error500', __ow_query: 'fromMins=1000&toMins=0', }); assert.ok(Array.isArray(body)); assert.deepEqual(EXPECTED_HEADERS, headers); assert.equal(statusCode, 200); - }); + }).timeout(6000); it('Rejects missing parameters', async () => { const result = await main(); diff --git a/test/run-query.test.js b/test/run-query.test.js index 4a8c056c..8446c9c8 100644 --- a/test/run-query.test.js +++ b/test/run-query.test.js @@ -15,21 +15,25 @@ const assert = require('assert'); const runQuery = require('../src/matchers/run-query.js'); describe('run query tests', () => { - const EXPECTED_HEADERS = { - 'Cache-Control': 'max-age=600', - 'Content-Type': 'application/json', - }; it('run query data embeds work', async () => { - const { body, headers, statusCode } = await runQuery.extract('/run_query/error500?fromMins=1000&toMins=0'); + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await runQuery.extract('https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@2.4.11/error500'); assert.ok(Array.isArray(body)); assert.deepEqual(EXPECTED_HEADERS, headers); assert.equal(statusCode, 200); - }); + }).timeout(6000); it('run query data embeds fail gracefully', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'no-cache', + 'Content-Type': 'application/json', + }; const { body, headers, statusCode } = await runQuery.extract('/run_query/fail'); assert.ok(Array.isArray(body)); assert.deepEqual(EXPECTED_HEADERS, headers); - assert.equal(statusCode, 500); - }); + assert.equal(statusCode, 502); + }).timeout(6000); }); From 5406c14f19e9c42ee24c2661d35355ad8adbdc5d Mon Sep 17 00:00:00 2001 From: Marquise Rosier Date: Fri, 8 May 2020 15:37:44 -0700 Subject: [PATCH 3/4] fix(run-query.js): fix url pattern code make data-embeds with _query/run-query pathname work, and unpin run-query api fix #44 --- src/matchers/run-query.js | 7 ++++--- test/index.test.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/matchers/run-query.js b/src/matchers/run-query.js index 43e320fc..424b38ef 100644 --- a/src/matchers/run-query.js +++ b/src/matchers/run-query.js @@ -15,12 +15,12 @@ const { utils } = require('@adobe/helix-shared'); async function extract(url, params, log = console) { const host = 'https://adobeioruntime.net'; - const path = '/api/v1/web/helix/helix-services/run-query@2.4.11/'; + const path = '/api/v1/web/helix/helix-services/run-query@v2/'; const query = url.split('/').pop(); const resource = `${host}${path}${query}`; const DEFAULT_CACHE = 'max-age=600'; - const results = await fetch(url.startsWith('http') ? url : resource); + const results = await fetch(url.startsWith(host) ? url : resource); const statusCode = utils.propagateStatusCode(results.status); const logLevel = utils.logLevelForStatusCode(results.status); const cacheControl = results.headers.get('cache-control'); @@ -50,6 +50,7 @@ async function extract(url, params, log = console) { module.exports = { required: [], - pattern: (url) => /(^https:\/\/adobeioruntime.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)|(^\/?run-query.*)/.test(url), + pattern: (url) => /(^https:\/\/adobeioruntime\.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)/.test(url) + || /^\/?_query\/run-query.*$/.test(new URL(url).pathname), extract, }; diff --git a/test/index.test.js b/test/index.test.js index f1fd340e..fde4ec60 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -41,7 +41,7 @@ describe('Integration Tests', () => { 'Content-Type': 'application/json', }; const { body, headers, statusCode } = await main({ - __ow_path: '/run-query/error500', + __ow_path: 'https://example.com/_query/run-query/error500', __ow_query: 'fromMins=1000&toMins=0', }); From cf3afd6d592989b310bf0e503496b29026942011 Mon Sep 17 00:00:00 2001 From: Marquise Rosier Date: Fri, 22 May 2020 16:49:46 -0700 Subject: [PATCH 4/4] fix(run-query.js): wrap extract logic in try/catch wrap extract logic in try/catch also add post.deploy tests fix #44 --- src/matchers/run-query.js | 14 ++++++++------ test/post-deploy.test.js | 30 ++++++++++++++++++++++++++++++ test/run-query.test.js | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/matchers/run-query.js b/src/matchers/run-query.js index 424b38ef..37ccc23f 100644 --- a/src/matchers/run-query.js +++ b/src/matchers/run-query.js @@ -25,7 +25,10 @@ async function extract(url, params, log = console) { const logLevel = utils.logLevelForStatusCode(results.status); const cacheControl = results.headers.get('cache-control'); - if (results.ok) { + try { + if (!results.ok) { + throw new Error(await results.text()); + } return { statusCode, headers: { @@ -34,14 +37,13 @@ async function extract(url, params, log = console) { }, body: (await results.json()).results, }; - } else { - const errText = await results.text(); - log[logLevel](`data request to ${resource} failed ${errText}`); + } catch (e) { + log[logLevel](`data request to ${resource} failed ${e.message}`); return { statusCode, headers: { 'Content-Type': 'application/json', - 'Cache-Control': 'no-cache', + 'Cache-Control': 'max-age=60', }, body: [], }; @@ -51,6 +53,6 @@ async function extract(url, params, log = console) { module.exports = { required: [], pattern: (url) => /(^https:\/\/adobeioruntime\.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)/.test(url) - || /^\/?_query\/run-query.*$/.test(new URL(url).pathname), + || /^\/?_query\/run-query\/.*$/.test(new URL(url).pathname), extract, }; diff --git a/test/post-deploy.test.js b/test/post-deploy.test.js index bbd5b492..8b61b5f3 100644 --- a/test/post-deploy.test.js +++ b/test/post-deploy.test.js @@ -130,4 +130,34 @@ describe('Post-Deploy Tests', () => { throw e; }); }).timeout(10000); + + it('Helix Run Query Embed', async () => { + console.log('Trying', `https://adobeioruntime.net/${getbaseurl()}/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@v2/error500`); + + await chai + .request('https://adobeioruntime.net/') + .get(`${getbaseurl()}/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@v2/error500?fromMins=1000&toMins=0`) + .then((response) => { + expect(response).to.be.json; + expect(response.body).to.be.an('array'); + expect(response).to.have.status(200); + }).catch((e) => { + throw e; + }); + }).timeout(10000); + + it('Helix Run Query Embed Works with Site prefixed', async () => { + console.log('Trying', `https://adobeioruntime.net/${getbaseurl()}/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@v2/error500`); + + await chai + .request('https://adobeioruntime.net/') + .get(`${getbaseurl()}/https://example.com/_query/run-query/error500?fromMins=1000&toMins=0`) + .then((response) => { + expect(response).to.be.json; + expect(response.body).to.be.an('array'); + expect(response).to.have.status(200); + }).catch((e) => { + throw e; + }); + }).timeout(10000); }); diff --git a/test/run-query.test.js b/test/run-query.test.js index 8446c9c8..98cdbcf6 100644 --- a/test/run-query.test.js +++ b/test/run-query.test.js @@ -28,7 +28,7 @@ describe('run query tests', () => { it('run query data embeds fail gracefully', async () => { const EXPECTED_HEADERS = { - 'Cache-Control': 'no-cache', + 'Cache-Control': 'max-age=60', 'Content-Type': 'application/json', }; const { body, headers, statusCode } = await runQuery.extract('/run_query/fail');