Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Treat an empty STORAGE_LOCAL_DIR as not set

Matthew Jacksonea0488fbff79
diff --git a/src/lib/services/storage/blob.test.ts b/src/lib/services/storage/blob.test.ts
index b643107..ff7c6d1 100644
--- a/src/lib/services/storage/blob.test.ts
+++ b/src/lib/services/storage/blob.test.ts
@@ -335,3 +335,30 @@ test("the storage driver is chosen from what is connected", () => {
assert.equal(supportsDirectUploads(connectedEnv), true);
assert.equal(supportsDirectUploads({}), false);
});
+
+test("an empty STORAGE_LOCAL_DIR keeps files in the default folder", async (t) => {
+ const workDir = await mkdtemp(path.join(tmpdir(), "signudoc-storage-"));
+ const previousDir = process.cwd();
+
+ t.mock.method(console, "warn", () => {});
+ process.chdir(workDir);
+
+ try {
+ const storage = createLocalStorageService({
+ env: { STORAGE_LOCAL_DIR: " " },
+ });
+
+ await storage.putObject({
+ body: new Uint8Array([1, 2, 3]),
+ contentType: "application/pdf",
+ key: KEY,
+ });
+
+ // Not in the working directory itself.
+ assert.deepEqual(await readdir(workDir), [".data"]);
+ assert.deepEqual([...(await storage.getObject(KEY)).body], [1, 2, 3]);
+ } finally {
+ process.chdir(previousDir);
+ await rm(workDir, { force: true, recursive: true });
+ }
+});
diff --git a/src/lib/services/storage/local.ts b/src/lib/services/storage/local.ts
index 1ef8182..38e131b 100644
--- a/src/lib/services/storage/local.ts
+++ b/src/lib/services/storage/local.ts
@@ -12,7 +12,7 @@ import {
import path from "node:path";
import { Readable } from "node:stream";
-import { type EnvSource } from "../../utils/env.ts";
+import { type EnvSource, readEnv } from "../../utils/env.ts";
import {
normalizeStorageKey,
StorageObjectNotFoundError,
@@ -27,6 +27,8 @@ const LOCAL_BUCKET_NAME = "local";
const DEFAULT_LOCAL_STORAGE_DIR = ".data/storage";
const METADATA_SUFFIX = ".meta.json";
+let warnedAboutEmptyDirectory = false;
+
type CreateLocalStorageServiceOptions = {
env?: EnvSource;
rootDir?: string;
@@ -90,11 +92,24 @@ export function createLocalStorageService({
);
}
+ // An empty STORAGE_LOCAL_DIR counts as not set. Earlier versions took it
+ // to mean the working directory itself. Said once per process.
+ if (
+ !warnedAboutEmptyDirectory &&
+ rootDir === undefined &&
+ env.STORAGE_LOCAL_DIR?.trim() === ""
+ ) {
+ warnedAboutEmptyDirectory = true;
+ console.warn(
+ `[storage] STORAGE_LOCAL_DIR is empty, so files are kept in ${DEFAULT_LOCAL_STORAGE_DIR}. Earlier versions kept them in the working directory: move them, or set STORAGE_LOCAL_DIR to that folder.`,
+ );
+ }
+
// The comment stops Turbopack from tracing the whole project into the
// server bundle because of a path it cannot analyse.
const resolvedRootDir = path.resolve(
/*turbopackIgnore: true*/ rootDir ??
- env.STORAGE_LOCAL_DIR?.trim() ??
+ readEnv(env, "STORAGE_LOCAL_DIR") ??
DEFAULT_LOCAL_STORAGE_DIR,
);