@softure-ai/files 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +213 -2
- package/dist/contract.d.ts +51 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +4 -0
- package/dist/contract.js.map +1 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/jobs/index.d.ts +12 -0
- package/dist/jobs/index.d.ts.map +1 -0
- package/dist/jobs/index.js +22 -0
- package/dist/jobs/index.js.map +1 -0
- package/dist/messages/en.d.ts +17 -0
- package/dist/messages/en.d.ts.map +1 -0
- package/dist/messages/en.js +17 -0
- package/dist/messages/en.js.map +1 -0
- package/dist/messages/index.d.ts +24 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +10 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/next/context.d.ts +4 -0
- package/dist/next/context.d.ts.map +1 -0
- package/dist/next/context.js +13 -0
- package/dist/next/context.js.map +1 -0
- package/dist/next/index.d.ts +3 -0
- package/dist/next/index.d.ts.map +1 -0
- package/dist/next/index.js +5 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/route.d.ts +3 -0
- package/dist/next/route.d.ts.map +1 -0
- package/dist/next/route.js +11 -0
- package/dist/next/route.js.map +1 -0
- package/dist/options.d.ts +26 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +59 -0
- package/dist/options.js.map +1 -0
- package/dist/schema.d.ts +214 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +19 -0
- package/dist/schema.js.map +1 -0
- package/dist/server/context.d.ts +13 -0
- package/dist/server/context.d.ts.map +1 -0
- package/dist/server/context.js +19 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/files.d.ts +48 -0
- package/dist/server/files.d.ts.map +1 -0
- package/dist/server/files.js +104 -0
- package/dist/server/files.js.map +1 -0
- package/dist/server/health.d.ts +4 -0
- package/dist/server/health.d.ts.map +1 -0
- package/dist/server/health.js +20 -0
- package/dist/server/health.js.map +1 -0
- package/dist/server/index.d.ts +15 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +17 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/keys.d.ts +14 -0
- package/dist/server/keys.d.ts.map +1 -0
- package/dist/server/keys.js +47 -0
- package/dist/server/keys.js.map +1 -0
- package/dist/server/links.d.ts +30 -0
- package/dist/server/links.d.ts.map +1 -0
- package/dist/server/links.js +122 -0
- package/dist/server/links.js.map +1 -0
- package/dist/server/local-storage.d.ts +7 -0
- package/dist/server/local-storage.d.ts.map +1 -0
- package/dist/server/local-storage.js +83 -0
- package/dist/server/local-storage.js.map +1 -0
- package/dist/server/names.d.ts +11 -0
- package/dist/server/names.d.ts.map +1 -0
- package/dist/server/names.js +32 -0
- package/dist/server/names.js.map +1 -0
- package/dist/server/options.d.ts +8 -0
- package/dist/server/options.d.ts.map +1 -0
- package/dist/server/options.js +31 -0
- package/dist/server/options.js.map +1 -0
- package/dist/server/privacy.d.ts +16 -0
- package/dist/server/privacy.d.ts.map +1 -0
- package/dist/server/privacy.js +39 -0
- package/dist/server/privacy.js.map +1 -0
- package/dist/server/prune.d.ts +13 -0
- package/dist/server/prune.d.ts.map +1 -0
- package/dist/server/prune.js +34 -0
- package/dist/server/prune.js.map +1 -0
- package/dist/server/queries.d.ts +7 -0
- package/dist/server/queries.d.ts.map +1 -0
- package/dist/server/queries.js +17 -0
- package/dist/server/queries.js.map +1 -0
- package/dist/server/s3-storage.d.ts +33 -0
- package/dist/server/s3-storage.d.ts.map +1 -0
- package/dist/server/s3-storage.js +223 -0
- package/dist/server/s3-storage.js.map +1 -0
- package/dist/server/save.d.ts +29 -0
- package/dist/server/save.d.ts.map +1 -0
- package/dist/server/save.js +141 -0
- package/dist/server/save.js.map +1 -0
- package/dist/server/serve.d.ts +11 -0
- package/dist/server/serve.d.ts.map +1 -0
- package/dist/server/serve.js +108 -0
- package/dist/server/serve.js.map +1 -0
- package/dist/server/sigv4.d.ts +23 -0
- package/dist/server/sigv4.d.ts.map +1 -0
- package/dist/server/sigv4.js +57 -0
- package/dist/server/sigv4.js.map +1 -0
- package/dist/storage.d.ts +32 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +10 -0
- package/dist/storage.js.map +1 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/links.d.ts +22 -0
- package/dist/testing/links.d.ts.map +1 -0
- package/dist/testing/links.js +30 -0
- package/dist/testing/links.js.map +1 -0
- package/dist/testing/memory-storage.d.ts +11 -0
- package/dist/testing/memory-storage.d.ts.map +1 -0
- package/dist/testing/memory-storage.js +52 -0
- package/dist/testing/memory-storage.js.map +1 -0
- package/migrations/0001_create_files.sql +32 -0
- package/module.json +24 -0
- package/package.json +72 -4
- package/src/contract.ts +59 -0
- package/src/index.ts +53 -0
- package/src/jobs/index.ts +23 -0
- package/src/messages/en.ts +16 -0
- package/src/messages/index.ts +13 -0
- package/src/next/context.ts +14 -0
- package/src/next/index.ts +4 -0
- package/src/next/route.ts +11 -0
- package/src/options.ts +64 -0
- package/src/schema.ts +22 -0
- package/src/server/context.ts +32 -0
- package/src/server/files.ts +133 -0
- package/src/server/health.ts +20 -0
- package/src/server/index.ts +45 -0
- package/src/server/keys.ts +56 -0
- package/src/server/links.ts +152 -0
- package/src/server/local-storage.ts +87 -0
- package/src/server/names.ts +35 -0
- package/src/server/options.ts +34 -0
- package/src/server/privacy.ts +53 -0
- package/src/server/prune.ts +46 -0
- package/src/server/queries.ts +19 -0
- package/src/server/s3-storage.ts +255 -0
- package/src/server/save.ts +167 -0
- package/src/server/serve.ts +106 -0
- package/src/server/sigv4.ts +82 -0
- package/src/storage.ts +43 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/links.ts +47 -0
- package/src/testing/memory-storage.ts +64 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Server-only API of @softure-ai/files. Every function receives the module context (`{ db, clock, config }`, plus
|
|
2
|
+
// an optional `env` for the link keyring) and never reads request scope.
|
|
3
|
+
export { getStorageKey } from "./context.js";
|
|
4
|
+
export { clipRange, deleteFile, findFile, getOwnerUsage, listFiles, openFile, } from "./files.js";
|
|
5
|
+
export { checkFilesHealth } from "./health.js";
|
|
6
|
+
export { FILES_LINK_KEYS_ENV, generateFilesLinkKey, readLinkKeyring } from "./keys.js";
|
|
7
|
+
export { createFileLink, revokeFileLinks, verifyFileLink, } from "./links.js";
|
|
8
|
+
export { createLocalStorage } from "./local-storage.js";
|
|
9
|
+
export { DEFAULT_CONTENT_TYPE, formatContentDisposition, normalizeContentType, sanitizeFileName } from "./names.js";
|
|
10
|
+
export { getFilesModule, getFilesOptions, getLinksRoute } from "./options.js";
|
|
11
|
+
export { deleteFilesUserData, exportFilesUserData, filesPrivacyContributor } from "./privacy.js";
|
|
12
|
+
export { pruneFiles } from "./prune.js";
|
|
13
|
+
export { createS3Storage, S3_ACCESS_KEY_ID_ENV, S3_SECRET_ACCESS_KEY_ENV, S3StorageError, splitIntoParts, } from "./s3-storage.js";
|
|
14
|
+
export { saveFile } from "./save.js";
|
|
15
|
+
export { parseRangeHeader, serveFileLink } from "./serve.js";
|
|
16
|
+
export { EMPTY_PAYLOAD_SHA256, encodeRfc3986, signRequest } from "./sigv4.js";
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,yEAAyE;AACzE,OAAO,EAAE,aAAa,EAA+B,MAAM,cAAc,CAAC;AAC1E,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,aAAa,EACb,SAAS,EACT,QAAQ,GAOT,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAA6B,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,eAAe,EAA4C,MAAM,WAAW,CAAC;AACjI,OAAO,EACL,cAAc,EACd,eAAe,EACf,cAAc,GAMf,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAA4B,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACpH,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,uBAAuB,EAAgD,MAAM,cAAc,CAAC;AAC/I,OAAO,EAAE,UAAU,EAAiD,MAAM,YAAY,CAAC;AACvF,OAAO,EACL,eAAe,EACf,oBAAoB,EACpB,wBAAwB,EACxB,cAAc,EACd,cAAc,GAEf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,QAAQ,EAA2C,MAAM,WAAW,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,WAAW,EAAgD,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Err, type Ok } from "@softure-ai/core";
|
|
2
|
+
import type { Env } from "./context.js";
|
|
3
|
+
export declare const FILES_LINK_KEYS_ENV = "SOFTURE_FILES_LINK_KEYS";
|
|
4
|
+
/** The keys, the signing one first. */
|
|
5
|
+
export interface LinkKeyring {
|
|
6
|
+
readonly current: Buffer;
|
|
7
|
+
readonly keys: readonly Buffer[];
|
|
8
|
+
}
|
|
9
|
+
export type LinkKeyringResult = Ok<LinkKeyring> | Err<"files.link_key_missing" | "files.link_key_invalid">;
|
|
10
|
+
/** Parses the keyring from the environment. Never echoes the value it read. */
|
|
11
|
+
export declare function readLinkKeyring(env?: Env): LinkKeyringResult;
|
|
12
|
+
/** A new keyring entry, `<id>:<base64url of 32 random bytes>`, for `SOFTURE_FILES_LINK_KEYS`. */
|
|
13
|
+
export declare function generateFilesLinkKey(id?: string): string;
|
|
14
|
+
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../src/server/keys.ts"],"names":[],"mappings":"AAKA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAExC,eAAO,MAAM,mBAAmB,4BAA4B,CAAC;AAK7D,uCAAuC;AACvC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;CAClC;AAED,MAAM,MAAM,iBAAiB,GAAG,EAAE,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,wBAAwB,GAAG,wBAAwB,CAAC,CAAC;AAE3G,+EAA+E;AAC/E,wBAAgB,eAAe,CAAC,GAAG,GAAE,GAAiB,GAAG,iBAAiB,CAczE;AAaD,iGAAiG;AACjG,wBAAgB,oBAAoB,CAAC,EAAE,GAAE,MAAkE,GAAG,MAAM,CAKnH"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// The link signing keyring from `SOFTURE_FILES_LINK_KEYS` (`<id>:<base64 of 32 bytes>[,<id>:<key>…]`), the same
|
|
2
|
+
// format as `@softure-ai/connections`: the first key signs, every key verifies. A key is rotated by putting a new
|
|
3
|
+
// one first and dropping the old one after the longest link lifetime. Failures are codes; a key never reaches an
|
|
4
|
+
// error or a log.
|
|
5
|
+
import { randomBytes } from "node:crypto";
|
|
6
|
+
import { err, ok } from "@softure-ai/core";
|
|
7
|
+
export const FILES_LINK_KEYS_ENV = "SOFTURE_FILES_LINK_KEYS";
|
|
8
|
+
const KEY_ID_PATTERN = /^[A-Za-z0-9_-]{1,32}$/;
|
|
9
|
+
const KEY_BYTES = 32;
|
|
10
|
+
/** Parses the keyring from the environment. Never echoes the value it read. */
|
|
11
|
+
export function readLinkKeyring(env = process.env) {
|
|
12
|
+
const raw = env[FILES_LINK_KEYS_ENV]?.trim();
|
|
13
|
+
if (raw === undefined || raw === "")
|
|
14
|
+
return err("files.link_key_missing");
|
|
15
|
+
const ids = new Set();
|
|
16
|
+
const keys = [];
|
|
17
|
+
for (const entry of raw.split(",")) {
|
|
18
|
+
const parsed = parseKeyEntry(entry.trim());
|
|
19
|
+
if (parsed === null || ids.has(parsed.id))
|
|
20
|
+
return err("files.link_key_invalid");
|
|
21
|
+
ids.add(parsed.id);
|
|
22
|
+
keys.push(parsed.key);
|
|
23
|
+
}
|
|
24
|
+
const [current] = keys;
|
|
25
|
+
// The split always yields one entry, so a parsed list has a current key.
|
|
26
|
+
return current === undefined ? err("files.link_key_invalid") : ok({ current, keys });
|
|
27
|
+
}
|
|
28
|
+
function parseKeyEntry(entry) {
|
|
29
|
+
const separator = entry.indexOf(":");
|
|
30
|
+
if (separator <= 0)
|
|
31
|
+
return null;
|
|
32
|
+
const id = entry.slice(0, separator);
|
|
33
|
+
const encoded = entry.slice(separator + 1);
|
|
34
|
+
if (!KEY_ID_PATTERN.test(id) || !/^[A-Za-z0-9+/_-]+={0,2}$/.test(encoded))
|
|
35
|
+
return null;
|
|
36
|
+
// Node's base64 decoder takes both alphabets, standard and URL-safe.
|
|
37
|
+
const key = Buffer.from(encoded, "base64");
|
|
38
|
+
return key.length === KEY_BYTES ? { id, key } : null;
|
|
39
|
+
}
|
|
40
|
+
/** A new keyring entry, `<id>:<base64url of 32 random bytes>`, for `SOFTURE_FILES_LINK_KEYS`. */
|
|
41
|
+
export function generateFilesLinkKey(id = new Date().toISOString().slice(0, 10).replaceAll("-", "")) {
|
|
42
|
+
if (!KEY_ID_PATTERN.test(id)) {
|
|
43
|
+
throw new RangeError("generateFilesLinkKey: the id must be 1 to 32 letters, digits, _ or -");
|
|
44
|
+
}
|
|
45
|
+
return `${id}:${randomBytes(KEY_BYTES).toString("base64url")}`;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=keys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../src/server/keys.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,kHAAkH;AAClH,iHAAiH;AACjH,kBAAkB;AAClB,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,EAAE,EAAqB,MAAM,kBAAkB,CAAC;AAG9D,MAAM,CAAC,MAAM,mBAAmB,GAAG,yBAAyB,CAAC;AAE7D,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAC/C,MAAM,SAAS,GAAG,EAAE,CAAC;AAUrB,+EAA+E;AAC/E,MAAM,UAAU,eAAe,CAAC,MAAW,OAAO,CAAC,GAAG;IACpD,MAAM,GAAG,GAAG,GAAG,CAAC,mBAAmB,CAAC,EAAE,IAAI,EAAE,CAAC;IAC7C,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,EAAE;QAAE,OAAO,GAAG,CAAC,wBAAwB,CAAC,CAAC;IAC1E,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,MAAM,IAAI,GAAa,EAAE,CAAC;IAC1B,KAAK,MAAM,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACnC,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3C,IAAI,MAAM,KAAK,IAAI,IAAI,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,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CAAC;IACzG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CAAC,sEAAsE,CAAC,CAAC;IAC/F,CAAC;IACD,OAAO,GAAG,EAAE,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Err, type Ok } from "@softure-ai/core";
|
|
2
|
+
import type { FileLink, FilesKeyErrorCode } from "../contract.js";
|
|
3
|
+
import type { FilesContext } from "./context.js";
|
|
4
|
+
import { type FileLookupOptions } from "./files.js";
|
|
5
|
+
export interface CreateFileLinkInput extends FileLookupOptions {
|
|
6
|
+
/** How long the link works, at most `links.maxTtlSeconds`; a file that expires sooner ends the link with it. */
|
|
7
|
+
readonly ttlSeconds: number;
|
|
8
|
+
/** The name the receiver sees; the file's own name when omitted. Sanitised like a saved name. */
|
|
9
|
+
readonly downloadName?: string;
|
|
10
|
+
}
|
|
11
|
+
export type CreateFileLinkResult = Ok<FileLink> | Err<"files.not_found" | "files.invalid_input" | FilesKeyErrorCode>;
|
|
12
|
+
/** A signed public link to a live file. */
|
|
13
|
+
export declare function createFileLink(ctx: FilesContext, fileId: string, input: CreateFileLinkInput): Promise<CreateFileLinkResult>;
|
|
14
|
+
export type RevokeFileLinksResult = Ok<undefined> | Err<"files.not_found">;
|
|
15
|
+
/** Stops every link made so far for a file; links made afterwards work. */
|
|
16
|
+
export declare function revokeFileLinks(ctx: FilesContext, fileId: string, options?: FileLookupOptions): Promise<RevokeFileLinksResult>;
|
|
17
|
+
/** What a link's path says, once its MAC and expiry are checked. */
|
|
18
|
+
export interface VerifiedLink {
|
|
19
|
+
readonly fileId: string;
|
|
20
|
+
readonly expiresAt: Date;
|
|
21
|
+
readonly generation: number;
|
|
22
|
+
readonly name: string;
|
|
23
|
+
}
|
|
24
|
+
export type VerifyFileLinkResult = Ok<VerifiedLink> | Err<"files.not_found" | FilesKeyErrorCode>;
|
|
25
|
+
/**
|
|
26
|
+
* Checks a link path (the request's pathname) without touching the database: its shape, its MAC under any key of
|
|
27
|
+
* the keyring, and that it has not expired. Every refusal of the link itself is `files.not_found`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function verifyFileLink(ctx: Pick<FilesContext, "clock" | "config" | "env">, pathname: string): VerifyFileLinkResult;
|
|
30
|
+
//# sourceMappingURL=links.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../../src/server/links.ts"],"names":[],"mappings":"AAMA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAE9D,OAAO,KAAK,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAElE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAA6B,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAU/E,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;IAC5D,gHAAgH;IAChH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,iGAAiG;IACjG,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,MAAM,MAAM,oBAAoB,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,iBAAiB,GAAG,qBAAqB,GAAG,iBAAiB,CAAC,CAAC;AAErH,2CAA2C;AAC3C,wBAAsB,cAAc,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA2BjI;AAED,MAAM,MAAM,qBAAqB,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAE3E,2EAA2E;AAC3E,wBAAsB,eAAe,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAUxI;AAED,oEAAoE;AACpE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,MAAM,oBAAoB,GAAG,EAAE,CAAC,YAAY,CAAC,GAAG,GAAG,CAAC,iBAAiB,GAAG,iBAAiB,CAAC,CAAC;AAEjG;;;GAGG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,oBAAoB,CAc1H"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Public links: `<route>/<id>/<expiry>/<generation>.<mac>/<name>`, where `expiry` is a Unix time in seconds,
|
|
2
|
+
// `generation` the file's link generation when the link was made and `mac` an HMAC-SHA256 (base64url) over all four
|
|
3
|
+
// parts under the keyring's first key. The MAC and the expiry are checked before any database read, so a forged or
|
|
4
|
+
// stale link costs no query; the generation is then compared with the row, so `revokeFileLinks` (which bumps it)
|
|
5
|
+
// kills every link made before.
|
|
6
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
7
|
+
import { err, ok } from "@softure-ai/core";
|
|
8
|
+
import { and, eq, isNull, sql } from "drizzle-orm";
|
|
9
|
+
import { files } from "../schema.js";
|
|
10
|
+
import { findLiveRow, UUID_PATTERN } from "./files.js";
|
|
11
|
+
import { readLinkKeyring } from "./keys.js";
|
|
12
|
+
import { sanitizeFileName } from "./names.js";
|
|
13
|
+
import { getFilesOptions, getLinksRoute } from "./options.js";
|
|
14
|
+
const MAC_CONTEXT = "softure-files-link/v1";
|
|
15
|
+
const MAC_PATTERN = /^[A-Za-z0-9_-]{43}$/;
|
|
16
|
+
const EXPIRY_PATTERN = /^[1-9]\d{0,11}$/;
|
|
17
|
+
const GENERATION_PATTERN = /^[1-9]\d{0,9}$/;
|
|
18
|
+
/** A signed public link to a live file. */
|
|
19
|
+
export async function createFileLink(ctx, fileId, input) {
|
|
20
|
+
const options = getFilesOptions(ctx.config);
|
|
21
|
+
if (!Number.isSafeInteger(input.ttlSeconds) || input.ttlSeconds < 1 || input.ttlSeconds > options.links.maxTtlSeconds) {
|
|
22
|
+
return err("files.invalid_input");
|
|
23
|
+
}
|
|
24
|
+
const keyring = readLinkKeyring(ctx.env ?? process.env);
|
|
25
|
+
if (!keyring.ok)
|
|
26
|
+
return keyring;
|
|
27
|
+
const row = await findLiveRow(ctx, fileId, input);
|
|
28
|
+
if (row === undefined)
|
|
29
|
+
return err("files.not_found");
|
|
30
|
+
const nowSeconds = Math.floor(ctx.clock.now().getTime() / 1000);
|
|
31
|
+
const fileEndSeconds = row.expiresAt === null ? Infinity : Math.floor(row.expiresAt.getTime() / 1000);
|
|
32
|
+
const expiry = Math.min(nowSeconds + input.ttlSeconds, fileEndSeconds);
|
|
33
|
+
const name = input.downloadName === undefined ? row.originalName : sanitizeFileName(input.downloadName);
|
|
34
|
+
const generation = String(row.linkGeneration);
|
|
35
|
+
const mac = signLink(keyring.value.current, {
|
|
36
|
+
fileId: row.id,
|
|
37
|
+
expiry: String(expiry),
|
|
38
|
+
generation,
|
|
39
|
+
name,
|
|
40
|
+
});
|
|
41
|
+
const path = `${getLinksRoute(ctx.config)}/${row.id}/${String(expiry)}/${generation}.${mac}/${encodeURIComponent(name)}`;
|
|
42
|
+
return ok({
|
|
43
|
+
url: new URL(path, ctx.config.appOrigin).toString(),
|
|
44
|
+
path,
|
|
45
|
+
expiresAt: new Date(expiry * 1000),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** Stops every link made so far for a file; links made afterwards work. */
|
|
49
|
+
export async function revokeFileLinks(ctx, fileId, options = {}) {
|
|
50
|
+
if (!UUID_PATTERN.test(fileId))
|
|
51
|
+
return err("files.not_found");
|
|
52
|
+
const conditions = [eq(files.id, fileId), isNull(files.deletedAt)];
|
|
53
|
+
if (options.ownerId !== undefined)
|
|
54
|
+
conditions.push(eq(files.ownerId, options.ownerId));
|
|
55
|
+
const updated = await ctx.db
|
|
56
|
+
.update(files)
|
|
57
|
+
.set({ linkGeneration: sql `${files.linkGeneration} + 1` })
|
|
58
|
+
.where(and(...conditions))
|
|
59
|
+
.returning({ id: files.id });
|
|
60
|
+
return updated.length === 0 ? err("files.not_found") : ok();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Checks a link path (the request's pathname) without touching the database: its shape, its MAC under any key of
|
|
64
|
+
* the keyring, and that it has not expired. Every refusal of the link itself is `files.not_found`.
|
|
65
|
+
*/
|
|
66
|
+
export function verifyFileLink(ctx, pathname) {
|
|
67
|
+
const parts = splitLinkPath(getLinksRoute(ctx.config), pathname);
|
|
68
|
+
if (parts === null)
|
|
69
|
+
return err("files.not_found");
|
|
70
|
+
const keyring = readLinkKeyring(ctx.env ?? process.env);
|
|
71
|
+
if (!keyring.ok)
|
|
72
|
+
return keyring;
|
|
73
|
+
if (!hasValidMac(keyring.value, parts))
|
|
74
|
+
return err("files.not_found");
|
|
75
|
+
const expiresAt = new Date(Number(parts.expiry) * 1000);
|
|
76
|
+
if (expiresAt.getTime() <= ctx.clock.now().getTime())
|
|
77
|
+
return err("files.not_found");
|
|
78
|
+
return ok({
|
|
79
|
+
fileId: parts.fileId,
|
|
80
|
+
expiresAt,
|
|
81
|
+
generation: Number(parts.generation),
|
|
82
|
+
name: parts.name,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function splitLinkPath(route, pathname) {
|
|
86
|
+
if (!pathname.startsWith(`${route}/`))
|
|
87
|
+
return null;
|
|
88
|
+
const segments = pathname.slice(route.length + 1).split("/");
|
|
89
|
+
if (segments.length !== 4)
|
|
90
|
+
return null;
|
|
91
|
+
const [fileId = "", expiry = "", signature = "", encodedName = ""] = segments;
|
|
92
|
+
const [generation = "", mac = "", extra] = signature.split(".");
|
|
93
|
+
const isWellFormed = UUID_PATTERN.test(fileId) && EXPIRY_PATTERN.test(expiry) && GENERATION_PATTERN.test(generation) && MAC_PATTERN.test(mac) && extra === undefined;
|
|
94
|
+
if (!isWellFormed || encodedName === "")
|
|
95
|
+
return null;
|
|
96
|
+
try {
|
|
97
|
+
return {
|
|
98
|
+
fileId,
|
|
99
|
+
expiry,
|
|
100
|
+
generation,
|
|
101
|
+
mac,
|
|
102
|
+
name: decodeURIComponent(encodedName),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// A malformed percent escape: not a link this module made.
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function signLink(key, parts) {
|
|
111
|
+
return createHmac("sha256", key).update([MAC_CONTEXT, parts.fileId, parts.expiry, parts.generation, parts.name].join("\n")).digest("base64url");
|
|
112
|
+
}
|
|
113
|
+
function hasValidMac(keyring, parts) {
|
|
114
|
+
// Compared as text: a base64url MAC has spare bits, so decoded bytes would accept a few spellings of one MAC.
|
|
115
|
+
const given = Buffer.from(parts.mac, "ascii");
|
|
116
|
+
// Every key is tried, so the time taken does not tell which one matched.
|
|
117
|
+
return keyring.keys.reduce((isMatch, key) => {
|
|
118
|
+
const expected = Buffer.from(signLink(key, parts), "ascii");
|
|
119
|
+
return timingSafeEqual(expected, given) || isMatch;
|
|
120
|
+
}, false);
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=links.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"links.js","sourceRoot":"","sources":["../../src/server/links.ts"],"names":[],"mappings":"AAAA,6GAA6G;AAC7G,oHAAoH;AACpH,mHAAmH;AACnH,iHAAiH;AACjH,gCAAgC;AACh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OAAO,CAAC,CAAC;QAC5D,OAAO,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,OAAO,CAAC;IACrD,CAAC,EAAE,KAAK,CAAC,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FileStorage } from "../storage.js";
|
|
2
|
+
export interface LocalStorageOptions {
|
|
3
|
+
/** The folder the files live in, absolute or relative to the working directory (e.g. `/data/files`). */
|
|
4
|
+
readonly dir: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function createLocalStorage(options: LocalStorageOptions): FileStorage;
|
|
7
|
+
//# sourceMappingURL=local-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage.d.ts","sourceRoot":"","sources":["../../src/server/local-storage.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAY,WAAW,EAAgB,MAAM,eAAe,CAAC;AAMzE,MAAM,WAAW,mBAAmB;IAClC,wGAAwG;IACxG,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,WAAW,CAuD5E"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// The local disk driver: one folder (a Docker volume on a one-VPS app), objects at `<dir>/<key>`. A write goes to
|
|
2
|
+
// a partial file next to its target and is renamed into place once complete, so a reader never sees half a file and
|
|
3
|
+
// a failed write leaves nothing. Creating the driver touches nothing; the folder is made on the first write.
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
6
|
+
import { mkdir, rename, rm, stat, statfs, writeFile } from "node:fs/promises";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { Readable } from "node:stream";
|
|
9
|
+
import { pipeline } from "node:stream/promises";
|
|
10
|
+
const KEY_PATTERN = /^[a-z0-9][a-z0-9-]*(?:\/[a-z0-9][a-z0-9-]*)*$/;
|
|
11
|
+
const PROBE_DIR = ".health";
|
|
12
|
+
const PARTIAL_SUFFIX = ".partial";
|
|
13
|
+
export function createLocalStorage(options) {
|
|
14
|
+
const root = resolve(options.dir);
|
|
15
|
+
function getPath(key) {
|
|
16
|
+
// Keys come from file ids; anything else is a bug, and must never reach the disk as a path.
|
|
17
|
+
if (!KEY_PATTERN.test(key))
|
|
18
|
+
throw new RangeError("@softure-ai/files: a storage key must be lowercase letters, digits, - and /");
|
|
19
|
+
return join(root, key);
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
kind: "local",
|
|
23
|
+
async put(body, key) {
|
|
24
|
+
const target = getPath(key);
|
|
25
|
+
const partial = `${target}.${randomUUID()}${PARTIAL_SUFFIX}`;
|
|
26
|
+
await mkdir(dirname(target), { recursive: true });
|
|
27
|
+
try {
|
|
28
|
+
await pipeline(Readable.from(body), createWriteStream(partial, { flags: "wx" }));
|
|
29
|
+
await rename(partial, target);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
await rm(partial, { force: true });
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
async get(key, range) {
|
|
37
|
+
const path = getPath(key);
|
|
38
|
+
if ((await statOrNull(path)) === null)
|
|
39
|
+
return null;
|
|
40
|
+
const stream = createReadStream(path, range === undefined ? {} : { start: range.start, end: range.end });
|
|
41
|
+
// Node's web stream type is structurally the DOM one; the cast bridges the two declarations.
|
|
42
|
+
return Readable.toWeb(stream);
|
|
43
|
+
},
|
|
44
|
+
async head(key) {
|
|
45
|
+
const stats = await statOrNull(getPath(key));
|
|
46
|
+
return stats === null ? null : { size: stats.size };
|
|
47
|
+
},
|
|
48
|
+
async delete(key) {
|
|
49
|
+
await rm(getPath(key), { force: true });
|
|
50
|
+
},
|
|
51
|
+
async check() {
|
|
52
|
+
const probe = join(root, PROBE_DIR, `${randomUUID()}.probe`);
|
|
53
|
+
let isWritable = true;
|
|
54
|
+
try {
|
|
55
|
+
await mkdir(dirname(probe), { recursive: true });
|
|
56
|
+
await writeFile(probe, "ok");
|
|
57
|
+
await rm(probe, { force: true });
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
isWritable = false;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const stats = await statfs(root);
|
|
64
|
+
return { isWritable, freeBytes: stats.bavail * stats.bsize };
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return { isWritable, freeBytes: null };
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
async function statOrNull(path) {
|
|
73
|
+
try {
|
|
74
|
+
const stats = await stat(path);
|
|
75
|
+
return stats.isFile() ? stats : null;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error.code === "ENOENT")
|
|
79
|
+
return null;
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=local-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage.js","sourceRoot":"","sources":["../../src/server/local-storage.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,oHAAoH;AACpH,6GAA6G;AAC7G,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAIhD,MAAM,WAAW,GAAG,+CAA+C,CAAC;AACpE,MAAM,SAAS,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AG,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;gBACjC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;YAC/D,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;YACzC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,IAAY;IACpC,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IACvC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACpE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
|
2
|
+
/**
|
|
3
|
+
* The last path part of `name` without control characters, slashes or backslashes, trimmed and at most 255
|
|
4
|
+
* characters (by code point); `file` when nothing is left.
|
|
5
|
+
*/
|
|
6
|
+
export declare function sanitizeFileName(name: string): string;
|
|
7
|
+
/** The media type with its type and subtype lowercased, or null when it is not one the table accepts. */
|
|
8
|
+
export declare function normalizeContentType(contentType: string): string | null;
|
|
9
|
+
/** A `Content-Disposition` value with an ASCII fallback name and the UTF-8 name (RFC 6266, RFC 8187). */
|
|
10
|
+
export declare function formatContentDisposition(disposition: "attachment" | "inline", name: string): string;
|
|
11
|
+
//# sourceMappingURL=names.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../../src/server/names.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,oBAAoB,6BAA6B,CAAC;AAE/D;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAMrD;AAED,yGAAyG;AACzG,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAMvE;AAED,yGAAyG;AACzG,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,YAAY,GAAG,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAInG"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Names and media types that come from users, made safe to store and to send back in a header or a URL.
|
|
2
|
+
const MAX_NAME_LENGTH = 255;
|
|
3
|
+
const FALLBACK_NAME = "file";
|
|
4
|
+
/** `type/subtype` with optional parameters, as the table's check allows. */
|
|
5
|
+
const CONTENT_TYPE_PATTERN = /^[a-z0-9][a-z0-9!#$&^_.+-]*\/[a-z0-9][a-z0-9!#$&^_.+-]*(;[ -~]*)?$/;
|
|
6
|
+
export const DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
|
7
|
+
/**
|
|
8
|
+
* The last path part of `name` without control characters, slashes or backslashes, trimmed and at most 255
|
|
9
|
+
* characters (by code point); `file` when nothing is left.
|
|
10
|
+
*/
|
|
11
|
+
export function sanitizeFileName(name) {
|
|
12
|
+
const base = name.split(/[/\\]/).at(-1) ?? "";
|
|
13
|
+
// eslint-disable-next-line no-control-regex -- control characters are exactly what the name must not carry
|
|
14
|
+
const cleaned = base.replace(/[\u0000-\u001f\u007f-\u009f]/g, "").trim();
|
|
15
|
+
const clipped = [...cleaned].slice(0, MAX_NAME_LENGTH).join("").trim();
|
|
16
|
+
return clipped === "" || clipped === "." || clipped === ".." ? FALLBACK_NAME : clipped;
|
|
17
|
+
}
|
|
18
|
+
/** The media type with its type and subtype lowercased, or null when it is not one the table accepts. */
|
|
19
|
+
export function normalizeContentType(contentType) {
|
|
20
|
+
const trimmed = contentType.trim();
|
|
21
|
+
const separator = trimmed.indexOf(";");
|
|
22
|
+
const essence = (separator === -1 ? trimmed : trimmed.slice(0, separator)).trim().toLowerCase();
|
|
23
|
+
const normalized = separator === -1 ? essence : `${essence}${trimmed.slice(separator)}`;
|
|
24
|
+
return normalized.length <= 255 && CONTENT_TYPE_PATTERN.test(normalized) ? normalized : null;
|
|
25
|
+
}
|
|
26
|
+
/** A `Content-Disposition` value with an ASCII fallback name and the UTF-8 name (RFC 6266, RFC 8187). */
|
|
27
|
+
export function formatContentDisposition(disposition, name) {
|
|
28
|
+
const fallback = name.replace(/[^\x20-\x7e]/g, "_").replace(/["\\]/g, "_");
|
|
29
|
+
const encoded = encodeURIComponent(name).replace(/['()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
30
|
+
return `${disposition}; filename="${fallback}"; filename*=UTF-8''${encoded}`;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=names.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"names.js","sourceRoot":"","sources":["../../src/server/names.ts"],"names":[],"mappings":"AAAA,wGAAwG;AAExG,MAAM,eAAe,GAAG,GAAG,CAAC;AAC5B,MAAM,aAAa,GAAG,MAAM,CAAC;AAC7B,4EAA4E;AAC5E,MAAM,oBAAoB,GAAG,oEAAoE,CAAC;AAClG,MAAM,CAAC,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAE/D;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC9C,2GAA2G;IAC3G,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,+BAA+B,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACzE,MAAM,OAAO,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,eAAe,C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,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;IAC3H,OAAO,GAAG,WAAW,eAAe,QAAQ,uBAAuB,OAAO,EAAE,CAAC;AAC/E,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type SoftureConfig } from "@softure-ai/core";
|
|
2
|
+
import type { FilesOptions } from "../options.js";
|
|
3
|
+
/** The enabled module. Throws when the app did not enable it: calling its functions then is a bug. */
|
|
4
|
+
export declare function getFilesModule(config: SoftureConfig): import("@softure-ai/core").AnySoftureModule;
|
|
5
|
+
export declare function getFilesOptions(config: SoftureConfig): FilesOptions;
|
|
6
|
+
/** The path prefix of public links after the app's override (`files({ routes: { links } })`), without a trailing slash. */
|
|
7
|
+
export declare function getLinksRoute(config: SoftureConfig): string;
|
|
8
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/server/options.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD,sGAAsG;AACtG,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,+CAMnD;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,YAAY,CAGnE;AAED,2HAA2H;AAC3H,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAK3D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// The files options of the running app, read from the configuration in the module context.
|
|
2
|
+
import { getModule } from "@softure-ai/core";
|
|
3
|
+
const MODULE_ID = "files";
|
|
4
|
+
/** The enabled module. Throws when the app did not enable it: calling its functions then is a bug. */
|
|
5
|
+
export function getFilesModule(config) {
|
|
6
|
+
const module = getModule(config, MODULE_ID);
|
|
7
|
+
if (module === undefined) {
|
|
8
|
+
throw new Error("@softure-ai/files: the module is not enabled; add files({ storage }) to modules in softure.config.ts");
|
|
9
|
+
}
|
|
10
|
+
return module;
|
|
11
|
+
}
|
|
12
|
+
export function getFilesOptions(config) {
|
|
13
|
+
// The module factory parsed these options with filesOptionsSchema.
|
|
14
|
+
return getFilesModule(config).options;
|
|
15
|
+
}
|
|
16
|
+
/** The path prefix of public links after the app's override (`files({ routes: { links } })`), without a trailing slash. */
|
|
17
|
+
export function getLinksRoute(config) {
|
|
18
|
+
const { links } = getFilesModule(config).routes;
|
|
19
|
+
// The manifest declares it, so a missing one means a broken module definition.
|
|
20
|
+
if (links === undefined)
|
|
21
|
+
throw new Error("@softure-ai/files: the links route is missing from the module manifest");
|
|
22
|
+
return trimTrailingSlashes(links);
|
|
23
|
+
}
|
|
24
|
+
/** The path without trailing slashes; a loop, not `/\/+$/`, which backtracks quadratically on a run of slashes. */
|
|
25
|
+
function trimTrailingSlashes(path) {
|
|
26
|
+
let end = path.length;
|
|
27
|
+
while (end > 0 && path[end - 1] === "/")
|
|
28
|
+
end -= 1;
|
|
29
|
+
return path.slice(0, end);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../src/server/options.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,OAAO,EAAE,SAAS,EAAsB,MAAM,kBAAkB,CAAC;AAGjE,MAAM,SAAS,GAAG,OAAO,CAAC;AAE1B,sGAAsG;AACtG,MAAM,UAAU,cAAc,CAAC,MAAqB;IAClD,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC5C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,sGAAsG,CAAC,CAAC;IAC1H,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,MAAqB;IACnD,mEAAmE;IACnE,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC,OAAuB,CAAC;AACxD,CAAC;AAED,2HAA2H;AAC3H,MAAM,UAAU,aAAa,CAAC,MAAqB;IACjD,MAAM,EAAE,KAAK,EAAE,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;IAChD,+EAA+E;IAC/E,IAAI,KAAK,KAAK,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;IACnH,OAAO,mBAAmB,CAAC,KAAK,CAAC,CAAC;AACpC,CAAC;AAED,mHAAmH;AACnH,SAAS,mBAAmB,CAAC,IAAY;IACvC,IAAI,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC;IACtB,OAAO,GAAG,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,KAAK,GAAG;QAAE,GAAG,IAAI,CAAC,CAAC;IAClD,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Ok, type PrivacyContributor } from "@softure-ai/core";
|
|
2
|
+
import type { StoredFile } from "../contract.js";
|
|
3
|
+
import { type FilesContext } from "./context.js";
|
|
4
|
+
/** What files holds about one user, as it appears in their export. */
|
|
5
|
+
export interface FilesUserData {
|
|
6
|
+
readonly files: readonly StoredFile[];
|
|
7
|
+
}
|
|
8
|
+
export interface FilesDeletionReport {
|
|
9
|
+
readonly deletedFiles: number;
|
|
10
|
+
/** Files whose storage delete failed; their rows stay marked deleted and `pruneFiles` removes them. */
|
|
11
|
+
readonly pendingFiles: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function exportFilesUserData(ctx: FilesContext, userId: string): Promise<Ok<FilesUserData>>;
|
|
14
|
+
export declare function deleteFilesUserData(ctx: FilesContext, userId: string): Promise<Ok<FilesDeletionReport>>;
|
|
15
|
+
export declare const filesPrivacyContributor: PrivacyContributor;
|
|
16
|
+
//# sourceMappingURL=privacy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"privacy.d.ts","sourceRoot":"","sources":["../../src/server/privacy.ts"],"names":[],"mappings":"AAGA,OAAO,EAAM,KAAK,EAAE,EAAE,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAGxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,EAAgB,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAI/D,sEAAsE;AACtE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,CAAC;CACvC;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,uGAAuG;IACvG,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC/B;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAOvG;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAU7G;AAED,eAAO,MAAM,uBAAuB,EAAE,kBAQrC,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The files part of a GDPR export and deletion (`@softure-ai/privacy`). A file belongs to its owner: the export
|
|
2
|
+
// lists the owner's live files (never the bytes or the storage key; the app links them itself when it wants), and
|
|
3
|
+
// the deletion removes every file of the owner from storage and the table.
|
|
4
|
+
import { ok } from "@softure-ai/core";
|
|
5
|
+
import { and, asc, eq } from "drizzle-orm";
|
|
6
|
+
import { files } from "../schema.js";
|
|
7
|
+
import { toStoredFile } from "./context.js";
|
|
8
|
+
import { removeMarkedFile } from "./files.js";
|
|
9
|
+
import { isLive } from "./queries.js";
|
|
10
|
+
export async function exportFilesUserData(ctx, userId) {
|
|
11
|
+
const rows = await ctx.db
|
|
12
|
+
.select()
|
|
13
|
+
.from(files)
|
|
14
|
+
.where(and(eq(files.ownerId, userId), isLive(ctx.clock.now())))
|
|
15
|
+
.orderBy(asc(files.createdAt), asc(files.id));
|
|
16
|
+
return ok({ files: rows.map(toStoredFile) });
|
|
17
|
+
}
|
|
18
|
+
export async function deleteFilesUserData(ctx, userId) {
|
|
19
|
+
const now = ctx.clock.now();
|
|
20
|
+
// Marked first, in one statement: nothing of the owner is served from here on, whatever storage does next.
|
|
21
|
+
await ctx.db.update(files).set({ deletedAt: now }).where(eq(files.ownerId, userId));
|
|
22
|
+
const rows = await ctx.db.select().from(files).where(eq(files.ownerId, userId));
|
|
23
|
+
let deletedFiles = 0;
|
|
24
|
+
for (const row of rows) {
|
|
25
|
+
if (await removeMarkedFile(ctx, row))
|
|
26
|
+
deletedFiles += 1;
|
|
27
|
+
}
|
|
28
|
+
return ok({ deletedFiles, pendingFiles: rows.length - deletedFiles });
|
|
29
|
+
}
|
|
30
|
+
export const filesPrivacyContributor = {
|
|
31
|
+
// Core types `db` as unknown; privacy passes the @softure-ai/db handle or its transaction.
|
|
32
|
+
exportUserData: (context, userId) => exportFilesUserData({ ...context, db: context.db }, userId),
|
|
33
|
+
// Privacy's contract answers a deletion with an empty ok; the counts are for apps that call the helper.
|
|
34
|
+
deleteUserData: async (context, userId) => {
|
|
35
|
+
await deleteFilesUserData({ ...context, db: context.db }, userId);
|
|
36
|
+
return ok();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=privacy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"privacy.js","sourceRoot":"","sources":["../../src/server/privacy.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,kHAAkH;AAClH,2EAA2E;AAC3E,OAAO,EAAE,EAAE,EAAoC,MAAM,kBAAkB,CAAC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C,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IAChF,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,MAAM,gBAAgB,CAAC,GAAG,EAAE,GAAG,CAAC;YAAE,YAAY,IAAI,CAAC,CAAC;IAC1D,CAAC;IACD,OAAO,EAAE,CAAC,EAAE,YAAY,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,GAAG,YAAY,EAAE,CAAC,CAAC;AACxE,CAAC;AAED,MAAM,CAAC,MAAM,uBAAuB,GAAuB;IACzD,2FAA2F;IAC3F,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,mBAAmB,CAAC,EAAE,GAAG,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,EAAe,EAAE,EAAE,MAAM,CAAC;IAC7G,wGAAwG;IACxG,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;QACxC,MAAM,mBAAmB,CAAC,EAAE,GAAG,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,EAAe,EAAE,EAAE,MAAM,CAAC,CAAC;QAC/E,OAAO,EAAE,EAAE,CAAC;IACd,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FilesContext } from "./context.js";
|
|
2
|
+
export interface PruneFilesOptions {
|
|
3
|
+
/** Rows read per round; rounds repeat until one comes back short. */
|
|
4
|
+
readonly batchSize?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface PruneFilesReport {
|
|
7
|
+
readonly deletedFiles: number;
|
|
8
|
+
readonly freedBytes: number;
|
|
9
|
+
/** Files whose storage delete failed; they stay for the next run. */
|
|
10
|
+
readonly failedFiles: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function pruneFiles(ctx: FilesContext, options?: PruneFilesOptions): Promise<PruneFilesReport>;
|
|
13
|
+
//# sourceMappingURL=prune.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../src/server/prune.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAKjD,MAAM,WAAW,iBAAiB;IAChC,qEAAqE;IACrE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,qEAAqE;IACrE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B;AAED,wBAAsB,UAAU,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAwB9G"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Retention: files past their expiry and files deleted but not yet out of storage leave storage, then the table.
|
|
2
|
+
// Run it from the `files.prune` job (`@softure-ai/files/jobs`) or a cron calling `pruneFiles(ctx)`.
|
|
3
|
+
import { asc, isNotNull, lte, or } from "drizzle-orm";
|
|
4
|
+
import { files } from "../schema.js";
|
|
5
|
+
import { removeMarkedFile } from "./files.js";
|
|
6
|
+
const DEFAULT_BATCH_SIZE = 500;
|
|
7
|
+
export async function pruneFiles(ctx, options = {}) {
|
|
8
|
+
const batchSize = Math.max(1, Math.trunc(options.batchSize ?? DEFAULT_BATCH_SIZE));
|
|
9
|
+
const now = ctx.clock.now();
|
|
10
|
+
let deletedFiles = 0;
|
|
11
|
+
let freedBytes = 0;
|
|
12
|
+
let failedFiles = 0;
|
|
13
|
+
for (;;) {
|
|
14
|
+
const rows = await ctx.db
|
|
15
|
+
.select()
|
|
16
|
+
.from(files)
|
|
17
|
+
.where(or(isNotNull(files.deletedAt), lte(files.expiresAt, now)))
|
|
18
|
+
.orderBy(asc(files.createdAt), asc(files.id))
|
|
19
|
+
.limit(batchSize)
|
|
20
|
+
.offset(failedFiles);
|
|
21
|
+
for (const row of rows) {
|
|
22
|
+
if (await removeMarkedFile(ctx, row)) {
|
|
23
|
+
deletedFiles += 1;
|
|
24
|
+
freedBytes += row.size;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
failedFiles += 1;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (rows.length < batchSize)
|
|
31
|
+
return { deletedFiles, freedBytes, failedFiles };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=prune.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune.js","sourceRoot":"","sources":["../../src/server/prune.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,oGAAoG;AACpG,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAc/B,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,GAAiB,EAAE,UAA6B,EAAE;IACjF,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,IAAI,kBAAkB,CAAC,CAAC,CAAC;IACnF,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IAC5B,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,IAAI,WAAW,GAAG,CAAC,CAAC;IACpB,SAAS,CAAC;QACR,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,EAAE;aACtB,MAAM,EAAE;aACR,IAAI,CAAC,KAAK,CAAC;aACX,KAAK,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,CAAC;aAChE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;aAC5C,KAAK,CAAC,SAAS,CAAC;aAChB,MAAM,CAAC,WAAW,CAAC,CAAC;QACvB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,MAAM,gBAAgB,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;gBACrC,YAAY,IAAI,CAAC,CAAC;gBAClB,UAAU,IAAI,GAAG,CAAC,IAAI,CAAC;YACzB,CAAC;iBAAM,CAAC;gBACN,WAAW,IAAI,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,GAAG,SAAS;YAAE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC;IAChF,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type SQL } from "drizzle-orm";
|
|
2
|
+
import type { Queryable } from "@softure-ai/db";
|
|
3
|
+
/** A file that is neither deleted nor past its expiry at `now`. */
|
|
4
|
+
export declare function isLive(now: Date): SQL;
|
|
5
|
+
/** The bytes an owner keeps in live files. */
|
|
6
|
+
export declare function sumOwnerBytes(db: Queryable, ownerId: string, now: Date): Promise<number>;
|
|
7
|
+
//# sourceMappingURL=queries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/server/queries.ts"],"names":[],"mappings":"AACA,OAAO,EAAgC,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,mEAAmE;AACnE,wBAAgB,MAAM,CAAC,GAAG,EAAE,IAAI,GAAG,GAAG,CAGrC;AAED,8CAA8C;AAC9C,wBAAsB,aAAa,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAM9F"}
|