@cmodjs/rulebook 0.2.2 → 0.2.4
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/package.json +1 -1
- package/src/serve.ts +18 -4
package/package.json
CHANGED
package/src/serve.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { rmSync, statSync } from 'node:fs'
|
|
2
2
|
import { tmpdir } from 'node:os'
|
|
3
3
|
import { join } from 'node:path'
|
|
4
|
-
import { isRecord } from './config.ts'
|
|
4
|
+
import { isRecord, isStrings } from './config.ts'
|
|
5
5
|
import { isQuestion, projectOf, type Question } from './questions.ts'
|
|
6
6
|
import type { Answered, Asked } from './worker.ts'
|
|
7
7
|
|
|
@@ -23,6 +23,8 @@ type Project = {
|
|
|
23
23
|
export async function serve(): Promise<never> {
|
|
24
24
|
const socket = join(tmpdir(), `rulebook-${process.pid}.sock`)
|
|
25
25
|
const projects = new Map<string, Project>()
|
|
26
|
+
const failing = new Map<string, readonly string[]>()
|
|
27
|
+
const shown = new Set<string>()
|
|
26
28
|
let lastId = 0
|
|
27
29
|
|
|
28
30
|
const stop = (project: Project): void => {
|
|
@@ -56,8 +58,7 @@ export async function serve(): Promise<never> {
|
|
|
56
58
|
return project
|
|
57
59
|
}
|
|
58
60
|
|
|
59
|
-
const projectFor = (root: string, environment: Environment): Project => {
|
|
60
|
-
const key = `${root}\0${JSON.stringify(environment)}`
|
|
61
|
+
const projectFor = (key: string, root: string, environment: Environment): Project => {
|
|
61
62
|
const known = projects.get(key)
|
|
62
63
|
if (known !== undefined && !isStale(known)) return known
|
|
63
64
|
if (known !== undefined) stop(known)
|
|
@@ -66,6 +67,16 @@ export async function serve(): Promise<never> {
|
|
|
66
67
|
return project
|
|
67
68
|
}
|
|
68
69
|
|
|
70
|
+
const unshown = (key: string, listing: unknown): unknown => {
|
|
71
|
+
if (!isRecord(listing) || !isStrings(listing.failures)) return listing
|
|
72
|
+
const { failures } = listing
|
|
73
|
+
for (const fixed of failing.get(key) ?? []) if (!failures.includes(fixed)) shown.delete(fixed)
|
|
74
|
+
failing.set(key, failures)
|
|
75
|
+
const fresh = failures.filter((failure) => !shown.has(failure))
|
|
76
|
+
for (const failure of fresh) shown.add(failure)
|
|
77
|
+
return { ...listing, failures: fresh }
|
|
78
|
+
}
|
|
79
|
+
|
|
69
80
|
const ask = (project: Project, question: Question, input: unknown, root: string): Promise<unknown> => {
|
|
70
81
|
lastId += 1
|
|
71
82
|
const asked = Promise.withResolvers<unknown>()
|
|
@@ -85,7 +96,10 @@ export async function serve(): Promise<never> {
|
|
|
85
96
|
const body: unknown = await request.json()
|
|
86
97
|
if (!isRecord(body)) throw new Error('rulebook serve reads {"input", "environment"}.')
|
|
87
98
|
const root = projectOf(question, body.input)
|
|
88
|
-
|
|
99
|
+
const environment = environmentOf(body.environment)
|
|
100
|
+
const key = `${root}\0${JSON.stringify(environment)}`
|
|
101
|
+
const answered = await ask(projectFor(key, root, environment), question, body.input, root)
|
|
102
|
+
return Response.json(question === 'rules' ? unshown(key, answered) : answered)
|
|
89
103
|
} catch (error) {
|
|
90
104
|
return new Response(error instanceof Error ? error.message : String(error), { status: 500 })
|
|
91
105
|
}
|