Skip to content

Commit 20173f8

Browse files
author
jin
committed
$mol_rest: refactoring
1 parent d498c27 commit 20173f8

File tree

13 files changed

+343
-195
lines changed

13 files changed

+343
-195
lines changed

object/object.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ namespace $ {
66

77
export class $mol_object extends $mol_object2 {
88

9-
public static make< Instance >( this : { new() : Instance } , config : Partial< Instance > ) : Instance {
9+
public static make< This extends typeof $mol_object >(
10+
this: This,
11+
config: Partial< InstanceType< This > >,
12+
) {
1013
return super.create( obj => {
1114
for( let key in config ) ( obj as any )[ key ] = config[ key ]!
12-
} ) as any
15+
} ) as InstanceType< This >
1316
}
1417

1518
}

object2/object2.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ namespace $ {
4949
return this[ Symbol.toStringTag ] || this.constructor.name + '<>'
5050
}
5151

52-
toJSON(): any {
53-
return this.toString()
54-
}
52+
// toJSON(): any {
53+
// return this.toString()
54+
// }
5555

5656
}
5757
}

rest/channel/http/http.node.ts

-56
This file was deleted.

rest/demo/demo.node.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,5 @@ namespace $ {
4545
@ $mol_mem nested() { return $mol_rest_demo_crud.make({}) }
4646

4747
}
48-
// $mol_rest_demo_crud.serve()
4948

5049
}

rest/message/http/http.node.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace $ {
2+
export class $mol_rest_message_http extends $mol_rest_message {
3+
4+
input!: InstanceType< $node['http']['IncomingMessage'] >
5+
6+
@ $mol_mem
7+
method() {
8+
return this.input.method ?? super.method()
9+
}
10+
11+
@ $mol_mem
12+
uri() {
13+
const addr = this.input.socket?.localAddress ?? 'localhost'
14+
const port = this.input.socket?.localPort ?? '80'
15+
return new URL( this.input.url!, `http://[${addr}]:${port}/` )
16+
}
17+
18+
@ $mol_mem
19+
type() {
20+
return ( this.input.headers['content-type'] ?? 'application/octet-stream' ) as $mol_rest_port_mime
21+
}
22+
23+
@ $mol_mem
24+
data(): null | string | Uint8Array | Element | object {
25+
26+
const consume = $mol_wire_sync( $node['stream/consumers'] )
27+
28+
if( this.type().startsWith( 'text/' ) ) {
29+
30+
const text = consume.text( this.input )
31+
32+
if( this.type() === 'text/html' ) {
33+
return $mol_dom_parse( text, 'application/xhtml+xml' ).documentElement
34+
}
35+
36+
return text
37+
38+
} else {
39+
40+
if( this.type() === 'application/json' ) {
41+
return consume.json( this.input )
42+
} else {
43+
return new Uint8Array( consume.arrayBuffer( this.input ) )
44+
}
45+
46+
}
47+
48+
}
49+
50+
@ $mol_action
51+
route( uri: URL ) {
52+
return $mol_rest_message_http.make({
53+
port: this.port,
54+
input: this.input,
55+
uri: $mol_const( uri ),
56+
data: ()=> this.data(),
57+
})
58+
}
59+
60+
}
61+
}

rest/message/message.node.ts

+30-44
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,28 @@ namespace $ {
22

33
export class $mol_rest_message extends $mol_object {
44

5-
channel(): $mol_rest_channel_http {
6-
return null!
7-
}
8-
9-
input() {
10-
return this.channel().input()
11-
}
5+
port!: $mol_rest_port
126

137
@ $mol_mem
148
method() {
15-
return this.input().method ?? 'POST'
9+
return 'POST'
1610
}
1711

1812
@ $mol_mem
1913
uri() {
20-
const addr = this.input().socket?.localAddress ?? 'localhost'
21-
const port = this.input().socket?.localPort ?? '80'
22-
return new URL( this.input().url!, `http://[${addr}]:${port}/` )
14+
return new URL( `rest://localhost/` )
2315
}
2416

2517
@ $mol_mem
2618
type() {
27-
return ( this.input().headers['content-type'] ?? 'application/octet-stream' ) as $mol_rest_channel_mime
19+
return 'application/octet-stream' as $mol_rest_port_mime
2820
}
2921

3022
@ $mol_mem
31-
data(): string | Uint8Array | object {
32-
33-
const consume = $mol_wire_sync( $node['stream/consumers'] )
34-
35-
if( this.type().startsWith( 'text/' ) ) {
36-
const text = consume.text( this.input() )
37-
if( this.type() === 'text/html' ) {
38-
return $mol_dom_parse( text, 'application/xhtml+xml' ).documentElement
39-
}
40-
return text
41-
} else {
42-
43-
if( this.type() === 'application/json' ) {
44-
return consume.json( this.input() )
45-
} else {
46-
return new Uint8Array( consume.arrayBuffer( this.input() ) )
47-
}
48-
49-
}
50-
23+
data(): null | string | Uint8Array | Element | object {
24+
return null
5125
}
52-
26+
5327
@ $mol_mem
5428
bin() {
5529
let data = this.data()
@@ -71,35 +45,47 @@ namespace $ {
7145
reply(
7246
data: null | string | Uint8Array | Element | object,
7347
meta?: {
74-
type?: $mol_rest_channel_mime,
48+
type?: $mol_rest_port_mime,
7549
code?: $mol_rest_code,
7650
},
7751
) {
78-
const channel = this.channel()
79-
if( meta?.code ) channel.send_code( meta.code )
80-
if( meta?.type ) channel.send_type( meta.type )
81-
channel.send_data( data )
52+
if( meta?.code ) this.port.send_code( meta.code )
53+
if( meta?.type ) this.port.send_type( meta.type )
54+
this.port.send_data( data )
8255
}
8356

8457
@ $mol_action
8558
route( uri: URL ) {
8659
return $mol_rest_message.make({
60+
port: this.port,
61+
method: ()=> this.method(),
8762
uri: $mol_const( uri ),
63+
type: ()=> this.type(),
8864
data: ()=> this.data(),
89-
method: ()=> this.method(),
90-
channel: ()=> this.channel(),
9165
})
9266
}
9367

9468
@ $mol_action
95-
static from(
96-
channel: $mol_rest_channel_http,
69+
derive(
70+
method: string,
71+
data: null | string | Uint8Array | Element | object,
9772
) {
98-
return this.make({
99-
channel: $mol_const( channel ),
73+
return $mol_rest_message.make({
74+
port: this.port,
75+
method: $mol_const( method ),
76+
uri: ()=> this.uri(),
77+
data: $mol_const( data ),
10078
})
10179
}
10280

81+
@ $mol_action< any, any >
82+
public static make< This extends typeof $mol_object >(
83+
this: This,
84+
config: Partial< InstanceType< This > >,
85+
) {
86+
return super.make( config ) as InstanceType< This >
87+
}
88+
10389
}
10490

10591
}

rest/port/http/http.node.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace $ {
2+
3+
export class $mol_rest_port_http extends $mol_rest_port {
4+
5+
output!: InstanceType< $node['http']['ServerResponse'] >
6+
7+
@ $mol_action
8+
send_code( code: $mol_rest_code ) {
9+
if( this.output.writableEnded ) return
10+
if( this.output.statusCode !== 400 ) return
11+
this.output.statusCode = code
12+
}
13+
14+
@ $mol_action
15+
send_type( mime: $mol_rest_port_mime ) {
16+
if( this.output.writableEnded ) return
17+
if( this.output.getHeader( 'content-type' ) ) return
18+
this.output.setHeader( 'content-type', mime )
19+
}
20+
21+
@ $mol_action
22+
send_bin( data: Uint8Array ) {
23+
if( this.output.writableEnded ) return
24+
super.send_bin( data )
25+
this.output.write( data )
26+
}
27+
28+
}
29+
30+
}

rest/channel/channel.ts rest/port/port.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
namespace $ {
22

3-
export type $mol_rest_channel_mime = `${ 'text' | 'application' }/${ string }`
3+
export type $mol_rest_port_mime_hi =
4+
| 'text' | 'application' | 'font'
5+
| 'audio' | 'video' | 'image' | 'model'
46

5-
export class $mol_rest_channel extends $mol_object {
7+
export type $mol_rest_port_mime = `${ $mol_rest_port_mime_hi }/${ string }`
8+
9+
export class $mol_rest_port extends $mol_object {
610

711
send_code( code: $mol_rest_code ) {}
8-
send_type( mime: $mol_rest_channel_mime ) {}
12+
send_type( mime: $mol_rest_port_mime ) {}
913

1014
@ $mol_action
1115
send_data( data: null | string | Uint8Array | Element | object ) {
@@ -48,6 +52,14 @@ namespace $ {
4852
this.send_text( $mol_dom_serialize( data ) )
4953
}
5054

55+
@ $mol_action< any, any >
56+
public static make< This extends typeof $mol_object >(
57+
this: This,
58+
config: Partial< InstanceType< This > >,
59+
) {
60+
return super.make( config ) as InstanceType< This >
61+
}
62+
5163
}
5264

5365
}

rest/port/webrtc/webrtc.node.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace $ {
2+
3+
export class $mol_rest_port_webrtc extends $mol_rest_port {
4+
5+
channel!: InstanceType< typeof import( 'node-datachannel/polyfill' ).RTCDataChannel >
6+
7+
@ $mol_action
8+
send_bin( data: Uint8Array ) {
9+
if( this.channel.readyState !== "open" ) return
10+
this.channel.send( data )
11+
}
12+
13+
@ $mol_action
14+
send_text( data: string ) {
15+
if( this.channel.readyState !== "open" ) return
16+
this.channel.send( data )
17+
}
18+
19+
}
20+
21+
}

rest/port/ws/ws.node.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace $ {
2+
3+
export class $mol_rest_port_ws extends $mol_rest_port {
4+
5+
socket!: InstanceType< $node['stream']['Duplex'] >
6+
7+
@ $mol_action
8+
send_nil() {
9+
if( this.socket.writableEnded ) return
10+
this.socket.write( $mol_websocket_frame.make( 'pong', 0 ).asArray() )
11+
}
12+
13+
@ $mol_action
14+
send_bin( data: Uint8Array ) {
15+
if( this.socket.writableEnded ) return
16+
this.socket.write( $mol_websocket_frame.make( 'bin', data.byteLength ).asArray() )
17+
this.socket.write( data )
18+
}
19+
20+
@ $mol_action
21+
send_text( data: string ) {
22+
if( this.socket.writableEnded ) return
23+
const bin = $mol_charset_encode( data )
24+
this.socket.write( $mol_websocket_frame.make( 'txt', bin.byteLength ).asArray() )
25+
this.socket.write( bin )
26+
}
27+
28+
}
29+
30+
}

0 commit comments

Comments
 (0)