merge-requests/9/merge
Michał 'rysiek' Woźniak 2021-04-06 17:18:37 +00:00
commit 9afc3ab3e4
19 zmienionych plików z 7635 dodań i 0 usunięć

2
.gitignore vendored 100644
Wyświetl plik

@ -0,0 +1,2 @@
node_modules/
radata/

130
CODE_OF_CONDUCT.md 100644
Wyświetl plik

@ -0,0 +1,130 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
`rysiek+libresilient[at]hackerspace.pl`.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series
of actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within
the community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.

661
LICENSE 100644
Wyświetl plik

@ -0,0 +1,661 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
LibResilient
Copyright (C) 2019 libre
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<http://www.gnu.org/licenses/>.

114
README.md 100644
Wyświetl plik

@ -0,0 +1,114 @@
# LibResilient
A browser-based decentralized content delivery network, implemented as a JavaScript library to be deployed easily on any website. LibResilient uses [ServiceWorkers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers) and a suite of non-standard in-browser delivery mechanisms, with a strong focus on decentralized tools like [IPFS](https://ipfs.io/).
Ideally, users should not need to install any special software nor change any settings to continue being able to access an overloaded LibResilient-enabled site as soon as they are able to access it *once*.
## Current status
LibResilient is currently considered *alpha*: the code works, but major rewrites and API changes are coming. It has been tested on Firefox, Chromium and Chrome on desktop, as well as Firefox for mobile on Android, but it should work in any browser implementing the ServiceWorker API.
Feel free to test it, but be aware that it might not work as expected. If you'd like to get in touch, please email us at `rysiek+libresilient[at]hackerspace.pl`, create an [issue](https://gitlab.com/rysiekpl/libresilient/-/issues/new).
## Rationale
While a number of content delivery technologies exist, these typically require enormous centralized services. This creates opportunities for gate-keeping, and [causes any disruption at these centralized providers to become a major problem for thousands of websites](https://blog.cloudflare.com/cloudflare-outage-on-july-17-2020/).
This project explores the possibility of solving this in a way that would not require website visitors to install any special software or change any settings; the only things that are needed are a modern Web browser and the ability to visit a website once, so that the JavaScript ServiceWorker kicks in.
You can read more in-depth overview of LibResilient [here](./docs/ARCHITECTURE.md). And [here](./docs/PHILOSOPHY.md) is a document describing the philosophy influencing project goals and relevant technical decisions.
## Architecture
A [ServiceWorker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) is used as a way to persist the library after the initial visit to the participating website.
After the ServiceWorker is downloaded and activated, it handles all `fetch()` events by first trying to use the regular HTTPS request to the original website. If that fails for whatever reason (be it a timeout or a `4xx`/`5xx` error), the plugins kick in, attempting to fetch the content via any means available.
A more complete overview of the architecture and technicalities of LibResilient is available [here](./docs/ARCHITECTURE.md).
## Draft API
The plan is to have an API to enable the use of different strategies for getting content. There are two basic functions a plugin needs to perform:
- **resolution**
*where* a given piece of content (image, stylesheet, script, HTML file, etc.) is to be found
- **delivery**
*how* to get it
These need to be closely integrated. For example, if using Gun and IPFS, resolution is performed using Gun, and delivery is performed using IPFS. However, Gun needs to resolve content to something that is usable with IPFS. If, alternatively, we're also using Gun to resolve content available on BitTorrent, that will have to be a separate namespace in the Gun graph, since it will have to resolve to magnet links.
Therefore, it doesn't seem to make sense to separate resolution and delivery. Thus, a LibResilient plugin would need to implement the whole pipeline, and work by receiving a URL and returning a Promise that resolves to a valid Response object containing the content.
It should be possible to chain the plugins (try the first one, in case of error try the next, and so on), or run them in parallel (fire requests using all available plugins and return the first complete successful response). Running in parallel might offer a better user experience, but will also be more resource-intensive.
An additional part of the API is going to deal with reporting the status of the plugins, their versions, and how a given piece of content was fetched (using which plugin). This will require modifying actual content from the ServiceWorker to pass that data to the DOM.
### Content versioning
Implementing content versioning might be necessary. Some delivery mechanisms (IPFS, BitTorrent) might be slow to pick up newly published content, and while information about this might be available, it might be faster to fetch and display older content that has already propagated across multiple peers or network nodes, with a message informing the reader that new content is available and that they might want to retry fetching it.
An important consideration related to content versioning is that it needs to be consistent across a full set of published pieces of content.
For example, consider a simple site that consists of an `index.html`, `style.css`, and `script.js`. Non-trivial changes in `index.html` will render older versions of `style.css` and `script.js` broken. A particular version of the whole published site needs to be fetched, otherwise things will not work as expected.
This will probably need to be fleshed out later on, but the initial API needs to be designed in a way where content versioning can be introduced without breaking backwards compatibility with plugins.
### Status information
Status information should be available to users, informing them that the content is being retrieved using non-standard means that might take longer.
LibResilient information is kept per-request in the ServiceWorker, meaning it is transient and does not survive ServiceWorker restarts, which might happen multiple times over the lifetime of an open tab. However, each update is communicated to the browser window context that is relevant for a given request via [`client.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Client/postMessage) calls. This is also how information on ServiceWorker commit SHAs and available plugins are made available to the browser window context.
The data provided (per each requested URL handled by the ServiceWorker) is:
- `clientId` &ndash; the [Client ID](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/clientId) for the request (that is, the Client ID of this browser window)
- `url` &ndash; the URL of the request
- `serviceWorker` &ndash; the commit SHA of the ServiceWorker that handled the request
- `fetchError` &ndash; `null` if the request completed successfully via regular HTTPS; otherwise the error message
- `method` &ndash; the method by which the request was completed: "`fetch`" is regular HTTPS `fetch()`, `gun-ipfs` means Gun and IPFS were used, etc.
- `state` &ndash; the state of the request (`running`, `error`, `success`)
The code in the browser window context is responsible for keeping a more permanent record of the URLs requested, the methods used, and the status of each, if needed.
## Review of possible resolution/delivery methods
- **[Gun](https//gun.eco/)**
Better suited for resolution than for delivery, although it could handle both. Pretty new project, dynamically developed. No global network of public peers available currently. Content is cryptographically signed.
- **[IPNS](https://docs.ipfs.io/guides/concepts/ipns/)**
Only suitable for resolution. Experimental, not fully functional in the browser yet. Fits like a hand in a glove with IPFS.
- **[DNSLink](https://docs.ipfs.io/guides/concepts/dnslink/)**
Only suitable for resolution. Deployed, stable, and well-documented. Fits like a hand in a glove with IPFS. The downside is that it requires publishing of DNS records to work (every time any new content is published), which means it might be difficult to implement by website admins.
- **[IPFS](https://ipfs.io/)**
Only suitable for delivery, since it is content-addressed. Resolution of a content URI to an IPFS address needs to be handled by some other technology (like Gun or IPNS, or using [gateways](https://ipfs.github.io/public-gateway-checker/)). Deployed and well-documented, with a large community of developers. Redeploying a new content package with certain files unchanged does not change the addresses of the unchanged files, meaning that small changes in content do not lead to the whole content tree needing to be re-seeded.
- **[WebTorrent](https://github.com/webtorrent/webtorrent)**
Only suitable for content delivery. It seems possible to fetch a particular file from a given torrent, so as not to have to download a torrent of the whole website just to display a single page with some CSS and JS. Requires a resolver to point to the newest torrent since torrents are immutable. Even small changes (for example, only a few files changed in the whole website tree) require creating a new torrent and re-seeding, which is obviously less than ideal.
- **Plain files via HTTPS**
This delivery method is obvious if we're talking simply about the originating site and it serving the files, but this can also mean non-standard strategies like pushing static HTML+CSS+JS to CloudFront or Wasabi, and having a minimal resolver kick in if the originating site is unavailable, to fetch content seamlessly from alternative locations (effectively implementing domain fronting and collateral freedom in the browser). However, this will require some thought being put into somehow signing content deployed to third-party locations &ndash; perhaps the resolver (like Gun) could be responsible for keeping SHA sums of known good content, or perhaps we should just address it using the hashes, effectively imitating IPFS.
## Limitations
There are certain limitations to what can be done with LibResilient:
### Service worker cannot be updated if origin is down
ServiceWorker script apparently cannot be delivered using any of the transport plugins, [since](https://gist.github.com/Rich-Harris/fd6c3c73e6e707e312d7c5d7d0f3b2f9#the-new-service-worker-isnt-fetched-by-the-old-one):
> when you call `navigator.serviceWorker.register('service-worker.js)` the request for service-worker.js isn't intercepted by any service worker's fetch event handler.
So, the ServiceWorker script will be un-updateable via LibResilient in case the origin site is down, unless we find a way to hack around it with caches etc.
### JS implementations of decentralized protocols are still bootstrapped using servers
Gun and IPFS (and probably other potential LibResilient strategies) still use bootstrapping servers (STUN/TURN, and other kinds of public nodes), so technically it would be possible for all of these to be overwhelmed by traffic also, rendering LibResilient ineffective. This is a limitation of browsers and is related to IPv4 and NATs.
One way to deal with this is to have a large list of such public nodes and send only 2-3 each time LibResilient calls home (including via already working decentralized means), so that the traffic is spread more evenly.
Plus, the ever-increasing adoption of IPv6 will also partially fix this.
## Related developments
- https://ipfs.io/ipfs/QmNhFJjGcMPqpuYfxL62VVB9528NXqDNMFXiqN5bgFYiZ1/its-time-for-the-permanent-web.html
- https://blog.archive.org/2015/02/11/locking-the-web-open-a-call-for-a-distributed-web/

37
config.js 100644
Wyświetl plik

@ -0,0 +1,37 @@
/*
* LibResilient config
*
* This is the config for LibResilient as deployed on the https://libresilient.is/ site
*
* When deploying LibResilient on your website you will need to create your own config,
* using this one as a template.
*/
// plugins config
self.LibResilientConfig.plugins = {
'fetch':{},
'cache':{},
'any-of': {
plugins: {
'alt-fetch':{
// configuring the alternate endpoints plugin to use IPNS gateways
//
// NOTICE: we cannot use gateways that use hash directly in the (sub)domain:
// https://github.com/node-fetch/node-fetch/issues/260
endpoints: [
'https://ninetailed.ninja/ipns/QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ/', // Russia
'https://10.via0.com/ipns/QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ/', // USA
'https://ipfs.sloppyta.co/ipns/QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ/', // UK
'https://gateway.temporal.cloud/ipns/QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ/', // Germany
'https://ipfs.best-practice.se/ipns/QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ/' // Sweden
]
},
'gun-ipfs': {
gunPubkey: 'WUK5ylwqqgUorceQRa84qfbBFhk7eNRDUoPbGK05SyE.-yohFhTzWPpDT-UDMuKGgemOUrw_cMMYWpy6plILqrg'
}
}
}
}
// we need to explicitly list components we want to see debug messages from
self.LibResilientConfig.loggedComponents = ['service-worker', 'fetch', 'cache', 'any-of', 'alt-fetch', 'gun-ipfs']

Wyświetl plik

@ -0,0 +1,101 @@
# Architecture
Eventually this will document the architecture of LibResilient.
## Plugins
There are three kinds of plugins:
- **Transport plugins**
Plugins that *retrieve* website content, e.g. by using regular HTTPS [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), or by going through [IPFS](https://js.ipfs.io/). They *should* also offer a way to *publish* content by website admins (if relevant credentials or encryption keys are provided, depending on the method).
Methods these plugins implement:
- `fetch` - fetch content from an external source (e.g., from IPFS)
- `publish` - publish the content to the external source (e.g., to IPFS)
- **Stashing plugins**
Plugins that *stash* content locally (e.g., in the [browser cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache)) for displaying when no *transport plugin* works, or before content is received via one of them.
Methods these plugins implement:
- `fetch` - fetch the locally stored content (e.g., from cache)
- `stash` - stash the content locally (e.g., in cache)
- `unstash` - clear the content from the local store (e.g., clear the cache)
- **Composing plugins**
Plugins that *compose* other plugins, for example by running them simultaneously to retrieve content from whichever succeeds first.
Methods these plugins implement depend on which plugins they compose. Additionally, plugins being composed the `uses` key, providing the configuration for them the same way configuration is provided for plugins in the `plugins` key of `LibResilientConfig`.
Any plugin needs to add itself to the LibResilientPlugins global variable, using a data structure as follows:
```javascript
self.LibResilientPlugins.push({
name: 'plugin-name',
description: 'Plugin description. Just a few words, ideally.',
version: 'any relevant plugin version information',
fetch: functionImplementingFetch,
publish|stash|unstash: functionsImplementingRelevantFunctionality,
uses: {
composed-plugin-1: {
configKey1: "whatever-data-here"
},
composed-plugin-2: {
configKey2: "whatever-data-here"
},
{...}
}
})
```
### Transport plugins
Transport plugins *must* add `X-LibResilient-Method` and `X-LibResilient-ETag` headers to the response they return, so as to facilitate informing the user about new content after content was displayed using a stashing plugin.
- **`X-LibResilient-Method`**:
contains the name of the plugin used to fetch the content.
- **`X-LibResilient-ETag`**:
contains the [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) for the content; this can be an actual `ETag` header for HTTPS-based plugins, or some arbitrary string identifying a particular version of the resource (e.g., for IPFS-based plugins this can be the IPFS address, since that is based on content and different content results in a different IPFS address).
### Stashing plugins
Stashing plugins *must* stash the request along with the `X-LibResilient-Method` and `X-LibResilient-ETag` headers.
### Composing plugins
Composing plugins work by composing other plugins, for example to run them simultaneously and retrieve content from the first one that succeeds. A composing plugin needs to set the `uses` key in it's `LibResilientPlugins`. The key should contain mappings from plugin names to configuration:
```javascript
uses: {
composed-plugin-1: {
configKey1: "whatever-data-here"
},
composed-plugin-2: {
configKey2: "whatever-data-here"
},
{...}
}
```
## Fetching a resource via LibResilient
Whenever a resource is being fetched on a LibResilient-enabled site, the `service-worker.js` script dispatches plugins in the set order. Currently this order is hard-coded in `service-worker.js`, and is:
1. `fetch`, to use the upstream site directly if it is available,
1. `cache`, to display the site immediately from the cache in case regular `fetch` fails,
1. `gun-ipfs`, in the background if `cache` call succeeded, otherwise as the active fetch handler.
If a background plugin `fetch()` succeeds, the result is added to the cache and will be immediately available on page reload.
## Stashed versions invalidation
Invalidation heuristic is rather naïve, and boils down to checking if either of `X-LibResilient-Method` or `X-LibResilient-ETag` differs between the response from a transport plugin and whatever has already been stashed by a stashing plugin. If either differs, the transport plugin response is considered "*fresher*".
This is far from ideal and will need improvements in the long-term. The difficulty is that different transport plugins can provide different ways of determining the "*freshness*" of fetched content -- HTTPS-based requests offer `ETag`, `Date`, `Last-Modified`, and other headers that can help with that; whereas IPFS can't really offer much apart from the address which itself is a hash of the content, so at least we know the content is *different* (but is it *fresher* though?).
## Messaging
The ServiceWorker can communicate with the browser window using the [`Client.postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Client/postMessage) to post messages to the browser window context using the relevant [`Client ID`](https://developer.mozilla.org/en-US/docs/Web/API/Client/id), retrieved from the fetch event object.
When the browser window context wants to message the service worker, it uses the [`Worker.postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage) call, with `clientId` field set to the relevant client ID if a response is expected. ServiceWorker then again responds using `Client.postMessage()` using the `clientId` field as source of the `Client ID`.
### Messages
This section is a work in progress.

26
docs/PHILOSOPHY.md 100644
Wyświetl plik

@ -0,0 +1,26 @@
# Project's Philosophy
LibResilient's philosophy can be boiled down to a single sentence:
**Information must remain easily accessible.**
The choice of words here is very deliberate.
## Information vs. communication
LibResilient purposely focuses on ***making information accessible***, as opposed to facilitating *live two-way communication flow*.
There is plenty of misinformation to go around, and plenty of communication that is meant solely to muddy the waters and create a toxic information environment. Those who organize such disingenuous communication and participate in it often rely on it being two-way, fast-paced, and emotional, intentionally leaving as little space for calm rational thought as possible.
There is a meaningful difference between a debate of ideas, and a shouting match or a lynch mob. LibResilient is not interested in supporting the latter. While discriminatory content does also come in the form of articles on websites, it becomes truly toxic when live mass communication can be employed in an aggressive manner.
This is where LibResilient draws a line by making specific architectural decisions. We cannot stop bigots from using LibResilient on their websites, but we can make LibResilient less useful for specific strategies often employed by them.
## Centralization as a dis-service
LibResilient grew out of the experience of managing websites that experience huge traffic spikes, and the frustration regarding options available to website admins in who find their websites became unavailable, entirely or only to certain groups of visitors, be it via direct malicious actions (like DDoS), or organic traffic overwhelming the webserver.
These options tend to be limited to a few massive gatekeepers like CloudFlare, and a few smaller ethical providers like [Deflect](https://deflect.ca/).
In practice, website owners are incentivised to use the massive gatekeepers' services, which [gradually centralizes the Internet](https://iscloudflaresafeyet.com/). Such centralization then becomes a problem itself, when these gatekeepers [find themselves under pressure to drop protection for specific sites](https://www.techrepublic.com/article/as-google-and-aws-kill-domain-fronting-users-must-find-a-new-way-to-fight-censorship/), leaving website owners with nowhere to go.
LibResilient is explicitly focusing on decentralized tools like [IPFS](https://ipfs.io); in some cases and for certain very specific threats using the biggest gatekeepers might still make sense, and LibResilient might facilitate that. But whenever that is the case, care will be taken to do it in a way that is not tied to particular service or company.

2451
lib/gun.js 100644

Plik diff jest za duży Load Diff

2
lib/ipfs.js 100644

File diff suppressed because one or more lines are too long

1475
lib/sea.js 100644

Plik diff jest za duży Load Diff

106
lib/webrtc.js 100644
Wyświetl plik

@ -0,0 +1,106 @@
;(function(){
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
Gun.on('opt', function(root){
this.to.next(root);
var opt = root.opt;
if(root.once){ return }
if(!Gun.Mesh){ return }
if(false === opt.RTCPeerConnection){ return }
var env;
if(typeof window !== "undefined"){ env = window }
if(typeof global !== "undefined"){ env = global }
env = env || {};
var rtcpc = opt.RTCPeerConnection || env.RTCPeerConnection || env.webkitRTCPeerConnection || env.mozRTCPeerConnection;
var rtcsd = opt.RTCSessionDescription || env.RTCSessionDescription || env.webkitRTCSessionDescription || env.mozRTCSessionDescription;
var rtcic = opt.RTCIceCandidate || env.RTCIceCandidate || env.webkitRTCIceCandidate || env.mozRTCIceCandidate;
if(!rtcpc || !rtcsd || !rtcic){ return }
opt.RTCPeerConnection = rtcpc;
opt.RTCSessionDescription = rtcsd;
opt.RTCIceCandidate = rtcic;
opt.rtc = opt.rtc || {'iceServers': [
{urls: 'stun:stun.l.google.com:19302'},
{urls: "stun:stun.sipgate.net:3478"}/*,
{urls: "stun:stun.stunprotocol.org"},
{urls: "stun:stun.sipgate.net:10000"},
{urls: "stun:217.10.68.152:10000"},
{urls: 'stun:stun.services.mozilla.com'}*/
]};
// TODO: Select the most appropriate stuns.
// FIXME: Find the wire throwing ICE Failed
// The above change corrects at least firefox RTC Peer handler where it **throws** on over 6 ice servers, and updates url: to urls: removing deprecation warning
opt.rtc.dataChannel = opt.rtc.dataChannel || {ordered: false, maxRetransmits: 2};
opt.rtc.sdp = opt.rtc.sdp || {mandatory: {OfferToReceiveAudio: false, OfferToReceiveVideo: false}};
opt.announce = function(to){
root.on('out', {rtc: {id: opt.pid, to:to}}); // announce ourself
};
var mesh = opt.mesh = opt.mesh || Gun.Mesh(root);
root.on('create', function(at){
this.to.next(at);
setTimeout(opt.announce, 1);
});
root.on('in', function(msg){
if(msg.rtc){ open(msg) }
this.to.next(msg);
});
function open(msg){
var rtc = msg.rtc, peer, tmp;
if(!rtc || !rtc.id){ return }
delete opt.announce[rtc.id]; /// remove after connect
if(tmp = rtc.answer){
if(!(peer = opt.peers[rtc.id] || open[rtc.id]) || peer.remoteSet){ return }
return peer.setRemoteDescription(peer.remoteSet = new opt.RTCSessionDescription(tmp));
}
if(tmp = rtc.candidate){
peer = opt.peers[rtc.id] || open[rtc.id] || open({rtc: {id: rtc.id}});
return peer.addIceCandidate(new opt.RTCIceCandidate(tmp));
}
//if(opt.peers[rtc.id]){ return }
if(open[rtc.id]){ return }
(peer = new opt.RTCPeerConnection(opt.rtc)).id = rtc.id;
var wire = peer.wire = peer.createDataChannel('dc', opt.rtc.dataChannel);
open[rtc.id] = peer;
wire.onclose = function(){
delete open[rtc.id];
mesh.bye(peer);
//reconnect(peer);
};
wire.onerror = function(err){};
wire.onopen = function(e){
//delete open[rtc.id];
mesh.hi(peer);
}
wire.onmessage = function(msg){
if(!msg){ return }
mesh.hear(msg.data || msg, peer);
};
peer.onicecandidate = function(e){ // source: EasyRTC!
if(!e.candidate){ return }
root.on('out', {'@': msg['#'], rtc: {candidate: e.candidate, id: opt.pid}});
}
peer.ondatachannel = function(e){
var rc = e.channel;
rc.onmessage = wire.onmessage;
rc.onopen = wire.onopen;
rc.onclose = wire.onclose;
}
if(tmp = rtc.offer){
peer.setRemoteDescription(new opt.RTCSessionDescription(tmp));
peer.createAnswer(function(answer){
peer.setLocalDescription(answer);
root.on('out', {'@': msg['#'], rtc: {answer: answer, id: opt.pid}});
}, function(){}, opt.rtc.sdp);
return;
}
peer.createOffer(function(offer){
peer.setLocalDescription(offer);
root.on('out', {'@': msg['#'], rtc: {offer: offer, id: opt.pid}});
}, function(){}, opt.rtc.sdp);
return peer;
}
});
var noop = function(){};
}());

620
libresilient.js 100644
Wyświetl plik

@ -0,0 +1,620 @@
/* ========================================================================= *\
|* === Basic utils useful only in browser window === *|
\* ========================================================================= */
// create an object to hold everything that needs to be held globally
var libresilient = {
info: {},
status: false,
contentUnavailable: false,
cacheStale: false,
clientId: null
}
// some basic method stats
libresilient.methodStats = {}
// UI elements displaying the status for each local resource URL
libresilient.resourceDisplays = {}
/**
* creating a safe CSS class name from a string
*/
libresilient.safeClassName = (name) => {
return encodeURIComponent(name.toLowerCase()).replace(/%[0-9A-F]{2}/gi,'-')
}
/**
* creating the standalone LibResilient UI
*/
libresilient.addUI = () => {
var uiTemplate = document.createElement('template')
uiTemplate.innerHTML = `<div id="libresilient-ui">
<div class="libresilient-message-container"></div>
<div id="libresilient-ui-container" class="libresilient-status-service-worker">
<input type="checkbox" id="libresilient-ui-toggle"/>
<div class="libresilient-description">
<p><a href="https://libresilient.is/">LibResilient</a> is a tool that helps keep websites up without centralized CDNs.<br/>If you are seeing this it means some content is unavailable.<br/>LibResilient will attempt to get it for you anyway.</p>
<div class="libresilient-status-display"></div>
</div>
<label for="libresilient-ui-toggle" class="libresilient-toggle"><div></div></label>
</div></div>`
var uiStyle = document.createElement('style')
uiStyle.innerHTML = `#libresilient-ui {
display:flex;
align-items: flex-end;
flex-direction:column-reverse;
flex-wrap:nowrap;
position:fixed;
top:0px;
right:0px;
visibility:hidden;
}
#libresilient-ui.content-unavailable,
#libresilient-ui:target {
visibility:visible;
}
#libresilient-ui .libresilient-message-container {
}
#libresilient-ui .libresilient-message {
font-size:90%;
text-align:center;
background:#dfd;
border-radius:1em;
box-shadow:0px 0px 3px #dfd;
padding:0.5em 2em 0.5em 1em;
transition: ease-in 0.5s opacity;
opacity: 1;
position: relative;
top:16px;
right:5px;
color: #060;
text-shadow: 0px 0px 2px white;
font-family: sans;
}
#libresilient-ui .libresilient-message::after {
display: block;
content: "x";
position: absolute;
right: 0.5em;
top: 0.7em;
font-size:90%;
border-radius: 100%;
width: 1em;
height: 1em;
line-height: 0.8em;
padding-left: 0.01em;
box-shadow: inset 0px 0px 2px #080;
transition: ease-in 0.5s color, ease-in 0.5s background-color, ease-in 0.5s box-shadow-color;
color: #080;
background:white;
}
#libresilient-ui .libresilient-message:hover::after {
background: #080;
color: white;
box-shadow: inset 0px 0px 2px black;
}
#libresilient-ui .libresilient-message:first-child::before {
display:block;
content:" ";
width:1em;
height:1em;
position:absolute;
right:1em;
top:-0.5em;
background:#dfd;
box-shadow:0px 0px 3px #dfd;
transform: rotate(45deg);
z-index:-1;
}
#libresilient-ui-container {
background:#ddd;
box-shadow:0px 0px 3px black;
border-bottom-left-radius:30px;
padding: 4px 4px 8px 8px;
display:flex;
flex-wrap:nowrap;
}
#libresilient-ui-container .libresilient-toggle {
width:32px;
height:32px;
background:url('data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9JzMwMHB4JyB3aWR0aD0nMzAwcHgnICBmaWxsPSIjMDAwMDAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA5MCA5MCIgeD0iMHB4IiB5PSIwcHgiPjx0aXRsZT5iYWxsMjwvdGl0bGU+PGcgZGF0YS1uYW1lPSJMYXllciAyIj48cGF0aCBkPSJNOTIuMTQsMzkuMjZhNDEuOCw0MS44LDAsMCwwLTEuMjktNUE0NSw0NSwwLDEsMCw0Ny40Nyw5M0g0OGE0NC4zNSw0NC4zNSwwLDAsMCw0LjY0LS4yNEE0NS4wNyw0NS4wNywwLDAsMCw5Myw0OCw0NC4yMSw0NC4yMSwwLDAsMCw5Mi4xNCwzOS4yNlpNMjksMjQuMTZBMzAuMzgsMzAuMzgsMCwwLDAsMTcuNTEsNDhhMi40MSwyLjQxLDAsMSwxLTQuODEsMEEzNS4xNCwzNS4xNCwwLDAsMSwyNiwyMC40MWEyLjQsMi40LDAsMSwxLDMsMy43NVptMTktNi42NWEzMC40NiwzMC40NiwwLDAsMC0xMC4yOSwxLjc3LDIuMjgsMi4yOCwwLDAsMS0uODEuMTUsMi40MSwyLjQxLDAsMCwxLS44MS00LjY4QTM1LjM4LDM1LjM4LDAsMCwxLDQ4LDEyLjdhMi40MSwyLjQxLDAsMSwxLDAsNC44MVoiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0zIC0zKSI+PC9wYXRoPjwvZz48L3N2Zz4=') center center no-repeat;
display: block;
background-size:contain;
}
#libresilient-ui-container .libresilient-toggle > div {
width:100%;
height:100%;
background:url('data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9JzMwMHB4JyB3aWR0aD0nMzAwcHgnICBmaWxsPSIjRjk3OTAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNTIgNTIiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDUyIDUyIiB4bWw6c3BhY2U9InByZXNlcnZlIj48Zz48Zz48cGF0aCBkPSJNMzYsMTEuMDI5NzU0Nmg3LjM2MjYwOTljLTAuMDc3ODgwOS0wLjEwMDIxOTctMC4xNTExODQxLTAuMjAzMzA4MS0wLjI0MjYxNDctMC4yOTQ3Mzg4bC03LjgyOTk1NjEtNy44MzAwMTcxICAgIEMzNS4yMDA4MDU3LDIuODE1NzY1NCwzNS4wOTg3NTQ5LDIuNzQzNDM4NywzNSwyLjY2NjcxNzV2Ny4zNjMwMzcxQzM1LDEwLjU4MTAyNDIsMzUuNDQ4NzMwNSwxMS4wMjk3NTQ2LDM2LDExLjAyOTc1NDZ6Ij48L3BhdGg+PC9nPjxnPjxwYXRoIGQ9Ik0zNiwxMy4wMjk3NTQ2Yy0xLjY1NDI5NjksMC0zLTEuMzQ1NzAzMS0zLTNWMi4wMjQ5OTM5SDExYy0xLjY0OTk2MzQsMC0zLDEuMzUwMDM2Ni0zLDN2NDEuOTUwMDEyMiAgICBjMCwxLjY2MDAzNDIsMS4zNTAwMzY2LDMsMywzaDMwYzEuNjUwMDI0NCwwLDMtMS4zMzk5NjU4LDMtM1YxMy4wMjk3NTQ2SDM2eiBNMjEsMTkuMDAyODk5MmgxMHYySDIxVjE5LjAwMjg5OTJ6IE0zMSw0My4wMDMzODc1ICAgIEgyMXYtMmgxMFY0My4wMDMzODc1eiBNMzcsMzYuMDAzMzg3NUgxNXYtMmgyMlYzNi4wMDMzODc1eiBNMzcsMjguMDAyODk5MkgxNXYtMmgyMlYyOC4wMDI4OTkyeiI+PC9wYXRoPjwvZz48L2c+PC9zdmc+') center center no-repeat;
display: block;
background-size:50% 50%;
}
#libresilient-ui-container.active .libresilient-toggle {
animation-name: libresilient-ball-rolling;
animation-duration:10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
#libresilient-ui-container #libresilient-ui-toggle {
display:none;
}
#libresilient-ui-container > div {
display:none;
}
#libresilient-ui-container > #libresilient-ui-toggle:checked ~ div {
display:block;
}
#libresilient-ui-container .libresilient-description > p {
font-size:80%;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-right: 1em;
text-align: right;
text-shadow: -1px -1px 0px #ccc, 1px 1px 0px #eee;
color: #666;
font-family: sans-serif;
}
#libresilient-ui-container .libresilient-description > p a {
color: #d70;
}
#libresilient-ui-container .libresilient-status-display {
justify-content: right;
display: flex;
padding-right: 0.5em;
}
/*
* these will be useful also outside the #libresilient-ui
* for example, if there is a .libresilient-status-display in the page's HTML
*/
.libresilient-status-display > li {
display:inline-block;
font-size:80%;
font-family: Monospace;
}
.libresilient-status-element {
font-weight: bold;
display: inline-block;
text-align: center;
text-decoration:none;
background:#bbb;
padding:0.4em 1em;
border-radius:0.6em;
color:#777;
box-shadow: inset 0px 0px 3px #777;
margin: 0.5em;
transition: background-color 1s ease, color 1s ease, box-shadow 1s ease;
}
.libresilient-status-element.active {
box-shadow: 0px 0px 3px #f80, 0px 0px 3px #a60;
color: #fff;
background: #e70;
}
@keyframes libresilient-ball-rolling {
from {transform:rotate(0deg)}
to {transform:rotate(359deg)}
}`
document.head.insertAdjacentElement('afterbegin', uiStyle)
document.body.insertAdjacentElement('afterbegin', uiTemplate.content.firstChild)
}
/**
* internal logging facility
*
* component - name of the component being logged about
* items - the rest of arguments will be passed to console.debug()
*/
self.log = function(component, ...items) {
console.debug(`LibResilient [COMMIT_UNKNOWN, ${component}] ::`, ...items)
}
/**
* fetched resource display element
*/
libresilient.addFetchedResourceElements = (url, fetchedResourcesDisplays) => {
// make sure we have the container element to work with
if (typeof fetchedResourcesDisplays !== 'object') {
fetchedResourcesDisplays = document.getElementsByClassName("libresilient-fetched-resources-list")
}
var itemHTML = `<li class="libresilient-fetched-resources-item"><label>`
var foundSuccess = false
var pluginsHTML = ''
Object.keys(libresilient.methodStats).forEach((plugin)=>{
var pclass = libresilient.safeClassName(plugin)
if (typeof libresilient.info[url] !== "undefined" && typeof libresilient.info[url][plugin] !== "undefined") {
pclass = pclass + ' ' + libresilient.info[url][plugin].state;
foundSuccess = foundSuccess || (libresilient.info[url][plugin].state === "success")
}
pluginsHTML += `<span class="libresilient-fetched-resource-method ${pclass}">${plugin}</span>`
})
itemHTML += `<input type="checkbox" ${ foundSuccess ? 'checked="checked"' : 'disabled="disabled"' }/><span class="libresilient-fetched-resource-url"><span>${url}</span></span>${pluginsHTML}</label></li>`;
var item = document.createElement('template')
item.innerHTML = itemHTML;
libresilient.resourceDisplays[url] = new Array()
for (let frd of fetchedResourcesDisplays) {
libresilient.resourceDisplays[url].push(
frd.insertAdjacentElement('beforeend', item.content.firstChild.cloneNode(true))
)
}
}
/**
* creating/updating fetched resources data
*/
libresilient.updateFetchedResources = () => {
// getting these elements once instead of once per URL...
var fetchedResourcesDisplays = document.getElementsByClassName("libresilient-fetched-resources-list")
Object.keys(libresilient.info).forEach((url)=>{
// simplify
si = libresilient.info[url]
// if there are no status display elements for this URL...
if (typeof libresilient.resourceDisplays[url] === 'undefined') {
// ...create the elements
libresilient.addFetchedResourceElements(url, fetchedResourcesDisplays)
// otherwise, if si.method evaluates to true (i.e. is not an empty string nor null in this case)
} else {
// libresilient.methodStats has the most comprehensive list of methods used
Object.keys(libresilient.methodStats).forEach((method)=>{
var pclass = libresilient.safeClassName(method);
var foundSuccess = false
// handle per-resource displays
// TODO: this needs to be done in a more efficient and elegant way
for (let rdisplay of libresilient.resourceDisplays[url]) {
// if we don't seem to have a display for this method in this resource displa...
if (rdisplay.getElementsByClassName(pclass).length == 0) {
var method_class = pclass
if (typeof libresilient.info[url] !== "undefined" && typeof libresilient.info[url][method] !== "undefined") {
method_class += ' ' + libresilient.info[url][method].state;
}
var method_item = document.createElement('template')
method_item.innerHTML = `<span class="libresilient-fetched-resource-method ${method_class}">${method}</span>`
rdisplay.childNodes[0].insertAdjacentElement('beforeend', method_item.content.firstChild.cloneNode(true))
}
}
// do we have the method even?
if (typeof si[method] === "object") {
// is this a success?
if (si[method].state === "success") {
for (let rdisplay of libresilient.resourceDisplays[url]) {
if (! rdisplay.getElementsByClassName(pclass)[0].classList.contains('success')) {
// make sure the right classes are on
rdisplay.getElementsByClassName(pclass)[0].classList.remove('running')
rdisplay.getElementsByClassName(pclass)[0].classList.add('success')
// make sure the checkbox is checked
rdisplay.getElementsByTagName('input')[0].checked = true
rdisplay.getElementsByTagName('input')[0].disabled = false
}
}
// is this a running thing?
} else if (si[method].state === "running") {
for (let rdisplay of libresilient.resourceDisplays[url]) {
if (! rdisplay.getElementsByClassName(pclass)[0].classList.contains('running')) {
// make sure the right classes are on
rdisplay.getElementsByClassName(pclass)[0].classList.remove('success')
rdisplay.getElementsByClassName(pclass)[0].classList.add('running')
}
}
// nope, an error presumably
} else {
for (let rdisplay of libresilient.resourceDisplays[url]) {
// make sure the right classes are on
rdisplay.getElementsByClassName(pclass)[0].classList.remove('success')
rdisplay.getElementsByClassName(pclass)[0].classList.remove('running')
}
}
// clarly this method has not even been used for the resource
} else {
for (let rdisplay of libresilient.resourceDisplays[url]) {
// make sure the right classes are on
rdisplay.getElementsByClassName(pclass)[0].classList.remove('success')
rdisplay.getElementsByClassName(pclass)[0].classList.remove('running')
}
}
})
}
})
}
/**
* adding status display per plugin
*
* plugin - plugin name
* description - plugin description (optional; default: empty string)
* status - status text (optional; default: number of resources fetched
* using this plugin, based on methodStats)
*/
libresilient.addPluginStatus = (plugin, description='', status=null) => {
self.log('browser-side', 'addPluginStatus(' + plugin + ')')
var statusDisplays = document.getElementsByClassName("libresilient-status-display");
var pclass = libresilient.safeClassName(plugin);
var pcount = 0;
if (typeof libresilient.methodStats[plugin] !== 'undefined') {
pcount = libresilient.methodStats[plugin];
}
// handle the status displays
for (let sd of statusDisplays) {
sd.insertAdjacentHTML('beforeend', `<li><abbr class="libresilient-status-element ${pcount ? 'active' : ''} libresilient-status-${pclass}" title="${description}">${plugin}: <span class="status">${status ? status : pcount}</span></abbr></li>`)
}
}
/**
* updating status display per plugin
*
* expects an object that contains at least `name` attribute
*/
libresilient.updatePluginStatus = (plugin) => {
//self.log('browser-side', 'updatePluginStatus :: ' + plugin)
var pclass = libresilient.safeClassName(plugin);
//self.log('browser-side', 'updatePluginStatus :: pclass: ' + pclass)
var statusDisplay = document.querySelectorAll(".libresilient-status-" + pclass + " > .status");
//self.log('browser-side', 'updatePluginStatus :: statusDisplay: ' + typeof statusDisplay)
var pcount = 0;
if (typeof libresilient.methodStats[plugin] !== 'undefined') {
pcount = libresilient.methodStats[plugin]
}
for (let statusDisplay of document.querySelectorAll(".libresilient-status-" + pclass + " > .status")) {
statusDisplay.innerText = pcount
if ( (pcount === 0) && statusDisplay.parentElement.classList.contains('active')) {
statusDisplay.parentElement.classList.remove('active')
} else if ( (pcount > 0) && ! statusDisplay.parentElement.classList.contains('active')) {
statusDisplay.parentElement.classList.add('active')
}
}
}
/**
* toggling resource checkboxes (only if not disabled)
*/
libresilient.toggleResourceCheckboxes = () => {
document.querySelectorAll('.libresilient-fetched-resources-item input')
.forEach((el)=>{
el.checked = ! el.disabled && ! el.checked
})
}
/**
* stashing and unstashing resources
*
* stash param means "stash" if set to true (the default), "unstash" otherwise
*/
libresilient.stashOrUnstashResources = (stash=true) => {
// what are we doing?
operation = {
clientId: libresilient.clientId
}
// get the resources
var resources = []
document
.querySelectorAll('.libresilient-fetched-resources-item input:checked')
.forEach((el)=>{
resources.push(el.parentElement.querySelector('.libresilient-fetched-resource-url').innerText)
})
if (stash) {
operation.stash = [resources]
self.log('browser-side', 'Calling `stash()` on the service worker to stash the resources...')
} else {
operation.unstash = [resources]
self.log('browser-side', 'Calling `unstash()` on the service worker to unstash the resources...')
}
// RPC call on the service worker
return navigator
.serviceWorker
.controller
.postMessage(operation)
}
/**
* publishing certain resources to Gun and IPFS
*/
libresilient.publishResourcesToGunAndIPFS = () => {
var user = document.getElementById('libresilient-gun-user').value
var pass = document.getElementById('libresilient-gun-password').value
if (! user || ! pass) {
throw new Error("Gun user/password required!")
}
var resources = []
document.querySelectorAll('.libresilient-fetched-resources-item input:checked')
.forEach((el)=>{
resources.push(el.parentElement.querySelector('.libresilient-fetched-resource-url').innerText)
})
// call it!
self.log('browser-side', 'Calling `publish()` on the service worker to publish the resources...')
return navigator
.serviceWorker
.controller
.postMessage({
clientId: libresilient.clientId,
publish: [resources, user, pass]
})
}
/**
* display a LibResilient message
*/
libresilient.displayMessage = (msg) => {
// prepare the template
var messageBox = document.createElement('template')
messageBox.innerHTML = `<div class="libresilient-message">${msg}</div>`
// attach it to all libresilient-message-containers out there
for (let smc of document.getElementsByClassName('libresilient-message-container')) {
var msg = messageBox.content.firstChild.cloneNode(true)
msg.onclick = (e) => {
e.target.style.opacity=0
setTimeout(()=>{e.target.remove()}, 1000)
}
smc.insertAdjacentElement('beforeend', msg)
setTimeout(()=>{
msg.style.opacity=0
setTimeout(()=>{msg.remove()}, 1000)
}, 5000)
}
self.log('browser-side', ' +-- message shown!')
}
/**
* onload handler just to mark stuff as loaded
* for purposes of informing the user all is loaded
* when service worker messages us about it
*/
window.addEventListener('load', function() {
libresilient.status = "loaded";
/*
* status display: how did this file get fetched?
*
* yes, this code has to be directly here,
* since we want to know how *this exact file* got fetched
*/
if (typeof libresilient.info[window.location.href] === 'object') {
// service worker info
for (let libresilient_sw of document.querySelectorAll(".libresilient-status-service-worker")) {
libresilient_sw.className += " active";
try {
libresilient_sw.querySelector('.status').innerHTML = "yes";
} catch(e) {}
}
}
// was any content unavailable so far?
if (libresilient.contentUnavailable) {
libresilient.displayMessage('Some content seems unavailable. Attempting to retrieve it via LibResilient.')
}
})
self.log('browser-side', 'DOMContentLoaded!')
// add the generic service worker "badge"
libresilient.addUI()
libresilient.addPluginStatus('service worker', 'A service worker is an event-driven worker that intercepts fetch events.', 'no')
/* ========================================================================= *\
|* === Service worker setup === *|
\* ========================================================================= */
if ('serviceWorker' in navigator) {
if (navigator.serviceWorker.controller) {
// Service worker already registered.
self.log('browser-side', 'Service Worker already registered.')
} else {
var scriptPath = document.currentScript.src
var scriptFolder = scriptPath.substr(0, scriptPath.lastIndexOf( '/' )+1 )
var serviceWorkerPath = scriptFolder + 'service-worker.js'
self.log('browser-side', 'Service Worker script at: ' + serviceWorkerPath)
navigator.serviceWorker.register(serviceWorkerPath, {
scope: './'
}).then(function(reg) {
// Success.
self.log('browser-side', 'Service Worker registered.')
}).catch(error => {
self.log('browser-side', "Error while registering a service worker: ", error)
})
}
// handling the messages from ServiceWorker
navigator.serviceWorker.addEventListener('message', event => {
self.log('browser-side', 'LibResilientInfo received!')
if (event.data.url) {
self.log('browser-side', '+-- for:', event.data.url)
if (event.data.method) {
self.log('browser-side', ' +-- method:', event.data.method)
self.log('browser-side', ' +-- state :', event.data.state)
libresilient.info[event.data.url] = libresilient.info[event.data.url] || {}
libresilient.info[event.data.url][event.data.method] = event.data
// update method stats
if (typeof libresilient.methodStats[event.data.method] === 'undefined') {
// setup the stats
libresilient.methodStats[event.data.method] = 0
// but also we now know this method has not been seen before
// so set-up the plugin status display
libresilient.addPluginStatus(event.data.method)
}
if (event.data.state === "success") {
libresilient.methodStats[event.data.method]++
self.log('browser-side', ' +-- methodStats incremented to:', libresilient.methodStats[event.data.method])
libresilient.updatePluginStatus(event.data.method)
// if the method was `fetch`, and that was the first method, and the outcome is `error`, we *might* be down
} else if ( event.data.state === "error"
&& event.data.method === "fetch"
&& Object.keys(libresilient.info[event.data.url]).length === 1
&& Object.keys(libresilient.info[event.data.url])[0] === "fetch" ) {
// we seem to be down
document.getElementById('libresilient-ui').classList.add('content-unavailable')
// if contentUnavailable is false, that means this is the first time we hit a problem fetching
if (!libresilient.contentUnavailable) {
// mark it properly
libresilient.contentUnavailable = true
// if loaded, show the message to the user.
// if not, the message will be shown on `load` event anyway
if (libresilient.status === "loaded") {
libresilient.displayMessage('Some content seems unavailable. Attempting to retrieve it via LibResilient.')
}
}
}
// update the fetched resources display
// TODO: this updates *all* resources on each received message,
// TODO: and so is rather wasteful
libresilient.updateFetchedResources()
}
// we only want to mark that new content is available, and handle the message
// at allFetched event
if (event.data.fetchedDiffers) {
self.log('browser-side', ' +-- fetched version apparently differs from cached for:', event.data.url)
// record fo the URL
libresilient.info[event.data.url].cacheStale = true
// record gloally
libresilient.cacheStale = true
}
}
if (event.data.allFetched) {
if (libresilient.status === "loaded") {
// set the status so that we don't get the message doubled
libresilient.status = "complete"
// inform the user
if (libresilient.cacheStale) {
libresilient.displayMessage('Newer version of this page is available; please reload to see it.')
} else {
self.log('browser-side', '+-- all fetched!..')
libresilient.displayMessage('Fetching via LibResilient finished; no new content found.')
}
}
}
if (event.data.clientId) {
self.log('browser-side', '+-- got our clientId:', event.data.clientId)
libresilient.clientId = event.data.clientId
}
if (event.data.plugins) {
var msg = '+-- got the plugin list:'
event.data.plugins.forEach((p)=>{
msg += '\n +-- ' + p
// initialize methodStats
if (typeof libresilient.methodStats[p] === 'undefined') {
libresilient.methodStats[p] = 0
// set-up the plugin status display
libresilient.addPluginStatus(p)
}
})
self.log('browser-side', msg)
}
if (event.data.serviceWorker) {
self.log('browser-side', '+-- got the serviceWorker version:', event.data.serviceWorker)
libresilient_sws = document.getElementsByClassName("libresilient-commit-service-worker");
for (let element of libresilient_sws) {
element.innerHTML = event.data.serviceWorker;
}
}
});
}

Wyświetl plik

@ -0,0 +1,150 @@
/* ========================================================================= *\
|* === HTTP(S) fetch() from alternative endpoints === *|
\* ========================================================================= */
/**
* this plugin does not implement any push method
*
* NOTICE: this plugin uses Promise.any()
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
* the polyfill is implemented in LibResilient's service-worker.js
*/
// no polluting of the global namespace please
(function () {
/*
* plugin config settings
*/
// sane defaults
let defaultConfig = {
// name of this plugin
// should not be changed
name: "alt-fetch",
// endpoints to use
//
// they have to respond to requests formatted like:
// <endpoint-url>/<path>
//
// let's say the endpoint is:
// https://example.com/api/endpoint/
// ...and that we are trying to get:
// <original-domain>/some/path/img.png
//
// the endpoint is supposed to return the expected image
// when this URL is requested:
// https://example.com/api/endpoint/some/path/img.png
//
// this has to be explicitly configured by the website admin
endpoints: [],
// how many simultaneous connections to different endpoints do we want
//
// more concurrency means higher chance of a request succeeding
// but uses more bandwidth and other resources;
//
// 3 seems to be a reasonable default
concurrency: 3
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
// reality check: endpoints need to be set to an array of non-empty strings
if (typeof(config.endpoints) !== "object" || !Array.isArray(config.endpoints)) {
let err = new Error("endpoints not confgured")
console.error(err)
throw err
}
/**
* getting content using regular HTTP(S) fetch()
*/
let fetchContentFromAlternativeEndpoints = (url) => {
// we're going to try a random endpoint and building an URL of the form:
// https://<endpoint_address>/<pubkey>/<rest_of_URL>
var path = url.replace(/https?:\/\/[^/]+\//, '')
// we don't want to modify the original endpoints array
var sourceEndpoints = [...config.endpoints]
// if we have fewer than the configured concurrency, use all of them
if (sourceEndpoints.length <= config.concurrency) {
var useEndpoints = sourceEndpoints
// otherwise get `config.concurrency` endpoints at random
} else {
var useEndpoints = new Array()
while (useEndpoints.length < config.concurrency) {
// put in the address while we're at it
useEndpoints.push(
sourceEndpoints
.splice(Math.floor(Math.random() * sourceEndpoints.length), 1)[0] + path
)
}
}
// debug log
self.log(config.name, `fetching from alternative endpoints:\n ${useEndpoints.join('\n ')}`)
return Promise.any(
useEndpoints.map(
u=>fetch(u, {cache: "reload"})
))
.then((response) => {
// 4xx? 5xx? that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to other plugins:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
self.log(config.name, "fetched:", response.url);
// we need to create a new Response object
// with all the headers added explicitly,
// since response.headers is immutable
var init = {
status: response.status,
statusText: response.statusText,
headers: {}
};
response.headers.forEach(function(val, header){
init.headers[header] = val;
});
// add the X-LibResilient-* headers to the mix
init.headers['X-LibResilient-Method'] = config.name
// we will not have it most of the time, due to CORS rules:
// https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header
init.headers['X-LibResilient-ETag'] = response.headers.get('ETag')
if (init.headers['X-LibResilient-ETag'] === null) {
// far from perfect, but what are we going to do, eh?
init.headers['X-LibResilient-ETag'] = response.headers.get('last-modified')
}
// return the new response, using the Blob from the original one
return response
.blob()
.then((blob) => {
return new Response(
blob,
init
)
})
})
}
// and add ourselves to it
// with some additional metadata
self.LibResilientPlugins.push({
name: config.name,
description: 'HTTP(S) fetch() using alternative endpoints',
version: 'COMMIT_UNKNOWN',
fetch: fetchContentFromAlternativeEndpoints
})
// done with not poluting the global namespace
})()

54
plugins/any-of.js 100644
Wyświetl plik

@ -0,0 +1,54 @@
/* ========================================================================= *\
|* === Any-of: running multiple plugins simultaneously === *|
\* ========================================================================= */
/**
* this plugin does not implement any push method
*/
// no polluting of the global namespace please
(function () {
/*
* plugin config settings
*/
// sane defaults
let defaultConfig = {
// name of this plugin
// should not be changed
name: "any-of",
// list of plugins to run simultaneously
plugins: {
"alt-fetch": {},
"gun-ipfs": {}
}
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
/**
* getting content using regular HTTP(S) fetch()
*/
let fetchContent = (url) => {
self.log(config.name, `using: [${Object.keys(config.plugins).join(', ')}]!`)
return Promise.any(
LibResilientPlugins
.filter(p=>Object.keys(config.plugins).includes(p.name))
.map(p=>p.fetch(url))
)
}
// and add ourselves to it
// with some additional metadata
self.LibResilientPlugins.push({
name: config.name,
description: `Running simultaneously: [${Object.keys(config.plugins).join(', ')}]`,
version: 'COMMIT_UNKNOWN',
fetch: fetchContent,
uses: config.plugins
})
// done with not poluting the global namespace
})()

147
plugins/cache.js 100644
Wyświetl plik

@ -0,0 +1,147 @@
/* ========================================================================= *\
|* === Stashing plugin using the Cache API === *|
\* ========================================================================= */
// no polluting of the global namespace please
(function () {
/*
* plugin config settings
*/
// sane defaults
let defaultConfig = {
// name of this plugin
// should not be changed
name: "cache"
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
/**
* getting content from cache
*/
let getContentFromCache = (url) => {
self.log(config.name, `getting from cache: ${url}`)
return caches.open('v1')
.then((cache) => {
return cache.match(url)
})
.then((response) => {
if (typeof response === 'undefined') {
throw new Error('Resource not found in cache');
} else {
var msg = 'retrieved cached headers:'
response.headers.forEach((v, k)=>{
msg += `\n+-- ${k} : ${v}`
})
self.log(config.name, msg)
// return the response
return response
}
})
}
/**
* add resources to cache
*
* implements the stash() LibResilient plugin method
*
* accepts either a Response
* or a string containing a URL
* or an Array of string URLs
*/
let cacheContent = (resource, key) => {
return caches.open('v1')
.then((cache) => {
if (typeof resource === 'string') {
// assume URL
self.log(config.name, "caching an URL")
return cache.add(resource)
} else if (Array.isArray(resource)) {
// assume array of URLs
self.log(config.name, "caching an Array of URLs")
return cache.addAll(resource)
} else {
// assume a Response
// which means we either have a Request in key, a string URL in key,
// or we can use the URL in resource.url
if ( (typeof key !== 'object') && ( (typeof key !== 'string') || (key === '') ) ) {
if (typeof resource.url !== 'string' || resource.url === '') {
throw new Error('No URL to work with!')
}
key = resource.url
}
// we need to create a new Response object
// with all the headers added explicitly
// otherwise the x-libresilient-* headers get ignored
var init = {
status: resource.status,
statusText: resource.statusText,
headers: {}
};
resource.headers.forEach(function(val, header){
init.headers[header] = val;
});
return resource
.blob()
.then((blob) => {
self.log(config.name, "caching a Response to: " + key)
return cache.put(key, new Response(
blob,
init
))
})
}
})
}
/**
* remove resources from cache
*
* implements the unstash() LibResilient plugin method
*
* accepts either a Response
* or a string containing a URL
* or an Array of string URLs
*/
let clearCachedContent = (resource) => {
return caches.open('v1')
.then((cache) => {
if (typeof resource === 'string') {
// assume URL
self.log(config.name, "deleting a cached URL")
return cache.delete(resource)
} else if (Array.isArray(resource)) {
// assume array of URLs
self.log(config.name, "deleting an Array of cached URLs")
return Promise.all(
resource.map((res)=>{
return cache.delete(res)
})
)
} else {
// assume a Response
// which means we have an URL in resource.url
self.log(config.name, "removing a Response from cache: " + resource.url)
return cache.delete(resource.url)
}
})
}
// and add ourselves to it
// with some additional metadata
self.LibResilientPlugins.push({
name: config.name,
description: 'Locally cached responses, using the Cache API.',
version: 'COMMIT_UNKNOWN',
fetch: getContentFromCache,
stash: cacheContent,
unstash: clearCachedContent
})
// done with not poluting the global namespace
})()

78
plugins/fetch.js 100644
Wyświetl plik

@ -0,0 +1,78 @@
/* ========================================================================= *\
|* === Regular HTTP(S) fetch() plugin === *|
\* ========================================================================= */
/**
* this plugin does not implement any push method
*/
// no polluting of the global namespace please
(function () {
/*
* plugin config settings
*/
// sane defaults
let defaultConfig = {
// name of this plugin
// should not be changed
name: "fetch"
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
/**
* getting content using regular HTTP(S) fetch()
*/
let fetchContent = (url) => {
self.log(config.name, `regular fetch: ${url}`)
return fetch(url, {cache: "reload"})
.then((response) => {
// 4xx? 5xx? that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to LibResilient:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
self.log(config.name, `fetched successfully: ${response.url}`);
// we need to create a new Response object
// with all the headers added explicitly,
// since response.headers is immutable
var init = {
status: response.status,
statusText: response.statusText,
headers: {}
};
response.headers.forEach(function(val, header){
init.headers[header] = val;
});
// add the X-LibResilient-* headers to the mix
init.headers['X-LibResilient-Method'] = config.name
init.headers['X-LibResilient-ETag'] = response.headers.get('ETag')
// return the new response, using the Blob from the original one
return response
.blob()
.then((blob) => {
return new Response(
blob,
init
)
})
})
}
// and add ourselves to it
// with some additional metadata
self.LibResilientPlugins.push({
name: config.name,
description: 'Just a regular HTTP(S) fetch()',
version: 'COMMIT_UNKNOWN',
fetch: fetchContent
})
// done with not poluting the global namespace
})()

480
plugins/gun-ipfs.js 100644
Wyświetl plik

@ -0,0 +1,480 @@
/**
* this is the default Gun+IPFS strategy plugin
* for LibResilient.
*
* it uses Gun for content address resolution
* and IPFS for delivery
*/
/**
* this is apparently needed by Gun
* `window` does not exist in ServiceWorker context
*/
if (typeof window === 'undefined') {
var window = self;
}
/* ========================================================================= *\
|* === General stuff and setup === *|
\* ========================================================================= */
// no polluting of the global namespace please
(function () {
var ipfs;
var gun;
var gunUser;
// sane defaults
let defaultConfig = {
// name of this plugin
// should not be changed
name: "gun-ipfs",
// Gun nodes to use
gunNodes: ['https://gunjs.herokuapp.com/gun'],
// the pubkey of the preconfigured Gun user; always needs to be set in config.js
gunPubkey: null,
// the IPFS gateway we're using for verification when publishing; default is usually ok
ipfsGateway: 'https://gateway.ipfs.io'
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
// reality check: Gun pubkey needs to be set to a non-empty string
if (typeof(config.gunPubkey) !== "string" || config.gunPubkey === "") {
let err = new Error("gunPubkey not confgured")
console.error(err)
throw err
}
/**
* importing stuff works differently between a browser window context
* and a ServiceWorker context, because things just can't be easy and sane
*/
function doImport() {
var args = Array.prototype.slice.call(arguments);
self.log(config.name, `doImport()\n+-- ${args.join('\n+-- ')}`)
if (typeof self.importScripts !== 'undefined') {
self.log(config.name, `+-- self.importScripts.apply()`)
self.importScripts.apply(self, args)
} else {
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
self.log(config.name, 'importing IPFS-related libraries...');
doImport(
"./lib/ipfs.js");
self.log(config.name, 'setting up IPFS...')
ipfs = await self.Ipfs.create();
self.log(config.name, '+-- IPFS loaded :: ipfs is : ' + typeof ipfs)
}
}
async function setup_gun() {
self.log(config.name, 'setup_gun()')
if (gun === undefined) {
gun = false // we don't want to start a few times over
self.log(config.name, 'importing Gun-related libraries...');
try {
doImport(
"./lib/gun.js",
"./lib/sea.js",
"./lib/webrtc.js");
} catch(e) {
console.error(e)
}
self.log(config.name, 'setting up Gun...')
gun = Gun(config.gunNodes);
self.log(config.name, '+-- gun loaded :: gun is : ' + typeof gun);
}
if ( (gun !== false) && (gun !== undefined) ) {
if (gunUser === undefined) {
gunUser = false // we don't want to start a few times over
self.log(config.name, 'setting up gunUser...')
gunUser = gun.user(config.gunPubkey)
self.log(config.name, '+-- gun init complete :: gunUser is: ' + typeof gunUser);
}
} else {
console.error("at this point Gun should have been set up already, but isn't!")
}
}
async function setup_gun_ipfs() {
self.log(config.name, 'setup_gun_ipfs()')
if (ipfs === undefined || gun === undefined) {
self.log(config.name, 'setting up...')
setup_ipfs();
setup_gun();
} else {
self.log(config.name, 'setup already underway (ipfs: ' + ( (ipfs) ? 'done' : 'loading' ) + ', gun: ' + ( (gun) ? 'done' : 'loading' ) + ')')
}
}
/* ========================================================================= *\
|* === Main functionality === *|
\* ========================================================================= */
let getGunData = (gunaddr) => {
return new Promise(
(resolve, reject) => {
self.log(
config.name,
'getGunData()\n',
`+-- gunUser : ${typeof gunUser}\n`,
`+-- gunaddr[] : ${gunaddr}`
);
// get the data
gunUser
.get(gunaddr[0])
.get(gunaddr[1])
.once(function(addr){
if (typeof addr !== 'undefined') {
self.log(config.name, `IPFS address: "${addr}"`);
resolve(addr);
} else {
// looks like we didn't get anything
reject(new Error('IPFS address is undefined for: ' + gunaddr[1]))
}
// ToDo: what happens when we hit the timeout here?
}, {wait: 5000});
}
);
};
/**
* the workhorse of this plugin
*/
async function getContentFromGunAndIPFS(url) {
var urlArray = url.replace(/https?:\/\//, '').split('/')
var gunaddr = [urlArray[0], '/' + urlArray.slice(1).join('/')]
/*
* if the gunaddr[1] ends in '/', append 'index.html' to it
*/
if (gunaddr[1].charAt(gunaddr[1].length - 1) === '/') {
self.log(config.name, "path ends in '/', assuming 'index.html' should be appended.");
gunaddr[1] += 'index.html';
}
// 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
* TODO: this needs a fix
*/
var contentType = '';
switch (gunaddr.slice(-1)[0].split('.', -1)[1].toLowerCase()) {
case 'html':
case 'htm':
contentType = 'text/html';
break;
case 'css':
contentType = 'text/css';
break;
case 'js':
contentType = 'text/javascript';
break;
case 'svg':
contentType = 'image/svg+xml';
break;
case 'ico':
contentType = 'image/x-icon';
break;
}
self.log(config.name, " +-- guessed contentType : " + contentType);
return getGunData(gunaddr).then(ipfsaddr => {
self.log(config.name, `starting IPFS lookup of: '${ipfsaddr}'`);
return ipfs.get(ipfsaddr).next();
}).then(file => {
// we only need one
if (file.value.content) {
async function getContent(source) {
var content = new Uint8Array()
var data = await source.next()
while (! data.done) {
var newContent = new Uint8Array(content.length + data.value.length);
newContent.set(content)
newContent.set(data.value, content.length)
content = newContent
data = await source.next()
}
return content
}
return getContent(file.value.content).then((content)=>{
self.log(config.name, `got a Gun-addressed IPFS-stored file: ${file.value.path}\n+-- content is: ${typeof content}`);
// creating and populating the blob
var blob = new Blob(
[content],
{'type': contentType}
);
return new Response(
blob,
{
'status': 200,
'statusText': 'OK',
'headers': {
'Content-Type': contentType,
'ETag': file.value.path,
'X-LibResilient-Method': config.name,
'X-LibResilient-ETag': file.value.path
}
}
);
})
};
});
}
/* ========================================================================= *\
|* === Publishing stuff === *|
\* ========================================================================= */
/*
* these are used for adding content to IPFS and Gun
*/
/**
* adding stuff to IPFS
* accepts an array of URLs
*
* returns a Promise that resolves to an object mapping URLs to IPFS hashes
*/
let addToIPFS = (resources) => {
return new Promise((resolve, reject) => {
self.log(config.name, "adding to IPFS...")
self.log(config.name, "+-- number of resources:", resources.length)
var ipfs_addresses = {};
resources.forEach(function(res){
self.log(config.name, " +-- handling internal resource:", res)
ipfs.add(Ipfs.urlSource(res))
.then((result) => {
// add to the list -- this is needed to add stuff to Gun
// result.path is just the filename stored in IPFS, not the actual path!
// res holds the full URL
// 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
self.log(config.name, "added 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);
})
});
})
}
/**
* verification that content pushed to IPFS
* is, in fact, available in IPFS
*
* a nice side-effect is that this will pre-load the content on
* a gateway, which tends to be a large (and fast) IPFS node
*
* this is prety naïve, in that it pulls the content from an ipfs gateway
* and assumes all is well if it get a HTTP 200 and any content
*
* that is, it does *not* check that the content matches what was pushed
* we trust IPFS here, I guess
*
* finally, we're using a regular fetch() instead of just going through our
* ipfs object because our IPFS object might have things cached and we want
* to test a completey independent route
*
* takes a object mapping paths to IPFS addresses
* and returns a Promise that resolves to true
*/
let verifyInIPFS = (ipfs_addresses) => {
return new Promise((resolve, reject) => {
self.log(config.name, 'checking 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) {
// start the fetch
fetch(config.ipfsGateway + ipfs_addresses[path])
.then((response) => {
ipfsaddr = response.url.replace(config.ipfsGateway, '')
if (response.ok) {
self.log(config.name, '+-- verified: ' + ipfsaddr)
var pathIndex = updatedPaths.indexOf(ipfsaddr)
if (pathIndex > -1) {
updatedPaths.splice(pathIndex, 1)
}
if (updatedPaths.length === 0) {
self.log(config.name, 'all updates confirmed successful!')
resolve(ipfs_addresses);
}
} else {
reject(new Error('HTTP error (' + response.status + ' ' + response.statusText + ' for: ' + ipfsaddr))
}
})
.catch((err) => {
// it would be nice to have the failed path here somehow
// alternatively, updating updatedPaths with info on failed
// requests might work
reject(err)
})
}
})
}
/**
* auth a Gun admin user
* (and verify it's the correct one with regards to the configured config.gunPubkey)
*/
let authGunAdmin = (user, pass) => {
return new Promise((resolve, reject) => {
// we need a separate Gun instance, otherwise gu will get merged with gunUser
// and we want these to be separate
var g = Gun(config.gunNodes)
var gu = g.user()
gu.auth(user, pass, (userReference) => {
if (userReference.err) {
reject(new Error(userReference.err))
// reality check -- does it match our preconfigured pubkey?
} else if (gu._.soul.slice(1) === config.gunPubkey) {
self.log(config.name, 'Gun admin user authenticated using password.');
// we need to keep the reference to g, otherwise gu becomes unusable
var gApi = {
user: gu,
gun: g
}
resolve(gApi)
} else {
reject(new Error('Password-authenticated user does not match preconfigured pubkey!'))
}
})
})
}
/**
* add IPFS addresses to Gun
*/
let addToGun = (user, pass, ipfs_addresses) => {
// we need an authenticated Gun user
return authGunAdmin(user, pass)
.then((gunAPI) => {
self.log(config.name, '+-- adding new IPFS addresses to Gun...')
gunAPI.user.get(window.location.host).put(ipfs_addresses /*, function(ack) {...}*/);
return gunAPI;
})
/**
* regular confirmations don't seem to work
*
* so instead we're using the regular read-only Gun user
* to .get() the data that we've .put() just a minute ago
*
* we then subscribe to the .on() events and once we notice the correct
* addresseswe consider our job done and quit.
*/
.then((gunAPI) => {
// get the paths
self.log(config.name, '+-- starting verification of updated Gun data...')
var updatedPaths = Object.keys(ipfs_addresses)
for (path in ipfs_addresses) {
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){
/*debuglog('+--', updpath)
debuglog(' updated :', ipfs_addresses[updpath])
debuglog(' received :', updaddr)*/
if (ipfs_addresses[updpath] == updaddr) {
// update worked!
gunUser.get(window.location.host).get(updpath).off()
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) {
self.log(config.name, 'all updates confirmed successful!')
return true;
}
}
})
}
})
}
/**
* example code for of adding content to IPFS, verifying it was successfully added,
* and adding the new addresses to Gun (and verifying changes propagated)
*
* TODO: this should accept a URL, a Response, or a list of URLs,
* and handle stuff appropriately
*/
let publishContent = (resource, user, password) => {
if (typeof resource === 'string') {
// we need this as an array of strings
resource = [resource]
} else if (typeof resource === 'object') {
if (!Array.isArray(resource)) {
// TODO: this needs to be implemented such that the Response is used directly
// but that would require all called functions to also accept a Response
// and act accordingly; #ThisIsComplicated
throw new Error("Handling a Response: not implemented yet")
}
} else {
// everything else -- that's a paddlin'!
throw new TypeError("Only accepts: string, Array of string, Response.")
}
// add to IPFS
var ipfsPromise = addToIPFS(resource)
return Promise.all([
// verify stuff ended up in IPFS
ipfsPromise.then(verifyInIPFS),
// add to Gun and verify Gun updates propagation
ipfsPromise.then((hashes) => {
addToGun(user, password, hashes)
})
])
}
/* ========================================================================= *\
|* === Initialization === *|
\* ========================================================================= */
// we probably need to handle this better
setup_gun_ipfs();
// and add ourselves to it
// with some additional metadata
self.LibResilientPlugins.push({
name: config.name,
description: 'Decentralized resource fetching using Gun for address resolution and IPFS for content delivery.',
version: 'COMMIT_UNKNOWN',
fetch: getContentFromGunAndIPFS,
publish: publishContent
})
// done with not poluting the global namespace
})()

Wyświetl plik

@ -0,0 +1,340 @@
/**
* this is the default IPNS+IPFS strategy plugin
* for LibResilient.
*
* it uses IPNS for content address resolution
* and IPFS for delivery
*/
/*
* THIS PLUGIN IS A WORK IN PROGRESS AND IS NOT FUNCTIONAL YET
*/
/* ========================================================================= *\
|* === General stuff and setup === *|
\* ========================================================================= */
// no polluting of the global namespace please
(function () {
var ipfs;
// sane defaults
let defaultConfig = {
// name of this plugin
// should not be changed
name: "ipns-ipfs",
// the pubkey of the preconfigured IPNS node; always needs to be set in config.js
ipnsPubkey: null,
// the IPFS gateway we're using for verification when publishing; default is usually ok
ipfsGateway: 'https://gateway.ipfs.io'
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
// reality check: Gun pubkey needs to be set to a non-empty string
if (typeof(config.ipnsPubkey) !== "string" || config.ipnsPubkey === "") {
let err = new Error("ipnsPubkey not confgured")
console.error(err)
throw err
}
/**
* importing stuff works differently between a browser window context
* and a ServiceWorker context, because things just can't be easy and sane
*/
function doImport() {
var args = Array.prototype.slice.call(arguments);
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)
})
}
}
async function setup_ipfs() {
if (ipfs === undefined) {
ipfs = false // we don't want to start a few times over
console.log('(COMMIT_UNKNOWN) Importing IPFS-related libraries...');
doImport(
"./lib/ipfs.js");
console.log('(COMMIT_UNKNOWN) Setting up IPFS...')
ipfs = await self.Ipfs.create({
config: {
dht: {
enabled: true,
clientMode: true
}
},
libp2p: {
config: {
dht: {
enabled: true,
clientMode: true
}
}
}
});
console.log('+-- IPFS loaded :: ipfs is : ' + typeof ipfs)
}
}
/* ========================================================================= *\
|* === Main functionality === *|
\* ========================================================================= */
/**
* the workhorse of this plugin
*/
async function getContentFromIPNSAndIPFS(url) {
return new Error("Not implemented yet.")
var urlArray = url.replace(/https?:\/\//, '').split('/')
var gunaddr = [urlArray[0], '/' + urlArray.slice(1).join('/')]
/*
* 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.");
gunaddr[1] += 'index.html';
}
console.log("2. Starting Gun lookup of: '" + gunaddr.join(', ') + "'");
console.log(" +-- gun : " + typeof gun);
console.log(" +-- gunUser : " + typeof gunUser);
/*
* naïvely assume content type based on file extension
* TODO: this needs a fix
*/
var contentType = '';
switch (gunaddr.slice(-1)[0].split('.', -1)[1].toLowerCase()) {
case 'html':
case 'htm':
contentType = 'text/html';
break;
case 'css':
contentType = 'text/css';
break;
case 'js':
contentType = 'text/javascript';
break;
case 'svg':
contentType = 'image/svg+xml';
break;
case 'ico':
contentType = 'image/x-icon';
break;
}
console.log(" +-- guessed contentType : " + contentType);
return getGunData(gunaddr).then(ipfsaddr => {
console.log("3. Starting IPFS lookup of: '" + ipfsaddr + "'");
return ipfs.get(ipfsaddr).next();
}).then(file => {
// we only need one
if (file.value.content) {
async function getContent(source) {
var content = new Uint8Array()
var data = await source.next()
while (! data.done) {
var newContent = new Uint8Array(content.length + data.value.length);
newContent.set(content)
newContent.set(data.value, content.length)
content = newContent
data = await source.next()
}
return content
}
return getContent(file.value.content).then((content)=>{
console.log('4. Got a Gun-addressed IPFS-stored file: ' + file.value.path + '; content is: ' + typeof content);
// creating and populating the blob
var blob = new Blob(
[content],
{'type': contentType}
);
return new Response(
blob,
{
'status': 200,
'statusText': 'OK',
'headers': {
'Content-Type': contentType,
'ETag': file.value.path,
'X-LibResilient-Method': config.name,
'X-LibResilient-ETag': file.value.path
}
}
);
})
};
});
}
/* ========================================================================= *\
|* === Publishing stuff === *|
\* ========================================================================= */
/*
* these are used for adding content to IPFS and Gun
*/
/**
* adding stuff to IPFS
* accepts an array of URLs
*
* returns a Promise that resolves to an object mapping URLs to IPFS hashes
*/
let addToIPFS = (resources) => {
return new Error("Not implemented yet.")
return new Promise((resolve, reject) => {
console.log("Adding to IPFS...")
console.log("+-- number of resources:", resources.length)
var ipfs_addresses = {};
resources.forEach(function(res){
console.log(" +-- handling internal resource:", res)
ipfs.add(Ipfs.urlSource(res))
.then((result) => {
// add to the list -- this is needed to add stuff to Gun
// result.path is just the filename stored in IPFS, not the actual path!
// res holds the full URL
// 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("Added 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);
})
});
})
}
/**
* verification that content pushed to IPFS
* is, in fact, available in IPFS
*
* a nice side-effect is that this will pre-load the content on
* a gateway, which tends to be a large (and fast) IPFS node
*
* this is prety naïve, in that it pulls the content from an ipfs gateway
* and assumes all is well if it get a HTTP 200 and any content
*
* that is, it does *not* check that the content matches what was pushed
* we trust IPFS here, I guess
*
* finally, we're using a regular fetch() instead of just going through our
* ipfs object because our IPFS object might have things cached and we want
* to test a completey independent route
*
* takes a object mapping paths to IPFS addresses
* and returns a Promise that resolves to true
*/
let verifyInIPFS = (ipfs_addresses) => {
return new Error("Not implemented yet.")
return new Promise((resolve, reject) => {
console.log('Checking IPFS content against a gateway...')
console.log('+-- gateway in use: ' + config.ipfsGateway)
// get the list of IPFS addresses
var updatedPaths = Object.values(ipfs_addresses)
for (path in ipfs_addresses) {
// start the fetch
fetch(config.ipfsGateway + ipfs_addresses[path])
.then((response) => {
ipfsaddr = response.url.replace(config.ipfsGateway, '')
if (response.ok) {
console.log('+-- verified: ' + ipfsaddr)
var pathIndex = updatedPaths.indexOf(ipfsaddr)
if (pathIndex > -1) {
updatedPaths.splice(pathIndex, 1)
}
if (updatedPaths.length === 0) {
console.log('All updates confirmed successful!')
resolve(ipfs_addresses);
}
} else {
reject(new Error('HTTP error (' + response.status + ' ' + response.statusText + ' for: ' + ipfsaddr))
}
})
.catch((err) => {
// it would be nice to have the failed path here somehow
// alternatively, updating updatedPaths with info on failed
// requests might work
reject(err)
})
}
})
}
/**
* example code for of adding content to IPFS, verifying it was successfully added,
* and adding the new addresses to Gun (and verifying changes propagated)
*
* TODO: this should accept a URL, a Response, or a list of URLs,
* and handle stuff appropriately
*/
let publishContent = (resource, user, password) => {
return new Error("Not implemented yet.")
if (typeof resource === 'string') {
// we need this as an array of strings
resource = [resource]
} else if (typeof resource === 'object') {
if (!Array.isArray(resource)) {
// TODO: this needs to be implemented such that the Response is used directly
// but that would require all called functions to also accept a Response
// and act accordingly; #ThisIsComplicated
throw new Error("Handling a Response: not implemented yet")
}
} else {
// everything else -- that's a paddlin'!
throw new TypeError("Only accepts: string, Array of string, Response.")
}
// add to IPFS
var ipfsPromise = addToIPFS(resource)
return Promise.all([
// verify stuff ended up in IPFS
ipfsPromise.then(verifyInIPFS),
// add to Gun and verify Gun updates propagation
ipfsPromise.then((hashes) => {
addToGun(user, password, hashes)
})
])
}
/* ========================================================================= *\
|* === Initialization === *|
\* ========================================================================= */
// we probably need to handle this better
setup_ipfs();
// and add ourselves to it
// with some additional metadata
self.LibResilientPlugins.push({
name: config.name,
description: 'Decentralized resource fetching using IPNS for address resolution and IPFS for content delivery.',
version: 'COMMIT_UNKNOWN',
fetch: getContentFromIPNSAndIPFS,
publish: publishContent
})
// done with not poluting the global namespace
})()

661
service-worker.js 100644
Wyświetl plik

@ -0,0 +1,661 @@
/*
* LibResilient Service Worker.
*
* Default strategy:
* 1. Try to load from main website.
* 2. If loading fails, load from LibResilient.
* 3. If loading is too slow, load from LibResilient.
* 4. If loaded content doesn't match authenticated versions, fall back to
* LibResilient.
*/
/*
* we need a Promise.any() polyfill
* so here it is
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
*
* TODO: remove once Promise.any() is implemented broadly
*/
if (typeof Promise.any === 'undefined') {
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)
);
};
}
// initialize the LibResilientPlugins array
if (!Array.isArray(self.LibResilientPlugins)) {
self.LibResilientPlugins = new Array()
}
// initialize the LibResilientConfig array
//
// this also sets some sane defaults,
// which then can be modified via config.js
if (typeof self.LibResilientConfig !== 'object' || self.LibResilientConfig === null) {
self.LibResilientConfig = {
// how long do we wait before we decide that a plugin is unresponsive,
// and move on?
defaultPluginTimeout: 10000,
// plugins settings namespace
//
// this defines which plugins get loaded,
// and the order in which they are deployed to try to retrieve content
// assumption: plugin path = ./plugins/<plugin-name>.js
//
// this relies on JavaScript preserving the insertion order for properties
// https://stackoverflow.com/a/5525820
plugins: {
'fetch':{},
'cache':{},
'alt-fetch':{},
'gun-ipfs':{}
},
// which components should be logged?
// this is an array of strings, components not listed here
// will have their debug output disabled
//
// by default, the service worker and all enabled plugins
// (so, all components that are used)
loggedComponents: [
'service-worker',
'fetch',
'cache',
'alt-fetch',
'gun-ipfs'
]
}
}
/**
* internal logging facility
*
* component - name of the component being logged about
* if the component is not in the LibResilientConfig.loggedComponents array,
* message will not be displayed
* items - the rest of arguments will be passed to console.debug()
*/
self.log = function(component, ...items) {
if (self.LibResilientConfig.loggedComponents.indexOf(component) >= 0) {
console.debug(`LibResilient [COMMIT_UNKNOWN, ${component}] ::`, ...items)
}
}
// load the plugins
//
// everything in a try-catch block
// so that we get an informative message if there's an error
try {
// get the config
self.importScripts("./config.js")
// only now load the plugins (config.js could have changed the defaults)
var plugins = Object.keys(self.LibResilientConfig.plugins)
for (var i=0; i<plugins.length; i++) {
// load a plugin
self.importScripts(`./plugins/${plugins[i]}.js`)
// check if it loaded properly
var plugin = LibResilientPlugins.find(p=>p.name===plugins[i])
if (plugin === undefined) {
throw new Error(`Plugin not found: ${plugins[i]} (available plugins: ${LibResilientPlugins.map(p=>p.name).join(', ')})`)
}
// make sure that the indirect flag is set if needed
if (self.LibResilientConfig.plugins[plugin.name].indirect===true) {
plugin.indirect=true
self.log('service-worker', `Loaded plugin: ${plugin.name} (indirect)`)
} else {
self.log('service-worker', `Loaded plugin: ${plugin.name}`)
}
// make sure plugins used by the just-loaded plugin are also added to the list
// but don't load a plugin twice
if (typeof plugin.uses !== "undefined") {
for (p in plugin.uses) {
if (plugins.indexOf(p) < 0) {
// okay, this plugin has not been added to the plugins list yet
// let's do that
plugins.push(p)
// but also, let's make sure that the config for them is available for use
var pConfig = {...self.LibResilientConfig.plugins[p], ...plugin.uses[p]}
// set the indirect flag,
// since we only have this plugin here to facilitate use by other plugins
pConfig.indirect = true
// set the config
self.LibResilientConfig.plugins[p] = pConfig
}
}
}
}
// inform
self.log('service-worker', `DEBUG: Strategy in use: ${LibResilientPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`)
} catch(e) {
// we only get a cryptic "Error while registering a service worker"
// unless we explicitly print the errors out in the console
console.error(e)
throw e
}
/**
* fetch counter per clientId
*
* we need to keep track of active fetches per clientId
* so that we can inform a given clientId when we're completely done
*/
self.activeFetches = {}
/**
* decrement fetches counter
* and inform the correct clientId if all is finished done
*/
let decrementActiveFetches = (clientId) => {
// decrement the fetch counter for the client
self.activeFetches[clientId]--
self.log('service-worker', '+-- activeFetches[' + clientId + ']:', self.activeFetches[clientId])
if (self.activeFetches[clientId] === 0) {
self.log('service-worker', 'All fetches done!')
// inform the client
// client has to be smart enough to know if that is just temporary
// (and new fetches will fire in a moment, because a CSS file just
// got fetched) or not
clients.get(clientId).then((client)=>{
client.postMessage({
allFetched: true
})
})
.then(()=>{
self.log('service-worker', 'all-fetched message sent.')
})
}
}
/*
* returns a Promise that either resolves or rejects after a set timeout
* optionally with a specific error message
*
* time - the timeout (in ms)
* timeout_resolves - whether the Promise should resolve() or reject() when hitting the timeout (default: false (reject))
* error_message - optional error message to use when rejecting (default: false (no error message))
*/
let promiseTimeout = (time, timeout_resolves=false, error_message=false) => {
return new Promise((resolve, reject)=>{
setTimeout(()=>{
if (timeout_resolves) {
resolve(time);
} else {
if (error_message) {
reject(new Error(error_message))
} else {
reject(time)
}
}
},time);
});
};
/* ========================================================================= *\
|* === LibResilientResourceInfo === *|
\* ========================================================================= */
/**
* LibResilient resource info class
*
* keeps the values as long as the service worker is running,
* and communicates all changes to relevant clients
*
* clients are responsible for saving and keeping the values across
* service worker restarts, if that's required
*/
let LibResilientResourceInfo = class {
// actual values of the fields
// only used internally, and stored into the Indexed DB
values = {
url: '', // read only after initialization
clientId: null,
fetchError: null,
method: null,
state: null, // can be "error", "success", "running"
serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only
}
client = null;
/**
* constructor
* needed to set the URL and clientId
*/
constructor(url, clientId) {
// set it
this.values.url = url
this.values.clientId = clientId
// we might not have a non-empty clientId if it's a cross-origin fetch
if (clientId) {
// get the client from Client API based on clientId
clients.get(clientId).then((client)=>{
// set the client
this.client = client
// Send a message to the client.
this.client.postMessage(this.values);
})
}
}
/**
* update this.values and immediately postMessage() to the relevant client
*
* data - an object with items to set in this.values
*/
update(data) {
// debug
var msg = 'Updated LibResilientResourceInfo for: ' + this.values.url
// was there a change? if not, no need to postMessage
var changed = false
// update the properties that are read-write
Object
.keys(data)
.filter((k)=>{
return ['fetchError', 'method', 'state'].includes(k)
})
.forEach((k)=>{
msg += '\n+-- ' + k + ': ' + data[k]
if (this.values[k] !== data[k]) {
msg += ' (changed!)'
changed = true
}
this.values[k] = data[k]
})
self.log('service-worker', msg)
// send the message to the client
if (this.client && changed) {
this.client.postMessage(this.values);
}
}
/**
* fetchError property
*/
get fetchError() {
return this.values.fetchError
}
/**
* method property
*/
get method() {
return this.values.method
}
/**
* state property
*/
get state() {
return this.values.state
}
/**
* serviceWorker property (read-only)
*/
get serviceWorker() {
return this.values.serviceWorker
}
/**
* url property (read-only)
*/
get url() {
return this.values.url
}
/**
* clientId property (read-only)
*/
get clientId() {
return this.values.clientId
}
}
/* ========================================================================= *\
|* === Main Brain of LibResilient === *|
\* ========================================================================= */
/**
* run a plugin's fetch() method
* while handling all the auxiliary stuff like saving info in reqInfo
*
* plugin - the plugin to use
* url - string containing the URL to fetch
* reqInfo - instance of LibResilientResourceInfo
*/
let libresilientFetch = (plugin, url, reqInfo) => {
// status of the plugin
reqInfo.update({
method: plugin.name,
state: "running"
})
// log stuff
self.log('service-worker', "LibResilient Service Worker handling URL:", url,
'\n+-- using method(s):', plugin.name)
// race the plugin(s) vs. a timeout
return Promise.race([
plugin.fetch(url),
promiseTimeout(
self.LibResilientConfig.defaultPluginTimeout,
false,
`LibResilient request using ${plugin.name} timed out after ${self.LibResilientConfig.defaultPluginTimeout}ms.`
)
])
}
/**
* calling a libresilient plugin function on the first plugin that implements it
*
* call - method name to call
* args - arguments that will be passed to it
*/
let callOnLibResilientPlugin = (call, args) => {
// find the first plugin implementing the method
for (i=0; i<LibResilientPlugins.length; i++) {
if (typeof LibResilientPlugins[i][call] === 'function') {
self.log('service-worker', 'Calling plugin ' + LibResilientPlugins[i].name + '.' + call + '()')
// call it
return LibResilientPlugins[i][call].apply(null, args)
}
}
}
/**
* Cycles through all the plugins, in the order they got registered,
* and returns a Promise resolving to a Response in case any of the plugins
* was able to get the resource
*
* request - string containing the URL we want to fetch
* clientId - string containing the clientId of the requesting client
* useStashed - use stashed resources; if false, only pull resources from live sources
* doStash - stash resources once fetched successfully; if false, do not stash pulled resources automagically
* stashedResponse - TBD
*/
let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStash=true, stashedResponse=null) => {
// clean the URL, removing any fragment identifier
var url = request.url.replace(/#.+$/, '');
// set-up reqInfo for the fetch event
var reqInfo = new LibResilientResourceInfo(url, clientId)
// fetch counter
self.activeFetches[clientId]++
// filter out stash plugins if need be
var LibResilientPluginsRun = LibResilientPlugins.filter((plugin)=>{
return ( (!plugin.indirect) && (useStashed || typeof plugin.stash !== 'function') )
})
/**
* this uses Array.reduce() to chain the LibResilientPlugins[]-generated Promises
* using the Promise the first registered plugin as the default value
*
* see: https://css-tricks.com/why-using-reduce-to-sequentially-resolve-promises-works/
*
* this also means that LibResilientPlugins[0].fetch() below will run first
* (counter-intutively!)
*
* we are slice()-ing it so that the first plugin is only run once; it is
* run in the initialValue parameter below already
*
* ref:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
*/
return LibResilientPluginsRun
.slice(1)
.reduce(
(prevPromise, currentPlugin)=>{
return prevPromise.catch((error)=>{
self.log('service-worker', "LibResilient plugin error for:", url,
'\n+-- method : ' + reqInfo.method,
'\n+-- error : ' + error.toString())
// save info in reqInfo -- status of the previous method
reqInfo.update({
state: "error",
fetchError: error.toString()
})
return libresilientFetch(currentPlugin, url, reqInfo)
})
},
// this libresilientFetch() will run first
// all other promises generated by LibResilientPlugins[] will be chained on it
// using the catch() in reduce() above
// skipping this very first plugin by way of slice(1)
libresilientFetch(LibResilientPluginsRun[0], url, reqInfo)
)
.then((response)=>{
// we got a successful response
decrementActiveFetches(clientId)
// record the success
reqInfo.update({state:"success"})
// get the plugin that was used to fetch content
plugin = LibResilientPlugins.find(p=>p.name===reqInfo.method)
// if it's a stashing plugin...
if (typeof plugin.stash === 'function') {
// we obviously do not want to stash
self.log('service-worker', 'Not stashing, since resource is already retrieved by a stashing plugin:', url);
// since we got the data from a stashing plugin,
// let's run the rest of plugins in the background to check if we can get a fresher resource
// and stash it in cache for later use
self.log('service-worker', 'starting background no-stashed fetch for:', url);
// event.waitUntil?
// https://stackoverflow.com/questions/37902441/what-does-event-waituntil-do-in-service-worker-and-why-is-it-needed/37906330#37906330
getResourceThroughLibResilient(request, clientId, false, true, response.clone())
// return the response so that stuff can keep happening
return response
// otherwise, let's see if we want to stash
// and if we already had a stashed version that differs
} else {
// do we have a stashed version that differs?
if (stashedResponse && stashedResponse.headers) {
// this is where we check if the response from whatever plugin we got it from
// is newer than what we've stashed
self.log('service-worker', 'checking freshness of stashed version of:', url,
'\n+-- stashed from :', stashedResponse.headers.get('X-LibResilient-Method'),
'\n+-- fetched using :', response.headers.get('X-LibResilient-Method'),
'\n+-- stashed X-LibResilient-ETag :', stashedResponse.headers.get('X-LibResilient-ETag'),
'\n+-- fetched X-LibResilient-ETag :', response.headers.get('X-LibResilient-ETag'))
// if the method does not match, or if it matches but the ETag doesn't
// we have a different response
// which means *probably* fresher content
if ( ( stashedResponse.headers.get('X-LibResilient-Method') !== response.headers.get('X-LibResilient-Method') )
|| ( stashedResponse.headers.get('X-LibResilient-ETag') !== response.headers.get('X-LibResilient-ETag') ) ) {
// inform!
self.log('service-worker', 'fetched version method or ETag differs from stashed for:', url)
clients.get(reqInfo.clientId).then((client)=>{
client.postMessage({
url: url,
fetchedDiffers: true
})
})
}
}
// do we want to stash?
if (doStash) {
// find the first stashing plugin
for (i=0; i<LibResilientPlugins.length; i++) {
if (typeof LibResilientPlugins[i].stash === 'function') {
// ok, now we're in business
var hdrs = '\n+-- headers:'
response.headers.forEach((v, k)=>{
hdrs += `\n +-- ${k} : ${v}`
})
self.log(
'service-worker',
`stashing a successful fetch of: ${url}`,
`\n+-- fetched using : ${response.headers.get('X-LibResilient-Method')}`,
`\n+-- stashing using : ${LibResilientPlugins[i].name}`,
hdrs
)
// working on clone()'ed response so that the original one is not touched
// TODO: should a failed stashing break the flow here? probably not!
return LibResilientPlugins[i].stash(response.clone(), url)
.then((res)=>{
// original response will be needed further down
return response
})
}
}
}
}
// if we're here it means we went through the whole list of plugins
// and found not a single stashing plugin
// or we don't want to stash the resources in the first place
// that's fine, but let's make sure the response goes forth
return response
})
// a final catch... in case all plugins fail
.catch((err)=>{
self.log('service-worker', "LibResilient also failed completely: ", err,
'\n+-- URL : ' + url)
// cleanup
reqInfo.update({
state: "error",
fetchError: err.toString()
})
// this is very naïve and should in fact be handled
// inside the relevant plugin, probably
// TODO: is this even needed?
reqInfo.update({method: null})
decrementActiveFetches(clientId)
// rethrow
throw err
})
}
/* ========================================================================= *\
|* === Setting up the event handlers === *|
\* ========================================================================= */
self.addEventListener('install', event => {
// TODO: Might we want to have a local cache?
// "COMMIT_UNKNOWN" will be replaced with commit ID
self.log('service-worker', "0. Installed LibResilient Service Worker (commit: COMMIT_UNKNOWN).");
// TODO: should we do some plugin initialization here?
});
self.addEventListener('activate', event => {
self.log('service-worker', "1. Activated LibResilient Service Worker (commit: COMMIT_UNKNOWN).");
// TODO: should we do some plugin initialization here?
});
self.addEventListener('fetch', event => {
// if event.resultingClientId is available, we need to use this
// otherwise event.clientId is what we want
// ref. https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/resultingClientId
var clientId = event.clientId
if (event.resultingClientId) {
clientId = event.resultingClientId
// yeah, we seem to have to send the client their clientId
// because there is no way to get that client-side
// and we need that for sane messaging later
//
// so let's also send the plugin list, why not
//
// *sigh* JS is great *sigh*
clients
.get(clientId)
.then((client)=>{
client.postMessage({
clientId: clientId,
plugins: LibResilientPlugins.filter(p=>(!p.indirect)).map((p)=>{return p.name}),
serviceWorker: 'COMMIT_UNKNOWN'
})
})
}
// counter!
if (typeof self.activeFetches[clientId] !== "number") {
self.activeFetches[clientId] = 0
}
// info
self.log('service-worker', "Fetching!",
"\n+-- url :", event.request.url,
"\n+-- clientId :", event.clientId,
"\n+-- resultingClientId:", event.resultingClientId,
"\n +-- activeFetches[" + clientId + "]:", self.activeFetches[clientId]
)
// External requests go through a regular fetch()
if (!event.request.url.startsWith(self.location.origin)) {
return void event.respondWith(fetch(event.request));
}
// Non-GET requests go through a regular fetch()
if (event.request.method !== 'GET') {
return void event.respondWith(fetch(event.request));
}
// GET requests to our own domain that are *not* #libresilient-info requests
// get handled by plugins in case of an error
return void event.respondWith(getResourceThroughLibResilient(event.request, clientId))
});
/**
* assumptions to be considered:
* every message contains clientId (so that we know where to respond if/when we need to)
*/
self.addEventListener('message', (event) => {
// inform
var msg = 'Message received!'
Object.keys(event.data).forEach((k)=>{
msg += '\n+-- key: ' + k + " :: val: " + event.data[k]
})
self.log('service-worker', msg);
/*
* supporting stash(), unstash(), and publish() only
*/
if (event.data.stash || event.data.unstash || event.data.publish) {
if (event.data.stash) {
callOnLibResilientPlugin('stash', event.data.stash)
}
if (event.data.unstash) {
callOnLibResilientPlugin('unstash', event.data.unstash)
}
if (event.data.publish) {
callOnLibResilientPlugin('publish', event.data.publish)
}
}
});