Refactor WKD lookup code (#1123)
* Replace chained then by await * Improve fetch fallback flow
This commit is contained in:
parent
20c468cbd7
commit
5801169432
34
src/wkd.js
34
src/wkd.js
|
@ -60,29 +60,19 @@ WKD.prototype.lookup = async function(options) {
|
||||||
const urlAdvanced = `https://openpgpkey.${domain}/.well-known/openpgpkey/${domain}/hu/${localEncoded}`;
|
const urlAdvanced = `https://openpgpkey.${domain}/.well-known/openpgpkey/${domain}/hu/${localEncoded}`;
|
||||||
const urlDirect = `https://${domain}/.well-known/openpgpkey/hu/${localEncoded}`;
|
const urlDirect = `https://${domain}/.well-known/openpgpkey/hu/${localEncoded}`;
|
||||||
|
|
||||||
return fetch(urlAdvanced).then(function(response) {
|
let response = await fetch(urlAdvanced);
|
||||||
if (response.status === 200) {
|
if (response.status !== 200) {
|
||||||
return response.arrayBuffer();
|
response = await fetch(urlDirect);
|
||||||
|
if (response.status !== 200) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}).then(function(publicKey) {
|
}
|
||||||
if (publicKey) {
|
|
||||||
return publicKey;
|
const rawBytes = new Uint8Array(await response.arrayBuffer());
|
||||||
} else {
|
if (options.rawBytes) {
|
||||||
return fetch(urlDirect).then(function(response) {
|
return rawBytes;
|
||||||
if (response.status === 200) {
|
}
|
||||||
return response.arrayBuffer();
|
return keyMod.read(rawBytes);
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}).then(function(publicKey) {
|
|
||||||
if (publicKey) {
|
|
||||||
const rawBytes = new Uint8Array(publicKey);
|
|
||||||
if (options.rawBytes) {
|
|
||||||
return rawBytes;
|
|
||||||
}
|
|
||||||
return keyMod.read(rawBytes);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default WKD;
|
export default WKD;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user