Skip to content

Commit

Permalink
Merge pull request #2 from helios2003/fixes
Browse files Browse the repository at this point in the history
Enable OG Tags and image generation based on the ``url`` parameter as well
  • Loading branch information
helios2003 authored Jul 22, 2024
2 parents 79b53e3 + ef655c8 commit 2779272
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/studio-next/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ const nextConfig = {
},
}

module.exports = nextConfig
module.exports = nextConfig
1 change: 1 addition & 0 deletions apps/studio-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/react": "18.2.18",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.14",
"axios": "^1.7.2",
"codemirror": "^6.0.1",
"crawler-user-agents": "^1.0.142",
"eslint-config-next": "13.4.12",
Expand Down
27 changes: 24 additions & 3 deletions apps/studio-next/src/app/api/crawler/route.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { NextRequest, NextResponse } from "next/server";
import parseURL from "@/helpers/parser";
import { DocumentInfo } from "@/types";
import axios from "axios";
import { parse } from "path";

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams.get('base64');
const Base64searchParams = request.nextUrl.searchParams.get('base64');
const URLsearchParams = request.nextUrl.searchParams.get('url');

try {
if (!searchParams) return new NextResponse(null, { status: 200 });
if (!Base64searchParams && !URLsearchParams) return new NextResponse(null, { status: 200 });
let info: DocumentInfo | null = null;

const info: DocumentInfo | null = await parseURL(searchParams);
if (Base64searchParams) {
// directly run the parsing function
info = await parseURL(Base64searchParams);
}
if (URLsearchParams) {
// fetch the document information from the URL
try {
const response = await axios.get(URLsearchParams);
if (response.status === 200) {
info = await parseURL(response.data);
} else {
return new NextResponse("Not a valid URL", { status: 500 });
}
} catch (error) {
return new NextResponse("Not a valid URL", { status: 500 });
}
}

if (!info) {
const crawlerInfo = `
Expand Down
14 changes: 11 additions & 3 deletions apps/studio-next/src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Parser } from '@asyncapi/parser';
import { Input, Parser } from '@asyncapi/parser';
import { DocumentInfo } from '@/types';

export default async function parseURL(base64Document: string): Promise<DocumentInfo | null> {
export default async function parseURL(asyncapiDocument: string): Promise<DocumentInfo | null> {
const parser = new Parser();

const base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/;

let decodedDocument: Input = "";
if (base64Regex.test(asyncapiDocument)) {
decodedDocument = Buffer.from(asyncapiDocument, "base64").toString("utf-8");
} else {
decodedDocument = asyncapiDocument;
}

const decodedDocument = Buffer.from(base64Document, "base64").toString("utf-8");
const { document, diagnostics } = await parser.parse(decodedDocument);

if (diagnostics.length) {
Expand Down
14 changes: 11 additions & 3 deletions apps/studio-next/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ export async function middleware(request: NextRequest) {
const userAgents = crawlers.map(crawler => crawler.pattern);
const requestInfo = userAgent(request);
const res = NextResponse.next();
const documentURL = request.nextUrl.searchParams.get("url");

for (const ua of userAgents) {
if (requestInfo.ua.toLowerCase().includes(ua.toLowerCase())) {
const encodedDocument = request.nextUrl.searchParams.get("base64");
if (!encodedDocument) {

if (!encodedDocument && !documentURL) {
return res;
}
return NextResponse.rewrite(new URL(`/api/crawler?base64=${encodedDocument}`, request.url));
if (encodedDocument) {
return NextResponse.rewrite(new URL(`/api/crawler?base64=${encodedDocument}`, request.url));
}
if (documentURL) {
return NextResponse.rewrite(new URL(`/api/crawler?url=${documentURL}`, request.url));
}
}
}
return res;
}

export const config = {
matcher: ['/:base64'],
matcher: ['/:base64', '/:url'],
}
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 2779272

Please sign in to comment.