wasm\api.mbt
0% · 0 / 118 points · 222 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # npm-wasm 导出层(S12) - 3
/// - 4
/// 只做三件事:把 base64 当输入、把 JSON(base64) 当输出。 - 5
/// 这样 wasm-gc 侧只需要 JS String Builtin(`wasm:js-string`), - 6
/// 不需要在 JS 里操作 MoonBit 的 GC 对象;Node 侧用 Buffer 做 base64 转换。 - 7
/// - 8
/// 导出的三个入口: - 9
/// - `image_info(base64) -> JSON`:格式/尺寸/通道/体积; - 10
/// - `image_convert(base64, format, quality) -> JSON{data}`:可写格式互转,`format=best` 选最小; - 11
/// - `image_resize(base64, w, h, mode, format, quality) -> JSON{data}`:cover / contain 缩放。 - 12
- 13
///| - 14
/// 本包自己的 JSON 对象别名(不复用 imgcli 的私有类型)。 - 15
type Out = Map[String, @json.Json] - 16
- 17
///| - 18
fn jstr(s : String) -> @json.Json { - 19○
@json.Json::string(s) - 20
} - 21
- 22
///| - 23
fn jnum(n : Int32) -> @json.Json { - 24○
@json.Json::number(n.to_double()) - 25
} - 26
- 27
///| - 28
fn jbool(b : Bool) -> @json.Json { - 29○
@json.Json::boolean(b) - 30
} - 31
- 32
///| - 33
fn format_name(f : @types.ImageFormat) -> String { - 34○
match f { - 35○
@types.ImageFormat::Png => "png" - 36○
@types.ImageFormat::Jpeg => "jpeg" - 37○
@types.ImageFormat::Bmp => "bmp" - 38○
@types.ImageFormat::Gif => "gif" - 39○
@types.ImageFormat::Webp => "webp" - 40○
@types.ImageFormat::Qoi => "qoi" - 41○
@types.ImageFormat::Pnm => "pnm" - 42○
@types.ImageFormat::Tga => "tga" - 43○
_ => "unknown" - 44
} - 45
} - 46
- 47
///| - 48
fn parse_format_arg(s : String) -> @types.ImageFormat raise @types.ImageError { - 49○
match s.to_lower() { - 50○
"png" => @types.ImageFormat::Png - 51○
"jpeg" | "jpg" => @types.ImageFormat::Jpeg - 52○
"bmp" => @types.ImageFormat::Bmp - 53○
"gif" => @types.ImageFormat::Gif - 54○
"webp" => @types.ImageFormat::Webp - 55○
"qoi" => @types.ImageFormat::Qoi - 56○
"pnm" | "ppm" => @types.ImageFormat::Pnm - 57○
"tga" => @types.ImageFormat::Tga - 58
_ => - 59○
raise @types.ImageError::Process( - 60
@types.ErrorCode::InvalidArgument, - 61
"不支持的输出格式: \{s}(可用 png/jpeg/webp/bmp/qoi/pnm/tga/gif/best)", - 62
) - 63
} - 64
} - 65
- 66
///| - 67
fn b64_decode(text : String) -> Bytes raise @types.ImageError { - 68○
@base64.decode(text) catch { - 69
_ => - 70○
raise @types.ImageError::Decode( - 71
@types.ErrorCode::InvalidArgument, - 72
"base64 解码失败:输入不是合法的 base64", - 73
) - 74
} - 75
} - 76
- 77
///| - 78
fn load(b64 : String) -> (@types.Image, Bytes) raise @types.ImageError { - 79○
let input = b64_decode(b64) - 80○
let img = @lib.decode_with_limits( - 81
input, - 82○
@types.DecodeLimits::default_limits(), - 83
) - 84
(img, input) - 85
} - 86
- 87
///| - 88
fn err_json(e : @types.ImageError) -> String { - 89○
let code = e.code() - 90○
@json.Json::object({ - 91○
"ok": jbool(false), - 92○
"code": jstr(code), - 93○
"message": jstr(e.message()), - 94○
"hint": jstr(@types.image_error_hint(code)), - 95○
}).stringify() - 96
} - 97
- 98
///| - 99
fn ok_meta(img : @types.Image, extra : Out) -> Out { - 100○
extra["ok"] = jbool(true) - 101○
extra["width"] = jnum(img.width.to_int()) - 102○
extra["height"] = jnum(img.height.to_int()) - 103○
extra["channels"] = jnum(img.channels.to_int()) - 104
extra - 105
} - 106
- 107
///| - 108
fn info_impl(b64 : String) -> String raise @types.ImageError { - 109○
let input = b64_decode(b64) - 110○
let (w, h) = @lib.check_decode_budget( - 111
input, - 112○
@types.DecodeLimits::default_limits(), - 113
) - 114○
@json.Json::object({ - 115○
"ok": jbool(true), - 116○
"format": jstr(format_name(@lib.detect_format(input))), - 117○
"width": jnum(w.to_int()), - 118○
"height": jnum(h.to_int()), - 119○
"bytes": jnum(input.length()), - 120○
}).stringify() - 121
} - 122
- 123
///| - 124
fn convert_impl( - 125
b64 : String, - 126
format : String, - 127
quality : Int32, - 128
) -> String raise @types.ImageError { - 129○
let (img, _) = load(b64) - 130○
let enc : @lib.EncodedImage = if format.to_lower() == "best" { - 131○
@lib.encode_best(img, quality) - 132
} else { - 133○
let fmt = parse_format_arg(format) - 134○
let data = @lib.save_image_with_quality(img, fmt, quality) catch { - 135○
e => raise @types.image_error_from_load(e) - 136
} - 137
{ format: fmt, bytes: data, } - 138
} - 139○
let out = ok_meta(img, { - 140○
"format": jstr(format_name(enc.format)), - 141○
"bytes": jnum(enc.bytes.length()), - 142○
"data": jstr(@base64.encode(enc.bytes)), - 143
}) - 144○
@json.Json::object(out).stringify() - 145
} - 146
- 147
///| - 148
fn resize_impl( - 149
b64 : String, - 150
w : Int32, - 151
h : Int32, - 152
mode : String, - 153
format : String, - 154
quality : Int32, - 155
) -> String raise @types.ImageError { - 156○
if w <= 0 || h <= 0 { - 157○
raise @types.ImageError::Process( - 158
@types.ErrorCode::InvalidArgument, - 159
"resize 需要正整数宽高(收到 \{w}x\{h})", - 160
) - 161
} - 162○
let (img, _) = load(b64) - 163
let resized = try { - 164○
if mode.to_lower() == "contain" { - 165○
let fill : Array[Byte] = [] - 166
for _ in 0..<img.channels { - 167○
fill.push(b'\xff') - 168
} - 169○
@transform.resize_to_contain(img, w, h, fill) - 170
} else { - 171○
@transform.resize_to_cover(img, w, h) - 172
} - 173
} catch { - 174○
e => raise @types.image_error_from_load(e) - 175
} - 176○
let enc : @lib.EncodedImage = if format.to_lower() == "best" { - 177○
@lib.encode_best(resized, quality) - 178
} else { - 179○
let fmt = parse_format_arg(format) - 180○
let data = @lib.save_image_with_quality(resized, fmt, quality) catch { - 181○
e => raise @types.image_error_from_load(e) - 182
} - 183
{ format: fmt, bytes: data, } - 184
} - 185○
let out = ok_meta(resized, { - 186○
"format": jstr(format_name(enc.format)), - 187○
"bytes": jnum(enc.bytes.length()), - 188○
"data": jstr(@base64.encode(enc.bytes)), - 189
}) - 190○
@json.Json::object(out).stringify() - 191
} - 192
- 193
///| - 194
#export_name("image_info") - 195
pub fn image_info(b64 : String) -> String { - 196○
info_impl(b64) catch { - 197○
e => err_json(e) - 198
} - 199
} - 200
- 201
///| - 202
#export_name("image_convert") - 203
pub fn image_convert(b64 : String, format : String, quality : Int32) -> String { - 204○
convert_impl(b64, format, quality) catch { - 205○
e => err_json(e) - 206
} - 207
} - 208
- 209
///| - 210
#export_name("image_resize") - 211
pub fn image_resize( - 212
b64 : String, - 213
w : Int32, - 214
h : Int32, - 215
mode : String, - 216
format : String, - 217
quality : Int32, - 218
) -> String { - 219○
resize_impl(b64, w, h, mode, format, quality) catch { - 220○
e => err_json(e) - 221
} - 222
}