Skip to content

Instantly share code, notes, and snippets.

View kevinswiber's full-sized avatar
🍄
lizard person

Kevin Swiber kevinswiber

🍄
lizard person
View GitHub Profile
@kevinswiber
kevinswiber / server.js
Last active September 4, 2024 14:02
Node.js server with best-effort completions on functions
import { AsyncResource } from "node:async_hooks";
import { createServer, request } from "node:http";
// A thin wrapper around AsyncResource to manage best-effort completions.
// Best-effort resources are not guaranteed to complete, but they are
// allowed time to complete before the process exits on SIGINT.
// In a real-world scenario, you would likely include more details
// in the resource context to help trace completed and
// abandoned best-effort executions.
class BestEffort extends AsyncResource {
@kevinswiber
kevinswiber / typescript-hook.js
Last active November 27, 2023 15:15
Run the TypeScript compiler in watch mode while running Node.js in watch mode. Yowza.
// Usage:
// node --import=./typescript-hook.js --watch ./dist/index.js
//
// Make changes to your TypeScript code, have it automatically
// recompiled, and see it reloaded in Node.js.
//
// This file is intended to be used as a Node.js preload module.
// The TypeScript compiler (tsc) will be run in watch mode.
// This is useful while running Node.js itself runs in watch mode.
//
@kevinswiber
kevinswiber / url-pattern.js
Last active October 24, 2023 21:04
Implementation of URL Pattern spec
import assert from "node:assert";
function escapeRegexString(str) {
return str.replace(/[\.\+\*\?\^\$\{\}\(\)\[\]\|\/\\]/g, "\\$&");
}
function isValidNameCodePoint(codePoint, isFirstCodePoint) {
if (isFirstCodePoint) {
return (codePoint >= 65 && codePoint <= 90) ||
(codePoint >= 97 && codePoint <= 122) ||
@kevinswiber
kevinswiber / .spectral.yaml
Created October 13, 2023 05:01
Spectral de-dupe minimal repro
rules:
missing_error_response_content:
given: '$..responses[?(@property && @property.match(/^(4|5)/))]'
then:
field: content
function: defined
formats:
- oas3
severity: warn
message: Error response should contain a response body.
@kevinswiber
kevinswiber / http-server.js
Last active October 19, 2023 21:49
Simple Node.js HTTP server with router and logging.
// Purpose: A simple HTTP server with routing and logging.
// Author: Kevin Swiber <[email protected]>
// Exports:
// serve({ routes, host, port, protocol, secure, serverOptions })
// routes: A Map of routes to handlers. Example:
// const routes = new Map();
// routes.set("/greeting", {
// get: ({ response }) => {
// response.setHeader("Content-Type", "application/json");
// response.end(JSON.stringify({ hello: "world" }));
@kevinswiber
kevinswiber / extensions.json
Last active September 26, 2023 19:39
Spectral JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "@stoplight/spectral-core/meta/extensions",
"$defs": {
"Extends": {
"$anchor": "extends",
"oneOf": [
{
"type": "string"
},
@kevinswiber
kevinswiber / spectral-language-server-support-workspace-folders.patch
Created September 23, 2023 15:40
Patches for yaml-language-server and spectral-language-server to work with Neovim's lsp and lsp-config
Changing configuration.workspaceFolder to connection.workspace.getWorkspaceFolders() to support non-VSCode workspace folders.
diff --git a/dist/spectral-language-server/src/server.js b/dist/spectral-language-server/src/server.js
index 8878d95..7ec85d6 100644
--- a/dist/spectral-language-server/src/server.js
+++ b/dist/spectral-language-server/src/server.js
@@ -118,19 +118,21 @@ function resolveSettings(document) {
}
let rulesetFile = null;
connection.console.log(`Using ruleset file: ${configuration.rulesetFile}.`);
@kevinswiber
kevinswiber / conference-api.txt
Last active March 6, 2023 11:22
Playing around with an API definition format
package dev.swiber.api.conference
error Error = NotFound | InternalServerError
[location="/conferences"]
resource Conference {
[method="GET", location="/{id}"]
operation Retrieve(RetrieveConference): (ConferenceRetrieved, Error)
[method="POST"]
operation Create(CreateConference): (ConferenceCreated, Error)
@kevinswiber
kevinswiber / graphql.json
Created August 19, 2022 01:26
GraphQL Introspection Example
{
"data": {
"__schema": {
"queryType": {
"name": "Root"
},
"mutationType": null,
"subscriptionType": null,
"types": [
{
@kevinswiber
kevinswiber / page.schema.json
Last active March 17, 2022 21:02
An example illustrating a use case of composition via templates.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/page.schema.json",
"title": "Page",
"description": "A page in a catalog",
"type": "object",
"properties": {
"title": {
"description": "A descriptive title for the page",
"type": "string"