create-typeser 2.0.0-rc.1 → 2.0.1
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 +113 -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
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {existsSync, readFileSync} from 'node:fs';
|
|
2
|
+
|
|
3
|
+
const javaHosts = ['github.com', 'objects.githubusercontent.com', 'release-assets.githubusercontent.com', 'download.java.net'];
|
|
4
|
+
const linuxPins = {
|
|
5
|
+
java: {
|
|
6
|
+
x64: ['26.0.2.1', 'h1602c4f_0', '806bfc6f62d3d7c0c3a7d3a979f0ef6d2fd5bf8228b4c405bbf8b97fe3e14962'],
|
|
7
|
+
arm64: ['26.0.2.1', 'h1684e21_0', '56c7887107460bddbc6de68c31dc0caa24e70291a9b5ae8e1c694989a2038c95'],
|
|
8
|
+
},
|
|
9
|
+
postgresql: {
|
|
10
|
+
x64: ['18.6', 'haf2d9ce_5', 'e70620a3d1b312cb9e5489dbea573c5115da1c092b6571e18858d0ff4c0c5a5d'],
|
|
11
|
+
arm64: ['18.6', 'hf3daf41_5', '312921f7247cea5e52a2fc24015f0a009b7f909d755940c6b9ac80d2234c6aee'],
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
const condaName = tool => tool === 'java' ? 'openjdk' : 'postgresql';
|
|
15
|
+
function windowsPinset() {
|
|
16
|
+
const name = 'scripts/toolchains/windows-x64.json';
|
|
17
|
+
const packaged = new URL('../' + name, import.meta.url);
|
|
18
|
+
const checkout = !existsSync(packaged) && JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).private;
|
|
19
|
+
return JSON.parse(readFileSync(checkout ? new URL('../../../' + name, import.meta.url) : packaged, 'utf8'));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function verifiedArtifact(tool, os = process.platform, arch = process.arch) {
|
|
23
|
+
if (os === 'win32' && arch === 'x64') {
|
|
24
|
+
const {version, url, checksum: sha256} = windowsPinset().tools.find(pin => pin.id === tool);
|
|
25
|
+
return {channel: 'verified', version, url, sha256};
|
|
26
|
+
}
|
|
27
|
+
if (os !== 'linux' || !linuxPins[tool]?.[arch]) throw new Error('Automatic tool installation is unavailable for ' + os + '/' + arch);
|
|
28
|
+
const [version, build, sha256] = linuxPins[tool][arch];
|
|
29
|
+
return {channel: 'verified', version, build, spec: condaName(tool) + '=' + version + '=' + build, sha256};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function allowedUrl(input, hosts) {
|
|
33
|
+
const url = new URL(input);
|
|
34
|
+
if (url.protocol !== 'https:' || url.port || url.username || url.password || !hosts.includes(url.hostname))
|
|
35
|
+
throw new Error('Unexpected tool download origin');
|
|
36
|
+
return url;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function trustedFetch(input, hosts, signal, request = fetch) {
|
|
40
|
+
let url = allowedUrl(input, hosts);
|
|
41
|
+
for (let count = 0; count < 6; count++) {
|
|
42
|
+
const response = await request(url, {redirect: 'manual', signal});
|
|
43
|
+
if (response.status === 200) return response;
|
|
44
|
+
const location = response.headers.get('location');
|
|
45
|
+
await response.body?.cancel();
|
|
46
|
+
if (![301, 302, 303, 307, 308].includes(response.status) || !location) throw new Error('Download returned HTTP ' + response.status);
|
|
47
|
+
url = allowedUrl(new URL(location, url), hosts);
|
|
48
|
+
}
|
|
49
|
+
throw new Error('Too many download redirects');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function downloadHosts(tool) { return tool === 'java' ? javaHosts : ['get.enterprisedb.com']; }
|
|
53
|
+
|
|
54
|
+
export function validateArtifact(tool, artifact, os, arch) {
|
|
55
|
+
if (!['java', 'postgresql'].includes(tool) || !(os === 'win32' && arch === 'x64' || os === 'linux' && ['x64', 'arm64'].includes(arch)))
|
|
56
|
+
throw new Error('Unsupported tool platform');
|
|
57
|
+
if (typeof artifact?.version !== 'string' || typeof artifact.sha256 !== 'string' || !['latest', 'verified'].includes(artifact.channel) || !/^[a-f0-9]{64}$/i.test(artifact.sha256))
|
|
58
|
+
throw new Error('Invalid tool artifact or SHA-256');
|
|
59
|
+
const pattern = tool === 'java' ? /^26(?:\.\d+){1,3}(?:\+\d+)?$/ : os === 'win32' ? /^18\.\d+-\d+$/ : /^18\.\d+$/;
|
|
60
|
+
if (!pattern.test(artifact.version)) throw new Error('Unsupported tool version');
|
|
61
|
+
if (os === 'linux') {
|
|
62
|
+
if (typeof artifact.build !== 'string' || !/^[a-zA-Z0-9_]+$/.test(artifact.build) || artifact.spec !== condaName(tool) + '=' + artifact.version + '=' + artifact.build)
|
|
63
|
+
throw new Error('Invalid conda artifact');
|
|
64
|
+
} else {
|
|
65
|
+
allowedUrl(artifact.url, downloadHosts(tool));
|
|
66
|
+
if (tool === 'postgresql' && artifact.url !== 'https://get.enterprisedb.com/postgresql/postgresql-' + artifact.version + '-windows-x64-binaries.zip')
|
|
67
|
+
throw new Error('Invalid PostgreSQL archive');
|
|
68
|
+
}
|
|
69
|
+
return artifact;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const compare = (a, b) => a.localeCompare(b, 'en', {numeric: true});
|
|
73
|
+
|
|
74
|
+
async function latestArtifact(tool, os, arch, request) {
|
|
75
|
+
const signal = AbortSignal.timeout(6500);
|
|
76
|
+
const json = async url => (await trustedFetch(url, [new URL(url).hostname], signal, request)).json();
|
|
77
|
+
if (os === 'linux') {
|
|
78
|
+
const name = condaName(tool);
|
|
79
|
+
const subdir = arch === 'arm64' ? 'linux-aarch64' : 'linux-64';
|
|
80
|
+
const data = await json('https://api.anaconda.org/package/conda-forge/' + name);
|
|
81
|
+
const pattern = tool === 'java' ? /^26(?:\.\d+){1,3}$/ : /^18\.\d+$/;
|
|
82
|
+
const candidates = data.files.filter(f => pattern.test(f.version) && f.labels?.includes('main') &&
|
|
83
|
+
f.attrs?.subdir === subdir && f.basename === subdir + '/' + name + '-' + f.version + '-' + f.attrs.build + '.conda');
|
|
84
|
+
const file = candidates.sort((a, b) => compare(b.version, a.version) || b.attrs.build_number - a.attrs.build_number)[0];
|
|
85
|
+
if (!file) throw new Error('No stable conda build');
|
|
86
|
+
return {channel: 'latest', version: file.version, build: file.attrs.build, spec: name + '=' + file.version + '=' + file.attrs.build, sha256: file.sha256};
|
|
87
|
+
}
|
|
88
|
+
if (tool === 'postgresql') {
|
|
89
|
+
const data = await json('https://raw.githubusercontent.com/tspoh/TypeSer/main/scripts/toolchains/postgresql-windows-18.json');
|
|
90
|
+
if (data.format !== 1) throw new Error('Unsupported PostgreSQL manifest');
|
|
91
|
+
return {channel: 'latest', version: data.version, url: data.url, sha256: data.sha256};
|
|
92
|
+
}
|
|
93
|
+
const releases = await json('https://api.adoptium.net/v3/assets/feature_releases/26/ga?sort_order=DESC&sort_method=DATE&page_size=1&jvm_impl=hotspot&architecture=x64&image_type=jdk&os=windows&vendor=eclipse');
|
|
94
|
+
const release = releases.find(r => r.release_type === 'ga' && r.vendor === 'eclipse');
|
|
95
|
+
const binary = release?.binaries.find(b => b.os === 'windows' && b.architecture === 'x64' && b.image_type === 'jdk' && b.jvm_impl === 'hotspot');
|
|
96
|
+
if (!binary) throw new Error('No stable JDK build');
|
|
97
|
+
return {channel: 'latest', version: release.version_data.openjdk_version, url: binary.package.link, sha256: binary.package.checksum};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function chooseArtifact(ui, tool, os = process.platform, arch = process.arch, request = fetch) {
|
|
101
|
+
const verified = verifiedArtifact(tool, os, arch);
|
|
102
|
+
const display = a => a.version + (a.build ? ' (' + a.build + ')' : '');
|
|
103
|
+
let latest;
|
|
104
|
+
try { latest = validateArtifact(tool, await latestArtifact(tool, os, arch, request), os, arch); }
|
|
105
|
+
catch { console.log('Latest stable ' + tool + ' unavailable. Using verified ' + display(verified)); return verified; }
|
|
106
|
+
if (compare(latest.version, verified.version) < 0 || latest.version === verified.version && latest.build === verified.build) {
|
|
107
|
+
console.log('Using verified ' + tool + ' ' + display(verified) + '; no newer stable version available.');
|
|
108
|
+
return verified;
|
|
109
|
+
}
|
|
110
|
+
const channel = await ui.ask(tool + ' download (verified = ' + display(verified) + ' / latest = ' + display(latest) + ')', 'verified',
|
|
111
|
+
{check: value => { if (!['verified', 'latest'].includes(value)) throw new Error('Choose verified or latest.'); return value; }});
|
|
112
|
+
return channel === 'latest' ? latest : verified;
|
|
113
|
+
}
|
package/lib/prepare-database.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {execFileSync} from 'node:child_process';
|
|
2
|
-
import {randomUUID} from 'node:crypto';
|
|
2
|
+
import {createHash, createHmac, pbkdf2Sync, randomBytes, randomUUID} from 'node:crypto';
|
|
3
3
|
import {existsSync, readFileSync, realpathSync, unlinkSync, writeFileSync} from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
5
6
|
|
|
6
7
|
function privateFile(root, contents, action) {
|
|
7
8
|
const directory = path.join(root, 'secrets');
|
|
@@ -11,11 +12,46 @@ function privateFile(root, contents, action) {
|
|
|
11
12
|
try { return action(file); } finally { unlinkSync(file); }
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
function passLine(plan, user, password) {
|
|
16
|
+
const escape = value => String(value).replaceAll('\\', '\\\\').replaceAll(':', '\\:');
|
|
17
|
+
return [escape(plan.host), plan.port, '*', escape(user), escape(password)].join(':') + '\n';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function psql(program, plan, user, database, args, credentials) {
|
|
21
|
+
const env = {...process.env, PGCONNECT_TIMEOUT: '5', PGCLIENTENCODING: 'UTF8'};
|
|
22
|
+
for (const key of Object.keys(env)) {
|
|
23
|
+
if (['PGPASSWORD', 'PGPASSFILE', 'PGHOSTADDR', 'PGSERVICE', 'PGOPTIONS'].includes(key.toUpperCase())) delete env[key];
|
|
24
|
+
}
|
|
25
|
+
return execFileSync(program, ['-X', '-w', '-qAt', '-v', 'ON_ERROR_STOP=1',
|
|
26
|
+
'-h', plan.host, '-p', String(plan.port), '-U', user, '-d', database, ...args],
|
|
27
|
+
{env: {...env, ...credentials}, encoding: 'utf8', windowsHide: true, stdio: ['ignore', 'pipe', 'pipe']}).trim();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The generated application password is ASCII. Only its PostgreSQL SCRAM verifier enters role DDL.
|
|
31
|
+
function scramVerifier(password) {
|
|
32
|
+
const salt = randomBytes(16);
|
|
33
|
+
const iterations = 4096;
|
|
34
|
+
const salted = pbkdf2Sync(password, salt, iterations, 32, 'sha256');
|
|
35
|
+
const key = label => createHmac('sha256', salted).update(label).digest();
|
|
36
|
+
const stored = createHash('sha256').update(key('Client Key')).digest('base64');
|
|
37
|
+
return 'SCRAM-SHA-256$' + iterations + ':' + salt.toString('base64') + '$' + stored + ':' + key('Server Key').toString('base64');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function prepareDatabase(root, plan, {dataPath, pgProgram, startDatabase, stopDatabase}, adminPassword) {
|
|
16
41
|
const secret = JSON.parse(readFileSync(path.join(root, 'secrets/dev-secrets.json'), 'utf8'));
|
|
17
42
|
const password = secret.TYPESER_DATABASE_PASSWORD;
|
|
18
|
-
const
|
|
43
|
+
const program = pgProgram(root, plan, 'psql');
|
|
44
|
+
if (plan.provisionId) {
|
|
45
|
+
if (plan.mode !== 'existing' || !['localhost', '127.0.0.1', '::1'].includes(plan.host) || typeof adminPassword !== 'string')
|
|
46
|
+
throw new Error('Provisioning requires a local PostgreSQL server and an administrator login.');
|
|
47
|
+
const user = plan.adminUser ?? 'postgres';
|
|
48
|
+
const provision = credentials => psql(program, plan, user, 'postgres', ['-v', 'database=' + plan.database, '-v', 'owner=' + plan.user,
|
|
49
|
+
'-v', 'setup_id=' + plan.provisionId, '-f', fileURLToPath(new URL('./provision-server.sql', import.meta.url))],
|
|
50
|
+
{...credentials, TYPESER_SETUP_VERIFIER: scramVerifier(password)});
|
|
51
|
+
if (adminPassword) privateFile(root, passLine(plan, user, adminPassword), file => provision({PGPASSFILE: file}));
|
|
52
|
+
else provision({});
|
|
53
|
+
}
|
|
54
|
+
const data = plan.mode === 'new' ? dataPath(root) : undefined;
|
|
19
55
|
if (plan.mode === 'new' && !existsSync(path.join(data, 'PG_VERSION'))) {
|
|
20
56
|
if (existsSync(data)) throw new Error('The data directory is incomplete. Choose a fresh project directory.');
|
|
21
57
|
privateFile(root, password + '\n', file => {
|
|
@@ -27,17 +63,15 @@ export function prepareDatabase(root, plan, {dataPath, pgProgram, startDatabase,
|
|
|
27
63
|
}
|
|
28
64
|
const started = startDatabase(root, plan);
|
|
29
65
|
try {
|
|
30
|
-
|
|
31
|
-
const line = [escape(plan.host), plan.port, '*', escape(plan.user), escape(password)].join(':') + '\n';
|
|
32
|
-
privateFile(root, line, file => {
|
|
66
|
+
privateFile(root, passLine(plan, plan.user, password), file => {
|
|
33
67
|
function sql(database, args) {
|
|
34
|
-
|
|
35
|
-
for (const key of ['PGPASSWORD', 'PGHOSTADDR', 'PGSERVICE', 'PGOPTIONS']) delete env[key];
|
|
36
|
-
return execFileSync(pgProgram(root, plan, 'psql'), ['-X', '-w', '-qAt', '-v', 'ON_ERROR_STOP=1',
|
|
37
|
-
'-h', plan.host, '-p', String(plan.port), '-U', plan.user, '-d', database, ...args],
|
|
38
|
-
{env, encoding: 'utf8', windowsHide: true, stdio: ['ignore', 'pipe', 'pipe']}).trim();
|
|
68
|
+
return psql(program, plan, plan.user, database, args, {PGPASSFILE: file});
|
|
39
69
|
}
|
|
40
70
|
const prepared = db => sql(db, ['-c', "SELECT to_regclass('app.entity_schema') IS NOT NULL"]) === 't';
|
|
71
|
+
if (plan.provisionId) {
|
|
72
|
+
if (!prepared(plan.database)) sql(plan.database, ['-f', path.join(root, 'node_modules/typeser/src/sql/bootstrap/0100-app.sql')]);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
41
75
|
if (plan.mode === 'existing') {
|
|
42
76
|
if (sql(plan.database, ['-c', "SELECT current_setting('server_version_num')::int >= 180000 AND NOT pg_is_in_recovery()"]) !== 't'
|
|
43
77
|
|| !prepared(plan.database) || sql(plan.database, ['-c', "SELECT pg_has_role(current_user,'r1_runtime','SET')"]) !== 't')
|