@crombolo/server 0.62.0 → 0.64.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/zip.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  /**
2
- * A minimal zip writer, because a created package is a zip and nothing else
3
- * here needs one (`docs/bundles.md`).
2
+ * A minimal zip writer, because a created package is a zip (`docs/bundles.md`),
3
+ * and a bounded reader, for a schema uploaded in Settings (story 342).
4
4
  *
5
5
  * No dependency: `node:zlib` provides `deflateRawSync` and `crc32`, which is
6
6
  * everything the format needs. Entries are written in the order given, each
7
7
  * stored if deflating does not make it smaller, and timestamps are fixed at the
8
8
  * epoch the format starts from so the same selection zips to the same bytes.
9
9
  */
10
- import { crc32, deflateRawSync } from 'node:zlib'
10
+ import { crc32, deflateRawSync, inflateRawSync } from 'node:zlib'
11
11
 
12
12
  export interface ZipEntry {
13
13
  /** The path inside the archive, `/`-separated and never absolute. */
@@ -150,3 +150,86 @@ export function writeZip(entries: readonly ZipEntry[]): Uint8Array<ArrayBuffer>
150
150
  out.u16(0)
151
151
  return out.concat()
152
152
  }
153
+
154
+ export class ZipError extends Error {}
155
+
156
+ export interface ZipLimits {
157
+ /** The most entries an archive may hold. */
158
+ maxEntries: number
159
+ /** The most bytes one entry may unpack to. */
160
+ maxEntryBytes: number
161
+ /** The most bytes every entry together may unpack to. */
162
+ maxTotalBytes: number
163
+ }
164
+
165
+ /**
166
+ * The files in an archive, read through its central directory as an unzip tool
167
+ * does. Directories are left out. Anything this writer would not write, an
168
+ * encrypted or unknown entry, or one past the limits, is refused rather than
169
+ * guessed at, so an upload can't unpack to more than the limits say.
170
+ */
171
+ export function readZip(archive: Uint8Array, limits: ZipLimits): ZipEntry[] {
172
+ const view = new DataView(archive.buffer, archive.byteOffset, archive.byteLength)
173
+ const decoder = new TextDecoder('utf-8', { fatal: true })
174
+ let end = archive.length - 22
175
+ const floor = Math.max(0, archive.length - 22 - 0xffff)
176
+ while (end >= floor && view.getUint32(end, true) !== END_OF_CENTRAL) end -= 1
177
+ if (end < floor) throw new ZipError('This is not a zip file.')
178
+
179
+ const count = view.getUint16(end + 10, true)
180
+ const centralOffset = view.getUint32(end + 16, true)
181
+ if (count > limits.maxEntries)
182
+ throw new ZipError(`It holds more than ${limits.maxEntries} files.`)
183
+
184
+ const entries: ZipEntry[] = []
185
+ let total = 0
186
+ let cursor = centralOffset
187
+ for (let index = 0; index < count; index += 1) {
188
+ if (cursor + 46 > archive.length || view.getUint32(cursor, true) !== CENTRAL_HEADER)
189
+ throw new ZipError('The zip file is damaged.')
190
+ const flags = view.getUint16(cursor + 8, true)
191
+ const method = view.getUint16(cursor + 10, true)
192
+ const compressedSize = view.getUint32(cursor + 20, true)
193
+ const size = view.getUint32(cursor + 24, true)
194
+ const nameLength = view.getUint16(cursor + 28, true)
195
+ const extraLength = view.getUint16(cursor + 30, true)
196
+ const commentLength = view.getUint16(cursor + 32, true)
197
+ const localOffset = view.getUint32(cursor + 42, true)
198
+ let path: string
199
+ try {
200
+ path = decoder.decode(archive.subarray(cursor + 46, cursor + 46 + nameLength))
201
+ } catch {
202
+ throw new ZipError('A file in the zip has a name that is not readable text.')
203
+ }
204
+ cursor += 46 + nameLength + extraLength + commentLength
205
+ if (path.endsWith('/')) continue
206
+ if (flags & 0x1) throw new ZipError(`"${path}" is encrypted.`)
207
+ if (method !== STORED && method !== DEFLATED)
208
+ throw new ZipError(`"${path}" is compressed in a way this can't read.`)
209
+ if (size > limits.maxEntryBytes) throw new ZipError(`"${path}" is too large.`)
210
+ total += size
211
+ if (total > limits.maxTotalBytes) throw new ZipError('The files in it are too large together.')
212
+
213
+ if (localOffset + 30 > archive.length || view.getUint32(localOffset, true) !== LOCAL_HEADER)
214
+ throw new ZipError('The zip file is damaged.')
215
+ const start =
216
+ localOffset +
217
+ 30 +
218
+ view.getUint16(localOffset + 26, true) +
219
+ view.getUint16(localOffset + 28, true)
220
+ if (start + compressedSize > archive.length) throw new ZipError('The zip file is damaged.')
221
+ const stored = archive.subarray(start, start + compressedSize)
222
+ let bytes: Uint8Array
223
+ try {
224
+ bytes =
225
+ method === STORED
226
+ ? stored.slice()
227
+ : new Uint8Array(inflateRawSync(stored, { maxOutputLength: limits.maxEntryBytes }))
228
+ } catch {
229
+ throw new ZipError(`"${path}" could not be unpacked.`)
230
+ }
231
+ if (bytes.length !== size) throw new ZipError('The zip file is damaged.')
232
+ entries.push({ path, bytes })
233
+ }
234
+ return entries
235
+ }