@pithy-sh/storage 0.2.10 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capability.d.ts +12 -3
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +35 -9
- package/dist/config/config.d.ts +20 -4
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +23 -6
- package/dist/data/storageObject.d.ts +12 -0
- package/dist/data/storageObject.d.ts.map +1 -1
- package/dist/data/storageObject.js +13 -2
- package/dist/data/tables.js +1 -1
- package/dist/error/errors.js +2 -2
- package/dist/http/handlers.d.ts +48 -16
- package/dist/http/handlers.d.ts.map +1 -1
- package/dist/http/handlers.js +169 -66
- package/dist/http/responses.d.ts +173 -0
- package/dist/http/responses.d.ts.map +1 -0
- package/dist/http/responses.js +92 -0
- package/dist/http/routes.d.ts +13 -2
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +134 -22
- package/dist/http/schemas.d.ts +6 -0
- package/dist/http/schemas.d.ts.map +1 -1
- package/dist/http/schemas.js +7 -6
- package/dist/http/scopes.d.ts +25 -0
- package/dist/http/scopes.d.ts.map +1 -0
- package/dist/http/scopes.js +32 -0
- package/dist/http/serve.d.ts +5 -2
- package/dist/http/serve.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/migrations/0002_ownership.d.ts +5 -0
- package/dist/migrations/0002_ownership.d.ts.map +1 -0
- package/dist/migrations/0002_ownership.js +114 -0
- package/dist/object/cloudflare.d.ts +3 -2
- package/dist/object/cloudflare.d.ts.map +1 -1
- package/dist/object/cloudflare.js +3 -3
- package/dist/object/store.d.ts +2 -7
- package/dist/object/store.d.ts.map +1 -1
- package/dist/object/store.js +1 -6
- package/dist/object/uploadedPart.d.ts +11 -0
- package/dist/object/uploadedPart.d.ts.map +1 -0
- package/dist/object/uploadedPart.js +12 -0
- package/dist/provision/provisionStorage.d.ts +14 -2
- package/dist/provision/provisionStorage.d.ts.map +1 -1
- package/dist/provision/provisionStorage.js +19 -4
- package/dist/provision/resolveStorageConfig.js +1 -1
- package/dist/quota/ceiling.d.ts +36 -0
- package/dist/quota/ceiling.d.ts.map +1 -0
- package/dist/quota/ceiling.js +31 -0
- package/dist/quota/quota.d.ts +24 -7
- package/dist/quota/quota.d.ts.map +1 -1
- package/dist/quota/quota.js +40 -12
- package/dist/secret/bundleToken.d.ts +24 -0
- package/dist/secret/bundleToken.d.ts.map +1 -0
- package/dist/secret/bundleToken.js +74 -0
- package/dist/secret/registry.d.ts +5 -1
- package/dist/secret/registry.d.ts.map +1 -1
- package/dist/secret/registry.js +7 -3
- package/dist/seeds/example.d.ts.map +1 -1
- package/dist/seeds/example.js +3 -0
- package/dist/test-utils/liveStorage.d.ts.map +1 -1
- package/dist/test-utils/liveStorage.js +5 -2
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.d.ts.map +1 -1
- package/dist/version.generated.js +1 -1
- package/package.json +7 -7
- package/pithy.manifest.json +20 -10
- package/src/capability.ts +52 -8
- package/src/config/config.ts +24 -6
- package/src/data/storageObject.ts +22 -6
- package/src/error/errors.ts +2 -2
- package/src/http/handlers.ts +215 -81
- package/src/http/responses.ts +168 -0
- package/src/http/routes.ts +245 -46
- package/src/http/schemas.ts +11 -5
- package/src/http/scopes.ts +39 -0
- package/src/http/serve.ts +5 -2
- package/src/index.ts +2 -0
- package/src/migrations/0002_ownership.ts +176 -0
- package/src/object/cloudflare.ts +4 -3
- package/src/object/store.ts +6 -10
- package/src/object/uploadedPart.ts +17 -0
- package/src/provision/provisionStorage.ts +22 -3
- package/src/quota/ceiling.ts +44 -0
- package/src/quota/quota.ts +63 -20
- package/src/secret/bundleToken.ts +81 -0
- package/src/secret/registry.ts +6 -8
- package/src/seeds/example.ts +3 -0
- package/src/test-utils/liveStorage.ts +3 -6
- package/src/version.generated.ts +1 -1
|
@@ -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 };
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import { CloudflareR2Manager } from "@pithy-sh/cloudflare/src/r2/r2Manager";
|
|
4
4
|
import type { R2StorageCredentials } from "../secret/registry";
|
|
5
|
-
import {
|
|
5
|
+
import type { PresignedObjects } from "./store";
|
|
6
6
|
/**
|
|
7
7
|
* The SDK-friction boundary: the one file in this package that touches `@pithy-sh/cloudflare`.
|
|
8
8
|
*
|
|
@@ -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,EAAE,
|
|
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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import { UploadedPart } from "./
|
|
3
|
+
import { UploadedPart } from "./uploadedPart.js";
|
|
4
4
|
import { CloudflareR2Manager } from "@pithy-sh/cloudflare/src/r2/r2Manager";
|
|
5
5
|
//#region src/object/cloudflare.ts
|
|
6
6
|
/**
|
|
@@ -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,
|
package/dist/object/store.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type { SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
import { type R2StorageCredentials } from "../secret/registry";
|
|
7
7
|
import type { ReportedPart } from "./multipart";
|
|
8
|
+
import type { UploadedPart } from "./uploadedPart";
|
|
9
|
+
export { UploadedPart } from "./uploadedPart";
|
|
8
10
|
/**
|
|
9
11
|
* `ObjectStore` — the object-plane seam, and the one piece of this package that is reusable outside it.
|
|
10
12
|
*
|
|
@@ -68,13 +70,6 @@ export declare const ObjectListing: z.ZodObject<{
|
|
|
68
70
|
cursor: z.ZodOptional<z.ZodString>;
|
|
69
71
|
}, z.core.$strip>;
|
|
70
72
|
export type ObjectListing = z.output<typeof ObjectListing>;
|
|
71
|
-
/** One part already stored against an in-flight multipart upload — what makes an upload resumable. */
|
|
72
|
-
export declare const UploadedPart: z.ZodObject<{
|
|
73
|
-
partNumber: z.ZodNumber;
|
|
74
|
-
etag: z.ZodString;
|
|
75
|
-
size: z.ZodNumber;
|
|
76
|
-
}, z.core.$strip>;
|
|
77
|
-
export type UploadedPart = z.output<typeof UploadedPart>;
|
|
78
73
|
/** How long a presigned URL stays valid. */
|
|
79
74
|
export interface PresignOptions {
|
|
80
75
|
/** Lifetime in seconds. Defaults to one hour — long enough for a large part, short enough to leak little. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/object/store.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAoC,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC5G,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAE1E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,KAAK,oBAAoB,EAA4C,MAAM,oBAAoB,CAAC;AAEzG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/object/store.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAoC,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC5G,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAE1E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,KAAK,oBAAoB,EAA4C,MAAM,oBAAoB,CAAC;AAEzG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;A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CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IACnE,sGAAsG;IACtG,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IAClD,4FAA4F;IAC5F,IAAI,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACpD,0CAA0C;IAC1C,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/D,iDAAiD;IACjD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED,oCAAoC;AACpC,MAAM,WAAW,kBAAkB;IACjC,sFAAsF;IACtF,MAAM,EAAE,QAAQ,CAAC;IACjB;;;OAGG;IACH,GAAG,EAAE,eAAe,CAAC;IACrB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,SAAS,CAAC,EAAE,CAAC,WAAW,EAAE,oBAAoB,KAAK,gBAAgB,CAAC;CACrE;AA8BD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,WAAW,CA6FpE"}
|
package/dist/object/store.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import { r2CredentialsRegistry } from "../secret/registry.js";
|
|
4
|
+
import { UploadedPart } from "./uploadedPart.js";
|
|
4
5
|
import { r2Presigned } from "./cloudflare.js";
|
|
5
6
|
import { z } from "zod";
|
|
6
7
|
import { sharedSecretsStore } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
@@ -56,12 +57,6 @@ const ObjectListing = z.object({
|
|
|
56
57
|
objects: z.array(ObjectMetadata).describe("This page's objects, in R2's lexicographic key order."),
|
|
57
58
|
cursor: z.string().min(1).optional().describe("Continuation cursor for the next page. Absent means this page was the last.")
|
|
58
59
|
}).describe("One page of a bucket listing. Pagination is caller-driven — pass `cursor` back to advance.");
|
|
59
|
-
/** One part already stored against an in-flight multipart upload — what makes an upload resumable. */
|
|
60
|
-
const UploadedPart = z.object({
|
|
61
|
-
partNumber: z.number().int().min(1).describe("The part's 1-based index within the upload."),
|
|
62
|
-
etag: z.string().min(1).describe("The part's entity tag, verbatim. Pass it back unchanged to complete."),
|
|
63
|
-
size: z.number().int().nonnegative().describe("The part's size in bytes, as R2 stored it.")
|
|
64
|
-
}).describe("One uploaded part — the unit a resumed upload skips re-sending.");
|
|
65
60
|
/** Map our range shape onto R2's — the binding's union admits exactly one of these three forms. */
|
|
66
61
|
function toR2Range(range) {
|
|
67
62
|
if (!range) return void 0;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/** One part already stored against an in-flight multipart upload — what makes an upload resumable. */
|
|
5
|
+
export declare const UploadedPart: z.ZodObject<{
|
|
6
|
+
partNumber: z.ZodNumber;
|
|
7
|
+
etag: z.ZodString;
|
|
8
|
+
size: z.ZodNumber;
|
|
9
|
+
}, z.core.$strip>;
|
|
10
|
+
export type UploadedPart = z.output<typeof UploadedPart>;
|
|
11
|
+
//# sourceMappingURL=uploadedPart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uploadedPart.d.ts","sourceRoot":"","sources":["../../src/object/uploadedPart.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB,sGAAsG;AACtG,eAAO,MAAM,YAAY;;;;iBAMqD,CAAC;AAC/E,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,YAAY,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
//#region src/object/uploadedPart.ts
|
|
5
|
+
/** One part already stored against an in-flight multipart upload — what makes an upload resumable. */
|
|
6
|
+
const UploadedPart = z.object({
|
|
7
|
+
partNumber: z.number().int().min(1).describe("The part's 1-based index within the upload."),
|
|
8
|
+
etag: z.string().min(1).describe("The part's entity tag, verbatim. Pass it back unchanged to complete."),
|
|
9
|
+
size: z.number().int().nonnegative().describe("The part's size in bytes, as R2 stored it.")
|
|
10
|
+
}).describe("One uploaded part — the unit a resumed upload skips re-sending.");
|
|
11
|
+
//#endregion
|
|
12
|
+
export { UploadedPart };
|
|
@@ -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
|
|
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
|
|
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,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,4FAA4F;IAC5F,YAAY,EAAE,KAAK,CAAC;QAAE,GAAG,EAAE,kBAAkB,CAAA;KAAE,GAAG,gBAAgB,CAAC,CAAC;CACrE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,gBAAgB,CACpC,WAAW,EAAE,kBAAkB,EAC/B,YAAY,EAAE,oBAAoB,GAAG,SAAS,MAAM,EAAE,GACrD,OAAO,CAAC,sBAAsB,CAAC,CAiBjC;AASD,4FAA4F;AAC5F,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,eAAe,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxD,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,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
|
|
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
|
|
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) {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { PithyHonoEnv } from "@pithy-sh/core/src/capability/capability";
|
|
4
|
+
import type { Context } from "hono";
|
|
5
|
+
import type { StorageConfig } from "../config/config";
|
|
6
|
+
/** The byte ceiling one request's writes are held to, and whose bytes it is the whole of. */
|
|
7
|
+
export interface StorageCeiling {
|
|
8
|
+
/** The ceiling in bytes. Null is unlimited. */
|
|
9
|
+
limitBytes: number | null;
|
|
10
|
+
/**
|
|
11
|
+
* The organization the ceiling covers, or null when it is the caller's own. Set, every byte stored for
|
|
12
|
+
* that organization counts against it, whoever uploaded it — an organization's plan is the organization's.
|
|
13
|
+
*/
|
|
14
|
+
tenant: string | null;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the number is an enforced `storageBytes` meter's. A refusal is then the plan's —
|
|
17
|
+
* `payments/quota_exceeded` at 402, the remedy a bigger tier — and otherwise the configured
|
|
18
|
+
* `bytesPerOwner`'s, `storage/quota_exceeded` at 413.
|
|
19
|
+
*/
|
|
20
|
+
metered: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The byte ceiling for one request: the `storageBytes` quota where an entitlement provider enforces that
|
|
24
|
+
* meter, the configured `quota.bytesPerOwner` where it does not.
|
|
25
|
+
*
|
|
26
|
+
* **Configured behavior is the fallback, never replaced by surprise.** With no provider composed, or one that
|
|
27
|
+
* leaves `storageBytes` advisory, this is `bytesPerOwner` exactly, counted per owner — which is what every
|
|
28
|
+
* project had before plans could say how much a customer may store. Enforced, it is the billing subject's
|
|
29
|
+
* effective quota: their plan's number, a number pinned for them, or `null` — unlimited — for an
|
|
30
|
+
* `unrestricted` subject. Under organization billing that subject is the organization, and so is the sum.
|
|
31
|
+
*
|
|
32
|
+
* A provider whose read fails refuses the write rather than serving it unbounded: `meterLimit` throws, and
|
|
33
|
+
* the request fails with it. Over-counting is recoverable; under-counting is not.
|
|
34
|
+
*/
|
|
35
|
+
export declare function storageCeiling(c: Context<PithyHonoEnv>, config: StorageConfig): Promise<StorageCeiling>;
|
|
36
|
+
//# sourceMappingURL=ceiling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ceiling.d.ts","sourceRoot":"","sources":["../../src/quota/ceiling.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAE7E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEtD,6FAA6F;AAC7F,MAAM,WAAW,cAAc;IAC7B,+CAA+C;IAC/C,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;OAGG;IACH,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB;;;;OAIG;IACH,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC,CAK7G"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { meterLimit } from "@pithy-sh/core/src/entitlement/allowance";
|
|
4
|
+
//#region src/quota/ceiling.ts
|
|
5
|
+
/**
|
|
6
|
+
* The byte ceiling for one request: the `storageBytes` quota where an entitlement provider enforces that
|
|
7
|
+
* meter, the configured `quota.bytesPerOwner` where it does not.
|
|
8
|
+
*
|
|
9
|
+
* **Configured behavior is the fallback, never replaced by surprise.** With no provider composed, or one that
|
|
10
|
+
* leaves `storageBytes` advisory, this is `bytesPerOwner` exactly, counted per owner — which is what every
|
|
11
|
+
* project had before plans could say how much a customer may store. Enforced, it is the billing subject's
|
|
12
|
+
* effective quota: their plan's number, a number pinned for them, or `null` — unlimited — for an
|
|
13
|
+
* `unrestricted` subject. Under organization billing that subject is the organization, and so is the sum.
|
|
14
|
+
*
|
|
15
|
+
* A provider whose read fails refuses the write rather than serving it unbounded: `meterLimit` throws, and
|
|
16
|
+
* the request fails with it. Over-counting is recoverable; under-counting is not.
|
|
17
|
+
*/
|
|
18
|
+
async function storageCeiling(c, config) {
|
|
19
|
+
const ceiling = await meterLimit(c, "storageBytes");
|
|
20
|
+
return ceiling.enforced ? {
|
|
21
|
+
limitBytes: ceiling.limit,
|
|
22
|
+
tenant: ceiling.tenant,
|
|
23
|
+
metered: true
|
|
24
|
+
} : {
|
|
25
|
+
limitBytes: config.quota.bytesPerOwner,
|
|
26
|
+
tenant: null,
|
|
27
|
+
metered: false
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { storageCeiling };
|
package/dist/quota/quota.d.ts
CHANGED
|
@@ -40,6 +40,10 @@ import { type StorageDatabase } from "../data/tables";
|
|
|
40
40
|
*
|
|
41
41
|
* A `null` limit is unlimited and skips the query entirely. A `null` owner (a system object) has no
|
|
42
42
|
* quota — there is no principal to bill it to.
|
|
43
|
+
*
|
|
44
|
+
* **Whose bytes are summed is the ceiling's to say.** A configured `bytesPerOwner` is per owner. An
|
|
45
|
+
* organization's plan is the organization's, so a check carrying `organizationId` sums every row held for
|
|
46
|
+
* that organization, whoever uploaded it — counted per member, the plan's number would be per member.
|
|
43
47
|
*/
|
|
44
48
|
/** What a quota check needs. */
|
|
45
49
|
export interface QuotaCheck {
|
|
@@ -47,8 +51,19 @@ export interface QuotaCheck {
|
|
|
47
51
|
db: StorageDatabase;
|
|
48
52
|
/** The owner the bytes are billed to. Null is a system object, which no quota applies to. */
|
|
49
53
|
ownerId: string | null;
|
|
50
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* The organization the ceiling covers, when it is an organization's. Set, the sum is every row held for it
|
|
56
|
+
* rather than the owner's alone. The row being written must be held for it too, or it would never count.
|
|
57
|
+
*/
|
|
58
|
+
organizationId?: string;
|
|
59
|
+
/** The ceiling in bytes — the configured `bytesPerOwner`, or a plan's. Null is unlimited. */
|
|
51
60
|
limitBytes: number | null;
|
|
61
|
+
/**
|
|
62
|
+
* Whether `limitBytes` is an enforced `storageBytes` meter's. True, a refusal is the plan's own —
|
|
63
|
+
* `payments/quota_exceeded` at 402, as the `emails` and `mediaItems` meters refuse — because the remedy
|
|
64
|
+
* is a bigger tier or a pinned number, not a config edit. Absent or false, it is `storage/quota_exceeded`.
|
|
65
|
+
*/
|
|
66
|
+
metered?: boolean;
|
|
52
67
|
/** The bytes this upload declares — what the pending row is about to reserve. */
|
|
53
68
|
additionalBytes: number;
|
|
54
69
|
}
|
|
@@ -76,12 +91,14 @@ export interface QuotaSettlement extends QuotaCheck {
|
|
|
76
91
|
*/
|
|
77
92
|
export declare const QUOTA_COUNTED_STATUSES: readonly StorageObjectStatus[];
|
|
78
93
|
/**
|
|
79
|
-
* Bytes an owner currently holds
|
|
80
|
-
* rows are excluded — they hold no bytes
|
|
94
|
+
* Bytes an owner currently holds — or, with `organizationId`, every byte held for that organization:
|
|
95
|
+
* every `pending` reservation plus every `stored` object. `failed` rows are excluded — they hold no bytes
|
|
96
|
+
* and reserve nothing.
|
|
81
97
|
*/
|
|
82
|
-
export declare function usedBytes(db: StorageDatabase, ownerId: string): Promise<number>;
|
|
98
|
+
export declare function usedBytes(db: StorageDatabase, ownerId: string, organizationId?: string): Promise<number>;
|
|
83
99
|
/**
|
|
84
|
-
* Throw `storage/quota_exceeded` when this upload would put the owner over their limit
|
|
100
|
+
* Throw `storage/quota_exceeded` when this upload would put the owner over their limit — or
|
|
101
|
+
* `payments/quota_exceeded` when the limit is a metered plan's ({@link QuotaCheck.metered}). The
|
|
85
102
|
* comparison is `used + additional > limit`, so an upload landing exactly on the limit is allowed —
|
|
86
103
|
* a quota is a ceiling you may reach, not one you must stay under.
|
|
87
104
|
*
|
|
@@ -93,7 +110,7 @@ export declare function usedBytes(db: StorageDatabase, ownerId: string): Promise
|
|
|
93
110
|
export declare function assertWithinQuota(check: QuotaCheck): Promise<void>;
|
|
94
111
|
/**
|
|
95
112
|
* Insert a storage row **only if** it fits inside the owner's quota, and throw
|
|
96
|
-
* `storage/quota_exceeded` when it does not.
|
|
113
|
+
* `storage/quota_exceeded` (or a metered plan's `payments/quota_exceeded`) when it does not.
|
|
97
114
|
*
|
|
98
115
|
* The sum and the write are one statement — `INSERT … SELECT <values> WHERE (SELECT sum(size) …) <=
|
|
99
116
|
* limit - additional` — because two statements cannot be made to hold a reservation without a
|
|
@@ -107,7 +124,7 @@ export declare function assertWithinQuota(check: QuotaCheck): Promise<void>;
|
|
|
107
124
|
export declare function insertReservingQuota(reservation: QuotaReservation): Promise<void>;
|
|
108
125
|
/**
|
|
109
126
|
* Rewrite one row **only if** the bytes it newly claims still fit inside the owner's quota, and throw
|
|
110
|
-
* `storage/quota_exceeded` when they do not.
|
|
127
|
+
* `storage/quota_exceeded` (or a metered plan's `payments/quota_exceeded`) when they do not.
|
|
111
128
|
*
|
|
112
129
|
* This is {@link insertReservingQuota}'s other half, and it exists for the identical reason. A
|
|
113
130
|
* completion measures what R2 actually holds against what the pending row reserved, and the difference
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quota.d.ts","sourceRoot":"","sources":["../../src/quota/quota.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"quota.d.ts","sourceRoot":"","sources":["../../src/quota/quota.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACnF,OAAO,EAAyB,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAG7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,gCAAgC;AAChC,MAAM,WAAW,UAAU;IACzB,4BAA4B;IAC5B,EAAE,EAAE,eAAe,CAAC;IACpB,6FAA6F;IAC7F,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACv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