@lukoweb/apitogo 0.2.20 → 0.2.22
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/cli/cli.js +1508 -227
- package/dist/declarations/cli/platform/client/platform-api.d.ts +22 -0
- package/dist/declarations/cli/platform/tools/lifecycle.d.ts +3 -0
- package/docs/configuration/gateway.md +221 -0
- package/package.json +1 -1
- package/src/config/v2/index.ts +2 -0
- package/src/config/v2/manifest.ts +89 -0
- package/src/config/v2/services.ts +4 -2
- package/src/vite/config.ts +2 -2
- package/src/vite/watch-ignore.ts +34 -9
|
@@ -413,6 +413,24 @@ export interface GatewayDryRunDto {
|
|
|
413
413
|
activeKeys: number;
|
|
414
414
|
comparedWithGateway: boolean;
|
|
415
415
|
}
|
|
416
|
+
export interface ProjectIdentityDto {
|
|
417
|
+
status: string;
|
|
418
|
+
issuer: string;
|
|
419
|
+
clientId: string;
|
|
420
|
+
audience: string;
|
|
421
|
+
portalRedirectUris: string[];
|
|
422
|
+
errorCode?: string | null;
|
|
423
|
+
errorMessage?: string | null;
|
|
424
|
+
nativeSignup: boolean;
|
|
425
|
+
}
|
|
426
|
+
export interface ProjectIdentityRequestBody {
|
|
427
|
+
audience?: string;
|
|
428
|
+
portalRedirectUris: string[];
|
|
429
|
+
nativeSignup?: boolean;
|
|
430
|
+
}
|
|
431
|
+
export interface DevPortalIdentityLinkDto {
|
|
432
|
+
jobId: string;
|
|
433
|
+
}
|
|
416
434
|
export interface GatewayTeardownAcceptedDto {
|
|
417
435
|
reconciliationId: string;
|
|
418
436
|
state: string;
|
|
@@ -521,6 +539,10 @@ export declare class PlatformApiClient {
|
|
|
521
539
|
spec?: string;
|
|
522
540
|
}): Promise<GatewayDryRunDto>;
|
|
523
541
|
deleteGateway(projectId: string): Promise<GatewayTeardownAcceptedDto>;
|
|
542
|
+
putProjectIdentity(projectId: string, body: ProjectIdentityRequestBody): Promise<ProjectIdentityDto>;
|
|
543
|
+
getProjectIdentity(projectId: string): Promise<ProjectIdentityDto | null>;
|
|
544
|
+
deleteProjectIdentity(projectId: string): Promise<ProjectIdentityDto | null>;
|
|
545
|
+
linkDevPortalIdentity(projectId: string): Promise<DevPortalIdentityLinkDto>;
|
|
524
546
|
getDevPortalBackend(projectId: string): Promise<DevPortalBackendDto | null>;
|
|
525
547
|
getDevPortalSetupStatus(projectId: string): Promise<DevPortalSetupStatusDto>;
|
|
526
548
|
getDevPortalSettings(projectId: string): Promise<DevPortalSettingsDto | null>;
|
|
@@ -10,6 +10,7 @@ export declare function resolveBootstrapProjectId(client: PlatformApiClient, arg
|
|
|
10
10
|
name: string;
|
|
11
11
|
description?: string;
|
|
12
12
|
modules: string[];
|
|
13
|
+
projectId?: string;
|
|
13
14
|
}, steps: BootstrapStep[]): Promise<{
|
|
14
15
|
projectId: string;
|
|
15
16
|
resumed: boolean;
|
|
@@ -30,6 +31,8 @@ export declare function handleBootstrapProject(client: PlatformApiClient, config
|
|
|
30
31
|
oidcAudiences?: string[];
|
|
31
32
|
limitsJson?: string;
|
|
32
33
|
publicHealthActionKey?: string;
|
|
34
|
+
projectId?: string;
|
|
35
|
+
onProjectResolved?: (projectId: string) => Promise<void> | void;
|
|
33
36
|
}, progress?: ProgressReporter): Promise<string>;
|
|
34
37
|
export declare function handleTeardownProject(client: PlatformApiClient, config: ApitogoConfig, args: {
|
|
35
38
|
projectId?: string;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Protect your API with the gateway
|
|
3
|
+
description:
|
|
4
|
+
Put the APItoGo gateway in front of an API you already host. What each setting means, where to
|
|
5
|
+
find the login values (OIDC), and how to write the rules yourself.
|
|
6
|
+
sidebar_icon: shield
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
The **gateway** sits in front of an API you already host. It checks who is calling, limits how
|
|
10
|
+
often, and passes only the calls you allow to your API. You do not move your API.
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
callers ──► APItoGo gateway ──► your API (stays where it is)
|
|
14
|
+
│ checks the login token
|
|
15
|
+
│ limits calls
|
|
16
|
+
│ allows only the calls in your OpenAPI file
|
|
17
|
+
└ your real API address stays hidden
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
You set it up in one file, `apitogo.config.json`, in a `gateway` section.
|
|
21
|
+
|
|
22
|
+
## The smallest setup
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"version": 2,
|
|
27
|
+
"gateway": {
|
|
28
|
+
"origin": { "mode": "proxy", "url": "https://api.example.com", "healthPath": "/health" },
|
|
29
|
+
"specs": ["apis/openapi.yaml"],
|
|
30
|
+
"auth": {
|
|
31
|
+
"mode": "oidc",
|
|
32
|
+
"metadataUrl": "https://login.example.com/.well-known/openid-configuration",
|
|
33
|
+
"issuers": ["https://login.example.com/"],
|
|
34
|
+
"audiences": ["my-api"]
|
|
35
|
+
},
|
|
36
|
+
"policies": {
|
|
37
|
+
"access": { "jwt": true, "apiKey": false },
|
|
38
|
+
"rateLimit": { "calls": 60, "periodSeconds": 60 },
|
|
39
|
+
"operations": { "public": ["GET /health"] }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`apitogo add gateway` writes the first part for you. The rest you write yourself, and the sections
|
|
46
|
+
below explain every value.
|
|
47
|
+
|
|
48
|
+
## 1. Your API (`origin` and `specs`)
|
|
49
|
+
|
|
50
|
+
| Setting | What to put there | Where to get it |
|
|
51
|
+
| ------------------- | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
|
|
52
|
+
| `origin.mode` | `proxy`: your API stays where it is. | Always `proxy` for an API you host. |
|
|
53
|
+
| `origin.url` | The address of your API. It must be `https://`. | You already know it. It is where your API answers today. |
|
|
54
|
+
| `origin.healthPath` | A path the platform can call to see that your API is alive, for example `/health`. | A cheap call on your API that needs no login. |
|
|
55
|
+
| `specs` | The OpenAPI file or files that describe your API. | Your API's `openapi.yaml`, or the file your framework generates. |
|
|
56
|
+
|
|
57
|
+
The gateway only lets through the calls listed in your OpenAPI file. A call that is not in the file
|
|
58
|
+
is refused, so a forgotten path never leaks.
|
|
59
|
+
|
|
60
|
+
## 2. Who may call (`auth`)
|
|
61
|
+
|
|
62
|
+
With `"mode": "oidc"` the caller sends a **login token** (a JWT) with each call:
|
|
63
|
+
`Authorization: Bearer <token>`. The token comes from your own login provider, for example Auth0,
|
|
64
|
+
Microsoft Entra ID, Okta or Keycloak. The gateway checks that the token is genuine, is from your
|
|
65
|
+
provider and is meant for your API.
|
|
66
|
+
|
|
67
|
+
You must give **all three** of these values. If one is missing, the deploy is blocked, so a settings
|
|
68
|
+
mistake can never leave your API open.
|
|
69
|
+
|
|
70
|
+
| Setting | What it is | How to find it |
|
|
71
|
+
| ------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
|
|
72
|
+
| `metadataUrl` | The address where the gateway reads your provider's public keys, so it can check a token's signature. | Your issuer address plus `/.well-known/openid-configuration`. |
|
|
73
|
+
| `issuers` | Who is allowed to issue tokens. It must match the `iss` value in your tokens **exactly**. | The `issuer` field inside the metadata document (see below). |
|
|
74
|
+
| `audiences` | Who the token is meant for. It must match the `aud` value in your tokens. | The identifier you gave your API at your provider. |
|
|
75
|
+
|
|
76
|
+
### Find the values in four steps
|
|
77
|
+
|
|
78
|
+
1. **Find your provider's issuer address.** It is on the provider's dashboard or in its docs.
|
|
79
|
+
Typical shapes are in the table below.
|
|
80
|
+
2. **Open the metadata address in a browser.** Add `/.well-known/openid-configuration` to the issuer
|
|
81
|
+
address. You see a JSON document. If you see an error, the address is wrong.
|
|
82
|
+
3. **Copy the `issuer` value from that document** into `issuers`, character for character, including
|
|
83
|
+
a trailing `/` if it has one.
|
|
84
|
+
4. **Use the identifier of your API as `audiences`.** You create this identifier at your provider
|
|
85
|
+
when you register your API. It is often called _API identifier_, _audience_ or _application ID
|
|
86
|
+
URI_.
|
|
87
|
+
|
|
88
|
+
### Typical values by provider
|
|
89
|
+
|
|
90
|
+
These are typical shapes. Always confirm them in your provider's dashboard.
|
|
91
|
+
|
|
92
|
+
| Provider | Issuer | Audience |
|
|
93
|
+
| ------------------ | ----------------------------------------------------------------------------- | -------------------------------------------------------------- |
|
|
94
|
+
| Auth0 | `https://YOUR-TENANT.auth0.com/` (with the trailing `/`) | The _API identifier_ of your API in Auth0 |
|
|
95
|
+
| Microsoft Entra ID | `https://login.microsoftonline.com/TENANT-ID/v2.0` | Your API's _Application ID URI_, for example `api://CLIENT-ID` |
|
|
96
|
+
| Okta | `https://YOUR-ORG.okta.com/oauth2/default` (or your own authorization server) | The audience set on that authorization server |
|
|
97
|
+
| Keycloak | `https://HOST/realms/YOUR-REALM` | The client or audience you configured for your API |
|
|
98
|
+
|
|
99
|
+
In every case the metadata address is the issuer plus `/.well-known/openid-configuration`.
|
|
100
|
+
|
|
101
|
+
### Check a real token
|
|
102
|
+
|
|
103
|
+
Ask your provider for a token for your API, then read what is inside it. The values `iss` and `aud`
|
|
104
|
+
must match your `issuers` and `audiences`:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
node -p "JSON.parse(Buffer.from(process.argv[1].split('.')[1],'base64url').toString())" "PASTE-THE-TOKEN-HERE"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
A token is a login credential. Read it on your own computer, as above, and never share it.
|
|
111
|
+
|
|
112
|
+
### The most common mistakes
|
|
113
|
+
|
|
114
|
+
| What you see | Usual cause |
|
|
115
|
+
| ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
|
|
116
|
+
| Every call answers `401` although the token looks right | `issuers` or `audiences` does not match `iss` or `aud` in the token. A trailing `/` is enough to break it. |
|
|
117
|
+
| `apitogo check` or `deploy` says the auth settings are incomplete | One of `metadataUrl`, `issuers` or `audiences` is empty. All three are needed. |
|
|
118
|
+
| The metadata address shows an error in the browser | The issuer address is wrong, or your provider uses a different path. Check its docs. |
|
|
119
|
+
|
|
120
|
+
> **Not available yet:** `"mode": "identity"` (APItoGo's own sign-up and login for your end users)
|
|
121
|
+
> is a planned service. For now, use `oidc` with your own provider.
|
|
122
|
+
|
|
123
|
+
## 3. The rules (`policies`)
|
|
124
|
+
|
|
125
|
+
Rules go in `gateway.policies`. Write them by hand: they are a few lines each.
|
|
126
|
+
|
|
127
|
+
### `access`: who gets in
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
"access": { "jwt": true, "apiKey": false }
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
| Setting | Effect |
|
|
134
|
+
| -------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| `jwt` | `true`: a valid login token is required. |
|
|
136
|
+
| `apiKey` | `true`: a key in the `Ocp-Apim-Subscription-Key` header is required. Keys need the plans service, which is not available yet. |
|
|
137
|
+
|
|
138
|
+
Set `access` yourself and do not rely on the default.
|
|
139
|
+
|
|
140
|
+
### `operations`: open a call to everyone, or close it
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
"operations": { "public": ["GET /health"], "deny": ["DELETE /orders"] }
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Each entry is `METHOD /path`, exactly as in your OpenAPI file.
|
|
147
|
+
|
|
148
|
+
| List | Effect |
|
|
149
|
+
| -------- | ------------------------------------------------------------------------------------ |
|
|
150
|
+
| `public` | Anyone can call it, with no token. Use it for a health check or a public price list. |
|
|
151
|
+
| `deny` | The gateway always refuses it, even with a valid token. |
|
|
152
|
+
|
|
153
|
+
Every other call needs a login.
|
|
154
|
+
|
|
155
|
+
### `rateLimit` and `quota`: how often
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
"rateLimit": { "calls": 60, "periodSeconds": 60 },
|
|
159
|
+
"quota": { "calls": 10000, "periodDays": 30 }
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
| Setting | Effect |
|
|
163
|
+
| ----------- | ---------------------------------------------------------------------------------------------------------------------- |
|
|
164
|
+
| `rateLimit` | At most `calls` calls in every `periodSeconds` seconds. Extra calls get `429`. Use it to protect your API from bursts. |
|
|
165
|
+
| `quota` | At most `calls` calls in every `periodDays` days. Use it for a monthly allowance. |
|
|
166
|
+
|
|
167
|
+
### `cors`: which websites may call from a browser
|
|
168
|
+
|
|
169
|
+
```json
|
|
170
|
+
"cors": ["https://app.example.com"]
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
A browser only lets a web page call your API if the API allows that page's site. List each site as
|
|
174
|
+
`https://host` only: no path and no trailing `/`. An entry like `https://app.example.com/` is
|
|
175
|
+
refused by `apitogo check`.
|
|
176
|
+
|
|
177
|
+
### Keep your API from being called directly
|
|
178
|
+
|
|
179
|
+
Your API has a public address, so someone could skip the gateway. Make your API accept calls only
|
|
180
|
+
with a secret that only the gateway sends:
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
"origin": {
|
|
184
|
+
"mode": "proxy",
|
|
185
|
+
"url": "https://api.example.com",
|
|
186
|
+
"auth": { "type": "header", "name": "X-Gateway-Secret", "value": "secret:GATEWAY_SECRET" }
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
npx apitogo secrets set GATEWAY_SECRET
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
The value is a reference, never the secret itself. The secret is stored encrypted and never shown
|
|
195
|
+
again. Then make your API refuse any call without that header.
|
|
196
|
+
|
|
197
|
+
## 4. Try it and check it
|
|
198
|
+
|
|
199
|
+
Run these before you deploy:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
npx apitogo check
|
|
203
|
+
npx apitogo plan
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`check` finds mistakes in the file, offline. `plan` shows what a deploy would change and changes
|
|
207
|
+
nothing. Then deploy and test the address that `apitogo status` shows:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
npx apitogo deploy --only gateway
|
|
211
|
+
curl -i https://YOUR-GATEWAY-ADDRESS/health
|
|
212
|
+
curl -i https://YOUR-GATEWAY-ADDRESS/orders
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
| Answer | Meaning |
|
|
216
|
+
| ------------------------------- | ------------------------------------------------------------- |
|
|
217
|
+
| `200` on a public call | The call is open and your API answered. |
|
|
218
|
+
| `401` on a call without a token | The gateway is protecting that call. |
|
|
219
|
+
| `404` | The path is not in your OpenAPI file. |
|
|
220
|
+
| `429` | The rate limit was reached. |
|
|
221
|
+
| `502` or `504` | Your API did not answer. Check `origin.url` and `healthPath`. |
|
package/package.json
CHANGED
package/src/config/v2/index.ts
CHANGED
|
@@ -319,6 +319,95 @@ export function withGatewayEntry(
|
|
|
319
319
|
};
|
|
320
320
|
}
|
|
321
321
|
|
|
322
|
+
/**
|
|
323
|
+
* Adds the identity entry an identity deploy learned: the issuer address, the portal's sign-in client and the audience
|
|
324
|
+
* of its tokens. Public values only. Entries of other services are kept as they are.
|
|
325
|
+
*/
|
|
326
|
+
export function withIdentityEntry(
|
|
327
|
+
existing: ServiceManifest | undefined,
|
|
328
|
+
info: {
|
|
329
|
+
projectId: string;
|
|
330
|
+
projectName: string;
|
|
331
|
+
/** `apitogo` for the platform's own identity, `oidc` for the customer's provider (nothing is made for it). */
|
|
332
|
+
provider?: "apitogo" | "oidc";
|
|
333
|
+
issuer: string;
|
|
334
|
+
clientId: string;
|
|
335
|
+
audience?: string;
|
|
336
|
+
nativeSignup: boolean;
|
|
337
|
+
now?: Date;
|
|
338
|
+
},
|
|
339
|
+
): ServiceManifest {
|
|
340
|
+
const base: ServiceManifest =
|
|
341
|
+
existing ??
|
|
342
|
+
parseManifest({
|
|
343
|
+
schemaVersion: SUPPORTED_MANIFEST_SCHEMA,
|
|
344
|
+
project: { name: info.projectName },
|
|
345
|
+
});
|
|
346
|
+
return {
|
|
347
|
+
...base,
|
|
348
|
+
schemaVersion: SUPPORTED_MANIFEST_SCHEMA,
|
|
349
|
+
generatedAt: (info.now ?? new Date()).toISOString(),
|
|
350
|
+
preview: undefined,
|
|
351
|
+
project: { ...base.project, id: info.projectId, name: info.projectName },
|
|
352
|
+
services: {
|
|
353
|
+
...base.services,
|
|
354
|
+
identity: {
|
|
355
|
+
...base.services.identity,
|
|
356
|
+
provider: info.provider ?? "apitogo",
|
|
357
|
+
status: "ready",
|
|
358
|
+
issuer: info.issuer.replace(/\/+$/, ""),
|
|
359
|
+
clientId: info.clientId,
|
|
360
|
+
scopes: ["openid", "profile", "email"],
|
|
361
|
+
...(info.audience ? { audience: info.audience } : {}),
|
|
362
|
+
capabilities: { nativeSignup: info.nativeSignup },
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Adds the plans entry a plans deploy learned: where the portal reaches the plans backend and what it can do. Public
|
|
370
|
+
* values only. Entries of other services are kept as they are.
|
|
371
|
+
*/
|
|
372
|
+
export function withPlansEntry(
|
|
373
|
+
existing: ServiceManifest | undefined,
|
|
374
|
+
info: {
|
|
375
|
+
projectId: string;
|
|
376
|
+
projectName: string;
|
|
377
|
+
apiBaseUrl: string;
|
|
378
|
+
billing: string;
|
|
379
|
+
catalogRevision: number;
|
|
380
|
+
capabilities: Record<string, boolean>;
|
|
381
|
+
now?: Date;
|
|
382
|
+
},
|
|
383
|
+
): ServiceManifest {
|
|
384
|
+
const base: ServiceManifest =
|
|
385
|
+
existing ??
|
|
386
|
+
parseManifest({
|
|
387
|
+
schemaVersion: SUPPORTED_MANIFEST_SCHEMA,
|
|
388
|
+
project: { name: info.projectName },
|
|
389
|
+
});
|
|
390
|
+
return {
|
|
391
|
+
...base,
|
|
392
|
+
schemaVersion: SUPPORTED_MANIFEST_SCHEMA,
|
|
393
|
+
generatedAt: (info.now ?? new Date()).toISOString(),
|
|
394
|
+
preview: undefined,
|
|
395
|
+
project: { ...base.project, id: info.projectId, name: info.projectName },
|
|
396
|
+
services: {
|
|
397
|
+
...base.services,
|
|
398
|
+
plans: {
|
|
399
|
+
...base.services.plans,
|
|
400
|
+
provider: "apitogo",
|
|
401
|
+
status: "ready",
|
|
402
|
+
apiBaseUrl: info.apiBaseUrl.replace(/\/+$/, ""),
|
|
403
|
+
billing: info.billing,
|
|
404
|
+
catalogRevision: info.catalogRevision,
|
|
405
|
+
capabilities: info.capabilities,
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
322
411
|
/** Replaces `services.json` atomically. Public values only: this file ships inside the portal. */
|
|
323
412
|
export async function writeManifest(
|
|
324
413
|
state: Pick<StatePaths, "dir">,
|
|
@@ -120,7 +120,8 @@ const PLAN_TEMPLATES: Record<"free" | "tiered" | "none", unknown[]> = {
|
|
|
120
120
|
displayName: "Free",
|
|
121
121
|
default: true,
|
|
122
122
|
limits: {
|
|
123
|
-
|
|
123
|
+
// The platform's Free tier allows at most 3 calls per 60 seconds per plan; a higher number is refused.
|
|
124
|
+
rateLimit: { calls: 3, periodSeconds: 60 },
|
|
124
125
|
quota: { calls: 1000, periodDays: 30 },
|
|
125
126
|
},
|
|
126
127
|
},
|
|
@@ -131,7 +132,8 @@ const PLAN_TEMPLATES: Record<"free" | "tiered" | "none", unknown[]> = {
|
|
|
131
132
|
displayName: "Free",
|
|
132
133
|
default: true,
|
|
133
134
|
limits: {
|
|
134
|
-
|
|
135
|
+
// The platform's Free tier allows at most 3 calls per 60 seconds per plan; a higher number is refused.
|
|
136
|
+
rateLimit: { calls: 3, periodSeconds: 60 },
|
|
135
137
|
quota: { calls: 1000, periodDays: 30 },
|
|
136
138
|
},
|
|
137
139
|
},
|
package/src/vite/config.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { loadDevPortalViteDefines } from "./dev-portal-env.js";
|
|
|
19
19
|
import { findPackageRoot } from "./package-root.js";
|
|
20
20
|
import { ensurePagefindDevStub } from "./pagefind-dev-stub.js";
|
|
21
21
|
import vitePlugin from "./plugin.js";
|
|
22
|
-
import { stateIgnore } from "./watch-ignore.js";
|
|
22
|
+
import { devServerAllow, stateIgnore } from "./watch-ignore.js";
|
|
23
23
|
import { getZuploSystemConfigurations } from "./zuplo.js";
|
|
24
24
|
|
|
25
25
|
/** Resolved absolute public directory (Vite default: `<root>/public`). */
|
|
@@ -144,7 +144,7 @@ export async function getViteConfig(
|
|
|
144
144
|
middlewareMode: true,
|
|
145
145
|
open: true,
|
|
146
146
|
fs: {
|
|
147
|
-
allow:
|
|
147
|
+
allow: devServerAllow(dir, config.__meta.moduleDir),
|
|
148
148
|
},
|
|
149
149
|
watch: {
|
|
150
150
|
ignored: [
|
package/src/vite/watch-ignore.ts
CHANGED
|
@@ -14,6 +14,29 @@ const within = (base: string, file: string) =>
|
|
|
14
14
|
|
|
15
15
|
const unique = (paths: string[]) => [...new Set(paths)];
|
|
16
16
|
|
|
17
|
+
/** Whether `root` is the site root (`<state>/generated`) of a config version 2 workspace: it has the `content` link. */
|
|
18
|
+
function isWorkspace(root: string): boolean {
|
|
19
|
+
try {
|
|
20
|
+
return (
|
|
21
|
+
path.basename(root) === "generated" &&
|
|
22
|
+
lstatSync(path.join(root, "content")).isSymbolicLink()
|
|
23
|
+
);
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The tenant's own folder of a config version 2 workspace (what `content` links to), or `undefined` for any other
|
|
31
|
+
* project. The site root sits inside the state folder, so the folder that holds `node_modules` and the pages is
|
|
32
|
+
* outside it and the dev server has to be told it may serve files from there.
|
|
33
|
+
*/
|
|
34
|
+
export function tenantDir(root: string): string | undefined {
|
|
35
|
+
return isWorkspace(root)
|
|
36
|
+
? realpathSync(path.join(root, "content"))
|
|
37
|
+
: undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
17
40
|
/**
|
|
18
41
|
* What the dev server's watcher must leave alone in a config version 2 workspace, or `undefined` for any other
|
|
19
42
|
* project. The watcher meets the state folder twice: below the `content` link and, after resolving the link,
|
|
@@ -23,15 +46,7 @@ const unique = (paths: string[]) => [...new Set(paths)];
|
|
|
23
46
|
export function stateIgnore(
|
|
24
47
|
root: string,
|
|
25
48
|
): ((file: string) => boolean) | undefined {
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
workspace =
|
|
29
|
-
path.basename(root) === "generated" &&
|
|
30
|
-
lstatSync(path.join(root, "content")).isSymbolicLink();
|
|
31
|
-
} catch {
|
|
32
|
-
workspace = false;
|
|
33
|
-
}
|
|
34
|
-
if (!workspace) return undefined;
|
|
49
|
+
if (!isWorkspace(root)) return undefined;
|
|
35
50
|
|
|
36
51
|
const state = path.dirname(root);
|
|
37
52
|
const states = unique([state, realpathSync(state)]);
|
|
@@ -43,3 +58,13 @@ export function stateIgnore(
|
|
|
43
58
|
return STATE_INSIDE_CONTENT.test(file);
|
|
44
59
|
};
|
|
45
60
|
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The folders the dev server may serve files from. The site root of a config version 2 workspace sits inside the
|
|
64
|
+
* state folder, so the package (`node_modules`) and the tenant's pages are outside it. Without them the page's main
|
|
65
|
+
* script fails with "Failed to load url ... Does the file exist?" and the server stops.
|
|
66
|
+
*/
|
|
67
|
+
export function devServerAllow(root: string, moduleDir: string): string[] {
|
|
68
|
+
const tenant = tenantDir(root);
|
|
69
|
+
return [root, path.dirname(root), moduleDir, ...(tenant ? [tenant] : [])];
|
|
70
|
+
}
|