@bigmistqke/workpad 0.0.0-stage → 0.18.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 +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1526 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1202 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2121 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +213 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +348 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +761 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +592 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +134 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +166 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +240 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +435 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +845 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
package/src/archive.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// An archived page (`workpad archive`): a page that did its job leaves the live pages, and what
|
|
2
|
+
// was answered on it stays readable without a server. Its file goes to `.workpad/archive/`, as
|
|
3
|
+
// the page's markdown with what happened on it written after: each form's answers with when they
|
|
4
|
+
// came (forms are the core's), then what each plugin makes of its own events there (a chat's
|
|
5
|
+
// messages, the comments: `Plugin.archive`). Then its events leave the log, as `destroy` does.
|
|
6
|
+
import fs from 'node:fs'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import type { Plugin, WpEvent } from './define.ts'
|
|
9
|
+
import { isQuestionName } from './status.ts'
|
|
10
|
+
|
|
11
|
+
/** When it came, to the minute, in UTC: `2026-11-26 10:14`. */
|
|
12
|
+
export const when = (e: WpEvent) => e.ts.slice(0, 16).replace('T', ' ')
|
|
13
|
+
|
|
14
|
+
/** A list item: its head, then text on the lines under it, indented to stay in the item. */
|
|
15
|
+
export const item = (head: string, text = '') =>
|
|
16
|
+
`- ${head}${text.trim() ? `\n\n${text.trim().replace(/^/gm, ' ')}` : ''}`
|
|
17
|
+
|
|
18
|
+
/** One answer to a form, in a line: its fields as given, and the button when it was another than Send. */
|
|
19
|
+
function answer(e: WpEvent) {
|
|
20
|
+
// a key with a colon is no field's (field names have none): something workpad or a plugin added
|
|
21
|
+
const own = Object.entries(e.data).filter(([k]) => !k.includes(':'))
|
|
22
|
+
const fields = own
|
|
23
|
+
.filter(([, v]) => v != null && v !== '' && v !== false && !(Array.isArray(v) && !v.length))
|
|
24
|
+
.map(([k, v]) => {
|
|
25
|
+
const value = Array.isArray(v) ? v.join(', ') : v === true ? 'yes' : String(v)
|
|
26
|
+
return value.includes('\n') ? null : `${k}: ${value}`
|
|
27
|
+
})
|
|
28
|
+
const long = own.filter(([, v]) => typeof v === 'string' && v.includes('\n'))
|
|
29
|
+
const button = e.action && !['Send', 'Submit'].includes(e.action) ? [`(${e.action})`] : []
|
|
30
|
+
const line = [...fields.filter(Boolean), ...button].join(' · ') || 'sent'
|
|
31
|
+
return item(`${when(e)}: ${line}`, long.map(([k, v]) => `${k}:\n\n${v}`).join('\n\n'))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The forms' answers: the reader's events of a question's type, by form, in the order they came. */
|
|
35
|
+
function answers(events: WpEvent[]) {
|
|
36
|
+
const byForm = new Map<string, WpEvent[]>()
|
|
37
|
+
for (const e of events)
|
|
38
|
+
if (isQuestionName(e.type) && e.from !== 'cli') byForm.set(e.type, [...(byForm.get(e.type) ?? []), e])
|
|
39
|
+
if (!byForm.size) return ''
|
|
40
|
+
return ['## Answers', ...[...byForm].map(([form, list]) => `### ${form}\n\n${list.map(answer).join('\n')}`)].join(
|
|
41
|
+
'\n\n',
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The archived page: its markdown, then what was answered on it. */
|
|
46
|
+
export async function archived(options: {
|
|
47
|
+
page: string
|
|
48
|
+
source: string
|
|
49
|
+
events: WpEvent[]
|
|
50
|
+
plugins: Plugin[]
|
|
51
|
+
at?: Date
|
|
52
|
+
}): Promise<string> {
|
|
53
|
+
const { page, source, events, plugins, at = new Date() } = options
|
|
54
|
+
const parts = [answers(events)]
|
|
55
|
+
for (const p of plugins) if (p.archive) parts.push((await p.archive(events))?.trim() ?? '')
|
|
56
|
+
const said = parts.filter(Boolean)
|
|
57
|
+
return [
|
|
58
|
+
source.trimEnd(),
|
|
59
|
+
'---',
|
|
60
|
+
`# Archived`,
|
|
61
|
+
`The page \`${page}\`, archived ${at.toISOString().slice(0, 16).replace('T', ' ')} (UTC), with what was answered on it.`,
|
|
62
|
+
...(said.length ? said : ['Nothing was answered on it.']),
|
|
63
|
+
]
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.join('\n\n')
|
|
66
|
+
.concat('\n')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Where an archived page goes: `.workpad/archive/<page>-<date>.md`, `-2` and on for another that day. */
|
|
70
|
+
export function archiveFile(wp: string, page: string, at = new Date()) {
|
|
71
|
+
const base = path.join(wp, 'archive', `${page}-${at.toISOString().slice(0, 10)}`)
|
|
72
|
+
let file = `${base}.md`
|
|
73
|
+
for (let n = 2; fs.existsSync(file); n++) file = `${base}-${n}.md`
|
|
74
|
+
return file
|
|
75
|
+
}
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// The project's token: what a browser or command needs for anything that runs code, such as an
|
|
2
|
+
// plugin that starts a process. Pages and events don't need it.
|
|
3
|
+
//
|
|
4
|
+
// /_/auth?token=…&to=/page sets the token as a cookie in this browser, then goes to `to`
|
|
5
|
+
//
|
|
6
|
+
// The token is made on first use and kept in `.workpad/token`, readable by its owner only. The
|
|
7
|
+
// command line sends it as a header; a browser gets it once through the link above (a command
|
|
8
|
+
// prints it). The cookie is HttpOnly and SameSite=Lax, and a request from another site is refused
|
|
9
|
+
// even with it, so a website open in the same browser can't use it. One kind is let through: opening
|
|
10
|
+
// a page as the tab's document, as a link from another app or a reload of such a tab does. Then the
|
|
11
|
+
// page shows what needs the token (the terminal drawer) to the reader alone: the site that sent them
|
|
12
|
+
// there can't read it, and the page's own requests come from its own origin.
|
|
13
|
+
//
|
|
14
|
+
// The cookie is Lax, not Strict, for that same case: a browser sends no Strict cookie on a
|
|
15
|
+
// navigation from outside, and a reload of that tab doesn't either, so such a tab never had the
|
|
16
|
+
// drawer until a link inside the page was followed.
|
|
17
|
+
import fs from 'node:fs'
|
|
18
|
+
import path from 'node:path'
|
|
19
|
+
import crypto from 'node:crypto'
|
|
20
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
21
|
+
|
|
22
|
+
const COOKIE = 'wp-token'
|
|
23
|
+
|
|
24
|
+
/** The project's token, made on first use. */
|
|
25
|
+
export function projectToken(wp: string): string {
|
|
26
|
+
const file = path.join(wp, 'token')
|
|
27
|
+
try {
|
|
28
|
+
return fs.readFileSync(file, 'utf8').trim()
|
|
29
|
+
} catch {}
|
|
30
|
+
const token = crypto.randomBytes(24).toString('base64url')
|
|
31
|
+
fs.writeFileSync(file, token + '\n', { mode: 0o600 })
|
|
32
|
+
return token
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* From a page of a workpad server on this machine, or from no page at all (the command line)?
|
|
37
|
+
* `origins`: the others a page may be at, as the machine config names them (a server in a
|
|
38
|
+
* container, reached at an address of its own: `https://workpad.local`).
|
|
39
|
+
*/
|
|
40
|
+
export const sameSite = (req: IncomingMessage, origins: readonly string[] = []): boolean => {
|
|
41
|
+
const origin = req.headers.origin
|
|
42
|
+
return !origin || /^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(origin) || origins.includes(origin)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `base`: the path the project is served under (see ServerOptions), which its cookie is kept to.
|
|
47
|
+
* `origins`: the other origins its pages may be at (see sameSite), asked for each request.
|
|
48
|
+
*/
|
|
49
|
+
export function createAuth(wp: string, base = '', origins: () => readonly string[] = () => []) {
|
|
50
|
+
const token = projectToken(wp)
|
|
51
|
+
const same = (a: unknown, b: string): boolean =>
|
|
52
|
+
typeof a === 'string' && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
|
|
53
|
+
const cookies = (req: IncomingMessage): Record<string, string | undefined> =>
|
|
54
|
+
Object.fromEntries(
|
|
55
|
+
(req.headers.cookie ?? '')
|
|
56
|
+
.split(';')
|
|
57
|
+
.map(c => c.trim().split('='))
|
|
58
|
+
.filter(([k]) => k),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
/** Has this request the token, from this origin? No request (a page being built) has none. */
|
|
62
|
+
function authorized(req: IncomingMessage | null): boolean {
|
|
63
|
+
if (!req) return false
|
|
64
|
+
const jar = cookies(req)
|
|
65
|
+
const has = same(jar[COOKIE], token) || same(req.headers['x-workpad-token'], token)
|
|
66
|
+
if (!has) return false
|
|
67
|
+
if (!sameSite(req, origins())) return false
|
|
68
|
+
const site = req.headers['sec-fetch-site']
|
|
69
|
+
if (site && site !== 'same-origin' && site !== 'none' && !opensPage(req)) return false
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Opening a page as the tab's document: no frame, no fetch, and not an API route. */
|
|
74
|
+
const opensPage = (req: IncomingMessage) =>
|
|
75
|
+
req.method === 'GET' &&
|
|
76
|
+
req.headers['sec-fetch-mode'] === 'navigate' &&
|
|
77
|
+
req.headers['sec-fetch-dest'] === 'document' &&
|
|
78
|
+
!(req.url ?? '').startsWith('/api/')
|
|
79
|
+
|
|
80
|
+
/** The /_/auth route; false for any other path. */
|
|
81
|
+
function handle(req: IncomingMessage, res: ServerResponse, p: string, q: Record<string, unknown>): boolean {
|
|
82
|
+
if (p !== '/_/auth') return false
|
|
83
|
+
if (!same(q.token, token)) {
|
|
84
|
+
res.writeHead(403, { 'content-type': 'text/plain' })
|
|
85
|
+
res.end('wrong token')
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
const to = typeof q.to === 'string' && q.to.startsWith('/') && !q.to.startsWith('//') ? q.to : '/'
|
|
89
|
+
res.writeHead(302, {
|
|
90
|
+
// under a shared server, each project's token is sent only to its own path
|
|
91
|
+
'set-cookie': `${COOKIE}=${token}; Path=${base || '/'}; HttpOnly; SameSite=Lax; Max-Age=31536000`,
|
|
92
|
+
location: base + to,
|
|
93
|
+
})
|
|
94
|
+
res.end()
|
|
95
|
+
return true
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { authorized, handle, token }
|
|
99
|
+
}
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Pages rendered once (`workpad build`), as files: each page's HTML with its links relative, and
|
|
2
|
+
// what it loads copied beside it, under its own ids and addresses (a site of its own: the server's is
|
|
3
|
+
// site.ts's servedSite). The page around a render is document.ts's, as for a page the server serves.
|
|
4
|
+
// See BuildConfig.
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
import { stripTypeScriptTypes } from 'node:module'
|
|
8
|
+
import type { Project } from './config.ts'
|
|
9
|
+
import type { BuildConfig } from './define.ts'
|
|
10
|
+
import { RUNTIME, pageDocument, pluginParts, type SetUp } from './document.ts'
|
|
11
|
+
import { toPath, type LoadedConfig } from './loader.ts'
|
|
12
|
+
import { PACKAGE_DIR } from './package.ts'
|
|
13
|
+
import { pageSource, within } from './site.ts'
|
|
14
|
+
|
|
15
|
+
const ENGINE_DIR = PACKAGE_DIR
|
|
16
|
+
const TYPESCRIPT = /\.m?ts$/i
|
|
17
|
+
const typeScriptModule = (file: string): string => stripTypeScriptTypes(fs.readFileSync(file, 'utf8'))
|
|
18
|
+
|
|
19
|
+
/** What a build made. */
|
|
20
|
+
export interface BuildResult {
|
|
21
|
+
/** the folder the site is in */
|
|
22
|
+
out: string
|
|
23
|
+
/** each page, and the file it became */
|
|
24
|
+
pages: { file: string; to: string }[]
|
|
25
|
+
/** what didn't fit: a link to a page outside the build, no index.html */
|
|
26
|
+
warnings: string[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What a build takes from the project's server: its config, its plugins set up, whether it serves the project's files. */
|
|
30
|
+
export interface BuildFrom {
|
|
31
|
+
project: Project
|
|
32
|
+
config(): Promise<LoadedConfig>
|
|
33
|
+
hooks(): Promise<SetUp[]>
|
|
34
|
+
files(): boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** What a build leaves out unless told otherwise: what is for writing a page, not for reading it. */
|
|
38
|
+
const BUILD_SKIP = ['form', 'chat', 'comments', 'draft']
|
|
39
|
+
/** In the folder a build writes, so that a next build knows it may replace it. */
|
|
40
|
+
const BUILD_MARK = '.workpad-build'
|
|
41
|
+
|
|
42
|
+
export async function build(
|
|
43
|
+
{ project, config, hooks: setUp, files: served }: BuildFrom,
|
|
44
|
+
options: BuildConfig = {},
|
|
45
|
+
): Promise<BuildResult> {
|
|
46
|
+
const cfg = await config()
|
|
47
|
+
if (cfg.errors.length) throw new Error(`the config failed to load: ${(cfg.errors[0] as Error).message}`)
|
|
48
|
+
if (!cfg.pages) throw new Error('the config has no pages renderer')
|
|
49
|
+
const pages = cfg.pages
|
|
50
|
+
const defined = <T>(...xs: (T | undefined)[]) => xs.find(x => x !== undefined)
|
|
51
|
+
const patterns = defined(options.pages, cfg.build.pages) ?? []
|
|
52
|
+
const skip = new Set(defined(options.skip, cfg.build.skip) ?? BUILD_SKIP)
|
|
53
|
+
const outDir = path.resolve(project.dir, defined(options.out, cfg.build.out) ?? 'site')
|
|
54
|
+
if (!patterns.length)
|
|
55
|
+
throw new Error(
|
|
56
|
+
"nothing to build: name the pages, as globs from the project's folder (workpad build 'docs/**/*.md'), or set build.pages in the config",
|
|
57
|
+
)
|
|
58
|
+
const warnings: string[] = []
|
|
59
|
+
|
|
60
|
+
// the pages: markdown files the globs match, but those a `!` glob leaves out
|
|
61
|
+
const include = patterns.filter(p => !p.startsWith('!'))
|
|
62
|
+
const exclude = patterns.filter(p => p.startsWith('!')).map(p => p.slice(1))
|
|
63
|
+
const files = [
|
|
64
|
+
...new Set(include.flatMap(p => fs.globSync(p, { cwd: project.dir }).map(f => path.resolve(project.dir, f)))),
|
|
65
|
+
]
|
|
66
|
+
.filter(f => /\.(md|markdown)$/i.test(f) && fs.statSync(f).isFile())
|
|
67
|
+
.filter(f => !exclude.some(g => path.matchesGlob(path.relative(project.dir, f), g)))
|
|
68
|
+
.sort()
|
|
69
|
+
if (!files.length) throw new Error(`no markdown files match ${patterns.join(' ')}`)
|
|
70
|
+
// each at its place below the folder they share; a README or index is its folder's index.html
|
|
71
|
+
const base = files.map(path.dirname).reduce((a, b) => {
|
|
72
|
+
while (!within(b, a)) a = path.dirname(a)
|
|
73
|
+
return a
|
|
74
|
+
})
|
|
75
|
+
const outOf = (file: string) => {
|
|
76
|
+
const rel = path.relative(base, file)
|
|
77
|
+
return path.join(
|
|
78
|
+
outDir,
|
|
79
|
+
/^(readme|index)\.(md|markdown)$/i.test(path.basename(rel))
|
|
80
|
+
? path.join(path.dirname(rel), 'index.html')
|
|
81
|
+
: rel.replace(/\.(md|markdown)$/i, '.html'),
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
const built = new Map(files.map(f => [f, outOf(f)]))
|
|
85
|
+
|
|
86
|
+
// the folder it writes: new, empty, or what an earlier build wrote
|
|
87
|
+
if (fs.existsSync(outDir)) {
|
|
88
|
+
if (fs.readdirSync(outDir).length && !fs.existsSync(path.join(outDir, BUILD_MARK)))
|
|
89
|
+
throw new Error(`${outDir} has files a build didn't write: give another folder (--out), or empty it`)
|
|
90
|
+
fs.rmSync(outDir, { recursive: true })
|
|
91
|
+
}
|
|
92
|
+
fs.mkdirSync(outDir, { recursive: true })
|
|
93
|
+
fs.writeFileSync(path.join(outDir, BUILD_MARK), 'written by workpad build; the next build replaces this folder\n')
|
|
94
|
+
|
|
95
|
+
// what a page loads, copied once: the package's files under _/workpad/, the project's under
|
|
96
|
+
// _/project/, at their places there (so a stylesheet's url()s still hold); TypeScript as JavaScript
|
|
97
|
+
const copied = new Map<string, string>()
|
|
98
|
+
function copyAsset(file: string): string {
|
|
99
|
+
const known = copied.get(file)
|
|
100
|
+
if (known) return known
|
|
101
|
+
const rel = within(file, ENGINE_DIR)
|
|
102
|
+
? path.join('_', 'workpad', path.relative(ENGINE_DIR, file))
|
|
103
|
+
: within(file, project.dir)
|
|
104
|
+
? path.join('_', 'project', path.relative(project.dir, file))
|
|
105
|
+
: path.join('_', 'files', path.basename(path.dirname(file)), path.basename(file))
|
|
106
|
+
const dest = path.join(outDir, TYPESCRIPT.test(rel) ? rel.replace(/\.m?ts$/i, '.js') : rel)
|
|
107
|
+
copied.set(file, dest)
|
|
108
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
|
109
|
+
if (TYPESCRIPT.test(file)) fs.writeFileSync(dest, typeScriptModule(file))
|
|
110
|
+
else fs.copyFileSync(file, dest)
|
|
111
|
+
// a stylesheet's own files (fonts, images), at the same places beside it
|
|
112
|
+
if (/\.css$/i.test(file)) {
|
|
113
|
+
for (const m of fs.readFileSync(file, 'utf8').matchAll(/url\(\s*['"]?([^'")]+)['"]?\s*\)/g)) {
|
|
114
|
+
const ref = m[1]!
|
|
115
|
+
if (/^(data:|[a-z][a-z0-9+.-]*:|\/|#)/i.test(ref)) continue
|
|
116
|
+
const target = path.resolve(path.dirname(file), ref.replace(/[?#].*$/, ''))
|
|
117
|
+
if (fs.existsSync(target) && fs.statSync(target).isFile()) copyAsset(target)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return dest
|
|
121
|
+
}
|
|
122
|
+
/** a file beside the pages (an image), copied to its place below the site */
|
|
123
|
+
function copyBeside(file: string): string | null {
|
|
124
|
+
if (!within(file, base)) return null
|
|
125
|
+
const dest = path.join(outDir, path.relative(base, file))
|
|
126
|
+
if (!fs.existsSync(dest)) {
|
|
127
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
|
128
|
+
fs.copyFileSync(file, dest)
|
|
129
|
+
}
|
|
130
|
+
return dest
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const hooks = await setUp()
|
|
134
|
+
const result: BuildResult = { out: outDir, pages: [], warnings }
|
|
135
|
+
for (const [file, dest] of built) {
|
|
136
|
+
const here = path.dirname(dest)
|
|
137
|
+
const relTo = (target: string) =>
|
|
138
|
+
path.relative(here, target).split(path.sep).map(encodeURIComponent).join('/') || '.'
|
|
139
|
+
const assetHere = (ref: string | URL) => relTo(copyAsset(toPath(ref)))
|
|
140
|
+
const urlFor = (abs: string): string | null => {
|
|
141
|
+
for (const candidate of [abs, abs + '.md']) {
|
|
142
|
+
if (!fs.existsSync(candidate)) continue
|
|
143
|
+
if (fs.statSync(candidate).isDirectory()) {
|
|
144
|
+
const index = [...built.keys()].find(
|
|
145
|
+
f => path.dirname(f) === candidate && /^(readme|index)\.(md|markdown)$/i.test(path.basename(f)),
|
|
146
|
+
)
|
|
147
|
+
if (index) return relTo(built.get(index)!)
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
if (built.has(candidate)) return relTo(built.get(candidate)!)
|
|
151
|
+
if (/\.(md|markdown)$/i.test(candidate)) {
|
|
152
|
+
warnings.push(
|
|
153
|
+
`${path.relative(project.dir, file)} links to ${path.relative(project.dir, candidate)}, which is not in the build`,
|
|
154
|
+
)
|
|
155
|
+
return null
|
|
156
|
+
}
|
|
157
|
+
const copy = copyBeside(candidate)
|
|
158
|
+
if (copy) return relTo(copy)
|
|
159
|
+
warnings.push(
|
|
160
|
+
`${path.relative(project.dir, file)} links to ${path.relative(project.dir, candidate)}, outside the built pages' folder`,
|
|
161
|
+
)
|
|
162
|
+
return null
|
|
163
|
+
}
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
const id = within(file, project.pagesDir)
|
|
167
|
+
? path
|
|
168
|
+
.relative(project.pagesDir, file)
|
|
169
|
+
.split(path.sep)
|
|
170
|
+
.join('/')
|
|
171
|
+
.replace(/\.(md|markdown)$/i, '')
|
|
172
|
+
: '-/' + path.relative(project.dir, file).split(path.sep).join('/')
|
|
173
|
+
const stored = pageSource(id, file)
|
|
174
|
+
if (stored.frontmatterError)
|
|
175
|
+
warnings.push(`${path.relative(project.dir, file)}: frontmatter: ${stored.frontmatterError}`)
|
|
176
|
+
const context = {
|
|
177
|
+
project: { name: project.name, dir: project.dir, files: served() },
|
|
178
|
+
asset: assetHere,
|
|
179
|
+
urlFor,
|
|
180
|
+
live: false,
|
|
181
|
+
skip,
|
|
182
|
+
}
|
|
183
|
+
const rendered = await pages.render(stored, context)
|
|
184
|
+
// a page without a heading of its own is named by its file (a README by its folder), not by
|
|
185
|
+
// the id the server knows it by
|
|
186
|
+
const named = /^(readme|index)\.(md|markdown)$/i.test(path.basename(file))
|
|
187
|
+
? path.basename(path.dirname(file))
|
|
188
|
+
: path.basename(file).replace(/\.(md|markdown)$/i, '')
|
|
189
|
+
const title = !rendered.title || rendered.title === id ? named : rendered.title
|
|
190
|
+
const out = { ...rendered, title, html: String(rendered.html ?? ''), head: String(rendered.head ?? '') }
|
|
191
|
+
// what the plugins add, but those the build leaves out, asked with no request: no browser
|
|
192
|
+
// asked for this page (what is only for the project's own browser has nothing to add here)
|
|
193
|
+
const parts = await pluginParts(hooks, null, {
|
|
194
|
+
asset: assetHere,
|
|
195
|
+
messages: [],
|
|
196
|
+
leaveOut: skip,
|
|
197
|
+
failed: message => warnings.push(`${path.relative(project.dir, file)}: ${message}`),
|
|
198
|
+
})
|
|
199
|
+
const doc = pageDocument(stored, { page: true, out, notices: [] }, parts, {
|
|
200
|
+
live: false,
|
|
201
|
+
base: '',
|
|
202
|
+
asset: assetHere,
|
|
203
|
+
runtime: assetHere(RUNTIME),
|
|
204
|
+
title: project.name,
|
|
205
|
+
})
|
|
206
|
+
fs.mkdirSync(here, { recursive: true })
|
|
207
|
+
fs.writeFileSync(dest, doc)
|
|
208
|
+
result.pages.push({ file, to: dest })
|
|
209
|
+
}
|
|
210
|
+
if (![...built.values()].includes(path.join(outDir, 'index.html')))
|
|
211
|
+
warnings.push('no README.md or index.md in the top folder: the site has no index.html')
|
|
212
|
+
return result
|
|
213
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The path the page's project is served under: '' on a server of its own, `/rg/develop` under a
|
|
2
|
+
// shared server (see Entries in DESIGN.md). The server says it in the page's handoff. Every request
|
|
3
|
+
// the page makes, and every link it writes, starts with it; what the browser keeps (drafts, what
|
|
4
|
+
// was seen) is kept per project with it, since the projects of a shared server share one origin.
|
|
5
|
+
const handoff = (() => {
|
|
6
|
+
try {
|
|
7
|
+
return JSON.parse(document.getElementById('wp-page')?.textContent ?? '{}') as { base?: string }
|
|
8
|
+
} catch {
|
|
9
|
+
return {}
|
|
10
|
+
}
|
|
11
|
+
})()
|
|
12
|
+
|
|
13
|
+
export const base: string = handoff.base ?? ''
|
|
14
|
+
|
|
15
|
+
/** A path of the project's server (`/api/events`) as this page requests it. */
|
|
16
|
+
export const url = (path: string): string => base + path
|
|
17
|
+
|
|
18
|
+
/** A localStorage key, kept apart from other projects' on the same origin. */
|
|
19
|
+
export const storageKey = (key: string): string => (base ? `${base}|${key}` : key)
|