From b345985e9e1807ea55519ddf12c65a9b837ecac7 Mon Sep 17 00:00:00 2001 From: Dominique Pfister Date: Thu, 5 Dec 2024 14:22:37 +0100 Subject: [PATCH] fix: use package.cjs import instead of requiring package.json --- src/core/request.js | 4 +--- src/package.cjs | 12 ++++++++++++ test/core/index.test.js | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/package.cjs diff --git a/src/core/request.js b/src/core/request.js index ef03dff..6f7120e 100644 --- a/src/core/request.js +++ b/src/core/request.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -import { createRequire } from 'module'; import { types } from 'util'; import { Readable } from 'stream'; import tls from 'tls'; @@ -25,8 +24,7 @@ import lock from './lock.js'; import { isFormData, FormDataSerializer } from '../common/formData.js'; import { isPlainObject } from '../common/utils.js'; -const require = createRequire(import.meta.url); -const pkg = require('../../package.json'); +import pkg from '../package.cjs'; const { version } = pkg; diff --git a/src/package.cjs b/src/package.cjs new file mode 100644 index 0000000..e3591bf --- /dev/null +++ b/src/package.cjs @@ -0,0 +1,12 @@ +/* + * Copyright 2021 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +module.exports = require('../package.json'); diff --git a/test/core/index.test.js b/test/core/index.test.js index 6f43d55..97f0626 100644 --- a/test/core/index.test.js +++ b/test/core/index.test.js @@ -26,6 +26,7 @@ import { isReadableStream, parseMultiPartFormData } from '../utils.js'; import { AbortController } from '../../src/fetch/abort.js'; import { RequestAbortedError } from '../../src/core/errors.js'; import core from '../../src/core/index.js'; +import pkg from '../../src/package.cjs'; import Server from '../server.js'; const HELLO_WORLD = 'Hello, World!'; @@ -242,6 +243,23 @@ describe('Core Tests', () => { assert((ts1 - ts0) < 1000 * 1.1); }); + it('standard user agent contains package version', async () => { + const customCtx = context({ + rejectUnauthorized: false, + }); + try { + const resp = await customCtx.request(`${server.origin}/inspect`); + assert.strictEqual(resp.statusCode, 200); + assert.strictEqual(resp.headers['content-type'], 'application/json'); + + const buf = await readStream(resp.readable); + const json = JSON.parse(buf); + assert.strictEqual(json.headers['user-agent'], `adobe-fetch/${pkg.version}`); + } finally { + await customCtx.reset(); + } + }); + it('overriding user-agent works (context)', async () => { const customUserAgent = 'custom-agent'; const customCtx = context({