限制

浏览器限制

¥Browser limitations

此库使用 Service Worker API 拦截浏览器中的请求。浏览器在实现或执行上述 API 时可能遇到的任何限制都会自动成为 Mock Service Worker 库的限制。我们无法解决此行为,因为它们存在于浏览器中,并且无法通过 JavaScript 规避。

¥This library uses the Service Worker API to intercept requests in the browser. Any limitations that browsers may have when implementing or executing the said API automatically become the limitations of the Mock Service Worker library. We cannot address this behaviors as they are present in the browser and cannot be circumvented by JavaScript.

Firefox:XMLHttpRequestfetch 事件

¥Firefox: fetch event for XMLHttpRequest

当页面上发生 XMLHttpRequest 时,Firefox 不会通知工作程序。这意味着工作者以及因此而产生的这个库不知道何时发生此类请求。即使你有一个匹配的请求处理程序,如果它是 XMLHttpRequest,它也不会被匹配,并且模拟响应不会被发送。

¥Firefox does not notify the worker when an XMLHttpRequest happens on the page. This means that the worker and, as the result, this library, do not know when such requests occur. Even if you have a matching request handler for the request, it won’t be matched and the mocked response won’t be sent if it’s an XMLHttpRequest.

Mock Service Worker 将自己定位为开发工具,这意味着我们无法保证与所有现代浏览器 100% 兼容。最后,每个浏览器在 Service Worker API 的实现方式上可能存在差异,我们也无法解释。

¥Mock Service Worker positions itself as a development tool, which means we cannot guarantee 100% compatibility with all modern browsers. In the end, each browser may have its discrepancies in how the Service Worker API is implemented, for which we also cannot account for.

Node.js 限制

¥Node.js limitations

直接网络连接

¥Direct network connections

由于技术限制,MSW 无法拦截通过直接 net.connect()/net.createConnection() 调用执行的请求。Node.js 中的大多数请求库都依赖 http.ClientRequest 来执行请求,而这正是 MSW 所拦截的。但是,某些库(例如 Undici)会直接利用 node:net 模块执行请求,而这些请求对 MSW 是不可见的。

¥Due to technical limitations, MSW cannot intercept requests performed via direct net.connect()/net.createConnection() calls. Most request libraries in Node.js rely on http.ClientRequest to perform requests, which is what MSW intercepts. However, certain libraries, like Undici, tap directly into the node:net module to perform requests, and those will not be visible to MSW.