@brud/kernel 0.0.0-stage → 0.0.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 (286) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +63 -2
  3. package/dist/index.d.ts +15 -0
  4. package/dist/index.js +13 -0
  5. package/dist/serviceModules/events.d.ts +1 -0
  6. package/dist/serviceModules/events.js +1 -0
  7. package/dist/serviceModules/exposedApiRegistry.d.ts +1 -0
  8. package/dist/serviceModules/exposedApiRegistry.js +1 -0
  9. package/dist/serviceModules/identityRegistry.d.ts +1 -0
  10. package/dist/serviceModules/identityRegistry.js +1 -0
  11. package/dist/serviceModules/interception.d.ts +1 -0
  12. package/dist/serviceModules/interception.js +1 -0
  13. package/dist/serviceModules/parser.d.ts +1 -0
  14. package/dist/serviceModules/parser.js +1 -0
  15. package/dist/serviceModules/router.d.ts +1 -0
  16. package/dist/serviceModules/router.js +1 -0
  17. package/dist/serviceModules/wasmCage.d.ts +1 -0
  18. package/dist/serviceModules/wasmCage.js +1 -0
  19. package/dist/services/Events/contracts/IEventsService.d.ts +64 -0
  20. package/dist/services/Events/contracts/IEventsService.js +1 -0
  21. package/dist/services/Events/contracts/errors.d.ts +31 -0
  22. package/dist/services/Events/contracts/errors.js +40 -0
  23. package/dist/services/Events/contracts/index.d.ts +3 -0
  24. package/dist/services/Events/contracts/index.js +3 -0
  25. package/dist/services/Events/contracts/tokens.d.ts +2 -0
  26. package/dist/services/Events/contracts/tokens.js +2 -0
  27. package/dist/services/Events/descriptor/EventsDescriptor.d.ts +9 -0
  28. package/dist/services/Events/descriptor/EventsDescriptor.js +56 -0
  29. package/dist/services/Events/descriptor/tokens.d.ts +1 -0
  30. package/dist/services/Events/descriptor/tokens.js +1 -0
  31. package/dist/services/Events/index.d.ts +4 -0
  32. package/dist/services/Events/index.js +4 -0
  33. package/dist/services/Events/orchestrator/EventsService.d.ts +51 -0
  34. package/dist/services/Events/orchestrator/EventsService.js +339 -0
  35. package/dist/services/Events/orchestrator/index.d.ts +2 -0
  36. package/dist/services/Events/orchestrator/index.js +2 -0
  37. package/dist/services/Events/orchestrator/limits.d.ts +22 -0
  38. package/dist/services/Events/orchestrator/limits.js +33 -0
  39. package/dist/services/Events/serviceModule.d.ts +2 -0
  40. package/dist/services/Events/serviceModule.js +5 -0
  41. package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.d.ts +56 -0
  42. package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.js +1 -0
  43. package/dist/services/ExposedApiRegistry/contracts/errors.d.ts +132 -0
  44. package/dist/services/ExposedApiRegistry/contracts/errors.js +195 -0
  45. package/dist/services/ExposedApiRegistry/contracts/index.d.ts +9 -0
  46. package/dist/services/ExposedApiRegistry/contracts/index.js +4 -0
  47. package/dist/services/ExposedApiRegistry/contracts/policy.d.ts +90 -0
  48. package/dist/services/ExposedApiRegistry/contracts/policy.js +169 -0
  49. package/dist/services/ExposedApiRegistry/contracts/registrant.d.ts +37 -0
  50. package/dist/services/ExposedApiRegistry/contracts/registrant.js +36 -0
  51. package/dist/services/ExposedApiRegistry/contracts/types.d.ts +133 -0
  52. package/dist/services/ExposedApiRegistry/contracts/types.js +73 -0
  53. package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.d.ts +44 -0
  54. package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.js +58 -0
  55. package/dist/services/ExposedApiRegistry/descriptor/tokens.d.ts +8 -0
  56. package/dist/services/ExposedApiRegistry/descriptor/tokens.js +8 -0
  57. package/dist/services/ExposedApiRegistry/index.d.ts +6 -0
  58. package/dist/services/ExposedApiRegistry/index.js +5 -0
  59. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.d.ts +68 -0
  60. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +452 -0
  61. package/dist/services/ExposedApiRegistry/orchestrator/index.d.ts +2 -0
  62. package/dist/services/ExposedApiRegistry/orchestrator/index.js +1 -0
  63. package/dist/services/ExposedApiRegistry/serviceModule.d.ts +10 -0
  64. package/dist/services/ExposedApiRegistry/serviceModule.js +20 -0
  65. package/dist/services/ExposedApiRegistry/storage/protection.d.ts +21 -0
  66. package/dist/services/ExposedApiRegistry/storage/protection.js +35 -0
  67. package/dist/services/ExposedApiRegistry/storage/scopeRecord.d.ts +45 -0
  68. package/dist/services/ExposedApiRegistry/storage/scopeRecord.js +178 -0
  69. package/dist/services/IdentityRegistry/contracts/errors.d.ts +88 -0
  70. package/dist/services/IdentityRegistry/contracts/errors.js +128 -0
  71. package/dist/services/IdentityRegistry/contracts/index.d.ts +5 -0
  72. package/dist/services/IdentityRegistry/contracts/index.js +3 -0
  73. package/dist/services/IdentityRegistry/contracts/types.d.ts +77 -0
  74. package/dist/services/IdentityRegistry/contracts/types.js +18 -0
  75. package/dist/services/IdentityRegistry/descriptor/descriptor.d.ts +36 -0
  76. package/dist/services/IdentityRegistry/descriptor/descriptor.js +44 -0
  77. package/dist/services/IdentityRegistry/descriptor/tokens.d.ts +8 -0
  78. package/dist/services/IdentityRegistry/descriptor/tokens.js +8 -0
  79. package/dist/services/IdentityRegistry/index.d.ts +11 -0
  80. package/dist/services/IdentityRegistry/index.js +6 -0
  81. package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.d.ts +47 -0
  82. package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.js +240 -0
  83. package/dist/services/IdentityRegistry/serviceModule.d.ts +10 -0
  84. package/dist/services/IdentityRegistry/serviceModule.js +24 -0
  85. package/dist/services/IdentityRegistry/storage/registryDocument.d.ts +55 -0
  86. package/dist/services/IdentityRegistry/storage/registryDocument.js +235 -0
  87. package/dist/services/Interception/contracts/IInterceptionService.d.ts +155 -0
  88. package/dist/services/Interception/contracts/IInterceptionService.js +12 -0
  89. package/dist/services/Interception/contracts/errors.d.ts +26 -0
  90. package/dist/services/Interception/contracts/errors.js +35 -0
  91. package/dist/services/Interception/contracts/index.d.ts +3 -0
  92. package/dist/services/Interception/contracts/index.js +3 -0
  93. package/dist/services/Interception/contracts/tokens.d.ts +2 -0
  94. package/dist/services/Interception/contracts/tokens.js +2 -0
  95. package/dist/services/Interception/descriptor/InterceptionDescriptor.d.ts +9 -0
  96. package/dist/services/Interception/descriptor/InterceptionDescriptor.js +50 -0
  97. package/dist/services/Interception/descriptor/tokens.d.ts +1 -0
  98. package/dist/services/Interception/descriptor/tokens.js +1 -0
  99. package/dist/services/Interception/index.d.ts +4 -0
  100. package/dist/services/Interception/index.js +4 -0
  101. package/dist/services/Interception/orchestrator/InterceptionService.d.ts +40 -0
  102. package/dist/services/Interception/orchestrator/InterceptionService.js +343 -0
  103. package/dist/services/Interception/orchestrator/index.d.ts +1 -0
  104. package/dist/services/Interception/orchestrator/index.js +1 -0
  105. package/dist/services/Interception/serviceModule.d.ts +2 -0
  106. package/dist/services/Interception/serviceModule.js +5 -0
  107. package/dist/services/Parser/contracts/IParserService.d.ts +21 -0
  108. package/dist/services/Parser/contracts/IParserService.js +1 -0
  109. package/dist/services/Parser/contracts/errors.d.ts +25 -0
  110. package/dist/services/Parser/contracts/errors.js +33 -0
  111. package/dist/services/Parser/contracts/index.d.ts +4 -0
  112. package/dist/services/Parser/contracts/index.js +1 -0
  113. package/dist/services/Parser/contracts/types.d.ts +42 -0
  114. package/dist/services/Parser/contracts/types.js +1 -0
  115. package/dist/services/Parser/descriptor/ParserDescriptor.d.ts +23 -0
  116. package/dist/services/Parser/descriptor/ParserDescriptor.js +50 -0
  117. package/dist/services/Parser/descriptor/tokens.d.ts +8 -0
  118. package/dist/services/Parser/descriptor/tokens.js +8 -0
  119. package/dist/services/Parser/index.d.ts +8 -0
  120. package/dist/services/Parser/index.js +8 -0
  121. package/dist/services/Parser/orchestrator/ParserService.d.ts +20 -0
  122. package/dist/services/Parser/orchestrator/ParserService.js +105 -0
  123. package/dist/services/Parser/orchestrator/index.d.ts +1 -0
  124. package/dist/services/Parser/orchestrator/index.js +1 -0
  125. package/dist/services/Parser/parser/indexSynthesis.d.ts +11 -0
  126. package/dist/services/Parser/parser/indexSynthesis.js +13 -0
  127. package/dist/services/Parser/parser/parameters.d.ts +14 -0
  128. package/dist/services/Parser/parser/parameters.js +89 -0
  129. package/dist/services/Parser/parser/scanner.d.ts +48 -0
  130. package/dist/services/Parser/parser/scanner.js +156 -0
  131. package/dist/services/Parser/parser/syntax.d.ts +30 -0
  132. package/dist/services/Parser/parser/syntax.js +34 -0
  133. package/dist/services/Parser/serviceModule.d.ts +2 -0
  134. package/dist/services/Parser/serviceModule.js +5 -0
  135. package/dist/services/Router/bootOptions.d.ts +2 -0
  136. package/dist/services/Router/bootOptions.js +2 -0
  137. package/dist/services/Router/contracts/IRouterService.d.ts +43 -0
  138. package/dist/services/Router/contracts/IRouterService.js +1 -0
  139. package/dist/services/Router/contracts/caller.d.ts +6 -0
  140. package/dist/services/Router/contracts/caller.js +6 -0
  141. package/dist/services/Router/contracts/errors.d.ts +67 -0
  142. package/dist/services/Router/contracts/errors.js +90 -0
  143. package/dist/services/Router/contracts/index.d.ts +8 -0
  144. package/dist/services/Router/contracts/index.js +4 -0
  145. package/dist/services/Router/contracts/kind.d.ts +7 -0
  146. package/dist/services/Router/contracts/kind.js +10 -0
  147. package/dist/services/Router/contracts/registrant.d.ts +23 -0
  148. package/dist/services/Router/contracts/registrant.js +13 -0
  149. package/dist/services/Router/contracts/types.d.ts +110 -0
  150. package/dist/services/Router/contracts/types.js +1 -0
  151. package/dist/services/Router/descriptor/RouterDescriptor.d.ts +32 -0
  152. package/dist/services/Router/descriptor/RouterDescriptor.js +92 -0
  153. package/dist/services/Router/descriptor/tokens.d.ts +9 -0
  154. package/dist/services/Router/descriptor/tokens.js +9 -0
  155. package/dist/services/Router/index.d.ts +4 -0
  156. package/dist/services/Router/index.js +4 -0
  157. package/dist/services/Router/orchestrator/RouterService.d.ts +93 -0
  158. package/dist/services/Router/orchestrator/RouterService.js +457 -0
  159. package/dist/services/Router/orchestrator/index.d.ts +2 -0
  160. package/dist/services/Router/orchestrator/index.js +1 -0
  161. package/dist/services/Router/serviceModule.d.ts +4 -0
  162. package/dist/services/Router/serviceModule.js +22 -0
  163. package/dist/services/WasmCage/index.d.ts +7 -0
  164. package/dist/services/WasmCage/index.js +4 -0
  165. package/dist/services/WasmCage/scripts/prepin.d.ts +1 -0
  166. package/dist/services/WasmCage/scripts/prepin.js +38 -0
  167. package/dist/services/WasmCage/serviceModule.d.ts +12 -0
  168. package/dist/services/WasmCage/serviceModule.js +18 -0
  169. package/dist/services/WasmCage/src/caged.d.ts +121 -0
  170. package/dist/services/WasmCage/src/caged.js +460 -0
  171. package/dist/services/WasmCage/src/descriptor.d.ts +47 -0
  172. package/dist/services/WasmCage/src/descriptor.js +56 -0
  173. package/dist/services/WasmCage/src/native.d.ts +29 -0
  174. package/dist/services/WasmCage/src/native.js +66 -0
  175. package/dist/services/WasmCage/src/nativeDigests.d.ts +9 -0
  176. package/dist/services/WasmCage/src/nativeDigests.js +15 -0
  177. package/dist/services/WasmCage/src/package.d.ts +36 -0
  178. package/dist/services/WasmCage/src/package.js +122 -0
  179. package/dist/services/shared/canonicalJson.d.ts +29 -0
  180. package/dist/services/shared/canonicalJson.js +93 -0
  181. package/dist/services/shared/documentStore.d.ts +16 -0
  182. package/dist/services/shared/documentStore.js +98 -0
  183. package/dist/src/bootstrap/bootKernelInternal.d.ts +19 -0
  184. package/dist/src/bootstrap/bootKernelInternal.js +633 -0
  185. package/dist/src/bootstrap/bootTypes.d.ts +113 -0
  186. package/dist/src/bootstrap/bootTypes.js +1 -0
  187. package/dist/src/bootstrap/config/index.d.ts +3 -0
  188. package/dist/src/bootstrap/config/index.js +2 -0
  189. package/dist/src/bootstrap/config/integrity.d.ts +45 -0
  190. package/dist/src/bootstrap/config/integrity.js +92 -0
  191. package/dist/src/bootstrap/config/parse.d.ts +15 -0
  192. package/dist/src/bootstrap/config/parse.js +81 -0
  193. package/dist/src/bootstrap/config/pathWriteGuard.d.ts +20 -0
  194. package/dist/src/bootstrap/config/pathWriteGuard.js +349 -0
  195. package/dist/src/bootstrap/config/types.d.ts +41 -0
  196. package/dist/src/bootstrap/config/types.js +15 -0
  197. package/dist/src/bootstrap/hardcore.d.ts +212 -0
  198. package/dist/src/bootstrap/hardcore.js +312 -0
  199. package/dist/src/bootstrap/hardcoreServices.d.ts +10 -0
  200. package/dist/src/bootstrap/hardcoreServices.js +258 -0
  201. package/dist/src/bootstrap/index.d.ts +13 -0
  202. package/dist/src/bootstrap/index.js +7 -0
  203. package/dist/src/bootstrap/loaded.d.ts +30 -0
  204. package/dist/src/bootstrap/loaded.js +45 -0
  205. package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
  206. package/dist/src/bootstrap/packageRequirements.js +52 -0
  207. package/dist/src/bootstrap/reload.d.ts +44 -0
  208. package/dist/src/bootstrap/reload.js +45 -0
  209. package/dist/src/bootstrap/seal.d.ts +32 -0
  210. package/dist/src/bootstrap/seal.js +101 -0
  211. package/dist/src/contracts/accessBroker.d.ts +53 -0
  212. package/dist/src/contracts/accessBroker.js +1 -0
  213. package/dist/src/contracts/brudOperation.d.ts +20 -0
  214. package/dist/src/contracts/brudOperation.js +20 -0
  215. package/dist/src/contracts/callChain.d.ts +161 -0
  216. package/dist/src/contracts/callChain.js +19 -0
  217. package/dist/src/contracts/capability.d.ts +26 -0
  218. package/dist/src/contracts/capability.js +26 -0
  219. package/dist/src/contracts/capabilitySet.d.ts +23 -0
  220. package/dist/src/contracts/capabilitySet.js +72 -0
  221. package/dist/src/contracts/container.d.ts +17 -0
  222. package/dist/src/contracts/container.js +8 -0
  223. package/dist/src/contracts/context.d.ts +19 -0
  224. package/dist/src/contracts/context.js +1 -0
  225. package/dist/src/contracts/contextContract.d.ts +132 -0
  226. package/dist/src/contracts/contextContract.js +274 -0
  227. package/dist/src/contracts/dispatchView.d.ts +21 -0
  228. package/dist/src/contracts/dispatchView.js +1 -0
  229. package/dist/src/contracts/handler.d.ts +21 -0
  230. package/dist/src/contracts/handler.js +1 -0
  231. package/dist/src/contracts/index.d.ts +26 -0
  232. package/dist/src/contracts/index.js +9 -0
  233. package/dist/src/contracts/kernel.d.ts +60 -0
  234. package/dist/src/contracts/kernel.js +1 -0
  235. package/dist/src/contracts/lifecycle.d.ts +59 -0
  236. package/dist/src/contracts/lifecycle.js +1 -0
  237. package/dist/src/contracts/manifest.d.ts +180 -0
  238. package/dist/src/contracts/manifest.js +334 -0
  239. package/dist/src/contracts/pipeline.d.ts +8 -0
  240. package/dist/src/contracts/pipeline.js +1 -0
  241. package/dist/src/contracts/policy.d.ts +75 -0
  242. package/dist/src/contracts/policy.js +77 -0
  243. package/dist/src/contracts/registry.d.ts +7 -0
  244. package/dist/src/contracts/registry.js +1 -0
  245. package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
  246. package/dist/src/contracts/serviceDescriptor.js +1 -0
  247. package/dist/src/contracts/serviceToken.d.ts +36 -0
  248. package/dist/src/contracts/serviceToken.js +16 -0
  249. package/dist/src/engine/BrudKernel.d.ts +120 -0
  250. package/dist/src/engine/BrudKernel.js +395 -0
  251. package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
  252. package/dist/src/engine/ExecutionPipeline.js +62 -0
  253. package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
  254. package/dist/src/engine/KernelAccessBroker.js +107 -0
  255. package/dist/src/engine/KernelRegistry.d.ts +14 -0
  256. package/dist/src/engine/KernelRegistry.js +33 -0
  257. package/dist/src/engine/OperationRegistry.d.ts +9 -0
  258. package/dist/src/engine/OperationRegistry.js +18 -0
  259. package/dist/src/engine/ServiceContainer.d.ts +13 -0
  260. package/dist/src/engine/ServiceContainer.js +53 -0
  261. package/dist/src/engine/callChain.d.ts +68 -0
  262. package/dist/src/engine/callChain.js +175 -0
  263. package/dist/src/engine/dag.d.ts +2 -0
  264. package/dist/src/engine/dag.js +34 -0
  265. package/dist/src/engine/errors.d.ts +26 -0
  266. package/dist/src/engine/errors.js +52 -0
  267. package/dist/src/engine/index.d.ts +11 -0
  268. package/dist/src/engine/index.js +8 -0
  269. package/dist/src/engine/state.d.ts +1 -0
  270. package/dist/src/engine/state.js +1 -0
  271. package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
  272. package/dist/src/validation/jsonSchema/compile.js +386 -0
  273. package/dist/src/validation/jsonSchema/index.d.ts +3 -0
  274. package/dist/src/validation/jsonSchema/index.js +2 -0
  275. package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
  276. package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
  277. package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
  278. package/dist/src/validation/jsonSchema/pattern.js +155 -0
  279. package/dist/src/validation/jsonSchema/types.d.ts +71 -0
  280. package/dist/src/validation/jsonSchema/types.js +15 -0
  281. package/package.json +59 -4
  282. package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
  283. package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
  284. package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
  285. package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
  286. package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
@@ -0,0 +1,386 @@
1
+ /**
2
+ * JSON Schema subset compiler and validator.
3
+ *
4
+ * `compileJsonSchema` checks an untrusted schema once, rejecting every keyword
5
+ * outside the supported subset (fail closed), and returns a frozen validator.
6
+ * Neither function throws, and neither mutates its arguments.
7
+ */
8
+ import { codePointLength, freezeJsonCopy, inspectJsonValue, jsonEqual, jsonTypeOf, pointerJoin, } from './jsonValue.js';
9
+ import { screenPattern } from './pattern.js';
10
+ import { DEFAULT_JSON_SCHEMA_LIMITS } from './types.js';
11
+ const TYPE_NAMES = new Set([
12
+ 'null', 'boolean', 'number', 'integer', 'string', 'array', 'object',
13
+ ]);
14
+ const CONSTRAINT_KEYWORDS = new Set([
15
+ 'type', 'properties', 'required', 'additionalProperties', 'enum', 'const',
16
+ 'items', 'minItems', 'maxItems', 'minimum', 'maximum', 'exclusiveMinimum',
17
+ 'exclusiveMaximum', 'minLength', 'maxLength', 'pattern', 'oneOf',
18
+ ]);
19
+ const ANNOTATION_KEYWORDS = new Set([
20
+ '$schema', '$id', 'title', 'description', '$comment', 'default', 'examples',
21
+ 'protected',
22
+ ]);
23
+ function isNonNegativeInteger(value) {
24
+ return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
25
+ }
26
+ function isFiniteNumber(value) {
27
+ return typeof value === 'number' && Number.isFinite(value);
28
+ }
29
+ function resolveLimits(overrides) {
30
+ const merged = { ...DEFAULT_JSON_SCHEMA_LIMITS };
31
+ for (const [key, value] of Object.entries(overrides ?? {})) {
32
+ if (!(key in DEFAULT_JSON_SCHEMA_LIMITS)) {
33
+ return `unknown limit "${key}"`;
34
+ }
35
+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 1) {
36
+ return `limit "${key}" must be a positive integer`;
37
+ }
38
+ merged[key] = value;
39
+ }
40
+ return Object.freeze(merged);
41
+ }
42
+ class SchemaCompiler {
43
+ limits;
44
+ errors = [];
45
+ nodes = 0;
46
+ constructor(limits) {
47
+ this.limits = limits;
48
+ }
49
+ compile(schema, schemaPath, depth) {
50
+ this.nodes += 1;
51
+ if (this.nodes > this.limits.maxSchemaNodes) {
52
+ this.error('SCHEMA_LIMIT_EXCEEDED', schemaPath, `schema has more than ${this.limits.maxSchemaNodes} nodes`);
53
+ return false;
54
+ }
55
+ if (depth > this.limits.maxSchemaDepth) {
56
+ this.error('SCHEMA_LIMIT_EXCEEDED', schemaPath, `schema is nested deeper than ${this.limits.maxSchemaDepth} levels`);
57
+ return false;
58
+ }
59
+ if (typeof schema === 'boolean') {
60
+ return schema;
61
+ }
62
+ if (schema === null || typeof schema !== 'object' || Array.isArray(schema)) {
63
+ this.error('INVALID_SCHEMA', schemaPath, 'a schema must be an object or a boolean');
64
+ return false;
65
+ }
66
+ const source = schema;
67
+ const node = { schemaPath, hasConst: false };
68
+ for (const key of Object.keys(source)) {
69
+ if (!CONSTRAINT_KEYWORDS.has(key) && !ANNOTATION_KEYWORDS.has(key)) {
70
+ this.error('UNSUPPORTED_KEYWORD', pointerJoin(schemaPath, key), `keyword "${key}" is not supported`);
71
+ }
72
+ }
73
+ const at = (keyword) => pointerJoin(schemaPath, keyword);
74
+ const has = (keyword) => Object.prototype.hasOwnProperty.call(source, keyword);
75
+ if (has('type')) {
76
+ const raw = source.type;
77
+ const list = typeof raw === 'string' ? [raw] : raw;
78
+ if (!Array.isArray(list)
79
+ || list.length === 0
80
+ || list.some((name) => typeof name !== 'string' || !TYPE_NAMES.has(name))
81
+ || new Set(list).size !== list.length) {
82
+ this.error('INVALID_KEYWORD_VALUE', at('type'), 'type must be a type name or a non-empty array of unique type names');
83
+ }
84
+ else {
85
+ node.types = Object.freeze([...list]);
86
+ }
87
+ }
88
+ if (has('properties')) {
89
+ const raw = source.properties;
90
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
91
+ this.error('INVALID_KEYWORD_VALUE', at('properties'), 'properties must be an object of schemas');
92
+ }
93
+ else {
94
+ const entries = [];
95
+ for (const [name, child] of Object.entries(raw)) {
96
+ entries.push(Object.freeze([name, this.compile(child, pointerJoin(at('properties'), name), depth + 1)]));
97
+ }
98
+ node.properties = Object.freeze(entries);
99
+ }
100
+ }
101
+ if (has('required')) {
102
+ const raw = source.required;
103
+ if (!Array.isArray(raw) || raw.some((name) => typeof name !== 'string') || new Set(raw).size !== raw.length) {
104
+ this.error('INVALID_KEYWORD_VALUE', at('required'), 'required must be an array of unique strings');
105
+ }
106
+ else {
107
+ node.required = Object.freeze([...raw]);
108
+ }
109
+ }
110
+ if (has('additionalProperties')) {
111
+ node.additionalProperties = this.compile(source.additionalProperties, at('additionalProperties'), depth + 1);
112
+ }
113
+ if (has('enum')) {
114
+ const raw = source.enum;
115
+ if (!Array.isArray(raw) || raw.length === 0) {
116
+ this.error('INVALID_KEYWORD_VALUE', at('enum'), 'enum must be a non-empty array');
117
+ }
118
+ else if (raw.length > this.limits.maxEnumValues) {
119
+ this.error('SCHEMA_LIMIT_EXCEEDED', at('enum'), `enum has more than ${this.limits.maxEnumValues} values`);
120
+ }
121
+ else if (raw.some((value, index) => raw.findIndex((other) => jsonEqual(other, value)) !== index)) {
122
+ this.error('INVALID_KEYWORD_VALUE', at('enum'), 'enum values must be unique');
123
+ }
124
+ else {
125
+ node.enumValues = freezeJsonCopy(raw);
126
+ }
127
+ }
128
+ if (has('const')) {
129
+ node.hasConst = true;
130
+ node.constValue = freezeJsonCopy(source.const);
131
+ }
132
+ if (has('items')) {
133
+ if (Array.isArray(source.items)) {
134
+ this.error('UNSUPPORTED_KEYWORD', at('items'), 'the tuple form of items is not supported');
135
+ }
136
+ else {
137
+ node.items = this.compile(source.items, at('items'), depth + 1);
138
+ }
139
+ }
140
+ for (const keyword of ['minItems', 'maxItems', 'minLength', 'maxLength']) {
141
+ if (has(keyword)) {
142
+ if (!isNonNegativeInteger(source[keyword])) {
143
+ this.error('INVALID_KEYWORD_VALUE', at(keyword), `${keyword} must be a non-negative integer`);
144
+ }
145
+ else {
146
+ node[keyword] = source[keyword];
147
+ }
148
+ }
149
+ }
150
+ for (const keyword of ['minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum']) {
151
+ if (has(keyword)) {
152
+ if (!isFiniteNumber(source[keyword])) {
153
+ this.error('INVALID_KEYWORD_VALUE', at(keyword), `${keyword} must be a finite number`);
154
+ }
155
+ else {
156
+ node[keyword] = source[keyword];
157
+ }
158
+ }
159
+ }
160
+ if (has('pattern')) {
161
+ const raw = source.pattern;
162
+ if (typeof raw !== 'string') {
163
+ this.error('INVALID_KEYWORD_VALUE', at('pattern'), 'pattern must be a string');
164
+ }
165
+ else {
166
+ const screened = screenPattern(raw, this.limits.maxPatternLength);
167
+ if (screened.ok) {
168
+ node.pattern = screened.regex;
169
+ }
170
+ else {
171
+ this.error(screened.code, at('pattern'), screened.reason);
172
+ }
173
+ }
174
+ }
175
+ if (has('oneOf')) {
176
+ const raw = source.oneOf;
177
+ if (!Array.isArray(raw) || raw.length === 0) {
178
+ this.error('INVALID_KEYWORD_VALUE', at('oneOf'), 'oneOf must be a non-empty array of schemas');
179
+ }
180
+ else {
181
+ node.oneOf = Object.freeze(raw.map((child, index) => this.compile(child, pointerJoin(at('oneOf'), index), depth + 1)));
182
+ }
183
+ }
184
+ return Object.freeze(node);
185
+ }
186
+ error(code, schemaPath, message) {
187
+ this.errors.push(Object.freeze({ code, schemaPath, message }));
188
+ }
189
+ }
190
+ /** Collects violations up to the configured cap. */
191
+ class ViolationSink {
192
+ max;
193
+ violations = [];
194
+ full = false;
195
+ constructor(max) {
196
+ this.max = max;
197
+ }
198
+ add(instancePath, schemaPath, keyword, message) {
199
+ if (this.full) {
200
+ return;
201
+ }
202
+ if (this.violations.length === this.max) {
203
+ this.full = true;
204
+ this.violations.push({ instancePath: '', schemaPath: '', keyword: 'limit', message: `more than ${this.max} violations; the rest were omitted` });
205
+ return;
206
+ }
207
+ this.violations.push({ instancePath, schemaPath, keyword, message });
208
+ }
209
+ }
210
+ function matchesType(value, types) {
211
+ const actual = jsonTypeOf(value);
212
+ return types.some((name) => name === actual || (name === 'number' && actual === 'integer'));
213
+ }
214
+ function validateNode(node, value, instancePath, schemaPath, limits, sink) {
215
+ if (node === true) {
216
+ return;
217
+ }
218
+ if (node === false) {
219
+ sink.add(instancePath, schemaPath, 'type', 'no value is allowed here');
220
+ return;
221
+ }
222
+ const at = (keyword) => pointerJoin(node.schemaPath, keyword);
223
+ if (node.types !== undefined && !matchesType(value, node.types)) {
224
+ sink.add(instancePath, at('type'), 'type', `must be of type ${node.types.join(' or ')}`);
225
+ }
226
+ if (node.enumValues !== undefined && !node.enumValues.some((candidate) => jsonEqual(candidate, value))) {
227
+ sink.add(instancePath, at('enum'), 'enum', 'must be one of the enumerated values');
228
+ }
229
+ if (node.hasConst && !jsonEqual(node.constValue, value)) {
230
+ sink.add(instancePath, at('const'), 'const', 'must equal the constant value');
231
+ }
232
+ if (typeof value === 'number') {
233
+ if (node.minimum !== undefined && value < node.minimum) {
234
+ sink.add(instancePath, at('minimum'), 'minimum', `must be >= ${node.minimum}`);
235
+ }
236
+ if (node.maximum !== undefined && value > node.maximum) {
237
+ sink.add(instancePath, at('maximum'), 'maximum', `must be <= ${node.maximum}`);
238
+ }
239
+ if (node.exclusiveMinimum !== undefined && value <= node.exclusiveMinimum) {
240
+ sink.add(instancePath, at('exclusiveMinimum'), 'exclusiveMinimum', `must be > ${node.exclusiveMinimum}`);
241
+ }
242
+ if (node.exclusiveMaximum !== undefined && value >= node.exclusiveMaximum) {
243
+ sink.add(instancePath, at('exclusiveMaximum'), 'exclusiveMaximum', `must be < ${node.exclusiveMaximum}`);
244
+ }
245
+ }
246
+ if (typeof value === 'string') {
247
+ const needsLength = node.minLength !== undefined || node.maxLength !== undefined || node.pattern !== undefined;
248
+ const length = needsLength ? codePointLength(value) : 0;
249
+ if (node.minLength !== undefined && length < node.minLength) {
250
+ sink.add(instancePath, at('minLength'), 'minLength', `must have at least ${node.minLength} characters`);
251
+ }
252
+ if (node.maxLength !== undefined && length > node.maxLength) {
253
+ sink.add(instancePath, at('maxLength'), 'maxLength', `must have at most ${node.maxLength} characters`);
254
+ }
255
+ if (node.pattern !== undefined) {
256
+ if (length > limits.maxPatternInputLength) {
257
+ sink.add(instancePath, at('pattern'), 'limit', `strings longer than ${limits.maxPatternInputLength} characters are not matched against pattern`);
258
+ }
259
+ else if (!node.pattern.test(value)) {
260
+ sink.add(instancePath, at('pattern'), 'pattern', 'must match the pattern');
261
+ }
262
+ }
263
+ }
264
+ if (Array.isArray(value)) {
265
+ if (node.minItems !== undefined && value.length < node.minItems) {
266
+ sink.add(instancePath, at('minItems'), 'minItems', `must have at least ${node.minItems} items`);
267
+ }
268
+ if (node.maxItems !== undefined && value.length > node.maxItems) {
269
+ sink.add(instancePath, at('maxItems'), 'maxItems', `must have at most ${node.maxItems} items`);
270
+ }
271
+ if (node.items !== undefined) {
272
+ for (let index = 0; index < value.length && !sink.full; index += 1) {
273
+ validateNode(node.items, value[index], pointerJoin(instancePath, index), at('items'), limits, sink);
274
+ }
275
+ }
276
+ }
277
+ else if (value !== null && typeof value === 'object') {
278
+ const record = value;
279
+ const own = (key) => Object.prototype.hasOwnProperty.call(record, key);
280
+ for (const name of node.required ?? []) {
281
+ if (!own(name)) {
282
+ sink.add(instancePath, at('required'), 'required', `must have required property "${name}"`);
283
+ }
284
+ }
285
+ const declared = new Set();
286
+ for (const [name, child] of node.properties ?? []) {
287
+ declared.add(name);
288
+ if (own(name) && !sink.full) {
289
+ validateNode(child, record[name], pointerJoin(instancePath, name), pointerJoin(at('properties'), name), limits, sink);
290
+ }
291
+ }
292
+ if (node.additionalProperties !== undefined && node.additionalProperties !== true) {
293
+ const extras = Object.keys(record).filter((key) => !declared.has(key)).sort();
294
+ for (const key of extras) {
295
+ if (sink.full) {
296
+ break;
297
+ }
298
+ if (node.additionalProperties === false) {
299
+ sink.add(pointerJoin(instancePath, key), at('additionalProperties'), 'additionalProperties', 'additional properties are not allowed');
300
+ }
301
+ else {
302
+ validateNode(node.additionalProperties, record[key], pointerJoin(instancePath, key), at('additionalProperties'), limits, sink);
303
+ }
304
+ }
305
+ }
306
+ }
307
+ if (node.oneOf !== undefined && !sink.full) {
308
+ let matches = 0;
309
+ for (const [index, branch] of node.oneOf.entries()) {
310
+ const probe = new ViolationSink(1);
311
+ validateNode(branch, value, instancePath, pointerJoin(at('oneOf'), index), limits, probe);
312
+ if (probe.violations.length === 0) {
313
+ matches += 1;
314
+ }
315
+ }
316
+ if (matches !== 1) {
317
+ sink.add(instancePath, at('oneOf'), 'oneOf', `must match exactly one schema in oneOf (matched ${matches} of ${node.oneOf.length})`);
318
+ }
319
+ }
320
+ }
321
+ /**
322
+ * Compiles an untrusted JSON Schema into a frozen, reusable validator.
323
+ *
324
+ * @param schema - Schema to compile. It is copied; later changes to it have no effect.
325
+ * @param limits - Optional overrides of {@link DEFAULT_JSON_SCHEMA_LIMITS}.
326
+ */
327
+ export function compileJsonSchema(schema, limits) {
328
+ try {
329
+ const resolved = resolveLimits(limits);
330
+ if (typeof resolved === 'string') {
331
+ return { ok: false, errors: [{ code: 'INVALID_SCHEMA', schemaPath: '', message: resolved }] };
332
+ }
333
+ const shape = inspectJsonValue(schema, {
334
+ maxDepth: resolved.maxSchemaDepth * 4,
335
+ maxNodes: resolved.maxSchemaNodes * 64,
336
+ });
337
+ if (shape !== null) {
338
+ return {
339
+ ok: false,
340
+ errors: [{
341
+ code: shape.keyword === 'limit' ? 'SCHEMA_LIMIT_EXCEEDED' : 'INVALID_SCHEMA',
342
+ schemaPath: shape.instancePath,
343
+ message: `schema is not plain JSON: ${shape.message}`,
344
+ }],
345
+ };
346
+ }
347
+ const compiler = new SchemaCompiler(resolved);
348
+ const root = compiler.compile(schema, '', 0);
349
+ if (compiler.errors.length > 0) {
350
+ return { ok: false, errors: Object.freeze([...compiler.errors]) };
351
+ }
352
+ const compiled = Object.freeze({
353
+ validate(value) {
354
+ try {
355
+ const shapeProblem = inspectJsonValue(value, {
356
+ maxDepth: resolved.maxValueDepth,
357
+ maxNodes: resolved.maxValueNodes,
358
+ });
359
+ if (shapeProblem !== null) {
360
+ return { valid: false, violations: Object.freeze([Object.freeze(shapeProblem)]) };
361
+ }
362
+ const sink = new ViolationSink(resolved.maxViolations);
363
+ validateNode(root, value, '', '', resolved, sink);
364
+ return sink.violations.length === 0
365
+ ? { valid: true }
366
+ : { valid: false, violations: Object.freeze(sink.violations.map((violation) => Object.freeze(violation))) };
367
+ }
368
+ catch {
369
+ return {
370
+ valid: false,
371
+ violations: Object.freeze([Object.freeze({
372
+ instancePath: '',
373
+ schemaPath: '',
374
+ keyword: 'limit',
375
+ message: 'internal validation error',
376
+ })]),
377
+ };
378
+ }
379
+ },
380
+ });
381
+ return { ok: true, schema: compiled };
382
+ }
383
+ catch {
384
+ return { ok: false, errors: [{ code: 'INVALID_SCHEMA', schemaPath: '', message: 'schema could not be compiled' }] };
385
+ }
386
+ }
@@ -0,0 +1,3 @@
1
+ export { compileJsonSchema } from './compile.js';
2
+ export { DEFAULT_JSON_SCHEMA_LIMITS } from './types.js';
3
+ export type { CompiledJsonSchema, JsonSchemaCompileResult, JsonSchemaDefinitionError, JsonSchemaDefinitionErrorCode, JsonSchemaKeyword, JsonSchemaLimits, JsonSchemaTypeName, JsonSchemaValidationResult, JsonSchemaViolation, } from './types.js';
@@ -0,0 +1,2 @@
1
+ export { compileJsonSchema } from './compile.js';
2
+ export { DEFAULT_JSON_SCHEMA_LIMITS } from './types.js';
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Plain-JSON value helpers: shape inspection, deep equality, cloning, and
3
+ * JSON Pointer encoding. Pure ECMAScript.
4
+ */
5
+ import type { JsonSchemaTypeName, JsonSchemaViolation } from './types.js';
6
+ /** Encodes one JSON Pointer reference token (RFC 6901). */
7
+ export declare function pointerToken(token: string | number): string;
8
+ /** Appends a token to a JSON Pointer. */
9
+ export declare function pointerJoin(base: string, token: string | number): string;
10
+ /** Length in Unicode code points, as JSON Schema counts string length. */
11
+ export declare function codePointLength(value: string): number;
12
+ /** JSON type of a value already known to be plain JSON. */
13
+ export declare function jsonTypeOf(value: unknown): Exclude<JsonSchemaTypeName, 'number'> | 'number';
14
+ export interface JsonInspectionLimits {
15
+ readonly maxDepth: number;
16
+ readonly maxNodes: number;
17
+ }
18
+ /**
19
+ * Checks that a value is plain JSON: it would survive `JSON.stringify` then
20
+ * `JSON.parse` unchanged. Never invokes user code (accessors are rejected
21
+ * without being read). Stops at the first problem.
22
+ *
23
+ * @returns `null` when the value is plain JSON, otherwise one violation.
24
+ */
25
+ export declare function inspectJsonValue(value: unknown, limits: JsonInspectionLimits): JsonSchemaViolation | null;
26
+ /** Deep equality for plain JSON values (key order ignored, `-0` equals `0`). */
27
+ export declare function jsonEqual(left: unknown, right: unknown): boolean;
28
+ /** Deep-frozen copy of a plain JSON value. */
29
+ export declare function freezeJsonCopy<T>(value: T): T;
@@ -0,0 +1,190 @@
1
+ /** Encodes one JSON Pointer reference token (RFC 6901). */
2
+ export function pointerToken(token) {
3
+ return String(token).replace(/~/gu, '~0').replace(/\//gu, '~1');
4
+ }
5
+ /** Appends a token to a JSON Pointer. */
6
+ export function pointerJoin(base, token) {
7
+ return `${base}/${pointerToken(token)}`;
8
+ }
9
+ /** Length in Unicode code points, as JSON Schema counts string length. */
10
+ export function codePointLength(value) {
11
+ let count = 0;
12
+ for (const _ of value) {
13
+ count += 1;
14
+ }
15
+ return count;
16
+ }
17
+ function isPlainObject(value) {
18
+ const prototype = Object.getPrototypeOf(value);
19
+ return prototype === Object.prototype || prototype === null;
20
+ }
21
+ /** JSON type of a value already known to be plain JSON. */
22
+ export function jsonTypeOf(value) {
23
+ if (value === null) {
24
+ return 'null';
25
+ }
26
+ if (typeof value === 'boolean') {
27
+ return 'boolean';
28
+ }
29
+ if (typeof value === 'number') {
30
+ return Number.isInteger(value) ? 'integer' : 'number';
31
+ }
32
+ if (typeof value === 'string') {
33
+ return 'string';
34
+ }
35
+ if (Array.isArray(value)) {
36
+ return 'array';
37
+ }
38
+ return 'object';
39
+ }
40
+ /**
41
+ * Checks that a value is plain JSON: it would survive `JSON.stringify` then
42
+ * `JSON.parse` unchanged. Never invokes user code (accessors are rejected
43
+ * without being read). Stops at the first problem.
44
+ *
45
+ * @returns `null` when the value is plain JSON, otherwise one violation.
46
+ */
47
+ export function inspectJsonValue(value, limits) {
48
+ let nodes = 0;
49
+ const ancestors = [];
50
+ const fail = (instancePath, keyword, message) => ({ instancePath, schemaPath: '', keyword, message });
51
+ const visit = (current, path, depth) => {
52
+ nodes += 1;
53
+ if (nodes > limits.maxNodes) {
54
+ return fail(path, 'limit', `value has more than ${limits.maxNodes} nodes`);
55
+ }
56
+ if (depth > limits.maxDepth) {
57
+ return fail(path, 'limit', `value is nested deeper than ${limits.maxDepth} levels`);
58
+ }
59
+ switch (typeof current) {
60
+ case 'string':
61
+ case 'boolean':
62
+ return null;
63
+ case 'number':
64
+ return Number.isFinite(current) ? null : fail(path, 'json', 'number must be finite');
65
+ case 'undefined':
66
+ return fail(path, 'json', 'undefined is not a JSON value');
67
+ case 'bigint':
68
+ return fail(path, 'json', 'bigint is not a JSON value');
69
+ case 'symbol':
70
+ return fail(path, 'json', 'symbol is not a JSON value');
71
+ case 'function':
72
+ return fail(path, 'json', 'function is not a JSON value');
73
+ default:
74
+ break;
75
+ }
76
+ if (current === null) {
77
+ return null;
78
+ }
79
+ const object = current;
80
+ if (ancestors.includes(object)) {
81
+ return fail(path, 'json', 'value contains a cycle');
82
+ }
83
+ if (Object.getOwnPropertySymbols(object).length > 0) {
84
+ return fail(path, 'json', 'symbol-keyed properties are not JSON');
85
+ }
86
+ ancestors.push(object);
87
+ try {
88
+ if (Array.isArray(object)) {
89
+ if (Object.getPrototypeOf(object) !== Array.prototype) {
90
+ return fail(path, 'json', 'array subclasses are not JSON');
91
+ }
92
+ const keys = Object.getOwnPropertyNames(object);
93
+ if (keys.length !== object.length + 1) {
94
+ return fail(path, 'json', 'arrays must be dense with no extra properties');
95
+ }
96
+ for (let index = 0; index < object.length; index += 1) {
97
+ const descriptor = Object.getOwnPropertyDescriptor(object, index);
98
+ if (descriptor === undefined) {
99
+ return fail(path, 'json', 'arrays must be dense with no extra properties');
100
+ }
101
+ if (!('value' in descriptor)) {
102
+ return fail(pointerJoin(path, index), 'json', 'accessor properties are not JSON');
103
+ }
104
+ const problem = visit(descriptor.value, pointerJoin(path, index), depth + 1);
105
+ if (problem !== null) {
106
+ return problem;
107
+ }
108
+ }
109
+ return null;
110
+ }
111
+ if (!isPlainObject(object)) {
112
+ return fail(path, 'json', 'only plain objects are JSON objects');
113
+ }
114
+ for (const key of Object.getOwnPropertyNames(object)) {
115
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
116
+ if (descriptor === undefined) {
117
+ continue;
118
+ }
119
+ if (!descriptor.enumerable) {
120
+ return fail(pointerJoin(path, key), 'json', 'non-enumerable properties are not JSON');
121
+ }
122
+ if (!('value' in descriptor)) {
123
+ return fail(pointerJoin(path, key), 'json', 'accessor properties are not JSON');
124
+ }
125
+ const problem = visit(descriptor.value, pointerJoin(path, key), depth + 1);
126
+ if (problem !== null) {
127
+ return problem;
128
+ }
129
+ }
130
+ return null;
131
+ }
132
+ finally {
133
+ ancestors.pop();
134
+ }
135
+ };
136
+ return visit(value, '', 0);
137
+ }
138
+ /** Deep equality for plain JSON values (key order ignored, `-0` equals `0`). */
139
+ export function jsonEqual(left, right) {
140
+ if (left === right) {
141
+ return true;
142
+ }
143
+ if (left === null || right === null || typeof left !== 'object' || typeof right !== 'object') {
144
+ return false;
145
+ }
146
+ if (Array.isArray(left) !== Array.isArray(right)) {
147
+ return false;
148
+ }
149
+ if (Array.isArray(left) && Array.isArray(right)) {
150
+ if (left.length !== right.length) {
151
+ return false;
152
+ }
153
+ for (let index = 0; index < left.length; index += 1) {
154
+ if (!jsonEqual(left[index], right[index])) {
155
+ return false;
156
+ }
157
+ }
158
+ return true;
159
+ }
160
+ const leftRecord = left;
161
+ const rightRecord = right;
162
+ const leftKeys = Object.keys(leftRecord);
163
+ const rightKeys = Object.keys(rightRecord);
164
+ if (leftKeys.length !== rightKeys.length) {
165
+ return false;
166
+ }
167
+ for (const key of leftKeys) {
168
+ if (!Object.prototype.hasOwnProperty.call(rightRecord, key)) {
169
+ return false;
170
+ }
171
+ if (!jsonEqual(leftRecord[key], rightRecord[key])) {
172
+ return false;
173
+ }
174
+ }
175
+ return true;
176
+ }
177
+ /** Deep-frozen copy of a plain JSON value. */
178
+ export function freezeJsonCopy(value) {
179
+ if (value === null || typeof value !== 'object') {
180
+ return value;
181
+ }
182
+ if (Array.isArray(value)) {
183
+ return Object.freeze(value.map((item) => freezeJsonCopy(item)));
184
+ }
185
+ const copy = {};
186
+ for (const [key, item] of Object.entries(value)) {
187
+ copy[key] = freezeJsonCopy(item);
188
+ }
189
+ return Object.freeze(copy);
190
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * `pattern` safety screen (decision U1-D2).
3
+ *
4
+ * ECMAScript `RegExp` has no time limit, so a hostile pattern can hang the
5
+ * kernel thread. Patterns are screened at compile time for the shapes behind
6
+ * catastrophic backtracking, then compiled once with the `u` flag. Combined
7
+ * with the input-length cap applied at validation time, this bounds the risk.
8
+ * It is a conservative heuristic, not a proof: some safe patterns (for example
9
+ * `(ab|cd)*`) are rejected too.
10
+ */
11
+ export type PatternScreenResult = {
12
+ readonly ok: true;
13
+ readonly regex: RegExp;
14
+ } | {
15
+ readonly ok: false;
16
+ readonly code: 'UNSAFE_PATTERN' | 'INVALID_KEYWORD_VALUE';
17
+ readonly reason: string;
18
+ };
19
+ /**
20
+ * Screens and compiles a JSON Schema `pattern`.
21
+ *
22
+ * Rejected as `UNSAFE_PATTERN`:
23
+ * - backreferences (`\1`…`\9`, `\k<name>`);
24
+ * - lookbehind (`(?<=`, `(?<!`);
25
+ * - a repeating quantifier applied to a group whose body contains a repeating
26
+ * quantifier (`(a+)+`, `(.*)*`) or an alternation (`(a|aa)*`).
27
+ */
28
+ export declare function screenPattern(source: string, maxLength: number): PatternScreenResult;