Skip to content

Commit

Permalink
perf(speeding up more): simplified for the sake of performance
Browse files Browse the repository at this point in the history
  • Loading branch information
acuciureanu committed Feb 9, 2023
1 parent a8edb04 commit 93c28f4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
Binary file modified results.txt
Binary file not shown.
33 changes: 2 additions & 31 deletions sandbox/index.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
<!DOCTYPE html>

<html>
<script id="payload">
const types = [Object, String, Number, Array, Function, Boolean];

const prototypesPropertiesReducer = (acc, type) => ({
...acc,
[type.name]: Object.getOwnPropertyNames(type.prototype),
});

const prototypePropertyNames = () => types.reduce(prototypesPropertiesReducer, {});

const probe = () =>
Object.keys(prototypePropertyNames()).reduce((acc, key) => {
for (let propKey of prototypePropertyNames()[key]) {
const payload = `${key}.prototype.${propKey}`;
try {
const propValue = eval(payload);

if (typeof propValue === 'function' && !propValue.toString().includes('[native code]')) {
// Check if property is user defined and not native
acc.push(payload);
}
} catch (e) {
// Nothing to catch now.
}
}
return acc;
}, []);
</script>
<script id="target" src="LIBRARY"></script>

</html>
<script id="target" src="LIBRARY"></script>
</html>
2 changes: 1 addition & 1 deletion sandbox/js/check.payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ const probe = () =>
}
}
return acc;
}, []);
}, []);
13 changes: 12 additions & 1 deletion services/cdnjs.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,31 @@ const client = axios.create({
timeout: 5000,
});

const payload = load('../sandbox/js/check.payload.js');

const createSandbox = (library) => load('../sandbox/index.html').replace('LIBRARY', library);

const getLibraries = async () =>
client
.get('/libraries')
.then((response) => response.data.results.filter((result) => result.latest !== null && result.latest.endsWith(".js")))
.then((response) =>
response.data.results.filter((result) => result.latest !== null && result.latest.endsWith('.js'))
)
.catch((error) => console.log(error));

const probe = async (library) => {
const page = await browser.newPage();

await page.setJavaScriptEnabled(true);

await page.goto(`data:text/html,${createSandbox(library.latest)}`, { waitUntil: 'networkidle0' });

await page.evaluate((payload) => {
const script = document.createElement('script');
script.innerHTML = payload;
document.head.appendChild(script);
}, payload);

const results = await page.evaluate(() => probe());

await page.close({ runBeforeUnload: true });
Expand Down
3 changes: 1 addition & 2 deletions services/check.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PromisePool } from '@supercharge/promise-pool';

const payload = load('../sandbox/js/check.payload.js');

const browser = await puppeteer.launch({ headless: false });
const browser = await puppeteer.launch({ headless: true });

const probe = async (pageUrl) => {
const page = await browser.newPage();
Expand All @@ -24,7 +24,6 @@ const probe = async (pageUrl) => {
return { url: pageUrl, findings: results };
};


const probeAll = async (urls, concurrency = 10) => {
const { results } = await PromisePool.for(urls)
.withConcurrency(concurrency)
Expand Down

0 comments on commit 93c28f4

Please sign in to comment.