Skip to content

Commit

Permalink
fix(zipper.run): ignore extension, search for name (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-nascimento authored Jan 16, 2024
1 parent 53f5f31 commit 9858613
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
24 changes: 12 additions & 12 deletions apps/zipper.run/src/components/applet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
__ZIPPER_TEMP_USER_ID,
X_ZIPPER_TEMP_USER_ID,
X_ZIPPER_ACCESS_TOKEN,
removeExtension,
} from '@zipper/utils';
import { deleteCookie } from 'cookies-next';
import { motion } from 'framer-motion';
Expand All @@ -53,7 +54,11 @@ import { useEffect, useMemo, useRef, useState } from 'react';
import { useForm } from 'react-hook-form';
import { HiChevronDoubleLeft, HiChevronDoubleRight } from 'react-icons/hi2';
import TimeAgo from 'react-timeago';
import { fetchBootPayloadCachedWithUserInfoOrThrow } from '~/utils/get-boot-info';
import {
fetchBootPayloadCachedWithUserInfoOrThrow,
getFilenameFromPath,
getPathFromFilename,
} from '~/utils/get-boot-info';
import { getConnectorsAuthUrl } from '~/utils/get-connectors-auth-url';
import { getAppletUrl, getRelayUrl } from '~/utils/get-relay-url';
import getValidSubdomain from '~/utils/get-valid-subdomain';
Expand All @@ -69,6 +74,7 @@ import ConnectorsAuthInputsSection from './connectors-auth-inputs-section';
import Header from './header';
import InputSummary from './input-summary';
import Unauthorized from './unauthorized';
import { get } from 'http';

const { __DEBUG__ } = process.env;

Expand Down Expand Up @@ -640,6 +646,8 @@ export const getServerSideProps: GetServerSideProps = async ({
resolvedUrl,
});

const path = filenameFromUrl ? getPathFromFilename(filenameFromUrl) : 'main';

const { token, userId } = await getZipperAuth(req);
const tempUserId = req.cookies[__ZIPPER_TEMP_USER_ID];

Expand All @@ -649,7 +657,7 @@ export const getServerSideProps: GetServerSideProps = async ({
subdomain,
tempUserId,
version: versionFromUrl,
filename: filenameFromUrl,
path,
token,
});
} catch (e: any) {
Expand All @@ -671,17 +679,9 @@ export const getServerSideProps: GetServerSideProps = async ({
const { app, entryPoint, parsedScripts, runnableScripts } = bootInfo;

const version = (versionFromUrl || 'latest').replace(/^@/, '');
const filename = filenameFromUrl || 'main';
// if (!filename.endsWith('.ts')) filename = `${filename}.ts}`;
const filename = getFilenameFromPath(path, runnableScripts);

if (
!runnableScripts.find((f) => {
return (
f === filename || f === `${filename}.ts` || f === `${filename}.tsx`
);
})
)
return { notFound: true };
if (!filename) return { notFound: true };

const parsedFile = findFileInParsedScripts(filename, parsedScripts);
const parsedAction = actionFromUrl && parsedFile?.actions?.[actionFromUrl];
Expand Down
20 changes: 18 additions & 2 deletions apps/zipper.run/src/utils/get-boot-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getZipperApiUrl,
noop,
parseDeploymentId,
removeExtension,
UNAUTHORIZED,
X_ZIPPER_TEMP_USER_ID,
} from '@zipper/utils';
Expand Down Expand Up @@ -235,16 +236,22 @@ export async function fetchBootPayloadCachedOrThrow({
}

export async function fetchBootPayloadCachedWithUserInfoOrThrow(
params: BootInfoParams & {
params: Omit<BootInfoParams, 'filename'> & {
path: string;
bootInfo?: BootInfo;
bootFetcher?: BootFetcher;
},
): Promise<BootPayload<true>> {
const bootPayload = await fetchBootPayloadCachedOrThrow(params);
const filename = getFilenameFromPath(
params.path,
bootPayload.bootInfo.runnableScripts,
);

// This is the non-cachable part (user specific)
const result = await fetchExtendedUserInfo({
...params,
filename,
bootInfo: params.bootInfo || bootPayload.bootInfo,
});

Expand All @@ -267,8 +274,17 @@ export async function fetchBootPayloadCachedWithUserInfoOrThrow(
};
}

export const getPathFromFilename = (filename: string) =>
removeExtension(filename);

export const getFilenameFromPath = (path: string, runnableScripts: string[]) =>
runnableScripts.find(
(scriptFilename) => path === removeExtension(scriptFilename),
);

export async function fetchBootInfoCachedWithUserOrThrow(
params: BootInfoParams & {
params: Omit<BootInfoParams, 'filename'> & {
path: string;
bootInfo?: BootInfo;
bootFetcher?: BootFetcher;
},
Expand Down
15 changes: 9 additions & 6 deletions apps/zipper.run/src/utils/relay-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import fetchBootInfo, {
fetchBootInfoCachedWithUserOrThrow,
fetchDeploymentCachedOrThrow,
fetchBasicUserInfo,
getPathFromFilename,
getFilenameFromPath,
} from './get-boot-info';
import getValidSubdomain from './get-valid-subdomain';
import { parseRunUrlPath } from '@zipper/utils';
Expand Down Expand Up @@ -243,11 +245,6 @@ export async function relayRequest(
tempUserId =
tempUserId || request.cookies.get(__ZIPPER_TEMP_USER_ID)?.value.toString();

let filename = filenamePassedIn || 'main.ts';
if (!filename.endsWith('.ts') && !filename.endsWith('.tsx')) {
filename = `${filename}.ts`;
}

const bootArgs = {
appId,
version,
Expand All @@ -262,12 +259,16 @@ export async function relayRequest(
return bootRelayRequest(bootArgs);
}

const path = filenamePassedIn
? getPathFromFilename(filenamePassedIn)
: 'main';

let bootInfo;
try {
bootInfo = await fetchBootInfoCachedWithUserOrThrow({
subdomain,
tempUserId,
filename,
path,
token,
deploymentId,
bootFetcher: async () => {
Expand Down Expand Up @@ -353,6 +354,8 @@ export async function relayRequest(
canUserEdit: userInfo.canUserEdit,
};

const filename = getFilenameFromPath(path, bootInfo.runnableScripts);

relayBody.path = filename;

const response = await fetch(relayUrl, {
Expand Down

0 comments on commit 9858613

Please sign in to comment.