ocel 0.0.1-alpha.1 → 0.0.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.
Files changed (271) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +2 -0
  3. package/README.md +98 -0
  4. package/dist/binding/binding.d.ts +22 -0
  5. package/dist/binding/binding.js +60 -0
  6. package/dist/binding/unprovisioned.d.ts +17 -0
  7. package/dist/binding/unprovisioned.js +25 -0
  8. package/dist/bucket/bucket-client.d.ts +4 -0
  9. package/dist/bucket/bucket-client.js +5 -0
  10. package/dist/bucket/bucket-context.d.ts +8 -0
  11. package/dist/bucket/bucket-context.js +10 -0
  12. package/dist/bucket/bucket.d.ts +60 -0
  13. package/dist/bucket/bucket.js +91 -0
  14. package/dist/bucket/client.d.ts +49 -0
  15. package/dist/bucket/client.js +95 -0
  16. package/dist/bucket/errors.d.ts +12 -0
  17. package/dist/bucket/errors.js +20 -0
  18. package/dist/bucket/express.d.ts +10 -0
  19. package/dist/bucket/express.js +24 -0
  20. package/dist/bucket/hono.d.ts +18 -0
  21. package/dist/bucket/hono.js +13 -0
  22. package/dist/bucket/index.d.ts +8 -0
  23. package/dist/bucket/index.js +6 -0
  24. package/dist/bucket/keys.d.ts +5 -0
  25. package/dist/bucket/keys.js +24 -0
  26. package/dist/bucket/metadata.d.ts +6 -0
  27. package/dist/bucket/metadata.js +6 -0
  28. package/dist/bucket/next.d.ts +15 -0
  29. package/dist/bucket/next.js +11 -0
  30. package/dist/bucket/objects.d.ts +130 -0
  31. package/dist/bucket/objects.js +350 -0
  32. package/dist/bucket/route.d.ts +13 -0
  33. package/dist/bucket/route.js +247 -0
  34. package/dist/bucket/server-only.js +5 -0
  35. package/dist/bucket/types.d.ts +63 -0
  36. package/dist/bucket/uploader.d.ts +3 -0
  37. package/dist/bucket/uploader.js +6 -0
  38. package/dist/build-env.d.ts +17 -0
  39. package/dist/build-env.js +41 -0
  40. package/dist/config.d.ts +30 -0
  41. package/dist/config.js +9 -0
  42. package/dist/declaration/callsite.d.ts +15 -0
  43. package/dist/declaration/callsite.js +43 -0
  44. package/dist/declaration/component.d.ts +4 -0
  45. package/dist/declaration/component.js +1 -0
  46. package/dist/declaration/defer.d.ts +5 -0
  47. package/dist/declaration/defer.js +9 -0
  48. package/dist/delivery/duration.d.ts +13 -0
  49. package/dist/delivery/duration.js +36 -0
  50. package/dist/delivery/lane.d.ts +4 -0
  51. package/dist/delivery/lane.js +15 -0
  52. package/dist/delivery/payload.d.ts +6 -0
  53. package/dist/delivery/payload.js +14 -0
  54. package/dist/delivery/retry.d.ts +16 -0
  55. package/dist/delivery/retry.js +10 -0
  56. package/dist/delivery/schema.d.ts +10 -0
  57. package/dist/delivery/schema.js +20 -0
  58. package/dist/dns.d.ts +11 -0
  59. package/dist/dns.js +4 -0
  60. package/dist/edge.d.ts +4 -0
  61. package/dist/edge.js +4 -0
  62. package/dist/env/access.d.ts +22 -0
  63. package/dist/env/access.js +49 -0
  64. package/dist/env/client.d.ts +5 -0
  65. package/dist/env/client.js +10 -0
  66. package/dist/env/declare.d.ts +2 -0
  67. package/dist/env/declare.js +83 -0
  68. package/dist/env/definition.d.ts +46 -0
  69. package/dist/env/definition.js +164 -0
  70. package/dist/env/deployment.d.ts +23 -0
  71. package/dist/env/deployment.js +13 -0
  72. package/dist/env/edge.d.ts +10 -0
  73. package/dist/env/edge.js +30 -0
  74. package/dist/env/errors.d.ts +9 -0
  75. package/dist/env/errors.js +9 -0
  76. package/dist/env/file.d.ts +1 -0
  77. package/dist/env/file.js +27 -0
  78. package/dist/env/index.d.ts +10 -0
  79. package/dist/env/index.js +63 -0
  80. package/dist/env/live.d.ts +3 -0
  81. package/dist/env/live.js +12 -0
  82. package/dist/env/schema.d.ts +27 -0
  83. package/dist/env/schema.js +43 -0
  84. package/dist/env/scope.d.ts +7 -0
  85. package/dist/env/scope.js +45 -0
  86. package/dist/env/standard.d.ts +10 -0
  87. package/dist/env/standard.js +21 -0
  88. package/dist/env/value.d.ts +5 -0
  89. package/dist/env/value.js +26 -0
  90. package/dist/env-source.d.ts +17 -0
  91. package/dist/env-source.js +16 -0
  92. package/dist/gen/proto/app/bucket/v1/bucket_pb.d.ts +911 -0
  93. package/dist/gen/proto/app/bucket/v1/bucket_pb.js +253 -0
  94. package/dist/gen/proto/app/realtime/v1/realtime_pb.d.ts +50 -0
  95. package/dist/gen/proto/app/realtime/v1/realtime_pb.js +23 -0
  96. package/dist/gen/proto/app/resources/v1/resources_pb.d.ts +554 -0
  97. package/dist/gen/proto/app/resources/v1/resources_pb.js +214 -0
  98. package/dist/gen/proto/app/resources/v1/variables_pb.d.ts +231 -0
  99. package/dist/gen/proto/app/resources/v1/variables_pb.js +101 -0
  100. package/dist/gen/proto/app/task/v1/task_pb.d.ts +498 -0
  101. package/dist/gen/proto/app/task/v1/task_pb.js +151 -0
  102. package/dist/gen/proto/app/topic/v1/topic_pb.d.ts +456 -0
  103. package/dist/gen/proto/app/topic/v1/topic_pb.js +125 -0
  104. package/dist/gen/proto/buf/validate/validate_pb.d.ts +4833 -0
  105. package/dist/gen/proto/buf/validate/validate_pb.js +356 -0
  106. package/dist/gen/proto/common/bindings/v1/bindings_pb.d.ts +378 -0
  107. package/dist/gen/proto/common/bindings/v1/bindings_pb.js +140 -0
  108. package/dist/generated/config.d.ts +428 -0
  109. package/dist/generated/config.js +2 -0
  110. package/dist/kv/entries.d.ts +193 -0
  111. package/dist/kv/entries.js +325 -0
  112. package/dist/kv/errors.d.ts +12 -0
  113. package/dist/kv/errors.js +14 -0
  114. package/dist/kv/index.d.ts +6 -0
  115. package/dist/kv/index.js +3 -0
  116. package/dist/kv/ioredis.d.ts +2 -0
  117. package/dist/kv/ioredis.js +20 -0
  118. package/dist/kv/kv.d.ts +84 -0
  119. package/dist/kv/kv.js +177 -0
  120. package/dist/kv/pattern.d.ts +27 -0
  121. package/dist/kv/pattern.js +73 -0
  122. package/dist/kv/ttl.d.ts +22 -0
  123. package/dist/kv/ttl.js +21 -0
  124. package/dist/postgres/index.d.ts +21 -2
  125. package/dist/postgres/index.js +73 -2
  126. package/dist/postgres/pg.d.ts +5 -4
  127. package/dist/postgres/pg.js +16 -12
  128. package/dist/providers/aws/dns.d.ts +11 -0
  129. package/dist/providers/aws/dns.js +4 -0
  130. package/dist/providers/aws/edge.d.ts +6 -0
  131. package/dist/providers/aws/edge.js +8 -0
  132. package/dist/providers/aws/index.d.ts +4 -0
  133. package/dist/providers/aws/index.js +4 -0
  134. package/dist/providers/gcp/edge.d.ts +14 -0
  135. package/dist/providers/gcp/edge.js +14 -0
  136. package/dist/providers/gcp/index.d.ts +9 -0
  137. package/dist/providers/gcp/index.js +9 -0
  138. package/dist/providers/vps/index.d.ts +4 -0
  139. package/dist/providers/vps/index.js +4 -0
  140. package/dist/realtime/client-error.d.ts +30 -0
  141. package/dist/realtime/client-error.js +17 -0
  142. package/dist/realtime/client-event-socket.d.ts +8 -0
  143. package/dist/realtime/client-event-socket.js +169 -0
  144. package/dist/realtime/client-transport.d.ts +29 -0
  145. package/dist/realtime/client-transport.js +13 -0
  146. package/dist/realtime/client.d.ts +150 -0
  147. package/dist/realtime/client.js +345 -0
  148. package/dist/realtime/express.d.ts +18 -0
  149. package/dist/realtime/express.js +83 -0
  150. package/dist/realtime/handler.d.ts +56 -0
  151. package/dist/realtime/handler.js +287 -0
  152. package/dist/realtime/hono.d.ts +19 -0
  153. package/dist/realtime/hono.js +24 -0
  154. package/dist/realtime/index.d.ts +2 -0
  155. package/dist/realtime/index.js +2 -0
  156. package/dist/realtime/next.d.ts +26 -0
  157. package/dist/realtime/next.js +16 -0
  158. package/dist/realtime/realtime.d.ts +181 -0
  159. package/dist/realtime/realtime.js +205 -0
  160. package/dist/realtime/token.d.ts +20 -0
  161. package/dist/realtime/token.js +45 -0
  162. package/dist/realtime/transport.d.ts +11 -0
  163. package/dist/realtime/transport.js +15 -0
  164. package/dist/realtime/wire.d.ts +30 -0
  165. package/dist/realtime/wire.js +104 -0
  166. package/dist/runtime/dev-server.d.ts +8 -0
  167. package/dist/runtime/dev-server.js +8 -0
  168. package/dist/runtime/rpc.d.ts +23 -0
  169. package/dist/runtime/rpc.js +24 -0
  170. package/dist/runtime/transport.d.ts +11 -0
  171. package/dist/runtime/transport.js +35 -0
  172. package/dist/task/errors.d.ts +7 -0
  173. package/dist/task/errors.js +7 -0
  174. package/dist/task/hooks.d.ts +57 -0
  175. package/dist/task/hooks.js +1 -0
  176. package/dist/task/index.d.ts +9 -0
  177. package/dist/task/index.js +4 -0
  178. package/dist/task/runs.d.ts +74 -0
  179. package/dist/task/runs.js +82 -0
  180. package/dist/task/task.d.ts +117 -0
  181. package/dist/task/task.js +109 -0
  182. package/dist/topic/dead-letters.d.ts +53 -0
  183. package/dist/topic/dead-letters.js +62 -0
  184. package/dist/topic/index.d.ts +8 -0
  185. package/dist/topic/index.js +4 -0
  186. package/dist/topic/topic.d.ts +85 -0
  187. package/dist/topic/topic.js +117 -0
  188. package/dist/version.d.ts +2 -0
  189. package/dist/version.js +2 -0
  190. package/dist/worker/context.d.ts +36 -0
  191. package/dist/worker/context.js +1 -0
  192. package/dist/worker/deliver.d.ts +13 -0
  193. package/dist/worker/deliver.js +258 -0
  194. package/dist/worker/index.d.ts +4 -0
  195. package/dist/worker/index.js +2 -0
  196. package/dist/worker/registry.d.ts +28 -0
  197. package/dist/worker/registry.js +22 -0
  198. package/dist/worker/semaphore.d.ts +7 -0
  199. package/dist/worker/semaphore.js +36 -0
  200. package/dist/worker/worker.d.ts +21 -0
  201. package/dist/worker/worker.js +32 -0
  202. package/package.json +158 -57
  203. package/bin/run.js +0 -39
  204. package/dist/blob/bucket.d.ts +0 -55
  205. package/dist/blob/bucket.js +0 -65
  206. package/dist/blob/index.d.ts +0 -8
  207. package/dist/blob/index.js +0 -13
  208. package/dist/blob/integrations/client.d.ts +0 -25
  209. package/dist/blob/integrations/client.js +0 -71
  210. package/dist/blob/integrations/hono.d.ts +0 -31
  211. package/dist/blob/integrations/hono.js +0 -11
  212. package/dist/blob/mime-types.d.ts +0 -1
  213. package/dist/blob/parse.d.ts +0 -16
  214. package/dist/blob/parse.js +0 -69
  215. package/dist/blob/s3-listener.d.ts +0 -7
  216. package/dist/blob/s3-listener.js +0 -35
  217. package/dist/blob/types.d.ts +0 -128
  218. package/dist/blob/types.js +0 -1
  219. package/dist/blob/upload-error.d.ts +0 -35
  220. package/dist/blob/upload-error.js +0 -54
  221. package/dist/blob/uploader.d.ts +0 -67
  222. package/dist/blob/uploader.js +0 -323
  223. package/dist/internal/db/index.d.ts +0 -68
  224. package/dist/internal/db/index.js +0 -73
  225. package/dist/lambda/index.d.ts +0 -2
  226. package/dist/lambda/index.js +0 -4
  227. package/dist/lambda/integrations/express.d.ts +0 -3
  228. package/dist/lambda/integrations/express.js +0 -13
  229. package/dist/lambda/integrations/hono.d.ts +0 -3
  230. package/dist/lambda/integrations/hono.js +0 -13
  231. package/dist/lambda/lambda.d.ts +0 -43
  232. package/dist/lambda/lambda.js +0 -30
  233. package/dist/utils/component.d.ts +0 -3
  234. package/dist/utils/constants.d.ts +0 -2
  235. package/dist/utils/constants.js +0 -2
  236. package/dist/utils/nanoid.d.ts +0 -1
  237. package/dist/utils/nanoid.js +0 -6
  238. package/dist/utils/rpc.d.ts +0 -11
  239. package/dist/utils/rpc.js +0 -17
  240. package/dist/utils/stack.d.ts +0 -4
  241. package/dist/utils/stack.js +0 -23
  242. package/dist/workflow/index.d.ts +0 -3
  243. package/dist/workflow/index.js +0 -4
  244. package/dist/workflow/workflow.d.ts +0 -34
  245. package/dist/workflow/workflow.js +0 -146
  246. package/src/blob/bucket.ts +0 -92
  247. package/src/blob/index.ts +0 -34
  248. package/src/blob/integrations/client.ts +0 -127
  249. package/src/blob/integrations/hono.ts +0 -16
  250. package/src/blob/mime-types.ts +0 -68
  251. package/src/blob/parse.ts +0 -93
  252. package/src/blob/s3-listener.ts +0 -48
  253. package/src/blob/types.ts +0 -142
  254. package/src/blob/upload-error.ts +0 -67
  255. package/src/blob/uploader.ts +0 -426
  256. package/src/internal/db/index.ts +0 -78
  257. package/src/lambda/index.ts +0 -8
  258. package/src/lambda/integrations/express.ts +0 -25
  259. package/src/lambda/integrations/hono.ts +0 -25
  260. package/src/lambda/lambda.ts +0 -94
  261. package/src/postgres/index.ts +0 -5
  262. package/src/postgres/pg.ts +0 -33
  263. package/src/utils/component.ts +0 -3
  264. package/src/utils/constants.ts +0 -2
  265. package/src/utils/nanoid.ts +0 -9
  266. package/src/utils/rpc.ts +0 -28
  267. package/src/utils/stack.ts +0 -26
  268. package/src/workflow/index.ts +0 -9
  269. package/src/workflow/workflow.ts +0 -235
  270. /package/dist/{blob/mime-types.js → bucket/server-only.d.ts} +0 -0
  271. /package/dist/{utils/component.js → bucket/types.js} +0 -0
@@ -0,0 +1,49 @@
1
+ import type { Bucket } from "./bucket.js";
2
+ import type { AnyUploader, Uploader } from "./types.js";
3
+ type Uploaders<B> = B extends Bucket<infer U> ? U : never;
4
+ type UploaderName<B> = keyof Uploaders<B> & string;
5
+ type InputOf<B, K extends UploaderName<B>> = Uploaders<B>[K] extends Uploader<infer I, any, any> ? I : never;
6
+ type UploadArgs<B, K extends UploaderName<B>> = {
7
+ files: BrowserFile[];
8
+ } & (InputOf<B, K> extends undefined ? {
9
+ input?: undefined;
10
+ } : {
11
+ input: InputOf<B, K>;
12
+ });
13
+ export interface BrowserFile {
14
+ name: string;
15
+ size: number;
16
+ type: string;
17
+ }
18
+ interface FetchResponse {
19
+ ok: boolean;
20
+ status: number;
21
+ json(): Promise<any>;
22
+ }
23
+ type FetchLike = (url: string, init?: {
24
+ method?: string;
25
+ body?: unknown;
26
+ headers?: Record<string, string>;
27
+ }) => Promise<FetchResponse>;
28
+ export interface UploadClientOptions {
29
+ url: string;
30
+ pollIntervalMs?: number;
31
+ maxPollMs?: number;
32
+ fetch?: FetchLike;
33
+ }
34
+ export interface UploadResultFile {
35
+ key: string;
36
+ name: string;
37
+ }
38
+ export interface UploadCallbacks {
39
+ onClientUploadComplete?: (result: {
40
+ files: UploadResultFile[];
41
+ }) => void;
42
+ onError?: (error: Error) => void;
43
+ }
44
+ export declare function createUploadClient<B extends Bucket<Record<string, AnyUploader>>>(options: UploadClientOptions): {
45
+ upload: <K extends UploaderName<B>>(name: K, args: UploadArgs<B, K>, callbacks?: UploadCallbacks) => Promise<{
46
+ files: UploadResultFile[];
47
+ }>;
48
+ };
49
+ export {};
@@ -0,0 +1,95 @@
1
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
2
+ const pollCeilingMs = 15_000;
3
+ async function pollUntilTerminal(fetchImpl, baseUrl, sessionId, intervalMs, maxMs) {
4
+ const deadline = Date.now() + maxMs;
5
+ let wait = intervalMs;
6
+ for (;;) {
7
+ const res = await fetchImpl(`${baseUrl}?op=poll&sessionId=${encodeURIComponent(sessionId)}`);
8
+ const body = (await res.json());
9
+ if (body.state === "succeeded" || body.state === "expired")
10
+ return body;
11
+ if (Date.now() >= deadline)
12
+ return { state: "expired" };
13
+ await sleep(wait);
14
+ wait = Math.min(wait * 2, pollCeilingMs);
15
+ }
16
+ }
17
+ export function createUploadClient(options) {
18
+ const fetchImpl = options.fetch ?? globalThis.fetch;
19
+ const intervalMs = options.pollIntervalMs ?? 1000;
20
+ const maxMs = options.maxPollMs ?? 15 * 60 * 1000;
21
+ async function upload(name, args, callbacks = {}) {
22
+ try {
23
+ const presignRes = await fetchImpl(`${options.url}?op=presign`, {
24
+ method: "POST",
25
+ headers: { "content-type": "application/json" },
26
+ body: JSON.stringify({
27
+ uploader: name,
28
+ input: args.input,
29
+ files: args.files.map((f) => ({
30
+ name: f.name,
31
+ size: f.size,
32
+ mimeType: f.type,
33
+ })),
34
+ }),
35
+ });
36
+ if (!presignRes.ok) {
37
+ throw new Error(`presign failed (${presignRes.status})`);
38
+ }
39
+ const presign = (await presignRes.json());
40
+ await Promise.all(presign.files.map((target, i) => {
41
+ const method = target.method || "PUT";
42
+ const file = args.files[i];
43
+ if (target.fields && Object.keys(target.fields).length > 0) {
44
+ const form = new FormData();
45
+ for (const [name, value] of Object.entries(target.fields))
46
+ form.append(name, value);
47
+ if (target.contentDisposition) {
48
+ form.append("Content-Disposition", target.contentDisposition);
49
+ }
50
+ form.append("file", file);
51
+ return fetchImpl(target.url, { method, body: form }).then((res) => {
52
+ if (!res.ok) {
53
+ throw new Error(`upload ${method} failed (${res.status})`);
54
+ }
55
+ });
56
+ }
57
+ const headers = {
58
+ ...target.headers,
59
+ ...(target.contentDisposition
60
+ ? { "content-disposition": target.contentDisposition }
61
+ : {}),
62
+ };
63
+ return fetchImpl(target.url, {
64
+ method,
65
+ body: file,
66
+ headers,
67
+ }).then((res) => {
68
+ if (!res.ok) {
69
+ throw new Error(`upload ${method} failed (${res.status})`);
70
+ }
71
+ });
72
+ }));
73
+ await fetchImpl(`${options.url}?op=complete`, {
74
+ method: "POST",
75
+ headers: { "content-type": "application/json" },
76
+ body: JSON.stringify({ sessionId: presign.sessionId }),
77
+ });
78
+ const status = await pollUntilTerminal(fetchImpl, options.url, presign.sessionId, intervalMs, maxMs);
79
+ if (status.state !== "succeeded") {
80
+ throw new Error(status.state === "expired" ? "upload expired" : (status.error ?? "upload failed"));
81
+ }
82
+ const result = {
83
+ files: presign.files.map((t) => ({ key: t.key, name: t.name })),
84
+ };
85
+ callbacks.onClientUploadComplete?.(result);
86
+ return result;
87
+ }
88
+ catch (err) {
89
+ const error = err instanceof Error ? err : new Error(String(err));
90
+ callbacks.onError?.(error);
91
+ throw error;
92
+ }
93
+ }
94
+ return { upload };
95
+ }
@@ -0,0 +1,12 @@
1
+ /** Thrown when an operation that cannot answer with `null` names an object the bucket does not have. */
2
+ export declare class ObjectNotFoundError extends Error {
3
+ /** The key that named nothing. */
4
+ readonly key: string;
5
+ constructor(key: string);
6
+ }
7
+ /** Thrown when a write set `ifNoneMatch` or `ifMatch` and the object did not meet it. */
8
+ export declare class PreconditionFailedError extends Error {
9
+ /** The key whose current state refused the write. */
10
+ readonly key: string;
11
+ constructor(key: string);
12
+ }
@@ -0,0 +1,20 @@
1
+ /** Thrown when an operation that cannot answer with `null` names an object the bucket does not have. */
2
+ export class ObjectNotFoundError extends Error {
3
+ /** The key that named nothing. */
4
+ key;
5
+ constructor(key) {
6
+ super(`the bucket has no object under "${key}"`);
7
+ this.name = "ObjectNotFoundError";
8
+ this.key = key;
9
+ }
10
+ }
11
+ /** Thrown when a write set `ifNoneMatch` or `ifMatch` and the object did not meet it. */
12
+ export class PreconditionFailedError extends Error {
13
+ /** The key whose current state refused the write. */
14
+ key;
15
+ constructor(key) {
16
+ super(`the object under "${key}" did not meet the condition this write set`);
17
+ this.name = "PreconditionFailedError";
18
+ this.key = key;
19
+ }
20
+ }
@@ -0,0 +1,10 @@
1
+ import type { Request as ExpressRequest, RequestHandler } from "express";
2
+ import type { z } from "zod";
3
+ import type { Bucket } from "./bucket.js";
4
+ import { type RouteOptions } from "./route.js";
5
+ import type { ParsedInput, Uploader, UploaderAuth, UploaderUpload } from "./types.js";
6
+ export { Bucket, type BucketOptions, bucket } from "./bucket.js";
7
+ export type { RouteOptions } from "./route.js";
8
+ export type { CompletedFile, FileInfo, Limits, PathConfig, Uploader, } from "./types.js";
9
+ export declare function uploader<TInput extends z.ZodType | undefined = undefined, TMetadata = unknown>(auth: UploaderAuth<ExpressRequest, TInput, TMetadata>, upload?: UploaderUpload<TMetadata>): Uploader<ParsedInput<TInput>, TMetadata, ExpressRequest>;
10
+ export declare function createRouteHandler(bucket: Bucket, options?: RouteOptions): RequestHandler;
@@ -0,0 +1,24 @@
1
+ import { createRouteHandler as coreCreateRouteHandler } from "./route.js";
2
+ import { uploader as coreUploader } from "./uploader.js";
3
+ export { Bucket, bucket } from "./bucket.js";
4
+ export function uploader(auth, upload) {
5
+ return coreUploader(auth, upload);
6
+ }
7
+ async function sendResponse(res, webRes) {
8
+ res.status(webRes.status);
9
+ webRes.headers.forEach((value, key) => {
10
+ res.setHeader(key, value);
11
+ });
12
+ res.end(Buffer.from(await webRes.arrayBuffer()));
13
+ }
14
+ export function createRouteHandler(bucket, options) {
15
+ const { GET, POST } = coreCreateRouteHandler(bucket, options);
16
+ return (req, res, next) => {
17
+ if (req.originalUrl)
18
+ req.url = req.originalUrl;
19
+ const handler = req.method === "GET" ? GET : POST;
20
+ handler(req)
21
+ .then((webRes) => sendResponse(res, webRes))
22
+ .catch(next);
23
+ };
24
+ }
@@ -0,0 +1,18 @@
1
+ import type { Context } from "hono";
2
+ import type { z } from "zod";
3
+ import type { Bucket } from "./bucket.js";
4
+ import { type RouteOptions } from "./route.js";
5
+ import type { MaybePromise, ParsedInput, Uploader, UploaderUpload } from "./types.js";
6
+ export { Bucket, type BucketOptions, bucket } from "./bucket.js";
7
+ export type { RouteOptions } from "./route.js";
8
+ export type { CompletedFile, FileInfo, Limits, PathConfig, Uploader, } from "./types.js";
9
+ export interface HonoUploaderAuth<TInput extends z.ZodType | undefined, TMetadata> {
10
+ input?: TInput;
11
+ middleware: (ctx: {
12
+ c: Context;
13
+ input: ParsedInput<TInput>;
14
+ }) => MaybePromise<TMetadata>;
15
+ }
16
+ export declare function uploader<TInput extends z.ZodType | undefined = undefined, TMetadata = unknown>(auth: HonoUploaderAuth<TInput, TMetadata>, upload?: UploaderUpload<TMetadata>): Uploader<ParsedInput<TInput>, TMetadata, Context>;
17
+ export type HonoRouteHandler = (c: Context) => Promise<Response>;
18
+ export declare function createRouteHandler(bucket: Bucket, options?: RouteOptions): HonoRouteHandler;
@@ -0,0 +1,13 @@
1
+ import { createRouteHandler as coreCreateRouteHandler } from "./route.js";
2
+ import { uploader as coreUploader } from "./uploader.js";
3
+ export { Bucket, bucket } from "./bucket.js";
4
+ export function uploader(auth, upload) {
5
+ return coreUploader({
6
+ input: auth.input,
7
+ middleware: ({ req, input }) => auth.middleware({ c: req, input }),
8
+ }, upload);
9
+ }
10
+ export function createRouteHandler(bucket, options) {
11
+ const { GET, POST } = coreCreateRouteHandler(bucket, options);
12
+ return (c) => (c.req.method === "GET" ? GET(c.req.raw) : POST(c.req.raw, c));
13
+ }
@@ -0,0 +1,8 @@
1
+ export { UnprovisionedResourceError } from "../binding/unprovisioned.js";
2
+ export { Bucket, type BucketOptions, bucket } from "./bucket.js";
3
+ export { type BucketContext, resolveBucketContext, } from "./bucket-context.js";
4
+ export { ObjectNotFoundError, PreconditionFailedError } from "./errors.js";
5
+ export type { GetOptions, ListOptions, ObjectBody, ObjectInfo, ObjectListing, ObjectPage, PutBody, PutOptions, SignedUpload, SignedUploadOptions, SignedUrlOptions, } from "./objects.js";
6
+ export { createRouteHandler, type RouteHandlers, type RouteOptions, } from "./route.js";
7
+ export type { AnyUploader, CompletedFile, FileInfo, Limits, PathConfig, Uploader, UploaderAuth, UploaderUpload, UploadRequest, } from "./types.js";
8
+ export { uploader } from "./uploader.js";
@@ -0,0 +1,6 @@
1
+ export { UnprovisionedResourceError } from "../binding/unprovisioned.js";
2
+ export { Bucket, bucket } from "./bucket.js";
3
+ export { resolveBucketContext, } from "./bucket-context.js";
4
+ export { ObjectNotFoundError, PreconditionFailedError } from "./errors.js";
5
+ export { createRouteHandler, } from "./route.js";
6
+ export { uploader } from "./uploader.js";
@@ -0,0 +1,5 @@
1
+ import type { FileInfo, PathConfig } from "./types.js";
2
+ export declare function generateKey(path: PathConfig<unknown> | undefined, ctx: {
3
+ file: FileInfo;
4
+ metadata: unknown;
5
+ }): string;
@@ -0,0 +1,24 @@
1
+ function randomToken() {
2
+ return crypto.randomUUID().replace(/-/g, "").slice(0, 8);
3
+ }
4
+ function sanitize(name) {
5
+ const base = name.split(/[\\/]/).pop() ?? name;
6
+ return base.replace(/[^a-zA-Z0-9._-]/g, "-").replace(/^\.+/, "");
7
+ }
8
+ function withSuffix(name, token) {
9
+ const dot = name.lastIndexOf(".");
10
+ if (dot <= 0)
11
+ return `${name}-${token}`;
12
+ return `${name.slice(0, dot)}-${token}${name.slice(dot)}`;
13
+ }
14
+ export function generateKey(path, ctx) {
15
+ if (typeof path === "function") {
16
+ return path(ctx);
17
+ }
18
+ const prefix = path?.prefix ?? "";
19
+ let name = sanitize(ctx.file.name);
20
+ if (path?.randomSuffix) {
21
+ name = withSuffix(name, randomToken());
22
+ }
23
+ return `${prefix}${name}`;
24
+ }
@@ -0,0 +1,6 @@
1
+ export interface MetadataEnvelope {
2
+ uploader: string;
3
+ metadata: unknown;
4
+ }
5
+ export declare function encodeMetadata(envelope: MetadataEnvelope): Uint8Array;
6
+ export declare function decodeMetadata(bytes: Uint8Array): MetadataEnvelope;
@@ -0,0 +1,6 @@
1
+ export function encodeMetadata(envelope) {
2
+ return new TextEncoder().encode(JSON.stringify(envelope));
3
+ }
4
+ export function decodeMetadata(bytes) {
5
+ return JSON.parse(new TextDecoder().decode(bytes));
6
+ }
@@ -0,0 +1,15 @@
1
+ import "./server-only.js";
2
+ import type { NextRequest } from "next/server";
3
+ import type { z } from "zod";
4
+ import type { Bucket } from "./bucket.js";
5
+ import { type RouteOptions } from "./route.js";
6
+ import type { ParsedInput, Uploader, UploaderAuth, UploaderUpload } from "./types.js";
7
+ export { Bucket, type BucketOptions, bucket } from "./bucket.js";
8
+ export type { RouteOptions } from "./route.js";
9
+ export type { CompletedFile, FileInfo, Limits, PathConfig, Uploader, } from "./types.js";
10
+ export declare function uploader<TInput extends z.ZodType | undefined = undefined, TMetadata = unknown>(auth: UploaderAuth<NextRequest, TInput, TMetadata>, upload?: UploaderUpload<TMetadata>): Uploader<ParsedInput<TInput>, TMetadata, NextRequest>;
11
+ export interface NextRouteHandlers {
12
+ GET: (req: NextRequest) => Promise<Response>;
13
+ POST: (req: NextRequest) => Promise<Response>;
14
+ }
15
+ export declare function createRouteHandler(bucket: Bucket, options?: RouteOptions): NextRouteHandlers;
@@ -0,0 +1,11 @@
1
+ import "./server-only.js";
2
+ import { createRouteHandler as coreCreateRouteHandler } from "./route.js";
3
+ import { uploader as coreUploader } from "./uploader.js";
4
+ export { Bucket, bucket } from "./bucket.js";
5
+ export function uploader(auth, upload) {
6
+ return coreUploader(auth, upload);
7
+ }
8
+ export function createRouteHandler(bucket, options) {
9
+ const { GET, POST } = coreCreateRouteHandler(bucket, options);
10
+ return { GET, POST };
11
+ }
@@ -0,0 +1,130 @@
1
+ import type { BucketContext } from "./bucket-context.js";
2
+ /** What a bucket knows about one of its objects. */
3
+ export interface ObjectInfo {
4
+ /** The key the object is addressed by. */
5
+ key: string;
6
+ /** The object's length in bytes. */
7
+ size: number;
8
+ /** The store's opaque version tag for these bytes. */
9
+ etag: string;
10
+ /** The media type the object was written with. */
11
+ contentType: string;
12
+ /** When the object last took its current bytes, where the store reports it. */
13
+ uploadedAt: Date | undefined;
14
+ /** The user metadata written alongside the object. */
15
+ metadata: Record<string, string>;
16
+ }
17
+ /** An object's bytes, with the info that came back beside them. */
18
+ export interface ObjectBody {
19
+ /** What the bucket knows about the object. */
20
+ info: ObjectInfo;
21
+ /** The bytes, as a stream that may be read once. */
22
+ body: ReadableStream<Uint8Array>;
23
+ /** The bytes decoded as UTF-8. */
24
+ text(): Promise<string>;
25
+ /** The bytes parsed as JSON. */
26
+ json<T = unknown>(): Promise<T>;
27
+ /** The bytes. */
28
+ bytes(): Promise<Uint8Array>;
29
+ }
30
+ /** A body a caller may hand to `put`. */
31
+ export type PutBody = string | Uint8Array | Blob | ReadableStream<Uint8Array>;
32
+ /** How a write is conditioned and described. */
33
+ export interface PutOptions {
34
+ /** The media type to store the object under. */
35
+ contentType?: string;
36
+ /** The cache-control the store should serve the object with. */
37
+ cacheControl?: string;
38
+ /** User metadata to keep beside the object, capped at 2 KB. */
39
+ metadata?: Record<string, string>;
40
+ /** `"*"` writes only when no object exists under the key. */
41
+ ifNoneMatch?: "*";
42
+ /** Writes only when the object's current etag matches. */
43
+ ifMatch?: string;
44
+ /** Abandons the write, including every part in flight. */
45
+ signal?: AbortSignal;
46
+ }
47
+ /** Which bytes of an object to read. */
48
+ export interface GetOptions {
49
+ /** The byte range to read; `length` omitted reads to the end. */
50
+ range?: {
51
+ offset: number;
52
+ length?: number;
53
+ };
54
+ }
55
+ /** How a listing is bounded. */
56
+ export interface ListOptions {
57
+ /** Only keys under this prefix. */
58
+ prefix?: string;
59
+ /** How many objects one page contains, at most 1000. */
60
+ limit?: number;
61
+ }
62
+ /** One page of a listing. */
63
+ export interface ObjectPage {
64
+ /** The objects on this page. */
65
+ objects: ObjectInfo[];
66
+ /** The cursor that continues the listing, empty when this was the last page. */
67
+ cursor: string;
68
+ }
69
+ /** A listing, walked as an async iterable or one page at a time. */
70
+ export interface ObjectListing extends AsyncIterable<ObjectInfo> {
71
+ /** One page, starting at `cursor` when given. */
72
+ page(options?: {
73
+ cursor?: string;
74
+ }): Promise<ObjectPage>;
75
+ }
76
+ /** How a signed read is bounded. */
77
+ export interface SignedUrlOptions {
78
+ /** How long the url stays valid, in seconds. */
79
+ expiresIn?: number;
80
+ /** Serves the object as a download, under this filename when a string. */
81
+ download?: boolean | string;
82
+ }
83
+ /** How a signed write is bounded. */
84
+ export interface SignedUploadOptions {
85
+ /** How long the target stays valid, in seconds. */
86
+ expiresIn?: number;
87
+ /** The largest body the target accepts, in bytes. */
88
+ maxSize?: number;
89
+ /** The media type the target accepts. */
90
+ contentType?: string;
91
+ }
92
+ /** A target an uploader drives itself. */
93
+ export interface SignedUpload {
94
+ /** Where to send the body. */
95
+ url: string;
96
+ /** The HTTP method to send it with. */
97
+ method: string;
98
+ /** Headers the signature covers, which the caller must send unchanged. */
99
+ headers: Record<string, string>;
100
+ /** Form fields a POST upload sends with the body; empty for a PUT target. */
101
+ fields: Record<string, string>;
102
+ }
103
+ /** The object operations a bucket handle exposes. */
104
+ export interface BucketObjects {
105
+ /** Writes `body` under `key`, in one request or in parts, and returns the stored object. */
106
+ put(key: string, body: PutBody, options?: PutOptions): Promise<ObjectInfo>;
107
+ /** Reads the object under `key`, or `null` when the bucket has none. */
108
+ get(key: string, options?: GetOptions): Promise<ObjectBody | null>;
109
+ /** What the bucket knows about `key`, or `null` when it has none. */
110
+ head(key: string): Promise<ObjectInfo | null>;
111
+ /** Removes one key or many; a key that is not there is not an error. */
112
+ delete(key: string | string[]): Promise<void>;
113
+ /** The objects under a prefix. */
114
+ list(options?: ListOptions): ObjectListing;
115
+ /** Copies `source` to `destination` within the bucket. */
116
+ copy(source: string, destination: string): Promise<ObjectInfo>;
117
+ /** A url that reads `key` without a credential, for as long as it is valid. */
118
+ signedUrl(key: string, options?: SignedUrlOptions): Promise<string>;
119
+ /** A target that writes `key` without a credential, for as long as it is valid. */
120
+ signedUpload(key: string, options?: SignedUploadOptions): Promise<SignedUpload>;
121
+ /** The object's address on a bucket declared `public: true`. */
122
+ publicUrl(key: string): string;
123
+ }
124
+ type FetchLike = typeof globalThis.fetch;
125
+ /** Builds the object operations against a resolved runtime context. */
126
+ export declare function createObjects(deps: {
127
+ runtime: () => BucketContext;
128
+ fetch?: FetchLike;
129
+ }): BucketObjects;
130
+ export {};