Revision bbc9fae4
Added by koszko over 1 year ago
| html/install.js | ||
|---|---|---|
| 51 | 51 |
#FROM common/misc.js IMPORT sha256_async AS compute_sha256 |
| 52 | 52 |
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas |
| 53 | 53 |
|
| 54 |
#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch |
|
| 55 |
|
|
| 54 | 56 |
const coll = new Intl.Collator(); |
| 55 | 57 |
|
| 56 | 58 |
/* |
| ... | ... | |
| 104 | 106 |
}; |
| 105 | 107 |
|
| 106 | 108 |
work.err = function (error, user_message) {
|
| 109 |
if (!this.is_ok) |
|
| 110 |
return; |
|
| 111 |
|
|
| 107 | 112 |
if (error) |
| 108 | 113 |
console.error("Haketilo:", error);
|
| 109 | 114 |
work.is_ok = false; |
| ... | ... | |
| 171 | 176 |
const url = ver ? |
| 172 | 177 |
`${this.repo_url}${item_type}/${id}/${ver.join(".")}` :
|
| 173 | 178 |
`${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 |
|
| 179 |
if ("error" in response) {
|
|
| 180 |
return work.err(response.error, |
|
| 181 |
"Failure to communicate with repository :(");
|
|
| 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 :(");
|
|
| 182 | 185 |
} |
| 183 | 186 |
|
| 187 |
if (!work.is_ok) |
|
| 188 |
return; |
|
| 189 |
|
|
| 184 | 190 |
if (!response.ok) {
|
| 185 | 191 |
return work.err(null, |
| 186 | 192 |
`Repository sent HTTP code ${response.status} :(`);
|
| 187 | 193 |
} |
| 188 | 194 |
|
| 189 |
if ("error_json" in response) {
|
|
| 190 |
return work.err(response.error_json, |
|
| 191 |
"Repository's response is not valid JSON :(");
|
|
| 195 |
try {
|
|
| 196 |
var json = await response.json(); |
|
| 197 |
} catch(e) {
|
|
| 198 |
return work.err(e, "Repository's response is not valid JSON :(");
|
|
| 192 | 199 |
} |
| 193 | 200 |
|
| 201 |
if (!work.is_ok) |
|
| 202 |
return; |
|
| 203 |
|
|
| 194 | 204 |
const captype = item_type[0].toUpperCase() + item_type.substring(1); |
| 195 | 205 |
|
| 196 | 206 |
const $id = |
| 197 | 207 |
`https://hydrilla.koszko.org/schemas/api_${item_type}_description-1.0.1.schema.json`;
|
| 198 | 208 |
const schema = haketilo_schemas[$id]; |
| 199 |
const result = haketilo_validator.validate(response.json, schema);
|
|
| 209 |
const result = haketilo_validator.validate(json, schema); |
|
| 200 | 210 |
if (result.errors.length > 0) {
|
| 201 | 211 |
const reg = new RegExp(schema.allOf[2].properties.$schema.pattern); |
| 202 |
if (response.json.$schema && !reg.test(response.json.$schema)) {
|
|
| 212 |
if (json.$schema && !reg.test(json.$schema)) {
|
|
| 203 | 213 |
const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
|
| 204 | 214 |
return work.err(result.errors, msg); |
| 205 | 215 |
} |
| ... | ... | |
| 208 | 218 |
return work.err(result.errors, msg); |
| 209 | 219 |
} |
| 210 | 220 |
|
| 211 |
const scripts = item_type === "resource" && response.json.scripts;
|
|
| 212 |
const files = response.json.source_copyright.concat(scripts || []);
|
|
| 221 |
const scripts = item_type === "resource" && json.scripts; |
|
| 222 |
const files = json.source_copyright.concat(scripts || []); |
|
| 213 | 223 |
|
| 214 | 224 |
if (item_type === "mapping") {
|
| 215 |
for (const res_ref of Object.values(response.json.payloads || {}))
|
|
| 225 |
for (const res_ref of Object.values(json.payloads || {}))
|
|
| 216 | 226 |
process_item(work, "resource", res_ref.identifier); |
| 217 | 227 |
} else {
|
| 218 |
for (const res_ref of (response.json.dependencies || []))
|
|
| 228 |
for (const res_ref of (json.dependencies || [])) |
|
| 219 | 229 |
process_item(work, "resource", res_ref.identifier); |
| 220 | 230 |
} |
| 221 | 231 |
|
| ... | ... | |
| 234 | 244 |
const msg = "Error accessing Haketilo's internal database :(";
|
| 235 | 245 |
return work.err(e, msg); |
| 236 | 246 |
} |
| 237 |
if (!db_def || db_def.version < response.json.version)
|
|
| 238 |
work.result.push({def: response.json, db_def});
|
|
| 247 |
if (!db_def || db_def.version < json.version) |
|
| 248 |
work.result.push({def: json, db_def});
|
|
| 239 | 249 |
|
| 240 | 250 |
if (--work.waiting === 0) |
| 241 | 251 |
work.resolve_cb(work.result); |
| ... | ... | |
| 320 | 330 |
return work.err(null, msg); |
| 321 | 331 |
} |
| 322 | 332 |
|
| 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 |
} |
|
| 333 |
const text = await response.text(); |
|
| 334 |
if (!work.is_ok) |
|
| 335 |
return; |
|
| 331 | 336 |
|
| 332 | 337 |
const digest = await compute_sha256(text); |
| 333 | 338 |
if (!work.is_ok) |
Also available in: Unified diff
serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage