Skip to content

Instantly share code, notes, and snippets.

@piglovesyou
piglovesyou / a.ts
Last active July 3, 2022 10:23
auth.getMobileSession fails
import fetch from 'node-fetch'
import crypto from 'crypto'
import {strictEqual, deepStrictEqual} from 'assert'
function md5(string: string) {
return crypto.createHash('md5').update(string, 'utf8').digest("hex");
}
export async function main() {
const url = new URL(`https://ws.audioscrobbler.com/2.0/`)
@piglovesyou
piglovesyou / a.js
Created May 6, 2022 06:58
JavaScript speed comparison: [...array] vs. array.concat()
const a = Array.from(Array(1000_000)).map((_, i) => i);
const b = Array.from(Array(1000_000)).map((_, i) => i);
const times = 1000;
console.time("spread");
for (let i = 0; i < times; i++) {
const c = [...a, ...b];
c.length;
}
@piglovesyou
piglovesyou / .husky_pre-commit
Created May 24, 2021 07:15
Husky in Monorepo, but want to run only in a subdirectory
#!/bin/sh
if [ $GIT_PREFIX != "frontend*" ]; then
exit 0
fi
. "$(dirname "$0")/_/husky.sh"
cd frontend || exit
yarn lint-staged
@piglovesyou
piglovesyou / gensync.d.ts
Last active May 11, 2021 02:27
gensync TypeScript type
declare module 'gensync' {
interface GensyncFn<Ps = any, R = any> {
(...args: Ps): Generator<unknown, R>;
sync: (...args: Ps) => R;
async: (...args: Ps) => Promise<R>;
}
function gensync<F extends (...args: any) => any>(obj: {
sync: F;
errback?: any;
async?: any;
@piglovesyou
piglovesyou / my.mjs
Created March 6, 2021 01:44
Speed comparison of Node 15 file access with factors size and methods (sync, async and p-map concurrency)
import { readFileSync, writeFileSync, unlinkSync, promises } from "fs";
import pMap from "p-map";
const { readFile, writeFile, unlink } = promises;
const testCount = 10_000;
main().catch(err => (console.error(err), process.exit(1)));
async function main() {
@piglovesyou
piglovesyou / db.ts
Created March 4, 2021 09:30
Node MySQL initialization snippet
import mysql, { Connection } from "mysql";
import { DB_HOST, DB_PASSWORD } from "./env";
const databaseName = "xxx";
const databaseUser = "xxx";
const databasePort = 1234;
export const getConn: () => Connection = (() => {
let conn: Connection;
process.on("exit", () => {
@piglovesyou
piglovesyou / test.js
Created February 26, 2021 11:20
graphql-let faster "readHash" test
import { createReadStream, existsSync, readFileSync, promises } from "fs";
import assert from 'assert'
const {readFile} = promises;
const filepath =
'__tests__/.__fixtures/hmr/__generated__/src/viewer.graphql.tsx';
const len = 1000;
const leadingStringOfGeneratedContent = '/* ';
const hexHashLength = 40;
@piglovesyou
piglovesyou / a.md
Last active July 25, 2020 16:36
test

Q. Bold text in code block in Markdown?

A. No in ```codeblock```

# <b>Required.</b>
schema: lib/type-defs.graphqls
@piglovesyou
piglovesyou / .graphql-let.yml
Last active July 25, 2020 10:59
graphql-let config design
# Allow multiple schema files
schema:
- 'src/schema/**/*.graphqls'
- '!src/schema/schema.graphqls'
# `schemaEntrypoint`: Generates bound GraphQL schema, from which users will import:
# - typeDefs for GraphQL server
# - schema types for resolver implementation
# Required if:
# - `schema` is local file path(s)
@piglovesyou
piglovesyou / README.md
Created April 4, 2020 10:09
How examples/api-routes-apollo-server-and-client/apollo/client.js works

How examples/api-routes-apollo-server-and-client/apollo/client.js works