| 121 | |
| 122 | // Behaviour that is specific to particular versions of the format. |
| 123 | private createCompatAdapter(formatVersion: FormatVersion): CompatAdapter { |
| 124 | if (formatVersion === B1) { |
| 125 | // format version b1 |
| 126 | return new (class implements CompatAdapter { |
| 127 | formatVersion: FormatVersion = B1; |
| 128 | private index: Map<string, [Uint8Array, number, number]> = new Map(); |
| 129 | private primaryURL: string | null = null; |
| 130 | |
| 131 | constructor(private bundleBuilder: BundleBuilder) {} |
| 132 | |
| 133 | onCreateBundle(): void { |
| 134 | if (this.primaryURL === null) { |
| 135 | throw new Error('Primary URL is not set'); |
| 136 | } |
| 137 | if (!this.index.has(this.primaryURL)) { |
| 138 | throw new Error( |
| 139 | `Exchange for primary URL (${this.primaryURL}) does not exist` |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | setPrimaryURL(url: string): BundleBuilder { |
| 145 | if (this.primaryURL !== null) { |
| 146 | throw new Error('Primary URL is already set'); |
| 147 | } |
| 148 | validateExchangeURL(url); |
| 149 | this.primaryURL = url; |
| 150 | return this.bundleBuilder; |
| 151 | } |
| 152 | |
| 153 | setManifestURL(url: string): BundleBuilder { |
| 154 | validateExchangeURL(url); |
| 155 | this.bundleBuilder.addSection('manifest', url); |
| 156 | return this.bundleBuilder; |
| 157 | } |
| 158 | |
| 159 | setIndexEntry(url: string, responseLength: number): void { |
| 160 | this.index.set(url, [ |
| 161 | new Uint8Array(0), // variants-value |
| 162 | this.bundleBuilder.currentResponsesOffset, |
| 163 | responseLength, |
| 164 | ]); |
| 165 | } |
| 166 | |
| 167 | updateIndexValues(responsesHeaderSize: number): Map<string, any[]> { |
| 168 | for (const value of this.index.values()) { |
| 169 | value[1] += responsesHeaderSize; |
| 170 | } |
| 171 | return this.index; |
| 172 | } |
| 173 | |
| 174 | createTopLevel(): unknown { |
| 175 | const sectionLengths: Array<string | number> = []; |
| 176 | for (const s of this.bundleBuilder.sectionLengths) { |
| 177 | sectionLengths.push(s.name, s.length); |
| 178 | } |
| 179 | return [ |
| 180 | byteString('🌐📦'), |