Gathering detailed insights and metrics for @unspezifisch/event-source-polyfill
Gathering detailed insights and metrics for @unspezifisch/event-source-polyfill
Gathering detailed insights and metrics for @unspezifisch/event-source-polyfill
Gathering detailed insights and metrics for @unspezifisch/event-source-polyfill
npm install @unspezifisch/event-source-polyfill
Typescript
Module System
Node Version
NPM Version
73.6
Supply Chain
98.9
Quality
75
Maintenance
100
Vulnerability
100
License
Total Downloads
322
Last Day
1
Last Week
2
Last Month
9
Last Year
87
Minified
Minified + Gzipped
Latest Version
1.0.30
Package Id
@unspezifisch/event-source-polyfill@1.0.30
Unpacked Size
55.24 kB
Size
15.80 kB
File Count
6
NPM Version
8.5.5
Node Version
16.15.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
-30.8%
9
Compared to previous month
Last Year
29.9%
87
Compared to previous year
3
You can get the code from npm or bower:
npm install event-source-polyfill
bower install event-source-polyfill
Just include src/eventsource.js
or src/eventsource.min.js
in your page to use the polyfill.
Unless a typescript definition file is created for this polyfill, this is how you would use it in an Ionic2 project. It should (in theory) be very similar in an Angular2 project.
npm install event-source-polyfill
Add to (or create) src/app/polyfills.ts (path is relative to where polyfills.ts is) :
import 'path/to/event-source-polyfill/src/eventsource.min.js'
Add anywhere you need access to EventSourcePolyfill class :
declare var EventSourcePolyfill: any;
1import { NativeEventSource, EventSourcePolyfill } from 'event-source-polyfill'; 2 3const EventSource = NativeEventSource || EventSourcePolyfill; 4// OR: may also need to set as global property 5global.EventSource = NativeEventSource || EventSourcePolyfill;
padding=true
query argument)npm install
) and then run the build (npm run build
). It should generate a new version of src/eventsource.min.js
.http://username:password@github.com
.var es = new EventSourcePolyfill('/events', {
headers: {
'X-Custom-Header': 'value'
}
});
lastEventId
var es = new EventSourcePolyfill(hubUrl, {
lastEventIdQueryParameterName: 'Last-Event-Id'
});
1var PORT = 8081; 2 3var http = require("http"); 4var fs = require("fs"); 5var url = require("url"); 6 7http.createServer(function (request, response) { 8 var parsedURL = url.parse(request.url, true); 9 var pathname = parsedURL.pathname; 10 if (pathname === "/events.php") { 11 12 response.writeHead(200, { 13 "Content-Type": "text/event-stream", 14 "Cache-Control": "no-store", 15 "Access-Control-Allow-Origin": "*" 16 }); 17 18 var padding = new Array(2049); 19 response.write(":" + padding.join(" ") + "\n"); // 2kB padding for IE 20 response.write("retry: 2000\n"); 21 22 var lastEventId = Number(request.headers["last-event-id"]) || Number(parsedURL.query.lastEventId) || 0; 23 24 var timeoutId = 0; 25 var i = lastEventId; 26 var c = i + 100; 27 var f = function () { 28 if (++i < c) { 29 response.write("id: " + i + "\n"); 30 response.write("data: " + i + "\n\n"); 31 timeoutId = setTimeout(f, 1000); 32 } else { 33 response.end(); 34 } 35 }; 36 37 f(); 38 39 response.on("close", function () { 40 clearTimeout(timeoutId); 41 }); 42 43 } else { 44 if (pathname === "/") { 45 pathname = "/index.html"; 46 } 47 if (pathname === "/index.html" || pathname === "../src/eventsource.js") { 48 response.writeHead(200, { 49 "Content-Type": pathname === "/index.html" ? "text/html" : "text/javascript" 50 }); 51 response.write(fs.readFileSync(__dirname + pathname)); 52 } 53 response.end(); 54 } 55}).listen(PORT);
1<?php 2 3 header("Content-Type: text/event-stream"); 4 header("Cache-Control: no-store"); 5 header("Access-Control-Allow-Origin: *"); 6 7 $lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0); 8 if ($lastEventId == 0) { 9 $lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0); 10 } 11 12 echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE 13 echo "retry: 2000\n"; 14 15 // event-stream 16 $i = $lastEventId; 17 $c = $i + 100; 18 while (++$i < $c) { 19 echo "id: " . $i . "\n"; 20 echo "data: " . $i . ";\n\n"; 21 ob_flush(); 22 flush(); 23 sleep(1); 24 } 25 26?>
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8" /> 5 <title>EventSource example</title> 6 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 7 <script src="../src/eventsource.js"></script> 8 <script> 9 var es = new EventSource("events.php"); 10 var listener = function (event) { 11 var div = document.createElement("div"); 12 var type = event.type; 13 div.appendChild(document.createTextNode(type + ": " + (type === "message" ? event.data : es.url))); 14 document.body.appendChild(div); 15 }; 16 es.addEventListener("open", listener); 17 es.addEventListener("message", listener); 18 es.addEventListener("error", listener); 19 </script> 20</head> 21<body> 22</body> 23</html>
With some dynamic imports it may work in node.js:
Install the library and the dependency:
npm install @titelmedia/node-fetch
npm install event-source-polyfill
x.js:
1// The @titelmedia/node-fetch is used instead of node-fetch as it supports ReadableStream Web API 2import('@titelmedia/node-fetch').then(function (fetch) { 3 globalThis.fetch = fetch.default; 4 globalThis.Response = fetch.default.Response; 5 import('event-source-polyfill').then(function (x) { 6 var es = new x.default.EventSourcePolyfill('http://localhost:8004/events'); 7 es.onerror = es.onopen = es.onmessage = function (event) { 8 console.log(event.type + ': ' + event.data); 9 }; 10 }); 11});
node --experimental-modules ./x.js
The MIT License (MIT)
Copyright (c) 2012 vic99999@yandex.ru
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
No security vulnerabilities found.