|
| 1 | +import { create_zip } from '../../../packages/shared/src/utils/create-zip/create-zip.ts' |
1 | 2 | import * as fs from 'node:fs' |
2 | 3 | import * as path from 'node:path' |
3 | | -import * as zlib from 'node:zlib' |
4 | 4 | import { fileURLToPath } from 'node:url' |
5 | 5 |
|
6 | 6 | const app_dir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') |
@@ -72,105 +72,6 @@ const resolve_files = () => { |
72 | 72 | return files.map((file) => file.split(path.sep).join('/')) |
73 | 73 | } |
74 | 74 |
|
75 | | -// --- Minimal zip writer --- |
76 | | - |
77 | | -const crc_table = (() => { |
78 | | - const table = new Uint32Array(256) |
79 | | - for (let n = 0; n < 256; n++) { |
80 | | - let c = n |
81 | | - for (let k = 0; k < 8; k++) { |
82 | | - c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1 |
83 | | - } |
84 | | - table[n] = c >>> 0 |
85 | | - } |
86 | | - return table |
87 | | -})() |
88 | | - |
89 | | -const crc32 = (buffer: Buffer | Uint8Array) => { |
90 | | - let crc = 0xffffffff |
91 | | - for (let i = 0; i < buffer.length; i++) { |
92 | | - crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8) |
93 | | - } |
94 | | - return (crc ^ 0xffffffff) >>> 0 |
95 | | -} |
96 | | - |
97 | | -const dos_datetime = (date: Date) => ({ |
98 | | - time: |
99 | | - (date.getHours() << 11) | |
100 | | - (date.getMinutes() << 5) | |
101 | | - Math.floor(date.getSeconds() / 2), |
102 | | - date: |
103 | | - ((date.getFullYear() - 1980) << 9) | |
104 | | - ((date.getMonth() + 1) << 5) | |
105 | | - date.getDate() |
106 | | -}) |
107 | | - |
108 | | -const create_zip = (entries: { name: string; data: Buffer | Uint8Array }[]) => { |
109 | | - const { time, date } = dos_datetime(new Date()) |
110 | | - const chunks = [] |
111 | | - const central = [] |
112 | | - let offset = 0 |
113 | | - |
114 | | - for (const entry of entries) { |
115 | | - const name = Buffer.from(entry.name, 'utf8') |
116 | | - const raw = entry.data |
117 | | - const deflated = zlib.deflateRawSync(raw) |
118 | | - const use_deflate = deflated.length < raw.length |
119 | | - const data = use_deflate ? deflated : raw |
120 | | - const method = use_deflate ? 8 : 0 |
121 | | - const crc = crc32(raw) |
122 | | - |
123 | | - const local = Buffer.alloc(30) |
124 | | - local.writeUInt32LE(0x04034b50, 0) |
125 | | - local.writeUInt16LE(20, 4) |
126 | | - local.writeUInt16LE(0x0800, 6) // UTF-8 names |
127 | | - local.writeUInt16LE(method, 8) |
128 | | - local.writeUInt16LE(time, 10) |
129 | | - local.writeUInt16LE(date, 12) |
130 | | - local.writeUInt32LE(crc, 14) |
131 | | - local.writeUInt32LE(data.length, 18) |
132 | | - local.writeUInt32LE(raw.length, 22) |
133 | | - local.writeUInt16LE(name.length, 26) |
134 | | - local.writeUInt16LE(0, 28) |
135 | | - |
136 | | - const header = Buffer.alloc(46) |
137 | | - header.writeUInt32LE(0x02014b50, 0) |
138 | | - header.writeUInt16LE(20, 4) |
139 | | - header.writeUInt16LE(20, 6) |
140 | | - header.writeUInt16LE(0x0800, 8) |
141 | | - header.writeUInt16LE(method, 10) |
142 | | - header.writeUInt16LE(time, 12) |
143 | | - header.writeUInt16LE(date, 14) |
144 | | - header.writeUInt32LE(crc, 16) |
145 | | - header.writeUInt32LE(data.length, 20) |
146 | | - header.writeUInt32LE(raw.length, 24) |
147 | | - header.writeUInt16LE(name.length, 28) |
148 | | - header.writeUInt16LE(0, 30) |
149 | | - header.writeUInt16LE(0, 32) |
150 | | - header.writeUInt16LE(0, 34) |
151 | | - header.writeUInt16LE(0, 36) |
152 | | - header.writeUInt32LE(0, 38) |
153 | | - header.writeUInt32LE(offset, 42) |
154 | | - |
155 | | - chunks.push(local, name, data) |
156 | | - central.push(header, name) |
157 | | - offset += local.length + name.length + data.length |
158 | | - } |
159 | | - |
160 | | - const central_buffer = Buffer.concat(central) |
161 | | - const end = Buffer.alloc(22) |
162 | | - end.writeUInt32LE(0x06054b50, 0) |
163 | | - end.writeUInt16LE(0, 4) |
164 | | - end.writeUInt16LE(0, 6) |
165 | | - end.writeUInt16LE(entries.length, 8) |
166 | | - end.writeUInt16LE(entries.length, 10) |
167 | | - end.writeUInt32LE(central_buffer.length, 12) |
168 | | - end.writeUInt32LE(offset, 16) |
169 | | - end.writeUInt16LE(0, 20) |
170 | | - |
171 | | - return Buffer.concat([...chunks, central_buffer, end]) |
172 | | -} |
173 | | - |
174 | 75 | // --- VSIX metadata --- |
175 | 76 |
|
176 | 77 | const create_content_types = (names: string[]) => { |
|
0 commit comments