SOLFIND
Web Lens
Portal home

protocol | Electron

https://www.electronjs.org/es/docs/latest/api/protocol • 123 KB fetched
Open original page


protocol | Electron Saltar al contenido principal Electron Documentación API Blog Herramientas * Electron Forge * Electron Fiddle Comunidad * Gobernanza * Muestra * Recursos Lanzamientos Español * English * Deutsch * Español * Français * 日本語 * Português * Русский * 中文 Búsqueda * Módulos del proceso principal * app * autoUpdater * BaseWindow * BrowserView * Deprecated * BrowserWindow * clipboard * contentTracing * crashReporter * desktopCapturer * dialog * acceso rápido global * ImageView * Compras integradas * ipcMain * Menu * MenuItem * MessageChannelMain * Título 1 Xpath:/h 1 * nativeImage * nativeTheme * net * netLog * Notification * powerMonitor * powerSaveBlocker * process * protocol * pushNotifications * safeStorage * screen * session * sharedTexture * ShareMenu * shell * systemPreferences * Barra táctil * Tray * utilityProcess * webContents * WebContentsView * webFrameMain * Vista * Módulos de Process Renderer * Utility Process Modules * Elementos de DOM personalizados * Chromium y Node.js * Clases * Estructuras del API * * Módulos del proceso principal * protocol En esta página protocol Registrar un protocolo personalizado e interceptar las peticiones de protocolo existentes. Proceso: principal Un ejemplo de la implementación de un protocolo que tiene el mismo efecto que el protocolo file:// : const { app , protocol , net } = require ( 'electron' ) const path = require ( 'node:path' ) const url = require ( 'node:url' ) app . whenReady ( ) . then ( ( ) => { protocol . handle ( 'atom' , ( request ) => { const filePath = request . url . slice ( 'atom://' . length ) return net . fetch ( url . pathToFileURL ( path . join ( __dirname , filePath ) ) . toString ( ) ) } ) } ) [!NOTE] All methods unless specified can only be used after the ready event of the app module gets emitted. Usando protocol con una partition o session personalizada ​ A protocol is registered to a specific Electron session object. Si no especificas una sesión, entonces tu protocol será aplicado a la sesión por defecto que Electron. Sin embargo, si defines un partition o session en tu webPreferences del browserWindow , luego esa ventana usará una sesión diferente y tu protocolo personalizado no funcionará si solo usas electron.protocol.XXX . Para tener su protocolo personalizado trabajando con una sesión personalizada, necesitas registrarlo a esa sesión explícitamente. const { app , BrowserWindow , net , protocol , session } = require ( 'electron' ) const path = require ( 'node:path' ) const url = require ( 'node:url' ) app . whenReady ( ) . then ( ( ) => { const partition = 'persist:example' const ses = session . fromPartition ( partition ) ses . protocol . handle ( 'atom' , ( request ) => { const filePath = request . url . slice ( 'atom://' . length ) return net . fetch ( url . pathToFileURL ( path . resolve ( __dirname , filePath ) ) . toString ( ) ) } ) const mainWindow = new BrowserWindow ( { webPreferences : { partition } } ) } ) Protocol names ​ RFC 3986 defines what a valid protocol name is: Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are case-insensitive, the canonical form is lowercase […]. Métodos ​ El módulo protocolo tiene los siguientes métodos: protocol.registerSchemesAsPrivileged(customSchemes) ​ * customSchemes CustomScheme[] [!NOTE] This method can only be used before the ready event of the app module gets emitted and can be called only once. Registers the scheme as standard, secure, bypasses content security policy for resources, allows registering ServiceWorker, supports fetch API, streaming video/audio, and V8 code cache. Specify a privilege with the value of true to enable the capability. Un ejemplo de registro de un esquema privilegiado, que elude la Política de Seguridad Contenido: const { protocol } = require ( 'electron' ) protocol . registerSchemesAsPrivileged ( [ { scheme : 'foo' , privileges : { bypassCSP : true } } ] ) A standard scheme adheres to what RFC 3986 calls generic URI syntax . For example http and https are standard schemes, while file is not. Registrar un esquema como estándar, permite a recursos relativos y absolutos ser resueltos correctamente cuando son servidos. De otra manera el esquema se comportaría como el protocolo archivo , pero sin la habilidad de resolver URLs relativas. Por ejemplo cuando usted carga la siguiente carga con un protocolo personalizado sin registrar como un esquema estándar, esta imagen no será cargada debido a que un esquema que no es estandar puede no reconocer URLs relativas: < body > < img src = ' test.png ' > </ body > Registering a scheme as standard will allow access to files through the FileSystem API . De otra manera el renderizador arrojará un error de seguridad en el sistema. Por defecto las apis de web storage (localStorage, sessionStorage, webSQL, indexedDB, cookies) están deshabilitadas para esquemas no estándares. Así que en general, si quieres registrar un protocolo personalizado para reemplazar el protocolo http , tienes que registrarlo como un esquema estándar. Protocols that use streams (http and stream protocols) should set stream: true . The <video> and <audio> HTML elements expect protocols to buffer their responses by default. The stream flag configures those elements to correctly expect streaming responses. protocol.handle(scheme, handler) ​ * scheme string - scheme to handle, for example https or my-app . This is the bit before the : in a URL. * handler Function< GlobalResponse | Promise<GlobalResponse>> * request GlobalRequest Register a protocol handler for scheme . Requests made to URLs with this scheme will delegate to this handler to determine what response should be sent. Either a Response or a Promise<Response> can be returned. Ejemplo: const { app , net , protocol } = require ( 'electron' ) const path = require ( 'node:path' ) const { pathToFileURL } = require ( 'node:url' ) protocol . registerSchemesAsPrivileged ( [ { scheme : 'app' , privileges : { standard : true , secure : true , supportFetchAPI : true } } ] ) app . whenReady ( ) . then ( ( ) => { protocol . handle ( 'app' , ( req ) => { const { host , pathname } = new URL ( req . url ) if ( host === 'bundle' ) { if ( pathname === '/' ) { return new Response ( '<h1>hello, world</h1>' , { headers : { 'content-type' : 'text/html' } } ) } // NB, this checks for paths that escape the bundle, e.g. // app://bundle/../../secret_file.txt const pathToServe = path . resolve ( __dirname , pathname ) const relativePath = path . relative ( __dirname , pathToServe ) const isSafe = relativePath && ! relativePath . startsWith ( '..' ) && ! path . isAbsolute ( relativePath ) if ( ! isSafe ) { return new Response ( 'bad' , { status : 400 , headers : { 'content-type' : 'text/html' } } ) } return net . fetch ( pathToFileURL ( pathToServe ) . toString ( ) ) } else if ( host === 'api' ) { return net . fetch ( 'https://api.my-server.com/' + pathname , { method : req . method , headers : req . headers , body : req . body } ) } } ) } ) See the MDN docs for Request and Response for more details. protocol.unhandle(scheme) ​ * scheme string - scheme for which to remove the handler. Removes a protocol handler registered with protocol.handle . protocol.isProtocolHandled(scheme) ​ * Cadena scheme Returns boolean - Whether scheme is already handled. protocol.registerFileProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (string | ProtocolResponse ) Returns boolean - Whether the protocol was successfully registered Registra un protocolo de scheme que enviará un archivo como repuesta. El handler será llamado con request y callback donde request es una solicitud entrante para el scheme . Para controlar la solicitud , la retrollamada debe ser llamada con la ruta al archivo o un objeto que tiene una propiedad ruta , ejemplo callback(filePath) o callback({ path: filePath }) . El filePath debe ser una ruta absoluta. Por defecto el scheme es tratado como http: , que es analizado de forma diferente que los protocolos que siguen la "generic URI syntax" como file: . protocol.registerBufferProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (Buffer | ProtocolResponse ) Returns boolean - Whether the protocol was successfully registered Registra un protocolo de esquema que enviará un Buffer como respuesta. El uso es el mismo con registerFileProtocol , excepto que el callback debería ser llamado con un objeto Buffer o un objeto que tiene la propiedad data . Ejemplo: protocol . registerBufferProtocol ( 'atom' , ( request , callback ) => { callback ( { mimeType : 'text/html' , data : Buffer . from ( '<h5>Response</h5>' ) } ) } ) protocol.registerStringProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (string | ProtocolResponse ) Returns boolean - Whether the protocol was successfully registered Registra un protocolo de esquema que enviará un string como respuesta. El uso es el mismo con registerFileProtocol , excepto que el callback debería ser llamado con un objeto string o un objeto que tiene la propiedad data . protocol.registerHttpProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response ProtocolResponse Returns boolean - Whether the protocol was successfully registered Registra un protocolo de esquema que enviará una solicitud HTTP como respuesta. El uso es el mismo con registerFileProtocol , excepto que el callback debería ser llamado con un objeto que tiene la propiedad url . protocol.registerStreamProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (ReadableStream | ProtocolResponse ) Returns boolean - Whether the protocol was successfully registered Registra un protocolo de scheme que enviará un stream como respuesta. The usage is the same with registerFileProtocol , except that the callback should be called with either a ReadableStream object or an object that has the data property. Ejemplo: const { protocol } = require ( 'electron' ) const { PassThrough } = require ( 'node:stream' ) function createStream ( text ) { const rv = new PassThrough ( ) // PassThrough is also a Readable stream rv . push ( text ) rv . push ( null ) return rv } protocol . registerStreamProtocol ( 'atom' , ( request , callback ) => { callback ( { statusCode : 200 , headers : { 'content-type' : 'text/html' } , data : createStream ( '<h5>Response</h5>' ) } ) } ) Es posible pasar cualquier objeto que implementa la API readable stream (emite los eventos data / end / error ). Por ejemplo, aquí está como puede devolver un archivo: protocol . registerStreamProtocol ( 'atom' , ( request , callback ) => { callback ( fs . createReadStream ( 'index.html' ) ) } ) protocol.unregisterProtocol(scheme) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme Returns boolean - Whether the protocol was successfully unregistered Anula el registro del protocolo predeterminado de esquema . protocol.isProtocolRegistered(scheme) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme Devuelve boolean - Si el scheme ya está registrado. protocol.interceptFileProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (string | ProtocolResponse ) Returns boolean - Whether the protocol was successfully intercepted Intercepta el protocolo esquema y usa controlador como el controlador del nuevo protocolo lo cual enviará un archivo como respuesta. protocol.interceptStringProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (string | ProtocolResponse ) Returns boolean - Whether the protocol was successfully intercepted Intercepta el protocolo de scheme y usa el handler como el nuevo manejador del protocolo, el cual envía un string como respuesta. protocol.interceptBufferProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (Buffer | ProtocolResponse ) Returns boolean - Whether the protocol was successfully intercepted Intercepta el protocolo de scheme y usa el handler como el nuevo manejador del protocolo, el cual envía un Buffer como respuesta. protocol.interceptHttpProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response ProtocolResponse Returns boolean - Whether the protocol was successfully intercepted Intercepta el protocolo scheme y utiliza el handler como el nuevo controlador del protocolo, el cual envía una nueva solicitud HTTP como respuesta. protocol.interceptStreamProtocol(scheme, handler) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme * Función handler * request ProtocolRequest * callback Function * response (ReadableStream | ProtocolResponse ) Returns boolean - Whether the protocol was successfully intercepted Mismo que protocol.registerStreamProtocol , excepto que reemplaza un manejador de protocolo existente. protocol.uninterceptProtocol(scheme) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme Returns boolean - Whether the protocol was successfully unintercepted Elimina el interceptor instalado para el scheme y restaura su controlador original. protocol.isProtocolIntercepted(scheme) Obsoleto ​ History Version(s) Changes None protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle * Cadena scheme Devuelve boolean - Si el scheme ya está interceptado. Editar esta página Anterior process Siguiente pushNotifications * Usando protocol con una partition o session personalizada * Protocol names * Métodos * registerSchemesAsPrivileged * handle * unhandle * isProtocolHandled * registerFileProtocol * registerBufferProtocol * registerStringProtocol * registerHttpProtocol * registerStreamProtocol * unregisterProtocol * isProtocolRegistered * interceptFileProtocol * interceptStringProtocol * interceptBufferProtocol * interceptHttpProtocol * interceptStreamProtocol * uninterceptProtocol * isProtocolIntercepted Documentación * Empezar * Referencia de la API Listas de verificación * Rendimiento * Seguridad Herramientas * Electron Forge * Electron Fiddle Comunidad * Gobernanza * Recursos * Discord * Bluesky * X * Mastodon * Stack Overflow Más * GitHub * Open Collective * Infraestructura de Pizarra Hosting and infrastructure graciously provided by

Links found on this page

  1. Saltar al contenido principal [direct]
  2. Electron [direct]
  3. Documentación [direct]
  4. API [direct]
  5. Blog [direct]
  6. Electron Forge [direct]
  7. Electron Fiddle [direct]
  8. Gobernanza [direct]
  9. Muestra [direct]
  10. Recursos [direct]
  11. Lanzamientos [direct]
  12. English [direct]
  13. Deutsch [direct]
  14. Français [direct]
  15. 日本語 [direct]
  16. Português [direct]
  17. Русский [direct]
  18. 中文 [direct]
  19. autoUpdater [direct]
  20. BaseWindow [direct]
  21. BrowserView Deprecated [direct]
  22. BrowserWindow [direct]
  23. clipboard [direct]
  24. contentTracing [direct]
  25. crashReporter [direct]
  26. desktopCapturer [direct]
  27. dialog [direct]
  28. acceso rápido global [direct]
  29. ImageView [direct]
  30. Compras integradas [direct]
  31. ipcMain [direct]
  32. Menu [direct]
  33. MenuItem [direct]
  34. MessageChannelMain [direct]
  35. Título 1 Xpath:/h 1 [direct]
  36. nativeImage [direct]
  37. nativeTheme [direct]
  38. net [direct]
  39. netLog [direct]
  40. Notification [direct]
  41. powerMonitor [direct]
  42. powerSaveBlocker [direct]
  43. process [direct]
  44. pushNotifications [direct]
  45. safeStorage [direct]
  46. screen [direct]
  47. session [direct]
  48. sharedTexture [direct]
  49. ShareMenu [direct]
  50. shell [direct]
  51. systemPreferences [direct]
  52. Barra táctil [direct]
  53. Tray [direct]
  54. utilityProcess [direct]
  55. webContents [direct]
  56. WebContentsView [direct]
  57. webFrameMain [direct]
  58. Vista [direct]
  59. Elementos de DOM personalizados [direct]
  60. Chromium y Node.js [direct]
  61. Clases [direct]
  62. Estructuras del API [direct]
  63. principal [direct]
  64. RFC 3986 [direct]
  65. CustomScheme[] [direct]
  66. generic URI syntax [direct]
  67. FileSystem API [direct]
  68. GlobalResponse [direct]
  69. Request [direct]
  70. Response [direct]
  71. protocol.register*Protocol and protocol.intercept*Protocol methods have been replaced with protocol.handle [direct]
  72. ProtocolRequest [direct]
  73. ProtocolResponse [direct]
  74. ReadableStream [direct]
  75. Editar esta página [direct]
  76. Rendimiento [direct]
  77. Seguridad [direct]
  78. Discord [direct]
  79. Bluesky [direct]
  80. X [direct]