raiton 4.0.0 → 5.5.0-beta.3
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/CHANGELOG.md +37 -0
- package/README.md +54 -2
- package/build/bin/index.mjs +568 -275
- package/examples/user-controller.example.ts +53 -0
- package/package.json +6 -8
- package/source/bin/cli-tools.ts +2 -0
- package/source/commands/build.command.ts +2 -1
- package/source/commands/develop.command.ts +1 -0
- package/source/commands/start.command.ts +15 -10
- package/source/core/application.ts +40 -31
- package/source/core/artifacts/artifact.ts +10 -0
- package/source/core/artifacts/index.ts +1 -0
- package/source/core/artifacts/runner.ts +3 -0
- package/source/core/builder.ts +42 -13
- package/source/core/hooks.ts +0 -8
- package/source/core/index.ts +15 -15
- package/source/core/injection/injection.ts +7 -4
- package/source/core/middleware/pipeline.ts +2 -8
- package/source/core/plugins/scope.ts +18 -6
- package/source/core/router/handler.ts +24 -78
- package/source/core/router/handler.utils.ts +135 -0
- package/source/core/router/matcher.ts +13 -9
- package/source/core/router/router.ts +5 -1
- package/source/core/thread.ts +19 -16
- package/source/framework/artifacts.ts +7 -1
- package/source/framework/constants/decorators.constant.ts +7 -0
- package/source/framework/decorators/api-response.decorator.ts +127 -0
- package/source/framework/decorators/api.decorator.ts +219 -0
- package/source/framework/decorators/index.ts +4 -2
- package/source/framework/decorators/{parametrable.ts → parametrable.decorator.ts} +1 -1
- package/source/framework/encryption.ts +2 -1
- package/source/framework/enums/encrypted.enum.ts +2 -2
- package/source/framework/enums/http-parameters.enum.ts +1 -0
- package/source/framework/enums/runtime.enum.ts +2 -2
- package/source/framework/env.ts +2 -2
- package/source/framework/exceptions/http-exception.ts +1 -1
- package/source/framework/index.ts +1 -0
- package/source/framework/plugins/body-parser.plugin.ts +28 -11
- package/source/framework/plugins/index.ts +3 -2
- package/source/framework/plugins/openapi.plugin.ts +98 -0
- package/source/framework/plugins/security/cors.ts +59 -25
- package/source/framework/responses/helpers.ts +4 -3
- package/source/framework/responses/http-throwable.ts +2 -2
- package/source/framework/responses/http.ts +3 -3
- package/source/framework/runtime/bun/server.ts +6 -2
- package/source/framework/runtime/deno/server.ts +93 -42
- package/source/framework/runtime/index.ts +18 -13
- package/source/framework/runtime/node/reply.ts +37 -0
- package/source/framework/runtime/node/request.ts +47 -0
- package/source/framework/runtime/node/server.ts +5 -33
- package/source/framework/runtime/web/server.ts +73 -57
- package/source/framework/utilities/cookie.util.ts +11 -0
- package/source/framework/utilities/index.ts +3 -1
- package/source/framework/utilities/openapi.utils.ts +497 -0
- package/source/framework/view-model.ts +4 -0
- package/source/types/access-guards.ts +4 -0
- package/source/types/artifact.ts +42 -1
- package/source/types/builder.ts +6 -1
- package/source/types/http-responses.ts +8 -0
- package/source/types/runtime.ts +4 -2
- package/source/types/scheme.ts +153 -0
- package/source/types/thread.ts +3 -1
package/source/framework/env.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {GenericValueType} from "../types/generic";
|
|
2
2
|
import {getType} from "./utilities";
|
|
3
3
|
|
|
4
|
-
export function env<T>(key: string, type?: GenericValueType): T | undefined {
|
|
5
|
-
const value = process.env[key];
|
|
4
|
+
export function env<T>(key: string, defaultValue?: string, type?: GenericValueType): T | undefined {
|
|
5
|
+
const value = process.env[key] || defaultValue;
|
|
6
6
|
type = type || getType(value) as GenericValueType;
|
|
7
7
|
|
|
8
8
|
if (value) {
|
|
@@ -16,7 +16,7 @@ export class HttpException extends Error {
|
|
|
16
16
|
message: this.message,
|
|
17
17
|
statusCode: this.statusCode,
|
|
18
18
|
error: true,
|
|
19
|
-
stack: Raiton.thread?.builder?.options?.
|
|
19
|
+
stack: Raiton.thread?.builder?.options?.serve
|
|
20
20
|
? this.stack?.split('\n').map(e => e.trim())
|
|
21
21
|
: undefined
|
|
22
22
|
}
|
|
@@ -8,6 +8,7 @@ export * from "./runtime/index"
|
|
|
8
8
|
export * from "./exceptions/index"
|
|
9
9
|
export * from "./controllers"
|
|
10
10
|
export * from "./data-transfer-object"
|
|
11
|
+
export * from "./view-model"
|
|
11
12
|
export * from "./encryption"
|
|
12
13
|
export * from "./parameter-bag"
|
|
13
14
|
export * from "./repositories"
|
|
@@ -8,21 +8,29 @@ export function bodyParserPlugin(): PluginInterface {
|
|
|
8
8
|
name: 'body-parser-plugin',
|
|
9
9
|
setup: (scope) => {
|
|
10
10
|
scope.use(async ({context, next}: MiddlewareParametersInterface) => {
|
|
11
|
-
|
|
11
|
+
readQueryBody(context);
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
const isGetOrHead = ['GET', 'HEAD'].includes(context.req.method.toUpperCase());
|
|
14
|
+
|
|
15
|
+
if (isGetOrHead) {
|
|
16
|
+
context.req.body = context.req.query;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const hasBody = context.req.body !== undefined && context.req.body !== null;
|
|
20
|
+
|
|
21
|
+
if (hasBody && !context.state.bodyParsed && !isGetOrHead) {
|
|
14
22
|
try {
|
|
23
|
+
const contentType = context.req.headers.get('content-type') || '';
|
|
15
24
|
const rawBody = await readRawBody(context.req.body);
|
|
16
|
-
context.req.body = rawBody as unknown as any;
|
|
17
25
|
const bodyString = decode(rawBody);
|
|
18
26
|
|
|
19
|
-
|
|
20
|
-
await readMultipartBody(context, contentType);
|
|
27
|
+
await readMultipartBody(context, contentType, rawBody);
|
|
21
28
|
readJsonBody(context, contentType, bodyString);
|
|
22
29
|
readUrlEncodedBody(context, contentType, bodyString);
|
|
23
30
|
readTextBody(context, contentType, bodyString);
|
|
24
31
|
|
|
25
32
|
if (!context.state.bodyParsed) {
|
|
33
|
+
context.req.body = rawBody as any;
|
|
26
34
|
context.state.bodyParsed = true;
|
|
27
35
|
}
|
|
28
36
|
|
|
@@ -37,22 +45,20 @@ export function bodyParserPlugin(): PluginInterface {
|
|
|
37
45
|
};
|
|
38
46
|
}
|
|
39
47
|
|
|
40
|
-
function readJsonBody(ctx: RequestContext, contentType: string, body:
|
|
48
|
+
function readJsonBody(ctx: RequestContext, contentType: string, body: string) {
|
|
41
49
|
if (contentType.includes('application/json')) {
|
|
42
|
-
ctx.req.body = tryParseJson(body) || {}
|
|
50
|
+
ctx.req.body = tryParseJson(body) || {};
|
|
43
51
|
ctx.state.bodyParsed = true;
|
|
44
52
|
}
|
|
45
53
|
}
|
|
46
54
|
|
|
47
|
-
async function readMultipartBody(ctx: RequestContext, contentType: string) {
|
|
55
|
+
async function readMultipartBody(ctx: RequestContext, contentType: string, rawBody: any) {
|
|
48
56
|
if (contentType.includes('multipart/form-data')) {
|
|
49
|
-
const body = ctx.req.body;
|
|
50
|
-
|
|
51
57
|
try {
|
|
52
58
|
const tempRequest = new Request(ctx.req.url, {
|
|
53
59
|
method: ctx.req.method,
|
|
54
60
|
headers: ctx.req.headers,
|
|
55
|
-
body:
|
|
61
|
+
body: rawBody
|
|
56
62
|
});
|
|
57
63
|
|
|
58
64
|
const formData = await tempRequest.formData();
|
|
@@ -104,6 +110,7 @@ function readTextBody(ctx: RequestContext, contentType: string, body: string) {
|
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
function readQueryBody(ctx: RequestContext) {
|
|
113
|
+
|
|
107
114
|
const searchParams = new URLSearchParams(ctx.req.url.split('?')[1] || '');
|
|
108
115
|
const record = {} as Record<string, any>;
|
|
109
116
|
searchParams.forEach((value, key) => record[key] = value);
|
|
@@ -111,6 +118,16 @@ function readQueryBody(ctx: RequestContext) {
|
|
|
111
118
|
}
|
|
112
119
|
|
|
113
120
|
async function readRawBody(body: any): Promise<Uint8Array | string> {
|
|
121
|
+
if (!body) return new Uint8Array(0);
|
|
122
|
+
|
|
123
|
+
if (body instanceof Uint8Array || typeof body === 'string') {
|
|
124
|
+
return body;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (typeof body === 'object' && !('on' in body) && !(body instanceof ReadableStream)) {
|
|
128
|
+
return JSON.stringify(body);
|
|
129
|
+
}
|
|
130
|
+
|
|
114
131
|
if (body instanceof ReadableStream) {
|
|
115
132
|
const reader = body.getReader();
|
|
116
133
|
const chunks: Uint8Array[] = [];
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from "./body-parser.plugin"
|
|
2
|
-
export * from "./security/index"
|
|
1
|
+
export * from "./body-parser.plugin";
|
|
2
|
+
export * from "./security/index";
|
|
3
|
+
export * from "./openapi.plugin";
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type {PluginInterface} from "../../types";
|
|
2
|
+
import type {ContextInterface} from "../../types/core";
|
|
3
|
+
import {PluginScope} from "../../core";
|
|
4
|
+
import {Logger} from "@protorians/logger";
|
|
5
|
+
import {generateOpenApiSpec} from "../utilities/openapi.utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* OpenAPI plugin options
|
|
9
|
+
*/
|
|
10
|
+
export interface OpenApiOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The base route for the documentation UI (e.g., '/docs')
|
|
13
|
+
* @default '/docs'
|
|
14
|
+
*/
|
|
15
|
+
route?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The HTML string page
|
|
19
|
+
*/
|
|
20
|
+
ui?: (config: Omit<OpenApiOptions, 'ui'>, context: ContextInterface) => string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The endpoint for the raw JSON OpenAPI spec (e.g., '/docs/json')
|
|
24
|
+
* If set to false, disables the JSON endpoint.
|
|
25
|
+
* @default '/docs/json'
|
|
26
|
+
*/
|
|
27
|
+
endpoint?: string | false;
|
|
28
|
+
/**
|
|
29
|
+
* Title of the API
|
|
30
|
+
* @default 'API'
|
|
31
|
+
*/
|
|
32
|
+
title?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Version of the API
|
|
35
|
+
* @default '1.0.0'
|
|
36
|
+
*/
|
|
37
|
+
version?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Description of the API
|
|
40
|
+
* @default ''
|
|
41
|
+
*/
|
|
42
|
+
description?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Terms of service URL
|
|
45
|
+
*/
|
|
46
|
+
termsOfService?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Contact information
|
|
49
|
+
*/
|
|
50
|
+
contact?: {
|
|
51
|
+
name?: string;
|
|
52
|
+
url?: string;
|
|
53
|
+
email?: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* License information
|
|
57
|
+
*/
|
|
58
|
+
license?: {
|
|
59
|
+
name?: string;
|
|
60
|
+
url?: string;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function openApiPlugin(options: OpenApiOptions = {}): PluginInterface {
|
|
65
|
+
const opts = {
|
|
66
|
+
endpoint: '/docs/json',
|
|
67
|
+
title: 'API',
|
|
68
|
+
version: '1.0.0',
|
|
69
|
+
description: '',
|
|
70
|
+
termsOfService: undefined,
|
|
71
|
+
contact: undefined,
|
|
72
|
+
license: undefined,
|
|
73
|
+
...options
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
name: 'openapi-plugin',
|
|
78
|
+
setup: (scope: PluginScope) => {
|
|
79
|
+
|
|
80
|
+
if (opts.route && opts.ui) {
|
|
81
|
+
scope.get(opts.route, async ({reply, req, ...context}: ContextInterface) => {
|
|
82
|
+
const optionsWithoutUi = {...opts, ui: undefined}
|
|
83
|
+
const html = opts.ui?.(optionsWithoutUi, {reply, req, ...context});
|
|
84
|
+
reply.type('text/html');
|
|
85
|
+
reply.send(html || 'No Ui function defined');
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (opts.endpoint) {
|
|
90
|
+
scope.get(opts.endpoint, async ({reply}: ContextInterface) => {
|
|
91
|
+
const spec = await generateOpenApiSpec(scope, opts);
|
|
92
|
+
reply.type('application/json');
|
|
93
|
+
return spec
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -1,44 +1,78 @@
|
|
|
1
1
|
import {definePlugin} from "../../../core/plugins";
|
|
2
|
-
import {
|
|
2
|
+
import {MiddlewareParametersInterface} from "../../../types";
|
|
3
|
+
import {Logger} from "@protorians/logger";
|
|
3
4
|
|
|
4
5
|
export interface CorsOptions {
|
|
5
|
-
origin?: string | string[]
|
|
6
|
+
origin?: string | string[] | boolean | ((origin: string) => boolean | Promise<boolean>)
|
|
6
7
|
methods?: string[]
|
|
7
8
|
headers?: string[]
|
|
9
|
+
credentials?: boolean
|
|
10
|
+
maxAge?: number
|
|
11
|
+
exposeHeaders?: string[]
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
export const secureCors = (opts: CorsOptions = {}) =>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
export const secureCors = (opts: CorsOptions = {}) => {
|
|
15
|
+
|
|
16
|
+
return (async ({context, next}: MiddlewareParametersInterface) => {
|
|
17
|
+
const origin = context.req.headers.get('origin')
|
|
18
|
+
|
|
19
|
+
if (origin) {
|
|
20
|
+
context.reply.header('Vary', 'Origin')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let allowedOrigin: string | null = null
|
|
24
|
+
|
|
25
|
+
if (opts.origin === undefined || opts.origin === '*') {
|
|
26
|
+
allowedOrigin = '*'
|
|
27
|
+
} else if (opts.origin === true) {
|
|
28
|
+
allowedOrigin = origin || '*'
|
|
29
|
+
} else if (typeof opts.origin === 'function') {
|
|
30
|
+
if (origin && await opts.origin(origin)) {
|
|
31
|
+
allowedOrigin = origin
|
|
32
|
+
}
|
|
33
|
+
} else if (Array.isArray(opts.origin)) {
|
|
34
|
+
if (origin && opts.origin.includes(origin)) {
|
|
35
|
+
allowedOrigin = origin
|
|
36
|
+
}
|
|
37
|
+
} else if (typeof opts.origin === 'string') {
|
|
38
|
+
if (opts.origin === origin) {
|
|
39
|
+
allowedOrigin = origin
|
|
40
|
+
} else if (opts.origin === '*') {
|
|
41
|
+
allowedOrigin = '*'
|
|
25
42
|
}
|
|
43
|
+
}
|
|
26
44
|
|
|
45
|
+
if (allowedOrigin) {
|
|
46
|
+
context.reply.header('Access-Control-Allow-Origin', allowedOrigin)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (opts.credentials) {
|
|
50
|
+
context.reply.header('Access-Control-Allow-Credentials', 'true')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (opts.exposeHeaders) {
|
|
54
|
+
context.reply.header('Access-Control-Expose-Headers', opts.exposeHeaders.join(','))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (context.req.method === 'OPTIONS') {
|
|
27
58
|
context.reply.header(
|
|
28
59
|
'Access-Control-Allow-Methods',
|
|
29
|
-
(opts.methods ?? ['GET', 'POST', 'PUT', 'DELETE']).join(',')
|
|
60
|
+
(opts.methods ?? ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).join(',')
|
|
30
61
|
)
|
|
31
62
|
|
|
32
63
|
context.reply.header(
|
|
33
64
|
'Access-Control-Allow-Headers',
|
|
34
|
-
(opts.headers ?? ['Content-Type', 'Authorization']).join(',')
|
|
65
|
+
(opts.headers ?? ['Content-Type', 'Authorization', 'X-Requested-With']).join(',')
|
|
35
66
|
)
|
|
36
67
|
|
|
37
|
-
if (
|
|
38
|
-
context.reply.
|
|
39
|
-
return context.send(null)
|
|
68
|
+
if (opts.maxAge) {
|
|
69
|
+
context.reply.header('Access-Control-Max-Age', opts.maxAge.toString())
|
|
40
70
|
}
|
|
41
71
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
72
|
+
context.reply.status(204)
|
|
73
|
+
return context.send(null)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
await next()
|
|
77
|
+
})
|
|
78
|
+
}
|
|
@@ -5,16 +5,17 @@ import {Raiton} from "../../core";
|
|
|
5
5
|
|
|
6
6
|
export function RaitonResponses(
|
|
7
7
|
message: string,
|
|
8
|
-
data:
|
|
8
|
+
data: any,
|
|
9
|
+
// data: HttpResponseInterface<any> | null,
|
|
9
10
|
statusCode: HttpStatus,
|
|
10
11
|
metadata?: Omit<HttpResponseInterface<any>, 'data' | 'message' | 'statusCode'>
|
|
11
12
|
) {
|
|
12
13
|
|
|
13
14
|
if (metadata) {
|
|
14
|
-
metadata.error = typeof data
|
|
15
|
+
metadata.error = typeof data?.error === 'boolean' ? data?.error : false
|
|
15
16
|
metadata.errorStack = (metadata.errorStack instanceof Error)
|
|
16
17
|
? metadata.errorStack
|
|
17
|
-
: (Raiton.thread?.builder?.options?.
|
|
18
|
+
: (Raiton.thread?.builder?.options?.serve
|
|
18
19
|
? (Array.isArray(metadata.errorStack) ? metadata.errorStack : [])
|
|
19
20
|
: undefined)
|
|
20
21
|
}
|
|
@@ -5,7 +5,7 @@ import {Raiton} from "../../core";
|
|
|
5
5
|
|
|
6
6
|
export class ThrowableResponse extends Error {
|
|
7
7
|
constructor(
|
|
8
|
-
public input: string | HttpResponseInterface
|
|
8
|
+
public input: string | HttpResponseInterface<any>,
|
|
9
9
|
public statusCode: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR,
|
|
10
10
|
) {
|
|
11
11
|
super(typeof input === 'string' ? input : input.message);
|
|
@@ -19,7 +19,7 @@ export class ThrowableResponse extends Error {
|
|
|
19
19
|
data: (typeof this.input === 'object') ? this.input.data : undefined,
|
|
20
20
|
error: (typeof this.input === 'object') ? this.input.error : false,
|
|
21
21
|
stack: stack instanceof Error ?
|
|
22
|
-
(Raiton.thread?.builder?.options?.
|
|
22
|
+
(Raiton.thread?.builder?.options?.serve
|
|
23
23
|
? this.stack?.split('\n').map(e => e.trim())
|
|
24
24
|
: undefined)
|
|
25
25
|
: stack
|
|
@@ -4,7 +4,7 @@ import {ThrowableResponse} from ".";
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
export class HttpResponse {
|
|
7
|
-
public static push(response: Partial<HttpResponseInterface
|
|
7
|
+
public static push(response: Partial<HttpResponseInterface<any>>): void {
|
|
8
8
|
throw new ThrowableResponse({
|
|
9
9
|
statusCode: response.statusCode || HttpStatus.BAD_REQUEST,
|
|
10
10
|
message: response.message || 'No response message provided',
|
|
@@ -14,7 +14,7 @@ export class HttpResponse {
|
|
|
14
14
|
})
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
constructor(public readonly response: HttpResponseInterface) {
|
|
17
|
+
constructor(public readonly response: HttpResponseInterface<any>) {
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
status(statusCode: HttpStatus): this {
|
|
@@ -42,7 +42,7 @@ export class HttpResponse {
|
|
|
42
42
|
return this;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
render(): HttpResponseInterface {
|
|
45
|
+
render(): HttpResponseInterface<any> {
|
|
46
46
|
return (new ThrowableResponse({...this.response})).render()
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {RuntimeAdapterInterface} from '../../../types'
|
|
2
|
+
import {Logger} from "@protorians/logger";
|
|
2
3
|
|
|
3
4
|
export const bunRuntime: RuntimeAdapterInterface = {
|
|
4
5
|
createServer(handler) {
|
|
@@ -23,7 +24,7 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
23
24
|
method: request.method,
|
|
24
25
|
url: request.url,
|
|
25
26
|
headers: request.headers as any,
|
|
26
|
-
body: request.body
|
|
27
|
+
body: request.body ? request.body : null
|
|
27
28
|
},
|
|
28
29
|
{
|
|
29
30
|
status(code) {
|
|
@@ -49,6 +50,9 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
49
50
|
headers.set('content-type', 'application/json')
|
|
50
51
|
responseBody = JSON.stringify(json)
|
|
51
52
|
},
|
|
53
|
+
type(contentType: string) {
|
|
54
|
+
headers.set('content-type', contentType)
|
|
55
|
+
}
|
|
52
56
|
}
|
|
53
57
|
)
|
|
54
58
|
|
|
@@ -70,4 +74,4 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
70
74
|
}
|
|
71
75
|
}
|
|
72
76
|
}
|
|
73
|
-
}
|
|
77
|
+
}
|
|
@@ -1,53 +1,104 @@
|
|
|
1
|
-
import {RuntimeAdapterInterface} from '../../../types'
|
|
1
|
+
import {RuntimeAdapterInterface, RuntimeRequestInterface, RuntimeReplyInterface} from '../../../types'
|
|
2
|
+
import {Logger} from "@protorians/logger";
|
|
2
3
|
|
|
4
|
+
// class DenoRequest implements RuntimeRequestInterface {
|
|
5
|
+
// constructor(private req: Request) {}
|
|
6
|
+
// get method(): string { return this.req.method; }
|
|
7
|
+
// get url(): string { return this.req.url; }
|
|
8
|
+
// get headers(): Headers { return new Headers(this.req.headers); }
|
|
9
|
+
// get body(): any { return this.req.body; }
|
|
10
|
+
// get query(): Record<string, any> | undefined { return undefined; }
|
|
11
|
+
// get params(): Record<string, any> | undefined { return undefined; }
|
|
12
|
+
// get file(): any { return undefined; }
|
|
13
|
+
// get files(): any { return undefined; }
|
|
14
|
+
// get remoteAddress(): string | undefined { return undefined; }
|
|
15
|
+
// }
|
|
16
|
+
|
|
17
|
+
// class DenoReply implements RuntimeReplyInterface {
|
|
18
|
+
// private statusCode: number = 200;
|
|
19
|
+
// private headers: Headers = new Headers();
|
|
20
|
+
// private body: BodyInit | null = null;
|
|
21
|
+
//
|
|
22
|
+
// status(code: number): void { this.statusCode = code; }
|
|
23
|
+
// header(name: string, value: string): void { this.headers.set(name, value); }
|
|
24
|
+
// send(body: any): void {
|
|
25
|
+
// if (body === undefined) {
|
|
26
|
+
// this.body = null;
|
|
27
|
+
// } else if (typeof body === 'string' || body instanceof Uint8Array) {
|
|
28
|
+
// this.body = body;
|
|
29
|
+
// } else {
|
|
30
|
+
// if (!this.headers.has('content-type')) {
|
|
31
|
+
// this.headers.set('content-type', 'application/json');
|
|
32
|
+
// }
|
|
33
|
+
// this.body = JSON.stringify(body);
|
|
34
|
+
// }
|
|
35
|
+
// }
|
|
36
|
+
// text(text: string | Buffer): void {
|
|
37
|
+
// // Accept string, Uint8Array, or any other value (including Buffer-like)
|
|
38
|
+
// if (typeof text === 'string' || text instanceof Uint8Array) {
|
|
39
|
+
// this.body = text;
|
|
40
|
+
// } else {
|
|
41
|
+
// // Fallback: treat as JSON string
|
|
42
|
+
// this.body = JSON.stringify(text);
|
|
43
|
+
// }
|
|
44
|
+
// }
|
|
45
|
+
// json(json: any): void {
|
|
46
|
+
// this.headers.set('content-type', 'application/json');
|
|
47
|
+
// this.body = JSON.stringify(json);
|
|
48
|
+
// }
|
|
49
|
+
// type(contentType: string): void {
|
|
50
|
+
// this.headers.set('content-type', contentType);
|
|
51
|
+
// }
|
|
52
|
+
// toResponse(): Response {
|
|
53
|
+
// let bodyToSend: BodyInit = '';
|
|
54
|
+
// if (this.body instanceof Uint8Array) {
|
|
55
|
+
// bodyToSend = this.body;
|
|
56
|
+
// } else if (typeof this.body === 'string') {
|
|
57
|
+
// bodyToSend = this.body;
|
|
58
|
+
// } else if (this.body === null) {
|
|
59
|
+
// bodyToSend = '';
|
|
60
|
+
// } else {
|
|
61
|
+
// // fallback: treat as JSON
|
|
62
|
+
// bodyToSend = JSON.stringify(this.body);
|
|
63
|
+
// }
|
|
64
|
+
// return new Response(bodyToSend, { status: this.statusCode, headers: this.headers });
|
|
65
|
+
// }
|
|
66
|
+
// }
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @alpha Not ready
|
|
70
|
+
*/
|
|
3
71
|
export const denoRuntime: RuntimeAdapterInterface = {
|
|
4
72
|
createServer(handler) {
|
|
5
|
-
let controller: AbortController
|
|
73
|
+
let controller: AbortController | null = null;
|
|
6
74
|
|
|
7
75
|
return {
|
|
8
76
|
async listen(port, hostname) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
status(code) {
|
|
28
|
-
status = code
|
|
29
|
-
},
|
|
30
|
-
header(name, value) {
|
|
31
|
-
headers.set(name, value)
|
|
32
|
-
},
|
|
33
|
-
send(value: any) {
|
|
34
|
-
body = value
|
|
35
|
-
},
|
|
36
|
-
text(text: string | Buffer) {
|
|
37
|
-
body = text
|
|
38
|
-
},
|
|
39
|
-
json(json: any) {
|
|
40
|
-
body = JSON.stringify(json)
|
|
41
|
-
},
|
|
42
|
-
}
|
|
43
|
-
)
|
|
77
|
+
// controller = new AbortController();
|
|
78
|
+
//
|
|
79
|
+
// const server = Deno.serve({
|
|
80
|
+
// port: Number(port),
|
|
81
|
+
// hostname,
|
|
82
|
+
// signal: controller.signal,
|
|
83
|
+
// handler: async (reqEvent) => {
|
|
84
|
+
// const denoReq = new DenoRequest(reqEvent.request);
|
|
85
|
+
// const denoRes = new DenoReply();
|
|
86
|
+
//
|
|
87
|
+
// await handler(denoReq, denoRes);
|
|
88
|
+
//
|
|
89
|
+
// const resp = denoRes.toResponse();
|
|
90
|
+
// await reqEvent.respondWith(resp);
|
|
91
|
+
// }
|
|
92
|
+
// });
|
|
44
93
|
|
|
45
|
-
|
|
46
|
-
})
|
|
94
|
+
// Server will run until close() is called
|
|
47
95
|
},
|
|
48
96
|
async close() {
|
|
49
|
-
controller
|
|
97
|
+
// if (controller) {
|
|
98
|
+
// controller.abort();
|
|
99
|
+
// }
|
|
100
|
+
// Note: Deno.serve does not expose a direct stop; closing the signal aborts incoming connections.
|
|
50
101
|
}
|
|
51
|
-
}
|
|
102
|
+
};
|
|
52
103
|
}
|
|
53
|
-
}
|
|
104
|
+
}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
RuntimeAdapterInterface,
|
|
3
|
+
RuntimeHandlerCallable,
|
|
4
|
+
RuntimeInterface,
|
|
5
|
+
RuntimeServerInterface
|
|
6
|
+
} from "../../types";
|
|
2
7
|
import {RuntimeType} from "../enums/runtime.enum";
|
|
3
8
|
import {nodeRuntime} from "./node/server";
|
|
4
9
|
import {bunRuntime} from "./bun/server";
|
|
5
10
|
import {denoRuntime} from "./deno/server";
|
|
6
|
-
import {webRuntime} from "./web/server";
|
|
11
|
+
// import {webRuntime} from "./web/server";
|
|
7
12
|
|
|
8
13
|
|
|
9
14
|
export class Runtime implements RuntimeInterface {
|
|
@@ -16,13 +21,13 @@ export class Runtime implements RuntimeInterface {
|
|
|
16
21
|
return this.type === RuntimeType.Node;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
|
-
get isDeno(): boolean {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get isWeb(): boolean {
|
|
24
|
-
|
|
25
|
-
}
|
|
24
|
+
// get isDeno(): boolean {
|
|
25
|
+
// return this.type === RuntimeType.Deno;
|
|
26
|
+
// }
|
|
27
|
+
//
|
|
28
|
+
// get isWeb(): boolean {
|
|
29
|
+
// return this.type === RuntimeType.Web;
|
|
30
|
+
// }
|
|
26
31
|
|
|
27
32
|
get isBun(): boolean {
|
|
28
33
|
return this.type === RuntimeType.Bun;
|
|
@@ -34,10 +39,10 @@ export class Runtime implements RuntimeInterface {
|
|
|
34
39
|
return nodeRuntime
|
|
35
40
|
case RuntimeType.Bun:
|
|
36
41
|
return bunRuntime
|
|
37
|
-
case RuntimeType.Deno:
|
|
38
|
-
|
|
39
|
-
case RuntimeType.Web:
|
|
40
|
-
|
|
42
|
+
// case RuntimeType.Deno:
|
|
43
|
+
// return denoRuntime
|
|
44
|
+
// case RuntimeType.Web:
|
|
45
|
+
// return webRuntime
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
|