@better-forge/runtime 0.1.0 → 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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 BetterStatusPage contributors
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 BetterStatusPage contributors
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,9 +1,9 @@
1
- # @better-forge/runtime
2
-
3
- Application bootstrap: Fastify defaults, health, shutdown, update check, app lock and SSE.
4
-
5
- Request logs hide secrets: `redactQueryParams` replaces the value of those query parameters (names are compared after decoding), `redactPathTokenPrefixes` the path segment after a prefix such as `/links/`. `redactUrl` does the same for any URL.
6
-
7
- `createSse(limits)` serves live event streams. `SseLimits` caps the open anonymous streams in total and per address (`rateLimitKey` groups an IPv6 address by its /64), and the unsent bytes a client may hold before it is disconnected; signed-in streams are not counted. `rateLimitKey(ip)` is the key to use for any per-address limit.
8
-
9
- Status: skeleton. Depends on: `@better-forge/db`.
1
+ # @better-forge/runtime
2
+
3
+ Application bootstrap: Fastify defaults, health, shutdown, update check, app lock and SSE.
4
+
5
+ Request logs hide secrets: `redactQueryParams` replaces the value of those query parameters (names are compared after decoding), `redactPathTokenPrefixes` the path segment after a prefix such as `/links/`. `redactUrl` does the same for any URL.
6
+
7
+ `createSse(limits)` serves live event streams. `SseLimits` caps the open anonymous streams in total and per address (`rateLimitKey` groups an IPv6 address by its /64), and the unsent bytes a client may hold before it is disconnected; signed-in streams are not counted. `rateLimitKey(ip)` is the key to use for any per-address limit.
8
+
9
+ Status: skeleton. Depends on: `@better-forge/db`.
package/package.json CHANGED
@@ -1,65 +1,65 @@
1
- {
2
- "name": "@better-forge/runtime",
3
- "version": "0.1.0",
4
- "description": "Application bootstrap: Fastify defaults, health, shutdown, update check, app lock and SSE.",
5
- "license": "MIT",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/BElluu/Better-Forge.git",
9
- "directory": "packages/runtime"
10
- },
11
- "homepage": "https://github.com/BElluu/Better-Forge/tree/main/packages/runtime#readme",
12
- "bugs": {
13
- "url": "https://github.com/BElluu/Better-Forge/issues"
14
- },
15
- "publishConfig": {
16
- "access": "public"
17
- },
18
- "type": "module",
19
- "main": "./dist/index.js",
20
- "types": "./dist/index.d.ts",
21
- "exports": {
22
- ".": {
23
- "types": "./dist/index.d.ts",
24
- "import": "./dist/index.js"
25
- },
26
- "./package.json": "./package.json"
27
- },
28
- "files": [
29
- "dist",
30
- "src",
31
- "!src/**/*.test.*"
32
- ],
33
- "scripts": {
34
- "build": "tsc -p tsconfig.json",
35
- "dev": "tsc -p tsconfig.json --watch --preserveWatchOutput"
36
- },
37
- "peerDependencies": {
38
- "@fastify/cookie": "^11.1.1",
39
- "@fastify/cors": "^10.0.2",
40
- "@fastify/jwt": "^10.1.0",
41
- "@fastify/multipart": "^9.0.3",
42
- "@fastify/rate-limit": "^10.3.0",
43
- "@fastify/static": "^10.1.3",
44
- "drizzle-orm": "^0.45.2",
45
- "fastify": "^5.3.2"
46
- },
47
- "devDependencies": {
48
- "@fastify/cookie": "^11.1.1",
49
- "@fastify/cors": "^10.0.2",
50
- "@fastify/jwt": "^10.1.0",
51
- "@fastify/multipart": "^9.0.3",
52
- "@fastify/rate-limit": "^10.3.0",
53
- "@fastify/static": "^10.1.3",
54
- "@types/node": "^26.6.2",
55
- "drizzle-orm": "^0.45.2",
56
- "fastify": "^5.3.2",
57
- "typescript": "^5.7.3"
58
- },
59
- "dependencies": {
60
- "@better-forge/db": "0.1.0"
61
- },
62
- "engines": {
63
- "node": ">=26.10.0"
64
- }
65
- }
1
+ {
2
+ "name": "@better-forge/runtime",
3
+ "version": "0.1.1",
4
+ "description": "Application bootstrap: Fastify defaults, health, shutdown, update check, app lock and SSE.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/BElluu/Better-Forge.git",
9
+ "directory": "packages/runtime"
10
+ },
11
+ "homepage": "https://github.com/BElluu/Better-Forge/tree/main/packages/runtime#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/BElluu/Better-Forge/issues"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "type": "module",
19
+ "main": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "src",
31
+ "!src/**/*.test.*"
32
+ ],
33
+ "scripts": {
34
+ "build": "tsc -p tsconfig.json",
35
+ "dev": "tsc -p tsconfig.json --watch --preserveWatchOutput"
36
+ },
37
+ "peerDependencies": {
38
+ "@fastify/cookie": "^11.1.1",
39
+ "@fastify/cors": "^10.0.2",
40
+ "@fastify/jwt": "^10.1.0",
41
+ "@fastify/multipart": "^9.0.3",
42
+ "@fastify/rate-limit": "^10.3.0",
43
+ "@fastify/static": "^10.1.3",
44
+ "drizzle-orm": "^0.45.2",
45
+ "fastify": "^5.3.2"
46
+ },
47
+ "devDependencies": {
48
+ "@fastify/cookie": "^11.1.1",
49
+ "@fastify/cors": "^10.0.2",
50
+ "@fastify/jwt": "^10.1.0",
51
+ "@fastify/multipart": "^9.0.3",
52
+ "@fastify/rate-limit": "^10.3.0",
53
+ "@fastify/static": "^10.1.3",
54
+ "@types/node": "^26.6.2",
55
+ "drizzle-orm": "^0.45.2",
56
+ "fastify": "^5.3.2",
57
+ "typescript": "^5.7.3"
58
+ },
59
+ "dependencies": {
60
+ "@better-forge/db": "0.1.1"
61
+ },
62
+ "engines": {
63
+ "node": ">=26.10.0"
64
+ }
65
+ }
package/src/appLock.ts CHANGED
@@ -1,63 +1,63 @@
1
- import fs from 'node:fs'
2
- import path from 'node:path'
3
- import { currentProcessOwner, isOwnerAlive, type ProcessOwner } from './processLiveness.js'
4
-
5
- const FRESH_MS = 30_000
6
-
7
- /**
8
- * A stalled heartbeat (event loop blocked by a long synchronous task) still counts as running
9
- * while the recorded pid is alive — but only for this long, so a reused pid cannot block forever.
10
- */
11
- const LIVE_OWNER_MS = 10 * 60_000
12
-
13
- /**
14
- * The marker file that tells a restore or a second start that the application is running. `name` is how the
15
- * application is called in the refusal, `fileName` the marker inside `dir()` (evaluated on each use).
16
- */
17
- export function createAppLock(options: { name: string; fileName: string; dir: () => string }) {
18
- const RUNNING_MESSAGE = `${options.name} is still running. Stop the application before restoring.`
19
- const lockPath = (): string => path.join(options.dir(), options.fileName)
20
-
21
- function readOwner(file: string): Partial<ProcessOwner> {
22
- try { return JSON.parse(fs.readFileSync(file, 'utf8')) as Partial<ProcessOwner> }
23
- catch { return {} }
24
- }
25
-
26
- /** Removes a marker whose owner is gone; throws while the app still looks alive. */
27
- function clearStaleMarker(options: { checkOwner: boolean }): void {
28
- const file = lockPath()
29
- if (!fs.existsSync(file)) return
30
- const age = Date.now() - fs.statSync(file).mtimeMs
31
- const ownerAlive = isOwnerAlive(readOwner(file))
32
- // A killed process (Ctrl+C on Windows, tsx watch restart) leaves a fresh marker; startup trusts a dead pid over the heartbeat.
33
- if (age < FRESH_MS && (options.checkOwner || ownerAlive)) throw new Error(RUNNING_MESSAGE)
34
- if (options.checkOwner && age < LIVE_OWNER_MS && ownerAlive) throw new Error(RUNNING_MESSAGE)
35
- fs.rmSync(file, { force: true })
36
- }
37
-
38
- /** Used by restore: also refuses while the recorded app process is alive but its heartbeat has stalled. */
39
- function assertAppStopped(): void {
40
- clearStaleMarker({ checkOwner: true })
41
- }
42
-
43
- /** Startup uses the heartbeat only, so a restart after a crash is never blocked by an unrelated process that reused the pid. */
44
- function acquireAppLock(): () => void {
45
- clearStaleMarker({ checkOwner: false })
46
- fs.mkdirSync(options.dir(), { recursive: true })
47
- const file = lockPath()
48
- fs.writeFileSync(file, JSON.stringify({ ...currentProcessOwner(), startedAt: Date.now() }), { flag: 'wx', mode: 0o600 })
49
- const heartbeat = setInterval(() => {
50
- try { fs.utimesSync(file, new Date(), new Date()) } catch { /* shutdown race */ }
51
- }, 5_000)
52
- heartbeat.unref()
53
- let released = false
54
- return () => {
55
- if (released) return
56
- released = true
57
- clearInterval(heartbeat)
58
- fs.rmSync(file, { force: true })
59
- }
60
- }
61
-
62
- return { assertAppStopped, acquireAppLock }
63
- }
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import { currentProcessOwner, isOwnerAlive, type ProcessOwner } from './processLiveness.js'
4
+
5
+ const FRESH_MS = 30_000
6
+
7
+ /**
8
+ * A stalled heartbeat (event loop blocked by a long synchronous task) still counts as running
9
+ * while the recorded pid is alive — but only for this long, so a reused pid cannot block forever.
10
+ */
11
+ const LIVE_OWNER_MS = 10 * 60_000
12
+
13
+ /**
14
+ * The marker file that tells a restore or a second start that the application is running. `name` is how the
15
+ * application is called in the refusal, `fileName` the marker inside `dir()` (evaluated on each use).
16
+ */
17
+ export function createAppLock(options: { name: string; fileName: string; dir: () => string }) {
18
+ const RUNNING_MESSAGE = `${options.name} is still running. Stop the application before restoring.`
19
+ const lockPath = (): string => path.join(options.dir(), options.fileName)
20
+
21
+ function readOwner(file: string): Partial<ProcessOwner> {
22
+ try { return JSON.parse(fs.readFileSync(file, 'utf8')) as Partial<ProcessOwner> }
23
+ catch { return {} }
24
+ }
25
+
26
+ /** Removes a marker whose owner is gone; throws while the app still looks alive. */
27
+ function clearStaleMarker(options: { checkOwner: boolean }): void {
28
+ const file = lockPath()
29
+ if (!fs.existsSync(file)) return
30
+ const age = Date.now() - fs.statSync(file).mtimeMs
31
+ const ownerAlive = isOwnerAlive(readOwner(file))
32
+ // A killed process (Ctrl+C on Windows, tsx watch restart) leaves a fresh marker; startup trusts a dead pid over the heartbeat.
33
+ if (age < FRESH_MS && (options.checkOwner || ownerAlive)) throw new Error(RUNNING_MESSAGE)
34
+ if (options.checkOwner && age < LIVE_OWNER_MS && ownerAlive) throw new Error(RUNNING_MESSAGE)
35
+ fs.rmSync(file, { force: true })
36
+ }
37
+
38
+ /** Used by restore: also refuses while the recorded app process is alive but its heartbeat has stalled. */
39
+ function assertAppStopped(): void {
40
+ clearStaleMarker({ checkOwner: true })
41
+ }
42
+
43
+ /** Startup uses the heartbeat only, so a restart after a crash is never blocked by an unrelated process that reused the pid. */
44
+ function acquireAppLock(): () => void {
45
+ clearStaleMarker({ checkOwner: false })
46
+ fs.mkdirSync(options.dir(), { recursive: true })
47
+ const file = lockPath()
48
+ fs.writeFileSync(file, JSON.stringify({ ...currentProcessOwner(), startedAt: Date.now() }), { flag: 'wx', mode: 0o600 })
49
+ const heartbeat = setInterval(() => {
50
+ try { fs.utimesSync(file, new Date(), new Date()) } catch { /* shutdown race */ }
51
+ }, 5_000)
52
+ heartbeat.unref()
53
+ let released = false
54
+ return () => {
55
+ if (released) return
56
+ released = true
57
+ clearInterval(heartbeat)
58
+ fs.rmSync(file, { force: true })
59
+ }
60
+ }
61
+
62
+ return { assertAppStopped, acquireAppLock }
63
+ }