akanjs 2.4.2-rc.1 → 2.4.2-rc.2
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/common/index.ts +1 -1
- package/common/subRoute.ts +61 -0
- package/constant/cascadePaths.ts +32 -0
- package/constant/fieldInfo.ts +7 -0
- package/constant/index.ts +1 -0
- package/constant/via.ts +6 -0
- package/package.json +1 -1
- package/server/di/diLifecycle.ts +7 -1
- package/server/proxy/hostBasePathWebProxy.ts +16 -5
- package/server/resolver/service.resolver.ts +27 -5
- package/server/webRouter.ts +27 -11
- package/types/common/index.d.ts +1 -1
- package/types/common/subRoute.d.ts +19 -0
- package/types/constant/cascadePaths.d.ts +11 -0
- package/types/constant/fieldInfo.d.ts +4 -0
- package/types/constant/index.d.ts +1 -0
- package/types/constant/via.d.ts +4 -0
- package/types/server/resolver/service.resolver.d.ts +3 -3
package/common/index.ts
CHANGED
|
@@ -52,7 +52,7 @@ export {
|
|
|
52
52
|
} from "./routeConvention";
|
|
53
53
|
export { sleep } from "./sleep";
|
|
54
54
|
export { splitVersion } from "./splitVersion";
|
|
55
|
-
export { getBasePathFromPathname, parseBasePaths } from "./subRoute";
|
|
55
|
+
export { getBasePathFromPathname, parseBasePaths, parseSubRouteHosts, resolveSubRouteHosts } from "./subRoute";
|
|
56
56
|
export type * from "./types";
|
|
57
57
|
export {
|
|
58
58
|
type WebsocketAuthAckData,
|
package/common/subRoute.ts
CHANGED
|
@@ -12,6 +12,67 @@ export const parseBasePaths = (value: string | string[] | Set<string> | undefine
|
|
|
12
12
|
return [...new Set(items.map((basePath) => basePath.trim()).filter(Boolean))];
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
+
const normalizeSubRouteHost = (host: string): string => host.trim().toLowerCase().replace(/:\d+$/, "");
|
|
16
|
+
|
|
17
|
+
/** A hostname never contains these; a fragment carrying one is a malformed entry, not a host we failed to match. */
|
|
18
|
+
const isSubRouteHost = (host: string): boolean => host.length > 0 && !/[\s/=]/.test(host);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `"soft=a.com,b.com;office=c.com"` -> `{ soft: ["a.com", "b.com"], office: ["c.com"] }`. A deployment platform
|
|
22
|
+
* renders this value, so a malformed entry is skipped rather than thrown: one bad character must not CrashLoop
|
|
23
|
+
* every pod that received it.
|
|
24
|
+
*/
|
|
25
|
+
export const parseSubRouteHosts = (value: string | undefined | null): Record<string, string[]> => {
|
|
26
|
+
const hostsByBasePath: Record<string, string[]> = {};
|
|
27
|
+
for (const group of (value ?? "").split(";")) {
|
|
28
|
+
const separatorIdx = group.indexOf("=");
|
|
29
|
+
if (separatorIdx < 0) continue;
|
|
30
|
+
const basePath = group
|
|
31
|
+
.slice(0, separatorIdx)
|
|
32
|
+
.trim()
|
|
33
|
+
.replace(/^\/+|\/+$/g, "");
|
|
34
|
+
if (!basePath) continue;
|
|
35
|
+
const hosts = group
|
|
36
|
+
.slice(separatorIdx + 1)
|
|
37
|
+
.split(",")
|
|
38
|
+
.map(normalizeSubRouteHost)
|
|
39
|
+
.filter(isSubRouteHost);
|
|
40
|
+
if (!hosts.length) continue;
|
|
41
|
+
hostsByBasePath[basePath] = [...new Set([...(hostsByBasePath[basePath] ?? []), ...hosts])];
|
|
42
|
+
}
|
|
43
|
+
return hostsByBasePath;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Unions the env mapping onto the one baked into the build artifact, never replacing it — dropping the env is the
|
|
48
|
+
* rollback path. A basePath the build does not serve is reported back instead of honoured: the route tree is a
|
|
49
|
+
* build output, so accepting one would answer every request under it with a 404 and nothing to explain why.
|
|
50
|
+
*/
|
|
51
|
+
export const resolveSubRouteHosts = ({
|
|
52
|
+
subRoutes,
|
|
53
|
+
basePaths,
|
|
54
|
+
env,
|
|
55
|
+
}: {
|
|
56
|
+
subRoutes: Record<string, string[]>;
|
|
57
|
+
basePaths: Iterable<string>;
|
|
58
|
+
env?: string | null;
|
|
59
|
+
}): { subRoutes: Record<string, string[]>; ignoredBasePaths: string[] } => {
|
|
60
|
+
const parsed = Object.entries(parseSubRouteHosts(env));
|
|
61
|
+
if (!parsed.length) return { subRoutes, ignoredBasePaths: [] };
|
|
62
|
+
|
|
63
|
+
const configuredBasePaths = new Set(parseBasePaths([...basePaths]));
|
|
64
|
+
const merged: Record<string, string[]> = { ...subRoutes };
|
|
65
|
+
const ignoredBasePaths: string[] = [];
|
|
66
|
+
for (const [basePath, hosts] of parsed) {
|
|
67
|
+
if (!configuredBasePaths.has(basePath)) {
|
|
68
|
+
ignoredBasePaths.push(basePath);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
merged[basePath] = [...new Set([...(merged[basePath] ?? []).map(normalizeSubRouteHost), ...hosts])];
|
|
72
|
+
}
|
|
73
|
+
return { subRoutes: merged, ignoredBasePaths };
|
|
74
|
+
};
|
|
75
|
+
|
|
15
76
|
export const getBasePathFromPathname = (
|
|
16
77
|
pathname: string,
|
|
17
78
|
{
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ConstantField, FieldObject } from "./fieldInfo";
|
|
2
|
+
import type { ConstantModelRef } from "./via";
|
|
3
|
+
|
|
4
|
+
/** What happens to the documents a relation field points at when the owner is removed. */
|
|
5
|
+
export const cascadeActions = ["remove"] as const;
|
|
6
|
+
export type CascadeAction = (typeof cascadeActions)[number];
|
|
7
|
+
|
|
8
|
+
export class CascadePaths {
|
|
9
|
+
/** Field key → the model its ids point at. Resolved to a refName later: the target may not be registered yet. */
|
|
10
|
+
readonly remove = new Map<string, ConstantModelRef>();
|
|
11
|
+
|
|
12
|
+
collect(fieldMap: FieldObject) {
|
|
13
|
+
for (const [key, field] of Object.entries(fieldMap)) {
|
|
14
|
+
if (!field.cascade) continue;
|
|
15
|
+
this.#assertCascadable(key, field.cascade, field);
|
|
16
|
+
this.remove.set(key, field.modelRef);
|
|
17
|
+
}
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#assertCascadable(key: string, action: CascadeAction, field: ConstantField) {
|
|
22
|
+
|
|
23
|
+
if (!cascadeActions.includes(action)) {
|
|
24
|
+
throw new Error(`Cascade field "${key}" declares cascade: "${action}", which is not one of ${cascadeActions}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!field.isClass || field.isScalar) {
|
|
28
|
+
throw new Error(`Cascade field "${key}" is not a model reference and has no document to remove`);
|
|
29
|
+
}
|
|
30
|
+
if (field.arrDepth > 1) throw new Error(`Cascade field "${key}" is a nested array and cannot cascade`);
|
|
31
|
+
}
|
|
32
|
+
}
|
package/constant/fieldInfo.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type SingleValue,
|
|
17
17
|
type UnCls,
|
|
18
18
|
} from "akanjs/base";
|
|
19
|
+
import type { CascadeAction } from "./cascadePaths";
|
|
19
20
|
import { ConstantRegistry } from "./constantRegistry";
|
|
20
21
|
import type { TextFieldRole } from "./textFieldPaths";
|
|
21
22
|
import type { BaseObject } from "./types";
|
|
@@ -104,6 +105,7 @@ export interface ConstantFieldProps<
|
|
|
104
105
|
of?: MapValue;
|
|
105
106
|
validate?: (value: FieldValue, model: any) => boolean;
|
|
106
107
|
text?: TextFieldRole;
|
|
108
|
+
cascade?: CascadeAction;
|
|
107
109
|
meta?: Metadata;
|
|
108
110
|
}
|
|
109
111
|
export const fieldPresets = ["email", "password", "url"] as const;
|
|
@@ -194,6 +196,7 @@ interface ConstantFieldBuildProps<
|
|
|
194
196
|
of?: MapValue;
|
|
195
197
|
validate?: (value: FieldValue, model: any) => boolean;
|
|
196
198
|
text?: TextFieldRole;
|
|
199
|
+
cascade?: CascadeAction;
|
|
197
200
|
modelRef: ConstantModelRef;
|
|
198
201
|
arrDepth: number;
|
|
199
202
|
optArrDepth: number;
|
|
@@ -317,6 +320,7 @@ export class ConstantField<
|
|
|
317
320
|
readonly of?: MapValue;
|
|
318
321
|
readonly validate?: (value: FieldValue, model: any) => boolean;
|
|
319
322
|
readonly text?: TextFieldRole;
|
|
323
|
+
readonly cascade?: CascadeAction;
|
|
320
324
|
readonly modelRef: ConstantModelRef;
|
|
321
325
|
readonly arrDepth: number;
|
|
322
326
|
readonly optArrDepth: number;
|
|
@@ -348,6 +352,7 @@ export class ConstantField<
|
|
|
348
352
|
this.of = props.of;
|
|
349
353
|
this.validate = props.validate;
|
|
350
354
|
this.text = props.text;
|
|
355
|
+
this.cascade = props.cascade;
|
|
351
356
|
this.modelRef = props.modelRef;
|
|
352
357
|
this.arrDepth = props.arrDepth;
|
|
353
358
|
this.optArrDepth = props.optArrDepth;
|
|
@@ -426,6 +431,7 @@ export class ConstantField<
|
|
|
426
431
|
of: option.of,
|
|
427
432
|
validate: option.validate,
|
|
428
433
|
text: option.text,
|
|
434
|
+
cascade: option.cascade,
|
|
429
435
|
modelRef,
|
|
430
436
|
arrDepth: arrDepth,
|
|
431
437
|
optArrDepth: optArrDepth,
|
|
@@ -465,6 +471,7 @@ export class ConstantField<
|
|
|
465
471
|
of: this.of,
|
|
466
472
|
validate: this.validate,
|
|
467
473
|
text: this.text,
|
|
474
|
+
cascade: this.cascade,
|
|
468
475
|
modelRef: this.modelRef,
|
|
469
476
|
arrDepth: this.arrDepth,
|
|
470
477
|
optArrDepth: this.optArrDepth,
|
package/constant/index.ts
CHANGED
package/constant/via.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { applyMixins } from "akanjs/common";
|
|
|
13
13
|
import { immerable } from "immer";
|
|
14
14
|
|
|
15
15
|
import { crystalize, getDefault } from ".";
|
|
16
|
+
import { CascadePaths } from "./cascadePaths";
|
|
16
17
|
import { ConstantRegistry } from "./constantRegistry";
|
|
17
18
|
import {
|
|
18
19
|
ConstantField,
|
|
@@ -193,6 +194,7 @@ const getBaseConstantClass = (field: FieldObject, modelType: ConstantType = "sca
|
|
|
193
194
|
static readonly [FIELD_META]: FieldObject = field;
|
|
194
195
|
static modelType: ConstantType = modelType;
|
|
195
196
|
static text: TextFieldPaths = new TextFieldPaths();
|
|
197
|
+
static cascade: CascadePaths = new CascadePaths();
|
|
196
198
|
static children: Set<ConstantModelRef> = new Set();
|
|
197
199
|
static relations: Set<ConstantModelRef> = new Set();
|
|
198
200
|
static enums: Set<EnumInstance> = new Set();
|
|
@@ -269,6 +271,7 @@ export interface ConstantStatics<
|
|
|
269
271
|
relations: Set<ConstantModelRef>;
|
|
270
272
|
enums: Set<EnumInstance>;
|
|
271
273
|
text: TextFieldPaths;
|
|
274
|
+
cascade: CascadePaths;
|
|
272
275
|
_OptionalKey: OptionalKey;
|
|
273
276
|
_RelationKey: RelationKey;
|
|
274
277
|
_PrimitiveKey: PrimitiveKey;
|
|
@@ -296,6 +299,7 @@ export interface DatabaseConstantStatics<Schema = any, FieldObj extends FieldObj
|
|
|
296
299
|
relations: Set<ConstantModelRef>;
|
|
297
300
|
enums: Set<EnumInstance>;
|
|
298
301
|
text: TextFieldPaths;
|
|
302
|
+
cascade: CascadePaths;
|
|
299
303
|
_DatabaseSchema: {
|
|
300
304
|
[K in keyof Schema]: K extends keyof FieldObj
|
|
301
305
|
? FieldObj[K]["fieldType"] extends "hidden"
|
|
@@ -318,6 +322,7 @@ export type ConstantModelRef<
|
|
|
318
322
|
relations: Set<ConstantModelRef>;
|
|
319
323
|
enums: Set<EnumInstance>;
|
|
320
324
|
text: TextFieldPaths;
|
|
325
|
+
cascade: CascadePaths;
|
|
321
326
|
}
|
|
322
327
|
>;
|
|
323
328
|
|
|
@@ -440,6 +445,7 @@ const applyConstantStatics = <Model>(model: ConstantCls<Model>, fieldMap: FieldO
|
|
|
440
445
|
for (const relation of field.modelRef.relations) model.relations.add(relation);
|
|
441
446
|
});
|
|
442
447
|
model.text.collect(fieldMap);
|
|
448
|
+
model.cascade.collect(fieldMap);
|
|
443
449
|
return model as unknown as ConstantCls<Model>;
|
|
444
450
|
};
|
|
445
451
|
|
package/package.json
CHANGED
package/server/di/diLifecycle.ts
CHANGED
|
@@ -457,7 +457,13 @@ export class DiLifecycle {
|
|
|
457
457
|
if (serviceCls.type === "database") {
|
|
458
458
|
const databaseModule = this.#database.get(serviceCls.refName);
|
|
459
459
|
if (!databaseModule) throw new Error(`Database "${serviceCls.refName}" is not registered`);
|
|
460
|
-
ServiceResolver.resolveDatabaseService(
|
|
460
|
+
ServiceResolver.resolveDatabaseService(
|
|
461
|
+
databaseModule.constant,
|
|
462
|
+
databaseModule.database,
|
|
463
|
+
serviceCls,
|
|
464
|
+
|
|
465
|
+
(refName) => this.getService(refName),
|
|
466
|
+
);
|
|
461
467
|
}
|
|
462
468
|
const service = new serviceCls();
|
|
463
469
|
await InjectInfo.resolveInjection(service, serviceCls, this.registry, this.#env);
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { getEnv } from "akanjs/base";
|
|
4
|
+
import { Logger, resolveSubRouteHosts } from "akanjs/common";
|
|
4
5
|
import type { BaseBuildArtifact } from "../types";
|
|
5
6
|
import { AkanResponse } from "./akanResponse";
|
|
6
7
|
import type { WebProxy } from "./types";
|
|
7
8
|
|
|
8
9
|
export class HostBasePathWebProxy implements WebProxy {
|
|
9
10
|
static readonly refName = "HostBasePathWebProxy";
|
|
11
|
+
#logger = new Logger("HostBasePathWebProxy");
|
|
10
12
|
#domainMap: Map<string, string> | null = null;
|
|
11
13
|
|
|
12
14
|
use(request: Bun.BunRequest) {
|
|
@@ -49,7 +51,16 @@ export class HostBasePathWebProxy implements WebProxy {
|
|
|
49
51
|
|
|
50
52
|
#getDomainMap(): Map<string, string> {
|
|
51
53
|
if (this.#domainMap) return this.#domainMap;
|
|
52
|
-
const
|
|
54
|
+
const metadata = loadWebRouteMetadata();
|
|
55
|
+
const { subRoutes, ignoredBasePaths } = resolveSubRouteHosts({
|
|
56
|
+
subRoutes: metadata.subRoutes,
|
|
57
|
+
basePaths: metadata.basePaths,
|
|
58
|
+
env: process.env.AKAN_SUB_ROUTE_HOSTS,
|
|
59
|
+
});
|
|
60
|
+
if (ignoredBasePaths.length)
|
|
61
|
+
this.#logger.warn(
|
|
62
|
+
`AKAN_SUB_ROUTE_HOSTS names basePaths this build does not serve, ignoring: ${ignoredBasePaths.join(", ")}`,
|
|
63
|
+
);
|
|
53
64
|
const map = new Map<string, string>();
|
|
54
65
|
for (const [basePath, domains] of Object.entries(subRoutes)) {
|
|
55
66
|
for (const domain of domains) map.set(normalizeHost(domain), basePath);
|
|
@@ -59,14 +70,14 @@ export class HostBasePathWebProxy implements WebProxy {
|
|
|
59
70
|
}
|
|
60
71
|
}
|
|
61
72
|
|
|
62
|
-
function loadWebRouteMetadata() {
|
|
73
|
+
function loadWebRouteMetadata(): Pick<BaseBuildArtifact, "subRoutes" | "basePaths"> {
|
|
63
74
|
const artifactPath = path.join(resolveArtifactDir(), "base-artifact.json");
|
|
64
|
-
if (!fs.existsSync(artifactPath)) return {
|
|
75
|
+
if (!fs.existsSync(artifactPath)) return { subRoutes: {}, basePaths: [] };
|
|
65
76
|
try {
|
|
66
77
|
const parsed = JSON.parse(fs.readFileSync(artifactPath, "utf8")) as Partial<BaseBuildArtifact>;
|
|
67
|
-
return parsed.subRoutes ?? {};
|
|
78
|
+
return { subRoutes: parsed.subRoutes ?? {}, basePaths: parsed.basePaths ?? [] };
|
|
68
79
|
} catch {
|
|
69
|
-
return {
|
|
80
|
+
return { subRoutes: {}, basePaths: [] };
|
|
70
81
|
}
|
|
71
82
|
}
|
|
72
83
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PromiseOrObject } from "akanjs/base";
|
|
2
2
|
import { capitalize } from "akanjs/common";
|
|
3
|
-
import type
|
|
3
|
+
import { type ConstantModel, ConstantRegistry, type QueryOf } from "akanjs/constant";
|
|
4
4
|
import {
|
|
5
5
|
type CRUDEventType,
|
|
6
6
|
type DatabaseModel,
|
|
@@ -17,7 +17,11 @@ import {
|
|
|
17
17
|
import type { DatabaseService, ServiceCls } from "akanjs/service";
|
|
18
18
|
|
|
19
19
|
export class ServiceResolver {
|
|
20
|
-
static #getDefaultDbServiceMethods(
|
|
20
|
+
static #getDefaultDbServiceMethods(
|
|
21
|
+
className: string,
|
|
22
|
+
cascades: [string, string][],
|
|
23
|
+
getService: (refName: string) => DatabaseService,
|
|
24
|
+
) {
|
|
21
25
|
const dbServiceMethods = {
|
|
22
26
|
async __get(this: DatabaseService, id: string) {
|
|
23
27
|
return await this.__databaseModel.__get(id);
|
|
@@ -95,9 +99,18 @@ export class ServiceResolver {
|
|
|
95
99
|
return this.__update(id, data);
|
|
96
100
|
},
|
|
97
101
|
async __remove(this: DatabaseService, id: string): Promise<Doc> {
|
|
102
|
+
|
|
103
|
+
const targets = cascades.map(([key, refName]) => [key, getService(refName)] as const);
|
|
98
104
|
await this.__libsPreRemove(id);
|
|
99
105
|
const doc = await this.__databaseModel.__remove(id);
|
|
100
|
-
|
|
106
|
+
const removed = await this.__libsPostRemove(doc);
|
|
107
|
+
for (const [key, target] of targets) {
|
|
108
|
+
const value = (removed as Record<string, unknown>)[key];
|
|
109
|
+
const ids = (Array.isArray(value) ? value : [value]).filter((v): v is string => typeof v === "string");
|
|
110
|
+
|
|
111
|
+
for (const targetId of ids) await target.__remove(targetId);
|
|
112
|
+
}
|
|
113
|
+
return removed;
|
|
101
114
|
},
|
|
102
115
|
async [`remove${className}`](this: DatabaseService, id: string): Promise<Doc> {
|
|
103
116
|
return this.__remove(id);
|
|
@@ -105,9 +118,18 @@ export class ServiceResolver {
|
|
|
105
118
|
};
|
|
106
119
|
return dbServiceMethods;
|
|
107
120
|
}
|
|
108
|
-
static resolveDatabaseService(
|
|
121
|
+
static resolveDatabaseService(
|
|
122
|
+
constant: ConstantModel,
|
|
123
|
+
database: DatabaseModel,
|
|
124
|
+
srvRef: ServiceCls,
|
|
125
|
+
getService: (refName: string) => DatabaseService,
|
|
126
|
+
): ServiceCls {
|
|
109
127
|
const className = capitalize(database.refName);
|
|
110
|
-
|
|
128
|
+
|
|
129
|
+
const cascades = [...constant.full.cascade.remove].map(
|
|
130
|
+
([key, modelRef]) => [key, ConstantRegistry.getRefName(modelRef)] as [string, string],
|
|
131
|
+
);
|
|
132
|
+
Object.assign(srvRef.prototype, ServiceResolver.#getDefaultDbServiceMethods(className, cascades, getService));
|
|
111
133
|
const getQueryDataFromKey = (queryKey: string, args: any): { query: any; queryOption: any } => {
|
|
112
134
|
const lastArg = args.at(-1);
|
|
113
135
|
const hasQueryOption =
|
package/server/webRouter.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
getBasePathFromPathname,
|
|
8
8
|
Logger,
|
|
9
9
|
parseAkanI18nEnv,
|
|
10
|
+
resolveSubRouteHosts,
|
|
10
11
|
} from "akanjs/common";
|
|
11
12
|
import { type AkanRequestStore, createRequestStore, parseCookieHeader } from "akanjs/fetch";
|
|
12
13
|
import type { AkanMetricsReport } from "akanjs/service";
|
|
@@ -267,6 +268,7 @@ export class WebRouter {
|
|
|
267
268
|
#logger = new Logger("WebRouter");
|
|
268
269
|
#artifactDir = WebRouter.#resolveArtifactDir();
|
|
269
270
|
#artifact: BaseBuildArtifact;
|
|
271
|
+
#subRoutes: Record<string, string[]>;
|
|
270
272
|
#rsc: RscWorker;
|
|
271
273
|
#hub: HmrWsHub | null = null;
|
|
272
274
|
#prodMode = process.env.NODE_ENV === "production";
|
|
@@ -293,6 +295,16 @@ export class WebRouter {
|
|
|
293
295
|
constructor({ artifact, cssBytesByUrl, rsc, seedIndex, upgradeHmrWs }: WebRouterOptions) {
|
|
294
296
|
this.#logger.verbose(`[SSR] loaded ${Object.keys(cssBytesByUrl).length} CSS assets`);
|
|
295
297
|
this.#artifact = artifact;
|
|
298
|
+
const { subRoutes, ignoredBasePaths } = resolveSubRouteHosts({
|
|
299
|
+
subRoutes: artifact.subRoutes,
|
|
300
|
+
basePaths: artifact.basePaths,
|
|
301
|
+
env: process.env.AKAN_SUB_ROUTE_HOSTS,
|
|
302
|
+
});
|
|
303
|
+
this.#subRoutes = subRoutes;
|
|
304
|
+
if (ignoredBasePaths.length)
|
|
305
|
+
this.#logger.warn(
|
|
306
|
+
`AKAN_SUB_ROUTE_HOSTS names basePaths this build does not serve, ignoring: ${ignoredBasePaths.join(", ")}`,
|
|
307
|
+
);
|
|
296
308
|
this.#rsc = rsc;
|
|
297
309
|
this.renderState = {
|
|
298
310
|
buildId: 0,
|
|
@@ -424,8 +436,7 @@ export class WebRouter {
|
|
|
424
436
|
const clientOrigin = WebRouter.#clientFacingOrigin(req);
|
|
425
437
|
const target = reqUrl.searchParams.get("url");
|
|
426
438
|
const rawTargetUrl = target ? new URL(target, clientOrigin) : reqUrl;
|
|
427
|
-
const requestBasePath =
|
|
428
|
-
req.headers.get("x-base-path") ?? WebRouter.#basePathForRequestHost(req, this.#artifact.subRoutes);
|
|
439
|
+
const requestBasePath = this.#requestBasePath(req);
|
|
429
440
|
const normalizedTarget = normalizeRscTargetUrlForHostBasePath(rawTargetUrl, {
|
|
430
441
|
basePath: requestBasePath,
|
|
431
442
|
basePaths: this.#artifact.basePaths,
|
|
@@ -517,11 +528,7 @@ export class WebRouter {
|
|
|
517
528
|
});
|
|
518
529
|
}
|
|
519
530
|
|
|
520
|
-
const sitemapBasePath = getSitemapBasePath(
|
|
521
|
-
url.pathname,
|
|
522
|
-
this.#artifact.basePaths,
|
|
523
|
-
req.headers.get("x-base-path") ?? WebRouter.#basePathForRequestHost(req, this.#artifact.subRoutes),
|
|
524
|
-
);
|
|
531
|
+
const sitemapBasePath = getSitemapBasePath(url.pathname, this.#artifact.basePaths, this.#requestBasePath(req));
|
|
525
532
|
if (sitemapBasePath !== undefined) {
|
|
526
533
|
return new Response(
|
|
527
534
|
createDefaultSitemapXml({
|
|
@@ -713,6 +720,17 @@ export class WebRouter {
|
|
|
713
720
|
return getClientFacingOrigin(req);
|
|
714
721
|
}
|
|
715
722
|
|
|
723
|
+
/**
|
|
724
|
+
* `x-base-path` is set by `HostBasePathWebProxy`, but it reaches here from the wire too, so it is checked against
|
|
725
|
+
* the basePaths this build serves — the same check `getBasePathFromPathname` already applies to it. An unknown
|
|
726
|
+
* value falls through to host matching instead of routing the request into a basePath that resolves to nothing.
|
|
727
|
+
*/
|
|
728
|
+
#requestBasePath(req: Request): string | null {
|
|
729
|
+
const headerBasePath = req.headers.get("x-base-path");
|
|
730
|
+
if (headerBasePath && this.#artifact.basePaths.includes(headerBasePath)) return headerBasePath;
|
|
731
|
+
return WebRouter.#basePathForRequestHost(req, this.#subRoutes);
|
|
732
|
+
}
|
|
733
|
+
|
|
716
734
|
static #basePathForRequestHost(req: Request, subRoutes: Record<string, string[]>): string | null {
|
|
717
735
|
const host = (req.headers.get("x-forwarded-host") ?? req.headers.get("host") ?? "")
|
|
718
736
|
.toLowerCase()
|
|
@@ -857,8 +875,7 @@ export class WebRouter {
|
|
|
857
875
|
pathname,
|
|
858
876
|
i18n: this.#artifact.i18n,
|
|
859
877
|
basePaths: this.#artifact.basePaths,
|
|
860
|
-
headerBasePath:
|
|
861
|
-
req.headers.get("x-base-path") ?? WebRouter.#basePathForRequestHost(req, this.#artifact.subRoutes),
|
|
878
|
+
headerBasePath: this.#requestBasePath(req),
|
|
862
879
|
});
|
|
863
880
|
}
|
|
864
881
|
|
|
@@ -866,8 +883,7 @@ export class WebRouter {
|
|
|
866
883
|
const basePath = getBasePathFromPathname(pathname, {
|
|
867
884
|
basePaths: Object.keys(this.renderState.cssAssets),
|
|
868
885
|
i18n: this.#artifact.i18n,
|
|
869
|
-
headerBasePath:
|
|
870
|
-
req.headers.get("x-base-path") ?? WebRouter.#basePathForRequestHost(req, this.#artifact.subRoutes),
|
|
886
|
+
headerBasePath: this.#requestBasePath(req),
|
|
871
887
|
});
|
|
872
888
|
return this.renderState.cssAssets[basePath ?? ""]?.cssUrl ?? null;
|
|
873
889
|
}
|
package/types/common/index.d.ts
CHANGED
|
@@ -25,6 +25,6 @@ export { randomPicks } from "./randomPicks.d.ts";
|
|
|
25
25
|
export { assertUniqueRoutePatterns, compareRouteSpecificity, isRouteSourceFile, isSpecialRouteLeaf, matchRoutePattern, normalizeRoutePattern, type ParsedRouteModuleKey, parseRouteModuleKey, type RouteModuleKind, routeSegmentToPatternPart, routeSegmentToTreePath, tryParseRouteModuleKey, type ValidatePageSourceFileOptions, type ValidateSubRoutePageKeyOptions, validatePageSourceFile, validateSubRoutePageKey, } from "./routeConvention.d.ts";
|
|
26
26
|
export { sleep } from "./sleep.d.ts";
|
|
27
27
|
export { splitVersion } from "./splitVersion.d.ts";
|
|
28
|
-
export { getBasePathFromPathname, parseBasePaths } from "./subRoute.d.ts";
|
|
28
|
+
export { getBasePathFromPathname, parseBasePaths, parseSubRouteHosts, resolveSubRouteHosts } from "./subRoute.d.ts";
|
|
29
29
|
export type * from "./types.d.ts";
|
|
30
30
|
export { type WebsocketAuthAckData, type WebsocketAuthRequest, websocketAuthContract, } from "./websocketAuth.d.ts";
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import type { AkanI18nConfig } from "./localeConfig.d.ts";
|
|
2
2
|
export declare const parseBasePaths: (value: string | string[] | Set<string> | undefined | null) => string[];
|
|
3
|
+
/**
|
|
4
|
+
* `"soft=a.com,b.com;office=c.com"` -> `{ soft: ["a.com", "b.com"], office: ["c.com"] }`. A deployment platform
|
|
5
|
+
* renders this value, so a malformed entry is skipped rather than thrown: one bad character must not CrashLoop
|
|
6
|
+
* every pod that received it.
|
|
7
|
+
*/
|
|
8
|
+
export declare const parseSubRouteHosts: (value: string | undefined | null) => Record<string, string[]>;
|
|
9
|
+
/**
|
|
10
|
+
* Unions the env mapping onto the one baked into the build artifact, never replacing it — dropping the env is the
|
|
11
|
+
* rollback path. A basePath the build does not serve is reported back instead of honoured: the route tree is a
|
|
12
|
+
* build output, so accepting one would answer every request under it with a 404 and nothing to explain why.
|
|
13
|
+
*/
|
|
14
|
+
export declare const resolveSubRouteHosts: ({ subRoutes, basePaths, env, }: {
|
|
15
|
+
subRoutes: Record<string, string[]>;
|
|
16
|
+
basePaths: Iterable<string>;
|
|
17
|
+
env?: string | null;
|
|
18
|
+
}) => {
|
|
19
|
+
subRoutes: Record<string, string[]>;
|
|
20
|
+
ignoredBasePaths: string[];
|
|
21
|
+
};
|
|
3
22
|
export declare const getBasePathFromPathname: (pathname: string, { basePaths, i18n, headerBasePath, }: {
|
|
4
23
|
basePaths: Iterable<string>;
|
|
5
24
|
i18n?: Pick<AkanI18nConfig, "locales" | "defaultLocale">;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FieldObject } from "./fieldInfo.d.ts";
|
|
2
|
+
import type { ConstantModelRef } from "./via.d.ts";
|
|
3
|
+
/** What happens to the documents a relation field points at when the owner is removed. */
|
|
4
|
+
export declare const cascadeActions: readonly ["remove"];
|
|
5
|
+
export type CascadeAction = (typeof cascadeActions)[number];
|
|
6
|
+
export declare class CascadePaths {
|
|
7
|
+
#private;
|
|
8
|
+
/** Field key → the model its ids point at. Resolved to a refName later: the target may not be registered yet. */
|
|
9
|
+
readonly remove: Map<string, ConstantModelRef>;
|
|
10
|
+
collect(fieldMap: FieldObject): this;
|
|
11
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Any, CLIENT_VALUE, type Cls, type Dayjs, type EnumInstance, type Float, Int, type PrimitiveScalar, SERVER_VALUE, type SingleValue, type UnCls } from "akanjs/base";
|
|
2
|
+
import type { CascadeAction } from "./cascadePaths.d.ts";
|
|
2
3
|
import type { TextFieldRole } from "./textFieldPaths.d.ts";
|
|
3
4
|
import type { BaseObject } from "./types.d.ts";
|
|
4
5
|
import type { ConstantModelRef } from "./via.d.ts";
|
|
@@ -45,6 +46,7 @@ export interface ConstantFieldProps<FieldType extends ConstantFieldKind = Consta
|
|
|
45
46
|
of?: MapValue;
|
|
46
47
|
validate?: (value: FieldValue, model: any) => boolean;
|
|
47
48
|
text?: TextFieldRole;
|
|
49
|
+
cascade?: CascadeAction;
|
|
48
50
|
meta?: Metadata;
|
|
49
51
|
}
|
|
50
52
|
export declare const fieldPresets: readonly ["email", "password", "url"];
|
|
@@ -91,6 +93,7 @@ interface ConstantFieldBuildProps<FieldType extends ConstantFieldKind = any, Fie
|
|
|
91
93
|
of?: MapValue;
|
|
92
94
|
validate?: (value: FieldValue, model: any) => boolean;
|
|
93
95
|
text?: TextFieldRole;
|
|
96
|
+
cascade?: CascadeAction;
|
|
94
97
|
modelRef: ConstantModelRef;
|
|
95
98
|
arrDepth: number;
|
|
96
99
|
optArrDepth: number;
|
|
@@ -134,6 +137,7 @@ export declare class ConstantField<FieldType extends ConstantFieldKind = Constan
|
|
|
134
137
|
readonly of?: MapValue;
|
|
135
138
|
readonly validate?: (value: FieldValue, model: any) => boolean;
|
|
136
139
|
readonly text?: TextFieldRole;
|
|
140
|
+
readonly cascade?: CascadeAction;
|
|
137
141
|
readonly modelRef: ConstantModelRef;
|
|
138
142
|
readonly arrDepth: number;
|
|
139
143
|
readonly optArrDepth: number;
|
package/types/constant/via.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CLIENT_VALUE, type Cls, DEFAULT_VALUE, type EnumInstance, FIELD_META, type ObjectAssign, type ObjectAssignKeyOfObjects, PURIFIED_VALUE, SERVER_VALUE } from "akanjs/base";
|
|
2
|
+
import { CascadePaths } from "./cascadePaths.d.ts";
|
|
2
3
|
import { type ExtractFieldInfoObject, type FieldBuilder, type FieldInfoObject, type FieldInfoObjectToFieldObject, type FieldObject, type FieldResolver } from "./fieldInfo.d.ts";
|
|
3
4
|
import { type PurifiedModel, type PurifyFunc } from "./purify.d.ts";
|
|
4
5
|
import { TextFieldPaths } from "./textFieldPaths.d.ts";
|
|
@@ -34,6 +35,7 @@ export interface ConstantStatics<Schema = any, OwnSchema = Schema, OwnFieldObj e
|
|
|
34
35
|
relations: Set<ConstantModelRef>;
|
|
35
36
|
enums: Set<EnumInstance>;
|
|
36
37
|
text: TextFieldPaths;
|
|
38
|
+
cascade: CascadePaths;
|
|
37
39
|
_OptionalKey: OptionalKey;
|
|
38
40
|
_RelationKey: RelationKey;
|
|
39
41
|
_PrimitiveKey: PrimitiveKey;
|
|
@@ -60,6 +62,7 @@ export interface DatabaseConstantStatics<Schema = any, FieldObj extends FieldObj
|
|
|
60
62
|
relations: Set<ConstantModelRef>;
|
|
61
63
|
enums: Set<EnumInstance>;
|
|
62
64
|
text: TextFieldPaths;
|
|
65
|
+
cascade: CascadePaths;
|
|
63
66
|
_DatabaseSchema: {
|
|
64
67
|
[K in keyof Schema]: K extends keyof FieldObj ? FieldObj[K]["fieldType"] extends "hidden" ? NonNullable<Schema[K]> : Schema[K] : Schema[K];
|
|
65
68
|
};
|
|
@@ -71,6 +74,7 @@ export type ConstantModelRef<Schema = any, FieldObj extends FieldObject = FieldO
|
|
|
71
74
|
relations: Set<ConstantModelRef>;
|
|
72
75
|
enums: Set<EnumInstance>;
|
|
73
76
|
text: TextFieldPaths;
|
|
77
|
+
cascade: CascadePaths;
|
|
74
78
|
}>;
|
|
75
79
|
export type DocumentConstantModelRef<Schema = any, FieldObj extends FieldObject = FieldObject, ModelType extends ConstantType = ConstantType, DatabaseSchema = DatabaseSchemaOf<Schema, never>> = Cls<Schema, {
|
|
76
80
|
[FIELD_META]: FieldObj;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ConstantModel } from "akanjs/constant";
|
|
2
2
|
import { type DatabaseModel } from "akanjs/document";
|
|
3
|
-
import type { ServiceCls } from "akanjs/service";
|
|
3
|
+
import type { DatabaseService, ServiceCls } from "akanjs/service";
|
|
4
4
|
export declare class ServiceResolver {
|
|
5
5
|
#private;
|
|
6
|
-
static resolveDatabaseService(constant: ConstantModel, database: DatabaseModel, srvRef: ServiceCls): ServiceCls;
|
|
6
|
+
static resolveDatabaseService(constant: ConstantModel, database: DatabaseModel, srvRef: ServiceCls, getService: (refName: string) => DatabaseService): ServiceCls;
|
|
7
7
|
}
|