-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { existsSync, promises as fsp, Stats } from "fs"; | ||
import { resolve, join } from "path"; | ||
import { createError, createRequiredError, defineDriver } from "./utils"; | ||
import { | ||
readFile, | ||
writeFile, | ||
readdirRecursive, | ||
rmRecursive, | ||
unlink, | ||
} from "./utils/node-fs"; | ||
import anymatch from "anymatch"; | ||
|
||
export interface FSStorageOptions { | ||
base?: string; | ||
ignore?: (path: string) => boolean; | ||
readOnly?: boolean; | ||
noClear?: boolean; | ||
} | ||
|
||
const PATH_TRAVERSE_RE = /\.\.\:|\.\.$/; | ||
|
||
const DRIVER_NAME = "fs-lite"; | ||
|
||
export default defineDriver((opts: FSStorageOptions = {}) => { | ||
if (!opts.base) { | ||
throw createRequiredError(DRIVER_NAME, "base"); | ||
} | ||
|
||
opts.base = resolve(opts.base); | ||
const r = (key: string) => { | ||
if (PATH_TRAVERSE_RE.test(key)) { | ||
throw createError( | ||
DRIVER_NAME, | ||
`Invalid key: ${JSON.stringify(key)}. It should not contain .. segments` | ||
); | ||
} | ||
const resolved = join(opts.base!, key.replace(/:/g, "/")); | ||
return resolved; | ||
}; | ||
|
||
return { | ||
name: DRIVER_NAME, | ||
options: opts, | ||
hasItem(key) { | ||
return existsSync(r(key)); | ||
}, | ||
getItem(key) { | ||
return readFile(r(key), "utf8"); | ||
}, | ||
getItemRaw(key) { | ||
return readFile(r(key)); | ||
}, | ||
async getMeta(key) { | ||
const { atime, mtime, size, birthtime, ctime } = await fsp | ||
.stat(r(key)) | ||
.catch(() => ({}) as Stats); | ||
return { atime, mtime, size, birthtime, ctime }; | ||
}, | ||
setItem(key, value) { | ||
if (opts.readOnly) { | ||
return; | ||
} | ||
return writeFile(r(key), value, "utf8"); | ||
}, | ||
setItemRaw(key, value) { | ||
if (opts.readOnly) { | ||
return; | ||
} | ||
return writeFile(r(key), value); | ||
}, | ||
removeItem(key) { | ||
if (opts.readOnly) { | ||
return; | ||
} | ||
return unlink(r(key)); | ||
}, | ||
getKeys() { | ||
return readdirRecursive(r("."), opts.ignore); | ||
}, | ||
async clear() { | ||
if (opts.readOnly || opts.noClear) { | ||
return; | ||
} | ||
await rmRecursive(r(".")); | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { resolve } from "path"; | ||
import { readFile } from "../../src/drivers/utils/node-fs"; | ||
import { testDriver } from "./utils"; | ||
import driver from "../../src/drivers/fs-lite"; | ||
|
||
describe("drivers: fs-lite", () => { | ||
const dir = resolve(__dirname, "tmp/fs-lite"); | ||
|
||
testDriver({ | ||
driver: driver({ base: dir }), | ||
additionalTests(ctx) { | ||
it("check filesystem", async () => { | ||
expect(await readFile(resolve(dir, "s1/a"), "utf8")).toBe("test_data"); | ||
}); | ||
it("native meta", async () => { | ||
const meta = await ctx.storage.getMeta("/s1/a"); | ||
expect(meta.atime?.constructor.name).toBe("Date"); | ||
expect(meta.mtime?.constructor.name).toBe("Date"); | ||
expect(meta.size).toBeGreaterThan(0); | ||
}); | ||
|
||
const invalidKeys = ["../foobar", "..:foobar", "../", "..:", ".."]; | ||
for (const key of invalidKeys) { | ||
it("disallow path travesal: ", async () => { | ||
await expect(ctx.storage.getItem(key)).rejects.toThrow("Invalid key"); | ||
}); | ||
} | ||
|
||
it("allow double dots in filename: ", async () => { | ||
await ctx.storage.setItem("s1/te..st..js", "ok"); | ||
expect(await ctx.storage.getItem("s1/te..st..js")).toBe("ok"); | ||
}); | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters