Skip to content

Commit

Permalink
Merge pull request #29 from DIG-Network/release/v0.0.1-alpha.32
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.32
  • Loading branch information
MichaelTaylor3D authored Sep 18, 2024
2 parents 85c3d77 + 02475ce commit 0600f3c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.32](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.31...v0.0.1-alpha.32) (2024-09-18)


### Bug Fixes

* duplicate path part ([1dfcc46](https://github.com/DIG-Network/dig-content-server/commit/1dfcc46c1ced8986395c082737033a0eca893138))

### [0.0.1-alpha.31](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.30...v0.0.1-alpha.31) (2024-09-18)

### [0.0.1-alpha.30](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.29...v0.0.1-alpha.30) (2024-09-17)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dig-content-server",
"version": "0.0.1-alpha.31",
"version": "0.0.1-alpha.32",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
57 changes: 32 additions & 25 deletions src/middleware/parseUdi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ import { DataStore } from "@dignetwork/dig-sdk";

const validChainNames = ["chia"]; // List of valid chain names

function removeDuplicatePathPart(path: string): string {
// Split the path into segments, ignoring leading/trailing slashes
const parts = path.split('/').filter(part => part.length > 0);

// Check if the path has at least two segments
if (parts.length >= 2) {
const firstPart = parts[0];
const secondPart = parts[1];

// Check if the first two parts are identical and at least 64 characters long
if (firstPart === secondPart && firstPart.length >= 64) {
// Remove the duplicate second part
parts.splice(1, 1);
}
}

// Reconstruct the path with a leading slash
return '/' + parts.join('/');
}

export const parseUdi = async (
req: Request,
res: Response,
Expand All @@ -15,19 +35,22 @@ export const parseUdi = async (
return next();
}

const referrer = req.get("Referer") || "";
// **Apply removeDuplicatePathPart to the request path**
const modifiedPath = removeDuplicatePathPart(req.originalUrl);

const referrer = req.get("Referer") || "";
let cookieData = req.cookies.udiData || null;

let chainName: string | null = null;
let storeId: string = "";
let rootHash: string | null = null;


// **Use modifiedPath instead of req.originalUrl**
const pathSegments = modifiedPath.split("/").filter(segment => segment.length > 0);

// Extract the first path part as the storeId (assumed app identifier)
const pathSegment = req.params.storeId || ""; // Expecting storeId to be the first path segment
const originalPathSegments = req.originalUrl.split("/").slice(2); // Remove the first segment, which is the storeId part
const pathSegment = pathSegments[0] || ""; // Expecting storeId to be the first path segment
const originalPathSegments = pathSegments.slice(1); // Remove the first segment, which is the storeId part
let appendPath =
originalPathSegments.length > 0
? `/${originalPathSegments.join("/")}`
Expand All @@ -52,21 +75,6 @@ export const parseUdi = async (
storeId = parts[0];
}


console.log(req.originalUrl);

// Check for the edge case where the first and second path parts are the same
if (originalPathSegments.length > 1 && storeId.length >= 64) {
const secondPathPart = originalPathSegments[0];
const thirdPathPart = originalPathSegments[1];

if (secondPathPart === thirdPathPart && secondPathPart.length >= 64) {
// Remove the duplicate second occurrence from appendPath
appendPath = `/${originalPathSegments.slice(2).join("/")}`;
console.log("Duplicate path part found, collapsing the path.");
}
}

// Log extracted values
console.log(
"Extracted values - Chain Name:",
Expand All @@ -80,19 +88,18 @@ export const parseUdi = async (
// Validate storeId length
if (!storeId || storeId.length !== 64) {
if (cookieData) {
const { chainName: cookieChainName, storeId: cookieStoreId } =
cookieData;
const { chainName: cookieChainName, storeId: cookieStoreId } = cookieData;

console.warn("Invalid storeId, redirecting to referrer:", referrer);
return res.redirect(
302,
`/${cookieChainName}.${cookieStoreId}` + req.originalUrl
`/${cookieChainName}.${cookieStoreId}` + appendPath
);
}

if (referrer) {
console.warn("Invalid storeId, redirecting to referrer:", referrer);
return res.redirect(302, referrer + req.originalUrl);
return res.redirect(302, referrer + appendPath);
}
return res.status(400).send("Invalid or missing storeId.");
}
Expand Down Expand Up @@ -163,8 +170,8 @@ export const parseUdi = async (
{
httpOnly: true,
secure: false,
maxAge: 5 * 60 * 1000, // Cookie expires after 5 minutes (5 * 60 * 1000 ms)
expires: new Date(Date.now() + 5 * 60 * 1000), // Expiry date explicitly set for 5 minutes
maxAge: 5 * 60 * 1000, // Cookie expires after 5 minutes
expires: new Date(Date.now() + 5 * 60 * 1000),
}
);

Expand Down

0 comments on commit 0600f3c

Please sign in to comment.