@podosoft/podokit 0.16.4 → 0.17.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/dist/deploy-compose-exec.d.ts +93 -0
- package/dist/deploy-compose-exec.js +198 -0
- package/dist/deploy-compose-profile.d.ts +107 -0
- package/dist/deploy-compose-profile.js +361 -0
- package/dist/deploy-compose-render.d.ts +48 -0
- package/dist/deploy-compose-render.js +333 -0
- package/dist/deploy-compose-sync.d.ts +144 -0
- package/dist/deploy-compose-sync.js +412 -0
- package/dist/deploy-compose.d.ts +98 -0
- package/dist/deploy-compose.js +667 -0
- package/dist/deploy-dispatch.d.ts +33 -0
- package/dist/deploy-dispatch.js +58 -0
- package/dist/deploy-driver.d.ts +25 -0
- package/dist/deploy-driver.js +78 -0
- package/dist/deploy-profile.d.ts +3 -10
- package/dist/deploy-profile.js +82 -291
- package/dist/deploy-schema.d.ts +56 -0
- package/dist/deploy-schema.js +261 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +178 -6
- package/dist/lib.d.ts +9 -0
- package/dist/lib.js +30 -1
- package/dist/templates/fullstack-nest-svelte/AGENTS.md +2 -1
- package/dist/templates/fullstack-nest-svelte/Dockerfile.dev +34 -3
- package/dist/templates/fullstack-nest-svelte/apps/api/Dockerfile +68 -7
- package/dist/templates/fullstack-nest-svelte/apps/web/Dockerfile +60 -8
- package/dist/templates/fullstack-nest-svelte/apps/web/server.js +31 -0
- package/dist/templates/fullstack-nest-svelte/apps/web/src/lib/server/backend-proxy.test.ts +21 -0
- package/dist/templates/fullstack-nest-svelte/apps/web/src/lib/server/backend-proxy.ts +15 -1
- package/dist/templates/fullstack-nest-svelte/apps/web/src/lib/server/ws-upgrade.js +267 -0
- package/dist/templates/fullstack-nest-svelte/apps/web/src/lib/server/ws-upgrade.test.js +134 -0
- package/dist/templates/fullstack-nest-svelte/apps/web/vite.config.ts +17 -7
- package/dist/templates/fullstack-nest-svelte/dot-agents/skills/podokit-deploy/SKILL.md +55 -10
- package/dist/templates/fullstack-nest-svelte/dot-agents/skills/podokit-deploy/references/profile.md +50 -1
- package/dist/templates/fullstack-nest-svelte/dot-agents/skills/podokit-deploy/references/release.md +64 -0
- package/dist/templates/fullstack-nest-svelte/dot-agents/skills/podokit-deploy-fast/SKILL.md +92 -0
- package/dist/templates/fullstack-nest-svelte/dot-claude/skills/podokit-deploy/SKILL.md +1 -1
- package/dist/templates/fullstack-nest-svelte/dot-claude/skills/podokit-deploy-fast/SKILL.md +8 -0
- package/dist/templates/fullstack-nest-svelte/dot-dockerignore +7 -0
- package/dist/templates/fullstack-nest-svelte/dot-env.example +7 -0
- package/dist/templates/fullstack-nest-svelte/dot-github/workflows/release.yml +142 -0
- package/dist/templates/fullstack-nest-svelte/infra/k3s/README.md +18 -0
- package/dist/templates/fullstack-nest-svelte/infra/k3s/api-deployment.yaml +6 -1
- package/dist/templates/fullstack-nest-svelte/infra/k3s/ingress.yaml +8 -0
- package/dist/templates/fullstack-nest-svelte/infra/k3s/web-deployment.yaml +11 -1
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/components/account-page.svelte +15 -2
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/device-label.test.ts +62 -0
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/device-label.ts +50 -0
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/i18n/catalogs/admin-dashboard/en.json +5 -0
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/i18n/catalogs/admin-dashboard/ko.json +5 -0
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/routes/(admin)/admin/sessions/+page.svelte +3 -2
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/routes/(admin)/admin/users/+page.svelte +3 -2
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/routes/(auth)/reset-password/+page.svelte +12 -1
- package/dist/templates/modules/admin-dashboard/files/apps/web/src/routes/(auth)/signup/+page.svelte +13 -1
- package/dist/templates/modules/admin-dashboard/files/tests/helpers/accounts.ts +27 -2
- package/dist/templates/modules/admin-dashboard/files/tests/ui/account.ui.spec.ts +20 -1
- package/dist/templates/modules/admin-dashboard/files/tests/ui/auth.ui.spec.ts +26 -1
- package/dist/templates/modules/admin-dashboard/files/tests/ui/email.ui.spec.ts +10 -1
- package/dist/templates/modules/admin-dashboard/files/tests/ui/two-factor.ui.spec.ts +3 -3
- package/dist/templates/modules/bullmq/files/infra/k3s/worker-deployment.yaml +2 -1
- package/dist/templates/todo/AGENTS.md +2 -1
- package/dist/templates/todo/Dockerfile.dev +34 -3
- package/dist/templates/todo/apps/api/Dockerfile +68 -7
- package/dist/templates/todo/apps/web/Dockerfile +60 -8
- package/dist/templates/todo/apps/web/server.js +31 -0
- package/dist/templates/todo/apps/web/src/lib/server/backend-proxy.test.ts +21 -0
- package/dist/templates/todo/apps/web/src/lib/server/backend-proxy.ts +15 -1
- package/dist/templates/todo/apps/web/src/lib/server/ws-upgrade.js +267 -0
- package/dist/templates/todo/apps/web/src/lib/server/ws-upgrade.test.js +134 -0
- package/dist/templates/todo/apps/web/vite.config.ts +17 -7
- package/dist/templates/todo/dot-agents/skills/podokit-deploy/SKILL.md +55 -10
- package/dist/templates/todo/dot-agents/skills/podokit-deploy/references/profile.md +47 -0
- package/dist/templates/todo/dot-agents/skills/podokit-deploy/references/release.md +64 -0
- package/dist/templates/todo/dot-agents/skills/podokit-deploy-fast/SKILL.md +92 -0
- package/dist/templates/todo/dot-claude/skills/podokit-deploy/SKILL.md +1 -1
- package/dist/templates/todo/dot-claude/skills/podokit-deploy-fast/SKILL.md +8 -0
- package/dist/templates/todo/dot-dockerignore +7 -0
- package/dist/templates/todo/dot-env.example +7 -0
- package/dist/templates/todo/dot-github/workflows/release.yml +142 -0
- package/dist/templates/todo/infra/k3s/README.md +18 -0
- package/dist/templates/todo/infra/k3s/api-deployment.yaml +6 -1
- package/dist/templates/todo/infra/k3s/ingress.yaml +8 -0
- package/dist/templates/todo/infra/k3s/web-deployment.yaml +11 -1
- package/package.json +1 -1
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
# Development image for the PodoKit monorepo (web + api).
|
|
2
2
|
# Dependencies and source are baked in; `docker compose watch` syncs your
|
|
3
3
|
# edits into the running container so the native file watchers fire.
|
|
4
|
+
|
|
5
|
+
# Every workspace manifest, collected rather than listed.
|
|
6
|
+
#
|
|
7
|
+
# Listing them by hand only works for the workspaces this template starts with. A
|
|
8
|
+
# project that adds `packages/*` then has workspace manifests that never reach the
|
|
9
|
+
# image, so npm does not know those names are local and goes looking for them on the
|
|
10
|
+
# public registry — `npm error 404 '@scope/thing' is not in this registry`, from an
|
|
11
|
+
# install that succeeds on the developer's own machine. The production images
|
|
12
|
+
# collect manifests for exactly this reason; this one was the exception until a
|
|
13
|
+
# generated project with shared packages could not start its dev stack at all.
|
|
14
|
+
FROM node:22-bookworm-slim AS manifests
|
|
15
|
+
WORKDIR /src
|
|
16
|
+
COPY . .
|
|
17
|
+
RUN mkdir -p /app \
|
|
18
|
+
&& cp package.json /app/ \
|
|
19
|
+
&& find . -name package.json -not -path "*/node_modules/*" \
|
|
20
|
+
-exec sh -c 'mkdir -p "/app/$(dirname "$1")" && cp "$1" "/app/$1"' _ {} \;
|
|
21
|
+
|
|
4
22
|
FROM node:22-bookworm-slim
|
|
5
23
|
|
|
6
24
|
# git for the toolchain (editors, AI agents); build tools for native modules.
|
|
@@ -14,9 +32,7 @@ WORKDIR /app
|
|
|
14
32
|
# NB: the host package-lock.json is intentionally NOT copied — a lockfile
|
|
15
33
|
# generated on macOS/Windows prunes platform-specific optional deps and can drop
|
|
16
34
|
# packages (e.g. @swc/cli) on linux. Resolving fresh in-container avoids that.
|
|
17
|
-
COPY
|
|
18
|
-
COPY apps/api/package.json ./apps/api/package.json
|
|
19
|
-
COPY apps/web/package.json ./apps/web/package.json
|
|
35
|
+
COPY --from=manifests /app ./
|
|
20
36
|
ARG PODOKIT_NPM_REGISTRY=""
|
|
21
37
|
RUN if [ -n "$PODOKIT_NPM_REGISTRY" ]; then npm config set registry "$PODOKIT_NPM_REGISTRY"; fi \
|
|
22
38
|
&& npm install
|
|
@@ -24,4 +40,19 @@ RUN if [ -n "$PODOKIT_NPM_REGISTRY" ]; then npm config set registry "$PODOKIT_NP
|
|
|
24
40
|
# Baked source (kept in sync during development by `docker compose watch`).
|
|
25
41
|
COPY . .
|
|
26
42
|
|
|
43
|
+
# Workspace packages compile before the apps that import them. Apps import them by
|
|
44
|
+
# their published entry point (`dist/index.js`), so without this the API starts and
|
|
45
|
+
# dies on MODULE_NOT_FOUND for a package whose symlink is right there — the link
|
|
46
|
+
# resolves, the built file behind it does not exist. `--if-present` keeps a project
|
|
47
|
+
# with no packages, or one whose packages need no build, working unchanged.
|
|
48
|
+
#
|
|
49
|
+
# `docker compose watch` syncs app source only, so a change under packages/* needs
|
|
50
|
+
# this image rebuilt rather than a container restart.
|
|
51
|
+
RUN if [ -d packages ]; then \
|
|
52
|
+
for manifest in packages/*/package.json; do \
|
|
53
|
+
[ -e "$manifest" ] || continue; \
|
|
54
|
+
npm run build --if-present --workspace "$(dirname "$manifest")"; \
|
|
55
|
+
done; \
|
|
56
|
+
fi
|
|
57
|
+
|
|
27
58
|
EXPOSE 5001 5002
|
|
@@ -1,27 +1,88 @@
|
|
|
1
1
|
# syntax=docker/dockerfile:1
|
|
2
|
+
|
|
3
|
+
# The npm that reads package-lock.json must be the major that wrote it. node:22
|
|
4
|
+
# bundles npm 10, and a lockfile written by npm 11 records platform-specific
|
|
5
|
+
# packages as extraneous with no `optional` flag; npm 10 rejects those with
|
|
6
|
+
# EBADPLATFORM instead of skipping them, so `npm ci` fails inside the image while
|
|
7
|
+
# `npm install` on the developer's machine is fine. Keep this in step with the npm
|
|
8
|
+
# the project is developed with.
|
|
9
|
+
ARG NPM_VERSION=11.17.0
|
|
10
|
+
|
|
11
|
+
# Every workspace manifest, and nothing else.
|
|
12
|
+
#
|
|
13
|
+
# Listing them by hand only works for the workspaces this template starts with: npm
|
|
14
|
+
# does NOT fail when a workspace's package.json is absent, it silently installs a
|
|
15
|
+
# tree without that package's dependencies. A project that adds packages/* then
|
|
16
|
+
# builds against different versions than it resolves locally, and only inside the
|
|
17
|
+
# image. Collecting them keeps `npm ci` correct whatever the workspace layout is,
|
|
18
|
+
# and the stages below still cache on the manifests' content.
|
|
19
|
+
FROM node:22-alpine AS manifests
|
|
20
|
+
WORKDIR /src
|
|
21
|
+
COPY . .
|
|
22
|
+
RUN mkdir -p /app \
|
|
23
|
+
&& cp package.json package-lock.json /app/ \
|
|
24
|
+
&& find . -name package.json -not -path "*/node_modules/*" \
|
|
25
|
+
-exec sh -c 'mkdir -p "/app/$(dirname "$1")" && cp "$1" "/app/$1"' _ {} \;
|
|
26
|
+
|
|
2
27
|
FROM node:22-alpine AS deps
|
|
3
28
|
WORKDIR /app
|
|
4
|
-
COPY
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
COPY tests/package.json ./tests/package.json
|
|
29
|
+
COPY --from=manifests /app ./
|
|
30
|
+
ARG NPM_VERSION
|
|
31
|
+
RUN npm i -g "npm@${NPM_VERSION}"
|
|
8
32
|
RUN npm ci --no-audit --no-fund
|
|
9
33
|
|
|
34
|
+
# Production dependencies only, resolved from the same lockfile. The runtime stage
|
|
35
|
+
# copies node_modules from here and never from deps or build, so the shipped image
|
|
36
|
+
# does not carry the TypeScript compiler, Vite, or the test tooling.
|
|
37
|
+
FROM node:22-alpine AS prod-deps
|
|
38
|
+
WORKDIR /app
|
|
39
|
+
COPY --from=manifests /app ./
|
|
40
|
+
ARG NPM_VERSION
|
|
41
|
+
RUN npm i -g "npm@${NPM_VERSION}"
|
|
42
|
+
# The runtime tree for this image only.
|
|
43
|
+
#
|
|
44
|
+
# Scoping to the one workspace matters: --omit=dev at the root still installs the
|
|
45
|
+
# union of every workspace's production dependencies, so the API image carried Vite
|
|
46
|
+
# and the web image carried Nest. Measured on a generated project, this is 198
|
|
47
|
+
# packages instead of 283. TypeScript stays either way -- typeorm depends on ts-node,
|
|
48
|
+
# which depends on it, so it is a production dependency and not bloat.
|
|
49
|
+
RUN npm ci --omit=dev --no-audit --no-fund --workspace=apps/api --include-workspace-root
|
|
50
|
+
|
|
10
51
|
FROM node:22-alpine AS build
|
|
11
52
|
WORKDIR /app
|
|
12
|
-
|
|
53
|
+
# Everything npm ci produced, not just the root node_modules: npm nests any
|
|
54
|
+
# dependency it cannot hoist, and copying only the root directory leaves a workspace
|
|
55
|
+
# package compiling against the wrong version of it.
|
|
56
|
+
COPY --from=deps /app ./
|
|
13
57
|
COPY . .
|
|
58
|
+
# Local workspace packages compile before the app that imports them. Apps import
|
|
59
|
+
# them by their published entry points and .dockerignore excludes dist, so nothing
|
|
60
|
+
# here has compiled output until it is compiled here.
|
|
61
|
+
RUN if [ -d packages ]; then \
|
|
62
|
+
for manifest in packages/*/package.json; do \
|
|
63
|
+
[ -e "$manifest" ] || continue; \
|
|
64
|
+
npm run build --if-present --workspace "$(dirname "$manifest")"; \
|
|
65
|
+
done; \
|
|
66
|
+
fi
|
|
14
67
|
RUN npm run build --workspace=apps/api
|
|
68
|
+
# The runtime needs the real workspace package directories, not just the symlinks
|
|
69
|
+
# npm leaves in node_modules pointing at them. Without this the image boots and
|
|
70
|
+
# dies on "Cannot find module <package>" -- a dangling link, at runtime only.
|
|
71
|
+
# mkdir keeps the COPY valid for a project that has no packages/ at all.
|
|
72
|
+
RUN mkdir -p /app/packages
|
|
15
73
|
|
|
16
74
|
FROM node:22-alpine AS runtime
|
|
17
75
|
WORKDIR /app
|
|
18
76
|
ENV NODE_ENV=production
|
|
19
77
|
RUN addgroup -S -g 101 app && adduser -S -D -H -u 100 -G app app
|
|
20
|
-
COPY --from=
|
|
21
|
-
COPY --from=
|
|
78
|
+
COPY --from=prod-deps /app/node_modules ./node_modules
|
|
79
|
+
COPY --from=manifests /app/apps/api ./apps/api
|
|
80
|
+
COPY --from=build /app/packages ./packages
|
|
22
81
|
COPY --from=build /app/apps/api/dist ./apps/api/dist
|
|
23
82
|
COPY --from=build /app/apps/api/scripts ./apps/api/scripts
|
|
24
83
|
WORKDIR /app/apps/api
|
|
25
84
|
USER 100:101
|
|
26
85
|
EXPOSE 3000
|
|
86
|
+
HEALTHCHECK --interval=10s --timeout=5s --start-period=30s --retries=6 \
|
|
87
|
+
CMD node -e "fetch('http://127.0.0.1:3000/health/ready').then((r) => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
|
27
88
|
CMD ["node", "dist/main"]
|
|
@@ -1,26 +1,78 @@
|
|
|
1
1
|
# syntax=docker/dockerfile:1
|
|
2
|
+
|
|
3
|
+
# The npm that reads package-lock.json must be the major that wrote it. node:22
|
|
4
|
+
# bundles npm 10, and a lockfile written by npm 11 records platform-specific
|
|
5
|
+
# packages as extraneous with no `optional` flag; npm 10 rejects those with
|
|
6
|
+
# EBADPLATFORM instead of skipping them, so `npm ci` fails inside the image while
|
|
7
|
+
# `npm install` on the developer's machine is fine. Keep this in step with the npm
|
|
8
|
+
# the project is developed with.
|
|
9
|
+
ARG NPM_VERSION=11.17.0
|
|
10
|
+
|
|
11
|
+
# See apps/api/Dockerfile for why the workspace manifests are collected rather than
|
|
12
|
+
# listed: an absent workspace package.json does not fail `npm ci`, it silently
|
|
13
|
+
# changes the resolved dependency tree inside the image.
|
|
14
|
+
FROM node:22-alpine AS manifests
|
|
15
|
+
WORKDIR /src
|
|
16
|
+
COPY . .
|
|
17
|
+
RUN mkdir -p /app \
|
|
18
|
+
&& cp package.json package-lock.json /app/ \
|
|
19
|
+
&& find . -name package.json -not -path "*/node_modules/*" \
|
|
20
|
+
-exec sh -c 'mkdir -p "/app/$(dirname "$1")" && cp "$1" "/app/$1"' _ {} \;
|
|
21
|
+
|
|
2
22
|
FROM node:22-alpine AS deps
|
|
3
23
|
WORKDIR /app
|
|
4
|
-
COPY
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
COPY tests/package.json ./tests/package.json
|
|
24
|
+
COPY --from=manifests /app ./
|
|
25
|
+
ARG NPM_VERSION
|
|
26
|
+
RUN npm i -g "npm@${NPM_VERSION}"
|
|
8
27
|
RUN npm ci --no-audit --no-fund
|
|
9
28
|
|
|
29
|
+
FROM node:22-alpine AS prod-deps
|
|
30
|
+
WORKDIR /app
|
|
31
|
+
COPY --from=manifests /app ./
|
|
32
|
+
ARG NPM_VERSION
|
|
33
|
+
RUN npm i -g "npm@${NPM_VERSION}"
|
|
34
|
+
# The runtime tree for this image only.
|
|
35
|
+
#
|
|
36
|
+
# Scoping to the one workspace matters: --omit=dev at the root still installs the
|
|
37
|
+
# union of every workspace's production dependencies, so the API image carried Vite
|
|
38
|
+
# and the web image carried Nest. Measured on a generated project, this is 198
|
|
39
|
+
# packages instead of 283. TypeScript stays either way -- typeorm depends on ts-node,
|
|
40
|
+
# which depends on it, so it is a production dependency and not bloat.
|
|
41
|
+
RUN npm ci --omit=dev --no-audit --no-fund --workspace=apps/web --include-workspace-root
|
|
42
|
+
|
|
10
43
|
FROM node:22-alpine AS build
|
|
11
44
|
WORKDIR /app
|
|
12
|
-
COPY --from=deps /app
|
|
45
|
+
COPY --from=deps /app ./
|
|
13
46
|
COPY . .
|
|
47
|
+
RUN if [ -d packages ]; then \
|
|
48
|
+
for manifest in packages/*/package.json; do \
|
|
49
|
+
[ -e "$manifest" ] || continue; \
|
|
50
|
+
npm run build --if-present --workspace "$(dirname "$manifest")"; \
|
|
51
|
+
done; \
|
|
52
|
+
fi
|
|
14
53
|
RUN npm run build --workspace=apps/web
|
|
54
|
+
# The runtime needs the real workspace package directories, not just the symlinks
|
|
55
|
+
# npm leaves in node_modules pointing at them. Without this the image boots and
|
|
56
|
+
# dies on "Cannot find module <package>" -- a dangling link, at runtime only.
|
|
57
|
+
# mkdir keeps the COPY valid for a project that has no packages/ at all.
|
|
58
|
+
RUN mkdir -p /app/packages
|
|
15
59
|
|
|
16
60
|
FROM node:22-alpine AS runtime
|
|
17
61
|
WORKDIR /app
|
|
18
62
|
ENV NODE_ENV=production
|
|
19
63
|
RUN addgroup -S -g 101 app && adduser -S -D -H -u 100 -G app app
|
|
20
|
-
COPY --from=
|
|
21
|
-
COPY --from=
|
|
64
|
+
COPY --from=prod-deps /app/node_modules ./node_modules
|
|
65
|
+
COPY --from=manifests /app/apps/web ./apps/web
|
|
66
|
+
COPY --from=build /app/packages ./packages
|
|
22
67
|
COPY --from=build /app/apps/web/build ./apps/web/build
|
|
68
|
+
# The entry and the modules it imports run OUTSIDE the bundle, so they ship as
|
|
69
|
+
# source. Without them the image falls back to an entry that cannot answer a
|
|
70
|
+
# WebSocket upgrade.
|
|
71
|
+
COPY --from=build /app/apps/web/server.js ./apps/web/server.js
|
|
72
|
+
COPY --from=build /app/apps/web/src/lib/server ./apps/web/src/lib/server
|
|
23
73
|
WORKDIR /app/apps/web
|
|
24
74
|
USER 100:101
|
|
25
75
|
EXPOSE 3000
|
|
26
|
-
|
|
76
|
+
HEALTHCHECK --interval=10s --timeout=5s --start-period=30s --retries=6 \
|
|
77
|
+
CMD node -e "fetch('http://127.0.0.1:3000/').then((r) => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
|
78
|
+
CMD ["node", "server.js"]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The production entry point: adapter-node's server plus WebSocket upgrades.
|
|
3
|
+
*
|
|
4
|
+
* `node build` — adapter-node's own entry — serves HTTP perfectly well and drops
|
|
5
|
+
* every WebSocket upgrade into SvelteKit, which cannot answer one. This entry keeps
|
|
6
|
+
* adapter-node in charge of listening, graceful shutdown, and the PORT/HOST/
|
|
7
|
+
* IDLE_TIMEOUT contract, and adds the one thing it cannot do.
|
|
8
|
+
*
|
|
9
|
+
* Nothing is proxied unless `WS_PROXY_PATHS` names a path, so a deployment that has
|
|
10
|
+
* no WebSocket behaves exactly as `node build` did.
|
|
11
|
+
*
|
|
12
|
+
* ⚠ Not type-checked: it imports `./build/`, which only exists after `npm run build`,
|
|
13
|
+
* and lint must not depend on a build. Everything with a decision in it lives in
|
|
14
|
+
* `src/lib/server/ws-upgrade.js`, which is unit-tested; this is wiring.
|
|
15
|
+
*/
|
|
16
|
+
import { server } from "./build/index.js";
|
|
17
|
+
import { createUpgradeProxy, relayPaths } from "./src/lib/server/ws-upgrade.js";
|
|
18
|
+
|
|
19
|
+
const allowed = relayPaths();
|
|
20
|
+
if (allowed.size > 0) {
|
|
21
|
+
// polka exposes the http.Server it was handed. If a future adapter stops doing so,
|
|
22
|
+
// fail at boot: serving silently without upgrades is the bug this entry exists for.
|
|
23
|
+
const httpServer = server.server;
|
|
24
|
+
if (!httpServer || typeof httpServer.on !== "function") {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"adapter-node did not expose its HTTP server; the WebSocket upgrade proxy cannot be mounted",
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
httpServer.on("upgrade", createUpgradeProxy({ allowed }));
|
|
30
|
+
console.log(`Mounted WebSocket upgrade proxy for ${[...allowed].join(", ")}`);
|
|
31
|
+
}
|
|
@@ -55,4 +55,25 @@ describe("backend proxy client address handling", () => {
|
|
|
55
55
|
expect(response.status).toBe(204);
|
|
56
56
|
expect(upstreamHeaders?.get("x-forwarded-for")).toBe("203.0.113.9");
|
|
57
57
|
});
|
|
58
|
+
|
|
59
|
+
// Not forwarding this does not leave the header unset: fetch substitutes its own
|
|
60
|
+
// default, so the API records "node" for every session and the account page shows
|
|
61
|
+
// that as the device for all of them.
|
|
62
|
+
it("forwards the browser's user agent so sessions record the real device", async () => {
|
|
63
|
+
let upstreamHeaders: Headers | undefined;
|
|
64
|
+
vi.stubGlobal("fetch", async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
65
|
+
upstreamHeaders = new Headers(init?.headers);
|
|
66
|
+
return new Response(null, { status: 204 });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/141.0.0.0 Safari/537.36";
|
|
70
|
+
const request = new Request("http://app.localhost/api/auth/sign-in/email", {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: { "user-agent": agent },
|
|
73
|
+
});
|
|
74
|
+
const response = await proxyRequest(request, "http://api:5002/auth/sign-in/email");
|
|
75
|
+
|
|
76
|
+
expect(response.status).toBe(204);
|
|
77
|
+
expect(upstreamHeaders?.get("user-agent")).toBe(agent);
|
|
78
|
+
});
|
|
58
79
|
});
|
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
// Server-side proxy boundary. The browser never talks to the API directly:
|
|
2
2
|
// requests go through SvelteKit server routes, which forward an allowlist of
|
|
3
3
|
// headers to BACKEND_INTERNAL_URL and relay the response (including Set-Cookie).
|
|
4
|
-
|
|
4
|
+
//
|
|
5
|
+
// `user-agent` is on the list for the same reason the client IP is forwarded
|
|
6
|
+
// below: better-auth records it on the session and the account page shows it as
|
|
7
|
+
// the device. Leaving it off does not produce an empty column -- fetch supplies
|
|
8
|
+
// its own default, so every session in the database reads "node", and the list
|
|
9
|
+
// of "your devices" becomes a list of identical rows that identify nothing.
|
|
10
|
+
const FORWARDED_HEADERS = [
|
|
11
|
+
"authorization",
|
|
12
|
+
"cookie",
|
|
13
|
+
"content-type",
|
|
14
|
+
"accept",
|
|
15
|
+
"origin",
|
|
16
|
+
"referer",
|
|
17
|
+
"user-agent",
|
|
18
|
+
];
|
|
5
19
|
const RELAYED_RESPONSE_HEADERS = ["content-type", "location", "cache-control"];
|
|
6
20
|
|
|
7
21
|
export function backendBaseUrl(): string {
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket upgrades, proxied by the web server itself.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS: a SvelteKit server route cannot serve a WebSocket upgrade.
|
|
5
|
+
* `+server.ts` handlers answer HTTP requests and never see an `upgrade` event, and
|
|
6
|
+
* the `/api/*` proxy is HTTP-only for the same reason — `fetch` cannot carry a 101.
|
|
7
|
+
* Meanwhile a deployment routes all public traffic to the web app, so an API
|
|
8
|
+
* WebSocket gateway has no path to reach at all. Development hides this, because
|
|
9
|
+
* Vite proxies upgrades; the feature then dies silently once deployed.
|
|
10
|
+
*
|
|
11
|
+
* ⚠ NOTHING IS FORWARDED BY DEFAULT. `WS_PROXY_PATHS` names the exact paths that may
|
|
12
|
+
* upgrade, comma-separated, and anything else is destroyed rather than proxied. A
|
|
13
|
+
* relay that forwards any path is an open tunnel to every private route on the API,
|
|
14
|
+
* reachable from the public origin without a session. Adding a path is a security
|
|
15
|
+
* decision, not a routing tweak — the API must authorise that socket at handshake
|
|
16
|
+
* time, from the session cookie or its own credential.
|
|
17
|
+
*
|
|
18
|
+
* This file is plain JavaScript on purpose: `server.js` imports `./build/`, which
|
|
19
|
+
* only exists after a build, so the entry point cannot be type-checked. Everything
|
|
20
|
+
* with a decision in it lives here, where it is unit-tested.
|
|
21
|
+
*/
|
|
22
|
+
import http from "node:http";
|
|
23
|
+
import https from "node:https";
|
|
24
|
+
|
|
25
|
+
/** @typedef {Record<string, string | string[] | undefined>} Headers */
|
|
26
|
+
/** @typedef {{ kind: "proxy", path: string } | { kind: "reject", reason: string }} UpgradeDecision */
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The upgrade paths this deployment allows, matched whole — never as a prefix, a
|
|
30
|
+
* wildcard, or a pattern.
|
|
31
|
+
*
|
|
32
|
+
* @returns {ReadonlySet<string>}
|
|
33
|
+
*/
|
|
34
|
+
export function relayPaths() {
|
|
35
|
+
return new Set(
|
|
36
|
+
(process.env.WS_PROXY_PATHS ?? "")
|
|
37
|
+
.split(",")
|
|
38
|
+
.map((entry) => entry.trim())
|
|
39
|
+
.filter((entry) => entry.startsWith("/")),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Where upgrades are sent. Same target the HTTP proxy uses. */
|
|
44
|
+
export function backendBaseUrl() {
|
|
45
|
+
return process.env.BACKEND_INTERNAL_URL ?? "http://localhost:5002";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Normalize a client address for the forwarded header: map loopback to a clean IPv4
|
|
50
|
+
* value and unwrap IPv4-mapped addresses.
|
|
51
|
+
*
|
|
52
|
+
* @param {string | undefined} address
|
|
53
|
+
* @returns {string | undefined}
|
|
54
|
+
*/
|
|
55
|
+
export function normalizeClientIp(address) {
|
|
56
|
+
if (!address) return undefined;
|
|
57
|
+
if (address === "::1" || address === "::") return "127.0.0.1";
|
|
58
|
+
if (address.startsWith("::ffff:")) return address.slice("::ffff:".length);
|
|
59
|
+
return address;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Should this upgrade be forwarded, and under which normalized path?
|
|
64
|
+
*
|
|
65
|
+
* The path is re-serialised from `URL` rather than echoed, so a target that only
|
|
66
|
+
* looks like an allowed path after resolving `..` cannot reach anything else. A
|
|
67
|
+
* request target carrying its own authority is refused outright.
|
|
68
|
+
*
|
|
69
|
+
* @param {string | undefined} url `req.url`, an origin-form request target
|
|
70
|
+
* @param {Headers} headers
|
|
71
|
+
* @param {ReadonlySet<string>} [allowed]
|
|
72
|
+
* @returns {UpgradeDecision}
|
|
73
|
+
*/
|
|
74
|
+
export function decideUpgrade(url, headers, allowed = relayPaths()) {
|
|
75
|
+
const upgrade = String(headers.upgrade ?? "").toLowerCase();
|
|
76
|
+
if (upgrade !== "websocket") return { kind: "reject", reason: "not a websocket upgrade" };
|
|
77
|
+
if (!url || !url.startsWith("/") || url.startsWith("//")) {
|
|
78
|
+
return { kind: "reject", reason: "not an origin-form request target" };
|
|
79
|
+
}
|
|
80
|
+
let parsed;
|
|
81
|
+
try {
|
|
82
|
+
parsed = new URL(url, "http://upgrade.invalid");
|
|
83
|
+
} catch {
|
|
84
|
+
return { kind: "reject", reason: "unparsable request target" };
|
|
85
|
+
}
|
|
86
|
+
if (!allowed.has(parsed.pathname)) return { kind: "reject", reason: "not an allowed upgrade path" };
|
|
87
|
+
return { kind: "proxy", path: `${parsed.pathname}${parsed.search}` };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* What the API is told about the request.
|
|
92
|
+
*
|
|
93
|
+
* Handshake headers and the session cookie pass through untouched — the API
|
|
94
|
+
* authorises the socket from them, so dropping either turns every connection into a
|
|
95
|
+
* 401. The `x-forwarded-*` trio is appended rather than replaced: behind another
|
|
96
|
+
* proxy this server is one hop in a chain, and overwriting it reports the wrong
|
|
97
|
+
* client upstream.
|
|
98
|
+
*
|
|
99
|
+
* @param {Headers} headers
|
|
100
|
+
* @param {{ remoteAddress?: string, encrypted?: boolean }} socket
|
|
101
|
+
* @returns {Headers}
|
|
102
|
+
*/
|
|
103
|
+
export function upstreamHeaders(headers, socket) {
|
|
104
|
+
/** @type {Headers} */
|
|
105
|
+
const forwarded = {};
|
|
106
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
107
|
+
if (value === undefined) continue;
|
|
108
|
+
if (name === "x-forwarded-for" || name === "x-forwarded-proto" || name === "x-forwarded-host") {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
forwarded[name] = value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const client = normalizeClientIp(socket.remoteAddress);
|
|
115
|
+
const chain = [headers["x-forwarded-for"], client]
|
|
116
|
+
.flat()
|
|
117
|
+
.filter((entry) => typeof entry === "string" && entry.length > 0);
|
|
118
|
+
if (chain.length > 0) forwarded["x-forwarded-for"] = chain.join(", ");
|
|
119
|
+
|
|
120
|
+
forwarded["x-forwarded-proto"] = headers["x-forwarded-proto"] ?? (socket.encrypted ? "https" : "http");
|
|
121
|
+
const host = headers["x-forwarded-host"] ?? headers.host;
|
|
122
|
+
if (host !== undefined) forwarded["x-forwarded-host"] = host;
|
|
123
|
+
|
|
124
|
+
return forwarded;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {string} path
|
|
129
|
+
* @param {Headers} headers
|
|
130
|
+
*/
|
|
131
|
+
export function upstreamOptions(path, headers) {
|
|
132
|
+
const base = new URL(backendBaseUrl());
|
|
133
|
+
return {
|
|
134
|
+
protocol: base.protocol,
|
|
135
|
+
hostname: base.hostname,
|
|
136
|
+
port: base.port || (base.protocol === "https:" ? "443" : "80"),
|
|
137
|
+
path,
|
|
138
|
+
headers,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {string} statusLine
|
|
144
|
+
* @param {Headers} headers
|
|
145
|
+
* @returns {string}
|
|
146
|
+
*/
|
|
147
|
+
function rawHead(statusLine, headers) {
|
|
148
|
+
const lines = [statusLine];
|
|
149
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
150
|
+
if (value === undefined) continue;
|
|
151
|
+
if (Array.isArray(value)) for (const item of value) lines.push(`${name}: ${item}`);
|
|
152
|
+
else lines.push(`${name}: ${value}`);
|
|
153
|
+
}
|
|
154
|
+
return `${lines.join("\r\n")}\r\n\r\n`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Streaming sockets carry many small frames, so Nagle's algorithm adds latency to
|
|
159
|
+
* every one, and the default timeout would cut an idle-but-attached connection.
|
|
160
|
+
*
|
|
161
|
+
* @param {{ setTimeout?: Function, setNoDelay?: Function, setKeepAlive?: Function }} socket
|
|
162
|
+
*/
|
|
163
|
+
function tune(socket) {
|
|
164
|
+
socket.setTimeout?.(0);
|
|
165
|
+
socket.setNoDelay?.(true);
|
|
166
|
+
socket.setKeepAlive?.(true, 0);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The real transport: one upstream HTTP request carrying the upgrade. */
|
|
170
|
+
const nodeConnect = (options, hooks) => {
|
|
171
|
+
const transport = options.protocol === "https:" ? https : http;
|
|
172
|
+
const upstream = transport.request({
|
|
173
|
+
protocol: options.protocol,
|
|
174
|
+
hostname: options.hostname,
|
|
175
|
+
port: options.port,
|
|
176
|
+
path: options.path,
|
|
177
|
+
method: "GET",
|
|
178
|
+
headers: options.headers,
|
|
179
|
+
});
|
|
180
|
+
upstream.on("upgrade", (response, socket, head) => {
|
|
181
|
+
hooks.onUpgrade(response.statusCode ?? 101, response.headers, socket, head);
|
|
182
|
+
});
|
|
183
|
+
upstream.on("response", (response) => {
|
|
184
|
+
hooks.onResponse(response.statusCode ?? 502, response.statusMessage ?? "", response.headers);
|
|
185
|
+
response.resume(); // Drain, or the socket is never released.
|
|
186
|
+
});
|
|
187
|
+
upstream.on("error", (error) => hooks.onError(error.message));
|
|
188
|
+
upstream.end();
|
|
189
|
+
return { abort: () => upstream.destroy() };
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @typedef {object} UpstreamHooks
|
|
194
|
+
* @property {(status: number, headers: Headers, socket: any, head: Uint8Array | null) => void} onUpgrade
|
|
195
|
+
* @property {(status: number, statusMessage: string, headers: Headers) => void} onResponse
|
|
196
|
+
* @property {(reason: string) => void} onError
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
/** @typedef {(options: ReturnType<typeof upstreamOptions>, hooks: UpstreamHooks) => { abort: () => void }} Connect */
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Build the `upgrade` listener the built server mounts.
|
|
203
|
+
*
|
|
204
|
+
* The transport is injected so the decisions here are testable without a socket.
|
|
205
|
+
*
|
|
206
|
+
* @param {{ connect?: Connect, allowed?: ReadonlySet<string> }} [options]
|
|
207
|
+
*/
|
|
208
|
+
export function createUpgradeProxy(options = {}) {
|
|
209
|
+
const connect = options.connect ?? nodeConnect;
|
|
210
|
+
const allowed = options.allowed ?? relayPaths();
|
|
211
|
+
|
|
212
|
+
return function onUpgrade(request, socket, head) {
|
|
213
|
+
const decision = decideUpgrade(request.url, request.headers, allowed);
|
|
214
|
+
if (decision.kind === "reject") {
|
|
215
|
+
socket.destroy();
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
tune(socket);
|
|
220
|
+
// Bytes the client already sent belong to the stream: push them back so the pipe
|
|
221
|
+
// below carries them upstream in order.
|
|
222
|
+
if (head && head.length > 0) socket.unshift(head);
|
|
223
|
+
|
|
224
|
+
let clientGone = false;
|
|
225
|
+
const upstream = connect(
|
|
226
|
+
upstreamOptions(decision.path, upstreamHeaders(request.headers, socket)),
|
|
227
|
+
{
|
|
228
|
+
onUpgrade(status, headers, upstreamSocket, upstreamHead) {
|
|
229
|
+
if (clientGone) {
|
|
230
|
+
upstreamSocket.destroy();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (upstreamHead && upstreamHead.length > 0) upstreamSocket.unshift(upstreamHead);
|
|
234
|
+
tune(upstreamSocket);
|
|
235
|
+
socket.write(rawHead(`HTTP/1.1 ${status} Switching Protocols`, headers));
|
|
236
|
+
// A client that hangs forever is worse than one told it lost the relay, so
|
|
237
|
+
// either end closing takes the other down.
|
|
238
|
+
upstreamSocket.on("error", () => socket.destroy());
|
|
239
|
+
upstreamSocket.on("close", () => socket.end());
|
|
240
|
+
upstreamSocket.pipe(socket);
|
|
241
|
+
socket.pipe(upstreamSocket);
|
|
242
|
+
},
|
|
243
|
+
onResponse(status, statusMessage, headers) {
|
|
244
|
+
// The API refused the handshake. Relaying ITS answer is what turns "the
|
|
245
|
+
// socket vanished" into a diagnosable failure in the browser.
|
|
246
|
+
socket.write(
|
|
247
|
+
rawHead(`HTTP/1.1 ${status} ${statusMessage}`.trim(), {
|
|
248
|
+
...headers,
|
|
249
|
+
connection: "close",
|
|
250
|
+
}),
|
|
251
|
+
);
|
|
252
|
+
socket.end();
|
|
253
|
+
},
|
|
254
|
+
onError() {
|
|
255
|
+
socket.write(rawHead("HTTP/1.1 502 Bad Gateway", { connection: "close" }));
|
|
256
|
+
socket.end();
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
socket.on("error", () => upstream.abort());
|
|
262
|
+
socket.on("close", () => {
|
|
263
|
+
clientGone = true;
|
|
264
|
+
upstream.abort();
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
}
|