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

chore(headless)!: remove headless fetch & crypto polyfills #4358

Merged
merged 11 commits into from
Sep 5, 2024
62 changes: 39 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions packages/atomic-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
"@coveo/atomic": "2.78.0"
},
"devDependencies": {
"@coveo/release": "1.0.0",
"@coveo/headless": "2.80.0",
"@coveo/release": "1.0.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-replace": "^5.0.0",
"@rollup/plugin-terser": "0.4.4",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "20.14.12",
"@types/react": "18.3.3",
Expand All @@ -45,8 +47,7 @@
"react": "18.3.1",
"react-dom": "18.3.1",
"rollup": "3.29.4",
"rollup-plugin-polyfill-node": "^0.13.0",
"@rollup/plugin-terser": "0.4.4"
"rollup-plugin-polyfill-node": "^0.13.0"
},
"peerDependencies": {
"@coveo/headless": "2.80.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/atomic-react/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
Expand Down Expand Up @@ -54,6 +55,7 @@ const outputIIFECommerce = ({minify}) => ({
});

const plugins = [
json(),
nodePolyfills(),
typescript({tsconfig: 'tsconfig.iife.json'}),
commonjs(),
Expand Down
3 changes: 1 addition & 2 deletions packages/headless-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"publint": "0.2.9",
"rimraf": "5.0.9",
"ts-jest": "29.2.3",
"typescript": "5.4.5",
"undici": "5.28.4"
"typescript": "5.4.5"
},
"peerDependencies": {
"react": "^18",
Expand Down
21 changes: 12 additions & 9 deletions packages/headless/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ function resolveEsm(moduleName) {
);
}

function resolveBrowser(moduleName) {
const packageJsonPath = require.resolve(`${moduleName}/package.json`);
const packageJson = require(packageJsonPath);
return resolve(
dirname(packageJsonPath),
packageJson['browser'] || packageJson['main']
);
}

/**
* @param {import('esbuild').BuildOptions} options
* @returns {Promise<import('esbuild').BuildResult>}
Expand All @@ -190,10 +199,7 @@ async function buildBrowserConfig(options, outDir) {
plugins: [
alias({
'coveo.analytics': resolveEsm('coveo.analytics'),
'@coveo/please-give-me-fetch': resolve(
'./ponyfills',
'fetch-ponyfill-browser.js'
alexprudhomme marked this conversation as resolved.
Show resolved Hide resolved
),
pino: resolveBrowser('pino'),
'@coveo/pendragon': resolve('./ponyfills', 'magic-cookie-browser.js'),
}),
...(options.plugins || []),
Expand Down Expand Up @@ -227,6 +233,7 @@ const nodeEsm = Object.entries(useCaseEntries).map((entry) => {
entryPoints: [entryPoint],
outfile,
format: 'esm',
external: ['pino'],
},
dir
);
Expand All @@ -244,11 +251,7 @@ async function buildNodeConfig(options, outDir) {
treeShaking: true,
plugins: [
alias({
'coveo.analytics': require.resolve('coveo.analytics'),
'@coveo/please-give-me-fetch': resolve(
'./ponyfills',
'fetch-ponyfill-node.js'
),
'coveo.analytics': resolveEsm('coveo.analytics'),
'@coveo/pendragon': resolve('./ponyfills', 'magic-cookie-node.js'),
}),
],
Expand Down
3 changes: 0 additions & 3 deletions packages/headless/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ process.env.TZ = 'Australia/Eucla';
module.exports = {
preset: 'ts-jest',
silent: true,
setupFiles: ['./jest.setup.js'],
moduleNameMapper: {
'@coveo/please-give-me-fetch':
'<rootDir>/ponyfills/fetch-ponyfill-browser.js',
'@coveo/pendragon': '<rootDir>/ponyfills/magic-cookie-node.js',
},
};
3 changes: 0 additions & 3 deletions packages/headless/jest.setup.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/headless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@
"node-abort-controller": "^3.0.0",
"pino": "8.21.0",
"redux-thunk": "3.1.0",
"ts-debounce": "4.0.0",
"undici": "5.28.4"
"ts-debounce": "4.0.0"
},
"devDependencies": {
"@coveo/release": "1.0.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/headless/ponyfills/fetch-ponyfill-browser.js

This file was deleted.

2 changes: 0 additions & 2 deletions packages/headless/ponyfills/fetch-ponyfill-node.js

This file was deleted.

This file was deleted.

8 changes: 2 additions & 6 deletions packages/headless/src/api/platform-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//TODO V3: remove this import, global fetch is a requirement now.
//@ts-expect-error package is just an alias.
import fetch from '@coveo/please-give-me-fetch';
import * as BackOff from 'exponential-backoff';
import pino from 'pino';
import {allValidPlatformCombination} from '../test/platform-url';
Expand All @@ -19,10 +16,9 @@ import {
PreprocessRequest,
} from './preprocess-request';

jest.mock('@coveo/please-give-me-fetch');

const {Response} = jest.requireActual('node-fetch');
const mockFetch = fetch as jest.Mock;
global.fetch = jest.fn();
const mockFetch = global.fetch as jest.Mock;

describe('url helper', () => {
it('return the correct #platformUrl()', () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/headless/src/api/platform-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//TODO V3: remove this import, global fetch is a requirement now.
//@ts-expect-error package is just an alias.
import fetch from '@coveo/please-give-me-fetch';
import {backOff} from 'exponential-backoff';
import {Logger} from 'pino';
import {DisconnectedError, ExpiredTokenError} from '../utils/errors';
Expand Down
3 changes: 0 additions & 3 deletions packages/headless/src/case-assist.index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';

polyfillCryptoNode();
// 3rd Party Libraries
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {Relay} from '@coveo/relay';
Expand Down
2 changes: 0 additions & 2 deletions packages/headless/src/commerce.index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';
import * as Selectors from './selectors/commerce-selectors.index';
import * as HighlightUtils from './utils/highlight';

export type {HighlightKeyword} from './utils/highlight';

polyfillCryptoNode();
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {Relay} from '@coveo/relay';

Expand Down
3 changes: 0 additions & 3 deletions packages/headless/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';
import * as HighlightUtils from './utils/highlight';

polyfillCryptoNode();

// 3rd Party Libraries
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {AnalyticsClientSendEventHook} from 'coveo.analytics';
Expand Down
2 changes: 0 additions & 2 deletions packages/headless/src/insight.index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';
import * as HighlightUtils from './utils/highlight';

polyfillCryptoNode();
// 3rd Party Libraries
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {Relay} from '@coveo/relay';
Expand Down
3 changes: 0 additions & 3 deletions packages/headless/src/product-listing.index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';

polyfillCryptoNode();
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {Relay} from '@coveo/relay';

Expand Down
4 changes: 0 additions & 4 deletions packages/headless/src/product-recommendation.index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';

polyfillCryptoNode();

export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {Relay} from '@coveo/relay';

Expand Down
2 changes: 0 additions & 2 deletions packages/headless/src/recommendation.index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';
import * as HighlightUtils from './utils/highlight';

export {HighlightUtils};

polyfillCryptoNode();
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {Relay} from '@coveo/relay';

Expand Down
4 changes: 0 additions & 4 deletions packages/headless/src/ssr.index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {polyfillCryptoNode} from './api/analytics/analytics-crypto-polyfill';

polyfillCryptoNode();

// 3rd Party Libraries
export type {Unsubscribe, Middleware} from '@reduxjs/toolkit';
export type {AnalyticsClientSendEventHook} from 'coveo.analytics';
Expand Down
31 changes: 31 additions & 0 deletions patches/coveo.analytics+2.30.26.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
diff --git a/node_modules/coveo.analytics/dist/browser.mjs b/node_modules/coveo.analytics/dist/browser.mjs
index 3de0ed7..b7d2c0e 100644
--- a/node_modules/coveo.analytics/dist/browser.mjs
+++ b/node_modules/coveo.analytics/dist/browser.mjs
@@ -1093,8 +1093,6 @@ class NoopAnalyticsClient {
}
}

-const fetch$1 = window.fetch;
-
class AnalyticsFetchClient {
constructor(opts) {
this.opts = opts;
@@ -1114,7 +1112,7 @@ class AnalyticsFetchClient {
const _a = Object.assign(Object.assign({}, defaultOptions), (preprocessRequest ? yield preprocessRequest(defaultOptions, 'analyticsFetch') : {})), { url } = _a, fetchData = __rest(_a, ["url"]);
let response;
try {
- response = yield fetch$1(url, fetchData);
+ response = yield fetch(url, fetchData);
}
catch (error) {
console.error('An error has occured when sending the event.', error);
@@ -1142,7 +1140,7 @@ class AnalyticsFetchClient {
return __awaiter(this, void 0, void 0, function* () {
const { baseUrl } = this.opts;
const url = `${baseUrl}/analytics/visit`;
- yield fetch$1(url, { headers: this.getHeaders(), method: 'DELETE' });
+ yield fetch(url, { headers: this.getHeaders(), method: 'DELETE' });
});
}
shouldAppendVisitorId(eventType) {
Loading
Loading