JSGI/Level0/A

From CommonJS Spec Wiki
Jump to: navigation, search

NOTE: the more recent version of this specification is Draft 2


JSGI Level 0 Proposal A, Draft 1 (in development)

Contents

Philosophy

Principles

Specification

Applications

A JSGI application is a javascript function. It takes exactly one argument, the environment, and returns a JavaScript Object containing three attributes: the status, the headers, and the body.

Request

The request environment must be a JavaScript Object instance that includes CGI-like headers. The application is free to modify the environment. The environment is required to include these variables (adopted from PEP333 and Rack), except when they would be empty (exceptions noted below):

Variables corresponding to the client-supplied HTTP request headers are stored in the .headers object. The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request. All keys in the .headers object MUST be the lower-case equivalent of the request's HTTP header keys.

In addition to this, the request environment MUST include these JSGI-specific variables:

JSGI server implementations are encouraged to include as much information in the request environment as possible. CGI-like keys (TODO: define?) should be all lower case, except letters following the underscores, which should be upper case, with the underscore removed, and stored at the top level of the environment (TODO: this wording sucks, somebody FIXME). These keys MUST have string values.

Servers or applications can also store their own data in the request environment. In order to prevent collisions with unspecified CGI keys, custom keys MUST be stored in the .env top level object.

Input Stream

Must be an input stream. (TODO can we reference something in particular?)

Output Stream

Must be an output stream.

Response

.status
The status MUST be an integer greater than or equal to 100. (TODO changed from "if parsed as an integer" -- was this right?)
.headers
The header MUST be a JavaScript object containing key/value pairs of Strings. All keys SHOULD be lower-case to prevent confusion. The header must not contain a Status key, contain keys with : or newlines in their name, contain keys names that end in - or _, but only contain keys that consist of letters, digits, _ or - and start with a letter. The values of the header must be Strings, consisting of lines (for multiple header values) separated by ā€œ\nā€. The lines must not contain characters below 037.
  • content-type: There must be a Content-Type, except when the Status is 1xx, 204 or 304, in which case there must be none given.
  • content-length: There must not be a Content-Length header when the Status is 1xx, 204 or 304.

Compliant Level 0 servers MUST accept header keys that are lower-case over keys that contain capital letters but are otherwise identical. Servers SHOULD ignore keys that contain capital letters. Servers MAY case-correct keys when serving the response in any manner. (TODO: DL assumed this was decided but it's still up in the air -- if you have an opinion one way or another please show your hand here or on the mailing list)

.body
The Body must respond to forEach and must only yield objects which have a toByteString method (including Strings and Binary objects) (TODO: is this a dependency on the binary spec?). If the Body responds to close, it will be called after iteration. The Body commonly is an array of Strings or ByteStrings.

Summary of changes from JSGI 0.2

NOTE: this area is not aligned with proposals in the Open Issues section and is most certainly out of date.

Environment

Top level keys

JSGI variables

Headers variables

Server or application variables:

Notes

The "jsgi" property only contains metadata related to JSGI itself, not HTTP, top level contains HTTP info. "input" is moved top level "body", and "url_scheme" is "scheme", like servlet's request.

Open questions

The "CGI" problem
JSGI server implementations built atop CGI SHOULD provide a truthy .jsgi.cgi parameter (in case we discover multiple levels of cgi FAIL)
Response key casing
(a) lower-case
(b) UPPER-CASE
(c) Mixed-case
(d) specify a case-insensitive object
(e) unspecified
Content-Length -- where should it appear?
(a) .headers["content-length"] as String
(b) .contentLength as integer, added by server if response is buffered
(c) Both
Header modification
Should it be noted whether servers or applications are encouraged, discouraged or prevented from adding or modifying headers (e.g. .headers["content-length"])? Is there any rationale for preventing this (e.g. the headers SHOULD reflect the initial state of the request)?
More spec-like
Should we call out normative references where they have been codified for required environment variable? This would allow us to explicitly override them in specific cases (PATH_INFO, SCRIPT_NAME).
Response Reason
Specifying a .reason or .statusText on the response object would duplicate information. If this is necessary servers could provide a mechanism to make this happen -- does this merit a note?
Replacing the "request" object
Overwriting the "request" object with a new environment -- should this be explicitly disallowed? If L0 middleware does this it could break L1 middleware. But in order for L1 to exist on an L0 server it would need to wrap the request. The function of L1 doesn't belong in this discussion, but we should probably at least discuss the implications here.
Response object defaults
Should applications and middleware be REQUIRED to return a full response object -- status, headers and body? If not, middleware developers will always have to feature-test the response -- is this a problem?
Async
How much should we say about async usage patterns? Kris Zyp has gone into great detail on the list, discussions we could link to -- but what kind of normative language should the spec contain about async?
Top-level env conflicts
When additional CGI keys are added to the environment, they should exist at the top level and be camelCased -- this opens the door to conflicts with user-added keys. How do we fix this?

Acknowledgements

This specification is adapted from the Rack specification written by Christian Neukirchen.

Some parts of this specification are adopted from PEP333: Python Web Server Gateway Interface v1.0.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox