Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/chai": "^4.3.3",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-as-promised": "^8.0.2",
"@types/connect": "^3.4.35",
"@types/mime-types": "^2.1.1",
"@types/mocha": "^10.0.0",
"@types/node": "^26.1.1",
"@types/sinon": "^22.0.0",
"@types/sinon-chai": "^4.0.0",
"@types/supertest": "^6.0.2",
"chai": "^4.4.1",
"chai-as-promised": "^7.1.2",
"chai-as-promised": "^8.0.2",
"concat-stream": "^2.0.0",
"eslint": "^10.6.0",
"eslint-config-google": "^0.14.0",
Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const networkLogger = require("morgan");
const superstatic = require("./superstatic");

/**
* @param {ServerOptions} spec superstatic options.
* @param {import("./options").ServerOptions} spec superstatic options.
* @returns unknown
*/
module.exports = function (spec) {
Expand Down
6 changes: 3 additions & 3 deletions src/superstatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const CWD = process.cwd();

/**
* Superstatic returns a router that can be used in a server.
* @param {MiddlewareOptions} spec superstatic options.
* @returns {HandleFunction} router handler.
* @param {import("./options").MiddlewareOptions} spec superstatic options.
* @returns {import("connect").HandleFunction} router handler.
*/
const superstatic = function (spec = {}) {
spec.stack ??= "default";
Expand All @@ -50,7 +50,7 @@ const superstatic = function (spec = {}) {
const cwd = spec.cwd ?? CWD;

// Load data
/** @type {Configuration} */
/** @type {import("./config").Configuration} */
const config = (spec.config = loadConfigFile(spec.config));
config.errorPage = config.errorPage ?? "/404.html";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

const chai = require("chai");
chai.use(require("chai-as-promised"));
const expect = chai.expect;
import { use, expect } from "chai";
import chaiAsPromised from "chai-as-promised";
use(chaiAsPromised);

const memoryProvider = require("../../../src/providers/memory");

describe("memory provider", () => {
let store;
let provider;
let store: Record<string, string> | null = null;
let provider: any;
beforeEach(() => {
store = store ?? {};
provider = memoryProvider({ store: store });
Expand All @@ -37,10 +38,13 @@ describe("memory provider", () => {
});

it("should return a stream of the content if found", (done) => {
if (!store) {
return done(new Error("store not initialized"));
}
store["/index.html"] = "foobar";
provider({}, "/index.html").then((result) => {
provider({}, "/index.html").then((result: any) => {
let out = "";
result.stream.on("data", (data) => {
result.stream.on("data", (data: any) => {
out += data;
});
result.stream.on("end", () => {
Expand All @@ -51,6 +55,9 @@ describe("memory provider", () => {
});

it("should return an etag of the content", async () => {
if (!store) {
throw new Error("store not initialized");
}
store["/a.html"] = "foo";
store["/b.html"] = "bar";
return Promise.resolve({
Expand All @@ -64,6 +71,9 @@ describe("memory provider", () => {
});

it("should return the length of content", () => {
if (!store) {
throw new Error("store not initialized");
}
store["/index.html"] = "foobar";
return expect(provider({}, "/index.html")).to.eventually.have.property(
"size",
Expand Down
53 changes: 27 additions & 26 deletions test/unit/responder.spec.js → test/unit/responder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { use, expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import * as sinon from "sinon";
import sinonChai from "sinon-chai";

const Responder = require("../../src/responder");
const chai = require("chai");

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
const sinon = require("sinon");
chai.use(require("chai-as-promised"));
chai.use(require("sinon-chai"));
const expect = chai.expect;
use(chaiAsPromised);
use(sinonChai);

describe("Responder", () => {
let responder;
let responder: any;

describe("#handle", () => {
beforeEach(() => {
Expand Down Expand Up @@ -103,19 +106,19 @@ describe("Responder", () => {
});

describe("#handleRewrite", () => {
it("should call through to a registered custom rewriter", () => {
let out;
it("should call through to a registered custom rewriter", async () => {
let out: any;
responder = new Responder(
{},
{
setHeader: noop,
end: function (data) {
end: function (data: any) {
out = data;
},
},
{
rewriters: {
message: function (rewrite) {
message: function (rewrite: any) {
return Promise.resolve({
data: rewrite.message,
contentType: "text/plain",
Expand All @@ -126,17 +129,16 @@ describe("Responder", () => {
},
);

return responder
.handleRewrite({ rewrite: { message: "hi" } })
.then((result) => {
expect(result).to.equal(true);
expect(out).to.equal("hi");
});
const result = await responder.handleRewrite({
rewrite: { message: "hi" },
});
expect(result).to.equal(true);
expect(out).to.equal("hi");
});
});

describe("#handleMiddleware", () => {
let rq;
let rq: any;
beforeEach(() => {
rq = {};
responder = new Responder(rq, { setHeader: noop, end: noop }, {});
Expand All @@ -148,21 +150,20 @@ describe("Responder", () => {
});
});

it("should resolve false if next is called", () => {
return responder
.handleMiddleware((req, res, next) => {
it("should resolve false if next is called", async () => {
const result = await responder.handleMiddleware(
(req: any, res: any, next: any) => {
next();
})
.then((result) => {
expect(result).to.equal(false);
});
},
);
expect(result).to.equal(false);
});
});

describe("#handleFile", () => {
const req = {};
const res = {};
let stub;
let stub: sinon.SinonStub;

beforeEach(() => {
stub = sinon.stub();
Expand All @@ -179,7 +180,7 @@ describe("Responder", () => {
});

describe("#isNotModified", () => {
let result;
let result: any;

beforeEach(() => {
responder = new Responder({ headers: {} }, {}, {});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { expect, use } = require("chai");
use(require("chai-as-promised"));
import { use, expect } from "chai";
import chaiAsPromised from "chai-as-promised";
use(chaiAsPromised);

const promiseback = require("../../../src/utils/promiseback");

describe("promiseback", () => {
it("should resolve a promise if one is returned", () => {
return expect(
promiseback((a1, a2) => {
promiseback((a1: any, a2: any) => {
return Promise.resolve({
a: a1,
b: a2,
Expand All @@ -28,15 +29,15 @@ describe("promiseback", () => {

it("should reject an errback if one is used and errors", () => {
return expect(
promiseback((a1, a2, cb) => {
promiseback((a1: any, a2: any, cb: any) => {
cb(a2);
}, 2)("foo", "bar"),
).to.be.rejectedWith("bar");
});

it("should resolve an errback if one is used and resolves", () => {
return expect(
promiseback((a1, a2, cb) => {
promiseback((a1: any, a2: any, cb: any) => {
cb(null, a2);
}, 2)("foo", "bar"),
);
Expand Down
Loading