Skip to content

Commit

Permalink
return the raw result from the relay when the request is coming from …
Browse files Browse the repository at this point in the history
…a non-browser user agent (#593)

* return the raw result from the relay when the request is coming from a non-browser user agent

* clean up after myself
  • Loading branch information
sachinr authored Nov 13, 2023
1 parent 62d1fe8 commit 6c753a1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
6 changes: 5 additions & 1 deletion apps/zipper.dev/src/components/playground/tab-runs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Avatar,
Button,
Spacer,
Code,
} from '@chakra-ui/react';
import { useRouter } from 'next/router';
import {
Expand All @@ -46,6 +47,7 @@ import { JSONViewer } from '../json-editor';
import { AppConsole } from './app-console';
import { LogMessage } from '@zipper/types';
import { TITLE_COLUMN_MIN_WIDTH } from './constants';
import { Markdown } from '@zipper/ui';

type HistoryTabProps = {
appId: string;
Expand Down Expand Up @@ -372,7 +374,9 @@ const HistoryTab: React.FC<HistoryTabProps> = ({ appId }) => {
<Heading fontSize="md" mb="4">
Inputs
</Heading>
<JSONViewer value={modalValue} options={{ readOnly: true }} />
<Markdown>{`\`\`\`js
${modalValue}
\`\`\``}</Markdown>
{logValue.length > 0 && (
<>
<Heading fontSize="md" my="4">
Expand Down
8 changes: 5 additions & 3 deletions apps/zipper.dev/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { ZIPPER_TEMP_USER_ID_COOKIE_NAME } from '@zipper/utils';
import {
hasBrowserLikeUserAgent,
ZIPPER_TEMP_USER_ID_COOKIE_NAME,
} from '@zipper/utils';
import { hasZipperEszipHeader } from '~/utils/eszip-utils';
import { hasBrowserLikeUserAgent } from '~/utils/user-agent';

const parseZipperSrcPath = (req: NextRequest) => {
const matches = req.nextUrl.pathname.match(
Expand All @@ -25,7 +27,7 @@ export default async (req: NextRequest) => {
const zipperSrcParams = parseZipperSrcPath(req);
if (
zipperSrcParams &&
(!hasBrowserLikeUserAgent(req) || hasZipperEszipHeader(req))
(!hasBrowserLikeUserAgent(req.headers) || hasZipperEszipHeader(req))
) {
const { appSlug, filename } = zipperSrcParams;
const url = new URL(`/api/src/${appSlug}/latest/${filename}`, req.url);
Expand Down
11 changes: 0 additions & 11 deletions apps/zipper.dev/src/utils/user-agent.ts

This file was deleted.

11 changes: 11 additions & 0 deletions apps/zipper.run/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import htmlHandler from './api-handlers/html.handler';

import {
getZipperApiUrl,
hasBrowserLikeUserAgent,
ZIPPER_TEMP_USER_ID_COOKIE_NAME,
} from '@zipper/utils';
import { jwtVerify } from 'jose';
Expand Down Expand Up @@ -95,6 +96,16 @@ async function maybeGetCustomResponse(
return NextResponse.rewrite(url, { request: { headers } });
}
}

default: {
if (!hasBrowserLikeUserAgent(request.headers)) {
console.log('not a browser so returning the relay');
return serveRelay({
request,
bootOnly: false,
});
}
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/@zipper-ui/src/components/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const Code = ({
language.toLowerCase(),
)
) {
code = prettierFormat(code);
try {
code = prettierFormat(code);
} catch (e) {}
}

return (
Expand Down
1 change: 1 addition & 0 deletions packages/@zipper-utils/generated/index.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export * from '../src/utils/parseBody';
export * from '../src/utils/safe-json';
export * from '../src/utils/settings';
export * from '../src/utils/url';
export * from '../src/utils/user-agent';
export * from '../src/utils/uuid';
export * from '../src/constants';
10 changes: 10 additions & 0 deletions packages/@zipper-utils/src/utils/user-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Bowser from 'bowser';

export const hasBrowserLikeUserAgent = (headers: Headers) => {
const parser = Bowser.getParser(headers.get('user-agent') || '');
return !!(
parser.getBrowserName() &&
parser.getEngineName() &&
parser.getOSName()
);
};

0 comments on commit 6c753a1

Please sign in to comment.