(buffer: Buffer, context?: string)
| 93 | } |
| 94 | }; |
| 95 | const decompressPackedPayload = async (buffer: Buffer, context?: string): Promise<Buffer> => { |
| 96 | const offsets = [] as number[]; |
| 97 | const addOffset = (offset: number) => { |
| 98 | if (offset < 0 || offset >= buffer.length) return; |
| 99 | if (!offsets.includes(offset)) offsets.push(offset); |
| 100 | }; |
| 101 | addOffset(4); |
| 102 | addOffset(0); |
| 103 | for (let i = 0; i <= Math.min(32, buffer.length - 4); i++) { |
| 104 | if (buffer.subarray(i, i + 4).equals(zstdFrameMagic)) addOffset(i); |
| 105 | if (buffer.subarray(i, i + 4).equals(lz4FrameMagic)) addOffset(i); |
| 106 | } |
| 107 | addOffset(8); |
| 108 | addOffset(12); |
| 109 | addOffset(16); |
| 110 | let lastError: unknown; |
| 111 | for (const offset of offsets) { |
| 112 | const payload = buffer.subarray(offset); |
| 113 | if (payload.length < 4) continue; |
| 114 | if (!payload.subarray(0, 4).equals(zstdFrameMagic)) continue; |
| 115 | try { |
| 116 | return await tryZstdDecompress(payload); |
| 117 | } catch (error) { |
| 118 | lastError = error; |
| 119 | } |
| 120 | } |
| 121 | for (const offset of offsets) { |
| 122 | const payload = buffer.subarray(offset); |
| 123 | if (payload.length < 4) continue; |
| 124 | if (!payload.subarray(0, 4).equals(lz4FrameMagic)) continue; |
| 125 | try { |
| 126 | return await tryLz4FrameDecompress(payload); |
| 127 | } catch (error) { |
| 128 | lastError = error; |
| 129 | } |
| 130 | } |
| 131 | for (const offset of offsets) { |
| 132 | const payload = buffer.subarray(offset); |
| 133 | if (payload.length < 4) continue; |
| 134 | try { |
| 135 | return await tryZstdDecompress(payload); |
| 136 | } catch (error) { |
| 137 | lastError = error; |
| 138 | } |
| 139 | } |
| 140 | for (const offset of offsets) { |
| 141 | const payload = buffer.subarray(offset); |
| 142 | if (payload.length < 2) continue; |
| 143 | try { |
| 144 | return tryZlibDecompress(payload); |
| 145 | } catch (error) { |
| 146 | lastError = error; |
| 147 | } |
| 148 | } |
| 149 | const contextSuffix = context ? ` (${context})` : ""; |
| 150 | throw new Error( |
| 151 | `Failed to decompress packed payload${contextSuffix}: ${ |
| 152 | lastError instanceof Error ? lastError.message : String(lastError) |
no test coverage detected