Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixed
* Use the extra info from Chrome for request and response headers and cookies, also when it comes before the first page (as with Puppeteer). Headers are no longer duplicated with different case, response cookies are parsed from `Set-Cookie` in the extra info, and a 304 from the server no longer removes the headers of the cached response. Thank you [jtung23](https://github.com/jtung23) for the first implementation and the test case in [#111](https://github.com/sitespeedio/chrome-har/pull/111).
* Stop using the deprecated `url.parse()`, so Node.js 24 and later no longer print a deprecation warning. The URL in the HAR is now exactly the URL that Chrome sent, before some characters like `|` and `{` were escaped [#114](https://github.com/sitespeedio/chrome-har/issues/114).

## 1.3.1 - 2026-05-19
* Fix for render blocking stats [#158](https://github.com/sitespeedio/chrome-har/pull/158).
Expand Down
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parse, format } from 'node:url';
import { randomUUID } from 'node:crypto';
import { createRequire } from 'node:module';
import debug from 'debug';
Expand All @@ -9,7 +8,7 @@ import {
formatMillis,
parsePostData,
isSupportedProtocol,
toNameValuePairs
parseQueryString
} from './lib/util.js';
import populateEntryFromResponse, {
addExtraResponseInfo
Expand Down Expand Up @@ -191,7 +190,7 @@ function createHarBuilder(options) {
// 2. If we remove it, the HAR will not have the same URL as we tested
// and that makes PageXray generate the wromng URL and we end up with two pages
// in sitespeed.io if we run in SPA mode
const url = parse(request.url + (request.urlFragment ?? ''), true);
const url = request.url + (request.urlFragment ?? '');

const postData = parsePostData(
getHeaderValue(request.headers, 'Content-Type'),
Expand All @@ -200,8 +199,8 @@ function createHarBuilder(options) {

const req = {
method: request.method,
url: format(url),
queryString: toNameValuePairs(url.query),
url,
queryString: parseQueryString(url),
postData,
headersSize: -1,
bodySize: isEmpty(request.postData) ? 0 : request.postData.length,
Expand Down
14 changes: 11 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parse } from 'node:url';
import debug from 'debug';
const log = debug('chrome-har');

Expand All @@ -23,8 +22,17 @@ export function toNameValuePairs(object) {
}, []);
}
export function parseUrlEncoded(data) {
const params = parse(`?${data}`, true).query;
return toNameValuePairs(params);
return [...new URLSearchParams(data)].map(([name, value]) => ({
name,
value
}));
}
export function parseQueryString(url) {
const withoutFragment = url.split('#')[0];
const queryStart = withoutFragment.indexOf('?');
return queryStart === -1
? []
: parseUrlEncoded(withoutFragment.slice(queryStart + 1));
}
export function parsePostData(contentType, postData) {
if (isEmpty(contentType) || isEmpty(postData)) {
Expand Down
27 changes: 27 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava';
import * as fs from 'node:fs/promises';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { harFromMessages, harFromMessagesAsync } from '../index.js';
import { fileURLToPath } from 'node:url';

Expand Down Expand Up @@ -788,3 +789,29 @@ test('Has no duplicate header names', async t => {
}
}
});

test('Keeps the URL as Chrome sent it', async t => {
const har = await parsePerflog(perflog('ryan.json'));
const entry = har.log.entries.find(e =>
e.request.url.startsWith('https://cdn.polyfill.io/v2/polyfill.min.js')
);
t.true(entry.request.url.includes('IntersectionObserver|always'));
t.deepEqual(
entry.request.queryString.find(q => q.name === 'features').value,
new URL(entry.request.url).searchParams.get('features')
);
});

test('Does not use deprecated Node.js APIs', t => {
const script = `
import fs from 'node:fs';
import { harFromMessages } from ${JSON.stringify(path.resolve(__dirname, '../index.js'))};
harFromMessages(JSON.parse(fs.readFileSync(${JSON.stringify(perflog('ryan.json'))}, 'utf8')));
`;
const result = spawnSync(
process.execPath,
['--throw-deprecation', '--input-type=module', '--eval', script],
{ encoding: 'utf8' }
);
t.is(result.status, 0, result.stderr);
});
Loading