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

Remove legacy node-fetch and document mod/cloudfront ☁️ #1645

Merged
merged 5 commits into from
Nov 1, 2024
Merged
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
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)
}

}
}
Loading