Skip to content

Commit 41497b0

Browse files
committed
avoid using node sync methods
1 parent 11a30c0 commit 41497b0

3 files changed

Lines changed: 15 additions & 18 deletions

File tree

server/src/broadcast/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,17 @@ export const broadcast = ({
106106
});
107107
});
108108

109-
app.get('/channels/:id', (req, res) => {
109+
app.get('/channels/:id', async (req, res) => {
110110
const channel = req.params.id;
111111
if (channels[channel]) {
112112
channels[channel].lastAccessed = Date.now();
113113
const hasData = Object.keys(channels[channel].data || {}).length > 0;
114114
const views = ['index', 'code', 'result'] as const;
115115
const view = req.query.view;
116116
const file = views.find((v) => v === view) || (hasData ? 'index' : 'result');
117-
const fileContent = fs
118-
.readFileSync(path.join(broadcastDir, `/${file}.html`), 'utf-8')
119-
.replaceAll('{{AppUrl}}', appUrl);
117+
const fileContent = (
118+
await fs.promises.readFile(path.join(broadcastDir, `/${file}.html`), 'utf-8')
119+
).replaceAll('{{AppUrl}}', appUrl);
120120
res.status(200).send(fileContent);
121121
} else {
122122
res.status(404).send('Channel not found!');

server/src/sandbox.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import path from 'node:path';
66
import { sandboxVersion } from '../../src/livecodes/html/sandbox/index.ts';
77
import { dirname } from './utils.ts';
88

9-
export const sandbox = ({ hostname, port }: { hostname: string; port: number }) => {
9+
export const sandbox = async ({ hostname, port }: { hostname: string; port: number }) => {
1010
const app = express();
1111

1212
app.use(cors());
1313
app.disable('x-powered-by');
1414

1515
const sandboxDir = path.resolve(dirname, 'sandbox');
1616
let sandboxVersionDir = path.resolve(sandboxDir, sandboxVersion);
17-
fs.readdirSync(sandboxDir).forEach((v) => {
18-
if (fs.statSync(path.resolve(sandboxDir, v)).isDirectory()) {
17+
const dirs = await fs.promises.readdir(sandboxDir);
18+
for (const v of dirs) {
19+
if ((await fs.promises.stat(path.resolve(sandboxDir, v))).isDirectory()) {
1920
sandboxVersionDir = path.resolve(sandboxDir, v);
2021
}
21-
});
22+
}
2223

2324
app.use('/', (req, res) => {
2425
if (req.path === '/') {
@@ -33,11 +34,8 @@ export const sandbox = ({ hostname, port }: { hostname: string; port: number })
3334
: req.path;
3435
res.set('Content-Type', 'text/html');
3536
const filePath = path.resolve(dirname, 'sandbox' + reqPath);
36-
if (fs.existsSync(filePath)) {
37-
res.status(200).sendFile(filePath);
38-
return;
39-
}
40-
res.status(404).sendFile(path.resolve(sandboxVersionDir, 'index.html'));
37+
const onError = () => res.status(404).sendFile(path.resolve(sandboxVersionDir, 'index.html'));
38+
res.status(200).sendFile(filePath, onError);
4139
});
4240

4341
app.listen(port, () => {

server/src/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const getDirname = (metaUrl: string) => path.dirname(fileURLToPath(metaUrl));
1010
export const dirname = getDirname(import.meta.url);
1111
export const appDir = path.resolve(dirname, '../../build/');
1212

13-
const getFileContent = async (fullUrl: string) => {
13+
const getFileContent = (fullUrl: string): Promise<string> => {
1414
let pathname: string;
1515
try {
1616
const url = new URL(fullUrl);
@@ -22,10 +22,9 @@ const getFileContent = async (fullUrl: string) => {
2222
pathname = 'index.html';
2323
}
2424
let filePath = path.resolve(appDir, pathname);
25-
if (!fs.existsSync(filePath)) {
26-
filePath = path.resolve(appDir, '404.html');
27-
}
28-
return fs.promises.readFile(filePath, 'utf8');
25+
return fs.promises
26+
.readFile(filePath, 'utf8')
27+
.catch(() => fs.promises.readFile(path.resolve(appDir, '404.html'), 'utf8'));
2928
};
3029

3130
const convertToWebRequest = (req: express.Request) => {

0 commit comments

Comments
 (0)