npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

sprucehttp_sjs

v1.0.11

Published

A module for responding to requests within SpruceHTTP in an SJS file

Readme

SpruceHTTP SJS:

A module for responding to requests within SpruceHTTP in an SJS file.

Table of Contents:

Documentation:

Importing the module:

const sjs = require("sprucehttp_sjs");

streamMode():

Sets the response into stream mode. This cannot be undone for the duration of the script, and should be the first action performed if this is the intended mode.

Stream mode means that responses are not buffered by the webserver, and thus are sent immediately as they're received from the SJS script. This means that the status line and headers must be sent first before the data, and the \r\n after the headers must be sent by the script as well.

sjs.streamMode(); // Set the response to stream mode.

writeStatusLine(statusCode[, reasonPhrase]):

  • statusLine: number: The HTTP status code to send.
  • reasonPhrase: string: The reason phrase to send.

Writes the status line to the response.

To send a 200 "OK" response, without needing to specify the reason phrase.

sjs.writeStatusLine(200);

To send a 418 "I'm a teapot" response, with a specified reason phrase.

sjs.writeStatusLine(418, "I'm a teapot");

writeHeader(name, value):

  • name: string: The name of the header to send.
  • value: string: The value of the header to send.

Writes a header to the response.

To write the "Content-Type: text/html" header.

sjs.writeHeader("Content-Type", "text/html");

writeData(data):

  • data: string | Buffer: The data to send.

Writes data (the body) to the response. When not in stream mode, the Content-Length header is automatically updated.

Writes text data to the body.

sjs.writeData("Hello, world!");

Writes binary data (a picture, in this case) to the body.

const fs = require('fs');
sjs.writeData(fs.readFileSync("image.png", 'binary'));

writeDataAsync(data):

  • data: string | Buffer: The data to send.

Writes data (the body) to the response asynchronously. When not in stream mode, the Content-Length header is automatically updated.

Writes text data to the body.

await sjs.writeDataAsync("Hello, world!");

Writes binary data (a picture, in this case) to the body.

const fs = require('fs');
await sjs.writeDataAsync(fs.readFileSync("image.png", 'binary'));

clearResponse():

Clears the currently written response and resets the status line, headers, and body. This is useful when you want to send a different response than the one you have already written.

This function does not work in stream mode.

sjs.writeStatusLine(418, "I'm a teapot");
sjs.writeHeader("Content-Type", "text/html");
sjs.writeData("<h1>I'm a teapot</h1>");
// Be serious
sjs.clearResponse();

sjs.writeStatusLine(200);
sjs.writeHeader("Content-Type", "text/html");
sjs.writeData("<h1>I'm <i>not</i> a teapot</h1>");

siteConfig():

Returns the site-specific configuration set in your config file.

console.log(sjs.siteConfig());
/*
{
	"type": "local",
	"location": "./",
	"upgradeInsecure": true,
	"directoryListing": true,
	"sjs": true,
	"headers": {
		"Access-Control-Allow-Origin": "*"
	},
	"ssl": {
		"key": "./ssl/key.pem",
		"cert": "./ssl/cert.pem",
		"minVersion": "TLSv1.2",
		"maxVersion": "TLSv1.3"
	}
}
*/

requestIP():

Returns the requestor's IP address.

console.log(sjs.requestIP());
// ::ffff:127.0.0.1

method():

Returns the request method.

console.log(sjs.method());
// get

path():

Returns the request path.

console.log(sjs.path());
// /index.sjs

pathInfo():

Returns the request's info path.

// Using path /index.sjs/info
console.log(sjs.path());
console.log(sjs.pathInfo());
// /index.sjs
// /info

query():

Returns the request query.

console.log(sjs.query());
/*
{
	"name": "John",
	"age": "42"
}
*/

queryValue(key):

  • key: string: The key of the query to get.

Returns the value of a query parameter.

console.log(sjs.queryValue("name"));
// John

httpVersion():

Returns the HTTP version of the request.

console.log(sjs.httpVersion());
// http/1.1

headers():

Returns the request headers.

console.log(sjs.headers());
/*
{
	"connection": "keep-alive",
	"host": "localhost:8080",
	"upgrade-insecure-requests": "1",
	"user-agent": "Bruh/1.0 (Macintosh; PPC Mac OS X 7_0_1) AppleBruhKit 1.3 (XHTML, like IE) Netscape/69.420 Moment/360 NoScope/1.0"
}
*/

headerValue(name):

  • name: string: The name of the header to get.

Returns the value of a header.

console.log(sjs.headerValue("user-agent"));
// Bruh/1.0 (Macintosh; PPC Mac OS X 7_0_1) AppleBruhKit 1.3 (XHTML, like IE) Netscape/69.420 Moment/360 NoScope/1.0

body():

Returns the request body as a buffer.

console.log(sjs.body().toString("utf8"));
// Hello, world!

bodyStream():

Returns the request body as a ReadStream.

console.log(sjs.bodyStream().read(13).toString("utf8"));
// Hello, world!