Lotsa changes :)

This commit is contained in:
bobbydigitales
2024-10-03 12:32:59 -07:00
parent c449267f01
commit 2236ff0e6d
41 changed files with 1402 additions and 541 deletions

42
static/app.webmanifest Normal file
View File

@@ -0,0 +1,42 @@
{
"name": "ddln",
"short_name": "ddln",
"start_url": "/",
"display": "standalone",
"display_override": [
"window-controls-overlay",
"standalone"
],
"id": "b1dbe643-36fc-4419-9448-80f32a1baa1a",
"background_color": "#000000",
"theme_color": "#000000",
"icons": [
{
"src": "/static/icons/dandelion_512x512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"screenshots": [
{
"src": "/static/images/screenshot1.jpg",
"sizes": "1280x720",
"type": "image/png",
"form_factor": "wide",
"label": "Dandelion desktop"
},
{
"src": "/static/images/screenshot2.jpg",
"sizes": "720x1280",
"type": "image/png",
"form_factor": "narrow",
"label": "Dandelion mobile"
}
],
"protocol_handlers": [
{
"protocol": "web+ddln",
"url": "/%s"
}
]
}

BIN
static/bookerly.woff2 Normal file

Binary file not shown.

286
static/db.js Normal file
View File

@@ -0,0 +1,286 @@
// interface MyJsonObject {
// id: string;
// name: string;
// email: string;
// }
// Efficiently storing data in indexdb: https://stackoverflow.com/a/62975917
const postStoreName = "posts";
let keyBase = "dandelion_posts_v1_";
let key = "";
let version = 1;
function upgrade_0to1(db) {
let store = db.createObjectStore(postStoreName, { keyPath: "id", autoIncrement: true });
store.createIndex("datetimeIndex", "post_timestamp", { unique: false });
store.createIndex("postIDIndex", "data.post_id", { unique: true });
}
export function openDatabase(userID) {
const dbName = `user_${userID}`;
return new Promise((resolve, reject) => {
const request = indexedDB.open(dbName, version);
request.onerror = (event) => {
const errorEvent = event;
reject(`Database error: ${errorEvent.target.error?.message}`);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
upgrade_0to1(db);
};
request.onsuccess = (event) => {
const db = event.target.result;
resolve(db);
};
});
}
async function getDBTransactionStore(userID) {
const db = await openDatabase(userID);
const transaction = db.transaction(postStoreName, "readwrite");
const store = transaction.objectStore(postStoreName);
return { db, transaction, store };
}
export async function addData(userID, data) {
try {
const { db, transaction, store } = await getDBTransactionStore(userID);
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
addRequest.onsuccess = (e) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
};
addRequest.onerror = (event) => {
// Use a type assertion to access the specific properties of IDBRequest error event
const errorEvent = event;
console.error('Error in adding data:', errorEvent.target.error?.message);
};
}
catch (error) {
console.error('Error in opening database:', error);
}
}
export async function deleteData(userID, postID) {
try {
const { db, transaction, store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
const getRequest = index.getKey(postID);
getRequest.onerror = e => console.log(e.target.error);
getRequest.onsuccess = e => {
const key = e.target.result;
if (key === undefined) {
console.error("Post not found");
return null;
}
const deleteRequest = store.delete(key);
deleteRequest.onerror = e => { console.error(e.target.error); return false; };
deleteRequest.onsuccess = () => true;
};
}
catch (error) {
console.error('Error in opening database:', error);
}
}
export async function clearData(userID) {
try {
const { db, transaction, store } = await getDBTransactionStore(userID);
const clearRequest = store.clear();
clearRequest.onsuccess = (e) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
};
clearRequest.onerror = (event) => {
// Use a type assertion to access the specific properties of IDBRequest error event
const errorEvent = event;
console.error('Error in clearing data:', errorEvent.target.error?.message);
};
}
catch (error) {
console.error('Error in opening database:', error);
}
}
export async function addDataArray(userID, array) {
try {
const { db, transaction, store } = await getDBTransactionStore(userID);
let count = 0;
array.reverse();
for (let data of array) {
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
addRequest.onsuccess = (e) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
};
addRequest.onerror = (event) => {
// Use a type assertion to access the specific properties of IDBRequest error event
const errorEvent = event;
console.error('Error in adding data:', errorEvent.target.error?.message);
};
count++;
// if (count % 100 === 0) {
// console.log(`Added ${count} posts...`);
// }
}
}
catch (error) {
console.error('Error in opening database:', error);
}
}
export async function checkPostIds(userID, post_ids) {
try {
const { db, transaction, store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
transaction.oncomplete = () => {
// console.log("Transaction completed successfully");
db.close();
};
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
db.close();
};
let postIdsNeeded = [];
for (let id of post_ids) {
try {
let havePost = await new Promise((resolve, reject) => {
const getRequest = index.getKey(id);
getRequest.onerror = (e) => {
console.log(e.target.error);
reject(e);
};
getRequest.onsuccess = async (e) => {
const key = e.target.result;
resolve(key !== undefined);
};
});
// console.log(post.post_id, havePost);
if (!havePost) {
postIdsNeeded.push(id);
}
}
catch (error) {
console.error("Error processing post:", error);
}
}
console.log(`checkPostIds need ${postIdsNeeded.length} posts`);
return postIdsNeeded;
}
catch (error) {
console.error("Error in opening database:", error);
}
}
export async function mergeDataArray(userID, array) {
try {
const { db, transaction, store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
transaction.oncomplete = () => {
// console.log("Transaction completed successfully");
db.close();
};
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
db.close();
};
let postsToWrite = [];
for (let post of array) {
try {
let havePost = await new Promise((resolve, reject) => {
const getRequest = index.getKey(post.post_id);
getRequest.onerror = (e) => {
console.log(e.target.error);
reject(e);
};
getRequest.onsuccess = async (e) => {
const key = e.target.result;
resolve(key !== undefined);
};
});
// console.log(post.post_id, havePost);
if (!havePost) {
postsToWrite.push(post);
}
}
catch (error) {
console.error("Error processing post:", error);
}
}
console.log(`Writing ${postsToWrite.length} posts`);
await addDataArray(userID, postsToWrite);
}
catch (error) {
console.error("Error in opening database:", error);
}
}
export async function getPostForUser(userID, postID) {
}
export async function getData(userID, lowerID, upperID) {
const { store } = await getDBTransactionStore(userID);
const keyRangeValue = IDBKeyRange.bound(lowerID, upperID);
const index = store.index("datetimeIndex");
return new Promise((resolve, reject) => {
const getAllRequest = index.getAll(keyRangeValue);
getAllRequest.onsuccess = () => {
const records = getAllRequest.result.map((item) => item.data);
resolve(records);
};
getAllRequest.onerror = () => {
console.error('Transaction failed:', getAllRequest.error?.message);
reject(getAllRequest.error);
};
});
}
export async function getAllData(userID) {
const { store } = await getDBTransactionStore(userID);
return new Promise((resolve, reject) => {
const getRequest = store.getAll();
getRequest.onsuccess = () => {
if (getRequest.result) {
// console.log('Retrieved data:', getRequest.result.jsonData);
// resolve(getRequest.result.jsonData as any);
resolve(getRequest.result);
}
else {
console.log('No data record found for key', key);
resolve(undefined); // explicitly resolve with undefined when no data is found
}
};
getRequest.onerror = (event) => {
// Use a type assertion to access the specific properties of IDBRequest error event
const errorEvent = event;
console.error('Transaction failed:', errorEvent.target.error?.message);
reject(errorEvent.target.error); // reject the promise if there's an error
};
});
}
export async function getAllIds(userID) {
const { store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
let keys = [];
return new Promise((resolve, reject) => {
let request = index.openKeyCursor();
request.onsuccess = (event) => {
let cursor = event.target.result;
if (cursor) {
keys.push(cursor.key);
cursor.continue();
}
else {
resolve(keys);
}
};
request.onerror = (event) => {
reject(event);
};
});
}
export async function getPostsByIds(userID, postIDs) {
const { store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
let posts = [];
for (const postID of postIDs) {
const post = await new Promise((resolve, reject) => {
let request = index.get(postID);
request.onsuccess = (event) => {
resolve(event.target.result); // Resolve with the post
};
request.onerror = (event) => {
reject(event); // Reject if any error occurs
};
});
if (post) {
posts.push(post); // Add the post to the result array if found
}
}
return posts; // Return the array of posts
}
//# sourceMappingURL=db.js.map

1
static/db.js.map Normal file

File diff suppressed because one or more lines are too long

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

68
static/index.html Normal file
View File

@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1">
<title>Dandelion</title>
<script type="module" src="/static/main.js"></script>
<script src="/static/lib/marked.min.js"></script>
<script src="/static/lib/qrcode.min.js"></script>
<!-- <script src="lib/lottie.min.js"></script> -->
<!-- <script src="https://unpkg.com/@dotlottie/player-component@latest/dist/dotlottie-player.mjs" type="module"></script> -->
<!-- <script src="/lib/webtorrent/webtorrent_1_8_0.min.js"></script> -->
<link rel="manifest" href="/static/app.webmanifest">
<link rel="stylesheet" href="/static/main.css">
</head>
<body>
<div class="flex-container">
<div class="content">
<div class="img-button" id="ddln_logo_button"><img class="logo" src="/static/favicon.ico"></div>
<div id="status"></div>
<div id="info" style="display:none">
<div id="profile">
<span class="form_label">username:</span><span class="form_field" id="username" contenteditable="true">unnamed</span>
</div>
<!-- <div id="following">
<div>fiona</div>
<div>fiona</div>
</div> -->
<div id="peers"></div>
<div id="log" ></div>
<div id="connectURL"></div>
<div id="qrcode"></div>
<!-- <dotlottie-player src="https://lottie.host/272b60dd-462d-42a3-8ed6-fec4143633d6/X4FxBascRI.json" background="transparent" speed="1" style="width: 300px; height: 300px" direction="1" playMode="normal" loop controls autoplay></dotlottie-player> -->
</div>
<!-- <div id="peer_display"><canvas></canvas></div> -->
<div id="buttons">
<!-- <button id="button_font1" >font1</button>
<button id="button_font2" >font2 </button> -->
<button id="import_tweets" >import</button>
<button id="clear_posts" >clear </button>
<button id="update_app" >check for updates</button>
<button id="toggle_dark" >light/dark</button>
</div>
<textarea cols="60" rows="6" id="textarea_post"></textarea>
<div class="right">
<label for="file_input" id="file_input_label" class="button">photo</label>
<input type="file" id="file_input" multiple style="display:none">
<!-- <button id="button_add_pic" >🏞️</button> -->
<button id="button_post" >post</button>
</div>
<!-- <div id="torrent-content"></div> -->
<div id="content"></div>
</div>
</div>
</body>
</html>

1
static/lib/lottie.min.js vendored Normal file

File diff suppressed because one or more lines are too long

6
static/lib/marked.min.js vendored Normal file

File diff suppressed because one or more lines are too long

1
static/lib/qrcode.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

153
static/main.css Normal file
View File

@@ -0,0 +1,153 @@
:root {
--main-bg-color: black;
--border-color:rgb(132,136,138);
--edge-color:rgb(60,60,60);
--main-fg-color:rgb(202,208,211);
--highlight-fg-color:rgb(255,255,255);
--link-color:rgb(29, 155, 240);
}
body {
font-family: sans-serif;
color: var(--main-fg-color);
background-color: var(--main-bg-color);
margin:0px;
}
hr {
border-color: rgb(60, 60, 60);
}
.form_field {
font-size: medium;
font-family: sans-serif;
background-color: var(--edge-color);
color: var(--main-fg-color);
width: 100%;
box-sizing: border-box;
padding-left: 5px;
padding-right: 5px;
border: 1px solid var(--border-color);
resize: vertical;
border-radius: 20px;
}
#textarea_post {
font-size: medium;
font-family: sans-serif;
background-color: var(--main-bg-color);
color: var(--main-fg-color);
width: 100%;
box-sizing: border-box;
padding-left: 30px;
padding-right: 30px;
padding-top: 10px;
border: 1px solid var(--border-color);
resize: vertical;
border-radius: 40px;
}
.flex-container {
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
padding: 0px;
/* Add some padding around the flex container */
}
.content {
max-width: 600px;
/* Your preferred max width for the content */
flex: 1;
/* Shorthand for flex-grow, flex-shrink and flex-basis */
min-width: 300px;
/* Minimum width the content can shrink to */
padding: 20px;
box-shadow: 0 0 5px var(--edge-color);
text-align: left;
overflow-x: hidden;
/* Hide horizontal overflow inside the flex container */
line-height: 1.3;
}
.embed {
max-width: 800px;
border-color: red;
border: 1px, solid;
padding: 20px;
}
.postImage {
width: 100%;
}
#log {
font-family: monospace;
text-wrap: nowrap;
font-size: 10px;
margin-bottom: 20px;
height: 150px;
width: 50%;
}
.right {
text-align: right;
}
#buttons {
margin-left: 40px;
}
#button_post {
margin-right: 40px;
}
a {
color: var(--link-color);
}
.logo {
width: 32px;
height: 32px;
image-rendering: pixelated;
}
#torrent-content {
border: solid 1px;
width: 800px;
}
.img-button {
cursor: pointer;
}
button,.button {
font-size:small;
background-color: var(--main-bg-color);
border-radius: 10px;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
margin-left: 5px;
color: var(--highlight-fg-color);
/* border:solid 1px white; */
border: 1px solid var(--border-color);
color: var(--main-fg-color);
cursor: pointer;
}
video {
width:100%
}
iframe {
width: 100%
}
.qrcode_image {
background-color: var(--highlight-fg-color);
padding: 10px;
}

952
static/main.js Normal file
View File

@@ -0,0 +1,952 @@
// TODO: virtual list, only rerender what's needed so things can keep playing.
import { getData, addData, addDataArray, clearData, deleteData, mergeDataArray, checkPostIds, getAllIds, getPostsByIds } from "./db.js";
// let posts:any;
// let keyBase = "dandelion_posts_v1_"
// let key:string = "";
// interface PostTimestamp {
// year: number,
// month: number,
// day: number,
// hour: number,
// minute: number,
// second: number,
// }
function waitMs(durationMs) {
return new Promise(resolve => setTimeout(resolve, durationMs));
}
function uuidv4() {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c) => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}
function uuidToBytes(uuid) {
return new Uint8Array(uuid.match(/[a-fA-F0-9]{2}/g).map((hex) => parseInt(hex, 16)));
}
// Base58 character set
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
// Base58 encoding
// Base58 encoding
function encodeBase58(buffer) {
let carry;
const digits = [0];
for (const byte of buffer) {
carry = byte;
for (let i = 0; i < digits.length; i++) {
carry += digits[i] << 8;
digits[i] = carry % 58;
carry = Math.floor(carry / 58);
}
while (carry > 0) {
digits.push(carry % 58);
carry = Math.floor(carry / 58);
}
}
let result = '';
for (const digit of digits.reverse()) {
result += BASE58_ALPHABET[digit];
}
// Handle leading zero bytes
for (const byte of buffer) {
if (byte === 0x00) {
result = BASE58_ALPHABET[0] + result;
}
else {
break;
}
}
return result;
}
// Convert UUID v4 to Base58
function uuidToBase58(uuid) {
const bytes = uuidToBytes(uuid);
return encodeBase58(bytes);
}
let logLines = [];
let logLength = 10;
function log(message) {
console.log(message);
logLines.push(`${new Date().toLocaleTimeString()}: ${message}`);
if (logLines.length > 10) {
logLines = logLines.slice(logLines.length - logLength);
}
let log = document.getElementById("log");
if (!log) {
throw new Error();
}
log.innerText = logLines.join("\n");
}
function generateID() {
if (self.crypto.hasOwnProperty("randomUUID")) {
return self.crypto.randomUUID();
}
return uuidv4();
}
class Post {
constructor(author, author_id, text, post_timestamp, imageData = null, importedFrom = null, importSource = null) {
this.post_timestamp = post_timestamp;
this.post_id = generateID();
this.author = author;
this.author_id = author_id;
this.text = text;
this.image_data = imageData;
this.importedFrom = importedFrom;
this.importSource = importSource;
}
}
window.addEventListener('scroll', () => {
// Total height of the document
const totalPageHeight = document.body.scrollHeight;
// Current scroll position
const scrollPoint = window.scrollY + window.innerHeight;
// Check if scrolled to bottom
if (scrollPoint >= totalPageHeight) {
console.log('Scrolled to the bottom!');
console.log(scrollPoint, totalPageHeight);
}
});
// let peer = await new PeerConnection(peer_id);
// let connectionReply = await wsConnection.send('hello');
// for (let peer of connectionReply) {
// let peerConnection = await wsConnection.send('connect', peer.id);
// if (peerConnection) {
// this.peers.push(peerConnection);
// let postIDs = await peerConnection.getPostIDs();
// let postsWeDontHave = this.diffPostIDs(postIDs);
// let newPosts = await peerConnection.getPosts(postsWeDontHave);
// this.addPosts(newPosts);
// }
// }
async function bytesToBase64DataUrl(bytes, type = "application/octet-stream") {
return await new Promise((resolve, reject) => {
const reader = Object.assign(new FileReader(), {
onload: () => resolve(reader.result),
onerror: () => reject(reader.error),
});
reader.readAsDataURL(new File([bytes], "", { type }));
});
}
async function arrayBufferToBase64(buffer) {
var bytes = new Uint8Array(buffer);
return (await bytesToBase64DataUrl(bytes)).replace("data:application/octet-stream;base64,", "");
}
// function base64ToArrayBuffer(base64: string) {
// var binaryString = atob(base64);
// var bytes = new Uint8Array(binaryString.length);
// for (var i = 0; i < binaryString.length; i++) {
// bytes[i] = binaryString.charCodeAt(i);
// }
// return bytes.buffer;
// }
async function base64ToArrayBuffer(base64String) {
let response = await fetch("data:application/octet-stream;base64," + base64String);
let arrayBuffer = await response.arrayBuffer();
return arrayBuffer;
// let buffer = new Uint8Array(arrayBuffer);
// return buffer;
}
class wsConnection {
send(message) {
let json = "";
try {
json = JSON.stringify(message);
}
catch (e) {
console.log(e, "wsConnection send: Couldn't serialize message", message);
}
// log(`ws->${json.slice(0, 240)}`)
this.websocket.send(json);
}
helloResponseHandler(data) {
let users = [];
try {
let currentUserPeers = data.userPeers[app.router.userID];
users.push([app.router.userID, data.userPeers[app.router.userID]]);
delete data.userPeers[app.router.userID];
}
catch (e) {
console.log('helloResponseHandler', e);
}
users = [...users, ...Object.entries(data.userPeers)];
log(`Network: got ${users.length} users from bootstrap peer. ${users.join(',')}`);
for (let [userID, peerIDs] of users) {
this.peers.set(userID, [...Object.keys(peerIDs)]);
for (let peerID of [...Object.keys(peerIDs)]) {
if (peerID === this.peerID) {
continue;
}
log(`Network: Requesting post IDs for user ${userID} from peer ${peerID}`);
this.send({
type: "peer_message",
from: this.peerID,
to: peerID,
message: { type: "get_post_ids_for_user", user_id: userID }
});
}
}
}
pongHandler(data) {
}
async getPostIdsForUserResponseHandler(data) {
// log(`getPostsForUserResponse: ${data}`)
let message = data.message;
log(`Network: got ${message.post_ids.length} post IDs for user ${data.message.user_id} from peer ${data.from}`);
console.log(`Checking post IDs...`);
let postIds = await checkPostIds(message.user_id, data.message.post_ids);
if (postIds.length === 0) {
log(`Don't need any posts from peer ${data.from}`);
return;
}
log(`Network: requesting ${postIds.length} posts for user ${message.user_id} from peer ${data.from}`);
let responseMessage = { type: "peer_message", from: app.peerID, to: data.from, message: { type: "get_posts_for_user", post_ids: postIds, user_id: message.user_id } };
this.send(responseMessage);
}
// static async compressArrayBuffer(data: ArrayBuffer): Promise<ArrayBuffer> {
// const compressionStream = new CompressionStream('gzip'); // You can also use 'deflate', 'deflate-raw', etc.
// const compressedStream = new Response(
// new Blob([data]).stream().pipeThrough(compressionStream)
// );
// const compressedArrayBuffer = await compressedStream.arrayBuffer();
// return compressedArrayBuffer;
// }
async getPostIdsForUserHandler(data) {
let message = data.message;
let postIds = await getAllIds(message.user_id) ?? [];
if (postIds.length === 0) {
log(`Network: I know about user ${message.user_id} but I have 0 posts, so I'm not sending any to to peer ${data.from}`);
return;
}
log(`Network: Sending ${postIds.length} post Ids for user ${message.user_id} to peer ${data.from}`);
let responseMessage = { type: "peer_message", from: app.peerID, to: data.from, message: { type: "get_post_ids_for_user_response", post_ids: postIds, user_id: message.user_id } };
this.send(responseMessage);
}
// Send posts to peer
async getPostsForUserHandler(data) {
let message = data.message;
let posts = await getPostsByIds(message.user_id, message.post_ids) ?? [];
log(`Network: Sending ${posts.length} posts for user ${message.user_id} to peer ${data.from}`);
app.timerStart();
let output = [];
for (let post of posts) {
let newPost = post.data;
if (newPost.image_data) {
// let compressedData = await wsConnection.compressArrayBuffer(newPost.image_data);
// console.log((newPost.image_data.byteLength - compressedData.byteLength) / 1024 / 1024);
newPost.image_data = await arrayBufferToBase64(newPost.image_data);
}
// let megs = JSON.stringify(newPost).length/1024/1024;
// console.log(`getPostsForUserHandler id:${newPost.post_id} post length:${megs}`);
output.push(newPost);
}
// posts = posts.map((post:any)=>{let newPost = post.data; if (newPost.image_data){newPost.image_data = arraybufferto};return newPost});
// posts = posts.map((post:any)=>{})
let responseMessage = { type: "peer_message", from: app.peerID, to: data.from, message: { type: "get_posts_for_user_response", posts: output, user_id: message.user_id } };
this.send(responseMessage);
let sendTime = app.timerDelta();
log(`send took: ${sendTime.toFixed(2)}ms`);
}
// Got posts from peer
async getPostsForUserReponseHandler(data) {
app.timerStart();
let message = data.message;
console.log(`Network: got ${message.posts.length} posts for user ${message.user_id} from peer ${data.from}`);
console.log(`getPostsForUserResponseHandler Got ${message.posts.length} from peer ${data.from}`);
for (let post of message.posts) {
if (message.user_id === app.userID) {
post.author_id = app.userID;
}
post.post_timestamp = new Date(post.post_timestamp);
if (post.image_data) {
post.image_data = await base64ToArrayBuffer(post.image_data);
}
}
console.log(`Merging same user peer posts...`);
await mergeDataArray(message.user_id, data.message.posts);
let receiveTime = app.timerDelta();
log(`Receive took: ${receiveTime.toFixed(2)}ms`);
if (message.user_id === app.router.userID) {
app.render();
}
}
async peerMessageHandler(data) {
// log(`peerMessageHandler ${JSON.stringify(data)}`)
let peerMessageType = data.message.type;
let handler = this.peerMessageHandlers.get(peerMessageType);
if (!handler) {
console.error(`got peer message type we don't have a handler for: ${peerMessageType}`);
return;
}
handler(data);
}
connect() {
if (this.websocket?.readyState === WebSocket.OPEN) {
return;
}
window.clearInterval(this.websocketPingInterval);
if (this.websocket) {
this.websocket.close();
}
;
try {
this.websocket = new WebSocket(`wss://${window.location.hostname}:${window.location.port}/ws`);
}
catch (error) {
console.log(error.message);
return;
}
this.websocket.onopen = async (event) => {
log("ws:connected");
let knownUsers = [...(await indexedDB.databases())].map((db) => db.name?.replace('user_', ''));
console.log('Network: Sending known users', knownUsers);
this.send({ type: "hello", user_id: this.userID, peer_id: this.peerID, known_users: knownUsers });
this.websocketPingInterval = window.setInterval(() => {
if (!navigator.onLine) {
return;
}
this.send({ type: "ping", peer_id: this.peerID });
}, 10000);
};
this.websocket.onclose = (event) => {
log("ws:disconnected");
// this.retry *= 2;
log(`Retrying in ${this.retry} seconds`);
window.setTimeout(() => { this.connect(); }, this.retry * 1000);
};
this.websocket.onmessage = (event) => {
// log('ws:<-' + event.data.slice(0, 240));
let data = JSON.parse(event.data);
let { type } = data;
let handler = this.messageHandlers.get(type);
if (!handler) {
console.warn(`Got a message we can't handle:`, type);
return;
}
handler(data);
};
this.websocket.onerror = (event) => {
log('ws:error: ' + event);
};
}
disconnect() {
this.websocket?.close();
}
constructor(userID, peerID) {
this.websocket = null;
this.userID = "";
this.peerID = "";
this.websocketPingInterval = 0;
this.retry = 10;
this.state = 'disconnected';
this.peers = new Map();
this.messageHandlers = new Map();
this.peerMessageHandlers = new Map();
this.userID = userID;
this.peerID = peerID;
this.messageHandlers.set('hello', this.helloResponseHandler.bind(this));
this.messageHandlers.set('pong', this.pongHandler);
this.messageHandlers.set('peer_message', this.peerMessageHandler.bind(this));
this.peerMessageHandlers.set('get_post_ids_for_user', this.getPostIdsForUserHandler.bind(this));
this.peerMessageHandlers.set('get_post_ids_for_user_response', this.getPostIdsForUserResponseHandler.bind(this));
this.peerMessageHandlers.set('get_posts_for_user', this.getPostsForUserHandler.bind(this));
this.peerMessageHandlers.set('get_posts_for_user_response', this.getPostsForUserReponseHandler.bind(this));
this.connect();
if (!this.websocket) {
// set a timer and retry?
}
}
}
class App {
constructor() {
this.username = '';
this.userID = '';
this.peerID = '';
this.posts = [];
this.isHeadless = false;
this.showLog = false;
this.time = 0;
this.animals = ['shrew', 'jerboa', 'lemur', 'weasel', 'possum', 'possum', 'marmoset', 'planigale', 'mole', 'narwhal'];
this.adjectives = ['snazzy', 'whimsical', 'jazzy', 'bonkers', 'wobbly', 'spiffy', 'chirpy', 'zesty', 'bubbly', 'perky', 'sassy'];
this.router = {
route: App.Route.HOME,
userID: '',
postID: '',
mediaID: ''
};
}
initMarkdown() {
const renderer = new marked.Renderer();
renderer.link = (href, title, text) => {
return `<a href="${href}" target="_blank"${title ? ` title="${title}"` : ''}>${text}</a>`;
};
marked.setOptions({ renderer: renderer });
}
// arrayBufferToBase64(buffer: ArrayBuffer) {
// return new Promise((resolve, reject) => {
// const blob = new Blob([buffer], { type: 'application/octet-stream' });
// const reader = new FileReader();
// reader.onloadend = () => {
// const dataUrl = reader.result as string;
// if (!dataUrl) {
// resolve(null);
// return;
// }
// const base64 = dataUrl.split(',')[1];
// resolve(base64);
// };
// reader.onerror = (error) => {
// reject(error);
// };
// reader.readAsDataURL(blob);
// });
// }
async createTestData() {
let postsTestData = await (await fetch("./postsTestData.json")).json();
return postsTestData;
}
timerStart() {
this.time = performance.now();
}
timerDelta() {
return performance.now() - this.time;
}
getFixedTweetText(entry) {
let fullText = entry.tweet.full_text;
let linkMarkdown = "";
for (const url of entry.tweet.entities.urls) {
linkMarkdown = `[${url.display_url}](${url.expanded_url})`;
fullText = fullText.replace(url.url, linkMarkdown);
}
return fullText;
}
async importTweetArchive(userID, tweetArchive) {
log("Importing tweet archive");
let postsTestData = [];
// let response = await fetch("./tweets.js");
// let tweetsText = await response.text();
// tweetsText = tweetsText.replace("window.YTD.tweets.part0", "window.tweetData");
// new Function(tweetsText)();
// let tweets = JSON.parse(tweetJSON);
let count = 0;
for (let entry of tweetArchive) {
// if (entry.tweet.hasOwnProperty("in_reply_to_screen_name") || entry.tweet.retweeted || entry.tweet.full_text.startsWith("RT")) {
// continue;
// }
let mediaURL = entry.tweet?.entities?.media?.[0]?.media_url_https;
let isImage = false;
if (mediaURL) {
isImage = mediaURL.includes('jpg');
}
let imageData = null;
// if (isImage) {
// try {
// let response = await fetch(mediaURL);
// await waitMs(100);
// if (response.status === 200) {
// imageData = await response.arrayBuffer();
// }
// console.log(imageData);
// } catch (e) {
// console.log(e);
// }
// }
let timeStamp = new Date(entry.tweet.created_at);
let tweetText = this.getFixedTweetText(entry);
let newPost = new Post('bobbydigitales', userID, tweetText, timeStamp, imageData, 'twitter', entry);
postsTestData.push(newPost);
count++;
if (count % 100 === 0) {
log(`Imported ${count} posts...`);
// render(postsTestData);
}
// if (count == 100-1) {
// break;
// }
}
return postsTestData;
}
async createTestData3(userID) {
let posts = await (await (fetch('./posts.json'))).json();
return posts;
}
async registerServiceWorker() {
if (!("serviceWorker" in navigator)) {
return;
}
let registrations = await navigator.serviceWorker.getRegistrations();
if (registrations.length > 0) {
console.log("Service worker already registered.");
return registrations[0];
}
navigator.serviceWorker
.register("/sw.js")
.then((registration) => {
console.log("Service Worker registered with scope:", registration.scope);
return registration;
})
.catch((error) => {
console.error("Service Worker registration failed:", error);
});
}
addPost(userID, postText, mediaData, mediaType) {
if ((typeof postText !== "string") || postText.length === 0) {
log("Not posting an empty string...");
return;
}
let post = new Post(this.username, userID, postText, new Date(), mediaData);
this.posts.push(post);
// localStorage.setItem(key, JSON.stringify(posts));
addData(userID, post);
this.render();
}
getPeerID() {
let id = localStorage.getItem("peer_id");
if (!id) {
id = generateID();
localStorage.setItem("peer_id", id);
}
return id;
}
getUserID() {
let id = localStorage.getItem("dandelion_id");
if (!id) {
id = generateID();
localStorage.setItem("dandelion_id", id);
}
return id;
}
hashUserIdToIndices() {
let indices = [];
for (let char of this.userID) {
if (char !== '0' && char !== '-') {
indices.push(parseInt(char, 16));
if (indices.length == 2) {
break;
}
}
}
return [indices[0], indices[1]];
}
getUsername() {
let username = localStorage.getItem("dandelion_username");
if (!username || username === "not_set") {
let [one, two] = this.hashUserIdToIndices();
let adjective = this.adjectives[one % this.adjectives.length];
let animal = this.animals[two % this.animals.length];
username = `${adjective}_${animal}`;
localStorage.setItem("dandelion_username", username);
}
return username;
}
setFont(fontName, fontSize) {
let content = document.getElementById('content');
if (!content) {
return;
}
content.style.fontFamily = fontName;
content.style.fontSize = fontSize;
let textArea = document.getElementById('textarea_post');
if (!textArea) {
return;
}
textArea.style.fontFamily = fontName;
textArea.style.fontSize = fontSize;
}
initOffline(connection) {
// Event listener for going offline
window.addEventListener('offline', () => {
log("offline");
});
// Event listener for going online
window.addEventListener('online', async () => {
log("online");
connection.connect();
this.render();
});
log(`Online status: ${navigator.onLine ? "online" : "offline"}`);
}
selectFile(contentType) {
return new Promise(resolve => {
let input = document.createElement('input');
input.type = 'file';
// input.multiple = multiple;
input.accept = contentType;
input.onchange = () => {
if (input.files == null) {
resolve(null);
return;
}
let files = Array.from(input.files);
// if (multiple)
// resolve(files);
// else
resolve(files[0]);
};
input.click();
});
}
readFile(file) {
// Always return a Promise
return new Promise((resolve, reject) => {
let content = '';
const reader = new FileReader();
// Wait till complete
reader.onloadend = function (e) {
content = e.target.result;
resolve(content);
};
// Make sure to handle error states
reader.onerror = function (e) {
reject(e);
};
reader.readAsText(file);
});
}
initButtons(userID, posts, registration) {
// let font1Button = document.getElementById("button_font1") as HTMLButtonElement;
// let font2Button = document.getElementById("button_font2") as HTMLButtonElement;
let importTweetsButton = document.getElementById("import_tweets");
let clearPostsButton = document.getElementById("clear_posts");
let updateApp = document.getElementById("update_app");
let ddlnLogoButton = document.getElementById('ddln_logo_button');
// let addPic = document.getElementById('button_add_pic') as HTMLDivElement;
let filePickerLabel = document.getElementById('file_input_label');
let filePicker = document.getElementById('file_input');
let toggleDark = document.getElementById('toggle_dark');
toggleDark.addEventListener('click', () => {
document.documentElement.style.setProperty('--main-bg-color', 'white');
document.documentElement.style.setProperty('--main-fg-color', 'black');
});
filePicker?.addEventListener('change', async (event) => {
for (let file of filePicker.files) {
let buffer = await file.arrayBuffer();
let type = this.addPost(this.userID, 'image...', buffer);
}
});
filePickerLabel?.addEventListener('click', () => {
console.log("Add pic...");
});
let usernameField = document.getElementById('username');
usernameField?.addEventListener('input', (event) => {
this.username = event.target.innerText;
localStorage.setItem("dandelion_username", this.username);
});
importTweetsButton.addEventListener('click', async () => {
let file = await this.selectFile('text/*');
console.log(file);
if (file == null) {
return;
}
let tweetData = await this.readFile(file);
tweetData = tweetData.replace('window.YTD.tweets.part0 = ', '');
const tweets = JSON.parse(tweetData);
let imported_posts = await this.importTweetArchive(userID, tweets);
clearData(userID);
// posts = posts.reverse();
addDataArray(userID, imported_posts);
this.render();
});
clearPostsButton.addEventListener('click', () => { clearData(userID); posts = []; this.render(); });
let postButton = document.getElementById("button_post");
let postText = document.getElementById("textarea_post");
if (!(postButton && postText)) {
throw new Error();
}
postText.addEventListener('paste', async (e) => {
const dataTransfer = e.clipboardData;
const file = dataTransfer.files[0];
let buffer = await file.arrayBuffer();
let type = this.addPost(this.userID, 'image...', buffer);
});
postButton.addEventListener("click", () => {
this.addPost(userID, postText.value);
postText.value = "";
});
updateApp.addEventListener("click", () => {
registration?.active?.postMessage({ type: "update_app" });
});
let infoElement = document.getElementById('info');
if (infoElement === null) {
return;
}
ddlnLogoButton.addEventListener('click', () => { infoElement.style.display == 'none' ? infoElement.style.display = 'block' : infoElement.style.display = 'none'; });
}
async loadPosts(userID, postID) {
this.timerStart();
let posts = [];
// if (postID) {
// posts = await gePostForUser(userID, postID);
// }
posts = await getData(userID, new Date(2022, 8), new Date());
if (posts.length > 0) {
log(`Loaded ${posts.length} posts in ${this.timerDelta().toFixed(2)}ms`);
return posts;
}
// posts = await createTestData2(userID);
// log("Adding test data...");
// addDataArray(userID, posts);
// return await getData(userID, new Date(2022, 8), new Date());
}
async purgeEmptyUsers() {
let knownUsers = [...(await indexedDB.databases())].map((db) => db.name?.replace('user_', ''));
if (!knownUsers) {
return;
}
for (let userID of knownUsers) {
let ids = await getAllIds(userID);
if (ids.length === 0) {
console.log(`Purging user ${userID}`);
indexedDB.deleteDatabase(`user_${userID}`);
}
console.log(`https://ddln.app/user/${userID}`);
}
}
async main() {
this.isHeadless = /\bHeadlessChrome\//.test(navigator.userAgent);
let userID = this.getUserID();
let peerID = this.getPeerID();
this.userID = userID;
this.peerID = peerID;
this.getRoute();
if (this.router.route === App.Route.CONNECT) {
console.log('connect', this.router.userID);
localStorage.setItem("dandelion_id", this.router.userID);
}
let urlParams = (new URL(window.location.href)).searchParams;
let connection_userID = urlParams.get('connect');
let registration = undefined;
if (urlParams.has('log')) {
document.getElementById('info').style.display = "block";
this.showLog = true;
}
let time = 0;
let delta = 0;
// let isPersisted = await navigator?.storage?.persisted();
// if (!isPersisted) {
// debugger;
// const isPersisted = await navigator.storage.persist();
// log(`Persisted storage granted: ${isPersisted}`);
// }
// log(`Persisted: ${(await navigator?.storage?.persisted())?.toString()}`);
this.initMarkdown();
// let main = await fetch("/main.js");
// let code = await main.text();
// console.log(code);
// registration.active.postMessage({type:"updateMain", code:code});
// this.posts = await this.loadPosts(userID) ?? [];
// debugger;
await this.render(); // , (postID:string)=>{this.deletePost(userID, postID)}
if (performance?.memory) {
log(`memory used: ${(performance.memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`);
}
// if (urlParams.get("sw") === "true") {
registration = await this.registerServiceWorker();
// }
this.username = this.getUsername();
document.getElementById('username').innerText = this.username;
this.initButtons(userID, this.posts, registration);
let connectURL = `https://${document.location.hostname}/connect/${this.userID}`;
document.getElementById('connectURL').innerHTML = `<a href="${connectURL}">connect</a>`;
let qrcode = await new QRCode(document.getElementById('qrcode'), {
text: connectURL,
width: 256,
height: 256,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
document.querySelector('#qrcode > img').classList.add('qrcode_image');
document.querySelector('#qrcode > canvas').classList.add('qrcode_image');
log(`username:${this.username} user:${userID} peer:${peerID}`);
await this.purgeEmptyUsers();
let websocket = new wsConnection(userID, peerID);
window.addEventListener('beforeunload', () => { websocket.disconnect(); });
this.initOffline(websocket);
// const client = new WebTorrent()
// // Sintel, a free, Creative Commons movie
// const torrentId = 'magnet:?xt=urn:btih:6091e199a8d9272a40dd9a25a621a5c355d6b0be&dn=WING+IT!+-+Blender+Open+Movie+1080p.mp4&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337';
// client.add(torrentId, function (torrent: any) {
// // Torrents can contain many files. Let's use the .mp4 file
// const file = torrent.files.find(function (file: any) {
// return file.name.endsWith('.mp4')
// })
// // Display the file by adding it to the DOM.
// // Supports video, audio, image files, and more!
// file.appendTo(document.getElementById('torrent-content'));
// })
}
// keep a map of posts to dom nodes.
// on re-render
// posts that are not in our list that we need at add
// posts that are in our list that we need to remove
// postsSet = new Set();
async render() {
if (this.isHeadless) {
console.log('Headless so skipping render...');
return;
}
this.timerStart();
let posts = [];
switch (this.router.route) {
case App.Route.HOME:
case App.Route.CONNECT: {
posts = await this.loadPosts(this.userID) ?? [];
break;
}
case App.Route.USER: {
posts = await this.loadPosts(this.router.userID) ?? [];
break;
}
case App.Route.POST: {
posts = await this.loadPosts(this.router.userID, this.router.postID) ?? [];
break;
}
default: {
console.log("Render: got a route I didn't understand. Rendering HOME:", this.router.route);
posts = await this.loadPosts(this.userID) ?? [];
break;
}
}
// let newPostsSet = new Set(posts.map(post=>post.post_id));
// let newPosts = (newPostsSet as any).difference(this.postsSet);
// // let removedPosts = (this.postsSet as any).difference(newPosts);
// let keepPosts = (this.postsSet as any).intersection(newPostsSet);
// let renderPosts = keepPosts.union(newPosts);
const fragment = document.createDocumentFragment();
let contentDiv = document.getElementById("content");
if (!contentDiv) {
throw new Error();
}
contentDiv.innerHTML = "";
// let count = 0;
for (let i = posts.length - 1; i >= 0; i--) {
let postData = posts[i];
// this.postsSet.add(postData);
let post = this.renderPost(postData);
if (post) {
fragment.appendChild(post);
// count++;
}
// if (count > 100) {
// break;
// }
}
if (!contentDiv) {
throw new Error("Couldn't get content div!");
}
contentDiv.appendChild(fragment);
let renderTime = this.timerDelta();
log(`render took: ${renderTime.toFixed(2)}ms`);
if (performance?.memory) {
log(`memory used: ${(performance.memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`);
}
}
async deletePost(userID, postID) {
deleteData(userID, postID);
this.render();
}
renderPost(post) {
if (!(post.hasOwnProperty("text"))) {
throw new Error("Post is malformed!");
}
let containerDiv = document.createElement("div");
let timestamp = `${post.post_timestamp.toLocaleTimeString()} · ${post.post_timestamp.toLocaleDateString()}`;
let deleteButton = document.createElement('button');
deleteButton.innerText = 'delete';
deleteButton.onclick = () => { this.deletePost(post.author_id, post.post_id); };
let editButton = document.createElement('button');
editButton.innerText = 'edit';
let shareButton = document.createElement('button');
shareButton.innerText = 'share';
shareButton.onclick = async () => {
let shareUrl = `https://${document.location.hostname}/user/${post.author_id}/post/${post.post_id}`;
await navigator.clipboard.writeText(shareUrl);
};
let ownPost = post.author_id === this.userID;
let postTemplate = `<div><hr>
<div>
<span class='header' title='${timestamp}'><img class="logo" src="/static/favicon.ico">@${post.author} -
<span style="color:rgb(128,128,128)">${post.post_timestamp.toLocaleDateString()}</span>
</span>
${ownPost ? `<span id="deleteButton"></span>` : ''}
${ownPost ? `<span id="editButton"></span>` : ''}
<span id="shareButton"></span>
</div>
<div>${marked.parse(post.text)}</div>
</div>`;
containerDiv.innerHTML = postTemplate;
if (ownPost) {
containerDiv.querySelector('#deleteButton')?.appendChild(deleteButton);
containerDiv.querySelector('#editButton')?.appendChild(editButton);
}
containerDiv.querySelector('#shareButton')?.appendChild(shareButton);
if (!("image_data" in post && post.image_data)) {
// containerDiv.appendChild(timestampDiv);
return containerDiv;
// return null;
}
let image = document.createElement("img");
image.title = `${(post.image_data.byteLength / 1024 / 1024).toFixed(2)}MBytes`;
// const blob = new Blob([post.image_data as ArrayBuffer], { type: 'image/png' });
const blob = new Blob([post.image_data]);
const url = URL.createObjectURL(blob);
image.onload = () => {
URL.revokeObjectURL(url);
};
image.src = url;
// image.src = image.src = "data:image/png;base64," + post.image;
image.className = "postImage";
image.onclick = () => { App.maximizeElement(image); };
containerDiv.appendChild(image);
// containerDiv.appendChild(timestampDiv);
return containerDiv;
}
static maximizeElement(element) {
element.style.transform = "scale(2.0)";
}
getRoute() {
app.router.userID = this.userID;
let path = document.location.pathname;
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>", path);
const regex = "(user/([a-zA-Z0-9\-]+)/?(post/([a-zA-Z0-9\-]+)?/?)?(media/([0-9]+)?)?)|(connect/([a-zA-Z0-9\-]+))";
const match = path.match(new RegExp(regex));
if (match) {
if (match[8]) { // Check for the connect route
this.router.userID = match[8];
this.router.route = App.Route.CONNECT;
}
else {
this.router.userID = match[2];
this.router.postID = match[4];
this.router.mediaID = match[6];
if (this.router.mediaID) {
this.router.route = App.Route.MEDIA;
}
else if (this.router.postID) {
this.router.route = App.Route.POST;
}
else {
this.router.route = App.Route.USER;
}
}
}
console.log(">>>>>>>>>>>>>", this.router, App.Route[this.router.route]);
// user = /user/<ID>
// post = /user/<ID>/post/<ID>
// media = /user/<ID>/post/<ID>/media/<index>
// group = /group/ID/post/<ID>
// hashtag = /hashtag/ -- maybe only hastags in groups
// home = /
}
}
(function (App) {
let Route;
(function (Route) {
Route[Route["USER"] = 0] = "USER";
Route[Route["POST"] = 1] = "POST";
Route[Route["MEDIA"] = 2] = "MEDIA";
Route[Route["GROUP"] = 3] = "GROUP";
Route[Route["HOME"] = 4] = "HOME";
Route[Route["CONNECT"] = 5] = "CONNECT";
})(Route = App.Route || (App.Route = {}));
;
})(App || (App = {}));
let app = new App();
window.addEventListener("load", app.main.bind(app));
//# sourceMappingURL=main.js.map

1
static/main.js.map Normal file

File diff suppressed because one or more lines are too long

20
static/peers.js Normal file
View File

@@ -0,0 +1,20 @@
var PeerMessageTypes;
(function (PeerMessageTypes) {
PeerMessageTypes[PeerMessageTypes["HELLO"] = 0] = "HELLO";
})(PeerMessageTypes || (PeerMessageTypes = {}));
export class Peer {
constructor() {
this.peer_id = '';
this.state = 'ready';
}
connect(peer_id) {
}
}
export class PeerManager {
constructor() {
this.peers = [];
}
connectPeer(peer_id) {
}
}
//# sourceMappingURL=peers.js.map

1
static/peers.js.map Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"peers.js","sourceRoot":"","sources":["../src/peers.ts"],"names":[],"mappings":"AACA,IAAK,gBAGJ;AAHD,WAAK,gBAAgB;IACjB,yDAAK,CAAA;AAET,CAAC,EAHI,gBAAgB,KAAhB,gBAAgB,QAGpB;AAED,MAAM,OAAO,IAAI;IAAjB;QACI,YAAO,GAAU,EAAE,CAAC;QAEpB,UAAK,GAAmD,OAAO,CAAC;IAIpE,CAAC;IAHG,OAAO,CAAC,OAAc;IAEtB,CAAC;CACJ;AAED,MAAM,OAAO,WAAW;IAAxB;QACI,UAAK,GAAU,EAAE,CAAC;IAOtB,CAAC;IALG,WAAW,CAAC,OAAc;IAE1B,CAAC;CAGJ"}

2
static/robots.txt Normal file
View File

@@ -0,0 +1,2 @@
User-agent: *
Disallow: /

117
static/sw.js Normal file
View File

@@ -0,0 +1,117 @@
"use strict";
const debugLog = false;
// Establish a cache name
const cacheName = "dandelion_cache_v1";
const contentToCache = [
'/static/index.html',
'/static/main.css',
'/static/main.js',
'/static/lib/marked.min.js',
'/static/lib/qrcode.min.js',
'/static/db.js',
'/static/favicon.ico'
];
self.addEventListener("install", (e) => {
e.waitUntil((async () => {
const cache = await caches.open(cacheName);
console.log("[Service Worker] Caching all: app shell and content", contentToCache);
try {
await cache.addAll(contentToCache);
}
catch (e) {
debugLog ? console.log(e) : null;
}
})());
});
async function staleWhileRevalidate(event) {
let cache = await caches.open(cacheName);
let response = await cache.match(event.request);
if (response) {
debugLog ? console.log('Service Worker: Cache hit', event.request.url) : null;
}
const fetchPromise = (async () => {
debugLog ? console.log('Service Worker: Fetching', event.request.url) : null;
let networkResponse = null;
try {
networkResponse = await fetch(event.request);
}
catch (e) {
debugLog ? console.log('Service Worker: Failed to fetch', e) : null;
return new Response('Network error occurred', {
status: 404,
statusText: 'Cache miss and fetch failed',
headers: { 'Content-Type': 'text/plain' }
});
}
debugLog ? console.log('Service Worker: Updating cache', event.request.url) : null;
try {
await cache.put(event.request, networkResponse.clone());
}
catch (e) {
debugLog ? console.log('Service Worker: failed to update cache', event.request.url, e) : null;
}
debugLog ? console.log('Service Worker: Returning networkResponse', event.request.url) : null;
return networkResponse;
})();
debugLog ? console.log('Service Worker: Returning return response || fetchPromise', event.request.url) : null;
return response || fetchPromise;
// if (networkResponse) {
// cache.put(event.request, networkResponse.clone())
// return networkResponse;
// }
// caches.open(cacheName)
// .then(function (cache) {
// return cache.match(event.request)
// .then(function (response) {
// var fetchPromise = fetch(event.request)
// .then(function (networkResponse) {
// cache.put(event.request, networkResponse.clone());
// return networkResponse;
// });
// return response || fetchPromise;
// });
// })
}
// async function responder(event: any) {
// debugLog ? console.log('Fetching', event.request.url) : null;
// let response = await fetch(event.request);
// if (!response) {
// debugLog ? console.log('Fetch failed, falling back to cache', event.request.url) : null;
// let cacheMatch = await caches.match(event.request);
// if (!cacheMatch) {
// // DUnno what to return here!
// }
// return cacheMatch;
// }
// if (response.status === 206) {
// debugLog ? console.log('Not caching partial content') : null;
// return response;
// }
// debugLog ? console.log('Fetch successful, updating cache', event.request.url) : null;
// const cache = await caches.open(cacheName);
// try {
// cache.put(event.request, response.clone()).catch((error) => debugLog ? console.log('failed to cache', event.request, error)) : null;
// } catch (e) {
// console.log('failed to cache', event.request)
// }
// return response;
// }
self.addEventListener('fetch', function (event) {
event.respondWith(staleWhileRevalidate(event));
// event.respondWith(responder(event));
});
addEventListener("message", async (e) => {
debugLog ? console.log(`Message received:`, e.data) : null;
switch (e.data.type) {
case "update_app":
const cache = await caches.open(cacheName);
debugLog ? console.log(`[Service Worker] Caching resources`) : null;
// cache.put("/main.js", new Response());
for (let item of contentToCache) {
cache.delete(item);
}
await cache.addAll(contentToCache);
break;
}
});
//# sourceMappingURL=sw.js.map

1
static/sw.js.map Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"sw.js","sourceRoot":"","sources":["../src/sw.ts"],"names":[],"mappings":";AAAA,MAAM,QAAQ,GAAG,KAAK,CAAC;AACvB,yBAAyB;AACzB,MAAM,SAAS,GAAG,oBAAoB,CAAC;AAEvC,MAAM,cAAc,GAAG;IACrB,oBAAoB;IACpB,kBAAkB;IAClB,iBAAiB;IACjB,2BAA2B;IAC3B,2BAA2B;IAC3B,eAAe;IACf,qBAAqB;CACtB,CAAC;AAEF,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,CAAM,EAAE,EAAE;IAC1C,CAAC,CAAC,SAAS,CACT,CAAC,KAAK,IAAI,EAAE;QACV,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC3C,OAAO,CAAC,GAAG,CACT,qDAAqD,EACrD,cAAc,CACf,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AACzB,sCAAsC;AACtC,QAAQ;AACR,yBAAyB;AACzB,MAAM;AAEN,mCAAmC;AACnC,oEAAoE;AACpE,uBAAuB;AACvB,MAAM;AAEN,0FAA0F;AAC1F,gDAAgD;AAChD,UAAU;AACV,2IAA2I;AAC3I,kBAAkB;AAClB,oDAAoD;AACpD,MAAM;AACN,qBAAqB;AACrB,IAAI;AAEJ,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,UAAU,KAAU;IACjD,KAAK,CAAC,WAAW,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/C,uCAAuC;AACzC,CAAC,CAAC,CAAC;AAEH,gBAAgB,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IACtC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAE3D,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,YAAY;YACf,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAC3C,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YACpE,yCAAyC;YAEzC,KAAK,IAAI,IAAI,IAAI,cAAc,EAAE,CAAC;gBAChC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACrB,CAAC;YAED,MAAM,KAAK,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;YACnC,MAAM;IACV,CAAC;AACH,CAAC,CAAC,CAAC"}

Binary file not shown.

BIN
static/test_data/wingit.mp4 Normal file

Binary file not shown.

BIN
static/test_data/yt-dlp Executable file

Binary file not shown.

BIN
static/virgil.woff2 Normal file

Binary file not shown.

96
static/webRTC.js Normal file
View File

@@ -0,0 +1,96 @@
"use strict";
class PeerManager {
connect(peerID) {
// Connect to the peer that has the peer id peerID
}
disconnect(peerID) {
}
}
class PeerConnection {
}
PeerConnection.config = {
iceServers: [
{ urls: "stun:stun.l.google.com" },
{ urls: "stun:stun1.l.google.com" },
{ urls: "stun:stun2.l.google.com" },
{ urls: "stun:stun3.l.google.com" },
{ urls: "stun:stun4.l.google.com" },
],
};
const config = {
iceServers: [{ urls: "stun:stun.mystunserver.tld" }],
};
let polite = true;
// const signaler = new SignalingChannel();
const signaler = {};
const pc = new RTCPeerConnection(config);
const constraints = { audio: true, video: true };
const selfVideo = document.querySelector("video.selfview");
const remoteVideo = document.querySelector("video.remoteview");
async function start() {
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
for (const track of stream.getTracks()) {
pc.addTrack(track, stream);
}
// selfVideo.srcObject = stream;
}
catch (err) {
console.error(err);
}
}
pc.ontrack = ({ track, streams }) => {
track.onunmute = () => {
// if (remoteVideo.srcObject) {
// return;
// }
// remoteVideo.srcObject = streams[0];
};
};
let makingOffer = false;
pc.onnegotiationneeded = async () => {
try {
makingOffer = true;
await pc.setLocalDescription();
signaler.send({ description: pc.localDescription });
}
catch (err) {
console.error(err);
}
finally {
makingOffer = false;
}
};
pc.onicecandidate = ({ candidate }) => signaler.send({ candidate });
let ignoreOffer = false;
signaler.onmessage = async ({ data: { description, candidate } }) => {
try {
if (description) {
const offerCollision = description.type === "offer" &&
(makingOffer || pc.signalingState !== "stable");
ignoreOffer = !polite && offerCollision;
if (ignoreOffer) {
return;
}
await pc.setRemoteDescription(description);
if (description.type === "offer") {
await pc.setLocalDescription();
signaler.send({ description: pc.localDescription });
}
}
else if (candidate) {
try {
await pc.addIceCandidate(candidate);
}
catch (err) {
if (!ignoreOffer) {
throw err;
}
}
}
}
catch (err) {
console.error(err);
}
};
//# sourceMappingURL=webRTC.js.map

1
static/webRTC.js.map Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"webRTC.js","sourceRoot":"","sources":["../src/webRTC.ts"],"names":[],"mappings":";A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