@madhanmaaz/udane 0.0.0-stage → 0.1.1

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/lib/udane.js ADDED
@@ -0,0 +1,159 @@
1
+ const net = require("node:net");
2
+ const path = require("node:path");
3
+
4
+ const { LogController } = require("fastify");
5
+ const chokidar = require("chokidar");
6
+ const { WebSocketServer, WebSocket } = require("ws");
7
+
8
+ const getConfig = require("../helpers/getConfig.helper");
9
+ const createIgnore = require("../helpers/ignore.helper");
10
+ const { WS_PATH, PUBLIC_DIR_PREFIX } = require("../constants/index.constant");
11
+ const buildApp = require("../app");
12
+
13
+ const PORT_ATTEMPTS = 20;
14
+
15
+ function isFree(host, port) {
16
+ return new Promise((resolve) => {
17
+ const probe = net.createServer();
18
+ probe.once("error", () => resolve(false));
19
+ probe.once("listening", () => probe.close(() => resolve(true)));
20
+ probe.listen(port, host);
21
+ });
22
+ }
23
+
24
+ // Returns the first free port starting at `port`.
25
+ async function findPort(host, port) {
26
+ const last = Math.min(port + PORT_ATTEMPTS - 1, 65535);
27
+
28
+ for (let candidate = port; candidate <= last; candidate++) {
29
+ if (await isFree(host, candidate)) {
30
+ return candidate;
31
+ }
32
+ }
33
+
34
+ throw new Error(`No free port found between ${port} and ${last}`);
35
+ }
36
+
37
+ function broadcast(wss, message) {
38
+ for (const client of wss.clients) {
39
+ if (client.readyState === WebSocket.OPEN) {
40
+ client.send(message);
41
+ }
42
+ }
43
+ }
44
+
45
+ module.exports = async function udane(options = {}) {
46
+ const config = getConfig(options);
47
+ const port = await findPort(config.host, config.port);
48
+
49
+ const logger = config.logRequests
50
+ ? {
51
+ transport: {
52
+ target: "@fastify/one-line-logger",
53
+ options: { timeOnly: true },
54
+ },
55
+ }
56
+ : false;
57
+
58
+ const app = buildApp(config, {
59
+ logController: new LogController({
60
+ disableRequestLogging: (request) => {
61
+ // Disable logs for /__udane/*
62
+ return (
63
+ request.url.startsWith(`/${PUBLIC_DIR_PREFIX}/`) ||
64
+ /\.(css|js|map|png|jpe?g|gif|svg|ico|webp|woff2?)(\?|$)/i.test(
65
+ request.url,
66
+ )
67
+ );
68
+ },
69
+ }),
70
+ logger,
71
+ });
72
+
73
+ await app.listen({ host: config.host, port });
74
+
75
+ const wss = new WebSocketServer({ server: app.server, path: WS_PATH });
76
+
77
+ // Polling spreads the events of one build over a full interval,
78
+ // so wait longer than that before sending anything.
79
+ const POLL_INTERVAL = 300;
80
+ const settle = Math.max(config.debounce, POLL_INTERVAL + 100);
81
+
82
+ let timer = null;
83
+ let pending = null;
84
+ const changed = new Set();
85
+
86
+ function flush() {
87
+ const message = pending;
88
+ pending = null;
89
+
90
+ if (config.logChanges && changed.size) {
91
+ const list = [...changed];
92
+ if (list.length <= 5) {
93
+ for (const item of list) console.log(`[+] ${item}`);
94
+ } else {
95
+ console.log(`[+] ${list.length} files changed`);
96
+ }
97
+ }
98
+ changed.clear();
99
+
100
+ broadcast(wss, message);
101
+ }
102
+
103
+ // "reload" always wins over "refreshcss"
104
+ function schedule(message, label) {
105
+ changed.add(label);
106
+ if (pending !== "reload") pending = message;
107
+
108
+ clearTimeout(timer);
109
+ timer = setTimeout(flush, settle);
110
+ }
111
+
112
+ const watcher = chokidar
113
+ .watch(config.watch, {
114
+ ignored: createIgnore(config.ignore, config.watch),
115
+ ignoreInitial: true,
116
+ usePolling: true,
117
+ interval: POLL_INTERVAL,
118
+ })
119
+ .on("all", (event, filepath) => {
120
+ const cssOnly =
121
+ config.cssHotSwap &&
122
+ event === "change" &&
123
+ filepath.endsWith(".css");
124
+
125
+ schedule(
126
+ cssOnly ? "refreshcss" : "reload",
127
+ `${event} ${path.relative(process.cwd(), filepath)}`,
128
+ );
129
+ })
130
+ .on("error", (error) => {
131
+ console.error("[-] watcher error:", error.message);
132
+ });
133
+
134
+ let closed = false;
135
+
136
+ async function close() {
137
+ if (closed) return;
138
+ closed = true;
139
+
140
+ clearTimeout(timer);
141
+
142
+ // open sockets would keep app.close() waiting
143
+ for (const client of wss.clients) {
144
+ client.terminate();
145
+ }
146
+ wss.close();
147
+
148
+ await watcher.close();
149
+ await app.close();
150
+ }
151
+
152
+ return {
153
+ app,
154
+ config: { ...config, port },
155
+ requestedPort: config.port,
156
+ port,
157
+ close,
158
+ };
159
+ };
package/license ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Madhan (Madhanmaaz)
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.
@@ -0,0 +1,42 @@
1
+ const { PUBLIC_DIR_PREFIX } = require("../constants/index.constant");
2
+
3
+ const SCRIPT = `<script src="/${PUBLIC_DIR_PREFIX}/js/client.js"></script>`;
4
+ const CLOSING_BODY = /<\/body\s*>/gi;
5
+
6
+ async function toText(payload) {
7
+ if (typeof payload === "string") return payload;
8
+ if (Buffer.isBuffer(payload)) return payload.toString("utf8");
9
+
10
+ // static files are streamed
11
+ const chunks = [];
12
+ for await (const chunk of payload) chunks.push(Buffer.from(chunk));
13
+ return Buffer.concat(chunks).toString("utf8");
14
+ }
15
+
16
+ function inject(html) {
17
+ if (html.includes(SCRIPT)) return html;
18
+
19
+ const last = [...html.matchAll(CLOSING_BODY)].at(-1);
20
+ if (!last) return html + SCRIPT;
21
+
22
+ return html.slice(0, last.index) + SCRIPT + "\n" + html.slice(last.index);
23
+ }
24
+
25
+ module.exports = async function injectMiddleware(request, reply, payload) {
26
+ if (request.method !== "GET" || payload == null) return payload;
27
+
28
+ // 304 and 206 have no full body to rewrite
29
+ if (reply.statusCode === 304 || reply.statusCode === 206) return payload;
30
+
31
+ const type = String(reply.getHeader("content-type") ?? "");
32
+ if (!type.includes("text/html")) return payload;
33
+
34
+ // compressed bodies cannot be edited as text
35
+ if (reply.getHeader("content-encoding")) return payload;
36
+
37
+ reply.removeHeader("content-length");
38
+ reply.removeHeader("accept-ranges");
39
+ reply.header("cache-control", "no-store");
40
+
41
+ return inject(await toText(payload));
42
+ };
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
- "name": "@madhanmaaz/udane",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@madhanmaaz/udane",
3
+ "version": "0.1.1",
4
+ "description": "Udane - a fast, lightweight development server with live reload, built on Fastify.",
5
+ "author": {
6
+ "name": "madhanmaaz",
7
+ "url": "https://madhanmaaz.netlify.app"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/madhanmaaz/udane.git"
12
+ },
13
+ "homepage": "https://github.com/madhanmaaz/udane#readme",
14
+ "funding": [
15
+ {
16
+ "type": "github",
17
+ "url": "https://github.com/sponsors/madhanmaaz"
18
+ }
19
+ ],
20
+ "bugs": {
21
+ "url": "https://github.com/madhanmaaz/udane/issues"
22
+ },
23
+ "license": "MIT",
24
+ "bin": {
25
+ "udane": "lib/bin.js"
26
+ },
27
+ "main": "lib/udane.js",
28
+ "engines": {
29
+ "node": ">=20"
30
+ },
31
+ "dependencies": {
32
+ "@fastify/one-line-logger": "^2.2.0",
33
+ "@fastify/static": "^10.1.3",
34
+ "chokidar": "^5.0.0",
35
+ "fastify": "^5.12.4",
36
+ "tarkine": "^1.3.3",
37
+ "ws": "^8.22.0",
38
+ "zod": "^4.6.4"
39
+ },
40
+ "keywords": [
41
+ "udane",
42
+ "live-server",
43
+ "live-reload",
44
+ "livereload",
45
+ "hot-reload",
46
+ "dev-server",
47
+ "fastify"
48
+ ]
49
+ }
@@ -0,0 +1,10 @@
1
+ @import url("./root.css");
2
+
3
+ @font-face {
4
+ font-family: "Roboto";
5
+ src: url("../../fonts/Roboto-Medium.ttf");
6
+ }
7
+
8
+ :root {
9
+ --app-ff: "Roboto";
10
+ }
@@ -0,0 +1,136 @@
1
+ /* koyaka */
2
+ :root {
3
+ --sp: 0.25rem;
4
+ --box-sz: 0.5rem;
5
+ --field-sz: 0.25rem;
6
+ --ctrl-sz: 0.25rem;
7
+ --box-rds: 0.5rem;
8
+ --field-rds: 0.25rem;
9
+ --ctrl-rds: 0.125rem;
10
+ --bw: 1px;
11
+ --ow: 2px;
12
+ --bc: var(--fg) / 0.11;
13
+ --border: var(--bw) solid hsl(var(--bc));
14
+ --outline: var(--ow) solid hsl(var(--fg-alt));
15
+ --transparent: 0 0% 0% / 0;
16
+ --ff: var(
17
+ --app-ff,
18
+ ui-sans-serif,
19
+ system-ui,
20
+ sans-serif,
21
+ "Apple Color Emoji",
22
+ "Segoe UI Emoji",
23
+ "Segoe UI Symbol",
24
+ "Noto Color Emoji"
25
+ );
26
+ --ff-mono: var(
27
+ --app-ff-mono,
28
+ ui-monospace,
29
+ "Cascadia Code",
30
+ "Source Code Pro",
31
+ Menlo,
32
+ Consolas,
33
+ "DejaVu Sans Mono",
34
+ monospace
35
+ );
36
+ color-scheme: dark;
37
+ --bg-100: 200 20% 6%;
38
+ --bg-200: 200 18% 9%;
39
+ --bg-300: 200 16% 12%;
40
+ --bg-400: 200 14% 16%;
41
+ --fg: 210 20% 92%;
42
+ --fg-alt: 210 10% 65%;
43
+ --primary: 87 77% 49%;
44
+ --primary-fg: 87 61% 7%;
45
+ --secondary: 74 90% 52%;
46
+ --secondary-fg: 74 50% 10%;
47
+ --accent: 160 70% 45%;
48
+ --accent-fg: 160 50% 10%;
49
+ --neutral: 200 10% 55%;
50
+ --neutral-fg: 200 15% 10%;
51
+ --info: 200 90% 60%;
52
+ --info-fg: 200 60% 10%;
53
+ --success: 140 60% 50%;
54
+ --success-fg: 140 50% 10%;
55
+ --warning: 45 100% 55%;
56
+ --warning-fg: 45 70% 10%;
57
+ --error: 0 85% 65%;
58
+ --error-fg: 0 60% 12%;
59
+ }
60
+
61
+ *,
62
+ ::after,
63
+ ::before {
64
+ margin: 0;
65
+ padding: 0;
66
+ box-sizing: border-box;
67
+ }
68
+
69
+ * {
70
+ border: none;
71
+ outline: none;
72
+ background: none;
73
+ text-decoration: none;
74
+ scrollbar-width: thin;
75
+ scrollbar-color: hsl(var(--fg) / 0.3) transparent;
76
+ outline-offset: calc(var(--ow) / 2);
77
+ }
78
+
79
+ html,
80
+ :host {
81
+ font-family: var(--ff);
82
+ font-size: 16px;
83
+ line-height: 1.5;
84
+ -webkit-font-smoothing: antialiased;
85
+ -moz-osx-font-smoothing: grayscale;
86
+ -webkit-tap-highlight-color: transparent;
87
+ text-size-adjust: 100%;
88
+ -webkit-text-size-adjust: 100%;
89
+ }
90
+
91
+ body {
92
+ background-color: hsl(var(--bg-100));
93
+ color: hsl(var(--fg));
94
+ }
95
+
96
+ code,
97
+ pre,
98
+ kbd,
99
+ samp {
100
+ font-family: var(--ff-mono);
101
+ font-size: 0.9em;
102
+ }
103
+
104
+ table {
105
+ border-collapse: collapse;
106
+ border-spacing: 0;
107
+ }
108
+
109
+ img,
110
+ svg,
111
+ video,
112
+ canvas,
113
+ audio,
114
+ iframe,
115
+ embed,
116
+ object {
117
+ display: block;
118
+ }
119
+
120
+ img,
121
+ video {
122
+ max-width: 100%;
123
+ height: auto;
124
+ }
125
+
126
+ iframe {
127
+ border: 0;
128
+ }
129
+
130
+ :disabled,
131
+ .disabled,
132
+ [disabled] {
133
+ opacity: 0.5;
134
+ user-select: none;
135
+ pointer-events: none;
136
+ }
@@ -0,0 +1,41 @@
1
+ /*! https://koyaka.netlify.app */
2
+ .input {
3
+ --input_py: 1.75;
4
+ --input_px: 2.5;
5
+ --input_rds: 2.5;
6
+ --input_fs: 0.875;
7
+ --input-bg: var(--input-clr, var(--bg-300));
8
+ --input-fg: var(--input-clr-fg, var(--fg));
9
+ --input-bc: var(--input-clr, var(--bc));
10
+ --input-py: calc(var(--field-sz) * var(--input_py));
11
+ --input-px: calc(var(--field-sz) * var(--input_px));
12
+ --input-rds: calc(var(--field-rds) * var(--input_rds));
13
+ --input-fs: calc(1rem * var(--input_fs));
14
+ font-size: var(--input-fs);
15
+ color: hsl(var(--input-fg));
16
+ background-color: hsl(var(--input-bg));
17
+ padding: var(--input-py) var(--input-px);
18
+ border-radius: var(--input-rds);
19
+ border: var(--bw) solid hsl(var(--input-bc));
20
+ line-height: 1;
21
+ width: 100%;
22
+ transition:
23
+ border-color 0.2s ease,
24
+ background-color 0.2s ease,
25
+ color 0.2s ease;
26
+ }
27
+
28
+ .input:focus {
29
+ --input-bc: var(--input-clr, var(--fg-alt)) / 0.8;
30
+ box-shadow: 0 0 0 var(--bw) hsl(var(--input-bc));
31
+ }
32
+
33
+ .input::placeholder {
34
+ color: hsl(var(--input-fg) / 0.5);
35
+ }
36
+
37
+ @media (prefers-reduced-motion: reduce) {
38
+ .input {
39
+ transition: none;
40
+ }
41
+ }
@@ -0,0 +1,88 @@
1
+ body {
2
+ display: flex;
3
+ flex-direction: column;
4
+ gap: calc(var(--sp) * 2);
5
+ height: 100vh;
6
+ height: 100dvh;
7
+ }
8
+
9
+ header,
10
+ main,
11
+ footer {
12
+ padding: 1rem;
13
+ width: 100%;
14
+ max-width: 1400px;
15
+ margin: auto;
16
+ }
17
+
18
+ header {
19
+ display: flex;
20
+ align-items: center;
21
+ gap: calc(var(--sp) * 5);
22
+ }
23
+
24
+ header .logo {
25
+ width: 50px;
26
+ }
27
+
28
+ #directory-input {
29
+ flex: 3;
30
+ }
31
+
32
+ #search-input {
33
+ flex: 1;
34
+ }
35
+
36
+ main {
37
+ flex: 1;
38
+ overflow-y: auto;
39
+ text-align: left;
40
+ }
41
+
42
+ main a {
43
+ display: inline-flex;
44
+ align-items: center;
45
+ gap: calc(var(--sp) * 2);
46
+
47
+ width: 300px;
48
+ margin: calc(var(--sp) * 1) 0;
49
+ padding: calc(var(--sp) * 2) calc(var(--sp) * 3);
50
+
51
+ background-color: hsl(var(--bg-200));
52
+ border: var(--border);
53
+ border-radius: calc(var(--field-rds) * 1.5);
54
+ }
55
+
56
+ main a span {
57
+ min-width: 0;
58
+ overflow: hidden;
59
+ text-overflow: ellipsis;
60
+ white-space: nowrap;
61
+ }
62
+
63
+ main a:hover {
64
+ background-color: hsl(var(--bg-300));
65
+ }
66
+
67
+ main a img {
68
+ width: 18px;
69
+ }
70
+
71
+ main a.hide {
72
+ display: none;
73
+ }
74
+
75
+ footer {
76
+ text-align: right;
77
+ }
78
+
79
+ @media (max-width: 640px) {
80
+ header {
81
+ flex-direction: column;
82
+ gap: 0.5rem;
83
+ }
84
+
85
+ main a {
86
+ width: 100%;
87
+ }
88
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,56 @@
1
+ (function () {
2
+ if (!("WebSocket" in window)) {
3
+ console.error(
4
+ "[udane] This browser does not support WebSocket, live reload is disabled.",
5
+ );
6
+ return;
7
+ }
8
+
9
+ const PARAM = "_udaneCacheOverride";
10
+ const RETRY_MS = 7000;
11
+
12
+ function refreshCSS() {
13
+ const links = document.querySelectorAll(
14
+ 'link[rel~="stylesheet"][href]',
15
+ );
16
+
17
+ for (const link of links) {
18
+ const url = new URL(link.href, location.href);
19
+ if (url.origin !== location.origin) continue;
20
+
21
+ url.searchParams.set(PARAM, Date.now());
22
+
23
+ // load the new copy first, remove the old one after it is ready
24
+ const next = link.cloneNode();
25
+ next.href = url.href;
26
+ next.addEventListener("load", () => link.remove());
27
+ next.addEventListener("error", () => next.remove());
28
+ link.after(next);
29
+ }
30
+ }
31
+
32
+ let wasConnected = false;
33
+
34
+ function connect() {
35
+ const protocol = location.protocol === "https:" ? "wss://" : "ws://";
36
+ const socket = new WebSocket(`${protocol}${location.host}/__udane-ws`);
37
+
38
+ socket.addEventListener("open", () => {
39
+ // the server came back after a restart, so the page may be stale
40
+ if (wasConnected) return location.reload();
41
+ wasConnected = true;
42
+ });
43
+
44
+ socket.addEventListener("message", (event) => {
45
+ if (event.data === "reload") {
46
+ location.reload();
47
+ } else if (event.data === "refreshcss") {
48
+ refreshCSS();
49
+ }
50
+ });
51
+
52
+ socket.addEventListener("close", () => setTimeout(connect, RETRY_MS));
53
+ }
54
+
55
+ connect();
56
+ })();