Skip to content

Commit

Permalink
chore: improve file resolution and server functionality of scripts
Browse files Browse the repository at this point in the history
- Add support for resolving files with .js extension
- Refactor server to handle requests more flexibly
- Update watch script to ignore multiple directories
- Change default server port to 8514
  • Loading branch information
lingbopro committed Jan 20, 2025
1 parent 968a098 commit fa64a05
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
62 changes: 46 additions & 16 deletions dev/scripts/lib/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const __dirname = path.dirname(__filename);

const root = path.resolve(__dirname, '../../..');

const port = 5114;
const port = process.env.PORT ?? 8514;
let server;

export async function main(options) {
Expand All @@ -21,29 +21,59 @@ export async function main(options) {
`http://${process.env.HOST ?? 'localhost'}${req.url}`,
).pathname;
debug(`Got request: ${req.method} '${reqUrl}'`);
const url = '.' + reqUrl;
const filePath = path.join(root, url);
const extension = path.extname(filePath).slice(1);
if (req.method === 'GET') {
if (reqUrl === '/') {
res.writeHead(302, { Location: '/demos/index.html' });
res.end();
return;
}

const getFileStat = async (filePath) => {
if (!fs.existsSync(filePath)) {
res.writeHead(404);
res.end(`Cannot ${req.method} ${reqUrl}: Not Found`);
return;
return 'NOT_FOUND';
}
if (!(await fs.promises.lstat(filePath)).isFile()) {
res.writeHead(404);
res.end(`Cannot ${req.method} ${reqUrl}: Is not a file`);
return;
return 'NOT_A_FILE';
}
return 'EXISTS';
};
const sendFile = async (res, filePath) => {
const extension = path.extname(filePath).slice(1);
const mimeType = MIMETypes[extension] || 'text/plain';
res.setHeader('Content-Type', mimeType);
res.setHeader('X-Real-File-Path', filePath);
fs.createReadStream(filePath).pipe(res);
};
const resolveAndSendFile = async (reqUrl) => {
const filePath = path.join(root, reqUrl);
if (reqUrl === '/') {
res.writeHead(302, { Location: '/demos/index.html' });
res.end();
return;
}
const stat = await getFileStat(filePath);
if (stat !== 'EXISTS') {
// try to resolve the file as .js
if (path.extname(filePath) !== '.js') {
const jsFilePath = `${filePath}.js`;
const jsStat = await getFileStat(jsFilePath);
if (jsStat === 'EXISTS') {
sendFile(res, jsFilePath);
return;
}
}

if (stat === 'NOT_FOUND') {
res.writeHead(404);
res.end(`Cannot ${req.method} ${reqUrl}: Not Found`);
return;
}
if (stat === 'NOT_A_FILE') {
res.writeHead(404);
res.end(`Cannot ${req.method} ${reqUrl}: Is not a file`);
return;
}
} else {
sendFile(res, filePath);
}
};

if (req.method === 'GET') {
resolveAndSendFile(reqUrl);
return;
} else {
res.writeHead(405);
Expand Down
20 changes: 14 additions & 6 deletions dev/scripts/lib/watch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ const __dirname = path.dirname(__filename);

const root = path.resolve(__dirname, '../../..');

const ignoredPaths = [
'node_modules',
'test',
'dist',
'coverage',
'dev',
'.vscode',
'.github',
'.git',
];

const watcherAbortController = new AbortController();
const taskStack = [];
let taskProcessing = false;
Expand All @@ -30,12 +41,9 @@ export async function main(options) {
return;
}
const normalizedPathname = path.resolve(root, filename);
const isIgnored =
checkPath(path.join(root, '.__compile_cache__'), normalizedPathname) ||
checkPath(path.join(root, 'node_modules'), normalizedPathname) ||
checkPath(path.join(root, 'dist'), normalizedPathname) ||
checkPath(path.join(root, 'dev'), normalizedPathname) ||
checkPath(path.join(root, '.git'), normalizedPathname);
const isIgnored = ignoredPaths.some((ignoredPath) =>
checkPath(path.join(root, ignoredPath), normalizedPathname),
);
if (isIgnored) {
return;
}
Expand Down

0 comments on commit fa64a05

Please sign in to comment.