Skip to content

Instantly share code, notes, and snippets.

export function bigIntP(value: bigint, percentage: number, scalingFactor = 1000000000): bigint {
const scaledPercentage = BigInt(Math.round(percentage * 100 * scalingFactor))
const result = (value * scaledPercentage) / (BigInt(100) * BigInt(scalingFactor))
return result
}
@hos
hos / InjectRuntimePublicEnv.ts
Last active April 11, 2024 15:04
Workaround to use runtime variables in the next.js app (App Router), so you can change them after build.
import Script from 'next/script'
// Add this file to some Server Component, in my case I use this in `app/layout.tsx`
// Next.js, will inline all the process.env.SOMETHING variables into the code at build time.
// Which will force us to build the app for each environment we want to deploy to, with the
// only different being few environment variables. Instead, what we want is to get the environment
// dynamically from the runtime, so if we change them in the env and run the app again, it must
// use the new environment variables. To do this, we will inject the environment variables into
// the window object, so we can access them in the runtime.
We can make this file beautiful and searchable if this error is corrected: It looks like row 8 should actually have 9 columns, instead of 6. in line 7.
city 1, city 2 distance, city 2, city 3 distance, city 3, city 4 distance, city 4, city 5 distance, city5
Bronx , 23 , Brooklyn , 11 , Manhattan , 18 , New York , 13 , Queens
Bronx , 23 , Brooklyn , 11 , Manhattan , 13 , Queens , 18 , New York
Bronx , 23 , Brooklyn , 18 , New York , 11 , Manhattan , 13 , Queens
Bronx , 23 , Brooklyn , 18 , New York , 13 , Queens , 11 , Manhattan
Bronx , 23 , Brooklyn , 13 , Queens , 11 , Manhattan , 18 , New York
Bronx , 23 , Brooklyn , 13 , Queens , 18 , New York , 11 , Manhattan
Bronx , 11 , Manhattan , 23 , Brooklyn , 1
@hos
hos / supabase-client.ts
Last active December 31, 2023 17:15
Validate telegram mini app init data in PostgreSQL, can be useful when you want to use supabase client in TMA, pass `initDataRaw` to headers, then in SQL queries get init data (username, user id...)
const supabase = useMemo(() => {
return createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_KEY!,
{
global: {
headers: {
telegram_init_data: initDataRaw || "",
},
},
--[[
=====================================================================
==================== READ THIS BEFORE CONTINUING ====================
=====================================================================
Kickstart.nvim is *not* a distribution.
Kickstart.nvim is a template for your own configuration.
The goal is that you can read every line of code, top-to-bottom, and understand
import { jsonParse } from "@dataplan/json";
import { access, context, lambda, listen } from "grafast";
import { gql, makeExtendSchemaPlugin } from "graphile-utils";
const PackagePlugin = makeExtendSchemaPlugin((build) => {
const { sql } = build;
if (!sql) {
throw new Error("sql not found");
}
import cors from "cors";
import express, { Express, NextFunction } from "express";
import { IncomingMessage, Server, ServerResponse } from "http";
import inspector from "inspector";
import { Duplex } from "stream";
import { isDev, isLocal } from "./config.js";
import * as middleware from "./middleware/index.js";
import { firebaseAuthMiddleware } from "./middleware/installFirebaseAuth.js";
import { makeShutdownActions, ShutdownAction } from "./shutdownActions.js";
import { PgSelectSingleStep } from "@dataplan/pg";
import { Episode } from "@mythrillfiction/types";
import { context, lambda } from "grafast";
import { gql, makeExtendSchemaPlugin, makeWrapPlansPlugin, PlanWrapperFn } from "graphile-utils";
import { WorkerUtils } from "graphile-worker";
import _ from "lodash";
import * as yup from "yup";
import { SCHEMA_NAME } from "../config.js";
import { adminDb } from "../firebase.js";
import { jsonParse } from "@dataplan/json";
import { access, context, lambda, listen } from "grafast";
import { gql, makeExtendSchemaPlugin } from "graphile-utils";
import { SCHEMA_NAME } from "../config.js";
/* The JSON object that `tg__graphql_subscription()` delivers via NOTIFY */
interface TgGraphQLSubscriptionPayload {
event: string;
subject: string | null;
@hos
hos / firestore-new-id.sql
Created February 11, 2023 05:48
Firestore's newId function implemented in PostgreSQL. Converted using ChatGPT.
create or replace function public.gen_id()
returns text as $$
declare
chars text := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
max_multiple integer := floor(256 / char_length(chars)) * char_length(chars);
i integer;
auto_id text := '';
target_length integer := 20;
bytes bytea;
begin