UNPKG

bippy

Version:

hack into react internals

9 lines 4.7 kB
/** * @license bippy * * Copyright (c) Aiden Bai * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./rdt-hook.cjs`),t=require(`./core.cjs`),n=require(`./get-source.cjs`),r=1e3,i=/wsToken = "([^"]+)"/,a=/\.(?:tsx|ts|jsx|js|mjs|cjs|css)$/,o=/^(?:\.\/)?\/?\([a-z][a-z0-9-]*\)\//,s=e=>{if(typeof e!=`object`||!e||!(`getConstants`in e))return null;let t=e.getConstants;if(typeof t!=`function`)return null;let n;try{n=t.call(e)}catch{return null}if(typeof n!=`object`||!n||!(`scriptURL`in n))return null;let r=n.scriptURL;return typeof r==`string`?r:null},c=()=>{if(typeof globalThis.__turboModuleProxy==`function`){let e;try{e=globalThis.__turboModuleProxy(`SourceCode`)}catch{e=null}let t=s(e);if(t)return t}let e=globalThis.nativeModuleProxy?.SourceCode;return s(e)},l=e=>e.replace(`//&`,`?`),u=e=>{let t;try{t=new URL(l(e))}catch{return null}let n=decodeURIComponent(t.pathname);return n.startsWith(`/`)&&(n=n.slice(1)),n.endsWith(`.bundle`)&&(n=n.slice(0,-7)),n.length>0?n:null},d=(e,t)=>{if(Array.isArray(e))for(let n of e){if(typeof n!=`object`||!n||!(`sourceURL`in n)||typeof n.sourceURL!=`string`)continue;let e=u(n.sourceURL);!e||e.includes(`node_modules`)||t.push(e)}},f=e=>{let t;try{t=JSON.parse(e)}catch{return[]}if(typeof t!=`object`||!t||!(`type`in t)||t.type!==`update`||!(`body`in t)||typeof t.body!=`object`||t.body===null)return[];let n=t.body;if(`isInitialUpdate`in n&&n.isInitialUpdate===!0)return[];let r=[];return`added`in n&&d(n.added,r),`modified`in n&&d(n.modified,r),r},p=(e,t={})=>{if(typeof WebSocket>`u`)return null;let n=t.bundleUrl??c();if(!n)return null;let i;try{let e=new URL(n);i=`${e.protocol===`https:`?`wss`:`ws`}://${e.host}/hot`}catch{return null}let a=!1,o=null,s,l=()=>{a||(s=setTimeout(u,r))},u=()=>{if(a)return;let t=new WebSocket(i);o=t,t.onopen=()=>{t.send(JSON.stringify({type:`register-entrypoints`,entryPoints:[n]}))},t.onmessage=t=>{let n=f(String(t.data));n.length>0&&e(n)},t.onclose=l};return u(),{dispose:()=>{a=!0,clearTimeout(s),o&&(o.onclose=null,o.close())}}},m=e=>{let t=n.r(e);return t=t.replace(o,``),t.startsWith(`./`)&&(t=t.slice(2)),t},h=e=>{let t=[];for(let n of e){if(n.includes(`node_modules`))continue;let e=m(n);a.test(e)&&t.push(e)}return t},g=e=>{if(typeof window>`u`)return null;let t=window.webpackHotUpdate_N_E;if(typeof t!=`function`)return null;let n=(n,r,i)=>{let a=h(Object.keys(r??{}));a.length>0&&e(a),t(n,r,i)};return window.webpackHotUpdate_N_E=n,{dispose:()=>{window.webpackHotUpdate_N_E===n&&(window.webpackHotUpdate_N_E=t)}}},_=e=>{let t;try{t=JSON.parse(e)}catch{return[]}if(typeof t!=`object`||!t||!(`type`in t)||t.type!==`update`||!(`updates`in t)||!Array.isArray(t.updates))return[];let n=[];for(let e of t.updates)typeof e!=`object`||!e||!(`type`in e)||e.type!==`js-update`||!(`acceptedPath`in e)||typeof e.acceptedPath!=`string`||n.push(e.acceptedPath);return n},v=async()=>{try{let e=await fetch(`/@vite/client`);if(!e.ok)return null;let t=await e.text();return i.exec(t)?.[1]??null}catch{return null}},y=async e=>{if(typeof window>`u`||typeof WebSocket>`u`)return null;let t=await v();if(!t)return null;let n=!1,i=null,a,o=()=>{n||(a=window.setTimeout(()=>{v().then(e=>{n||(e?s(e):o())})},r))},s=t=>{if(n)return;let r=location.protocol===`https:`?`wss`:`ws`,a=new WebSocket(`${r}://${location.host}/?token=${t}`,`vite-hmr`);i=a,a.onmessage=t=>{let n=_(String(t.data));n.length>0&&e(n)},a.onclose=o};return s(t),{dispose:()=>{n=!0,window.clearTimeout(a),i&&(i.onclose=null,i.close())}}},b=async t=>e.s()?g(t)||p(t)||y(t):null,x=(e,n)=>{if(n.size===0||!e.current)return[];let r=[];return t.traverseFiber(e.current,e=>{(n.has(e.type)||n.has(t.getType(e.type)))&&r.push(e)}),r},S=new Set,C=new WeakSet;let w=[],T=0;const E=e=>{let t=Date.now();t-T>1e4&&(w=[]),w.push(...e),T=t},D=()=>{if(Date.now()-T>1e4)return[];let e=[...w];return queueMicrotask(()=>{w=[]}),e},O=e=>{if(C.has(e))return;let t=e.scheduleRefresh;typeof t==`function`&&(C.add(e),e.scheduleRefresh=(n,r)=>{if(t.call(e,n,r),S.size===0)return;let i=Array.from(r.staleFamilies,e=>e.current),a=Array.from(r.updatedFamilies,e=>e.current),o={filePaths:D(),root:n,staleComponents:i,staleFibers:x(n,new Set(i)),updatedComponents:a,updatedFibers:x(n,new Set(a))};for(let e of S)e(o)})};let k=!1;const A=()=>{if(k)return;k=!0;let t=e.i();for(let e of t.renderers.values())O(e);e.u(O)};let j=null;const M=()=>{b(E).then(e=>{e&&(j?.dispose(),j=e)})},N=t=>{let{onRefresh:n}=t;return!n||!e.s()?e.m(()=>{}):(A(),S.size===0&&M(),S.add(n),e.m(()=>{S.delete(n)}))};exports.instrumentReactRefresh=N;