Skip to content

Commit

Permalink
feat: initial commit of workbox-ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m3sb0mb committed Jul 21, 2022
0 parents commit 85c3b32
Show file tree
Hide file tree
Showing 22 changed files with 26,643 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .commitlintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@commitlint/config-conventional"
]
}
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!.storybook
lib
node_modules
34 changes: 34 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
'parser': '@typescript-eslint/parser',
'extends': [
'eslint:recommended',
'prettier',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:import/typescript'
],
'plugins': ['@typescript-eslint', 'import', 'prettier'],
'env': {
'node': true,
'browser': true,
'commonjs': true,
'es6': true,
'jest': true
},
'rules': {
'prettier/prettier': 'error',
'indent': ['error', 2, { 'SwitchCase': 1 }],
'linebreak-style': ['error', 'unix'],
'no-console': 'error',
'quotes': ['error', 'single', { 'avoidEscape': true }],
'semi': ['error', 'always'],
'no-unused-vars': 'off',
'no-return-await': 'error',
'import/order': 'error',
'import/no-cycle': 'error',
'import/no-useless-path-segments': 'error',
'import/first': 'error',
'import/newline-after-import': 'error',
'import/no-extraneous-dependencies': 'error'
}
};
20 changes: 20 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: publish-npm-package
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
106 changes: 106 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
############################
# OS X
############################

.DS_Store
.AppleDouble
.LSOverride
Icon
.Spotlight-V100
.Trashes
._*


############################
# Linux
############################

*~


############################
# Windows
############################

Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msm
*.msp


############################
# Packages
############################

*.7z
*.csv
*.dat
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.com
*.class
*.dll
*.exe
*.o
*.seed
*.so
*.swo
*.swp
*.swn
*.swm
*.out
*.pid


############################
# Logs and databases
############################

*.log


############################
# Misc.
############################

*#
ssl
.idea
nbproject


############################
# Node.js
############################

lib-cov
lcov.info
pids
logs
results
node_modules
.node_history
.env


############################
# Tests
############################

coverage


############################
# Build
############################

lib
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run commit-msg
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-commit
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
!.storybook
lib
node_modules
.eslintrc.js
README.md
package-lock.json
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"printWidth": 120
}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2022 Giacomo Minighin

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Workbox IPFS

IPFS router for a Workbox service worker.

## Installation

```bash
npm install --save workbox-ipfs
```

## Usage

```js
import { initialize } from 'workbox-ipfs';

initialize({
/* IPFS Options */
})
```

## Install Dev Environment

```bash
npm install

npm run husky:install
```

## Run Dev Application

```bash
npm run develop
```
18 changes: 18 additions & 0 deletions dev/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Development</title>
</head>

<body>
<div id="root">
<p>Try it with /ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu</p>
<input id="ipfs-url" type="text" />
<input id="ipfs-get" type="button" value="Get File" />
<div id="ipfs-show"></div>
</div>
</body>

</html>
40 changes: 40 additions & 0 deletions dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Workbox } from 'workbox-window';

if ('serviceWorker' in navigator) {
const wb = new Workbox('/sw.js');
wb.addEventListener('waiting', (event) => {
wb.addEventListener('controlling', () => {
window.location.reload();
});
wb.messageSkipWaiting();
});
wb.register();
}

const input = <HTMLInputElement>document.getElementById('ipfs-url');
const button = <HTMLInputElement>document.getElementById('ipfs-get');
const show = <HTMLInputElement>document.getElementById('ipfs-show');

if (button && input) {
button.addEventListener('click', (event) => {
const value = input.value;
if (value) {
show.innerHTML = '';
fetch(value).then((res) => {
if (res.status >= 400) {
show.appendChild(document.createTextNode('Request failed with error code: ' + res.status));
} else {
res.blob().then((file) => {
show.appendChild(document.createTextNode('Request succeed with type: ' + file.type));
if (/^image\//.test(file.type)) {
const img = document.createElement('img');
img.src = URL.createObjectURL(file);
show.appendChild(document.createElement('br'));
show.appendChild(img);
}
});
}
});
}
});
}
21 changes: 21 additions & 0 deletions dev/sw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference lib="WebWorker" />
import { clientsClaim } from 'workbox-core';
import { initialize } from '../src';

declare const self: ServiceWorkerGlobalScope;

initialize({
config: {
Addresses: {
Gateway: 'https://gateway.ipfs.io',
},
},
});

addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});

clientsClaim();
Loading

0 comments on commit 85c3b32

Please sign in to comment.