@pithy-sh/storage 0.5.0 → 0.6.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.
@@ -1 +1 @@
1
- {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAGvE,OAAO,EAAE,KAAK,UAAU,EAAoB,MAAM,0CAA0C,CAAC;AAE7F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAG3E,OAAO,EAA+B,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAWtG,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAE3C,MAAM,MAAM,cAAc,GAAG,kBAAkB,GAAG;IAChD,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,UAAU,CAAC,EAAE,cAAc,CAAC;IAC5B,0GAA0G;IAC1G,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,iBACf,SAAQ,UAAU,CAAC,eAAe,EAAE,kBAAkB,EAAE,SAAS,EAAE,OAAO,gBAAgB,CAAC;IAC3F,mCAAmC;IACnC,aAAa,EAAE,aAAa,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,iBAAiB,CA0EvE;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,IAAI,iBAAiB,CAE3F"}
1
+ {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAGvE,OAAO,EAAE,KAAK,UAAU,EAAoB,MAAM,0CAA0C,CAAC;AAE7F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAG3E,OAAO,EAA+B,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAYtG,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAE3C,MAAM,MAAM,cAAc,GAAG,kBAAkB,GAAG;IAChD,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,UAAU,CAAC,EAAE,cAAc,CAAC;IAC5B,0GAA0G;IAC1G,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,iBACf,SAAQ,UAAU,CAAC,eAAe,EAAE,kBAAkB,EAAE,SAAS,EAAE,OAAO,gBAAgB,CAAC;IAC3F,mCAAmC;IACnC,aAAa,EAAE,aAAa,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,iBAAiB,CA6EvE;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,IAAI,iBAAiB,CAE3F"}
@@ -8,6 +8,7 @@ import { storageAdminRoutes } from "./http/scopes.js";
8
8
  import { registerStorageRoutes } from "./http/routes.js";
9
9
  import { storage_0001_objects } from "./migrations/0001_objects.js";
10
10
  import { storage_0002_ownership } from "./migrations/0002_ownership.js";
11
+ import { storageOrganizationPurge } from "./purge/contribution.js";
11
12
  import { bundleTokenCheck } from "./secret/bundleToken.js";
12
13
  import { storageExampleSeed } from "./seeds/example.js";
13
14
  import { PACKAGE_NAME, PACKAGE_VERSION } from "./version.generated.js";
@@ -66,6 +67,7 @@ function storage(options = {}) {
66
67
  dependsOn: ["secrets"],
67
68
  secretRegistry: storageSecretsRegistry,
68
69
  workflows: storageWorkflows,
70
+ purge: [storageOrganizationPurge()],
69
71
  requiredBindings,
70
72
  config: StorageConfig,
71
73
  powers: [STORAGE_MANAGE_POWER],
@@ -0,0 +1,24 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import type { PurgeContribution } from "@pithy-sh/core/src/capability/purge";
4
+ /**
5
+ * What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
6
+ * organization's, with its bytes in R2 and every share link to it.
7
+ *
8
+ * **The bytes go first, then the shares, then the row** — the row is the only thing that names the key, so a row
9
+ * deleted ahead of its object would leave bytes nothing can find. An R2 delete that fails throws, and this step
10
+ * fails and retries alone, with the rows not yet reached still naming what is left. An in-flight multipart upload
11
+ * is aborted best-effort, as the sweep aborts one: the abort is an S3-protocol call on the R2 credentials, and
12
+ * parts it cannot reach expire under the bucket's own multipart lifecycle.
13
+ *
14
+ * **Idempotent**, as the contract requires: R2 deletes a missing key without complaint, and an organization with
15
+ * no files is a step that finds nothing.
16
+ *
17
+ * Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
18
+ * one written for another organization, or for none, is untouched.
19
+ */
20
+ /** The one sentence the purge records with this step. */
21
+ export declare const STORAGE_ORGANIZATION_PURGE_SUMMARY = "Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
22
+ /** The organization contribution `storage()` declares. */
23
+ export declare function storageOrganizationPurge(): PurgeContribution;
24
+ //# sourceMappingURL=contribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contribution.d.ts","sourceRoot":"","sources":["../../src/purge/contribution.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AAO7E;;;;;;;;;;;;;;;GAeG;AAEH,yDAAyD;AACzD,eAAO,MAAM,kCAAkC,gGACgD,CAAC;AAkBhG,0DAA0D;AAC1D,wBAAgB,wBAAwB,IAAI,iBAAiB,CA0B5D"}
@@ -0,0 +1,69 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, storageDatabase } from "../data/tables.js";
4
+ import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry.js";
5
+ import { objectStore } from "../object/store.js";
6
+ import { InternalError } from "@pithy-sh/core/src/error/pithyError";
7
+ //#region src/purge/contribution.ts
8
+ /**
9
+ * What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
10
+ * organization's, with its bytes in R2 and every share link to it.
11
+ *
12
+ * **The bytes go first, then the shares, then the row** — the row is the only thing that names the key, so a row
13
+ * deleted ahead of its object would leave bytes nothing can find. An R2 delete that fails throws, and this step
14
+ * fails and retries alone, with the rows not yet reached still naming what is left. An in-flight multipart upload
15
+ * is aborted best-effort, as the sweep aborts one: the abort is an S3-protocol call on the R2 credentials, and
16
+ * parts it cannot reach expire under the bucket's own multipart lifecycle.
17
+ *
18
+ * **Idempotent**, as the contract requires: R2 deletes a missing key without complaint, and an organization with
19
+ * no files is a step that finds nothing.
20
+ *
21
+ * Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
22
+ * one written for another organization, or for none, is untouched.
23
+ */
24
+ /** The one sentence the purge records with this step. */
25
+ const STORAGE_ORGANIZATION_PURGE_SUMMARY = "Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
26
+ /** How many files one read takes, so an organization with many files is erased in bounded statements. */
27
+ const PAGE = 100;
28
+ /** One binding off a run's env, or a wiring fault naming it. */
29
+ function bindingOf(env, name, kind) {
30
+ const binding = env[name];
31
+ if (typeof binding !== "object" || binding === null) throw new InternalError({
32
+ message: "Storage is not bound, so this purge step could not run.",
33
+ action: `Bind ${kind} named ${name} to the Worker that runs the purge.`,
34
+ detail: `storage organization purge contribution found no ${name} binding on the env`
35
+ });
36
+ return binding;
37
+ }
38
+ /** The organization contribution `storage()` declares. */
39
+ function storageOrganizationPurge() {
40
+ return {
41
+ subject: "organization",
42
+ summary: STORAGE_ORGANIZATION_PURGE_SUMMARY,
43
+ erase: async ({ subject, env }) => {
44
+ const db = storageDatabase(bindingOf(env, "DB", "a D1 database"));
45
+ const bucket = bindingOf(env, STORAGE_BUCKET_BINDING, "an R2 bucket");
46
+ const store = objectStore({
47
+ bucket,
48
+ env,
49
+ secretName: STORAGE_R2_SECRET
50
+ });
51
+ for (;;) {
52
+ const page = await db.selectFrom(STORAGE_OBJECTS_TABLE).select([
53
+ "id",
54
+ "key",
55
+ "uploadId"
56
+ ]).where("organizationId", "=", subject.id).limit(PAGE).execute();
57
+ if (page.length === 0) return;
58
+ for (const object of page) {
59
+ if (object.uploadId) await store.abortMultipart(object.key, object.uploadId).catch(() => {});
60
+ await store.delete(object.key);
61
+ await db.deleteFrom(STORAGE_SHARES_TABLE).where("objectId", "=", object.id).execute();
62
+ await db.deleteFrom(STORAGE_OBJECTS_TABLE).where("id", "=", object.id).execute();
63
+ }
64
+ }
65
+ }
66
+ };
67
+ }
68
+ //#endregion
69
+ export { STORAGE_ORGANIZATION_PURGE_SUMMARY, storageOrganizationPurge };
@@ -3,5 +3,5 @@
3
3
  /** This package's npm name — the join key against a release feed. */
4
4
  export declare const PACKAGE_NAME = "@pithy-sh/storage";
5
5
  /** This package's version, stamped from its own package.json at generation time. */
6
- export declare const PACKAGE_VERSION = "0.5.0";
6
+ export declare const PACKAGE_VERSION = "0.6.0";
7
7
  //# sourceMappingURL=version.generated.d.ts.map
@@ -4,6 +4,6 @@
4
4
  /** This package's npm name — the join key against a release feed. */
5
5
  const PACKAGE_NAME = "@pithy-sh/storage";
6
6
  /** This package's version, stamped from its own package.json at generation time. */
7
- const PACKAGE_VERSION = "0.5.0";
7
+ const PACKAGE_VERSION = "0.6.0";
8
8
  //#endregion
9
9
  export { PACKAGE_NAME, PACKAGE_VERSION };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pithy-sh/storage",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,9 +39,9 @@
39
39
  "@hono/zod-validator": "^0.9.0"
40
40
  },
41
41
  "peerDependencies": {
42
- "@pithy-sh/cloudflare": "^0.7.0",
43
- "@pithy-sh/core": "^0.12.0",
44
- "@pithy-sh/secrets": "^0.6.0",
42
+ "@pithy-sh/cloudflare": "^0.7.1",
43
+ "@pithy-sh/core": "^0.13.0",
44
+ "@pithy-sh/secrets": "^0.6.1",
45
45
  "hono": "^4.13.2",
46
46
  "kysely": "^0.29.0",
47
47
  "zod": "^4.4.0"
package/src/capability.ts CHANGED
@@ -17,6 +17,7 @@ import { registerStorageRoutes } from "./http/routes";
17
17
  import { storageAdminRoutes } from "./http/scopes";
18
18
  import { storage_0001_objects } from "./migrations/0001_objects";
19
19
  import { storage_0002_ownership } from "./migrations/0002_ownership";
20
+ import { storageOrganizationPurge } from "./purge/contribution";
20
21
  import { bundleTokenCheck } from "./secret/bundleToken";
21
22
  import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET, storageSecretsRegistry } from "./secret/registry";
22
23
  import { storageExampleSeed } from "./seeds/example";
@@ -106,6 +107,9 @@ export function storage(options: StorageOptions = {}): StorageCapability {
106
107
  dependsOn: ["secrets"],
107
108
  secretRegistry: storageSecretsRegistry,
108
109
  workflows: storageWorkflows,
110
+ // What erasing one organization means here (#623): its files, their bytes and their share links. The
111
+ // organization purge runs it as its own step, in the adopter's Worker, which binds what it reads.
112
+ purge: [storageOrganizationPurge()],
109
113
  requiredBindings,
110
114
  config: StorageConfig,
111
115
  // The power a role holds to edit, delete and re-scope any file in its own organization. Declared so
@@ -0,0 +1,76 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import type { D1Database, R2Bucket } from "@cloudflare/workers-types";
5
+ import type { PurgeContribution } from "@pithy-sh/core/src/capability/purge";
6
+ import { InternalError } from "@pithy-sh/core/src/error/pithyError";
7
+ import type { SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
8
+ import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, storageDatabase } from "../data/tables";
9
+ import { objectStore } from "../object/store";
10
+ import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry";
11
+
12
+ /**
13
+ * What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
14
+ * organization's, with its bytes in R2 and every share link to it.
15
+ *
16
+ * **The bytes go first, then the shares, then the row** — the row is the only thing that names the key, so a row
17
+ * deleted ahead of its object would leave bytes nothing can find. An R2 delete that fails throws, and this step
18
+ * fails and retries alone, with the rows not yet reached still naming what is left. An in-flight multipart upload
19
+ * is aborted best-effort, as the sweep aborts one: the abort is an S3-protocol call on the R2 credentials, and
20
+ * parts it cannot reach expire under the bucket's own multipart lifecycle.
21
+ *
22
+ * **Idempotent**, as the contract requires: R2 deletes a missing key without complaint, and an organization with
23
+ * no files is a step that finds nothing.
24
+ *
25
+ * Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
26
+ * one written for another organization, or for none, is untouched.
27
+ */
28
+
29
+ /** The one sentence the purge records with this step. */
30
+ export const STORAGE_ORGANIZATION_PURGE_SUMMARY =
31
+ "Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
32
+
33
+ /** How many files one read takes, so an organization with many files is erased in bounded statements. */
34
+ const PAGE = 100;
35
+
36
+ /** One binding off a run's env, or a wiring fault naming it. */
37
+ function bindingOf<T>(env: Record<string, unknown>, name: string, kind: string): T {
38
+ const binding = env[name];
39
+ if (typeof binding !== "object" || binding === null) {
40
+ throw new InternalError({
41
+ message: "Storage is not bound, so this purge step could not run.",
42
+ action: `Bind ${kind} named ${name} to the Worker that runs the purge.`,
43
+ detail: `storage organization purge contribution found no ${name} binding on the env`,
44
+ });
45
+ }
46
+ return binding as T;
47
+ }
48
+
49
+ /** The organization contribution `storage()` declares. */
50
+ export function storageOrganizationPurge(): PurgeContribution {
51
+ return {
52
+ subject: "organization",
53
+ summary: STORAGE_ORGANIZATION_PURGE_SUMMARY,
54
+ erase: async ({ subject, env }) => {
55
+ const db = storageDatabase(bindingOf<D1Database>(env, "DB", "a D1 database"));
56
+ const bucket = bindingOf<R2Bucket>(env, STORAGE_BUCKET_BINDING, "an R2 bucket");
57
+ // Credentials resolve lazily inside the store, so a purge with no multipart upload in flight reads no secret.
58
+ const store = objectStore({ bucket, env: env as unknown as SecretsStoreEnv, secretName: STORAGE_R2_SECRET });
59
+ for (;;) {
60
+ const page = await db
61
+ .selectFrom(STORAGE_OBJECTS_TABLE)
62
+ .select(["id", "key", "uploadId"])
63
+ .where("organizationId", "=", subject.id)
64
+ .limit(PAGE)
65
+ .execute();
66
+ if (page.length === 0) return;
67
+ for (const object of page) {
68
+ if (object.uploadId) await store.abortMultipart(object.key, object.uploadId).catch(() => {});
69
+ await store.delete(object.key);
70
+ await db.deleteFrom(STORAGE_SHARES_TABLE).where("objectId", "=", object.id).execute();
71
+ await db.deleteFrom(STORAGE_OBJECTS_TABLE).where("id", "=", object.id).execute();
72
+ }
73
+ }
74
+ },
75
+ };
76
+ }
@@ -13,4 +13,4 @@
13
13
  export const PACKAGE_NAME = "@pithy-sh/storage";
14
14
 
15
15
  /** This package's version, stamped from its own package.json at generation time. */
16
- export const PACKAGE_VERSION = "0.5.0";
16
+ export const PACKAGE_VERSION = "0.6.0";