Skip to content

Instantly share code, notes, and snippets.

@nichoth
nichoth / badges.md
Last active March 15, 2024 18:05
badges.md

Socket Badge tests npm version npm version encode decode types module license dependencies

@nichoth
nichoth / components__ServiceWorkers.tsx
Created October 27, 2023 20:01 — forked from itsjavi/components__ServiceWorkers.tsx
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 { 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.
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 ) {
@nichoth
nichoth / errors.js
Last active June 16, 2023 03:13
Get an error from http request
import { HTTPError } from 'ky'
async function getErrorBody (error) {
if (error instanceof HTTPError) return (await error.response.text())
return String(error)
}
function getErrorMessage (error) {
if (error instanceof Error) return error.message
return String(error)
@nichoth
nichoth / evented-p2p.js
Created May 16, 2023 01:15 — forked from heapwolf/evented-p2p.js
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
@nichoth
nichoth / sw.js
Created May 8, 2023 02:25 — forked from ukmadlz/sw.js
Cloudinary PWA React Advanced Service Worker
(function () {
"use strict";
var cacheNameStatic = 'cloudinary-pwa-react-v2';
var currentCacheNames = [ cacheNameStatic ];
var cachedUrls = [
// 3rd party CDN
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css',
@nichoth
nichoth / README.md
Created February 15, 2023 18:09 — forked from saulshanabrook/README.md
Saving Web Crypto Keys using indexedDB

This is a working example on how to store CryptoKeys locally in your browser. We are able to save the objects, without serializing them. This means we can keep them not exportable (which might be more secure?? not sure what attack vectors this prevents).

To try out this example, first make sure you are in a browser that has support for async...await and indexedDB (latest chrome canary with chrome://flags "Enable Experimental Javascript" works). Load some page and copy and paste this code into the console. Then call encryptDataSaveKey(). This will create a private/public key pair and encrypted some random data with the private key. Then save both of them. Now reload the page, copy in the code, and run loadKeyDecryptData(). It will load the keys and encrypted data and decrypt it. You should see the same data logged both times.

const express = require('express');
const { html, Component } = require('htm/preact');
const renderToString = require('preact-render-to-string');
class App extends Component {
render(props) {
return html`
<div class="app">
<h1>This is an app</h1>
<p>Current server time: ${new Date + ''}</p>