@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.
Files changed (91) hide show
  1. package/dist/capability.d.ts +12 -3
  2. package/dist/capability.d.ts.map +1 -1
  3. package/dist/capability.js +35 -9
  4. package/dist/config/config.d.ts +20 -4
  5. package/dist/config/config.d.ts.map +1 -1
  6. package/dist/config/config.js +23 -6
  7. package/dist/data/storageObject.d.ts +12 -0
  8. package/dist/data/storageObject.d.ts.map +1 -1
  9. package/dist/data/storageObject.js +13 -2
  10. package/dist/data/tables.js +1 -1
  11. package/dist/error/errors.js +2 -2
  12. package/dist/http/handlers.d.ts +48 -16
  13. package/dist/http/handlers.d.ts.map +1 -1
  14. package/dist/http/handlers.js +169 -66
  15. package/dist/http/responses.d.ts +173 -0
  16. package/dist/http/responses.d.ts.map +1 -0
  17. package/dist/http/responses.js +92 -0
  18. package/dist/http/routes.d.ts +13 -2
  19. package/dist/http/routes.d.ts.map +1 -1
  20. package/dist/http/routes.js +134 -22
  21. package/dist/http/schemas.d.ts +6 -0
  22. package/dist/http/schemas.d.ts.map +1 -1
  23. package/dist/http/schemas.js +7 -6
  24. package/dist/http/scopes.d.ts +25 -0
  25. package/dist/http/scopes.d.ts.map +1 -0
  26. package/dist/http/scopes.js +32 -0
  27. package/dist/http/serve.d.ts +5 -2
  28. package/dist/http/serve.d.ts.map +1 -1
  29. package/dist/index.d.ts +2 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +5 -3
  32. package/dist/migrations/0002_ownership.d.ts +5 -0
  33. package/dist/migrations/0002_ownership.d.ts.map +1 -0
  34. package/dist/migrations/0002_ownership.js +114 -0
  35. package/dist/object/cloudflare.d.ts +3 -2
  36. package/dist/object/cloudflare.d.ts.map +1 -1
  37. package/dist/object/cloudflare.js +3 -3
  38. package/dist/object/store.d.ts +2 -7
  39. package/dist/object/store.d.ts.map +1 -1
  40. package/dist/object/store.js +1 -6
  41. package/dist/object/uploadedPart.d.ts +11 -0
  42. package/dist/object/uploadedPart.d.ts.map +1 -0
  43. package/dist/object/uploadedPart.js +12 -0
  44. package/dist/provision/provisionStorage.d.ts +14 -2
  45. package/dist/provision/provisionStorage.d.ts.map +1 -1
  46. package/dist/provision/provisionStorage.js +19 -4
  47. package/dist/provision/resolveStorageConfig.js +1 -1
  48. package/dist/quota/ceiling.d.ts +36 -0
  49. package/dist/quota/ceiling.d.ts.map +1 -0
  50. package/dist/quota/ceiling.js +31 -0
  51. package/dist/quota/quota.d.ts +24 -7
  52. package/dist/quota/quota.d.ts.map +1 -1
  53. package/dist/quota/quota.js +40 -12
  54. package/dist/secret/bundleToken.d.ts +24 -0
  55. package/dist/secret/bundleToken.d.ts.map +1 -0
  56. package/dist/secret/bundleToken.js +74 -0
  57. package/dist/secret/registry.d.ts +5 -1
  58. package/dist/secret/registry.d.ts.map +1 -1
  59. package/dist/secret/registry.js +7 -3
  60. package/dist/seeds/example.d.ts.map +1 -1
  61. package/dist/seeds/example.js +3 -0
  62. package/dist/test-utils/liveStorage.d.ts.map +1 -1
  63. package/dist/test-utils/liveStorage.js +5 -2
  64. package/dist/version.generated.d.ts +1 -1
  65. package/dist/version.generated.d.ts.map +1 -1
  66. package/dist/version.generated.js +1 -1
  67. package/package.json +7 -7
  68. package/pithy.manifest.json +20 -10
  69. package/src/capability.ts +52 -8
  70. package/src/config/config.ts +24 -6
  71. package/src/data/storageObject.ts +22 -6
  72. package/src/error/errors.ts +2 -2
  73. package/src/http/handlers.ts +215 -81
  74. package/src/http/responses.ts +168 -0
  75. package/src/http/routes.ts +245 -46
  76. package/src/http/schemas.ts +11 -5
  77. package/src/http/scopes.ts +39 -0
  78. package/src/http/serve.ts +5 -2
  79. package/src/index.ts +2 -0
  80. package/src/migrations/0002_ownership.ts +176 -0
  81. package/src/object/cloudflare.ts +4 -3
  82. package/src/object/store.ts +6 -10
  83. package/src/object/uploadedPart.ts +17 -0
  84. package/src/provision/provisionStorage.ts +22 -3
  85. package/src/quota/ceiling.ts +44 -0
  86. package/src/quota/quota.ts +63 -20
  87. package/src/secret/bundleToken.ts +81 -0
  88. package/src/secret/registry.ts +6 -8
  89. package/src/seeds/example.ts +3 -0
  90. package/src/test-utils/liveStorage.ts +3 -6
  91. package/src/version.generated.ts +1 -1
@@ -1,8 +1,9 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
+ import { VisibilityPolicy } from "@pithy-sh/core/src/access/visibility";
5
+ import { ValidationError } from "@pithy-sh/core/src/error/pithyError";
4
6
  import { z } from "zod";
5
- import { StorageVisibility } from "../data/storageObject";
6
7
  import {
7
8
  DEFAULT_MULTIPART_THRESHOLD_BYTES,
8
9
  DEFAULT_PART_SIZE_BYTES,
@@ -25,8 +26,9 @@ import {
25
26
  * fixes the practical object ceiling: 10,000 × 64 MiB ≈ 625 GiB. Raise `partSizeBytes` to raise the
26
27
  * ceiling (R2's own object cap is 4.995 TiB); the cost is that every retry re-sends a larger part.
27
28
  *
28
- * **Default visibility is private.** The safe default is the one where forgetting to set a field
29
- * cannot leak a file.
29
+ * **Default visibility is private, and the project decides which levels exist.** The safe default is the
30
+ * one where forgetting to set a field cannot leak a file, and `visibility.allowed` — all three levels
31
+ * unless narrowed — is how a project promises that a file can never be published at all.
30
32
  */
31
33
 
32
34
  /** The largest single-part upload R2 accepts — 5 GiB, less 5 MiB. Above it, multipart is not optional. */
@@ -68,8 +70,8 @@ export const StorageConfig = z
68
70
  .describe(
69
71
  "Bytes per multipart part, for every part but the last. R2 allows at most 10,000 parts, so this fixes the largest object you can store: 64 MiB gives ~625 GiB. The floor is S3's 5 MiB rule for non-final parts.",
70
72
  ),
71
- defaultVisibility: StorageVisibility.default("private").describe(
72
- "Visibility for an upload that does not name one. Private, so a forgotten field can never publish a file.",
73
+ visibility: VisibilityPolicy.prefault({}).describe(
74
+ "Which visibility levels a file may carry, and the one an upload gets when it names none. Private by default; every level allowed unless narrowed.",
73
75
  ),
74
76
  pendingTtlSeconds: z
75
77
  .number()
@@ -80,7 +82,7 @@ export const StorageConfig = z
80
82
  "How long a `pending` row holds its quota reservation before the orphan sweep reclaims it. Long enough for a slow multipart upload to finish; short enough that an abandoned one does not hold a quota forever.",
81
83
  ),
82
84
  })
83
- .describe("Configuration for the storage capability — quotas, multipart sizing, and default visibility.")
85
+ .describe("Configuration for the storage capability — quotas, multipart sizing, and the visibility policy.")
84
86
  .check((ctx) => {
85
87
  // A ceiling below the part size would mean the first "multipart" upload is a single short part,
86
88
  // which R2 accepts but which makes the threshold meaningless. Say so rather than silently allowing it.
@@ -103,3 +105,19 @@ export type StorageConfigInput = z.input<typeof StorageConfig>;
103
105
  export function maxObjectBytes(config: StorageConfig): number {
104
106
  return config.partSizeBytes * MAX_UPLOAD_PARTS;
105
107
  }
108
+
109
+ /**
110
+ * Refuse a config that still names an option a release renamed, naming the new one.
111
+ *
112
+ * `defaultVisibility` became `visibility.default`, beside `visibility.allowed`. Zod would strip the old key
113
+ * and parse the rest, so a project upgrading without the edit would quietly get the default it may have
114
+ * meant to change — refused here instead, before the config is parsed.
115
+ */
116
+ export function refuseRenamedStorageOptions(input: Record<string, unknown>): void {
117
+ if (!("defaultVisibility" in input)) return;
118
+ throw new ValidationError({
119
+ message: "`defaultVisibility` was renamed `visibility.default`.",
120
+ action: `Replace \`defaultVisibility: ${JSON.stringify(input.defaultVisibility)}\` with \`visibility: { default: ${JSON.stringify(input.defaultVisibility)} }\` in storage({ … }) in this Worker's pithy.config.ts.`,
121
+ detail: "storage({ defaultVisibility }) names an option this release renamed",
122
+ });
123
+ }
@@ -1,6 +1,7 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
+ import { RecordVisibility } from "@pithy-sh/core/src/access/visibility";
4
5
  import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
5
6
  import { z } from "zod";
6
7
 
@@ -20,13 +21,20 @@ import { z } from "zod";
20
21
  * `size` is declared by the client at init and *confirmed* against R2 at completion. It is nullable
21
22
  * because a caller may not know it up front; a null size reserves no quota, which is why the handler
22
23
  * requires it whenever a quota is configured.
24
+ *
25
+ * **Whose it is.** `ownerId` is the authenticated user and `organizationId` the organization they were
26
+ * acting for when the file was written — the same two columns, in the same terms, as a media record, so
27
+ * one name answers *what is this account holding* across both. The organization is the fact at the time
28
+ * of the write, never a join through current membership.
23
29
  */
24
30
 
25
- export const StorageVisibility = z
26
- .enum(["private", "public"])
27
- .describe(
28
- "Who may read an object without a share link: `private` is the owner alone, `public` is anyone with the id. Visibility is authorization, not a bucket setting — the bucket itself stays private.",
29
- );
31
+ /**
32
+ * Who may read an object without a share link — core's three levels, the same ones media uses. Visibility
33
+ * is authorization, not a bucket setting: the bucket itself stays private.
34
+ */
35
+ export const StorageVisibility = RecordVisibility.describe(
36
+ "Who may read an object without a share link: `private` is its owner, acting for its organization; `organization` is any member acting for it; `public` is anyone with the id. Visibility is authorization, not a bucket setting — the bucket itself stays private.",
37
+ );
30
38
  export type StorageVisibility = z.infer<typeof StorageVisibility>;
31
39
 
32
40
  export const StorageObjectStatus = z
@@ -51,6 +59,12 @@ export const StorageObject = z
51
59
  .string()
52
60
  .nullable()
53
61
  .describe("The authenticated user who owns the file, from AuthContext. Null for system-owned objects."),
62
+ organizationId: z
63
+ .string()
64
+ .nullable()
65
+ .describe(
66
+ "The organization the owner was acting for when the file was written, from the acting-organization seam. Null when none was in force, and for system-owned objects.",
67
+ ),
54
68
  contentType: z
55
69
  .string()
56
70
  .min(1)
@@ -63,7 +77,9 @@ export const StorageObject = z
63
77
  .nonnegative()
64
78
  .nullable()
65
79
  .describe("Byte count. Declared at init so the pending row reserves quota; confirmed against R2 at completion."),
66
- visibility: StorageVisibility.describe("Whether anyone with the id may read this object, or only its owner."),
80
+ visibility: StorageVisibility.describe(
81
+ "Who may read this object: its owner, its organization's members, or anyone with the id.",
82
+ ),
67
83
  checksum: z
68
84
  .string()
69
85
  .nullable()
@@ -47,8 +47,8 @@ export class StorageForbiddenError extends PithyError {
47
47
  {
48
48
  code: "storage/forbidden",
49
49
  status: 403,
50
- message: args.message ?? "That file is not yours.",
51
- action: args.action ?? "Sign in as the owner, or ask them for a share link.",
50
+ message: args.message ?? "You can see that file, but it is not yours to change.",
51
+ action: args.action ?? "Sign in as the owner, or ask a role holding storage:manage in its organization.",
52
52
  detail: args.detail,
53
53
  params: args.params,
54
54
  },