@bakery-framework/core 2.0.0-rc.1 → 2.0.0-rc.3
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/package.json +1 -1
- package/src/core/context.ts +46 -0
- package/src/core/index.ts +10 -1
package/package.json
CHANGED
package/src/core/context.ts
CHANGED
|
@@ -5,6 +5,16 @@ import { fs } from '../utils/fs'
|
|
|
5
5
|
export type HostContext = {
|
|
6
6
|
config: Readonly<ProcessedAppConfig>
|
|
7
7
|
hostname: string
|
|
8
|
+
/**
|
|
9
|
+
* The request this store was entered for, when there is one.
|
|
10
|
+
*
|
|
11
|
+
* Optional because two of the three `hostStore.run` sites have no request
|
|
12
|
+
* to offer: a WebSocket event carries `ws.data` rather than a `Request`,
|
|
13
|
+
* and the server's `error` hook receives one only sometimes. `getRequest()`
|
|
14
|
+
* below is what callers should use, and it reports the absence rather than
|
|
15
|
+
* handing back `undefined` for them to trip over.
|
|
16
|
+
*/
|
|
17
|
+
req?: Request
|
|
8
18
|
/**
|
|
9
19
|
* Per-request memo of raw `.forbidden` marker probes, keyed by the marker
|
|
10
20
|
* path (`<dir>/.forbidden`). Lazily created by `fs.isForbidden` and never
|
|
@@ -153,3 +163,39 @@ export function getFrameworkVersion() {
|
|
|
153
163
|
}
|
|
154
164
|
return _frameworkVersion || UNKNOWN_FW
|
|
155
165
|
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The request being served, from anywhere inside the request.
|
|
169
|
+
*
|
|
170
|
+
* The point is that it needs no ambient declaration to reach a file. The Vue
|
|
171
|
+
* plugin's `<script server>` blocks used to get `req` and `body` as globals
|
|
172
|
+
* declared in `plugin-vue/src/vue.d.ts`, which works only if that file lands
|
|
173
|
+
* in whatever tsconfig project the editor picks for an SFC. It routinely does
|
|
174
|
+
* not: the generated project that names it lives under `.cache/tsconfig/`,
|
|
175
|
+
* which is not an ancestor of `src/`, so no editor resolves an SFC to it, and
|
|
176
|
+
* the symptom is `req` unresolved and `req.session` missing with nothing to
|
|
177
|
+
* point at. An import cannot fail that way.
|
|
178
|
+
*
|
|
179
|
+
* Two things it fixes beyond reachability. The ambient said `Request` while
|
|
180
|
+
* the value the Vue wrapper actually passes is typed `any`, so the promise
|
|
181
|
+
* and the runtime disagreed; this returns the one real type, `session`
|
|
182
|
+
* included. And it works inside a helper the block calls, which a wrapper
|
|
183
|
+
* parameter cannot reach without being threaded through by hand.
|
|
184
|
+
*
|
|
185
|
+
* **Throws when there is no request**, rather than returning `undefined`.
|
|
186
|
+
* There are exactly two such places and neither is application code that
|
|
187
|
+
* meant to ask: a WebSocket event (which has `ws.data`, not a `Request`) and
|
|
188
|
+
* boot-time code running before any request. Handing back `undefined` would
|
|
189
|
+
* push a null check into every call site to serve two cases that are bugs.
|
|
190
|
+
*/
|
|
191
|
+
export function getRequest(): Request {
|
|
192
|
+
const store = hostStore.getStore()
|
|
193
|
+
if (!store?.req) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
'getRequest() was called outside a request. A WebSocket event has no ' +
|
|
196
|
+
'Request (use the handler argument), and boot-time code runs before ' +
|
|
197
|
+
'there is one.',
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
return store.req
|
|
201
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { getConfig, NOOP } from './config'
|
|
|
7
7
|
// `./context` is already in this barrel's graph: line 5 reaches it through
|
|
8
8
|
// `./bakery`, which re-exports `hostStore` from exactly here, so naming it
|
|
9
9
|
// adds no module edge, only a name.
|
|
10
|
-
import { getFrameworkVersion } from './context'
|
|
10
|
+
import { getFrameworkVersion, getRequest } from './context'
|
|
11
11
|
import { createElement, Fragment, html, raw } from './jsx'
|
|
12
12
|
|
|
13
13
|
export const defineConfig = <T extends AppConfig>(config: T): T => config
|
|
@@ -92,6 +92,15 @@ export {
|
|
|
92
92
|
* ask. The dashboard's footer showed a hardcoded `v3` for want of it.
|
|
93
93
|
*/
|
|
94
94
|
getFrameworkVersion,
|
|
95
|
+
/**
|
|
96
|
+
* The request being served, from anywhere inside the request.
|
|
97
|
+
*
|
|
98
|
+
* Here rather than reachable only through `./core/context` because the
|
|
99
|
+
* whole point is that it needs no ambient declaration and no deep
|
|
100
|
+
* subpath to reach a file: a Vue `<script server>` block, a helper it
|
|
101
|
+
* calls, a `.tsx` page and an API route all import it the same way.
|
|
102
|
+
*/
|
|
103
|
+
getRequest,
|
|
95
104
|
// Multi-host helpers. Documented in docs/configuration/multi-host.md, and
|
|
96
105
|
// the only reason `./core/bakery` had to be a subpath of its own.
|
|
97
106
|
getHostname,
|