Skip to content

Instantly share code, notes, and snippets.

View acomagu's full-sized avatar

Yuki Ito acomagu

  • The Designium, Inc.
  • Musashimurayama-shi, Tokyo, JAPAN
  • X @acomagu_u
View GitHub Profile
@acomagu
acomagu / chan.ts
Created February 4, 2025 09:35
Simple channel with async generator for TypeScript(without buffers)
function chan<T>() {
let p = Promise.withResolvers<T>();
async function* gen() {
while (true) {
let v;
try {
v = await p.promise;
} catch {
return;
@acomagu
acomagu / setup-squid-on-ec2.sh
Created November 18, 2024 11:50
EC2(AmazonLinux) Userdata to setup Squid server
sudo yum update -y
sudo yum install -y squid
sudo cat <<EOL > /etc/squid/squid.conf
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
auth_param basic children 5
acl HTTP_ports port 443
acl HTTP_ports port 80
@acomagu
acomagu / cred.ts
Last active December 5, 2023 09:06
The Demo of Stripe Embedded Checkout
import * as toml from 'https://deno.land/[email protected]/toml/mod.ts';
async function getStripeConfig() {
const { stdout } = await new Deno.Command('stripe', {
args: ['config', '--list'],
stderr: 'inherit',
}).output();
return toml.parse(new TextDecoder().decode(stdout)) as any;
}
@acomagu
acomagu / memoize.ts
Created October 28, 2023 09:22
TypeScript generic recursive memoization
function memoize<Args extends unknown[], R>(f: (...args: Args) => R): (...args: Args) => R {
const cache = new Map();
let retCache: { value: R } | undefined;
return (...args: Args): R => {
if (args.length > 0) {
if (!cache.has(args[0])) {
cache.set(args[0], memoize((...restArgs) => (f as any)(args[0], ...restArgs)));
}
return cache.get(args[0])(...args.slice(1));
}
@acomagu
acomagu / gist:ebe883e861a36e3e95b0180e6e7c3b19
Created September 30, 2023 08:51
Get Schema SDL from Remote GraphQL API
import graphql from 'graphql';
const resp = await fetch(url, { method: 'POST', body: JSON.stringify({ query: graphql.getIntrospectionQuery() }), headers: { 'content-type': 'application/json' } });
const introspectionResult = await resp.json();
const schema = graphql.buildClientSchema(introspectionResult.data);
const sdl = graphql.printSchema(schema);
import { list } from '@keystone-6/core';
import { graphql } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields';
import { select } from '@keystone-6/core/fields';
import { BaseListTypeInfo, CommonFieldConfig, FieldTypeFunc, fieldType } from '@keystone-6/core/types';
interface TranslatedFieldConfig<
ListTypeInfo extends BaseListTypeInfo,
LanguageTag extends string,
@acomagu
acomagu / index.js
Created February 16, 2023 10:34
JavaScript: Log all HTTP request/response and patch console.log to send log to external API
const fetch = require('node-fetch');
const util = require('util');
util.inspect.defaultOptions.depth = null;
console.log = (...args) => {
return fetch('https://webhook.site/XXXXX', {
method: 'POST',
body: util.format(...args),
});
};
@acomagu
acomagu / auth-directive.ts
Created February 5, 2022 11:40
@auth(role: ...) GraphQL directive implementation in TypeScript.
import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils';
import { defaultFieldResolver, GraphQLError, GraphQLFieldResolver, GraphQLSchema } from 'graphql';
function wrapField<Field extends { resolve?: GraphQLFieldResolver<any, any> }>(field: Field, authDirective: Record<string, any>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = (src, args, ctx, info) => {
if (ctx.role !== authDirective.role) throw new GraphQLError(`${authDirective.role} role is needed but your role is ${ctx.role}.`);
return resolve(src, args, ctx, info);
};
@acomagu
acomagu / encrypt-with-github-key.sh
Last active March 31, 2021 09:40
Script to encrypt/decrypt short message with the SSH public key on GitHub.
echo abc | openssl rsautl -encrypt -pubin -inkey <(ssh-keygen -f <(curl https://github.com/acomagu.keys) -e -m PKCS8) | openssl rsautl -decrypt -inkey ~/.ssh/id_rsa
<!DOCTYPE html>
<body>
<form method="POST" action="https://raraya:Qm3VMV7pgnAgNch2WzqWYN5kDVNLGmiNCk6nihwc8U4KwEKSmd@shop-stage.raraya.co.jp/external/ordermadeparts.php">
<input type="hidden" name="apiKey" value="ZWJcQYnhM9YNS4OiVcphyDmEtNGr8rKv4qgEUKdDxGZ1WEzBnc" />
<input type="hidden" name="a" value="1" />
<input type="hidden" name="b" value="1" />
<input type="hidden" name="c" value="1" />
<input type="hidden" name="d" value="1" />
<input type="hidden" name="e" value="1" />
<input type="hidden" name="f" value="1" />