Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devDependenciesの更新とCIのNode.jsバージョンの更新 #14

Merged
merged 3 commits into from
Feb 28, 2023
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
2 changes: 2 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
npx --no-install lint-staged
8 changes: 3 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
name: test
on: [push, pull_request]
env:
CI: true
jobs:
test:
name: "Test on Node.js ${{ matrix.node-version }}"
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10, 12, 14]
node-version: [ 14, 16, 18 ]
steps:
- name: checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install
Expand Down
50 changes: 21 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
"url": "https://github.com/js-primer/local-server.git"
},
"scripts": {
"build": "cross-env NODE_ENV=production tsc -p .",
"build": "tsc -p .",
"prepublish": "npm run --if-present build",
"test": "mocha \"test/**/*.{js,ts}\"",
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
"watch": "tsc -p . --watch"
"watch": "tsc -p . --watch",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
"prepare": "git config --local core.hooksPath .githooks"
},
"prettier": {
"printWidth": 120,
"singleQuote": false,
"tabWidth": 4
"printWidth": 120,
"tabWidth": 4,
"trailingComma": "none"
},
"dependencies": {
"chalk": "^4.1.0",
Expand All @@ -51,36 +53,26 @@
"serve-static": "^1.14.1"
},
"devDependencies": {
"@types/connect": "^3.4.33",
"@types/detect-port": "^1.3.0",
"@types/meow": "^5.0.0",
"@types/mocha": "^8.0.4",
"@types/node": "^14.14.9",
"@types/serve-static": "^1.13.8",
"@types/supertest": "^2.0.10",
"cross-env": "^7.0.2",
"husky": "^4.3.0",
"lint-staged": "^10.5.1",
"mocha": "^8.2.1",
"prettier": "^2.2.0",
"supertest": "^6.0.1",
"ts-node": "^9.0.0",
"ts-node-test-register": "^9.0.0",
"typescript": "^4.1.2"
"@types/connect": "^3.4.35",
"@types/detect-port": "^1.3.2",
"@types/mocha": "^10.0.1",
"@types/node": "^18.14.2",
"@types/serve-static": "^1.15.1",
"@types/supertest": "^2.0.12",
"lint-staged": "^13.1.2",
"mocha": "^10.2.0",
"prettier": "^2.8.4",
"supertest": "^6.3.3",
"ts-node": "^10.9.1",
"ts-node-test-register": "^10.0.0",
"typescript": "^4.9.5"
},
"publishConfig": {
"access": "public"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,css}": [
"prettier --write",
"git add"
"prettier --write"
]
},
"husky": {
"hooks": {
"post-commit": "git reset",
"pre-commit": "lint-staged"
}
}
}
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export const run = () => {
port: cli.flags.port !== undefined ? Number(cli.flags.port) : 3000
});
// Ctrl + C を押したときにメッセージを出す
process.on("SIGINT", function() {
process.on("SIGINT", function () {
server.stop();
process.exit();
});
return server
.start()
.then(() => {})
.catch(error => {
.catch((error) => {
console.error(error.message);
});
};
2 changes: 1 addition & 1 deletion src/local-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class LocalServer {
}

start() {
return detectPort(this.port).then(newPort => {
return detectPort(this.port).then((newPort) => {
if (this.port !== newPort) {
console.log(
`${logSymbols.warning} ポート番号:${this.port}はすでに使われています。利用できる別のポート番号を探索中です。`
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/response-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const responseLog = (): HandleFunction => {
// items shared between the request and response of just one cycle
const cycle: Cycle = {
log: getLogger(),
time: process.hrtime(),
time: process.hrtime()
};
const handleClose = () => logClose(req, res, cycle);
res.on("finish", () => {
Expand Down
83 changes: 29 additions & 54 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,87 +13,69 @@ async function createServer(dir?: string) {
return server;
}

describe("LocalServer", function() {
describe("basic operations", function() {
describe("LocalServer", function () {
describe("basic operations", function () {
let localServer: LocalServer;
let server: http.Server;
before(async function() {
before(async function () {
localServer = await createServer();
server = localServer.server;
});
after(() => {
return localServer.stop();
});

it("should serve static files", function(done) {
request(server)
.get("/todo.txt")
.expect(200, "- groceries", done);
it("should serve static files", function (done) {
request(server).get("/todo.txt").expect(200, "- groceries", done);
});

it("should support nesting", function(done) {
request(server)
.get("/users/tobi.txt")
.expect(200, "ferret", done);
it("should support nesting", function (done) {
request(server).get("/users/tobi.txt").expect(200, "ferret", done);
});

it("should set Content-Type", function(done) {
request(server)
.get("/todo.txt")
.expect("Content-Type", "text/plain; charset=UTF-8")
.expect(200, done);
it("should set Content-Type", function (done) {
request(server).get("/todo.txt").expect("Content-Type", "text/plain; charset=UTF-8").expect(200, done);
});

it("should set Last-Modified", function(done) {
it("should set Last-Modified", function (done) {
request(server)
.get("/todo.txt")
.expect("Last-Modified", /\d{2} \w{3} \d{4}/)
.expect(200, done);
});

it("should default max-age=0", function(done) {
request(server)
.get("/todo.txt")
.expect("Cache-Control", "public, max-age=0")
.expect(200, done);
it("should default max-age=0", function (done) {
request(server).get("/todo.txt").expect("Cache-Control", "public, max-age=0").expect(200, done);
});

it("should support urlencoded pathnames", function(done) {
request(server)
.get("/foo%20bar")
.expect(200, Buffer.from("baz"), done);
it("should support urlencoded pathnames", function (done) {
request(server).get("/foo%20bar").expect(200, Buffer.from("baz"), done);
});

it("should not choke on auth-looking URL", function(done) {
request(server)
.get("//todo@txt")
.expect(404, done);
it("should not choke on auth-looking URL", function (done) {
request(server).get("//todo@txt").expect(404, done);
});

it("should support index.html", function(done) {
it("should support index.html", function (done) {
request(server)
.get("/users/")
.expect(200)
.expect("Content-Type", /html/)
.expect("<p>tobi, loki, jane</p>", done);
});

it("should support ../", function(done) {
request(server)
.get("/users/../todo.txt")
.expect(200, "- groceries", done);
it("should support ../", function (done) {
request(server).get("/users/../todo.txt").expect(200, "- groceries", done);
});

it("should support HEAD", function(done) {
request(server)
.head("/todo.txt")
.expect(200, {}, done);
it("should support HEAD", function (done) {
request(server).head("/todo.txt").expect(200, {}, done);
});

it("should support conditional requests", function(done) {
it("should support conditional requests", function (done) {
request(server)
.get("/todo.txt")
.end(function(err, res) {
.end(function (err, res) {
if (err) {
throw err;
}
Expand All @@ -104,23 +86,16 @@ describe("LocalServer", function() {
});
});

it("should support precondition checks", function(done) {
request(server)
.get("/todo.txt")
.set("If-Match", '"foo"')
.expect(412, done);
it("should support precondition checks", function (done) {
request(server).get("/todo.txt").set("If-Match", '"foo"').expect(412, done);
});

it("should serve zero-length files", function(done) {
request(server)
.get("/empty.txt")
.expect(200, "", done);
it("should serve zero-length files", function (done) {
request(server).get("/empty.txt").expect(200, "", done);
});

it("should ignore hidden files", function(done) {
request(server)
.get("/.hidden")
.expect(404, done);
it("should ignore hidden files", function (done) {
request(server).get("/.hidden").expect(404, done);
});
});
});
Loading