filesystem-sandbox
Advanced tools
Comparing version 1.16.0 to 1.17.0
@@ -19,8 +19,9 @@ /// <reference types="node" /> | ||
mkdir(name: string): Promise<string>; | ||
private resolveRelativePath; | ||
readTextFile(at: string): Promise<string>; | ||
readFile(at: string): Promise<Buffer>; | ||
fullPathFor(relativePath: string, ...parts: string[]): string; | ||
stat(relativePath: string): Promise<StatsBase<any> | null>; | ||
folderExists(relativePath: string): Promise<boolean>; | ||
fileExists(relativePath: string): Promise<boolean>; | ||
stat(at: string): Promise<StatsBase<any> | null>; | ||
folderExists(at: string): Promise<boolean>; | ||
fileExists(at: string): Promise<boolean>; | ||
private runOnStat; | ||
@@ -27,0 +28,0 @@ run<T>(fn: Func<T> | AsyncFunc<T>, relativePath?: string): Promise<T>; |
@@ -109,2 +109,4 @@ "use strict"; | ||
async writeFile(at, contents) { | ||
const providedAt = at; | ||
at = this.resolveRelativePath(at); | ||
const fullPath = this.fullPathFor(at), options = contents instanceof Buffer | ||
@@ -117,6 +119,12 @@ ? undefined | ||
await this.mkdir(path_1.default.dirname(at)); | ||
await writeFile(fullPath, contents, options); | ||
try { | ||
await writeFile(fullPath, contents, options); | ||
} | ||
catch (e) { | ||
throw new Error(`Unable to write file at: ${providedAt}: ${e.message || e}`); | ||
} | ||
return fullPath; | ||
} | ||
async mkdir(name) { | ||
name = this.resolveRelativePath(name); | ||
return new Promise(async (resolve, reject) => { | ||
@@ -136,2 +144,12 @@ const fullpath = path_1.default.join(this._path, name); | ||
} | ||
resolveRelativePath(at) { | ||
if (!path_1.default.isAbsolute(at)) { | ||
return at; | ||
} | ||
const result = path_1.default.relative(this._path, at); | ||
if (result.startsWith("..")) { | ||
throw new Error(`${at} is outside the sandbox at ${this._path}`); | ||
} | ||
return result; | ||
} | ||
async readTextFile(at) { | ||
@@ -144,7 +162,9 @@ return readFile(this.fullPathFor(at), { encoding: "utf8" }); | ||
fullPathFor(relativePath, ...parts) { | ||
relativePath = this.resolveRelativePath(relativePath); | ||
return path_1.default.join(this._path, relativePath, ...parts); | ||
} | ||
async stat(relativePath) { | ||
async stat(at) { | ||
at = this.resolveRelativePath(at); | ||
try { | ||
return await fs_1.promises.stat(this.fullPathFor(relativePath)); | ||
return await fs_1.promises.stat(this.fullPathFor(at)); | ||
} | ||
@@ -155,7 +175,9 @@ catch (e) { | ||
} | ||
async folderExists(relativePath) { | ||
return this.runOnStat(relativePath, st => !!st && st.isDirectory()); | ||
async folderExists(at) { | ||
at = this.resolveRelativePath(at); | ||
return this.runOnStat(at, st => !!st && st.isDirectory()); | ||
} | ||
async fileExists(relativePath) { | ||
return this.runOnStat(relativePath, st => !!st && st.isFile()); | ||
async fileExists(at) { | ||
at = this.resolveRelativePath(at); | ||
return this.runOnStat(at, st => !!st && st.isFile()); | ||
} | ||
@@ -162,0 +184,0 @@ async runOnStat(relativePath, fn) { |
{ | ||
"name": "filesystem-sandbox", | ||
"version": "1.16.0", | ||
"version": "1.17.0", | ||
"description": "JavaScript module to provide filesystem sandboxes for testing", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
15190
280