This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,25 @@
import type { InsightsAdditionalEventParams } from "../types";
export type WithAdditionalParams<TEventType> =
| InsightsAdditionalEventParams
| TEventType;
export function extractAdditionalParams<TEventType extends { index: string }>(
params: Array<InsightsAdditionalEventParams | TEventType>
): { events: TEventType[]; additionalParams?: InsightsAdditionalEventParams } {
return params.reduce(
({ events, additionalParams }, param) => {
// Real events all have `index` as a mandatory parameter, which we
// can rely on to distinguish them from additional parameters
if ("index" in param) {
return { additionalParams, events: [...events, param] };
}
return { events, additionalParams: param };
},
{
events: [] as TEventType[],
additionalParams: undefined as InsightsAdditionalEventParams | undefined
}
);
}

View File

@@ -0,0 +1,43 @@
export const supportsCookies = (): boolean => {
try {
return Boolean(navigator.cookieEnabled);
} catch (e) {
return false;
}
};
export const supportsSendBeacon = (): boolean => {
try {
return Boolean(navigator.sendBeacon);
} catch (e) {
return false;
}
};
export const supportsXMLHttpRequest = (): boolean => {
try {
return Boolean(XMLHttpRequest);
} catch (e) {
return false;
}
};
export const supportsNodeHttpModule = (): boolean => {
try {
/* eslint-disable @typescript-eslint/no-var-requires */
const { request: nodeHttpRequest } = require("http");
const { request: nodeHttpsRequest } = require("https");
/* eslint-enable */
return Boolean(nodeHttpRequest) && Boolean(nodeHttpsRequest);
} catch (e) {
return false;
}
};
export const supportsNativeFetch = (): boolean => {
try {
return fetch !== undefined;
} catch (e) {
return false;
}
};

View File

@@ -0,0 +1,29 @@
import {
supportsNativeFetch,
supportsSendBeacon,
supportsXMLHttpRequest
} from "./featureDetection";
import type { RequestFnType } from "./request";
import {
requestWithNativeFetch,
requestWithSendBeacon,
requestWithXMLHttpRequest
} from "./request";
export function getRequesterForBrowser(): RequestFnType {
if (supportsSendBeacon()) {
return requestWithSendBeacon;
}
if (supportsXMLHttpRequest()) {
return requestWithXMLHttpRequest;
}
if (supportsNativeFetch()) {
return requestWithNativeFetch;
}
throw new Error(
"Could not find a supported HTTP request client in this environment."
);
}

View File

@@ -0,0 +1,20 @@
import {
supportsNodeHttpModule,
supportsNativeFetch
} from "./featureDetection";
import type { RequestFnType } from "./request";
import { requestWithNodeHttpModule, requestWithNativeFetch } from "./request";
export function getRequesterForNode(): RequestFnType {
if (supportsNodeHttpModule()) {
return requestWithNodeHttpModule;
}
if (supportsNativeFetch()) {
return requestWithNativeFetch;
}
throw new Error(
"Could not find a supported HTTP request client in this environment."
);
}

15
node_modules/search-insights/lib/utils/index.ts generated vendored Normal file
View File

@@ -0,0 +1,15 @@
// use theses type checking helpers to avoid mistyping "undefind", I mean "undfined"
export const isUndefined = (value: any): value is undefined =>
typeof value === "undefined";
export const isString = (value: any): value is string =>
typeof value === "string";
export const isNumber = (value: any): value is number =>
typeof value === "number";
/* eslint-disable @typescript-eslint/ban-types */
export const isFunction = (value: any): value is Function =>
typeof value === "function";
/* eslint-enable */
export * from "./extractAdditionalParams";
export * from "./featureDetection";

98
node_modules/search-insights/lib/utils/request.ts generated vendored Normal file
View File

@@ -0,0 +1,98 @@
import type { request as nodeRequest } from "http";
import type { UrlWithStringQuery } from "url";
export type RequestFnType = (
url: string,
data: Record<string, unknown>
) => Promise<boolean>;
export const requestWithXMLHttpRequest: RequestFnType = (url, data) => {
return new Promise((resolve, reject) => {
const serializedData = JSON.stringify(data);
const req = new XMLHttpRequest();
req.addEventListener("readystatechange", () => {
if (req.readyState === 4 && req.status === 200) {
resolve(true);
} else if (req.readyState === 4) {
resolve(false);
}
});
/* eslint-disable prefer-promise-reject-errors */
req.addEventListener("error", () => reject());
/* eslint-enable */
req.addEventListener("timeout", () => resolve(false));
req.open("POST", url);
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("Content-Length", `${serializedData.length}`);
req.send(serializedData);
});
};
export const requestWithSendBeacon: RequestFnType = (url, data) => {
const serializedData = JSON.stringify(data);
const beacon = navigator.sendBeacon(url, serializedData);
return Promise.resolve(beacon ? true : requestWithXMLHttpRequest(url, data));
};
export const requestWithNodeHttpModule: RequestFnType = (url, data) => {
return new Promise((resolve, reject) => {
const serializedData = JSON.stringify(data);
/* eslint-disable @typescript-eslint/no-var-requires */
const { protocol, host, path }: UrlWithStringQuery =
require("url").parse(url);
/* eslint-enable */
const options = {
protocol,
host,
path,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": serializedData.length
}
};
const { request }: { request: typeof nodeRequest } = url.startsWith(
"https://"
)
? require("https")
: require("http");
const req = request(options, ({ statusCode }) => {
if (statusCode === 200) {
resolve(true);
} else {
resolve(false);
}
});
req.on("error", (error: any) => {
/* eslint-disable no-console */
console.error(error);
/* eslint-enable */
reject(error);
});
req.on("timeout", () => resolve(false));
req.write(serializedData);
req.end();
});
};
export const requestWithNativeFetch: RequestFnType = (url, data) => {
return new Promise((resolve, reject) => {
fetch(url, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
})
.then((response) => {
resolve(response.status === 200);
})
.catch((e) => {
reject(e);
});
});
};

15
node_modules/search-insights/lib/utils/uuid.ts generated vendored Normal file
View File

@@ -0,0 +1,15 @@
/**
* Create UUID according to
* https://www.ietf.org/rfc/rfc4122.txt.
*
* @returns Generated UUID.
*/
export function createUUID(): string {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
/* eslint-disable no-bitwise */
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
/* eslint-enable */
return v.toString(16);
});
}