(ref: string, pointer: string, ctx: ReadContext)
| 253 | ) => BaseRoot | undefined |
| 254 | |
| 255 | const readRef = (ref: string, pointer: string, ctx: ReadContext): BaseRoot => { |
| 256 | const target = resolveRef(ref, pointer, ctx) |
| 257 | // refs within the document's own resource are keyed as written |
| 258 | const key = |
| 259 | target.resource.uri === documentUri ? |
| 260 | `#${target.fragment}` |
| 261 | : `${target.resource.uri}#${target.fragment}` |
| 262 | if (ctx.currentRef !== undefined) { |
| 263 | const edges = (ctx.shallowEdgesByRef[ctx.currentRef] ??= { |
| 264 | plain: new Set(), |
| 265 | pred: new Set() |
| 266 | }) |
| 267 | if (ctx.inPredicate > ctx.refInPredicate) edges.pred.add(key) |
| 268 | else edges.plain.add(key) |
| 269 | } |
| 270 | if (hasOwn(ctx.rootsByRef, key)) { |
| 271 | if (hasShallowPredicateCycle(key, ctx)) |
| 272 | return throwParseError(writeShallowCycleMessage(ref, pointer)) |
| 273 | const targets = ctx.shallowBackEdgesByRef[key] |
| 274 | if (targets?.size) { |
| 275 | for (const active of ctx.shallowRefPredDepth.keys()) { |
| 276 | const activeTargets = (ctx.shallowBackEdgesByRef[active] ??= new Set()) |
| 277 | for (const t of targets) activeTargets.add(t) |
| 278 | } |
| 279 | } |
| 280 | return ctx.rootsByRef[key] |
| 281 | } |
| 282 | // a ref within its own target is a cycle, resolved once that's read |
| 283 | if (ctx.refsBeingRead.includes(key)) { |
| 284 | const entryDepth = ctx.shallowRefPredDepth.get(key) |
| 285 | if (entryDepth !== undefined && ctx.inPredicate > entryDepth) |
| 286 | return throwParseError(writeShallowCycleMessage(ref, pointer)) |
| 287 | for (const active of ctx.shallowRefPredDepth.keys()) |
| 288 | (ctx.shallowBackEdgesByRef[active] ??= new Set()).add(key) |
| 289 | ctx.cyclesByRef[key] ??= jsonSchemaScope().openSynthetic( |
| 290 | cycleNameOf(target, key, ref, pointer, ctx) |
| 291 | ) |
| 292 | return jsonSchemaScope().lazilyResolve( |
| 293 | () => ctx.rootsByRef[key], |
| 294 | ctx.cyclesByRef[key].id |
| 295 | ) |
| 296 | } |
| 297 | const enclosing = ctx.resource |
| 298 | const prevReading = ctx.reading |
| 299 | const prevRef = ctx.currentRef |
| 300 | const prevRefInPredicate = ctx.refInPredicate |
| 301 | setResource(ctx, target.resource) |
| 302 | ctx.reading = new Set() |
| 303 | ctx.currentRef = key |
| 304 | ctx.refInPredicate = ctx.inPredicate |
| 305 | ctx.refsBeingRead.push(key) |
| 306 | ctx.shallowRefPredDepth.set(key, ctx.inPredicate) |
| 307 | try { |
| 308 | const root = read(target.schema, ref, ctx) |
| 309 | return (ctx.rootsByRef[key] = root) |
| 310 | } finally { |
| 311 | ctx.shallowRefPredDepth.delete(key) |
| 312 | ctx.refsBeingRead.pop() |
no test coverage detected