invariant.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Package-owned invariant companion for `@deepseek-ai/dsh-client-modules`.
  3. * @module @deepseek-ai/dsh-client-modules/invariant
  4. */
  5. /* jscpd:ignore-start */
  6. import type { Context } from '@deepseek-ai/cordis'
  7. import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
  8. const PACKAGE_NAME = '@deepseek-ai/dsh-client-modules'
  9. /** Cordis companion plugin name. */
  10. export const name = 'client-modules-invariant'
  11. /** Service required before the companion can reserve package ownership. */
  12. export const inject = ['invariants']
  13. /**
  14. * Owned relation: the node half's boot entry graph must stay self-consistent
  15. * — every row must resolve a clientPath under the same id (the
  16. * /plugins/<id>/client.js URL it advertises would otherwise 404 on a browser
  17. * that just received the graph). Checked on every scan trigger (cordis
  18. * 'internal/plugin'): graph() and clientPath() read the same table object,
  19. * so the relation holds at any instant — no need to wait out the node half's
  20. * own microtask-debounced flush.
  21. */
  22. const install: InvariantInstaller = (ctx, fail) => {
  23. ctx.on('internal/plugin', () => {
  24. const host = ctx.get('clientModuleHost')
  25. if (host === undefined) return // browser side / host without the node half: nothing to audit
  26. for (const row of host.graph().entries) {
  27. if (host.clientPath(row.id) === undefined) {
  28. fail(`web plugin graph row "${row.id}" advertises ${row.url} but resolves no client bundle path — the served __DSH_BOOT__ would 404 on fetch`)
  29. }
  30. }
  31. }, { global: true })
  32. }
  33. /**
  34. * Register this package's invariant companion.
  35. * @param ctx - Cordis context carrying the invariant service.
  36. * @returns the installed registration's disposer after setup succeeds.
  37. */
  38. export const apply = (ctx: Context): Promise<() => void> =>
  39. Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
  40. /* jscpd:ignore-end */