burger-api 0.9.6 → 1.0.0-beta
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 +84 -241
- package/dist/src/adapter/bun/index.d.ts +14 -0
- package/dist/src/adapter/bun/index.js +73 -0
- package/dist/src/adapter/bun/types.d.ts +15 -0
- package/dist/src/adapter/bun/types.js +1 -0
- package/dist/src/adapter/index.d.ts +1 -0
- package/dist/src/adapter/index.js +1 -0
- package/dist/src/adapter/types.d.ts +49 -0
- package/dist/src/adapter/types.js +1 -0
- package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
- package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
- package/dist/src/adapter/web-standard/index.d.ts +8 -0
- package/dist/src/adapter/web-standard/index.js +7 -0
- package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
- package/dist/src/analysis/route-access-analyzer.js +287 -0
- package/dist/src/chain/chain.d.ts +16 -0
- package/dist/src/chain/chain.js +17 -0
- package/dist/src/chain/flattener.d.ts +3 -0
- package/dist/src/chain/flattener.js +78 -0
- package/dist/src/chain/node.d.ts +25 -0
- package/dist/src/chain/node.js +1 -0
- package/dist/src/compiler/conventions.d.ts +67 -0
- package/dist/src/compiler/conventions.js +129 -0
- package/dist/src/compiler/index.d.ts +13 -0
- package/dist/src/compiler/index.js +11 -0
- package/dist/src/compiler/module-loader.d.ts +75 -0
- package/dist/src/compiler/module-loader.js +276 -0
- package/dist/src/compiler/route-module.d.ts +97 -0
- package/dist/src/compiler/route-module.js +1 -0
- package/dist/src/compiler/route-tree.d.ts +19 -0
- package/dist/src/compiler/route-tree.js +28 -0
- package/dist/src/compiler/scanner.d.ts +42 -0
- package/dist/src/compiler/scanner.js +201 -0
- package/dist/src/context/context-set.d.ts +26 -0
- package/dist/src/context/context-set.js +45 -0
- package/dist/src/context/context.d.ts +357 -0
- package/dist/src/context/context.js +401 -0
- package/dist/src/context/query-parser.d.ts +23 -0
- package/dist/src/context/query-parser.js +132 -0
- package/dist/src/context/route-access.d.ts +7 -0
- package/dist/src/context/route-access.js +18 -0
- package/dist/src/context/types.d.ts +62 -0
- package/dist/src/context/types.js +5 -0
- package/dist/src/core/asset-mime.d.ts +9 -0
- package/dist/src/core/asset-mime.js +35 -0
- package/dist/src/core/assets.d.ts +30 -0
- package/dist/src/core/assets.js +67 -0
- package/dist/src/core/docs-providers.d.ts +13 -0
- package/dist/src/core/docs-providers.js +89 -0
- package/dist/src/core/embedded-assets.d.ts +18 -0
- package/dist/src/core/embedded-assets.js +13 -0
- package/dist/src/core/openapi.d.ts +9 -10
- package/dist/src/core/openapi.js +333 -127
- package/dist/src/core/page-router.d.ts +13 -17
- package/dist/src/core/page-router.js +86 -55
- package/dist/src/core/server.d.ts +20 -11
- package/dist/src/core/server.js +41 -45
- package/dist/src/errors/forbidden.d.ts +14 -0
- package/dist/src/errors/forbidden.js +16 -0
- package/dist/src/errors/http-error.d.ts +47 -0
- package/dist/src/errors/http-error.js +146 -0
- package/dist/src/errors/method-not-allowed.d.ts +13 -0
- package/dist/src/errors/method-not-allowed.js +15 -0
- package/dist/src/errors/not-found.d.ts +14 -0
- package/dist/src/errors/not-found.js +16 -0
- package/dist/src/errors/unauthorized.d.ts +14 -0
- package/dist/src/errors/unauthorized.js +16 -0
- package/dist/src/index.d.ts +263 -50
- package/dist/src/index.js +919 -332
- package/dist/src/lifecycle/executor.d.ts +64 -0
- package/dist/src/lifecycle/executor.js +201 -0
- package/dist/src/lifecycle/hook-runner.d.ts +11 -0
- package/dist/src/lifecycle/hook-runner.js +156 -0
- package/dist/src/lifecycle/jit.d.ts +50 -0
- package/dist/src/lifecycle/jit.js +258 -0
- package/dist/src/lifecycle/transform.d.ts +26 -0
- package/dist/src/lifecycle/transform.js +99 -0
- package/dist/src/lifecycle/types.d.ts +135 -0
- package/dist/src/lifecycle/types.js +1 -0
- package/dist/src/plugin/composer.d.ts +3 -0
- package/dist/src/plugin/composer.js +28 -0
- package/dist/src/plugin/registry.d.ts +25 -0
- package/dist/src/plugin/registry.js +107 -0
- package/dist/src/plugin/types.d.ts +20 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/router/allow-cache.d.ts +20 -0
- package/dist/src/router/allow-cache.js +26 -0
- package/dist/src/router/compiler.d.ts +39 -0
- package/dist/src/router/compiler.js +817 -0
- package/dist/src/router/define.d.ts +67 -0
- package/dist/src/router/define.js +34 -0
- package/dist/src/router/index.d.ts +6 -0
- package/dist/src/router/index.js +5 -0
- package/dist/src/router/param-extract.d.ts +33 -0
- package/dist/src/router/param-extract.js +87 -0
- package/dist/src/router/regex-matcher.d.ts +42 -0
- package/dist/src/router/regex-matcher.js +216 -0
- package/dist/src/router/router.d.ts +201 -0
- package/dist/src/router/router.js +600 -0
- package/dist/src/router/static-map.d.ts +39 -0
- package/dist/src/router/static-map.js +48 -0
- package/dist/src/router/trie.d.ts +69 -0
- package/dist/src/router/trie.js +572 -0
- package/dist/src/router/types.d.ts +104 -0
- package/dist/src/router/types.js +1 -0
- package/dist/src/runtime/capabilities.d.ts +29 -0
- package/dist/src/runtime/capabilities.js +48 -0
- package/dist/src/types/index.d.ts +260 -109
- package/dist/src/types/index.js +1 -1
- package/dist/src/types/inference.d.ts +78 -0
- package/dist/src/types/inference.js +20 -0
- package/dist/src/types/openapi-config.d.ts +164 -0
- package/dist/src/types/openapi-config.js +8 -0
- package/dist/src/utils/env.d.ts +13 -0
- package/dist/src/utils/env.js +24 -0
- package/dist/src/utils/fs.d.ts +29 -0
- package/dist/src/utils/fs.js +75 -0
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +2 -2
- package/dist/src/utils/pathConversion.js +13 -7
- package/dist/src/utils/response.d.ts +42 -6
- package/dist/src/utils/response.js +172 -7
- package/dist/src/utils/routing.d.ts +32 -13
- package/dist/src/utils/routing.js +41 -18
- package/dist/src/utils/thenable.d.ts +8 -0
- package/dist/src/utils/thenable.js +12 -0
- package/dist/src/utils/timing-safe.d.ts +9 -0
- package/dist/src/utils/timing-safe.js +20 -0
- package/dist/src/utils/wildcard.d.ts +2 -9
- package/dist/src/utils/wildcard.js +20 -58
- package/dist/src/validation/adapter.d.ts +54 -0
- package/dist/src/validation/adapter.js +75 -0
- package/dist/src/validation/adapters/standard.d.ts +11 -0
- package/dist/src/validation/adapters/standard.js +110 -0
- package/dist/src/validation/adapters/zod.d.ts +9 -0
- package/dist/src/validation/adapters/zod.js +128 -0
- package/dist/src/validation/cache.d.ts +26 -0
- package/dist/src/validation/cache.js +35 -0
- package/dist/src/validation/coerce.d.ts +28 -0
- package/dist/src/validation/coerce.js +176 -0
- package/dist/src/validation/compiler.d.ts +27 -0
- package/dist/src/validation/compiler.js +191 -0
- package/dist/src/validation/error.d.ts +69 -0
- package/dist/src/validation/error.js +137 -0
- package/dist/src/validation/index.d.ts +16 -0
- package/dist/src/validation/index.js +12 -0
- package/dist/src/validation/response.d.ts +25 -0
- package/dist/src/validation/response.js +79 -0
- package/dist/src/validation/types.d.ts +148 -0
- package/dist/src/validation/types.js +6 -0
- package/dist/src/validation/validator.d.ts +39 -0
- package/dist/src/validation/validator.js +441 -0
- package/dist/src/ws/adapter.d.ts +219 -0
- package/dist/src/ws/adapter.js +623 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +156 -0
- package/dist/src/ws/index.d.ts +11 -0
- package/dist/src/ws/index.js +8 -0
- package/dist/src/ws/platform.d.ts +81 -0
- package/dist/src/ws/platform.js +113 -0
- package/dist/src/ws/router.d.ts +48 -0
- package/dist/src/ws/router.js +174 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +193 -0
- package/dist/src/ws/types.d.ts +380 -0
- package/dist/src/ws/types.js +213 -0
- package/package.json +72 -59
- package/dist/src/core/api-router.d.ts +0 -65
- package/dist/src/core/api-router.js +0 -264
- package/dist/src/core/swagger-ui.d.ts +0 -1
- package/dist/src/core/swagger-ui.js +0 -33
- package/dist/src/middleware/validator.d.ts +0 -14
- package/dist/src/middleware/validator.js +0 -133
- package/dist/src/utils/error.d.ts +0 -17
- package/dist/src/utils/error.js +0 -63
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Holds registered plugins and resolves them (factories are called once).
|
|
3
|
+
*
|
|
4
|
+
* Identity is the resolved `name` (+ optional `seed`). A plugin object is
|
|
5
|
+
* keyed immediately; a factory only after it resolves, since keying by
|
|
6
|
+
* function name would collapse every anonymous arrow factory into one.
|
|
7
|
+
* Duplicate registrations warn instead of being dropped silently.
|
|
8
|
+
*/
|
|
9
|
+
export class PluginRegistry {
|
|
10
|
+
entries = new Map();
|
|
11
|
+
resolved = new Map();
|
|
12
|
+
/** Unique key counter for not-yet-resolved factory entries. */
|
|
13
|
+
factoryCount = 0;
|
|
14
|
+
key(name, seed) {
|
|
15
|
+
return seed !== undefined ? `${name}\u0000${seed}` : name;
|
|
16
|
+
}
|
|
17
|
+
register(plugin, scope = 'plugin', seed) {
|
|
18
|
+
if (typeof plugin === 'function') {
|
|
19
|
+
// The same factory function registered twice (same seed) is the
|
|
20
|
+
// same plugin; different factories dedupe after resolution.
|
|
21
|
+
for (const entry of this.entries.values()) {
|
|
22
|
+
if (entry.plugin === plugin && entry.seed === seed) {
|
|
23
|
+
warnDuplicate(plugin.name || '(anonymous factory)', seed);
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
this.entries.set(`\u0001factory${this.factoryCount++}`, {
|
|
28
|
+
plugin,
|
|
29
|
+
scope,
|
|
30
|
+
seed,
|
|
31
|
+
});
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
const k = this.key(plugin.name, seed);
|
|
35
|
+
if (this.entries.has(k)) {
|
|
36
|
+
warnDuplicate(plugin.name, seed);
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
this.entries.set(k, { plugin, scope, seed });
|
|
40
|
+
// Invalidate cached resolution
|
|
41
|
+
this.resolved.delete(k);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
has(name, seed) {
|
|
45
|
+
const k = this.key(name, seed);
|
|
46
|
+
if (this.entries.has(k))
|
|
47
|
+
return true;
|
|
48
|
+
for (const r of this.resolved.values()) {
|
|
49
|
+
if (this.key(r.name, r.seed) === k)
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
async resolve(name, seed) {
|
|
55
|
+
const all = await this.resolveAll();
|
|
56
|
+
return all.find((p) => p.name === name && p.seed === seed);
|
|
57
|
+
}
|
|
58
|
+
/** Resolves one entry (calling its factory at most once). */
|
|
59
|
+
async resolveEntry(entryKey, entry) {
|
|
60
|
+
const cached = this.resolved.get(entryKey);
|
|
61
|
+
if (cached)
|
|
62
|
+
return cached;
|
|
63
|
+
const raw = typeof entry.plugin === 'function'
|
|
64
|
+
? await entry.plugin()
|
|
65
|
+
: entry.plugin;
|
|
66
|
+
const hooks = raw.hooks ?? {};
|
|
67
|
+
const resolved = {
|
|
68
|
+
name: raw.name,
|
|
69
|
+
hooks,
|
|
70
|
+
scope: entry.scope,
|
|
71
|
+
seed: entry.seed,
|
|
72
|
+
};
|
|
73
|
+
this.resolved.set(entryKey, resolved);
|
|
74
|
+
return resolved;
|
|
75
|
+
}
|
|
76
|
+
async resolveAll() {
|
|
77
|
+
const out = [];
|
|
78
|
+
const seen = new Set();
|
|
79
|
+
for (const [entryKey, entry] of this.entries) {
|
|
80
|
+
const resolved = await this.resolveEntry(entryKey, entry);
|
|
81
|
+
const identity = this.key(resolved.name, resolved.seed);
|
|
82
|
+
if (seen.has(identity)) {
|
|
83
|
+
// Warn once per duplicate entry, then forget it so later
|
|
84
|
+
// resolveAll() calls stay quiet.
|
|
85
|
+
warnDuplicate(resolved.name, resolved.seed);
|
|
86
|
+
this.entries.delete(entryKey);
|
|
87
|
+
this.resolved.delete(entryKey);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
seen.add(identity);
|
|
91
|
+
out.push(resolved);
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
clear() {
|
|
96
|
+
this.entries.clear();
|
|
97
|
+
this.resolved.clear();
|
|
98
|
+
}
|
|
99
|
+
size() {
|
|
100
|
+
return this.entries.size;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function warnDuplicate(name, seed) {
|
|
104
|
+
console.warn(`[burger-api] Plugin "${name}"${seed !== undefined ? ` (seed "${seed}")` : ''} ` +
|
|
105
|
+
'is already registered — this registration is ignored. Pass a ' +
|
|
106
|
+
'distinct seed (usePlugin(plugin, scope, seed)) to register it twice.');
|
|
107
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GlobalHooks } from '../lifecycle/types.js';
|
|
2
|
+
import type { Scope } from '../chain/node.js';
|
|
3
|
+
export interface Plugin {
|
|
4
|
+
name: string;
|
|
5
|
+
hooks?: GlobalHooks;
|
|
6
|
+
}
|
|
7
|
+
export type PluginFactory = () => Plugin | Promise<Plugin>;
|
|
8
|
+
export interface ResolvedPlugin {
|
|
9
|
+
name: string;
|
|
10
|
+
hooks: GlobalHooks;
|
|
11
|
+
scope: Scope;
|
|
12
|
+
/** Disambiguation seed from `usePlugin(plugin, scope, seed)`. */
|
|
13
|
+
seed?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface PluginEntry {
|
|
16
|
+
plugin: Plugin | PluginFactory;
|
|
17
|
+
scope: Scope;
|
|
18
|
+
seed?: string;
|
|
19
|
+
}
|
|
20
|
+
export type { Scope };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Precomputed `Allow` header strings for known routes, built once at compile
|
|
3
|
+
* time — no comma-joined method list rebuild per 405 response.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AllowCache {
|
|
6
|
+
private cache;
|
|
7
|
+
/**
|
|
8
|
+
* Builds the `Allow` header value from a list of methods.
|
|
9
|
+
* @example compute(['GET', 'POST']) => 'GET, POST'
|
|
10
|
+
*/
|
|
11
|
+
compute(methods: string[]): string;
|
|
12
|
+
/**
|
|
13
|
+
* Stores the `Allow` value for a path.
|
|
14
|
+
*/
|
|
15
|
+
set(path: string, value: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Returns the precomputed `Allow` value for a path, if known.
|
|
18
|
+
*/
|
|
19
|
+
get(path: string): string | undefined;
|
|
20
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Precomputed `Allow` header strings for known routes, built once at compile
|
|
3
|
+
* time — no comma-joined method list rebuild per 405 response.
|
|
4
|
+
*/
|
|
5
|
+
export class AllowCache {
|
|
6
|
+
cache = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* Builds the `Allow` header value from a list of methods.
|
|
9
|
+
* @example compute(['GET', 'POST']) => 'GET, POST'
|
|
10
|
+
*/
|
|
11
|
+
compute(methods) {
|
|
12
|
+
return methods.join(', ');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Stores the `Allow` value for a path.
|
|
16
|
+
*/
|
|
17
|
+
set(path, value) {
|
|
18
|
+
this.cache.set(path, value);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the precomputed `Allow` value for a path, if known.
|
|
22
|
+
*/
|
|
23
|
+
get(path) {
|
|
24
|
+
return this.cache.get(path);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { RouteDefinition } from '../types/index.js';
|
|
2
|
+
import type { RouteModule } from '../compiler/route-module.js';
|
|
3
|
+
import type { GlobalResponsePlan } from '../lifecycle/executor.js';
|
|
4
|
+
import type { RouteHooks } from '../lifecycle/types.js';
|
|
5
|
+
import type { ResolvedPlugin } from '../plugin/types.js';
|
|
6
|
+
import type { BurgerServices, RequestIPHolder } from '../context/context.js';
|
|
7
|
+
import type { CompiledRouter } from './types.js';
|
|
8
|
+
import type { ValidatorConfig } from '../validation/types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Builds the global + plugin (+ framework) response plan, in the same
|
|
11
|
+
* nearest-first order route plans use (global → plugin → framework; route
|
|
12
|
+
* scope is excluded because it only runs for matched routes).
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildGlobalResponsePlan(plugins?: ResolvedPlugin[], globalHooks?: RouteHooks): GlobalResponsePlan;
|
|
15
|
+
/**
|
|
16
|
+
* Compiles a `RouteDefinition[]` into the dispatch structures used by `Router`:
|
|
17
|
+
* builds one `CompiledHandler` per route (method dispatch + 405/Allow +
|
|
18
|
+
* auto-HEAD + hook pipeline), classifies routes as static (`StaticMap`) or
|
|
19
|
+
* dynamic/wildcard (`Trie`), populates the `AllowCache`, optionally runs the
|
|
20
|
+
* `RouteAccessAnalyzer` (compile-time only; its output is never read at
|
|
21
|
+
* runtime), and fails fast on duplicate or ambiguous routes.
|
|
22
|
+
*/
|
|
23
|
+
export declare class RouterCompiler {
|
|
24
|
+
private debug?;
|
|
25
|
+
private config;
|
|
26
|
+
/** JIT HookPlan compilation (capability-gated; on unless `jit: false`). */
|
|
27
|
+
private jit;
|
|
28
|
+
constructor(debug?: boolean, config?: ValidatorConfig, jit?: boolean);
|
|
29
|
+
compile(defs: RouteDefinition[], plugins?: ResolvedPlugin[], providers?: Map<string, unknown> | BurgerServices, onRequestHooksCount?: number, globalHooks?: RouteHooks, ipHolder?: RequestIPHolder): CompiledRouter;
|
|
30
|
+
/**
|
|
31
|
+
* Compiles a `RouteModule[]` (the Module Loader's output) into the
|
|
32
|
+
* dispatch structures — the compiler entry point for the file-based
|
|
33
|
+
* pipeline (Scanner → Module Loader → `RouteModule` → Compiler).
|
|
34
|
+
*
|
|
35
|
+
* Each module is normalized to the `RouteDefinition` shape, then compiled
|
|
36
|
+
* through {@link compile}.
|
|
37
|
+
*/
|
|
38
|
+
compileModules(modules: RouteModule[]): CompiledRouter;
|
|
39
|
+
}
|