Skip to content

Commit

Permalink
fix: use package.cjs import instead of requiring package.json (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister authored Dec 5, 2024
1 parent b4cde00 commit fad8a1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions src/package.cjs
Original file line number Diff line number Diff line change
@@ -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');
18 changes: 18 additions & 0 deletions test/core/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!';
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit fad8a1e

Please sign in to comment.