Explorer

definy.serve-virtual-wasm

description
(en) /virtual/wasm/<key>(.wasm): respond with the Wasm of the store. Cached forever only if the key is the content hash of the bytes
type
Fn(path: String) -> @definy.service-io
expression
fn(path$0) =>
  let name$1 = @core.string-slice(value: $0, start: 14, end: @core.string-length(value: $0));
  let length$2 = @core.string-length(value: $1);
  let key$3 = match @core.and(
        left: @core.less-than(left: 4, right: $2),
        right: @core.equal(
          left: @core.string-slice(value: $1, start: @core.minus(left: $2, right: 5), end: $2),
          right: ".wasm"
        )
      ) {
      true =>
        @core.string-slice(value: $1, start: 0, end: @core.minus(left: $2, right: 5)),
      false =>
        $1
    };
  @definy.store-read-raw-content(
    key: $3,
    then: fn(content$4) =>
      match $4 {
        #none(_) =>
          #done<@definy.service-io>(#respond({
            status: 404,
            headers: @definy.cors-headers,
            body: #text<@definy.http-body>("Wasm binary not found in CAS")
          })),
        #some(found$5) =>
          #done<@definy.service-io>(#respond({
            status: 200,
            headers: @core.list-concat(
              left: [
                {name: "Content-Type", value: "application/wasm"},
                {
                  name: "Cache-Control",
                  value: match @core.equal(left: $5.content_hash, right: $3) {
                    true =>
                      "public, max-age=31536000, immutable",
                    false =>
                      "public, max-age=0, must-revalidate"
                  }
                }
              ],
              right: @definy.cors-headers
            ),
            body: #base64<@definy.http-body>($5.bytes_base64)
          }))
      }
  )
visibility
public
definy.serve-virtual-wasm (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: 3xAFkHzOqjI1XREBAUavS-Mvh0Ot-wj_5lXeQ_NwbYo
content hash: 8hYXkU8I_H9pPLAqxOavwHQFfVhymCl0Y0QeZYWuCf0