fetch-css 4.0.9 → 4.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +22 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -11,6 +11,15 @@ import fetchEnhanced from "fetch-enhanced";
11
11
  const fetch = fetchEnhanced(undiciFetch, {undici: true});
12
12
  const clone = cloner();
13
13
 
14
+ async function doFetch(url, opts) {
15
+ try {
16
+ return await fetch(url, opts);
17
+ } catch (err) {
18
+ err.message = `${err.message} (${url})`;
19
+ throw err;
20
+ }
21
+ }
22
+
14
23
  async function extract(res) {
15
24
  const styleUrls = [];
16
25
  const styleTags = [];
@@ -63,6 +72,15 @@ function isValidCSS(string) {
63
72
  return false;
64
73
  }
65
74
 
75
+ function arrayBufferToBufferCycle(ab) {
76
+ const buffer = new Buffer(ab.byteLength);
77
+ const view = new Uint8Array(ab);
78
+ for (let i = 0; i < buffer.length; ++i) {
79
+ buffer[i] = view[i];
80
+ }
81
+ return buffer;
82
+ }
83
+
66
84
  function extractCssFromJs(js) {
67
85
  let css = "";
68
86
 
@@ -97,10 +115,10 @@ async function extensionCss({crx, contentScriptsOnly, strict}) {
97
115
  url += `&x=id%3D${crx}`;
98
116
  url += `%26uc`;
99
117
 
100
- const res = await fetch(url);
118
+ const res = await doFetch(url);
101
119
  validateStatus(res, url, strict);
102
120
 
103
- const crxBuffer = await res.buffer();
121
+ const crxBuffer = arrayBufferToBufferCycle(await res.arrayBuffer());
104
122
  const zipBuffer = Buffer.from(crxToZip(crxBuffer));
105
123
 
106
124
  const files = {};
@@ -167,7 +185,7 @@ export default async function fetchCss(sources) { // eslint-disable-line import/
167
185
  const sourceResponses = await Promise.all(sources.map(source => {
168
186
  if (!source.url) return null;
169
187
  const {pathname} = new URL(source.url);
170
- return pathname.endsWith(".css") || pathname.endsWith(".js") ? null : fetch(source.url, source.fetchOpts);
188
+ return pathname.endsWith(".css") || pathname.endsWith(".js") ? null : doFetch(source.url, source.fetchOpts);
171
189
  }));
172
190
 
173
191
  for (const [index, res] of Object.entries(sourceResponses)) {
@@ -184,7 +202,7 @@ export default async function fetchCss(sources) { // eslint-disable-line import/
184
202
 
185
203
  const fetchResponses = await Promise.all(sources.map(source => {
186
204
  if (!source.url) return null;
187
- return Promise.all(source.urls.map(url => fetch(url).then(res => res.text())));
205
+ return Promise.all(source.urls.map(url => doFetch(url).then(res => res.text())));
188
206
  }));
189
207
 
190
208
  for (const [index, responses] of Object.entries(fetchResponses)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-css",
3
- "version": "4.0.9",
3
+ "version": "4.0.11",
4
4
  "description": "Extract CSS from websites and browser extensions",
5
5
  "author": "silverwind",
6
6
  "repository": "silverwind/fetch-css",