Skip to content

Use whatwg-url over deprecated url apis, and swap out http-https for axios #32

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

Open
wants to merge 3 commits 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
42 changes: 18 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const postcss = require('postcss');
const hh = require('http-https');
const axios = require('axios');
const isUrl = require('is-url');
const trim = require('lodash.trim');
const resolveRelative = require('resolve-relative-url');
const assign = require('lodash.assign');
const url = require('url');

const defaults = {
recursive: true,
Expand All @@ -26,7 +24,7 @@ function postcssImportUrl(options) {
const params = space(atRule.params);
let remoteFile = cleanupRemoteFile(params[0]);
if (parentRemoteFile) {
remoteFile = resolveRelative(remoteFile, parentRemoteFile);
remoteFile = new URL(remoteFile, parentRemoteFile).href;
}
if (!isUrl(remoteFile)) {
return;
Expand Down Expand Up @@ -102,7 +100,9 @@ function postcssImportUrl(options) {
: Promise.resolve(newNode));

if (options.dataUrls) {
atRule.params = `url(data:text/css;base64,${Buffer.from(importedTree.toString()).toString('base64')})`;
atRule.params = `url(data:text/css;base64,${Buffer.from(
importedTree.toString(),
).toString('base64')})`;
} else {
atRule.replaceWith(importedTree);
}
Expand Down Expand Up @@ -131,13 +131,16 @@ function cleanupRemoteFile(value) {
}

function resolveUrls(to, from) {
return 'url("' + resolveRelative(cleanupRemoteFile(to), from) + '")';
return 'url("' + new URL(cleanupRemoteFile(to), from).href + '")';
}

function createPromise(remoteFile, options) {
const reqOptions = urlParse(remoteFile);
reqOptions.headers = {};
reqOptions.headers['connection'] = 'keep-alive';
const reqOptions = {
url: remoteFile,
headers: {
connection: 'keep-alive',
},
};
if (options.modernBrowser) {
reqOptions.headers['user-agent'] =
'Mozilla/5.0 AppleWebKit/538.0 Chrome/88.0.0.0 Safari/538';
Expand All @@ -146,25 +149,16 @@ function createPromise(remoteFile, options) {
reqOptions.headers['user-agent'] = String(options.userAgent);
}
function executor(resolve, reject) {
const request = hh.get(reqOptions, response => {
let body = '';
response.on('data', chunk => {
body += chunk.toString();
});
response.on('end', () => {
axios(reqOptions)
.then(response => {
resolve({
body: body,
body: response.data,
parent: remoteFile,
});
})
.catch(error => {
return reject(error);
});
});
request.on('error', reject);
request.end();
}
return new Promise(executor);
}

function urlParse(remoteFile) {
const reqOptions = url.parse(remoteFile);
return reqOptions;
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
"test:w": "gulp test:w"
},
"dependencies": {
"http-https": "^1.0.0",
"axios": "^1.4.0",
"is-url": "^1.2.4",
"lodash.assign": "^4.2.0",
"lodash.trim": "^4.5.1",
"resolve-relative-url": "^1.0.0"
"lodash.trim": "^4.5.1"
},
"peerDependencies": {
"postcss": "^8.0.0"
Expand Down