Skip to content

Commit 67d16e6

Browse files
committed
Replace sse library with sse-channel
1 parent f5804b1 commit 67d16e6

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,16 @@ sse.addEventListener('micro-analytics-ping', function (e) { console.log('[sse]',
100100
#### Browser support
101101

102102
Server-sent events is not supported in all browsers. This can easily be fixed by using a polyfill.
103-
Take a look at [the caniuse table][] for server-sent events if you need one.
103+
Take a look at [the caniuse table][] for server-sent events if you need one. Polyfills that are
104+
supported(disclaimer this list is from the documentation of the sse library we use [rexxars/sse-channel][]):
105+
106+
* [amvtek/EventSource](https://github.com/amvtek/EventSource)
107+
* [Yaffle/EventSource)](https://github.com/Yaffle/EventSource)
108+
* [remy/polyfills/EventSource.js](https://github.com/remy/polyfills/blob/master/EventSource.js)
104109

105110
[server-sent events]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
106111
[the caniuse table]: http://caniuse.com/#feat=eventsource
112+
[rexxars/sse-channel]: https://github.com/rexxars/sse-channel
107113

108114
## License
109115

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"micro-analytics-adapter-flat-file-db": "^1.2.1",
3232
"promise": "^7.1.1",
3333
"shelljs": "^0.7.6",
34-
"sse": "0.0.6",
34+
"sse-channel": "^2.0.6",
3535
"update-notifier": "^1.0.3",
3636
"zen-observable": "^0.4.0"
3737
},

src/handler.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@ const { send, createError, sendError } = require('micro')
44
const db = require('./db')
55
const { pushView } = require('./utils')
66

7+
let sse;
8+
9+
if (db.hasFeature("subscribe")) {
10+
const SseChannel = require('sse-channel')
11+
const sseHandler = require('./sse')
12+
sse = new SseChannel({ cors: { origins: ['*'] } })
13+
sseHandler(sse)
14+
}
15+
716
module.exports = async function (req, res) {
817
const { pathname, query } = url.parse(req.url, /* parseQueryString */ true)
18+
19+
if (pathname === '/sse') {
20+
if (sse) {
21+
sse.addClient(req, res);
22+
} else {
23+
send(res, 400, {error: 'The current database adapter does not support live updates.'})
24+
}
25+
}
26+
927
res.setHeader('Access-Control-Allow-Origin', '*')
1028
// Send all views down if "?all" is true
1129
if (String(query.all) === 'true') {

src/index.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const micro = require('micro')
2-
const SSE = require('sse')
32

43
const parseArgs = require('./parseArgs')
54
const db = require('./db');
@@ -11,12 +10,6 @@ db.initDbAdapter(flags.adapter)
1110
const handler = require('./handler')
1211
const server = micro(handler)
1312

14-
if (db.hasFeature("subscribe")) {
15-
const sseHandler = require('./sse')
16-
const sse = new SSE(server)
17-
sse.on('connection', sseHandler)
18-
}
19-
2013
server.listen(flags.port, flags.host, (error) => {
2114
if (error) {
2215
console.error(error)

yarn.lock

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ abbrev@1:
1010
version "1.1.0"
1111
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f"
1212

13+
14+
version "1.0.0"
15+
resolved "https://registry.yarnpkg.com/access-control/-/access-control-1.0.0.tgz#aeba282cee77313e85240163d69e35b29e36d626"
16+
dependencies:
17+
millisecond "0.1.x"
18+
setheader "0.0.x"
19+
vary "1.1.x"
20+
1321
acorn-globals@^3.1.0:
1422
version "3.1.0"
1523
resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-3.1.0.tgz#fd8270f71fbb4996b004fa880ee5d46573a731bf"
@@ -671,6 +679,10 @@ dashdash@^1.12.0:
671679
dependencies:
672680
assert-plus "^1.0.0"
673681

682+
683+
version "0.7.4"
684+
resolved "https://registry.yarnpkg.com/debug/-/debug-0.7.4.tgz#06e1ea8082c2cb14e39806e22e2f6f757f92af39"
685+
674686
debug@^2.1.1, debug@^2.2.0:
675687
version "2.6.3"
676688
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.3.tgz#0f7eb8c30965ec08c72accfa0130c8b79984141d"
@@ -1138,6 +1150,10 @@ imurmurhash@^0.1.4:
11381150
version "0.1.4"
11391151
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
11401152

1153+
in-publish@^2.0.0:
1154+
version "2.0.0"
1155+
resolved "https://registry.yarnpkg.com/in-publish/-/in-publish-2.0.0.tgz#e20ff5e3a2afc2690320b6dc552682a9c7fadf51"
1156+
11411157
infinity-agent@^2.0.0:
11421158
version "2.0.3"
11431159
resolved "https://registry.yarnpkg.com/infinity-agent/-/infinity-agent-2.0.3.tgz#45e0e2ff7a9eb030b27d62b74b3744b7a7ac4216"
@@ -1763,7 +1779,7 @@ lodash.restparam@^3.0.0:
17631779
version "3.6.1"
17641780
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
17651781

1766-
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.2.0:
1782+
lodash@^4.0.1, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.2.0:
17671783
version "4.17.4"
17681784
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
17691785

@@ -1848,6 +1864,10 @@ micromatch@^2.1.5, micromatch@^2.3.11:
18481864
parse-glob "^3.0.4"
18491865
regex-cache "^0.4.2"
18501866

1867+
1868+
version "0.1.2"
1869+
resolved "https://registry.yarnpkg.com/millisecond/-/millisecond-0.1.2.tgz#6cc5ad386241cab8e78aff964f87028eec92dac5"
1870+
18511871
mime-db@~1.27.0:
18521872
version "1.27.0"
18531873
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.27.0.tgz#820f572296bbd20ec25ed55e5b5de869e5436eb1"
@@ -2038,10 +2058,6 @@ optionator@^0.8.1:
20382058
type-check "~0.3.2"
20392059
wordwrap "~1.0.0"
20402060

2041-
options@latest:
2042-
version "0.0.6"
2043-
resolved "https://registry.yarnpkg.com/options/-/options-0.0.6.tgz#ec22d312806bb53e731773e7cdaefcf1c643128f"
2044-
20452061
os-homedir@^1.0.0:
20462062
version "1.0.2"
20472063
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
@@ -2437,6 +2453,12 @@ set-immediate-shim@^1.0.1:
24372453
version "1.0.1"
24382454
resolved "https://registry.yarnpkg.com/set-immediate-shim/-/set-immediate-shim-1.0.1.tgz#4b2b1b27eb808a9f8dcc481a58e5e56f599f3f61"
24392455

2456+
2457+
version "0.0.4"
2458+
resolved "https://registry.yarnpkg.com/setheader/-/setheader-0.0.4.tgz#926ed28cf762149620931e7aea3f1b95816ec694"
2459+
dependencies:
2460+
debug "0.7.x"
2461+
24402462
shelljs@^0.7.6:
24412463
version "0.7.7"
24422464
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.7.tgz#b2f5c77ef97148f4b4f6e22682e10bba8667cff1"
@@ -2513,11 +2535,13 @@ sprintf-js@~1.0.2:
25132535
version "1.0.3"
25142536
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
25152537

2516-
sse@0.0.6:
2517-
version "0.0.6"
2518-
resolved "https://registry.yarnpkg.com/sse/-/sse-0.0.6.tgz#3192461dfa38c78424dd9bf8ea025619a125aa10"
2538+
sse-channel@^2.0.6:
2539+
version "2.0.6"
2540+
resolved "https://registry.yarnpkg.com/sse-channel/-/sse-channel-2.0.6.tgz#311cc45efb21628f4e4989c9ceb976bb47835592"
25192541
dependencies:
2520-
options latest
2542+
access-control "1.0.0"
2543+
in-publish "^2.0.0"
2544+
lodash "^4.0.1"
25212545

25222546
sshpk@^1.7.0:
25232547
version "1.11.0"
@@ -2786,6 +2810,10 @@ validate-npm-package-license@^3.0.1:
27862810
spdx-correct "~1.0.0"
27872811
spdx-expression-parse "~1.0.0"
27882812

2813+
2814+
version "1.1.1"
2815+
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.1.tgz#67535ebb694c1d52257457984665323f587e8d37"
2816+
27892817
27902818
version "1.3.6"
27912819
resolved "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c"

0 commit comments

Comments
 (0)