-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refac: add esm dist format, rewrite in typescript
- Loading branch information
Showing
5 changed files
with
3,325 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Imports | ||
|
||
import type * as http from 'http'; | ||
import readCredentials from 'basic-auth'; | ||
import serveStatic from 'serve-static'; | ||
|
||
// Types | ||
|
||
interface Options { | ||
directory?: string; | ||
realm?: string; | ||
|
||
onAuthFailed?: (res) => void; | ||
} | ||
|
||
type ValidatorFunction = (username: string, password: string) => boolean; | ||
|
||
// Variables | ||
|
||
const optionsDefaultValues: Options = { | ||
directory: process.cwd(), | ||
realm: 'default-realm', | ||
|
||
onAuthFailed: res => res.write('401 Unauthorized') // by default send a basic error message | ||
}; | ||
|
||
// Exports | ||
|
||
export default (url: string, validator: ValidatorFunction, optionsUserValues: Options = {}): http.RequestListener => { | ||
// Check required parameters | ||
|
||
if (typeof url !== 'string') | ||
throw new Error('`url` is not a string'); | ||
if (typeof validator !== 'function') | ||
throw new Error('`validator` is not a function'); | ||
|
||
// Make sure all options are initialized | ||
|
||
const options = { | ||
...optionsDefaultValues, | ||
...optionsUserValues, | ||
}; | ||
|
||
// Everything OK | ||
|
||
const serve = serveStatic(options.directory); | ||
|
||
return (req: http.IncomingMessage, res: http.ServerResponse) => { | ||
if (req.url.startsWith(url)) { // if request URL requires authentication | ||
const credentials = readCredentials(req); | ||
|
||
if (!credentials || !validator(credentials.name, credentials.pass)) { // if credentials are missing / invalid | ||
res.writeHead(401, { 'WWW-Authenticate': `Basic realm="${options.realm}"` }); | ||
options.onAuthFailed(res); // tell the user | ||
return res.end(); // don't serve the requested file | ||
} | ||
} | ||
|
||
serve(req, res, () => { | ||
res.statusCode = 404; | ||
res.end('404 Not Found'); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.