@bakery-framework/plugin-vue 2.0.0-rc.2 → 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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/utils.ts +44 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakery-framework/plugin-vue",
3
- "version": "2.0.0-rc.2",
3
+ "version": "2.0.0-rc.3",
4
4
  "description": "Bakery vue plugin.",
5
5
  "keywords": [
6
6
  "bakery",
@@ -43,7 +43,7 @@
43
43
  "vue": "^3.5.38"
44
44
  },
45
45
  "dependencies": {
46
- "@bakery-framework/core": "^2.0.0-rc.2"
46
+ "@bakery-framework/core": "^2.0.0-rc.3"
47
47
  },
48
48
  "engines": {
49
49
  "bun": ">=1.4.0"
package/src/utils.ts CHANGED
@@ -4,10 +4,11 @@ import {
4
4
  unlink as nodeUnlink,
5
5
  } from 'node:fs/promises'
6
6
  import { LRUCache } from '@bakery-framework/core/cache/lru'
7
- import { Bakery } from '@bakery-framework/core/core/bakery'
7
+ import { Bakery, hostStore } from '@bakery-framework/core/core/bakery'
8
8
  import { Logger } from '@bakery-framework/core/logger'
9
9
  import { fs, is, Try } from '@bakery-framework/core/utils'
10
10
  import { runWithServerBody } from './server-context'
11
+ import type { HostContext } from '@bakery-framework/core/core/bakery'
11
12
  import type { ParsedCacheEntry, ServerResponseOptions, VueMeta } from './types'
12
13
 
13
14
  const logger = new Logger('vue')
@@ -435,6 +436,34 @@ ${
435
436
  `
436
437
  }
437
438
 
439
+ /**
440
+ * Run `fn` in a host store that carries `req`.
441
+ *
442
+ * Preserves an outer store when there is one: it holds the host config and
443
+ * hostname the router resolved, and only `req` needs adding. Outside one
444
+ * (a test driving a route directly, a direct handler call) it builds a
445
+ * minimal context from the process config, which is what `Bakery.config`
446
+ * already falls back to.
447
+ */
448
+ function runInRequest<T>(req: Request, fn: () => T): T {
449
+ const outer = hostStore.getStore()
450
+ if (outer) return hostStore.run({ ...outer, req }, fn)
451
+
452
+ // No outer store: carry the request and leave `config` unread.
453
+ //
454
+ // A plain `{ config: Bakery.config, ... }` throws "Config has not been
455
+ // initialized" the moment this runs before `initConfig()`, which is every
456
+ // test that drives a route directly, so the eager read reintroduced the
457
+ // failure it was added to fix. A getter defers it to whoever actually
458
+ // wants the config, and nothing in a block that only needs `req` does.
459
+ const ctx = { hostname: '', req } as HostContext
460
+ Object.defineProperty(ctx, 'config', {
461
+ get: () => Bakery.config,
462
+ enumerable: true,
463
+ })
464
+ return hostStore.run(ctx, fn)
465
+ }
466
+
438
467
  /** Drop stale compiled versions of the same source so the cache dir stays bounded. */
439
468
  async function pruneServerCache(dir: string, id: string, keepName: string) {
440
469
  const entries = await Try(nodeReaddir(dir))
@@ -525,10 +554,20 @@ export async function getServerResponse(options: ServerResponseOptions) {
525
554
  })
526
555
  const execPromise = Promise.resolve(
527
556
  is.function(exported)
528
- ? // The parameters stay, so every existing block keeps working; the
529
- // store is what lets `getBody()` reach a helper the block calls.
530
- runWithServerBody(body, () =>
531
- exported(req, body, actionName, actionArgs),
557
+ ? // The block runs inside a store carrying its own request, so
558
+ // `getRequest()` works whatever the caller did. Depending on an
559
+ // outer store looked fine because the worker always enters one,
560
+ // and then every test that drives a route directly got a throw,
561
+ // which `getServerResponse` swallows into `{}`: a guard block
562
+ // stopped redirecting and served 200. Entering it here is what
563
+ // makes the function total.
564
+ //
565
+ // An outer store is preserved rather than replaced: it carries the
566
+ // host config and hostname this request resolved.
567
+ runInRequest(req, () =>
568
+ runWithServerBody(body, () =>
569
+ exported(req, body, actionName, actionArgs),
570
+ ),
532
571
  )
533
572
  : exported,
534
573
  )