fcgi
FastCGI protocol implementation for Deno.
This library allows the following:
- To create Deno backend application behind a FastCGI-capable web server (like Apache or Nginx).
- To make queries to a FastCGI service, such as PHP (as web server does).
- To create 2 Deno applications, and communicate between them through FastCGI protocol.
FastCGI is a simple protocol designed to forward HTTP requests. Usually itâs used to forward different HTTP requests to different applications from one HTTP server that listens on single host/port. Having master HTTP server is convenient. It allows to have confuguration in single place, that controls all the WWW actions.
Backend application example (FastCGI server)
// File: backend.ts
import {fcgi} from 'https://deno.land/x/fcgi@v1.0.4/mod.ts';
const listener = fcgi.listen
( 'localhost:9988', // FastCGI service will listen on this address
'', // Handle all URL paths
async req =>
{ // Handle the request
console.log(req.url);
req.responseHeaders.set('Content-Type', 'text/html');
await req.respond({body: 'Hello world!'});
}
);
console.log(`Started on ${listener.addr.transport=='tcp' ? listener.addr.hostname+':'+listener.addr.port : listener.addr.transport}`);
And you can set up your web server to forward HTTP requests to localhost:9988
, and the application will get them.
Frontend application example (FastCGI client)
Or you can create a web server with Deno, and make requests to some FastCGI server. This can be PHP-FPM, or this can be our Deno backend application shown above.
In the following example iâll use x/oak to create HTTP server in Deno:
// File: frontend.ts
import {Application} from 'https://deno.land/x/oak@v9.0.1/mod.ts';
import {fcgi} from 'https://deno.land/x/fcgi@v1.0.4/mod.ts';
const app = new Application;
app.use
( async ctx =>
{ const resp = await fcgi.fetch
( { addr: 'localhost:9988',
},
new Request
( ctx.request.url.href,
{ method: ctx.request.method,
headers: ctx.request.headers,
body: ctx.request.hasBody ? ctx.request.body({type: 'stream'}).value : undefined,
}
)
);
ctx.response.status = resp.status;
ctx.response.headers = resp.headers;
ctx.response.body = resp.body;
}
);
app.listen('localhost:8123');
console.log(`Started on http://localhost:8123`);
Hereâs how to run the previous 2 examples:
deno run --allow-net backend.ts &
deno run --allow-net frontend.ts
Now HTTP requests on http://localhost:8123
will be forwarded to fcgi://localhost:9988
and handled by the backend application.
Setup examples:
- Nginx â Deno: how to set up Nginx HTTP server.
- Apache â Deno: how to set up Apache HTTP server.
- Deno â PHP: how to set up PHP-FPM and forward requests to it.
Using the API
This library provides first-class object through which you can do all the supported FastCGI operations: starting FastCGI server, and making queries to FastCGI services.
This object is called fcgi.
import {fcgi} from 'https://deno.land/x/fcgi@v1.0.4/mod.ts';
Methods:
fcgi.listen(addr_or_listener:
FcgiAddr|
Deno.Listener, path_pattern: PathPattern, callback: Callback)
Registers a FastCGI server on specified network address. The address can be given as:
- a port number (
8000
), - a hostname with optional port (
localhost:8000
,0.0.0.0:8000
,[::1]:8000
,::1
), - a unix-domain socket file name (
/run/deno-fcgi-server.sock
), - a
Deno.Addr
({transport: 'tcp', hostname: '127.0.0.1', port: 8000}
), - or a ready
Deno.Listener
object can also be given.
This function can be called multiple times with the same or different addresses. Calling with the same address adds another handler callback that will be tried to handle matching requests. Calling with different address creates another FastCGI server.
Second argument allows to filter arriving requests. It uses x/path_to_regexp library, just like x/oak does.
Third argument is callback function with signature (request: ServerRequest, params: any) => Promise<unknown>
that will be called for incoming requests that match filters.
params
contains regexp groups from the path filter.
âcallbackâ can handle the request and call itâs req.respond()
method (not returning from the callback till this happens), or it can decide not to handle this request,
and return without awaiting, so other handlers (registered with fcgi.listen()
) will take chance to handle this request. If none handled, 404 response will be returned.
Example:
fcgi.listen
( 9988,
'/page-1.html',
async req =>
{ await req.respond({body: 'Hello world'});
}
);
fcgi.listen
( 9988,
'/catalog/:item',
async (req, params) =>
{ await req.respond({body: `Item ${params.item}`});
}
);
fcgi.listen
( 9988,
'', // match all paths
async req =>
{ await req.respond({body: 'Something else'});
}
);
fcgi.unlisten(addr?:
FcgiAddr)
Stop serving requests on specified address, or on all addresses (if the addr parameter was undefined). Removing all listeners will trigger âendâ event.
fcgi.onError(callback)
- catch FastCGI server errors. Multiple event handlers can be added.fcgi.onEnd(callback)
orawait onEnd()
- catch the moment when FastCGI server stops accepting connections (when all listeners removed, and ongoing requests completed).fcgi.offError(callback)
- remove this callback that was added throughonError(callback)
.
fcgi.offError()
- remove all callbacks.
fcgi.offEnd(callback)
- remove this callback that was added throughonEnd(callback)
.
fcgi.offEnd()
- remove all callbacks.
options(options?:
ServerOptions&
ClientOptions): ServerOptions & ClientOptions
Allows to modify Server
and/or Client
options. Not specified options will retain their previous values.
This function can be called at any time, even after server started running, and new option values will take effect when possible.
This function returns all the options after modification.
console.log(`maxConns=${fcgi.options().maxConns}`);
fcgi.options({maxConns: 123});
console.log(`Now maxConns=${fcgi.options().maxConns}`);
fcgi.fetch(request_options:
RequestOptions, input:
Request|
URL| string, init?: RequestInit & { bodyIter: AsyncIterable<Uint8Array> }): Promise<
ResponseWithCookies>
Send request to a FastCGI service, such as PHP, just like Apache and Nginx do.
First argument (request_options
) specifies how to connect to the service, and what parameters to send to it.
2 most important parameters are request_options.addr
(service socket address), and request_options.scriptFilename
(path to script file that the service must execute).
Second (input
) and 3rd (init
) arguments are the same as in built-in fetch()
function, except that init
allows to read request body from an AsyncIterable<Uint8Array>
(init.bodyIter
).
Returned response object extends built-in Response
(that regular fetch()
returns) by adding cookies
property, that contains all Set-Cookie
headers.
Also response.body
object extends regular ReadableStream<Uint8Array>
by adding Deno.Reader
implementation.
The response body must be explicitly read, before specified request_options.timeout
period elapses. After this period, the connection will be forced to close.
Each not closed connection counts towards ClientOptions.maxConns
. After response.body
read to the end, the connection returns to pool, and can be reused
(except the case where existing Deno.Conn
was given to request_options.addr
- in this case the creator of this object decides what to do with this object then).
Idle connections will be closed after request_options.keepAliveTimeout
milliseconds, and after request_options.keepAliveMax
times used.
fcgi.fetchCapabilities(addr: FcgiAddr | Deno.Conn): Promise<{ FCGI_MAX_CONNS: number, FCGI_MAX_REQS: number, FCGI_MPXS_CONNS: number }>
Ask a FastCGI service (like PHP) for itâs protocol capabilities. This is not so useful information. Only for those who curious. As i know, Apache and Nginx donât even ask for this during protocol usage.
fcgi.canFetch(): boolean
When number of ongoing requests is more than the configured value (maxConns
), fetch()
and fetchCapabilities()
will wait.
canFetch()
checks whether there are free slots, and returns true if so.
Itâs recommended not to call fetch()
untill canFetch()
grants a green light.
Example:
if (!fcgi.canFetch())
{ await fcgi.waitCanFetch();
}
await fcgi.fetch(...);
fcgi.waitCanFetch(): Promise<void>
fcgi.closeIdle()
If keepAliveTimeout
option was > 0, fcgi.fetch()
will reuse connections. After each fetch, connection will wait for specified number of milliseconds for next fetch. Idle connections donât let Deno application from exiting naturally.
You can call fcgi.closeIdle()
to close all idle connections.
Using low-level API
The mentioned fcgi
object is just a wrapper around low-level functions and classes. Itâs possible to use them directly.
import {Server} from 'https://deno.land/x/fcgi@v1.0.4/mod.ts';
const listener = Deno.listen({hostname: "::1", port: 9988});
const server = new Server(listener);
console.log(`Started on ${(listener.addr as Deno.NetAddr).port}`);
for await (let req of server)
{ console.log(req.url);
req.responseHeaders.set('Content-Type', 'text/html');
await req.respond({body: 'Your cookies: '+JSON.stringify([...req.cookies.entries()])});
}
ServerRequest
object
Callback given to fcgi.listen()
receives incoming request as ServerRequest
object. Also asynchronous iteration over Server
yields such objects. ServerRequest
contains information sent from FastCGI server.
url
(string) - REQUEST_URI of the request, like â/path/index.html?a=1âmethod
(string) - LikeGET
.proto
(string) - LikeHTTP/1.1
orHTTP/2
.protoMinor
(number)protoMajor
(number)params
(Headers) - Environment params sent from FastCGI frontend. This usually includes âREQUEST_URIâ, âSCRIPT_URIâ, âSCRIPT_FILENAMEâ, âDOCUMENT_ROOTâ, can contain âCONTEXT_DOCUMENT_ROOTâ (if using Apache MultiViews), etc.headers
(Headers) - Request HTTP headers.get
(Map) - Query string parameters.post
(Map) - POST parameters, that can contain uploaded files. To initialize this property, callawait req.post.parse()
.cookies
(Map) - Request cookies. Adding and deleting them adds corresponding response HTTP headers.body
(Deno.Reader) - Allows to read raw POST body ifreq.post.parse()
was not called. The body can be also read fromServerRequest
object itself, as it implementsDeno.Reader
(req.body == req
).responseStatus
(number) - Set this to HTTP status code before callingrespond()
. However status given torespond()
(if given) overrides this one.responseHeaders
(Headers) - Set response HTTP headers here, before callingrespond()
, and/or pass them torespond()
(the latter have precedence).headersSent
(boolean) - Indicates that response headers are already sent. They will be sent byrespond()
or earlier if you write data to theServerRequest
object (it implementsDeno.Writer
).
To respond to the request, you need to call req.respond()
method, that sends all pending data to FastCGI server, and terminates the request, freeing all the resources, and deleting all the uploaded files (you need to move them to different location to keep them). The object will be not usable after calling respond()
.
If using Server
object, itâs your responsibility to call respond()
when youâre finished with this request. fcgi.listen()
API will call respond()
automatically with 404 status, if you donât call it in any of registered request handlers.
Response headers and data can be set before calling respond()
, or they can be given to the response()
.
Response body can be given to respond()
, or it can be written to ServerRequest
object.
// test like this: curl --data 'INPUT DATA' http://deno-server.loc/test.ts
import {fcgi} from 'https://deno.land/x/fcgi@v1.0.4/mod.ts';
import {readAll, writeAll} from 'https://deno.land/std@0.113.0/streams/conversion.ts';
console.log(`Started on [::1]:9988`);
fcgi.listen
( '[::1]:9988',
'',
async req =>
{ console.log(req.url);
// read raw POST input
let raw_input = await readAll(req.body);
// write response
req.responseHeaders.set('Content-Type', 'text/plain');
await writeAll(req, new TextEncoder().encode('The POST input was:\n'));
await writeAll(req, raw_input);
await req.respond();
}
);