@antelopejs/dms-antelope-cloud 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.
Files changed (224) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +447 -2
  3. package/dist/cloud-status.d.ts +13 -0
  4. package/dist/cloud-status.js +77 -0
  5. package/dist/config.d.ts +84 -0
  6. package/dist/config.js +104 -0
  7. package/dist/configured.d.ts +16 -0
  8. package/dist/configured.js +26 -0
  9. package/dist/connection/actions.d.ts +40 -0
  10. package/dist/connection/actions.js +120 -0
  11. package/dist/connection/announce.d.ts +8 -0
  12. package/dist/connection/announce.js +20 -0
  13. package/dist/connection/cloud.d.ts +60 -0
  14. package/dist/connection/cloud.js +87 -0
  15. package/dist/connection/content.d.ts +70 -0
  16. package/dist/connection/content.js +462 -0
  17. package/dist/connection/current.d.ts +26 -0
  18. package/dist/connection/current.js +28 -0
  19. package/dist/connection/device-flow.d.ts +16 -0
  20. package/dist/connection/device-flow.js +146 -0
  21. package/dist/connection/refusals.d.ts +51 -0
  22. package/dist/connection/refusals.js +85 -0
  23. package/dist/connection/repository.d.ts +28 -0
  24. package/dist/connection/repository.js +69 -0
  25. package/dist/connection/seal.d.ts +22 -0
  26. package/dist/connection/seal.js +47 -0
  27. package/dist/connection/store.d.ts +294 -0
  28. package/dist/connection/store.js +165 -0
  29. package/dist/connection/view.d.ts +39 -0
  30. package/dist/connection/view.js +39 -0
  31. package/dist/dashboard/actions.d.ts +40 -0
  32. package/dist/dashboard/actions.js +115 -0
  33. package/dist/dashboard/dockerfile.d.ts +34 -0
  34. package/dist/dashboard/dockerfile.js +71 -0
  35. package/dist/dashboard/service.d.ts +61 -0
  36. package/dist/dashboard/service.js +137 -0
  37. package/dist/dashboard/variables.d.ts +30 -0
  38. package/dist/dashboard/variables.js +34 -0
  39. package/dist/deployments/build-log.d.ts +39 -0
  40. package/dist/deployments/build-log.js +57 -0
  41. package/dist/deployments/cloud-steps.d.ts +77 -0
  42. package/dist/deployments/cloud-steps.js +132 -0
  43. package/dist/deployments/content.d.ts +61 -0
  44. package/dist/deployments/content.js +268 -0
  45. package/dist/deployments/deployment.d.ts +96 -0
  46. package/dist/deployments/deployment.js +3 -0
  47. package/dist/deployments/follow.d.ts +18 -0
  48. package/dist/deployments/follow.js +112 -0
  49. package/dist/deployments/ledger.d.ts +23 -0
  50. package/dist/deployments/ledger.js +359 -0
  51. package/dist/deployments/lifecycle.d.ts +123 -0
  52. package/dist/deployments/lifecycle.js +146 -0
  53. package/dist/deployments/live.d.ts +28 -0
  54. package/dist/deployments/live.js +54 -0
  55. package/dist/deployments/publish-read.d.ts +20 -0
  56. package/dist/deployments/publish-read.js +24 -0
  57. package/dist/deployments/refusals.d.ts +13 -0
  58. package/dist/deployments/refusals.js +35 -0
  59. package/dist/environments/actions.d.ts +86 -0
  60. package/dist/environments/actions.js +210 -0
  61. package/dist/environments/address.d.ts +60 -0
  62. package/dist/environments/address.js +149 -0
  63. package/dist/environments/content.d.ts +97 -0
  64. package/dist/environments/content.js +275 -0
  65. package/dist/environments/follower.d.ts +32 -0
  66. package/dist/environments/follower.js +114 -0
  67. package/dist/environments/pipeline.d.ts +58 -0
  68. package/dist/environments/pipeline.js +76 -0
  69. package/dist/environments/publish-targets.d.ts +18 -0
  70. package/dist/environments/publish-targets.js +55 -0
  71. package/dist/environments/roles.d.ts +73 -0
  72. package/dist/environments/roles.js +97 -0
  73. package/dist/frontend-module.d.ts +12 -0
  74. package/dist/frontend-module.js +25 -0
  75. package/dist/git/changed-files.d.ts +30 -0
  76. package/dist/git/changed-files.js +107 -0
  77. package/dist/git/changes.d.ts +89 -0
  78. package/dist/git/changes.js +162 -0
  79. package/dist/git/command.d.ts +51 -0
  80. package/dist/git/command.js +231 -0
  81. package/dist/git/promotion.d.ts +23 -0
  82. package/dist/git/promotion.js +46 -0
  83. package/dist/git/publish-input.d.ts +42 -0
  84. package/dist/git/publish-input.js +62 -0
  85. package/dist/git/publish-job.d.ts +82 -0
  86. package/dist/git/publish-job.js +99 -0
  87. package/dist/git/publish-steps.d.ts +90 -0
  88. package/dist/git/publish-steps.js +205 -0
  89. package/dist/git/publisher.d.ts +23 -0
  90. package/dist/git/publisher.js +206 -0
  91. package/dist/git/refs.d.ts +99 -0
  92. package/dist/git/refs.js +178 -0
  93. package/dist/git/refusal.d.ts +49 -0
  94. package/dist/git/refusal.js +70 -0
  95. package/dist/git/repository.d.ts +101 -0
  96. package/dist/git/repository.js +273 -0
  97. package/dist/git/versions.d.ts +99 -0
  98. package/dist/git/versions.js +174 -0
  99. package/dist/git/write-lock.d.ts +19 -0
  100. package/dist/git/write-lock.js +49 -0
  101. package/dist/index.d.ts +5 -0
  102. package/dist/index.js +59 -0
  103. package/dist/lifecycle.d.ts +8 -0
  104. package/dist/lifecycle.js +78 -0
  105. package/dist/logs/relay.d.ts +25 -0
  106. package/dist/logs/relay.js +138 -0
  107. package/dist/null-prototype.d.ts +7 -0
  108. package/dist/null-prototype.js +3 -0
  109. package/dist/overview/content.d.ts +40 -0
  110. package/dist/overview/content.js +172 -0
  111. package/dist/pages/changes.d.ts +32 -0
  112. package/dist/pages/changes.js +141 -0
  113. package/dist/pages/connection.d.ts +43 -0
  114. package/dist/pages/connection.js +295 -0
  115. package/dist/pages/deployment.d.ts +67 -0
  116. package/dist/pages/deployment.js +261 -0
  117. package/dist/pages/deployments.d.ts +46 -0
  118. package/dist/pages/deployments.js +266 -0
  119. package/dist/pages/environments.d.ts +34 -0
  120. package/dist/pages/environments.js +368 -0
  121. package/dist/pages/logs.d.ts +46 -0
  122. package/dist/pages/logs.js +140 -0
  123. package/dist/pages/navigation.d.ts +61 -0
  124. package/dist/pages/navigation.js +107 -0
  125. package/dist/pages/overview.d.ts +35 -0
  126. package/dist/pages/overview.js +201 -0
  127. package/dist/pages/project-guard.d.ts +32 -0
  128. package/dist/pages/project-guard.js +99 -0
  129. package/dist/pages/query.d.ts +6 -0
  130. package/dist/pages/query.js +14 -0
  131. package/dist/pages/resources.d.ts +37 -0
  132. package/dist/pages/resources.js +211 -0
  133. package/dist/pages/setup.d.ts +40 -0
  134. package/dist/pages/setup.js +271 -0
  135. package/dist/pages/slugs.d.ts +15 -0
  136. package/dist/pages/slugs.js +28 -0
  137. package/dist/pages/variables.d.ts +43 -0
  138. package/dist/pages/variables.js +327 -0
  139. package/dist/pages/version.d.ts +26 -0
  140. package/dist/pages/version.js +214 -0
  141. package/dist/pages/versions.d.ts +20 -0
  142. package/dist/pages/versions.js +115 -0
  143. package/dist/resources/apply.d.ts +15 -0
  144. package/dist/resources/apply.js +127 -0
  145. package/dist/resources/cards.d.ts +41 -0
  146. package/dist/resources/cards.js +211 -0
  147. package/dist/resources/connect.d.ts +66 -0
  148. package/dist/resources/connect.js +78 -0
  149. package/dist/resources/content.d.ts +26 -0
  150. package/dist/resources/content.js +158 -0
  151. package/dist/resources/format.d.ts +10 -0
  152. package/dist/resources/format.js +30 -0
  153. package/dist/resources/guard.d.ts +23 -0
  154. package/dist/resources/guard.js +60 -0
  155. package/dist/resources/modules.d.ts +23 -0
  156. package/dist/resources/modules.js +39 -0
  157. package/dist/resources/plan.d.ts +8 -0
  158. package/dist/resources/plan.js +93 -0
  159. package/dist/resources/read.d.ts +54 -0
  160. package/dist/resources/read.js +69 -0
  161. package/dist/resources/refusals.d.ts +15 -0
  162. package/dist/resources/refusals.js +33 -0
  163. package/dist/resources/routes.d.ts +32 -0
  164. package/dist/resources/routes.js +53 -0
  165. package/dist/setup/actions.d.ts +79 -0
  166. package/dist/setup/actions.js +188 -0
  167. package/dist/setup/apply.d.ts +37 -0
  168. package/dist/setup/apply.js +172 -0
  169. package/dist/setup/content.d.ts +50 -0
  170. package/dist/setup/content.js +440 -0
  171. package/dist/setup/rollout.d.ts +119 -0
  172. package/dist/setup/rollout.js +227 -0
  173. package/dist/setup/steps.d.ts +93 -0
  174. package/dist/setup/steps.js +95 -0
  175. package/dist/setup/storage.d.ts +29 -0
  176. package/dist/setup/storage.js +78 -0
  177. package/dist/setup/store.d.ts +214 -0
  178. package/dist/setup/store.js +174 -0
  179. package/dist/setup/volume.d.ts +31 -0
  180. package/dist/setup/volume.js +52 -0
  181. package/dist/slug.d.ts +7 -0
  182. package/dist/slug.js +26 -0
  183. package/dist/variables/actions.d.ts +76 -0
  184. package/dist/variables/actions.js +185 -0
  185. package/dist/variables/content.d.ts +69 -0
  186. package/dist/variables/content.js +310 -0
  187. package/dist/variables/decide.d.ts +27 -0
  188. package/dist/variables/decide.js +155 -0
  189. package/dist/variables/decided-dialog.d.ts +11 -0
  190. package/dist/variables/decided-dialog.js +129 -0
  191. package/dist/variables/decided.d.ts +80 -0
  192. package/dist/variables/decided.js +240 -0
  193. package/dist/variables/dialogs.d.ts +8 -0
  194. package/dist/variables/dialogs.js +219 -0
  195. package/dist/variables/model.d.ts +106 -0
  196. package/dist/variables/model.js +156 -0
  197. package/dist/variables/project-config.d.ts +117 -0
  198. package/dist/variables/project-config.js +248 -0
  199. package/dist/variables/read.d.ts +33 -0
  200. package/dist/variables/read.js +118 -0
  201. package/dist/web-url.d.ts +9 -0
  202. package/dist/web-url.js +19 -0
  203. package/frontend-vue/app/components/BuildLog.vue +99 -0
  204. package/frontend-vue/app/components/ChangedFiles.vue +165 -0
  205. package/frontend-vue/app/components/DashboardEnvironments.vue +102 -0
  206. package/frontend-vue/app/components/LogLine.vue +33 -0
  207. package/frontend-vue/app/components/LogViewer.vue +314 -0
  208. package/frontend-vue/app/components/PublishChipIcon.vue +39 -0
  209. package/frontend-vue/app/components/PublishForm.vue +505 -0
  210. package/frontend-vue/app/components/PublishPopover.vue +90 -0
  211. package/frontend-vue/app/components/PublishRoute.vue +222 -0
  212. package/frontend-vue/app/composables/usePublishSummary.ts +80 -0
  213. package/frontend-vue/app/plugins/publish-chip.client.ts +44 -0
  214. package/frontend-vue/app/types/publishing.ts +154 -0
  215. package/frontend-vue/app/utils/build-log.ts +96 -0
  216. package/frontend-vue/app/utils/changed-files.ts +142 -0
  217. package/frontend-vue/app/utils/log-line.ts +15 -0
  218. package/frontend-vue/app/utils/log-stream.ts +361 -0
  219. package/frontend-vue/app/utils/publish-state.ts +299 -0
  220. package/frontend-vue/dms.frontend.ts +27 -0
  221. package/frontend-vue/i18n/locales/antelope-cloud-en-GB.json +110 -0
  222. package/frontend-vue/package.json +38 -0
  223. package/frontend-vue/pnpm-lock.yaml +2697 -0
  224. package/package.json +115 -3
@@ -0,0 +1,142 @@
1
+ import type { ChangeKind, ChangedFile } from '../types/publishing'
2
+
3
+ const CHANGE_KINDS: readonly ChangeKind[] = ['added', 'modified', 'deleted']
4
+
5
+ /** The letter, tone and group title the list draws for each kind. */
6
+ export const KIND_PRESENTATION: Record<
7
+ ChangeKind,
8
+ { letter: string; title: string; badge: string }
9
+ > = {
10
+ added: {
11
+ letter: 'A',
12
+ title: 'Added',
13
+ badge: 'bg-success/10 text-success ring-success/30',
14
+ },
15
+ modified: {
16
+ letter: 'M',
17
+ title: 'Modified',
18
+ badge: 'bg-primary/10 text-primary ring-primary/30',
19
+ },
20
+ deleted: {
21
+ letter: 'D',
22
+ title: 'Deleted',
23
+ badge: 'bg-error/10 text-error ring-error/30',
24
+ },
25
+ }
26
+
27
+ export interface ChangedFileGroup {
28
+ kind: ChangeKind
29
+ files: ChangedFile[]
30
+ }
31
+
32
+ function isCount(value: unknown): value is number | null {
33
+ return value === null || (typeof value === 'number' && Number.isFinite(value))
34
+ }
35
+
36
+ function isChangedFile(value: unknown): value is ChangedFile {
37
+ if (typeof value !== 'object' || value === null) return false
38
+ return (
39
+ 'kind' in value &&
40
+ 'path' in value &&
41
+ 'additions' in value &&
42
+ 'deletions' in value &&
43
+ (value.kind === 'added' ||
44
+ value.kind === 'modified' ||
45
+ value.kind === 'deleted') &&
46
+ typeof value.path === 'string' &&
47
+ isCount(value.additions) &&
48
+ isCount(value.deletions)
49
+ )
50
+ }
51
+
52
+ /** The well-formed files of an answer's array, in order; anything else is dropped. */
53
+ function parseChangedFiles(value: unknown): ChangedFile[] {
54
+ return Array.isArray(value) ? value.filter(isChangedFile) : []
55
+ }
56
+
57
+ /** The files of one named field of a route's answer. */
58
+ export function readFilesField(answer: unknown, field: string): ChangedFile[] {
59
+ if (typeof answer !== 'object' || answer === null) return []
60
+ return parseChangedFiles(
61
+ Object.hasOwn(answer, field) ? Reflect.get(answer, field) : undefined,
62
+ )
63
+ }
64
+
65
+ /** `+N` and `−M` sums of a list; a binary file counts for neither. */
66
+ export function sumLines(files: readonly ChangedFile[]): {
67
+ additions: number
68
+ deletions: number
69
+ } {
70
+ let additions = 0
71
+ let deletions = 0
72
+ for (const file of files) {
73
+ additions += file.additions ?? 0
74
+ deletions += file.deletions ?? 0
75
+ }
76
+ return { additions, deletions }
77
+ }
78
+
79
+ /**
80
+ * Splits the files into the Added, Modified and Deleted groups, in that order,
81
+ * skipping empty groups and keeping the order of the list inside each one. At
82
+ * most `limit` files are kept in all.
83
+ */
84
+ export function groupChangedFiles(
85
+ files: readonly ChangedFile[],
86
+ limit?: number,
87
+ ): ChangedFileGroup[] {
88
+ const kept = limit === undefined ? files : files.slice(0, Math.max(0, limit))
89
+ return CHANGE_KINDS.map((kind) => ({
90
+ kind,
91
+ files: kept.filter((file) => file.kind === kind),
92
+ })).filter((group) => group.files.length > 0)
93
+ }
94
+
95
+ /** A repo-relative path split into its dimmed directory (with trailing slash) and its basename. */
96
+ export function splitPath(path: string): { dir: string; base: string } {
97
+ const index = path.lastIndexOf('/')
98
+ return { dir: path.slice(0, index + 1), base: path.slice(index + 1) }
99
+ }
100
+
101
+ const PAGE_PARAM_TOKEN = /\{\{\s*params\.([^{}\s]+)\s*\}\}/g
102
+
103
+ /**
104
+ * Fills the `{{params.X}}` tokens of a URL with the page's route parameters,
105
+ * encoded. Answers `null` while a token names a parameter the page lacks, so
106
+ * nothing is read from a half-filled URL.
107
+ */
108
+ export function fillPageParams(
109
+ url: string,
110
+ params: Readonly<Record<string, string>>,
111
+ ): string | null {
112
+ let missing = false
113
+ const filled = url.replace(PAGE_PARAM_TOKEN, (_, name: string) => {
114
+ const value = Object.hasOwn(params, name) ? params[name] : ''
115
+ if (value === '') missing = true
116
+ return encodeURIComponent(value)
117
+ })
118
+ return missing ? null : filled
119
+ }
120
+
121
+ /**
122
+ * The parameters a path gives a route pattern such as `/version/:number`, or
123
+ * `null` when the path does not match it.
124
+ */
125
+ export function matchRouteParams(
126
+ pattern: string,
127
+ path: string,
128
+ ): Record<string, string> | null {
129
+ const patternSegments = pattern.split('/').filter(Boolean)
130
+ const pathSegments = path.split('/').filter(Boolean)
131
+ if (patternSegments.length !== pathSegments.length) return null
132
+ const params: Record<string, string> = {}
133
+ for (const [index, segment] of patternSegments.entries()) {
134
+ const value = pathSegments[index] ?? ''
135
+ if (segment.startsWith(':')) {
136
+ params[segment.slice(1)] = decodeURIComponent(value)
137
+ } else if (segment !== value) {
138
+ return null
139
+ }
140
+ }
141
+ return params
142
+ }
@@ -0,0 +1,15 @@
1
+ /** A log line as a list shows it: when, how loud, where from, what. */
2
+ export interface LogLineView {
3
+ readonly at: string
4
+ readonly level: string
5
+ readonly message: string
6
+ readonly instance: string | null
7
+ }
8
+
9
+ /** `HH:MM:SS` on a 24-hour clock, in the reader's time zone. */
10
+ export const LOG_TIME = new Intl.DateTimeFormat(undefined, {
11
+ hour: '2-digit',
12
+ minute: '2-digit',
13
+ second: '2-digit',
14
+ hourCycle: 'h23',
15
+ })
@@ -0,0 +1,361 @@
1
+ /** A line as the relay sends it, with the Cloud cursor it came with. */
2
+ export interface LogEntry {
3
+ readonly cursor: string
4
+ readonly at: string
5
+ readonly level: string
6
+ readonly message: string
7
+ readonly instance: string | null
8
+ }
9
+
10
+ /** The relay's stream statuses, as `src/logs/relay.ts` sends them. */
11
+ type RelayStatus =
12
+ 'live' | 'sleeping' | 'busy' | 'not-deployed' | 'ended' | 'refused'
13
+
14
+ /** Why a stream closed for good. */
15
+ type ClosedReason = 'not-deployed' | 'ended' | 'refused'
16
+
17
+ /**
18
+ * What the viewer shows above the lines. `reconnecting` keeps when the
19
+ * stream dropped, for "Stream dropped at HH:MM:SS".
20
+ */
21
+ type Connection =
22
+ | { readonly kind: 'connecting' }
23
+ | { readonly kind: 'live' }
24
+ | { readonly kind: 'sleeping' }
25
+ | { readonly kind: 'reconnecting'; readonly droppedAt: Date }
26
+ | { readonly kind: 'busy'; readonly retryAt: Date }
27
+ | { readonly kind: 'closed'; readonly reason: ClosedReason }
28
+
29
+ /** All, Warn+ or Error. */
30
+ export type LevelFilter = 'all' | 'warn' | 'error'
31
+
32
+ /** The viewer's whole state. */
33
+ export interface LogState {
34
+ /** Oldest first, at most {@link MAX_LINES}. */
35
+ readonly lines: readonly LogEntry[]
36
+ /** The cursor to resume from: the last line's, kept when old lines drop out. */
37
+ readonly cursor: string | null
38
+ readonly connection: Connection
39
+ readonly level: LevelFilter
40
+ readonly search: string
41
+ }
42
+
43
+ /** Each kind of change to the viewer's state, and what it carries. */
44
+ export interface LogActions {
45
+ readonly line: { readonly entry: LogEntry }
46
+ readonly status: {
47
+ readonly status: RelayStatus
48
+ readonly now: Date
49
+ readonly retryMs: number | null
50
+ }
51
+ readonly dropped: { readonly now: Date }
52
+ readonly filter: {
53
+ readonly level?: LevelFilter
54
+ readonly search?: string
55
+ }
56
+ readonly 'clear-filters': Record<never, never>
57
+ readonly 'source-changed': Record<never, never>
58
+ }
59
+
60
+ /** One change of a given kind. */
61
+ export type LogActionOf<K extends keyof LogActions> = {
62
+ readonly type: K
63
+ } & LogActions[K]
64
+
65
+ /** What changes the viewer's state. */
66
+ export type LogAction = {
67
+ [K in keyof LogActions]: LogActionOf<K>
68
+ }[keyof LogActions]
69
+
70
+ /** The lines the viewer keeps; older ones drop out. */
71
+ export const MAX_LINES = 2000
72
+
73
+ /** A viewer that has not connected yet. */
74
+ export const INITIAL_LOG_STATE: LogState = {
75
+ lines: [],
76
+ cursor: null,
77
+ connection: { kind: 'connecting' },
78
+ level: 'all',
79
+ search: '',
80
+ }
81
+
82
+ const CLOSED_STATUSES = new Set<RelayStatus>([
83
+ 'not-deployed',
84
+ 'ended',
85
+ 'refused',
86
+ ])
87
+
88
+ const LEVELS_SHOWN: Readonly<Record<LevelFilter, ReadonlySet<string> | null>> =
89
+ {
90
+ all: null,
91
+ warn: new Set(['warn', 'warning', 'error', 'fatal']),
92
+ error: new Set(['error', 'fatal']),
93
+ }
94
+
95
+ function isClosedReason(status: RelayStatus): status is ClosedReason {
96
+ return CLOSED_STATUSES.has(status)
97
+ }
98
+
99
+ function connectionOf(
100
+ status: RelayStatus,
101
+ now: Date,
102
+ retryMs: number | null,
103
+ ): Connection {
104
+ if (isClosedReason(status)) return { kind: 'closed', reason: status }
105
+ if (status === 'busy') {
106
+ return { kind: 'busy', retryAt: new Date(now.getTime() + (retryMs ?? 0)) }
107
+ }
108
+ return { kind: status }
109
+ }
110
+
111
+ type Handlers = {
112
+ readonly [K in keyof LogActions]: (
113
+ state: LogState,
114
+ action: LogActionOf<K>,
115
+ ) => LogState
116
+ }
117
+
118
+ const HANDLERS: Handlers = {
119
+ line: (state, { entry }) => {
120
+ if (entry.cursor === state.cursor) return state
121
+ return {
122
+ ...state,
123
+ lines: [...state.lines, entry].slice(-MAX_LINES),
124
+ cursor: entry.cursor,
125
+ connection:
126
+ state.connection.kind === 'sleeping'
127
+ ? state.connection
128
+ : { kind: 'live' },
129
+ }
130
+ },
131
+ status: (state, { status, now, retryMs }) => ({
132
+ ...state,
133
+ connection: connectionOf(status, now, retryMs),
134
+ }),
135
+ dropped: (state, { now }) => {
136
+ if (state.connection.kind === 'closed') return state
137
+ const droppedAt =
138
+ state.connection.kind === 'reconnecting'
139
+ ? state.connection.droppedAt
140
+ : now
141
+ return { ...state, connection: { kind: 'reconnecting', droppedAt } }
142
+ },
143
+ filter: (state, { level, search }) => ({
144
+ ...state,
145
+ level: level ?? state.level,
146
+ search: search ?? state.search,
147
+ }),
148
+ 'clear-filters': (state) => ({ ...state, level: 'all', search: '' }),
149
+ 'source-changed': (state) => ({
150
+ ...INITIAL_LOG_STATE,
151
+ level: state.level,
152
+ search: state.search,
153
+ }),
154
+ }
155
+
156
+ /**
157
+ * The next state. A line puts the stream back to live, unless the relay said
158
+ * the service sleeps: the lines it printed before still arrive then. A line
159
+ * whose cursor is the last one is a replay and changes nothing. A drop keeps
160
+ * the time of the first drop until a line arrives.
161
+ */
162
+ export function reduceLog<K extends keyof LogActions>(
163
+ state: LogState,
164
+ action: LogActionOf<K>,
165
+ ): LogState {
166
+ return HANDLERS[action.type](state, action)
167
+ }
168
+
169
+ function passes(
170
+ entry: LogEntry,
171
+ levels: ReadonlySet<string> | null,
172
+ search: string,
173
+ ): boolean {
174
+ if (levels && !levels.has(entry.level.toLowerCase())) return false
175
+ return search === '' || entry.message.toLowerCase().includes(search)
176
+ }
177
+
178
+ /**
179
+ * The lines the filters keep, and why there are none when there are none:
180
+ * `no-lines` before the first line, `no-match` once lines arrived and the
181
+ * filters hide them all.
182
+ */
183
+ export function visibleLines(state: LogState): {
184
+ readonly lines: readonly LogEntry[]
185
+ readonly empty: 'no-lines' | 'no-match' | null
186
+ } {
187
+ if (state.lines.length === 0) return { lines: [], empty: 'no-lines' }
188
+ const search = state.search.trim().toLowerCase()
189
+ const lines = state.lines.filter((entry) =>
190
+ passes(entry, LEVELS_SHOWN[state.level], search),
191
+ )
192
+ return { lines, empty: lines.length === 0 ? 'no-match' : null }
193
+ }
194
+
195
+ /** One SSE request, as the DMS's `openSseStream` opens it. */
196
+ export interface SseRequest {
197
+ url: string
198
+ onEvent: (eventName: string, data: unknown, id: string | null) => void
199
+ onError?: (error: unknown) => void
200
+ onClose?: (reason: 'eof' | 'error' | 'aborted') => void
201
+ }
202
+
203
+ /** What {@link followLogs} needs; `open` is `openSseStream` in the app. */
204
+ export interface FollowOptions {
205
+ readonly url: string
206
+ readonly open: (request: SseRequest) => { close(): void }
207
+ readonly dispatch: (action: LogAction) => void
208
+ /** The cursor to resume from, read at each attempt and sent as `after`. */
209
+ readonly cursor: () => string | null
210
+ readonly wait: (ms: number) => Promise<void>
211
+ readonly random?: () => number
212
+ }
213
+
214
+ const DROP_DELAY_MS = 250
215
+ const MAX_DROP_DELAY_MS = 5000
216
+ const BUSY_DELAY_MS = 1000
217
+ const MAX_BUSY_DELAY_MS = 30_000
218
+ const HTTP_CLIENT_ERROR = 400
219
+ const HTTP_TOO_MANY_REQUESTS = 429
220
+ const HTTP_SERVER_ERROR = 500
221
+
222
+ function isRecord(value: unknown): value is Record<string, unknown> {
223
+ return typeof value === 'object' && value !== null
224
+ }
225
+
226
+ function entryOf(data: unknown, cursor: string | null): LogEntry | null {
227
+ if (!isRecord(data) || cursor === null) return null
228
+ const { at, level, message, instance } = data
229
+ if (
230
+ typeof at !== 'string' ||
231
+ typeof level !== 'string' ||
232
+ typeof message !== 'string'
233
+ ) {
234
+ return null
235
+ }
236
+ return {
237
+ cursor,
238
+ at,
239
+ level,
240
+ message,
241
+ instance: typeof instance === 'string' ? instance : null,
242
+ }
243
+ }
244
+
245
+ const RELAY_STATUSES = new Set<string>([
246
+ 'live',
247
+ 'sleeping',
248
+ 'busy',
249
+ ...CLOSED_STATUSES,
250
+ ])
251
+
252
+ function isRelayStatus(value: unknown): value is RelayStatus {
253
+ return typeof value === 'string' && RELAY_STATUSES.has(value)
254
+ }
255
+
256
+ function statusOf(
257
+ data: unknown,
258
+ ): { status: RelayStatus; retryMs: number | null } | null {
259
+ if (!isRecord(data) || !isRelayStatus(data.status)) return null
260
+ return {
261
+ status: data.status,
262
+ retryMs: typeof data.retryMs === 'number' ? data.retryMs : null,
263
+ }
264
+ }
265
+
266
+ function withCursor(url: string, cursor: string | null): string {
267
+ if (cursor === null) return url
268
+ const separator = url.includes('?') ? '&' : '?'
269
+ return `${url}${separator}after=${encodeURIComponent(cursor)}`
270
+ }
271
+
272
+ function isRefusal(error: unknown): boolean {
273
+ if (!isRecord(error) || typeof error.status !== 'number') return false
274
+ return (
275
+ error.status >= HTTP_CLIENT_ERROR &&
276
+ error.status < HTTP_SERVER_ERROR &&
277
+ error.status !== HTTP_TOO_MANY_REQUESTS
278
+ )
279
+ }
280
+
281
+ /**
282
+ * Opens the stream and keeps it open: each attempt sends the last cursor as
283
+ * `after`, so the relay replays what was missed. After a drop it
284
+ * waits 250 ms, doubling to 5 s; after `busy` it waits the relay's
285
+ * `retryMs` at least, and a jittered 1 s to 30 s backoff, the CLI's policy.
286
+ * A closing status or an HTTP refusal ends it for good. Returns `stop`.
287
+ */
288
+ export function followLogs(options: FollowOptions): () => void {
289
+ const random = options.random ?? Math.random
290
+ let stopped = false
291
+ let handle: { close(): void } | null = null
292
+ let dropDelay = DROP_DELAY_MS
293
+ let busyDelay: number | null = null
294
+
295
+ const later = (ms: number) => {
296
+ void options.wait(ms).then(connect)
297
+ }
298
+
299
+ const afterBusy = (retryMs: number | null) => {
300
+ busyDelay =
301
+ busyDelay === null
302
+ ? BUSY_DELAY_MS
303
+ : Math.min(busyDelay * 2, MAX_BUSY_DELAY_MS)
304
+ later(
305
+ Math.max(
306
+ Math.min(retryMs ?? 0, MAX_BUSY_DELAY_MS),
307
+ busyDelay / 2 + (random() * busyDelay) / 2,
308
+ ),
309
+ )
310
+ }
311
+
312
+ const afterDrop = () => {
313
+ options.dispatch({ type: 'dropped', now: new Date() })
314
+ later(dropDelay)
315
+ dropDelay = Math.min(dropDelay * 2, MAX_DROP_DELAY_MS)
316
+ }
317
+
318
+ function connect(): void {
319
+ if (stopped) return
320
+ let last: { status: RelayStatus; retryMs: number | null } | null = null
321
+ handle = options.open({
322
+ url: withCursor(options.url, options.cursor()),
323
+ onEvent: (eventName, data, id) => {
324
+ if (eventName === 'line') {
325
+ const entry = entryOf(data, id)
326
+ if (!entry) return
327
+ dropDelay = DROP_DELAY_MS
328
+ busyDelay = null
329
+ options.dispatch({ type: 'line', entry })
330
+ return
331
+ }
332
+ const status = eventName === 'status' ? statusOf(data) : null
333
+ if (!status) return
334
+ last = status
335
+ options.dispatch({ type: 'status', now: new Date(), ...status })
336
+ },
337
+ onError: (error) => {
338
+ if (!isRefusal(error)) return
339
+ last = { status: 'refused', retryMs: null }
340
+ options.dispatch({
341
+ type: 'status',
342
+ status: 'refused',
343
+ now: new Date(),
344
+ retryMs: null,
345
+ })
346
+ },
347
+ onClose: (reason) => {
348
+ if (stopped || reason === 'aborted') return
349
+ if (last && isClosedReason(last.status)) return
350
+ if (last?.status === 'busy') afterBusy(last.retryMs)
351
+ else afterDrop()
352
+ },
353
+ })
354
+ }
355
+
356
+ connect()
357
+ return () => {
358
+ stopped = true
359
+ handle?.close()
360
+ }
361
+ }