({ nodes, server_loads, dictionary, matchers })
| 5 | * @returns {import('types').CSRRoute[]} |
| 6 | */ |
| 7 | export function parse({ nodes, server_loads, dictionary, matchers }) { |
| 8 | const layouts_with_server_load = new Set(server_loads); |
| 9 | |
| 10 | return Object.entries(dictionary).map(([id, [leaf, layouts, errors]]) => { |
| 11 | const { pattern, params } = parse_route_id(id); |
| 12 | |
| 13 | /** @type {import('types').CSRRoute} */ |
| 14 | const route = { |
| 15 | id, |
| 16 | /** @param {string} path */ |
| 17 | exec: (path) => { |
| 18 | const match = pattern.exec(path); |
| 19 | if (match) return exec(match, params, matchers); |
| 20 | }, |
| 21 | errors: [1, ...(errors || [])].map((n) => nodes[n]), |
| 22 | layouts: [0, ...(layouts || [])].map(create_layout_loader), |
| 23 | leaf: create_leaf_loader(leaf) |
| 24 | }; |
| 25 | |
| 26 | // bit of a hack, but ensures that layout/error node lists are the same |
| 27 | // length, without which the wrong data will be applied if the route |
| 28 | // manifest looks like `[[a, b], [c,], d]` |
| 29 | route.errors.length = route.layouts.length = Math.max( |
| 30 | route.errors.length, |
| 31 | route.layouts.length |
| 32 | ); |
| 33 | |
| 34 | return route; |
| 35 | }); |
| 36 | |
| 37 | /** |
| 38 | * @param {number} id |
| 39 | * @returns {[boolean, import('types').CSRPageNodeLoader]} |
| 40 | */ |
| 41 | function create_leaf_loader(id) { |
| 42 | // whether or not the route uses the server data is |
| 43 | // encoded using the ones' complement, to save space |
| 44 | const uses_server_data = id < 0; |
| 45 | if (uses_server_data) id = ~id; |
| 46 | return [uses_server_data, nodes[id]]; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param {number | undefined} id |
| 51 | * @returns {[boolean, import('types').CSRPageNodeLoader] | undefined} |
| 52 | */ |
| 53 | function create_layout_loader(id) { |
| 54 | // whether or not the layout uses the server data is |
| 55 | // encoded in the layouts array, to save space |
| 56 | return id === undefined ? id : [layouts_with_server_load.has(id), nodes[id]]; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param {import('types').CSRRouteServer} input |
no test coverage detected