Skip to content

Commit

Permalink
Fix type errors. (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey authored Dec 6, 2024
1 parent e99f4db commit 314edd0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
12 changes: 6 additions & 6 deletions conf/node/controllers/cloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export const getDateLessThan = () => {
const hours = date.getHours();

if (hours >= 12) {
return parseInt(new Date(date.setHours(24, 0, 0, 0)).getTime() / 1000);
return new Date(date.setHours(24, 0, 0, 0)).getTime() / 1000;
}

// This should be midnight tomorrow.
date.setDate(date.getDate() + 1);
return parseInt(new Date(date.setHours(0, 0, 0, 0)).getTime() / 1000);
return new Date(date.setHours(0, 0, 0, 0)).getTime() / 1000;
};

/**
Expand All @@ -87,11 +87,11 @@ export const getDateLessThan = () => {
* @param {Object} props
* @param {string} props.resource
* @param {number} props.dateLessThan
* @param {string} props.ipAddress
* @param {string} props.clientIp
* @returns {import('@aws-sdk/cloudfront-signer').CloudfrontSignedCookiesOutput} cookies - The signed CloudFront cookies
*/

export const getCookies = ({ resource, dateLessThan, ipAddress }) => {
export const getCookies = ({ resource, dateLessThan, clientIp }) => {
const policy = {
Statement: [
{
Expand All @@ -100,10 +100,10 @@ export const getCookies = ({ resource, dateLessThan, ipAddress }) => {
DateLessThan: {
"AWS:EpochTime": dateLessThan, // time in seconds
},
...(ipAddress?.length && {
...(clientIp?.length && {
// Optional, only if the IP address is provided
IpAddress: {
"AWS:SourceIp": `${ipAddress}/32`,
"AWS:SourceIp": `${clientIp}/32`,
},
}),
},
Expand Down
6 changes: 3 additions & 3 deletions conf/node/controllers/cloudfront.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ describe("getCookies", () => {
it("should return cookies for CloudFront", () => {
const dateLessThan = getDateLessThan();
const resource = "https://archive.example.com/*";
const ipAddress = "127.0.0.1";
const clientIp = "127.0.0.1";

const result = getCookies({
resource,
dateLessThan,
ipAddress,
clientIp,
});

expect(result).toBeDefined();
Expand All @@ -61,6 +61,6 @@ describe("getCookies", () => {
expect(statement.Condition.DateLessThan["AWS:EpochTime"]).toBe(
dateLessThan,
);
expect(statement.Condition.IpAddress["AWS:SourceIp"]).toBe(ipAddress);
expect(statement.Condition.IpAddress["AWS:SourceIp"]).toBe(`${clientIp}/32`);
});
});
16 changes: 10 additions & 6 deletions conf/node/controllers/httrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,18 @@ export const runHttrack = (cliArgs) => {
};
};

/**
* @typedef {Object} HttrackProgress
* @property {number} requestCount - the number of requests made
* @property {number} rate - the requests per second over the last 10 seconds
* @property {boolean} complete - true if the mirror is complete
*/

/**
* Get httrack progress from destination folder
*
* @param {string} dest
* @returns {Object} object
* @returns {number} object.requestCount - the number of requests made
* @returns {number} object.rate - the requests per second over the last 10 seconds
* @returns {bool} object.complete - true if the mirror is complete
* @returns {Promise<HttrackProgress>}
*/

export const getHttrackProgress = async (dest) => {
Expand Down Expand Up @@ -206,8 +210,8 @@ export const getHttrackProgress = async (dest) => {
* Wait for httrack to complete
*
* @param {string} dest
* @param {number} timeOut
* @returns {Promise<boolean>}
* @param {number} timeOutSeconds
* @returns {Promise<{timedOut: boolean}>}
*/

export const waitForHttrackComplete = async (
Expand Down
5 changes: 4 additions & 1 deletion conf/node/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"exclude": ["node_modules"]
"exclude": ["node_modules"],
"compilerOptions": {
"checkJs": true
},
}
6 changes: 5 additions & 1 deletion conf/node/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {
allowedTargetAgencies,
} from "./constants.js";

/**
* @typedef {import('express').Request & { mirror: { url: URL, agency: string, depth: number }}} Request
*/

/**
* Middleware to parse the url and agency from the incoming request body
*
* @param {import('express').Request} req
* @param {Request} req
* @param {import('express').Response} res
* @param {import('express').NextFunction} next
* @returns {void}
Expand Down
21 changes: 21 additions & 0 deletions conf/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions conf/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"s3-sync-client": "^4.3.1"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0"
}
}
12 changes: 3 additions & 9 deletions conf/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ app.post("/fetch-test", async function (req, res, next) {
});
res.status(200).send({ status });
} catch (err) {
// Handling errors like this will send the error to the defaule Express error handler.
// Handling errors like this will send the error to the default Express error handler.
// It will log the error to the console, return a 500 error page,
// and show the error message on dev environments, but hide it on production.
next(err);
Expand All @@ -76,12 +76,6 @@ app.post("/spider", function (req, res) {

app.get("/access-archive", async function (req, res, next) {
try {
const clientIp = req.headers["x-forwarded-for"]?.split(',')[0].trim();

console.log('headers', req.headers);

console.log('clientIp', clientIp);

// Get the current domain from the request
const appUrl = new URL(
`${req.headers["x-forwarded-proto"] || req.protocol}://${
Expand All @@ -96,15 +90,15 @@ app.get("/access-archive", async function (req, res, next) {
const cookies = getCookies({
resource: `${cdnUrl.origin}/*`,
dateLessThan: getDateLessThan(),
clientIp,
clientIp: Array.isArray(req.headers["x-real-ip"]) ? req.headers["x-real-ip"][0] : req.headers["x-real-ip"],
});

// Set the cookies on the response
Object.entries(cookies).forEach(([name, value]) => {
res.cookie(name, value, {
domain: cdnUrl.host,
secure: cdnUrl.protocol === "https:",
sameSite: "Lax",
sameSite: "lax",
httpOnly: true,
});
});
Expand Down

0 comments on commit 314edd0

Please sign in to comment.