@beyonk/google-fonts-webpack-plugin
Advanced tools
Comparing version 1.2.3 to 1.4.2
{ | ||
"name": "@beyonk/google-fonts-webpack-plugin", | ||
"version": "1.2.3", | ||
"version": "1.4.2", | ||
"description": "Google Fonts Webpack plugin", | ||
@@ -21,4 +21,4 @@ "main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha spec --colors", | ||
"watch": "mocha spec --colors --watch" | ||
"test": "mocha --timeout 5000 spec --colors", | ||
"watch": "mocha --timeout 5000 spec --colors --watch" | ||
}, | ||
@@ -34,2 +34,3 @@ "devDependencies": { | ||
"lodash": "^4.17.4", | ||
"md5": "^2.2.1", | ||
"node-fetch": "^2.1.2", | ||
@@ -36,0 +37,0 @@ "webpack-sources": "^1.1.0", |
const should = require("should") | ||
const GoogleWebfonts = require("../src/GoogleWebfonts") | ||
const fs = require('fs') | ||
const path = require('path') | ||
const os = require('os') | ||
const md5 = require("md5") | ||
@@ -97,2 +101,3 @@ describe("GoogleWebfonts", () => { | ||
.then(res => { | ||
console.log(res) | ||
res.status.should.equal(200) | ||
@@ -99,0 +104,0 @@ }) |
const should = require("should"); | ||
const webpack = require("webpack"); | ||
const MemoryFs = require("memory-fs"); | ||
const nodeFS = require("fs"); | ||
const os = require("os"); | ||
const GoogleWebfontPlugin = require("../src"); | ||
@@ -67,2 +69,8 @@ | ||
}); | ||
it("downloaded files are cached", () => { | ||
nodeFS.readdirSync(os.tmpdir()).filter(fn => { | ||
return fn.endsWith(".zip") && fn.startsWith("google-fonts-webpack") | ||
}).should.not.be.exactly(0) | ||
}) | ||
}); |
@@ -5,2 +5,5 @@ const _ = require("lodash") | ||
const fetch = require("node-fetch") | ||
const os = require("os") | ||
const md5 = require("md5") | ||
const fs = require("fs") | ||
const { RawSource } = require("webpack-sources") | ||
@@ -24,4 +27,8 @@ const FontTypes = require("./FontTypes") | ||
function tmpFile(filename) { | ||
return path.join(os.tmpdir(), filename); | ||
} | ||
function getVariantCss({ variant, info, font, formats, fontsPath }) { | ||
const src = info.local.map(fileName => `local("${fileName}")`) | ||
const src = Object.prototype.hasOwnProperty.call(info, 'local') ? info.local.map(fileName => `local("${fileName}")`) : ["local("+info.fontFamily+")"] | ||
let fallback | ||
@@ -86,6 +93,3 @@ formats.forEach(ext => { | ||
download() { | ||
if(this._response) { | ||
return Promise.resolve(this._response) | ||
} | ||
getZipURL() { | ||
const { font } = this | ||
@@ -99,3 +103,10 @@ const { subsets, variants, formats } = this.query | ||
} | ||
return fetch(url) | ||
return url | ||
} | ||
download() { | ||
if(this._response) { | ||
return Promise.resolve(this._response) | ||
} | ||
return fetch(this.getZipURL()) | ||
.then(response => { | ||
@@ -110,2 +121,22 @@ if(response.status !== 200) { | ||
// Download zip, but return cached if there is one | ||
downloadWithCache() { | ||
const url = this.getZipURL() | ||
const cacheFilePath = tmpFile("google-fonts-webpack-" + md5(url) + ".zip") | ||
if (fs.existsSync(cacheFilePath)) { | ||
return Promise.resolve(fs.readFileSync(cacheFilePath)) | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
this.download() | ||
.then(response => response.buffer()) | ||
.then(buffer => { | ||
fs.writeFile(cacheFilePath, buffer, (err) => { | ||
if (err) console.log("Couldn't cache file") | ||
}) | ||
resolve(buffer) | ||
}) | ||
}) | ||
} | ||
} | ||
files() { | ||
@@ -115,4 +146,3 @@ if(this._files) { | ||
} | ||
return this.download() | ||
.then(response => response.buffer()) | ||
return this.downloadWithCache() | ||
.then(buffer => new Promise((resolve, reject) => { | ||
@@ -119,0 +149,0 @@ this._files = {} |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
23622
650
6
3
+ Addedmd5@^2.2.1
+ Addedcharenc@0.0.2(transitive)
+ Addedcrypt@0.0.2(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedmd5@2.3.0(transitive)