@dawans/promptshield
Version:
Secure your LLM stack with enterprise-grade RulePacks for AI safety scanning
152 lines (151 loc) • 4.9 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalFileReader = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const glob_1 = require("glob");
const Result_1 = require("../../../shared/types/Result");
/**
* Local file system implementation of FileReader
*/
class LocalFileReader {
/**
* Reads a file and returns its content
*/
async readFile(filePath) {
try {
const content = await fs.promises.readFile(filePath, 'utf-8');
return (0, Result_1.ok)(content);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to read file ${filePath}: ${error}`));
}
}
/**
* Checks if a file exists
*/
async exists(filePath) {
try {
await fs.promises.access(filePath);
return true;
}
catch {
return false;
}
}
/**
* Checks if a path is a directory
*/
async isDirectory(filePath) {
try {
const stats = await fs.promises.stat(filePath);
return stats.isDirectory();
}
catch {
return false;
}
}
/**
* Lists files in a directory
*/
async listFiles(dirPath, pattern) {
try {
if (!(await this.isDirectory(dirPath))) {
return (0, Result_1.err)(new Error(`${dirPath} is not a directory`));
}
const searchPattern = pattern || '**/*.{json,ndjson,jsonl,txt}';
const files = await (0, glob_1.glob)(searchPattern, {
cwd: dirPath,
absolute: true,
nodir: true,
});
return (0, Result_1.ok)(files);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to list files in ${dirPath}: ${error}`));
}
}
/**
* Gets file size in bytes
*/
async getFileSize(filePath) {
try {
const stats = await fs.promises.stat(filePath);
return (0, Result_1.ok)(stats.size);
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to get file size for ${filePath}: ${error}`));
}
}
/**
* Gets file metadata
*/
async getFileMetadata(filePath) {
try {
const stats = await fs.promises.stat(filePath);
const extension = path.extname(filePath).toLowerCase();
let type = 'unknown';
if (extension === '.json')
type = 'json';
else if (extension === '.ndjson' || extension === '.jsonl')
type = 'ndjson';
else if (extension === '.txt')
type = 'txt';
return (0, Result_1.ok)({
path: filePath,
size: stats.size,
extension,
type,
});
}
catch (error) {
return (0, Result_1.err)(new Error(`Failed to get metadata for ${filePath}: ${error}`));
}
}
/**
* Determines if a file should be treated as NDJSON
*/
isNdjsonFile(filePath, ndjsonFlag) {
return (ndjsonFlag || filePath.endsWith('.ndjson') || filePath.endsWith('.jsonl'));
}
/**
* Creates a read stream for a file
*/
createReadStream(filePath) {
return fs.createReadStream(filePath, { encoding: 'utf-8' });
}
}
exports.LocalFileReader = LocalFileReader;