Profiles / Business & IT
WA

Web & API developer

Build JSON responses, encode URLs and headers, parse logs with regex.

10 recipes on this page · open in the interactive view

Recipes
  1. Build and validate a JSON API response
  2. Encode an HTTP request: Basic auth, query string, safe HTML
  3. Parse an access log line into a JSON event
  4. Pagination with a Link header and an ETag
  5. Parse a Cookie header and a form body
  6. Resolve npm semver ranges
  7. WCAG contrast ratio of a button color
  8. srcset widths of a responsive image
  9. Token bucket rate limiter with Retry-After
  10. Decode and build a Snowflake id
executed on MentDB

Build and validate a JSON API response

An endpoint returns a list of products received from a form. The script builds the JSON response in memory, adds a generated UUID, and flags each item whose id is not a UUID or whose link is not a valid URL.

Pipeline
Script
admin
#1. Start the API response;
json load "rx_api" "{\"status\": 200, \"data\": []}";
#2. Add two items received from a form;
json iarray "rx_api" "/data" (concat "{\"id\": \"" (string uuid) "\", \"sku\": \"BLT-M8\", \"url\": \"https://shop.example.com/p/blt-m8\"}") OBJ;
json iarray "rx_api" "/data" "{\"id\": \"42\", \"sku\": \"NUT-M8\", \"url\": \"shop/p/nut-m8\"}" OBJ;
#3. Flag each item: a valid UUID and a valid URL;
json iobject "rx_api" "/data[0]" "valid" (* (type is_uuid (json select "rx_api" "/data[0]/id")) (type is_url (json select "rx_api" "/data[0]/url"))) INT;
json iobject "rx_api" "/data[1]" "valid" (* (type is_uuid (json select "rx_api" "/data[1]/id")) (type is_url (json select "rx_api" "/data[1]/url"))) INT;
json iobject "rx_api" "/" "count" (json count "rx_api" "/data") INT;
json doc "rx_api";
Result
mentdb
{
  "data": [
    {
      "valid": 1,
      "id": "9b03cbe3-50d0-41f8-9848-0f58edee91f7",
      "sku": "BLT-M8",
      "url": "https://shop.example.com/p/blt-m8"
    },
    {
      "valid": 0,
      "id": "42",
      "sku": "NUT-M8",
      "url": "shop/p/nut-m8"
    }
  ],
  "count": 2,
  "status": 200
}
Go further
  • Loop on any number of items with json parse_array instead of the fixed indexes
  • Sort the items before returning them with json sort
executed on MentDB

Encode an HTTP request: Basic auth, query string, safe HTML

A client calls a search API with Basic authentication and a query containing accents and an ampersand, then shows a user comment in a page. The script encodes the header in Base64, the query for the URL, and escapes the comment for HTML.

Pipeline
Script
admin
#1. Credentials and the query of the search;
-> "[auth]" (concat "Basic " (string encode_b64 "api_user:s3cret!"));
-> "[q]" (string url_encode "café & croissant");
#2. URL of the request;
-> "[url]" (concat "https://api.example.com/v1/search?q=" [q] "&lang=fr");
#3. A user comment shown safely in HTML;
-> "[html]" (concat "<p>" (string html_escape "<script>alert(\"x\")</script> 5 > 3") "</p>");
concat "Authorization: " [auth] [_n_] "GET " [url] [_n_] "Valid URL: " (type is_url [url]) [_n_] "Decoded: " (string url_decode [q]) [_n_] [html];
Result
mentdb
Authorization: Basic YXBpX3VzZXI6czNjcmV0IQ==
GET https://api.example.com/v1/search?q=caf%C3%A9%20%26%20croissant&lang=fr
Valid URL: 1
Decoded: café & croissant
<p>&lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt; 5 &gt; 3</p>
Go further
executed on MentDB

Parse an access log line into a JSON event

An HTTP access log line must become a structured event for monitoring. The script extracts the client IP, the method, the path without its query string, the order id and the status code with regular expressions, then flags the errors.

Pipeline
Script
admin
#1. One line of the access log;
-> "[log]" "203.0.113.42 - - [01/Nov/2026:10:15:32 +0400] \"GET /api/v1/orders/1842?expand=lines HTTP/1.1\" 404 512 0.087";
#2. Extract the fields with regular expressions;
-> "[ip]" (string regexp_extract [log] "^([0-9.]+)" 1);
-> "[path]" (string regexp_extract [log] "\"[A-Z]+ ([^ ?]+)" 1);
-> "[status]" (string regexp_extract [log] "\" ([0-9]{3}) " 1);
-> "[order]" (string regexp_extract [path] "/orders/([0-9]+)" 1);
#3. Build a JSON event;
concat "{\"ip\": \"" [ip] "\", \"ipv4\": " (type is_ipv4 [ip]) ", \"method\": \"" (string regexp_extract [log] "\"([A-Z]+) " 1) "\", \"path\": \"" [path] "\", \"order_id\": " [order] ", \"status\": " [status] ", \"error\": " (>= [status] 400) "}";
Result
mentdb
{"ip": "203.0.113.42", "ipv4": 1, "method": "GET", "path": "/api/v1/orders/1842", "order_id": 1842, "status": 404, "error": 1}
Go further
executed on MentDB

Resolve npm semver ranges

A build tool must pick the version of a dependency that matches ^1.4.0 and ~1.4.1 among the published versions. A text sort puts 1.10.0 before 1.9.3, so the script builds a numeric key from the three parts, sorts it, and resolves the caret range (same major) and the tilde range (same major.minor) to the highest match.

Pipeline
Script
admin
#1. Versions of a package published on the registry, the project requires ^1.4.0 and ~1.4.1;
json load "rx_ver" "[\"1.4.10\", \"1.10.0\", \"1.4.2\", \"2.0.0\", \"1.3.9\", \"1.4.1\", \"1.9.3\"]";
json load "rx_key" "[]";
#2. Numeric sort key major x 1000000 + minor x 1000 + patch, 1.10.0 must come after 1.9.3;
json parse_array "rx_ver" "/" "[v]" {
	json iarray "rx_key" "/" (math trunc (+ (+ (* (atom get [v] 1 ".") 1000000) (* (atom get [v] 2 ".") 1000)) (atom get [v] 3 "."))) INT;
};
json load "rx_s" (math order_long (json doc "rx_key"));
#3. Caret range: same major and at least 1.4.0, tilde range: same 1.4 and at least 1.4.1;
-> "[sorted]" ""; -> "[caret]" ""; -> "[tilde]" "";
json parse_array "rx_s" "/" "[k]" {
	-> "[v]" (concat (math trunc (/ [k] 1000000)) "." (math trunc (/ (math mod [k] 1000000) 1000)) "." (math mod [k] 1000));
	-> "[sorted]" (concat [sorted] [v] " ");
	if (and (== (atom get [v] 1 ".") 1) (>= [k] 1004000)) {-> "[caret]" [v];};
	if (and (equal (atom before_includ [v] 2 ".") "1.4") (>= [k] 1004001)) {-> "[tilde]" [v];};
};
json unload "rx_ver"; json unload "rx_key"; json unload "rx_s";
concat "sorted: " (string trim [sorted]) " | ^1.4.0 resolves to " [caret] " | ~1.4.1 resolves to " [tilde]
Result
mentdb
sorted: 1.3.9 1.4.1 1.4.2 1.4.10 1.9.3 1.10.0 2.0.0 | ^1.4.0 resolves to 1.10.0 | ~1.4.1 resolves to 1.4.10
Go further
  • atom before_includ "1.4.10" 2 "." keeps the atoms up to the second one: 1.4
  • Pre-releases such as 2.0.0-beta.1 sort before 2.0.0: split on the dash first
executed on MentDB

WCAG contrast ratio of a button color

The front-end developer checks the brand blue against three possible backgrounds. The script converts each hex channel to a number, linearises the sRGB values, computes the relative luminance and the contrast ratio (L1 + 0.05) / (L2 + 0.05), and checks the AA levels 4.5 for normal text and 3 for large text.

Pipeline
Script
admin
#1. Brand text color and three candidate backgrounds of a button, CSS hex colors without the hash sign;
-> "[fg]" "1F6FEB"; json load "rx_bg" "[\"FFFFFF\", \"0D1117\", \"E6EDF3\"]";
#2. Relative luminance of WCAG 2: each sRGB channel c/255 linearised, then 0.2126 R + 0.7152 G + 0.0722 B;
-> "[w]" "[0.2126, 0.7152, 0.0722]"; json load "rx_w" [w];
-> "[col]" [fg]; -> "[L]" 0;
for (-> "[i]" 0) (< [i] 3) (++ "[i]") {
	-> "[c]" (/ (string hex_to_int (string mid [col] (* [i] 2) (+ (* [i] 2) 2))) 255);
	-> "[lin]" (/ [c] 12.92);
	if (> [c] 0.04045) {-> "[lin]" (math power (/ (+ [c] 0.055) 1.055) 2.4);};
	-> "[L]" (+ [L] (* (json select "rx_w" (concat "/[" [i] "]")) [lin]));
};
-> "[Lfg]" [L]; -> "[out]" (concat "text " [fg] " L=" (math round [Lfg] 4) "; ");
#3. Contrast ratio L1 + 0.05 / L2 + 0.05 with each background, AA needs 4.5 for normal text, 3 for large text;
json parse_array "rx_bg" "/" "[col]" {
	-> "[L]" 0;
	for (-> "[i]" 0) (< [i] 3) (++ "[i]") {
		-> "[c]" (/ (string hex_to_int (string mid [col] (* [i] 2) (+ (* [i] 2) 2))) 255);
		-> "[lin]" (/ [c] 12.92);
	if (> [c] 0.04045) {-> "[lin]" (math power (/ (+ [c] 0.055) 1.055) 2.4);};
		-> "[L]" (+ [L] (* (json select "rx_w" (concat "/[" [i] "]")) [lin]));
	};
	-> "[cr]" (/ (+ (math max [L] [Lfg]) 0.05) (+ (math min [L] [Lfg]) 0.05));
	-> "[out]" (concat [out] "on " [col] " " (math round [cr] 2) ":1 " (if (>= [cr] 4.5) {"AA";} {if (>= [cr] 3) {"AA large only";} {"fail";}}) "; ");
};
json unload "rx_bg"; json unload "rx_w";
[out]
Result
mentdb
text 1F6FEB L=0.1766; on FFFFFF 4.63:1 AA; on 0D1117 4.08:1 AA large only; on E6EDF3 3.92:1 AA large only;
Go further
  • The blue passes AA only on white: use a lighter blue on the dark theme
  • AAA asks for 7:1, the same code checks it with another threshold
executed on MentDB

srcset widths of a responsive image

A hero image is displayed at 100vw on phones, 50vw on tablets and 720 px on desktop. For the device widths and pixel ratios of the audience, the script computes the image width each device needs, rounds it up to a 160 px step to limit the CDN variants, sorts and dedupes the widths and writes the srcset and sizes attributes.

Pipeline
Script
admin
#1. A hero image shown at 100vw on phones up to 767 px, 50vw on tablets, 720 px fixed from 1280 px, devices with DPR 1 to 3;
json load "rx_dev" "[[360, 3], [390, 3], [414, 2], [768, 2], [1024, 2], [1280, 1], [1440, 2], [1920, 1]]";
json load "rx_need" "[]";
#2. Rendered CSS width x DPR, rounded up to a 160 px step so the CDN caches few variants;
json parse_array "rx_dev" "/" "[d]" {
	json load "rx_d" [d];
	-> "[vw]" (json select "rx_d" "/[0]");
	-> "[css]" (if (< [vw] 768) {[vw];} {if (< [vw] 1280) {/ [vw] 2;} {720;}});
	-> "[px]" (* (math ceil (/ (* [css] (json select "rx_d" "/[1]")) 160)) 160);
	json iarray "rx_need" "/" (math trunc [px]) INT;
};
#3. Sorted widths without duplicates, then the srcset and sizes attributes of the img tag;
json load "rx_w" (math order_int (json doc "rx_need"));
-> "[srcset]" ""; -> "[prev]" 0; -> "[list]" "";
json parse_array "rx_w" "/" "[w]" {
	if (!= [w] [prev]) {-> "[srcset]" (concat [srcset] "hero-" [w] ".webp " [w] "w, "); -> "[list]" (concat [list] [w] " ");};
	-> "[prev]" [w];
};
-> "[out]" (concat "widths needed " (string trim [list]) " | srcset=\"" (string regexp_replace [srcset] ", $" "") "\" sizes=\"(min-width: 1280px) 720px, (min-width: 768px) 50vw, 100vw\"");
json unload "rx_dev"; json unload "rx_d"; json unload "rx_need"; json unload "rx_w";
[out]
Result
mentdb
widths needed 800 960 1120 1280 1440 | srcset="hero-800.webp 800w, hero-960.webp 960w, hero-1120.webp 1120w, hero-1280.webp 1280w, hero-1440.webp 1440w" sizes="(min-width: 1280px) 720px, (min-width: 768px) 50vw, 100vw"
Go further
  • The browser chooses in srcset with sizes: the server never needs to know the device
  • Generate the variants at the build with an image tool, one per width of the list
executed on MentDB

Token bucket rate limiter with Retry-After

An API limits each key to bursts of 5 requests and 0.5 request per second on average. The script replays the arrival times through a token bucket: refill since the last request (capped at the capacity), one token per request, else a 429 response with the Retry-After seconds, and the remaining tokens.

Pipeline
Script
admin
#1. Token bucket of an API key: capacity 5 requests, refill 0.5 token per second, arrival times in seconds;
-> "[cap]" 5; -> "[rate]" 0.5;
json load "rx_req" "[0.0, 0.2, 0.4, 0.5, 0.9, 1.1, 1.3, 3.0, 3.1, 7.6, 7.7, 7.8]";
-> "[tokens]" [cap]; -> "[last]" 0; -> "[log]" ""; -> "[ok]" 0;
json parse_array "rx_req" "/" "[t]" {
	#2. Refill since the last request, never above the capacity;
	-> "[tokens]" (math min [cap] (+ [tokens] (* (- [t] [last]) [rate])));
	-> "[last]" [t];
	#3. One token per request, else 429 with Retry-After = seconds to get one token, rounded up;
	if (>= [tokens] 1) {
		-> "[tokens]" (- [tokens] 1); -> "[ok]" (+ [ok] 1);
		-> "[log]" (concat [log] [t] "s 200, ");
	} {
		-> "[log]" (concat [log] [t] "s 429 Retry-After " (math ceil (/ (- 1 [tokens]) [rate])) ", ");
	};
};
json unload "rx_req";
concat [log] "accepted " [ok] " of 12, X-RateLimit-Remaining " (math trunc [tokens])
Result
mentdb
0.0s 200, 0.2s 200, 0.4s 200, 0.5s 200, 0.9s 200, 1.1s 429 Retry-After 1, 1.3s 429 Retry-After 1, 3.0s 200, 3.1s 429 Retry-After 1, 7.6s 200, 7.7s 200, 7.8s 429 Retry-After 1, accepted 8 of 12, X-RateLimit-Remaining 0
Go further
  • Store the tokens and the last time per key in a json object or a node to share them between requests
  • A sliding window counter is simpler but lets bursts of twice the limit pass at the window edges
executed on MentDB

Decode and build a Snowflake id

A support ticket gives a 64-bit Snowflake id. The script splits it exactly (19 digits do not fit a double): milliseconds since the epoch by an integer division by 2^22, worker id and sequence with bit masks, the creation date in UTC, and builds a new id with OR for another worker.

Pipeline
Script
admin
#1. A Snowflake id: 41 bits of milliseconds since the Twitter epoch 1288834974657, 10 bits of worker, 12 bits of sequence;
-> "[id]" "1541815603606036480"; -> "[epoch]" 1288834974657;
#2. Shifts are exact integer divisions by 2^22 and 2^12, masks keep the low bits;
-> "[ms]" (bigdec round (bigdec div [id] 4194304 40) 0 DOWN);
-> "[worker]" (math bit_and (bigdec round (bigdec div [id] 4096 40) 0 DOWN) 1023);
-> "[seq]" (math bit_and [id] 4095);
-> "[unix_ms]" (bd+ [ms] [epoch]);
#3. Creation date in UTC, and a new id rebuilt with OR for worker 37, sequence 5 at the same millisecond;
-> "[created]" (date to_utc (date from_epoch (bigdec round (bigdec div [unix_ms] 1000 40) 0 DOWN)));
-> "[new]" (math bit_or (math bit_or (bd* [ms] 4194304) (* 37 4096)) 5);
concat "ms since epoch " [ms] ", worker " [worker] ", sequence " [seq] ", created " [created] " UTC | new id " [new] " in hex " (string int_to_hex [new])
Result
mentdb
ms since epoch 367597485448, worker 378, sequence 0, created 2022-06-28 16:07:40 UTC | new id 1541815603604639749 in hex 1565a11f62025005
Go further
  • Ids sort by creation time: a range of ids is a range of dates, without a date column
  • Keep big ids as strings in JSON: JavaScript numbers lose the last digits above 2^53

Also in Business & IT

AM
Accountant & manager
Invoices with VAT, margins, break-even, depreciation and payment due dates.
10 recipes
FA
Financial analyst
Loan schedules, NPV and IRR, portfolio optimisation: solve and minimise in one line.
10 recipes
AR
Actuary & risk analyst
Frequency and severity laws, simulated losses, quantiles and risk capital.
10 recipes
HP
HR & payroll
Ages, seniority, leave balances and salary statistics.
10 recipes
LS
Logistics & supply chain
Order quantities, safety stock, reorder points and delivery dates.
10 recipes
SM
Sales & marketing analyst
Growth trends, seasonality, A/B tests and forecasts.
10 recipes
SE
Security engineer
Password hashing, webhook signatures, password strength and entropy.
10 recipes
TL
Text & language processing
Count words, match misspellings, convert names between case conventions.
10 recipes