next-csrf
Version:
CSRF mitigation library for Next.js
1 lines • 4.73 kB
JSON
{"ast":null,"code":"\"use strict\";\n\nvar _interopRequireDefault = require(\"@babel/runtime/helpers/interopRequireDefault\");\n\nexports.__esModule = true;\nexports.closePing = closePing;\nexports.setupPing = setupPing;\nexports.currentPage = void 0;\n\nvar _unfetch = _interopRequireDefault(require(\"next/dist/build/polyfills/unfetch\"));\n\nvar _eventsource = require(\"./error-overlay/eventsource\");\n/* global location */\n\n\nvar evtSource;\nvar currentPage;\nexports.currentPage = currentPage;\n\nfunction closePing() {\n if (evtSource) evtSource.close();\n evtSource = null;\n}\n\nfunction setupPing(assetPrefix, pathnameFn, retry) {\n var pathname = pathnameFn(); // Make sure to only create new EventSource request if page has changed\n\n if (pathname === currentPage && !retry) return;\n exports.currentPage = currentPage = pathname; // close current EventSource connection\n\n closePing();\n var url = \"\".concat(assetPrefix, \"/_next/webpack-hmr?page=\").concat(currentPage);\n evtSource = (0, _eventsource.getEventSourceWrapper)({\n path: url,\n timeout: 5000,\n ondemand: 1\n });\n evtSource.addMessageListener(function (event) {\n if (event.data.indexOf('{') === -1) return;\n\n try {\n var payload = JSON.parse(event.data);\n\n if (payload.invalid) {\n // Payload can be invalid even if the page does not exist.\n // So, we need to make sure it exists before reloading.\n (0, _unfetch[\"default\"])(location.href, {\n credentials: 'same-origin'\n }).then(function (pageRes) {\n if (pageRes.status === 200) {\n location.reload();\n }\n });\n }\n } catch (err) {\n console.error('on-demand-entries failed to parse response', err);\n }\n });\n}","map":{"version":3,"sources":["../../../client/dev/on-demand-entries-utils.js"],"names":["evtSource","pathname","pathnameFn","closePing","url","assetPrefix","path","timeout","ondemand","event","payload","JSON","location","credentials","pageRes","console"],"mappings":";;;;;;;;;AAEA,IAAA,QAAA,GAAA,sBAAA,CAAA,OAAA,CAAA,mCAAA,CAAA,CAAA;;AACA,IAAA,YAAA,GAAA,OAAA,CAAA,6BAAA,CAAA;AAHA;;;AAKA,IAAA,SAAA;AACO,IAAA,WAA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4CAAAA,EAAAA,GAAAA;AAEH;AAlBDf,GAAAA;AAmBD","sourcesContent":["/* global location */\n\nimport fetch from 'next/dist/build/polyfills/unfetch'\nimport { getEventSourceWrapper } from './error-overlay/eventsource'\n\nlet evtSource\nexport let currentPage\n\nexport function closePing() {\n if (evtSource) evtSource.close()\n evtSource = null\n}\n\nexport function setupPing(assetPrefix, pathnameFn, retry) {\n const pathname = pathnameFn()\n\n // Make sure to only create new EventSource request if page has changed\n if (pathname === currentPage && !retry) return\n currentPage = pathname\n // close current EventSource connection\n closePing()\n\n const url = `${assetPrefix}/_next/webpack-hmr?page=${currentPage}`\n evtSource = getEventSourceWrapper({ path: url, timeout: 5000, ondemand: 1 })\n\n evtSource.addMessageListener((event) => {\n if (event.data.indexOf('{') === -1) return\n try {\n const payload = JSON.parse(event.data)\n if (payload.invalid) {\n // Payload can be invalid even if the page does not exist.\n // So, we need to make sure it exists before reloading.\n fetch(location.href, {\n credentials: 'same-origin',\n }).then((pageRes) => {\n if (pageRes.status === 200) {\n location.reload()\n }\n })\n }\n } catch (err) {\n console.error('on-demand-entries failed to parse response', err)\n }\n })\n}\n"]},"metadata":{},"sourceType":"script"}