infinity.scgi¶
Provides functionality for setting up an INFINITY.JS instance as a SCGI server.
Module: infinity.scgi
server¶
The class for operations with SCGI servers in INFINITY.JS.
Methods:
Example:
infinity.loadModule('infinity.scgi');
let myScgi = new infinity.scgi.server(false, false, false, 9000);
myScgi.registerHandler(infinity.scgi.server.handler.staticFile, '/', '../web/');
scgi.registerHandler(infinity.scgi.server.handler.jsonRpc, '/v1/jsonrpc');
myScgi.registerService('system', 'system', 'system.js', [
{name: 'shutdown'},
{name: 'getVersion', result: 'string'}
]);
myScgi.start();
while (!infinity.terminated) {
infinity.event.processQueue();
infinity.sleep(25);
}
myScgi.stop();
system.ts:
namespace system {
export function shutdown(): void {
infinity.terminate();
}
export function getVersion(): string {
return '1.0.0 build 1234';
}
}
Folder structure:
+ example
+ ts
- main.ts
- system.ts
+ web
- index.html
Methods
constructor()¶
Lets you create an INFINITY.JS SCGI server object instance.
Signature:
constructor( allowCrossOrigin?: boolean, compress?: boolean, accessLog?: boolean, port?: number, maxConnections?: number, poolSize?: number, maxContentLength?: number, slowRequestTime?: number, maxRateCounter?: number )
Parameters:
-
allowCrossOrigin:
boolean
, optionalFor allowing cross-origin-requests.
-
compress:
boolean
, optionalFor turning compression on or off.
-
accessLog:
boolean
, optionalFor turning on or off the logging of all access events.
-
port:
number
, optionalSpecifies the port to listen on.
-
maxConnections:
number
, optionalSpecifies the number of simultaneous connections.
-
poolSize:
number
, optionalSpecifies the number of worker threads to be held ready.
-
maxContentLength:
number
, optionalSpecifies the maximum number of bytes (content-length header) that the server accepts in http requests.
-
slowRequestTime:
number
, optionalSpecifies how many seconds the processing of a request is allowed to take without being logged as "slow". Accepts decimals with
.
as well. -
maxRateCounter:
number
, optionalThe maximum number of requests that a client can make in a specific time frame for rate limiting.
Example:
infinity.loadModule('infinity.scgi');
let myScgi = new infinity.scgi.server(false, false, false, 9000);
registerHandler()¶
Registers a handler for processing client requests on the SCGI server, with optional rate limiting.
Signature:
registerHandler( handler: infinity.scgi.server.handler, path: string, limit?: number, period?: number, delay?: number ): void
Parameters:
-
handler:
infinity.scgi.server.handler
The type of handler to register for handling client requests.
-
path:
string
The URL path on which this handler will be activated.
-
limit:
number
, optionalThe maximum number of requests a client can make on this path within the specified time period.
-
period:
number
, optionalThe time frame in seconds for the rate limit.
-
delay:
number
, optionalThe time frame in seconds for the rate limit.
Example:
myScgi.registerHandler(infinity.scgi.server.handler.rest, '/api', 100, 3600, 300);
registerHandler()¶
Registers a handler for processing client requests on the SCGI server, linking them to local resources and optionally setting rate limits.
Signature:
registerHandler( handler: infinity.scgi.server.handler, path: string, alias: string, limit?: number, period?: number, delay?: number ): void
Parameters:
-
handler:
infinity.scgi.server.handler
The type of handler to be registered.
-
path:
string
The URL path on which the handler will be activated.
-
alias:
string
A local path or alias pointing to the resources (such as a folder or file) that the handler will use to serve content or execute routines in response to client requests.
-
limit:
number
, optionalThe maximum number of requests a client can make on this path within the specified time period.
-
period:
number
, optionalThe time frame in seconds for the rate limit.
-
delay:
number
, optionalThe time frame in seconds for the rate limit.
Example:
myScgi.registerHandler(infinity.scgi.server.handler.jsonRpc, '/v1/jsonrpc');
registerHandler()¶
Registers a handler for client requests, invoking local resources, including caching functionality.
Signature:
registerHandler( handler: infinity.scgi.server.handler, path: string, alias: string, expire: number, maxCacheSize: number ): void
Parameters:
-
handler:
infinity.scgi.server.handler
The type of handler to register.
-
path:
string
The request path for the client.
-
alias:
string
The path to a local folder or file with contents or defined routines to be served corresponding to the client request.
-
expire:
number
, optionalCache expiration time in seconds. After the specified period the file will have to be examined for changes at the next call.
-
maxCacheSize:
number
, optionalThe cache threshold in bytes. Files with sizes exceeding the specified number will not be cached.
-
headers:
infinity.http.headerArray
The headers to be sent with the response.
-
limit:
number
, optionalThe maximum number of requests that can be made in the defined period. Used for rate limiting.
-
period:
number
, optionalThe rate limit time period in seconds.
-
delay:
number
, optionalThe "cool down" period in seconds that a client must wait after hitting the rate limit before making new requests.
Example:
myScgi.registerHandler(infinity.scgi.server.handler.staticFile, '/', '../web/');
registerService()¶
Registers a service, allowing requests for execution of custom defined methods.
Signature:
registerService( name: string, namespace: string, path: string, methods: infinity.scgi.methodArray ): void
Parameters:
-
name:
string
The name of the service.
-
namespace:
string
The namespace for the service.
-
path:
string
The path to the file with the custom defined methods.
-
methods:
infinity.scgi.methodArray
An array of the method names for the service. Parameters for return types, REST methods and HTTP environment can also be specified.
Example:
myScgi.registerService('system', 'system', 'system.js', [
{name: 'shutdown'},
{name: 'getVersion', result: 'string'}
]);
system.ts:
namespace system {
export function shutdown(): void {
infinity.terminate();
}
export function getVersion(): string {
return '1.0.0 build 1234';
}
}
start()¶
Starts the server with previously specified parameters.
Signature:
start(): void
Example:
myScgi.start();
stop()¶
Shuts down the server.
Signature:
stop(): void
Example:
myScgi.stop();
unregisterHandler()¶
Unregisters the specified registered handler, making the previously specified functionality unavailable.
Signature:
unregisterHandler( path: string ): void
Parameters:
- path:
string
The request path for the client, which the handler has previously been registered for.
Example:
myScgi.unregisterHandler('/');
unregisterService()¶
Unregisters the specified registered service, making the previously specified functionality unavailable.
Signature:
unregisterService( name: string ): void
Parameters:
- name:
string
The name of the previously registered service.
Example:
myScgi.unregisterService('system');
infinity.scgi.server¶
infinity.scgi.server.handler¶
Values:
-
custom:
0
A custom handler to be defined in a file, specified in the third function parameter of registerHandler().
-
staticFile:
1
A handler for serving static file content. Prefers showing the index.html file.
-
jsonRpc:
2
A handler for requests according to the JSON-RPC protocol.
-
rest:
3
A handler for REST-requests.
-
api:
4
A handler for requests to an API.
-
webDav:
5
A handler for requests according to the WebDAV protocol.
-
cardDav:
6
A handler for requests according to the CardDAV protocol.
-
calDav:
7
A handler for requests according to the CalDAV protocol.
-
status:
8
A handler for status requests.
Example:
infinity.loadModule('infinity.scgi');
let handler = infinity.scgi.server.handler.custom;
headerArray¶
Extends: Array<{name: string, value: string}>{}
An array of objects containing name-value string pairs.
methodArray¶
Extends: Array<{name: string, params?: Array<string>, result?: any, rest?: string, httpEnvironment?: boolean}>
An array containing objects with data inside its properties.
Properties:¶
-
name¶
Type:
string
. The name of the function. -
params¶
Type:
Array<string>
, optional. The function parameters. -
result¶
Type:
any
, optional. The return type of the function result. -
rest¶
Type:
string
, optional. The implied REST method for the function call. -
httpEnvironment¶
Type:
boolean
, optional. If set to true, theinfinity.http.request
values will be provided.