From 0f11b4a90360744123bde0a708dc4767a0db631b Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 10 Feb 2019 14:05:12 +0200 Subject: [PATCH] Fix error preventing 'constructor' from being valid icon name --- app.js | 2 +- package-lock.json | 6 ++--- package.json | 4 +-- src/dirs.js | 10 ++++---- src/files.js | 6 ++--- src/json.js | 2 +- src/log.js | 4 +-- src/reload.js | 8 +++--- src/sync.js | 6 ++--- tests/request_icons_test.js | 49 ++++++++++++++++++++++++++++++------- 10 files changed, 64 insertions(+), 33 deletions(-) diff --git a/app.js b/app.js index 438b84f..92c2da4 100644 --- a/app.js +++ b/app.js @@ -108,7 +108,7 @@ if (!app.dirs.getRepos().length) { } // Collections -app.collections = {}; +app.collections = Object.create(null); app.reload = require('./src/reload').bind(this, app); // Sync module diff --git a/package-lock.json b/package-lock.json index 8832e10..e3bd55b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,9 +10,9 @@ "optional": true }, "@iconify/json-tools": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@iconify/json-tools/-/json-tools-1.0.1.tgz", - "integrity": "sha512-A15I5GRny9gDZIfgu4nutUzdMsG0Bd2Ju6GbochRLu/ZSoMSH22L8mQrQdvdj0U/ydzR68mktUXrPUyG786Zlw==" + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@iconify/json-tools/-/json-tools-1.0.5.tgz", + "integrity": "sha512-CfgaLFnd+fuXg2zcxI2/LZpFZHcPUl8BmNNzeP2usLw+Z+JPDwibt8H41pJ86dy8ADMONsTBeTpkFXLh+n9Gsw==" }, "accepts": { "version": "1.3.5", diff --git a/package.json b/package.json index 850b04c..381921e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "2.0.0-beta1", + "version": "2.0.0-beta2", "description": "Node.js version of api.iconify.design", "private": true, "main": "app.js", @@ -16,7 +16,7 @@ "url": "git+ssh://git@github.com/iconify-design/api.js.git" }, "dependencies": { - "@iconify/json-tools": "^1.0.0-beta2", + "@iconify/json-tools": "^1.0.5", "express": "^4.16.4" }, "devDependencies": { diff --git a/src/dirs.js b/src/dirs.js index 14d1fe0..352cd7b 100644 --- a/src/dirs.js +++ b/src/dirs.js @@ -19,9 +19,9 @@ const fs = require('fs'); * @returns {object} */ module.exports = app => { - let functions = {}, - dirs = {}, - custom = {}, + let functions = Object.create(null), + dirs = Object.create(null), + custom = Object.create(null), repos = [], storageDir = null, versionsFile = null; @@ -132,7 +132,7 @@ module.exports = app => { */ // Get synchronized repositories - let cached = {}; + let cached = Object.create(null); app.config.canSync = false; try { if (app.config.sync.versions && app.config.sync.storage) { @@ -147,7 +147,7 @@ module.exports = app => { } } catch (err) { if (typeof cached !== 'object') { - cached = {}; + cached = Object.create(null); } } diff --git a/src/files.js b/src/files.js index 61746f5..9ebabf3 100644 --- a/src/files.js +++ b/src/files.js @@ -28,7 +28,7 @@ let functions = { if (err) { _app.error('Error deleting file ' + file, Object.assign({ key: 'unlink-' + file - }, typeof options === 'object' ? options : {})); + }, typeof options === 'object' ? options : Object.create(null))); } fulfill(); }) @@ -42,7 +42,7 @@ let functions = { * @return {Promise} */ rmdir: (dir, options) => new Promise((fulfill, reject) => { - options = typeof options === 'object' ? options : {}; + options = typeof options === 'object' ? options : Object.create(null); function done() { fs.rmdir(dir, err => { @@ -62,7 +62,7 @@ let functions = { return; } - let children = {}; + let children = Object.create(null); files.forEach(file => { let filename = dir + '/' + file, diff --git a/src/json.js b/src/json.js index 4ba2c59..ddd1524 100644 --- a/src/json.js +++ b/src/json.js @@ -13,7 +13,7 @@ const fs = require('fs'); const util = require('util'); // List of imported modules -let imported = {}; +let imported = Object.create(null); /** * Import json file diff --git a/src/log.js b/src/log.js index 96ffc85..33d5d7a 100644 --- a/src/log.js +++ b/src/log.js @@ -26,7 +26,7 @@ const defaultOptions = { }; // List of notices that are sent only once per session -let logged = {}; +let logged = Object.create(null); // List of throttled messages let throttled = null; @@ -54,7 +54,7 @@ const sendQueue = app => { * @param {object|boolean} [options] */ module.exports = (app, error, message, options) => { - options = Object.assign({}, defaultOptions, options === void 0 ? {} : (typeof options === 'boolean' ? { + options = Object.assign(Object.create(null), defaultOptions, options === void 0 ? Object.create(null) : (typeof options === 'boolean' ? { log: options }: options)); diff --git a/src/reload.js b/src/reload.js index 0bbe39b..668966a 100644 --- a/src/reload.js +++ b/src/reload.js @@ -19,9 +19,9 @@ const defaultOptions = { logger: null }; -let repoItems = {}, - collectionRepos = {}, - hashes = {}, +let repoItems = Object.create(null), + collectionRepos = Object.create(null), + hashes = Object.create(null), nextReload = 0; class Loader { @@ -257,7 +257,7 @@ class Loader { */ module.exports = (app, repos, options) => new Promise((fulfill, reject) => { // Options - options = Object.assign({}, defaultOptions, typeof options === 'object' ? options : {}); + options = Object.assign(Object.create(null), defaultOptions, typeof options === 'object' ? options : Object.create(null)); // Get list of repositories to reload let availableRepos = app.dirs.getRepos(); diff --git a/src/sync.js b/src/sync.js index 27bcfa4..38ac91a 100644 --- a/src/sync.js +++ b/src/sync.js @@ -19,8 +19,8 @@ const defaultOptions = { reload: true }; -let active = {}, - queued = {}; +let active = Object.create(null), + queued = Object.create(null); class Sync { constructor(app, repo, options) { @@ -108,7 +108,7 @@ class Sync { module.exports = (app, repo, options) => new Promise((fulfill, reject) => { // Options - options = Object.assign({}, defaultOptions, typeof options !== 'object' ? options : {}); + options = Object.assign(Object.create(null), defaultOptions, typeof options !== 'object' ? options : Object.create(null)); // Check if synchronization is disabled if (!app.config.canSync || !app.config.sync[repo] || !app.config.sync.git) { diff --git a/tests/request_icons_test.js b/tests/request_icons_test.js index d5478ac..5f60bee 100644 --- a/tests/request_icons_test.js +++ b/tests/request_icons_test.js @@ -84,16 +84,38 @@ expect(parseQuery('test1', 'icons', 'js', { icons: 'alias1' })).to.be.eql({ - type: 'application/javascript; charset=utf-8', - body: 'SimpleSVG._loaderCallback({"prefix":"test","icons":{"icon2":{"body":""}},"aliases":{"alias1":{"parent":"icon2","hFlip":true}},"width":24,"height":24})' + js: true, + defaultCallback: 'SimpleSVG._loaderCallback', + data: { + prefix: 'test', + icons: { + icon2: { body: '' } + }, + aliases: { + alias1: { parent: 'icon2', hFlip: true } + }, + width: 24, + height: 24 + } }); // Query collection without prefix, json expect(parseQuery('test2', 'icons', 'json', { icons: 'alias1' })).to.be.eql({ - type: 'application/json; charset=utf-8', - body: '{"prefix":"test2","icons":{"icon2":{"body":""}},"aliases":{"alias1":{"parent":"icon2","hFlip":true}},"width":24,"height":24}' + js: false, + defaultCallback: 'SimpleSVG._loaderCallback', + data: { + prefix: 'test2', + icons: { + icon2: { body: '' } + }, + aliases: { + alias1: { parent: 'icon2', hFlip: true } + }, + width: 24, + height: 24 + } }); // Custom callback @@ -101,8 +123,17 @@ icons: 'icon1,icon2', callback: 'console.log' })).to.be.eql({ - type: 'application/javascript; charset=utf-8', - body: 'console.log({"prefix":"test","icons":{"icon1":{"body":"","width":30},"icon2":{"body":""}},"width":24,"height":24})' + js: true, + defaultCallback: 'SimpleSVG._loaderCallback', + data: { + prefix: 'test', + icons: { + icon1: { body: '', width: 30 }, + icon2: { body: '' } + }, + width: 24, + height: 24 + } }); }); @@ -112,7 +143,7 @@ })).to.be.eql({ filename: 'icon1.svg', type: 'image/svg+xml; charset=utf-8', - body: '' + body: '' }); // Icon with custom attributes @@ -121,7 +152,7 @@ })).to.be.eql({ filename: 'alias1.svg', type: 'image/svg+xml; charset=utf-8', - body: '' + body: '' }); // Icon with id replacement @@ -130,7 +161,7 @@ rotate: '90deg' }).body.replace(/IconifyId-[0-9a-f]+-[0-9a-f]+-[0-9]+/g, 'some-id'); - expect(result).to.be.equal(''); + expect(result).to.be.equal(''); }); }); })();