nyte 1.0.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/LICENSE +13 -0
- package/README.md +59 -0
- package/dist/adapters/express.d.ts +7 -0
- package/dist/adapters/express.js +63 -0
- package/dist/adapters/factory.d.ts +23 -0
- package/dist/adapters/factory.js +121 -0
- package/dist/adapters/fastify.d.ts +25 -0
- package/dist/adapters/fastify.js +61 -0
- package/dist/adapters/native.d.ts +8 -0
- package/dist/adapters/native.js +200 -0
- package/dist/api/console.d.ts +81 -0
- package/dist/api/console.js +318 -0
- package/dist/api/http.d.ts +180 -0
- package/dist/api/http.js +469 -0
- package/dist/bin/nytejs.d.ts +2 -0
- package/dist/bin/nytejs.js +277 -0
- package/dist/builder.d.ts +32 -0
- package/dist/builder.js +634 -0
- package/dist/client/DefaultNotFound.d.ts +1 -0
- package/dist/client/DefaultNotFound.js +79 -0
- package/dist/client/client.d.ts +4 -0
- package/dist/client/client.js +27 -0
- package/dist/client/clientRouter.d.ts +58 -0
- package/dist/client/clientRouter.js +132 -0
- package/dist/client/entry.client.d.ts +1 -0
- package/dist/client/entry.client.js +455 -0
- package/dist/client/rpc.d.ts +8 -0
- package/dist/client/rpc.js +97 -0
- package/dist/components/Link.d.ts +7 -0
- package/dist/components/Link.js +13 -0
- package/dist/global/global.d.ts +117 -0
- package/dist/global/global.js +17 -0
- package/dist/helpers.d.ts +20 -0
- package/dist/helpers.js +604 -0
- package/dist/hotReload.d.ts +32 -0
- package/dist/hotReload.js +545 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +515 -0
- package/dist/loaders.d.ts +1 -0
- package/dist/loaders.js +138 -0
- package/dist/renderer.d.ts +14 -0
- package/dist/renderer.js +380 -0
- package/dist/router.d.ts +101 -0
- package/dist/router.js +659 -0
- package/dist/rpc/server.d.ts +11 -0
- package/dist/rpc/server.js +166 -0
- package/dist/rpc/types.d.ts +22 -0
- package/dist/rpc/types.js +20 -0
- package/dist/types/framework.d.ts +37 -0
- package/dist/types/framework.js +2 -0
- package/dist/types.d.ts +218 -0
- package/dist/types.js +2 -0
- package/package.json +87 -0
- package/src/adapters/express.ts +87 -0
- package/src/adapters/factory.ts +112 -0
- package/src/adapters/fastify.ts +104 -0
- package/src/adapters/native.ts +245 -0
- package/src/api/console.ts +348 -0
- package/src/api/http.ts +535 -0
- package/src/bin/nytejs.js +331 -0
- package/src/builder.js +690 -0
- package/src/client/DefaultNotFound.tsx +119 -0
- package/src/client/client.ts +24 -0
- package/src/client/clientRouter.ts +153 -0
- package/src/client/entry.client.tsx +529 -0
- package/src/client/rpc.ts +101 -0
- package/src/components/Link.tsx +38 -0
- package/src/global/global.ts +171 -0
- package/src/helpers.ts +657 -0
- package/src/hotReload.ts +566 -0
- package/src/index.ts +582 -0
- package/src/loaders.js +160 -0
- package/src/renderer.tsx +421 -0
- package/src/router.ts +732 -0
- package/src/rpc/server.ts +190 -0
- package/src/rpc/types.ts +45 -0
- package/src/types/framework.ts +58 -0
- package/src/types.ts +288 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the Nyte.js Project.
|
|
3
|
+
* Copyright (c) 2026 itsmuzin
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import fs from 'fs';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
import { RpcRequestPayload, RpcResponsePayload } from './types';
|
|
21
|
+
import { NyteRequest } from '../api/http';
|
|
22
|
+
import type { GenericRequest } from '../types/framework';
|
|
23
|
+
|
|
24
|
+
const DEFAULT_ALLOWED_SERVER_DIRS = ['src/backend'] as const;
|
|
25
|
+
|
|
26
|
+
function normalizeToPosix(p: string): string {
|
|
27
|
+
return p.replace(/\\/g, '/');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isDisallowedPathInput(file: string): boolean {
|
|
31
|
+
if (!file) return true;
|
|
32
|
+
if (file.includes('\u0000')) return true;
|
|
33
|
+
// disallow protocol-like or absolute paths (Windows drive letters included)
|
|
34
|
+
if (/^[a-zA-Z]+:/.test(file)) return true;
|
|
35
|
+
if (file.startsWith('/') || file.startsWith('\\')) return true;
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function validatePayload(payload: any): payload is RpcRequestPayload {
|
|
40
|
+
return (
|
|
41
|
+
payload &&
|
|
42
|
+
typeof payload === 'object' &&
|
|
43
|
+
typeof payload.file === 'string' &&
|
|
44
|
+
typeof payload.fn === 'string' &&
|
|
45
|
+
Array.isArray(payload.args)
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RpcExecutionContext {
|
|
50
|
+
/** absolute project root (same as options.dir in start()) */
|
|
51
|
+
projectDir: string;
|
|
52
|
+
/** allow override for tests */
|
|
53
|
+
allowedServerDirs?: string[];
|
|
54
|
+
/** real incoming request (so cookies/headers/session work) */
|
|
55
|
+
request?: GenericRequest;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function tryResolveWithinAllowedDirs(projectDir: string, allowedDirs: string[], requestedFile: string): string | null {
|
|
59
|
+
const req = requestedFile.replace(/\\/g, '/').replace(/^\.(?:\/|\\)/, '');
|
|
60
|
+
|
|
61
|
+
for (const d of allowedDirs) {
|
|
62
|
+
const baseAbs = path.resolve(projectDir, d);
|
|
63
|
+
|
|
64
|
+
// Interpret client path as relative to src/web (where it's typically authored)
|
|
65
|
+
const fromWebAbs = path.resolve(projectDir, 'src/web', req);
|
|
66
|
+
|
|
67
|
+
// Map: <project>/src/backend/* (coming from ../../backend/* from web code)
|
|
68
|
+
const mappedFromWebAbs = fromWebAbs.replace(
|
|
69
|
+
path.resolve(projectDir, 'backend') + path.sep,
|
|
70
|
+
path.resolve(projectDir, 'src', 'backend') + path.sep
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// Also accept callers passing a backend-relative path like "./auth" or "auth"
|
|
74
|
+
const fromBackendAbs = path.resolve(baseAbs, req);
|
|
75
|
+
|
|
76
|
+
const candidateAbsList = [mappedFromWebAbs, fromBackendAbs];
|
|
77
|
+
|
|
78
|
+
for (const candidateAbs of candidateAbsList) {
|
|
79
|
+
const baseWithSep = baseAbs.endsWith(path.sep) ? baseAbs : baseAbs + path.sep;
|
|
80
|
+
if (!candidateAbs.startsWith(baseWithSep) && candidateAbs !== baseAbs) continue;
|
|
81
|
+
if (!fs.existsSync(baseAbs)) continue;
|
|
82
|
+
return candidateAbs;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function toCookies(cookieHeader: string | undefined): Record<string, string> {
|
|
90
|
+
if (!cookieHeader) return {};
|
|
91
|
+
const out: Record<string, string> = {};
|
|
92
|
+
for (const part of cookieHeader.split(';')) {
|
|
93
|
+
const idx = part.indexOf('=');
|
|
94
|
+
if (idx === -1) continue;
|
|
95
|
+
const k = part.slice(0, idx).trim();
|
|
96
|
+
const v = part.slice(idx + 1).trim();
|
|
97
|
+
if (k) out[k] = v;
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Keep this for fallback when ctx.request isn't provided
|
|
103
|
+
function buildRpcRequestFromPayload(payload: RpcRequestPayload): NyteRequest {
|
|
104
|
+
const headers: Record<string, string | string[]> = {};
|
|
105
|
+
const rawHeaders = payload.request?.headers || {};
|
|
106
|
+
for (const [k, v] of Object.entries(rawHeaders)) {
|
|
107
|
+
headers[k.toLowerCase()] = v;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const cookieHeader = (headers['cookie'] as string | undefined) ?? undefined;
|
|
111
|
+
|
|
112
|
+
const req: GenericRequest = {
|
|
113
|
+
method: payload.request?.method || 'RPC',
|
|
114
|
+
url: payload.request?.url || 'http://localhost/_rpc',
|
|
115
|
+
headers,
|
|
116
|
+
body: null,
|
|
117
|
+
query: {},
|
|
118
|
+
params: {},
|
|
119
|
+
cookies: payload.request?.cookies || toCookies(cookieHeader),
|
|
120
|
+
raw: null
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return new NyteRequest(req);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function executeRpc(ctx: RpcExecutionContext, payload: any): Promise<RpcResponsePayload> {
|
|
127
|
+
try {
|
|
128
|
+
if (!validatePayload(payload)) {
|
|
129
|
+
return { success: false, error: 'Invalid RPC payload' };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const fileInput = payload.file;
|
|
133
|
+
if (isDisallowedPathInput(fileInput)) {
|
|
134
|
+
return { success: false, error: 'Invalid file path' };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const allowedDirs = (ctx.allowedServerDirs || [...DEFAULT_ALLOWED_SERVER_DIRS]).map(normalizeToPosix);
|
|
138
|
+
const absFile = tryResolveWithinAllowedDirs(ctx.projectDir, allowedDirs, fileInput);
|
|
139
|
+
if (!absFile) {
|
|
140
|
+
return { success: false, error: 'File not allowed for RPC' };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!absFile.startsWith(path.resolve(ctx.projectDir) + path.sep)) {
|
|
144
|
+
return { success: false, error: 'Invalid file path' };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Ensure fresh code in dev
|
|
148
|
+
try {
|
|
149
|
+
const resolved = require.resolve(absFile);
|
|
150
|
+
if (require.cache[resolved]) delete require.cache[resolved];
|
|
151
|
+
} catch {
|
|
152
|
+
// ignore
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let mod: any;
|
|
156
|
+
try {
|
|
157
|
+
mod = require(absFile);
|
|
158
|
+
} catch {
|
|
159
|
+
// try again letting require do extension resolution from absFile without explicit extension
|
|
160
|
+
try {
|
|
161
|
+
mod = require(absFile);
|
|
162
|
+
} catch {
|
|
163
|
+
return { success: false, error: 'RPC file not found' };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Support multiple TS/CJS interop shapes:
|
|
168
|
+
// - module.exports.fn
|
|
169
|
+
// - exports.fn
|
|
170
|
+
// - module.exports.default (function)
|
|
171
|
+
// - module.exports.default.fn
|
|
172
|
+
const fnName = payload.fn;
|
|
173
|
+
const fnValue =
|
|
174
|
+
(mod && typeof mod[fnName] === 'function' && mod[fnName]) ||
|
|
175
|
+
(mod?.default && typeof mod.default === 'function' && fnName === 'default' && mod.default) ||
|
|
176
|
+
(mod?.default && typeof mod.default[fnName] === 'function' && mod.default[fnName]) ||
|
|
177
|
+
undefined;
|
|
178
|
+
|
|
179
|
+
if (typeof fnValue !== 'function') {
|
|
180
|
+
return { success: false, error: `RPC function not found: ${fnName}` };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const rpcRequest = ctx.request ? new NyteRequest(ctx.request) : buildRpcRequestFromPayload(payload);
|
|
184
|
+
const result = await fnValue(rpcRequest, ...payload.args);
|
|
185
|
+
return { success: true, return: result };
|
|
186
|
+
} catch (err: any) {
|
|
187
|
+
const message = typeof err?.message === 'string' ? err.message : 'Unknown RPC error';
|
|
188
|
+
return { success: false, error: message };
|
|
189
|
+
}
|
|
190
|
+
}
|
package/src/rpc/types.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the Nyte.js Project.
|
|
3
|
+
* Copyright (c) 2026 itsmuzin
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export const RPC_ENDPOINT = '/api/rpc' as const;
|
|
19
|
+
|
|
20
|
+
export type RpcArgs = unknown[];
|
|
21
|
+
|
|
22
|
+
export interface RpcRequestPayload {
|
|
23
|
+
file: string;
|
|
24
|
+
fn: string;
|
|
25
|
+
args: RpcArgs;
|
|
26
|
+
|
|
27
|
+
request?: {
|
|
28
|
+
url?: string;
|
|
29
|
+
method?: string;
|
|
30
|
+
headers?: Record<string, string>;
|
|
31
|
+
cookies?: Record<string, string>;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type RpcSuccessResponse = {
|
|
36
|
+
success: true;
|
|
37
|
+
return: unknown;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type RpcErrorResponse = {
|
|
41
|
+
success: false;
|
|
42
|
+
error: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type RpcResponsePayload = RpcSuccessResponse | RpcErrorResponse;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the Nyte.js Project.
|
|
3
|
+
* Copyright (c) 2026 itsmuzin
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
// Tipos genéricos para abstrair diferentes frameworks web
|
|
18
|
+
export interface GenericRequest {
|
|
19
|
+
method: string;
|
|
20
|
+
url: string;
|
|
21
|
+
headers: Record<string, string | string[]>;
|
|
22
|
+
body?: any;
|
|
23
|
+
query?: Record<string, any>;
|
|
24
|
+
params?: Record<string, string>;
|
|
25
|
+
cookies?: Record<string, string>;
|
|
26
|
+
raw?: any; // Requisição original do framework
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GenericResponse {
|
|
30
|
+
status(code: number): GenericResponse;
|
|
31
|
+
header(name: string, value: string): GenericResponse;
|
|
32
|
+
cookie(name: string, value: string, options?: CookieOptions): GenericResponse;
|
|
33
|
+
clearCookie(name: string, options?: CookieOptions): GenericResponse;
|
|
34
|
+
json(data: any): void;
|
|
35
|
+
text(data: string): void;
|
|
36
|
+
send(data: any): void;
|
|
37
|
+
redirect(url: string): void;
|
|
38
|
+
raw?: any; // Resposta original do framework
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface CookieOptions {
|
|
42
|
+
domain?: string;
|
|
43
|
+
expires?: Date;
|
|
44
|
+
httpOnly?: boolean;
|
|
45
|
+
maxAge?: number;
|
|
46
|
+
path?: string;
|
|
47
|
+
secure?: boolean;
|
|
48
|
+
signed?: boolean;
|
|
49
|
+
sameSite?: boolean | 'lax' | 'strict' | 'none';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type FrameworkType = 'express' | 'fastify' | 'native';
|
|
53
|
+
|
|
54
|
+
export interface FrameworkAdapter {
|
|
55
|
+
type: FrameworkType;
|
|
56
|
+
parseRequest(req: any): GenericRequest;
|
|
57
|
+
createResponse(res: any): GenericResponse;
|
|
58
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the Nyte.js Project.
|
|
3
|
+
* Copyright (c) 2026 itsmuzin
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import type { ComponentType } from 'react';
|
|
18
|
+
import type { GenericRequest } from './types/framework';
|
|
19
|
+
import {NyteRequest, NyteResponse} from "./api/http";
|
|
20
|
+
import { WebSocket } from 'ws';
|
|
21
|
+
import { IncomingMessage } from 'http';
|
|
22
|
+
|
|
23
|
+
// Interface do contexto WebSocket simplificada
|
|
24
|
+
export interface WebSocketContext {
|
|
25
|
+
ws: WebSocket;
|
|
26
|
+
req: IncomingMessage;
|
|
27
|
+
nyteReq: NyteRequest;
|
|
28
|
+
url: URL;
|
|
29
|
+
params: Record<string, string>;
|
|
30
|
+
query: Record<string, string>;
|
|
31
|
+
send: (data: any) => void;
|
|
32
|
+
close: (code?: number, reason?: string) => void;
|
|
33
|
+
broadcast: (data: any, exclude?: WebSocket[]) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// --- Tipos do Frontend (sem alteração) ---
|
|
37
|
+
export interface NyteOptions {
|
|
38
|
+
dev?: boolean;
|
|
39
|
+
hostname?: string;
|
|
40
|
+
port?: number;
|
|
41
|
+
dir?: string;
|
|
42
|
+
framework?: 'express' | 'fastify' | 'native';
|
|
43
|
+
ssl?: {
|
|
44
|
+
redirectPort: number;
|
|
45
|
+
key: string;
|
|
46
|
+
cert: string;
|
|
47
|
+
ca?: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// --- Tipos de Configuração ---
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Interface para as configurações avançadas do servidor NyteJS.
|
|
55
|
+
* Essas configurações podem ser definidas no arquivo nytejs.config.js
|
|
56
|
+
*/
|
|
57
|
+
export interface NyteConfig {
|
|
58
|
+
/**
|
|
59
|
+
* Limita o número máximo de headers HTTP permitidos por requisição.
|
|
60
|
+
* Padrão: 100
|
|
61
|
+
*/
|
|
62
|
+
maxHeadersCount?: number;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Timeout em milissegundos para receber os headers HTTP.
|
|
66
|
+
* Padrão: 60000 (60 segundos)
|
|
67
|
+
*/
|
|
68
|
+
headersTimeout?: number;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Timeout em milissegundos para uma requisição completa.
|
|
72
|
+
* Padrão: 30000 (30 segundos)
|
|
73
|
+
*/
|
|
74
|
+
requestTimeout?: number;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Timeout geral do servidor em milissegundos.
|
|
78
|
+
* Padrão: 35000 (35 segundos)
|
|
79
|
+
*/
|
|
80
|
+
serverTimeout?: number;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Timeout por requisição individual em milissegundos.
|
|
84
|
+
* Padrão: 30000 (30 segundos)
|
|
85
|
+
*/
|
|
86
|
+
individualRequestTimeout?: number;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Tamanho máximo permitido para a URL em caracteres.
|
|
90
|
+
* Padrão: 2048
|
|
91
|
+
*/
|
|
92
|
+
maxUrlLength?: number;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Habilita o log de acesso HTTP (ex: GET /api/users 200 15ms).
|
|
96
|
+
* Padrão: false
|
|
97
|
+
*/
|
|
98
|
+
accessLogging?: boolean;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Configurações de CORS (Cross-Origin Resource Sharing).
|
|
102
|
+
* Define quais origens podem acessar seus recursos.
|
|
103
|
+
*/
|
|
104
|
+
cors?: {
|
|
105
|
+
/**
|
|
106
|
+
* Origens permitidas. Pode ser:
|
|
107
|
+
* - Uma string específica: 'https://exemplo.com'
|
|
108
|
+
* - Um array de strings: ['https://exemplo.com', 'https://outro.com']
|
|
109
|
+
* - Um wildcard: '*' (permite todas as origens - não recomendado em produção)
|
|
110
|
+
* - Uma função que retorna boolean: (origin) => origin.endsWith('.exemplo.com')
|
|
111
|
+
*/
|
|
112
|
+
origin?: string | string[] | ((origin: string) => boolean);
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Métodos HTTP permitidos.
|
|
116
|
+
* Padrão: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']
|
|
117
|
+
*/
|
|
118
|
+
methods?: string[];
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Headers permitidos nas requisições.
|
|
122
|
+
* Padrão: ['Content-Type', 'Authorization']
|
|
123
|
+
*/
|
|
124
|
+
allowedHeaders?: string[];
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Headers que serão expostos ao cliente.
|
|
128
|
+
* Padrão: []
|
|
129
|
+
*/
|
|
130
|
+
exposedHeaders?: string[];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Permite o envio de credenciais (cookies, headers de autenticação).
|
|
134
|
+
* Padrão: false
|
|
135
|
+
*/
|
|
136
|
+
credentials?: boolean;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Tempo em segundos que o navegador deve cachear a resposta preflight.
|
|
140
|
+
* Padrão: 86400 (24 horas)
|
|
141
|
+
*/
|
|
142
|
+
maxAge?: number;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Habilita ou desabilita completamente o CORS.
|
|
146
|
+
* Padrão: false
|
|
147
|
+
*/
|
|
148
|
+
enabled?: boolean;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Configurações de segurança de headers HTTP.
|
|
153
|
+
*/
|
|
154
|
+
security?: {
|
|
155
|
+
/**
|
|
156
|
+
* Content-Security-Policy: Define de onde o navegador pode carregar recursos.
|
|
157
|
+
* Exemplo: "default-src 'self'; script-src 'self' 'unsafe-inline'"
|
|
158
|
+
*/
|
|
159
|
+
contentSecurityPolicy?: string;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Permissions-Policy: Controla quais recursos e APIs o navegador pode usar.
|
|
163
|
+
* Exemplo: "geolocation=(), microphone=()"
|
|
164
|
+
*/
|
|
165
|
+
permissionsPolicy?: string;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Strict-Transport-Security: Força o uso de HTTPS.
|
|
169
|
+
* Padrão (quando SSL ativo): "max-age=31536000; includeSubDomains"
|
|
170
|
+
* Exemplo: "max-age=63072000; includeSubDomains; preload"
|
|
171
|
+
*/
|
|
172
|
+
strictTransportSecurity?: string;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Headers HTTP personalizados que serão adicionados a todas as respostas.
|
|
177
|
+
* Exemplo: { 'X-Custom-Header': 'value', 'X-Powered-By': 'Nyte.js' }
|
|
178
|
+
*/
|
|
179
|
+
customHeaders?: Record<string, string>;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Tipo da função de configuração que pode ser exportada no nytejs.config.js
|
|
184
|
+
*/
|
|
185
|
+
export type NyteConfigFunction = (
|
|
186
|
+
phase: string,
|
|
187
|
+
context: { defaultConfig: NyteConfig }
|
|
188
|
+
) => NyteConfig | Promise<NyteConfig>;
|
|
189
|
+
|
|
190
|
+
export interface Metadata {
|
|
191
|
+
// Basic metadata
|
|
192
|
+
title?: string;
|
|
193
|
+
description?: string;
|
|
194
|
+
keywords?: string | string[];
|
|
195
|
+
author?: string;
|
|
196
|
+
favicon?: string;
|
|
197
|
+
|
|
198
|
+
// Viewport and mobile
|
|
199
|
+
viewport?: string;
|
|
200
|
+
themeColor?: string;
|
|
201
|
+
|
|
202
|
+
// SEO
|
|
203
|
+
canonical?: string;
|
|
204
|
+
robots?: string;
|
|
205
|
+
|
|
206
|
+
// Open Graph (Facebook, LinkedIn, etc.)
|
|
207
|
+
openGraph?: {
|
|
208
|
+
title?: string;
|
|
209
|
+
description?: string;
|
|
210
|
+
type?: string;
|
|
211
|
+
url?: string;
|
|
212
|
+
image?: string | {
|
|
213
|
+
url: string;
|
|
214
|
+
width?: number;
|
|
215
|
+
height?: number;
|
|
216
|
+
alt?: string;
|
|
217
|
+
};
|
|
218
|
+
siteName?: string;
|
|
219
|
+
locale?: string;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// Twitter Card
|
|
223
|
+
twitter?: {
|
|
224
|
+
card?: 'summary' | 'summary_large_image' | 'app' | 'player';
|
|
225
|
+
site?: string;
|
|
226
|
+
creator?: string;
|
|
227
|
+
title?: string;
|
|
228
|
+
description?: string;
|
|
229
|
+
image?: string;
|
|
230
|
+
imageAlt?: string;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// Additional metadata
|
|
234
|
+
language?: string;
|
|
235
|
+
charset?: string;
|
|
236
|
+
appleTouchIcon?: string;
|
|
237
|
+
manifest?: string;
|
|
238
|
+
|
|
239
|
+
// Custom meta tags
|
|
240
|
+
other?: Record<string, string>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
export interface RouteConfig {
|
|
245
|
+
pattern: string;
|
|
246
|
+
component: ComponentType<any>;
|
|
247
|
+
generateMetadata?: (params: any, req: GenericRequest) => Promise<Metadata> | Metadata;
|
|
248
|
+
}
|
|
249
|
+
export type RequestHandler = (req: any, res: any) => Promise<void>;
|
|
250
|
+
|
|
251
|
+
// --- NOVO: Tipos do Backend ---
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Define o formato de uma função que manipula uma rota da API.
|
|
255
|
+
*/
|
|
256
|
+
export type BackendHandler = (
|
|
257
|
+
request: NyteRequest, // HWebRequest será importado onde necessário
|
|
258
|
+
params: { [key: string]: string }
|
|
259
|
+
) => Promise<NyteResponse> | NyteResponse; // HWebResponse será importado onde necessário
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
export type NyteMiddleware = (
|
|
264
|
+
request: NyteRequest, // HWebRequest será importado onde necessário
|
|
265
|
+
params: { [key: string]: string },
|
|
266
|
+
next: () => Promise<NyteResponse>
|
|
267
|
+
) => Promise<NyteResponse> | NyteResponse; // HWebResponse será importado onde necessário
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Define o formato de uma função que manipula uma rota WebSocket.
|
|
272
|
+
*/
|
|
273
|
+
export type WebSocketHandler = (
|
|
274
|
+
context: WebSocketContext
|
|
275
|
+
) => Promise<void> | void;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Define a estrutura de cada rota da API, com suporte para métodos HTTP e WebSocket.
|
|
279
|
+
*/
|
|
280
|
+
export interface BackendRouteConfig {
|
|
281
|
+
pattern: string;
|
|
282
|
+
GET?: BackendHandler;
|
|
283
|
+
POST?: BackendHandler;
|
|
284
|
+
PUT?: BackendHandler;
|
|
285
|
+
DELETE?: BackendHandler;
|
|
286
|
+
WS?: WebSocketHandler; // Suporte para WebSocket
|
|
287
|
+
middleware?: NyteMiddleware[]; // Permite adicionar middlewares específicos à rota
|
|
288
|
+
}
|