@silkweave/nestjs 1.9.0 → 1.10.0

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.
@@ -1,58 +0,0 @@
1
- import { Inject, Injectable, type OnApplicationBootstrap, type OnApplicationShutdown, type OnModuleInit } from '@nestjs/common'
2
- import { HttpAdapterHost } from '@nestjs/core'
3
- import { type AdapterGenerator, silkweave as createSilkweave, type Silkweave } from '@silkweave/core'
4
- import { ActionDiscovery } from './discovery.js'
5
- import { SILKWEAVE_MODULE_OPTIONS, type SilkweaveModuleOptions } from './types.js'
6
-
7
- /**
8
- * Coordinates the Nest lifecycle for `@silkweave/nestjs`:
9
- *
10
- * - `onModuleInit`: each configured adapter calls `install(host)` to reserve a
11
- * placeholder middleware slot at its base path. This must happen here, not
12
- * in `onApplicationBootstrap`, because Nest installs its 404 catch-all later
13
- * during `init()` — routes registered after the catch-all are unreachable.
14
- *
15
- * - `onApplicationBootstrap`: discovers every `@Action` method, then drives the
16
- * standard `silkweave().adapter(...).actions(...).start()` flow, which calls
17
- * each adapter's `start(actions)` to populate the slot reserved earlier.
18
- */
19
- @Injectable()
20
- export class SilkweaveService implements OnModuleInit, OnApplicationBootstrap, OnApplicationShutdown {
21
- private builder?: Silkweave
22
- private readonly generators: AdapterGenerator[] = []
23
-
24
- constructor(
25
- @Inject(SILKWEAVE_MODULE_OPTIONS) private readonly options: SilkweaveModuleOptions,
26
- private readonly discovery: ActionDiscovery,
27
- private readonly httpAdapterHost: HttpAdapterHost
28
- ) {}
29
-
30
- onModuleInit(): void {
31
- for (const adapter of this.options.adapters) {
32
- this.generators.push(adapter.install(this.httpAdapterHost))
33
- }
34
- }
35
-
36
- async onApplicationBootstrap(): Promise<void> {
37
- const actions = this.discovery.discover()
38
- const builder = createSilkweave(this.options.silkweave)
39
- for (const [key, value] of Object.entries(this.options.context ?? {})) {
40
- builder.set(key, value)
41
- }
42
- for (const gen of this.generators) {
43
- builder.adapter(gen)
44
- }
45
- builder.actions(actions)
46
- await builder.start()
47
- this.builder = builder
48
- }
49
-
50
- async onApplicationShutdown(): Promise<void> {
51
- this.builder = undefined
52
- }
53
-
54
- /** Access the underlying silkweave builder once bootstrap has completed. */
55
- getBuilder(): Silkweave | undefined {
56
- return this.builder
57
- }
58
- }
package/src/lib/slot.ts DELETED
@@ -1,51 +0,0 @@
1
- import type { IncomingMessage, ServerResponse } from 'http'
2
-
3
- export type NodeMiddleware = (
4
- req: IncomingMessage,
5
- res: ServerResponse,
6
- next?: (err?: unknown) => void
7
- ) => unknown
8
-
9
- /**
10
- * A mountable middleware slot that delegates to a settable handler. Used to
11
- * reserve a path on Nest's HTTP server *before* its 404 catch-all is
12
- * installed, then populate the real handler later in `OnApplicationBootstrap`.
13
- *
14
- * Before `set()` is called, the slot responds with HTTP 503.
15
- */
16
- export interface MiddlewareSlot {
17
- middleware: NodeMiddleware
18
- set(handler: NodeMiddleware): void
19
- }
20
-
21
- export function createMiddlewareSlot(label: string): MiddlewareSlot {
22
- let handler: NodeMiddleware = (_req, res) => {
23
- res.statusCode = 503
24
- res.setHeader('Content-Type', 'application/json')
25
- res.end(JSON.stringify({ error: 'not_ready', message: `${label} adapter has not started yet` }))
26
- }
27
- return {
28
- middleware: (req, res, next) => handler(req, res, next),
29
- set: (h) => { handler = h }
30
- }
31
- }
32
-
33
- interface HttpAdapterUseLike {
34
- use(path: string, handler: NodeMiddleware): unknown
35
- }
36
-
37
- /**
38
- * Mount a middleware slot at the given base path on Nest's HTTP adapter and
39
- * return the slot's `set()` callback. The slot middleware is installed
40
- * synchronously so it sits in the Express stack before Nest's later-installed
41
- * 404 catch-all.
42
- */
43
- export function reserveSlot(
44
- httpAdapter: HttpAdapterUseLike,
45
- basePath: string,
46
- label: string
47
- ): (handler: NodeMiddleware) => void {
48
- const slot = createMiddlewareSlot(label)
49
- httpAdapter.use(basePath, slot.middleware)
50
- return slot.set
51
- }