@vyriy/server 0.1.9
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/LICENSE +21 -0
- package/README.md +35 -0
- package/body.d.ts +2 -0
- package/body.js +23 -0
- package/index.d.ts +2 -0
- package/index.js +1 -0
- package/listen.d.ts +2 -0
- package/listen.js +27 -0
- package/listener.d.ts +2 -0
- package/listener.js +12 -0
- package/log.d.ts +3 -0
- package/log.js +17 -0
- package/package.json +113 -0
- package/params.d.ts +3 -0
- package/params.js +52 -0
- package/result.d.ts +3 -0
- package/result.js +24 -0
- package/server.d.ts +2 -0
- package/server.js +4 -0
- package/shutdown.d.ts +5 -0
- package/shutdown.js +27 -0
- package/types.d.ts +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vyriy contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @vyriy/server
|
|
2
|
+
|
|
3
|
+
Small HTTP server adapter for running Lambda-style API Gateway handlers.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
With npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @vyriy/server
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
With Yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @vyriy/server
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Run a Lambda-style handler over HTTP:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { server } from '@vyriy/server';
|
|
25
|
+
|
|
26
|
+
server(async () => ({
|
|
27
|
+
statusCode: 200,
|
|
28
|
+
headers: {
|
|
29
|
+
'content-type': 'application/json',
|
|
30
|
+
},
|
|
31
|
+
body: JSON.stringify({ ok: true }),
|
|
32
|
+
}));
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The server listens on `PORT` from `@vyriy/env`. The default port is `3000`.
|
package/body.d.ts
ADDED
package/body.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { toError } from '@vyriy/error';
|
|
2
|
+
export const getBody = (request) => new Promise((resolve, reject) => {
|
|
3
|
+
const chunks = [];
|
|
4
|
+
let ended = false;
|
|
5
|
+
request
|
|
6
|
+
.on('data', (chunk) => {
|
|
7
|
+
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
|
|
8
|
+
})
|
|
9
|
+
.on('end', () => {
|
|
10
|
+
ended = true;
|
|
11
|
+
if (!chunks.length) {
|
|
12
|
+
resolve(undefined);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
resolve(Buffer.concat(chunks).toString());
|
|
16
|
+
})
|
|
17
|
+
.on('close', () => {
|
|
18
|
+
if (!ended) {
|
|
19
|
+
reject(toError('Request body stream closed before it finished reading'));
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
.on('error', (error) => reject(toError(error)));
|
|
23
|
+
});
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { server } from './server.js';
|
package/listen.d.ts
ADDED
package/listen.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getPort } from '@vyriy/env';
|
|
2
|
+
import { toError } from '@vyriy/error';
|
|
3
|
+
import { logError, logListening } from './log.js';
|
|
4
|
+
import { graceful } from './shutdown.js';
|
|
5
|
+
export const listen = (server) => {
|
|
6
|
+
server.on('error', (error) => {
|
|
7
|
+
logError('Server error:', error);
|
|
8
|
+
process.exit(1);
|
|
9
|
+
});
|
|
10
|
+
server.on('request', (request, response) => {
|
|
11
|
+
request.on('error', (error) => {
|
|
12
|
+
logError('Request error:', toError(error));
|
|
13
|
+
});
|
|
14
|
+
response.on('error', (error) => {
|
|
15
|
+
logError('Response error:', error);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
server.on('listening', () => {
|
|
19
|
+
const address = server.address();
|
|
20
|
+
if (address && typeof address !== 'string') {
|
|
21
|
+
logListening(address);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
graceful(server);
|
|
25
|
+
server.listen(Number(getPort()));
|
|
26
|
+
return server;
|
|
27
|
+
};
|
package/listener.d.ts
ADDED
package/listener.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { mapParams } from './params.js';
|
|
2
|
+
import { error, result } from './result.js';
|
|
3
|
+
export const listener = (handler) => async (request, response) => {
|
|
4
|
+
try {
|
|
5
|
+
const { context, event } = await mapParams(request);
|
|
6
|
+
const value = await handler(event, context);
|
|
7
|
+
result(response, value);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
error(response);
|
|
11
|
+
}
|
|
12
|
+
};
|
package/log.d.ts
ADDED
package/log.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { networkInterfaces } from 'node:os';
|
|
2
|
+
import { createLogger } from '@vyriy/logger';
|
|
3
|
+
export const logListening = (address) => {
|
|
4
|
+
const logger = createLogger();
|
|
5
|
+
Object.values(networkInterfaces())
|
|
6
|
+
.flat()
|
|
7
|
+
.forEach((details) => {
|
|
8
|
+
if (details?.family === 'IPv4') {
|
|
9
|
+
logger.warn(`http://${details.address}:${address.port}/`);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
export const logError = (label, value) => {
|
|
14
|
+
const logger = createLogger();
|
|
15
|
+
logger.error(label);
|
|
16
|
+
logger.error(value);
|
|
17
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vyriy/server",
|
|
3
|
+
"version": "0.1.9",
|
|
4
|
+
"description": "Small HTTP server adapter for Lambda-style Vyriy handlers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@types/aws-lambda": "^8.10.161",
|
|
9
|
+
"@vyriy/env": "0.1.9",
|
|
10
|
+
"@vyriy/error": "0.1.9",
|
|
11
|
+
"@vyriy/handler": "0.1.9",
|
|
12
|
+
"@vyriy/logger": "0.1.9"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"import": "./index.js",
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
},
|
|
22
|
+
"./body": {
|
|
23
|
+
"types": "./body.d.ts",
|
|
24
|
+
"import": "./body.js",
|
|
25
|
+
"default": "./body.js"
|
|
26
|
+
},
|
|
27
|
+
"./body.js": {
|
|
28
|
+
"types": "./body.d.ts",
|
|
29
|
+
"import": "./body.js",
|
|
30
|
+
"default": "./body.js"
|
|
31
|
+
},
|
|
32
|
+
"./index": {
|
|
33
|
+
"types": "./index.d.ts",
|
|
34
|
+
"import": "./index.js",
|
|
35
|
+
"default": "./index.js"
|
|
36
|
+
},
|
|
37
|
+
"./index.js": {
|
|
38
|
+
"types": "./index.d.ts",
|
|
39
|
+
"import": "./index.js",
|
|
40
|
+
"default": "./index.js"
|
|
41
|
+
},
|
|
42
|
+
"./listen": {
|
|
43
|
+
"types": "./listen.d.ts",
|
|
44
|
+
"import": "./listen.js",
|
|
45
|
+
"default": "./listen.js"
|
|
46
|
+
},
|
|
47
|
+
"./listen.js": {
|
|
48
|
+
"types": "./listen.d.ts",
|
|
49
|
+
"import": "./listen.js",
|
|
50
|
+
"default": "./listen.js"
|
|
51
|
+
},
|
|
52
|
+
"./listener": {
|
|
53
|
+
"types": "./listener.d.ts",
|
|
54
|
+
"import": "./listener.js",
|
|
55
|
+
"default": "./listener.js"
|
|
56
|
+
},
|
|
57
|
+
"./listener.js": {
|
|
58
|
+
"types": "./listener.d.ts",
|
|
59
|
+
"import": "./listener.js",
|
|
60
|
+
"default": "./listener.js"
|
|
61
|
+
},
|
|
62
|
+
"./log": {
|
|
63
|
+
"types": "./log.d.ts",
|
|
64
|
+
"import": "./log.js",
|
|
65
|
+
"default": "./log.js"
|
|
66
|
+
},
|
|
67
|
+
"./log.js": {
|
|
68
|
+
"types": "./log.d.ts",
|
|
69
|
+
"import": "./log.js",
|
|
70
|
+
"default": "./log.js"
|
|
71
|
+
},
|
|
72
|
+
"./params": {
|
|
73
|
+
"types": "./params.d.ts",
|
|
74
|
+
"import": "./params.js",
|
|
75
|
+
"default": "./params.js"
|
|
76
|
+
},
|
|
77
|
+
"./params.js": {
|
|
78
|
+
"types": "./params.d.ts",
|
|
79
|
+
"import": "./params.js",
|
|
80
|
+
"default": "./params.js"
|
|
81
|
+
},
|
|
82
|
+
"./result": {
|
|
83
|
+
"types": "./result.d.ts",
|
|
84
|
+
"import": "./result.js",
|
|
85
|
+
"default": "./result.js"
|
|
86
|
+
},
|
|
87
|
+
"./result.js": {
|
|
88
|
+
"types": "./result.d.ts",
|
|
89
|
+
"import": "./result.js",
|
|
90
|
+
"default": "./result.js"
|
|
91
|
+
},
|
|
92
|
+
"./server": {
|
|
93
|
+
"types": "./server.d.ts",
|
|
94
|
+
"import": "./server.js",
|
|
95
|
+
"default": "./server.js"
|
|
96
|
+
},
|
|
97
|
+
"./server.js": {
|
|
98
|
+
"types": "./server.d.ts",
|
|
99
|
+
"import": "./server.js",
|
|
100
|
+
"default": "./server.js"
|
|
101
|
+
},
|
|
102
|
+
"./shutdown": {
|
|
103
|
+
"types": "./shutdown.d.ts",
|
|
104
|
+
"import": "./shutdown.js",
|
|
105
|
+
"default": "./shutdown.js"
|
|
106
|
+
},
|
|
107
|
+
"./shutdown.js": {
|
|
108
|
+
"types": "./shutdown.d.ts",
|
|
109
|
+
"import": "./shutdown.js",
|
|
110
|
+
"default": "./shutdown.js"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
package/params.d.ts
ADDED
package/params.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { URL } from 'node:url';
|
|
2
|
+
import { getBody } from './body.js';
|
|
3
|
+
export const normalizeHeaders = (headers) => Object.fromEntries(Object.entries(headers).map(([key, value]) => [
|
|
4
|
+
key,
|
|
5
|
+
Array.isArray(value) ? value.join(', ') : value,
|
|
6
|
+
]));
|
|
7
|
+
const mapQuery = (url) => {
|
|
8
|
+
const queryStringParameters = {};
|
|
9
|
+
const multiValueQueryStringParameters = {};
|
|
10
|
+
url.searchParams.forEach((value, key) => {
|
|
11
|
+
queryStringParameters[key] = value;
|
|
12
|
+
multiValueQueryStringParameters[key] = [...(multiValueQueryStringParameters[key] ?? []), value];
|
|
13
|
+
});
|
|
14
|
+
return { multiValueQueryStringParameters, queryStringParameters };
|
|
15
|
+
};
|
|
16
|
+
const createContext = () => ({
|
|
17
|
+
awsRequestId: 'local',
|
|
18
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
19
|
+
done: () => undefined,
|
|
20
|
+
fail: () => undefined,
|
|
21
|
+
functionName: 'local',
|
|
22
|
+
functionVersion: '$LATEST',
|
|
23
|
+
getRemainingTimeInMillis: () => 30000,
|
|
24
|
+
invokedFunctionArn: 'local',
|
|
25
|
+
logGroupName: 'local',
|
|
26
|
+
logStreamName: 'local',
|
|
27
|
+
memoryLimitInMB: '128',
|
|
28
|
+
succeed: () => undefined,
|
|
29
|
+
});
|
|
30
|
+
export const mapParams = async (request) => {
|
|
31
|
+
const url = new URL(request.url ?? '/', `http://${request.headers.host ?? 'localhost'}`);
|
|
32
|
+
const { multiValueQueryStringParameters, queryStringParameters } = mapQuery(url);
|
|
33
|
+
const headers = normalizeHeaders(request.headers);
|
|
34
|
+
const body = await getBody(request);
|
|
35
|
+
return {
|
|
36
|
+
event: {
|
|
37
|
+
body: body ?? null,
|
|
38
|
+
headers,
|
|
39
|
+
httpMethod: request.method?.toUpperCase() ?? 'GET',
|
|
40
|
+
isBase64Encoded: false,
|
|
41
|
+
multiValueHeaders: Object.fromEntries(Object.entries(headers).map(([key, value]) => [key, value ? [value] : []])),
|
|
42
|
+
multiValueQueryStringParameters,
|
|
43
|
+
path: url.pathname,
|
|
44
|
+
pathParameters: null,
|
|
45
|
+
queryStringParameters,
|
|
46
|
+
requestContext: {},
|
|
47
|
+
resource: url.pathname,
|
|
48
|
+
stageVariables: null,
|
|
49
|
+
},
|
|
50
|
+
context: createContext(),
|
|
51
|
+
};
|
|
52
|
+
};
|
package/result.d.ts
ADDED
package/result.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
export const result = (response, value) => {
|
|
3
|
+
if (!value) {
|
|
4
|
+
response
|
|
5
|
+
.writeHead(404, {
|
|
6
|
+
'content-type': 'application/json',
|
|
7
|
+
})
|
|
8
|
+
.end(JSON.stringify({ message: STATUS_CODES[404] }));
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const { body, headers, multiValueHeaders, statusCode } = value;
|
|
12
|
+
response.writeHead(statusCode, {
|
|
13
|
+
...headers,
|
|
14
|
+
...multiValueHeaders,
|
|
15
|
+
});
|
|
16
|
+
response.end(body);
|
|
17
|
+
};
|
|
18
|
+
export const error = (response) => {
|
|
19
|
+
response
|
|
20
|
+
.writeHead(500, {
|
|
21
|
+
'content-type': 'application/json',
|
|
22
|
+
})
|
|
23
|
+
.end(JSON.stringify({ message: STATUS_CODES[500] }));
|
|
24
|
+
};
|
package/server.d.ts
ADDED
package/server.js
ADDED
package/shutdown.d.ts
ADDED
package/shutdown.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createLogger } from '@vyriy/logger';
|
|
2
|
+
export const shutdown = (server, signal) => {
|
|
3
|
+
const logger = createLogger();
|
|
4
|
+
logger.warn(`Received ${signal}. Closing server...`);
|
|
5
|
+
server.close((error) => {
|
|
6
|
+
if (error) {
|
|
7
|
+
logger.error('Server shutdown error:');
|
|
8
|
+
logger.error(error);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
logger.warn('Server closed.');
|
|
13
|
+
process.exit(0);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
export const graceful = (server) => {
|
|
17
|
+
let closing = false;
|
|
18
|
+
['SIGINT', 'SIGTERM'].forEach((signal) => {
|
|
19
|
+
process.once(signal, () => {
|
|
20
|
+
if (closing) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
closing = true;
|
|
24
|
+
shutdown(server, signal);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
};
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
2
|
+
import type { Handler } from '@vyriy/handler';
|
|
3
|
+
import type { IncomingHttpHeaders, OutgoingHttpHeaders, Server as HttpServer } from 'node:http';
|
|
4
|
+
export type { Context } from 'aws-lambda';
|
|
5
|
+
export type Headers = OutgoingHttpHeaders;
|
|
6
|
+
export type LambdaEvent = APIGatewayProxyEvent;
|
|
7
|
+
export type LambdaResult = APIGatewayProxyResult;
|
|
8
|
+
export type LambdaHandler = Handler<LambdaEvent, LambdaResult>;
|
|
9
|
+
export type RequestMessage = {
|
|
10
|
+
headers: IncomingHttpHeaders;
|
|
11
|
+
method?: string;
|
|
12
|
+
url?: string;
|
|
13
|
+
on(event: 'close', listener: () => void): RequestMessage;
|
|
14
|
+
on(event: 'data', listener: (chunk: Buffer | string) => void): RequestMessage;
|
|
15
|
+
on(event: 'end', listener: () => void): RequestMessage;
|
|
16
|
+
on(event: 'error', listener: (error: unknown) => void): RequestMessage;
|
|
17
|
+
};
|
|
18
|
+
export type ResponseMessage = {
|
|
19
|
+
end: {
|
|
20
|
+
(): ResponseMessage;
|
|
21
|
+
(chunk: string): ResponseMessage;
|
|
22
|
+
};
|
|
23
|
+
writeHead(statusCode: number, headers?: OutgoingHttpHeaders): ResponseMessage;
|
|
24
|
+
};
|
|
25
|
+
export type ErrorEventTarget = {
|
|
26
|
+
on(event: 'error', listener: (error: Error) => void): ErrorEventTarget;
|
|
27
|
+
};
|
|
28
|
+
export type NativeRequestListener<Request = RequestMessage, Response = ResponseMessage> = (request: Request, response: Response) => void;
|
|
29
|
+
export type GetBody = (request: RequestMessage) => Promise<string | undefined>;
|
|
30
|
+
export type MapParams = (request: RequestMessage) => Promise<{
|
|
31
|
+
event: LambdaEvent;
|
|
32
|
+
context: Context;
|
|
33
|
+
}>;
|
|
34
|
+
export type Server = HttpServer<typeof import('node:http').IncomingMessage, typeof import('node:http').ServerResponse>;
|
|
35
|
+
export type Listen = (server: Server) => Server;
|
|
36
|
+
export type NormalizeHeaders = (headers: IncomingHttpHeaders) => Record<string, string | undefined>;
|
|
37
|
+
export type WriteResult<Response extends ResponseMessage = ResponseMessage> = (response: Response, result: LambdaResult | void) => void;
|
|
38
|
+
export type WriteError<Response extends ResponseMessage = ResponseMessage> = (response: Response) => void;
|
|
39
|
+
export type CreateServer = (handler: LambdaHandler) => Server;
|