@ramonyx/router 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +13 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/match.d.ts +27 -0
- package/dist/match.js +91 -0
- package/dist/match.js.map +1 -0
- package/package.json +28 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ramonymous
|
|
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
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ramonyx/router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Paket framework Ramonyx.
|
|
4
|
+
|
|
5
|
+
> **Alpha (0.1.0)** — framework Node.js ini adalah baseline produksi;
|
|
6
|
+
> runtime Rust/V8 bersifat eksperimental. Butuh **Node.js ≥ 24**.
|
|
7
|
+
|
|
8
|
+
## Instalasi
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @ramonyx/router
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Repo: https://github.com/RamonyxJS/ramonyx · Lisensi: MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../packages/router/src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export type { RouteMatch, RouteTable } from \"./match.ts\";\nexport { createRouteTable, matchRoute } from \"./match.ts\";\n"]}
|
package/dist/match.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { RouteEntry } from "@ramonyx/core";
|
|
2
|
+
export type RouteMatch = {
|
|
3
|
+
readonly kind: "match";
|
|
4
|
+
readonly entry: RouteEntry;
|
|
5
|
+
readonly params: Readonly<Record<string, string>>;
|
|
6
|
+
} | {
|
|
7
|
+
readonly kind: "method-not-allowed";
|
|
8
|
+
readonly allow: readonly string[];
|
|
9
|
+
} | {
|
|
10
|
+
readonly kind: "not-found";
|
|
11
|
+
};
|
|
12
|
+
interface CompiledEntry {
|
|
13
|
+
readonly entry: RouteEntry;
|
|
14
|
+
readonly segments: readonly string[];
|
|
15
|
+
readonly method: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RouteTable {
|
|
18
|
+
readonly entries: readonly RouteEntry[];
|
|
19
|
+
readonly compiled: readonly CompiledEntry[];
|
|
20
|
+
}
|
|
21
|
+
export declare function createRouteTable(entries: readonly RouteEntry[]): RouteTable;
|
|
22
|
+
/**
|
|
23
|
+
* Matching sederhana untuk Phase 1: statis + parameter (`[name]`).
|
|
24
|
+
* Route statis menang atas parameter; trailing slash dinormalisasi.
|
|
25
|
+
*/
|
|
26
|
+
export declare function matchRoute(table: RouteTable, method: string, pathname: string): RouteMatch;
|
|
27
|
+
export {};
|
package/dist/match.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** Segmen pola catch-all `[...name]` — hanya valid di posisi terakhir. */
|
|
2
|
+
const CATCH_ALL = /^\[\.\.\.([A-Za-z0-9_]+)\]$/;
|
|
3
|
+
function splitPath(path) {
|
|
4
|
+
const normalized = path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
|
|
5
|
+
if (normalized === "/" || normalized === "") {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
return normalized.split("/").filter((segment) => segment.length > 0);
|
|
9
|
+
}
|
|
10
|
+
function safeDecode(value) {
|
|
11
|
+
try {
|
|
12
|
+
return decodeURIComponent(value);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// Nilai URL tidak valid; pakai apa adanya.
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function createRouteTable(entries) {
|
|
20
|
+
const compiled = entries.map((entry) => ({
|
|
21
|
+
entry,
|
|
22
|
+
segments: splitPath(entry.path),
|
|
23
|
+
method: entry.method.toUpperCase(),
|
|
24
|
+
}));
|
|
25
|
+
return { entries, compiled };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Matching sederhana untuk Phase 1: statis + parameter (`[name]`).
|
|
29
|
+
* Route statis menang atas parameter; trailing slash dinormalisasi.
|
|
30
|
+
*/
|
|
31
|
+
export function matchRoute(table, method, pathname) {
|
|
32
|
+
const segments = splitPath(pathname);
|
|
33
|
+
const upperMethod = method.toUpperCase();
|
|
34
|
+
const allow = new Set();
|
|
35
|
+
let best;
|
|
36
|
+
for (const candidate of table.compiled) {
|
|
37
|
+
const lastPattern = candidate.segments.at(-1);
|
|
38
|
+
const catchAll = lastPattern === undefined ? null : CATCH_ALL.exec(lastPattern);
|
|
39
|
+
const fixedLength = candidate.segments.length - (catchAll === null ? 0 : 1);
|
|
40
|
+
// Tanpa catch-all: panjang harus sama. Dengan catch-all `[...name]`: minimal 1 segmen sisa.
|
|
41
|
+
if (catchAll === null
|
|
42
|
+
? candidate.segments.length !== segments.length
|
|
43
|
+
: segments.length < fixedLength + 1) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const params = {};
|
|
47
|
+
let score = 0;
|
|
48
|
+
let matched = true;
|
|
49
|
+
for (let index = 0; index < fixedLength; index += 1) {
|
|
50
|
+
const pattern = candidate.segments[index];
|
|
51
|
+
const value = segments[index];
|
|
52
|
+
if (pattern === undefined || value === undefined) {
|
|
53
|
+
matched = false;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (pattern.startsWith("[") && pattern.endsWith("]") && pattern.length > 2) {
|
|
57
|
+
params[pattern.slice(1, -1)] = safeDecode(value);
|
|
58
|
+
score += 1;
|
|
59
|
+
}
|
|
60
|
+
else if (pattern === value) {
|
|
61
|
+
score += 2;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
matched = false;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (matched && catchAll?.[1] !== undefined) {
|
|
69
|
+
// Catch-all menambah skor 0 → selalu kalah dari pola statis/parameter yang cocok.
|
|
70
|
+
params[catchAll[1]] = segments.slice(fixedLength).map(safeDecode).join("/");
|
|
71
|
+
}
|
|
72
|
+
if (!matched) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (candidate.method !== upperMethod) {
|
|
76
|
+
allow.add(candidate.method);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (best === undefined || score > best.score) {
|
|
80
|
+
best = { entry: candidate.entry, params, score };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (best !== undefined) {
|
|
84
|
+
return { kind: "match", entry: best.entry, params: best.params };
|
|
85
|
+
}
|
|
86
|
+
if (allow.size > 0) {
|
|
87
|
+
return { kind: "method-not-allowed", allow: [...allow].sort() };
|
|
88
|
+
}
|
|
89
|
+
return { kind: "not-found" };
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=match.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match.js","sourceRoot":"","sources":["../../../packages/router/src/match.ts"],"names":[],"mappings":"AAiBA,0EAA0E;AAC1E,MAAM,SAAS,GAAG,6BAA6B,CAAC;AAOhD,SAAS,SAAS,CAAC,IAAY;IAC7B,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACpF,IAAI,UAAU,KAAK,GAAG,IAAI,UAAU,KAAK,EAAE,EAAE,CAAC;QAC5C,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,UAAU,CAAC,KAAa;IAC/B,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAAC,MAAM,CAAC;QACP,2CAA2C;QAC3C,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,OAA8B;IAC7D,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACvC,KA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ACX,CAAC;QAED,IAAI,IAAI,KAAK,SAAS,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;YAC7C,IAAI,GAAG,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;QACnD,CAAC;IACH,CAAC;IAED,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;IACnE,CAAC;IACD,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;QACnB,OAAO,EAAE,IAAI,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;IAClE,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;AAC/B,CAAC","sourcesContent":["import type { RouteEntry } from \"@ramonyx/core\";\n\nexport type RouteMatch =\n | {\n readonly kind: \"match\";\n readonly entry: RouteEntry;\n readonly params: Readonly<Record<string, string>>;\n }\n | { readonly kind: \"method-not-allowed\"; readonly allow: readonly string[] }\n | { readonly kind: \"not-found\" };\n\ninterface CompiledEntry {\n readonly entry: RouteEntry;\n readonly segments: readonly string[];\n readonly method: string;\n}\n\n/** Segmen pola catch-all `[...name]` — hanya valid di posisi terakhir. */\nconst CATCH_ALL = /^\\[\\.\\.\\.([A-Za-z0-9_]+)\\]$/;\n\nexport interface RouteTable {\n readonly entries: readonly RouteEntry[];\n readonly compiled: readonly CompiledEntry[];\n}\n\nfunction splitPath(path: string): readonly string[] {\n const normalized = path.length > 1 && path.endsWith(\"/\") ? path.slice(0, -1) : path;\n if (normalized === \"/\" || normalized === \"\") {\n return [];\n }\n return normalized.split(\"/\").filter((segment) => segment.length > 0);\n}\n\nfunction safeDecode(value: string): string {\n try {\n return decodeURIComponent(value);\n } catch {\n // Nilai URL tidak valid; pakai apa adanya.\n return value;\n }\n}\n\nexport function createRouteTable(entries: readonly RouteEntry[]): RouteTable {\n const compiled = entries.map((entry) => ({\n entry,\n segments: splitPath(entry.path),\n method: entry.method.toUpperCase(),\n }));\n return { entries, compiled };\n}\n\n/**\n * Matching sederhana untuk Phase 1: statis + parameter (`[name]`).\n * Route statis menang atas parameter; trailing slash dinormalisasi.\n */\nexport function matchRoute(table: RouteTable, method: string, pathname: string): RouteMatch {\n const segments = splitPath(pathname);\n const upperMethod = method.toUpperCase();\n const allow = new Set<string>();\n let best: { entry: RouteEntry; params: Record<string, string>; score: number } | undefined;\n\n for (const candidate of table.compiled) {\n const lastPattern = candidate.segments.at(-1);\n const catchAll = lastPattern === undefined ? null : CATCH_ALL.exec(lastPattern);\n const fixedLength = candidate.segments.length - (catchAll === null ? 0 : 1);\n\n // Tanpa catch-all: panjang harus sama. Dengan catch-all `[...name]`: minimal 1 segmen sisa.\n if (\n catchAll === null\n ? candidate.segments.length !== segments.length\n : segments.length < fixedLength + 1\n ) {\n continue;\n }\n\n const params: Record<string, string> = {};\n let score = 0;\n let matched = true;\n\n for (let index = 0; index < fixedLength; index += 1) {\n const pattern = candidate.segments[index];\n const value = segments[index];\n if (pattern === undefined || value === undefined) {\n matched = false;\n break;\n }\n if (pattern.startsWith(\"[\") && pattern.endsWith(\"]\") && pattern.length > 2) {\n params[pattern.slice(1, -1)] = safeDecode(value);\n score += 1;\n } else if (pattern === value) {\n score += 2;\n } else {\n matched = false;\n break;\n }\n }\n\n if (matched && catchAll?.[1] !== undefined) {\n // Catch-all menambah skor 0 → selalu kalah dari pola statis/parameter yang cocok.\n params[catchAll[1]] = segments.slice(fixedLength).map(safeDecode).join(\"/\");\n }\n\n if (!matched) {\n continue;\n }\n\n if (candidate.method !== upperMethod) {\n allow.add(candidate.method);\n continue;\n }\n\n if (best === undefined || score > best.score) {\n best = { entry: candidate.entry, params, score };\n }\n }\n\n if (best !== undefined) {\n return { kind: \"match\", entry: best.entry, params: best.params };\n }\n if (allow.size > 0) {\n return { kind: \"method-not-allowed\", allow: [...allow].sort() };\n }\n return { kind: \"not-found\" };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ramonyx/router",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Paket framework Ramonyx.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=24"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@ramonyx/core": "^0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/RamonyxJS/ramonyx"
|
|
30
|
+
}
|
|
6
31
|
}
|