FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modelscript/apps/api/src/app.ts at main · modelscript/modelscript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
modelscript
/
modelscript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
12
Code
Issues
1
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
modelscript
/
apps
/
api
/
src
/
app.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
378 lines (339 loc) · 14.3 KB
Breadcrumbs
modelscript
/
apps
/
api
/
src
/
app.ts
Copy path
File metadata and controls
378 lines (339 loc) · 14.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// SPDX-License-Identifier: AGPL-3.0-or-later
import
type
{
CosimMqttClient
}
from
"@modelscript/language/cosim"
;
import
bcrypt
from
"bcryptjs"
;
import
express
from
"express"
;
import
rateLimit
from
"express-rate-limit"
;
import
path
from
"node:path"
;
import
type
{
Pool
}
from
"pg"
;
import
{
initializeArtifactSystem
}
from
"./artifacts/index.js"
;
import
{
LibraryDatabase
}
from
"./database.js"
;
import
{
JobQueue
}
from
"./jobs.js"
;
import
{
setAuthDatabase
}
from
"./middleware/auth-middleware.js"
;
import
{
artifactViewerRouter
}
from
"./routes/artifact-viewer.js"
;
import
{
authRouter
}
from
"./routes/auth.js"
;
import
{
cosimRouter
,
mqttParticipantsRouter
}
from
"./routes/cosim.js"
;
import
{
federationRouter
}
from
"./routes/federation.js"
;
import
{
fmuRouter
}
from
"./routes/fmu.js"
;
import
{
gitRouter
}
from
"./routes/git.js"
;
import
{
graphqlRouter
}
from
"./routes/graphql.js"
;
import
{
historianRouter
}
from
"./routes/historian.js"
;
import
{
npmAuthRouter
}
from
"./routes/npm-auth.js"
;
import
{
npmRegistryRouter
}
from
"./routes/npm-registry.js"
;
import
{
packagesRouter
}
from
"./routes/packages.js"
;
import
{
physicsRouter
}
from
"./routes/physics.js"
;
import
{
publishRouter
}
from
"./routes/publish.js"
;
import
{
rdfRouter
}
from
"./routes/rdf.js"
;
import
{
reposRouter
}
from
"./routes/repos.js"
;
import
{
scriptsRouter
}
from
"./routes/scripts.js"
;
import
{
searchRouter
}
from
"./routes/search.js"
;
import
{
simulateRouter
}
from
"./routes/simulate.js"
;
import
{
socialRouter
}
from
"./routes/social.js"
;
import
{
sparqlRouter
}
from
"./routes/sparql.js"
;
import
{
storageRouter
}
from
"./routes/storage.js"
;
import
{
usersRouter
}
from
"./routes/users.js"
;
import
{
seedCadAssembly
}
from
"./seed-cad-assembly.js"
;
import
{
seedCfdAnimation
}
from
"./seed-cfd-animation.js"
;
import
{
seedDroneCfd
}
from
"./seed-drone-cfd.js"
;
import
{
seedDroneFea
}
from
"./seed-drone-fea.js"
;
import
{
seedScriptsAndTemplates
}
from
"./seed-scripts.js"
;
import
{
LibraryStorage
}
from
"./storage.js"
;
import
{
seedExamplePackages
,
seedPrepackagedLibraries
}
from
"./util/seed-examples.js"
;
/** Options for creating the Express application. */
export
interface
AppOptions
{
/** Optional library storage override. */
storage
?:
LibraryStorage
|
undefined
;
/** MQTT client for co-simulation (null = no MQTT). */
mqttClient
?:
CosimMqttClient
|
null
|
undefined
;
/** PostgreSQL pool for historian queries (null = stubs). */
dbPool
?:
Pool
|
null
|
undefined
;
}
export
function
createApp
(
options
?:
AppOptions
|
LibraryStorage
)
:
express
.
Express
{
const
app
=
express
(
)
;
// Support legacy signature: createApp(storage?)
const
opts
:
AppOptions
=
options
&&
"storage"
in
options
?
(
options
as
AppOptions
)
:
{
storage
:
options
as
LibraryStorage
|
undefined
}
;
const
libraryStorage
=
opts
.
storage
??
new
LibraryStorage
(
)
;
const
jobQueue
=
new
JobQueue
(
)
;
app
.
locals
.
jobQueue
=
jobQueue
;
const
database
=
new
LibraryDatabase
(
)
;
const
mqttClient
=
opts
.
mqttClient
??
null
;
const
dbPool
=
opts
.
dbPool
??
null
;
setAuthDatabase
(
database
)
;
// Initialize the extensible artifact system (FMU, Dataset, etc.)
initializeArtifactSystem
(
)
;
if
(
process
.
env
[
"NODE_ENV"
]
!==
"production"
||
process
.
env
[
"SEED_EXAMPLES"
]
===
"true"
)
{
console
.
log
(
"[DevServer] Development mode detected. Running auto-seeding..."
)
;
// Seed dev users
const
devUsers
=
[
{
username
:
"dev"
,
email
:
"dev@modelscript.org"
,
location
:
"Syria"
}
,
{
username
:
"alice"
,
email
:
"alice@modelscript.org"
,
location
:
"United States"
}
,
{
username
:
"bob"
,
email
:
"bob@modelscript.org"
,
location
:
"China"
}
,
]
;
for
(
const
u
of
devUsers
)
{
if
(
!
database
.
getUserByUsername
(
u
.
username
)
)
{
const
hash
=
bcrypt
.
hashSync
(
"password"
,
10
)
;
const
{
id
}
=
database
.
createUser
(
u
.
username
,
u
.
email
,
hash
)
;
database
.
updateProfile
(
id
,
{
location
:
u
.
location
}
)
;
}
}
// Seed FEA examples
seedDroneFea
(
database
)
;
seedCadAssembly
(
database
)
;
seedDroneCfd
(
database
)
;
seedScriptsAndTemplates
(
database
)
;
seedCfdAnimation
(
database
)
.
catch
(
console
.
error
)
;
console
.
log
(
"[DevServer] Restarted to fix parabolic flow 92% cap issue!"
)
;
// Run asynchronously in the background
void
seedExamplePackages
(
libraryStorage
,
database
,
jobQueue
)
.
catch
(
(
err
)
=>
{
console
.
error
(
"[DevServer] Failed to seed example packages:"
,
err
)
;
}
)
;
void
seedPrepackagedLibraries
(
libraryStorage
,
database
,
jobQueue
)
.
catch
(
(
err
)
=>
{
console
.
error
(
"[DevServer] Failed to seed prepackaged libraries:"
,
err
)
;
}
)
;
app
.
post
(
"/api/v1/dev/reset"
,
async
(
req
,
res
)
=>
{
console
.
log
(
"[DevServer] Resetting database and re-seeding..."
)
;
try
{
jobQueue
.
clear
(
)
;
database
.
resetDevData
(
)
;
for
(
const
u
of
devUsers
)
{
if
(
!
database
.
getUserByUsername
(
u
.
username
)
)
{
const
hash
=
bcrypt
.
hashSync
(
"password"
,
10
)
;
const
{
id
}
=
database
.
createUser
(
u
.
username
,
u
.
email
,
hash
)
;
database
.
updateProfile
(
id
,
{
location
:
u
.
location
}
)
;
}
}
// Seed some dummy posts
const
devUser
=
database
.
getUserByUsername
(
"dev"
)
;
const
alice
=
database
.
getUserByUsername
(
"alice"
)
;
if
(
devUser
&&
alice
)
{
const
devPost
=
database
.
createPost
(
devUser
.
id
,
"Welcome to the new ModelScript social platform! We're excited to see what you build. #welcome"
,
)
;
database
.
createPost
(
alice
.
id
,
"Just testing out the new federated features. Very smooth so far! 🚀"
)
;
database
.
createPost
(
devUser
.
id
,
"Has anyone played with the new Modelica parser yet? The AST is looking really clean."
,
undefined
,
devPost
.
id
,
)
;
const
cadViewId
=
database
.
createArtifactView
(
alice
.
id
,
"cad_step"
,
"url"
,
JSON
.
stringify
(
{
url
:
"http://localhost:3000/static-examples/drone-chassis/cad/drone.step"
}
)
,
"Drone Chassis CAD Model"
,
)
;
database
.
createPost
(
alice
.
id
,
"Just finished the initial 3D design for the drone chassis! The STEP file is attached below. Let me know what you think of the rotor placement. 🚁 #cad"
,
cadViewId
,
)
;
// Vega-Lite trajectory plot
const
vegaSpec
=
{
$schema
:
"https://vega.github.io/schema/vega-lite/v5.json"
,
description
:
"Simulation Trajectory of a Pendulum"
,
mark
:
"line"
,
encoding
:
{
x
:
{
field
:
"time"
,
type
:
"quantitative"
,
title
:
"Time (s)"
}
,
y
:
{
field
:
"angle"
,
type
:
"quantitative"
,
title
:
"Angle (rad)"
}
,
}
,
}
;
const
vegaData
=
[
{
time
:
0
,
angle
:
0.5
}
,
{
time
:
0.1
,
angle
:
0.48
}
,
{
time
:
0.2
,
angle
:
0.42
}
,
{
time
:
0.3
,
angle
:
0.34
}
,
{
time
:
0.4
,
angle
:
0.24
}
,
{
time
:
0.5
,
angle
:
0.12
}
,
{
time
:
0.6
,
angle
:
0
}
,
{
time
:
0.7
,
angle
:
-
0.12
}
,
{
time
:
0.8
,
angle
:
-
0.24
}
,
{
time
:
0.9
,
angle
:
-
0.34
}
,
{
time
:
1.0
,
angle
:
-
0.42
}
,
]
;
const
vegaViewId
=
database
.
createArtifactView
(
devUser
.
id
,
"vega-plot"
,
"inline"
,
JSON
.
stringify
(
{
spec
:
vegaSpec
,
data
:
vegaData
}
)
,
"Pendulum Simulation Trajectory"
,
)
;
database
.
createPost
(
devUser
.
id
,
"Here is the simulation trajectory for the pendulum model over 1 second. Vega-Lite makes it so easy to visualize this! 📉"
,
vegaViewId
,
)
;
// Mermaid diagram
const
mermaidCode
=
`
graph TD
A[Start] --> B{Is it working?}
B -- Yes --> C[Great!]
B -- No --> D[Debug]
D --> B
`
;
const
mermaidViewId
=
database
.
createArtifactView
(
devUser
.
id
,
"mermaid-diagram"
,
"inline"
,
JSON
.
stringify
(
{
code
:
mermaidCode
}
)
,
"Flowchart Diagram"
,
)
;
database
.
createPost
(
devUser
.
id
,
"I've also mapped out the debugging process using a Mermaid diagram. What do you think? 🧜♀️"
,
mermaidViewId
,
)
;
// PDF Document
const
pdfViewId
=
database
.
createArtifactView
(
devUser
.
id
,
"pdf"
,
"inline"
,
JSON
.
stringify
(
{
url
:
"http://localhost:3000/static-examples/drone-chassis/docs/drone-manual.pdf"
}
)
,
"Dummy PDF Document"
,
)
;
database
.
createPost
(
devUser
.
id
,
"Just reading through this interesting document. The PDF viewer embeds it perfectly! 📄"
,
pdfViewId
,
)
;
// CSV Table
const
csvData
=
`Name,Age,Role,Score\nAlice,28,Engineer,95\nBob,34,Designer,88\nCharlie,22,Intern,91`
;
const
csvViewId
=
database
.
createArtifactView
(
devUser
.
id
,
"csv"
,
"inline"
,
JSON
.
stringify
(
{
data
:
csvData
}
)
,
"Team Statistics"
,
)
;
database
.
createPost
(
devUser
.
id
,
"Check out these team statistics! The CSV table viewer renders the data cleanly. 📊"
,
csvViewId
,
)
;
// GCode Toolpath
const
gcodeViewId
=
database
.
createArtifactView
(
alice
.
id
,
"gcode"
,
"upload"
,
JSON
.
stringify
(
{
url
:
"https://raw.githubusercontent.com/mrdoob/three.js/master/examples/models/gcode/benchy.gcode"
,
thumbnail_url
:
"https://images.unsplash.com/photo-1620917670359-4781498b0ed1?auto=format&fit=crop&q=80&w=600"
,
}
)
,
"3DBenchy Toolpath"
,
)
;
database
.
createPost
(
alice
.
id
,
"Check out the sliced GCode for the 3DBenchy benchmark test. Ready for the machine!"
,
gcodeViewId
,
)
;
// FEA Simulation
seedDroneFea
(
database
)
;
seedCadAssembly
(
database
)
;
seedDroneCfd
(
database
)
;
seedScriptsAndTemplates
(
database
)
;
await
seedCfdAnimation
(
database
)
;
}
await
seedExamplePackages
(
libraryStorage
,
database
,
jobQueue
)
;
await
seedPrepackagedLibraries
(
libraryStorage
,
database
,
jobQueue
)
;
res
.
json
(
{
success
:
true
}
)
;
}
catch
(
err
)
{
console
.
error
(
"[DevServer] Failed to reset dev data:"
,
err
)
;
res
.
status
(
500
)
.
json
(
{
success
:
false
,
error
:
String
(
err
)
}
)
;
}
}
)
;
}
// ── Trending Topics Periodic Worker ──
// Run every 15 minutes (900,000 ms)
const
decayWorkerInterval
=
setInterval
(
(
)
=>
{
try
{
database
.
decayTrendingTopics
(
)
;
}
catch
(
err
)
{
console
.
error
(
"Failed to decay trending topics:"
,
err
)
;
}
}
,
15
*
60
*
1000
,
)
;
app
.
locals
.
decayWorkerInterval
=
decayWorkerInterval
;
// ── RSS Worker ──
// Run immediately on startup, then every 15 minutes
const
runRssWorker
=
(
)
=>
{
import
(
"./util/rss-worker.js"
)
.
then
(
(
{
processRssFeeds
}
)
=>
{
void
processRssFeeds
(
database
)
;
}
)
.
catch
(
console
.
error
)
;
}
;
runRssWorker
(
)
;
const
rssWorkerInterval
=
setInterval
(
runRssWorker
,
15
*
60
*
1000
)
;
app
.
locals
.
rssWorkerInterval
=
rssWorkerInterval
;
// Increased limit for npm publish payloads (base64-encoded tarballs in JSON body)
app
.
use
(
express
.
json
(
{
limit
:
"50mb"
}
)
)
;
// Rate Limiting
const
limiter
=
rateLimit
(
{
windowMs
:
15
*
60
*
1000
,
// 15 minutes
max
:
200
,
// Limit each IP to 200 requests per `window`
standardHeaders
:
true
,
legacyHeaders
:
false
,
message
:
{
error
:
"Too many requests, please try again later."
}
,
}
)
;
if
(
process
.
env
[
"NODE_ENV"
]
===
"production"
)
{
app
.
use
(
limiter
)
;
}
// CORS — allow all origins (VS Code webviews, Morsel, etc.)
app
.
use
(
(
_req
,
res
,
next
)
=>
{
res
.
header
(
"Access-Control-Allow-Origin"
,
"*"
)
;
res
.
header
(
"Access-Control-Allow-Methods"
,
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
)
;
res
.
header
(
"Access-Control-Allow-Headers"
,
"Content-Type, Authorization"
)
;
if
(
_req
.
method
===
"OPTIONS"
)
{
res
.
sendStatus
(
204
)
;
return
;
}
next
(
)
;
}
)
;
// Auth routes
app
.
use
(
"/api/v1/auth"
,
authRouter
(
database
)
)
;
app
.
use
(
"/api/v1/users"
,
usersRouter
(
database
)
)
;
app
.
use
(
"/api/v1/social"
,
socialRouter
(
database
)
)
;
app
.
use
(
"/api/v1/repos"
,
reposRouter
(
database
)
)
;
app
.
use
(
"/api/v1/search"
,
searchRouter
(
database
)
)
;
app
.
use
(
"/api/v1/storage"
,
storageRouter
(
)
)
;
app
.
use
(
"/"
,
federationRouter
(
database
)
)
;
// Mount the library routers
app
.
use
(
"/api/v1/libraries"
,
packagesRouter
(
libraryStorage
,
jobQueue
,
database
)
)
;
app
.
use
(
"/api/v1/libraries"
,
publishRouter
(
libraryStorage
,
jobQueue
,
database
)
)
;
app
.
use
(
"/api/v1/libraries"
,
rdfRouter
(
database
)
)
;
app
.
use
(
"/api/v1/libraries"
,
graphqlRouter
(
database
)
)
;
app
.
use
(
"/api/v1/libraries"
,
sparqlRouter
(
database
)
)
;
app
.
use
(
"/api/v1"
,
simulateRouter
(
libraryStorage
,
jobQueue
)
)
;
app
.
use
(
"/api/v1"
,
physicsRouter
(
jobQueue
,
database
)
)
;
app
.
use
(
"/api/v1/jobs"
,
scriptsRouter
(
database
)
)
;
// Artifact viewer routes (query artifact metadata, viewer configs)
app
.
use
(
"/api/v1"
,
artifactViewerRouter
(
database
)
)
;
// Co-simulation routes (with MQTT client injection)
app
.
use
(
"/api/v1/cosim"
,
cosimRouter
(
mqttClient
)
)
;
app
.
use
(
"/api/v1/mqtt/participants"
,
mqttParticipantsRouter
(
mqttClient
)
)
;
app
.
use
(
"/api/v1/historian"
,
historianRouter
(
dbPool
,
mqttClient
)
)
;
app
.
use
(
"/api/v1/fmus"
,
fmuRouter
(
)
)
;
app
.
use
(
"/api/v1/git"
,
gitRouter
(
)
)
;
app
.
use
(
"/api/v1/gitlab"
,
gitRouter
(
)
)
;
// Keep for backwards compatibility
app
.
use
(
"/api/thumbnails"
,
express
.
static
(
path
.
resolve
(
process
.
cwd
(
)
,
"public/thumbnails"
)
)
)
;
app
.
use
(
"/uploads"
,
express
.
static
(
path
.
resolve
(
process
.
cwd
(
)
,
"uploads"
)
)
)
;
if
(
process
.
env
[
"NODE_ENV"
]
!==
"production"
||
process
.
env
[
"SEED_EXAMPLES"
]
===
"true"
)
{
app
.
use
(
"/static-examples"
,
express
.
static
(
path
.
resolve
(
process
.
cwd
(
)
,
"../../packages/examples"
)
)
)
;
}
// ── npm-compatible registry (mounted at root for `npm --registry=` compat) ──
app
.
use
(
"/"
,
npmAuthRouter
(
database
)
)
;
app
.
use
(
"/"
,
npmRegistryRouter
(
database
)
)
;
// Health check
app
.
get
(
"/health"
,
(
_req
,
res
)
=>
{
res
.
json
(
{
status
:
"ok"
,
mqtt
:
mqttClient
?
"connected"
:
"unavailable"
,
historian
:
dbPool
?
"connected"
:
"unavailable"
,
}
)
;
}
)
;
return
app
;
}
Back
|
FazBrowse Home
|
New Git URL