UNPKG

@datadome/module-nextjs

Version:

DataDome module for Next.js applications

189 lines 6.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertHeadersToMap = convertHeadersToMap; exports.getHeadersList = getHeadersList; exports.getAuthorizationLength = getAuthorizationLength; exports.stringify = stringify; exports.getCookieData = getCookieData; exports.isGraphQLRequest = isGraphQLRequest; exports.isGraphQLDataAvailable = isGraphQLDataAvailable; exports.collectGraphQL = collectGraphQL; exports.parseCookieAttributes = parseCookieAttributes; 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') { const cookies = value.split(',').map((cookie) => cookie.trim()); for (const cookie of cookies) { if (!cookie) continue; const host = reqHeaders.get('host'); let processedCookie = cookie; if (host && /domain=\.vercel\.app/i.test(cookie)) { processedCookie = cookie.replace(/domain=\.vercel\.app/i, `Domain=${host}`); } const existingCookies = map.get('set-cookie') || ''; map.set('set-cookie', existingCookies ? `${existingCookies},${processedCookie}` : processedCookie); } } else { map.set(header, value); } } } return map; } function getHeadersList(req) { return [...req.headers.keys()].join(','); } function getAuthorizationLength(req) { const authorization = req.headers.get('authorization'); return authorization === null || authorization === void 0 ? void 0 : authorization.length; } function stringify(obj) { return obj ? Object.keys(obj) .reduce((acc, key) => { const value = obj[key]; if (value == null) { return acc; } acc.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); return acc; }, []) .join('&') : ''; } function getCookieData(cookies) { const datadomeCookie = cookies.get('datadome'); if (datadomeCookie !== undefined) { return datadomeCookie.value; } return ''; } 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; } 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; const { value, done } = await reader.read(); if (value === undefined && done === true) { return null; } const chunk = textDecoder.decode(value, { stream: !done }); while (iteration * chunkSize < maximumBodySize) { bodyString += chunk.slice(iteration * chunkSize, (iteration + 2) * chunkSize); match = bodyString.match(regex); if (match !== null && match.length > 0) { return match[1]; } iteration += 1; } return null; } 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; } function parseCookieAttributes(cookieString) { const [nameValue, ...attrs] = cookieString.split(';'); const [name, val] = nameValue.split('='); if (!name || !val) { return null; } const options = {}; attrs.forEach((attr) => { const [k, v] = attr.trim().split('='); const key = k.toLowerCase(); switch (key) { case 'secure': options.secure = true; break; case 'httponly': options.httpOnly = true; break; case 'domain': if (v) options.domain = v; break; case 'path': if (v) options.path = v; break; case 'samesite': if (v) { const sameSiteValue = v.toLowerCase(); options.sameSite = sameSiteValue; } break; case 'max-age': if (v) options.maxAge = parseInt(v, 10); break; case 'expires': if (v) options.expires = new Date(v); break; case 'partitioned': options.partitioned = true; break; } }); return { name: name.trim(), value: val.trim(), options, }; } //# sourceMappingURL=utils.js.map