Skip to content

Commit 5cdd145

Browse files
committed
upgrade: moon 0.9.3+67df401b9
1 parent dc30233 commit 5cdd145

10 files changed

Lines changed: 69 additions & 42 deletions

File tree

cors/moon.pkg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import {
22
"oboard/mocket",
33
}
4+
5+
// Suppress warning 20 from MoonBit's generated native test driver.
6+
7+
warnings = "-20"

dispatch.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub async fn dispatch_http(
1717
}
1818
let responder = mocket.execute_middlewares(event, handler)
1919
responder.options(event.res)
20-
let buf = @buffer.new()
20+
let buf = Buffer()
2121
responder.output(buf)
2222
event.res.raw_body = buf.to_bytes()
2323
event.res

index.mbt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(all) enum SameSiteOption {
5353
} derive(Eq)
5454

5555
///|
56-
pub impl Show for SameSiteOption with output(self, logger) -> Unit {
56+
pub impl Show for SameSiteOption with fn output(self, logger) -> Unit {
5757
match self {
5858
Lax => logger.write_string("Lax")
5959
Strict => logger.write_string("Strict")
@@ -62,7 +62,7 @@ pub impl Show for SameSiteOption with output(self, logger) -> Unit {
6262
}
6363

6464
///|
65-
pub impl ToJson for SameSiteOption with to_json(self : SameSiteOption) -> Json {
65+
pub impl ToJson for SameSiteOption with fn to_json(self : SameSiteOption) -> Json {
6666
match self {
6767
Lax => "lax"
6868
Strict => "strict"
@@ -83,7 +83,7 @@ pub(all) struct CookieItem {
8383
} derive(Eq)
8484

8585
///|
86-
pub impl Show for CookieItem with output(self, logger) -> Unit {
86+
pub impl Show for CookieItem with fn output(self, logger) -> Unit {
8787
logger.write_string(self.name)
8888
logger.write_char('=')
8989
logger.write_string(self.value)
@@ -112,8 +112,8 @@ pub impl Show for CookieItem with output(self, logger) -> Unit {
112112
}
113113

114114
///|
115-
pub impl Show for CookieItem with to_string(self : CookieItem) -> String {
116-
let buf = @buffer.new()
115+
pub impl Show for CookieItem with fn to_string(self : CookieItem) -> String {
116+
let buf = StringBuilder::new()
117117
self.output(buf)
118118
buf.to_string()
119119
}

js/moon.pkg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import {
33
"moonbitlang/core/test",
44
}
55

6+
// Suppress warning 20 from MoonBit's generated native test driver.
7+
8+
warnings = "-20"
9+
610
options(
711
targets: {
812
"async.mbt": [ "js" ],

mocket.js.mbt

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ extern "js" fn HttpRequestInternal::on(
3333
handler : (@js.Value) -> Unit,
3434
) -> Unit = "(s, event, handler) => s.on(event, handler)"
3535

36+
///|
37+
#borrow(data)
38+
extern "js" fn node_body_chunk_to_bytes(data : @js.Value) -> Bytes =
39+
#| (data) => {
40+
#| if (data instanceof Uint8Array) {
41+
#| return new Uint8Array(data);
42+
#| }
43+
#| return new TextEncoder().encode(String(data));
44+
#| }
45+
3646
///|
3747
#borrow(self)
3848
extern "js" fn HttpRequestInternal::headers(
@@ -329,10 +339,12 @@ pub fn listen_ffi(mocket : Mocket, address : String) -> Unit {
329339
async_run(() => {
330340
// 如果是 post,先等待 data 事件
331341
if event.req.http_method == "POST" {
332-
let buffer = @buffer.new()
342+
let buffer = Buffer()
333343
(try? suspend(fn(res, _) {
334-
req.on("data", data => buffer.write_string(data.to_string()))
335-
req.on("end", _ => res(buffer.to_string()))
344+
req.on("data", data => {
345+
buffer.write_bytes(node_body_chunk_to_bytes(data))
346+
})
347+
req.on("end", _ => res(()))
336348
}))
337349
|> ignore
338350
event.req.raw_body = buffer.to_bytes()
@@ -358,7 +370,7 @@ pub fn listen_ffi(mocket : Mocket, address : String) -> Unit {
358370
headers_obj
359371
},
360372
)
361-
let buf = @buffer.new()
373+
let buf = Buffer()
362374
responder.output(buf)
363375
event.res.raw_body = buf.to_bytes()
364376
res.end(@js.Value::cast_from(event.res.raw_body))
@@ -446,14 +458,14 @@ pub fn __ws_emit_js_port(
446458
}
447459
let peer = WebSocketPeer::{ connection_id, subscribed_channels: [] }
448460
match event_type {
449-
"open" => handler(WebSocketEvent::Open(peer))
461+
"open" => handler(Open(peer))
450462
"message" => {
451463
let msg = @utf8.decode(payload) catch { _ => "" }
452-
handler(WebSocketEvent::Message(peer, Text(msg)))
464+
handler(Message(peer, Text(msg)))
453465
}
454-
"binary" => handler(WebSocketEvent::Message(peer, Binary(payload)))
455-
"ping" => handler(WebSocketEvent::Message(peer, Ping))
456-
"close" => handler(WebSocketEvent::Close(peer))
466+
"binary" => handler(Message(peer, Binary(payload)))
467+
"ping" => handler(Message(peer, Ping))
468+
"close" => handler(Close(peer))
457469
_ => ()
458470
}
459471
}

moon.mod

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name = "oboard/mocket"
2+
3+
version = "0.7.4"
4+
5+
import {
6+
"moonbitlang/async@0.19.1",
7+
"moonbitlang/x@0.4.43",
8+
}
9+
10+
readme = "README.md"
11+
12+
repository = "https://github.com/oboard/mocket"
13+
14+
license = "Apache-2.0"
15+
16+
keywords = [ "http", "server" ]
17+
18+
description = "A web framework for MoonBit."
19+
20+
preferred_target = "native"

moon.mod.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

pkg.generated.mbti

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub type WebSocketHandler = (WebSocketEvent) -> Unit
304304

305305
// Traits
306306
pub(open) trait BodyReader {
307-
from_request(HttpRequest) -> Self raise
307+
fn from_request(HttpRequest) -> Self raise
308308
}
309309
pub impl BodyReader for String
310310
pub impl BodyReader for FixedArray[Byte]
@@ -313,8 +313,8 @@ pub impl BodyReader for Array[Byte]
313313
pub impl BodyReader for Json
314314

315315
pub(open) trait Responder {
316-
options(Self, HttpResponse) -> Unit
317-
output(Self, @buffer.Buffer) -> Unit
316+
fn options(Self, HttpResponse) -> Unit
317+
fn output(Self, @buffer.Buffer) -> Unit
318318
}
319319
pub impl Responder for String
320320
pub impl Responder for Bytes
@@ -323,11 +323,11 @@ pub impl Responder for &ToJson
323323
pub impl Responder for StringView
324324

325325
pub(open) trait ServeStaticProvider {
326-
get_meta(Self, String) -> StaticAssetMeta?
327-
get_contents(Self, String) -> &Responder
328-
get_type(Self, String) -> String?
329-
get_encodings(Self) -> Map[String, String]
330-
get_index_names(Self) -> Array[String]
331-
get_fallthrough(Self) -> Bool
326+
fn get_meta(Self, String) -> StaticAssetMeta?
327+
fn get_contents(Self, String) -> &Responder
328+
fn get_type(Self, String) -> String?
329+
fn get_encodings(Self) -> Map[String, String]
330+
fn get_index_names(Self) -> Array[String]
331+
fn get_fallthrough(Self) -> Bool
332332
}
333333

response.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn HttpResponse::body(
3636
self : HttpResponse,
3737
body : &Responder,
3838
) -> HttpResponse {
39-
let buf = @buffer.new()
39+
let buf = Buffer()
4040
body.output(buf)
4141
self.raw_body = buf.to_bytes()
4242
self

static_file/moon.pkg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ import {
22
"oboard/mocket",
33
"moonbitlang/x/fs",
44
}
5+
6+
// Suppress warning 20 from MoonBit's generated native test driver.
7+
8+
warnings = "-20"

0 commit comments

Comments
 (0)