Skip to content

Instantly share code, notes, and snippets.

@timostamm
timostamm / protoc-gen-reg.ts
Created November 23, 2023 15:38
protoc-gen-reg - generate a reg.ts file for each proto file, with a registry for all types in the file
#!/usr/bin/env -S npx tsx
// See here how to run this plugin: https://github.com/bufbuild/protobuf-es/tree/main/packages/protoplugin-example
import { createEcmaScriptPlugin, runNodeJs } from "@bufbuild/protoplugin";
import type { Schema } from "@bufbuild/protoplugin/ecmascript";
import type {
DescEnum,
DescExtension,
DescFile,
@timostamm
timostamm / check.mjs
Created November 8, 2023 14:56
Analyze "module" exports of high-impact npm packages
import {npmHighImpact} from 'npm-high-impact'
import {readFileSync, existsSync, writeFileSync} from "node:fs";
void main();
async function main() {
const pkgs = await getPackages();
const exports = pkgs.filter(pkg =>
hasExports(pkg, (obj => {
return true;
@timostamm
timostamm / jest.config.js
Last active May 30, 2023 13:55
createRouterTransport with Jest's jsdom raises error
/** @type {import('jest').Config} */
module.exports = {
transform: {
"^.+\\.(ts|tsx)$": ["ts-jest", {}],
},
testEnvironment: "jsdom",
globals: {
TextEncoder: TextEncoder,
TextDecoder: TextDecoder,
Uint8Array: Uint8Array,
@timostamm
timostamm / typeswrong.ts
Created April 18, 2023 11:24
analyze a package in the local file system with @arethetypeswrong/core
import type {Analysis, FS, Host} from "@arethetypeswrong/core";
import {checkPackage, getProblems, summarizeProblems} from "@arethetypeswrong/core";
import {readdirSync, readFileSync} from "node:fs";
import path from "node:path";
import {z} from "zod";
export async function typeswrong(projectPath: string) {
const pkgPath = path.join(projectPath, "package.json");
const pkg = readJsonFile(pkgPath, PackageJson);
@timostamm
timostamm / fetch-universal-handler.ts
Last active March 27, 2023 17:51
Create request handler using the fetch API Request/Response types from a @bufbuild/connect universal handler (early and barely tested)
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@timostamm
timostamm / Makefile
Created March 9, 2023 12:16
Basic protoc plugin example
default:
buf generate buf.build/bufbuild/eliza
@timostamm
timostamm / [[...connect]].ts
Created February 28, 2023 10:08
PoC for running Connect on Next.js
// place this file in pages/api/[[...connect]].ts
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
@timostamm
timostamm / buf.gen.yaml
Created December 12, 2022 10:07
Simple plugin to generate an enum object for every oneof
version: v1
plugins:
- name: oneofhelper
path: ./src/protoc-gen-oneofhelper.ts
opt: target=ts
out: src/gen
@timostamm
timostamm / Makefile
Created November 10, 2022 15:21
Run a Connect-Web client against the Eliza demo service with Deno
default:
buf generate buf.build/bufbuild/eliza
deno lint .
deno run --import-map import_map.json --allow-net client.ts
@timostamm
timostamm / generate-ts.ts
Created October 27, 2022 13:41
Using exports and imports in @bufbuild/protoplugin
import {localName, GeneratedFile, ImportSymbol, Schema} from "@bufbuild/protoplugin/ecmascript";
import type {DescMessage} from "@bufbuild/protobuf";
export function generateTs(schema: Schema) {
const map = new Map<DescMessage, ImportSymbol>();
function getFunctionName(f: GeneratedFile, message: DescMessage): ImportSymbol {
let s = map.get(message);
if (!s) {
s = f.export(`validate${localName(message)}`);
map.set(message, s);