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

http: add http.createStaticServer #45096

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
318610e
http: add `http.createStaticServer`
aduh95 Oct 20, 2022
77e0416
Add docs and parameters
aduh95 Oct 21, 2022
b1cb361
default to `'localhost'`, return server, update docs
aduh95 Oct 21, 2022
ac549c4
Apply suggestions from code review
aduh95 Oct 21, 2022
d3b189f
Ensure the server doesn't start if it's being required
aduh95 Oct 21, 2022
b6dbf4f
use options instead of function arguments
aduh95 Oct 21, 2022
bc6fd54
s/Simple/Static/
aduh95 Oct 21, 2022
39dbb48
add `mimeOverrides` option
aduh95 Oct 21, 2022
c53b51e
add `serveDotFiles` option
aduh95 Oct 21, 2022
c394473
add `filter` option
aduh95 Oct 22, 2022
6502cbc
`stream.promises.pipeline`
aduh95 Oct 23, 2022
9d408e7
add more validation
aduh95 Oct 23, 2022
9a9caa7
Revert "`stream.promises.pipeline`"
aduh95 Oct 23, 2022
bd9a6bd
fixup! add more validation
aduh95 Oct 23, 2022
a0c7864
pass two args to `filter`
aduh95 Oct 23, 2022
20e3d86
serve index files with correct MIME
aduh95 Oct 23, 2022
98a5563
add tests
aduh95 Oct 23, 2022
1259d43
add fixtures files
aduh95 Oct 23, 2022
f3d663d
lint
aduh95 Oct 23, 2022
60d3f0b
rename `http/server` -> `http/static`
aduh95 Oct 26, 2022
41fc901
list missing features in docs
aduh95 Oct 26, 2022
47d2f8b
add tests to ensure that using a "hidden" folder as root is blocked b…
aduh95 Nov 23, 2022
fa52ee0
Use 403 instead of 401
aduh95 Nov 23, 2022
968efde
fix type in docs
aduh95 Nov 23, 2022
5e018ef
remove `http/static` module
aduh95 Dec 7, 2022
3698f0c
add line return
aduh95 Dec 15, 2022
d8ce4b3
add `log` and `onStart` option to let user control logging
aduh95 Dec 19, 2022
0c91db4
add tests to ensure using `..` can't escape the root dir
aduh95 Dec 19, 2022
7b40455
add tests with encoded chars and several slashes in a row
aduh95 Dec 20, 2022
0bc9f5e
liint + `.json`
aduh95 Sep 18, 2023
29257f9
Apply suggestions from code review
aduh95 Sep 19, 2023
a5ea0d7
fix failing test
aduh95 Apr 3, 2024
c1e6869
move `mime` out of `requestHandler`
aduh95 Apr 3, 2024
2e63aba
fix lint
aduh95 Apr 3, 2024
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
Prev Previous commit
Next Next commit
add line return
  • Loading branch information
aduh95 committed Apr 3, 2024
commit 3698f0c010463b5feb0357a8bb3686ed8ba15cdb
8 changes: 4 additions & 4 deletions lib/internal/http/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function createStaticServer(options = kEmptyObject) {
if (filter != null && !filter(req.url, url)) {
console.log('403', req.url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are exposing this as a public function, we should not be using console.log() in it. Logging should be configurable somehow.

res.statusCode = 403;
res.end('Forbidden');
res.end('Forbidden\n');
return;
}

Expand Down Expand Up @@ -106,7 +106,7 @@ function createStaticServer(options = kEmptyObject) {
`${req.url}/` :
`${StringPrototypeSlice(req.url, 0, index)}/${StringPrototypeSlice(req.url, index)}`
);
res.end('Temporary Redirect');
res.end('Temporary Redirect\n');
}
} else {
throw err;
Expand All @@ -116,12 +116,12 @@ function createStaticServer(options = kEmptyObject) {
if (err?.code === 'ENOENT') {
console.log('404', req.url);
res.statusCode = 404;
res.end('Not Found');
res.end('Not Found\n');
} else {
console.error(`Error while loading ${req.url}:`);
console.error(err);
res.statusCode = 500;
res.end('Internal Error');
res.end('Internal Error\n');
}
}
});
Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-http-createstaticserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const { createStaticServer } = require('node:http');
assert.strictEqual(response.status, 403);
assert.strictEqual(response.statusText, 'Forbidden');
assert.strictEqual(response.headers.get('Content-Type'), null);
assert.strictEqual(await response.text(), 'Forbidden');
assert.strictEqual(await response.text(), 'Forbidden\n');
}),
fetch(`http://localhost:${port}/bar.js`).then(async (response) => {
assert(!response.ok);
assert(!response.redirected);
assert.strictEqual(response.status, 403);
assert.strictEqual(response.statusText, 'Forbidden');
assert.strictEqual(response.headers.get('Content-Type'), null);
assert.strictEqual(await response.text(), 'Forbidden');
assert.strictEqual(await response.text(), 'Forbidden\n');
}),
]).then(mustCall()).finally(() => server.close());
}));
Expand Down Expand Up @@ -96,7 +96,7 @@ const { createStaticServer } = require('node:http');
assert.strictEqual(response.status, 403);
assert.strictEqual(response.statusText, 'Forbidden');
assert.strictEqual(response.headers.get('Content-Type'), null);
assert.strictEqual(await response.text(), 'Forbidden');
assert.strictEqual(await response.text(), 'Forbidden\n');
}),
fetch(`http://localhost:${port}/.foo`).then(async (response) => {
assert(!response.ok);
Expand All @@ -118,7 +118,7 @@ const { createStaticServer } = require('node:http');
assert.strictEqual(response.status, 403);
assert.strictEqual(response.statusText, 'Forbidden');
assert.strictEqual(response.headers.get('Content-Type'), null);
assert.strictEqual(await response.text(), 'Forbidden');
assert.strictEqual(await response.text(), 'Forbidden\n');
}),
]).then(mustCall()).finally(() => server.close());
}));
Expand Down Expand Up @@ -226,7 +226,7 @@ const { createStaticServer } = require('node:http');
assert.strictEqual(response.status, 403);
assert.strictEqual(response.statusText, 'Forbidden');
assert.strictEqual(response.headers.get('Content-Type'), null);
assert.strictEqual(await response.text(), 'Forbidden');
assert.strictEqual(await response.text(), 'Forbidden\n');
}),
fetch(`http://localhost:${port}/.bar`).then(async (response) => {
assert(response.ok);
Expand Down Expand Up @@ -256,7 +256,7 @@ const { createStaticServer } = require('node:http');
assert.strictEqual(response.status, 403);
assert.strictEqual(response.statusText, 'Forbidden');
assert.strictEqual(response.headers.get('Content-Type'), null);
assert.strictEqual(await response.text(), 'Forbidden');
assert.strictEqual(await response.text(), 'Forbidden\n');
}),
]).then(mustCall()).finally(() => server.close());
}));
Expand Down