Skip to content

Commit

Permalink
refactor(cli): polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
charIeszhao committed Jul 31, 2024
1 parent ec8aa6e commit d013cc2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
8 changes: 7 additions & 1 deletion .changeset/slow-buses-rhyme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ Or if you don't have your custom UI pages hosted on a dev server, you can use th
npm cli proxy --endpoint https://<tenant-id>.logto.app --port 9000 --experience-path /path/to/your/custom/ui
```

This sets up the proxy and it will be running on your local machine at `http://localhost:9000/`.
This command also works if you have enabled custom domain in your Logto tenant. E.g.:

```bash
npm cli proxy --endpoint https://your-custom-domain.com --port 9000 --experience-path /path/to/your/custom/ui
```

This should set up the proxy and it will be running on your local machine at `http://localhost:9000/`.

Finally, run your application and set its Logto endpoint to the proxy address `http://localhost:9000/` instead.

Expand Down
32 changes: 19 additions & 13 deletions packages/cli/src/commands/proxy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ export const createProxy = (targetUrl: string, onProxyResponse?: OnProxyEvent['p
});
};

export const createStaticFileProxy = (staticPath: string) => {
const index = 'index.html';
const indexContentType = 'text/html; charset=utf-8';
const noCache = 'no-cache, no-store, must-revalidate';
const maxAgeSevenDays = 'max-age=604_800_000';
const index = 'index.html';
const indexContentType = 'text/html; charset=utf-8';
const noCache = 'no-cache, no-store, must-revalidate';
const maxAgeSevenDays = 'max-age=604_800_000';

return async (request: http.IncomingMessage, response: http.ServerResponse) => {
export const createStaticFileProxy =
(staticPath: string) => async (request: http.IncomingMessage, response: http.ServerResponse) => {
if (!request.url) {
response.writeHead(400).end();
return;
}

if (request.method === 'HEAD' || request.method === 'GET') {
const loadIndex = !isFileAssetPath(request.url);
const requestPath = path.join(staticPath, loadIndex ? index : request.url);
const fallBackToIndex = !isFileAssetPath(request.url);
const requestPath = path.join(staticPath, fallBackToIndex ? index : request.url);
try {
const content = await fs.readFile(requestPath, 'utf8');
response.setHeader('cache-control', loadIndex ? noCache : maxAgeSevenDays);
response.setHeader('content-type', loadIndex ? indexContentType : getMimeType(request.url));
response.setHeader('cache-control', fallBackToIndex ? noCache : maxAgeSevenDays);
response.setHeader('content-type', getMimeType(request.url));
response.writeHead(200);
response.end(content);
} catch (error: unknown) {
Expand All @@ -62,7 +62,6 @@ export const createStaticFileProxy = (staticPath: string) => {
}
}
};
};

/**
* Intercept the response from Logto endpoint and replace Logto endpoint URLs in the response with the
Expand Down Expand Up @@ -131,7 +130,7 @@ export const checkExperienceInput = (url?: string, staticPath?: string) => {
'A valid sign-in experience URI must be provided. E.g.: http://localhost:4000'
);
}
if (staticPath && !existsSync(path.join(staticPath, 'index.html'))) {
if (staticPath && !existsSync(path.join(staticPath, index))) {
consoleLog.fatal('The provided path does not contain a valid index.html file.');
}
};
Expand All @@ -147,4 +146,11 @@ export const isLogtoRequestPath = (requestPath?: string) =>
['/oidc/', '/api/'].some((path) => requestPath?.startsWith(path)) || requestPath === '/consent';

const isFileAssetPath = (url: string) => url.split('/').at(-1)?.includes('.');
const getMimeType = (filePath: string) => mime.getType(filePath) ?? 'application/octet-stream';

const getMimeType = (requestPath: string) => {
const fallBackToIndex = !isFileAssetPath(requestPath);
if (fallBackToIndex) {
return indexContentType;
}
return mime.getType(requestPath) ?? 'application/octet-stream';
};

0 comments on commit d013cc2

Please sign in to comment.