UNPKG

@datadome/module-nextjs

Version:

DataDome module for Next.js applications

126 lines 4.55 kB
export function convertHeadersToMap(reqHeaders, dataDomeResHeaders, listKey) { const map = new Map(); const list = dataDomeResHeaders.get(listKey); if (!list) { return map; } for (const header of list.split(' ')) { const value = dataDomeResHeaders.get(header); if (value) { if (header.toLowerCase() === 'set-cookie' && /domain=\.vercel\.app/i.test(value)) { map.set(header, value.replace(/domain=\.vercel\.app/i, `Domain=${reqHeaders.get('host')}`)); } else { map.set(header, value); } } } return map; } export function getHeadersList(req) { return [...req.headers.keys()].join(','); } export function getAuthorizationLength(req) { const authorization = req.headers.get('authorization'); return authorization === null || authorization === void 0 ? void 0 : authorization.length; } export function stringify(obj) { return obj ? Object.keys(obj) .reduce((acc, key) => { const value = obj[key]; if (!value) { return acc; } acc.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); return acc; }, []) .join('&') : ''; } export function getCookieData(cookies) { const datadomeCookie = cookies.get('datadome'); if (datadomeCookie !== undefined) { return datadomeCookie.value; } return ''; } export function isGraphQLRequest({ url, method, bodyExists, contentType, }) { if (method === 'POST' && bodyExists == true && (contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json'))) { return url.pathname.toLowerCase().includes('graphql'); } return false; } export function isGraphQLDataAvailable(graphQLData) { return graphQLData != null && graphQLData['count'] > 0; } function getGraphQLQueryStringFromQueryParams(fullUrl) { return fullUrl.searchParams.get('query'); } async function getGraphQLQueryStringFromBody(request, maximumBodySize) { const regex = /"query"\s*:\s*(".*)/; const textDecoder = new TextDecoder(); if (request.body === null) { return null; } const reader = request.body.getReader(); const chunkSize = 1024; let iteration = 0; let bodyString = ''; let match = null; while (true) { if (iteration * chunkSize >= maximumBodySize) { break; } const { value, done } = await reader.read(); if (value === undefined && done === true) { break; } const chunk = textDecoder.decode(value, { stream: !done }); bodyString += chunk; if (bodyString.length > 2 * chunkSize) { bodyString = bodyString.slice(-2 * chunkSize); } match = bodyString.match(regex); if (match !== null && match.length > 0) { return match[1]; } if (done === true) { break; } iteration += 1; } return null; } export async function collectGraphQL(request, fullUrl, maximumBodySize) { var _a, _b, _c, _d; const result = { name: '', type: 'query', count: 0, }; let queryString; queryString = getGraphQLQueryStringFromQueryParams(fullUrl); if (queryString == null) { const clonedRequest = request.clone(); queryString = await getGraphQLQueryStringFromBody(clonedRequest, maximumBodySize); } if (queryString == null) { return result; } const regex = /(?<operationType>query|mutation|subscription)\s*(?<operationName>[A-Za-z_][A-Za-z0-9_]*)?\s*[({@]/gm; const matches = Array.from(queryString.matchAll(regex)); let matchLength = matches.length; if (matchLength > 0) { result.type = ((_b = (_a = matches[0].groups) === null || _a === void 0 ? void 0 : _a.operationType) !== null && _b !== void 0 ? _b : 'query'); result.name = (_d = (_c = matches[0].groups) === null || _c === void 0 ? void 0 : _c.operationName) !== null && _d !== void 0 ? _d : ''; } else { const shorthandSyntaxRegex = /"(?<operationType>(?:query|mutation|subscription))?\s*(?<operationName>[A-Za-z_][A-Za-z0-9_]*)?\s*[({@]/gm; const shorthandSyntaxMatches = Array.from(queryString.matchAll(shorthandSyntaxRegex)); matchLength = shorthandSyntaxMatches.length; } result['count'] = matchLength; return result; } //# sourceMappingURL=utils.js.map