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 / api.js
Last active June 10, 2018 23:12
Worker Pool Prototype
const url = require('url');
const https = require('https');
const agent = new https.Agent({
keepAlive: true
});
function getAPI(uri) {
return new Promise((resolve, reject) => {
const { protocol, hostname, path } = url.parse(uri);
@shuhei
shuhei / README.md
Last active June 10, 2018 18:48
Execution of Promise Chain in Node.js

Execution of Promise Chain in Node.js

After reading The Node.js Event Loop, Timers, and process.nextTick()...

Q. I write a lot of promise chains in applications. How is a promise chain executed? Is it performant?

The experiment shows:

  • Chained promises are eagerly executed before proceeding to the next phase.
  • Promise chaining is a bit slower than synchronous execution, but not too much.
@shuhei
shuhei / image-onload.jsx
Last active January 16, 2024 14:13
img.onload and unit test in React
import * as React from 'react';
import { mount } from 'enzyme';
import sinon from 'sinon';
function isImageLoaded(img: HTMLImageElement) {
return img.complete && img.naturalHeight > 0;
}
type Props = {
src: string,
@shuhei
shuhei / Main.elm
Created December 31, 2017 07:32
Form Validation
module App exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Validate exposing (..)
-- FIELD
@shuhei
shuhei / README.md
Last active October 22, 2021 16:34
Node.js HTTP timeout

Node.js HTTP timeout

Findings

  • http.request({ timeout }) is not working for response timeout.
    • Agent timeout supercedes the option
  • Doesn't leak because request.on('timeout') instead of socket.on('timeout'). It's easier because request is always thrown away.
    • Then why header leaks?
      • Print socket's listeners('timeout').length
  • Try dropRequestAfter together
@shuhei
shuhei / request-timeout.test.js
Last active November 23, 2017 23:15
Tests for request timeout of perron
'use strict';
const assert = require('assert');
const http = require('http');
const Agent = require('agentkeepalive');
const request = require('../lib/request');
describe('request timeout', () => {
it('should not leak timeout event handler', (done) => {
const server = http.createServer((req, res) => {
@shuhei
shuhei / decoder.js
Last active September 4, 2017 14:53
Sketch of JSON Decoder in JavaScript
declare opaque type Decoder<T>;
declare var num: Decoder<number>;
declare var str: Decoder<string>;
declare var bool: Decoder<boolean>;
declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
declare function field<T>(key: string, decoder: Decoder<T>): Decoder<T>;
declare function nullable<T>(decoder: Decoder<T>): Decoder<?T>;
declare function maybe<T>(decoder: Decoder<T>): Decoder<?T>;
declare function at<T>(keys: string[], decoder: Decoder<T>): Decoder<T>;
@shuhei
shuhei / jobs.go
Last active August 19, 2017 09:14
Process only one job for a same ID at a moment
package main
import (
"fmt"
"time"
)
func main() {
input := make(chan int)
go startMaster(input)
@shuhei
shuhei / request.js
Last active May 14, 2017 17:52
request() timeout
const net = require('net');
const dns = require('dns');
const request = require('request');
const start = Date.now();
function log() {
const time = (Date.now() - start).toString();
console.log.apply(console, [time].concat(Array.from(arguments)));
}
@shuhei
shuhei / http-request.js
Last active May 14, 2017 17:24
Node.js http.request timeout
const net = require('net');
const dns = require('dns');
const http = require('http');
const start = Date.now();
function log() {
const time = (Date.now() - start).toString();
console.log.apply(console, [time].concat(Array.from(arguments)));
}