├── .gitignore ├── index.d.ts ├── package.json ├── tests └── system-tests.html ├── LICENSE ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | tags 2 | node_modules 3 | .*swp 4 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import * as L from 'leaflet'; 2 | import {Observable} from 'rxjs'; 3 | 4 | declare module 'leaflet' { 5 | namespace TileLayer { 6 | export class WMSHeader extends WMS { 7 | constructor( 8 | baseUrl: string, 9 | options: WMSOptions, 10 | header: { header: string; value: string }[], 11 | abort?: Observable 12 | ); 13 | } 14 | export function wmsHeader( 15 | baseUrl: string, 16 | options: WMSOptions, 17 | header: { header: string; value: string }[], 18 | abort?: Observable 19 | ): L.TileLayer.WMSHeader; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leaflet-wms-header", 3 | "version": "1.0.9", 4 | "description": "Custom headers on Leaflet TileLayer WMS", 5 | "main": "index.js", 6 | "types": "index.d.ts", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/ticinum-aerospace/leaflet-wms-header" 10 | }, 11 | "keywords": [ 12 | "leaflet", 13 | "headers", 14 | "tilelayer", 15 | "wms", 16 | "plugin", 17 | "typescript", 18 | "types" 19 | ], 20 | "author": "Ticinum Aerospace", 21 | "license": "ISC", 22 | "peerDependencies": { 23 | "leaflet": "^1.7.1" 24 | }, 25 | "dependencies": { 26 | "rxjs": "^6.5.3", 27 | "typescript": "^2.9.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/system-tests.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2018, Ticinum Aerospace Srl 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # leaflet-wms-header 2 | Custom headers on Leaflet TileLayer WMS. 3 | It's a simple plugin that allow to set custom header for WMS interface. 4 | 5 | It works with javascript and typescript without any dependencies! 6 | 7 | Based on https://github.com/Leaflet/Leaflet/issues/2091#issuecomment-302706529. 8 | 9 | ### Javascript 10 | ```sh 11 | $ npm install leaflet leaflet-wms-header --save 12 | ``` 13 | 14 | ```html 15 | 16 | 17 | 18 | ``` 19 | 20 | ```js 21 | 22 | // YOUR LEAFLET CODE 23 | 24 | var wmsLayer = L.TileLayer.wmsHeader( 25 | 'https://GEOSERVER_PATH/geoserver/wms?', 26 | { 27 | layers: 'ne:ne', 28 | format: 'image/png', 29 | transparent: true, 30 | }, 31 | [ 32 | { header: 'Authorization', value: 'JWT ' + MYAUTHTOKEN }, 33 | { header: 'content-type', value: 'text/plain'}, 34 | ], 35 | null 36 | ).addTo(map); 37 | ``` 38 | 39 | ### Typescript 40 | ```sh 41 | $ npm install leaflet @types/leaflet leaflet-wms-header --save 42 | ``` 43 | ```ts 44 | import * as L from 'leaflet'; 45 | import 'leaflet-wms-header'; 46 | 47 | // YOUR LEAFLET CODE 48 | 49 | let wmsLayer: L.TileLayer.WMSHeader = L.TileLayer.wmsHeader( 50 | 'https://GEOSERVER_PATH/geoserver/wms?', 51 | { 52 | layers: layers, 53 | format: 'image/png', 54 | transparent: true, 55 | }, [ 56 | { header: 'Authorization', value: 'JWT ' + MYAUTHTOKEN }, 57 | { header: 'content-type', value: 'text/plain'}, 58 | ], 59 | null 60 | ).addTo(map); 61 | ``` 62 | 63 | ### Abort parameter 64 | 65 | Abort parameter allow to abort the http request through an Observable. This optimization function might be usefull to stop the http request when it is not necessary anymore, mostly if many requests are pending. An example is provided on /tests/system-tests.html . 66 | 67 | See below an example using an Observable as "abort" parameter. 68 | 69 | ```ts 70 | let tileLayer: L.TileLayer.WMSHeader = L.TileLayer.wmsHeader( 71 | 'https://GEOSERVER_PATH/geoserver/wms?', 72 | { 73 | layers: layers, 74 | format: 'image/png', 75 | transparent: true, 76 | }, [ 77 | { header: 'Authorization', value: 'JWT ' + MYAUTHTOKEN }, 78 | { header: 'content-type', value: 'text/plain'}, 79 | ], 80 | this.abortWMSObservable$.pipe(take(1)) 81 | ); 82 | ``` 83 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import * as Util from "leaflet/src/core/Util"; 4 | import * as DomUtil from "leaflet/src/dom/DomUtil"; 5 | 6 | async function fetchImage(url, callback, headers, abort, requests) { 7 | let _headers = {}; 8 | if (headers) { 9 | headers.forEach(h => { 10 | _headers[h.header] = h.value; 11 | }); 12 | } 13 | const controller = new AbortController(); 14 | const signal = controller.signal; 15 | if (abort) { 16 | abort.subscribe(() => { 17 | controller.abort(); 18 | }); 19 | } 20 | 21 | const request = { 22 | url, 23 | controller 24 | }; 25 | requests.push(request); 26 | 27 | fetch(url, { 28 | method: "GET", 29 | headers: _headers, 30 | mode: "cors", 31 | signal: signal 32 | }).then(async f => { 33 | const blob = await f.blob(); 34 | callback(blob); 35 | }); 36 | } 37 | 38 | L.TileLayer.WMSHeader = L.TileLayer.WMS.extend({ 39 | initialize: function (url, options, headers, abort) { 40 | L.TileLayer.WMS.prototype.initialize.call(this, url, options); 41 | this.headers = headers; 42 | this.abort = abort; 43 | this.requests = []; 44 | }, 45 | createTile(coords, done) { 46 | const url = this.getTileUrl(coords); 47 | const img = document.createElement("img"); 48 | img.setAttribute("role", "presentation"); 49 | img.setAttribute("data-url", url); 50 | 51 | fetchImage( 52 | url, 53 | resp => { 54 | const reader = new FileReader(); 55 | reader.onload = () => { 56 | img.src = reader.result; 57 | }; 58 | reader.readAsDataURL(resp); 59 | done(null, img); 60 | }, 61 | this.headers, 62 | this.abort, 63 | this.requests 64 | ); 65 | return img; 66 | }, 67 | _abortLoading: function() { 68 | for (const i in this._tiles) { 69 | if (this._tiles[i].coords.z !== this._tileZoom) { 70 | const tile = this._tiles[i].el; 71 | 72 | tile.onload = Util.falseFn; 73 | tile.onerror = Util.falseFn; 74 | 75 | const url = tile.getAttribute("data-url"); 76 | const j = this.requests.findIndex(r => r && r.url === url); 77 | if (j >= 0) { 78 | this.requests[j].controller.abort(); 79 | 80 | tile.src = Util.emptyImageUrl; 81 | DomUtil.remove(tile); 82 | delete this._tiles[i]; 83 | delete this.requests[j]; 84 | } 85 | } 86 | } 87 | } 88 | }); 89 | 90 | L.TileLayer.wmsHeader = function (url, options, headers, abort) { 91 | return new L.TileLayer.WMSHeader(url, options, headers, abort); 92 | }; 93 | --------------------------------------------------------------------------------