Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Libre
Samizdat
Commits
95176033
Commit
95176033
authored
Sep 18, 2020
by
Michał Woźniak
Browse files
WIP #64: using 3 gateways and returning first successful result
parent
17c31bcc
Changes
1
Hide whitespace changes
Inline
Side-by-side
plugins/gateway-ipns.js
View file @
95176033
...
@@ -22,18 +22,58 @@ var gateways = [
...
@@ -22,18 +22,58 @@ var gateways = [
'
https://ipfs.best-practice.se/ipns/
'
// Sweden
'
https://ipfs.best-practice.se/ipns/
'
// Sweden
]
]
/*
* to do this right we need a Promise.any() polyfill
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
*/
Promise
.
any
=
async
(
promises
)
=>
{
// Promise.all() is the polar opposite of Promise.any()
// in that it returns as soon as there is a first rejection
// but without it, it returns an array of resolved results
return
Promise
.
all
(
promises
.
map
(
p
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
// swap reject and resolve, so that we can use Promise.all()
// and get the result we need
Promise
.
resolve
(
p
).
then
(
reject
,
resolve
)
);
})
// now, swap errors and values back
).
then
(
err
=>
Promise
.
reject
(
err
),
val
=>
Promise
.
resolve
(
val
)
);
};
/**
/**
* getting content using regular HTTP(S) fetch()
* getting content using regular HTTP(S) fetch()
*/
*/
let
fetchContentFromGatewayIPNS
=
(
url
)
=>
{
let
fetchContentFromGatewayIPNS
=
(
url
)
=>
{
// we're going to try a random gateway and building an URL of the form:
// we're going to try a random gateway and building an URL of the form:
// https://<gateway_address>/<ipnsPubkey>/<rest_of_URL>
// https://<gateway_address>/<ipnsPubkey>/<rest_of_URL>
var
ipnsUrl
=
gateways
[
Math
.
floor
(
Math
.
random
()
*
gateways
.
length
)]
+
ipnsPubKey
+
url
.
replace
(
/https
?
:
\/\/[^/]
+/
,
''
)
var
ipnsUrl
=
ipnsPubKey
+
url
.
replace
(
/https
?
:
\/\/[^/]
+/
,
''
)
// pick 3 gateways, at random
var
useGateways
=
new
Array
()
while
(
useGateways
.
length
<
3
)
{
// put in the address while we're at it
useGateways
.
push
(
gateways
.
splice
(
Math
.
floor
(
Math
.
random
()
*
gateways
.
length
),
1
)[
0
]
+
ipnsUrl
)
}
console
.
log
(
`Samizdat: gateway IPNS:\n+-- fetching:
${
ipnsUrl
}
`
)
// debug log
console
.
log
(
`Samizdat: gateway IPNS fetching:\n
${
useGateways
.
join
(
'
\n
'
)}
`
)
return
fetch
(
ipnsUrl
,
{
cache
:
"
reload
"
})
return
Promise
.
any
(
useGateways
.
map
(
u
=>
fetch
(
u
,
{
cache
:
"
reload
"
})
))
.
then
((
response
)
=>
{
.
then
((
response
)
=>
{
// 4xx? 5xx? that's a paddlin'
// 4xx? 5xx? that's a paddlin'
if
(
response
.
status
>=
400
)
{
if
(
response
.
status
>=
400
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment