aboutsummaryrefslogtreecommitdiff
path: root/html/install.js
blob: 82df6613f9bad7299040a834f426756103b94d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
 * This file is part of Haketilo.
 *
 * Function: Install mappings/resources in Haketilo.
 *
 * Copyright (C) 2022 Wojtek Kosior
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * As additional permission under GNU GPL version 3 section 7, you
 * may distribute forms of that code without the copy of the GNU
 * GPL normally required by section 4, provided you include this
 * license notice and, in case of non-source distribution, a URL
 * through which recipients can access the Corresponding Source.
 * If you modify file(s) with this exception, you may extend this
 * exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this
 * exception statement from your version.
 *
 * As a special exception to the GPL, any HTML file which merely
 * makes function calls to this code, and for that purpose
 * includes it by reference shall be deemed a separate work for
 * copyright law purposes. If you modify this code, you may extend
 * this exception to your version of the code, but you are not
 * obligated to do so. If you do not wish to do so, delete this
 * exception statement from your version.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
 * license. Although I request that you do not make use of this code in a
 * proprietary program, I am not going to enforce this in court.
 */

#IMPORT common/indexeddb.js AS haketilodb
#IMPORT html/dialog.js
#IMPORT html/item_preview.js AS ip

#FROM common/browser.js    IMPORT browser
#FROM html/DOM_helpers.js  IMPORT clone_template, Showable
#FROM common/entities.js   IMPORT item_id_string, version_string, get_files
#FROM common/misc.js       IMPORT sha256_async AS compute_sha256
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
#FROM common/entities.js   IMPORT haketilo_schema_name_regex

#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch

const coll = new Intl.Collator();

/*
 * Comparator used to sort items in the order we want them to appear in
 * install dialog: first mappings alphabetically, then resources alphabetically.
 */
function compare_items(def1, def2) {
    if (def1.type !== def2.type)
	return def1.type === "mapping" ? -1 : 1;

    const name_comparison = coll.compare(def1.long_name, def2.long_name);
    return name_comparison === 0 ?
	coll.compare(def1.identifier, def2.identifier) : name_comparison;
}

function ItemEntry(install_view, item) {
    Object.assign(this, clone_template("install_list_entry"));
    this.item_def = item.def;

    this.item_name.innerText = item.def.long_name;
    this.item_id.innerText = item_id_string(item.def);
    if (item.db_def) {
	this.old_ver.innerText =
	    version_string(item.db_def.version, item.db_def.revision);
	this.update_info.classList.remove("hide");
    }

    let preview_cb = () => install_view.preview_item(item.def);
    preview_cb = install_view.dialog_ctx.when_hidden(preview_cb);
    this.details_but.addEventListener("click", preview_cb);
}

const container_ids = [
    "install_preview",
    "dialog_container",
    "mapping_preview_container",
    "resource_preview_container"
];

/*
 * Work object is used to communicate between asynchronously executing
 * functions when computing dependencies tree of an item and when fetching
 * files for installation.
 */
async function init_work() {
    const work = {
	waiting: 0,
	is_ok: true,
	db: (await haketilodb.get()),
	result: []
    };

    work.err = function (error, user_message) {
	if (!this.is_ok)
	    return;

	if (error)
	    console.error("Haketilo:", error);
	work.is_ok = false;
	work.reject_cb(user_message);
    }

    return [work,
	    new Promise((...cbs) => [work.resolve_cb, work.reject_cb] = cbs)];
}

function InstallView(tab_id, on_view_show, on_view_hide) {
    Showable.call(this, on_view_show, on_view_hide);

    Object.assign(this, clone_template("install_view"));

    const show_container = name => {
	for (const cid of container_ids) {
	    if (cid !== name)
		this[cid].classList.add("hide");
	}
	this[name].classList.remove("hide");
    }

    this.dialog_ctx = dialog.make(() => show_container("dialog_container"),
				  () => show_container("install_preview"));
    this.dialog_container.prepend(this.dialog_ctx.main_div);

    /* Make a link to view a file from the repository. */
    const make_file_link = (preview_ctx, file_ref) => {
	const a = document.createElement("a");
	a.href = `${this.repo_url}file/sha256/${file_ref.sha256}`;
	a.innerText = file_ref.file;

	return a;
    }

    this.previews_ctx = {};

    this.preview_item = item_def => {
	if (!this.shown)
	    return;

	const fun = ip[`${item_def.type}_preview`];
	const preview_ctx = fun(item_def, this.previews_ctx[item_def.type],
				make_file_link);
	this.previews_ctx[item_def.type] = preview_ctx;

	const container_name = `${item_def.type}_preview_container`;
	show_container(container_name);
	this[container_name].prepend(preview_ctx.main_div);
    }

    let back_cb = () => show_container("install_preview");
    back_cb = this.dialog_ctx.when_hidden(back_cb);
    for (const type of ["resource", "mapping"])
	this[`${type}_back_but`].addEventListener("click", back_cb);

    const process_item = async (work, item_type, id, ver) => {
	if (!work.is_ok || work.processed_by_type[item_type].has(id))
	    return;

	work.processed_by_type[item_type].add(id);
	work.waiting++;

	const url = ver ?
	      `${this.repo_url}${item_type}/${id}/${ver.join(".")}` :
	      `${this.repo_url}${item_type}/${id}.json`;


	try {
	    var response = await indirect_fetch(tab_id, url);
	} catch(e) {
	    return work.err(e, "Failure to communicate with repository :(");
	}

	if (!work.is_ok)
	    return;

	if (!response.ok) {
	    return work.err(null,
			    `Repository sent HTTP code ${response.status} :(`);
	}

	try {
	    var json = await response.json();
	} catch(e) {
	    return work.err(e, "Repository's response is not valid JSON :(");
	}

	if (!work.is_ok)
	    return;

	const captype = item_type[0].toUpperCase() + item_type.substring(1);

	const nonconforming_format_error_msg =
	      `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`;

	try {
	    const match = haketilo_schema_name_regex.exec(json.$schema);
	    var major_schema_version = match.groups.major;

	    if (!["1", "2"].includes(major_schema_version)) {
		const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
		return work.err(null, msg);
	    }
	} catch(e) {
	    return work.err(e, nonconforming_format_error_msg);
	}

	const schema_name = `api_${item_type}_description-${major_schema_version}.schema.json`;

	const schema = haketilo_schemas[schema_name];
	const result = haketilo_validator.validate(json, schema);
	if (result.errors.length > 0)
	    return work.err(result.errors, nonconforming_format_error_msg);

	const scripts = item_type === "resource" && json.scripts;
	const files = json.source_copyright.concat(scripts || []);

	if (item_type === "mapping") {
	    for (const res_ref of Object.values(json.payloads || {}))
		process_item(work, "resource", res_ref.identifier);
	} else {
	    for (const res_ref of (json.dependencies || []))
		process_item(work, "resource", res_ref.identifier);
	}

	if (major_schema_version >= 2) {
	    for (const map_ref of (json.required_mappings || []))
		process_item(work, "mapping", map_ref.identifier);
	}

	/*
	 * At this point we already have JSON definition of the item and we
	 * triggered processing of its dependencies. We now have to verify if
	 * the same or newer version of the item is already present in the
	 * database and if so - omit this item.
	 */
	const transaction = work.db.transaction(item_type);
	try {
	    var db_def = await haketilodb.idb_get(transaction, item_type, id);
	    if (!work.is_ok)
		return;
	} catch(e) {
	    const msg = "Error accessing Haketilo's internal database :(";
	    return work.err(e, msg);
	}
	if (!db_def || db_def.version < json.version)
	    work.result.push({def: json, db_def});

	if (--work.waiting === 0)
	    work.resolve_cb(work.result);
    }

    async function compute_deps(item_type, item_id, item_ver) {
	const [work, work_prom] = await init_work();
	work.processed_by_type = {"mapping" : new Set(), "resource": new Set()};

	process_item(work, item_type, item_id, item_ver);

	const items = await work_prom;
	items.sort((i1, i2) => compare_items(i1.def, i2.def));
	return items;
    }

    const show_super = this.show;
    this.show = async (repo_url, item_type, item_id, item_ver) => {
	if (!show_super())
	    return;

	this.repo_url = repo_url;

	dialog.loader(this.dialog_ctx, "Fetching data from repository...");

	try {
	    var items = await compute_deps(item_type, item_id, item_ver);
	} catch(e) {
	    var dialog_prom = dialog.error(this.dialog_ctx, e);
	}

	if (!dialog_prom && items.length === 0) {
	    const msg = "Nothing to do - packages already installed.";
	    var dialog_prom = dialog.info(this.dialog_ctx, msg);
	}

	if (dialog_prom) {
	    dialog.close(this.dialog_ctx);

	    await dialog_prom;

	    this.hide();
	    return;
	}

	this.item_entries = items.map(i => new ItemEntry(this, i));
	this.to_install_list.append(...this.item_entries.map(ie => ie.main_li));

	dialog.close(this.dialog_ctx);
    }

    const process_file = async (work, sha256) => {
	if (!work.is_ok)
	    return;

	work.waiting++;

	try {
	    var file_uses = await haketilodb.idb_get(work.file_uses_transaction,
						     "file_uses", sha256);
	    if (!work.is_ok)
		return;
	} catch(e) {
	    const msg = "Error accessing Haketilo's internal database :(";
	    return work.err(e, msg);
	}

	if (!file_uses) {
	    const url = `${this.repo_url}file/sha256/${sha256}`;

	    try {
		var response = await fetch(url);
		if (!work.is_ok)
		    return;
	    } catch(e) {
		const msg = "Failure to communicate with repository :(";
		return work.err(e, msg);
	    }

	    if (!response.ok) {
		const msg = `Repository sent HTTP code ${response.status} :(`;
		return work.err(null, msg);
	    }

	    const text = await response.text();
	    if (!work.is_ok)
		return;

	    const digest = await compute_sha256(text);
	    if (!work.is_ok)
		return;
	    if (digest !== sha256) {
		const msg = `${url} served a file with different SHA256 cryptographic sum :(`;
		return work.err(null, msg);
	    }

	    work.result.push([sha256, text]);
	}

	if (--work.waiting === 0)
	    work.resolve_cb(work.result);
    }

    const get_missing_files = async item_defs => {
	const [work, work_prom] = await init_work();
	work.file_uses_transaction = work.db.transaction("file_uses");

	const processed_files = new Set();

	for (const item_def of item_defs) {
	    for (const file of get_files(item_def)) {
		if (!processed_files.has(file.sha256)) {
		    processed_files.add(file.sha256);
		    process_file(work, file.sha256);
		}
	    }
	}

	return processed_files.size > 0 ? work_prom : [];
    }

    const perform_install = async () => {
	if (!this.show || !this.item_entries)
	    return;

	dialog.loader(this.dialog_ctx, "Installing...");

	const item_defs = this.item_entries.map(ie => ie.item_def);

	try {
	    var files = (await get_missing_files(item_defs))
		.reduce((ac, [h, txt]) => Object.assign(ac, {[h]: txt}), {});
	} catch(e) {
	    var dialog_prom = dialog.error(this.dialog_ctx, e);
	}

	if (files !== undefined) {
	    const data = {file: {sha256: files}};

	    for (const type of ["resource", "mapping"]) {
		const set = {};

		for (const def of item_defs.filter(def => def.type === type))
		    set[def.identifier] = {[version_string(def.version)]: def};

		data[type] = set;
	    }

	    try {
		await haketilodb.save_items(data);
	    } catch(e) {
		console.error("Haketilo:", e);
		const msg = "Error writing to Haketilo's internal database :(";
		var dialog_prom = dialog.error(this.dialog_ctx, msg);
	    }
	}

	if (!dialog_prom) {
	    const msg = "Successfully installed!";
	    var dialog_prom = dialog.info(this.dialog_ctx, msg);
	}

	dialog.close(this.dialog_ctx);

	await dialog_prom;

	this.hide();
    }

    const hide_super = this.hide;
    this.hide = () => {
	if (!hide_super())
	    return;

	delete this.item_entries;
	[...this.to_install_list.children].forEach(n => n.remove());
    }

    const hide_cb = this.dialog_ctx.when_hidden(this.hide);
    this.cancel_but.addEventListener("click", hide_cb);

    const install_cb = this.dialog_ctx.when_hidden(perform_install);
    this.install_but.addEventListener("click", install_cb);
}
#EXPORT InstallView