UNPKG

leaflet-wms-header

Version:
64 lines (58 loc) 1.51 kB
'use strict'; async function fetchImage(url, callback, headers, abort) { let _headers = {}; if (headers) { headers.forEach(h => { _headers[h.header] = h.value; }); } const controller = new AbortController(); const signal = controller.signal; if (abort) { abort.subscribe(() => { controller.abort(); }); } const f = await fetch(url, { method: "GET", headers: _headers, mode: "cors", signal: signal }); const blob = await f.blob(); callback(blob); } L.TileLayer.WMSHeader = L.TileLayer.WMS.extend({ initialize: function (url, options, headers, abort, results) { L.TileLayer.WMS.prototype.initialize.call(this, url, options); this.headers = headers; this.abort = abort; this.results = results; }, createTile(coords, done) { const url = this.getTileUrl(coords); const img = document.createElement("img"); img.setAttribute("role", "presentation"); self = this; fetchImage( url, resp => { const reader = new FileReader(); reader.onload = () => { img.src = reader.result; if (self.results) { self.results.next(reader.result); }; }; reader.readAsDataURL(resp); done(null, img); }, this.headers, this.abort ); return img; } }); L.TileLayer.wmsHeader = function (url, options, headers, abort, results) { return new L.TileLayer.WMSHeader(url, options, headers, abort, results); };