Project

General

Profile

Download (12.6 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / html / install.js @ 13a707c6

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Install mappings/resources in Haketilo.
5
 *
6
 * Copyright (C) 2022 Wojtek Kosior
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#IMPORT common/indexeddb.js AS haketilodb
45
#IMPORT html/dialog.js
46
#IMPORT html/item_preview.js AS ip
47

    
48
#FROM common/browser.js    IMPORT browser
49
#FROM html/DOM_helpers.js  IMPORT clone_template, Showable
50
#FROM common/entities.js   IMPORT item_id_string, version_string, get_files
51
#FROM common/misc.js       IMPORT sha256_async AS compute_sha256
52
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
53

    
54
#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch
55

    
56
const coll = new Intl.Collator();
57

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

    
66
    const name_comparison = coll.compare(def1.long_name, def2.long_name);
67
    return name_comparison === 0 ?
68
	coll.compare(def1.identifier, def2.identifier) : name_comparison;
69
}
70

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

    
75
    this.item_name.innerText = item.def.long_name;
76
    this.item_id.innerText = item_id_string(item.def);
77
    if (item.db_def) {
78
	this.old_ver.innerText =
79
	    version_string(item.db_def.version, item.db_def.revision);
80
	this.update_info.classList.remove("hide");
81
    }
82

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

    
88
const container_ids = [
89
    "install_preview",
90
    "dialog_container",
91
    "mapping_preview_container",
92
    "resource_preview_container"
93
];
94

    
95
/*
96
 * Work object is used to communicate between asynchronously executing
97
 * functions when computing dependencies tree of an item and when fetching
98
 * files for installation.
99
 */
100
async function init_work() {
101
    const work = {
102
	waiting: 0,
103
	is_ok: true,
104
	db: (await haketilodb.get()),
105
	result: []
106
    };
107

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

    
112
	if (error)
113
	    console.error("Haketilo:", error);
114
	work.is_ok = false;
115
	work.reject_cb(user_message);
116
    }
117

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

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

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

    
127
    const show_container = name => {
128
	for (const cid of container_ids) {
129
	    if (cid !== name)
130
		this[cid].classList.add("hide");
131
	}
132
	this[name].classList.remove("hide");
133
    }
134

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

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

    
145
	return a;
146
    }
147

    
148
    this.previews_ctx = {};
149

    
150
    this.preview_item = item_def => {
151
	if (!this.shown)
152
	    return;
153

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

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

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

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

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

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

    
180

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

    
187
	if (!work.is_ok)
188
	    return;
189

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

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

    
201
	if (!work.is_ok)
202
	    return;
203

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

    
206
	const $id =
207
	      `https://hydrilla.koszko.org/schemas/api_${item_type}_description-1.0.1.schema.json`;
208
	const schema = haketilo_schemas[$id];
209
	const result = haketilo_validator.validate(json, schema);
210
	if (result.errors.length > 0) {
211
	    const reg = new RegExp(schema.allOf[2].properties.$schema.pattern);
212
	    if (json.$schema && !reg.test(json.$schema)) {
213
		const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
214
		return work.err(result.errors, msg);
215
	    }
216

    
217
	    const msg = `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`;
218
	    return work.err(result.errors, msg);
219
	}
220

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

    
224
	if (item_type === "mapping") {
225
	    for (const res_ref of Object.values(json.payloads || {}))
226
		process_item(work, "resource", res_ref.identifier);
227
	} else {
228
	    for (const res_ref of (json.dependencies || []))
229
		process_item(work, "resource", res_ref.identifier);
230
	}
231

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

    
250
	if (--work.waiting === 0)
251
	    work.resolve_cb(work.result);
252
    }
253

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

    
258
	process_item(work, item_type, item_id, item_ver);
259

    
260
	const items = await work_prom;
261
	items.sort((i1, i2) => compare_items(i1.def, i2.def));
262
	return items;
263
    }
264

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

    
270
	this.repo_url = repo_url;
271

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

    
274
	try {
275
	    var items = await compute_deps(item_type, item_id, item_ver);
276
	} catch(e) {
277
	    var dialog_prom = dialog.error(this.dialog_ctx, e);
278
	}
279

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

    
285
	if (dialog_prom) {
286
	    dialog.close(this.dialog_ctx);
287

    
288
	    await dialog_prom;
289

    
290
	    this.hide();
291
	    return;
292
	}
293

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

    
297
	dialog.close(this.dialog_ctx);
298
    }
299

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

    
304
	work.waiting++;
305

    
306
	try {
307
	    var file_uses = await haketilodb.idb_get(work.file_uses_transaction,
308
						     "file_uses", sha256);
309
	    if (!work.is_ok)
310
		return;
311
	} catch(e) {
312
	    const msg = "Error accessing Haketilo's internal database :(";
313
	    return work.err(e, msg);
314
	}
315

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

    
319
	    try {
320
		var response = await fetch(url);
321
		if (!work.is_ok)
322
		    return;
323
	    } catch(e) {
324
		const msg = "Failure to communicate with repository :(";
325
		return work.err(e, msg);
326
	    }
327

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

    
333
	    const text = await response.text();
334
	    if (!work.is_ok)
335
		return;
336

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

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

    
348
	if (--work.waiting === 0)
349
	    work.resolve_cb(work.result);
350
    }
351

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

    
356
	const processed_files = new Set();
357

    
358
	for (const item_def of item_defs) {
359
	    for (const file of get_files(item_def)) {
360
		if (!processed_files.has(file.sha256)) {
361
		    processed_files.add(file.sha256);
362
		    process_file(work, file.sha256);
363
		}
364
	    }
365
	}
366

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

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

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

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

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

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

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

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

    
394
		data[type] = set;
395
	    }
396

    
397
	    try {
398
		await haketilodb.save_items(data);
399
	    } catch(e) {
400
		console.error("Haketilo:", e);
401
		const msg = "Error writing to Haketilo's internal database :(";
402
		var dialog_prom = dialog.error(this.dialog_ctx, msg);
403
	    }
404
	}
405

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

    
411
	dialog.close(this.dialog_ctx);
412

    
413
	await dialog_prom;
414

    
415
	this.hide();
416
    }
417

    
418
    const hide_super = this.hide;
419
    this.hide = () => {
420
	if (!hide_super())
421
	    return;
422

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

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

    
430
    const install_cb = this.dialog_ctx.when_hidden(perform_install);
431
    this.install_but.addEventListener("click", install_cb);
432
}
433
#EXPORT InstallView
(11-11/27)