Update protocol module

This commit is contained in:
Milan Burda
2016-03-25 21:58:12 +01:00
parent 4cede46324
commit c8fb877522
2 changed files with 135 additions and 53 deletions

View File

@@ -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('<h5>Response</h5>')});
});
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);
});
});

View File

@@ -6,56 +6,116 @@
/// <reference path="../node/node.d.ts" />
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 protocols 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 protocols 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 protocols 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 protocols 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;
}
}