@sovereignfs/create 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/create.mjs +53 -25
- package/package.json +1 -1
- package/template/app/Dockerfile +13 -7
- package/template/app/README.md +31 -16
- package/template/app/_env.example +7 -2
- package/template/app/_github/workflows/publish.yml +63 -0
- package/template/app/instrumentation.ts +1 -1
- package/template/app/lib/config.ts +1 -1
- package/template/app/lib/db.ts +34 -3
- package/template/app/lib/migrate.ts +5 -4
- package/template/app/lib/stores.ts +3 -2
- package/template/app/sovereign.manifest.json +4 -4
- package/template/app/scripts/manifest.mjs +0 -25
package/dist/create.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import { createInterface } from "readline/promises";
|
|
|
7
7
|
// package.json
|
|
8
8
|
var package_default = {
|
|
9
9
|
name: "@sovereignfs/create",
|
|
10
|
-
version: "0.
|
|
10
|
+
version: "0.6.0",
|
|
11
11
|
type: "module",
|
|
12
12
|
description: "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
|
|
13
13
|
license: "AGPL-3.0-or-later",
|
|
@@ -135,9 +135,22 @@ var CreateError = class extends Error {
|
|
|
135
135
|
hint;
|
|
136
136
|
name = "CreateError";
|
|
137
137
|
};
|
|
138
|
+
var DEFAULT_APP_PORT = 3100;
|
|
139
|
+
function parsePort(value) {
|
|
140
|
+
const port = /^[0-9]+$/.test(value) ? Number(value) : Number.NaN;
|
|
141
|
+
if (!Number.isInteger(port) || port < 1024 || port > 65535) {
|
|
142
|
+
throw new CreateError(
|
|
143
|
+
`${value || "(nothing)"} is not a port an app can listen on.`,
|
|
144
|
+
2,
|
|
145
|
+
"Use a whole number from 1024 to 65535: the image does not run as root, so it cannot bind a lower one."
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return port;
|
|
149
|
+
}
|
|
138
150
|
function readRequest(argv, env) {
|
|
139
151
|
let kind;
|
|
140
152
|
let remote;
|
|
153
|
+
let port;
|
|
141
154
|
const words = [];
|
|
142
155
|
for (let i = 0; i < argv.length; i += 1) {
|
|
143
156
|
const word = argv[i];
|
|
@@ -145,11 +158,13 @@ function readRequest(argv, env) {
|
|
|
145
158
|
else if (word === "--app") kind = "app";
|
|
146
159
|
else if (word === "--remote") remote = argv[++i];
|
|
147
160
|
else if (word.startsWith("--remote=")) remote = word.slice("--remote=".length);
|
|
161
|
+
else if (word === "--port") port = argv[++i] ?? "";
|
|
162
|
+
else if (word.startsWith("--port=")) port = word.slice("--port=".length);
|
|
148
163
|
else if (word.startsWith("--"))
|
|
149
164
|
throw new CreateError(
|
|
150
165
|
`There is no option ${word}.`,
|
|
151
166
|
2,
|
|
152
|
-
"Use: npm create @sovereignfs instance <folder> [--remote URL]"
|
|
167
|
+
"Use: npm create @sovereignfs instance <folder> [--remote URL], or app <folder> [--port N]"
|
|
153
168
|
);
|
|
154
169
|
else words.push(word);
|
|
155
170
|
}
|
|
@@ -166,7 +181,8 @@ function readRequest(argv, env) {
|
|
|
166
181
|
2,
|
|
167
182
|
"Use: npm create @sovereignfs instance <folder>"
|
|
168
183
|
);
|
|
169
|
-
|
|
184
|
+
port ??= env.npm_config_port === void 0 || env.npm_config_port === "" ? void 0 : env.npm_config_port;
|
|
185
|
+
return { kind, dir: words[0], remote, port: port === void 0 ? void 0 : parsePort(port) };
|
|
170
186
|
}
|
|
171
187
|
async function checkEmpty(dir) {
|
|
172
188
|
try {
|
|
@@ -251,13 +267,15 @@ var APP_DEPENDENCIES = {
|
|
|
251
267
|
"@dnd-kit/core": "^6.3.1",
|
|
252
268
|
"@dnd-kit/sortable": "^10.0.0",
|
|
253
269
|
"@sovereignfs/sdk": "^2.0.0",
|
|
254
|
-
"@sovereignfs/ui": "^0.
|
|
270
|
+
"@sovereignfs/ui": "^0.92.0",
|
|
255
271
|
next: "^16.4.3",
|
|
256
272
|
pg: "^8.23.1",
|
|
257
273
|
react: "^19.3.1",
|
|
258
274
|
"react-dom": "^19.3.1"
|
|
259
275
|
};
|
|
260
276
|
var APP_DEV_DEPENDENCIES = {
|
|
277
|
+
// Makes the image's label and port from the manifest, and checks the manifest (`sovereign-manifest`).
|
|
278
|
+
"@sovereignfs/manifest": "^0.1.0",
|
|
261
279
|
"@types/node": "^24.19.2",
|
|
262
280
|
"@types/pg": "^8.23.1",
|
|
263
281
|
"@types/react": "^19.3.0",
|
|
@@ -273,7 +291,8 @@ var RENAMED = {
|
|
|
273
291
|
_dockerignore: ".dockerignore",
|
|
274
292
|
"_env.example": ".env.example",
|
|
275
293
|
// A published package may not carry a file called tsconfig.json.
|
|
276
|
-
"_tsconfig.json": "tsconfig.json"
|
|
294
|
+
"_tsconfig.json": "tsconfig.json",
|
|
295
|
+
_github: ".github"
|
|
277
296
|
};
|
|
278
297
|
function namesOf(dir) {
|
|
279
298
|
const name = path2.basename(path2.resolve(dir)).toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
@@ -302,7 +321,7 @@ async function* walk(dir, base = dir) {
|
|
|
302
321
|
else yield path2.relative(base, full);
|
|
303
322
|
}
|
|
304
323
|
}
|
|
305
|
-
function appPackageJson(names) {
|
|
324
|
+
function appPackageJson(names, port = DEFAULT_APP_PORT) {
|
|
306
325
|
return `${JSON.stringify(
|
|
307
326
|
{
|
|
308
327
|
name: names.name,
|
|
@@ -311,12 +330,12 @@ function appPackageJson(names) {
|
|
|
311
330
|
description: `${names.title}, an app for a Sovereign instance`,
|
|
312
331
|
engines: { node: ">=24.15" },
|
|
313
332
|
scripts: {
|
|
314
|
-
dev
|
|
333
|
+
// The manifest's port, so `npm run dev` and the image listen where the instance and .env.example say.
|
|
334
|
+
dev: `next dev --port ${port}`,
|
|
315
335
|
build: "next build",
|
|
316
|
-
start:
|
|
336
|
+
start: `PORT=\${PORT:-${port}} node .next/standalone/server.js`,
|
|
317
337
|
typecheck: "tsc --noEmit",
|
|
318
|
-
"manifest:check": "
|
|
319
|
-
"manifest:write": "node scripts/manifest.mjs --write"
|
|
338
|
+
"manifest:check": "sovereign-manifest check"
|
|
320
339
|
},
|
|
321
340
|
dependencies: APP_DEPENDENCIES,
|
|
322
341
|
devDependencies: APP_DEV_DEPENDENCIES
|
|
@@ -326,32 +345,29 @@ function appPackageJson(names) {
|
|
|
326
345
|
)}
|
|
327
346
|
`;
|
|
328
347
|
}
|
|
329
|
-
async function writeApp(target, names) {
|
|
348
|
+
async function writeApp(target, names, port = DEFAULT_APP_PORT) {
|
|
330
349
|
await checkEmpty(target);
|
|
331
350
|
const written = [];
|
|
332
|
-
const replace = (text
|
|
333
|
-
const manifestText = replace(
|
|
334
|
-
await readFile(path2.join(TEMPLATE, "sovereign.manifest.json"), "utf8")
|
|
335
|
-
);
|
|
336
|
-
const label = JSON.stringify(JSON.stringify(JSON.parse(manifestText)));
|
|
351
|
+
const replace = (text) => text.replaceAll("__PREFIX_LOWER__", names.prefix.toLowerCase()).replaceAll("__PREFIX__", names.prefix).replaceAll("__NAME__", names.name).replaceAll("__TITLE__", names.title).replaceAll("__PORT__", String(port));
|
|
337
352
|
for await (const file of walk(TEMPLATE)) {
|
|
338
|
-
const
|
|
339
|
-
const last = parts.pop();
|
|
340
|
-
const out = path2.join(...parts, RENAMED[last] ?? last);
|
|
353
|
+
const out = path2.join(...file.split(path2.sep).map((part) => RENAMED[part] ?? part));
|
|
341
354
|
const target_ = path2.join(target, out);
|
|
342
355
|
await mkdir2(path2.dirname(target_), { recursive: true });
|
|
343
|
-
|
|
356
|
+
let text = replace(await readFile(path2.join(TEMPLATE, file), "utf8"));
|
|
357
|
+
if (out === "sovereign.manifest.json") text = text.replace(/"port": \d+/, `"port": ${port}`);
|
|
358
|
+
await writeFile2(target_, text);
|
|
344
359
|
written.push(out);
|
|
345
360
|
}
|
|
346
|
-
await writeFile2(path2.join(target, "package.json"), appPackageJson(names));
|
|
361
|
+
await writeFile2(path2.join(target, "package.json"), appPackageJson(names, port));
|
|
347
362
|
written.push("package.json");
|
|
348
363
|
return written.sort();
|
|
349
364
|
}
|
|
350
365
|
async function createApp(request, io2, exec2, cwd) {
|
|
351
366
|
const target = path2.resolve(cwd, request.dir);
|
|
352
367
|
const names = namesOf(target);
|
|
353
|
-
const
|
|
354
|
-
|
|
368
|
+
const port = request.port ?? DEFAULT_APP_PORT;
|
|
369
|
+
const written = await writeApp(target, names, port);
|
|
370
|
+
io2.err(`Wrote the files of ${names.name}, on port ${port}.`);
|
|
355
371
|
const step = async (label, command, args, hint) => {
|
|
356
372
|
const done = await exec2(command, args, { cwd: target });
|
|
357
373
|
if (done.code !== 0) {
|
|
@@ -384,7 +400,8 @@ async function createApp(request, io2, exec2, cwd) {
|
|
|
384
400
|
io2.err(
|
|
385
401
|
`Made ${names.name}. Next:
|
|
386
402
|
cd ${request.dir}
|
|
387
|
-
npm install && npm run
|
|
403
|
+
npm install && npm run dev (http://localhost:${port})
|
|
404
|
+
npm run build
|
|
388
405
|
docker build -t ${names.name} .
|
|
389
406
|
Then host it with ./sv app add ${names.name} --image <the image> in your instance. See README.md.`
|
|
390
407
|
);
|
|
@@ -448,8 +465,19 @@ async function main() {
|
|
|
448
465
|
2,
|
|
449
466
|
"Add a remote to an app with git remote add."
|
|
450
467
|
);
|
|
451
|
-
|
|
468
|
+
let { port } = request;
|
|
469
|
+
if (port === void 0 && io.interactive) {
|
|
470
|
+
const answer = await io.ask(`Which port should it listen on? (${DEFAULT_APP_PORT})`);
|
|
471
|
+
port = answer === "" ? DEFAULT_APP_PORT : parsePort(answer);
|
|
472
|
+
}
|
|
473
|
+
await createApp({ dir, port }, io, exec, process.cwd());
|
|
452
474
|
} else {
|
|
475
|
+
if (request.port !== void 0)
|
|
476
|
+
throw new CreateError(
|
|
477
|
+
"--port is for an app.",
|
|
478
|
+
2,
|
|
479
|
+
"An instance\u2019s apps each listen on the port their manifest names."
|
|
480
|
+
);
|
|
453
481
|
await createInstance(
|
|
454
482
|
{ dir, remote: request.remote, cliVersion: package_default.version },
|
|
455
483
|
io,
|
package/package.json
CHANGED
package/template/app/Dockerfile
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
# This app as one image (
|
|
2
|
-
#
|
|
1
|
+
# This app as one image (contracts/app-manifest.md §4): a process that is not root, configured by its environment alone, listening on
|
|
2
|
+
# the port its manifest names, that applies its own migrations when it starts.
|
|
3
|
+
#
|
|
4
|
+
# The manifest (sovereign.manifest.json) is the one place the port is written, and the image's label is made from it, so build with the
|
|
5
|
+
# arguments @sovereignfs/manifest prints from it:
|
|
6
|
+
#
|
|
7
|
+
# eval "docker build $(npx sovereign-manifest docker-args) -t __NAME__ ."
|
|
3
8
|
|
|
4
9
|
ARG NODE_VERSION=24
|
|
5
10
|
|
|
@@ -12,9 +17,13 @@ ENV NEXT_TELEMETRY_DISABLED=1
|
|
|
12
17
|
RUN npm run build
|
|
13
18
|
|
|
14
19
|
FROM node:${NODE_VERSION}-alpine AS run
|
|
20
|
+
# The port comes from the manifest, as the build argument sovereign-manifest docker-args gives. A build without it would listen where the
|
|
21
|
+
# instance does not look, so it stops here.
|
|
22
|
+
ARG PORT
|
|
23
|
+
RUN test -n "${PORT}" || { echo 'Build with: eval "docker build $(npx sovereign-manifest docker-args) ."' >&2; exit 1; }
|
|
15
24
|
ENV NODE_ENV=production \
|
|
16
25
|
NEXT_TELEMETRY_DISABLED=1 \
|
|
17
|
-
PORT
|
|
26
|
+
PORT=${PORT} \
|
|
18
27
|
HOSTNAME=0.0.0.0
|
|
19
28
|
WORKDIR /app
|
|
20
29
|
COPY --from=build --chown=node:node /app/.next/standalone ./
|
|
@@ -22,8 +31,5 @@ COPY --from=build --chown=node:node /app/.next/static ./.next/static
|
|
|
22
31
|
# The migrations are read from disk when the server starts, and Next does not trace them.
|
|
23
32
|
COPY --from=build --chown=node:node /app/migrations ./migrations
|
|
24
33
|
USER node
|
|
25
|
-
EXPOSE
|
|
34
|
+
EXPOSE ${PORT}
|
|
26
35
|
CMD ["node", "server.js"]
|
|
27
|
-
|
|
28
|
-
# What the instance reads to host this app, without pulling the image. Keep it equal to sovereign.manifest.json: `npm run manifest:check`, or `npm run manifest:write` to copy it over.
|
|
29
|
-
LABEL io.github.sovereignfs.app-manifest=__MANIFEST_LABEL__
|
package/template/app/README.md
CHANGED
|
@@ -6,33 +6,44 @@ stands. It is a starting point: a notes page, to show the pieces. Change it into
|
|
|
6
6
|
|
|
7
7
|
## What is here
|
|
8
8
|
|
|
9
|
-
| Where | What
|
|
10
|
-
| ----------------------------------------------------- |
|
|
11
|
-
| `sovereign.manifest.json` |
|
|
12
|
-
| `Dockerfile` | One image, not root, on port
|
|
13
|
-
|
|
|
14
|
-
| `
|
|
15
|
-
| `
|
|
16
|
-
| `
|
|
17
|
-
| `
|
|
9
|
+
| Where | What |
|
|
10
|
+
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
11
|
+
| `sovereign.manifest.json` | The manifest, the one copy: what the instance needs to know to host it (its address paths, the scopes it asks for, its port and health path) |
|
|
12
|
+
| `Dockerfile` | One image, not root, on the manifest's port. Its label and port are made from the manifest when it is built |
|
|
13
|
+
| `.github/workflows/publish.yml` | On a `vX.Y.Z` tag, builds the image for amd64 and arm64 and publishes it to `ghcr.io/<owner>/<repository>` |
|
|
14
|
+
| `app/auth/[action]/route.ts` | Sign-in, sign-out, core's back-channel logout, and core's notice that a person deleted their account |
|
|
15
|
+
| `lib/auth.ts`, `lib/stores.ts` | The SDK's sign-in, and where it keeps sessions and refresh tokens (your database; tokens encrypted) |
|
|
16
|
+
| `app/api/health/route.ts` | Answers 200 when the database does |
|
|
17
|
+
| `instrumentation.ts`, `lib/migrate.ts`, `migrations/` | The app applies its own migrations when it starts, each once |
|
|
18
|
+
| `app/page.tsx` | A page behind sign-in, using [`@sovereignfs/ui`](https://www.npmjs.com/package/@sovereignfs/ui) |
|
|
18
19
|
|
|
19
20
|
## Build and host it
|
|
20
21
|
|
|
22
|
+
The image's label (which is how an instance reads the manifest without pulling the image) and its port are made from
|
|
23
|
+
`sovereign.manifest.json` by [`@sovereignfs/manifest`](https://www.npmjs.com/package/@sovereignfs/manifest), so build with what it prints:
|
|
24
|
+
|
|
21
25
|
```bash
|
|
22
|
-
|
|
26
|
+
npm install
|
|
27
|
+
eval "docker build $(npx sovereign-manifest docker-args) -t ghcr.io/you/__NAME__:0.1.0 ."
|
|
23
28
|
docker push ghcr.io/you/__NAME__:0.1.0
|
|
24
29
|
```
|
|
25
30
|
|
|
31
|
+
or push a tag and let `.github/workflows/publish.yml` do it (`package.json`'s `version` must be the tag's):
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
git tag v0.1.0 && git push origin v0.1.0
|
|
35
|
+
```
|
|
36
|
+
|
|
26
37
|
then, in your instance repository:
|
|
27
38
|
|
|
28
39
|
```bash
|
|
29
|
-
./sv app add __NAME__ --image ghcr.io/you/__NAME__:0.1.0
|
|
40
|
+
./sv app add __NAME__ --image ghcr.io/you/__NAME__:0.1.0 # the name is the app's address; it may be left out, and is then the manifest's prefix
|
|
30
41
|
./sv push && ./sv deploy
|
|
31
42
|
```
|
|
32
43
|
|
|
33
|
-
`sv` reads the manifest from the image's label, shows you the scopes it asks for, gives it its address, its
|
|
34
|
-
and registers it with core. Change `sovereign.manifest.json
|
|
35
|
-
|
|
44
|
+
`sv` reads the manifest from the image's label, shows you the scopes it asks for, gives it its address, its port (as `PORT`), its database and
|
|
45
|
+
its secrets, and registers it with core. Change `sovereign.manifest.json` and build again; `npm run manifest:check` says whether it is
|
|
46
|
+
valid.
|
|
36
47
|
|
|
37
48
|
Two things to know. A person is identified by `session.sub` and by nothing else. And when you add data about people, delete it in
|
|
38
49
|
`onAccountDeleted` in `lib/auth.ts`, which core calls when someone deletes their account.
|
|
@@ -41,8 +52,12 @@ Two things to know. A person is identified by `session.sub` and by nothing else.
|
|
|
41
52
|
|
|
42
53
|
```bash
|
|
43
54
|
cp .env.example .env # point it at a core you can reach and a PostgreSQL database
|
|
44
|
-
npm run dev
|
|
55
|
+
npm run dev # http://localhost:__PORT__
|
|
45
56
|
```
|
|
46
57
|
|
|
58
|
+
The port is `port` in `sovereign.manifest.json`, the one copy: the image listens on it, and `npm run dev`, `npm start` and
|
|
59
|
+
`__PREFIX___URL` in `.env.example` were made with it. To change it, change the manifest, the `dev` and `start` scripts and
|
|
60
|
+
`__PREFIX___URL` together.
|
|
61
|
+
|
|
47
62
|
See [Using the SDK](https://github.com/sovereignfs/sovereign/blob/main/docs/using-the-sdk.md) for what `sv.as(person)` can call, and
|
|
48
|
-
[the app manifest](https://github.com/sovereignfs/sovereign/blob/main/
|
|
63
|
+
[the app manifest](https://github.com/sovereignfs/sovereign/blob/main/contracts/app-manifest.md) for what the instance gives and expects.
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
# What the instance gives this app (
|
|
1
|
+
# What the instance gives this app (contracts/app-manifest.md §3), for running it on your own machine against a core you can reach.
|
|
2
2
|
# A secret may be given as NAME_FILE=/path instead of NAME=value.
|
|
3
|
-
__PREFIX___URL=http://localhost:
|
|
3
|
+
__PREFIX___URL=http://localhost:__PORT__
|
|
4
4
|
__PREFIX___CORE_URL=https://core.sovereign.local
|
|
5
5
|
__PREFIX___CORE_CLIENT_ID=
|
|
6
6
|
__PREFIX___CORE_CLIENT_SECRET=
|
|
7
7
|
__PREFIX___DB_DIALECT=postgres
|
|
8
8
|
__PREFIX___POSTGRES_URL=postgres://user:password@localhost:5432/__NAME__
|
|
9
|
+
# The same database on the server itself, never behind a pooler: the migrations and the token lock use it. Without it, the line above is used.
|
|
10
|
+
__PREFIX___POSTGRES_DIRECT_URL=postgres://user:password@localhost:5432/__NAME__
|
|
11
|
+
# The most connections the app may hold in all (2 or more), 2 of them for the direct address. An instance sets it, and limits the app's
|
|
12
|
+
# database role to that number plus 2. Without it, 10.
|
|
13
|
+
__PREFIX___POSTGRES_POOL_MAX=10
|
|
9
14
|
__PREFIX___AUTH_SECRET=change-me-to-something-long-and-random
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
# Builds this app's image when a version tag is pushed, and publishes it to GitHub's container registry as ghcr.io/<owner>/<repository>
|
|
4
|
+
# tagged X.Y.Z, which is where `sv app add --git` looks for it (operator-cli §4.2.1):
|
|
5
|
+
#
|
|
6
|
+
# git tag v1.0.0 && git push origin v1.0.0
|
|
7
|
+
#
|
|
8
|
+
# The image carries the manifest as its label and is built to listen on the manifest's port, both made from sovereign.manifest.json by
|
|
9
|
+
# @sovereignfs/manifest, so the manifest is written once.
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
tags:
|
|
13
|
+
- 'v*.*.*'
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
packages: write
|
|
18
|
+
|
|
19
|
+
jobs:
|
|
20
|
+
image:
|
|
21
|
+
runs-on: ubuntu-latest
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
24
|
+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
25
|
+
with:
|
|
26
|
+
node-version: 24
|
|
27
|
+
- run: npm ci --ignore-scripts --no-audit --no-fund
|
|
28
|
+
|
|
29
|
+
# The tag names the version, so it has to be the one the package says. The image is tagged X.Y.Z, without the v.
|
|
30
|
+
- id: version
|
|
31
|
+
run: |
|
|
32
|
+
expected="${GITHUB_REF_NAME#v}"
|
|
33
|
+
actual="$(node -p "require('./package.json').version")"
|
|
34
|
+
if [ "$actual" != "$expected" ]; then
|
|
35
|
+
echo "The tag $GITHUB_REF_NAME expects version $expected, but package.json says $actual." >&2
|
|
36
|
+
exit 1
|
|
37
|
+
fi
|
|
38
|
+
echo "version=$expected" >> "$GITHUB_OUTPUT"
|
|
39
|
+
|
|
40
|
+
# Fails the build on a manifest an instance would refuse.
|
|
41
|
+
- run: npx sovereign-manifest check
|
|
42
|
+
|
|
43
|
+
- id: manifest
|
|
44
|
+
run: npx sovereign-manifest docker-args --format github >> "$GITHUB_OUTPUT"
|
|
45
|
+
|
|
46
|
+
- id: image
|
|
47
|
+
run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
|
|
48
|
+
|
|
49
|
+
- uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0
|
|
50
|
+
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
|
|
51
|
+
- uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0
|
|
52
|
+
with:
|
|
53
|
+
registry: ghcr.io
|
|
54
|
+
username: ${{ github.actor }}
|
|
55
|
+
password: ${{ secrets.GITHUB_TOKEN }}
|
|
56
|
+
- uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
|
|
57
|
+
with:
|
|
58
|
+
context: .
|
|
59
|
+
platforms: linux/amd64,linux/arm64
|
|
60
|
+
push: true
|
|
61
|
+
tags: ${{ steps.image.outputs.name }}:${{ steps.version.outputs.version }}
|
|
62
|
+
labels: ${{ steps.manifest.outputs.labels }}
|
|
63
|
+
build-args: ${{ steps.manifest.outputs.build-args }}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Runs once when the server starts. The app applies its own migrations before it takes a request (
|
|
1
|
+
/** Runs once when the server starts. The app applies its own migrations before it takes a request (contracts/app-manifest.md §4). */
|
|
2
2
|
export async function register() {
|
|
3
3
|
if (process.env.NEXT_RUNTIME === 'nodejs') {
|
|
4
4
|
const { migrate } = await import('./lib/migrate');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Reads a setting the instance gives this app (
|
|
4
|
+
* Reads a setting the instance gives this app (contracts/app-manifest.md §3). A secret arrives as a file named by `NAME_FILE`, so it is
|
|
5
5
|
* never in the process list or a log; the plain `NAME` is read as well, for running it on your own machine.
|
|
6
6
|
*/
|
|
7
7
|
export function setting(name: string): string | undefined {
|
package/template/app/lib/db.ts
CHANGED
|
@@ -1,10 +1,41 @@
|
|
|
1
1
|
import { Pool } from 'pg';
|
|
2
|
-
import { required } from './config';
|
|
2
|
+
import { required, setting } from './config';
|
|
3
3
|
|
|
4
4
|
let pool: Pool | undefined;
|
|
5
|
+
let direct: Pool | undefined;
|
|
5
6
|
|
|
6
|
-
/**
|
|
7
|
+
/** What of the pool goes to `directDb()`: a lock held across a call needs a connection of its own, and it counts against the same limit. */
|
|
8
|
+
const DIRECT_MAX = 2;
|
|
9
|
+
|
|
10
|
+
/** How many connections the app may hold in all: what the instance gives it, whose limit for the app's database role is set from the same number, or 10. */
|
|
11
|
+
function poolMax(): number {
|
|
12
|
+
const given = Number(process.env.__PREFIX___POSTGRES_POOL_MAX);
|
|
13
|
+
return Number.isInteger(given) && given >= 2 ? given : 10;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The app's own database, as its own role, for everything that is safe when each statement may run on a different server connection: this
|
|
18
|
+
* is the address an instance may put behind a transaction pooler (`database.pooling` in the manifest). One pool for the process, made when
|
|
19
|
+
* it is first used so that building needs none.
|
|
20
|
+
*/
|
|
7
21
|
export function db(): Pool {
|
|
8
|
-
pool ??= new Pool({
|
|
22
|
+
pool ??= new Pool({
|
|
23
|
+
connectionString: required('__PREFIX___POSTGRES_URL'),
|
|
24
|
+
max: Math.max(poolMax() - DIRECT_MAX, 2),
|
|
25
|
+
});
|
|
9
26
|
return pool;
|
|
10
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The same database on the server itself, never through a pooler, for what needs one connection throughout: the advisory locks of the
|
|
31
|
+
* migrations and of the token refresh. An instance made before the direct address existed does not give it, and has no pooler, so the
|
|
32
|
+
* pooled address is the server and is used instead.
|
|
33
|
+
*/
|
|
34
|
+
export function directDb(): Pool {
|
|
35
|
+
direct ??= new Pool({
|
|
36
|
+
connectionString:
|
|
37
|
+
setting('__PREFIX___POSTGRES_DIRECT_URL') ?? required('__PREFIX___POSTGRES_URL'),
|
|
38
|
+
max: DIRECT_MAX,
|
|
39
|
+
});
|
|
40
|
+
return direct;
|
|
41
|
+
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { readdirSync, readFileSync } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import { directDb } from './db';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Applies the files in `migrations/`, in name order, each once (
|
|
7
|
-
* starts, and is safe to start again). One advisory lock holds a second copy of the app back until the first has finished.
|
|
6
|
+
* Applies the files in `migrations/`, in name order, each once (contracts/app-manifest.md §4: an app applies its own migrations when it
|
|
7
|
+
* starts, and is safe to start again). One advisory lock holds a second copy of the app back until the first has finished. It is a session
|
|
8
|
+
* lock, and a session is one connection, so all of it runs on the direct address, never behind a pooler.
|
|
8
9
|
*/
|
|
9
10
|
export async function migrate(): Promise<void> {
|
|
10
|
-
const client = await
|
|
11
|
+
const client = await directDb().connect();
|
|
11
12
|
try {
|
|
12
13
|
await client.query('SELECT pg_advisory_lock(hashtext($1))', ['__NAME__-migrations']);
|
|
13
14
|
await client.query(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SessionStore, StoredRecord, TokenStore } from '@sovereignfs/sdk';
|
|
2
|
-
import { db } from './db';
|
|
2
|
+
import { db, directDb } from './db';
|
|
3
3
|
import { fingerprint, open, seal } from './crypto';
|
|
4
4
|
|
|
5
5
|
/** The SDK's sessions, in the app's database (sdk §9.1). Nothing is returned after its expiry. */
|
|
@@ -83,7 +83,8 @@ export const tokenStore: TokenStore = {
|
|
|
83
83
|
},
|
|
84
84
|
|
|
85
85
|
async lock(sub, work) {
|
|
86
|
-
|
|
86
|
+
// A session lock, held across a call to core: on the direct address, which a pooler is never in front of.
|
|
87
|
+
const client = await directDb().connect();
|
|
87
88
|
try {
|
|
88
89
|
await client.query('SELECT pg_advisory_lock(hashtext($1))', [`__NAME__-token:${sub}`]);
|
|
89
90
|
return await work();
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"manifest":
|
|
3
|
-
"prefix": "
|
|
2
|
+
"manifest": 2,
|
|
3
|
+
"prefix": "__PREFIX_LOWER__",
|
|
4
4
|
"name": "__TITLE__",
|
|
5
5
|
"description": "Notes, as a starting point for an app on a Sovereign instance",
|
|
6
|
-
"port":
|
|
6
|
+
"port": 3100,
|
|
7
7
|
"health": "/api/health",
|
|
8
8
|
"launch": "/",
|
|
9
9
|
"signIn": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"accountDeleted": "/auth/account-deleted",
|
|
14
14
|
"scopes": ["openid", "profile", "email", "offline_access"]
|
|
15
15
|
},
|
|
16
|
-
"database":
|
|
16
|
+
"database": { "pooling": "transaction" },
|
|
17
17
|
"generate": ["__PREFIX___AUTH_SECRET"],
|
|
18
18
|
"core": ">=0.49.0 <1.0.0"
|
|
19
19
|
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// The manifest label in the Dockerfile is what an instance reads to host this app, so it must say what sovereign.manifest.json says.
|
|
2
|
-
// node scripts/manifest.mjs checks that it does
|
|
3
|
-
// node scripts/manifest.mjs --write copies the manifest into the label
|
|
4
|
-
import { readFileSync, writeFileSync } from 'node:fs';
|
|
5
|
-
|
|
6
|
-
const PREFIX = 'LABEL io.github.sovereignfs.app-manifest=';
|
|
7
|
-
const manifest = JSON.parse(readFileSync('sovereign.manifest.json', 'utf8'));
|
|
8
|
-
// A Dockerfile quotes a value as JSON quotes a string, so the label is the manifest as a JSON string of JSON.
|
|
9
|
-
const wanted = `${PREFIX}${JSON.stringify(JSON.stringify(manifest))}`;
|
|
10
|
-
const lines = readFileSync('Dockerfile', 'utf8').split('\n');
|
|
11
|
-
const at = lines.findIndex((line) => line.startsWith(PREFIX));
|
|
12
|
-
|
|
13
|
-
if (process.argv.includes('--write')) {
|
|
14
|
-
if (at === -1) lines.push(wanted);
|
|
15
|
-
else lines[at] = wanted;
|
|
16
|
-
writeFileSync('Dockerfile', lines.join('\n'));
|
|
17
|
-
console.log('Wrote the manifest label.');
|
|
18
|
-
} else if (lines[at] !== wanted) {
|
|
19
|
-
console.error(
|
|
20
|
-
'The manifest label in the Dockerfile differs from sovereign.manifest.json. Run: npm run manifest:write',
|
|
21
|
-
);
|
|
22
|
-
process.exit(1);
|
|
23
|
-
} else {
|
|
24
|
-
console.log('The manifest label matches.');
|
|
25
|
-
}
|