| 127 | |
| 128 | // Behaviour that is specific to particular versions of the format. |
| 129 | private createCompatAdapter(formatVersion: FormatVersion): CompatAdapter { |
| 130 | if (formatVersion === B1) { |
| 131 | // format version b1 |
| 132 | return new (class implements CompatAdapter { |
| 133 | wbnLength: number = 6; |
| 134 | |
| 135 | constructor(private bundle: Bundle) {} |
| 136 | |
| 137 | getResponse(url: string): Response { |
| 138 | const indexEntry = asArray(this.bundle.indexSection.get(url)); |
| 139 | if (!indexEntry) { |
| 140 | throw new Error('No entry for ' + url); |
| 141 | } |
| 142 | const [variants, offset, length] = indexEntry; |
| 143 | if (asBytestring(variants).length !== 0) { |
| 144 | throw new Error('Variants are not supported'); |
| 145 | } |
| 146 | if (indexEntry.length !== 3) { |
| 147 | throw new Error('Unexpected length of index entry for ' + url); |
| 148 | } |
| 149 | const resp = this.bundle.responses[asNumber(offset)]; |
| 150 | if (!resp) { |
| 151 | throw new Error(`Response for ${url} is not found (broken index)`); |
| 152 | } |
| 153 | return resp; |
| 154 | } |
| 155 | |
| 156 | destructureBundle(wbn: unknown[]): [any, any, any, any, any, any] { |
| 157 | return [wbn[0], wbn[1], wbn[2], wbn[3], wbn[4], wbn[5]]; |
| 158 | } |
| 159 | })(this); |
| 160 | } else { |
| 161 | // format version b2 |
| 162 | return new (class implements CompatAdapter { |
| 163 | wbnLength: number = 5; |
| 164 | |
| 165 | constructor(private bundle: Bundle) {} |
| 166 | |
| 167 | getResponse(url: string): Response { |
| 168 | const indexEntry = asArray(this.bundle.indexSection.get(url)); |
| 169 | if (!indexEntry) { |
| 170 | throw new Error('No entry for ' + url); |
| 171 | } |
| 172 | const [offset, length] = indexEntry; |
| 173 | if (indexEntry.length !== 2) { |
| 174 | throw new Error('Unexpected length of index entry for ' + url); |
| 175 | } |
| 176 | const resp = this.bundle.responses[asNumber(offset)]; |
| 177 | if (!resp) { |
| 178 | throw new Error(`Response for ${url} is not found (broken index)`); |
| 179 | } |
| 180 | return resp; |
| 181 | } |
| 182 | |
| 183 | destructureBundle(wbn: unknown[]): [any, any, any, any, any, any] { |
| 184 | return [wbn[0], wbn[1], null, wbn[2], wbn[3], wbn[4]]; |
| 185 | } |
| 186 | })(this); |