Enhance Node Backend Server File I/O Performance

This commit is contained in:
Yuhwan Kim
2024-06-05 01:25:16 +09:00
parent 1b776c3076
commit de5d6a32bf
2 changed files with 13 additions and 25 deletions

View File

@@ -7,7 +7,8 @@ const bodyParser = require('body-parser');
const fs = require('fs/promises') const fs = require('fs/promises')
const crypto = require('crypto') const crypto = require('crypto')
app.use(express.static(path.join(process.cwd(), 'dist'), {index: false})); app.use(express.static(path.join(process.cwd(), 'dist'), {index: false}));
app.use(bodyParser.json({ limit: 100000000 })); app.use(bodyParser.raw({ limit: 100000000 }));
app.use(bodyParser.json())
const {pipeline} = require('stream/promises') const {pipeline} = require('stream/promises')
let password = '' let password = ''
@@ -148,17 +149,11 @@ app.get('/api/read', async (req, res, next) => {
} }
try { try {
if(!existsSync(path.join(savePath, filePath))){ if(!existsSync(path.join(savePath, filePath))){
res.send({ res.send();
success: true,
content: null
});
} }
else{ else{
const data = await fs.readFile(path.join(savePath, filePath)); res.setHeader('Content-Type','application/octet-stream');
res.send({ res.sendFile(path.join(savePath, filePath));
success: true,
content: data.toString('base64')
});
} }
} catch (error) { } catch (error) {
next(error); next(error);
@@ -206,12 +201,9 @@ app.get('/api/list', async (req, res, next) => {
return return
} }
try { try {
const data = (await fs.readdir(path.join(savePath))).map((v) => {
return Buffer.from(v, 'hex').toString('utf-8')
})
res.send({ res.send({
success: true, success: true,
content: data content: await fs.readdir(path.join(savePath))
}); });
} catch (error) { } catch (error) {
next(error); next(error);
@@ -227,7 +219,7 @@ app.post('/api/write', async (req, res, next) => {
return return
} }
const filePath = req.headers['file-path']; const filePath = req.headers['file-path'];
const fileContent = Buffer.from(req.body.content, 'base64'); const fileContent = req.body
if (!filePath || !fileContent) { if (!filePath || !fileContent) {
res.status(400).send({ res.status(400).send({
error:'File path required' error:'File path required'

View File

@@ -9,11 +9,9 @@ export class NodeStorage{
await this.checkAuth() await this.checkAuth()
const da = await fetch('/api/write', { const da = await fetch('/api/write', {
method: "POST", method: "POST",
body: JSON.stringify({ body: value,
content: Buffer.from(value).toString('base64')
}),
headers: { headers: {
'content-type': 'application/json', 'content-type': 'application/octet-stream',
'file-path': Buffer.from(key, 'utf-8').toString('hex'), 'file-path': Buffer.from(key, 'utf-8').toString('hex'),
'risu-auth': auth 'risu-auth': auth
} }
@@ -35,17 +33,15 @@ export class NodeStorage{
'risu-auth': auth 'risu-auth': auth
} }
}) })
const data = await da.json()
if(da.status < 200 || da.status >= 300){ if(da.status < 200 || da.status >= 300){
throw "getItem Error" throw "getItem Error"
} }
if(data.error){
throw data.error const data = Buffer.from(await da.arrayBuffer())
} if (data.length == 0){
if(data.content === null){
return null return null
} }
return Buffer.from(data.content, 'base64') return data
} }
async keys():Promise<string[]>{ async keys():Promise<string[]>{
await this.checkAuth() await this.checkAuth()