summaryrefslogtreecommitdiff
path: root/app/note-store.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/note-store.js')
-rw-r--r--app/note-store.js34
1 files changed, 10 insertions, 24 deletions
diff --git a/app/note-store.js b/app/note-store.js
index c816efd..eeebf28 100644
--- a/app/note-store.js
+++ b/app/note-store.js
@@ -1,17 +1,8 @@
-"use strict";
+import {readdir as rd, readFile as rf, writeFile as wf} from "node:fs/promises";
+import {sj, res204, err400, err500} from "./utils.js";
+import {authed} from "./auth.js";
-const fs = require("fs");
-const util = require("util");
-const {sj, res204, err400, err500} = require("./utils");
-
-const rd = util.promisify(fs.readdir);
-const rf = util.promisify(fs.readFile);
-const wf = util.promisify(fs.writeFile);
-
-const {R_OK,W_OK} = fs.constants;
-const exists = s => new Promise(r => fs.access(s, R_OK|W_OK, e => r(!e)));
-
-const NOTE_DIR = "./users";
+import {NOTE_DIR} from "./config.js";
function genNoteID() {
const now = new Date().toISOString();
@@ -19,15 +10,9 @@ function genNoteID() {
}
async function newNote(req, res) {
- let noteID, noteFile;
-
- do {
- noteID = genNoteID();
- noteFile = `${NOTE_DIR}/${req.uid}/${noteID}.md`;
- } while (await exists(noteFile)) // TODO increment
-
+ const noteID = genNoteID();
+ const noteFile = `${NOTE_DIR}/${req.uid}/${noteID}.md`;
console.log(Date.now()+` Creating note ${req.uid}:${noteID}`);
-
await wf(noteFile, "");
sj(res, {id:noteID, content:""});
@@ -36,7 +21,7 @@ async function newNote(req, res) {
async function getNote(req, res, match) {
console.log(Date.now()+` Getting note ${req.uid}:${match.noteID}`);
const noteFile = `${NOTE_DIR}/${req.uid}/${match.noteID}.md`;
- const content = await rf(noteFile, "UTF-8"); // TODO exists
+ const content = await rf(noteFile, "UTF-8");
sj(res, {id:match.noteID, content});
}
@@ -56,9 +41,10 @@ async function listNotes(req, res) {
}
// TODO list in order of recent updates w/ timestamps
-// TODO pagination?
+// TODO pagination? Bulk get?
+// TODO handle fs errors
-module.exports.attach = (app, {authed}) => {
+export const attach = app => {
app.get("/list", authed(listNotes));
app.post("/new", authed(newNote));
app.get("/(?<noteID>[0-9]{16})", authed(getNote));