Verified Commit 32986b71 authored by Aral Balkan's avatar Aral Balkan
Browse files

Initial add

parents
Pipeline #797 canceled with stages
node_modules/
.nyc_output/
coverage/
dist/
tmp/
npm-debug.log*
.DS_Storeweb/dist
# Hypha change log
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
Nothing yet.
## [1.0.0]
- In progress.
This diff is collapsed.
# Hypha
An attempt at building a peer web.
## Usage
Command | Description |
-----------------------|--------------------------------------------------|
`$ npm run development`| Start Hypha in development mode with live reload
`$ npm run production` | Start Hypha in production mode
`$ npm test` | Lint, validate deps & run tests
`$ npm run build` | Compile all files into `dist/`
`$ npm run create` | Generate a scaffold file
`$ npm run inspect` | Inspect the bundle's dependencies
`$ npm start` | Alias for `npm run development`
`$ npm run dev` | Alias for `npm run development`
`$ npm run prod` | Alias for `npm run production`
## License
[AGPLv3 or later](./LICENSE.md)
This diff is collapsed.
{
"name": "@ind.ie/hypha",
"version": "1.0.0",
"license": "AGPL-3.0-or-later",
"author": "Aral Balkan <aral@ind.ie> (https://ar.al)",
"private": false,
"scripts": {
"create": "choo-scaffold",
"inspect": "./node_modules/@hypha/web-compiler/bin.js inspect web/index.js",
"build": "./node_modules/@hypha/web-compiler/bin.js build web/index.js",
"development": "./node_modules/nodemon/bin/nodemon.js untrusted/index.js --watch untrusted/index.js",
"dev": "./node_modules/nodemon/bin/nodemon.js untrusted/index.js --watch untrusted/index.js",
"start": "./node_modules/nodemon/bin/nodemon.js untrusted/index.js --watch untrusted/index.js",
"production": "NODE_ENV=production ./node_modules/nodemon/bin/nodemon.js untrusted/index.js --watch untrusted/index.js",
"prod": "NODE_ENV=production ./node_modules/nodemon/bin/nodemon.js untrusted/index.js --watch untrusted/index.js",
"test": "standard && npm run test-deps",
"test-deps": "dependency-check . && dependency-check . --extra --no-dev -i tachyons"
},
"dependencies": {
"@ind.ie/https-server": "^2.0.0",
"choo": "^6.13.1",
"choo-service-worker": "^2.4.0",
"express": "^4.16.4",
"express-ws": "^4.0.0",
"sheetify": "^7.3.3"
},
"devDependencies": {
"@hypha/web-compiler": "git+https://aral@source.ind.ie/hypha/tools/web-compiler",
"choo-devtools": "^2.5.1",
"choo-scaffold": "^1.2.0",
"dependency-check": "^3.3.0",
"nodemon": "^1.18.10",
"standard": "^12.0.1"
}
}
const httpsServer = require('@ind.ie/https-server')
const express = require('express')
const expressWebSocket = require('express-ws')
const path = require('path')
// Catch any uncaught errors.
process.on('uncaughtException', function (error) {
console.log('Uncaught exception:', error)
})
// Create the Express app, the HTTPS server, and add WebSocket support.
const app = express()
const server = httpsServer.createServer({}, app)
expressWebSocket(app, server, { perMessageDeflate: false })
//
// Websocket routes go here.
//
app.ws('/echo', (webSocket, request) => {
webSocket.on('message', message => {
console.log('Got web socket request to echo ', message)
webSocket.send(message)
})
})
//
// HTTPS routes go here.
//
app.get('/https-get', (request, response) => {
response.writeHeader(200, {'Content-Type': 'text/html'})
response.end('<!doctype html><html lang=\'en\'><head><meta charset=\'utf-8\'/><title>Hello</title><style>body{background-color: white; font-family: sans-serif;}</style></head><body><h1>Hypha</h1><p>Hello, I am a dynamically-served HTTPS GET route.</p></body></html>')
})
//
// Set up @hypha/web-compiler.
//
// In development, we use it as middleware to enable live compilation and live reload.
// In production, use build a static distribution and serve it with express.static.
//
const entryPoint = path.join(__dirname, '../web/index.js')
if (process.env.NODE_ENV === 'production') {
//
// Production mode.
//
console.log(' 🔒 [hypha] Production mode\n')
// Build the static distribution.
const build = require('@hypha/web-compiler/lib/cmd-build')
build(entryPoint, null, {base: 'https://localhost'})
// TODO: Extend cmd-build to emit a done event so we can listen for it.
// TODO: Disable SSR.
// Serve the static distribution.
app.use(express.static('web/dist'))
} else {
//
// Development mode.
//
console.log(' 👷 [hypha] Development mode\n')
// Set up development mode with live compilation and reload.
const webCompilerMiddleware = require('@hypha/web-compiler/http')(entryPoint)
app.use(webCompilerMiddleware)
webCompilerMiddleware.compiler.on('error', (nodeName, edgeName, error) => {
console.log('Compiler error', nodeName, edgeName, error)
})
}
// Handle errors.
app.use(function(error, request, response, next) {
console.log('Error', error)
res.send(500)
})
// Start the server.
server.listen(443, () => {
console.log(' 📡 [hypha] Server running on port 443.\n')
})
var choo = require('choo')
var app = choo()
// console.log(`process.env.NODE_ENV`, process.env.NODE_ENV)
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-devtools')())
} else {
app.use(require('choo-service-worker')())
}
app.route('/', require('./views'))
app.route('/*', require('./views/404'))
module.exports = app.mount('body')
{
"name": "Hypha",
"short_name": "Hypha",
"description": "An attempt at building a peer web.",
"start_url": "/",
"display": "standalone",
"background_color": "#000",
"theme_color": "#000",
"icons": [{
"src": "/assets/icon.png",
"type": "image/png",
"sizes": "512x512"
}]
}
\ No newline at end of file
/* eslint-env serviceworker */
var VERSION = require('../package.json').version
var URLS = process.env.FILE_LIST
URLS.push('index.html')
URLS.push('/')
// Respond with cached resources
self.addEventListener('fetch', function (e) {
e.respondWith(self.caches.match(e.request).then(function (request) {
if (request) return request
else return self.fetch(e.request)
}))
})
// Register worker
self.addEventListener('install', function (e) {
e.waitUntil(self.caches.open(VERSION).then(function (cache) {
return cache.addAll(URLS)
}))
})
// Remove outdated resources
self.addEventListener('activate', function (e) {
e.waitUntil(self.caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key, i) {
if (keyList[i] !== VERSION) return self.caches.delete(keyList[i])
}))
}))
})
const html = require('choo/html')
const css = require('sheetify')
var TITLE = 'Hypha: route not found'
module.exports = view
function view (state, emit) {
if (state.title !== TITLE) emit(state.events.DOMTITLECHANGE, TITLE)
const bodyClass = css('./styles/body.css')
return html`
<body class='${bodyClass}'>
<h1>Route not found.</h1>
<a href='/'>Back to index.</a>
</body>
`
}
const html = require('choo/html')
const css = require('sheetify')
const TITLE = 'Hypha'
module.exports = view
function view (state, emit) {
if (state.title !== TITLE) emit(state.events.DOMTITLECHANGE, TITLE)
const bodyClass = css('./styles/body.css')
const socket = new WebSocket('wss://localhost/echo')
socket.onopen = event => {
console.log('Socket open.')
socket.send('Hello, there!')
}
socket.onmessage = event => {
console.log(`Received: ${event.data}`)
}
return html`
<body class='${bodyClass}'>
<h1>Hypha</h1>
<p>An attempt to build a peer web.</p>
</body>
`
}
/* Body styles */
:host {
background-color: white;
font-family: sans-serif;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment