Comparing version 0.0.1-alpha to 0.1.0
{ | ||
"name": "fscs", | ||
"version": "0.0.1-alpha", | ||
"version": "0.1.0", | ||
"description": "File System CRUD Server", | ||
@@ -5,0 +5,0 @@ "main": "./src/server.js", |
@@ -20,3 +20,3 @@ const DefaultMessages = { | ||
res.setHeader("Content-Type", "application/json"); | ||
res.end(JSON.stringify({ message: this.message || DefaultMessages[this.statusCode] })); | ||
res.end(JSON.stringify({ error: this.message || DefaultMessages[this.statusCode] })); | ||
} | ||
@@ -23,0 +23,0 @@ } |
@@ -71,3 +71,6 @@ const fs = require("fs"); | ||
switch (req.method) { | ||
case "GET": // get folder list, file content | ||
case "GET": { // get folder list, file content | ||
if (!stats && !fs.existsSync(filePath)) { | ||
throw new HTTPResponseError(404); | ||
} | ||
if (stats.isFile()) { | ||
@@ -84,3 +87,5 @@ log("> Reading file: " + filePath); | ||
.filter(f => options.hidden || f[0] !== ".") | ||
.map(f => { | ||
.map( | ||
/** @returns {FscsFileDescriptor} */ | ||
f => { | ||
const fp = path.join(filePath, f); | ||
@@ -99,3 +104,3 @@ const fileStats = fs.statSync(fp); | ||
res.setHeader("Content-Type", "application/json"); | ||
res.write(JSON.stringify(files)); | ||
res.write(JSON.stringify({result: files}, null, 2)); | ||
res.end(); | ||
@@ -110,4 +115,4 @@ } else { | ||
: `<tr><td>📁</td><td><a href="${ | ||
relativePathname.split("/").slice(0, -2).join("/") || "/" | ||
}">..</a></td></tr>`; | ||
relativePathname.split("/").slice(0, -2).join("/") || "/" | ||
}">..</a></td></tr>`; | ||
for (let f = 0; f < files.length; f++) { | ||
@@ -130,9 +135,9 @@ let file = files[f]; | ||
break; | ||
case "POST": // create dir | ||
} | ||
case "POST": { // create dir | ||
log("> Creating path: " + filePath); | ||
fs.mkdirSync(filePath, 0o777); | ||
fs.mkdirSync(filePath, {recursive: true}); | ||
break; | ||
case "PUT": // create / update file | ||
} | ||
case "PUT": { // create / update file | ||
log("> Writing file: " + filePath); | ||
@@ -149,9 +154,13 @@ const stream = fs.createWriteStream(filePath); | ||
return; | ||
case "PATCH": // rename / move | ||
if (!url.searchParams.to) { | ||
} | ||
case "PATCH": {// rename / move | ||
if (!url.searchParams.has("to")) { | ||
throw new HTTPResponseError(400, "'to' query parameter is required."); | ||
} | ||
log(`> Renaming: ${filePath} (to ${url.searchParams.to})`); | ||
const newPath = path.join(options.path, url.searchParams.to); | ||
const to = url.searchParams.get("to"); | ||
if (!stats && !fs.existsSync(filePath)) { | ||
throw new HTTPResponseError(404); | ||
} | ||
log(`> Renaming: ${filePath} (to ${to})`); | ||
const newPath = path.join(options.path, to); | ||
if (!newPath.startsWith(options.path)) { | ||
@@ -162,4 +171,4 @@ throw new HTTPResponseError(403, "Path traversal is not allowed"); | ||
break; | ||
case "DELETE": // delete file / dir | ||
} | ||
case "DELETE": { // delete file / dir | ||
if (!stats && !fs.existsSync(filePath)) { | ||
@@ -172,7 +181,8 @@ throw new HTTPResponseError(404); | ||
} else { | ||
log("> Deleting directory: " + filePath); | ||
fs.rmdirSync(filePath); | ||
const isRecursive = url.searchParams.has("recursive"); | ||
log("> Deleting directory: " + filePath + (isRecursive ? " (recursive)" : "")); | ||
fs.rmdirSync(filePath, isRecursive ? {recursive: true} : null); | ||
} | ||
break; | ||
} | ||
default: | ||
@@ -179,0 +189,0 @@ throw new HTTPResponseError(405); |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
25854
28
562
1
121
1