@nx.js/http 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +8 -4
- package/dist/index.js +11 -6
- package/dist/index.js.map +1 -1
- package/dist/static.d.ts +12 -2
- package/dist/static.js +8 -6
- package/dist/static.js.map +1 -1
- package/package.json +10 -3
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export { createStaticFileHandler, type StaticFileHandlerOpts } from './static';
|
|
|
4
4
|
*/
|
|
5
5
|
export type ServerHandler = (req: Request) => Response | Promise<Response>;
|
|
6
6
|
/**
|
|
7
|
-
* Options object for the {@link listen | `
|
|
7
|
+
* Options object for the {@link listen | `listen()`} function.
|
|
8
8
|
*/
|
|
9
9
|
export interface ListenOptions extends Omit<Switch.ListenOptions, 'accept'> {
|
|
10
10
|
fetch: ServerHandler;
|
|
@@ -13,11 +13,13 @@ export interface ListenOptions extends Omit<Switch.ListenOptions, 'accept'> {
|
|
|
13
13
|
* Creates a socket handler function which accepts a socket
|
|
14
14
|
* event and parses the incoming data as an HTTP request.
|
|
15
15
|
*
|
|
16
|
-
*
|
|
16
|
+
* > NOTE: This is a low-level function which usually should not be used directly.
|
|
17
|
+
* > See {@link listen | `listen()`} instead, which is a higher-level wrapper
|
|
18
|
+
* > around this function.
|
|
17
19
|
*
|
|
18
20
|
* @param handler The HTTP handler function to invoke when an HTTP request is received
|
|
19
21
|
*/
|
|
20
|
-
export declare function createServerHandler(handler: ServerHandler): (
|
|
22
|
+
export declare function createServerHandler(handler: ServerHandler): (event: Switch.SocketEvent) => Promise<void>;
|
|
21
23
|
/**
|
|
22
24
|
* Creates an HTTP server and listens on the `port` number specified in the options object.
|
|
23
25
|
* The `fetch` function will be invoked upon receiving an HTTP request.
|
|
@@ -25,7 +27,9 @@ export declare function createServerHandler(handler: ServerHandler): ({ socket }
|
|
|
25
27
|
* @example
|
|
26
28
|
*
|
|
27
29
|
* ```typescript
|
|
28
|
-
* http
|
|
30
|
+
* import { listen } from '@nx.js/http';
|
|
31
|
+
*
|
|
32
|
+
* listen({
|
|
29
33
|
* port: 8080,
|
|
30
34
|
* fetch(req) {
|
|
31
35
|
* console.log(`Got HTTP ${req.method} request for "${req.url}"`);
|
package/dist/index.js
CHANGED
|
@@ -5,21 +5,24 @@ export { createStaticFileHandler } from './static';
|
|
|
5
5
|
* Creates a socket handler function which accepts a socket
|
|
6
6
|
* event and parses the incoming data as an HTTP request.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* > NOTE: This is a low-level function which usually should not be used directly.
|
|
9
|
+
* > See {@link listen | `listen()`} instead, which is a higher-level wrapper
|
|
10
|
+
* > around this function.
|
|
9
11
|
*
|
|
10
12
|
* @param handler The HTTP handler function to invoke when an HTTP request is received
|
|
11
13
|
*/
|
|
12
14
|
export function createServerHandler(handler) {
|
|
13
|
-
return async (
|
|
14
|
-
const
|
|
15
|
+
return async (event) => {
|
|
16
|
+
const s = event.socket;
|
|
17
|
+
const unshiftable = new UnshiftableStream(s.readable);
|
|
15
18
|
while (true) {
|
|
16
19
|
const req = await readRequest(unshiftable);
|
|
17
20
|
if (!req)
|
|
18
21
|
break;
|
|
19
22
|
const res = await handler(req);
|
|
20
|
-
writeResponse(
|
|
23
|
+
writeResponse(s.writable, res, 'HTTP/1.1');
|
|
21
24
|
}
|
|
22
|
-
|
|
25
|
+
s.close();
|
|
23
26
|
};
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
@@ -29,7 +32,9 @@ export function createServerHandler(handler) {
|
|
|
29
32
|
* @example
|
|
30
33
|
*
|
|
31
34
|
* ```typescript
|
|
32
|
-
* http
|
|
35
|
+
* import { listen } from '@nx.js/http';
|
|
36
|
+
*
|
|
37
|
+
* listen({
|
|
33
38
|
* port: 8080,
|
|
34
39
|
* fetch(req) {
|
|
35
40
|
* console.log(`Got HTTP ${req.method} request for "${req.url}"`);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtD,OAAO,EAAE,uBAAuB,EAA8B,MAAM,UAAU,CAAC;AAc/E
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtD,OAAO,EAAE,uBAAuB,EAA8B,MAAM,UAAU,CAAC;AAc/E;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAsB;IACzD,OAAO,KAAK,EAAE,KAAyB,EAAE,EAAE;QAC1C,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC;QACvB,MAAM,WAAW,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QACtD,OAAO,IAAI,EAAE,CAAC;YACb,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,WAAW,CAAC,CAAC;YAC3C,IAAI,CAAC,GAAG;gBAAE,MAAM;YAChB,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;YAC/B,aAAa,CAAC,CAAC,CAAC,QAAQ,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC5C,CAAC;QACD,CAAC,CAAC,KAAK,EAAE,CAAC;IACX,CAAC,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,MAAM,CAAC,IAAmB;IACzC,MAAM,MAAM,GAAG,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/C,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AAC3C,CAAC"}
|
package/dist/static.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options object for the {@link createStaticFileHandler | `createStaticFileHandler()`} function.
|
|
3
|
+
*/
|
|
1
4
|
export interface StaticFileHandlerOpts {
|
|
5
|
+
/**
|
|
6
|
+
* Addional HTTP headers to include in the response. Can be a static
|
|
7
|
+
* `Headers` object, or a function which returns a `Headers` object.
|
|
8
|
+
*/
|
|
2
9
|
headers?: HeadersInit | ((req: Request, path: URL) => HeadersInit);
|
|
3
10
|
}
|
|
4
11
|
export declare function resolvePath(url: string, root: string): URL;
|
|
@@ -8,9 +15,11 @@ export declare function resolvePath(url: string, root: string): URL;
|
|
|
8
15
|
* @example
|
|
9
16
|
*
|
|
10
17
|
* ```typescript
|
|
11
|
-
*
|
|
18
|
+
* import { createStaticFileHandler, listen } from '@nx.js/http';
|
|
19
|
+
*
|
|
20
|
+
* const fileHandler = createStaticFileHandler('sdmc:/switch/');
|
|
12
21
|
*
|
|
13
|
-
*
|
|
22
|
+
* listen({
|
|
14
23
|
* port: 8080,
|
|
15
24
|
* async fetch(req) {
|
|
16
25
|
* let res = await fileHandler(req);
|
|
@@ -23,5 +32,6 @@ export declare function resolvePath(url: string, root: string): URL;
|
|
|
23
32
|
* ```
|
|
24
33
|
*
|
|
25
34
|
* @param root Root directory where static files are served from
|
|
35
|
+
* @param opts Optional options object
|
|
26
36
|
*/
|
|
27
37
|
export declare function createStaticFileHandler(root: Switch.PathLike, opts?: StaticFileHandlerOpts): (req: Request) => Promise<Response | null>;
|
package/dist/static.js
CHANGED
|
@@ -10,9 +10,11 @@ export function resolvePath(url, root) {
|
|
|
10
10
|
* @example
|
|
11
11
|
*
|
|
12
12
|
* ```typescript
|
|
13
|
-
*
|
|
13
|
+
* import { createStaticFileHandler, listen } from '@nx.js/http';
|
|
14
14
|
*
|
|
15
|
-
*
|
|
15
|
+
* const fileHandler = createStaticFileHandler('sdmc:/switch/');
|
|
16
|
+
*
|
|
17
|
+
* listen({
|
|
16
18
|
* port: 8080,
|
|
17
19
|
* async fetch(req) {
|
|
18
20
|
* let res = await fileHandler(req);
|
|
@@ -25,11 +27,13 @@ export function resolvePath(url, root) {
|
|
|
25
27
|
* ```
|
|
26
28
|
*
|
|
27
29
|
* @param root Root directory where static files are served from
|
|
30
|
+
* @param opts Optional options object
|
|
28
31
|
*/
|
|
29
|
-
export function createStaticFileHandler(root, opts
|
|
32
|
+
export function createStaticFileHandler(root, opts) {
|
|
30
33
|
let rootStr = String(root);
|
|
31
34
|
if (!rootStr.endsWith('/'))
|
|
32
35
|
rootStr += '/';
|
|
36
|
+
const oHeaders = opts?.headers;
|
|
33
37
|
return async (req) => {
|
|
34
38
|
const url = resolvePath(req.url, rootStr);
|
|
35
39
|
const stat = await Switch.stat(url);
|
|
@@ -39,9 +43,7 @@ export function createStaticFileHandler(root, opts = {}) {
|
|
|
39
43
|
const data = await Switch.readFile(url);
|
|
40
44
|
if (!data)
|
|
41
45
|
return null;
|
|
42
|
-
const headers = new Headers(typeof
|
|
43
|
-
? opts.headers(req, url)
|
|
44
|
-
: opts.headers);
|
|
46
|
+
const headers = new Headers(typeof oHeaders === 'function' ? oHeaders(req, url) : oHeaders);
|
|
45
47
|
headers.set('content-length', String(data.byteLength));
|
|
46
48
|
headers.set('content-type', mime.getType(url.pathname) || 'application/octet-stream');
|
|
47
49
|
return new Response(data, { headers });
|
package/dist/static.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static.js","sourceRoot":"","sources":["../src/static.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"static.js","sourceRoot":"","sources":["../src/static.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAaxB,MAAM,UAAU,WAAW,CAAC,GAAW,EAAE,IAAY;IACpD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AAI,0BAA0B,CACxD,CAAC;QACF,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx.js/http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "HTTP server for nx.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/TooTallNate/nx.js.git",
|
|
8
|
+
"directory": "packages/http"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://nxjs.n8.io/http",
|
|
5
11
|
"main": "dist/index.js",
|
|
6
12
|
"files": [
|
|
7
13
|
"dist"
|
|
@@ -12,7 +18,7 @@
|
|
|
12
18
|
"devDependencies": {
|
|
13
19
|
"vite": "^5.1.6",
|
|
14
20
|
"vitest": "^1.3.1",
|
|
15
|
-
"nxjs-runtime": "0.0.
|
|
21
|
+
"nxjs-runtime": "0.0.48"
|
|
16
22
|
},
|
|
17
23
|
"keywords": [
|
|
18
24
|
"nx.js",
|
|
@@ -27,6 +33,7 @@
|
|
|
27
33
|
},
|
|
28
34
|
"scripts": {
|
|
29
35
|
"build": "tsc",
|
|
30
|
-
"test": "vitest"
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"docs": "typedoc && ../../type-aliases-meta.sh"
|
|
31
38
|
}
|
|
32
39
|
}
|