@@ -2,7 +2,6 @@ import { createServer } from "http";
22import serveHandler from "serve-handler" ;
33import { server as WebSocketServer } from "websocket" ;
44import { watchFile } from "fs" ;
5- import { resolve as pathResolve } from "path" ;
65function originIsAllowed ( origin ) {
76 return true ;
87}
@@ -16,8 +15,6 @@ async function main() {
1615 return mod . auth ;
1716 }
1817 let authFunc = await loadAuthFunc ( ) ;
19- const currentDirectory = pathResolve ( '.' ) ;
20- console . log ( "Current dir (aka . ) is" , currentDirectory ) ;
2118 watchFile ( "./dst/auth.js" , {
2219 persistent : true ,
2320 interval : 4000
@@ -31,7 +28,7 @@ async function main() {
3128 }
3229 } ) ;
3330 httpServer = createServer ( ( req , res ) => {
34- console . log ( req . url ) ;
31+ // console.log(req.url);
3532 if ( req . url . startsWith ( "/api" ) ) {
3633 res . writeHead ( 200 , "success" , {
3734 "Content-Type" : "application/json"
@@ -83,19 +80,26 @@ async function main() {
8380 const storage = subStorageGetOrCreate ( topic ) ;
8481 if ( id !== undefined ) {
8582 idSubsListGetOrCreate ( storage , id ) . add ( ws ) ;
86- console . log ( "Added sub" , topic , id , ws . remoteAddress ) ;
83+ console . log ( `[ sub] ${ topic } : ${ id } -> client` ) ;
8784 }
8885 else {
8986 storage . topicSubscribers . add ( ws ) ;
90- console . log ( "Added sub" , topic , ws . remoteAddress ) ;
87+ console . log ( `[ sub] ${ topic } -> client` ) ;
9188 }
9289 }
9390 function walkSubscribers ( topic , id = undefined , cb ) {
9491 const storage = subStorageGetOrCreate ( topic ) ;
95- let list = id === undefined ?
96- storage . topicSubscribers :
97- idSubsListGetOrCreate ( storage , id ) ;
98- for ( const ws of list ) {
92+ //call ID specific listeners first if present
93+ if ( id !== undefined ) {
94+ const idSubs = storage . idSubScribers . get ( id ) ;
95+ if ( idSubs ) {
96+ for ( const ws of idSubs ) {
97+ cb ( ws ) ;
98+ }
99+ }
100+ }
101+ //call topic subscribers always if present
102+ for ( const ws of storage . topicSubscribers ) {
99103 cb ( ws ) ;
100104 }
101105 }
@@ -137,23 +141,25 @@ async function main() {
137141 res . error = `Invalid auth to create schema` ;
138142 }
139143 else {
140- console . log ( " schema-set" , topic ) ;
144+ console . log ( `[ schema] created " ${ topic } "` ) ;
141145 schemas . set ( topic , {
142146 shape,
143147 instances : new Map ( )
144148 } ) ;
145149 }
146150 }
147151 break ;
148- case "schema-get" : {
149- let { topic } = req . msg ;
150- const schema = schemas . get ( topic ) ;
151- if ( ! schema ) {
152- res . error = "no schema for topic" ;
153- break ;
152+ case "schema-get" :
153+ {
154+ let { topic } = req . msg ;
155+ const schema = schemas . get ( topic ) ;
156+ if ( ! schema ) {
157+ res . error = "no schema for topic" ;
158+ break ;
159+ }
160+ res . response . shape = schema . shape ;
154161 }
155- res . response . shape = schema . shape ;
156- }
162+ break ;
157163 case "instance" :
158164 {
159165 const topic = req . msg . topic ;
@@ -165,7 +171,24 @@ async function main() {
165171 const instanceId = Math . floor ( Math . random ( ) * Number . MAX_SAFE_INTEGER ) . toString ( ) ;
166172 res . response . id = instanceId ;
167173 storage . instances . set ( instanceId , { } ) ;
168- // console.log("instance", instanceId);
174+ console . log ( "instance req" , req ) ;
175+ /**create a message to send to topic subscribers
176+ * to let them know a new instance exists
177+ */
178+ const subInstRes = {
179+ response : {
180+ type : "sub-inst" ,
181+ topic,
182+ id : instanceId
183+ } ,
184+ id : - 1
185+ } ;
186+ const subInstResStr = JSON . stringify ( subInstRes ) ;
187+ console . log ( `[schema] instanced "${ topic } :${ instanceId } "` ) ;
188+ walkSubscribers ( topic , undefined , ( ws ) => {
189+ console . log ( "Notifying" , ws . remoteAddress , "of instance" , instanceId ) ;
190+ ws . send ( subInstResStr ) ;
191+ } ) ;
169192 }
170193 break ;
171194 case "mut" :
@@ -210,11 +233,9 @@ async function main() {
210233 } ,
211234 id : - 1
212235 } ;
213- // console.log("received mutate", change);
214236 const subResStr = JSON . stringify ( subRes ) ;
215237 walkSubscribers ( topic , id , ( ws ) => {
216238 ws . send ( subResStr ) ;
217- // console.log("Send mut to", subResStr, ws.remoteAddress);
218239 } ) ;
219240 }
220241 break ;
@@ -228,17 +249,19 @@ async function main() {
228249 res . error = "unsub is not impl yet" ;
229250 break ;
230251 case "list" :
231- const topic = req . msg . topic ;
232- const storage = schemas . get ( topic ) ;
233- if ( ! storage ) {
234- res . error = `schema for topic was not found` ;
235- break ;
252+ {
253+ const topic = req . msg . topic ;
254+ const storage = schemas . get ( topic ) ;
255+ if ( ! storage ) {
256+ res . error = `schema for topic was not found` ;
257+ break ;
258+ }
259+ const list = { } ;
260+ storage . instances . forEach ( ( v , k ) => {
261+ list [ k ] = v ;
262+ } ) ;
263+ res . response . list = list ;
236264 }
237- const list = { } ;
238- storage . instances . forEach ( ( v , k ) => {
239- list [ k ] = v ;
240- } ) ;
241- res . response . list = list ;
242265 break ;
243266 }
244267 let str = JSON . stringify ( res ) ;
0 commit comments