ignotum 0.0.13 → 0.0.15

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.
Files changed (131) hide show
  1. package/README.md +6 -6
  2. package/dist/cli/bin.mjs +6111 -4448
  3. package/dist/cli/bin.mjs.map +1 -1
  4. package/dist/cli/emscripten-module-D5GzfUNv.mjs +533 -0
  5. package/dist/cli/emscripten-module-D5GzfUNv.mjs.map +1 -0
  6. package/dist/cli/ffi-7IElU4Co.mjs +148 -0
  7. package/dist/cli/ffi-7IElU4Co.mjs.map +1 -0
  8. package/dist/cli/managed-release-WHYLZwaS.mjs +10902 -0
  9. package/dist/cli/managed-release-WHYLZwaS.mjs.map +1 -0
  10. package/dist/cli/managed-release.d.mts +1 -0
  11. package/dist/cli/managed-release.mjs +14 -0
  12. package/dist/cli/managed-release.mjs.map +1 -0
  13. package/dist/cli/module-ES6BEMUI-D36I4NVn.mjs +1289 -0
  14. package/dist/cli/module-ES6BEMUI-D36I4NVn.mjs.map +1 -0
  15. package/dist/runtime/{api-Cv3hMbzo.d.ts → api-B4fwp5a2.d.ts} +4 -4
  16. package/dist/runtime/bootstrap-BISQSn_O.js +916 -0
  17. package/dist/runtime/bootstrap-BISQSn_O.js.map +1 -0
  18. package/dist/runtime/bootstrap-BTFScKAc.d.ts +545 -0
  19. package/dist/runtime/client.d.ts +50 -15
  20. package/dist/runtime/client.js +7 -1138
  21. package/dist/runtime/client.js.map +1 -1
  22. package/dist/runtime/descriptor-CTY4mtwS-cS43EbwS.js +269 -0
  23. package/dist/runtime/descriptor-CTY4mtwS-cS43EbwS.js.map +1 -0
  24. package/dist/runtime/{id-Bt9XWRGL.js → id--1m0NuUL.js} +4 -4
  25. package/dist/runtime/id--1m0NuUL.js.map +1 -0
  26. package/dist/runtime/{id-BzFHf3Wo-DGPCjgrf.d.ts → id-7an3nxTu-DZJSM5xh.d.ts} +2 -2
  27. package/dist/runtime/id-CBOt2kDo.d.ts +1 -0
  28. package/dist/runtime/identity-QjtJRxBD.d.ts +2 -0
  29. package/dist/runtime/image-Djtjy4Z8.js +19 -0
  30. package/dist/runtime/image-Djtjy4Z8.js.map +1 -0
  31. package/dist/runtime/index-D4seNHlX.d.ts +184 -0
  32. package/dist/runtime/internal/api.d.ts +1 -1
  33. package/dist/runtime/internal/api.js +30 -1
  34. package/dist/runtime/internal/api.js.map +1 -0
  35. package/dist/runtime/internal/bootstrap.d.ts +2 -0
  36. package/dist/runtime/internal/bootstrap.js +2 -0
  37. package/dist/runtime/internal/client.d.ts +2 -0
  38. package/dist/runtime/internal/client.js +75 -0
  39. package/dist/runtime/internal/client.js.map +1 -0
  40. package/dist/runtime/internal/host.d.ts +25 -8
  41. package/dist/runtime/internal/host.js +27 -6
  42. package/dist/runtime/internal/host.js.map +1 -1
  43. package/dist/runtime/internal/routes.d.ts +2 -0
  44. package/dist/runtime/internal/routes.js +2 -0
  45. package/dist/runtime/internal/server.d.ts +1 -1
  46. package/dist/runtime/internal/server.js +1 -1
  47. package/dist/runtime/internal/types.d.ts +2 -1
  48. package/dist/runtime/internal/types.js +1 -1
  49. package/dist/runtime/pagination-CBOt2kDo.d.ts +1 -0
  50. package/dist/runtime/pagination-D3qd6s9N.js +33 -0
  51. package/dist/runtime/pagination-D3qd6s9N.js.map +1 -0
  52. package/dist/runtime/result-BkziOG1L.d.ts +1 -0
  53. package/dist/runtime/router-BSBI1oN1.js +2377 -0
  54. package/dist/runtime/router-BSBI1oN1.js.map +1 -0
  55. package/dist/runtime/routes-Ce8TVP-T.js +690 -0
  56. package/dist/runtime/routes-Ce8TVP-T.js.map +1 -0
  57. package/dist/runtime/{schema-1Zs03-iS.js → schema-D1wOqiNH.js} +40 -12
  58. package/dist/runtime/schema-D1wOqiNH.js.map +1 -0
  59. package/dist/runtime/server.d.ts +5 -5
  60. package/dist/runtime/server.js +2 -2
  61. package/dist/runtime/{sync-Bs8J3fIr.d.ts → sync-D-GK2sv9.d.ts} +3 -2
  62. package/dist/runtime/{api-CAgKDij7.js → sync-DzUwA8W9.js} +22 -41
  63. package/dist/runtime/sync-DzUwA8W9.js.map +1 -0
  64. package/dist/runtime/types-DeCCyqLU-BIm7rU-0.d.ts +341 -0
  65. package/package.json +29 -4
  66. package/src/cli/agent-files.ts +14 -8
  67. package/src/cli/build/managed-client.ts +411 -0
  68. package/src/cli/build/managed-server.ts +294 -0
  69. package/src/cli/build/managed.ts +67 -0
  70. package/src/cli/build/output.ts +47 -0
  71. package/src/cli/build/server.ts +51 -220
  72. package/src/cli/client-config.ts +1 -140
  73. package/src/cli/client-styles.ts +1 -0
  74. package/src/cli/codegen.ts +9 -4
  75. package/src/cli/command.ts +28 -5
  76. package/src/cli/control-client.ts +84 -101
  77. package/src/cli/deploy.ts +133 -177
  78. package/src/cli/dev.ts +506 -107
  79. package/src/cli/image-assets.ts +499 -0
  80. package/src/cli/managed-client-boundaries.ts +103 -0
  81. package/src/cli/managed-dev-platform.ts +118 -0
  82. package/src/cli/managed-exports.ts +219 -0
  83. package/src/cli/managed-release-bin.ts +13 -0
  84. package/src/cli/managed-release.ts +260 -0
  85. package/src/cli/module-boundaries.ts +11 -1
  86. package/src/cli/new-app.ts +111 -87
  87. package/src/cli/route-codegen.ts +311 -0
  88. package/src/cli/route-static.ts +710 -0
  89. package/src/client/bootstrap.ts +110 -0
  90. package/src/client/errors.ts +13 -7
  91. package/src/client/files.ts +64 -0
  92. package/src/client/id.ts +9 -5
  93. package/src/client/image.ts +28 -0
  94. package/src/client/index.ts +25 -2
  95. package/src/client/managed-client.ts +729 -0
  96. package/src/client/managed-upgrade.ts +283 -0
  97. package/src/client/recovery-journal.ts +195 -0
  98. package/src/client/route-boundaries.ts +184 -0
  99. package/src/client/router-history.ts +152 -0
  100. package/src/client/router-store.ts +644 -0
  101. package/src/client/router.ts +324 -0
  102. package/src/client/routes.ts +28 -0
  103. package/src/client/sync.ts +66 -592
  104. package/src/dev-runtime/database.ts +77 -2
  105. package/src/dev-runtime/functions.ts +3 -3
  106. package/src/dev-runtime/id.ts +19 -4
  107. package/src/dev-runtime/managed-functions.ts +255 -0
  108. package/src/dev-runtime/managed-sync.ts +263 -0
  109. package/src/dev-runtime/managed-websocket.ts +71 -0
  110. package/src/dev-runtime/migrations.ts +20 -0
  111. package/src/dev-runtime/sync.ts +236 -236
  112. package/src/image-assets.d.ts +36 -0
  113. package/src/internal/client.ts +186 -0
  114. package/src/internal/host.ts +1 -1
  115. package/src/internal/http-paths.ts +0 -3
  116. package/src/internal/routes.ts +45 -0
  117. package/dist/runtime/api-CAgKDij7.js.map +0 -1
  118. package/dist/runtime/descriptor-Cyo9FP9n-C0SRVXNW.js +0 -154
  119. package/dist/runtime/descriptor-Cyo9FP9n-C0SRVXNW.js.map +0 -1
  120. package/dist/runtime/id-Bt9XWRGL.js.map +0 -1
  121. package/dist/runtime/id-Dz0apuB3.d.ts +0 -1
  122. package/dist/runtime/index-D54flWtH.d.ts +0 -402
  123. package/dist/runtime/pagination-DnKg3dkI-r5ZUxBBx.d.ts +0 -112
  124. package/dist/runtime/pagination-Dz0apuB3.d.ts +0 -1
  125. package/dist/runtime/result-DKAA4gpS.d.ts +0 -1
  126. package/dist/runtime/schema-1Zs03-iS.js.map +0 -1
  127. package/src/cli/build/client.ts +0 -119
  128. package/src/cli/build/public.ts +0 -186
  129. package/src/cli/client-entry.ts +0 -152
  130. package/src/cli/client-plugin.ts +0 -136
  131. package/src/client/app.ts +0 -15
@@ -1,119 +0,0 @@
1
- import { Array, Effect, FileSystem, Path, String } from "effect";
2
- import tailwindcss from "@tailwindcss/vite";
3
- import { ClientManifest, clientShellPath } from "@ignotum/contracts/deployment";
4
- import { artifactReference, encodeCanonical, sha256, utf8Bytes } from "@ignotum/deployment";
5
-
6
- import { resolveClientBuildConfig, validateClientFiles } from "../client-config.js";
7
- import { loadClientEntryTitle } from "../client-entry.js";
8
- import { clientEntryId, ignotumClientPlugin, renderClientDocument } from "../client-plugin.js";
9
- import { moduleBoundariesPlugin } from "../module-boundaries.js";
10
- import { InvalidBuildOutput, runViteBuild } from "./artifact.js";
11
- import { copyPublicFiles } from "./public.js";
12
-
13
- export interface ClientBuildResult {
14
- readonly assets: number;
15
- readonly manifestPath: string;
16
- readonly shellPath: string;
17
- }
18
-
19
- export const buildClient = Effect.fn("Deploy.buildClient")(function* (
20
- appDirectory: string,
21
- outputDirectory: string,
22
- ) {
23
- const fileSystem = yield* FileSystem.FileSystem;
24
- const path = yield* Path.Path;
25
- const clientFiles = yield* validateClientFiles(appDirectory);
26
- const clientDirectory = clientFiles.clientDirectory;
27
- const title = yield* loadClientEntryTitle(clientFiles.entryPath);
28
- const clientConfig = yield* resolveClientBuildConfig(false);
29
- const boundaries = yield* moduleBoundariesPlugin("Client", appDirectory);
30
-
31
- yield* fileSystem.makeDirectory(outputDirectory, { recursive: true });
32
-
33
- const output = yield* runViteBuild("client", {
34
- appType: "spa",
35
- build: {
36
- assetsDir: "assets",
37
- emptyOutDir: true,
38
- outDir: outputDirectory,
39
- rolldownOptions: {
40
- input: clientEntryId,
41
- output: {
42
- assetFileNames: (asset) =>
43
- asset.names.some((name) => name.endsWith(".css"))
44
- ? "assets/styles-[hash][extname]"
45
- : "assets/[name]-[hash][extname]",
46
- chunkFileNames: "assets/chunks/[name]-[hash].js",
47
- entryFileNames: "assets/main-[hash].js",
48
- },
49
- },
50
- sourcemap: false,
51
- },
52
- configFile: false,
53
- logLevel: "warn",
54
- mode: "production",
55
- oxc: {
56
- jsx: {
57
- importSource: "ignotum/client",
58
- runtime: "automatic",
59
- },
60
- },
61
- plugins: [
62
- boundaries,
63
- ignotumClientPlugin({ development: false, icon: undefined, loadTitle: undefined }),
64
- tailwindcss(),
65
- ],
66
- publicDir: false,
67
- resolve: {
68
- alias: clientConfig.aliases,
69
- conditions: [...clientConfig.conditions],
70
- dedupe: ["preact"],
71
- tsconfigPaths: true,
72
- },
73
- root: clientDirectory,
74
- });
75
- const entry = Array.findFirst(output, (item) => item.type === "chunk" && item.isEntry);
76
- const entryFile = yield* Effect.fromOption(entry, () =>
77
- InvalidBuildOutput.make({ message: "The client build did not emit an entry chunk." }),
78
- ).pipe(Effect.map((item) => item.fileName));
79
- const styles = Array.sort(String.Order)(
80
- Array.map(
81
- Array.filter(output, (item) => item.type === "asset" && item.fileName.endsWith(".css")),
82
- (item) => item.fileName,
83
- ),
84
- );
85
- let icon: string | undefined;
86
- if (clientFiles.iconPath !== undefined) {
87
- const bytes = yield* fileSystem.readFile(clientFiles.iconPath);
88
- const digest = yield* sha256(bytes);
89
- const fileName = `icon-${digest.slice(0, 16)}.svg`;
90
- const assetsDirectory = path.join(outputDirectory, "assets");
91
- yield* fileSystem.makeDirectory(assetsDirectory, { recursive: true });
92
- yield* fileSystem.writeFile(path.join(assetsDirectory, fileName), bytes);
93
- icon = `/_ignotum/assets/${fileName}`;
94
- }
95
- const shellPath = path.join(outputDirectory, "shell.html");
96
- const document = renderClientDocument({
97
- icon,
98
- scripts: [{ source: `_ignotum/${entryFile}`, type: "External" }],
99
- styles: styles.map((style) => `_ignotum/${style}`),
100
- title,
101
- });
102
-
103
- const shellBytes = utf8Bytes(document);
104
- yield* fileSystem.writeFile(shellPath, shellBytes);
105
- const publicBuild = yield* copyPublicFiles(appDirectory, outputDirectory);
106
- const manifest = {
107
- formatVersion: 1,
108
- shell: yield* artifactReference(clientShellPath, shellBytes),
109
- routes: publicBuild.routes,
110
- } satisfies ClientManifest;
111
- const manifestPath = path.join(outputDirectory, "manifest.json");
112
- yield* fileSystem.writeFileString(manifestPath, `${encodeCanonical(ClientManifest, manifest)}\n`);
113
-
114
- return {
115
- assets: output.length + (icon === undefined ? 0 : 1) + publicBuild.files,
116
- manifestPath,
117
- shellPath,
118
- } satisfies ClientBuildResult;
119
- });
@@ -1,186 +0,0 @@
1
- import {
2
- ArtifactPath,
3
- ClientPath,
4
- clientPublicFileExtensions,
5
- deploymentArtifactLimits,
6
- type ClientRoute,
7
- } from "@ignotum/contracts/deployment";
8
- import { artifactReference } from "@ignotum/deployment";
9
- import { Effect, FileSystem, Option, Path, Schema } from "effect";
10
-
11
- export class InvalidPublicFile extends Schema.TaggedError<InvalidPublicFile>()(
12
- "InvalidPublicFile",
13
- {
14
- message: Schema.String,
15
- path: Schema.String,
16
- },
17
- ) {}
18
-
19
- export interface PublicBuildResult {
20
- readonly files: number;
21
- readonly routes: ReadonlyArray<ClientRoute>;
22
- }
23
-
24
- interface PublicFile {
25
- readonly bytes: Uint8Array;
26
- readonly destination: string;
27
- readonly route: ClientRoute;
28
- readonly source: string;
29
- }
30
-
31
- const pathSegmentPattern = /^[A-Za-z0-9._~-]+$/;
32
-
33
- const startsWith = (bytes: Uint8Array, signature: ReadonlyArray<number>, offset = 0): boolean =>
34
- signature.every((byte, index) => bytes[offset + index] === byte);
35
-
36
- const asciiAt = (bytes: Uint8Array, value: string, offset = 0): boolean =>
37
- startsWith(
38
- bytes,
39
- globalThis.Array.from(value, (character) => character.charCodeAt(0)),
40
- offset,
41
- );
42
-
43
- const hasValidSignature = (extension: string, bytes: Uint8Array): boolean => {
44
- switch (extension) {
45
- case ".avif": {
46
- if (!asciiAt(bytes, "ftyp", 4)) return false;
47
- const headerLength = Math.min(bytes.length, 64);
48
- for (let offset = 8; offset + 4 <= headerLength; offset += 4) {
49
- if (asciiAt(bytes, "avif", offset) || asciiAt(bytes, "avis", offset)) return true;
50
- }
51
- return false;
52
- }
53
- case ".gif":
54
- return asciiAt(bytes, "GIF87a") || asciiAt(bytes, "GIF89a");
55
- case ".ico":
56
- return startsWith(bytes, [0x00, 0x00, 0x01, 0x00]);
57
- case ".jpeg":
58
- case ".jpg":
59
- return startsWith(bytes, [0xff, 0xd8, 0xff]);
60
- case ".pdf":
61
- return asciiAt(bytes, "%PDF-");
62
- case ".png":
63
- return startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
64
- case ".webp":
65
- return asciiAt(bytes, "RIFF") && asciiAt(bytes, "WEBP", 8);
66
- default:
67
- return false;
68
- }
69
- };
70
-
71
- const invalid = (path: string, message: string) => InvalidPublicFile.make({ message, path });
72
-
73
- export const copyPublicFiles = Effect.fn("Deploy.copyPublicFiles")(function* (
74
- appDirectory: string,
75
- outputDirectory: string,
76
- ) {
77
- const fileSystem = yield* FileSystem.FileSystem;
78
- const path = yield* Path.Path;
79
- const publicDirectory = path.join(appDirectory, "public");
80
- if (!(yield* fileSystem.exists(publicDirectory))) {
81
- return { files: 0, routes: [] } satisfies PublicBuildResult;
82
- }
83
-
84
- if (Option.isSome(yield* Effect.option(fileSystem.readLink(publicDirectory)))) {
85
- return yield* invalid(
86
- publicDirectory,
87
- "The top-level public directory cannot be a symbolic link.",
88
- );
89
- }
90
- const publicInfo = yield* fileSystem.stat(publicDirectory);
91
- if (publicInfo.type !== "Directory") {
92
- return yield* invalid(publicDirectory, "The top-level public path must be a directory.");
93
- }
94
-
95
- const entries = (yield* fileSystem.readDirectory(publicDirectory, {
96
- recursive: true,
97
- })).toSorted();
98
- const publicFiles = yield* Effect.forEach(entries, (entry) =>
99
- Effect.gen(function* () {
100
- const relativePath = entry.replaceAll("\\", "/");
101
- const source = path.join(publicDirectory, entry);
102
- if (Option.isSome(yield* Effect.option(fileSystem.readLink(source)))) {
103
- return yield* invalid(source, "Public entries cannot be symbolic links.");
104
- }
105
- const info = yield* fileSystem.stat(source);
106
- if (info.type === "Directory") return undefined;
107
- if (info.type !== "File") {
108
- return yield* invalid(source, "Public entries must be regular files or directories.");
109
- }
110
-
111
- const segments = relativePath.split("/");
112
- if (segments.some((segment) => !pathSegmentPattern.test(segment))) {
113
- return yield* invalid(
114
- source,
115
- "Public file paths may only contain letters, numbers, dots, underscores, tildes, hyphens, and directory separators.",
116
- );
117
- }
118
- if (segments[0] === "_ignotum") {
119
- return yield* invalid(source, "The /_ignotum namespace is reserved by the platform.");
120
- }
121
-
122
- const extension = path.extname(relativePath).toLowerCase();
123
- if (!clientPublicFileExtensions.some((allowedExtension) => allowedExtension === extension)) {
124
- return yield* invalid(
125
- source,
126
- `Unsupported public file type '${extension || "(none)"}'. Allowed types: ${clientPublicFileExtensions.join(", ")}.`,
127
- );
128
- }
129
- if (info.size > BigInt(deploymentArtifactLimits.fileBytes)) {
130
- return yield* invalid(
131
- source,
132
- `Public files cannot exceed ${deploymentArtifactLimits.fileBytes} bytes.`,
133
- );
134
- }
135
-
136
- const bytes = yield* fileSystem.readFile(source);
137
- if (!hasValidSignature(extension, bytes)) {
138
- return yield* invalid(
139
- source,
140
- `The contents do not match the '${extension}' file extension.`,
141
- );
142
- }
143
-
144
- const pathname = `/${relativePath}`;
145
- return {
146
- bytes,
147
- destination: path.join(outputDirectory, "routes", entry),
148
- route: {
149
- pathname: ClientPath.make(pathname),
150
- artifact: yield* artifactReference(
151
- ArtifactPath.make(`client/routes/${relativePath}`),
152
- bytes,
153
- ),
154
- },
155
- source,
156
- } satisfies PublicFile;
157
- }),
158
- );
159
- const files = publicFiles.filter((file): file is PublicFile => file !== undefined);
160
-
161
- yield* Effect.forEach(files, (file) =>
162
- Effect.gen(function* () {
163
- if (yield* fileSystem.exists(file.destination)) {
164
- return yield* invalid(
165
- file.source,
166
- `Public route '${file.route.pathname}' overlaps generated client output.`,
167
- );
168
- }
169
- }),
170
- );
171
-
172
- yield* Effect.forEach(
173
- files,
174
- (file) =>
175
- Effect.gen(function* () {
176
- yield* fileSystem.makeDirectory(path.dirname(file.destination), { recursive: true });
177
- yield* fileSystem.writeFile(file.destination, file.bytes);
178
- }),
179
- { discard: true },
180
- );
181
-
182
- return {
183
- files: files.length,
184
- routes: files.map((file) => file.route),
185
- } satisfies PublicBuildResult;
186
- });
@@ -1,152 +0,0 @@
1
- import { Effect, FileSystem, Predicate, Result, Schema } from "effect";
2
- import { parseSync, type Node, type ObjectProperty, type ObjectPropertyKind } from "oxc-parser";
3
-
4
- export class InvalidClientEntry extends Schema.TaggedError<InvalidClientEntry>()(
5
- "InvalidClientEntry",
6
- {
7
- column: Schema.Int,
8
- line: Schema.Int,
9
- message: Schema.String,
10
- path: Schema.String,
11
- },
12
- ) {}
13
-
14
- export interface ClientEntrySource {
15
- readonly path: string;
16
- readonly source: string;
17
- }
18
-
19
- const requiredForm = 'export default app({ title: "My App", component: App });';
20
-
21
- const location = (source: string, offset: number) => {
22
- const prefix = source.slice(0, offset);
23
- const line = prefix.split("\n").length;
24
- const lastNewline = prefix.lastIndexOf("\n");
25
- return { column: offset - lastNewline, line };
26
- };
27
-
28
- const invalid = (entry: ClientEntrySource, node: Pick<Node, "start">, detail: string) => {
29
- const position = location(entry.source, node.start);
30
- return InvalidClientEntry.make({
31
- ...position,
32
- message: `${entry.path}:${position.line}:${position.column}: ${detail} Required form: ${requiredForm}`,
33
- path: entry.path,
34
- });
35
- };
36
-
37
- const isNamedProperty = (property: ObjectProperty, name: string): boolean =>
38
- property.kind === "init" &&
39
- !property.computed &&
40
- !property.method &&
41
- !property.shorthand &&
42
- property.key.type === "Identifier" &&
43
- property.key.name === name;
44
-
45
- const isObjectProperty = (property: ObjectPropertyKind): property is ObjectProperty =>
46
- property.type === "Property";
47
-
48
- export const extractClientEntryTitle = (
49
- entry: ClientEntrySource,
50
- ): Result.Result<string, InvalidClientEntry> => {
51
- const parsed = parseSync(entry.path, entry.source, {
52
- lang: "tsx",
53
- preserveParens: true,
54
- showSemanticErrors: true,
55
- sourceType: "module",
56
- });
57
- const parserError = parsed.errors[0];
58
- if (parserError !== undefined) {
59
- const label = parserError.labels[0];
60
- return Result.fail(
61
- invalid(
62
- entry,
63
- { start: label?.start ?? 0 },
64
- `The client entry is invalid: ${parserError.message}.`,
65
- ),
66
- );
67
- }
68
-
69
- const importsApp = parsed.program.body.some(
70
- (statement) =>
71
- statement.type === "ImportDeclaration" &&
72
- statement.source.value === "ignotum/client" &&
73
- statement.importKind !== "type" &&
74
- statement.specifiers.some(
75
- (specifier) =>
76
- specifier.type === "ImportSpecifier" &&
77
- specifier.importKind !== "type" &&
78
- specifier.imported.type === "Identifier" &&
79
- specifier.imported.name === "app" &&
80
- specifier.local.name === "app",
81
- ),
82
- );
83
- if (!importsApp) {
84
- return Result.fail(
85
- invalid(entry, parsed.program, 'Import { app } from "ignotum/client" without an alias.'),
86
- );
87
- }
88
-
89
- const defaultExports = parsed.program.body.filter(
90
- (statement) => statement.type === "ExportDefaultDeclaration",
91
- );
92
- const defaultExport = defaultExports[0];
93
- if (defaultExports.length !== 1 || defaultExport === undefined) {
94
- return Result.fail(
95
- invalid(entry, parsed.program, "The client entry needs one default export."),
96
- );
97
- }
98
- const call = defaultExport.declaration;
99
- if (
100
- call.type !== "CallExpression" ||
101
- call.optional ||
102
- call.typeArguments != null ||
103
- call.callee.type !== "Identifier" ||
104
- call.callee.name !== "app" ||
105
- call.arguments.length !== 1
106
- ) {
107
- return Result.fail(invalid(entry, defaultExport, "The default export must be an app call."));
108
- }
109
- const definition = call.arguments[0];
110
- if (definition?.type !== "ObjectExpression" || definition.properties.length !== 2) {
111
- return Result.fail(
112
- invalid(entry, call, "The app call needs exactly title and component properties."),
113
- );
114
- }
115
- const properties = definition.properties;
116
- if (!properties.every(isObjectProperty)) {
117
- return Result.fail(invalid(entry, definition, "Spread properties are not supported."));
118
- }
119
- const titleProperties = properties.filter((property) => isNamedProperty(property, "title"));
120
- const componentProperties = properties.filter((property) =>
121
- isNamedProperty(property, "component"),
122
- );
123
- if (titleProperties.length !== 1 || componentProperties.length !== 1) {
124
- return Result.fail(
125
- invalid(entry, definition, "Use unquoted title and component properties with no extras."),
126
- );
127
- }
128
- const titleProperty = titleProperties[0];
129
- if (titleProperty === undefined) {
130
- return Result.fail(invalid(entry, definition, "The title property is missing."));
131
- }
132
- const title = titleProperty.value;
133
- if (
134
- title.type !== "Literal" ||
135
- !Predicate.isString(title.value) ||
136
- (title.raw?.startsWith('"') !== true && title.raw?.startsWith("'") !== true)
137
- ) {
138
- return Result.fail(invalid(entry, title, "The title must be a quoted string literal."));
139
- }
140
- if (title.value.trim().length === 0) {
141
- return Result.fail(invalid(entry, title, "The title must contain a non-whitespace character."));
142
- }
143
- return Result.succeed(title.value);
144
- };
145
-
146
- export const loadClientEntryTitle = Effect.fn("ClientEntry.loadTitle")(function* (path: string) {
147
- const fileSystem = yield* FileSystem.FileSystem;
148
- const source = yield* fileSystem.readFileString(path);
149
- const extracted = extractClientEntryTitle({ path, source });
150
- if (Result.isFailure(extracted)) return yield* extracted.failure;
151
- return extracted.success;
152
- });
@@ -1,136 +0,0 @@
1
- import type { Plugin } from "vite";
2
-
3
- export const clientEntryId = "virtual:ignotum/client-entry";
4
- const resolvedClientEntryId = `\0${clientEntryId}`;
5
- export const clientStylesId = "virtual:ignotum/_styles.css";
6
- const resolvedClientStylesId = `\0${clientStylesId}`;
7
- export const clientStylesSource = '@import "tailwindcss";\n';
8
-
9
- interface ExternalClientScript {
10
- readonly source: string;
11
- readonly type: "External";
12
- }
13
-
14
- interface InlineClientScript {
15
- readonly source: string;
16
- readonly type: "Inline";
17
- }
18
-
19
- interface ClientDocumentOptions {
20
- readonly icon: string | undefined;
21
- readonly scripts: ReadonlyArray<ExternalClientScript | InlineClientScript>;
22
- readonly styles: ReadonlyArray<string>;
23
- readonly title: string;
24
- }
25
-
26
- export interface ClientPluginOptions {
27
- readonly development: boolean;
28
- readonly icon: string | undefined;
29
- readonly loadTitle: (() => Promise<string>) | undefined;
30
- }
31
-
32
- const escapeHtml = (value: string): string =>
33
- value
34
- .replaceAll("&", "&amp;")
35
- .replaceAll("<", "&lt;")
36
- .replaceAll(">", "&gt;")
37
- .replaceAll('"', "&quot;")
38
- .replaceAll("'", "&#39;");
39
-
40
- export const renderClientDocument = (options: ClientDocumentOptions): string => `<!doctype html>
41
- <html lang="en">
42
- <head>
43
- <meta charset="UTF-8" />
44
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
45
- <title>${escapeHtml(options.title)}</title>
46
- ${options.icon === undefined ? "" : ` <link rel="icon" type="image/svg+xml" href="${escapeHtml(options.icon)}" />`}
47
- ${options.styles.map((href) => ` <link rel="stylesheet" crossorigin href="/${escapeHtml(href)}" />`).join("\n")}
48
- </head>
49
- <body>
50
- <div id="app"></div>
51
- ${options.scripts
52
- .map((script) =>
53
- script.type === "External"
54
- ? ` <script type="module" crossorigin src="/${escapeHtml(script.source)}"></script>`
55
- : ` <script type="module">\n ${script.source}\n </script>`,
56
- )
57
- .join("\n")}
58
- </body>
59
- </html>`;
60
-
61
- export const renderClientEntry = (
62
- development: boolean,
63
- ): string => `${development ? 'import "preact/debug"\n' : ""}import { render } from "preact"
64
- import { jsx } from "preact/jsx-runtime"
65
-
66
- import "${clientStylesId}"
67
- import application from "/index.tsx"
68
-
69
- const root = document.getElementById("app")
70
-
71
- if (root === null) {
72
- throw new Error("Ignotum mount element is missing.")
73
- }
74
-
75
- document.title = application.title
76
- render(jsx(application.component, {}), root)
77
- `;
78
-
79
- const loadClientModule = (id: string, development: boolean): string | undefined => {
80
- if (id === resolvedClientEntryId) return renderClientEntry(development);
81
- if (id === resolvedClientStylesId) return clientStylesSource;
82
- return undefined;
83
- };
84
-
85
- export const resolveClientModule = (id: string): string | undefined => {
86
- if (id === clientEntryId) return resolvedClientEntryId;
87
- if (id === clientStylesId) return resolvedClientStylesId;
88
- return undefined;
89
- };
90
-
91
- export const ignotumClientPlugin = (options: ClientPluginOptions): Plugin => ({
92
- name: "ignotum:client",
93
-
94
- configureServer: (server) => {
95
- server.middlewares.use((request, response, next) => {
96
- const acceptsHtml = request.headers.accept?.includes("text/html") === true;
97
- const isNavigation = request.method === "GET" || request.method === "HEAD";
98
-
99
- if (!acceptsHtml || !isNavigation) {
100
- next();
101
- return;
102
- }
103
-
104
- const requestUrl = request.originalUrl ?? request.url ?? "/";
105
-
106
- const loadTitle = options.loadTitle;
107
- if (loadTitle === undefined) {
108
- next(new Error("Ignotum's development title loader is missing."));
109
- return;
110
- }
111
-
112
- void loadTitle()
113
- .then((title) =>
114
- server.transformIndexHtml(
115
- requestUrl,
116
- renderClientDocument({
117
- icon: options.icon,
118
- scripts: [{ source: `import "${clientEntryId}"`, type: "Inline" }],
119
- styles: [],
120
- title,
121
- }),
122
- ),
123
- )
124
- .then((html) => {
125
- response.statusCode = 200;
126
- response.setHeader("Content-Type", "text/html; charset=utf-8");
127
- response.end(request.method === "HEAD" ? undefined : html);
128
- })
129
- .catch(next);
130
- });
131
- },
132
-
133
- load: (id) => loadClientModule(id, options.development),
134
-
135
- resolveId: resolveClientModule,
136
- });
package/src/client/app.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { ComponentType } from "preact";
2
-
3
- export interface AppDefinition<Component extends ComponentType<{}> = ComponentType<{}>> {
4
- readonly component: Component;
5
- readonly title: string;
6
- }
7
-
8
- export const app = <Component extends ComponentType<{}>>(
9
- definition: AppDefinition<Component>,
10
- ): AppDefinition<Component> => {
11
- if (definition.title.trim().length === 0) {
12
- throw new Error("The app title must contain a non-whitespace character.");
13
- }
14
- return definition;
15
- };