Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add browser support #122

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added bun.lockb
Binary file not shown.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-loki",
"version": "6.0.6",
"version": "6.0.7-rc1",
"description": "A Winston transport for Grafana Loki",
"keywords": [
"winston",
Expand Down Expand Up @@ -52,6 +52,7 @@
"async-exit-hook": "2.0.1",
"btoa": "^1.2.1",
"protobufjs": "^6.8.8",
"url-polyfill": "^1.1.12",
"winston-transport": "^4.3.0"
},
"optionalDependencies": {
Expand Down
19 changes: 16 additions & 3 deletions src/batcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const url = require('url')
const exitHook = require('async-exit-hook')

const { logproto } = require('./proto')
const protoHelpers = require('./proto/helpers')
const req = require('./requests')
Expand All @@ -16,6 +14,20 @@ class Batcher {
return require('snappy')
}

loadUrl () {
let URL
try {
if (typeof window !== 'undefined' && window.URL) {
URL = window.URL
} else {
URL = require('url').URL
}
} catch (_error) {
URL = require('url-polyfill').URL
}
return URL
}

/**
* Creates an instance of Batcher.
* Starts the batching loop if enabled.
Expand All @@ -27,7 +39,8 @@ class Batcher {
this.options = options

// Construct Grafana Loki push API url
this.url = new url.URL(this.options.host + '/loki/api/v1/push')
const URL = this.loadUrl()
this.url = new URL(this.options.host + '/loki/api/v1/push')

// Parse basic auth parameters if given
if (options.basicAuth) {
Expand Down