Project

General

Profile

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

haketilo / html / install.js @ 57ce414c

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
const coll = new Intl.Collator();
55

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

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

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

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

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

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

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

    
106
    work.err = function (error, user_message) {
107
	if (error)
108
    	    console.error(error);
109
	work.is_ok = false;
110
	work.reject_cb(user_message);
111
    }
112

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

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

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

    
122
    const show_container = name => {
123
	for (const cid of container_ids) {
124
	    if (cid !== name)
125
		this[cid].classList.add("hide");
126
	}
127
	this[name].classList.remove("hide");
128
    }
129

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

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

    
140
	return a;
141
    }
142

    
143
    this.previews_ctx = {};
144

    
145
    this.preview_item = item_def => {
146
	if (!this.shown)
147
	    return;
148

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

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

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

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

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

    
171
	const url = ver ?
172
	      `${this.repo_url}${item_type}/${id}/${ver.join(".")}` :
173
	      `${this.repo_url}${item_type}/${id}.json`;
174
	const response =
175
	      await browser.tabs.sendMessage(tab_id, ["repo_query", url]);
176
	if (!work.is_ok)
177
	    return;
178

    
179
	if ("error" in response) {
180
	    return work.err(response.error,
181
			    "Failure to communicate with repository :(");
182
	}
183

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

    
189
	if ("error_json" in response) {
190
	    return work.err(response.error_json,
191
			    "Repository's response is not valid JSON :(");
192
	}
193

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

    
196
	const $id =
197
	      `https://hydrilla.koszko.org/schemas/api_${item_type}_description-1.0.1.schema.json`;
198
	const schema = haketilo_schemas[$id];
199
	const result = haketilo_validator.validate(response.json, schema);
200
	if (result.errors.length > 0) {
201
	    const reg = new RegExp(schema.allOf[2].properties.$schema.pattern);
202
	    if (response.json.$schema && !reg.test(response.json.$schema)) {
203
		const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
204
		return work.err(result.errors, msg);
205
	    }
206

    
207
	    const msg = `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`;
208
	    return work.err(result.errors, msg);
209
	}
210

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

    
214
	if (item_type === "mapping") {
215
	    for (const res_ref of Object.values(response.json.payloads || {}))
216
		process_item(work, "resource", res_ref.identifier);
217
	} else {
218
	    for (const res_ref of (response.json.dependencies || []))
219
		process_item(work, "resource", res_ref.identifier);
220
	}
221

    
222
	/*
223
	 * At this point we already have JSON definition of the item and we
224
	 * triggered processing of its dependencies. We now have to verify if
225
	 * the same or newer version of the item is already present in the
226
	 * database and if so - omit this item.
227
	 */
228
	const transaction = work.db.transaction(item_type);
229
	try {
230
	    var db_def = await haketilodb.idb_get(transaction, item_type, id);
231
	    if (!work.is_ok)
232
		return;
233
	} catch(e) {
234
	    const msg = "Error accessing Haketilo's internal database :(";
235
	    return work.err(e, msg);
236
	}
237
	if (!db_def || db_def.version < response.json.version)
238
	    work.result.push({def: response.json, db_def});
239

    
240
	if (--work.waiting === 0)
241
	    work.resolve_cb(work.result);
242
    }
243

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

    
248
	process_item(work, item_type, item_id, item_ver);
249

    
250
	const items = await work_prom;
251
	items.sort((i1, i2) => compare_items(i1.def, i2.def));
252
	return items;
253
    }
254

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

    
260
	this.repo_url = repo_url;
261

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

    
264
	try {
265
	    var items = await compute_deps(item_type, item_id, item_ver);
266
	} catch(e) {
267
	    var dialog_prom = dialog.error(this.dialog_ctx, e);
268
	}
269

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

    
275
	if (dialog_prom) {
276
	    dialog.close(this.dialog_ctx);
277

    
278
	    await dialog_prom;
279

    
280
	    this.hide();
281
	    return;
282
	}
283

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

    
287
	dialog.close(this.dialog_ctx);
288
    }
289

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

    
294
	work.waiting++;
295

    
296
	try {
297
	    var file_uses = await haketilodb.idb_get(work.file_uses_transaction,
298
						     "file_uses", sha256);
299
	    if (!work.is_ok)
300
		return;
301
	} catch(e) {
302
	    const msg = "Error accessing Haketilo's internal database :(";
303
	    return work.err(e, msg);
304
	}
305

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

    
309
	    try {
310
		var response = await fetch(url);
311
		if (!work.is_ok)
312
		    return;
313
	    } catch(e) {
314
		const msg = "Failure to communicate with repository :(";
315
		return work.err(e, msg);
316
	    }
317

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

    
323
	    try {
324
		var text = await response.text();
325
		if (!work.is_ok)
326
		    return;
327
	    } catch(e) {
328
		const msg = "Repository's response is not valid text :(";
329
		return work.err(e, msg);
330
	    }
331

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

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

    
343
	if (--work.waiting === 0)
344
	    work.resolve_cb(work.result);
345
    }
346

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

    
351
	const processed_files = new Set();
352

    
353
	for (const item_def of item_defs) {
354
	    for (const file of get_files(item_def)) {
355
		if (!processed_files.has(file.sha256)) {
356
		    processed_files.add(file.sha256);
357
		    process_file(work, file.sha256);
358
		}
359
	    }
360
	}
361

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

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

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

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

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

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

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

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

    
389
		data[type] = set;
390
	    }
391

    
392
	    try {
393
		await haketilodb.save_items(data);
394
	    } catch(e) {
395
		console.error(e);
396
		const msg = "Error writing to Haketilo's internal database :(";
397
		var dialog_prom = dialog.error(this.dialog_ctx, msg);
398
	    }
399
	}
400

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

    
406
	dialog.close(this.dialog_ctx);
407

    
408
	await dialog_prom;
409

    
410
	this.hide();
411
    }
412

    
413
    const hide_super = this.hide;
414
    this.hide = () => {
415
	if (!hide_super())
416
	    return;
417

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

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

    
425
    const install_cb = this.dialog_ctx.when_hidden(perform_install);
426
    this.install_but.addEventListener("click", install_cb);
427
}
428
#EXPORT InstallView
(11-11/27)