Project

General

Profile

« Previous | Next » 

Revision bbc9fae4

Added by koszko over 1 year ago

serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage

View differences:

html/repo_query.js
49 49
#FROM html/install.js      IMPORT InstallView
50 50
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
51 51

  
52
#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch
53

  
52 54
const coll = new Intl.Collator();
53 55

  
54 56
function ResultEntry(repo_entry, mapping_ref) {
......
76 78

  
77 79
    this.repo_url_label.innerText = repo_url;
78 80

  
79
    const query_results = async () => {
80
	const msg = [
81
	    "repo_query",
82
	    `${repo_url}query?url=${encodeURIComponent(query_view.url)}`
83
	];
84
	const response = await browser.tabs.sendMessage(query_view.tab_id, msg);
81
    const encoded_queried_url = encodeURIComponent(query_view.url);
82
    const query_url = `${repo_url}query?url=${encoded_queried_url}`;
85 83

  
86
	if ("error" in response)
84
    const query_results = async () => {
85
	try {
86
	    var response = await indirect_fetch(query_view.tab_id, query_url);
87
	} catch(e) {
88
	    console.error("Haketilo:", e);
87 89
	    throw "Failure to communicate with repository :(";
90
	}
88 91

  
89 92
	if (!response.ok)
90 93
	    throw `Repository sent HTTP code ${response.status} :(`;
91
	if ("error_json" in response)
94

  
95
	try {
96
	    var json = await response.json();
97
	} catch(e) {
98
	    console.error("Haketilo:", e);
92 99
	    throw "Repository's response is not valid JSON :(";
100
	}
93 101

  
94 102
	const $id =
95 103
	      `https://hydrilla.koszko.org/schemas/api_query_result-1.0.1.schema.json`;
96 104
	const schema = haketilo_schemas[$id];
97
	const result = haketilo_validator.validate(response.json, schema);
105
	const result = haketilo_validator.validate(json, schema);
98 106
	if (result.errors.length > 0) {
99 107
	    console.error("Haketilo:", result.errors);
100 108

  
101 109
	    const reg = new RegExp(schema.properties.$schema.pattern);
102
	    if (response.json.$schema && !reg.test(response.json.$schema))
110
	    if (json.$schema && !reg.test(json.$schema))
103 111
		throw "Results were served using unsupported Hydrilla API version. You might need to update Haketilo.";
104 112

  
105 113
	    throw "Results were served using a nonconforming response format.";
106 114
	}
107 115

  
108
	return response.json.mappings;
116
	return json.mappings;
109 117
    }
110 118

  
111 119
    const populate_results = async () => {

Also available in: Unified diff