Skip to content

Instantly share code, notes, and snippets.

@bellbind
bellbind / closable-stream.js
Last active December 11, 2022 14:46
[js-ipfs/libp2p] dial to read events from infinite sending from handler, then close read
View closable-stream.js
// wrapping libp2p stream (mplex/stream)
// - stream.source: AsyncIterable<Uint8Array>
// - stream.sink: (Iterable<Uint8Array> | AsyncIterable<Uint8Array>) => Promise<undefined>
// - stream.close, stream.closeRead, stream.closeWrite, stream.abort, stream.reset
const newQueue = () => {
const [gets, polls] = [[], []];
const next = () => new Promise(
get => polls.length > 0 ? polls.shift()(get) : gets.push(get));
const poll = () => new Promise(
poll => gets.length > 0 ? poll(gets.shift()) : polls.push(poll));
@bellbind
bellbind / cache-method.js
Last active November 19, 2022 14:46
[Web Cache API][browser][deno] Web Cache API basic examples
View cache-method.js
await caches.delete("tmp");
const cache = await caches.open("tmp");
const req1 = new Request("http://example.com/");
const req2 = new Request("http://example.com/", {method: "head"});
const req3 = new Request("http://example.com/", {method: "post"});
const res1 = new Response("hello en-US", {headers: {"Content-Type": "text/plain;charset=utf-8"}});
const res2 = new Response("", {headers: {}});
await cache.put(req1, res1); // cache "http://example.com/ with res1
@bellbind
bellbind / example-relay-http2p.mjs
Created November 17, 2022 13:51
[ipfs][nodejs] HTTP like messaging for PeerID via libp2p in IPFS node
View example-relay-http2p.mjs
#!/usr/bin/env node
import * as fs from "node:fs";
// IPFS
import * as IPFS from "ipfs-core";
// WebRTCStar
import wrtc from "@koush/wrtc";
import {sigServer} from "@libp2p/webrtc-star-signalling-server";
import {webRTCStar} from "@libp2p/webrtc-star";
// pubsub peerDiscovery
//import {floodsub} from "@libp2p/floodsub";
@bellbind
bellbind / index.html
Last active October 11, 2022 00:28
[CSS]Tree view with :has and :not selector
View index.html
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="./tree.css" />
</head>
<body>
<label class="css-tree"><input type="checkbox" checked="checked">Tree (root)</label>
<ul>
<li>
<label class="css-tree"><input type="checkbox">Tree 1</label>
@bellbind
bellbind / index.html
Last active October 5, 2022 22:29
[HTML][JavaScript] Origin Private File Systrem Viewer
View index.html
<!doctype html>
<html>
<head>
<title>Origin Private File System Viewer</title>
<script type="module">
// see https://fs.spec.whatwg.org/
const showDir = (dir, check, ul) => {
check.addEventListener("change", ev => {
if (check.checked) (async () => {
@bellbind
bellbind / async-timeout-interval.js
Created September 5, 2022 02:16
[ecmascript] timeout promise and inverval async iterator
View async-timeout-interval.js
export const timeout = (msec, ...args) => new Promise(f => setTimeout(f, msec, args));
export const interval = (msec, ...args) => ({
[Symbol.asyncIterator]() {
const rs = [];
const id = setInterval(() => rs.shift()?.call(undefined, {value: args, done: false}), msec);
return {
next() {
return new Promise(f => rs.push(f));
},
throw() {
@bellbind
bellbind / clock.html
Last active August 17, 2022 00:02
[SVG][JavaScript] SVG Analog Clock
View clock.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="user-scalable=no" />
<link rel="icon" href="././clock.svg" />
<link rel="manifest" href="./manifest.json" />
<title>SVG Clock</title>
<style>
html {height: 100%;}
@bellbind
bellbind / index.html
Created July 29, 2022 14:13
[JavaScript][HTML] run es modules in iframe context
View index.html
<!doctype html>
<html>
<head>
<script type="module">
document.querySelector("#run").addEventListener("click", async ev => {
const modUrl = new URL("./module.js", location.href).href;
//console.log(modUrl);
const iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.append(iframe);
@bellbind
bellbind / index.html
Last active June 8, 2022 13:07
[css] shadow button example
View index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
/* shadow button */
button {
border-style: none;
border-radius: 0.5em;
color: inherit;
@bellbind
bellbind / server.js
Last active May 27, 2022 15:28
[browser][deno] Example registration page and its server of API: navigator.registerProtocolHandler()
View server.js
#!/usr/bin/env -S deno run --allow-net
// Example registration page and its server of API: navigator.registerProtocolHandler()
import {serve} from "https://deno.land/std/http/server.ts";
// A pattern of protocol scheme name is limited with prefix "web+" as web+lowerletters ,
// or whitelisted names in https://developer.mozilla.org/en-US/docs/Web/API/Navigator/registerProtocolHandler
const protocol = "web+local";
// navigator.registerProtocolHandler() should be called in user inputs (e.g. button click)
// - forwarded url must be same origin of the location.href