Skip to content

Commit b0c205f

Browse files
committed
Extract zip creation utility into shared package and reuse across apps
- can we drop [Fragment] for a shared archiver utility? ├── apps │ ├── browser │ │ ├── package.json │ │ ├── scripts │ │ │ ├── build.mjs │ │ │ ├── create-firefox-build.ts │ │ │ └── create-zip-archives.ts │ │ └── tsconfig.json │ ├── editor │ │ ├── package.json │ │ ├── scripts │ │ │ └── package.mts │ │ └── tsconfig.json │ ├── storybook │ │ └── tsconfig.json │ └── web │ └── tsconfig.json ├── packages │ ├── shared │ │ ├── package.json │ │ ├── src │ │ │ └── utils │ │ │ ├── create-zip │ │ │ │ ├── create-zip.ts │ │ │ │ └── index.ts │ │ │ ├── display-token-count.ts │ │ │ ├── extract-path-from-line-of-code │ │ │ │ ├── extract-path-from-line-of-code.spec.ts │ │ │ │ ├── extract-path-from-line-of-code.ts │ │ │ │ └── index.ts │ │ │ ├── logger.ts │ │ │ └── simplify-prompt-symbols │ │ │ ├── index.ts │ │ │ ├── simplify-prompt-symbols.spec.ts │ │ │ └── simplify-prompt-symbols.ts │ │ └── tsconfig.json │ └── ui │ └── tsconfig.json ├── pnpm-workspace.yaml └── tsconfig.json
1 parent 9bf7527 commit b0c205f

7 files changed

Lines changed: 138 additions & 376 deletions

File tree

‎apps/browser/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
"bump": "node scripts/bump.cjs"
88
},
99
"devDependencies": {
10-
"@types/archiver": "6.0.4",
1110
"@types/node": "22.x",
1211
"@types/react": "18.2.79",
1312
"@types/react-dom": "18.2.25",
1413
"@types/webextension-polyfill": "0.12.6",
15-
"archiver": "6.0.2",
1614
"baseline-browser-mapping": "2.11.35",
1715
"sass": "1.93.3",
1816
"vite": "8.3.6",
1917
"webextension-polyfill": "0.12.0"
2018
},
2119
"dependencies": {
20+
"@cwc/ui": "workspace:*",
21+
"@cwc/shared": "workspace:*",
2222
"classnames": "2.5.1",
2323
"react": "18.3.1",
2424
"react-dom": "18.3.1"

‎apps/browser/scripts/create-zip-archives.ts‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { create_zip } from '../../../packages/shared/src/utils/create-zip/create-zip.ts'
12
import * as fs from 'fs'
23
import * as path from 'path'
3-
import archiver from 'archiver'
44
import { fileURLToPath } from 'url'
55

66
const __filename = fileURLToPath(import.meta.url)
@@ -12,21 +12,33 @@ const get_version = (): string => {
1212
return manifest.version
1313
}
1414

15-
const create_zip_from_directory = (
15+
const collect_files = (
16+
dir: string,
17+
base_dir = dir
18+
): { name: string; data: Buffer }[] => {
19+
const entries: { name: string; data: Buffer }[] = []
20+
if (!fs.existsSync(dir)) return entries
21+
const files = fs.readdirSync(dir)
22+
for (const file of files) {
23+
const fullPath = path.join(dir, file)
24+
const stat = fs.statSync(fullPath)
25+
if (stat.isDirectory()) {
26+
entries.push(...collect_files(fullPath, base_dir))
27+
} else {
28+
const relativePath = path.relative(base_dir, fullPath).replace(/\\/g, '/')
29+
entries.push({ name: relativePath, data: fs.readFileSync(fullPath) })
30+
}
31+
}
32+
return entries
33+
}
34+
35+
const create_zip_from_directory = async (
1636
source_dir: string,
1737
output_path: string
1838
): Promise<void> => {
19-
return new Promise((resolve, reject) => {
20-
const output = fs.createWriteStream(output_path)
21-
const archive = archiver('zip', {
22-
zlib: { level: 9 }
23-
})
24-
output.on('close', resolve)
25-
archive.on('error', reject)
26-
archive.pipe(output)
27-
archive.directory(source_dir, false)
28-
archive.finalize()
29-
})
39+
const entries = collect_files(source_dir)
40+
const zip_buffer = create_zip(entries)
41+
fs.writeFileSync(output_path, zip_buffer)
3042
}
3143

3244
const log = (message: string) => console.log(`[create-zip-archives] ${message}`)

‎apps/editor/scripts/package.mts‎

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { create_zip } from '../../../packages/shared/src/utils/create-zip/create-zip.ts'
12
import * as fs from 'node:fs'
23
import * as path from 'node:path'
3-
import * as zlib from 'node:zlib'
44
import { fileURLToPath } from 'node:url'
55

66
const app_dir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
@@ -72,105 +72,6 @@ const resolve_files = () => {
7272
return files.map((file) => file.split(path.sep).join('/'))
7373
}
7474

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-
17475
// --- VSIX metadata ---
17576

17677
const create_content_types = (names: string[]) => {

‎packages/shared/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@cwc/shared",
3+
"type": "module",
34
"dependencies": {
45
"react": "18.2.0",
56
"react-dom": "18.2.0"
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import * as zlib from 'node:zlib'
2+
3+
const crc_table = (() => {
4+
const table = new Uint32Array(256)
5+
for (let n = 0; n < 256; n++) {
6+
let c = n
7+
for (let k = 0; k < 8; k++) {
8+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
9+
}
10+
table[n] = c >>> 0
11+
}
12+
return table
13+
})()
14+
15+
export const crc32 = (buffer: Buffer | Uint8Array) => {
16+
let crc = 0xffffffff
17+
for (let i = 0; i < buffer.length; i++) {
18+
crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8)
19+
}
20+
return (crc ^ 0xffffffff) >>> 0
21+
}
22+
23+
export const dos_datetime = (date: Date) => ({
24+
time:
25+
(date.getHours() << 11) |
26+
(date.getMinutes() << 5) |
27+
Math.floor(date.getSeconds() / 2),
28+
date:
29+
((date.getFullYear() - 1980) << 9) |
30+
((date.getMonth() + 1) << 5) |
31+
date.getDate()
32+
})
33+
34+
export const create_zip = (
35+
entries: { name: string; data: Buffer | Uint8Array }[]
36+
) => {
37+
const { time, date } = dos_datetime(new Date())
38+
const chunks = []
39+
const central = []
40+
let offset = 0
41+
42+
for (const entry of entries) {
43+
const name = Buffer.from(entry.name, 'utf8')
44+
const raw = Buffer.isBuffer(entry.data)
45+
? entry.data
46+
: Buffer.from(entry.data)
47+
const deflated = zlib.deflateRawSync(raw)
48+
const use_deflate = deflated.length < raw.length
49+
const data = use_deflate ? deflated : raw
50+
const method = use_deflate ? 8 : 0
51+
const crc = crc32(raw)
52+
53+
const local = Buffer.alloc(30)
54+
local.writeUInt32LE(0x04034b50, 0)
55+
local.writeUInt16LE(20, 4)
56+
local.writeUInt16LE(0x0800, 6) // UTF-8 names
57+
local.writeUInt16LE(method, 8)
58+
local.writeUInt16LE(time, 10)
59+
local.writeUInt16LE(date, 12)
60+
local.writeUInt32LE(crc, 14)
61+
local.writeUInt32LE(data.length, 18)
62+
local.writeUInt32LE(raw.length, 22)
63+
local.writeUInt16LE(name.length, 26)
64+
local.writeUInt16LE(0, 28)
65+
66+
const header = Buffer.alloc(46)
67+
header.writeUInt32LE(0x02014b50, 0)
68+
header.writeUInt16LE(20, 4)
69+
header.writeUInt16LE(20, 6)
70+
header.writeUInt16LE(0x0800, 8)
71+
header.writeUInt16LE(method, 10)
72+
header.writeUInt16LE(time, 12)
73+
header.writeUInt16LE(date, 14)
74+
header.writeUInt32LE(crc, 16)
75+
header.writeUInt32LE(data.length, 20)
76+
header.writeUInt32LE(raw.length, 24)
77+
header.writeUInt16LE(name.length, 28)
78+
header.writeUInt16LE(0, 30)
79+
header.writeUInt16LE(0, 32)
80+
header.writeUInt16LE(0, 34)
81+
header.writeUInt16LE(0, 36)
82+
header.writeUInt32LE(0, 38)
83+
header.writeUInt32LE(offset, 42)
84+
85+
chunks.push(local, name, data)
86+
central.push(header, name)
87+
offset += local.length + name.length + data.length
88+
}
89+
90+
const central_buffer = Buffer.concat(central)
91+
const end = Buffer.alloc(22)
92+
end.writeUInt32LE(0x06054b50, 0)
93+
end.writeUInt16LE(0, 4)
94+
end.writeUInt16LE(0, 6)
95+
end.writeUInt16LE(entries.length, 8)
96+
end.writeUInt16LE(entries.length, 10)
97+
end.writeUInt32LE(central_buffer.length, 12)
98+
end.writeUInt32LE(offset, 16)
99+
end.writeUInt16LE(0, 20)
100+
101+
return Buffer.concat([...chunks, central_buffer, end])
102+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './create-zip'

0 commit comments

Comments
 (0)