@lango-project/lango-client 0.0.0-stage → 0.2.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.
Files changed (112) hide show
  1. package/LICENSE +200 -0
  2. package/NOTICE +2 -0
  3. package/README.md +929 -2
  4. package/dist/.lango/bignumber.mjs +3119 -0
  5. package/dist/.lango/runtime.mjs +607 -0
  6. package/dist/.lango/xmldom/conventions.js +429 -0
  7. package/dist/.lango/xmldom/dom-parser.js +591 -0
  8. package/dist/.lango/xmldom/dom.js +3759 -0
  9. package/dist/.lango/xmldom/entities.js +2171 -0
  10. package/dist/.lango/xmldom/errors.js +205 -0
  11. package/dist/.lango/xmldom/grammar.js +559 -0
  12. package/dist/.lango/xmldom/index.js +41 -0
  13. package/dist/.lango/xmldom/package.json +1 -0
  14. package/dist/.lango/xmldom/sax.js +983 -0
  15. package/dist/.lango/xmldom.LICENSE.md +8 -0
  16. package/dist/application.mjs +429 -0
  17. package/dist/application.mjs.map +1 -0
  18. package/dist/coordinator.mjs +91 -0
  19. package/dist/coordinator.mjs.map +1 -0
  20. package/dist/demo.mjs +110 -0
  21. package/dist/demo.mjs.map +1 -0
  22. package/dist/demo_controls.mjs +59 -0
  23. package/dist/demo_controls.mjs.map +1 -0
  24. package/dist/errors.mjs +201 -0
  25. package/dist/errors.mjs.map +1 -0
  26. package/dist/form_state.mjs +266 -0
  27. package/dist/form_state.mjs.map +1 -0
  28. package/dist/heartbeat.mjs +330 -0
  29. package/dist/heartbeat.mjs.map +1 -0
  30. package/dist/inheritance.mjs +240 -0
  31. package/dist/inheritance.mjs.map +1 -0
  32. package/dist/input_values.mjs +65 -0
  33. package/dist/input_values.mjs.map +1 -0
  34. package/dist/library.mjs +824 -0
  35. package/dist/library.mjs.map +1 -0
  36. package/dist/navigation.mjs +466 -0
  37. package/dist/navigation.mjs.map +1 -0
  38. package/dist/node/.lango/bignumber.mjs +3119 -0
  39. package/dist/node/.lango/runtime.mjs +609 -0
  40. package/dist/node/.lango/xmldom/conventions.js +429 -0
  41. package/dist/node/.lango/xmldom/dom-parser.js +591 -0
  42. package/dist/node/.lango/xmldom/dom.js +3759 -0
  43. package/dist/node/.lango/xmldom/entities.js +2171 -0
  44. package/dist/node/.lango/xmldom/errors.js +205 -0
  45. package/dist/node/.lango/xmldom/grammar.js +559 -0
  46. package/dist/node/.lango/xmldom/index.js +41 -0
  47. package/dist/node/.lango/xmldom/package.json +1 -0
  48. package/dist/node/.lango/xmldom/sax.js +983 -0
  49. package/dist/node/.lango/xmldom.LICENSE.md +8 -0
  50. package/dist/node/browser_host.mjs +351 -0
  51. package/dist/node/browser_host.mjs.map +1 -0
  52. package/dist/node/create_app.mjs +118 -0
  53. package/dist/node/create_app.mjs.map +1 -0
  54. package/dist/presentation.mjs +89 -0
  55. package/dist/presentation.mjs.map +1 -0
  56. package/dist/protocol.mjs +706 -0
  57. package/dist/protocol.mjs.map +1 -0
  58. package/dist/session.mjs +471 -0
  59. package/dist/session.mjs.map +1 -0
  60. package/dist/tokens.mjs +298 -0
  61. package/dist/tokens.mjs.map +1 -0
  62. package/examples/contacts.lango +293 -0
  63. package/examples/contacts_flow.lango +174 -0
  64. package/examples/contacts_store.lango +109 -0
  65. package/examples/contacts_store_test.lango +65 -0
  66. package/examples/demo.css +87 -0
  67. package/examples/demo_test.lango +356 -0
  68. package/examples/index.html +32 -0
  69. package/examples/pages.lango +174 -0
  70. package/examples/server.lango +189 -0
  71. package/examples/worker.lango +142 -0
  72. package/package.json +68 -4
  73. package/src/application.lango +327 -0
  74. package/src/browser_host.lango +276 -0
  75. package/src/coordinator.lango +57 -0
  76. package/src/demo.lango +110 -0
  77. package/src/demo_controls.lango +53 -0
  78. package/src/errors.lango +149 -0
  79. package/src/errors_test.lango +157 -0
  80. package/src/form_state.lango +168 -0
  81. package/src/heartbeat.lango +230 -0
  82. package/src/heartbeat_test.lango +479 -0
  83. package/src/inheritance.lango +167 -0
  84. package/src/input_values.lango +37 -0
  85. package/src/library.lango +599 -0
  86. package/src/logout_test.lango +564 -0
  87. package/src/navigation.lango +309 -0
  88. package/src/navigation_test.lango +556 -0
  89. package/src/presentation.lango +55 -0
  90. package/src/protocol.lango +787 -0
  91. package/src/renderer_test.lango +2047 -0
  92. package/src/session.lango +343 -0
  93. package/src/session_test.lango +455 -0
  94. package/src/tabs_test.lango +216 -0
  95. package/src/tokens.lango +215 -0
  96. package/template/.vscode/extensions.json +3 -0
  97. package/template/.vscode/settings.json +9 -0
  98. package/template/README.md +44 -0
  99. package/template/gitignore +4 -0
  100. package/template/package.json +22 -0
  101. package/template/public/app.css +10 -0
  102. package/template/public/index.html +24 -0
  103. package/template/src/client.lango +78 -0
  104. package/template/src/pages.lango +55 -0
  105. package/template/src/server.lango +24 -0
  106. package/template/src/worker.lango +77 -0
  107. package/template/test/app.lango +9 -0
  108. package/tools/browser_runner.lango +32 -0
  109. package/tools/browser_test.lango +343 -0
  110. package/tools/create_app.lango +88 -0
  111. package/tools/create_app_test.lango +43 -0
  112. package/tools/starter_test.lango +227 -0
@@ -0,0 +1,327 @@
1
+ import js global Promise
2
+ import "./library"
3
+ mount(document, root, definition, types = null)
4
+ class Renderer
5
+ class Binding
6
+ class Types
7
+ import "./session"
8
+ class Session
9
+ import "./tokens"
10
+ browserTokenStore(key = "lango:last-token")
11
+ import "./heartbeat"
12
+ class Heartbeat
13
+ import "./inheritance"
14
+ inheritedControls(form, parent)
15
+ inheritanceResources(path, parentPath, spec)
16
+ import "./coordinator"
17
+ class Coordinator
18
+
19
+ export class Application
20
+ properties
21
+ control
22
+ methods
23
+ reconcile()
24
+ session(formId)
25
+ dispose()
26
+ logout()
27
+ pause()
28
+ revokeSession()
29
+ rememberRevocation(previousToken)
30
+ restartAnonymous()
31
+ resume(token)
32
+ finishLogout()
33
+ createHeartbeat()
34
+ controlsFor(form)
35
+ isDirty()
36
+ confirmDiscard(window)
37
+
38
+ Application(document, root, definition, options = null)
39
+ if options == null
40
+ options = {
41
+ }
42
+ types = options.types
43
+ if types == null
44
+ types = new Types()
45
+ @control = mount(document, root, definition, types)
46
+ @renderer = new Renderer(document, types)
47
+ @transport = options.transport
48
+ @errors = options.errors
49
+ @tokens = options.tokens
50
+ if @tokens == null
51
+ key = options.storageKey
52
+ if key == null
53
+ key = "lango:last-token"
54
+ @tokens = browserTokenStore(key)
55
+ if options.bootstrapPath != null
56
+ @tokens.initialize(options.bootstrapPath)
57
+ @pageToken = @tokens.latest()
58
+ @bindings = []
59
+ @coordinator = new Coordinator()
60
+ @disposed = false
61
+ @window = options.window
62
+ @unload = null
63
+ @paused = false
64
+ @logout = null
65
+ @revoked = false
66
+ @restartToken = null
67
+ @bootstrapPath = options.bootstrapPath
68
+ if @bootstrapPath == null
69
+ @bootstrapPath = "/_lango/start"
70
+ @logoutPath = options.logoutPath
71
+ if @logoutPath == null
72
+ @logoutPath = "/_lango/logout"
73
+ @heartbeat = null
74
+ pingEnabled = options.pingEnabled
75
+ if pingEnabled == null
76
+ pingEnabled = options.bootstrapPath != null or options.pingPath != null
77
+ if !is_bool(pingEnabled)
78
+ throw "UiPingEnabledMustBeBoolean"
79
+ @pingEnabled = pingEnabled
80
+ @heartbeatOptions = {
81
+ path: options.pingPath
82
+ idleMs: options.pingIdleMs
83
+ transport: options.pingTransport
84
+ }
85
+ createHeartbeat()
86
+ reconcile()
87
+ if @heartbeat != null
88
+ @heartbeat.start()
89
+ observeStartup(@heartbeat, @tokens)
90
+ if @window != null
91
+ unload = (event)
92
+ if isDirty()
93
+ event.preventDefault()
94
+ event.returnValue = ""
95
+ @unload = unload
96
+ @window.addEventListener("beforeunload", unload)
97
+
98
+ Application:createHeartbeat()
99
+ if @pingEnabled
100
+ @heartbeat = new Heartbeat(@heartbeatOptions)
101
+ return null
102
+
103
+ Application:session(formId)
104
+ id = formId
105
+ if is_array(formId)
106
+ id = managedFormId(formId)
107
+ for @bindings as entry
108
+ if entry.formId == id
109
+ return entry.session
110
+ return null
111
+
112
+ Application:reconcile()
113
+ if @disposed or @paused
114
+ return null
115
+ forms = []
116
+ collectForms(@control, [], forms)
117
+ for forms as form
118
+ form.resources = inheritanceResources(form.path, form.parentPath, form.control.inherit)
119
+ retained = []
120
+ for @bindings as entry
121
+ if retainsForm(forms, entry)
122
+ retained.add(entry)
123
+ else
124
+ entry.binding.dispose()
125
+ entry.session.dispose()
126
+ @bindings = retained
127
+ for forms as form
128
+ if not containsForm(@bindings, form.control)
129
+ session = new Session({
130
+ formId: form.formId
131
+ controls: form.control.controls
132
+ root: form.control.element
133
+ effects: @renderer
134
+ transport: @transport
135
+ lifecycle: @this
136
+ tokens: @tokens
137
+ heartbeat: @heartbeat
138
+ coordinator: @coordinator
139
+ resources: form.resources
140
+ form: form.control
141
+ parent: form.parent
142
+ window: @window
143
+ seedFromBootstrap: true
144
+ seedToken: @pageToken
145
+ })
146
+ binding = new Binding(form.control, session, @errors)
147
+ @bindings.add({
148
+ formId: form.formId
149
+ control: form.control
150
+ parent: form.parent
151
+ session: session
152
+ binding: binding
153
+ })
154
+ if @heartbeat != null
155
+ sessions = []
156
+ for @bindings as entry
157
+ sessions.add(entry.session)
158
+ @heartbeat.sessions(sessions)
159
+ return null
160
+
161
+ Application:controlsFor(form)
162
+ for @bindings as entry
163
+ if entry.control == form
164
+ return inheritedControls(form, entry.parent)
165
+ throw "UiSessionDisposed"
166
+
167
+ Application:dispose()
168
+ if @disposed
169
+ return null
170
+ @disposed = true
171
+ if @window != null
172
+ @window.removeEventListener("beforeunload", @unload)
173
+ if @paused and @tokens.cancelInitialization != null
174
+ @tokens.cancelInitialization()
175
+ if @heartbeat != null
176
+ @heartbeat.dispose()
177
+ for @bindings as entry
178
+ entry.binding.dispose()
179
+ entry.session.dispose()
180
+ @bindings = []
181
+ @renderer.release(@control)
182
+ return null
183
+
184
+ Application:isDirty()
185
+ if @disposed
186
+ return false
187
+ for @bindings as entry
188
+ if entry.session.isDirty()
189
+ return true
190
+ return false
191
+
192
+ Application:confirmDiscard(window)
193
+ if !isDirty()
194
+ return true
195
+ return window.confirm("You have unsaved changes. Discard them?")
196
+
197
+ Application:logout()
198
+ if @disposed
199
+ throw "UiApplicationDisposed"
200
+ if @logout != null
201
+ return @logout
202
+ if @tokens.logout == null or @tokens.cancelInitialization == null
203
+ throw "UiLogoutRequiresTokenStore"
204
+ pause()
205
+ ready = Promise/resolve()
206
+ revoked = ready.then(()
207
+ return revokeSession()
208
+ )
209
+ restarted = revoked.then(()
210
+ return restartAnonymous()
211
+ )
212
+ work = restarted.then((token)
213
+ return resume(token)
214
+ )
215
+ pending = work.finally(()
216
+ finishLogout()
217
+ )
218
+ @logout = pending
219
+ return pending
220
+
221
+ Application:pause()
222
+ @paused = true
223
+ if @heartbeat != null
224
+ @heartbeat.dispose()
225
+ for @bindings as entry
226
+ entry.binding.dispose()
227
+ entry.session.dispose()
228
+ @bindings = []
229
+ @control.element.inert = true
230
+ @control.element.setAttribute("aria-busy", "true")
231
+ return null
232
+
233
+ Application:revokeSession()
234
+ if @disposed
235
+ throw "UiApplicationDisposed"
236
+ if @revoked
237
+ return null
238
+ return @tokens.logout(@logoutPath).then((previousToken)
239
+ rememberRevocation(previousToken)
240
+ )
241
+
242
+ Application:rememberRevocation(previousToken)
243
+ @revoked = true
244
+ @restartToken = previousToken
245
+ return null
246
+
247
+ Application:restartAnonymous()
248
+ if @disposed
249
+ throw "UiApplicationDisposed"
250
+ return @tokens.initialize(@bootstrapPath, @restartToken)
251
+
252
+ Application:resume(token)
253
+ if @disposed
254
+ throw "UiApplicationDisposed"
255
+ @revoked = false
256
+ @restartToken = null
257
+ @pageToken = token
258
+ @paused = false
259
+ createHeartbeat()
260
+ reconcile()
261
+ @control.element.inert = false
262
+ @control.element.setAttribute("aria-busy", "false")
263
+ if @heartbeat != null
264
+ @heartbeat.start()
265
+ return token
266
+
267
+ Application:finishLogout()
268
+ @logout = null
269
+ return null
270
+
271
+ containsForm(forms, control)
272
+ for forms as form
273
+ if form.control == control
274
+ return true
275
+ return false
276
+
277
+ retainsForm(forms, entry)
278
+ for forms as form
279
+ if form.control == entry.control and form.formId == entry.formId and form.parent == entry.parent
280
+ return true
281
+ return false
282
+
283
+ collectForms(control, parentPath, forms, parentForm = null, parentFormPath = null)
284
+ path = parentPath.slice()
285
+ nearest = parentForm
286
+ nearestPath = parentFormPath
287
+ if control.name != null and control.name != ""
288
+ path.add(control.name)
289
+ if control.element.langoFormRoot == true
290
+ if control.name == null or control.name == ""
291
+ throw "UiManagedFormRequiresName"
292
+ formId = managedFormId(path)
293
+ for forms as existing
294
+ if existing.formId == formId
295
+ throw "UiDuplicateFormPath"
296
+ forms.add({
297
+ formId: formId
298
+ control: control
299
+ path: path
300
+ parent: parentForm
301
+ parentPath: parentFormPath
302
+ })
303
+ nearest = control
304
+ nearestPath = path
305
+ if is_array(control.controls)
306
+ for control.controls as child
307
+ collectForms(child, path, forms, nearest, nearestPath)
308
+ return null
309
+
310
+ managedFormId(path)
311
+ if path.length == 1
312
+ return path[0]
313
+ return to_json(path)
314
+
315
+ observeStartup(heartbeat, tokens)
316
+ if tokens.ready == null
317
+ return null
318
+ ready = tokens.ready()
319
+ observed = ready.then(()
320
+ capture heartbeat
321
+ heartbeat.activity()
322
+ )
323
+ observed.catch((error)
324
+ capture heartbeat
325
+ heartbeat.activity()
326
+ )
327
+ return null
@@ -0,0 +1,276 @@
1
+ import js "node:fs" as fs
2
+ import js "node:path" as path
3
+ import js "node:http" as http
4
+ import js "node:crypto" as crypto
5
+ import js "node:module" as modules
6
+ import js global fetch
7
+ import js global process
8
+ import js global Promise
9
+ import js global URL
10
+ import js "@lango-project/lango-api" as api
11
+ import js "@lango-project/lango-api/http-input" as input
12
+ import js "@lango-project/lango-api/rate-limit" as rates
13
+
14
+ export class BrowserHost
15
+ methods
16
+ start()
17
+ close()
18
+
19
+ BrowserHost(options)
20
+ @options = options
21
+ @bodyOptions = input/requestBodyOptions(options)
22
+ @rateLimiter = new rates/RateLimiter(options)
23
+ @root = path.resolve(options.appRoot)
24
+ @pages = options.pages
25
+ @port = options.port
26
+ @apiPort = options.apiPort
27
+ if @port == null
28
+ @port = 8080
29
+ if @apiPort == null
30
+ @apiPort = 8081
31
+ if !is_number(@port) or !is_number(@apiPort) or @port < 1 or @port > 65535 or @apiPort < 1 or @apiPort > 65535 or @port % 1 != 0 or @apiPort % 1 != 0 or @port == @apiPort
32
+ throw "UiHostRequiresDistinctPorts"
33
+ @assets = {
34
+ }
35
+ @server = null
36
+ @api = null
37
+ @closing = null
38
+ @starting = null
39
+ @signal = null
40
+ @closed = false
41
+
42
+ BrowserHost:start()
43
+ if @closed
44
+ throw "UiHostClosed"
45
+ if @starting != null
46
+ return @starting
47
+ @starting = launch()
48
+ return @starting
49
+
50
+ BrowserHost:launch()
51
+ options = @options
52
+ resolve = modules/createRequire(path.join(@root, "package.json"))
53
+ sdk = path.dirname(resolve.resolve("@lango-project/lango-client"))
54
+ files = ["library.mjs", "application.mjs", "session.mjs", "tokens.mjs", "heartbeat.mjs", "protocol.mjs", "inheritance.mjs", "coordinator.mjs", "navigation.mjs", "presentation.mjs", "input_values.mjs", "form_state.mjs", "errors.mjs", ".lango/runtime.mjs", ".lango/bignumber.mjs"]
55
+ for files as file
56
+ addAsset("/vendor/lango-client/${file}", path.join(sdk, file))
57
+ if options.assets != null
58
+ for options.assets as url: file
59
+ if url.startsWith("/vendor/")
60
+ throw "UiHostReservedAssetPath"
61
+ addAsset(url, path.resolve(@root, file))
62
+ @shell = fs.readFileSync(path.resolve(@root, options.shell))
63
+ state = path.join(@root, ".lango", "state")
64
+ fs.mkdirSync(state, {
65
+ recursive: true
66
+ })
67
+ keyFile = path.join(state, "jwt.key")
68
+ if !fs.existsSync(keyFile)
69
+ fs.writeFileSync(keyFile, crypto/randomBytes(32), {
70
+ mode: 384
71
+ flag: "wx"
72
+ })
73
+ keys = path.join(state, "keys.json")
74
+ key = fs.readFileSync(keyFile)
75
+ fs.writeFileSync(keys, to_json({
76
+ app: key.toString("base64")
77
+ }), {
78
+ mode: 384
79
+ })
80
+ @api = new api/Host({
81
+ applicationName: "lango-application"
82
+ appRoot: @root
83
+ stateDir: state
84
+ mode: "server"
85
+ host: "127.0.0.1"
86
+ port: @apiPort
87
+ workers: {
88
+ bodyLimit: @bodyOptions.bodyLimit
89
+ bodyTimeoutMs: @bodyOptions.bodyTimeoutMs
90
+ application: path.resolve(@root, options.worker)
91
+ workerFile: resolve.resolve("@lango-project/lango-api/worker-entry")
92
+ }
93
+ jwt: {
94
+ issuer: "lango-application"
95
+ audience: "lango-browser"
96
+ activeKeyId: "app"
97
+ keysFile: keys
98
+ }
99
+ })
100
+ return @api.start().then(()
101
+ if @closed
102
+ throw "UiHostClosed"
103
+ return listen()
104
+ ).catch((error)
105
+ return close().then(()
106
+ capture error
107
+ throw error
108
+ )
109
+ )
110
+
111
+ BrowserHost:addAsset(url, file)
112
+ if !is_string(url) or !url.startsWith("/") or url.includes("?") or url.includes("#") or url.includes("\\")
113
+ throw "UiHostInvalidAssetPath"
114
+ if url in @assets
115
+ throw "UiHostDuplicateAsset"
116
+ type = "text/javascript; charset=utf-8"
117
+ if file.endsWith(".css")
118
+ type = "text/css; charset=utf-8"
119
+ content = fs.readFileSync(file)
120
+ hash = crypto/createHash("sha256")
121
+ hash.update(content)
122
+ @assets[url] = {
123
+ content: content
124
+ type: type
125
+ etag: "\"${hash.digest('hex')}\""
126
+ }
127
+
128
+ BrowserHost:listen()
129
+ server = new http/Server((req, res)
130
+ serve(req, res)
131
+ )
132
+ @server = server
133
+ pending = new Promise((resolve, reject)
134
+ server = @server
135
+ server.once("error", reject)
136
+ server.listen(@port, "127.0.0.1", ()
137
+ capture resolve
138
+ signal = ()
139
+ signalClose()
140
+ @signal = signal
141
+ process.once("SIGINT", @signal)
142
+ process.once("SIGTERM", @signal)
143
+ context log
144
+ log.info("Open http://localhost:${@port}/")
145
+ resolve(null)
146
+ )
147
+ )
148
+ return pending
149
+
150
+ BrowserHost:serve(req, res)
151
+ try
152
+ url = new URL(req.url, "http://localhost")
153
+ pathname = url.pathname
154
+ if req.method == "POST" and (pathname.startsWith("/app/") or pathname.startsWith("/_lango/"))
155
+ return receive(req, res)
156
+ if req.method != "GET"
157
+ res.writeHead(404)
158
+ res.end("Not found")
159
+ return null
160
+ if pathname in @assets
161
+ asset = @assets[pathname]
162
+ res.setHeader("Content-Type", asset.type)
163
+ res.setHeader("Cache-Control", "public, max-age=0, must-revalidate")
164
+ res.setHeader("ETag", asset.etag)
165
+ if req.headers["if-none-match"] == asset.etag
166
+ res.writeHead(304)
167
+ res.end()
168
+ else
169
+ res.writeHead(200)
170
+ res.end(asset.content)
171
+ return null
172
+ if pathname == "/" or @pages.match(pathname) != null
173
+ res.writeHead(200, {
174
+ "Content-Type": "text/html; charset=utf-8"
175
+ "Cache-Control": "no-store"
176
+ })
177
+ res.end(@shell)
178
+ return null
179
+ res.writeHead(404)
180
+ res.end("Not found")
181
+ catch error
182
+ res.writeHead(400)
183
+ res.end("Invalid request")
184
+ return null
185
+
186
+ BrowserHost:receive(req, res)
187
+ retry = @rateLimiter.admit(req.socket.remoteAddress)
188
+ if retry > 0
189
+ return input/rejectHttpRequest(req, res, 429, "TooManyRequests", retry, null)
190
+ completed = (body)
191
+ capture req
192
+ capture res
193
+ forward(req, res, body)
194
+ rejected = (status, error)
195
+ capture res
196
+ res.writeHead(status, {
197
+ "Content-Type": "application/json; charset=utf-8"
198
+ "Cache-Control": "no-store"
199
+ })
200
+ res.end(to_json({
201
+ error: error
202
+ }))
203
+ input/readRequestBody(req, res, @bodyOptions, completed, rejected)
204
+ return null
205
+
206
+ BrowserHost:forward(req, res, body)
207
+ headers = {
208
+ "Content-Type": "application/json"
209
+ }
210
+ if is_string(req.headers.authorization)
211
+ headers.Authorization = req.headers.authorization
212
+ return fetch("http://127.0.0.1:${@apiPort}${req.url}", {
213
+ method: "POST"
214
+ headers: headers
215
+ body: body
216
+ }).then((upstream)
217
+ capture res
218
+ return readUpstream(upstream, res)
219
+ ).catch((error)
220
+ capture res
221
+ res.writeHead(502, {
222
+ "Content-Type": "application/json"
223
+ })
224
+ res.end(to_json({
225
+ error: "UpstreamUnavailable"
226
+ }))
227
+ )
228
+
229
+ BrowserHost:readUpstream(upstream, res)
230
+ return upstream.text().then((text)
231
+ capture upstream
232
+ capture res
233
+ relay(res, upstream, text)
234
+ )
235
+
236
+ BrowserHost:relay(res, upstream, text)
237
+ for ["Content-Type", "Cache-Control", "X-Lango-Token", "WWW-Authenticate", "Retry-After"] as name
238
+ value = upstream.headers.get(name)
239
+ if value != null
240
+ res.setHeader(name, value)
241
+ res.writeHead(upstream.status)
242
+ res.end(text)
243
+ return null
244
+
245
+ BrowserHost:close()
246
+ if @closing != null
247
+ return @closing
248
+ @closed = true
249
+ if @signal != null
250
+ process.removeListener("SIGINT", @signal)
251
+ process.removeListener("SIGTERM", @signal)
252
+ @closing = stop()
253
+ return @closing
254
+
255
+ BrowserHost:signalClose()
256
+ return close().catch((error)
257
+ context log
258
+ log.err("Browser host shutdown failed: ${error}")
259
+ process.exitCode = 1
260
+ )
261
+
262
+ BrowserHost:stop()
263
+ pending = new Promise((resolve)
264
+ server = @server
265
+ if server == null or !server.listening
266
+ resolve(null)
267
+ else
268
+ server.close(()
269
+ capture resolve
270
+ resolve(null)
271
+ )
272
+ )
273
+ return pending.finally(()
274
+ if @api != null
275
+ return @api.close()
276
+ )
@@ -0,0 +1,57 @@
1
+ import js global Promise
2
+
3
+ export class Coordinator
4
+ methods
5
+ reserve(resources)
6
+ release(reservation)
7
+
8
+ Coordinator()
9
+ @reservations = []
10
+
11
+ Coordinator:reserve(resources)
12
+ waiting = []
13
+ for @reservations as previous
14
+ if overlaps(resources, previous.resources)
15
+ waiting.add(previous.done.promise)
16
+ reservation = {
17
+ resources: resources
18
+ ready: Promise/all(waiting)
19
+ done: Promise/withResolvers()
20
+ released: false
21
+ }
22
+ @reservations.add(reservation)
23
+ return reservation
24
+
25
+ Coordinator:release(reservation)
26
+ if reservation.released
27
+ return null
28
+ reservation.released = true
29
+ index = @reservations.indexOf(reservation)
30
+ if index >= 0
31
+ @reservations.splice(index, 1)
32
+ reservation.done.resolve(null)
33
+ return null
34
+
35
+ overlaps(first, second)
36
+ for first.writes as write
37
+ for second.reads as read
38
+ if related(write, read)
39
+ return true
40
+ for second.writes as other
41
+ if related(write, other)
42
+ return true
43
+ for first.reads as read
44
+ for second.writes as write
45
+ if related(read, write)
46
+ return true
47
+ return false
48
+
49
+ related(first, second)
50
+ count = first.length
51
+ if second.length < count
52
+ count = second.length
53
+ for first as index: name
54
+ if index < count
55
+ if name != second[index]
56
+ return false
57
+ return true