@sovereignfs/create 0.17.0 → 0.18.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 +4 -2
- package/package.json +2 -1
- package/template/app/app/layout.tsx +15 -4
- package/template/app/app/manifest.ts +13 -0
- package/template/app/app/pwa-icon/[variant]/route.tsx +43 -0
- package/template/app/app/sw.js/route.ts +40 -0
- package/template/app/lib/csp.ts +66 -0
- package/template/app/proxy.ts +42 -0
- package/template/app/sovereign.manifest.json +1 -0
- package/template/encryption/sovereign.manifest.json +1 -0
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.18.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",
|
|
@@ -35,6 +35,7 @@ var package_default = {
|
|
|
35
35
|
typecheck: "tsc --noEmit"
|
|
36
36
|
},
|
|
37
37
|
devDependencies: {
|
|
38
|
+
"@sovereignfs/pwa": "workspace:*",
|
|
38
39
|
"@sovereignfs/tsconfig": "workspace:*",
|
|
39
40
|
"@types/node": "catalog:",
|
|
40
41
|
"@types/semver": "^7.8.0",
|
|
@@ -320,9 +321,10 @@ var APP_DEPENDENCIES = {
|
|
|
320
321
|
"@dnd-kit/core": "^6.3.1",
|
|
321
322
|
"@dnd-kit/sortable": "^10.0.0",
|
|
322
323
|
"@sovereignfs/config": "^0.1.0",
|
|
324
|
+
"@sovereignfs/pwa": "^0.1.0",
|
|
323
325
|
"@sovereignfs/sdk": "^2.0.0",
|
|
324
326
|
"@sovereignfs/storage": "^0.1.0",
|
|
325
|
-
"@sovereignfs/ui": "^0.
|
|
327
|
+
"@sovereignfs/ui": "^0.101.0",
|
|
326
328
|
next: "^16.4.6",
|
|
327
329
|
pg: "^8.23.1",
|
|
328
330
|
react: "^19.3.1",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sovereignfs/create",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
|
|
6
6
|
"license": "AGPL-3.0-or-later",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"node": ">=24.15"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
+
"@sovereignfs/pwa": "0.1.0",
|
|
27
28
|
"@sovereignfs/tsconfig": "0.0.0",
|
|
28
29
|
"@types/node": "^24.19.2",
|
|
29
30
|
"@types/semver": "^7.8.0",
|
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
import type { Metadata } from 'next';
|
|
1
|
+
import type { Metadata, Viewport } from 'next';
|
|
2
|
+
import { connection } from 'next/server';
|
|
2
3
|
import type { ReactNode } from 'react';
|
|
4
|
+
import { pwaMetadata, pwaViewport } from '@sovereignfs/pwa';
|
|
5
|
+
import { ServiceWorkerRegistration } from '@sovereignfs/pwa/client';
|
|
3
6
|
import '@sovereignfs/ui/tokens.css';
|
|
4
7
|
import './globals.css';
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
// What a device is told to install the app and open it as one (`@sovereignfs/pwa`): an iPhone reads these, not the manifest.
|
|
10
|
+
export const metadata: Metadata = { title: '__TITLE__', ...pwaMetadata({ name: '__TITLE__' }) };
|
|
11
|
+
export const viewport: Viewport = pwaViewport();
|
|
7
12
|
|
|
8
|
-
export default function RootLayout({ children }: { children: ReactNode }) {
|
|
13
|
+
export default async function RootLayout({ children }: { children: ReactNode }) {
|
|
14
|
+
// Every page is rendered per request: its scripts carry the nonce of that request's policy (proxy.ts).
|
|
15
|
+
await connection();
|
|
9
16
|
return (
|
|
10
17
|
<html lang="en">
|
|
11
|
-
<body>
|
|
18
|
+
<body>
|
|
19
|
+
{children}
|
|
20
|
+
{/* Registers the service worker in production, and says when a new version is ready. */}
|
|
21
|
+
<ServiceWorkerRegistration appName="__TITLE__" />
|
|
22
|
+
</body>
|
|
12
23
|
</html>
|
|
13
24
|
);
|
|
14
25
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { manifest } from '@sovereignfs/pwa';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a browser reads to install __TITLE__ (`@sovereignfs/pwa`): a window of its own, the icons in `app/pwa-icon`, and what lets an
|
|
5
|
+
* installed core open the installed app rather than a browser view. Its id is the start address: never change it once people have
|
|
6
|
+
* installed the app, or each install becomes another app.
|
|
7
|
+
*/
|
|
8
|
+
export default function webManifest() {
|
|
9
|
+
return manifest({
|
|
10
|
+
name: '__TITLE__',
|
|
11
|
+
description: 'Notes, as a starting point for an app on a Sovereign instance',
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ImageResponse } from 'next/og';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The app's icons, as PNGs drawn from its initial: the sizes the manifest names (`iconSet()` in `@sovereignfs/pwa`), one drawn to be
|
|
5
|
+
* masked (its mark inside the middle 80%), and the 180 pixel one an iPhone puts on its Home Screen. Replace them with your own pictures
|
|
6
|
+
* when you have them, at the same addresses. Made once, at build time.
|
|
7
|
+
*/
|
|
8
|
+
const VARIANTS: Record<string, { size: number; mark: number }> = {
|
|
9
|
+
'192': { size: 192, mark: 0.6 },
|
|
10
|
+
'512': { size: 512, mark: 0.6 },
|
|
11
|
+
maskable: { size: 512, mark: 0.45 },
|
|
12
|
+
apple: { size: 180, mark: 0.6 },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const dynamic = 'force-static';
|
|
16
|
+
|
|
17
|
+
export function generateStaticParams() {
|
|
18
|
+
return Object.keys(VARIANTS).map((variant) => ({ variant }));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function GET(_request: Request, { params }: { params: Promise<{ variant: string }> }) {
|
|
22
|
+
const { variant } = await params;
|
|
23
|
+
const icon = Object.hasOwn(VARIANTS, variant) ? VARIANTS[variant] : undefined;
|
|
24
|
+
if (!icon) return new Response(null, { status: 404 });
|
|
25
|
+
return new ImageResponse(
|
|
26
|
+
<div
|
|
27
|
+
style={{
|
|
28
|
+
width: '100%',
|
|
29
|
+
height: '100%',
|
|
30
|
+
display: 'flex',
|
|
31
|
+
alignItems: 'center',
|
|
32
|
+
justifyContent: 'center',
|
|
33
|
+
background: '#18181b',
|
|
34
|
+
color: '#ffffff',
|
|
35
|
+
fontSize: Math.round(icon.size * icon.mark),
|
|
36
|
+
fontWeight: 600,
|
|
37
|
+
}}
|
|
38
|
+
>
|
|
39
|
+
{'__TITLE__'.charAt(0)}
|
|
40
|
+
</div>,
|
|
41
|
+
{ width: icon.size, height: icon.size },
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { serviceWorkerResponse, serviceWorkerSource } from '@sovereignfs/pwa';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The service worker, served from the root so it controls the whole app (`@sovereignfs/pwa`). It keeps only what is the same for
|
|
7
|
+
* everyone, never a page of a person. Its version is the build's, so a new build is a new worker, announced to an open page.
|
|
8
|
+
*/
|
|
9
|
+
export const dynamic = 'force-dynamic';
|
|
10
|
+
|
|
11
|
+
/** The build's id, or `development` when this is not a production build (where nothing registers the worker). */
|
|
12
|
+
function buildId(): string {
|
|
13
|
+
try {
|
|
14
|
+
return readFileSync(join(process.cwd(), '.next', 'BUILD_ID'), 'utf8').trim();
|
|
15
|
+
} catch {
|
|
16
|
+
return 'development';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The build's static files as the addresses they are served at: scripts, styles and fonts, the same for everyone. */
|
|
21
|
+
function builtFiles(): string[] {
|
|
22
|
+
const out: string[] = [];
|
|
23
|
+
const walk = (directory: string, prefix: string) => {
|
|
24
|
+
for (const name of readdirSync(directory)) {
|
|
25
|
+
const path = join(directory, name);
|
|
26
|
+
if (statSync(path).isDirectory()) walk(path, `${prefix}${name}/`);
|
|
27
|
+
else if (!name.endsWith('.map')) out.push(`/_next/static/${prefix}${name}`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
walk(join(process.cwd(), '.next', 'static'), '');
|
|
32
|
+
} catch {
|
|
33
|
+
// Not a production build: nothing to keep.
|
|
34
|
+
}
|
|
35
|
+
return out.sort();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function GET() {
|
|
39
|
+
return serviceWorkerResponse(serviceWorkerSource({ version: buildId(), precache: builtFiles() }));
|
|
40
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { PWA_CSP_DIRECTIVES } from '@sovereignfs/pwa';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Content-Security-Policy every page is sent with (`proxy.ts`), after the deployed apps' own. Nothing may run that the server did not
|
|
5
|
+
* mark, and nothing loads from another site:
|
|
6
|
+
*
|
|
7
|
+
* - **Scripts** run only with this request's nonce, or when a script that has it loads them (`'strict-dynamic'`): the bundles Next adds.
|
|
8
|
+
* No inline handler, no `javascript:` address, no `eval` but in development, where React wants it.
|
|
9
|
+
* - **Style elements** carry the nonce; **style attributes** are allowed, which the design system positions overlays with and which can
|
|
10
|
+
* change how something looks but not run anything.
|
|
11
|
+
* - **Everything else is this origin's**: images, fonts, what the page fetches, the service worker and the manifest.
|
|
12
|
+
* - **Forms** go to this origin and to core: signing out posts here, and the answer sends the browser on to core.
|
|
13
|
+
* - Nobody may frame a page, `<base>` cannot be changed, and over https every request of the page is made over https.
|
|
14
|
+
*
|
|
15
|
+
* Kept free of Node's built-ins, since the proxy imports it.
|
|
16
|
+
*/
|
|
17
|
+
export type PolicyOptions = {
|
|
18
|
+
/** New for every request, unguessable. */
|
|
19
|
+
nonce: string;
|
|
20
|
+
/** The app's own address is https. */
|
|
21
|
+
secure: boolean;
|
|
22
|
+
/** `next dev`, which needs `eval` and inline styles for its overlay. */
|
|
23
|
+
development: boolean;
|
|
24
|
+
/** Core's origin, where signing out goes on to. */
|
|
25
|
+
core?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function contentSecurityPolicy({ nonce, secure, development, core }: PolicyOptions): string {
|
|
29
|
+
// A nonce in the list makes browsers ignore 'unsafe-inline', so in development the styles allow inline alone.
|
|
30
|
+
const styles = development ? "'self' 'unsafe-inline'" : `'self' 'nonce-${nonce}'`;
|
|
31
|
+
return [
|
|
32
|
+
"default-src 'self'",
|
|
33
|
+
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'${development ? " 'unsafe-eval'" : ''}`,
|
|
34
|
+
`style-src ${styles}`,
|
|
35
|
+
`style-src-elem ${styles}`,
|
|
36
|
+
"style-src-attr 'unsafe-inline'",
|
|
37
|
+
"img-src 'self' data: blob:",
|
|
38
|
+
"font-src 'self'",
|
|
39
|
+
"connect-src 'self'",
|
|
40
|
+
...PWA_CSP_DIRECTIVES,
|
|
41
|
+
"frame-src 'none'",
|
|
42
|
+
"object-src 'none'",
|
|
43
|
+
"base-uri 'self'",
|
|
44
|
+
`form-action 'self'${core ? ` ${core}` : ''}`,
|
|
45
|
+
"frame-ancestors 'none'",
|
|
46
|
+
...(secure ? ['upgrade-insecure-requests'] : []),
|
|
47
|
+
].join('; ');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 128 bits from the platform's random source, for one request. */
|
|
51
|
+
export function newNonce(): string {
|
|
52
|
+
const bytes = new Uint8Array(16);
|
|
53
|
+
crypto.getRandomValues(bytes);
|
|
54
|
+
return btoa(String.fromCharCode(...bytes));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The origin of an address, or nothing when it is not an http(s) address. */
|
|
58
|
+
export function originOf(address: string | undefined): string | undefined {
|
|
59
|
+
if (!address) return undefined;
|
|
60
|
+
try {
|
|
61
|
+
const url = new URL(address);
|
|
62
|
+
return url.protocol === 'https:' || url.protocol === 'http:' ? url.origin : undefined;
|
|
63
|
+
} catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { contentSecurityPolicy, newNonce, originOf } from './lib/csp';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* **Every page is sent with a Content-Security-Policy** with a nonce of its own (`lib/csp.ts`). The proxy makes the nonce and puts the
|
|
6
|
+
* policy on the request, where Next reads it and puts the nonce on the scripts it adds, and on the response, where the browser enforces
|
|
7
|
+
* it; the layout makes every page render per request, which a nonce needs. API answers, the sign-in routes, the service worker, the
|
|
8
|
+
* manifest, the icons and the built files are not pages, and are left alone.
|
|
9
|
+
*
|
|
10
|
+
* It only looks at the request: no database, no Node built-in, and never the only thing standing in a request's way.
|
|
11
|
+
*/
|
|
12
|
+
export function proxy(request: NextRequest) {
|
|
13
|
+
const nonce = newNonce();
|
|
14
|
+
const policy = contentSecurityPolicy({
|
|
15
|
+
nonce,
|
|
16
|
+
secure: (process.env.__PREFIX___URL ?? '').startsWith('https://'),
|
|
17
|
+
development: process.env.NODE_ENV === 'development',
|
|
18
|
+
core: originOf(process.env.__PREFIX___CORE_URL),
|
|
19
|
+
});
|
|
20
|
+
const headers = new Headers(request.headers);
|
|
21
|
+
headers.set('x-nonce', nonce);
|
|
22
|
+
headers.set('content-security-policy', policy);
|
|
23
|
+
const response = NextResponse.next({ request: { headers } });
|
|
24
|
+
response.headers.set('content-security-policy', policy);
|
|
25
|
+
response.headers.set('x-content-type-options', 'nosniff');
|
|
26
|
+
response.headers.set('x-frame-options', 'DENY');
|
|
27
|
+
return response;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const config = {
|
|
31
|
+
matcher: [
|
|
32
|
+
{
|
|
33
|
+
source:
|
|
34
|
+
'/((?!api/|auth/|_next/static|_next/image|sw\\.js|manifest\\.webmanifest|pwa-icon/|favicon\\.ico).*)',
|
|
35
|
+
// A prefetch is not a page someone sees: it needs no nonce of its own.
|
|
36
|
+
missing: [
|
|
37
|
+
{ type: 'header', key: 'next-router-prefetch' },
|
|
38
|
+
{ type: 'header', key: 'purpose', value: 'prefetch' },
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|