Avatar

tmcw

👷 Building Val Town
Joined August 31, 2022
Public vals
167
tmcw avatar
tmcw
addTwoNumbersExample
Script
An interactive, runnable TypeScript val by tmcw
0
tmcw avatar
tmcw
californiaBusinessNotifications
Script
California business records change notifications There's nothing I like more than a little open data and transparency! This Val shows you how to use the California Secretary of State Business Search as a faux-API and get notifications when records change. This lets you know when a business updates their records, changes their good-standing status, and so on. Here's how to use it: Fork this val Open the California Secretary of State Search . Open your browser's inspector tools. Search for the business you're interested in, and click on its header row. Now, in the network tab of your developer tools, you should see a request that ends with false . You'll find a number in that URL. Plug that number into the val at the spot at the top. (We're almost there!) Now just run the val and you should get a notification! If you want to get these intermittently, schedule the val. It's probably not necessary to run this more than once a week - businesses update these details very infrequently. Notes opencorporates has a way better system for open data around businesses, but to get a free API key you need to be a nonprofit or a registered journalist, and I am neither. But if you are, use that! Thanks to America's laws around business registration, it's commonplace for businesses to mask their control and ownership by using registration agents and a few layers of corporate structure.
0
tmcw avatar
tmcw
dominoDomExample
Script
domino example Domino is an implementation of the document object model that you can use to build "webpages" without requiring a browser or a heavy browser-like abstraction. If you need even less DOM-compatibility and just want to parse some HTML, cheerio is an even more lightweight abstraction that works great.
0
tmcw avatar
tmcw
happyDomExample
Script
happy-dom Happy-dom is a module that provides a DOM-like abstraction that's pretty popular for test frameworks and server-side rendering, because it's a lot faster and lighter than more full-fledged browser-like options. By default, happy-dom doesn't support Deno , which is the basis for Val Town's runtime, but there's a fork that conveniently swaps out the thing it doesn't support, which is the Node.js vm module. Another way to work with the DOM is to use domino .
0
tmcw avatar
tmcw
datascriptExample
Script
datascript example An example using tonsky’s datascript module, which implements a datalog-style database in JavaScript. The module itself is really written in ClojureScript, but they've helpfully produced an npm-compatible module for us to use. Datalog languages are fascinating, and have some real-world use in Roam Research and Logseq.
0
tmcw avatar
tmcw
reasonPhrase
Script
The story behind HTTP 200 "OK" What's in an HTTP response? I've been writing software for the web since the early 2000s and have incrementally learned things about HTTP. There are status codes, like "200" and "404". There are headers, for Content-Type and headers to control cache settings. There are different versions of HTTP itself, like 1.1, 2, and 3. HTTP requests and responses can contain data, in the message body. But there's one thing I didn't notice until yesterday. A quirk that was included in the HTTP 1.1 specification with an authors note that it's mostly there for historical reasons: the reason-phrase . None of this information is useful. The reason-phrase is barely supported on the web and was always an oddity, but keep reading if you like oddities! If you're used to JavaScript’s fetch() method to make HTTP requests, you've seen the reason-phrase under a different name: statusText : (await fetch('https://example.com/')).statusText What is statusText ? I had assumed that it was something that JavaScript itself provides, by looking up the status code 200 and matching it with the text "OK". I was wrong! When I look at a raw HTTP response, I see the first few lines are like this: HTTP/1.1 200 OK Date: Thu, 17 Aug 2023 15:16:42 GMT Content-Type: text/plain;charset=UTF-8 The reason phrase So what is that text? I dug around in the HTTP 1.0 specification and found the section Status Code and Reason Phrase . The Status-Code element is a 3-digit integer result code of the attempt to understand and satisfy the request. The Reason-Phrase is intended to give a short textual description of the Status-Code. The Status-Code is intended for use by automata and the Reason-Phrase is intended for the human user. The client is not required to examine or display the Reason-Phrase. That also lists recommended reason phrases, like OK for 200 and Not Found for 404. And notes that you can choose different phrases without affecting the protocol. The HTTP 1.1 specification adds a little color about the reason-phrase : So, with a HTTP server, you can customize your reason phrase! Here's an example with a val on Val Town: let customReason = (req) => new Response("", { statusText: 'Hello world!', }); Unfortunately, this doesn't work! The response that Val Town produces is reorganized and optimized by Cloudflare, which upgrades requests and responses from HTTP 1.1 to HTTP 2. And sadly, HTTP 2 dropped support for the custom reason-phrase . RIP the reason-phrase . It was present even in a 1992 draft of the HTTP specification , and was a weird and under-appreciated way to pilfer extra information in a response. Now, thanks to HTTP/2 and the commonplace use of proxies and CDNs like Cloudflare, it's no longer usable. It was fun while it lasted.
1
tmcw avatar
tmcw
tweetnacl
Script
tweetnacl example An example of using tweetnacl from Val Town - this uses the keyPair method and then encodes the keypair using hex encoding.
0
tmcw avatar
tmcw
activitypub
HTTP
WebFinger This is a not-quite-complete ActivityPub implementation based on my blog post about building an AP implementation . It includes enough to look up bot@tmcw-activitypub.web.val.run on Mastodon and get some basic information.
1
tmcw avatar
tmcw
duckDbCsv
Script
An interactive, runnable TypeScript val by tmcw
0
tmcw avatar
tmcw
getDuckDB
Script
Get a DuckDB database This method sets up a duckdb-wasm database that you can then use to store and retrieve data.
5
tmcw avatar
tmcw
duckdbExample
Script
DuckDB DuckDB works on Val Town, with only one small tweak! We're basically using DuckDB in the same way you'd use it with a browser - using the WASM package with its dependencies fetched from jsdelivr . The only trick is to create the worker ourselves rather than using duckdb.createWorker . DuckDB's built-in createWorker method doesn't specify a worker type, which causes type to default to classic , and Deno (our runtime) doesn't support classic workers.
1
tmcw avatar
tmcw
pdfExample
HTTP
Generate a pdf with pdf-lib 👉 Visit the web site generated by this Val pdf-lib is an incredible pure-JavaScript module that generates PDFs! In this example, it's generating a simple page that embeds the PDF, and exposes another route (with Hono ) that serves the PDF.
1
tmcw avatar
tmcw
publicKey
Script
An interactive, runnable TypeScript val by tmcw
0
tmcw avatar
tmcw
streamExample
Script
Response with ReadableStream example Demonstrates our ability to handle streaming responses. We do, in practice, read the streams and then return the response - basically buffering - but with the support for the Response object we include the option to pass a ReadableStream instance as the first argument. Note that it's required that the ReadableStream returns Uint8Array items.
1
tmcw avatar
tmcw
openstreetmapAPIexample
Script
Requesting data from the OpenStreetMap API The OpenStreetMap API allows both unauthenticated public reads and writes using authentication tokens. This example shows us getting the first node ever created in the project, which was last edited by owene .
0
tmcw avatar
tmcw
checkIsDiscordInvite
Script
Check if a URL is a Discord invite A handy val that checks whether a URL is one.
0