@@ -46,10 +46,11 @@ const closeClients = () => {
46
46
} ;
47
47
48
48
class Client {
49
- constructor ( req , res , application ) {
49
+ constructor ( req , res , application , connection ) {
50
50
this . req = req ;
51
51
this . res = res ;
52
52
this . application = application ;
53
+ this . connection = connection ;
53
54
}
54
55
55
56
static ( ) {
@@ -79,9 +80,8 @@ class Client {
79
80
}
80
81
const { req, res } = this ;
81
82
const { url } = req ;
82
- const ip = req . connection . remoteAddress ;
83
83
const name = url . substring ( METHOD_OFFSET ) ;
84
- const session = await this . application . auth . restore ( req ) ;
84
+ const session = await this . application . auth . restore ( this ) ;
85
85
const args = await receiveArgs ( req ) ;
86
86
const sandbox = session ? session . sandbox : undefined ;
87
87
const context = session ? session . context : { } ;
@@ -100,8 +100,7 @@ class Client {
100
100
return ;
101
101
}
102
102
if ( ! session && access === 'public' ) {
103
- const session = this . application . auth . start ( req , ip , result . userId ) ;
104
- res . setHeader ( 'Set-Cookie' , session . cookie ) ;
103
+ this . application . auth . start ( this , result . userId ) ;
105
104
}
106
105
res . end ( JSON . stringify ( result ) ) ;
107
106
} catch ( err ) {
@@ -141,7 +140,8 @@ const listener = application => (req, res) => {
141
140
}
142
141
} ;
143
142
144
- const apiws = ( application , connection , req ) => async message => {
143
+ const apiws = async ( client , message ) => {
144
+ const { connection, application } = client ;
145
145
const { semaphore } = application . server ;
146
146
const send = obj => connection . send ( JSON . stringify ( obj ) ) ;
147
147
try {
@@ -151,9 +151,8 @@ const apiws = (application, connection, req) => async message => {
151
151
return ;
152
152
}
153
153
try {
154
- const ip = req . connection . remoteAddress ;
155
154
const { method : name , args } = JSON . parse ( message ) ;
156
- const session = await application . auth . restore ( req ) ;
155
+ const session = await application . auth . restore ( client ) ;
157
156
const sandbox = session ? session . sandbox : undefined ;
158
157
const context = session ? session . context : { } ;
159
158
const exp = application . runScript ( name , sandbox ) ;
@@ -166,8 +165,8 @@ const apiws = (application, connection, req) => async message => {
166
165
}
167
166
const result = await method ( args ) ;
168
167
if ( ! session && access === 'public' ) {
169
- const session = application . auth . start ( req , ip , result . userId ) ;
170
- result . token = session . cookie ;
168
+ const session = application . auth . start ( client , result . userId ) ;
169
+ result . token = session . token ;
171
170
}
172
171
send ( result ) ;
173
172
} catch ( err ) {
@@ -191,7 +190,10 @@ class Server {
191
190
if ( transport . startsWith ( 'ws' ) ) {
192
191
this . ws = new WebSocket . Server ( { server : this . instance } ) ;
193
192
this . ws . on ( 'connection' , ( connection , req ) => {
194
- connection . on ( 'message' , apiws ( application , connection , req ) ) ;
193
+ const client = new Client ( req , null , application , connection ) ;
194
+ connection . on ( 'message' , message => {
195
+ apiws ( client , message ) ;
196
+ } ) ;
195
197
} ) ;
196
198
}
197
199
this . instance . listen ( port , host ) ;
0 commit comments