UNPKG

@browser-bunyan/server-stream

Version:

Bunyan stream for batching and sending log records to a server

1 lines 6.81 kB
{"version":3,"file":"index.cjs","sources":["../src/index.js"],"sourcesContent":["const userAgent = typeof window !== 'undefined' ? window.navigator.userAgent : 'no-window';\nconst isBot = /bot|crawler|spider|crawling/i.test(userAgent);\n\nconst defaultHeaders = { 'Content-Type': 'application/json' };\n\nexport class ServerStream {\n\n constructor(opts = {}) {\n const {\n method = 'PUT',\n url = '/log',\n headers = {},\n throttleInterval = 3000,\n withCredentials = false,\n onError,\n flushOnClose = false,\n writeCondition = ServerStream.defaultWriteCondition,\n } = opts;\n\n this.writeCondition = writeCondition;\n this.records = {};\n\n this.headers = { ...defaultHeaders, ...headers };\n\n this.start({ method, url, throttleInterval, withCredentials, onError });\n\n if (flushOnClose && typeof window.fetch !== 'undefined') {\n // TODO: look at page visibility api here\n // https://github.com/GoogleChromeLabs/page-lifecycle\n window.addEventListener('beforeunload', () => {\n if (this.currentThrottleTimeout) {\n window.clearTimeout(this.currentThrottleTimeout);\n }\n const recs = this.recordsAsArray();\n if (recs.length) {\n // swapped sendBeacon for fetch as discussed here:\n // https://stackoverflow.com/questions/55421459/http-request-beforeunload-sendbeacon-vs-img-src\n window.fetch(url, {\n method,\n body: JSON.stringify(recs),\n headers: this.headers,\n credentials: withCredentials ? 'include' : 'same-origin',\n mode: 'cors',\n keepalive: true,\n });\n }\n }, false);\n }\n }\n\n start({ method, url, throttleInterval, withCredentials, onError }) {\n const throttleRequests = () => {\n // wait for any errors to accumulate\n this.currentThrottleTimeout = setTimeout(() => {\n const recs = this.recordsAsArray();\n if (recs.length) {\n const xhr = new XMLHttpRequest();\n xhr.onreadystatechange = () => {\n if (xhr.readyState === XMLHttpRequest.DONE) {\n if (xhr.status >= 400) {\n if (typeof onError === 'function') {\n onError.call(this, recs, xhr);\n } else {\n console.warn('Browser Bunyan: A server log write failed');\n }\n }\n this.records = {};\n throttleRequests();\n }\n };\n xhr.open(method, url);\n for (const [name, value] of Object.entries(this.headers)) {\n xhr.setRequestHeader(name, value);\n }\n xhr.withCredentials = withCredentials;\n xhr.send(JSON.stringify(recs));\n } else {\n throttleRequests();\n }\n }, throttleInterval);\n };\n\n throttleRequests();\n }\n\n stop() {\n setTimeout(() => {\n if (this.currentThrottleTimeout) {\n clearTimeout(this.currentThrottleTimeout);\n this.currentThrottleTimeout = null;\n }\n }, 1);\n }\n\n write(rec) {\n rec.url = typeof window !== 'undefined' && window.location.href;\n rec.userAgent = userAgent;\n if (this.currentThrottleTimeout && this.writeCondition(rec)) {\n if (this.records[rec.msg]) {\n this.records[rec.msg].count++;\n } else {\n rec.count = 1;\n this.records[rec.msg] = rec;\n }\n }\n }\n\n recordsAsArray() {\n return Object.keys(this.records).map(errKey => this.records[errKey]);\n }\n\n static defaultWriteCondition() {\n return window.navigator.onLine && !isBot;\n }\n}\n"],"names":["userAgent","window","navigator","isBot","test","defaultHeaders","opts","method","url","headers","throttleInterval","withCredentials","onError","flushOnClose","writeCondition","this","ServerStream","defaultWriteCondition","records","start","fetch","addEventListener","_this","currentThrottleTimeout","clearTimeout","recs","recordsAsArray","length","body","JSON","stringify","credentials","mode","keepalive","throttleRequests","_this2","setTimeout","xhr","XMLHttpRequest","onreadystatechange","readyState","DONE","status","call","console","warn","open","Object","entries","setRequestHeader","send","stop","_this3","write","rec","location","href","msg","count","keys","map","errKey","_this4","onLine"],"mappings":"gNAAA,IAAMA,EAA8B,oBAAXC,OAAyBA,OAAOC,UAAUF,UAAY,YACzEG,EAAQ,+BAA+BC,KAAKJ,GAE5CK,EAAiB,CAAE,eAAgB,iEAIrC,WAAYC,uBAAAA,IAAAA,EAAO,IACf,MASIA,EARAC,OAAAA,aAAS,UAQTD,EAPAE,IAAAA,aAAM,WAONF,EANAG,QAAAA,aAAU,OAMVH,EALAI,iBAAAA,aAAmB,QAKnBJ,EAJAK,gBAAAA,gBACAC,EAGAN,EAHAM,UAGAN,EAFAO,aAAAA,kBAEAP,EADAQ,eAGJC,KAAKD,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