inspector.NetworkResources.put


稳定性: 1.1 - 处于活跃开发中

此功能仅在启用 --experimental-inspector-network-resource 标志时可用。

【This feature is only available with the --experimental-inspector-network-resource flag enabled.】

inspector.NetworkResources.put 方法用于响应通过 Chrome 开发者工具协议(CDP)发出的 loadNetworkResource 请求。通常当通过 URL 指定了源映射,并且 DevTools 前端(例如 Chrome)请求该资源以获取源映射时,会触发此操作。

【The inspector.NetworkResources.put method is used to provide a response for a loadNetworkResource request issued via the Chrome DevTools Protocol (CDP). This is typically triggered when a source map is specified by URL, and a DevTools frontend—such as Chrome—requests the resource to retrieve the source map.】

此方法允许开发者预定义响应此类 CDP 请求时要提供的资源内容。

【This method allows developers to predefine the resource content to be served in response to such CDP requests.】

const inspector = require('node:inspector');
// By preemptively calling put to register the resource, a source map can be resolved when
// a loadNetworkResource request is made from the frontend.
async function setNetworkResources() {
  const mapUrl = 'http://localhost:3000/dist/app.js.map';
  const tsUrl = 'http://localhost:3000/src/app.ts';
  const distAppJsMap = await fetch(mapUrl).then((res) => res.text());
  const srcAppTs = await fetch(tsUrl).then((res) => res.text());
  inspector.NetworkResources.put(mapUrl, distAppJsMap);
  inspector.NetworkResources.put(tsUrl, srcAppTs);
};
setNetworkResources().then(() => {
  require('./dist/app');
}); 

欲了解更多详情,请参阅官方 CDP 文档:Network.loadNetworkResource

【For more details, see the official CDP documentation: Network.loadNetworkResource