@pithy-sh/storage 0.5.0 → 0.7.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/README.md +4 -0
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +2 -0
- package/dist/http/scopes.d.ts +2 -2
- package/dist/http/scopes.d.ts.map +1 -1
- package/dist/http/scopes.js +2 -0
- package/dist/purge/contribution.d.ts +34 -0
- package/dist/purge/contribution.d.ts.map +1 -0
- package/dist/purge/contribution.js +98 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/package.json +4 -4
- package/src/capability.ts +5 -0
- package/src/http/scopes.ts +4 -2
- package/src/purge/contribution.ts +110 -0
- package/src/version.generated.ts +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,10 @@ pithy add storage
|
|
|
12
12
|
|
|
13
13
|
_Everything else is on the site. `pithy.sh/docs` is canonical — new prose goes there, not here._
|
|
14
14
|
|
|
15
|
+
## A person's data
|
|
16
|
+
|
|
17
|
+
**Erased.** When an account is purged, storage deletes every file the person holds as themselves, with its bytes in R2 and every share link to it, and an export hands the file records back first — never a share link's token. A file made while acting for an organization is the organization's, and that organization's purge erases it. See [`@pithy-sh/auth`'s account lifecycle](../auth/docs/account-lifecycle.md#what-each-capability-does-with-a-persons-rows).
|
|
18
|
+
|
|
15
19
|
## License
|
|
16
20
|
|
|
17
21
|
MIT — adopter-side app value. The root `LICENSE` covers it.
|
package/dist/capability.d.ts.map
CHANGED
|
@@ -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;
|
|
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,CA8EvE;AAED,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,IAAI,iBAAiB,CAE3F"}
|
package/dist/capability.js
CHANGED
|
@@ -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, storageUserPurge } 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(), storageUserPurge()],
|
|
69
71
|
requiredBindings,
|
|
70
72
|
config: StorageConfig,
|
|
71
73
|
powers: [STORAGE_MANAGE_POWER],
|
package/dist/http/scopes.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import type {
|
|
3
|
+
import type { AdminRouteDeclaration } from "@pithy-sh/core/src/controlPlane/discovery/adminRoute";
|
|
4
4
|
import type { ControlPlaneScope } from "@pithy-sh/core/src/controlPlane/scope/scope";
|
|
5
5
|
/**
|
|
6
6
|
* Storage's control-plane scope, and the admin surface a manifest advertises.
|
|
@@ -21,5 +21,5 @@ export declare const STORAGE_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[];
|
|
|
21
21
|
* The admin routes this capability advertises on `GET /control-plane/manifest`, built from the **resolved**
|
|
22
22
|
* mount path, never the default.
|
|
23
23
|
*/
|
|
24
|
-
export declare function storageAdminRoutes(basePath: string):
|
|
24
|
+
export declare function storageAdminRoutes(basePath: string): AdminRouteDeclaration[];
|
|
25
25
|
//# sourceMappingURL=scopes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scopes.d.ts","sourceRoot":"","sources":["../../src/http/scopes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"scopes.d.ts","sourceRoot":"","sources":["../../src/http/scopes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sDAAsD,CAAC;AAClG,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,qBAAqB,EAAE,CAY5E"}
|
package/dist/http/scopes.js
CHANGED
|
@@ -23,8 +23,10 @@ const STORAGE_CONTROL_PLANE_SCOPES = [STORAGE_USAGE_READ_SCOPE];
|
|
|
23
23
|
function storageAdminRoutes(basePath) {
|
|
24
24
|
return [{
|
|
25
25
|
method: "GET",
|
|
26
|
+
operation: "usage.read",
|
|
26
27
|
path: `${basePath}/admin/usage`,
|
|
27
28
|
scope: STORAGE_USAGE_READ_SCOPE,
|
|
29
|
+
kind: "reads",
|
|
28
30
|
summary: "Files and bytes held, per organization or per owner within one organization. Never across organizations."
|
|
29
31
|
}];
|
|
30
32
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { type ErasingPurgeContribution } 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
|
+
* ## And one person's (#814)
|
|
21
|
+
*
|
|
22
|
+
* The same erase, for the files a person holds as themselves: `ownerId` is theirs and no organization holds the
|
|
23
|
+
* file. One they wrote while acting for an organization is that organization's, and its purge is what erases it.
|
|
24
|
+
* The export is those files' records — never a share link's token, which is the credential that opens one.
|
|
25
|
+
*/
|
|
26
|
+
/** The one sentence the purge records with this step. */
|
|
27
|
+
export declare const STORAGE_ORGANIZATION_PURGE_SUMMARY = "Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
|
|
28
|
+
/** The one sentence a person's purge records with this step (#814). */
|
|
29
|
+
export declare const STORAGE_USER_PURGE_SUMMARY = "Deletes every file the user holds as themselves, with its bytes in R2 and every share link to it. A file they wrote for an organization is the organization's.";
|
|
30
|
+
/** The organization contribution `storage()` declares. */
|
|
31
|
+
export declare function storageOrganizationPurge(): ErasingPurgeContribution;
|
|
32
|
+
/** The user contribution `storage()` declares (#814): a person's own files, erased as above and exported first. */
|
|
33
|
+
export declare function storageUserPurge(): ErasingPurgeContribution;
|
|
34
|
+
//# 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,EAAE,KAAK,wBAAwB,EAAc,MAAM,qCAAqC,CAAC;AAQhG;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,yDAAyD;AACzD,eAAO,MAAM,kCAAkC,gGACgD,CAAC;AAEhG,uEAAuE;AACvE,eAAO,MAAM,0BAA0B,mKAC2H,CAAC;AA+CnK,0DAA0D;AAC1D,wBAAgB,wBAAwB,IAAI,wBAAwB,CAMnE;AAED,mHAAmH;AACnH,wBAAgB,gBAAgB,IAAI,wBAAwB,CAW3D"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { StorageObject } from "../data/storageObject.js";
|
|
4
|
+
import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, storageDatabase } from "../data/tables.js";
|
|
5
|
+
import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry.js";
|
|
6
|
+
import { objectStore } from "../object/store.js";
|
|
7
|
+
import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
8
|
+
import { exportRows } from "@pithy-sh/core/src/capability/purge";
|
|
9
|
+
//#region src/purge/contribution.ts
|
|
10
|
+
/**
|
|
11
|
+
* What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
|
|
12
|
+
* organization's, with its bytes in R2 and every share link to it.
|
|
13
|
+
*
|
|
14
|
+
* **The bytes go first, then the shares, then the row** — the row is the only thing that names the key, so a row
|
|
15
|
+
* deleted ahead of its object would leave bytes nothing can find. An R2 delete that fails throws, and this step
|
|
16
|
+
* fails and retries alone, with the rows not yet reached still naming what is left. An in-flight multipart upload
|
|
17
|
+
* is aborted best-effort, as the sweep aborts one: the abort is an S3-protocol call on the R2 credentials, and
|
|
18
|
+
* parts it cannot reach expire under the bucket's own multipart lifecycle.
|
|
19
|
+
*
|
|
20
|
+
* **Idempotent**, as the contract requires: R2 deletes a missing key without complaint, and an organization with
|
|
21
|
+
* no files is a step that finds nothing.
|
|
22
|
+
*
|
|
23
|
+
* Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
|
|
24
|
+
* one written for another organization, or for none, is untouched.
|
|
25
|
+
*
|
|
26
|
+
* ## And one person's (#814)
|
|
27
|
+
*
|
|
28
|
+
* The same erase, for the files a person holds as themselves: `ownerId` is theirs and no organization holds the
|
|
29
|
+
* file. One they wrote while acting for an organization is that organization's, and its purge is what erases it.
|
|
30
|
+
* The export is those files' records — never a share link's token, which is the credential that opens one.
|
|
31
|
+
*/
|
|
32
|
+
/** The one sentence the purge records with this step. */
|
|
33
|
+
const STORAGE_ORGANIZATION_PURGE_SUMMARY = "Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
|
|
34
|
+
/** The one sentence a person's purge records with this step (#814). */
|
|
35
|
+
const STORAGE_USER_PURGE_SUMMARY = "Deletes every file the user holds as themselves, with its bytes in R2 and every share link to it. A file they wrote for an organization is the organization's.";
|
|
36
|
+
/** How many files one read takes, so an organization with many files is erased in bounded statements. */
|
|
37
|
+
const PAGE = 100;
|
|
38
|
+
/** One binding off a run's env, or a wiring fault naming it. */
|
|
39
|
+
function bindingOf(env, name, kind) {
|
|
40
|
+
const binding = env[name];
|
|
41
|
+
if (typeof binding !== "object" || binding === null) throw new InternalError({
|
|
42
|
+
message: "Storage is not bound, so this purge step could not run.",
|
|
43
|
+
action: `Bind ${kind} named ${name} to the Worker that runs the purge.`,
|
|
44
|
+
detail: `storage purge contribution found no ${name} binding on the env`
|
|
45
|
+
});
|
|
46
|
+
return binding;
|
|
47
|
+
}
|
|
48
|
+
/** The files `holder` holds, as a filter on the objects table. */
|
|
49
|
+
function held(db, holder) {
|
|
50
|
+
const files = db.selectFrom(STORAGE_OBJECTS_TABLE);
|
|
51
|
+
return "organizationId" in holder ? files.where("organizationId", "=", holder.organizationId) : files.where("ownerId", "=", holder.ownerId).where("organizationId", "is", null);
|
|
52
|
+
}
|
|
53
|
+
/** Erase every file `holder` holds — bytes, then shares, then the row — a page at a time. See the header. */
|
|
54
|
+
async function eraseHeld(env, holder) {
|
|
55
|
+
const db = storageDatabase(bindingOf(env, "DB", "a D1 database"));
|
|
56
|
+
const bucket = bindingOf(env, STORAGE_BUCKET_BINDING, "an R2 bucket");
|
|
57
|
+
const store = objectStore({
|
|
58
|
+
bucket,
|
|
59
|
+
env,
|
|
60
|
+
secretName: STORAGE_R2_SECRET
|
|
61
|
+
});
|
|
62
|
+
for (;;) {
|
|
63
|
+
const page = await held(db, holder).select([
|
|
64
|
+
"id",
|
|
65
|
+
"key",
|
|
66
|
+
"uploadId"
|
|
67
|
+
]).limit(PAGE).execute();
|
|
68
|
+
if (page.length === 0) return;
|
|
69
|
+
for (const object of page) {
|
|
70
|
+
if (object.uploadId) await store.abortMultipart(object.key, object.uploadId).catch(() => {});
|
|
71
|
+
await store.delete(object.key);
|
|
72
|
+
await db.deleteFrom(STORAGE_SHARES_TABLE).where("objectId", "=", object.id).execute();
|
|
73
|
+
await db.deleteFrom(STORAGE_OBJECTS_TABLE).where("id", "=", object.id).execute();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** The organization contribution `storage()` declares. */
|
|
78
|
+
function storageOrganizationPurge() {
|
|
79
|
+
return {
|
|
80
|
+
subject: "organization",
|
|
81
|
+
summary: STORAGE_ORGANIZATION_PURGE_SUMMARY,
|
|
82
|
+
erase: ({ subject, env }) => eraseHeld(env, { organizationId: subject.id })
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** The user contribution `storage()` declares (#814): a person's own files, erased as above and exported first. */
|
|
86
|
+
function storageUserPurge() {
|
|
87
|
+
return {
|
|
88
|
+
subject: "user",
|
|
89
|
+
summary: STORAGE_USER_PURGE_SUMMARY,
|
|
90
|
+
erase: ({ subject, env }) => eraseHeld(env, { ownerId: subject.id }),
|
|
91
|
+
export: async ({ subject, env }) => {
|
|
92
|
+
const rows = await held(storageDatabase(bindingOf(env, "DB", "a D1 database")), { ownerId: subject.id }).selectAll().orderBy("createdAt").orderBy("id").execute();
|
|
93
|
+
return { files: exportRows(rows.map((row) => StorageObject.parse(row))) };
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { STORAGE_ORGANIZATION_PURGE_SUMMARY, STORAGE_USER_PURGE_SUMMARY, storageOrganizationPurge, storageUserPurge };
|
|
@@ -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.
|
|
6
|
+
export declare const PACKAGE_VERSION = "0.7.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.
|
|
7
|
+
const PACKAGE_VERSION = "0.7.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.
|
|
3
|
+
"version": "0.7.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.
|
|
43
|
-
"@pithy-sh/core": "^0.
|
|
44
|
-
"@pithy-sh/secrets": "^0.6.
|
|
42
|
+
"@pithy-sh/cloudflare": "^0.8.0",
|
|
43
|
+
"@pithy-sh/core": "^0.14.0",
|
|
44
|
+
"@pithy-sh/secrets": "^0.6.2",
|
|
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, storageUserPurge } 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,10 @@ 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
|
+
// A person's own files too (#814): exported on an access request and erased by their purge.
|
|
113
|
+
purge: [storageOrganizationPurge(), storageUserPurge()],
|
|
109
114
|
requiredBindings,
|
|
110
115
|
config: StorageConfig,
|
|
111
116
|
// The power a role holds to edit, delete and re-scope any file in its own organization. Declared so
|
package/src/http/scopes.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type { AdminRouteDeclaration } from "@pithy-sh/core/src/controlPlane/discovery/adminRoute";
|
|
5
5
|
import type { ControlPlaneScope } from "@pithy-sh/core/src/controlPlane/scope/scope";
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -26,12 +26,14 @@ export const STORAGE_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[] = [STORA
|
|
|
26
26
|
* The admin routes this capability advertises on `GET /control-plane/manifest`, built from the **resolved**
|
|
27
27
|
* mount path, never the default.
|
|
28
28
|
*/
|
|
29
|
-
export function storageAdminRoutes(basePath: string):
|
|
29
|
+
export function storageAdminRoutes(basePath: string): AdminRouteDeclaration[] {
|
|
30
30
|
return [
|
|
31
31
|
{
|
|
32
32
|
method: "GET",
|
|
33
|
+
operation: "usage.read",
|
|
33
34
|
path: `${basePath}/admin/usage`,
|
|
34
35
|
scope: STORAGE_USAGE_READ_SCOPE,
|
|
36
|
+
kind: "reads",
|
|
35
37
|
summary:
|
|
36
38
|
"Files and bytes held, per organization or per owner within one organization. Never across organizations.",
|
|
37
39
|
},
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { D1Database, R2Bucket } from "@cloudflare/workers-types";
|
|
5
|
+
import { type ErasingPurgeContribution, exportRows } 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 { StorageObject } from "../data/storageObject";
|
|
9
|
+
import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, type StorageDatabase, storageDatabase } from "../data/tables";
|
|
10
|
+
import { objectStore } from "../object/store";
|
|
11
|
+
import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
|
|
15
|
+
* organization's, with its bytes in R2 and every share link to it.
|
|
16
|
+
*
|
|
17
|
+
* **The bytes go first, then the shares, then the row** — the row is the only thing that names the key, so a row
|
|
18
|
+
* deleted ahead of its object would leave bytes nothing can find. An R2 delete that fails throws, and this step
|
|
19
|
+
* fails and retries alone, with the rows not yet reached still naming what is left. An in-flight multipart upload
|
|
20
|
+
* is aborted best-effort, as the sweep aborts one: the abort is an S3-protocol call on the R2 credentials, and
|
|
21
|
+
* parts it cannot reach expire under the bucket's own multipart lifecycle.
|
|
22
|
+
*
|
|
23
|
+
* **Idempotent**, as the contract requires: R2 deletes a missing key without complaint, and an organization with
|
|
24
|
+
* no files is a step that finds nothing.
|
|
25
|
+
*
|
|
26
|
+
* Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
|
|
27
|
+
* one written for another organization, or for none, is untouched.
|
|
28
|
+
*
|
|
29
|
+
* ## And one person's (#814)
|
|
30
|
+
*
|
|
31
|
+
* The same erase, for the files a person holds as themselves: `ownerId` is theirs and no organization holds the
|
|
32
|
+
* file. One they wrote while acting for an organization is that organization's, and its purge is what erases it.
|
|
33
|
+
* The export is those files' records — never a share link's token, which is the credential that opens one.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** The one sentence the purge records with this step. */
|
|
37
|
+
export const STORAGE_ORGANIZATION_PURGE_SUMMARY =
|
|
38
|
+
"Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
|
|
39
|
+
|
|
40
|
+
/** The one sentence a person's purge records with this step (#814). */
|
|
41
|
+
export const STORAGE_USER_PURGE_SUMMARY =
|
|
42
|
+
"Deletes every file the user holds as themselves, with its bytes in R2 and every share link to it. A file they wrote for an organization is the organization's.";
|
|
43
|
+
|
|
44
|
+
/** How many files one read takes, so an organization with many files is erased in bounded statements. */
|
|
45
|
+
const PAGE = 100;
|
|
46
|
+
|
|
47
|
+
/** One binding off a run's env, or a wiring fault naming it. */
|
|
48
|
+
function bindingOf<T>(env: Record<string, unknown>, name: string, kind: string): T {
|
|
49
|
+
const binding = env[name];
|
|
50
|
+
if (typeof binding !== "object" || binding === null) {
|
|
51
|
+
throw new InternalError({
|
|
52
|
+
message: "Storage is not bound, so this purge step could not run.",
|
|
53
|
+
action: `Bind ${kind} named ${name} to the Worker that runs the purge.`,
|
|
54
|
+
detail: `storage purge contribution found no ${name} binding on the env`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return binding as T;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Which files one erase reaches: an organization's, or a person's own. */
|
|
61
|
+
type Holder = { readonly organizationId: string } | { readonly ownerId: string };
|
|
62
|
+
|
|
63
|
+
/** The files `holder` holds, as a filter on the objects table. */
|
|
64
|
+
function held(db: StorageDatabase, holder: Holder) {
|
|
65
|
+
const files = db.selectFrom(STORAGE_OBJECTS_TABLE);
|
|
66
|
+
return "organizationId" in holder
|
|
67
|
+
? files.where("organizationId", "=", holder.organizationId)
|
|
68
|
+
: files.where("ownerId", "=", holder.ownerId).where("organizationId", "is", null);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Erase every file `holder` holds — bytes, then shares, then the row — a page at a time. See the header. */
|
|
72
|
+
async function eraseHeld(env: Record<string, unknown>, holder: Holder): Promise<void> {
|
|
73
|
+
const db = storageDatabase(bindingOf<D1Database>(env, "DB", "a D1 database"));
|
|
74
|
+
const bucket = bindingOf<R2Bucket>(env, STORAGE_BUCKET_BINDING, "an R2 bucket");
|
|
75
|
+
// Credentials resolve lazily inside the store, so a purge with no multipart upload in flight reads no secret.
|
|
76
|
+
const store = objectStore({ bucket, env: env as unknown as SecretsStoreEnv, secretName: STORAGE_R2_SECRET });
|
|
77
|
+
for (;;) {
|
|
78
|
+
const page = await held(db, holder).select(["id", "key", "uploadId"]).limit(PAGE).execute();
|
|
79
|
+
if (page.length === 0) return;
|
|
80
|
+
for (const object of page) {
|
|
81
|
+
if (object.uploadId) await store.abortMultipart(object.key, object.uploadId).catch(() => {});
|
|
82
|
+
await store.delete(object.key);
|
|
83
|
+
await db.deleteFrom(STORAGE_SHARES_TABLE).where("objectId", "=", object.id).execute();
|
|
84
|
+
await db.deleteFrom(STORAGE_OBJECTS_TABLE).where("id", "=", object.id).execute();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The organization contribution `storage()` declares. */
|
|
90
|
+
export function storageOrganizationPurge(): ErasingPurgeContribution {
|
|
91
|
+
return {
|
|
92
|
+
subject: "organization",
|
|
93
|
+
summary: STORAGE_ORGANIZATION_PURGE_SUMMARY,
|
|
94
|
+
erase: ({ subject, env }) => eraseHeld(env, { organizationId: subject.id }),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The user contribution `storage()` declares (#814): a person's own files, erased as above and exported first. */
|
|
99
|
+
export function storageUserPurge(): ErasingPurgeContribution {
|
|
100
|
+
return {
|
|
101
|
+
subject: "user",
|
|
102
|
+
summary: STORAGE_USER_PURGE_SUMMARY,
|
|
103
|
+
erase: ({ subject, env }) => eraseHeld(env, { ownerId: subject.id }),
|
|
104
|
+
export: async ({ subject, env }) => {
|
|
105
|
+
const db = storageDatabase(bindingOf<D1Database>(env, "DB", "a D1 database"));
|
|
106
|
+
const rows = await held(db, { ownerId: subject.id }).selectAll().orderBy("createdAt").orderBy("id").execute();
|
|
107
|
+
return { files: exportRows(rows.map((row) => StorageObject.parse(row))) };
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
package/src/version.generated.ts
CHANGED