Skip to content

Commit

Permalink
fixing parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
helios2003 committed Jul 22, 2024
1 parent bddad2a commit ef655c8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 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
5 changes: 3 additions & 2 deletions apps/studio-next/src/app/api/crawler/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 Base64searchParams = request.nextUrl.searchParams.get('base64');
Expand All @@ -16,11 +17,11 @@ export async function GET(request: NextRequest) {
info = await parseURL(Base64searchParams);
}
if (URLsearchParams) {
// fetch the document
// fetch the document information from the URL
try {
const response = await axios.get(URLsearchParams);
if (response.status === 200) {
info = response.data;
info = await parseURL(response.data);
} else {
return new NextResponse("Not a valid URL", { status: 500 });
}
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
3 changes: 2 additions & 1 deletion apps/studio-next/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ 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");
const documentURL = request.nextUrl.searchParams.get("url");

if (!encodedDocument && !documentURL) {
return res;
}
Expand Down

0 comments on commit ef655c8

Please sign in to comment.