Skip to content

Commit

Permalink
Merge branch 'patch' into dataview-queryparams
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderGeere authored Nov 1, 2024
2 parents e500b1c + e61199b commit bed42ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
51 changes: 38 additions & 13 deletions mod/provider/cloudfront.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
/**
## /provider/cloudfront
The cloudfront provider module exports a method to fetch resources from an AWS cloudfront service.
@requires fs
@requires path
@requires module:/utils/logger
@requires @aws-sdk/cloudfront-signer
@module /provider/cloudfront
*/

const { readFileSync } = require('fs')

const { join } = require('path')

const { getSignedUrl } = require('@aws-sdk/cloudfront-signer');

const logger = require('../utils/logger')

const nodeFetch = require('node-fetch')
/**
@function cloudfront
@async
@description
The method creates a signed URL for a cloudfront resource, fetches the resource and returns the fetched resource.
A buffer is returned with the ref.params.buffer flag.
module.exports = async ref => {
JSON is returned if the URL path matches the .json file ending to fetch a JSON resource from the cloudfront service.
The fetch response will be parsed as text by default.
@param {Object|string} ref Reference object or URL string.
@property {Object} [ref.params] Optional parameters for the request.
@property {string} [params.url] Cloudfront resource URL.
@property {boolean} [params.signedURL] Return a signedURL only.
@property {boolean} [params.buffer] Return a buffer from the fetch.
@returns {Promise<String|JSON|Buffer|Error>} The method resolves to either JSON, Text, or Buffer dependent ref.params.
*/
module.exports = async function cloudfront(ref) {

try {

// Subtitutes {*} with process.env.SRC_* key values.
// Substitutes {*} with process.env.SRC_* key values.
const url = (ref.params?.url || ref).replace(/{(?!{)(.*?)}/g,
matched => process.env[`SRC_${matched.replace(/(^{)|(}$)/g, '')}`])

const date = new Date(Date.now())

date.setDate(date.getDate() + 1);

const signedURL = getSignedUrl({
Expand All @@ -32,13 +58,12 @@ module.exports = async ref => {

// Return signedURL only from request.
if (ref.params?.signedURL) {

return signedURL;
}

const response = await nodeFetch(signedURL)

logger(`${response.status} - ${url}`,'cloudfront')
const response = await fetch(signedURL)
logger(`${response.status} - ${url}`, 'cloudfront')

if (response.status >= 300) return new Error(`${response.status} ${ref}`)

Expand All @@ -48,8 +73,8 @@ module.exports = async ref => {

return await response.text()

} catch(err) {
} catch (err) {

console.error(err)
}

}
}
2 changes: 1 addition & 1 deletion public/views/_default.html
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
// Show the tabview if not already visible.
if (tabview.classList.contains('desktop-display-none')) {
tabview.classList.remove('desktop-display-none');
document.body.style.gridTemplateRows = 'auto 10px 50px';
document.body.style.gridTemplateRows = 'auto 10px 200px';
}
},
removeLastTab: () => {
Expand Down

0 comments on commit bed42ad

Please sign in to comment.