Skip to content
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

Add asyncIterator #133

Open
wants to merge 1 commit into
base: main
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
67 changes: 35 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function populateRedirectResponse(page, params, entries, options) {
}
}

export function harFromMessages(messages, options) {
export async function harFromMessages(messages, options) {
options = Object.assign({}, defaultOptions, options);

const ignoredRequests = new Set(),
Expand All @@ -69,13 +69,12 @@ export function harFromMessages(messages, options) {
responseReceivedExtraInfos = [],
currentPageId;

for (const message of messages) {
const processMessage = (message) => {
const params = message.params;

const method = message.method;

if (!/^(Page|Network)\..+/.test(method)) {
continue;
return;
}

switch (method) {
Expand All @@ -86,7 +85,7 @@ export function harFromMessages(messages, options) {
const frameId = params.frameId;
const rootFrame = rootFrameMappings.get(frameId) || frameId;
if (pages.some((page) => page.__frameId === rootFrame)) {
continue;
return;
}
currentPageId = randomUUID();
const title =
Expand Down Expand Up @@ -141,7 +140,7 @@ export function harFromMessages(messages, options) {
const request = params.request;
if (!isSupportedProtocol(request.url)) {
ignoredRequests.add(params.requestId);
continue;
return;
}
const page = pages[pages.length - 1];
const cookieHeader = getHeaderValue(request.headers, 'Cookie');
Expand Down Expand Up @@ -231,7 +230,7 @@ export function harFromMessages(messages, options) {
// ignoredRequests.add(params.requestId);
entriesWithoutPage.push(entry);
paramsWithoutPage.push(params);
continue;
return;
}

entries.push(entry);
Expand All @@ -250,11 +249,11 @@ export function harFromMessages(messages, options) {
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
return;
}

if (ignoredRequests.has(params.requestId)) {
continue;
return;
}

const entry = entries.find(
Expand All @@ -264,7 +263,7 @@ export function harFromMessages(messages, options) {
log(
`Received requestServedFromCache for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}

entry.__servedFromCache = true;
Expand All @@ -279,7 +278,7 @@ export function harFromMessages(messages, options) {
case 'Network.requestWillBeSentExtraInfo':
{
if (ignoredRequests.has(params.requestId)) {
continue;
return;
}

const entry = entries.find(
Expand All @@ -289,7 +288,7 @@ export function harFromMessages(messages, options) {
log(
`Extra info sent for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}

if (params.headers) {
Expand All @@ -312,11 +311,11 @@ export function harFromMessages(messages, options) {
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
return;
}

if (ignoredRequests.has(params.requestId)) {
continue;
return;
}

let entry = entries.find(
Expand All @@ -331,7 +330,7 @@ export function harFromMessages(messages, options) {

if (!entry) {
responseReceivedExtraInfos.push(params);
continue;
return;
}

if (!entry.response) {
Expand All @@ -341,7 +340,7 @@ export function harFromMessages(messages, options) {
blockedCookies: params.blockedCookies,
};
responseReceivedExtraInfos.push(params);
continue;
return;
}

if (params.headers) {
Expand All @@ -355,11 +354,11 @@ export function harFromMessages(messages, options) {
if (pages.length < 1) {
//we haven't loaded any pages yet.
responsesWithoutPage.push(params);
continue;
return;
}

if (ignoredRequests.has(params.requestId)) {
continue;
return;
}

let entry = entries.find(
Expand All @@ -376,7 +375,7 @@ export function harFromMessages(messages, options) {
log(
`Received network response for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}

const frameId =
Expand All @@ -388,7 +387,7 @@ export function harFromMessages(messages, options) {
log(
`Received network response for requestId ${params.requestId} that can't be mapped to any page.`,
);
continue;
return;
}

try {
Expand Down Expand Up @@ -416,10 +415,10 @@ export function harFromMessages(messages, options) {
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
return;
}
if (ignoredRequests.has(params.requestId)) {
continue;
return;
}

const entry = entries.find(
Expand All @@ -429,7 +428,7 @@ export function harFromMessages(messages, options) {
log(
`Received network data for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}
// It seems that people sometimes have an entry without a response,
// I wonder how that works
Expand Down Expand Up @@ -460,11 +459,11 @@ export function harFromMessages(messages, options) {
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
return;
}
if (ignoredRequests.has(params.requestId)) {
ignoredRequests.delete(params.requestId);
continue;
return;
}

const entry = entries.find(
Expand All @@ -474,7 +473,7 @@ export function harFromMessages(messages, options) {
log(
`Network loading finished for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}

finalizeEntry(entry, params);
Expand All @@ -485,7 +484,7 @@ export function harFromMessages(messages, options) {
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
return;
}

const page = pages[pages.length - 1];
Expand All @@ -502,7 +501,7 @@ export function harFromMessages(messages, options) {
{
if (pages.length < 1) {
//we haven't loaded any pages yet.
continue;
return;
}

const page = pages[pages.length - 1];
Expand Down Expand Up @@ -534,7 +533,7 @@ export function harFromMessages(messages, options) {
{
if (ignoredRequests.has(params.requestId)) {
ignoredRequests.delete(params.requestId);
continue;
return;
}

const entry = entries.find(
Expand All @@ -544,15 +543,15 @@ export function harFromMessages(messages, options) {
log(
`Network loading failed for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}

if (params.errorText === 'net::ERR_ABORTED') {
finalizeEntry(entry, params);
log(
`Loading was canceled due to Chrome or a user action for requestId ${params.requestId}.`,
);
continue;
return;
}

// This could be due to incorrect domain name etc. Sad, but unfortunately not something that a HAR file can
Expand All @@ -576,7 +575,7 @@ export function harFromMessages(messages, options) {
log(
`Received resourceChangedPriority for requestId ${params.requestId} with no matching request.`,
);
continue;
return;
}

entry._priority = message.params.newPriority;
Expand All @@ -588,6 +587,10 @@ export function harFromMessages(messages, options) {
ignoredEvents(method);
break;
}
};

for await (const message of messages) {
processMessage(message);
}

if (!options.includeResourcesFromDiskCache) {
Expand Down
31 changes: 29 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,27 @@ function perflogs() {
);
}

async function* asyncMessageGenerator(messages) {
for (const message of messages) {
await Promise.resolve();
yield message;
}
}

function parsePerflog(perflogPath, options) {
return fs.readFile(perflogPath, { encoding: 'utf8' }).then((data) => {
return fs.readFile(perflogPath, { encoding: 'utf8' }).then(async (data) => {
const log = JSON.parse(data);
const har = await harFromMessages(log, options);
validator.har(har);
return har;
});
}

function asyncParsePerflog(perflogPath, options) {
return fs.readFile(perflogPath, { encoding: 'utf8' }).then(async (data) => {
const log = JSON.parse(data);
const har = harFromMessages(log, options);
const asyncIterator = asyncMessageGenerator(log);
const har = await harFromMessages(asyncIterator, options);
validator.har(har);
return har;
});
Expand Down Expand Up @@ -113,6 +130,16 @@ test('ryan', (t) => {
});
});

test('ryan asyncIterator', (t) => {
const perflogPath = perflog('ryan.json');
return asyncParsePerflog(perflogPath)
.then((har) => har.log)
.then((log) => {
t.is(log.pages.length, 1);
return log;
});
});

test('chrome66', (t) => {
const perflogPath = perflog('www.sitepeed.io.chrome66.json');
return parsePerflog(perflogPath)
Expand Down
Loading