@pithy-sh/storage 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +2 -2
- 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 +12 -2
- package/dist/purge/contribution.d.ts.map +1 -1
- package/dist/purge/contribution.js +53 -24
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/package.json +4 -4
- package/src/capability.ts +3 -2
- package/src/http/scopes.ts +4 -2
- package/src/purge/contribution.ts +57 -23
- 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;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,
|
|
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,7 +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
|
+
import { storageOrganizationPurge, storageUserPurge } from "./purge/contribution.js";
|
|
12
12
|
import { bundleTokenCheck } from "./secret/bundleToken.js";
|
|
13
13
|
import { storageExampleSeed } from "./seeds/example.js";
|
|
14
14
|
import { PACKAGE_NAME, PACKAGE_VERSION } from "./version.generated.js";
|
|
@@ -67,7 +67,7 @@ function storage(options = {}) {
|
|
|
67
67
|
dependsOn: ["secrets"],
|
|
68
68
|
secretRegistry: storageSecretsRegistry,
|
|
69
69
|
workflows: storageWorkflows,
|
|
70
|
-
purge: [storageOrganizationPurge()],
|
|
70
|
+
purge: [storageOrganizationPurge(), storageUserPurge()],
|
|
71
71
|
requiredBindings,
|
|
72
72
|
config: StorageConfig,
|
|
73
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
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import type
|
|
3
|
+
import { type ErasingPurgeContribution } from "@pithy-sh/core/src/capability/purge";
|
|
4
4
|
/**
|
|
5
5
|
* What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
|
|
6
6
|
* organization's, with its bytes in R2 and every share link to it.
|
|
@@ -16,9 +16,19 @@ import type { PurgeContribution } from "@pithy-sh/core/src/capability/purge";
|
|
|
16
16
|
*
|
|
17
17
|
* Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
|
|
18
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.
|
|
19
25
|
*/
|
|
20
26
|
/** The one sentence the purge records with this step. */
|
|
21
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.";
|
|
22
30
|
/** The organization contribution `storage()` declares. */
|
|
23
|
-
export declare function storageOrganizationPurge():
|
|
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;
|
|
24
34
|
//# sourceMappingURL=contribution.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contribution.d.ts","sourceRoot":"","sources":["../../src/purge/contribution.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
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"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { StorageObject } from "../data/storageObject.js";
|
|
3
4
|
import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, storageDatabase } from "../data/tables.js";
|
|
4
5
|
import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry.js";
|
|
5
6
|
import { objectStore } from "../object/store.js";
|
|
6
7
|
import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
8
|
+
import { exportRows } from "@pithy-sh/core/src/capability/purge";
|
|
7
9
|
//#region src/purge/contribution.ts
|
|
8
10
|
/**
|
|
9
11
|
* What erasing one organization means for `@pithy-sh/storage` (#623): every file whose `organizationId` is the
|
|
@@ -20,9 +22,17 @@ import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
|
20
22
|
*
|
|
21
23
|
* Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
|
|
22
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.
|
|
23
31
|
*/
|
|
24
32
|
/** The one sentence the purge records with this step. */
|
|
25
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.";
|
|
26
36
|
/** How many files one read takes, so an organization with many files is erased in bounded statements. */
|
|
27
37
|
const PAGE = 100;
|
|
28
38
|
/** One binding off a run's env, or a wiring fault naming it. */
|
|
@@ -31,39 +41,58 @@ function bindingOf(env, name, kind) {
|
|
|
31
41
|
if (typeof binding !== "object" || binding === null) throw new InternalError({
|
|
32
42
|
message: "Storage is not bound, so this purge step could not run.",
|
|
33
43
|
action: `Bind ${kind} named ${name} to the Worker that runs the purge.`,
|
|
34
|
-
detail: `storage
|
|
44
|
+
detail: `storage purge contribution found no ${name} binding on the env`
|
|
35
45
|
});
|
|
36
46
|
return binding;
|
|
37
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
|
+
}
|
|
38
77
|
/** The organization contribution `storage()` declares. */
|
|
39
78
|
function storageOrganizationPurge() {
|
|
40
79
|
return {
|
|
41
80
|
subject: "organization",
|
|
42
81
|
summary: STORAGE_ORGANIZATION_PURGE_SUMMARY,
|
|
43
|
-
erase:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
}
|
|
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))) };
|
|
65
94
|
}
|
|
66
95
|
};
|
|
67
96
|
}
|
|
68
97
|
//#endregion
|
|
69
|
-
export { STORAGE_ORGANIZATION_PURGE_SUMMARY, storageOrganizationPurge };
|
|
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.1";
|
|
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.1";
|
|
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.1",
|
|
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.1",
|
|
43
|
+
"@pithy-sh/core": "^0.15.0",
|
|
44
|
+
"@pithy-sh/secrets": "^0.6.3",
|
|
45
45
|
"hono": "^4.13.2",
|
|
46
46
|
"kysely": "^0.29.0",
|
|
47
47
|
"zod": "^4.4.0"
|
package/src/capability.ts
CHANGED
|
@@ -17,7 +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
|
+
import { storageOrganizationPurge, storageUserPurge } from "./purge/contribution";
|
|
21
21
|
import { bundleTokenCheck } from "./secret/bundleToken";
|
|
22
22
|
import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET, storageSecretsRegistry } from "./secret/registry";
|
|
23
23
|
import { storageExampleSeed } from "./seeds/example";
|
|
@@ -109,7 +109,8 @@ export function storage(options: StorageOptions = {}): StorageCapability {
|
|
|
109
109
|
workflows: storageWorkflows,
|
|
110
110
|
// What erasing one organization means here (#623): its files, their bytes and their share links. The
|
|
111
111
|
// organization purge runs it as its own step, in the adopter's Worker, which binds what it reads.
|
|
112
|
-
|
|
112
|
+
// A person's own files too (#814): exported on an access request and erased by their purge.
|
|
113
|
+
purge: [storageOrganizationPurge(), storageUserPurge()],
|
|
113
114
|
requiredBindings,
|
|
114
115
|
config: StorageConfig,
|
|
115
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
|
},
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import type { D1Database, R2Bucket } from "@cloudflare/workers-types";
|
|
5
|
-
import type
|
|
5
|
+
import { type ErasingPurgeContribution, exportRows } from "@pithy-sh/core/src/capability/purge";
|
|
6
6
|
import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
7
7
|
import type { SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
8
|
-
import {
|
|
8
|
+
import { StorageObject } from "../data/storageObject";
|
|
9
|
+
import { STORAGE_OBJECTS_TABLE, STORAGE_SHARES_TABLE, type StorageDatabase, storageDatabase } from "../data/tables";
|
|
9
10
|
import { objectStore } from "../object/store";
|
|
10
11
|
import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry";
|
|
11
12
|
|
|
@@ -24,12 +25,22 @@ import { STORAGE_BUCKET_BINDING, STORAGE_R2_SECRET } from "../secret/registry";
|
|
|
24
25
|
*
|
|
25
26
|
* Only `organizationId` decides. A file its owner wrote while acting for this organization is the organization's;
|
|
26
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.
|
|
27
34
|
*/
|
|
28
35
|
|
|
29
36
|
/** The one sentence the purge records with this step. */
|
|
30
37
|
export const STORAGE_ORGANIZATION_PURGE_SUMMARY =
|
|
31
38
|
"Deletes every file the organization holds, with its bytes in R2 and every share link to it.";
|
|
32
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
|
+
|
|
33
44
|
/** How many files one read takes, so an organization with many files is erased in bounded statements. */
|
|
34
45
|
const PAGE = 100;
|
|
35
46
|
|
|
@@ -40,37 +51,60 @@ function bindingOf<T>(env: Record<string, unknown>, name: string, kind: string):
|
|
|
40
51
|
throw new InternalError({
|
|
41
52
|
message: "Storage is not bound, so this purge step could not run.",
|
|
42
53
|
action: `Bind ${kind} named ${name} to the Worker that runs the purge.`,
|
|
43
|
-
detail: `storage
|
|
54
|
+
detail: `storage purge contribution found no ${name} binding on the env`,
|
|
44
55
|
});
|
|
45
56
|
}
|
|
46
57
|
return binding as T;
|
|
47
58
|
}
|
|
48
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
|
+
|
|
49
89
|
/** The organization contribution `storage()` declares. */
|
|
50
|
-
export function storageOrganizationPurge():
|
|
90
|
+
export function storageOrganizationPurge(): ErasingPurgeContribution {
|
|
51
91
|
return {
|
|
52
92
|
subject: "organization",
|
|
53
93
|
summary: STORAGE_ORGANIZATION_PURGE_SUMMARY,
|
|
54
|
-
erase:
|
|
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 }) => {
|
|
55
105
|
const db = storageDatabase(bindingOf<D1Database>(env, "DB", "a D1 database"));
|
|
56
|
-
const
|
|
57
|
-
|
|
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
|
-
}
|
|
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))) };
|
|
74
108
|
},
|
|
75
109
|
};
|
|
76
110
|
}
|
package/src/version.generated.ts
CHANGED