statebus 6.1.4 → 6.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/server.js +39 -8
  3. package/statebus.js +2 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "6.1.4",
3
+ "version": "6.1.8",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/server.js CHANGED
@@ -99,24 +99,55 @@ function import_server (bus, options)
99
99
  // User will put their routes in here
100
100
  bus.express.use('/', bus.http)
101
101
 
102
+ // Use braidify if it's available
103
+ try {
104
+ var braidify = require('braidify').http_server
105
+ console.log('Found braidify library. Braid-HTTP enabled!')
106
+ } catch (e) {
107
+ var braidify = undefined
108
+ }
109
+
102
110
  // Add a fallback that goes to state
103
111
  bus.express.get('*', function (req, res) {
104
112
  // Make a temporary client bus
105
113
  var cbus = bus.bus_for_http_client(req, res)
114
+ var cb
115
+ function end_it_all () {
116
+ cbus.forget(key, cb)
117
+ cbus.delete_bus()
118
+ }
119
+
120
+ braidify && braidify(req, res)
121
+ if (req.subscribe) {
122
+ res.startSubscription({ onClose: end_it_all })
123
+ console.log('yay subscription!')
124
+ } else
125
+ res.statusCode = 200
106
126
 
107
127
  // Do the fetch
108
128
  cbus.honk = 'statelog'
109
- var singleton = req.path.match(/^\/code\//)
110
- cbus.fetch_once(req.path.substr(1), (o) => {
111
- var unwrap = (Object.keys(o).length === 2
112
- && '_' in o
113
- && typeof o._ === 'string')
114
- // To do: translate pointers as keys
115
- res.send(unwrap ? o._ : JSON.stringify(o))
116
- cbus.delete_bus()
129
+ var key = req.path.substr(1)
130
+ cbus.fetch(key, cb = (o) => {
131
+ if (braidify)
132
+ res.sendVersion({body: bus.to_http_body(o)})
133
+ else
134
+ res.send(bus.to_http_body(o))
135
+
136
+ if (!braidify || !req.subscribe)
137
+ end_it_all()
117
138
  })
118
139
  })
119
140
 
141
+ // Set up default linked json converters
142
+ bus.to_http_body = (o) => {
143
+ var unwrap = (Object.keys(o).length === 2
144
+ && '_' in o
145
+ && typeof o._ === 'string')
146
+ // To do: translate pointers as keys
147
+ return unwrap ? o._ : JSON.stringify(o)
148
+ }
149
+ bus.from_http_body = (o) => {}
150
+
120
151
  // Serve Client Coffee
121
152
  bus.serve_client_coffee()
122
153
 
package/statebus.js CHANGED
@@ -2205,6 +2205,8 @@
2205
2205
  console.log.apply(console, arguments)
2206
2206
  }
2207
2207
  function statelog (key, color, icon, message) {
2208
+ if (bus.honking_colors === false)
2209
+ color = ''
2208
2210
  if (honking_at(key))
2209
2211
  indented_log(color + icon + ' ' + message + normal)
2210
2212
  }