@@ -70,36 +70,33 @@ class Client {
70
70
this . res . end ( `HTTP ${ status } : ${ http . STATUS_CODES [ status ] } ` ) ;
71
71
}
72
72
73
- async api ( ) {
74
- const { semaphore } = this . application . server ;
73
+ async apihttp ( method , args ) {
74
+ const { res, application } = this ;
75
+ const { semaphore } = application . server ;
75
76
try {
76
77
await semaphore . enter ( ) ;
77
78
} catch {
78
79
this . error ( 504 ) ;
79
80
return ;
80
81
}
81
- const { req, res } = this ;
82
- const { url } = req ;
83
- const name = url . substring ( METHOD_OFFSET ) ;
84
82
const session = await this . application . auth . restore ( this ) ;
85
- const args = await receiveArgs ( req ) ;
86
83
const sandbox = session ? session . sandbox : undefined ;
87
84
const context = session ? session . context : { } ;
88
85
try {
89
- const exp = this . application . runScript ( name , sandbox ) ;
90
- const { method , access } = exp ( context ) ;
91
- if ( ! session && access !== 'public' ) {
92
- this . application . logger . error ( `Forbidden ${ url } ` ) ;
86
+ const exp = this . application . runScript ( method , sandbox ) ;
87
+ const proc = exp ( context ) ;
88
+ if ( ! session && proc . access !== 'public' ) {
89
+ this . application . logger . error ( `Forbidden ${ method } ` ) ;
93
90
this . error ( 403 ) ;
94
91
semaphore . leave ( ) ;
95
92
return ;
96
93
}
97
- const result = await method ( args ) ;
94
+ const result = await proc . method ( args ) ;
98
95
if ( res . finished ) {
99
96
semaphore . leave ( ) ;
100
97
return ;
101
98
}
102
- if ( ! session && access === 'public' ) {
99
+ if ( ! session && proc . access === 'public' ) {
103
100
this . application . auth . start ( this , result . userId ) ;
104
101
}
105
102
res . end ( JSON . stringify ( result ) ) ;
@@ -109,6 +106,41 @@ class Client {
109
106
}
110
107
semaphore . leave ( ) ;
111
108
}
109
+
110
+ async apiws ( method , args ) {
111
+ const { connection, application } = this ;
112
+ const { semaphore } = application . server ;
113
+ const send = obj => connection . send ( JSON . stringify ( obj ) ) ;
114
+ try {
115
+ await semaphore . enter ( ) ;
116
+ } catch {
117
+ send ( { result : 'error' , reason : 'timeout' } ) ;
118
+ return ;
119
+ }
120
+ try {
121
+ const session = await application . auth . restore ( this ) ;
122
+ const sandbox = session ? session . sandbox : undefined ;
123
+ const context = session ? session . context : { } ;
124
+ const exp = application . runScript ( method , sandbox ) ;
125
+ const proc = exp ( context ) ;
126
+ if ( ! session && proc . access !== 'public' ) {
127
+ application . logger . error ( `Forbidden: ${ method } ` ) ;
128
+ send ( { result : 'error' , reason : 'forbidden' } ) ;
129
+ semaphore . leave ( ) ;
130
+ return ;
131
+ }
132
+ const result = await proc . method ( args ) ;
133
+ if ( ! session && proc . access === 'public' ) {
134
+ const session = application . auth . start ( this , result . userId ) ;
135
+ result . token = session . token ;
136
+ }
137
+ send ( result ) ;
138
+ } catch ( err ) {
139
+ application . logger . error ( err . stack ) ;
140
+ send ( { result : 'error' } ) ;
141
+ }
142
+ semaphore . leave ( ) ;
143
+ }
112
144
}
113
145
114
146
const listener = application => ( req , res ) => {
@@ -133,48 +165,19 @@ const listener = application => (req, res) => {
133
165
134
166
application . logger . log ( `${ method } \t${ url } ` ) ;
135
167
if ( url . startsWith ( '/api/' ) ) {
136
- if ( method === 'POST' ) client . api ( ) ;
137
- else client . error ( 403 ) ;
168
+ if ( method !== 'POST' ) {
169
+ client . error ( 403 ) ;
170
+ return ;
171
+ }
172
+ receiveArgs ( req ) . then ( args => {
173
+ const method = url . substring ( METHOD_OFFSET ) ;
174
+ client . apihttp ( method , args ) ;
175
+ } ) ;
138
176
} else {
139
177
client . static ( ) ;
140
178
}
141
179
} ;
142
180
143
- const apiws = async ( client , message ) => {
144
- const { connection, application } = client ;
145
- const { semaphore } = application . server ;
146
- const send = obj => connection . send ( JSON . stringify ( obj ) ) ;
147
- try {
148
- await semaphore . enter ( ) ;
149
- } catch {
150
- send ( { result : 'error' , reason : 'timeout' } ) ;
151
- return ;
152
- }
153
- try {
154
- const { method : name , args } = JSON . parse ( message ) ;
155
- const session = await application . auth . restore ( client ) ;
156
- const sandbox = session ? session . sandbox : undefined ;
157
- const context = session ? session . context : { } ;
158
- const exp = application . runScript ( name , sandbox ) ;
159
- const { method, access } = exp ( context ) ;
160
- if ( ! session && access !== 'public' ) {
161
- application . logger . error ( `Forbidden: ${ name } ` ) ;
162
- send ( { result : 'error' , reason : 'forbidden' } ) ;
163
- semaphore . leave ( ) ;
164
- return ;
165
- }
166
- const result = await method ( args ) ;
167
- if ( ! session && access === 'public' ) {
168
- const session = application . auth . start ( client , result . userId ) ;
169
- result . token = session . token ;
170
- }
171
- send ( result ) ;
172
- } catch ( err ) {
173
- application . logger . error ( err . stack ) ;
174
- send ( { result : 'error' } ) ;
175
- }
176
- semaphore . leave ( ) ;
177
- } ;
178
181
179
182
class Server {
180
183
constructor ( config , application ) {
@@ -192,7 +195,8 @@ class Server {
192
195
this . ws . on ( 'connection' , ( connection , req ) => {
193
196
const client = new Client ( req , null , application , connection ) ;
194
197
connection . on ( 'message' , message => {
195
- apiws ( client , message ) ;
198
+ const { method, args } = JSON . parse ( message ) ;
199
+ client . apiws ( method , args ) ;
196
200
} ) ;
197
201
} ) ;
198
202
}
0 commit comments