UNPKG

bippy

Version:

hack into react internals

9 lines 4.66 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. */ import{i as e,m as t,s as n,u as r}from"./rdt-hook.js";import{getType as i,traverseFiber as a}from"./core.js";import{r as o}from"./get-source.js";const s=1e3,c=/wsToken = "([^"]+)"/,l=/\.(?:tsx|ts|jsx|js|mjs|cjs|css)$/,u=/^(?:\.\/)?\/?\([a-z][a-z0-9-]*\)\//,d=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},f=()=>{if(typeof globalThis.__turboModuleProxy==`function`){let e;try{e=globalThis.__turboModuleProxy(`SourceCode`)}catch{e=null}let t=d(e);if(t)return t}let e=globalThis.nativeModuleProxy?.SourceCode;return d(e)},p=e=>e.replace(`//&`,`?`),m=e=>{let t;try{t=new URL(p(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},h=(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=m(n.sourceURL);!e||e.includes(`node_modules`)||t.push(e)}},g=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&&h(n.added,r),`modified`in n&&h(n.modified,r),r},_=(e,t={})=>{if(typeof WebSocket>`u`)return null;let n=t.bundleUrl??f();if(!n)return null;let r;try{let e=new URL(n);r=`${e.protocol===`https:`?`wss`:`ws`}://${e.host}/hot`}catch{return null}let i=!1,a=null,o,c=()=>{i||(o=setTimeout(l,s))},l=()=>{if(i)return;let t=new WebSocket(r);a=t,t.onopen=()=>{t.send(JSON.stringify({type:`register-entrypoints`,entryPoints:[n]}))},t.onmessage=t=>{let n=g(String(t.data));n.length>0&&e(n)},t.onclose=c};return l(),{dispose:()=>{i=!0,clearTimeout(o),a&&(a.onclose=null,a.close())}}},v=e=>{let t=o(e);return t=t.replace(u,``),t.startsWith(`./`)&&(t=t.slice(2)),t},y=e=>{let t=[];for(let n of e){if(n.includes(`node_modules`))continue;let e=v(n);l.test(e)&&t.push(e)}return t},b=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=y(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)}}},x=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},S=async()=>{try{let e=await fetch(`/@vite/client`);if(!e.ok)return null;let t=await e.text();return c.exec(t)?.[1]??null}catch{return null}},C=async e=>{if(typeof window>`u`||typeof WebSocket>`u`)return null;let t=await S();if(!t)return null;let n=!1,r=null,i,a=()=>{n||(i=window.setTimeout(()=>{S().then(e=>{n||(e?o(e):a())})},s))},o=t=>{if(n)return;let i=location.protocol===`https:`?`wss`:`ws`,o=new WebSocket(`${i}://${location.host}/?token=${t}`,`vite-hmr`);r=o,o.onmessage=t=>{let n=x(String(t.data));n.length>0&&e(n)},o.onclose=a};return o(t),{dispose:()=>{n=!0,window.clearTimeout(i),r&&(r.onclose=null,r.close())}}},w=async e=>n()?b(e)||_(e)||C(e):null,T=(e,t)=>{if(t.size===0||!e.current)return[];let n=[];return a(e.current,e=>{(t.has(e.type)||t.has(i(e.type)))&&n.push(e)}),n},E=new Set,D=new WeakSet;let O=[],k=0;const A=e=>{let t=Date.now();t-k>1e4&&(O=[]),O.push(...e),k=t},j=()=>{if(Date.now()-k>1e4)return[];let e=[...O];return queueMicrotask(()=>{O=[]}),e},M=e=>{if(D.has(e))return;let t=e.scheduleRefresh;typeof t==`function`&&(D.add(e),e.scheduleRefresh=(n,r)=>{if(t.call(e,n,r),E.size===0)return;let i=Array.from(r.staleFamilies,e=>e.current),a=Array.from(r.updatedFamilies,e=>e.current),o={filePaths:j(),root:n,staleComponents:i,staleFibers:T(n,new Set(i)),updatedComponents:a,updatedFibers:T(n,new Set(a))};for(let e of E)e(o)})};let N=!1;const P=()=>{if(N)return;N=!0;let t=e();for(let e of t.renderers.values())M(e);r(M)};let F=null;const I=()=>{w(A).then(e=>{e&&(F?.dispose(),F=e)})},L=e=>{let{onRefresh:r}=e;return!r||!n()?t(()=>{}):(P(),E.size===0&&I(),E.add(r),t(()=>{E.delete(r)}))};export{L as instrumentReactRefresh};