Skip to content

Commit

Permalink
Add a URL constructor loader with polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Jani Anttonen authored and JaniAnttonen committed Nov 16, 2022
1 parent 1cebef2 commit b37af27
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
21 changes: 18 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,22 @@ class Batcher {
return require('snappy')
}

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

/**
* Creates an instance of Batcher.
* Starts the batching loop if enabled.
Expand All @@ -27,7 +41,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

0 comments on commit b37af27

Please sign in to comment.