@softure-ai/files 0.3.0 → 0.4.0

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 CHANGED
@@ -4,6 +4,15 @@ Newest first. Each version lists what changed for an app that uses `@softure-ai/
4
4
  in production, the version gets a line `verified in: <app>@<commit>` ([docs/05](../../docs/05-adoption-playbook.md),
5
5
  "Definition of done").
6
6
 
7
+ ## 0.4.0
8
+
9
+ - The `files.prune` job logs every run (#1192): one line `files.prune finished` with `removed`, `freedBytes`,
10
+ `failedFiles`, `deletedUploads`, `failedUploads` and, when at most 10 files went, their ids as `fileIds` (also
11
+ `removed=0 freedBytes=0` for an empty run). `createFilesJobHandlers({ logger })` takes any logger with `info`
12
+ (`createLogger("files")` from `@softure-ai/observability` as it is); `filesJobHandlers` logs to the console.
13
+ - `PruneFilesReport.removedFileIds`: the ids of the first `PRUNE_REPORT_MAX_FILE_IDS` (10) removed files.
14
+ - Migration: none. A test that compares a whole `pruneFiles` report with `toEqual` adds `removedFileIds`.
15
+
7
16
  ## 0.3.0
8
17
 
9
18
  - `serveStoredFile(ctx, fileId, request, { ownerId, disposition? })` in `/server` and `createStoredFileRoute({ getOwnerId })`
package/README.md CHANGED
@@ -43,7 +43,10 @@ sandbox, a phone behind a proxy with a body limit) uploads through a signed URL
43
43
  `create_upload` (a signed URL, a ready `curl` loop and the browser page) and `finalize_upload`, so an assistant hands
44
44
  over a video without its bytes going through a tool call. See §4.2.
45
45
  - **Retention**: `pruneFiles(ctx)` removes expired and deleted files from storage and the table, and expired upload
46
- sessions with their chunks; the `files.prune` job (`@softure-ai/files/jobs`) runs it hourly.
46
+ sessions with their chunks; the `files.prune` job (`@softure-ai/files/jobs`) runs it hourly. Every run logs one line
47
+ `files.prune finished removed=<n> freedBytes=<b> failedFiles deletedUploads failedUploads`, with the removed ids as
48
+ `fileIds` when at most 10 went; `createFilesJobHandlers({ logger: createLogger("files") })` sends it through
49
+ `@softure-ai/observability` instead of the console.
47
50
  - **A privacy contributor** (the export lists the owner's files, the deletion removes them) and **a health check**
48
51
  (the table answers, storage takes a write, free space above `health.minFreeBytes`).
49
52
  - **`/testing`**: an in-memory driver, a fixed link keyring, `fetchFileLink` (status, headers and bytes of a link,
package/dist/index.js CHANGED
@@ -22,7 +22,7 @@ export const FILES_RATE_LIMIT_BUCKETS = {
22
22
  export const files = defineModule({
23
23
  manifest: {
24
24
  id: MODULE_ID,
25
- version: "0.3.0",
25
+ version: "0.4.0",
26
26
  dependsOn: { jobs: "^0.1.6?", security: "^0.2.5?" },
27
27
  dbSchema: "files",
28
28
  tables: ["files", "uploads"],
@@ -1,4 +1,26 @@
1
- /** Handlers for the worker: `handlers: { ...filesJobHandlers }`. */
1
+ export type FilesJobsLogFields = Readonly<Record<string, unknown>>;
2
+ /**
3
+ * Where the files jobs report a run. A subset of `@softure-ai/observability`'s `Logger`, so an app passes
4
+ * `createLogger("files")` as it is; without one, lines go to the console.
5
+ */
6
+ export interface FilesJobsLogger {
7
+ info(message: string, fields?: FilesJobsLogFields): void;
8
+ }
9
+ export interface CreateFilesJobHandlersOptions {
10
+ readonly logger?: FilesJobsLogger;
11
+ }
12
+ /** One `key=value` line per call; an array is joined with commas. */
13
+ export declare const consoleFilesJobsLogger: FilesJobsLogger;
14
+ /**
15
+ * Handlers for the worker with a logger of the app's choice: `handlers: { ...createFilesJobHandlers({ logger }) }`.
16
+ * Every `files.prune` run logs one line `files.prune finished` with `removed`, `freedBytes`, `failedFiles`,
17
+ * `deletedUploads`, `failedUploads` and, when at most 10 files went, their ids as `fileIds`.
18
+ */
19
+ export declare function createFilesJobHandlers(options?: CreateFilesJobHandlersOptions): {
20
+ /** Removes expired and deleted files from storage and the table (`pruneFiles`). */
21
+ "files.prune": import("@softure-ai/jobs").JobHandler<Record<string, never>>;
22
+ };
23
+ /** Handlers for the worker: `handlers: { ...filesJobHandlers }`; they log to the console. */
2
24
  export declare const filesJobHandlers: {
3
25
  /** Removes expired and deleted files from storage and the table (`pruneFiles`). */
4
26
  "files.prune": import("@softure-ai/jobs").JobHandler<Record<string, never>>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAOA,oEAAoE;AACpE,eAAO,MAAM,gBAAgB;IAC3B,mFAAmF;;CAQ9D,CAAC;AAExB,6EAA6E;AAC7E,eAAO,MAAM,iBAAiB;;;;CAED,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAEnE;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAAC;CAC1D;AAED,MAAM,WAAW,6BAA6B;IAC5C,QAAQ,CAAC,MAAM,CAAC,EAAE,eAAe,CAAC;CACnC;AAID,qEAAqE;AACrE,eAAO,MAAM,sBAAsB,EAAE,eAEpC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,6BAAkC;IAG9E,mFAAmF;;EAStF;AAED,6FAA6F;AAC7F,eAAO,MAAM,gBAAgB;IAZzB,mFAAmF;;CAY/B,CAAC;AAEzD,6EAA6E;AAC7E,eAAO,MAAM,iBAAiB;;;;CAED,CAAC"}
@@ -3,20 +3,55 @@
3
3
  import { ok } from "@softure-ai/core";
4
4
  import { defineJobHandler } from "@softure-ai/jobs";
5
5
  import { z } from "zod";
6
- import { pruneFiles } from "../server/prune.js";
7
- /** Handlers for the worker: `handlers: { ...filesJobHandlers }`. */
8
- export const filesJobHandlers = {
9
- /** Removes expired and deleted files from storage and the table (`pruneFiles`). */
10
- "files.prune": defineJobHandler({
11
- payload: z.object({}),
12
- run: async (_payload, ctx) => {
13
- await pruneFiles(ctx);
14
- return ok();
15
- },
16
- }),
6
+ import { PRUNE_REPORT_MAX_FILE_IDS, pruneFiles } from "../server/prune.js";
7
+ const PREFIX = "@softure-ai/files:";
8
+ /** One `key=value` line per call; an array is joined with commas. */
9
+ export const consoleFilesJobsLogger = {
10
+ info: (message, fields) => console.log(formatLine(message, fields)),
17
11
  };
12
+ /**
13
+ * Handlers for the worker with a logger of the app's choice: `handlers: { ...createFilesJobHandlers({ logger }) }`.
14
+ * Every `files.prune` run logs one line `files.prune finished` with `removed`, `freedBytes`, `failedFiles`,
15
+ * `deletedUploads`, `failedUploads` and, when at most 10 files went, their ids as `fileIds`.
16
+ */
17
+ export function createFilesJobHandlers(options = {}) {
18
+ const logger = options.logger ?? consoleFilesJobsLogger;
19
+ return {
20
+ /** Removes expired and deleted files from storage and the table (`pruneFiles`). */
21
+ "files.prune": defineJobHandler({
22
+ payload: z.object({}),
23
+ run: async (_payload, ctx) => {
24
+ logger.info("files.prune finished", getPruneLogFields(await pruneFiles(ctx)));
25
+ return ok();
26
+ },
27
+ }),
28
+ };
29
+ }
30
+ /** Handlers for the worker: `handlers: { ...filesJobHandlers }`; they log to the console. */
31
+ export const filesJobHandlers = createFilesJobHandlers();
18
32
  /** Schedules for `jobs({ schedules: { ...filesJobSchedules } })`: hourly. */
19
33
  export const filesJobSchedules = {
20
34
  "files.prune": { every: "1h" },
21
35
  };
36
+ function getPruneLogFields(report) {
37
+ const fields = {
38
+ removed: report.deletedFiles,
39
+ freedBytes: report.freedBytes,
40
+ failedFiles: report.failedFiles,
41
+ deletedUploads: report.deletedUploads,
42
+ failedUploads: report.failedUploads,
43
+ };
44
+ return report.deletedFiles > PRUNE_REPORT_MAX_FILE_IDS ? fields : { ...fields, fileIds: report.removedFileIds };
45
+ }
46
+ function formatLine(message, fields) {
47
+ const parts = Object.entries(fields ?? {}).map(([key, value]) => `${key}=${formatValue(value)}`);
48
+ return [PREFIX, message, ...parts].join(" ");
49
+ }
50
+ function formatValue(value) {
51
+ if (Array.isArray(value))
52
+ return value.map(formatValue).join(",");
53
+ if (typeof value === "string")
54
+ return value;
55
+ return JSON.stringify(value) ?? String(value);
56
+ }
22
57
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,yDAAyD;AACzD,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAA4C,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,oEAAoE;AACpE,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,mFAAmF;IACnF,aAAa,EAAE,gBAAgB,CAAC;QAC9B,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;QACrB,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,EAAE;YAC3B,MAAM,UAAU,CAAC,GAAG,CAAC,CAAC;YACtB,OAAO,EAAE,EAAE,CAAC;QACd,CAAC;KACF,CAAC;CACmB,CAAC;AAExB,6EAA6E;AAC7E,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE;CACH,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,yDAAyD;AACzD,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAA4C,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,UAAU,EAAyB,MAAM,oBAAoB,CAAC;AAgBlG,MAAM,MAAM,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E,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,cAAc,EAAE,CAAC;AAClH,CAAC;AAED,SAAS,UAAU,CAAC,OAAe,EAAE,MAAsC;IACzE,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACjG,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAClE,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;AAChD,CAAC"}
@@ -7,7 +7,7 @@ export { createLocalStorage, type LocalStorageOptions } from "./local-storage.js
7
7
  export { DEFAULT_CONTENT_TYPE, formatContentDisposition, normalizeContentType, sanitizeFileName } from "./names.js";
8
8
  export { getFilesModule, getFilesOptions, getLinksRoute, getUploadsRoute } from "./options.js";
9
9
  export { deleteFilesUserData, exportFilesUserData, filesPrivacyContributor, type FilesDeletionReport, type FilesUserData } from "./privacy.js";
10
- export { pruneFiles, type PruneFilesOptions, type PruneFilesReport } from "./prune.js";
10
+ export { PRUNE_REPORT_MAX_FILE_IDS, pruneFiles, type PruneFilesOptions, type PruneFilesReport } from "./prune.js";
11
11
  export { createS3Storage, S3_ACCESS_KEY_ID_ENV, S3_SECRET_ACCESS_KEY_ENV, S3StorageError, splitIntoParts, type S3StorageOptions, } from "./s3-storage.js";
12
12
  export { saveFile, validateFileDescription, type FileDescription, type FileDescriptionInput, type SaveFileInput, type SaveFileResult } from "./save.js";
13
13
  export { countChunks, createUpload, finalizeUpload, getPartKey, getUpload, writeUploadChunk, type CreateUploadInput, type CreateUploadResult, type FinalizeUploadResult, type UploadChunkErrorCode, type UploadLookupOptions, type WriteUploadChunkInput, type WriteUploadChunkResult, } from "./uploads.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,KAAK,GAAG,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAC1E,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,aAAa,EACb,SAAS,EACT,QAAQ,EACR,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAAE,KAAK,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,eAAe,EAAE,KAAK,WAAW,EAAE,KAAK,iBAAiB,EAAE,MAAM,WAAW,CAAC;AACjI,OAAO,EACL,cAAc,EACd,eAAe,EACf,cAAc,EACd,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,EAC1B,KAAK,YAAY,EACjB,KAAK,oBAAoB,GAC1B,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAAE,KAAK,mBAAmB,EAAE,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,e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AACzB,OAAO,EAAE,QAAQ,EAAE,uBAAuB,EAAE,KAAK,eAAe,EAAE,KAAK,oBAAoB,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AACxJ,OAAO,EACL,WAAW,EACX,YAAY,EACZ,cAAc,EACd,UAAU,EACV,SAAS,EACT,gBAAgB,EAChB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,GAC5B,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,8BAA8B,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAC3G,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
@@ -9,7 +9,7 @@ export { createLocalStorage } from "./local-storage.js";
9
9
  export { DEFAULT_CONTENT_TYPE, formatContentDisposition, normalizeContentType, sanitizeFileName } from "./names.js";
10
10
  export { getFilesModule, getFilesOptions, getLinksRoute, getUploadsRoute } from "./options.js";
11
11
  export { deleteFilesUserData, exportFilesUserData, filesPrivacyContributor } from "./privacy.js";
12
- export { pruneFiles } from "./prune.js";
12
+ export { PRUNE_REPORT_MAX_FILE_IDS, pruneFiles } from "./prune.js";
13
13
  export { createS3Storage, S3_ACCESS_KEY_ID_ENV, S3_SECRET_ACCESS_KEY_ENV, S3StorageError, splitIntoParts, } from "./s3-storage.js";
14
14
  export { saveFile, validateFileDescription } from "./save.js";
15
15
  export { countChunks, createUpload, finalizeUpload, getPartKey, getUpload, writeUploadChunk, } from "./uploads.js";
@@ -1 +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,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/F,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,EAAE,uBAAuB,EAA4F,MAAM,WAAW,CAAC;AACxJ,OAAO,EACL,WAAW,EACX,YAAY,EACZ,cAAc,EACd,UAAU,EACV,SAAS,EACT,gBAAgB,GAQjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtE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B,EAA4F,MAAM,WAAW,CAAC;AACxJ,OAAO,EACL,WAAW,EACX,YAAY,EACZ,cAAc,EACd,UAAU,EACV,SAAS,EACT,gBAAgB,GAQjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,8BAA8B,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,eAAe,EAA+B,MAAM,YAAY,CAAC;AAC3G,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,WAAW,EAAgD,MAAM,YAAY,CAAC"}
@@ -1,4 +1,6 @@
1
1
  import type { FilesContext } from "./context.js";
2
+ /** How many removed file ids a prune report keeps (`removedFileIds`). */
3
+ export declare const PRUNE_REPORT_MAX_FILE_IDS = 10;
2
4
  export interface PruneFilesOptions {
3
5
  /** Rows read per round; rounds repeat until one comes back short. */
4
6
  readonly batchSize?: number;
@@ -6,6 +8,8 @@ export interface PruneFilesOptions {
6
8
  export interface PruneFilesReport {
7
9
  readonly deletedFiles: number;
8
10
  readonly freedBytes: number;
11
+ /** Ids of the first `PRUNE_REPORT_MAX_FILE_IDS` (10) removed files, in removal order; `deletedFiles` has the count. */
12
+ readonly removedFileIds: readonly string[];
9
13
  /** Files whose storage delete failed; they stay for the next run. */
10
14
  readonly failedFiles: number;
11
15
  /** Expired upload sessions removed with their parts. */
@@ -1 +1 @@
1
- {"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../src/server/prune.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAMjD,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;IAC7B,wDAAwD;IACxD,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,4FAA4F;IAC5F,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAChC;AAED,wBAAsB,UAAU,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAwB9G"}
1
+ {"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../src/server/prune.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAMjD,yEAAyE;AACzE,eAAO,MAAM,yBAAyB,KAAK,CAAC;AAE5C,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,uHAAuH;IACvH,QAAQ,CAAC,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3C,qEAAqE;IACrE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,wDAAwD;IACxD,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,4FAA4F;IAC5F,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAChC;AAED,wBAAsB,UAAU,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA4B9G"}
@@ -6,12 +6,15 @@ import { files } from "../schema.js";
6
6
  import { removeMarkedFile } from "./files.js";
7
7
  import { pruneUploads } from "./uploads.js";
8
8
  const DEFAULT_BATCH_SIZE = 500;
9
+ /** How many removed file ids a prune report keeps (`removedFileIds`). */
10
+ export const PRUNE_REPORT_MAX_FILE_IDS = 10;
9
11
  export async function pruneFiles(ctx, options = {}) {
10
12
  const batchSize = Math.max(1, Math.trunc(options.batchSize ?? DEFAULT_BATCH_SIZE));
11
13
  const now = ctx.clock.now();
12
14
  let deletedFiles = 0;
13
15
  let freedBytes = 0;
14
16
  let failedFiles = 0;
17
+ const removedFileIds = [];
15
18
  for (;;) {
16
19
  const rows = await ctx.db
17
20
  .select()
@@ -24,13 +27,16 @@ export async function pruneFiles(ctx, options = {}) {
24
27
  if (await removeMarkedFile(ctx, row)) {
25
28
  deletedFiles += 1;
26
29
  freedBytes += row.size;
30
+ if (removedFileIds.length < PRUNE_REPORT_MAX_FILE_IDS)
31
+ removedFileIds.push(row.id);
27
32
  }
28
33
  else {
29
34
  failedFiles += 1;
30
35
  }
31
36
  }
32
- if (rows.length < batchSize)
33
- return { deletedFiles, freedBytes, failedFiles, ...(await pruneUploads(ctx, batchSize)) };
37
+ if (rows.length < batchSize) {
38
+ return { deletedFiles, freedBytes, removedFileIds, failedFiles, ...(await pruneUploads(ctx, batchSize)) };
39
+ }
34
40
  }
35
41
  }
36
42
  //# sourceMappingURL=prune.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"prune.js","sourceRoot":"","sources":["../../src/server/prune.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,4DAA4D;AAC5D,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;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAkB/B,MAAM,CAAC,KAAK,UAAU,UAAU,CA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,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;gBACvB,IAAI,cAAc,CAAC,MAAM,GAAG,yBAAyB;oBAAE,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACrF,CAAC;iBAAM,CAAC;gBACN,WAAW,IAAI,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC;YAC5B,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC;QAC5G,CAAC;IACH,CAAC;AACH,CAAC"}
package/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "files",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "dependsOn": { "jobs": "^0.1.6?", "security": "^0.2.5?" },
5
5
  "dbSchema": "files",
6
6
  "tables": ["files", "uploads"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softure-ai/files",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "SOFTURE AI files module: an app's stored files (content hash, size, type, owner, kind, expiry, a per-owner quota, opt-in dedupe) on a storage driver (local disk with atomic writes, S3-compatible with SigV4 and multipart uploads, in memory for tests), streamed and never buffered, public links that expire (HMAC-signed, checked before any database read, revocable per file, a rotating keyring) served by a Next.js route with byte ranges and one 404 for every dead link, the same answer for the signed-in owner by id without a link, resumable chunked uploads through signed URLs (with a browser client, an upload list, a browser page and MCP upload tools), a prune job, a privacy contributor and a health check.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -70,7 +70,7 @@
70
70
  "@softure-ai/core": "^0.2.7",
71
71
  "@softure-ai/db": "^0.1.14",
72
72
  "@softure-ai/security": "^0.2.5",
73
- "zod": "^4.6.5"
73
+ "zod": "^4.6.7"
74
74
  },
75
75
  "peerDependencies": {
76
76
  "@modelcontextprotocol/server": "^2.0.0",
package/src/index.ts CHANGED
@@ -26,7 +26,7 @@ export const FILES_RATE_LIMIT_BUCKETS = {
26
26
  export const files = defineModule({
27
27
  manifest: {
28
28
  id: MODULE_ID,
29
- version: "0.3.0",
29
+ version: "0.4.0",
30
30
  dependsOn: { jobs: "^0.1.6?", security: "^0.2.5?" },
31
31
  dbSchema: "files",
32
32
  tables: ["files", "uploads"],
package/src/jobs/index.ts CHANGED
@@ -3,21 +3,74 @@
3
3
  import { ok } from "@softure-ai/core";
4
4
  import { defineJobHandler, type JobHandlers, type JobSchedulesInput } from "@softure-ai/jobs";
5
5
  import { z } from "zod";
6
- import { pruneFiles } from "../server/prune.js";
7
-
8
- /** Handlers for the worker: `handlers: { ...filesJobHandlers }`. */
9
- export const filesJobHandlers = {
10
- /** Removes expired and deleted files from storage and the table (`pruneFiles`). */
11
- "files.prune": defineJobHandler({
12
- payload: z.object({}),
13
- run: async (_payload, ctx) => {
14
- await pruneFiles(ctx);
15
- return ok();
16
- },
17
- }),
18
- } satisfies JobHandlers;
6
+ import { PRUNE_REPORT_MAX_FILE_IDS, pruneFiles, type PruneFilesReport } from "../server/prune.js";
7
+
8
+ export type FilesJobsLogFields = Readonly<Record<string, unknown>>;
9
+
10
+ /**
11
+ * Where the files jobs report a run. A subset of `@softure-ai/observability`'s `Logger`, so an app passes
12
+ * `createLogger("files")` as it is; without one, lines go to the console.
13
+ */
14
+ export interface FilesJobsLogger {
15
+ info(message: string, fields?: FilesJobsLogFields): void;
16
+ }
17
+
18
+ export interface CreateFilesJobHandlersOptions {
19
+ readonly logger?: FilesJobsLogger;
20
+ }
21
+
22
+ const PREFIX = "@softure-ai/files:";
23
+
24
+ /** One `key=value` line per call; an array is joined with commas. */
25
+ export const consoleFilesJobsLogger: FilesJobsLogger = {
26
+ info: (message, fields) => console.log(formatLine(message, fields)),
27
+ };
28
+
29
+ /**
30
+ * Handlers for the worker with a logger of the app's choice: `handlers: { ...createFilesJobHandlers({ logger }) }`.
31
+ * Every `files.prune` run logs one line `files.prune finished` with `removed`, `freedBytes`, `failedFiles`,
32
+ * `deletedUploads`, `failedUploads` and, when at most 10 files went, their ids as `fileIds`.
33
+ */
34
+ export function createFilesJobHandlers(options: CreateFilesJobHandlersOptions = {}) {
35
+ const logger = options.logger ?? consoleFilesJobsLogger;
36
+ return {
37
+ /** Removes expired and deleted files from storage and the table (`pruneFiles`). */
38
+ "files.prune": defineJobHandler({
39
+ payload: z.object({}),
40
+ run: async (_payload, ctx) => {
41
+ logger.info("files.prune finished", getPruneLogFields(await pruneFiles(ctx)));
42
+ return ok();
43
+ },
44
+ }),
45
+ } satisfies JobHandlers;
46
+ }
47
+
48
+ /** Handlers for the worker: `handlers: { ...filesJobHandlers }`; they log to the console. */
49
+ export const filesJobHandlers = createFilesJobHandlers();
19
50
 
20
51
  /** Schedules for `jobs({ schedules: { ...filesJobSchedules } })`: hourly. */
21
52
  export const filesJobSchedules = {
22
53
  "files.prune": { every: "1h" },
23
54
  } satisfies JobSchedulesInput;
55
+
56
+ function getPruneLogFields(report: PruneFilesReport): FilesJobsLogFields {
57
+ const fields = {
58
+ removed: report.deletedFiles,
59
+ freedBytes: report.freedBytes,
60
+ failedFiles: report.failedFiles,
61
+ deletedUploads: report.deletedUploads,
62
+ failedUploads: report.failedUploads,
63
+ };
64
+ return report.deletedFiles > PRUNE_REPORT_MAX_FILE_IDS ? fields : { ...fields, fileIds: report.removedFileIds };
65
+ }
66
+
67
+ function formatLine(message: string, fields: FilesJobsLogFields | undefined): string {
68
+ const parts = Object.entries(fields ?? {}).map(([key, value]) => `${key}=${formatValue(value)}`);
69
+ return [PREFIX, message, ...parts].join(" ");
70
+ }
71
+
72
+ function formatValue(value: unknown): string {
73
+ if (Array.isArray(value)) return value.map(formatValue).join(",");
74
+ if (typeof value === "string") return value;
75
+ return JSON.stringify(value) ?? String(value);
76
+ }
@@ -31,7 +31,7 @@ export { createLocalStorage, type LocalStorageOptions } from "./local-storage.js
31
31
  export { DEFAULT_CONTENT_TYPE, formatContentDisposition, normalizeContentType, sanitizeFileName } from "./names.js";
32
32
  export { getFilesModule, getFilesOptions, getLinksRoute, getUploadsRoute } from "./options.js";
33
33
  export { deleteFilesUserData, exportFilesUserData, filesPrivacyContributor, type FilesDeletionReport, type FilesUserData } from "./privacy.js";
34
- export { pruneFiles, type PruneFilesOptions, type PruneFilesReport } from "./prune.js";
34
+ export { PRUNE_REPORT_MAX_FILE_IDS, pruneFiles, type PruneFilesOptions, type PruneFilesReport } from "./prune.js";
35
35
  export {
36
36
  createS3Storage,
37
37
  S3_ACCESS_KEY_ID_ENV,
@@ -9,6 +9,9 @@ import { pruneUploads } from "./uploads.js";
9
9
 
10
10
  const DEFAULT_BATCH_SIZE = 500;
11
11
 
12
+ /** How many removed file ids a prune report keeps (`removedFileIds`). */
13
+ export const PRUNE_REPORT_MAX_FILE_IDS = 10;
14
+
12
15
  export interface PruneFilesOptions {
13
16
  /** Rows read per round; rounds repeat until one comes back short. */
14
17
  readonly batchSize?: number;
@@ -17,6 +20,8 @@ export interface PruneFilesOptions {
17
20
  export interface PruneFilesReport {
18
21
  readonly deletedFiles: number;
19
22
  readonly freedBytes: number;
23
+ /** Ids of the first `PRUNE_REPORT_MAX_FILE_IDS` (10) removed files, in removal order; `deletedFiles` has the count. */
24
+ readonly removedFileIds: readonly string[];
20
25
  /** Files whose storage delete failed; they stay for the next run. */
21
26
  readonly failedFiles: number;
22
27
  /** Expired upload sessions removed with their parts. */
@@ -31,6 +36,7 @@ export async function pruneFiles(ctx: FilesContext, options: PruneFilesOptions =
31
36
  let deletedFiles = 0;
32
37
  let freedBytes = 0;
33
38
  let failedFiles = 0;
39
+ const removedFileIds: string[] = [];
34
40
  for (;;) {
35
41
  const rows = await ctx.db
36
42
  .select()
@@ -43,10 +49,13 @@ export async function pruneFiles(ctx: FilesContext, options: PruneFilesOptions =
43
49
  if (await removeMarkedFile(ctx, row)) {
44
50
  deletedFiles += 1;
45
51
  freedBytes += row.size;
52
+ if (removedFileIds.length < PRUNE_REPORT_MAX_FILE_IDS) removedFileIds.push(row.id);
46
53
  } else {
47
54
  failedFiles += 1;
48
55
  }
49
56
  }
50
- if (rows.length < batchSize) return { deletedFiles, freedBytes, failedFiles, ...(await pruneUploads(ctx, batchSize)) };
57
+ if (rows.length < batchSize) {
58
+ return { deletedFiles, freedBytes, removedFileIds, failedFiles, ...(await pruneUploads(ctx, batchSize)) };
59
+ }
51
60
  }
52
61
  }