Skip to content

Commit a1746a0

Browse files
committed
pasta is done
1 parent 83e52bc commit a1746a0

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

database/pasta.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,47 @@ const fs = require('fs').promises;
22
const path = require('path');
33
const { commandHash } = require('./commands');
44

5+
const exists = async (dir) => !!await fs.stat(dir).catch(_err => false);
6+
57
function getFilename(commandName, name) {
68
const namespace = commandHash(commandName);
79
const cleanName = commandHash(name);
810
return path.join(__dirname, `../storage/pasta/${namespace}/${cleanName}`);
911
}
1012

11-
async function dirSize(commandName) {
12-
const dir = path.join(
13+
function getDir(commandName) {
14+
return path.join(
1315
__dirname,
1416
`../storage/pasta/${commandHash(commandName)}`,
1517
);
16-
const stats = (await readdir(dir)).map((file) =>
18+
}
19+
20+
async function dirSize(dir) {
21+
const stats = (await fs.readdir(dir)).map((file) =>
1722
fs.stat(path.join(dir, file)),
1823
);
1924
return (await Promise.all(stats)).reduce((acc, { size }) => acc + size, 0);
2025
}
2126

2227
async function loadPasta(commandName, pastaName) {
2328
const filename = getFilename(commandName, pastaName);
24-
if (!(await fs.access(filename))) return;
29+
if (!(await exists(filename))) return;
2530
return await fs.readFile(filename, 'utf8');
2631
}
2732
async function savePasta(commandName, pastaName, content) {
28-
if ((await dirSize(commandName)) > 20971520)
33+
const dir = getDir(commandName);
34+
if (!await exists(dir)) {
35+
await fs.mkdir(dir);
36+
}
37+
if ((await dirSize(dir)) > 20971520)
2938
throw new Error('paste namespace limit is 20MB');
3039
const filename = getFilename(commandName, pastaName);
3140
if (typeof content !== 'string')
3241
throw new Error('content must be a string');
3342
if (!content.length) throw new Error('string cannot be length zero');
3443
if (content.length > 1048576) throw new Error('paste size limit is 1MB');
3544
await fs.writeFile(filename, content, 'utf8');
36-
const url = `/${commandName}/${pastaName}`;
45+
const url = `/${encodeURIComponent(commandName)}/${encodeURIComponent(pastaName)}`;
3746
return {
3847
html: `/html${url}`,
3948
text: `/text${url}`,

irc/evaluate/vm.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ async function createVM({ node, maxTimeout = 60000 }) {
211211
ctx.setSync('_makePasta', timeoutRef((name, content) => (
212212
env.namespace
213213
&& node.parent.database.pasta.save(env.namespace, name, content)
214+
.then(result => new ivm.ExternalCopy(result).copyInto())
214215
)));
215216

216217
const scriptRef = await (await isolate.compileScript(`
@@ -360,7 +361,13 @@ async function createVM({ node, maxTimeout = 60000 }) {
360361
});
361362

362363
IRC.makePasta = (name, pasta) => {
363-
return ref.makePasta.applySyncPromise(undefined, [name, pasta]);
364+
const { html, text } = ref.makePasta
365+
.applySyncPromise(undefined, [name, pasta]);
366+
367+
return {
368+
html: IRC.webAddress + html,
369+
text: IRC.webAddress + text,
370+
};
364371
};
365372

366373
IRC.resetBuffer = () => {

web/server/pasta/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { readFileSync } = require('fs');
33
module.exports = ({ app, parent }) => {
44
const htmlTemplate = readFileSync(__dirname + '/index.html', 'utf8');
55
const { pasta } = parent.database;
6-
const btoa = str => Buffer.from(str, 'base64').toString('binary');
6+
const btoa = str => Buffer.from(str).toString('base64');
77

88
app.get('/:type(html|text)/:cmd/:name', (req, res) => {
99
const { cmd, name, type } = req.params;

0 commit comments

Comments
 (0)