9.0.0
Context Variables that are available anywhere in the requests lifespan
1.2.1
Protected abortReadonly clientClient Infos
Readonly internal: booleanWhether the Client IP Address is from an internal fetch
9.3.0
Readonly ip: IPAddress<4 | 6>The IP Address that the Client is using
When a valid Proxy Request is made (and proxy is enabled) this will be the proper IP
3.0.0
Readonly origin: stringThe Origin of the Clients Request
9.5.0
localhost:8000
Readonly port: numberThe Port that the Client is using
3.0.0
Readonly proxied: booleanWhether the Client IP Address is proxied
9.3.0
Readonly referrer: stringThe Referrer of the Clients Request
9.5.0
https://localhost:8000/me
Readonly userThe User Agent of the Client
3.0.0
The Request Context Object used by the server
9.0.0
The Global Context Object used by the server
9.0.0
Readonly headersA Collection of all Headers
if (ctr.headers.has('authorization')) console.log('Authorization Header is present')
console.log(ctr.headers.get('authorization')) // Will print undefined if not present
console.log(ctr.headers.get('authorization', 'hello')) // Will print 'hello' if not present
2.0.0
Readonly paramsA Collection of all Path Parameters
console.log(ctr.params.get('server')) // Will print undefined if not present
2.0.0
Protected rawThe Server Object that initialized this Request
9.8.0
Readonly typeThe Type of this Request
5.7.0
Readonly urlThe Requested URL
0.0.2
A Collection of all Client Cookies
import { Cookie } from "rjweb-server"
if (ctr.cookies.has('theme')) console.log('Theme Cookie is present')
console.log(ctr.cookies.get('theme')) // Will print undefined if not present
console.log(ctr.cookies.get('theme', 'light')) // Will print 'light' if not present
ctr.cookies.set('session', new Cookie(Math.random(), {
path: '/'
}))
2.0.0
A Collection of all URL Fragments
if (ctr.fragments.has('user')) console.log('User Fragment is present')
console.log(ctr.fragments.get('user')) // Will print undefined if not present
console.log(ctr.fragments.get('user', 'default')) // Will print 'default' if not present
7.0.0
A Collection of all URL Queries
if (ctr.queries.has('user')) console.log('User Query is present')
console.log(ctr.queries.get('user')) // Will print undefined if not present
console.log(ctr.queries.get('user', 'default')) // Will print 'default' if not present
2.0.0
The Request Body as a Blob-like Object
const size = await ctr.$body().size()
return ctr.print(`The size of the body is ${size} bytes`)
9.7.0
Grab a Channel from either a string identifier or a Channel object
const channel = ctr.$channel('channel')
await channel.send('text', 'Ok')
// or
const ref = new Channel<string>()
const channel = ctr.$channel(ref)
await channel.send('text', 'Ok')
9.8.0
Bind the Body using Zod
This uses .body internally so no binary data
const [ data, error ] = await ctr.bindBody((z) => z.object({
name: z.string().max(24),
gender: z.union([ z.literal('male'), z.literal('female') ])
}))
if (!data) return ctr.status(ctr.$status.BAD_REQUEST).print(error.toString())
ctr.print('Everything valid! 👍')
ctr.printPart(`
your name is ${data.name}
and you are a ${data.gender}
`)
8.8.0
The Request Body (JSON and Urlencoding Automatically parsed if enabled)
0.4.0
Get Infos about the current Ratelimit
This will get all information about the currently applied ratelimit
to the endpoint. If none is active, will return null.
8.6.0
Print a Message to the Client (automatically Formatted)
This Message will be the one actually sent to the client, nothing
can be "added" to the content using this function, it can only be replaced using .print()
To add content to the response body, use .printPart() instead.
ctr.print({
message: 'this is json!'
})
// content will be `{"message":"this is json!"}`
/// or
ctr.print({
message: 'this is json!'
}, true)
// content will be `{\n "message": "this is json!"\n}`
/// or
ctr.print('this is text!')
// content will be `this is text!`
0.0.2
Print a Message to the client (without resetting the previous message state)
This will turn your response into a chunked response, this means that you cannot add headers or cookies after this function has been called. This function is useful if you want to add content to the response body without resetting the previous content. And when you manually want to print massive amounts of data to the client without having to store it in memory.
const file = fs.createReadStream('./10GB.bin')
ctr.printChunked((print) => new Promise<void>((end) => {
file.on('data', (chunk) => {
file.pause()
print(chunk)
.then(() => file.resume())
})
file.on('end', () => {
end()
})
ctr.$abort(() => {
file.destroy()
end()
})
}))
8.2.0
Print the Content of a File to the Client
This will print a file to the client using transfer encoding chunked and
if addTypes is enabled automatically add some content types based on the
file extension. This function wont respect any other http response body set by
.print() or any other normal print as this overwrites the custom ctx execution
function.
Optional addWhether some Content Type Headers will be added automatically
true
2.2.0
Optional compress?: booleanWhether to compress this File
true
7.9.0
Optional download?: booleanWhether to download the file or display it in the browser
ctr.context.response.headers.get('content-type') === 'application/octet-stream'
9.1.4
Optional name?: stringThe Name of the File (if not set, the basename of the file will be used)
Only applied if the content-disposition header is not set and options.download is true
path.basename(file)
9.1.4
ctr.printFile('./profile.png', {
addTypes: true // Automatically add Content types
})
0.6.3
Redirect a Client to another URL
This will set the location header and the status to either to 301 or 302 depending on whether the server should tell the browser that the page has permanently moved or temporarily. Obviously this will only work correctly if the client supports the 30x Statuses combined with the location header.
ctr.redirect('https://example.com', 'permanent') // Will redirect to that URL
2.8.5
Skips counting the request to the Client IPs Rate limit (if there is one)
When a specific IP makes a request to an endpoint under a ratelimit, the maxhits will be increased instantly to prevent bypassing the rate limit by spamming requests faster than the host can handle. When this function is called, the server removes the set hit again.
8.6.0
The Request Status to Send
This will set the status of the request that the client will recieve, by default
the status will be 200, the server will not change this value unless calling the
.redirect() method. If you want to add a custom message to the status you can provide
a second argument that sets that, for RFC documented codes this will automatically be
set but can be overridden, the mapping is provided by http.STATUS_CODES
Optional message: stringctr.status(401).print('Unauthorized')
// or
ctr.status(666, 'The Devil').print('The Devil')
// or
ctr.status(ctr.$status.IM_A_TEAPOT).print('Im a Teapot, mate!')
0.0.2
Add a Header the response depended on (vary header)
This will add a header to the response that tells the client that the response is dependent on the value of the header, this is useful if you want to cache responses that are dependent on the value of a header.
switch (ctr.client.origin) {
case "localhost": {
return ctr.vary('origin').print('Hi, ur cool')
}
default: {
return ctr.vary('origin').print('You are gay.')
}
}
9.3.4
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary
HTTP WWW-Authentication Checker
This will validate the Authorization Header using the WWW-Authentication Standard,
you can choose between basic and digest authentication, in most cases digest
should be used unless you are using an outdated client or want to test easily.
When not matching any user the method will return null and the request should be
ended with a Status.UNAUTHORIZED (401) status code.
const user = ctr.wwwAuth('basic', 'Access this Page.', { // Automatically adds www-authenticate header
bob: '123!',
rotvproHD: 'password'
})
if (!user) return ctr.status(ctr.$status.UNAUTHORIZED).print('Invalid credentials')
ctr.print(`You authenticated with user: ${user}`)
8.0.0
Yield the Request to the next Route that matches the URL
This will yield the request to the next route that matches the URL, this is useful if you want to have multiple routes that match the same URL but have different methods or if you want to have a fallback route that matches all URLs. You can also pass data to the next route by providing it as an argument.
Optional data: Dataconst server = new Server(...)
server.path('/api', (path) => path
.http('GET', '/', (http) => http
.onRequest((ctr) => {
if (ctr.queries.has('yield')) return ctr.yield('Hello World!')
ctr.headers.set('content-type', 'text/html')
ctr.print('<a href="/api?yield">yield this shit</a>')
})
)
.http('GET', '/', (http) => http
.onRequest((ctr) => {
ctr.print(`u yielded, ${ctr.yield().data()}`) // u yielded, Hello World!
})
)
)
9.2.0
HTTP Status Codes Enum