@pithy-sh/storage 0.3.0 → 0.5.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.
Files changed (84) hide show
  1. package/dist/capability.d.ts +12 -3
  2. package/dist/capability.d.ts.map +1 -1
  3. package/dist/capability.js +35 -9
  4. package/dist/config/config.d.ts +20 -4
  5. package/dist/config/config.d.ts.map +1 -1
  6. package/dist/config/config.js +23 -6
  7. package/dist/data/storageObject.d.ts +12 -0
  8. package/dist/data/storageObject.d.ts.map +1 -1
  9. package/dist/data/storageObject.js +13 -2
  10. package/dist/data/tables.js +1 -1
  11. package/dist/error/errors.d.ts +4 -3
  12. package/dist/error/errors.d.ts.map +1 -1
  13. package/dist/error/errors.js +2 -2
  14. package/dist/http/handlers.d.ts +45 -14
  15. package/dist/http/handlers.d.ts.map +1 -1
  16. package/dist/http/handlers.js +169 -66
  17. package/dist/http/responses.d.ts +6 -0
  18. package/dist/http/responses.d.ts.map +1 -1
  19. package/dist/http/responses.js +2 -1
  20. package/dist/http/routes.d.ts +13 -2
  21. package/dist/http/routes.d.ts.map +1 -1
  22. package/dist/http/routes.js +37 -9
  23. package/dist/http/schemas.d.ts +6 -0
  24. package/dist/http/schemas.d.ts.map +1 -1
  25. package/dist/http/schemas.js +6 -5
  26. package/dist/http/scopes.d.ts +25 -0
  27. package/dist/http/scopes.d.ts.map +1 -0
  28. package/dist/http/scopes.js +32 -0
  29. package/dist/http/serve.d.ts +5 -2
  30. package/dist/http/serve.d.ts.map +1 -1
  31. package/dist/index.d.ts +2 -0
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +5 -3
  34. package/dist/migrations/0002_ownership.d.ts +5 -0
  35. package/dist/migrations/0002_ownership.d.ts.map +1 -0
  36. package/dist/migrations/0002_ownership.js +114 -0
  37. package/dist/object/cloudflare.d.ts +2 -1
  38. package/dist/object/cloudflare.d.ts.map +1 -1
  39. package/dist/object/cloudflare.js +2 -2
  40. package/dist/provision/provisionStorage.d.ts +14 -2
  41. package/dist/provision/provisionStorage.d.ts.map +1 -1
  42. package/dist/provision/provisionStorage.js +19 -4
  43. package/dist/provision/resolveStorageConfig.js +1 -1
  44. package/dist/quota/ceiling.d.ts +36 -0
  45. package/dist/quota/ceiling.d.ts.map +1 -0
  46. package/dist/quota/ceiling.js +31 -0
  47. package/dist/quota/quota.d.ts +24 -7
  48. package/dist/quota/quota.d.ts.map +1 -1
  49. package/dist/quota/quota.js +40 -12
  50. package/dist/secret/bundleToken.d.ts +24 -0
  51. package/dist/secret/bundleToken.d.ts.map +1 -0
  52. package/dist/secret/bundleToken.js +74 -0
  53. package/dist/secret/registry.d.ts +5 -1
  54. package/dist/secret/registry.d.ts.map +1 -1
  55. package/dist/secret/registry.js +7 -3
  56. package/dist/seeds/example.d.ts.map +1 -1
  57. package/dist/seeds/example.js +3 -0
  58. package/dist/test-utils/liveStorage.d.ts.map +1 -1
  59. package/dist/test-utils/liveStorage.js +5 -2
  60. package/dist/version.generated.d.ts +1 -1
  61. package/dist/version.generated.js +1 -1
  62. package/package.json +5 -5
  63. package/pithy.manifest.json +20 -10
  64. package/src/capability.ts +52 -8
  65. package/src/config/config.ts +24 -6
  66. package/src/data/storageObject.ts +22 -6
  67. package/src/error/errors.ts +6 -5
  68. package/src/http/handlers.ts +212 -79
  69. package/src/http/responses.ts +13 -1
  70. package/src/http/routes.ts +64 -10
  71. package/src/http/schemas.ts +10 -4
  72. package/src/http/scopes.ts +39 -0
  73. package/src/http/serve.ts +5 -2
  74. package/src/index.ts +2 -0
  75. package/src/migrations/0002_ownership.ts +176 -0
  76. package/src/object/cloudflare.ts +2 -2
  77. package/src/provision/provisionStorage.ts +22 -3
  78. package/src/quota/ceiling.ts +44 -0
  79. package/src/quota/quota.ts +63 -20
  80. package/src/secret/bundleToken.ts +81 -0
  81. package/src/secret/registry.ts +6 -8
  82. package/src/seeds/example.ts +3 -0
  83. package/src/test-utils/liveStorage.ts +3 -6
  84. package/src/version.generated.ts +1 -1
@@ -1,14 +1,19 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
- import { storageDatabase } from "../data/tables.js";
3
+ import { STORAGE_OBJECTS_TABLE, storageDatabase } from "../data/tables.js";
4
+ import { abortUpload, completeUpload, copyObject, createShare, deleteObject, initUpload, listObjects, listUploadParts, presignObject, readableObject, resolveShare, revokeShare, updateObject } from "./handlers.js";
4
5
  import { STORAGE_R2_SECRET } from "../secret/registry.js";
5
6
  import { objectStore } from "../object/store.js";
7
+ import { storageCeiling } from "../quota/ceiling.js";
6
8
  import { requireAuth } from "./guard.js";
7
- import { abortUpload, completeUpload, copyObject, createShare, deleteObject, initUpload, listObjects, listUploadParts, presignObject, readableObject, resolveShare, revokeShare, updateObject } from "./handlers.js";
8
9
  import { ObjectDeletedResponse, ObjectListResponse, PresignedUrlResponse, ShareResponse, ShareRevokedResponse, StorageObjectResponse, UploadInitResponse, UploadPartsResponse } from "./responses.js";
9
10
  import { CompleteUploadInput, CopyObjectInput, CreateShareInput, CreateUploadInput, ListObjectsQuery, ObjectIdParam, ObjectReadQuery, ShareTokenParam, UpdateObjectInput } from "./schemas.js";
11
+ import { STORAGE_USAGE_READ_SCOPE } from "./scopes.js";
10
12
  import { parseConditions, parseRangeHeader, rangeNotSatisfiable, serveMetadata, serveObject, toObjectRange } from "./serve.js";
11
13
  import { InternalError } from "@pithy-sh/core/src/error/pithyError";
14
+ import { accessCaller } from "@pithy-sh/core/src/access/access";
15
+ import { UsageQuery, UsageResponse, usageRollup } from "@pithy-sh/core/src/access/usage";
16
+ import { requireControlPlane } from "@pithy-sh/core/src/controlPlane/http/guard";
12
17
  import { describeRoute } from "@pithy-sh/core/src/http/routeSpec";
13
18
  import { validate } from "@pithy-sh/core/src/http/validation";
14
19
  //#region src/http/routes.ts
@@ -26,7 +31,7 @@ function requireBinding(env, name, kind) {
26
31
  return value;
27
32
  }
28
33
  /** Build the default per-request dependency resolver from the resolved config. */
29
- function defaultResolveDeps(config) {
34
+ function defaultResolveDeps(config, permission) {
30
35
  return async (c) => {
31
36
  const env = c.env;
32
37
  const bucket = requireBinding(env, "STORAGE_BUCKET", "an R2 bucket");
@@ -39,13 +44,24 @@ function defaultResolveDeps(config) {
39
44
  secretName: STORAGE_R2_SECRET
40
45
  }),
41
46
  config,
42
- ownerId: c.var.auth?.userId ?? null,
47
+ caller: await accessCaller(c.var),
48
+ ...permission ? { permission } : {},
49
+ env: c.env,
43
50
  newId: () => crypto.randomUUID(),
44
51
  newToken: () => shareToken(),
45
- now: () => /* @__PURE__ */ new Date()
52
+ now: () => /* @__PURE__ */ new Date(),
53
+ quotaLimit: memoized(() => storageCeiling(c, config))
46
54
  };
47
55
  };
48
56
  }
57
+ /** One computation per request, however many writes ask for it. */
58
+ function memoized(compute) {
59
+ let pending;
60
+ return () => {
61
+ pending ??= compute();
62
+ return pending;
63
+ };
64
+ }
49
65
  /** A 256-bit share token, base64url-encoded and unpadded so it is safe in a path segment. */
50
66
  function shareToken() {
51
67
  const bytes = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32));
@@ -70,7 +86,7 @@ const BYTE_RESPONSES = {
70
86
  function registerStorageRoutes(options) {
71
87
  const base = options.basePath ?? "/storage";
72
88
  const share = options.sharePath ?? "/s";
73
- const resolve = options.resolveDeps ?? defaultResolveDeps(options.config);
89
+ const resolve = options.resolveDeps ?? defaultResolveDeps(options.config, options.permission);
74
90
  /** Stream one object, honoring `Range` and `If-None-Match`. Shared by the id route and the share route. */
75
91
  const stream = async (c, deps, object, download) => {
76
92
  const requested = parseRangeHeader(c.req.header("range"), object.size);
@@ -99,12 +115,24 @@ function registerStorageRoutes(options) {
99
115
  return c.json(await initUpload(await resolve(c), c.req.valid("json")), 201);
100
116
  });
101
117
  app.get(base, describeRoute({
102
- summary: "List the caller's files.",
118
+ summary: "List the files the caller may see.",
119
+ description: "The caller's own files and their organization's, by path. `includePublic=true` adds every public file.",
103
120
  verification: AUTHENTICATED,
104
121
  responses: { 200: ObjectListResponse }
105
122
  }), requireAuth(), validate("query", ListObjectsQuery), async (c) => {
106
123
  return c.json(await listObjects(await resolve(c), c.req.valid("query")));
107
124
  });
125
+ app.get(`${base}/admin/usage`, describeRoute({
126
+ summary: "Count files held, per organization or per owner.",
127
+ description: "Stored files and their bytes. A per-owner read names its organization, so no figure ever spans two.",
128
+ verification: "control-plane",
129
+ scopes: [STORAGE_USAGE_READ_SCOPE],
130
+ responses: { 200: UsageResponse }
131
+ }), requireControlPlane(STORAGE_USAGE_READ_SCOPE), validate("query", UsageQuery), async (c) => {
132
+ const d1 = requireBinding(c.env, "DB", "a D1 database");
133
+ const db = storageDatabase(d1);
134
+ return c.json(await usageRollup(db, STORAGE_OBJECTS_TABLE, c.req.valid("query")));
135
+ });
108
136
  app.delete(`${base}/shares/:token`, describeRoute({
109
137
  summary: "Revoke a share link.",
110
138
  description: "Takes effect on the next fetch. Revoking twice is a no-op that keeps the first time.",
@@ -153,7 +181,7 @@ function registerStorageRoutes(options) {
153
181
  });
154
182
  app.get(`${base}/:id/url`, describeRoute({
155
183
  summary: "Mint a direct download URL.",
156
- description: "For a public file or one the caller owns. The URL cannot be revoked, so it lapses in five minutes.",
184
+ description: "For a file the caller may read. The URL cannot be revoked, so it lapses in five minutes.",
157
185
  verification: AUTHENTICATED,
158
186
  responses: { 200: PresignedUrlResponse }
159
187
  }), requireAuth(), validate("param", ObjectIdParam), async (c) => {
@@ -161,7 +189,7 @@ function registerStorageRoutes(options) {
161
189
  });
162
190
  app.get(`${base}/:id`, describeRoute({
163
191
  summary: "Read a file's bytes.",
164
- description: "Public files need no credential. A private file needs the owner's bearer token or session, and reads as missing to anyone else. Honors `Range` and `If-None-Match`.",
192
+ description: "Public files need no credential. Any other needs the bearer token or session of someone it is visible to, and reads as missing to anyone else. Honors `Range` and `If-None-Match`.",
165
193
  verification: [
166
194
  "public",
167
195
  "bearer",
@@ -36,6 +36,7 @@ export declare const CreateUploadInput: z.ZodObject<{
36
36
  contentType: z.ZodString;
37
37
  size: z.ZodNumber;
38
38
  visibility: z.ZodOptional<z.ZodEnum<{
39
+ organization: "organization";
39
40
  private: "private";
40
41
  public: "public";
41
42
  }>>;
@@ -52,6 +53,7 @@ export type CompleteUploadInput = z.output<typeof CompleteUploadInput>;
52
53
  export declare const UpdateObjectInput: z.ZodObject<{
53
54
  path: z.ZodOptional<z.ZodString>;
54
55
  visibility: z.ZodOptional<z.ZodEnum<{
56
+ organization: "organization";
55
57
  private: "private";
56
58
  public: "public";
57
59
  }>>;
@@ -67,6 +69,10 @@ export declare const CreateShareInput: z.ZodObject<{
67
69
  export type CreateShareInput = z.output<typeof CreateShareInput>;
68
70
  export declare const ListObjectsQuery: z.ZodObject<{
69
71
  prefix: z.ZodOptional<z.ZodString>;
72
+ includePublic: z.ZodOptional<z.ZodEnum<{
73
+ false: "false";
74
+ true: "true";
75
+ }>>;
70
76
  cursor: z.ZodOptional<z.ZodString>;
71
77
  limit: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
72
78
  }, z.core.$strip>;
@@ -1 +1 @@
1
- {"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../../src/http/schemas.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAkDxB;;;;;;GAMG;AACH,eAAO,MAAM,aAAa;;iBAQsC,CAAC;AACjE,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,aAAa,CAAC,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,eAAe;;iBAQiC,CAAC;AAC9D,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,eAAe,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,eAAO,MAAM,eAAe;;iBAQiE,CAAC;AAC9F,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,eAAe,CAAC,CAAC;AAE/D,eAAO,MAAM,iBAAiB;;;;;;;;i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C,MAAM,CAAC,OAAO,eAAe,CAAC,CAAC;AAE/D,eAAO,MAAM,iBAAiB;;;;;;;;;iBAqBuE,CAAC;AACtG,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAEnE,eAAO,MAAM,mBAAmB;;;;;;iBAgBqE,CAAC;AACtG,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEvE,eAAO,MAAM,iBAAiB;;;;;;;iBAmB1B,CAAC;AACL,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAEnE,eAAO,MAAM,eAAe;;iBAE8D,CAAC;AAC3F,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,eAAe,CAAC,CAAC;AAK/D,eAAO,MAAM,gBAAgB;;iBAYwC,CAAC;AACtE,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAEjE,eAAO,MAAM,gBAAgB;;;;;;;;iBA0ByF,CAAC;AACvH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
@@ -1,7 +1,7 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
- import { StorageVisibility } from "../data/storageObject.js";
4
3
  import { ReportedPart } from "../object/multipart.js";
4
+ import { StorageVisibility } from "../data/storageObject.js";
5
5
  import { MAX_OBJECT_KEY_BYTES } from "../object/key.js";
6
6
  import { z } from "zod";
7
7
  //#region src/http/schemas.ts
@@ -65,7 +65,7 @@ const CreateUploadInput = z.object({
65
65
  path: logicalPath,
66
66
  contentType: z.string().min(1).max(255).describe("The file's MIME type, as you intend it. A declaration, not a constraint: a presigned PUT cannot sign a content type, so completion replaces this with the type R2 actually stored."),
67
67
  size: z.number().int().nonnegative().describe("The exact byte count. Required: the presigned PUT signs Content-Length, and the pending row reserves these bytes against the owner's quota the moment it is written."),
68
- visibility: StorageVisibility.optional().describe("Who may read the file once stored. Omitted takes the configured default, which is private.")
68
+ visibility: StorageVisibility.optional().describe("Who may read the file once stored. Omitted takes the configured `visibility.default`, which is private; a level outside `visibility.allowed` is refused, never coerced.")
69
69
  }).describe("A request to start an upload — what the file is called, what it is, and how big it is.");
70
70
  const CompleteUploadInput = z.object({
71
71
  parts: z.array(ReportedPart).default([]).describe("Every part's number and the ETag R2 answered its PUT with. Empty for a single-PUT upload, which has no parts to assemble."),
@@ -73,7 +73,7 @@ const CompleteUploadInput = z.object({
73
73
  }).describe("A request to finalize an upload — the parts to assemble, and optionally what was sent.");
74
74
  const UpdateObjectInput = z.object({
75
75
  path: logicalPath.optional(),
76
- visibility: StorageVisibility.optional().describe("Change who may read the file. Takes effect on the next request.")
76
+ visibility: StorageVisibility.optional().describe("Change who may read the file. Takes effect on the next request. A level outside `visibility.allowed` is refused.")
77
77
  }).describe("A rename, a visibility change, or both. At least one field must be present.").check((ctx) => {
78
78
  if (ctx.value.path === void 0 && ctx.value.visibility === void 0) ctx.issues.push({
79
79
  code: "custom",
@@ -85,9 +85,10 @@ const UpdateObjectInput = z.object({
85
85
  const CopyObjectInput = z.object({ path: logicalPath }).describe("A server-side copy — the new file's logical path. The bytes never leave R2.");
86
86
  const CreateShareInput = z.object({ expiresInSeconds: z.number().int().positive().max(31536e3).optional().describe("How long the link works, in seconds. Omitted never expires — revocation is then the only way to end it, which is a deliberate choice rather than a default.") }).describe("A request to mint a revocable share link for one file.");
87
87
  const ListObjectsQuery = z.object({
88
- prefix: z.string().max(MAX_OBJECT_KEY_BYTES).optional().describe("Only files whose logical path starts with this. Omitted lists everything you own."),
88
+ prefix: z.string().max(MAX_OBJECT_KEY_BYTES).optional().describe("Only files whose logical path starts with this. Omitted lists everything you may see."),
89
+ includePublic: z.enum(["true", "false"]).optional().describe("`true` adds every public file to your own and your organization's. Off by default, so nobody receives what they did not ask for."),
89
90
  cursor: z.string().min(1).optional().describe("The `cursor` a previous page returned. Opaque — pass it back unchanged to advance."),
90
91
  limit: z.coerce.number().int().min(1).max(100).optional().describe("Files per page. Defaults to 50, capped at 100 so one request cannot scan an entire owner's files.")
91
- }).describe("Query parameters for the owner's file listing.");
92
+ }).describe("Query parameters for the file listing: your own files, your organization's, and public ones on request.");
92
93
  //#endregion
93
94
  export { CompleteUploadInput, CopyObjectInput, CreateShareInput, CreateUploadInput, ListObjectsQuery, ObjectIdParam, ObjectReadQuery, ShareTokenParam, UpdateObjectInput };
@@ -0,0 +1,25 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import type { AdminRoute } from "@pithy-sh/core/src/controlPlane/discovery/adminRoute";
4
+ import type { ControlPlaneScope } from "@pithy-sh/core/src/controlPlane/scope/scope";
5
+ /**
6
+ * Storage's control-plane scope, and the admin surface a manifest advertises.
7
+ *
8
+ * **This module imports types and nothing else**, because a management client in a browser reads the scope
9
+ * names to render what a connection may do, and `tooling/browser-scopes` compiles every scope the kit
10
+ * declares against the DOM lib alone.
11
+ *
12
+ * **One scope, a read, and it is numbers.** Files and bytes held, per owner and per organization — never a
13
+ * path, a key or a link. A per-owner figure is always one organization's, so a tool holding this scope
14
+ * still cannot learn that a person holds an account in two of them.
15
+ */
16
+ /** Files and bytes held, per organization and per owner within one. */
17
+ export declare const STORAGE_USAGE_READ_SCOPE: ControlPlaneScope;
18
+ /** Every control-plane scope this capability defines. */
19
+ export declare const STORAGE_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[];
20
+ /**
21
+ * The admin routes this capability advertises on `GET /control-plane/manifest`, built from the **resolved**
22
+ * mount path, never the default.
23
+ */
24
+ export declare function storageAdminRoutes(basePath: string): AdminRoute[];
25
+ //# sourceMappingURL=scopes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scopes.d.ts","sourceRoot":"","sources":["../../src/http/scopes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sDAAsD,CAAC;AACvF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AAErF;;;;;;;;;;GAUG;AAEH,uEAAuE;AACvE,eAAO,MAAM,wBAAwB,EAAE,iBAAwC,CAAC;AAEhF,yDAAyD;AACzD,eAAO,MAAM,4BAA4B,EAAE,SAAS,iBAAiB,EAA+B,CAAC;AAErG;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,EAAE,CAUjE"}
@@ -0,0 +1,32 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ //#region src/http/scopes.ts
4
+ /**
5
+ * Storage's control-plane scope, and the admin surface a manifest advertises.
6
+ *
7
+ * **This module imports types and nothing else**, because a management client in a browser reads the scope
8
+ * names to render what a connection may do, and `tooling/browser-scopes` compiles every scope the kit
9
+ * declares against the DOM lib alone.
10
+ *
11
+ * **One scope, a read, and it is numbers.** Files and bytes held, per owner and per organization — never a
12
+ * path, a key or a link. A per-owner figure is always one organization's, so a tool holding this scope
13
+ * still cannot learn that a person holds an account in two of them.
14
+ */
15
+ /** Files and bytes held, per organization and per owner within one. */
16
+ const STORAGE_USAGE_READ_SCOPE = "storage:usage:read";
17
+ /** Every control-plane scope this capability defines. */
18
+ const STORAGE_CONTROL_PLANE_SCOPES = [STORAGE_USAGE_READ_SCOPE];
19
+ /**
20
+ * The admin routes this capability advertises on `GET /control-plane/manifest`, built from the **resolved**
21
+ * mount path, never the default.
22
+ */
23
+ function storageAdminRoutes(basePath) {
24
+ return [{
25
+ method: "GET",
26
+ path: `${basePath}/admin/usage`,
27
+ scope: STORAGE_USAGE_READ_SCOPE,
28
+ summary: "Files and bytes held, per organization or per owner within one organization. Never across organizations."
29
+ }];
30
+ }
31
+ //#endregion
32
+ export { STORAGE_CONTROL_PLANE_SCOPES, STORAGE_USAGE_READ_SCOPE, storageAdminRoutes };
@@ -86,8 +86,11 @@ export interface ServeOptions {
86
86
  metadata: ObjectMetadata;
87
87
  /** The logical path — its last segment becomes the download filename. */
88
88
  path: string;
89
- /** Whether anyone may read the object, which decides whether a shared cache may hold it. */
90
- visibility: "private" | "public";
89
+ /**
90
+ * Who may read the object, which decides whether a shared cache may hold it: only a public object may
91
+ * be cached publicly, so an organization's file is as uncacheable as a private one.
92
+ */
93
+ visibility: "private" | "organization" | "public";
91
94
  /** Serve as a download (`attachment`) rather than inline. */
92
95
  download?: boolean;
93
96
  }
@@ -1 +1 @@
1
- {"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../../src/http/serve.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH,iEAAiE;AACjE,MAAM,MAAM,YAAY;AACtB,qDAAqD;AACnD;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;AAClB,mDAAmD;GACjD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE;AACnD,kEAAkE;GAChE;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,CAAC;AAK9B;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,YAAY,CAwBrG;AAED,4FAA4F;AAC5F,wBAAgB,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,WAAW,GAAG,SAAS,CAE5E;AAED;;;;GAIG;AACH,wBAAgB,eA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ANGED
@@ -14,6 +14,8 @@ export { maxObjectBytes, StorageConfig, type StorageConfigInput, StorageQuota }
14
14
  export { StorageShare } from "./data/share";
15
15
  export { StorageObject, StorageObjectStatus, StorageVisibility } from "./data/storageObject";
16
16
  export { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, type StorageDatabase, storageDatabase } from "./data/tables";
17
+ export { STORAGE_MANAGE_POWER } from "./http/handlers";
18
+ export { STORAGE_USAGE_READ_SCOPE } from "./http/scopes";
17
19
  export { deriveObjectKey, isDerivedObjectKey, OBJECT_KEY_PREFIX } from "./object/key";
18
20
  export { collectParts, MultipartPlan, needsMultipart, PartPlan, planMultipart, ReportedPart } from "./object/multipart";
19
21
  export { ObjectListing, ObjectMetadata, type ObjectStore, type ObjectStoreOptions, objectStore, type PresignedObjects, } from "./object/store";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AAEH,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,OAAO,GACR,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,kBAAkB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACvG,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC7F,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,KAAK,eAAe,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACnH,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,QAAQ,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC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EACb,cAAc,EACd,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,WAAW,EACX,KAAK,gBAAgB,GACtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC7F,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -1,14 +1,16 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
- import { StorageObject, StorageObjectStatus, StorageVisibility } from "./data/storageObject.js";
4
3
  import { MultipartPlan, PartPlan, ReportedPart, collectParts, needsMultipart, planMultipart } from "./object/multipart.js";
5
4
  import { StorageConfig, StorageQuota, maxObjectBytes } from "./config/config.js";
6
5
  import { StorageShare } from "./data/share.js";
6
+ import { StorageObject, StorageObjectStatus, StorageVisibility } from "./data/storageObject.js";
7
7
  import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, storageDatabase } from "./data/tables.js";
8
+ import { OBJECT_KEY_PREFIX, deriveObjectKey, isDerivedObjectKey } from "./object/key.js";
9
+ import { STORAGE_MANAGE_POWER } from "./http/handlers.js";
8
10
  import { R2StorageCredentials, STORAGE_R2_SECRET, r2CredentialsRegistry, storageSecretsRegistry } from "./secret/registry.js";
9
11
  import { ObjectListing, ObjectMetadata, objectStore } from "./object/store.js";
10
- import { OBJECT_KEY_PREFIX, deriveObjectKey, isDerivedObjectKey } from "./object/key.js";
12
+ import { STORAGE_USAGE_READ_SCOPE } from "./http/scopes.js";
11
13
  import { STORAGE_CAPABILITY, StorageSweepParams, storageWorkflows } from "./workflows/specs.js";
12
14
  import { STORAGE_MIGRATION_ORDER, isStorageCapability, storage } from "./capability.js";
13
15
  import { sweepStorage } from "./workflows/sweep.js";
14
- export { MultipartPlan, OBJECT_KEY_PREFIX, ObjectListing, ObjectMetadata, PartPlan, R2StorageCredentials, ReportedPart, STORAGE_CAPABILITY, STORAGE_MIGRATION_ORDER, STORAGE_OBJECTS_TABLE, STORAGE_R2_SECRET, STORAGE_SHARES_TABLE, StorageConfig, StorageObject, StorageObjectStatus, StorageQuota, StorageShare, StorageSweepParams, StorageVisibility, collectParts, deriveObjectKey, isDerivedObjectKey, isStorageCapability, maxObjectBytes, needsMultipart, objectStore, planMultipart, r2CredentialsRegistry, storage, storageDatabase, storageSecretsRegistry, storageWorkflows, sweepStorage };
16
+ export { MultipartPlan, OBJECT_KEY_PREFIX, ObjectListing, ObjectMetadata, PartPlan, R2StorageCredentials, ReportedPart, STORAGE_CAPABILITY, STORAGE_MANAGE_POWER, STORAGE_MIGRATION_ORDER, STORAGE_OBJECTS_TABLE, STORAGE_R2_SECRET, STORAGE_SHARES_TABLE, STORAGE_USAGE_READ_SCOPE, StorageConfig, StorageObject, StorageObjectStatus, StorageQuota, StorageShare, StorageSweepParams, StorageVisibility, collectParts, deriveObjectKey, isDerivedObjectKey, isStorageCapability, maxObjectBytes, needsMultipart, objectStore, planMultipart, r2CredentialsRegistry, storage, storageDatabase, storageSecretsRegistry, storageWorkflows, sweepStorage };
@@ -0,0 +1,5 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import type { Migration } from "kysely/migration";
4
+ export declare const storage_0002_ownership: Migration;
5
+ //# sourceMappingURL=0002_ownership.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"0002_ownership.d.ts","sourceRoot":"","sources":["../../src/migrations/0002_ownership.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAgKlD,eAAO,MAAM,sBAAsB,EAAE,SAWpC,CAAC"}
@@ -0,0 +1,114 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import { sql } from "kysely";
4
+ //#region src/migrations/0002_ownership.ts
5
+ /**
6
+ * The organization a stored file belongs to, and the middle visibility level.
7
+ *
8
+ * **`organizationId` is added beside the existing `ownerId`**, nullable and in the same terms: the
9
+ * organization the owner was acting for when the file was written, or null when none was. Existing rows
10
+ * get null, which is true of them — nothing recorded one.
11
+ *
12
+ * **`visibility` gains `organization`, and in SQLite a `CHECK` cannot be altered.** So the objects table is
13
+ * rebuilt: a new table with the new constraint, every row copied across, the old one dropped, the new one
14
+ * renamed into its place. The shares table is rebuilt beside it, in the order that keeps its foreign key
15
+ * honest — a `DROP TABLE` of a parent runs an implicit `DELETE`, and with `ON DELETE CASCADE` that would
16
+ * take every share link with it. So the new shares table points at the new objects table from the start,
17
+ * the old shares go before the old objects, nothing references the old objects when they are dropped, and
18
+ * the rename carries the foreign key across to the new name. Not one share is lost, and the tests count.
19
+ *
20
+ * **Indexed the way the reads ask.** The existing `(ownerId, path)` and `(status, createdAt)` indexes come
21
+ * back with the table, and two join them: `(ownerId, createdAt)` and `(organizationId, createdAt)`, so
22
+ * *this owner's files over a window* and *this organization's* are each one index range.
23
+ *
24
+ * `down` is the tested inverse: the same rebuild the other way. A row at `organization` goes back to
25
+ * `private` — the narrower level, so an inverse can never publish a file — and `organizationId` is
26
+ * dropped.
27
+ */
28
+ const OBJECTS = "pithyStorageObjects";
29
+ const OBJECTS_NEXT = "pithyStorageObjectsNext";
30
+ const SHARES = "pithyStorageShares";
31
+ const SHARES_NEXT = "pithyStorageSharesNext";
32
+ /** The columns both shapes of the objects table share, in the order they were declared. */
33
+ const SHARED_COLUMNS = [
34
+ "id",
35
+ "key",
36
+ "path",
37
+ "ownerId",
38
+ "contentType",
39
+ "size",
40
+ "visibility",
41
+ "checksum",
42
+ "status",
43
+ "uploadId",
44
+ "createdAt",
45
+ "updatedAt"
46
+ ];
47
+ /** Every column of the shares table. */
48
+ const SHARE_COLUMNS = [
49
+ "token",
50
+ "objectId",
51
+ "expiresAt",
52
+ "revokedAt",
53
+ "createdAt"
54
+ ];
55
+ /** A comma-separated list of column references, snake-cased by the `CamelCasePlugin`. */
56
+ function columnList(columns) {
57
+ return sql.join(columns.map((column) => sql.ref(column)));
58
+ }
59
+ /** Create an objects table under `name`, with or without the organization column and level. */
60
+ async function createObjects(db, name, withOrganization) {
61
+ let table = db.schema.createTable(name).addColumn("id", "text", (c) => c.primaryKey()).addColumn("key", "text", (c) => c.notNull().unique()).addColumn("path", "text", (c) => c.notNull()).addColumn("ownerId", "text");
62
+ if (withOrganization) table = table.addColumn("organizationId", "text");
63
+ await table.addColumn("contentType", "text", (c) => c.notNull()).addColumn("size", "integer").addColumn("visibility", "text", (c) => c.notNull().defaultTo("private")).addColumn("checksum", "text").addColumn("status", "text", (c) => c.notNull().defaultTo("pending")).addColumn("uploadId", "text").addColumn("createdAt", "integer", (c) => c.notNull()).addColumn("updatedAt", "integer", (c) => c.notNull()).addCheckConstraint("pithyStorageObjectsVisibility", withOrganization ? sql`visibility IN ('private', 'organization', 'public')` : sql`visibility IN ('private', 'public')`).addCheckConstraint("pithyStorageObjectsStatus", sql`status IN ('pending', 'stored', 'failed')`).execute();
64
+ }
65
+ /** Create a shares table under `name`, pointing at the objects table `parent`. */
66
+ async function createShares(db, name, parent) {
67
+ await db.schema.createTable(name).addColumn("token", "text", (c) => c.primaryKey()).addColumn("objectId", "text", (c) => c.notNull()).addColumn("expiresAt", "integer").addColumn("revokedAt", "integer").addColumn("createdAt", "integer", (c) => c.notNull()).addForeignKeyConstraint("pithyStorageSharesObjectFk", ["objectId"], parent, ["id"], (fk) => fk.onDelete("cascade")).execute();
68
+ }
69
+ /** The indexes the objects table carries in both shapes. */
70
+ async function createObjectIndexes(db, withOrganization) {
71
+ await db.schema.createIndex("pithyStorageObjectsOwnerPathIdx").on(OBJECTS).columns(["ownerId", "path"]).execute();
72
+ await db.schema.createIndex("pithyStorageObjectsStatusIdx").on(OBJECTS).columns(["status", "createdAt"]).execute();
73
+ await db.schema.createIndex("pithyStorageSharesObjectIdx").on(SHARES).columns(["objectId"]).execute();
74
+ if (!withOrganization) return;
75
+ await db.schema.createIndex("pithyStorageObjectsOwnerCreatedIdx").on(OBJECTS).columns(["ownerId", "createdAt"]).execute();
76
+ await db.schema.createIndex("pithyStorageObjectsOrganizationCreatedIdx").on(OBJECTS).columns(["organizationId", "createdAt"]).execute();
77
+ }
78
+ /** Drop the indexes the objects table carries in one of its two shapes — the inverse of `createObjectIndexes`. */
79
+ async function dropObjectIndexes(db, withOrganization) {
80
+ if (withOrganization) {
81
+ await db.schema.dropIndex("pithyStorageObjectsOrganizationCreatedIdx").execute();
82
+ await db.schema.dropIndex("pithyStorageObjectsOwnerCreatedIdx").execute();
83
+ }
84
+ await db.schema.dropIndex("pithyStorageSharesObjectIdx").execute();
85
+ await db.schema.dropIndex("pithyStorageObjectsStatusIdx").execute();
86
+ await db.schema.dropIndex("pithyStorageObjectsOwnerPathIdx").execute();
87
+ }
88
+ /**
89
+ * Rebuild both tables into a new shape, copying every row. `visibility` is the expression the copied
90
+ * visibility is read through — the column itself going up, a narrowing going down.
91
+ */
92
+ async function rebuild(db, withOrganization, visibility) {
93
+ await createObjects(db, OBJECTS_NEXT, withOrganization);
94
+ const copied = SHARED_COLUMNS.filter((column) => column !== "visibility");
95
+ await sql`insert into ${sql.table(OBJECTS_NEXT)} (${columnList(copied)}, ${sql.ref("visibility")}) select ${columnList(copied)}, ${visibility} from ${sql.table(OBJECTS)}`.execute(db);
96
+ await createShares(db, SHARES_NEXT, OBJECTS_NEXT);
97
+ await sql`insert into ${sql.table(SHARES_NEXT)} (${columnList(SHARE_COLUMNS)}) select ${columnList(SHARE_COLUMNS)} from ${sql.table(SHARES)}`.execute(db);
98
+ await dropObjectIndexes(db, !withOrganization);
99
+ await db.schema.dropTable(SHARES).execute();
100
+ await db.schema.dropTable(OBJECTS).execute();
101
+ await db.schema.alterTable(OBJECTS_NEXT).renameTo(OBJECTS).execute();
102
+ await db.schema.alterTable(SHARES_NEXT).renameTo(SHARES).execute();
103
+ await createObjectIndexes(db, withOrganization);
104
+ }
105
+ const storage_0002_ownership = {
106
+ up: async (db) => {
107
+ await rebuild(db, true, sql.ref("visibility"));
108
+ },
109
+ down: async (db) => {
110
+ await rebuild(db, false, sql`case when ${sql.ref("visibility")} = 'organization' then 'private' else ${sql.ref("visibility")} end`);
111
+ }
112
+ };
113
+ //#endregion
114
+ export { storage_0002_ownership };
@@ -17,7 +17,8 @@ import type { PresignedObjects } from "./store";
17
17
  export declare function r2Manager(manager: CloudflareR2Manager): PresignedObjects;
18
18
  /**
19
19
  * The default port factory: build an R2 manager from the resolved credential bundle and adapt it.
20
- * This is what `objectStore` calls when no `presigned` seam is injected.
20
+ * This is what `objectStore` calls when no `presigned` seam is injected. S3 only: the bundle carries no
21
+ * Cloudflare API token, and nothing here calls the Cloudflare API (#728).
21
22
  */
22
23
  export declare function r2Presigned(credentials: R2StorageCredentials): PresignedObjects;
23
24
  //# sourceMappingURL=cloudflare.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cloudflare.d.ts","sourceRoot":"","sources":["../../src/object/cloudflare.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AAC5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGhD;;;;;;;;;GASG;AAEH,gFAAgF;AAChF,wBAAgB,SAAS,CAAC,OAAO,EAAE,mBAAmB,GAAG,gBAAgB,CAmBxE;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,WAAW,EAAE,oBAAoB,GAAG,gBAAgB,CAU/E"}
1
+ {"version":3,"file":"cloudflare.d.ts","sourceRoot":"","sources":["../../src/object/cloudflare.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AAC5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGhD;;;;;;;;;GASG;AAEH,gFAAgF;AAChF,wBAAgB,SAAS,CAAC,OAAO,EAAE,mBAAmB,GAAG,gBAAgB,CAmBxE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,WAAW,EAAE,oBAAoB,GAAG,gBAAgB,CAS/E"}
@@ -33,11 +33,11 @@ function r2Manager(manager) {
33
33
  }
34
34
  /**
35
35
  * The default port factory: build an R2 manager from the resolved credential bundle and adapt it.
36
- * This is what `objectStore` calls when no `presigned` seam is injected.
36
+ * This is what `objectStore` calls when no `presigned` seam is injected. S3 only: the bundle carries no
37
+ * Cloudflare API token, and nothing here calls the Cloudflare API (#728).
37
38
  */
38
39
  function r2Presigned(credentials) {
39
40
  return r2Manager(new CloudflareR2Manager({
40
- apiToken: credentials.apiToken,
41
41
  accountId: credentials.accountId,
42
42
  accessKeyId: credentials.accessKeyId,
43
43
  secretAccessKey: credentials.secretAccessKey,
@@ -24,7 +24,7 @@ import { type DeprovisionTarget, type ManagedEnvironment } from "@pithy-sh/secre
24
24
  * bucket, create-then-update for the secret, and a deploy that overwrites. Re-running is a no-op.
25
25
  *
26
26
  * **What it does not do.** Cloudflare exposes no API for minting an R2 S3 access-key pair, so nothing
27
- * here mints one. The pair is supplied by the operator — flags, or `R2_CREDENTIALS` in `.dev.vars` —
27
+ * here mints one. The pair is supplied by the operator — flags, or `R2_CREDENTIALS` in the account config —
28
28
  * and written into the secret as given. That is a real limitation, and the manifest says so rather
29
29
  * than promising a mint that does not happen.
30
30
  */
@@ -63,6 +63,13 @@ export declare function storageBucketName(project: string, env: ManagedEnvironme
63
63
  * cannot be renamed once a deploy or a `service` binding points at it.
64
64
  */
65
65
  export declare function storageWorkerName(project: string, env: ManagedEnvironment): string;
66
+ /**
67
+ * The Workflows the sweep worker is deployed with in one environment — `<project>-<env>-storage-sweep`.
68
+ * Derived from the specs by the call `resolveStorageConfig` deploys with, so teardown deletes the names
69
+ * provision created and no others (#789). Deleting a Worker does not delete the Workflows registered
70
+ * against it.
71
+ */
72
+ export declare function storageWorkflowNames(project: string, env: ManagedEnvironment): string[];
66
73
  /** The provisioned resources an environment's worker and secret are wired to. */
67
74
  export interface StorageResources {
68
75
  /** The R2 bucket objects live in. */
@@ -114,6 +121,11 @@ export interface StorageProvisionResult {
114
121
  export declare function provisionStorage(provisioner: StorageProvisioner, environments: DeclaredEnvironments | readonly string[]): Promise<StorageProvisionResult>;
115
122
  /** The teardown seam — the inverse of {@link StorageProvisioner}. Every step idempotent. */
116
123
  export interface StorageDeprovisioner {
124
+ /**
125
+ * Delete this environment's sweep Workflows, by {@link storageWorkflowNames}. Idempotent (a Workflow
126
+ * already gone is a no-op). Deleting the worker leaves them registered, so they go first (#789).
127
+ */
128
+ deleteWorkflows(env: ManagedEnvironment): Promise<void>;
117
129
  /** Delete this environment's sweep worker. Idempotent (a missing worker is a no-op). */
118
130
  deleteWorker(env: ManagedEnvironment): Promise<void>;
119
131
  /**
@@ -137,7 +149,7 @@ export interface StorageDeprovisionOptions {
137
149
  }
138
150
  /**
139
151
  * Tear down **one named environment's** storage infrastructure, reversing {@link provisionStorage} for it:
140
- * its sweep worker first (it binds the bucket), then — only when `deleteStorage` is set — its bucket and
152
+ * its sweep Workflows, then its sweep worker (it binds the bucket), then — only when `deleteStorage` is set — its bucket and
141
153
  * everything in it. Idempotent end to end.
142
154
  *
143
155
  * The environment is resolved by `@pithy-sh/secrets`' {@link deprovisionTarget}, before anything is deleted:
@@ -1 +1 @@
1
- {"version":3,"file":"provisionStorage.d.ts","sourceRoot":"","sources":["../../src/provision/provisionStorage.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAElF,OAAO,EACL,KAAK,iBAAiB,EAEtB,KA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oBAAoB;IACnC,wFAAwF;IACxF,YAAY,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD;;;;;;;;OAQG;IACH,YAAY,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtD;AAED,qGAAqG;AACrG,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CACtC,aAAa,EAAE,oBAAoB,EACnC,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE,yBAA8B,GACtC,OAAO,CAAC;IAAE,GAAG,EAAE,kBAAkB,CAAA;CAAE,CAAC,CAKtC"}
1
+ {"version":3,"file":"provisionStorage.d.ts","sourceRoot":"","sources":["../../src/provision/provisionStorage.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAGlF,OAAO,EACL,KAAK,iBAAiB,EAEtB,KAAK,kBAAkB,EAExB,MAAM,6BAA6B,CAAC;AAGrC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,kBAAkB,GAAG,MAAM,CAElF;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,kBAAkB,GAAG,MAAM,CAElF;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,kBAAkB,GAAG,MAAM,EAAE,CAIvF;AAED,iFAAiF;AACjF,MAAM,WAAW,gBAAgB;IAC/B,qCAAqC;IACrC,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,uEAAuE;AACvE,MAAM,WAAW,kBAAkB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C,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE,yBAA8B,GACtC,OAAO,CAAC;IAAE,GAAG,EAAE,kBAAkB,CAAA;CAAE,CAAC,CAMtC"}
@@ -1,8 +1,9 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
- import { STORAGE_CAPABILITY } from "../workflows/specs.js";
3
+ import { STORAGE_CAPABILITY, storageWorkflowRegistry } from "../workflows/specs.js";
4
4
  import { InternalError } from "@pithy-sh/core/src/error/pithyError";
5
5
  import { resourceNames } from "@pithy-sh/core/src/naming/resourceNames";
6
+ import { hostWorkflowsFor } from "@pithy-sh/core/src/workflow/host";
6
7
  import { deprovisionTarget, managedEnvironments } from "@pithy-sh/secrets/src/scope";
7
8
  //#region src/provision/provisionStorage.ts
8
9
  /**
@@ -27,7 +28,7 @@ import { deprovisionTarget, managedEnvironments } from "@pithy-sh/secrets/src/sc
27
28
  * bucket, create-then-update for the secret, and a deploy that overwrites. Re-running is a no-op.
28
29
  *
29
30
  * **What it does not do.** Cloudflare exposes no API for minting an R2 S3 access-key pair, so nothing
30
- * here mints one. The pair is supplied by the operator — flags, or `R2_CREDENTIALS` in `.dev.vars` —
31
+ * here mints one. The pair is supplied by the operator — flags, or `R2_CREDENTIALS` in the account config —
31
32
  * and written into the secret as given. That is a real limitation, and the manifest says so rather
32
33
  * than promising a mint that does not happen.
33
34
  */
@@ -71,6 +72,19 @@ function storageWorkerName(project, env) {
71
72
  return resourceNames(project).env(env).worker(STORAGE_CAPABILITY);
72
73
  }
73
74
  /**
75
+ * The Workflows the sweep worker is deployed with in one environment — `<project>-<env>-storage-sweep`.
76
+ * Derived from the specs by the call `resolveStorageConfig` deploys with, so teardown deletes the names
77
+ * provision created and no others (#789). Deleting a Worker does not delete the Workflows registered
78
+ * against it.
79
+ */
80
+ function storageWorkflowNames(project, env) {
81
+ return hostWorkflowsFor(storageWorkflowRegistry, {
82
+ project,
83
+ capability: STORAGE_CAPABILITY,
84
+ env
85
+ }).workflows.map((workflow) => workflow.name);
86
+ }
87
+ /**
74
88
  * Provision the storage infrastructure: check the account, create every environment's bucket, write
75
89
  * every environment's credentials, then deploy every environment's sweep worker.
76
90
  *
@@ -106,7 +120,7 @@ function resourcesFor(resources, env) {
106
120
  }
107
121
  /**
108
122
  * Tear down **one named environment's** storage infrastructure, reversing {@link provisionStorage} for it:
109
- * its sweep worker first (it binds the bucket), then — only when `deleteStorage` is set — its bucket and
123
+ * its sweep Workflows, then its sweep worker (it binds the bucket), then — only when `deleteStorage` is set — its bucket and
110
124
  * everything in it. Idempotent end to end.
111
125
  *
112
126
  * The environment is resolved by `@pithy-sh/secrets`' {@link deprovisionTarget}, before anything is deleted:
@@ -116,9 +130,10 @@ function resourcesFor(resources, env) {
116
130
  */
117
131
  async function deprovisionStorage(deprovisioner, target, options = {}) {
118
132
  const env = deprovisionTarget(target);
133
+ await deprovisioner.deleteWorkflows(env);
119
134
  await deprovisioner.deleteWorker(env);
120
135
  if (options.deleteStorage) await deprovisioner.deleteBucket(env);
121
136
  return { env };
122
137
  }
123
138
  //#endregion
124
- export { deprovisionStorage, provisionStorage, storageBucketName, storageWorkerName };
139
+ export { deprovisionStorage, provisionStorage, storageBucketName, storageWorkerName, storageWorkflowNames };
@@ -1,8 +1,8 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
3
  import { STORAGE_CAPABILITY, storageWorkflowRegistry } from "../workflows/specs.js";
4
- import { hostWorkflowsFor, resolveWorkflowHost } from "@pithy-sh/core/src/workflow/host";
5
4
  import { masterKeySecretName } from "@pithy-sh/secrets/src/provision/provisionSecrets";
5
+ import { hostWorkflowsFor, resolveWorkflowHost } from "@pithy-sh/core/src/workflow/host";
6
6
  //#region src/provision/resolveStorageConfig.ts
7
7
  /** Fill the template for one environment. */
8
8
  function resolveStorageConfig(template, params) {