Skip to content

Instantly share code, notes, and snippets.

View shuhei's full-sized avatar
🐶
This is fine

Shuhei Kagawa shuhei

🐶
This is fine
View GitHub Profile
@shuhei
shuhei / axios.md
Last active August 4, 2021 08:19
axios' behaviors when the server hangs up or destroys the connection after sending response headers
@shuhei
shuhei / replace-text.js
Last active November 21, 2020 11:23
Replacing text in a document
// Recursive
function replaceText(node, from, to) {
switch (node.nodeType) {
case Node.TEXT_NODE:
node.textContent = node.textContent.replace(from, to);
break;
case Node.ELEMENT_NODE:
for (const childNode of node.childNodes) {
replaceText(childNode, from, to);
}
@shuhei
shuhei / Main.java
Created February 23, 2020 21:53
Java int overflow
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MAX_VALUE + 1); // -2147483648
System.out.println(-Integer.MIN_VALUE); // -2147483648
System.out.println(-(Integer.MIN_VALUE + 1)); // 2147483647
}
}
@shuhei
shuhei / README.md
Last active February 12, 2020 22:41
Firefox http2 beacon bug

Create a key pair.

openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
  -keyout localhost-privkey.pem -out localhost-cert.pem

Run the server.

@shuhei
shuhei / convert-textile.js
Created January 25, 2020 23:04
Convert textile files into markdown files
const { promises: fs } = require("fs");
const textile = require("textile-js");
const prettier = require("prettier");
const path = require("path");
async function main() {
const postsDir = path.resolve("source", "_posts");
const files = (await fs.readdir(postsDir)).filter(file =>
file.endsWith(".textile")
);
@shuhei
shuhei / monty-hall.js
Last active August 25, 2019 21:08
Monty Hall Problem
const doors = [0, 1, 2];
const tries = 100000;
let pickHasCar = 0;
let theOtherHasCar = 0;
for (let i = 0; i < tries; i++) {
const car = doors[random(3)];
const pick = doors[random(3)];
// Open a door that doesn't have the car.
@shuhei
shuhei / 1-basic.js
Last active August 8, 2019 22:30
An idea for testing custom React hooks
const React = require("react");
const { mount } = require("enzyme");
// A custom hook to test
function useCounter(initial) {
const [count, setCount] = React.useState(initial);
const increment = React.useCallback(() => setCount(c => c + 1), []);
const reset = React.useCallback(() => setCount(initial), [initial]);
return { count, increment, reset };
}
@shuhei
shuhei / README.md
Created July 3, 2019 19:28
gzip chunkSize and readable/writableHighWatermark

Not much difference...

default: chunkSize 16KB, hwm: 16KB x 20.80 ops/sec ±1.65% (51 runs sampled)
chunkSize 128KB, hwm: 16KB x 22.86 ops/sec ±1.07% (55 runs sampled)
chunkSize 128KB, hwm: 128KB x 21.54 ops/sec ±8.46% (52 runs sampled)
chunkSize 16KB, hwm: 128KB x 21.48 ops/sec ±1.87% (52 runs sampled)
Fastest is [ 'chunkSize 128KB, hwm: 16KB', 'chunkSize 128KB, hwm: 128KB' ]
@shuhei
shuhei / README.md
Last active July 3, 2019 19:27
jobqueue in JavaScript

jobqueue

This is a somewhat evil restaurant. When it has available seats, customers can have seats immediately and stay as long as they want. When the restaurant is full, customers need to wait on a queue. The strange thing is that new customers are led to the head of the queue. If you are the first customer who starts a queue, be prepared to wait for a long time because new customers join in front of you. And the waiter will tell you to leave the queue because you waited too long (timeout) or a new customer is joining the queue but the queue is too long (stack full)!

Implementations

@shuhei
shuhei / README.md
Last active May 14, 2019 21:03
Node.js server.keepAliveTimeout