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

remove bluebird/modernize somewhat #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions lib/capabilities.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
'use strict';
var Promise = require('./promise');
var fs = Promise.promisifyAll(require('fs'));
var fs = require('fs/promises');
var {callbackify} = require('util');
var handlebars = require('handlebars');
var path = require('path');
var getEtag = require('./getEtag');
var wms = fs.readFileAsync(path.join(__dirname, '../templates/wms.hbs')).then(function (template) {
var wms = fs.readFile(path.join(__dirname, '../templates/wms.hbs')).then(function (template) {
return handlebars.compile(template.toString());
});
var wmts = fs.readFileAsync(path.join(__dirname, '../templates/wmts.hbs')).then(function (template) {
var wmts = fs.readFile(path.join(__dirname, '../templates/wmts.hbs')).then(function (template) {
return handlebars.compile(template.toString());
});
var merc = require('./merc');
var normalizeObj = require('./normalizeObj');
var defaultCache = require('./memCache');

var makeUnknownError = require('./makeUnknownError');
var getCapabilities = Promise.coroutine(_getCapabilities);
function filterAbstract (abstract) {
if (!abstract) {
return '';
Expand All @@ -26,20 +25,23 @@ function filterAbstract (abstract) {
return abstract;
}
var makeHost = require('./makeHost');
module.exports = module.exports = function (layers, params, cache, callback) {
module.exports = function (layers, params, cache, callback) {
if (typeof cache === 'function') {
callback = cache;
cache = undefined;
}
if (typeof cache === 'undefined') {
cache = defaultCache;
}
return getCapabilities(layers, params, cache).asCallback(callback);
if (callback) {
return getCapabilitiesCallback(layers, params, cache, callback)
}
return getCapabilities(layers, params, cache);
};

function * _getCapabilities(layers, rawParams) {
const getCapabilitiesCallback = callbackify(getCapabilities);
async function getCapabilities(layers, rawParams) {
try {
layers = yield layers;
layers = await layers;
} catch (e) {
return makeUnknownError(e);
}
Expand All @@ -50,9 +52,9 @@ function * _getCapabilities(layers, rawParams) {
var template;
var service = params.service && params.service.toLowerCase();
if (service === 'wms') {
template = yield wms;
template = await wms;
} else if (service === 'wmts') {
template = yield wmts;
template = await wmts;
} else {
throw new TypeError(`unknown service: ${params.service}`);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/getEtag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var crypto = require('crypto');

const {createHash} = require('crypto');

module.exports = getEtag;

function getEtag(data) {
return crypto.createHash('sha224').update(data).digest('base64');
return createHash('sha224').update(data).digest('base64');
}
1 change: 0 additions & 1 deletion lib/image-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

'use strict';
module.exports = imageType;
function imageType(data) {
if (data.slice(0, 4).toString('hex') === '89504e47') {
Expand Down
31 changes: 21 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
'use strict';
var normalizeObj = require('./normalizeObj');
var Promise = require('./promise');
module.exports = exports = wms;
exports.getCapabilities = require('./capabilities');
exports.getTile = require('./tile');
exports.getMap = require('./map');

function wms(options, rawParams, cache, callback) {
var params = normalizeObj(rawParams);
if (typeof params.request !== 'string') {
const invalid = (callback)=>{
if (!callback) {
return Promise.resolve({
code: 400,
data: 'unknown/invalid request'
}).asCallback(callback);
})
}
process.nextTick(()=>{
callback(null, {
code: 400,
data: 'unknown/invalid request'
})
});
}
function wms(options, rawParams, cache, callback) {
var params = normalizeObj(rawParams);
if (typeof params.request !== 'string') {
return invalid(callback);
}
switch (params.request.toLowerCase()) {
case 'getcapabilities':
Expand All @@ -22,9 +31,11 @@ function wms(options, rawParams, cache, callback) {
case 'gettile':
return exports.getTile(options, params, cache, callback);
default:
return Promise.resolve({
code: 400,
data: 'unknown/invalid request'
}).asCallback(callback);
if (callback) {
process.nextTick(()=>{

})
}
return invalid(callback);
}
}
15 changes: 10 additions & 5 deletions lib/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var Promise = require('./promise');
var normalizeObj = require('./normalizeObj');


var makeError = require('./makeError');
var checkTile = require('./checkTile');
var merc = require('./merc');
Expand Down Expand Up @@ -120,7 +121,7 @@ async function getMap(layer, rawParams) {
if (scale !== scale) {
scale = 1;
}
var format = mime.extension(params.format);
var format = mime.getExtension(params.format);
var bgcolor = params.bgcolor || '0xffffff';
bgcolor = bgcolor.toLowerCase();
if (params.transparent && params.transparent.toLowerCase() === 'true' && format === 'png') {
Expand Down Expand Up @@ -200,7 +201,7 @@ async function getMap(layer, rawParams) {
return makeUnknownError(new Error('aborted'));
}
var headers = {
'content-type': mime.lookup(format),
'content-type': mime.getType(format),
'content-length': resizedImage.length,
etag: getEtag(resizedImage)
};
Expand Down Expand Up @@ -228,6 +229,7 @@ function getZoom(bounds, dimensions, minzoom, maxzoom) {
return Math.max(minzoom, Math.min(maxzoom, adjusted));

}
const getMapCallback = util.callbackify(getMap);
module.exports = function(layer, params, cache, callback) {
if (typeof cache === 'function') {
callback = cache;
Expand All @@ -236,14 +238,17 @@ module.exports = function(layer, params, cache, callback) {
if (typeof cache === 'undefined') {
cache = defaultCache;
}
return Promise.resolve(getMap(layer, params, cache)).asCallback(callback);
if (callback) {
return getMapCallback(layer, params, cache, callback)
}
return getMap(layer, params, cache);
};
var blankPNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAABmJLR0QA/wD/AP+gvaeTAAABFUlEQVR42u3BMQEAAADCoPVP7WsIoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAeAMBPAAB2ClDBAAAAABJRU5ErkJggg==', 'base64');
var blanks = new Map();

function callBlankBack(tile, format, callback) {
var headers = {
'content-type': mime.lookup(format),
'content-type': mime.getType(format),
'content-length': tile.length
};
callback(null, tile, headers);
Expand Down
21 changes: 18 additions & 3 deletions lib/memCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
var Cache = require('lru-cache');

var lru = new Cache({
max: 20 * 1024 * 1024,
length: function (item) {
maxSize: 20 * 1024 * 1024,
sizeCalculation: function (item) {
return item.length;
}
});
Expand All @@ -16,7 +16,14 @@ function get (key, callback) {
}
process.nextTick(callback, new Error('not found'));
}

exports.getAsync = getAsync;
async function getAsync (key) {
var item = lru.get(key);
if (Buffer.isBuffer(item)) {
return item;
}
throw new Error('not found');
}
exports.set = set;

function set(key, value, callback) {
Expand All @@ -26,3 +33,11 @@ function set(key, value, callback) {
lru.set(key, value);
process.nextTick(callback);
}

exports.setAsync = setAsync;
async function setAsync(key, value) {
if (!Buffer.isBuffer(value)) {
value = Buffer.from(value);
}
lru.set(key, value);
}
8 changes: 0 additions & 8 deletions lib/promise.js

This file was deleted.

25 changes: 17 additions & 8 deletions lib/tile.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
'use strict';
var Promise = require('./promise');
var checkTile = require('./checkTile');
const util = require('util');
var getEtag = require('./getEtag');
var makeError = require('./makeError');
var mime = require('mime');
var mapnik = require('mapnik');
var normalizeObj = require('./normalizeObj');
var find = require('./find');
var makeUnknownError = require('./makeUnknownError');
var defaultCache = Promise.promisifyAll(require('./memCache'));
const defaultCache = require('./memCache');
var crypto = require('crypto');
var EE = require('events').EventEmitter;
var imageType = require('./image-type');
const getTileCallback = util.callbackify(getTile);
const promisifyAll = (obj) => {
obj.getAsync = util.promisify(obj.get).bind(obj);
obj.setAsync = util.promisify(obj.set).bind(obj);
}

module.exports = function(layer, params, cache, callback) {
if (typeof cache === 'function') {
callback = cache;
Expand All @@ -20,9 +26,12 @@ module.exports = function(layer, params, cache, callback) {
if (typeof cache === 'undefined') {
cache = defaultCache;
} else {
cache = Promise.promisifyAll(cache);
cache = promisifyAll(cache);
}
if (callback) {
return getTileCallback(layer, params, cache, callback)
}
return Promise.resolve(getTile(layer, params, cache)).asCallback(callback);
return getTile(layer, params, cache);
};
var requiredParams = [
'layer',
Expand Down Expand Up @@ -64,7 +73,7 @@ async function getTile(layer, rawParams, cache) {
};
}
layer.abort = abort || new EE();
var desiredFormat = mime.extension(params.format);
var desiredFormat = mime.getExtension(params.format);
if (!desiredFormat) {
return makeError(`Invalid paramter value for FORMAT: ${params.format}`, 'InvalidParameterValue', 'FORMAT');
}
Expand Down Expand Up @@ -117,7 +126,7 @@ async function getTile(layer, rawParams, cache) {

var headers = normalizeObj(tile[1]);
headers['content-type'] = imageType(data);
var returnedFormat = mime.extension(headers['content-type']);
var returnedFormat = mime.getExtension(headers['content-type']);
if (returnedFormat === desiredFormat) {
if (!headers['content-length']) {
headers['content-length'] = data.length;
Expand All @@ -132,7 +141,7 @@ async function getTile(layer, rawParams, cache) {
};
}
var transcodedData = await transCode(data, desiredFormat, cache, layer.abort);
headers['content-type'] = mime.lookup(desiredFormat);
headers['content-type'] = mime.getType(desiredFormat);
headers['content-length'] = transcodedData.length;
headers.etag = getEtag(data);
return {
Expand All @@ -144,7 +153,7 @@ async function getTile(layer, rawParams, cache) {
return makeUnknownError(e);
}
}
var fromBytes = Promise.promisify(mapnik.Image.fromBytes, mapnik.Image);
var fromBytes = util.promisify(mapnik.Image.fromBytes, mapnik.Image);

function encode(image, format) {
if (!image || typeof image.encode !== 'function') {
Expand Down
Loading