Watch
1
0
Fork
You've already forked packages
0

packages/ at markup-kit dd1e6eb5270e

A snapshot of packages/ in the Weft repository; the official packages are published from these commits.

Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
This commit is contained in:
Weft packages 2026-11-19 20:43:26 +00:00
commit 306b143104
72 changed files with 10915 additions and 0 deletions

110
stripe/lib/form.weft Normal file
View file

@ -0,0 +1,110 @@
// Stripe's request encoding: `application/x-www-form-urlencoded` with nesting in brackets (`a[b][0]=c`). A `json` value's
// keys can't be listed in Weft, so this walks its JSON text, which keeps the fields in the order they were written.
import { parse, stringify } from "std/json"
fn isUnreserved(c: int): boolean {
return (c >= 48 && c <= 57) ||
(c >= 65 && c <= 90) ||
(c >= 97 && c <= 122) ||
c == 45 ||
c == 95 ||
c == 46 ||
c == 126
}
/// `text` percent-encoded as UTF-8, keeping only RFC 3986's unreserved characters.
export fn encodeComponent(text: string): string {
let mut out: string[] = []
for b of Bytes.fromUtf8(text).toList() {
if isUnreserved(b) {
out.push(String.fromCharCode(b))
} else {
out.push("%" + str(b, { radix: 16 }).toUpperCase().padStart(2, "0"))
}
}
return out.join("")
}
fn stringEnd(text: string, from: int): int {
let mut i = from + 1
while i < text.length {
let c = text.codeUnitAt(i) ?? 34
if c == 92 {
i += 2
} else if c == 34 {
return i + 1
} else {
i += 1
}
}
return i
}
fn pair(key: string, value: string): string {
let name = encodeComponent(key).replaceAll("%5B", "[").replaceAll("%5D", "]")
return `${name}=${encodeComponent(value)}`
}
fn child(key: string, name: string): string {
return key == "" ? name : `${key}[${name}]`
}
/// Writes the JSON value at `at` under `key`, and gives the position after it.
fn walk(text: string, at: int, key: string, mut out: string[]): int {
let c = text.codeUnitAt(at) ?? 0
if c == 123 {
let mut i = at + 1
while text.codeUnitAt(i) != 125 {
let end = stringEnd(text, i)
let name = try parse(text.slice(i, end)) else ""
let label = name is string ? name : ""
// `extra` holds fields this package doesn't model: they belong to the object it is written in.
i = walk(text, end + 1, label == "extra" ? key : child(key, label), &out)
if text.codeUnitAt(i) == 44 {
i += 1
}
}
return i + 1
}
if c == 91 {
let mut i = at + 1
let mut index = 0
while text.codeUnitAt(i) != 93 {
i = walk(text, i, `${key}[${index}]`, &out)
index += 1
if text.codeUnitAt(i) == 44 {
i += 1
}
}
return i + 1
}
if c == 34 {
let end = stringEnd(text, at)
let value = try parse(text.slice(at, end)) else ""
out.push(pair(key, value is string ? value : ""))
return end
}
let mut end = at
while end < text.length {
let u = text.codeUnitAt(end) ?? 44
if u == 44 || u == 125 || u == 93 {
break
}
end += 1
}
let literal = text.slice(at, end)
// A field left out is `null`: Stripe is sent nothing for it. To clear a field, send an empty string.
if literal != "null" {
out.push(pair(key, literal))
}
return end
}
/// `params` in Stripe's form encoding. `null` fields are left out.
export fn encodeForm(params: json): string {
let mut out: string[] = []
if params != null {
walk(stringify(params), 0, "", &out)
}
return out.join("&")
}

195
stripe/lib/sha256.weft Normal file
View file

@ -0,0 +1,195 @@
// SHA-256 and HMAC-SHA-256 (FIPS 180-4, RFC 2104), for verifying webhook signatures. Written here until `std/crypto` is
// on main: when it is, this file goes and `webhooks.weft` calls `hmac` and `timingSafeEqual` from there.
let K: i64[] = [
0x428a2f98,
0x71374491,
0xb5c0fbcf,
0xe9b5dba5,
0x3956c25b,
0x59f111f1,
0x923f82a4,
0xab1c5ed5,
0xd807aa98,
0x12835b01,
0x243185be,
0x550c7dc3,
0x72be5d74,
0x80deb1fe,
0x9bdc06a7,
0xc19bf174,
0xe49b69c1,
0xefbe4786,
0x0fc19dc6,
0x240ca1cc,
0x2de92c6f,
0x4a7484aa,
0x5cb0a9dc,
0x76f988da,
0x983e5152,
0xa831c66d,
0xb00327c8,
0xbf597fc7,
0xc6e00bf3,
0xd5a79147,
0x06ca6351,
0x14292967,
0x27b70a85,
0x2e1b2138,
0x4d2c6dfc,
0x53380d13,
0x650a7354,
0x766a0abb,
0x81c2c92e,
0x92722c85,
0xa2bfe8a1,
0xa81a664b,
0xc24b8b70,
0xc76c51a3,
0xd192e819,
0xd6990624,
0xf40e3585,
0x106aa070,
0x19a4c116,
0x1e376c08,
0x2748774c,
0x34b0bcb5,
0x391c0cb3,
0x4ed8aa4a,
0x5b9cca4f,
0x682e6ff3,
0x748f82ee,
0x78a5636f,
0x84c87814,
0x8cc70208,
0x90befffa,
0xa4506ceb,
0xbef9a3f7,
0xc67178f2,
]
/// The low 32 bits. Words are held in an `i64`, whose sums can't overflow, and masked: SHA-256's additions wrap, and an
/// `int`'s panic.
let WORD: i64 = 0xffffffff
fn shr(x: i64, n: int): i64 {
return x >>> n
}
fn rotr(x: i64, n: int): i64 {
return (shr(x, n) | (x << (32 - n))) & WORD
}
fn compress(mut h: i64[], m: int[], at: int) {
let mut w: i64[] = []
for t of 0..16 {
let i = at + t * 4
w.push(i64((m[i] << 24) | (m[i + 1] << 16) | (m[i + 2] << 8) | m[i + 3]) & WORD)
}
for t of 16..64 {
let a = w[t - 15]
let b = w[t - 2]
let s0 = rotr(a, 7) ^ rotr(a, 18) ^ shr(a, 3)
let s1 = rotr(b, 17) ^ rotr(b, 19) ^ shr(b, 10)
w.push((w[t - 16] + s0 + w[t - 7] + s1) & WORD)
}
let mut a = h[0]
let mut b = h[1]
let mut c = h[2]
let mut d = h[3]
let mut e = h[4]
let mut f = h[5]
let mut g = h[6]
let mut k = h[7]
for t of 0..64 {
let s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)
let ch = (e & f) ^ (~e & g & WORD)
let t1 = (k + s1 + ch + K[t] + w[t]) & WORD
let s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)
let maj = (a & b) ^ (a & c) ^ (b & c)
let t2 = (s0 + maj) & WORD
k = g
g = f
f = e
e = (d + t1) & WORD
d = c
c = b
b = a
a = (t1 + t2) & WORD
}
let sums = [a, b, c, d, e, f, g, k]
for i of 0..8 {
h[i] = (h[i] + sums[i]) & WORD
}
}
/// The byte of `word` that `shift` bits down.
fn byteOf(word: i64, shift: int): int {
return try int(shr(word, shift) & 255) else 0
}
/// The SHA-256 digest of `data`.
export fn sha256(data: Bytes): Bytes {
let mut m = data.toList()
let length = i64(m.length)
m.push(0x80)
while m.length % 64 != 56 {
m.push(0)
}
// The length in bits, as 64 bits big-endian.
let bits = length * 8
let high = shr(bits, 32) & WORD
let low = bits & WORD
for shift of [24, 16, 8, 0] {
m.push(byteOf(high, shift))
}
for shift of [24, 16, 8, 0] {
m.push(byteOf(low, shift))
}
let mut h: i64[] = [
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19,
]
let mut at = 0
while at < m.length {
compress(&h, m, at)
at += 64
}
let mut out: int[] = []
for word of h {
for shift of [24, 16, 8, 0] {
out.push(byteOf(word, shift))
}
}
return Bytes.of(out)
}
/// HMAC-SHA-256 of `data` with `key`.
export fn hmacSha256(key: Bytes, data: Bytes): Bytes {
let mut block = (key.length > 64 ? sha256(key) : key).toList()
while block.length < 64 {
block.push(0)
}
let inner = Bytes.of(block.map(b => b ^ 0x36)).concat(data)
let outer = Bytes.of(block.map(b => b ^ 0x5c))
return sha256(outer.concat(sha256(inner)))
}
/// Whether two byte strings are equal, looking at every byte whatever it finds.
export fn constantTimeEqual(a: Bytes, b: Bytes): boolean {
if a.length != b.length {
return false
}
let x = a.toList()
let y = b.toList()
let mut diff = 0
for i of 0..x.length {
diff = diff | (x[i] ^ y[i])
}
return diff == 0
}