@ggui-ai/dev-stack 0.1.0-rc.1
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/LICENSE +201 -0
- package/README.md +63 -0
- package/dist/dev-server/auth.d.ts +44 -0
- package/dist/dev-server/auth.d.ts.map +1 -0
- package/dist/dev-server/auth.js +176 -0
- package/dist/dev-server/events.d.ts +43 -0
- package/dist/dev-server/events.d.ts.map +1 -0
- package/dist/dev-server/events.js +61 -0
- package/dist/dev-server/http.d.ts +79 -0
- package/dist/dev-server/http.d.ts.map +1 -0
- package/dist/dev-server/http.js +277 -0
- package/dist/dev-server/hub-preview.d.ts +56 -0
- package/dist/dev-server/hub-preview.d.ts.map +1 -0
- package/dist/dev-server/hub-preview.js +237 -0
- package/dist/dev-server/hub.d.ts +59 -0
- package/dist/dev-server/hub.d.ts.map +1 -0
- package/dist/dev-server/hub.js +723 -0
- package/dist/hub-preview/client.js +52346 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/local-registry/compile-ui.d.ts +67 -0
- package/dist/local-registry/compile-ui.d.ts.map +1 -0
- package/dist/local-registry/compile-ui.js +148 -0
- package/dist/local-registry/local-registry.d.ts +93 -0
- package/dist/local-registry/local-registry.d.ts.map +1 -0
- package/dist/local-registry/local-registry.js +272 -0
- package/dist/local-registry/watcher.d.ts +42 -0
- package/dist/local-registry/watcher.d.ts.map +1 -0
- package/dist/local-registry/watcher.js +208 -0
- package/dist/run-dev.d.ts +147 -0
- package/dist/run-dev.d.ts.map +1 -0
- package/dist/run-dev.js +216 -0
- package/dist/runtime-supervisor.d.ts +140 -0
- package/dist/runtime-supervisor.d.ts.map +1 -0
- package/dist/runtime-supervisor.js +91 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Loqu, Inc. (Guuey)
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @ggui-ai/dev-stack
|
|
2
|
+
|
|
3
|
+
> Shared local dev engine for the [ggui](https://github.com/ggui-ai/ggui) protocol.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@ggui-ai/dev-stack)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
This package is the engine behind `ggui dev`. It owns the local UI registry, compile-on-demand, a filesystem watcher with SSE, and a small read-only HTTP surface. It is framework-neutral — agent runtime supervision plugs in through an adapter seam, so no agent SDK is hardcoded.
|
|
9
|
+
|
|
10
|
+
Most developers consume this engine indirectly via [`@ggui-ai/cli`](https://www.npmjs.com/package/@ggui-ai/cli) (`ggui dev`). Install this package directly only if you are embedding the dev loop into your own tooling.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @ggui-ai/dev-stack
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## What's inside
|
|
19
|
+
|
|
20
|
+
| Area | Exports |
|
|
21
|
+
| ------------------- | ---------------------------------------------------------------- |
|
|
22
|
+
| Orchestration | `runDev`, `GguiDevError`, `DEFAULT_DEV_PORT`, `DEFAULT_DEV_HOST` |
|
|
23
|
+
| Local UI registry | `LocalUiRegistry` — esbuild-backed compile-on-demand |
|
|
24
|
+
| Compile-on-demand | `compileUiOnDemand`, `resolveEntryFile` |
|
|
25
|
+
| Filesystem watcher | `createLocalWatcher` |
|
|
26
|
+
| HTTP dev server | `startDevServer`, `openEventStream` (SSE) |
|
|
27
|
+
| Dev hub | `renderHubHtml`, `serveHubShell`, `serveHubPreviewShell` |
|
|
28
|
+
| Runtime supervision | `RuntimeSupervisor`, `formatRuntimeEventLine` |
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { runDev } from "@ggui-ai/dev-stack";
|
|
34
|
+
|
|
35
|
+
const bootstrap = await runDev({
|
|
36
|
+
serve: true,
|
|
37
|
+
port: 6780,
|
|
38
|
+
host: "127.0.0.1",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// bootstrap.server is bound and listening; close it to shut down.
|
|
42
|
+
await bootstrap.server?.close();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`runDev` reads `ggui.json` from the current project, discovers local UIs, and starts the dev server. Pass a `runtime` adapter to also supervise a local agent process.
|
|
46
|
+
|
|
47
|
+
## Layering
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
@ggui-ai/ui-registry contract (types only)
|
|
51
|
+
@ggui-ai/project-config manifest schema + loaders
|
|
52
|
+
@ggui-ai/agent-runtime agent runtime adapter seam
|
|
53
|
+
│
|
|
54
|
+
▼
|
|
55
|
+
@ggui-ai/dev-stack this package — the local engine
|
|
56
|
+
│
|
|
57
|
+
▼
|
|
58
|
+
@ggui-ai/cli the `ggui` binary (thin shell)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
Apache 2.0
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
/**
|
|
3
|
+
* Policy shape used by the HTTP layer. `allowOrigin(origin)` returns
|
|
4
|
+
* the exact string to echo in `Access-Control-Allow-Origin` (or
|
|
5
|
+
* `null` to reject). `authorize(req)` decides whether a request
|
|
6
|
+
* carries a valid token.
|
|
7
|
+
*/
|
|
8
|
+
export interface DevServerSecurityPolicy {
|
|
9
|
+
/** Human-readable token the CLI prints. Never put this in logs
|
|
10
|
+
* beyond boot. */
|
|
11
|
+
readonly token: string;
|
|
12
|
+
/** Was the token auto-generated this run? Informs the boot banner. */
|
|
13
|
+
readonly tokenGenerated: boolean;
|
|
14
|
+
/** List of exact-match allowed origins. `'localhost'` is expanded
|
|
15
|
+
* at check time to cover any port. Empty = default set only. */
|
|
16
|
+
readonly allowedOrigins: readonly string[];
|
|
17
|
+
/**
|
|
18
|
+
* Apply the policy to a request. Returns:
|
|
19
|
+
* - `{ outcome: 'handled' }` if the response was already
|
|
20
|
+
* terminated (preflight, unauthorized, etc.) — the caller must
|
|
21
|
+
* NOT write further.
|
|
22
|
+
* - `{ outcome: 'proceed' }` if the request may continue.
|
|
23
|
+
*
|
|
24
|
+
* Mutates `res` to attach CORS headers in the `proceed` path so
|
|
25
|
+
* the main handler doesn't have to repeat them.
|
|
26
|
+
*/
|
|
27
|
+
apply(req: IncomingMessage, res: ServerResponse): PolicyOutcome;
|
|
28
|
+
}
|
|
29
|
+
export type PolicyOutcome = {
|
|
30
|
+
outcome: 'handled';
|
|
31
|
+
} | {
|
|
32
|
+
outcome: 'proceed';
|
|
33
|
+
};
|
|
34
|
+
export interface CreatePolicyOptions {
|
|
35
|
+
/** Override the bearer token. If omitted and `GGUI_DEV_TOKEN` is
|
|
36
|
+
* unset, a random 24-byte hex token is generated. */
|
|
37
|
+
token?: string;
|
|
38
|
+
/** Explicit extra origins (beyond the localhost defaults). */
|
|
39
|
+
extraOrigins?: readonly string[];
|
|
40
|
+
/** Read env vars from this record. Defaults to `process.env`. */
|
|
41
|
+
env?: Record<string, string | undefined>;
|
|
42
|
+
}
|
|
43
|
+
export declare function createSecurityPolicy(options?: CreatePolicyOptions): DevServerSecurityPolicy;
|
|
44
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/dev-server/auth.ts"],"names":[],"mappings":"AA0CA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC;sBACkB;IAClB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,sEAAsE;IACtE,QAAQ,CAAC,cAAc,EAAE,OAAO,CAAC;IACjC;oEACgE;IAChE,QAAQ,CAAC,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAE3C;;;;;;;;;OASG;IACH,KAAK,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,GAAG,aAAa,CAAC;CACjE;AAED,MAAM,MAAM,aAAa,GAAG;IAAE,OAAO,EAAE,SAAS,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,SAAS,CAAA;CAAE,CAAC;AAE5E,MAAM,WAAW,mBAAmB;IAClC;yDACqD;IACrD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8DAA8D;IAC9D,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,iEAAiE;IACjE,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;CAC1C;AA6BD,wBAAgB,oBAAoB,CAClC,OAAO,GAAE,mBAAwB,GAChC,uBAAuB,CA8DzB"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal auth + CORS policy for the local dev server.
|
|
3
|
+
*
|
|
4
|
+
* Trust model (foundation slice, 2026-04-18):
|
|
5
|
+
*
|
|
6
|
+
* - The dev server binds to `127.0.0.1` by default, so the *primary*
|
|
7
|
+
* trust boundary is the loopback interface. This module adds a
|
|
8
|
+
* belt-and-suspenders token + origin check so the same server can
|
|
9
|
+
* also be reached by a browser-side Studio preview opened in a
|
|
10
|
+
* different localhost port (different origin → CORS-regulated)
|
|
11
|
+
* without inventing a wide-open public-by-default posture.
|
|
12
|
+
*
|
|
13
|
+
* - Bearer tokens: `Authorization: Bearer <token>`. A single token
|
|
14
|
+
* per run, either supplied via `GGUI_DEV_TOKEN` or generated from
|
|
15
|
+
* `crypto.randomBytes(24)` on boot. The CLI prints the token + a
|
|
16
|
+
* ready-to-curl `Authorization` header so the developer can paste
|
|
17
|
+
* it wherever they need. `/health` is intentionally unauthenticated
|
|
18
|
+
* — any reachability check, Studio handshake probe, or liveness
|
|
19
|
+
* monitor wants this path without needing the token first.
|
|
20
|
+
*
|
|
21
|
+
* - Origin allowlist: browser clients must send `Origin` matching an
|
|
22
|
+
* allowed pattern. Defaults cover localhost / 127.0.0.1 on any
|
|
23
|
+
* port (`http(s)://localhost:NNNN` / `http(s)://127.0.0.1:NNNN`).
|
|
24
|
+
* Extra origins can be declared via `GGUI_DEV_ORIGINS` as a
|
|
25
|
+
* comma-separated exact-match list — no wildcards by design,
|
|
26
|
+
* Studio's real hostname goes in explicitly or not at all.
|
|
27
|
+
*
|
|
28
|
+
* - Non-browser clients (no `Origin` header) pass the origin check;
|
|
29
|
+
* they are gated by the bearer token alone. That matches `curl`
|
|
30
|
+
* scripts and the paired-client case where the "origin" concept
|
|
31
|
+
* doesn't apply.
|
|
32
|
+
*
|
|
33
|
+
* - Preflight `OPTIONS` is handled here too — short-circuited with
|
|
34
|
+
* a 204 + the CORS headers the browser needs to follow up with the
|
|
35
|
+
* real request. The real handler never sees OPTIONS.
|
|
36
|
+
*
|
|
37
|
+
* This is deliberately the *minimum viable* auth layer. Long-term
|
|
38
|
+
* the pairing protocol in the plan doc §4 replaces per-invocation
|
|
39
|
+
* tokens with device-scoped long-lived tokens; that's a different
|
|
40
|
+
* slice.
|
|
41
|
+
*/
|
|
42
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
43
|
+
const DEFAULT_LOCALHOST_HOSTS = ['localhost', '127.0.0.1'];
|
|
44
|
+
const BEARER_PREFIX = 'bearer ';
|
|
45
|
+
/** Endpoints that skip the token check. Kept in one place so the
|
|
46
|
+
* auth rule is visible at a glance. `/hub` is public because it's
|
|
47
|
+
* the HTML shell a browser loads before it has the token — the
|
|
48
|
+
* existing origin allowlist still blocks cross-origin access, and
|
|
49
|
+
* the shell itself embeds the token for its same-origin XHRs.
|
|
50
|
+
* `/hub/preview` follows the same pattern: the browser loads it as
|
|
51
|
+
* an iframe under the hub, and the shell embeds the bearer for its
|
|
52
|
+
* own data XHRs. */
|
|
53
|
+
const PUBLIC_PATHS = new Set([
|
|
54
|
+
'/health',
|
|
55
|
+
'/health/',
|
|
56
|
+
'/hub',
|
|
57
|
+
'/hub/',
|
|
58
|
+
'/hub/preview',
|
|
59
|
+
'/hub/preview/',
|
|
60
|
+
// The preview iframe loads this bundle with a plain
|
|
61
|
+
// `<script type="module" src="/hub/preview.js">` — browsers do not
|
|
62
|
+
// attach Authorization headers to element-triggered script loads,
|
|
63
|
+
// so the bundle must be reachable without a bearer. The origin
|
|
64
|
+
// allowlist still blocks cross-origin reads; the bundle itself
|
|
65
|
+
// only reads from the iframe's same-origin bootstrap.
|
|
66
|
+
'/hub/preview.js',
|
|
67
|
+
]);
|
|
68
|
+
export function createSecurityPolicy(options = {}) {
|
|
69
|
+
const env = options.env ?? process.env;
|
|
70
|
+
const envToken = env.GGUI_DEV_TOKEN?.trim();
|
|
71
|
+
const explicitToken = options.token ?? (envToken && envToken.length > 0 ? envToken : undefined);
|
|
72
|
+
const tokenGenerated = !explicitToken;
|
|
73
|
+
const token = explicitToken ?? randomBytes(24).toString('hex');
|
|
74
|
+
const fromEnv = (env.GGUI_DEV_ORIGINS ?? '')
|
|
75
|
+
.split(',')
|
|
76
|
+
.map((s) => s.trim())
|
|
77
|
+
.filter((s) => s.length > 0);
|
|
78
|
+
const allowedOrigins = Object.freeze([...(options.extraOrigins ?? []), ...fromEnv]);
|
|
79
|
+
const apply = (req, res) => {
|
|
80
|
+
const origin = header(req, 'origin');
|
|
81
|
+
const pathname = pathOf(req);
|
|
82
|
+
// Preflight: respond even before authentication so the browser
|
|
83
|
+
// can actually deliver the `Authorization` header on the real
|
|
84
|
+
// request. An allowed origin is still required — otherwise we
|
|
85
|
+
// teach the browser to send credentials to a server we might
|
|
86
|
+
// disown.
|
|
87
|
+
if (req.method === 'OPTIONS') {
|
|
88
|
+
if (origin !== null && !isOriginAllowed(origin, allowedOrigins)) {
|
|
89
|
+
res.writeHead(403, { 'content-type': 'application/json' });
|
|
90
|
+
res.end(JSON.stringify({ error: 'origin-not-allowed', origin }));
|
|
91
|
+
return { outcome: 'handled' };
|
|
92
|
+
}
|
|
93
|
+
writeCorsHeaders(res, origin);
|
|
94
|
+
res.writeHead(204);
|
|
95
|
+
res.end();
|
|
96
|
+
return { outcome: 'handled' };
|
|
97
|
+
}
|
|
98
|
+
// Origin check — browser-only. Non-browser clients (`curl`, agent
|
|
99
|
+
// pods, etc.) omit `Origin` and are gated by the token alone.
|
|
100
|
+
if (origin !== null && !isOriginAllowed(origin, allowedOrigins)) {
|
|
101
|
+
res.writeHead(403, { 'content-type': 'application/json' });
|
|
102
|
+
res.end(JSON.stringify({ error: 'origin-not-allowed', origin }));
|
|
103
|
+
return { outcome: 'handled' };
|
|
104
|
+
}
|
|
105
|
+
// Token check — skipped for the public health path so liveness
|
|
106
|
+
// probes don't need the secret.
|
|
107
|
+
if (!PUBLIC_PATHS.has(pathname)) {
|
|
108
|
+
const presented = extractBearerToken(req);
|
|
109
|
+
if (!presented || !constantTimeEqual(presented, token)) {
|
|
110
|
+
writeCorsHeaders(res, origin);
|
|
111
|
+
res.writeHead(401, {
|
|
112
|
+
'content-type': 'application/json',
|
|
113
|
+
'www-authenticate': 'Bearer realm="ggui-dev"',
|
|
114
|
+
});
|
|
115
|
+
res.end(JSON.stringify({ error: 'unauthorized' }));
|
|
116
|
+
return { outcome: 'handled' };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
writeCorsHeaders(res, origin);
|
|
120
|
+
return { outcome: 'proceed' };
|
|
121
|
+
};
|
|
122
|
+
return { token, tokenGenerated, allowedOrigins, apply };
|
|
123
|
+
}
|
|
124
|
+
function writeCorsHeaders(res, origin) {
|
|
125
|
+
if (origin === null)
|
|
126
|
+
return;
|
|
127
|
+
res.setHeader('access-control-allow-origin', origin);
|
|
128
|
+
res.setHeader('access-control-allow-credentials', 'true');
|
|
129
|
+
res.setHeader('access-control-allow-methods', 'GET, HEAD, OPTIONS');
|
|
130
|
+
res.setHeader('access-control-allow-headers', 'authorization, content-type');
|
|
131
|
+
res.setHeader('vary', 'origin');
|
|
132
|
+
}
|
|
133
|
+
function isOriginAllowed(origin, extras) {
|
|
134
|
+
if (extras.includes(origin))
|
|
135
|
+
return true;
|
|
136
|
+
// Normalise and check localhost / 127.0.0.1 defaults.
|
|
137
|
+
let url;
|
|
138
|
+
try {
|
|
139
|
+
url = new URL(origin);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:')
|
|
145
|
+
return false;
|
|
146
|
+
return DEFAULT_LOCALHOST_HOSTS.includes(url.hostname);
|
|
147
|
+
}
|
|
148
|
+
function extractBearerToken(req) {
|
|
149
|
+
const auth = header(req, 'authorization');
|
|
150
|
+
if (auth === null)
|
|
151
|
+
return null;
|
|
152
|
+
if (!auth.toLowerCase().startsWith(BEARER_PREFIX))
|
|
153
|
+
return null;
|
|
154
|
+
const value = auth.slice(BEARER_PREFIX.length).trim();
|
|
155
|
+
return value.length > 0 ? value : null;
|
|
156
|
+
}
|
|
157
|
+
function header(req, name) {
|
|
158
|
+
const raw = req.headers[name];
|
|
159
|
+
if (raw === undefined)
|
|
160
|
+
return null;
|
|
161
|
+
if (Array.isArray(raw))
|
|
162
|
+
return raw[0] ?? null;
|
|
163
|
+
return raw;
|
|
164
|
+
}
|
|
165
|
+
function pathOf(req) {
|
|
166
|
+
const url = new URL(req.url ?? '/', 'http://ggui-dev.local/');
|
|
167
|
+
return url.pathname;
|
|
168
|
+
}
|
|
169
|
+
function constantTimeEqual(a, b) {
|
|
170
|
+
// Pad to equal length so `timingSafeEqual` doesn't throw; length
|
|
171
|
+
// mismatch is still rejected because the buffers won't match.
|
|
172
|
+
const max = Math.max(a.length, b.length);
|
|
173
|
+
const ab = Buffer.from(a.padEnd(max, '\0'));
|
|
174
|
+
const bb = Buffer.from(b.padEnd(max, '\0'));
|
|
175
|
+
return ab.length === bb.length && a.length === b.length && timingSafeEqual(ab, bb);
|
|
176
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-Sent Events endpoint for `ggui dev`.
|
|
3
|
+
*
|
|
4
|
+
* One-way server → client stream of `UiRegistryEvent`. SSE is the
|
|
5
|
+
* minimum viable transport for the dev HMR loop: browser-native
|
|
6
|
+
* on EventSource / fetch-streaming, no handshake complexity, no
|
|
7
|
+
* duplex message framing, survives reconnect via the built-in
|
|
8
|
+
* last-event-id mechanism (not used in this first slice but the
|
|
9
|
+
* format is compatible if we need it later).
|
|
10
|
+
*
|
|
11
|
+
* Auth: `/events` is bearer-gated like the rest of the endpoints
|
|
12
|
+
* (not public like `/health`). The existing security-policy layer
|
|
13
|
+
* in `./http.ts` runs before this handler and applies the same
|
|
14
|
+
* origin allowlist + Authorization token check.
|
|
15
|
+
*
|
|
16
|
+
* Wire format (per event):
|
|
17
|
+
*
|
|
18
|
+
* event: ui
|
|
19
|
+
* data: {"type":"changed","id":"weather-card","contentHash":""}
|
|
20
|
+
*
|
|
21
|
+
* A periodic `:keep-alive` comment keeps the connection open
|
|
22
|
+
* through idle proxies without affecting the data stream.
|
|
23
|
+
*/
|
|
24
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
25
|
+
import type { LocalUiRegistry } from '../local-registry/local-registry.js';
|
|
26
|
+
/**
|
|
27
|
+
* Subscribe options the http router hands down so the tests can
|
|
28
|
+
* override the keep-alive cadence and the heartbeat doesn't keep
|
|
29
|
+
* processes alive past test shutdown.
|
|
30
|
+
*/
|
|
31
|
+
export interface SseOptions {
|
|
32
|
+
/** Milliseconds between `:keep-alive` comments. `0` disables. */
|
|
33
|
+
keepAliveMs?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Attach a new SSE stream for `req`. Writes the SSE preamble,
|
|
37
|
+
* subscribes to `registry`, and forwards every event until the
|
|
38
|
+
* client disconnects. Returns a function that the caller can
|
|
39
|
+
* invoke to force-close the stream (used by the server's close()
|
|
40
|
+
* lifecycle).
|
|
41
|
+
*/
|
|
42
|
+
export declare function openEventStream(req: IncomingMessage, res: ServerResponse, registry: LocalUiRegistry, options?: SseOptions): () => void;
|
|
43
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/dev-server/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAE3E;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,iEAAiE;IACjE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAID;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,EACnB,QAAQ,EAAE,eAAe,EACzB,OAAO,GAAE,UAAe,GACvB,MAAM,IAAI,CAsDZ"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const DEFAULT_KEEP_ALIVE_MS = 15_000;
|
|
2
|
+
/**
|
|
3
|
+
* Attach a new SSE stream for `req`. Writes the SSE preamble,
|
|
4
|
+
* subscribes to `registry`, and forwards every event until the
|
|
5
|
+
* client disconnects. Returns a function that the caller can
|
|
6
|
+
* invoke to force-close the stream (used by the server's close()
|
|
7
|
+
* lifecycle).
|
|
8
|
+
*/
|
|
9
|
+
export function openEventStream(req, res, registry, options = {}) {
|
|
10
|
+
const keepAliveMs = options.keepAliveMs ?? DEFAULT_KEEP_ALIVE_MS;
|
|
11
|
+
// 200 + SSE content-type. Leave CORS / auth headers alone —
|
|
12
|
+
// the security policy already set them on the way in.
|
|
13
|
+
res.writeHead(200, {
|
|
14
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
15
|
+
'cache-control': 'no-cache, no-transform',
|
|
16
|
+
connection: 'keep-alive',
|
|
17
|
+
// Explicit message framing hint for browsers behind proxies
|
|
18
|
+
// that buffer by default. Harmless when unused.
|
|
19
|
+
'x-accel-buffering': 'no',
|
|
20
|
+
});
|
|
21
|
+
// Initial hello — lets the client know the stream is live
|
|
22
|
+
// before any UI event fires. Studio uses this to flip its
|
|
23
|
+
// status indicator to "live."
|
|
24
|
+
res.write(`event: hello\ndata: {"ok":true}\n\n`);
|
|
25
|
+
const unsubscribe = registry.subscribe((event) => {
|
|
26
|
+
try {
|
|
27
|
+
res.write(`event: ui\ndata: ${JSON.stringify(event)}\n\n`);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Client already went away; the `close` listener below
|
|
31
|
+
// runs the full unsub.
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const keepAlive = keepAliveMs > 0
|
|
35
|
+
? setInterval(() => {
|
|
36
|
+
try {
|
|
37
|
+
res.write(`:keep-alive\n\n`);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// Same as above — let the close handler clean up.
|
|
41
|
+
}
|
|
42
|
+
}, keepAliveMs)
|
|
43
|
+
: null;
|
|
44
|
+
if (keepAlive)
|
|
45
|
+
keepAlive.unref?.();
|
|
46
|
+
function closeStream() {
|
|
47
|
+
unsubscribe();
|
|
48
|
+
if (keepAlive)
|
|
49
|
+
clearInterval(keepAlive);
|
|
50
|
+
try {
|
|
51
|
+
res.end();
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// Already ended.
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
req.on('close', closeStream);
|
|
58
|
+
req.on('aborted', closeStream);
|
|
59
|
+
res.on('close', closeStream);
|
|
60
|
+
return closeStream;
|
|
61
|
+
}
|