Search
libmultibase
@vladimyr
// SPDX-License-Identifier: 0BSD
Script
0xeb51: KeyType["JWK-JCS"],
} as const;
export function multibaseToBytes(input: string) {
const bytes = base58btc.decode(input);
const [codec, prefixLength] = varint.decode(bytes);
throw new Error("error: invalid key size");
return { keyBytes, keyType: keyType.name };
export function bytesToMultibase(keyBytes: Uint8Array, keyType: string) {
const type = KeyType[keyType];
if (!type) throw new TypeError("error: unsupported key type");
HttpMiddlewareChain
@emarref
An implementation of chainable http middleware. Wrap your http handlers in middleware to add functionality and reduce code duplication. export default function chain()
.then(add(requireHttpMethod("post")))
.then(add(requireAuthentication()))
.then(send())
Script
An implementation of chainable http middleware.
Wrap your http handlers in middleware to add functionality and reduce code duplication.
```ts
export default function chain()
.then(add(requireHttpMethod("post")))
send: (response: Response) => HttpHandler;
export function chain(): Promise<Chain> {
const stack = new Set<HttpMiddleware>();
async function getChain(): Promise<Chain> {
return { add, send };
let happy = true;
async function loop() {
middleware = queue.shift();
return getChain();
export function add(middleware: HttpMiddleware) {
return function({ add }: Chain) {
return add(middleware);
export function send(response?: Response) {
return function({ send }: Chain) {
return send(response ?? success());
checkTomorrowTempoEdf
@dotim
Ce script permet de vérifier le statut du jour Tempo de demain (Bleu, Blanc ou Rouge) en utilisant l'API publique Couleur Tempo. S'il détecte un jour Rouge ou blanc, il envoie une notification pour avertir l'utilisateur. En cas d'erreur lors de la récupération des données, une notification d'erreur est également envoyée.
Cron
day: "numeric",
} as const,
export default async function checkTempoStatus() {
try {
const tempoData = await TempoService.getTomorrow();
sendDiscordMessage
@ktodaz
Send Chunked Discord Message This function is used to send a message to a Discord webhook. If the message exceeds the maximum character limit (2000 characters), it will be divided into chunks and sent as multiple messages. Parameters message (string): The message to be sent to the Discord webhook. Return Value This function does not return any value. Example Usage: const message = "This is a long message that needs to be sent to Discord. It may exceed the character limit, so it will be divided into smaller chunks.";
await @ktodaz.sendDiscordMessage(message); In the above example, the sendDiscordMessage function is used to send the message to the Discord webhook. If the message exceeds 2000 characters, it will be split into smaller chunks and sent as separate messages. Required Secrets: Ensure you have added the secret for discord_webhook in your val.town secrets settings (Profile -> Secrets)
Script
# Send Chunked Discord Message
This function is used to send a message to a Discord webhook. If the message exceeds the maximum character limit (2000 characters), it will be divided into chunks and sent as multiple messages.
### Parameters
### Return Value
This function does not return any value.
## Example Usage:
await @ktodaz.sendDiscordMessage(message);
In the above example, the sendDiscordMessage function is used to send the message to the Discord webhook. If the message exceeds 2000 characters, it will be split into smaller chunks and sent as separate messages.
## Required Secrets:
webhook_url: string = process.env.discord_webhook,
function chunkString(str) {
const chunks = [];
datetimeFormatterTool
@maxencelav
@jsxImportSource https://esm.sh/react
HTTP
{ label: "Custom", value: "custom" },
function App() {
const [dateTime, setDateTime] = useState("2023-06-15T14:30:00");
</div>
function client() {
createRoot(document.getElementById("root")).render(<App />);
client();
export default async function server(request: Request): Promise<Response> {
return new Response(
multiFormat
@postpostscript
multiFormat: easily create outputs of multiple formats (e.g. text/html for Email) using tagged templates Examples import { format } from "https://esm.town/v/postpostscript/multiFormat";
console.log(format.Plain`
${format.Heading1`Some Heading`}
Lorem ipsum dolor ${format.Strong`sit`} amet
`); {
"text": "\nSome Heading\n\nLorem ipsum dolor sit amet\n",
"html": "\n<br><h1>Some Heading</h1>\n<br>\n<br>Lorem ipsum dolor <strong>sit</strong> amet\n<br>"
} Create Your Own Formatters import { createFormatMethod, wrapHTMLTag, type MultiFormatWithHTML } from "https://esm.town/v/postpostscript/multiFormat";
export const Red = createFormatMethod<MultiFormatWithHTML>((value) => {
return wrapHTMLTag("span", value, {
style: "color: red;",
});
});
console.log(Red`red text!`) {
"text": "red text!",
"html": "<span style=\"color: red;\">red text!</span>"
} Sanitization Text is automatically sanitized when converted to HTML: console.log(format.Trim`
${format.Heading1`Some Heading`}
<script>alert(1)</script>
`.html.toString()) <h1>Some Heading</h1>
<br>
<br><script>alert(1)</script>
Script
export function multiFormat<R extends MultiFormat = { text: string }>(
export function combineMultiFormat<R extends MultiFormat>(
export function joinMultiFormat<R extends MultiFormat>(values: (R | string)[], join: R | string) {
export function normalizeMultiFormat<R extends MultiFormat>(value: R | string | number | boolean | null | undefined) {
export function getMultiFormatValue<R extends MultiFormat, K extends keyof R>(
function isString(value: unknown): value is string {
function toString(value: unknown) {
export function addStrings(valueA: unknown, valueB: unknown) {
export function wrapHTMLTag(
export function createFormatMethod<R extends MultiFormat>(method: (value: R) => R) {
setCorsHeaders
@neverstew
An interactive, runnable TypeScript val by neverstew
Script
"Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
} as const;
export default function setCorsHeaders(
response: Response,
headers: Partial<Record<keyof typeof DEFAULT_CORS_HEADERS, string>> = DEFAULT_CORS_HEADERS,
dateHelpers
@iamseeley
An interactive, runnable TypeScript val by iamseeley
Script
export function formatDateRange(startDateString, endDateString, format) {
const startDate = new Date(startDateString);
const endDate = new Date(endDateString);
return startFormatted;
return `${startFormatted} - ${endFormatted}`;
function formatSingleDate(date, format) {
const options = {};
switch (format) {
HttpMiddlewareRequireValidHmacSignature
@emarref
Middleware to ensure the signed request is valid. export default chain()
.then(add(requireValidHmacSignature({
// The secret value shared between you and the originating party
signingKey: "my_super_secret_key",
// The name of the header containing the signature to compare with our value
signatureHeaderName: "x-signature",
})))
.then(send())
Script
type Hmac = ReturnType<HmacFactory>;
type DigestFunction = Hmac["digest"];
type Algorithm = Parameters<HmacFactory>[0];
type Encoding = Parameters<DigestFunction>[0];
type Options = {
encoding?: Encoding;
export function requireValidHmacSignature({
algorithm = "sha256",
}: Options): HttpMiddleware {
return function(next) {
return async function(request) {
const providedSignature = request.headers.get(signatureHeaderName) ?? "";
myBookmarkManager
@arfan
@jsxImportSource https://esm.sh/react@18.2.0
HTTP
function MyBookmarkManager() {
function getSiteIconURL(bookmark) {
async function fetchSavedBookmarks() {
function showAddNewBookmarkOverlay() {
function handleRightClickOnBookmark(e, bookmark) {
function closeAddBookmarkOverlay() {
function closeSettingsOverlay() {
async function handleBookmarkFormSubmit(e) {
async function saveAllBookmarks() {
function importBookmarksFromFile(event) {
btcPriceAlert
@luizhrios
BTC Price Alert This val monitors the price of Bitcoin (BTC) and sends an email alert if the price fluctuates significantly. Specifically, it checks the current BTC price against the last recorded price and triggers an email notification if the change exceeds 20%. The email includes the new price, formatted as currency. Fork this val to get these notifications on your inbox.
Cron
import { email } from "https://esm.town/v/std/email?v=9";
import { currency } from "https://esm.town/v/stevekrouse/currency";
export async function btcPriceAlert() {
const lastBtcPrice: number = await blob.getJSON("lastBtcPrice");
let btcPrice = await currency("usd", "btc");
diffHtml
@chet
An interactive, runnable TypeScript val by chet
Script
import { cleanHtml } from "https://esm.town/v/chet/cleanHtml";
import { diffLines } from "https://esm.town/v/chet/diffLines";
export function diffHtml(before: string, after: string) {
return diffLines(cleanHtml(before), cleanHtml(after));
reactEmailTemplate
@tomas_padrieza
Simple demo of a React Email rendering
Script
import * as React from "npm:react";
import { sendMail } from "https://esm.town/v/tomas_padrieza/sendEmail";
export function Email(props) {
const { url } = props;
return (
computeSchedule
@toowired
computeSchedule function Combines several schedule segments together to form a complete schedule for a 24-hour period. Parameters: scheduleSegments - An array of ScheduleSegment objects describing the daily loop. Returns: An array of TimeSpan objects representing the complete schedule for the device. TimeSpan type Represents a span of time with a starting point and a duration in minutes. DutyCycle type Represents the "on" state scheduling pattern for a device. It's similar to the concept of Pulse-Width Modulation (PWM). ScheduleSegment type A part of a complete schedule, describing a time frame with a specified duty cycle.
Script
# `computeSchedule` function
Combines several schedule segments together to form a complete schedule for a 24-hour period.
publishYoutubeToLemmy
@pdebie
Publish a Youtube feed to Lemmy This allows you to automatically publish a Youtube channel to to a Lemmy community. Example usage: async function publishMyFeed() {
try {
return @pdebie.publishYoutubeToLemmy({
instance: "lemm.ee",
auth: @me.secrets.lemmee,
communityId: 23316,
youtubeChannelId: "UChZgikssAJkmiQOv_sO-ngQ",
lastSyncTime: @pdebie.spacexLemmyDb.lastSync,
});
}
finally {
@pdebie.spacexLemmyDb.lastSync = new Date().toISOString();
}
} Get a Youtube channel ID here . Make sure to set your lastSync properly, since otherwise you'll keep publishing new posts!
Script
```typescript
async function publishMyFeed() {
try {
import { youtubeFeed } from "https://esm.town/v/pdebie/youtubeFeed";
export async function publishYoutubeToLemmy(
{ instance, communityId, auth, youtubeChannelId, syncInfo, filter }: {
let client = new LemmyHttp(`https://${instance}`, {
fetchFunction: fetch,
const items =