From c8fb877522ea9559e82b72b2d16d0c6da8527fe5 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Fri, 25 Mar 2016 21:58:12 +0100 Subject: [PATCH] Update protocol module --- github-electron/github-electron-main-tests.ts | 28 ++- github-electron/github-electron.protocol.d.ts | 160 ++++++++++++------ 2 files changed, 135 insertions(+), 53 deletions(-) diff --git a/github-electron/github-electron-main-tests.ts b/github-electron/github-electron-main-tests.ts index 7b0dc800a7..af44415b89 100644 --- a/github-electron/github-electron-main-tests.ts +++ b/github-electron/github-electron-main-tests.ts @@ -550,9 +550,31 @@ powerSaveBlocker.stop(id); // https://github.com/atom/electron/blob/master/docs/api/protocol.md app.on('ready', () => { - protocol.registerProtocol('atom', (request: any) => { - var url = request.url.substr(7); - return new protocol.RequestFileJob(path.normalize(`${__dirname}/${url}`)); + protocol.registerStandardSchemes(['https']); + protocol.registerServiceWorkerSchemes(['https']); + + protocol.registerFileProtocol('atom', (request, callback) => { + callback(`${__dirname}/${request.url}`); + }); + + protocol.registerBufferProtocol('atom', (request, callback) => { + callback({mimeType: 'text/html', data: new Buffer('
Response
')}); + }); + + protocol.registerStringProtocol('atom', (request, callback) => { + callback('Hello World!'); + }); + + protocol.registerHttpProtocol('atom', (request, callback) => { + callback({url: request.url, method: request.method}); + }); + + protocol.unregisterProtocol('atom', (error) => { + console.log(error ? error.message : 'ok'); + }); + + protocol.isProtocolHandled('atom', (handled) => { + console.log(handled); }); }); diff --git a/github-electron/github-electron.protocol.d.ts b/github-electron/github-electron.protocol.d.ts index d28c3f025f..f34de22e84 100644 --- a/github-electron/github-electron.protocol.d.ts +++ b/github-electron/github-electron.protocol.d.ts @@ -6,56 +6,116 @@ /// declare namespace Electron { - - class RequestFileJob { - /** - * Create a request job which would query a file of path and set corresponding mime types. - */ - constructor(path: string); - } - - class RequestStringJob { - /** - * Create a request job which sends a string as response. - */ - constructor(options?: { - /** - * Default is "text/plain". - */ - mimeType?: string; - /** - * Default is "UTF-8". - */ - charset?: string; - data?: string; - }); - } - - class RequestBufferJob { - /** - * Create a request job which accepts a buffer and sends a string as response. - */ - constructor(options?: { - /** - * Default is "application/octet-stream". - */ - mimeType?: string; - /** - * Default is "UTF-8". - */ - encoding?: string; - data?: Buffer; - }); - } - + /** + * The protocol module can register a custom protocol or intercept an existing protocol. + */ interface Protocol { - registerProtocol(scheme: string, handler: (request: any) => void): void; - unregisterProtocol(scheme: string): void; - isHandledProtocol(scheme: string): boolean; - interceptProtocol(scheme: string, handler: (request: any) => void): void; - uninterceptProtocol(scheme: string): void; - RequestFileJob: typeof RequestFileJob; - RequestStringJob: typeof RequestStringJob; - RequestBufferJob: typeof RequestBufferJob; + /** + * Registers custom schemes as standard schemes. + */ + registerStandardSchemes(schemes: string[]): void; + /** + * Registers custom schemes to handle service workers. + */ + registerServiceWorkerSchemes(schemes: string[]): void; + /** + * Registers a protocol of scheme that will send the file as a response. + */ + registerFileProtocol(scheme: string, handler: (request: ProtocolRequest, callback: FileProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Registers a protocol of scheme that will send a Buffer as a response. + */ + registerBufferProtocol(scheme: string, handler: (request: ProtocolRequest, callback: BufferProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Registers a protocol of scheme that will send a String as a response. + */ + registerStringProtocol(scheme: string, handler: (request: ProtocolRequest, callback: StringProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Registers a protocol of scheme that will send an HTTP request as a response. + */ + registerHttpProtocol(scheme: string, handler: (request: ProtocolRequest, callback: HttpProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Unregisters the custom protocol of scheme. + */ + unregisterProtocol(scheme: string, completion?: (error: Error) => void): void; + /** + * The callback will be called with a boolean that indicates whether there is already a handler for scheme. + */ + isProtocolHandled(scheme: string, callback: (handled: boolean) => void): void; + /** + * Intercepts scheme protocol and uses handler as the protocol’s new handler which sends a file as a response. + */ + interceptFileProtocol(scheme: string, handler: (request: ProtocolRequest, callback: FileProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Intercepts scheme protocol and uses handler as the protocol’s new handler which sends a String as a response. + */ + interceptStringProtocol(scheme: string, handler: (request: ProtocolRequest, callback: BufferProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Intercepts scheme protocol and uses handler as the protocol’s new handler which sends a Buffer as a response. + */ + interceptBufferProtocol(scheme: string, handler: (request: ProtocolRequest, callback: StringProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Intercepts scheme protocol and uses handler as the protocol’s new handler which sends a new HTTP request as a response. + */ + interceptHttpProtocol(scheme: string, handler: (request: ProtocolRequest, callback: HttpProtocolCallback) => void, completion?: (error: Error) => void): void; + /** + * Remove the interceptor installed for scheme and restore its original handler. + */ + uninterceptProtocol(scheme: string, completion?: (error: Error) => void): void; + } + + type ProtocolRequest = { + url: string; + referrer: string; + method: string; + uploadData?: { + bytes: Buffer, + file: string + }[]; + } + + interface ProtocolCallback { + (error: number): void; + (obj: { + error: number + }): void; + (): void; + } + + interface FileProtocolCallback extends ProtocolCallback { + (filePath: string): void; + (obj: { + path: string + }): void; + } + + interface BufferProtocolCallback extends ProtocolCallback { + (buffer: Buffer): void; + (obj: { + data: Buffer, + mimeType: string, + charset?: string + }): void; + } + + interface StringProtocolCallback extends ProtocolCallback { + (str: string): void; + (obj: { + data: Buffer, + mimeType: string, + charset?: string + }): void; + } + + interface HttpProtocolCallback extends ProtocolCallback { + (redirectRequest: { + url: string; + method: string; + session?: Object; + uploadData?: { + contentType: string; + data: string; + }; + }): void; } }