castrum 0.9.0 → 0.9.2

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 (120) hide show
  1. package/README.md +153 -630
  2. package/castrum.linux-x64-gnu.node +0 -0
  3. package/dist/index.d.ts +1653 -1817
  4. package/dist/index.js +6172 -4294
  5. package/docs/API.md +337 -0
  6. package/docs/ARCHITECTURE.md +111 -17
  7. package/docs/BENCHMARKS.md +117 -49
  8. package/docs/CASE_STUDY.md +243 -0
  9. package/docs/ENVIRONMENT.md +24 -7
  10. package/docs/FFI_BUN_GUIDE.md +335 -0
  11. package/docs/GETTING_STARTED.md +18 -34
  12. package/docs/HOW_TO_ADD_AN_OP.md +180 -0
  13. package/docs/INGRESS-ROUTER.md +82 -0
  14. package/docs/INGRESS.md +11 -7
  15. package/docs/NATIVE-ROUTE.md +77 -0
  16. package/docs/REPO_MAP.md +103 -53
  17. package/docs/adr/0001-two-wire-formats.md +1 -1
  18. package/docs/adr/0003-bun-builtins-delegation.md +6 -5
  19. package/docs/ai/LOCAL_DEV.md +81 -0
  20. package/docs/ai/TREE.md +1070 -0
  21. package/docs/bun-builtins-decision-matrix.md +25 -18
  22. package/index.ts +9 -16
  23. package/package.json +41 -28
  24. package/src/ingress/body.ts +167 -27
  25. package/src/ingress/decode/result-base.ts +10 -2
  26. package/src/ingress/handlers.ts +116 -285
  27. package/src/ingress/headers/security.ts +41 -0
  28. package/src/ingress/health.ts +1 -0
  29. package/src/ingress/index.ts +45 -169
  30. package/src/ingress/native-route.ts +182 -0
  31. package/src/ingress/packing/gather-raw-headers.ts +110 -22
  32. package/src/ingress/packing/header-packing.ts +5 -5
  33. package/src/ingress/packing/input-packer.ts +17 -3
  34. package/src/ingress/packing/route-wire.ts +197 -0
  35. package/src/ingress/packing/select-headers.ts +20 -7
  36. package/src/ingress/path-matcher.ts +115 -0
  37. package/src/ingress/response/baked-response.ts +302 -0
  38. package/src/ingress/router.ts +277 -0
  39. package/src/ingress/routes/common.ts +50 -1
  40. package/src/ingress/routes/fallback.ts +2 -2
  41. package/src/ingress/routes/head.ts +3 -13
  42. package/src/ingress/routes/index.ts +5 -4
  43. package/src/ingress/routes/json-write.ts +26 -47
  44. package/src/ingress/routes/native.ts +130 -0
  45. package/src/ingress/routes/options.ts +2 -12
  46. package/src/ingress/routes/read.ts +3 -13
  47. package/src/ingress/routes/responder.ts +159 -0
  48. package/src/ingress/server-node.ts +7 -7
  49. package/src/ingress/server.ts +83 -112
  50. package/src/ingress/sync.ts +151 -0
  51. package/src/ingress/types.ts +44 -1
  52. package/src/integration/pipeline.ts +3 -0
  53. package/src/integration/streaming.ts +2 -3
  54. package/src/integration/websocket.ts +4 -3
  55. package/src/loader/batch.ts +4 -0
  56. package/src/loader/cost.ts +1 -0
  57. package/src/loader/index.ts +43 -228
  58. package/src/loader/state.ts +84 -0
  59. package/src/loader/types.ts +145 -0
  60. package/src/native/ffi/build/codecs.ts +736 -0
  61. package/src/native/ffi/build/compress.ts +248 -0
  62. package/src/native/ffi/build/instances.ts +598 -0
  63. package/src/native/ffi/build/parse.ts +539 -0
  64. package/src/native/ffi/build/util.ts +140 -0
  65. package/src/native/ffi/build.ts +94 -0
  66. package/src/native/ffi/constants.ts +58 -0
  67. package/src/native/ffi/selftest.ts +22 -0
  68. package/src/native/ffi/types.ts +499 -0
  69. package/src/native/ffi.ts +307 -1357
  70. package/src/native/loader.ts +52 -6
  71. package/src/native/types/instances.ts +279 -0
  72. package/src/native/types.ts +41 -238
  73. package/src/runtime/builtins.ts +131 -0
  74. package/src/runtime/codec.ts +147 -0
  75. package/src/runtime/detect.ts +36 -0
  76. package/src/runtime/index.ts +28 -0
  77. package/src/runtime/native.ts +35 -0
  78. package/src/runtime/server.ts +31 -0
  79. package/src/runtime/transport.ts +64 -0
  80. package/src/runtime/types.ts +173 -0
  81. package/src/runtime/uuid.ts +45 -0
  82. package/src/runtime/websocket.ts +30 -0
  83. package/src/rust-ffi/addon.ts +1 -1
  84. package/src/rust-ffi/batch/build.ts +155 -0
  85. package/src/rust-ffi/batch/index.ts +8 -0
  86. package/src/rust-ffi/batch/types.ts +92 -0
  87. package/src/rust-ffi/client.ts +5 -6
  88. package/src/rust-ffi/context.ts +39 -10
  89. package/src/rust-ffi/index.ts +28 -17
  90. package/src/rust-ffi/into.ts +21 -0
  91. package/src/rust-ffi/options.ts +1 -1
  92. package/src/rust-ffi/packed.ts +40 -2
  93. package/src/rust-ffi/proven.ts +16 -30
  94. package/src/rust-ffi/scalar/crypto.ts +188 -107
  95. package/src/rust-ffi/scalar/factories.ts +257 -27
  96. package/src/rust-ffi/scalar/hashing.ts +43 -41
  97. package/src/rust-ffi/scalar/http.ts +208 -77
  98. package/src/rust-ffi/scalar/index.ts +3 -3
  99. package/src/rust-ffi/scalar/interface.ts +88 -213
  100. package/src/rust-ffi/scalar/json-packed.ts +175 -0
  101. package/src/rust-ffi/scalar/json.ts +28 -12
  102. package/src/rust-ffi/scalar/payload.ts +159 -42
  103. package/src/rust-ffi/text.ts +31 -5
  104. package/src/selection.ts +27 -31
  105. package/src/shared/adaptive.ts +1 -2
  106. package/src/shared/buffer-pool.ts +72 -17
  107. package/src/shared/bytes.ts +75 -6
  108. package/src/shared/codec.ts +78 -0
  109. package/src/shared/env.ts +4 -4
  110. package/src/shared/log.ts +1 -0
  111. package/src/shared/packed/index.ts +10 -0
  112. package/src/shared/packed/parsers.ts +31 -0
  113. package/src/shared/packed/schema.ts +23 -0
  114. package/src/shared/{packed.ts → packed/wire.ts} +31 -39
  115. package/src/shared/proven.ts +189 -496
  116. package/src/shared/runtime.ts +20 -25
  117. package/src/shared/uuid.ts +12 -28
  118. package/src/data/json-rows.ts +0 -33
  119. package/src/rust-ffi/batch.ts +0 -431
  120. package/src/shared/bench-classify.ts +0 -209
@@ -0,0 +1,145 @@
1
+ // src/loader/types.ts — public loader type surface (pure types).
2
+ //
3
+ // The callable loader contract (`Loader`), its option/stats shapes, the
4
+ // specialized op-function type (`LoaderOpFn`), and the `load()` helpers.
5
+ // No runtime code — erased at compile time.
6
+
7
+ import type { SchemaValidator } from '../shared/packed'
8
+ import type { LoaderBulk, LoaderBulkArgs, LoaderOpArgs, LoaderOpName, LoaderScalar } from './ops'
9
+
10
+ // ── Public types ────────────────────────────────────────────────────────────
11
+
12
+ /** Options for `createLoader()` / `loader.configure()`. */
13
+ export interface LoaderOptions {
14
+ /** Let each op adapt `batchMin` from measured dispatch cost. Default true. */
15
+ adaptive?: boolean
16
+ /** Initial batch threshold in [2, 8]. Default 2 (batch wins for n >= 2). */
17
+ batchMin?: number
18
+ /** Max cached keys (bounded LRU). 0 disables caching. Default 256. */
19
+ maxCacheKeys?: number
20
+ /** Default `cache` behavior for `load()`. Default true. */
21
+ caching?: boolean
22
+ /** Sample timing every N dispatches (hot-path cost control). Default 32. */
23
+ sampleEvery?: number
24
+ /**
25
+ * `load()` dispatch strategy. `"auto"` (default) switches each op between
26
+ * `"single"` (low load → direct scalar call, no coalescer) and
27
+ * `"coalesce"` (rising load → one packed batch call) from observed load.
28
+ */
29
+ loadStrategy?: LoadStrategy
30
+ }
31
+
32
+ /** Options for `load()` / `opFn.load()`. */
33
+ export interface LoadOptions {
34
+ /**
35
+ * Explicit cache key. When omitted (and caching is on) the default key is
36
+ * `fnv1a64(input)` — which costs one extra native call per load.
37
+ */
38
+ key?: string
39
+ /** Override the loader-wide caching toggle for this call. Default true. */
40
+ cache?: boolean
41
+ }
42
+
43
+ /** Per-op counters exposed on `opFn.stats`. */
44
+ export interface LoaderOpStats {
45
+ scalarCalls: number
46
+ batchCalls: number
47
+ itemsDispatched: number
48
+ cachedHits: number
49
+ /** Current learned batch threshold for this op. */
50
+ batchMin: number
51
+ /** Current `load()` strategy: "single" (low load) or "coalesce" (bulk). */
52
+ mode: 'single' | 'coalesce'
53
+ }
54
+
55
+ /** Aggregate loader statistics. */
56
+ export interface LoaderStats {
57
+ scalarCalls: number
58
+ batchCalls: number
59
+ itemsDispatched: number
60
+ flushes: number
61
+ cachedHits: number
62
+ cacheSize: number
63
+ cacheEvictions: number
64
+ }
65
+
66
+ /** A specialized, pre-bound op function returned by `loader(op)`. */
67
+ export interface LoaderOpFn<K extends LoaderOpName> {
68
+ /** Single item → scalar result. */
69
+ (input: Uint8Array, ...rest: LoaderOpArgs[K]): LoaderScalar<K>
70
+ /** Bulk items → ONE packed batch call; same shape as `rust.batch.<op>`. */
71
+ (inputs: Uint8Array[], ...rest: LoaderBulkArgs<K>): LoaderBulk<K>
72
+ readonly name: K
73
+ readonly stats: LoaderOpStats
74
+ clear(): void
75
+ /** Sync cache peek (default key `fnv1a64(input)` unless `key` given). */
76
+ cache(input: Uint8Array, key?: string): LoaderScalar<K> | undefined
77
+ /**
78
+ * Coalesced, cache-aware load. Unavailable (`undefined`) on ops that require
79
+ * extra arguments (e.g. `hmacSha256`, `signCookie`) — use `run` for those.
80
+ */
81
+ load: K extends LoadableName ? LoaderLoadFn<K> : undefined
82
+ }
83
+
84
+ type LoaderLoadFn<K extends LoaderOpName> = (
85
+ input: Uint8Array,
86
+ opts?: LoadOptions,
87
+ ) => Promise<LoaderScalar<K>>
88
+
89
+ /** Ops whose extra args are all optional/empty → they support `load()`. */
90
+ export type LoadableName = {
91
+ [K in LoaderOpName]: LoaderOpArgs[K] extends
92
+ | []
93
+ | [unknown?]
94
+ | [unknown?, unknown?]
95
+ | [unknown?, unknown?, unknown?]
96
+ ? K
97
+ : never
98
+ }[LoaderOpName]
99
+
100
+ /** A validator-bound helper returned by `loader.schema(validator)`. */
101
+ export interface LoaderSchemaFn {
102
+ /** Single JSON document → valid? */
103
+ (input: Uint8Array): boolean
104
+ /** Bulk JSON documents → bitset (1 per doc). */
105
+ (inputs: Uint8Array[]): Uint8Array
106
+ /** Count how many bulk documents are valid. */
107
+ count(inputs: Uint8Array[]): number
108
+ }
109
+
110
+ /** The global callable loader. */
111
+ export interface Loader {
112
+ /** Higher-order function: `loader(op)` → specialized hot op function. */
113
+ <K extends LoaderOpName>(op: K): LoaderOpFn<K>
114
+ /** Single-item dispatch. */
115
+ run<K extends LoaderOpName>(op: K, input: Uint8Array, ...rest: LoaderOpArgs[K]): LoaderScalar<K>
116
+ /** Bulk dispatch (one packed batch call, or adaptive scalar loop). */
117
+ run<K extends LoaderOpName>(
118
+ op: K,
119
+ inputs: Uint8Array[],
120
+ ...rest: LoaderBulkArgs<K>
121
+ ): LoaderBulk<K>
122
+ /** Coalesced, cache-aware load (ops with required extra args throw). */
123
+ load<K extends LoadableName>(
124
+ op: K,
125
+ input: Uint8Array,
126
+ opts?: LoadOptions,
127
+ ): Promise<LoaderScalar<K>>
128
+ /** Sync cache peek. */
129
+ cache<K extends LoaderOpName>(op: K, input: Uint8Array, key?: string): LoaderScalar<K> | undefined
130
+ /**
131
+ * Bind a `SchemaValidator` for repeated single/bulk JSON-schema validation
132
+ * plus a whole-batch `count`. The returned callable uses the loader's normal
133
+ * dispatch machinery (cost model + counters).
134
+ */
135
+ schema(validator: SchemaValidator): LoaderSchemaFn
136
+ /** Clear the shared cache (pending coalesced loads still flush). */
137
+ clear(): void
138
+ /** Update options at runtime. */
139
+ configure(options: LoaderOptions): void
140
+ readonly stats: LoaderStats
141
+ readonly opNames: LoaderOpName[]
142
+ }
143
+
144
+ /** Load-dispatch strategy selector for `load()`. */
145
+ export type LoadStrategy = 'auto' | 'single' | 'coalesce'