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
bf7c505c
Commit
bf7c505c
authored
Oct 05, 2020
by
Michał Woźniak
Browse files
WIP #71: gun-ipfs now uses self.log()
parent
e4ae9157
Changes
1
Hide whitespace changes
Inline
Side-by-side
plugins/gun-ipfs.js
View file @
bf7c505c
...
...
@@ -44,7 +44,6 @@
* and `window` does not exist in ServiceWorker context
*/
if
(
typeof
window
===
'
undefined
'
)
{
console
.
log
(
'
(COMMIT_UNKNOWN) redefining window...
'
)
var
window
=
self
;
}
...
...
@@ -57,52 +56,53 @@
if
(
typeof
self
.
importScripts
!==
'
undefined
'
)
{
self
.
importScripts
.
apply
(
self
,
args
)
}
else
{
console
.
log
(
'
(COMMIT_UNKNOWN) assuming these scripts are already included:
'
)
args
.
forEach
(
function
(
src
){
console
.
log
(
'
+--
'
,
src
)
})
self
.
log
(
config
.
name
,
'
assuming these scripts are already included:
\n
'
,
args
.
join
(
'
\n
+--
'
)
)
}
}
async
function
setup_ipfs
()
{
if
(
ipfs
===
undefined
)
{
ipfs
=
false
// we don't want to start a few times over
console
.
log
(
'
(COMMIT_UNKNOWN) I
mporting IPFS-related libraries...
'
);
self
.
log
(
config
.
name
,
'
i
mporting IPFS-related libraries...
'
);
doImport
(
"
./lib/ipfs.js
"
);
console
.
log
(
'
(COMMIT_UNKNOWN) S
etting up IPFS...
'
)
self
.
log
(
config
.
name
,
'
s
etting up IPFS...
'
)
ipfs
=
await
self
.
Ipfs
.
create
();
console
.
log
(
'
+-- IPFS loaded :: ipfs is :
'
+
typeof
ipfs
)
self
.
log
(
config
.
name
,
'
+-- IPFS loaded :: ipfs is :
'
+
typeof
ipfs
)
}
}
async
function
setup_gun
()
{
if
(
gun
===
undefined
)
{
gun
=
false
// we don't want to start a few times over
console
.
log
(
'
(COMMIT_UNKNOWN) I
mporting Gun-related libraries...
'
);
self
.
log
(
config
.
name
,
'
i
mporting Gun-related libraries...
'
);
doImport
(
"
./lib/gun.js
"
,
"
./lib/sea.js
"
,
"
./lib/webrtc.js
"
);
console
.
log
(
'
(COMMIT_UNKNOWN) S
etting up Gun...
'
)
self
.
log
(
config
.
name
,
'
s
etting up Gun...
'
)
gun
=
Gun
([
'
https://gunjs.herokuapp.com/gun
'
,
'
https://samizdat.is/gun
'
]);
console
.
log
(
'
+--
G
un loaded :: gun is :
'
+
typeof
gun
);
self
.
log
(
config
.
name
,
'
+--
g
un loaded :: gun is :
'
+
typeof
gun
);
}
if
(
(
gun
!==
false
)
&&
(
gun
!==
undefined
)
&&
(
gunUser
===
undefined
)
)
{
gunUser
=
false
// we don't want to start a few times over
console
.
log
(
'
(COMMIT_UNKNOWN) S
etting up gunUser...
'
)
self
.
log
(
config
.
name
,
'
s
etting up gunUser...
'
)
gunUser
=
gun
.
user
(
config
.
gunPubkey
)
console
.
log
(
'
+--
G
un init complete :: gunUser is:
'
+
typeof
gunUser
);
self
.
log
(
config
.
name
,
'
+--
g
un init complete :: gunUser is:
'
+
typeof
gunUser
);
}
}
async
function
setup_gun_ipfs
()
{
if
(
ipfs
===
undefined
||
gun
===
undefined
)
{
console
.
log
(
'
(COMMIT_UNKNOWN) Setting up Samizdat
...
'
)
self
.
log
(
config
.
name
,
'
setting up
...
'
)
setup_ipfs
();
setup_gun
();
}
else
{
console
.
log
(
'
(COMMIT_UNKNOWN) Samizdat
setup already underway (ipfs:
'
+
(
(
ipfs
)
?
'
done
'
:
'
loading
'
)
+
'
, gun:
'
+
(
(
gun
)
?
'
done
'
:
'
loading
'
)
+
'
)
'
)
self
.
log
(
config
.
name
,
'
setup already underway (ipfs:
'
+
(
(
ipfs
)
?
'
done
'
:
'
loading
'
)
+
'
, gun:
'
+
(
(
gun
)
?
'
done
'
:
'
loading
'
)
+
'
)
'
)
}
}
...
...
@@ -114,9 +114,12 @@
let
getGunData
=
(
gunaddr
)
=>
{
return
new
Promise
(
(
resolve
,
reject
)
=>
{
console
.
log
(
'
(COMMIT_UNKNOWN) getGunData()
'
);
console
.
log
(
'
(COMMIT_UNKNOWN) getGunData() :: +-- gunUser :
'
+
typeof
gunUser
);
console
.
log
(
'
(COMMIT_UNKNOWN) getGunData() :: +-- gunaddr[] :
'
+
gunaddr
);
self
.
log
(
config
.
name
,
'
getGunData()
\n
'
,
`+-- gunUser :
${
typeof
gunUser
}
\n`
,
`+-- gunaddr[] :
${
gunaddr
}
`
);
// get the data
gunUser
...
...
@@ -124,7 +127,7 @@
.
get
(
gunaddr
[
1
])
.
once
(
function
(
addr
){
if
(
typeof
addr
!==
'
undefined
'
)
{
console
.
log
(
"
2.1 IPFS address: '
"
+
addr
+
"
'
"
);
self
.
log
(
config
.
name
,
`IPFS address: "
${
addr
}
"`
);
resolve
(
addr
);
}
else
{
// looks like we didn't get anything
...
...
@@ -147,13 +150,17 @@
* if the gunaddr[1] ends in '/', append 'index.html' to it
*/
if
(
gunaddr
[
1
].
charAt
(
gunaddr
[
1
].
length
-
1
)
===
'
/
'
)
{
console
.
log
(
"
NOTICE: address
ends in '/', assuming '
/
index.html' should be appended.
"
);
self
.
log
(
config
.
name
,
"
path
ends in '/', assuming 'index.html' should be appended.
"
);
gunaddr
[
1
]
+=
'
index.html
'
;
}
console
.
log
(
"
2. Starting Gun lookup of: '
"
+
gunaddr
.
join
(
'
,
'
)
+
"
'
"
);
console
.
log
(
"
+-- gun :
"
+
typeof
gun
);
console
.
log
(
"
+-- gunUser :
"
+
typeof
gunUser
);
// inform
self
.
log
(
config
.
name
,
`starting Gun lookup of:
${
gunaddr
.
join
(
'
,
'
)}
\n`
,
`+-- gun :
${
typeof
gun
}
\n`
,
`+-- gunUser :
${
typeof
gunUser
}
`
);
/*
* naïvely assume content type based on file extension
...
...
@@ -178,10 +185,10 @@
contentType
=
'
image/x-icon
'
;
break
;
}
console
.
log
(
"
+-- guessed contentType :
"
+
contentType
);
self
.
log
(
config
.
name
,
"
+-- guessed contentType :
"
+
contentType
);
return
getGunData
(
gunaddr
).
then
(
ipfsaddr
=>
{
console
.
log
(
"
3. S
tarting IPFS lookup of: '
"
+
ipfsaddr
+
"
'
"
);
self
.
log
(
config
.
name
,
`s
tarting IPFS lookup of: '
${
ipfsaddr
}
'`
);
return
ipfs
.
get
(
ipfsaddr
).
next
();
}).
then
(
file
=>
{
// we only need one
...
...
@@ -199,7 +206,7 @@
return
content
}
return
getContent
(
file
.
value
.
content
).
then
((
content
)
=>
{
console
.
log
(
'
4. G
ot a Gun-addressed IPFS-stored file:
'
+
file
.
value
.
path
+
'
;
content is:
'
+
typeof
content
);
self
.
log
(
config
.
name
,
`g
ot a Gun-addressed IPFS-stored file:
${
file
.
value
.
path
}
\n+--
content is:
${
typeof
content
}
`
);
// creating and populating the blob
var
blob
=
new
Blob
(
[
content
],
...
...
@@ -243,12 +250,12 @@
let
addToIPFS
=
(
resources
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
console
.
log
(
"
A
dding to IPFS...
"
)
console
.
log
(
"
+-- number of resources:
"
,
resources
.
length
)
self
.
log
(
config
.
name
,
"
a
dding to IPFS...
"
)
self
.
log
(
config
.
name
,
"
+-- number of resources:
"
,
resources
.
length
)
var
ipfs_addresses
=
{};
resources
.
forEach
(
function
(
res
){
console
.
log
(
"
+-- handling internal resource:
"
,
res
)
self
.
log
(
config
.
name
,
"
+-- handling internal resource:
"
,
res
)
ipfs
.
add
(
Ipfs
.
urlSource
(
res
))
.
then
((
result
)
=>
{
...
...
@@ -258,7 +265,7 @@
// what we need in ipfs_addresses is in fact the absolute path (no domain, no scheme)
var
abs_path
=
res
.
replace
(
window
.
location
.
origin
,
''
)
ipfs_addresses
[
abs_path
]
=
'
/ipfs/
'
+
result
.
cid
.
string
console
.
log
(
"
A
dded to IPFS:
"
+
abs_path
+
'
as
'
+
ipfs_addresses
[
abs_path
])
self
.
log
(
config
.
name
,
"
a
dded to IPFS:
"
+
abs_path
+
'
as
'
+
ipfs_addresses
[
abs_path
])
// if we seem to have all we need, resolve!
if
(
Object
.
keys
(
ipfs_addresses
).
length
===
resources
.
length
)
resolve
(
ipfs_addresses
);
})
...
...
@@ -289,8 +296,8 @@
*/
let
verifyInIPFS
=
(
ipfs_addresses
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
console
.
log
(
'
C
hecking IPFS content against a gateway...
'
)
console
.
log
(
'
+-- gateway in use:
'
+
config
.
ipfsGateway
)
self
.
log
(
config
.
name
,
'
c
hecking IPFS content against a gateway...
'
)
self
.
log
(
config
.
name
,
'
+-- gateway in use:
'
+
config
.
ipfsGateway
)
// get the list of IPFS addresses
var
updatedPaths
=
Object
.
values
(
ipfs_addresses
)
for
(
path
in
ipfs_addresses
)
{
...
...
@@ -299,13 +306,13 @@
.
then
((
response
)
=>
{
ipfsaddr
=
response
.
url
.
replace
(
config
.
ipfsGateway
,
''
)
if
(
response
.
ok
)
{
console
.
log
(
'
+-- verified:
'
+
ipfsaddr
)
self
.
log
(
config
.
name
,
'
+-- verified:
'
+
ipfsaddr
)
var
pathIndex
=
updatedPaths
.
indexOf
(
ipfsaddr
)
if
(
pathIndex
>
-
1
)
{
updatedPaths
.
splice
(
pathIndex
,
1
)
}
if
(
updatedPaths
.
length
===
0
)
{
console
.
log
(
'
A
ll updates confirmed successful!
'
)
self
.
log
(
config
.
name
,
'
a
ll updates confirmed successful!
'
)
resolve
(
ipfs_addresses
);
}
}
else
{
...
...
@@ -337,7 +344,7 @@
reject
(
new
Error
(
userReference
.
err
))
// reality check -- does it match our preconfigured pubkey?
}
else
if
(
gu
.
_
.
soul
.
slice
(
1
)
===
config
.
gunPubkey
)
{
console
.
log
(
'
Gun
A
dmin user authenticated using password.
'
);
self
.
log
(
config
.
name
,
'
Gun
a
dmin user authenticated using password.
'
);
// we need to keep the reference to g, otherwise gu becomes unusable
var
gApi
=
{
user
:
gu
,
...
...
@@ -358,7 +365,7 @@
// we need an authenticated Gun user
return
authGunAdmin
(
user
,
pass
)
.
then
((
gunAPI
)
=>
{
console
.
log
(
'
+-- adding new IPFS addresses to Gun...
'
)
self
.
log
(
config
.
name
,
'
+-- adding new IPFS addresses to Gun...
'
)
gunAPI
.
user
.
get
(
window
.
location
.
host
).
put
(
ipfs_addresses
/*, function(ack) {...}*/
);
return
gunAPI
;
})
...
...
@@ -373,10 +380,10 @@
*/
.
then
((
gunAPI
)
=>
{
// get the paths
console
.
log
(
'
+-- starting verification of updated Gun data...
'
)
self
.
log
(
config
.
name
,
'
+-- starting verification of updated Gun data...
'
)
var
updatedPaths
=
Object
.
keys
(
ipfs_addresses
)
for
(
path
in
ipfs_addresses
)
{
console
.
log
(
'
+-- watching:
'
+
path
)
self
.
log
(
config
.
name
,
'
+-- watching:
'
+
path
)
//debuglog('watching path for updates:', path)
// using the global gunUser to check if updates propagated
gunUser
.
get
(
window
.
location
.
host
).
get
(
path
).
on
(
function
(
updaddr
,
updpath
){
...
...
@@ -386,13 +393,13 @@
if
(
ipfs_addresses
[
updpath
]
==
updaddr
)
{
// update worked!
gunUser
.
get
(
window
.
location
.
host
).
get
(
updpath
).
off
()
console
.
log
(
'
+-- update confirmed for:
'
,
updpath
,
'
[
'
+
updaddr
+
'
]
'
)
self
.
log
(
config
.
name
,
'
+-- update confirmed for:
'
,
updpath
,
'
[
'
+
updaddr
+
'
]
'
)
var
pathIndex
=
updatedPaths
.
indexOf
(
updpath
)
if
(
pathIndex
>
-
1
)
{
updatedPaths
.
splice
(
pathIndex
,
1
)
}
if
(
updatedPaths
.
length
===
0
)
{
console
.
log
(
'
A
ll updates confirmed successful!
'
)
self
.
log
(
config
.
name
,
'
a
ll updates confirmed successful!
'
)
return
true
;
}
}
...
...
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