create-typeser 2.0.0 → 2.0.3
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/README.md +64 -46
- package/bin/create-typeser.mjs +206 -169
- package/lib/install-versions.mjs +124 -0
- package/lib/prepare-database.mjs +46 -12
- package/lib/project.mjs +211 -184
- package/lib/provision-server.sql +68 -0
- package/lib/questions.mjs +2 -2
- package/lib/tools.mjs +141 -127
- package/lib/windows-install.mjs +50 -0
- package/package.json +2 -1
- package/scripts/toolchains/extract-install.ps1 +39 -0
package/lib/tools.mjs
CHANGED
|
@@ -1,127 +1,141 @@
|
|
|
1
|
-
import {execFileSync, spawnSync} from 'node:child_process';
|
|
2
|
-
import {createHash} from 'node:crypto';
|
|
3
|
-
import {chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, writeFileSync} from 'node:fs';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
import {fileURLToPath} from 'node:url';
|
|
6
|
-
|
|
7
|
-
function executable(name) {
|
|
8
|
-
return process.platform === 'win32' ? name + '.exe' : name;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function probe(program, args) {
|
|
12
|
-
try {
|
|
13
|
-
return execFileSync(program, args, {encoding: 'utf8', windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'], timeout: 8000}).trim();
|
|
14
|
-
} catch { return ''; }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function compatibleBin(tool, directory) {
|
|
18
|
-
if (!directory) return;
|
|
19
|
-
for (const bin of [directory, path.join(directory, 'bin')]) {
|
|
20
|
-
if (!existsSync(path.join(bin, executable(tool === 'java' ? 'javac' : 'postgres')))) continue;
|
|
21
|
-
if (tool === 'java') {
|
|
22
|
-
const java = path.join(bin, executable('java'));
|
|
23
|
-
const javac = path.join(bin, executable('javac'));
|
|
24
|
-
const real = realpathSync(javac);
|
|
25
|
-
if (process.platform === 'darwin' && real === '/usr/bin/javac') continue;
|
|
26
|
-
if (/^(?:openjdk|java)
|
|
27
|
-
return path.dirname(real);
|
|
28
|
-
} else {
|
|
29
|
-
const commands = ['postgres', 'initdb', 'pg_ctl', 'psql'];
|
|
30
|
-
if (commands.every(name => Number(probe(path.join(bin, executable(name)), ['--version']).match(/PostgreSQL\) (\d+)/)?.[1]) >= 18))
|
|
31
|
-
return path.dirname(realpathSync(path.join(bin, executable('postgres'))));
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function detectedBin(tool) {
|
|
37
|
-
const directories = tool === 'java' ? [process.env.JAVA_HOME] : [probe('pg_config', ['--bindir'])];
|
|
38
|
-
if (process.platform === 'darwin') {
|
|
39
|
-
for (const prefix of [probe('brew', ['--prefix']), '/opt/homebrew', '/usr/local'].filter(Boolean))
|
|
40
|
-
for (const formula of tool === 'java' ? ['openjdk@25','openjdk@26','openjdk'] : ['postgresql@19','postgresql@18'])
|
|
41
|
-
directories.push(path.join(prefix, 'opt', formula, 'bin'));
|
|
42
|
-
if (tool === 'java') {
|
|
43
|
-
for (const version of ['25', '
|
|
44
|
-
const homes = '/Library/Java/JavaVirtualMachines';
|
|
45
|
-
if (existsSync(homes)) for (const jdk of readdirSync(homes)) directories.push(path.join(homes, jdk, 'Contents/Home/bin'));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
directories.push(...(process.env.PATH ?? '').split(path.delimiter).filter(Boolean));
|
|
49
|
-
if (tool === 'postgresql' && process.platform === 'win32' && process.env.ProgramFiles) {
|
|
50
|
-
for (const major of [19, 18]) directories.push(path.join(process.env.ProgramFiles, 'PostgreSQL', String(major)));
|
|
51
|
-
}
|
|
52
|
-
if (tool === 'postgresql' && process.platform === 'linux') {
|
|
53
|
-
for (const major of [19, 18]) directories.push('/usr/lib/postgresql/' + major + '/bin');
|
|
54
|
-
}
|
|
55
|
-
for (const directory of new Set(directories)) {
|
|
56
|
-
const bin = compatibleBin(tool, directory);
|
|
57
|
-
if (bin) return bin;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function selectedBin(tool, value) {
|
|
62
|
-
if (value === 'install') {
|
|
63
|
-
if (process.platform === 'darwin') throw new Error('Install openjdk@
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
const bin = compatibleBin(tool, path.resolve(value));
|
|
67
|
-
if (!bin) throw new Error('Expected a JDK
|
|
68
|
-
return bin;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function linuxTool(root, tool) {
|
|
72
|
-
const checksums = {x64: ['64', '701f39c06f8e6a670a0e186c8479b9e5c0502a2f0cda6cef65d83bdd2875e8bd'],
|
|
73
|
-
arm64: ['aarch64', '3bb56786e21b9b24024f599fe9185b4a59dc3f9f59937a42311ee583adbbb23c']};
|
|
74
|
-
const [arch, checksum] = checksums[process.arch];
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (process.platform === '
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
1
|
+
import {execFileSync, spawnSync} from 'node:child_process';
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import {chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, writeFileSync} from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
6
|
+
|
|
7
|
+
function executable(name) {
|
|
8
|
+
return process.platform === 'win32' ? name + '.exe' : name;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function probe(program, args) {
|
|
12
|
+
try {
|
|
13
|
+
return execFileSync(program, args, {encoding: 'utf8', windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'], timeout: 8000}).trim();
|
|
14
|
+
} catch { return ''; }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function compatibleBin(tool, directory, clientOnly = false) {
|
|
18
|
+
if (!directory) return;
|
|
19
|
+
for (const bin of [directory, path.join(directory, 'bin')]) {
|
|
20
|
+
if (!existsSync(path.join(bin, executable(tool === 'java' ? 'javac' : clientOnly ? 'psql' : 'postgres')))) continue;
|
|
21
|
+
if (tool === 'java') {
|
|
22
|
+
const java = path.join(bin, executable('java'));
|
|
23
|
+
const javac = path.join(bin, executable('javac'));
|
|
24
|
+
const real = realpathSync(javac);
|
|
25
|
+
if (process.platform === 'darwin' && real === '/usr/bin/javac') continue;
|
|
26
|
+
if (/^(?:openjdk|java) 2[1-6]\b/i.test(probe(java, ['--version'])) && /^javac 2[1-6]\b/.test(probe(javac, ['--version'])))
|
|
27
|
+
return path.dirname(real);
|
|
28
|
+
} else {
|
|
29
|
+
const commands = clientOnly ? ['psql'] : ['postgres', 'initdb', 'pg_ctl', 'psql'];
|
|
30
|
+
if (commands.every(name => Number(probe(path.join(bin, executable(name)), ['--version']).match(/PostgreSQL\) (\d+)/)?.[1]) >= 18))
|
|
31
|
+
return path.dirname(realpathSync(path.join(bin, executable(clientOnly ? 'psql' : 'postgres'))));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function detectedBin(tool) {
|
|
37
|
+
const directories = tool === 'java' ? [process.env.JAVA_HOME] : [probe('pg_config', ['--bindir'])];
|
|
38
|
+
if (process.platform === 'darwin') {
|
|
39
|
+
for (const prefix of [probe('brew', ['--prefix']), '/opt/homebrew', '/usr/local'].filter(Boolean))
|
|
40
|
+
for (const formula of tool === 'java' ? ['openjdk@25','openjdk@26','openjdk@21','openjdk'] : ['postgresql@19','postgresql@18'])
|
|
41
|
+
directories.push(path.join(prefix, 'opt', formula, 'bin'));
|
|
42
|
+
if (tool === 'java') {
|
|
43
|
+
for (const version of ['26', '25', '24', '23', '22', '21']) directories.push(probe('/usr/libexec/java_home', ['-v', version]));
|
|
44
|
+
const homes = '/Library/Java/JavaVirtualMachines';
|
|
45
|
+
if (existsSync(homes)) for (const jdk of readdirSync(homes)) directories.push(path.join(homes, jdk, 'Contents/Home/bin'));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
directories.push(...(process.env.PATH ?? '').split(path.delimiter).filter(Boolean));
|
|
49
|
+
if (tool === 'postgresql' && process.platform === 'win32' && process.env.ProgramFiles) {
|
|
50
|
+
for (const major of [19, 18]) directories.push(path.join(process.env.ProgramFiles, 'PostgreSQL', String(major)));
|
|
51
|
+
}
|
|
52
|
+
if (tool === 'postgresql' && process.platform === 'linux') {
|
|
53
|
+
for (const major of [19, 18]) directories.push('/usr/lib/postgresql/' + major + '/bin');
|
|
54
|
+
}
|
|
55
|
+
for (const directory of new Set(directories)) {
|
|
56
|
+
const bin = compatibleBin(tool, directory);
|
|
57
|
+
if (bin) return bin;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function selectedBin(tool, value, clientOnly = false) {
|
|
62
|
+
if (value === 'install') {
|
|
63
|
+
if (process.platform === 'darwin') throw new Error('Install openjdk@21 and postgresql@18 with Homebrew, then select their directories.');
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
const bin = compatibleBin(tool, path.resolve(value), clientOnly);
|
|
67
|
+
if (!bin) throw new Error('Expected a JDK 21–26 or a PostgreSQL 18+ installation in the selected ' + tool + ' directory.');
|
|
68
|
+
return bin;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function linuxTool(root, tool, artifact) {
|
|
72
|
+
const checksums = {x64: ['64', '701f39c06f8e6a670a0e186c8479b9e5c0502a2f0cda6cef65d83bdd2875e8bd'],
|
|
73
|
+
arm64: ['aarch64', '3bb56786e21b9b24024f599fe9185b4a59dc3f9f59937a42311ee583adbbb23c']};
|
|
74
|
+
const [arch, checksum] = checksums[process.arch];
|
|
75
|
+
const {physicalPath} = await import('./project.mjs');
|
|
76
|
+
const tools = physicalPath(path.join(root, '.local/toolchains'));
|
|
77
|
+
const prefix = physicalPath(path.join(tools, tool + '-' + artifact.sha256));
|
|
78
|
+
const existing = compatibleBin(tool, prefix);
|
|
79
|
+
function checkRecord() {
|
|
80
|
+
const name = (tool === 'java' ? 'openjdk' : 'postgresql') + '-' + artifact.version + '-' + artifact.build + '.json';
|
|
81
|
+
const record = JSON.parse(readFileSync(physicalPath(path.join(prefix, 'conda-meta', name)), 'utf8'));
|
|
82
|
+
if (record.sha256 !== artifact.sha256) throw new Error('Installed conda package differs from the saved SHA-256');
|
|
83
|
+
}
|
|
84
|
+
if (existing) { checkRecord(); return existing; }
|
|
85
|
+
const manager = path.join(tools, 'micromamba');
|
|
86
|
+
mkdirSync(tools, {recursive: true});
|
|
87
|
+
if (!existsSync(manager)) {
|
|
88
|
+
const response = await fetch('https://github.com/mamba-org/micromamba-releases/releases/download/2.10.0-0/micromamba-linux-' + arch,
|
|
89
|
+
{signal: AbortSignal.timeout(120000)});
|
|
90
|
+
if (!response.ok) throw new Error('Micromamba download failed with HTTP ' + response.status);
|
|
91
|
+
const contents = Buffer.from(await response.arrayBuffer());
|
|
92
|
+
if (createHash('sha256').update(contents).digest('hex') !== checksum) throw new Error('Micromamba checksum mismatch');
|
|
93
|
+
writeFileSync(manager + '.partial', contents, {mode: 0o700});
|
|
94
|
+
renameSync(manager + '.partial', manager);
|
|
95
|
+
}
|
|
96
|
+
if (createHash('sha256').update(readFileSync(manager)).digest('hex') !== checksum) throw new Error('Micromamba checksum mismatch');
|
|
97
|
+
chmodSync(manager, 0o700);
|
|
98
|
+
execFileSync(manager, ['--no-rc', 'create', '--yes', '--override-channels', '-c', 'conda-forge', '-p', prefix,
|
|
99
|
+
artifact.spec + '[sha256=' + artifact.sha256 + ']'], {
|
|
100
|
+
env: {...process.env, MAMBA_ROOT_PREFIX: path.join(tools, 'mamba')}, stdio: 'inherit',
|
|
101
|
+
});
|
|
102
|
+
checkRecord();
|
|
103
|
+
return path.join(prefix, 'bin');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function resolveTool(root, tool, selection, clientOnly = false, savedArtifact) {
|
|
107
|
+
if (selection !== 'install') return selectedBin(tool, selection, clientOnly);
|
|
108
|
+
if (process.platform === 'darwin') {
|
|
109
|
+
const installed = detectedBin(tool);
|
|
110
|
+
if (installed) return installed;
|
|
111
|
+
throw new Error('Install openjdk@21 and postgresql@18 with Homebrew, then rerun the initializer in this project.');
|
|
112
|
+
}
|
|
113
|
+
console.log('Preparing project-local ' + tool + '...');
|
|
114
|
+
const {validateArtifact, verifiedArtifact} = await import('./install-versions.mjs');
|
|
115
|
+
const artifact = validateArtifact(tool, savedArtifact ?? verifiedArtifact(tool), process.platform, process.arch);
|
|
116
|
+
let bin;
|
|
117
|
+
if (process.platform === 'win32' && process.arch === 'x64') {
|
|
118
|
+
const current = verifiedArtifact(tool);
|
|
119
|
+
if (artifact.channel === 'latest' || artifact.version !== current.version || artifact.sha256 !== current.sha256 || artifact.url !== current.url) {
|
|
120
|
+
const {installWindows} = await import('./windows-install.mjs');
|
|
121
|
+
return installWindows(root, tool, artifact, selectedBin);
|
|
122
|
+
}
|
|
123
|
+
const script = fileURLToPath(new URL('../scripts/toolchains/checkout.ps1', import.meta.url));
|
|
124
|
+
const manifest = JSON.parse(readFileSync(path.join(path.dirname(script), 'windows-x64.json'), 'utf8'));
|
|
125
|
+
const pin = manifest.tools.find(item => item.id === tool);
|
|
126
|
+
const shell = path.join(process.env.SystemRoot, 'System32/WindowsPowerShell/v1.0/powershell.exe');
|
|
127
|
+
const child = spawnSync(shell, ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', script,
|
|
128
|
+
'prepare', '-Tool', tool, '-ApplicationRoot', root, '-Json'], {windowsHide: true, encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit']});
|
|
129
|
+
if (child.error) throw child.error;
|
|
130
|
+
const report = JSON.parse(child.stdout);
|
|
131
|
+
if (child.status !== 0 || !report.tools?.some(item => item.id === tool && item.state === 'verified'))
|
|
132
|
+
throw new Error(report.message || 'Could not prepare ' + tool);
|
|
133
|
+
bin = path.join(root, '.local/toolchains', pin.directory, 'bin');
|
|
134
|
+
console.log('Using ' + tool + ' ' + pin.runtimeVersion + ' from ' + bin);
|
|
135
|
+
} else if (process.platform === 'linux' && ['x64', 'arm64'].includes(process.arch)) {
|
|
136
|
+
bin = await linuxTool(root, tool, artifact);
|
|
137
|
+
} else {
|
|
138
|
+
throw new Error('Automatic installation supports Windows x64 and Linux x64/arm64. Select an existing tool directory on this system.');
|
|
139
|
+
}
|
|
140
|
+
return selectedBin(tool, bin);
|
|
141
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {createHash} from 'node:crypto';
|
|
2
|
+
import {execFileSync} from 'node:child_process';
|
|
3
|
+
import {createReadStream, createWriteStream, existsSync, mkdirSync, mkdtempSync, renameSync, rmSync} from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
6
|
+
import {pipeline} from 'node:stream/promises';
|
|
7
|
+
import {Transform} from 'node:stream';
|
|
8
|
+
import {physicalPath} from './project.mjs';
|
|
9
|
+
import {downloadHosts, trustedFetch} from './install-versions.mjs';
|
|
10
|
+
|
|
11
|
+
export async function verifyArchive(file, expected) {
|
|
12
|
+
const hash = createHash('sha256');
|
|
13
|
+
for await (const chunk of createReadStream(file)) hash.update(chunk);
|
|
14
|
+
if (hash.digest('hex') !== expected.toLowerCase()) throw new Error('Tool archive SHA-256 mismatch');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function installWindows(root, tool, artifact, checkBin) {
|
|
18
|
+
const tools = physicalPath(path.join(root, '.local/toolchains'));
|
|
19
|
+
mkdirSync(tools, {recursive: true});
|
|
20
|
+
const target = physicalPath(path.join(tools, tool + '-' + artifact.sha256));
|
|
21
|
+
function installedBin(directory) {
|
|
22
|
+
const bin = checkBin(tool, physicalPath(path.join(directory, 'bin')));
|
|
23
|
+
const command = tool === 'java' ? 'java.exe' : 'postgres.exe';
|
|
24
|
+
const output = execFileSync(path.join(bin, command), ['--version'], {encoding: 'utf8', windowsHide: true});
|
|
25
|
+
const version = tool === 'java' ? output.match(/\(build ([\d.+]+)/)?.[1] : output.match(/PostgreSQL\) ([\d.]+)/)?.[1];
|
|
26
|
+
if (version !== artifact.version.split('-')[0]) throw new Error('Installed version differs from the saved tool artifact');
|
|
27
|
+
return bin;
|
|
28
|
+
}
|
|
29
|
+
if (existsSync(target)) return installedBin(target);
|
|
30
|
+
const work = mkdtempSync(path.join(tools, '.install-'));
|
|
31
|
+
try {
|
|
32
|
+
const archive = path.join(work, 'archive.zip');
|
|
33
|
+
const response = await trustedFetch(artifact.url, downloadHosts(tool), AbortSignal.timeout(600000));
|
|
34
|
+
let bytes = 0;
|
|
35
|
+
const limit = new Transform({transform(chunk, encoding, callback) {
|
|
36
|
+
bytes += chunk.length;
|
|
37
|
+
callback(bytes > 1024 ** 3 ? new Error('Tool archive exceeds download limit') : null, chunk);
|
|
38
|
+
}});
|
|
39
|
+
await pipeline(response.body, limit, createWriteStream(archive, {flags: 'wx'}));
|
|
40
|
+
await verifyArchive(archive, artifact.sha256);
|
|
41
|
+
const stage = path.join(work, 'extracted');
|
|
42
|
+
mkdirSync(stage);
|
|
43
|
+
const script = fileURLToPath(new URL('../scripts/toolchains/extract-install.ps1', import.meta.url));
|
|
44
|
+
execFileSync(path.join(process.env.SystemRoot, 'System32/WindowsPowerShell/v1.0/powershell.exe'),
|
|
45
|
+
['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', script, archive, stage, tool], {windowsHide: true});
|
|
46
|
+
installedBin(stage);
|
|
47
|
+
renameSync(stage, target);
|
|
48
|
+
return path.join(target, 'bin');
|
|
49
|
+
} finally { rmSync(physicalPath(work), {recursive: true, force: true}); }
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-typeser",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Create a local TypeSer application with Java and PostgreSQL",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"create",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"bin",
|
|
33
33
|
"lib",
|
|
34
34
|
"scripts/toolchains/checkout.ps1",
|
|
35
|
+
"scripts/toolchains/extract-install.ps1",
|
|
35
36
|
"scripts/toolchains/windows-x64.json"
|
|
36
37
|
]
|
|
37
38
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
param([Parameter(Mandatory)][string]$Archive, [Parameter(Mandatory)][string]$Destination, [Parameter(Mandatory)][ValidateSet('java','postgresql')][string]$Tool)
|
|
2
|
+
$ErrorActionPreference = 'Stop'
|
|
3
|
+
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
4
|
+
# The caller supplies a freshly created, physical, empty staging directory.
|
|
5
|
+
if ((Get-ChildItem -Force -LiteralPath $Destination) -or
|
|
6
|
+
((Get-Item -LiteralPath $Destination).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'Invalid extraction directory' }
|
|
7
|
+
$base = [IO.Path]::GetFullPath($Destination).TrimEnd('\') + '\'
|
|
8
|
+
$zip = [IO.Compression.ZipFile]::OpenRead($Archive)
|
|
9
|
+
try {
|
|
10
|
+
$seen = [Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
|
|
11
|
+
$root = $null
|
|
12
|
+
[long]$total = 0
|
|
13
|
+
if ($zip.Entries.Count -gt 200000) { throw 'Too many ZIP entries' }
|
|
14
|
+
foreach ($entry in $zip.Entries) {
|
|
15
|
+
$name = $entry.FullName.TrimEnd('/')
|
|
16
|
+
$parts = $name.Split('/')
|
|
17
|
+
foreach ($part in $parts) {
|
|
18
|
+
if (!$part -or $part -in @('.', '..') -or $part -ne $part.TrimEnd(' ', '.') -or
|
|
19
|
+
$part -match '[\\\x00-\x1f:<>"|?*]' -or $part -match '^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(?:\.|$)') { throw 'Unsafe ZIP entry' }
|
|
20
|
+
}
|
|
21
|
+
if (!$root) { $root = $parts[0] }
|
|
22
|
+
$kind = ($entry.ExternalAttributes -shr 16) -band 61440
|
|
23
|
+
if ($parts[0] -cne $root -or !$seen.Add($name) -or $kind -notin @(0,16384,32768) -or
|
|
24
|
+
($entry.ExternalAttributes -band 1024)) { throw 'Unexpected ZIP layout or link' }
|
|
25
|
+
$total += $entry.Length
|
|
26
|
+
if ($total -gt 6GB -or $entry.Length -gt 1GB) { throw 'ZIP exceeds extraction limit' }
|
|
27
|
+
if ($parts.Length -eq 1) {
|
|
28
|
+
if (!$entry.FullName.EndsWith('/')) { throw 'Expected one ZIP directory' }
|
|
29
|
+
continue
|
|
30
|
+
}
|
|
31
|
+
# The project needs PostgreSQL server/client files and their licenses (same selection as checkout.ps1).
|
|
32
|
+
if ($Tool -eq 'postgresql' -and $parts[1] -cnotin @('bin','lib','share','commandlinetools_3rd_party_licenses.txt','server_license.txt')) { continue }
|
|
33
|
+
$target = [IO.Path]::GetFullPath([IO.Path]::Combine($base, $name.Substring($root.Length + 1)))
|
|
34
|
+
if (!$target.StartsWith($base,[StringComparison]::OrdinalIgnoreCase)) { throw 'Unsafe ZIP destination' }
|
|
35
|
+
if ($entry.FullName.EndsWith('/')) { [void][IO.Directory]::CreateDirectory($target); continue }
|
|
36
|
+
[void][IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($target))
|
|
37
|
+
[IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $target, $false)
|
|
38
|
+
}
|
|
39
|
+
} finally { $zip.Dispose() }
|