Skip to content

Instantly share code, notes, and snippets.

@bellbind
bellbind / 01-object-readable.js
Last active Nov 1, 2022
[ES2017][WHATWG-Streams] Basic examples for WHATWG Streams API (on nodejs)
View 01-object-readable.js
// npm i web-streams-polyfill
const streams = require("web-streams-polyfill");
const {ReadableStream, WritableStream, TransformStream} = streams;
// (default) object stream
const rs1 = new ReadableStream({
async start(controller) {
// called by constructor
console.log("[start]");
controller.enqueue("a");
@bellbind
bellbind / main.js
Created Jun 29, 2018
[nodejs][worker_threads] async req/res to Worker with MessageChannel
View main.js
// node.js worker_threads module example: main thread side
//$ node --exprimental-worker main.js
const path = require("path");
const {Worker, MessageChannel} = require("worker_threads");
//NOTE: worker code accepts only absolute file path
// not URLs includes "data:" URL
const w = new Worker(path.resolve(__dirname, "./worker.js"));
(async () => {
console.log(await send(w, "Hello"));
@bellbind
bellbind / index.html
Last active Oct 11, 2022
[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 Oct 5, 2022
[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 / cpuinfo.c
Created Oct 29, 2015
[c][intel][clang] get cpu info from CPUID intrinsic in clang/gcc
View cpuinfo.c
// intel CPUID opcode
// see: https://en.wikipedia.org/wiki/CPUID
// clang -Wall -Wextra -std=c11 cpuinfo.c -o cpuinfo
#include <stdint.h>
#include <stdio.h>
#include <cpuid.h> //macro __cpuid(eaxin, eaxout, ebx, ecx, edx)
int main() {
{
@bellbind
bellbind / fragment.glsl
Last active Sep 19, 2022
[webgl2]example for webgl2 (with glsl3)
View fragment.glsl
#version 300 es
precision highp float;
//invariant gl_FragCoord;
uniform Screen {
vec2 wh;
} screen;
uniform Timer {
int count;
@bellbind
bellbind / main.js
Last active Sep 13, 2022
[electron] Tray launcher example
View main.js
"use strict";
// [run the app]
// $ npm install electron
// $ ./node_modules/.bin/electron .
const {app, nativeImage, Tray, Menu, BrowserWindow} = require("electron");
let top = {}; // prevent gc to keep windows
@bellbind
bellbind / movconcat-fps60.js
Last active Sep 9, 2022
[deno]10M bps video files concation with x4 speed and x2 volume with ffmpeg command
View movconcat-fps60.js
#!/usr/bin/env -S deno run --allow-read --allow-run
// usage: deno run --allow-read --allow-run movconcat.js /Volumes/UNTITLED/Normal/F/ result.mp4
import * as path from "https://deno.land/std/path/mod.ts";
import * as flags from "https://deno.land/std/flags/mod.ts";
const parsed = flags.parse(Deno.args);
const sources = parsed._.at(-2);
const output = parsed._.at(-1);
const inputs = [];
for await (const {name} of Deno.readDir(sources)) inputs.push(name);
@bellbind
bellbind / async-timeout-interval.js
Created Sep 5, 2022
[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 / main.js
Last active Aug 21, 2022
[nodejs][worker_threads][es2018] req/res to Worker with for-await loop of ES2018
View main.js
// node.js worker_threads module example with for-await loop: main thread side
//$ node --exprimental-worker main.js
const path = require("path");
const {Worker, MessageChannel} = require("worker_threads");
// async queue as "asyncIterator" in ES2018
class Queue {
constructor() {
this.polls = [];
this.gets = [];