@vyriy/server 0.2.1 → 0.3.0
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/README.md +54 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/listener.d.ts +2 -2
- package/listener.js +10 -1
- package/package.json +16 -5
- package/result.js +10 -7
- package/static.d.ts +8 -0
- package/static.js +120 -0
- package/types.d.ts +11 -5
package/README.md
CHANGED
|
@@ -32,4 +32,58 @@ server(async () => ({
|
|
|
32
32
|
}));
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
Run a Lambda response streaming handler locally:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { api, streamify } from '@vyriy/handler';
|
|
39
|
+
import { server } from '@vyriy/server';
|
|
40
|
+
|
|
41
|
+
server(
|
|
42
|
+
streamify(
|
|
43
|
+
api(async (event, responseStream) => {
|
|
44
|
+
responseStream.setContentType?.('text/plain');
|
|
45
|
+
responseStream.write(`Path: ${event.path}\n`);
|
|
46
|
+
responseStream.end('Done');
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Keep the AWS-specific `awslambda.streamifyResponse(handler)` wrapper in a separate Lambda entrypoint.
|
|
53
|
+
|
|
54
|
+
Serve static files through `@vyriy/router` prefix routes:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { createRouter } from '@vyriy/router';
|
|
58
|
+
import { staticFiles } from '@vyriy/server/static';
|
|
59
|
+
|
|
60
|
+
const router = createRouter().prefix('/static', staticFiles('./public'));
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Serve a static directory directly when the server only needs files:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { server, staticFiles } from '@vyriy/server';
|
|
67
|
+
|
|
68
|
+
server(
|
|
69
|
+
staticFiles('./public', {
|
|
70
|
+
fallback: 'index.html',
|
|
71
|
+
fallbackStatusCode: 200,
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
By default, missing static files fall back to `404.html` from the same directory with status `404` when that file exists.
|
|
77
|
+
For static apps that should fall back to `index.html`, configure the fallback explicitly:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const router = createRouter().prefix(
|
|
81
|
+
'/static',
|
|
82
|
+
staticFiles('./public', {
|
|
83
|
+
fallback: 'index.html',
|
|
84
|
+
fallbackStatusCode: 200,
|
|
85
|
+
}),
|
|
86
|
+
);
|
|
87
|
+
```
|
|
88
|
+
|
|
35
89
|
The server listens on `PORT` from `@vyriy/env`. The default port is `3000`.
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/listener.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const listener: (handler:
|
|
1
|
+
import type { NativeRequestListener, ServerHandler } from './types.js';
|
|
2
|
+
export declare const listener: (handler: ServerHandler) => NativeRequestListener;
|
package/listener.js
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { mapParams } from './params.js';
|
|
2
2
|
import { error, result } from './result.js';
|
|
3
|
+
const isStreamHandler = (handler) => handler.length >= 3;
|
|
4
|
+
const createResponseStream = (response) => {
|
|
5
|
+
response.setContentType = (contentType) => response.setHeader?.('content-type', contentType) ?? response;
|
|
6
|
+
return response;
|
|
7
|
+
};
|
|
3
8
|
export const listener = (handler) => async (request, response) => {
|
|
4
9
|
try {
|
|
5
10
|
const { context, event } = await mapParams(request);
|
|
11
|
+
if (isStreamHandler(handler)) {
|
|
12
|
+
await handler(event, createResponseStream(response), context);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
6
15
|
const value = await handler(event, context);
|
|
7
|
-
result(response, value);
|
|
16
|
+
await result(response, value);
|
|
8
17
|
}
|
|
9
18
|
catch {
|
|
10
19
|
error(response);
|
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vyriy/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Small HTTP server adapter for Lambda-style Vyriy handlers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@types/aws-lambda": "^8.10.161",
|
|
9
|
-
"@vyriy/env": "0.
|
|
10
|
-
"@vyriy/error": "0.
|
|
11
|
-
"@vyriy/handler": "0.
|
|
12
|
-
"@vyriy/logger": "0.
|
|
9
|
+
"@vyriy/env": "0.3.0",
|
|
10
|
+
"@vyriy/error": "0.3.0",
|
|
11
|
+
"@vyriy/handler": "0.3.0",
|
|
12
|
+
"@vyriy/logger": "0.3.0",
|
|
13
|
+
"@vyriy/router": "0.3.0"
|
|
13
14
|
},
|
|
14
15
|
"agents": "./AGENTS.md",
|
|
15
16
|
"license": "MIT",
|
|
@@ -114,6 +115,16 @@
|
|
|
114
115
|
"types": "./shutdown.d.ts",
|
|
115
116
|
"import": "./shutdown.js",
|
|
116
117
|
"default": "./shutdown.js"
|
|
118
|
+
},
|
|
119
|
+
"./static": {
|
|
120
|
+
"types": "./static.d.ts",
|
|
121
|
+
"import": "./static.js",
|
|
122
|
+
"default": "./static.js"
|
|
123
|
+
},
|
|
124
|
+
"./static.js": {
|
|
125
|
+
"types": "./static.d.ts",
|
|
126
|
+
"import": "./static.js",
|
|
127
|
+
"default": "./static.js"
|
|
117
128
|
}
|
|
118
129
|
}
|
|
119
130
|
}
|
package/result.js
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
const notFound = (response) => {
|
|
3
|
+
response
|
|
4
|
+
.writeHead(404, {
|
|
5
|
+
'content-type': 'application/json',
|
|
6
|
+
})
|
|
7
|
+
.end(JSON.stringify({ message: STATUS_CODES[404] }));
|
|
8
|
+
};
|
|
2
9
|
export const result = (response, value) => {
|
|
3
10
|
if (!value) {
|
|
4
|
-
response
|
|
5
|
-
.writeHead(404, {
|
|
6
|
-
'content-type': 'application/json',
|
|
7
|
-
})
|
|
8
|
-
.end(JSON.stringify({ message: STATUS_CODES[404] }));
|
|
11
|
+
notFound(response);
|
|
9
12
|
return;
|
|
10
13
|
}
|
|
11
|
-
const { body, headers, multiValueHeaders, statusCode } = value;
|
|
14
|
+
const { body, headers, isBase64Encoded, multiValueHeaders, statusCode } = value;
|
|
12
15
|
response.writeHead(statusCode, {
|
|
13
16
|
...headers,
|
|
14
17
|
...multiValueHeaders,
|
|
15
18
|
});
|
|
16
|
-
response.end(body);
|
|
19
|
+
response.end(isBase64Encoded ? Buffer.from(body, 'base64') : body);
|
|
17
20
|
};
|
|
18
21
|
export const error = (response) => {
|
|
19
22
|
response
|
package/static.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
2
|
+
import type { Handler } from '@vyriy/router';
|
|
3
|
+
export type StaticFilesOptions = {
|
|
4
|
+
fallback?: false | string;
|
|
5
|
+
fallbackStatusCode?: number;
|
|
6
|
+
};
|
|
7
|
+
export type StaticFilesHandler = Handler & ((event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>);
|
|
8
|
+
export declare const staticFiles: (directory: string, options?: StaticFilesOptions) => StaticFilesHandler;
|
package/static.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
3
|
+
import { extname, resolve, sep } from 'node:path';
|
|
4
|
+
const CONTENT_TYPES = {
|
|
5
|
+
'.css': 'text/css; charset=utf-8',
|
|
6
|
+
'.gif': 'image/gif',
|
|
7
|
+
'.html': 'text/html; charset=utf-8',
|
|
8
|
+
'.ico': 'image/x-icon',
|
|
9
|
+
'.jpeg': 'image/jpeg',
|
|
10
|
+
'.jpg': 'image/jpeg',
|
|
11
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
12
|
+
'.json': 'application/json; charset=utf-8',
|
|
13
|
+
'.mjs': 'text/javascript; charset=utf-8',
|
|
14
|
+
'.png': 'image/png',
|
|
15
|
+
'.svg': 'image/svg+xml; charset=utf-8',
|
|
16
|
+
'.txt': 'text/plain; charset=utf-8',
|
|
17
|
+
'.webp': 'image/webp',
|
|
18
|
+
};
|
|
19
|
+
const getContentType = (filePath) => CONTENT_TYPES[extname(filePath).toLowerCase()] ?? 'application/octet-stream';
|
|
20
|
+
const notFound = () => ({
|
|
21
|
+
body: JSON.stringify({
|
|
22
|
+
message: STATUS_CODES[404],
|
|
23
|
+
}),
|
|
24
|
+
headers: {
|
|
25
|
+
'content-type': 'application/json',
|
|
26
|
+
},
|
|
27
|
+
statusCode: 404,
|
|
28
|
+
});
|
|
29
|
+
const methodNotAllowed = () => ({
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
message: STATUS_CODES[405],
|
|
32
|
+
}),
|
|
33
|
+
headers: {
|
|
34
|
+
allow: 'GET, HEAD',
|
|
35
|
+
'content-type': 'application/json',
|
|
36
|
+
},
|
|
37
|
+
statusCode: 405,
|
|
38
|
+
});
|
|
39
|
+
const getFilePath = (directory, relativePath) => {
|
|
40
|
+
const rootPath = resolve(directory);
|
|
41
|
+
const filePath = resolve(rootPath, relativePath.replace(/^\/+/, '') || 'index.html');
|
|
42
|
+
if (filePath !== rootPath && !filePath.startsWith(`${rootPath}${sep}`)) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
return filePath;
|
|
46
|
+
};
|
|
47
|
+
const readFileResponse = async (filePath, method, statusCode = 200) => {
|
|
48
|
+
const file = await stat(filePath);
|
|
49
|
+
if (!file.isFile()) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const headers = {
|
|
53
|
+
'content-length': String(file.size),
|
|
54
|
+
'content-type': getContentType(filePath),
|
|
55
|
+
};
|
|
56
|
+
if (method === 'HEAD') {
|
|
57
|
+
return {
|
|
58
|
+
body: '',
|
|
59
|
+
headers,
|
|
60
|
+
statusCode,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
body: (await readFile(filePath)).toString('base64'),
|
|
65
|
+
headers,
|
|
66
|
+
isBase64Encoded: true,
|
|
67
|
+
statusCode,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const readFallbackResponse = async (directory, method, options) => {
|
|
71
|
+
if (options.fallback === false) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const fallbackPath = getFilePath(directory, options.fallback);
|
|
75
|
+
if (!fallbackPath) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
return await readFileResponse(fallbackPath, method, options.fallbackStatusCode);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const isRouterParams = (value) => 'event' in value;
|
|
86
|
+
const getRequest = (value) => {
|
|
87
|
+
if (isRouterParams(value)) {
|
|
88
|
+
return {
|
|
89
|
+
event: value.event,
|
|
90
|
+
relativePath: value.pathParameters?.proxy ?? '',
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
event: value,
|
|
95
|
+
relativePath: value.path.replace(/^\/+/, ''),
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
export const staticFiles = (directory, options = {}) => async (params) => {
|
|
99
|
+
const { event, relativePath } = getRequest(params);
|
|
100
|
+
const normalizedOptions = {
|
|
101
|
+
fallback: '404.html',
|
|
102
|
+
fallbackStatusCode: 404,
|
|
103
|
+
...options,
|
|
104
|
+
};
|
|
105
|
+
if (event.httpMethod !== 'GET' && event.httpMethod !== 'HEAD') {
|
|
106
|
+
return methodNotAllowed();
|
|
107
|
+
}
|
|
108
|
+
const filePath = getFilePath(directory, relativePath);
|
|
109
|
+
if (!filePath) {
|
|
110
|
+
return (await readFallbackResponse(directory, event.httpMethod, normalizedOptions)) ?? notFound();
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return ((await readFileResponse(filePath, event.httpMethod)) ??
|
|
114
|
+
(await readFallbackResponse(directory, event.httpMethod, normalizedOptions)) ??
|
|
115
|
+
notFound());
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return (await readFallbackResponse(directory, event.httpMethod, normalizedOptions)) ?? notFound();
|
|
119
|
+
}
|
|
120
|
+
};
|
package/types.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ResponseStream } from '@vyriy/handler';
|
|
3
3
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders, Server as HttpServer } from 'node:http';
|
|
4
4
|
export type { Context } from 'aws-lambda';
|
|
5
5
|
export type Headers = OutgoingHttpHeaders;
|
|
6
6
|
export type LambdaEvent = APIGatewayProxyEvent;
|
|
7
7
|
export type LambdaResult = APIGatewayProxyResult;
|
|
8
|
-
export type LambdaHandler =
|
|
8
|
+
export type LambdaHandler = (event: LambdaEvent, context: Context) => Promise<LambdaResult>;
|
|
9
|
+
export type LambdaStreamHandler = (event: LambdaEvent, responseStream: ResponseStream, context: Context) => Promise<LambdaResult | void>;
|
|
10
|
+
export type ServerHandler = LambdaHandler | LambdaStreamHandler;
|
|
9
11
|
export type RequestMessage = {
|
|
10
12
|
headers: IncomingHttpHeaders;
|
|
11
13
|
method?: string;
|
|
@@ -18,8 +20,12 @@ export type RequestMessage = {
|
|
|
18
20
|
export type ResponseMessage = {
|
|
19
21
|
end: {
|
|
20
22
|
(): ResponseMessage;
|
|
21
|
-
(chunk: string): ResponseMessage;
|
|
23
|
+
(chunk: string | Buffer | Uint8Array): ResponseMessage;
|
|
22
24
|
};
|
|
25
|
+
writableEnded?: boolean;
|
|
26
|
+
setContentType?: (contentType: string) => ResponseMessage;
|
|
27
|
+
setHeader?(name: string, value: number | string | readonly string[]): ResponseMessage;
|
|
28
|
+
write(chunk: string | Buffer | Uint8Array): boolean;
|
|
23
29
|
writeHead(statusCode: number, headers?: OutgoingHttpHeaders): ResponseMessage;
|
|
24
30
|
};
|
|
25
31
|
export type ErrorEventTarget = {
|
|
@@ -34,6 +40,6 @@ export type MapParams = (request: RequestMessage) => Promise<{
|
|
|
34
40
|
export type Server = HttpServer<typeof import('node:http').IncomingMessage, typeof import('node:http').ServerResponse>;
|
|
35
41
|
export type Listen = (server: Server) => Server;
|
|
36
42
|
export type NormalizeHeaders = (headers: IncomingHttpHeaders) => Record<string, string | undefined>;
|
|
37
|
-
export type WriteResult<Response extends ResponseMessage = ResponseMessage> = (response: Response, result: LambdaResult | void) => void;
|
|
43
|
+
export type WriteResult<Response extends ResponseMessage = ResponseMessage> = (response: Response, result: LambdaResult | void) => Promise<void> | void;
|
|
38
44
|
export type WriteError<Response extends ResponseMessage = ResponseMessage> = (response: Response) => void;
|
|
39
|
-
export type CreateServer = (handler:
|
|
45
|
+
export type CreateServer = (handler: ServerHandler) => Server;
|