Files
simplex-chat/packages/simplex-chat-nodejs/tests/zip.ts
T
sh f65c2da587 nodejs: lazy per-backend libsimplex download (#7543)
* ci: drop dlltool from nodejs libs release

* nodejs: download libsimplex into the user cache

* nodejs: load libsimplex at runtime per backend

* support bot: select db backend with --db

* python: match backend switch error text

* nodejs: download prebuilt add-on at runtime

* ci: build nodejs add-on for libs release

* calculator bot: load libs and add-on in image

* support bot: document add-on override

* nodejs: keep worker test output free of colors

* nodejs: verify sha-256 of downloaded assets

* python: pin core libs release with sha-256

* ci: add packages release script

* ci: release nodejs add-on and open release PR

* ci: publish packages after release PR

* nodejs: keep npm from building the add-on

* calculator bot: build the add-on in the image

* ci: merge package release workflows

* ci: open release PR when publishing fails

* ci: build the add-on with node-addon-api only

* ci: skip publishing in a called packages workflow

* ci: update package release actions to Node 24

* nodejs: fix receive test timeout

* ci: test packages on pull requests

* nodejs: reuse the database in the close stress test

* ci: keep failed package tests from hanging

* ci: skip preset server tests

* ci: plan package builds from the changes

* ci: test packages with a given libsimplex

* ci: test packages with libsimplex of core changes
2026-10-14 09:53:53 +01:00

48 lines
1.7 KiB
TypeScript

import * as zlib from "zlib"
const LOCAL_FILE_HEADER = 0x04034b50
const CENTRAL_DIRECTORY_HEADER = 0x02014b50
const END_OF_CENTRAL_DIRECTORY = 0x06054b50
const ZIP_VERSION = 20
const LOCAL_HEADER_SIZE = 30
const CENTRAL_ENTRY_SIZE = 46
const END_RECORD_SIZE = 22
export function storedZip(files: {[name: string]: string}): Buffer {
const local: Buffer[] = []
const central: Buffer[] = []
let offset = 0
for (const [name, content] of Object.entries(files)) {
const nameBuf = Buffer.from(name)
const data = Buffer.from(content)
const crc = zlib.crc32(data)
const header = Buffer.alloc(LOCAL_HEADER_SIZE)
header.writeUInt32LE(LOCAL_FILE_HEADER, 0)
header.writeUInt16LE(ZIP_VERSION, 4)
header.writeUInt32LE(crc, 14)
header.writeUInt32LE(data.length, 18)
header.writeUInt32LE(data.length, 22)
header.writeUInt16LE(nameBuf.length, 26)
local.push(header, nameBuf, data)
const entry = Buffer.alloc(CENTRAL_ENTRY_SIZE)
entry.writeUInt32LE(CENTRAL_DIRECTORY_HEADER, 0)
entry.writeUInt16LE(ZIP_VERSION, 4)
entry.writeUInt16LE(ZIP_VERSION, 6)
entry.writeUInt32LE(crc, 16)
entry.writeUInt32LE(data.length, 20)
entry.writeUInt32LE(data.length, 24)
entry.writeUInt16LE(nameBuf.length, 28)
entry.writeUInt32LE(offset, 42)
central.push(entry, nameBuf)
offset += header.length + nameBuf.length + data.length
}
const centralBuf = Buffer.concat(central)
const end = Buffer.alloc(END_RECORD_SIZE)
end.writeUInt32LE(END_OF_CENTRAL_DIRECTORY, 0)
end.writeUInt16LE(Object.keys(files).length, 8)
end.writeUInt16LE(Object.keys(files).length, 10)
end.writeUInt32LE(centralBuf.length, 12)
end.writeUInt32LE(offset, 16)
return Buffer.concat([...local, centralBuf, end])
}