@sovereignfs/create 0.6.0 → 0.12.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/README.md +2 -0
- package/dist/create.mjs +27 -9
- package/package.json +1 -1
- package/template/app/CONTRIBUTING.md +86 -0
- package/template/app/Dockerfile +3 -0
- package/template/app/README.md +44 -1
- package/template/app/_dockerignore +2 -0
- package/template/app/_env.example +8 -0
- package/template/app/_githooks/pre-push +5 -0
- package/template/app/_github/workflows/ci.yml +39 -0
- package/template/app/_github/workflows/publish.yml +21 -0
- package/template/app/_github/workflows/sync-develop.yml +51 -0
- package/template/app/_gitignore +2 -0
- package/template/app/app/actions.ts +13 -0
- package/template/app/app/api/files/[id]/route.ts +45 -0
- package/template/app/app/api/files/route.ts +43 -0
- package/template/app/app/page.tsx +44 -1
- package/template/app/app/upload.tsx +54 -0
- package/template/app/compose.yml +48 -0
- package/template/app/lib/auth.ts +3 -0
- package/template/app/lib/files.ts +46 -0
- package/template/app/sovereign.manifest.json +2 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @sovereignfs/create
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@sovereignfs/create) [](https://github.com/sovereignfs/sovereign/blob/main/LICENSE)
|
|
4
|
+
|
|
3
5
|
`npm create @sovereignfs` makes an instance repository for [Sovereign](https://github.com/sovereignfs/sovereign): a folder with a
|
|
4
6
|
`./sv` launcher and a `package.json` that pins `@sovereignfs/cli` of the same version as this package.
|
|
5
7
|
|
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.12.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",
|
|
@@ -91,6 +91,8 @@ var GITIGNORE = `node_modules/
|
|
|
91
91
|
age.key
|
|
92
92
|
.env
|
|
93
93
|
.env.*
|
|
94
|
+
# Backups taken by sv backup, which are never committed: encrypted in .backups/, plain copies in .backups/unsafe/.
|
|
95
|
+
.backups/
|
|
94
96
|
`;
|
|
95
97
|
function readme(name) {
|
|
96
98
|
return `# ${name}
|
|
@@ -266,16 +268,18 @@ var APP_DEPENDENCIES = {
|
|
|
266
268
|
// `@sovereignfs/ui` is one bundle that imports these two, which it lists as optional peers: an app has to have them for the bundle to load.
|
|
267
269
|
"@dnd-kit/core": "^6.3.1",
|
|
268
270
|
"@dnd-kit/sortable": "^10.0.0",
|
|
271
|
+
"@sovereignfs/config": "^0.1.0",
|
|
269
272
|
"@sovereignfs/sdk": "^2.0.0",
|
|
270
|
-
"@sovereignfs/
|
|
271
|
-
|
|
273
|
+
"@sovereignfs/storage": "^0.1.0",
|
|
274
|
+
"@sovereignfs/ui": "^0.96.1",
|
|
275
|
+
next: "^16.4.6",
|
|
272
276
|
pg: "^8.23.1",
|
|
273
277
|
react: "^19.3.1",
|
|
274
278
|
"react-dom": "^19.3.1"
|
|
275
279
|
};
|
|
276
280
|
var APP_DEV_DEPENDENCIES = {
|
|
277
281
|
// Makes the image's label and port from the manifest, and checks the manifest (`sovereign-manifest`).
|
|
278
|
-
"@sovereignfs/manifest": "^0.
|
|
282
|
+
"@sovereignfs/manifest": "^0.2.0",
|
|
279
283
|
"@types/node": "^24.19.2",
|
|
280
284
|
"@types/pg": "^8.23.1",
|
|
281
285
|
"@types/react": "^19.3.0",
|
|
@@ -292,7 +296,9 @@ var RENAMED = {
|
|
|
292
296
|
"_env.example": ".env.example",
|
|
293
297
|
// A published package may not carry a file called tsconfig.json.
|
|
294
298
|
"_tsconfig.json": "tsconfig.json",
|
|
295
|
-
_github: ".github"
|
|
299
|
+
_github: ".github",
|
|
300
|
+
// Hooks are versioned here and turned on by the `prepare` script (CONTRIBUTING.md).
|
|
301
|
+
_githooks: ".githooks"
|
|
296
302
|
};
|
|
297
303
|
function namesOf(dir) {
|
|
298
304
|
const name = path2.basename(path2.resolve(dir)).toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
@@ -335,7 +341,11 @@ function appPackageJson(names, port = DEFAULT_APP_PORT) {
|
|
|
335
341
|
build: "next build",
|
|
336
342
|
start: `PORT=\${PORT:-${port}} node .next/standalone/server.js`,
|
|
337
343
|
typecheck: "tsc --noEmit",
|
|
338
|
-
"manifest:check": "sovereign-manifest check"
|
|
344
|
+
"manifest:check": "sovereign-manifest check",
|
|
345
|
+
// What the pre-push hook runs: CI does not run for `develop` or a feature branch (CONTRIBUTING.md).
|
|
346
|
+
verify: "npm run typecheck && npm run manifest:check",
|
|
347
|
+
// Turns the versioned hooks on for whoever installs; harmless where there is no repository (the image's build).
|
|
348
|
+
prepare: "git config core.hooksPath .githooks || true"
|
|
339
349
|
},
|
|
340
350
|
dependencies: APP_DEPENDENCIES,
|
|
341
351
|
devDependencies: APP_DEV_DEPENDENCIES
|
|
@@ -348,14 +358,16 @@ function appPackageJson(names, port = DEFAULT_APP_PORT) {
|
|
|
348
358
|
async function writeApp(target, names, port = DEFAULT_APP_PORT) {
|
|
349
359
|
await checkEmpty(target);
|
|
350
360
|
const written = [];
|
|
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));
|
|
361
|
+
const replace = (text) => text.replaceAll("__PREFIX_LOWER__", names.prefix.toLowerCase()).replaceAll("__PREFIX__", names.prefix).replaceAll("__NAME__", names.name).replaceAll("__TITLE__", names.title).replaceAll("**TITLE**", names.title).replaceAll("__PORT__", String(port));
|
|
352
362
|
for await (const file of walk(TEMPLATE)) {
|
|
353
363
|
const out = path2.join(...file.split(path2.sep).map((part) => RENAMED[part] ?? part));
|
|
354
364
|
const target_ = path2.join(target, out);
|
|
355
365
|
await mkdir2(path2.dirname(target_), { recursive: true });
|
|
356
366
|
let text = replace(await readFile(path2.join(TEMPLATE, file), "utf8"));
|
|
357
367
|
if (out === "sovereign.manifest.json") text = text.replace(/"port": \d+/, `"port": ${port}`);
|
|
358
|
-
await writeFile2(target_, text
|
|
368
|
+
await writeFile2(target_, text, {
|
|
369
|
+
mode: out.startsWith(`.githooks${path2.sep}`) ? 493 : 420
|
|
370
|
+
});
|
|
359
371
|
written.push(out);
|
|
360
372
|
}
|
|
361
373
|
await writeFile2(path2.join(target, "package.json"), appPackageJson(names, port));
|
|
@@ -397,8 +409,14 @@ async function createApp(request, io2, exec2, cwd) {
|
|
|
397
409
|
["commit", "-m", "Create the app"],
|
|
398
410
|
"git needs a name and an email: git config --global user.name ... and user.email ..., then commit by hand."
|
|
399
411
|
);
|
|
412
|
+
await step(
|
|
413
|
+
"Making develop",
|
|
414
|
+
"git",
|
|
415
|
+
["switch", "--create", "develop"],
|
|
416
|
+
"Run git switch --create develop by hand."
|
|
417
|
+
);
|
|
400
418
|
io2.err(
|
|
401
|
-
`Made ${names.name}. Next:
|
|
419
|
+
`Made ${names.name}, on the branch develop (CONTRIBUTING.md: main takes release pull requests). Next:
|
|
402
420
|
cd ${request.dir}
|
|
403
421
|
npm install && npm run dev (http://localhost:${port})
|
|
404
422
|
npm run build
|
package/package.json
CHANGED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Contributing to **TITLE**
|
|
2
|
+
|
|
3
|
+
How work gets into `develop`, and from there into `main`. It is the workflow of
|
|
4
|
+
[Sovereign](https://github.com/sovereignfs/sovereign/blob/main/CONTRIBUTING.md) itself, so an app made with
|
|
5
|
+
`npm create @sovereignfs app` is worked on the same way.
|
|
6
|
+
|
|
7
|
+
## Branches
|
|
8
|
+
|
|
9
|
+
| Branch | What it is |
|
|
10
|
+
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
11
|
+
| `develop` | The working branch. Every change lands here first. `npm create @sovereignfs app` leaves you on it. It can never be deleted or force-pushed. |
|
|
12
|
+
| `main` | What is released. It is **protected**: a change reaches it only through a release pull request from `develop`, never by a push (an administrator may bypass this). |
|
|
13
|
+
|
|
14
|
+
There are two ways to get a change into `develop`; pick per change:
|
|
15
|
+
|
|
16
|
+
1. **Directly on `develop`.** Commit on an up-to-date `develop` and push it. Suits small, low-risk changes by a maintainer.
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
git switch develop && git pull
|
|
20
|
+
# edit, commit
|
|
21
|
+
git push origin develop
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
2. **A feature branch.** Branch from an up-to-date `develop`, open a draft pull request **into `develop`**, and merge it with
|
|
25
|
+
**rebase and merge** once it is reviewed. Suits anything that wants review, and every outside contribution.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
git switch develop && git pull
|
|
29
|
+
git switch -c feat/your-feature-name
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Name a branch by its change: `feat/`, `fix/`, `docs/` or `chore/`, then a short slug.
|
|
33
|
+
|
|
34
|
+
Either way, a commit explains _why_, not only _what_: a subject under 72 characters such as `fix: end the session after sign-out`, and a
|
|
35
|
+
body that says what was checked. Pull requests into `develop` are merged with **rebase and merge**, never squashed and never with a merge
|
|
36
|
+
commit; fix commit messages before they reach `develop`, which is never rewritten.
|
|
37
|
+
|
|
38
|
+
## Continuous integration
|
|
39
|
+
|
|
40
|
+
CI (`.github/workflows/ci.yml`: typecheck, the manifest check, the build) runs only where a change is about to reach what is
|
|
41
|
+
released:
|
|
42
|
+
|
|
43
|
+
- on a pull request into `main` that is not a draft (the promotion from `develop`); marking a draft ready for review runs it;
|
|
44
|
+
- on each release tag, before the image is built (`.github/workflows/publish.yml` calls it);
|
|
45
|
+
- by hand, for any branch: **Actions → CI → Run workflow**.
|
|
46
|
+
|
|
47
|
+
It never runs on a push to `develop` or a feature branch, nor on a pull request into `develop`, so runner time is not spent on every
|
|
48
|
+
commit. Those are checked on your machine instead: `npm install` turns on the versioned hooks in `.githooks/`, and the pre-push hook runs
|
|
49
|
+
`npm run verify` (typecheck and the manifest check) before anything is pushed. This is the CI strategy of every Sovereign app.
|
|
50
|
+
|
|
51
|
+
## Promoting `develop` into `main`
|
|
52
|
+
|
|
53
|
+
1. Set the new version in `package.json` on `develop` (semver: a fix is a patch, a feature a minor, a break a major).
|
|
54
|
+
2. Open a pull request from `develop` into `main`. Its title is the version, **`vX.Y.Z`**, or **`Release vX.Y.Z`** when that version
|
|
55
|
+
will be tagged and published once it is merged; not every version is. The body lists what it carries.
|
|
56
|
+
3. Merge it with **a merge commit**, the one place a merge commit is used; the merge commit takes the pull request's title.
|
|
57
|
+
`develop` is never rewritten, so it stays an ancestor of `main` and the next pull request shows only what is new.
|
|
58
|
+
4. To publish, tag the merge commit and push the tag, which builds and publishes the image (`.github/workflows/publish.yml`):
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
git switch main && git pull
|
|
62
|
+
git tag vX.Y.Z && git push origin vX.Y.Z
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A tag is not a branch, so `main`'s protection does not stop it; the workflow refuses a tag whose commit is not on `main`, or whose
|
|
66
|
+
version is not the one in `package.json`.
|
|
67
|
+
|
|
68
|
+
Once the promotion is merged, `.github/workflows/sync-develop.yml` moves `develop` forward to the merge commit. It is a fast-forward,
|
|
69
|
+
never a rewrite (`develop` is already in the merge commit's history), so `develop` is not shown as behind `main` and the release tags are
|
|
70
|
+
in its history. If a commit reached `develop` after the merge, the run leaves `develop` as it is and says so; pull before you start new
|
|
71
|
+
work either way.
|
|
72
|
+
|
|
73
|
+
## Setting it up on GitHub
|
|
74
|
+
|
|
75
|
+
Once the repository is on GitHub, push both branches and protect `main`:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
git push -u origin main develop
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Then, in **Settings → Rules → Rulesets**, add a ruleset for the default branch `main` that requires a pull request before merging, allows
|
|
82
|
+
merge commits, and blocks force pushes and deletion, with the Repository admin role allowed to bypass it. Add a second ruleset for `develop`
|
|
83
|
+
that blocks deletion and force pushes, with no one allowed to bypass it: GitHub's "Automatically delete head branches" setting would
|
|
84
|
+
otherwise delete `develop` when a promotion is merged, and it leaves a branch alone when its deletion is blocked. In **Settings → General → Pull
|
|
85
|
+
Requests**, keep merge commits and rebase merging allowed, and set the default merge commit message to the pull request's title and
|
|
86
|
+
description, so the merge commit reads `vX.Y.Z` or `Release vX.Y.Z`.
|
package/template/app/Dockerfile
CHANGED
|
@@ -30,6 +30,9 @@ COPY --from=build --chown=node:node /app/.next/standalone ./
|
|
|
30
30
|
COPY --from=build --chown=node:node /app/.next/static ./.next/static
|
|
31
31
|
# The migrations are read from disk when the server starts, and Next does not trace them.
|
|
32
32
|
COPY --from=build --chown=node:node /app/migrations ./migrations
|
|
33
|
+
# People's files (manifest `storage`): an instance mounts this app's own folder here, and running it yourself mounts a volume (compose.yml). The
|
|
34
|
+
# folder is made in the image and is the user's, because Docker makes a volume from it, owned as it is, and a root-owned one cannot be written.
|
|
35
|
+
RUN mkdir -p /data/s3/__NAME__ && chown -R node:node /data
|
|
33
36
|
USER node
|
|
34
37
|
EXPOSE ${PORT}
|
|
35
38
|
CMD ["node", "server.js"]
|
package/template/app/README.md
CHANGED
|
@@ -10,12 +10,24 @@ stands. It is a starting point: a notes page, to show the pieces. Change it into
|
|
|
10
10
|
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
11
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
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
|
|
13
|
+
| `.github/workflows/publish.yml` | On a `vX.Y.Z` tag of a commit on `main`, builds the image for amd64 and arm64 and publishes it to `ghcr.io/<owner>/<repository>` |
|
|
14
|
+
| `CONTRIBUTING.md` | How work lands on `develop` and is released to `main` (below) |
|
|
15
|
+
| `.github/workflows/ci.yml`, `.githooks/pre-push` | The checks: CI for pull requests into `main` and release tags only, the pre-push hook for everything else |
|
|
16
|
+
| `.github/workflows/sync-develop.yml` | After a promotion into `main` is merged, fast-forwards `develop` to the merge commit |
|
|
14
17
|
| `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
18
|
| `lib/auth.ts`, `lib/stores.ts` | The SDK's sign-in, and where it keeps sessions and refresh tokens (your database; tokens encrypted) |
|
|
16
19
|
| `app/api/health/route.ts` | Answers 200 when the database does |
|
|
17
20
|
| `instrumentation.ts`, `lib/migrate.ts`, `migrations/` | The app applies its own migrations when it starts, each once |
|
|
18
21
|
| `app/page.tsx` | A page behind sign-in, using [`@sovereignfs/ui`](https://www.npmjs.com/package/@sovereignfs/ui) |
|
|
22
|
+
| `lib/files.ts`, `app/api/files/`, `app/upload.tsx` | People's files, kept the suite's way: a folder and a quota for each person, served only to them (below) |
|
|
23
|
+
| `compose.yml` | Runs the app and a PostgreSQL database for it on your own machine with Docker (below) |
|
|
24
|
+
|
|
25
|
+
## Branches and releases
|
|
26
|
+
|
|
27
|
+
Work lands on `develop`, committed directly or merged from a feature branch; `main` is what is released, protected, and changed only by a
|
|
28
|
+
pull request from `develop` titled with its version, merged with a merge commit, which is tagged `vX.Y.Z` when that version is published. `npm create @sovereignfs app`
|
|
29
|
+
made both branches and left you on `develop`. [CONTRIBUTING.md](CONTRIBUTING.md) has the whole workflow, and how to protect `main` on
|
|
30
|
+
GitHub.
|
|
19
31
|
|
|
20
32
|
## Build and host it
|
|
21
33
|
|
|
@@ -45,6 +57,37 @@ then, in your instance repository:
|
|
|
45
57
|
its secrets, and registers it with core. Change `sovereign.manifest.json` and build again; `npm run manifest:check` says whether it is
|
|
46
58
|
valid.
|
|
47
59
|
|
|
60
|
+
## People's files
|
|
61
|
+
|
|
62
|
+
The manifest says `"storage": true`, so an instance gives the app a folder of its own for the files people upload, at `/data/s3/__NAME__`,
|
|
63
|
+
and nothing else of its disk ([`app-manifest.md`](https://github.com/sovereignfs/sovereign/blob/main/contracts/app-manifest.md) §2.7).
|
|
64
|
+
`lib/files.ts` makes the store with [`@sovereignfs/storage`](https://www.npmjs.com/package/@sovereignfs/storage): one folder for each
|
|
65
|
+
person, named by `session.sub`, and a quota for each person (`__PREFIX___STORAGE_QUOTA_MB` and `__PREFIX___STORAGE_FILE_QUOTA`, which an
|
|
66
|
+
operator can change; 200 GB and 1000 files unless they do).
|
|
67
|
+
|
|
68
|
+
- `POST /api/files` keeps a file (the body is the file, its name is in `X-File-Name`), `GET /api/files` lists the person's own,
|
|
69
|
+
`GET /api/files/{id}` serves one to its owner, and `DELETE /api/files/{id}` removes it. Each asks who is calling, and a request that
|
|
70
|
+
changes something must come from the app's own pages.
|
|
71
|
+
- A file is served whole or by `Range`, so a video can be sought in and a download resumed.
|
|
72
|
+
- Images are shown, and everything else is downloaded: what a file is comes from its bytes, so nothing uploaded runs as script.
|
|
73
|
+
- A refusal is a `StorageError` with a code and words a person can act on (`over_quota`, `too_large`), which the routes pass on.
|
|
74
|
+
- When a person deletes their account, `onAccountDeleted` in `lib/auth.ts` deletes their folder along with their notes.
|
|
75
|
+
|
|
76
|
+
If your app keeps no files, set `"storage": false` (or remove it), delete `lib/files.ts`, `app/api/files/` and `app/upload.tsx`, and the
|
|
77
|
+
`files()` calls in `lib/auth.ts`, `app/actions.ts` and `app/page.tsx`.
|
|
78
|
+
|
|
79
|
+
## Run it with Docker
|
|
80
|
+
|
|
81
|
+
`compose.yml` starts the app and a PostgreSQL database for it, with the files in a volume mounted where an instance mounts the app's folder,
|
|
82
|
+
so the code is the same in both:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
cp .env.example .env # and give it a core you can reach, and its client there
|
|
86
|
+
docker compose up --build # http://localhost:__PORT__
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The image makes `/data/s3/__NAME__` and gives it to the user the app runs as, which is what lets Docker's volume be written by it.
|
|
90
|
+
|
|
48
91
|
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
|
|
49
92
|
`onAccountDeleted` in `lib/auth.ts`, which core calls when someone deletes their account.
|
|
50
93
|
|
|
@@ -12,3 +12,11 @@ __PREFIX___POSTGRES_DIRECT_URL=postgres://user:password@localhost:5432/__NAME__
|
|
|
12
12
|
# database role to that number plus 2. Without it, 10.
|
|
13
13
|
__PREFIX___POSTGRES_POOL_MAX=10
|
|
14
14
|
__PREFIX___AUTH_SECRET=change-me-to-something-long-and-random
|
|
15
|
+
# People's files (contracts/app-manifest.md §2.7). An instance mounts this app's folder at /data/s3/__NAME__ and sets these; running it yourself,
|
|
16
|
+
# the folder is here (docker compose mounts a volume at /data/s3/__NAME__ instead). Each person's quota: megabytes and files.
|
|
17
|
+
__PREFIX___STORAGE_DRIVER=fs
|
|
18
|
+
__PREFIX___STORAGE_PATH=./data/s3/__NAME__
|
|
19
|
+
__PREFIX___STORAGE_QUOTA_MB=204800
|
|
20
|
+
__PREFIX___STORAGE_FILE_QUOTA=1000
|
|
21
|
+
# Signs the addresses that serve a file without a session, for a few minutes. At least 32 characters. An instance makes its own.
|
|
22
|
+
__PREFIX___STORAGE_SIGNING_KEY=change-me-to-something-at-least-thirty-two-characters
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Checks the work before it is pushed (CONTRIBUTING.md, "Continuous integration"): CI does not run for `develop` or a feature branch,
|
|
3
|
+
# so this is their check. `npm install` turns it on (the `prepare` script sets core.hooksPath to .githooks). Skip it once, knowingly,
|
|
4
|
+
# with `git push --no-verify`.
|
|
5
|
+
exec npm run --silent verify
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
# The checks, run only where a change is about to reach what is released (CONTRIBUTING.md, "Continuous integration"): on pull requests
|
|
4
|
+
# into `main` that are not drafts (the promotion from `develop`), on each release tag (publish.yml calls this first), and by hand
|
|
5
|
+
# (Actions -> CI -> Run workflow, for any branch). Never on a push to `develop` or a feature branch, nor on a pull request into
|
|
6
|
+
# `develop`: the pre-push hook (.githooks/pre-push) checks those on your machine, so runner time is not spent on every commit. This is
|
|
7
|
+
# the CI strategy of every Sovereign app (docs/building-an-app.md §13 in sovereignfs/sovereign).
|
|
8
|
+
on:
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [main]
|
|
11
|
+
types: [opened, synchronize, reopened, ready_for_review]
|
|
12
|
+
workflow_dispatch:
|
|
13
|
+
workflow_call:
|
|
14
|
+
|
|
15
|
+
concurrency:
|
|
16
|
+
group: ci-${{ github.event.pull_request.number || github.sha }}
|
|
17
|
+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
18
|
+
|
|
19
|
+
permissions:
|
|
20
|
+
contents: read
|
|
21
|
+
|
|
22
|
+
jobs:
|
|
23
|
+
check:
|
|
24
|
+
name: Check
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
timeout-minutes: 20
|
|
27
|
+
# A draft pull request runs nothing; marking it ready for review runs it.
|
|
28
|
+
if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
31
|
+
with:
|
|
32
|
+
persist-credentials: false
|
|
33
|
+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
34
|
+
with:
|
|
35
|
+
node-version: 24
|
|
36
|
+
- run: npm ci --ignore-scripts --no-audit --no-fund
|
|
37
|
+
- run: npm run typecheck
|
|
38
|
+
- run: npm run manifest:check
|
|
39
|
+
- run: npm run build
|
|
@@ -5,6 +5,8 @@ name: Publish
|
|
|
5
5
|
#
|
|
6
6
|
# git tag v1.0.0 && git push origin v1.0.0
|
|
7
7
|
#
|
|
8
|
+
# Nothing is published unless the checks in ci.yml pass on the tagged commit and that commit is on `main`.
|
|
9
|
+
#
|
|
8
10
|
# 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
11
|
# @sovereignfs/manifest, so the manifest is written once.
|
|
10
12
|
on:
|
|
@@ -17,10 +19,29 @@ permissions:
|
|
|
17
19
|
packages: write
|
|
18
20
|
|
|
19
21
|
jobs:
|
|
22
|
+
# The app's checks (ci.yml) on the tagged commit, before anything is built or published.
|
|
23
|
+
validate:
|
|
24
|
+
uses: ./.github/workflows/ci.yml
|
|
25
|
+
|
|
20
26
|
image:
|
|
27
|
+
needs: validate
|
|
21
28
|
runs-on: ubuntu-latest
|
|
22
29
|
steps:
|
|
23
30
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
31
|
+
with:
|
|
32
|
+
persist-credentials: false
|
|
33
|
+
fetch-depth: 0
|
|
34
|
+
|
|
35
|
+
# A release is the merge commit of a pull request from `develop` into `main` (CONTRIBUTING.md), never a commit of `develop` or of a
|
|
36
|
+
# branch that was tagged by mistake.
|
|
37
|
+
- name: Check the tagged commit is on main
|
|
38
|
+
run: |
|
|
39
|
+
git fetch --no-tags origin main
|
|
40
|
+
if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then
|
|
41
|
+
echo "The tagged commit is not on main; refusing to publish." >&2
|
|
42
|
+
exit 1
|
|
43
|
+
fi
|
|
44
|
+
|
|
24
45
|
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
25
46
|
with:
|
|
26
47
|
node-version: 24
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
name: Sync develop
|
|
2
|
+
|
|
3
|
+
# After a promotion pull request (develop -> main) is merged, moves `develop` forward to the merge commit (CONTRIBUTING.md, "Promoting
|
|
4
|
+
# develop into main"). It is a fast-forward, never a rewrite: the merge commit's first parent is `main` and its second is the `develop`
|
|
5
|
+
# that was promoted, so `develop` is already in its history. Afterwards GitHub no longer shows `develop` as behind `main`, and the release
|
|
6
|
+
# tags on merge commits are in `develop`'s history (`git describe`, `git tag --merged`).
|
|
7
|
+
#
|
|
8
|
+
# When `develop` moved after the merge (a commit pushed in between), it is not a fast-forward any more: the run says so and changes
|
|
9
|
+
# nothing, and `develop` stays as it would be without this workflow. It never forces.
|
|
10
|
+
on:
|
|
11
|
+
pull_request:
|
|
12
|
+
branches: [main]
|
|
13
|
+
types: [closed]
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
concurrency:
|
|
19
|
+
group: sync-develop
|
|
20
|
+
cancel-in-progress: false
|
|
21
|
+
|
|
22
|
+
jobs:
|
|
23
|
+
fast-forward:
|
|
24
|
+
name: Fast-forward develop
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
timeout-minutes: 5
|
|
27
|
+
# Only a merged promotion from this repository's own `develop`.
|
|
28
|
+
if: >-
|
|
29
|
+
github.event.pull_request.merged == true &&
|
|
30
|
+
github.event.pull_request.head.ref == 'develop' &&
|
|
31
|
+
github.event.pull_request.head.repo.full_name == github.repository
|
|
32
|
+
permissions:
|
|
33
|
+
# To push `develop`, which is not protected; `main` is never pushed.
|
|
34
|
+
contents: write
|
|
35
|
+
steps:
|
|
36
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
37
|
+
with:
|
|
38
|
+
fetch-depth: 0
|
|
39
|
+
# The push below needs the job's token; the job runs no code from the repository.
|
|
40
|
+
persist-credentials: true
|
|
41
|
+
- name: Move develop to the merge commit, if it is a fast-forward
|
|
42
|
+
env:
|
|
43
|
+
MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }}
|
|
44
|
+
run: |
|
|
45
|
+
git fetch --no-tags origin develop
|
|
46
|
+
if git merge-base --is-ancestor origin/develop "$MERGE_SHA"; then
|
|
47
|
+
git push origin "$MERGE_SHA:refs/heads/develop"
|
|
48
|
+
echo "develop is now $MERGE_SHA."
|
|
49
|
+
else
|
|
50
|
+
echo "::notice::develop has commits that are not in $MERGE_SHA (pushed after the merge), so it was left as it is."
|
|
51
|
+
fi
|
package/template/app/_gitignore
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
import { revalidatePath } from 'next/cache';
|
|
4
4
|
import { auth } from '@/lib/auth';
|
|
5
5
|
import { db } from '@/lib/db';
|
|
6
|
+
import { files } from '@/lib/files';
|
|
7
|
+
import { StorageError } from '@sovereignfs/storage';
|
|
6
8
|
|
|
7
9
|
/** Every server action is a public POST endpoint, so each one asks who is calling before it does anything. */
|
|
8
10
|
export async function addNote(formData: FormData): Promise<void> {
|
|
@@ -23,3 +25,14 @@ export async function deleteNote(formData: FormData): Promise<void> {
|
|
|
23
25
|
await db().query('DELETE FROM notes WHERE id = $1 AND owner = $2', [id, session.sub]);
|
|
24
26
|
revalidatePath('/');
|
|
25
27
|
}
|
|
28
|
+
|
|
29
|
+
export async function deleteFile(formData: FormData): Promise<void> {
|
|
30
|
+
const session = await auth().requireSession('/');
|
|
31
|
+
try {
|
|
32
|
+
// Only the person's own: the store is asked for the person's folder, so another person's file is a file that is not there.
|
|
33
|
+
await files().remove(session.sub, String(formData.get('id') ?? ''));
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (!(error instanceof StorageError && error.code === 'not_found')) throw error;
|
|
36
|
+
}
|
|
37
|
+
revalidatePath('/');
|
|
38
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { auth } from '@/lib/auth';
|
|
2
|
+
import { failure, files, refuseCrossOrigin, unauthorised } from '@/lib/files';
|
|
3
|
+
|
|
4
|
+
export const dynamic = 'force-dynamic';
|
|
5
|
+
|
|
6
|
+
type Context = { params: Promise<{ id: string }> };
|
|
7
|
+
|
|
8
|
+
/** One file, to its owner: to their session, or through an address signed for them that has not run out. Someone else's is not found. */
|
|
9
|
+
export async function GET(request: Request, { params }: Context) {
|
|
10
|
+
const { id } = await params;
|
|
11
|
+
const query = new URL(request.url).searchParams;
|
|
12
|
+
const sub = query.has('signature')
|
|
13
|
+
? files().signedFor(id, query)
|
|
14
|
+
: (await auth().getSession())?.sub;
|
|
15
|
+
if (!sub) return query.has('signature') ? expired() : unauthorised();
|
|
16
|
+
try {
|
|
17
|
+
return await files().serve(sub, id, request);
|
|
18
|
+
} catch (error) {
|
|
19
|
+
const known = failure(error);
|
|
20
|
+
if (known) return known;
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Deletes one of the signed-in person's files and gives its space back. */
|
|
26
|
+
export async function DELETE(request: Request, { params }: Context) {
|
|
27
|
+
const refused = refuseCrossOrigin(request);
|
|
28
|
+
if (refused) return refused;
|
|
29
|
+
const session = await auth().getSession();
|
|
30
|
+
if (!session) return unauthorised();
|
|
31
|
+
try {
|
|
32
|
+
await files().remove(session.sub, (await params).id);
|
|
33
|
+
return new Response(null, { status: 204 });
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const known = failure(error);
|
|
36
|
+
if (known) return known;
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const expired = () =>
|
|
42
|
+
Response.json(
|
|
43
|
+
{ error: 'expired', message: 'This link has run out. Open the file again from the app.' },
|
|
44
|
+
{ status: 403 },
|
|
45
|
+
);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { auth } from '@/lib/auth';
|
|
2
|
+
import { failure, files, refuseCrossOrigin, unauthorised } from '@/lib/files';
|
|
3
|
+
|
|
4
|
+
export const dynamic = 'force-dynamic';
|
|
5
|
+
|
|
6
|
+
/** The signed-in person's own files, newest first, and how much of their space they use. */
|
|
7
|
+
export async function GET() {
|
|
8
|
+
const session = await auth().getSession();
|
|
9
|
+
if (!session) return unauthorised();
|
|
10
|
+
const [list, usage] = await Promise.all([files().list(session.sub), files().usage(session.sub)]);
|
|
11
|
+
return Response.json(
|
|
12
|
+
{ files: list.map((file) => ({ ...file, url: `/api/files/${file.id}` })), usage },
|
|
13
|
+
{ headers: { 'cache-control': 'no-store' } },
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Stores a file for the signed-in person. The body is the file itself and its name is in `X-File-Name`, percent-encoded. */
|
|
18
|
+
export async function POST(request: Request) {
|
|
19
|
+
const refused = refuseCrossOrigin(request);
|
|
20
|
+
if (refused) return refused;
|
|
21
|
+
const session = await auth().getSession();
|
|
22
|
+
if (!session) return unauthorised();
|
|
23
|
+
try {
|
|
24
|
+
const length = Number(request.headers.get('content-length') ?? Number.NaN);
|
|
25
|
+
let name: string | undefined;
|
|
26
|
+
try {
|
|
27
|
+
name = decodeURIComponent(request.headers.get('x-file-name') ?? '');
|
|
28
|
+
} catch {
|
|
29
|
+
name = undefined;
|
|
30
|
+
}
|
|
31
|
+
const record = await files().put(
|
|
32
|
+
session.sub,
|
|
33
|
+
(request.body ?? new Uint8Array(0)) as unknown as AsyncIterable<Uint8Array>,
|
|
34
|
+
// Without a length the upload reserves all the room the person has left, so uploads of unknown size run one at a time.
|
|
35
|
+
{ name, ...(Number.isSafeInteger(length) && length >= 0 ? { size: length } : {}) },
|
|
36
|
+
);
|
|
37
|
+
return Response.json({ ...record, url: `/api/files/${record.id}` }, { status: 201 });
|
|
38
|
+
} catch (error) {
|
|
39
|
+
const known = failure(error);
|
|
40
|
+
if (known) return known;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import { auth } from '@/lib/auth';
|
|
2
2
|
import { db } from '@/lib/db';
|
|
3
|
-
import {
|
|
3
|
+
import { files } from '@/lib/files';
|
|
4
|
+
import { addNote, deleteFile, deleteNote } from './actions';
|
|
4
5
|
import { Button, Card, Input, PageHeader } from './ui';
|
|
6
|
+
import { Upload } from './upload';
|
|
7
|
+
|
|
8
|
+
/** A size as a person reads it: 512 KB, 3.4 MB, 200 GB. */
|
|
9
|
+
function size(bytes: number): string {
|
|
10
|
+
const units = ['bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
11
|
+
let value = bytes;
|
|
12
|
+
let unit = 0;
|
|
13
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
14
|
+
value /= 1024;
|
|
15
|
+
unit += 1;
|
|
16
|
+
}
|
|
17
|
+
return unit === 0 ? `${value} bytes` : `${value.toFixed(value < 10 ? 1 : 0)} ${units[unit]}`;
|
|
18
|
+
}
|
|
5
19
|
|
|
6
20
|
// The page is for the person signed in, so it is made for each request.
|
|
7
21
|
export const dynamic = 'force-dynamic';
|
|
@@ -13,6 +27,8 @@ export default async function Home() {
|
|
|
13
27
|
'SELECT id, body FROM notes WHERE owner = $1 ORDER BY id DESC LIMIT 100',
|
|
14
28
|
[session.sub],
|
|
15
29
|
);
|
|
30
|
+
// Their files, kept the suite's way: a folder for this person and a quota for this person (lib/files.ts).
|
|
31
|
+
const [kept, usage] = await Promise.all([files().list(session.sub), files().usage(session.sub)]);
|
|
16
32
|
const name = typeof session.claims.name === 'string' ? session.claims.name : 'there';
|
|
17
33
|
|
|
18
34
|
return (
|
|
@@ -39,6 +55,33 @@ export default async function Home() {
|
|
|
39
55
|
</li>
|
|
40
56
|
))}
|
|
41
57
|
</ul>
|
|
58
|
+
<Card>
|
|
59
|
+
<div>
|
|
60
|
+
<h2>Files</h2>
|
|
61
|
+
<p>
|
|
62
|
+
{usage.files} of {usage.quota.files} files, {size(usage.bytes)} of{' '}
|
|
63
|
+
{size(usage.quota.bytes)} used
|
|
64
|
+
</p>
|
|
65
|
+
<Upload />
|
|
66
|
+
</div>
|
|
67
|
+
</Card>
|
|
68
|
+
<ul>
|
|
69
|
+
{kept.map((file) => (
|
|
70
|
+
<li key={file.id}>
|
|
71
|
+
<Card>
|
|
72
|
+
<form action={deleteFile}>
|
|
73
|
+
<a href={`/api/files/${file.id}`} style={{ flex: 1 }}>
|
|
74
|
+
{file.name} ({size(file.size)})
|
|
75
|
+
</a>
|
|
76
|
+
<input type="hidden" name="id" value={file.id} />
|
|
77
|
+
<Button type="submit" variant="ghost" size="sm">
|
|
78
|
+
Delete
|
|
79
|
+
</Button>
|
|
80
|
+
</form>
|
|
81
|
+
</Card>
|
|
82
|
+
</li>
|
|
83
|
+
))}
|
|
84
|
+
</ul>
|
|
42
85
|
<form action="/auth/logout" method="post">
|
|
43
86
|
<Button type="submit" variant="secondary">
|
|
44
87
|
Sign out
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useRouter } from 'next/navigation';
|
|
4
|
+
import { useRef, useState } from 'react';
|
|
5
|
+
import { Button, Input } from './ui';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sends the chosen file to /api/files as it is, with its name beside it, and shows what the app answered when it would not keep it
|
|
9
|
+
* (too large, no room left). The page is made again afterwards, so the new file is in the list.
|
|
10
|
+
*/
|
|
11
|
+
export function Upload() {
|
|
12
|
+
const router = useRouter();
|
|
13
|
+
const input = useRef<HTMLInputElement>(null);
|
|
14
|
+
const [message, setMessage] = useState('');
|
|
15
|
+
const [busy, setBusy] = useState(false);
|
|
16
|
+
|
|
17
|
+
async function send(file: File) {
|
|
18
|
+
setBusy(true);
|
|
19
|
+
setMessage('');
|
|
20
|
+
try {
|
|
21
|
+
const reply = await fetch('/api/files', {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'x-file-name': encodeURIComponent(file.name) },
|
|
24
|
+
body: file,
|
|
25
|
+
});
|
|
26
|
+
if (!reply.ok) {
|
|
27
|
+
const body = (await reply.json().catch(() => ({}))) as { message?: string };
|
|
28
|
+
setMessage(body.message ?? 'That file could not be kept. Try again.');
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (input.current) input.current.value = '';
|
|
32
|
+
router.refresh();
|
|
33
|
+
} finally {
|
|
34
|
+
setBusy(false);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div>
|
|
40
|
+
<Input
|
|
41
|
+
ref={input}
|
|
42
|
+
type="file"
|
|
43
|
+
aria-label="A file to keep"
|
|
44
|
+
disabled={busy}
|
|
45
|
+
onChange={(event) => {
|
|
46
|
+
const file = event.target.files?.[0];
|
|
47
|
+
if (file) void send(file);
|
|
48
|
+
}}
|
|
49
|
+
/>
|
|
50
|
+
{busy ? <Button disabled>Sending…</Button> : null}
|
|
51
|
+
{message ? <p role="alert">{message}</p> : null}
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Runs this app on your own machine with Docker: the app, and a PostgreSQL database for it.
|
|
2
|
+
#
|
|
3
|
+
# cp .env.example .env # point it at a core you can reach, and give it its client there
|
|
4
|
+
# docker compose up --build
|
|
5
|
+
#
|
|
6
|
+
# The app still signs people in through a core: set __PREFIX___CORE_URL, __PREFIX___CORE_CLIENT_ID and __PREFIX___CORE_CLIENT_SECRET in .env. People's
|
|
7
|
+
# files are kept in the volume `files`, mounted where an instance mounts the app's own folder, so the app's code is the same in both
|
|
8
|
+
# (contracts/app-manifest.md §2.7).
|
|
9
|
+
services:
|
|
10
|
+
app:
|
|
11
|
+
build:
|
|
12
|
+
context: .
|
|
13
|
+
args:
|
|
14
|
+
# The manifest's port, which the image listens on. `npx sovereign-manifest docker-args` prints it, with the image's label.
|
|
15
|
+
PORT: '__PORT__'
|
|
16
|
+
ports:
|
|
17
|
+
- '__PORT__:__PORT__'
|
|
18
|
+
env_file: .env
|
|
19
|
+
environment:
|
|
20
|
+
__PREFIX___POSTGRES_URL: postgres://app:app@db:5432/app
|
|
21
|
+
__PREFIX___POSTGRES_DIRECT_URL: postgres://app:app@db:5432/app
|
|
22
|
+
__PREFIX___STORAGE_DRIVER: fs
|
|
23
|
+
__PREFIX___STORAGE_PATH: /data/s3/__NAME__
|
|
24
|
+
volumes:
|
|
25
|
+
- files:/data/s3/__NAME__
|
|
26
|
+
depends_on:
|
|
27
|
+
db:
|
|
28
|
+
condition: service_healthy
|
|
29
|
+
restart: unless-stopped
|
|
30
|
+
|
|
31
|
+
db:
|
|
32
|
+
image: postgres:18-alpine
|
|
33
|
+
environment:
|
|
34
|
+
POSTGRES_USER: app
|
|
35
|
+
POSTGRES_PASSWORD: app
|
|
36
|
+
POSTGRES_DB: app
|
|
37
|
+
volumes:
|
|
38
|
+
- db:/var/lib/postgresql
|
|
39
|
+
healthcheck:
|
|
40
|
+
test: ['CMD-SHELL', 'pg_isready -U app -d app']
|
|
41
|
+
interval: 5s
|
|
42
|
+
timeout: 5s
|
|
43
|
+
retries: 10
|
|
44
|
+
restart: unless-stopped
|
|
45
|
+
|
|
46
|
+
volumes:
|
|
47
|
+
files:
|
|
48
|
+
db:
|
package/template/app/lib/auth.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { nextAuth } from '@sovereignfs/sdk/next';
|
|
2
2
|
import { db } from './db';
|
|
3
|
+
import { files } from './files';
|
|
3
4
|
import { sovereign } from './sovereign';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -10,6 +11,8 @@ export const auth = () =>
|
|
|
10
11
|
nextAuth(sovereign(), {
|
|
11
12
|
onAccountDeleted: async ({ sub }) => {
|
|
12
13
|
await db().query('DELETE FROM notes WHERE owner = $1', [sub]);
|
|
14
|
+
// And the files they uploaded: their folder, which is gone once this has run, however often it does.
|
|
15
|
+
await files().removeAll(sub);
|
|
13
16
|
// The SDK has already ended the person's sessions and forgotten their refresh token.
|
|
14
17
|
},
|
|
15
18
|
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { defineConfig } from '@sovereignfs/config';
|
|
2
|
+
import {
|
|
3
|
+
createFileStore,
|
|
4
|
+
StorageError,
|
|
5
|
+
storageVariables,
|
|
6
|
+
type FileStore,
|
|
7
|
+
} from '@sovereignfs/storage';
|
|
8
|
+
import { required } from './config';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The files people upload to this app (contracts/app-manifest.md §2.7): a folder per person and a quota per person, kept by
|
|
12
|
+
* `@sovereignfs/storage`. An instance mounts this app's own folder at /data/s3/__NAME__ and sets the variables; running it yourself, set
|
|
13
|
+
* __PREFIX___STORAGE_PATH (see .env.example). Made when first used, so building the app needs none of it.
|
|
14
|
+
*/
|
|
15
|
+
const config = defineConfig({ prefix: '__PREFIX__', variables: { ...storageVariables } });
|
|
16
|
+
let store: FileStore | undefined;
|
|
17
|
+
|
|
18
|
+
export function files(): FileStore {
|
|
19
|
+
store ??= createFileStore(config.load(), {
|
|
20
|
+
prefix: '__PREFIX__',
|
|
21
|
+
defaultPath: '/data/s3/__NAME__',
|
|
22
|
+
});
|
|
23
|
+
return store;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A route that changes something answers only a request that came from this app's own pages: the cookie is sent with a request from
|
|
28
|
+
* anywhere, so without this a page on another site could make the signed-in person upload or delete.
|
|
29
|
+
*/
|
|
30
|
+
export function refuseCrossOrigin(request: Request): Response | undefined {
|
|
31
|
+
const origin = request.headers.get('origin');
|
|
32
|
+
if (origin !== null && origin === new URL(required('__PREFIX___URL')).origin) return undefined;
|
|
33
|
+
return Response.json(
|
|
34
|
+
{ error: 'forbidden', message: 'This request did not come from this app.' },
|
|
35
|
+
{ status: 403 },
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What a store's refusal looks like to a page: a code, and words a person can act on. Anything else is not caught here. */
|
|
40
|
+
export function failure(error: unknown): Response | undefined {
|
|
41
|
+
if (!(error instanceof StorageError)) return undefined;
|
|
42
|
+
return Response.json({ error: error.code, message: error.message }, { status: error.status });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const unauthorised = () =>
|
|
46
|
+
Response.json({ error: 'unauthorised', message: 'Sign in first.' }, { status: 401 });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"manifest":
|
|
2
|
+
"manifest": 3,
|
|
3
3
|
"prefix": "__PREFIX_LOWER__",
|
|
4
4
|
"name": "__TITLE__",
|
|
5
5
|
"description": "Notes, as a starting point for an app on a Sovereign instance",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"scopes": ["openid", "profile", "email", "offline_access"]
|
|
15
15
|
},
|
|
16
16
|
"database": { "pooling": "transaction" },
|
|
17
|
+
"storage": true,
|
|
17
18
|
"generate": ["__PREFIX___AUTH_SECRET"],
|
|
18
19
|
"core": ">=0.49.0 <1.0.0"
|
|
19
20
|
}
|