feat: implement basic header modifier base on service worker #4975
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
🤖[deprecated] Generated by Copilot at c8ee6c0
This pull request adds a new feature to the devtools client and plugin for Modern.js, which allows users to modify request headers using a service worker script. It adds a new webpack plugin, a new UI tab, a new state module, and several new components and utils for the header modifier feature. It also refactors the rpc module and updates the package.json and tsconfig.json files. It creates a new service-worker.ts file, which defines the logic of the header modification service worker. It also adds a new cli.ts file, which sets up the server route and the HTML injection for the service worker script.
Details
🤖[deprecated] Generated by Copilot at c8ee6c0
ServiceWorkerCompilerPlugin
class inplugins/sw-compiler-plugin.ts
, which creates a child compiler for the service worker script using the@rspack/core
package and theDefinePlugin
. (link, link)ServiceWorkerCompilerPlugin
to the webpack chain inmodern.config.ts
, using theplugin
anduse
methods. (link, link)package.json
file, which maps the./sw-proxy
subpath to the./dist/public/sw-proxy.js
file. (link)outDir
property to thetsconfig.json
file, which specifies thedist
folder as the output directory for the compiled TypeScript files. (link)plugins
folder to theinclude
array in thetsconfig.json
file, which specifies the files and folders that are included in the TypeScript compilation. (link)src/service.worker.ts
, which uses theidb-keyval
package, theprocess.env.VERSION
variable, and therules
parameter to store and modify the headers of the network requests. (link, link)handleServiceWorker
function insrc/mount/proxy/index.ts
, which registers and unregisters the service worker script using thenavigator.serviceWorker
API. (link)ModifyHeaderRule
andServiceStatus
interfaces insrc/client/utils/service-agent.ts
, which describe the shape of the objects used for the header modification feature. (link)src/client/routes/headers/state.ts
, which uses thevaltio
package to create a reactive state object with aservice
property. (link)registerService
,unregisterService
, andfetchServiceStatus
functions insrc/client/routes/headers/state.ts
, which use thenavigator.serviceWorker
API to register, unregister, and get the status of the service worker script. (link)Page
component for the header modifier view insrc/client/routes/headers/page.tsx
, which uses theuseSnapshot
anduseNavigate
hooks to check the service status and navigate to the editor or welcome view. (link)Page
component for the header modifier welcome view insrc/client/routes/headers/welcome/page.tsx
, which uses theLink
component to navigate to the editor view and the service worker script. (link)Page
component for the header modifier editor view insrc/client/routes/headers/editor/page.tsx
, which uses theuseList
anduseSnapshot
hooks to manage and update an array of rules. (link)defaultTabs
array insrc/client/constants.tsx
, which defines the properties of the header modifier tab. (link)HiOutlineAcademicCap
icon from the@radix-ui/react-icons
package insrc/client/constants.tsx
, which is used for the header modifier tab. (link)devtoolsPlugin
object inplugin/src/cli.ts
, which modifies the server routes for the/sw-proxy.js
path. (link)webpackConfig
object inplugin/src/cli.ts
, which copy the service worker script file to the public folder and inject a script tag into the HTML template. (link, link)rpc
module, which provides a bidirectional RPC communication between the devtools client and the server. (link, link)setupServerConnection
function to a separate file, which issrc/client/rpc/server.ts
. (link)src/client/rpc/index.ts
file with a single export statement, which re-exports everything from thesrc/client/rpc/server.ts
file. (link)assert
function. (link, link)assert
function insrc/client/utils/assert.ts
, which takes a value and an optional message or error, and throws an error if the value is falsy. (link)src/client/utils/index.ts
file, which re-exports everything from thesrc/client/utils/assert.ts
file. (link)devDependencies
alphabetically in thepackage.json
file, which is a common convention for organizing dependencies. (link)module.exports
object in the eslint configuration file, which helps with type checking and code completion. (link)Related Issue
Checklist
pnpm run change
.