create-feedbax 0.1.0 → 0.2.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/LICENSE +201 -21
- package/README.md +11 -1
- package/dist/cli.js +3283 -22
- package/dist/index.js +3394 -160
- package/package.json +40 -14
- package/template/.feedbax-template.sha256 +1 -0
- package/template/.gitignore.template +6 -0
- package/template/.npmignore.template +5 -0
- package/template/.prettierignore.template +4 -0
- package/template/AGENTS.md +13 -0
- package/template/CONNECTED.md +404 -0
- package/template/LICENSE +201 -0
- package/template/README.md +56 -5
- package/template/components.json +25 -0
- package/template/eslint.config.mjs +48 -0
- package/template/migrations/0001_connected.sql +6 -0
- package/template/migrations/0002_participation.sql +8 -0
- package/template/migrations/0003_images.sql +3 -0
- package/template/migrations/0004_followed_status.sql +9 -0
- package/template/package.json +74 -14
- package/template/public/feedbax-mark.svg +4 -0
- package/template/public/robots.txt +3 -0
- package/template/src/authorized-draft-post.test.tsx +212 -0
- package/template/src/authorized-draft-post.tsx +205 -0
- package/template/src/authorized-draft-query.ts +74 -0
- package/template/src/browser-post-state.test.ts +120 -0
- package/template/src/browser-post-state.ts +134 -0
- package/template/src/browser-vote-state.test.ts +14 -0
- package/template/src/browser-vote-state.ts +27 -0
- package/template/src/capability-authorization-error.ts +3 -0
- package/template/src/changelog-page.test.tsx +139 -0
- package/template/src/changelog-page.tsx +228 -0
- package/template/src/changelog-query.ts +13 -0
- package/template/src/changelog-runtime.ts +27 -0
- package/template/src/cloudflare-turnstile.ts +31 -0
- package/template/src/cloudflare-workers-test.ts +3 -0
- package/template/src/cloudflare-workers.d.ts +24 -0
- package/template/src/comment-creation.test.tsx +311 -0
- package/template/src/comment-profile.ts +5 -0
- package/template/src/comment-thread-page.ts +28 -0
- package/template/src/components/command-palette-context.tsx +71 -0
- package/template/src/components/command-palette.tsx +275 -0
- package/template/src/components/connected-identity-provider.tsx +108 -0
- package/template/src/components/create-post-trigger.tsx +44 -0
- package/template/src/components/device-profile-provider.tsx +324 -0
- package/template/src/components/follow-post-button.tsx +89 -0
- package/template/src/components/header.tsx +97 -0
- package/template/src/components/image-attachments.tsx +187 -0
- package/template/src/components/loader.tsx +53 -0
- package/template/src/components/notification-inbox.tsx +232 -0
- package/template/src/components/portal-footer.tsx +14 -0
- package/template/src/components/profile-menu.tsx +119 -0
- package/template/src/components/ui/components/alert-dialog.tsx +185 -0
- package/template/src/components/ui/components/attachment.tsx +207 -0
- package/template/src/components/ui/components/avatar.tsx +107 -0
- package/template/src/components/ui/components/badge.tsx +52 -0
- package/template/src/components/ui/components/bubble.tsx +128 -0
- package/template/src/components/ui/components/button-group.tsx +87 -0
- package/template/src/components/ui/components/button.tsx +56 -0
- package/template/src/components/ui/components/card.tsx +103 -0
- package/template/src/components/ui/components/checkbox.tsx +26 -0
- package/template/src/components/ui/components/command.tsx +195 -0
- package/template/src/components/ui/components/dialog.tsx +154 -0
- package/template/src/components/ui/components/drawer.tsx +229 -0
- package/template/src/components/ui/components/dropdown-menu.tsx +270 -0
- package/template/src/components/ui/components/empty.tsx +101 -0
- package/template/src/components/ui/components/field.tsx +236 -0
- package/template/src/components/ui/components/input-group.tsx +155 -0
- package/template/src/components/ui/components/input.tsx +20 -0
- package/template/src/components/ui/components/item.tsx +201 -0
- package/template/src/components/ui/components/kbd.tsx +26 -0
- package/template/src/components/ui/components/label.tsx +18 -0
- package/template/src/components/ui/components/marker.tsx +71 -0
- package/template/src/components/ui/components/message-scroller.tsx +128 -0
- package/template/src/components/ui/components/message.tsx +92 -0
- package/template/src/components/ui/components/native-select.tsx +65 -0
- package/template/src/components/ui/components/popover.tsx +88 -0
- package/template/src/components/ui/components/portal-primitives.test.tsx +85 -0
- package/template/src/components/ui/components/radio-group.tsx +36 -0
- package/template/src/components/ui/components/scroll-area.tsx +51 -0
- package/template/src/components/ui/components/secondary-form-actions.test.tsx +23 -0
- package/template/src/components/ui/components/select.tsx +199 -0
- package/template/src/components/ui/components/separator.tsx +23 -0
- package/template/src/components/ui/components/sheet.tsx +135 -0
- package/template/src/components/ui/components/skeleton.tsx +13 -0
- package/template/src/components/ui/components/sonner.tsx +43 -0
- package/template/src/components/ui/components/spinner.tsx +16 -0
- package/template/src/components/ui/components/switch.tsx +29 -0
- package/template/src/components/ui/components/tabs.tsx +80 -0
- package/template/src/components/ui/components/textarea.tsx +18 -0
- package/template/src/components/ui/components/tooltip.tsx +64 -0
- package/template/src/components/ui/hooks/.gitkeep +0 -0
- package/template/src/components/ui/lib/utils.ts +6 -0
- package/template/src/components/ui/styles/globals.css +151 -0
- package/template/src/connected-identity.test.ts +144 -0
- package/template/src/connected-identity.ts +122 -0
- package/template/src/connected-participation.test.tsx +67 -0
- package/template/src/connected-runtime.test.ts +241 -0
- package/template/src/connected-runtime.ts +176 -0
- package/template/src/connected-social.test.tsx +158 -0
- package/template/src/create-post-overlay.tsx +82 -0
- package/template/src/draft-post-list.tsx +39 -0
- package/template/src/feedback-cache-invalidation.test.ts +185 -0
- package/template/src/feedback-cache-invalidation.ts +98 -0
- package/template/src/feedback-runtime.ts +105 -0
- package/template/src/feedbax/changelog/index.test.ts +156 -0
- package/template/src/feedbax/changelog/index.ts +206 -0
- package/template/src/feedbax/changelog/notion-changelog.test.ts +395 -0
- package/template/src/feedbax/changelog/notion-changelog.ts +400 -0
- package/template/src/feedbax/config/index.test.ts +139 -0
- package/template/src/feedbax/config/index.ts +364 -0
- package/template/src/feedbax/feedback/connected-feedback.ts +206 -0
- package/template/src/feedbax/feedback/connected.test.ts +272 -0
- package/template/src/feedbax/feedback/connected.ts +215 -0
- package/template/src/feedbax/feedback/email-delivery.test.ts +65 -0
- package/template/src/feedbax/feedback/email-delivery.ts +66 -0
- package/template/src/feedbax/feedback/feedback-storage.ts +43 -0
- package/template/src/feedbax/feedback/identity-handoff.test.ts +81 -0
- package/template/src/feedbax/feedback/identity-handoff.ts +119 -0
- package/template/src/feedbax/feedback/image-feedback.ts +101 -0
- package/template/src/feedbax/feedback/images.test.ts +223 -0
- package/template/src/feedbax/feedback/images.ts +324 -0
- package/template/src/feedbax/feedback/index.test.ts +900 -0
- package/template/src/feedbax/feedback/index.ts +996 -0
- package/template/src/feedbax/feedback/notion-comments.test.ts +171 -0
- package/template/src/feedbax/feedback/notion-comments.ts +231 -0
- package/template/src/feedbax/feedback/notion-data-source.test.ts +364 -0
- package/template/src/feedbax/feedback/notion-data-source.ts +458 -0
- package/template/src/feedbax/feedback/notion-feedback.test.ts +998 -0
- package/template/src/feedbax/feedback/notion-feedback.ts +651 -0
- package/template/src/feedbax/feedback/notion-image-review.ts +55 -0
- package/template/src/feedbax/feedback/notion-request.ts +74 -0
- package/template/src/feedbax/feedback/participation.test.ts +653 -0
- package/template/src/feedbax/feedback/participation.ts +502 -0
- package/template/src/feedbax.test-fixture.ts +64 -0
- package/template/src/feedbax.ts +69 -0
- package/template/src/images-runtime.test.ts +133 -0
- package/template/src/images-runtime.ts +125 -0
- package/template/src/index.css +1795 -0
- package/template/src/lazy-create-post-overlay.tsx +6 -0
- package/template/src/lazy-post-detail-overlay.tsx +21 -0
- package/template/src/masked-post-link.tsx +27 -0
- package/template/src/overlay-post-detail.test.tsx +181 -0
- package/template/src/overlay-post-detail.tsx +43 -0
- package/template/src/participation-client.ts +63 -0
- package/template/src/participation-runtime.test.ts +35 -0
- package/template/src/participation-runtime.ts +255 -0
- package/template/src/portal-comments.test.ts +375 -0
- package/template/src/portal-comments.ts +403 -0
- package/template/src/portal-features.test.tsx +150 -0
- package/template/src/portal-feedback-form.tsx +530 -0
- package/template/src/portal-feedback-runtime.test.ts +32 -0
- package/template/src/portal-feedback-runtime.ts +58 -0
- package/template/src/portal-feedback-server-function.ts +96 -0
- package/template/src/portal-feedback-submission.test.ts +307 -0
- package/template/src/portal-feedback-submission.ts +224 -0
- package/template/src/portal-head.ts +58 -0
- package/template/src/portal-server-entry.ts +68 -0
- package/template/src/portal-voting.test.ts +106 -0
- package/template/src/portal-voting.ts +149 -0
- package/template/src/post-badges.tsx +27 -0
- package/template/src/post-creation-flow.test.tsx +230 -0
- package/template/src/post-creation-flow.tsx +88 -0
- package/template/src/post-detail-content.test.tsx +233 -0
- package/template/src/post-detail-content.tsx +162 -0
- package/template/src/post-detail-overlay.test.tsx +114 -0
- package/template/src/post-detail-overlay.tsx +67 -0
- package/template/src/post-detail-query.ts +34 -0
- package/template/src/post-feed-controls.test.tsx +239 -0
- package/template/src/post-feed-controls.tsx +298 -0
- package/template/src/post-presentation.ts +37 -0
- package/template/src/post-queries.test.ts +158 -0
- package/template/src/post-queries.ts +26 -0
- package/template/src/post-results.tsx +165 -0
- package/template/src/public-cache-policy.test.ts +148 -0
- package/template/src/public-cache-policy.ts +119 -0
- package/template/src/public-cache-tags.ts +7 -0
- package/template/src/public-changelog-page.ts +15 -0
- package/template/src/public-changelog-server-function.ts +27 -0
- package/template/src/public-date.ts +11 -0
- package/template/src/public-failure-state.test.tsx +69 -0
- package/template/src/public-feedback-index.tsx +94 -0
- package/template/src/public-feedback-page.test.tsx +355 -0
- package/template/src/public-feedback-page.ts +86 -0
- package/template/src/public-feedback-server-function.ts +22 -0
- package/template/src/public-portal-error.tsx +27 -0
- package/template/src/public-post-detail-skeleton.tsx +54 -0
- package/template/src/public-post-detail.tsx +772 -0
- package/template/src/public-post-page.test.tsx +173 -0
- package/template/src/public-post-page.ts +15 -0
- package/template/src/public-post-route.test.tsx +250 -0
- package/template/src/public-post-route.tsx +158 -0
- package/template/src/public-post-server-function.ts +48 -0
- package/template/src/public-post-unavailable.tsx +10 -0
- package/template/src/public-roadmap-page.test.tsx +139 -0
- package/template/src/public-roadmap-page.ts +24 -0
- package/template/src/public-roadmap-route.test.tsx +254 -0
- package/template/src/public-roadmap-route.tsx +50 -0
- package/template/src/public-roadmap-server-function.ts +44 -0
- package/template/src/public-roadmap-view.tsx +411 -0
- package/template/src/public-shell.test.tsx +544 -0
- package/template/src/roadmap-query.ts +21 -0
- package/template/src/root-branding.test.ts +53 -0
- package/template/src/routeTree.gen.ts +177 -0
- package/template/src/routed-post-creation-form.tsx +28 -0
- package/template/src/router.tsx +27 -0
- package/template/src/routes/__root.tsx +114 -0
- package/template/src/routes/changelog.tsx +39 -0
- package/template/src/routes/email-preferences.tsx +77 -0
- package/template/src/routes/index.tsx +61 -0
- package/template/src/routes/p.$slug.tsx +16 -0
- package/template/src/routes/roadmap.tsx +11 -0
- package/template/src/routes/submit.tsx +28 -0
- package/template/src/safe-public-failure.ts +47 -0
- package/template/src/server.ts +151 -0
- package/template/src/similar-posts.tsx +45 -0
- package/template/src/submit-route.test.tsx +134 -0
- package/template/src/trusted-feedback-handler.test.ts +425 -0
- package/template/src/trusted-feedback-handler.ts +181 -0
- package/template/src/trusted-feedback-runtime.ts +17 -0
- package/template/src/turnstile-configuration.test.ts +37 -0
- package/template/src/turnstile-configuration.ts +19 -0
- package/template/src/use-media-query.ts +17 -0
- package/template/src/vote-toggle.test.tsx +171 -0
- package/template/src/vote-toggle.tsx +244 -0
- package/template/tsconfig.json +45 -0
- package/template/vite.config.ts +20 -0
- package/template/vitest.config.ts +4 -0
- package/template/vitest.setup.ts +3 -0
- package/template/wrangler.jsonc +35 -0
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/index.d.ts +0 -21
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/template/.env.example +0 -4
- package/template/apps/portal/components.json +0 -20
- package/template/apps/portal/package.json +0 -31
- package/template/apps/portal/src/lib/utils.ts +0 -6
- package/template/apps/portal/src/routeTree.gen.ts +0 -59
- package/template/apps/portal/src/router.tsx +0 -10
- package/template/apps/portal/src/routes/__root.tsx +0 -31
- package/template/apps/portal/src/routes/index.tsx +0 -35
- package/template/apps/portal/src/styles.css +0 -91
- package/template/apps/portal/tsconfig.json +0 -15
- package/template/apps/portal/vite.config.ts +0 -13
- package/template/feedbax.config.ts +0 -13
- package/template/feedbax.jsonc +0 -11
- package/template/pnpm-workspace.yaml +0 -3
- package/template/turbo.json +0 -8
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import { Miniflare } from "miniflare";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import {
|
|
6
|
+
createConnectedStore,
|
|
7
|
+
createFeedbackModule,
|
|
8
|
+
createImageModule,
|
|
9
|
+
} from "@feedbax/feedback";
|
|
10
|
+
import { createImageRequestHandler } from "./images-runtime";
|
|
11
|
+
import { readSessionToken, sessionHash } from "./connected-identity";
|
|
12
|
+
it("authenticates uploads, bounds image bodies, and serves private objects only through authorized no-store responses", async () => {
|
|
13
|
+
const runtime = new Miniflare({
|
|
14
|
+
modules: true,
|
|
15
|
+
script: "export default {fetch(){return new Response('ok')}}",
|
|
16
|
+
compatibilityDate: "2026-07-30",
|
|
17
|
+
d1Databases: ["DB"],
|
|
18
|
+
r2Buckets: ["IMAGES"],
|
|
19
|
+
});
|
|
20
|
+
try {
|
|
21
|
+
const database = await runtime.getD1Database("DB");
|
|
22
|
+
for (const name of [
|
|
23
|
+
"0001_connected.sql",
|
|
24
|
+
"0002_participation.sql",
|
|
25
|
+
"0003_images.sql",
|
|
26
|
+
])
|
|
27
|
+
await database.exec(
|
|
28
|
+
await readFile(
|
|
29
|
+
`${existsSync("apps/portal/migrations") ? "apps/portal/migrations" : "migrations"}/${name}`,
|
|
30
|
+
"utf8",
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
const store = createConnectedStore(database);
|
|
34
|
+
await store.acceptIdentity({
|
|
35
|
+
subject: "alice",
|
|
36
|
+
displayName: "Alice",
|
|
37
|
+
nonce: "a",
|
|
38
|
+
sessionHash: sessionHash("session"),
|
|
39
|
+
expiresAt: Date.now() + 60000,
|
|
40
|
+
});
|
|
41
|
+
const participant = async (request: Request) => {
|
|
42
|
+
const token = readSessionToken(request);
|
|
43
|
+
return token ? store.session(sessionHash(token)) : undefined;
|
|
44
|
+
};
|
|
45
|
+
const bucket = await runtime.getR2Bucket("IMAGES");
|
|
46
|
+
const feedback = createFeedbackModule();
|
|
47
|
+
const handler = createImageRequestHandler({
|
|
48
|
+
enabled: true,
|
|
49
|
+
siteUrl: "https://feedback.example.com",
|
|
50
|
+
signedIn: async (request) => Boolean(await participant(request)),
|
|
51
|
+
getImages: async (request) =>
|
|
52
|
+
createImageModule({
|
|
53
|
+
database,
|
|
54
|
+
bucket,
|
|
55
|
+
feedback,
|
|
56
|
+
participant: await participant(request),
|
|
57
|
+
reviewSecret: "s".repeat(32),
|
|
58
|
+
}),
|
|
59
|
+
rateLimit: async () => true,
|
|
60
|
+
});
|
|
61
|
+
const png = Uint8Array.from(
|
|
62
|
+
Buffer.from(
|
|
63
|
+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aE2sAAAAASUVORK5CYII=",
|
|
64
|
+
"base64",
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
const headers = {
|
|
68
|
+
Origin: "https://feedback.example.com",
|
|
69
|
+
"Content-Type": "image/png",
|
|
70
|
+
Cookie: "__Host-feedbax-session=session",
|
|
71
|
+
};
|
|
72
|
+
expect(
|
|
73
|
+
(
|
|
74
|
+
await handler(
|
|
75
|
+
new Request("https://feedback.example.com/internal/images", {
|
|
76
|
+
method: "POST",
|
|
77
|
+
headers: { ...headers, Origin: "https://evil.example.com" },
|
|
78
|
+
body: png,
|
|
79
|
+
}),
|
|
80
|
+
)
|
|
81
|
+
)?.status,
|
|
82
|
+
).toBe(403);
|
|
83
|
+
expect(
|
|
84
|
+
(
|
|
85
|
+
await handler(
|
|
86
|
+
new Request("https://feedback.example.com/internal/images", {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: { Origin: headers.Origin, "Content-Type": "image/png" },
|
|
89
|
+
body: png,
|
|
90
|
+
}),
|
|
91
|
+
)
|
|
92
|
+
)?.status,
|
|
93
|
+
).toBe(401);
|
|
94
|
+
expect(
|
|
95
|
+
(
|
|
96
|
+
await handler(
|
|
97
|
+
new Request("https://feedback.example.com/internal/images", {
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers: {
|
|
100
|
+
...headers,
|
|
101
|
+
"Content-Length": String(5 * 1024 * 1024 + 1),
|
|
102
|
+
},
|
|
103
|
+
body: png,
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
)?.status,
|
|
107
|
+
).toBe(413);
|
|
108
|
+
const upload = await handler(
|
|
109
|
+
new Request("https://feedback.example.com/internal/images", {
|
|
110
|
+
method: "POST",
|
|
111
|
+
headers,
|
|
112
|
+
body: png,
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
expect(upload?.status).toBe(201);
|
|
116
|
+
const image = JSON.parse(await upload!.text());
|
|
117
|
+
const publicRead = await handler(
|
|
118
|
+
new Request(`https://feedback.example.com${image.src}`),
|
|
119
|
+
);
|
|
120
|
+
expect(publicRead?.status).toBe(404);
|
|
121
|
+
const ownRead = await handler(
|
|
122
|
+
new Request(`https://feedback.example.com${image.src}`, {
|
|
123
|
+
headers: { Cookie: headers.Cookie },
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
expect(ownRead?.status).toBe(200);
|
|
127
|
+
expect(ownRead?.headers.get("Cache-Control")).toBe("no-store");
|
|
128
|
+
expect(ownRead?.headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
129
|
+
expect(new Uint8Array(await ownRead!.arrayBuffer())).toEqual(png);
|
|
130
|
+
} finally {
|
|
131
|
+
await runtime.dispose();
|
|
132
|
+
}
|
|
133
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ImageAttachmentError,
|
|
3
|
+
type createImageModule,
|
|
4
|
+
} from "@feedbax/feedback";
|
|
5
|
+
import { env } from "cloudflare:workers";
|
|
6
|
+
import feedbax from "./feedbax";
|
|
7
|
+
import {
|
|
8
|
+
configuredImages,
|
|
9
|
+
createConfiguredFeedbackModule,
|
|
10
|
+
requestParticipant,
|
|
11
|
+
} from "./feedback-runtime";
|
|
12
|
+
import { sameOrigin } from "./connected-identity";
|
|
13
|
+
|
|
14
|
+
export function createImageRequestHandler(options: {
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
siteUrl: string;
|
|
17
|
+
getImages(request: Request): Promise<ReturnType<typeof createImageModule>>;
|
|
18
|
+
signedIn(request: Request): Promise<boolean>;
|
|
19
|
+
rateLimit(request: Request): Promise<boolean>;
|
|
20
|
+
}) {
|
|
21
|
+
return async (request: Request): Promise<Response | undefined> => {
|
|
22
|
+
const url = new URL(request.url);
|
|
23
|
+
if (
|
|
24
|
+
url.pathname !== "/internal/images" &&
|
|
25
|
+
!url.pathname.startsWith("/internal/images/")
|
|
26
|
+
)
|
|
27
|
+
return;
|
|
28
|
+
const json = (body: unknown, status = 200) =>
|
|
29
|
+
Response.json(body, { status, headers: { "Cache-Control": "no-store" } });
|
|
30
|
+
if (!options.enabled) return json({ error: "Not found." }, 404);
|
|
31
|
+
const id = url.pathname.slice("/internal/images/".length);
|
|
32
|
+
try {
|
|
33
|
+
if (request.method === "GET" && /^[-a-f0-9]{36}$/.test(id)) {
|
|
34
|
+
const image = await (
|
|
35
|
+
await options.getImages(request)
|
|
36
|
+
).read(id, url.searchParams.get("review") ?? undefined);
|
|
37
|
+
if (!image) return json({ error: "Image is unavailable." }, 404);
|
|
38
|
+
return new Response(image.body as BodyInit, {
|
|
39
|
+
headers: {
|
|
40
|
+
"Content-Type": image.contentType,
|
|
41
|
+
"Content-Length": String(image.bytes),
|
|
42
|
+
"Cache-Control": "no-store",
|
|
43
|
+
"X-Content-Type-Options": "nosniff",
|
|
44
|
+
"Content-Security-Policy": "default-src 'none'; sandbox",
|
|
45
|
+
"Cross-Origin-Resource-Policy": "same-origin",
|
|
46
|
+
"Referrer-Policy": "no-referrer",
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (request.method !== "POST" && request.method !== "DELETE")
|
|
51
|
+
return json({ error: "Method not allowed." }, 405);
|
|
52
|
+
if (!sameOrigin(request, options.siteUrl))
|
|
53
|
+
return json({ error: "Request origin is invalid." }, 403);
|
|
54
|
+
if (!(await options.signedIn(request)))
|
|
55
|
+
return json({ error: "Sign in to add images." }, 401);
|
|
56
|
+
if (!(await options.rateLimit(request)))
|
|
57
|
+
return json(
|
|
58
|
+
{ error: "Too many image requests. Try again shortly." },
|
|
59
|
+
429,
|
|
60
|
+
);
|
|
61
|
+
const images = await options.getImages(request);
|
|
62
|
+
if (request.method === "DELETE") {
|
|
63
|
+
if (!/^[-a-f0-9]{36}$/.test(id))
|
|
64
|
+
return json({ error: "Invalid image." }, 400);
|
|
65
|
+
await images.removePending(id);
|
|
66
|
+
return json({ removed: true });
|
|
67
|
+
}
|
|
68
|
+
if (url.pathname !== "/internal/images")
|
|
69
|
+
return json({ error: "Not found." }, 404);
|
|
70
|
+
const type = request.headers.get("Content-Type") ?? "";
|
|
71
|
+
if (!["image/jpeg", "image/png", "image/webp"].includes(type))
|
|
72
|
+
return json({ error: "Choose a JPEG, PNG, or WebP image." }, 400);
|
|
73
|
+
const max = 5 * 1024 * 1024;
|
|
74
|
+
if (Number(request.headers.get("Content-Length")) > max)
|
|
75
|
+
return json({ error: "Each image must be 5 MiB or smaller." }, 413);
|
|
76
|
+
const reader = request.body?.getReader();
|
|
77
|
+
if (!reader) return json({ error: "Choose an image." }, 400);
|
|
78
|
+
const chunks: Uint8Array[] = [];
|
|
79
|
+
let length = 0;
|
|
80
|
+
while (true) {
|
|
81
|
+
const { value, done } = await reader.read();
|
|
82
|
+
if (done) break;
|
|
83
|
+
length += value.byteLength;
|
|
84
|
+
if (length > max) {
|
|
85
|
+
await reader.cancel();
|
|
86
|
+
return json({ error: "Each image must be 5 MiB or smaller." }, 413);
|
|
87
|
+
}
|
|
88
|
+
chunks.push(value);
|
|
89
|
+
}
|
|
90
|
+
const bytes = new Uint8Array(length);
|
|
91
|
+
let offset = 0;
|
|
92
|
+
for (const chunk of chunks) {
|
|
93
|
+
bytes.set(chunk, offset);
|
|
94
|
+
offset += chunk.byteLength;
|
|
95
|
+
}
|
|
96
|
+
return json(await images.upload(bytes, type), 201);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return json(
|
|
99
|
+
{
|
|
100
|
+
error:
|
|
101
|
+
error instanceof ImageAttachmentError
|
|
102
|
+
? error.message
|
|
103
|
+
: "Images are temporarily unavailable. Try again.",
|
|
104
|
+
},
|
|
105
|
+
error instanceof ImageAttachmentError ? 400 : 503,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
export const handleImageRequest = createImageRequestHandler({
|
|
111
|
+
enabled: Boolean(feedbax.connected?.images),
|
|
112
|
+
siteUrl: feedbax.product.siteUrl ?? "https://not-configured.invalid",
|
|
113
|
+
signedIn: async (request) => Boolean(await requestParticipant(request)),
|
|
114
|
+
getImages: async (request) =>
|
|
115
|
+
configuredImages(
|
|
116
|
+
createConfiguredFeedbackModule(),
|
|
117
|
+
await requestParticipant(request),
|
|
118
|
+
),
|
|
119
|
+
rateLimit: async (request) =>
|
|
120
|
+
(
|
|
121
|
+
await env.PORTAL_SUBMISSIONS_RATE_LIMITER.limit({
|
|
122
|
+
key: `images:${request.headers.get("CF-Connecting-IP") ?? "unknown"}`,
|
|
123
|
+
})
|
|
124
|
+
).success,
|
|
125
|
+
});
|