Skip to content

Instantly share code, notes, and snippets.

@garth
garth / readme.md
Last active January 18, 2024 21:58
A Yjs provider for socketsupply

You can use it the same as you do with other yjs providers, but you have to pass it a socket which can be created with someting like this:

// set the peer id
const peerId = window.localStorage.getItem('peerId') ?? (await Encryption.createId())
window.localStorage.setItem('peerId', peerId)

// set the signing keys
const keySeed = window.localStorage.getItem('keySeed') ?? createId()
@itsjavi
itsjavi / components__ServiceWorkers.tsx
Last active March 11, 2024 15:02
Simple Service Worker for Next.js static assets
'use client'
import { useEffect } from 'react'
export function ServiceWorkers(): JSX.Element {
useEffect(() => {
if (typeof window === 'undefined') {
return
}
if ('serviceWorker' in navigator) {
import { signal, effect } from "@preact/signals-core";
interface CustomStorage {
getItem(key: string): void;
setItem(key: string, value: string | null): void;
}
/**
* A version of signal() that persists and recalls its value in localStorage.
*
@nichoth
nichoth / index.html
Last active August 20, 2023 20:36 — forked from dsheiko/index.html
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
<!DOCTYPE html>
<html>
<head>
<title>Service-worker demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
if ( "serviceWorker" in navigator ) {
@heapwolf
heapwolf / evented-p2p.js
Created April 15, 2023 20:20
real time streaming p2p demo with socket runtime
import { Peer } from 'socket:peer'
import process from 'socket:process'
import Buffer from 'socket:buffer'
import fs from 'socket:fs'
window.onload = async () => {
const clusterId = '14ecd42...' // truncated, make your own clusterId
const publicKeyHex = 'c43c1ddd...' // truncated, make your own hex encoded key
const privateKeyHex = '46adc2f8e9077c72...' // truncated, make your own hex encoded key
import { useSignal, signal, effect } from '@preact/signals';
import { useLayoutEffect, useMemo, useRef } from 'preact/hooks';
/** @template T @typedef {T extends (infer U)[] ? U : never} Items */
/** @param {{ v, k?, f }} props */
const Item = ({ v, k, f }) => f(v, k);
/**
* Like signal.value.map(fn), but doesn't re-render.
@HDegroote
HDegroote / replicate-corestores.js
Created July 25, 2022 18:24
Replicate corestores example
// Tested on Hyperswarm V4, corestore V6, Hypercore V10
// Server
const Hyperswarm = require('hyperswarm')
const Corestore = require("corestore")
const ram = require("random-access-memory")
async function runServer(){
const corestore = new Corestore(ram)

How to setup a practically free CDN using Backblaze B2 and Cloudflare

⚠️ Note 2023-01-21
Some things have changed since I originally wrote this in 2016. I have updated a few minor details, and the advice is still broadly the same, but there are some new Cloudflare features you can (and should) take advantage of. In particular, pay attention to Trevor Stevens' comment here from 22 January 2022, and Matt Stenson's useful caching advice. In addition, Backblaze, with whom Cloudflare are a Bandwidth Alliance partner, have published their own guide detailing how to use Cloudflare's Web Workers to cache content from B2 private buckets. That is worth reading,

@mohanpedala
mohanpedala / bash_strict_mode.md
Last active May 23, 2024 21:36
set -e, -u, -o, -x pipefail explanation
@dsheiko
dsheiko / index.html
Last active October 27, 2023 19:59
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
<!DOCTYPE html>
<html>
<head>
<title>Service-worker demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
if ( "serviceWorker" in navigator ) {