From c67ce58380a1d9228cc2aad8121e9038b6206abe Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Sat, 5 Oct 2024 16:42:38 +0200 Subject: [PATCH] Remove dayjs dependency (#128) --- index.js | 5 ++--- lib/cookies.js | 21 ++++++++++++++++----- lib/entryFromResponse.js | 3 +-- package-lock.json | 6 ------ package.json | 1 - 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 6b83f16..315fe0b 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,6 @@ const { name, version, homepage } = require('./package'); const urlParser = require('url'); const crypto = require('crypto'); -const dayjs = require('dayjs'); const debug = require('debug')(name); const ignoredEvents = require('./lib/ignoredEvents'); const { parseRequestCookies, formatCookie } = require('./lib/cookies'); @@ -28,7 +27,7 @@ function addFromFirstRequest(page, params) { if (!page.__timestamp) { page.__wallTime = params.wallTime; page.__timestamp = params.timestamp; - page.startedDateTime = dayjs.unix(params.wallTime).toISOString(); //epoch float64, eg 1440589909.59248 + page.startedDateTime = new Date(params.wallTime * 1000).toISOString(); // URL is better than blank, and it's what devtools uses. page.title = page.title === '' ? params.request.url : page.title; } @@ -245,7 +244,7 @@ module.exports = { // (see https://cs.chromium.org/chromium/src/third_party/WebKit/Source/platform/network/ResourceLoadTiming.h?q=requestTime+package:%5Echromium$&dr=CSs&l=84) const entrySecs = page.__wallTime + (params.timestamp - page.__timestamp); - entry.startedDateTime = dayjs.unix(entrySecs).toISOString(); + entry.startedDateTime = new Date(entrySecs * 1000).toISOString(); } break; diff --git a/lib/cookies.js b/lib/cookies.js index ecd8407..8a9a5c9 100644 --- a/lib/cookies.js +++ b/lib/cookies.js @@ -1,16 +1,27 @@ const Cookie = require('tough-cookie').Cookie; -const dayjs = require('dayjs'); function formatCookie(cookie) { + let expiresISO; + + if (cookie.expires instanceof Date) { + expiresISO = cookie.expires.toISOString(); + } else if (cookie.expires === 'Infinity' || cookie.expires === null) { + expiresISO = null; + } else { + const date = new Date(cookie.expires); + if (!isNaN(date)) { + expiresISO = date.toISOString(); + } else { + // There's no date + expiresISO = null; + } + } return { name: cookie.key || cookie.name, value: cookie.value, path: cookie.path || undefined, // must be undefined, not null, to exclude empty path domain: cookie.domain || undefined, // must be undefined, not null, to exclude empty domain - expires: - cookie.expires === 'Infinity' - ? undefined - : dayjs(cookie.expires).toISOString(), + expires: expiresISO, httpOnly: cookie.httpOnly, secure: cookie.secure }; diff --git a/lib/entryFromResponse.js b/lib/entryFromResponse.js index 6a68f30..2c59efc 100644 --- a/lib/entryFromResponse.js +++ b/lib/entryFromResponse.js @@ -1,5 +1,4 @@ 'use strict'; -const dayjs = require('dayjs'); const max = Math.max; const { @@ -186,7 +185,7 @@ module.exports = function(entry, response, page, options) { // (see https://cs.chromium.org/chromium/src/third_party/WebKit/Source/platform/network/ResourceLoadTiming.h?q=requestTime+package:%5Echromium$&dr=CSs&l=84) const entrySecs = page.__wallTime + (timing.requestTime - page.__timestamp); - entry.startedDateTime = dayjs.unix(entrySecs).toISOString(); + entry.startedDateTime = new Date(entrySecs * 1000).toISOString(); const queuedMillis = (timing.requestTime - entry.__requestWillBeSentTime) * 1000; diff --git a/package-lock.json b/package-lock.json index 9ad0dcb..2b4585f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.13.5", "license": "MIT", "dependencies": { - "dayjs": "1.11.7", "debug": "4.3.4", "tough-cookie": "5.0.0" }, @@ -1854,11 +1853,6 @@ "node": ">=0.10.0" } }, - "node_modules/dayjs": { - "version": "1.11.7", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.7.tgz", - "integrity": "sha512-+Yw9U6YO5TQohxLcIkrXBeY73WP3ejHWVvx8XCk3gxvQDCTEmS48ZrSZCKciI7Bhl/uCMyxYtE9UqRILmFphkQ==" - }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index d425ad0..63f8e9d 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ "prettier": "~1.11.1" }, "dependencies": { - "dayjs": "1.11.7", "debug": "4.3.4", "tough-cookie": "5.0.0" }