Skip to content

Commit

Permalink
extract to other files
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Jan 28, 2025
1 parent 1eef575 commit 2bff250
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 92 deletions.
2 changes: 1 addition & 1 deletion packages/nextjs/src/app-router/keyless-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ export async function deleteKeylessAction() {
return;
}

await import('../server/keyless-node.js').then(m => m.removeKeyless());
await import('../server/keyless-node.js').then(m => m.removeKeyless()).catch(() => {});
return;
}
2 changes: 1 addition & 1 deletion packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const auth: AuthFn = async () => {
}

try {
const isSrcAppDir = await import('../../server/keyless-node.js').then(m => m.hasSrcAppDir());
const isSrcAppDir = await import('../../server/fs/middleware-location.js').then(m => m.hasSrcAppDir());
return [`Your Middleware exists at ./${isSrcAppDir ? 'src/' : ''}middleware.ts`];
} catch {
return [];
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/server/createGetAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createAsyncGetAuth = ({
assertAuthStatus(req, noAuthStatusMessage);
}

const missConfiguredMiddlewareLocation = await import('./keyless-node.js')
const missConfiguredMiddlewareLocation = await import('./fs/middleware-location.js')
.then(m => m.suggestMiddlewareLocation())
.catch(() => undefined);

Expand Down
48 changes: 48 additions & 0 deletions packages/nextjs/src/server/fs/middleware-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { nodeCwdOrThrow, nodeFsOrThrow, nodePathOrThrow } from './utils';

function hasSrcAppDir() {
const { existsSync } = nodeFsOrThrow();
const path = nodePathOrThrow();
const cwd = nodeCwdOrThrow();

const projectWithAppSrc = path.join(cwd(), 'src', 'app');

return !!existsSync(projectWithAppSrc);
}

function suggestMiddlewareLocation() {
const suggestionMessage = (to?: 'src/', from?: 'src/app/' | 'app/') =>
`Clerk: clerkMiddleware() was not run, your middleware file might be misplaced. Move your middleware file to ./${to || ''}middleware.ts. Currently located at ./${from || ''}middleware.ts`;

const { existsSync } = nodeFsOrThrow();
const path = nodePathOrThrow();
const cwd = nodeCwdOrThrow();

const projectWithAppSrcPath = path.join(cwd(), 'src', 'app');
const projectWithAppPath = path.join(cwd(), 'app');

if (existsSync(projectWithAppSrcPath)) {
if (existsSync(path.join(projectWithAppSrcPath, 'middleware.ts'))) {
return suggestionMessage('src/', 'src/app/');
}

if (existsSync(path.join(cwd(), 'middleware.ts'))) {
return suggestionMessage('src/');
}

// default error
return undefined;
}

if (existsSync(projectWithAppPath)) {
if (existsSync(path.join(projectWithAppPath, 'middleware.ts'))) {
return suggestionMessage(undefined, 'app/');
}
// default error
return undefined;
}

return undefined;
}

export { suggestMiddlewareLocation, hasSrcAppDir };
102 changes: 13 additions & 89 deletions packages/nextjs/src/server/keyless-node.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { AccountlessApplication } from '@clerk/backend';

/**
* Attention: Only import this module when the node runtime is used.
* We are using conditional imports to mitigate bundling issues with Next.js server actions on version prior to 14.1.0.
*/
// @ts-ignore
import nodeRuntime from '#safe-node-apis';

import { createClerkClientWithOptions } from './createClerkClient';
import { nodeCwdOrThrow, nodeFsOrThrow, nodePathOrThrow } from './fs/utils';

/**
* The Clerk-specific directory name.
Expand All @@ -20,40 +14,15 @@ const CLERK_HIDDEN = '.clerk';
*/
const CLERK_LOCK = 'clerk.lock';

const throwMissingFsModule = () => {
throw new Error("Clerk: fsModule.fs is missing. This is an internal error. Please contact Clerk's support.");
};

const safeNodeRuntimeFs = () => {
if (!nodeRuntime.fs) {
throwMissingFsModule();
}
return nodeRuntime.fs;
};

const safeNodeRuntimePath = () => {
if (!nodeRuntime.path) {
throwMissingFsModule();
}
return nodeRuntime.path;
};

const safeNodeCwd = () => {
if (!nodeRuntime.cwd) {
throwMissingFsModule();
}
return nodeRuntime.cwd;
};

/**
* The `.clerk/` directory is NOT safe to be committed as it may include sensitive information about a Clerk instance.
* It may include an instance's secret key and the secret token for claiming that instance.
*/
function updateGitignore() {
const { existsSync, writeFileSync, readFileSync, appendFileSync } = safeNodeRuntimeFs();
const { existsSync, writeFileSync, readFileSync, appendFileSync } = nodeFsOrThrow();

const path = safeNodeRuntimePath();
const cwd = safeNodeCwd();
const path = nodePathOrThrow();
const cwd = nodeCwdOrThrow();
const gitignorePath = path.join(cwd(), '.gitignore');
if (!existsSync(gitignorePath)) {
writeFileSync(gitignorePath, '');
Expand All @@ -68,8 +37,8 @@ function updateGitignore() {
}

const generatePath = (...slugs: string[]) => {
const path = safeNodeRuntimePath();
const cwd = safeNodeCwd();
const path = nodePathOrThrow();
const cwd = nodeCwdOrThrow();
return path.join(cwd(), CLERK_HIDDEN, ...slugs);
};

Expand All @@ -80,7 +49,7 @@ const getKeylessReadMePath = () => generatePath(_TEMP_DIR_NAME, 'README.md');
let isCreatingFile = false;

export function safeParseClerkFile(): AccountlessApplication | undefined {
const { readFileSync } = safeNodeRuntimeFs();
const { readFileSync } = nodeFsOrThrow();
try {
const CONFIG_PATH = getKeylessConfigurationPath();
let fileAsString;
Expand All @@ -99,7 +68,7 @@ export function safeParseClerkFile(): AccountlessApplication | undefined {
* Using both an in-memory and file system lock seems to be the most effective solution.
*/
const lockFileWriting = () => {
const { writeFileSync } = safeNodeRuntimeFs();
const { writeFileSync } = nodeFsOrThrow();

isCreatingFile = true;

Expand All @@ -116,7 +85,7 @@ const lockFileWriting = () => {
};

const unlockFileWriting = () => {
const { rmSync } = safeNodeRuntimeFs();
const { rmSync } = nodeFsOrThrow();

try {
rmSync(CLERK_LOCK, { force: true, recursive: true });
Expand All @@ -128,12 +97,12 @@ const unlockFileWriting = () => {
};

const isFileWritingLocked = () => {
const { existsSync } = safeNodeRuntimeFs();
const { existsSync } = nodeFsOrThrow();
return isCreatingFile || existsSync(CLERK_LOCK);
};

async function createOrReadKeyless(): Promise<AccountlessApplication | null> {
const { writeFileSync, mkdirSync } = safeNodeRuntimeFs();
const { writeFileSync, mkdirSync } = nodeFsOrThrow();

/**
* If another request is already in the process of acquiring keys return early.
Expand Down Expand Up @@ -197,7 +166,7 @@ This directory is auto-generated from \`@clerk/nextjs\` because you are running
}

function removeKeyless() {
const { rmSync } = safeNodeRuntimeFs();
const { rmSync } = nodeFsOrThrow();

/**
* If another request is already in the process of acquiring keys return early.
Expand All @@ -221,49 +190,4 @@ function removeKeyless() {
unlockFileWriting();
}

function hasSrcAppDir() {
const { existsSync } = safeNodeRuntimeFs();
const path = safeNodeRuntimePath();
const cwd = safeNodeCwd();

const projectWithAppSrc = path.join(cwd(), 'src', 'app');

return !!existsSync(projectWithAppSrc);
}

function suggestMiddlewareLocation() {
const suggestionMessage = (to?: 'src/', from?: 'src/app/' | 'app/') =>
`Clerk: Move your middleware file to ./${to || ''}middleware.ts. Currently located at ./${from || ''}middleware.ts`;

const { existsSync } = safeNodeRuntimeFs();
const path = safeNodeRuntimePath();
const cwd = safeNodeCwd();

const projectWithAppSrcPath = path.join(cwd(), 'src', 'app');
const projectWithAppPath = path.join(cwd(), 'app');

if (existsSync(projectWithAppSrcPath)) {
if (existsSync(path.join(projectWithAppSrcPath, 'middleware.ts'))) {
return suggestionMessage('src/', 'src/app/');
}

if (existsSync(path.join(cwd(), 'middleware.ts'))) {
return suggestionMessage('src/');
}

// default error
return undefined;
}

if (existsSync(projectWithAppPath)) {
if (existsSync(path.join(projectWithAppPath, 'middleware.ts'))) {
return suggestionMessage(undefined, 'app/');
}
// default error
return undefined;
}

return undefined;
}

export { createOrReadKeyless, removeKeyless, suggestMiddlewareLocation, hasSrcAppDir };
export { createOrReadKeyless, removeKeyless };

0 comments on commit 2bff250

Please sign in to comment.