@restroom-mw/files
Advanced tools
Comparing version 0.13.1-4a7f9b1.35 to 0.13.1-4bbb149.69
export declare const DOWNLOAD = "download the {} and extract it into {}"; | ||
export declare const STORE_RESULT = "store {} in the file {}"; | ||
export declare const READ = "read the content of {}"; | ||
export declare const LS = "list the content of directory {} as {}"; | ||
//# sourceMappingURL=actions.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.READ = exports.STORE_RESULT = exports.DOWNLOAD = void 0; | ||
exports.LS = exports.READ = exports.STORE_RESULT = exports.DOWNLOAD = void 0; | ||
exports.DOWNLOAD = "download the {} and extract it into {}"; | ||
exports.STORE_RESULT = "store {} in the file {}"; | ||
exports.READ = "read the content of {}"; | ||
exports.LS = "list the content of directory {} as {}"; |
@@ -19,2 +19,3 @@ "use strict"; | ||
const fs_1 = __importDefault(require("fs")); | ||
const promises_1 = require("node:fs/promises"); | ||
const os_1 = __importDefault(require("os")); | ||
@@ -51,6 +52,8 @@ const extract_zip_1 = __importDefault(require("extract-zip")); | ||
if (exports.FILES_DIR != "/") { | ||
// Even if path contains .., they are resolved in a absolute path | ||
// https://github.com/nodejs/node/blob/74a6f0bd0763a1aa1a241da55322c2556834036b/lib/path.js#L507-L508 | ||
const relative = path_1.default.relative(exports.FILES_DIR, p); | ||
const isSubdir = relative && !relative.startsWith('..') && !path_1.default.isAbsolute(relative); | ||
const isSubdir = !relative.startsWith('..') && !path_1.default.isAbsolute(relative); | ||
if (!isSubdir) { | ||
throw new Error(`Result path outside ${exports.FILES_DIR}`); | ||
throw new Error(`Result path ${relative} outside ${exports.FILES_DIR}`); | ||
} | ||
@@ -63,5 +66,7 @@ } | ||
const { zencode, keys, data } = params; | ||
input = rr.combineDataKeys(data, keys); | ||
if (zencode.match(actions_3.READ)) { | ||
const params = zencode.paramsOf(actions_3.READ); | ||
for (const file of params) { | ||
for (const f of params) { | ||
const file = input[f] || f; | ||
validatePath(file); | ||
@@ -78,2 +83,45 @@ const absoluteFile = path_1.default.join(exports.FILES_DIR, file); | ||
} | ||
if (zencode.match(actions_3.LS)) { | ||
const params = zencode.chunkedParamsOf(actions_3.LS, 2); | ||
const fileStats = {}; | ||
const allLs = (yield Promise.all(params.map(([p, name]) => __awaiter(void 0, void 0, void 0, function* () { | ||
const f = input[p] || p; | ||
validatePath(f); | ||
try { | ||
const content = yield (0, promises_1.readdir)(f); | ||
// I am not checking if `name` is used multiple times | ||
fileStats[name] = []; | ||
return content.map((current) => [name, f, current]); | ||
} | ||
catch (e) { | ||
throw new Error(`[FILES] error while reading the file ${f}`); | ||
} | ||
})))).flat(); | ||
// list with all files I want to see the stats of | ||
const allStats = yield Promise.all(allLs.map(([name, p, current]) => __awaiter(void 0, void 0, void 0, function* () { | ||
const currentFile = path_1.default.join(p, current); | ||
const fileStat = yield (0, promises_1.stat)(currentFile); | ||
return [name, current, fileStat]; | ||
}))); | ||
for (const [name, current, currentStat] of allStats) { | ||
// see https://unix.stackexchange.com/questions/317855/file-mode-on-macosx#317907 | ||
// for the meaning of the mode field | ||
fileStats[name].push({ | ||
'name': current, | ||
'mode': currentStat.mode.toString(8), | ||
'dev': currentStat.dev, | ||
'nlink': currentStat.nlink, | ||
'uid': currentStat.uid, | ||
'gid': currentStat.gid, | ||
'size': currentStat.size, | ||
'blksize': currentStat.blksize, | ||
'blocks': currentStat.blocks, | ||
'atime': currentStat.atime.toISOString(), | ||
'mtime': currentStat.mtime.toISOString(), | ||
'ctime': currentStat.ctime.toISOString(), | ||
'birthtime': currentStat.birthtime.toISOString(), | ||
}); | ||
} | ||
Object.assign(data, fileStats); | ||
} | ||
input = rr.combineDataKeys(data, keys); | ||
@@ -86,4 +134,6 @@ })); | ||
for (let i = 0; i < allPassOutputs.length; i += 2) { | ||
const file = result[allPassOutputs[i]]; | ||
const folder = input[allPassOutputs[i + 1]]; | ||
const file = result[allPassOutputs[i]] || | ||
input[allPassOutputs[i]]; | ||
const folder = result[allPassOutputs[i + 1]] || | ||
input[allPassOutputs[i + 1]]; | ||
if (!file) { | ||
@@ -117,4 +167,6 @@ throw new Error(`[FILES] url not defined`); | ||
for (let i = 0; i < allPassOutputs.length; i += 2) { | ||
const variable = result[allPassOutputs[i]]; | ||
const file = input[allPassOutputs[i + 1]]; | ||
const variable = result[allPassOutputs[i]] || | ||
input[allPassOutputs[i]]; | ||
const file = result[allPassOutputs[i + 1]] || | ||
input[allPassOutputs[i + 1]]; | ||
if (!variable) { | ||
@@ -121,0 +173,0 @@ throw new Error(`[FILES] variable not defined`); |
{ | ||
"name": "@restroom-mw/files", | ||
"version": "0.13.1-4a7f9b1.35+4a7f9b1", | ||
"version": "0.13.1-4bbb149.69+4bbb149", | ||
"description": "Utilities middleware to work with files for Restroom", | ||
@@ -35,3 +35,3 @@ "author": "Alberto Lerda <alberto@dyne.org>", | ||
}, | ||
"gitHead": "4a7f9b1520490c30314849fbe2e533b8d81899f2" | ||
"gitHead": "4bbb1496d6c8cd6d9bc5750cdf04b84122b403bd" | ||
} |
export const DOWNLOAD = "download the {} and extract it into {}"; | ||
export const STORE_RESULT = "store {} in the file {}"; | ||
export const READ = "read the content of {}"; | ||
export const LS = "list the content of directory {} as {}"; |
@@ -5,2 +5,3 @@ import {Restroom} from "@restroom-mw/core"; | ||
import fs from 'fs' | ||
import { readdir, stat } from 'node:fs/promises'; | ||
import os from 'os' | ||
@@ -25,6 +26,5 @@ import extract from 'extract-zip'; | ||
*/ | ||
import {STORE_RESULT} from "./actions"; | ||
import {READ} from "./actions"; | ||
import {READ, LS} from "./actions"; | ||
@@ -47,6 +47,8 @@ | ||
if (FILES_DIR != "/") { | ||
// Even if path contains .., they are resolved in a absolute path | ||
// https://github.com/nodejs/node/blob/74a6f0bd0763a1aa1a241da55322c2556834036b/lib/path.js#L507-L508 | ||
const relative = path.relative(FILES_DIR, p); | ||
const isSubdir = relative && !relative.startsWith('..') && !path.isAbsolute(relative); | ||
const isSubdir = !relative.startsWith('..') && !path.isAbsolute(relative); | ||
if (!isSubdir) { | ||
throw new Error(`Result path outside ${FILES_DIR}`) | ||
throw new Error(`Result path ${relative} outside ${FILES_DIR}`) | ||
} | ||
@@ -59,9 +61,10 @@ } | ||
rr.onBefore(async (params) => { | ||
const { zencode, keys, data } = params; | ||
input = rr.combineDataKeys(data, keys); | ||
if (zencode.match(READ)) { | ||
const params = zencode.paramsOf(READ); | ||
for(const file of params) { | ||
for(const f of params) { | ||
const file = input[f] || f; | ||
validatePath(file); | ||
@@ -77,2 +80,46 @@ const absoluteFile = path.join(FILES_DIR, file) | ||
} | ||
if (zencode.match(LS)) { | ||
const params = zencode.chunkedParamsOf(LS, 2); | ||
const fileStats: Record<string, any> = {} | ||
const allLs = (await Promise.all(params.map( | ||
async ([p, name]: string[]) => { | ||
const f = input[p] || p; | ||
validatePath(f); | ||
try { | ||
const content = await readdir(f) | ||
// I am not checking if `name` is used multiple times | ||
fileStats[name] = [] | ||
return content.map((current) => [name, f, current]); | ||
} catch(e) { | ||
throw new Error(`[FILES] error while reading the file ${f}`); | ||
} | ||
}))).flat() | ||
// list with all files I want to see the stats of | ||
const allStats = await Promise.all(allLs.map(async ([name, p, current]) => { | ||
const currentFile = path.join(p, current) | ||
const fileStat = await stat(currentFile) | ||
return [name, current, fileStat] | ||
})) | ||
for(const [name, current, currentStat] of allStats) { | ||
// see https://unix.stackexchange.com/questions/317855/file-mode-on-macosx#317907 | ||
// for the meaning of the mode field | ||
fileStats[name].push({ | ||
'name': current, | ||
'mode': currentStat.mode.toString(8), | ||
'dev': currentStat.dev, | ||
'nlink': currentStat.nlink, | ||
'uid': currentStat.uid, | ||
'gid': currentStat.gid, | ||
'size': currentStat.size, | ||
'blksize': currentStat.blksize, | ||
'blocks': currentStat.blocks, | ||
'atime': currentStat.atime.toISOString(), | ||
'mtime': currentStat.mtime.toISOString(), | ||
'ctime': currentStat.ctime.toISOString(), | ||
'birthtime': currentStat.birthtime.toISOString(), | ||
}) | ||
} | ||
Object.assign(data, fileStats) | ||
} | ||
input = rr.combineDataKeys(data, keys); | ||
@@ -86,4 +133,6 @@ }); | ||
for (let i = 0; i < allPassOutputs.length; i += 2) { | ||
const file = result[allPassOutputs[i]] | ||
const folder = input[allPassOutputs[i + 1]]; | ||
const file = result[allPassOutputs[i]] || | ||
input[allPassOutputs[i]]; | ||
const folder = result[allPassOutputs[i + 1]] || | ||
input[allPassOutputs[i + 1]]; | ||
@@ -119,4 +168,6 @@ if(!file) { | ||
for (let i = 0; i < allPassOutputs.length; i += 2) { | ||
const variable = result[allPassOutputs[i]] | ||
const file = input[allPassOutputs[i + 1]] | ||
const variable = result[allPassOutputs[i]] || | ||
input[allPassOutputs[i]]; | ||
const file = result[allPassOutputs[i + 1]] || | ||
input[allPassOutputs[i + 1]]; | ||
@@ -123,0 +174,0 @@ if(!variable) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
52669
384