// examples/18_io_streams_regex_and_codecs.vk // End-to-end demo of Veko 1.0's streams, pattern matchers, dialect engine, codecs, and compression: // `buf`, `io`, `glob`, `regex`, `dialect`, `codec/{wire,hex,url,csv,ini,vbin,cbor,http2}`, and `compress/deflate`. use [ bits buf cbor = codec/cbor csv = codec/csv d = dialect def = compress/deflate glob h2 = codec/http2 hex = codec/hex ini = codec/ini io re = regex t = testing url = codec/url vbin = codec/vbin wire = codec/wire ] run-demo = fn [] i64 [ mut suite = t/new '18-io-matchers-codecs-deflate' // 1. ORO-native ByteReader/ByteWriter & Stream SchemeMux (`lib/buf.vk`, `lib/io.vk`) mux = io/mux-default mut src = io/mux-open mux 'mem://veko-stream-payload-2026' mut dst = io/mem-stream (bytes/with-cap 0) copied = io/copy mut src mut dst 8 _ = t/eq mut suite copied 24 'io/copy bytes' _ = t/eq mut suite (bytes/to-str (io/read-all mut dst)) 'veko-stream-payload-2026' 'io mem-stream copy' mut sb = buf/sb-new 32 _ = buf/sb-push mut sb 'veko:' _ = buf/sb-push mut sb 'ok' _ = t/eq mut suite (buf/sb-build sb) 'veko:ok' 'StringBuilder' // 2. Glob & ReDoS-proof Thompson NFA Regex (`lib/glob.vk`, `lib/regex.vk`) paths = ['lib/crypto/mlkem.vk' 'lib/codec/cbor.vk' 'docs/user/readme.md'] vk_files = glob/filter 'lib/**/*.vk' paths _ = t/eq mut suite (len vk_files) 2 'glob ** filter' rx_num = re/compile '[a-z]+=[0-9]+' matches = re/find-all rx_num 'host=edge port=8443 retries=3' _ = t/eq mut suite (len matches) 2 'regex find-all' _ = t/eq mut suite matches[0].text 'port=8443' 'regex first match' // 3. Homoiconic Dialect Rule Rewriter (`lib/dialect.vk`) rules = [ d/Rule { name = 'route' pattern = [.route '$method' '$path' '$target'] transform = fn [b] [ { method = (map/get b 'method') path = (map/get b 'path') target = (map/get b 'target') } ] } ] compiled_routes = d/apply-rules [ [.route .get '/health' 'health-svc'] [.route .post '/events' 'ingest-svc'] ] rules _ = t/eq mut suite (len compiled_routes) 2 'dialect rule count' _ = t/eq mut suite compiled_routes[1].target 'ingest-svc' 'dialect binding extraction' // 4. Schema-Driven Wire Packet Codec (`lib/codec/wire.vk`) pkt_schema = [ wire/Field { name = 'magic', kind = .u16_be } wire/Field { name = 'seq', kind = .varint } wire/Field { name = 'node', kind = .str_u16_be } ] pkt_bytes = wire/pack pkt_schema { magic = 0x564b, seq = 4096, node = 'eu-west-1' } pkt_dec = wire/unpack pkt_schema pkt_bytes _ = t/eq mut suite (map/get pkt_dec 'magic') 0x564b 'wire magic' _ = t/eq mut suite (map/get pkt_dec 'seq') 4096 'wire varint' _ = t/eq mut suite (map/get pkt_dec 'node') 'eu-west-1' 'wire str' // 5. Text & Structured Codecs: Hex, URL, CSV, INI, VBIN, CBOR, HTTP/2 + HPACK _ = t/eq mut suite (hex/encode (bytes/from-str 'OK')) '4f4b' 'codec/hex' u = url/parse 'https://api.veko.dev:8443/v1/items?env=prod&limit=50#top' q = url/parse-query u.query _ = t/eq mut suite u.host 'api.veko.dev' 'url host' _ = t/eq mut suite (map/get q 'limit') '50' 'url query' csv_recs = csv/parse-records 'id,region\n1,us-east\n2,eu-central' _ = t/eq mut suite (map/get csv_recs[1] 'region') 'eu-central' 'codec/csv' ini_cfg = ini/parse '[db]\nhost = 10.0.0.5\nport = 5432' _ = t/eq mut suite (ini/get ini_cfg 'db' 'port') '5432' 'codec/ini' payload_obj = { ok = true, count = -99, empty = .null, tags = ['pq' 'oro'] } vbin_dec = vbin/decode (vbin/encode payload_obj) cbor_dec = cbor/decode (cbor/encode payload_obj) _ = t/eq mut suite (map/get vbin_dec 'empty') .null 'vbin .null roundtrip' _ = t/eq mut suite (map/get cbor_dec 'count') -99 'cbor negative int roundtrip' hpack_blk = h2/hpack-encode [ h2/Header { name = ':method', value = 'GET' } h2/Header { name = ':path', value = '/v1/status' } h2/Header { name = 'x-proto', value = 'h2' } ] h2_frame = h2/encode-frame (h2/Frame { ftype = h2/frame-headers flags = h2/flag-end-headers stream_id = 3 payload = hpack_blk }) h2_dec = h2/decode-frame h2_frame h2_hdrs = h2/hpack-decode h2_dec.payload _ = t/eq mut suite h2_dec.stream_id 3 'http2 stream id' _ = t/eq mut suite h2_hdrs[1].value '/v1/status' 'http2 HPACK decode' // 6. RFC 1951 DEFLATE, RFC 1950 zlib, & RFC 1952 gzip (`lib/compress/deflate.vk`) raw_txt = bytes/from-str 'veko zero-trust sandbox runtime veko zero-trust sandbox runtime veko zero-trust sandbox runtime' gz = def/gzip-compress raw_txt zl = def/zlib-compress raw_txt _ = t/ok mut suite ((len gz) < (len raw_txt)) 'gzip shrinks repetitive payload' _ = t/eq mut suite (def/gzip-decompress gz) raw_txt 'gzip roundtrip' _ = t/eq mut suite (def/zlib-decompress zl) raw_txt 'zlib roundtrip' _ = t/eq mut suite (bits/crc32 (bytes/from-str '123456789')) 0xcbf43926 'CRC-32 IEEE vector' println (str/join ['Compressed ' (mold (len raw_txt)) ' B -> ' (mold (len gz)) ' B (gzip) / ' (mold (len zl)) ' B (zlib)'] '') t/report suite ] res = run-demo if err? res [ println (mold res) res ] else [ 0 ]