Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
POS/scripts/dev-server.js at develop · Posnic/POS · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Posnic
POS
Repository navigation
Code
Issues
396
(396)
Pull requests
8
(8)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
POS
/
scripts
/
dev-server.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
212 lines (190 loc) · 7.79 KB
develop
Breadcrumbs
POS
/
scripts
/
dev-server.js
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
212 lines (190 loc) · 7.79 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
#!/usr/bin/env node
'use strict'
;
/*
* Run Posnic in a browser.
*
* npm run dev -> http://localhost:3000
*
* `npm start` opens the Electron desktop app, which is right for the product
* and wrong for a contributor: you cannot hit refresh, and you cannot look at
* a translation in place without building an installer. This starts the same
* pieces the desktop app starts, and leaves them in a browser.
*
* Nothing here is a second implementation. The API already serves the built
* frontend at /public on its own origin - that is how the desktop app loads it
* too - so this only has to arrange what the desktop app arranges:
*
* 1. secrets, which the API refuses to start without
* 2. a built frontend, because the API serves files rather than sources
* 3. mongod, using the same manager main.js uses
* 4. the API itself
*
* Every failure below names the fix. Somebody trying Posnic for the first time
* should not have to read this file to get past it.
*/
const
fs
=
require
(
'fs'
)
;
const
path
=
require
(
'path'
)
;
const
crypto
=
require
(
'crypto'
)
;
const
{
spawn
,
spawnSync
}
=
require
(
'child_process'
)
;
const
ROOT
=
path
.
resolve
(
__dirname
,
'..'
)
;
const
API
=
path
.
join
(
ROOT
,
'api'
)
;
const
PORT
=
Number
(
process
.
env
.
PORT
)
||
3000
;
const
MONGO_PORT
=
Number
(
process
.
env
.
POSNIC_MONGO_PORT
)
||
47017
;
const
say
=
(
msg
)
=>
console
.
log
(
`
${
msg
}
`
)
;
const
step
=
(
msg
)
=>
console
.
log
(
`\n
${
msg
}
`
)
;
function
die
(
what
,
fix
)
{
console
.
error
(
`\n
${
what
}
`
)
;
if
(
fix
)
console
.
error
(
` ->
${
fix
}
`
)
;
process
.
exit
(
1
)
;
}
/* ------------------------------------------------------------- 1 secrets --- */
/*
* The API signs tokens and encrypts identifiers, and refuses to start with
* placeholders - every Posnic in the world once shared the same signing string,
* and that is what this replaced.
*
* The desktop app generates these per installation into userData. For
* development they go in api/.env, generated once and kept, because
* regenerating them on every run signs you out on every restart.
*/
function
ensureSecrets
(
)
{
const
envFile
=
path
.
join
(
API
,
'.env'
)
;
const
validateExisting
=
(
)
=>
{
const
text
=
fs
.
readFileSync
(
envFile
,
'utf8'
)
;
const
missing
=
[
'JWT_SECRET'
,
'SESSION_SECRET'
,
'ENCRYPTION_KEY'
,
'ENCRYPTION_IV'
]
.
filter
(
(
key
)
=>
!
text
.
split
(
/
\r
?
\n
/
)
.
some
(
(
line
)
=>
line
.
startsWith
(
`
${
key
}
=`
)
&&
line
.
length
>
key
.
length
+
1
)
)
;
if
(
!
missing
.
length
)
return
say
(
'api/.env already has its secrets'
)
;
die
(
`api/.env exists but is missing:
${
missing
.
join
(
', '
)
}
`
,
'Delete api/.env and run this again to regenerate it, or fill those in by hand.'
)
;
}
;
/* Same shapes as the desktop app's getLocalSecrets(): AES-256-CBC wants a
32-byte key and a 16-byte IV, which is 16 and 8 bytes of hex. */
const
lines
=
[
'# Generated by scripts/dev-server.js for local development.'
,
'# Not shared with any other machine, and not what a real installation uses -'
,
'# the desktop app generates its own per install. Safe to delete; it will be'
,
'# made again, though doing so signs out any local session.'
,
`JWT_SECRET=
${
crypto
.
randomBytes
(
48
)
.
toString
(
'hex'
)
}
`
,
/* Not secrets, and not optional either: the API used to read these
straight off process.env and a login with neither set threw inside
jwt.sign, which took the whole process down. They have defaults now,
and a development environment should still be explicit. */
'JWT_EXPIRES_IN=90d'
,
'JWT_COOKIE_EXPIRES_IN=90'
,
`SESSION_SECRET=
${
crypto
.
randomBytes
(
48
)
.
toString
(
'hex'
)
}
`
,
`ENCRYPTION_KEY=
${
crypto
.
randomBytes
(
16
)
.
toString
(
'hex'
)
}
`
,
`ENCRYPTION_IV=
${
crypto
.
randomBytes
(
8
)
.
toString
(
'hex'
)
}
`
,
`KIOSK_API_KEY=
${
crypto
.
randomBytes
(
32
)
.
toString
(
'hex'
)
}
`
,
`POSNIC_KEY=
${
crypto
.
randomBytes
(
24
)
.
toString
(
'hex'
)
}
`
,
`POSNIC_SECRET=
${
crypto
.
randomBytes
(
24
)
.
toString
(
'hex'
)
}
`
,
`MONGODB_URI=mongodb://127.0.0.1:
${
MONGO_PORT
}
/PosnicPro`
,
''
,
]
;
let
fd
;
try
{
fd
=
fs
.
openSync
(
envFile
,
'wx'
,
0o600
)
;
}
catch
(
error
)
{
if
(
error
.
code
===
'EEXIST'
)
return
validateExisting
(
)
;
throw
error
;
}
try
{
fs
.
writeFileSync
(
fd
,
lines
.
join
(
'\n'
)
,
'utf8'
)
;
}
finally
{
fs
.
closeSync
(
fd
)
;
}
say
(
'generated api/.env with development secrets'
)
;
}
/* ------------------------------------------------------------ 2 frontend --- */
/*
* The API serves files, not sources. Without a build there is nothing at
* /public and the browser gets a 404 that explains nothing.
*/
function
ensureBuilt
(
)
{
const
built
=
path
.
join
(
ROOT
,
'frontend'
,
'public'
,
'login.html'
)
;
if
(
fs
.
existsSync
(
built
)
)
return
say
(
'frontend is built'
)
;
say
(
'frontend is not built yet - building (this takes about ten seconds)'
)
;
const
r
=
spawnSync
(
'npx'
,
[
'gulp'
,
'build'
]
,
{
cwd
:
path
.
join
(
ROOT
,
'frontend'
)
,
stdio
:
'inherit'
,
shell
:
process
.
platform
===
'win32'
,
}
)
;
if
(
r
.
status
!==
0
||
!
fs
.
existsSync
(
built
)
)
{
die
(
'the frontend build did not produce frontend/public/login.html'
,
'Run it directly to see why: cd frontend && npx gulp build'
)
;
}
say
(
'frontend built'
)
;
}
/* --------------------------------------------------------------- 3 mongo --- */
async
function
ensureMongo
(
)
{
let
MongoDBManager
;
try
{
MongoDBManager
=
require
(
path
.
join
(
ROOT
,
'src'
,
'mongodb-manager.js'
)
)
;
}
catch
(
e
)
{
die
(
`could not load the MongoDB manager:
${
e
.
message
}
`
,
'Run this from the repository root.'
)
;
}
process
.
env
.
POSNIC_MONGO_PORT
=
String
(
MONGO_PORT
)
;
const
mongo
=
new
MongoDBManager
(
)
;
try
{
await
mongo
.
start
(
)
;
}
catch
(
e
)
{
die
(
`MongoDB did not start:
${
e
.
message
}
`
,
'The first run downloads and unpacks it, which needs a few minutes and '
+
'about 1 GB. If it keeps failing, say so in an issue with this message.'
)
;
}
say
(
`mongod listening on
${
MONGO_PORT
}
`
)
;
return
mongo
;
}
/* ----------------------------------------------------------------- 4 api --- */
function
startApi
(
)
{
const
child
=
spawn
(
process
.
execPath
,
[
'server.js'
]
,
{
cwd
:
API
,
stdio
:
'inherit'
,
env
:
{
...
process
.
env
,
PORT
:
String
(
PORT
)
,
POSNIC_MONGO_PORT
:
String
(
MONGO_PORT
)
,
MONGODB_URI
:
process
.
env
.
MONGODB_URI
||
`mongodb://127.0.0.1:
${
MONGO_PORT
}
/PosnicPro`
,
NODE_ENV
:
process
.
env
.
NODE_ENV
||
'development'
,
}
,
}
)
;
child
.
on
(
'exit'
,
(
code
)
=>
{
if
(
code
)
console
.
error
(
`\n the API exited with code
${
code
}
`
)
;
process
.
exit
(
code
||
0
)
;
}
)
;
return
child
;
}
/* ---------------------------------------------------------------- run it --- */
(
async
(
)
=>
{
console
.
log
(
'\nPosnic development server'
)
;
step
(
'Secrets'
)
;
ensureSecrets
(
)
;
step
(
'Frontend'
)
;
ensureBuilt
(
)
;
step
(
'Database'
)
;
const
mongo
=
await
ensureMongo
(
)
;
step
(
'API'
)
;
const
api
=
startApi
(
)
;
/* Printed after a beat so it lands under the API's own startup noise rather
than scrolling away above it. */
setTimeout
(
(
)
=>
{
console
.
log
(
''
)
;
console
.
log
(
' ────────────────────────────────────────────────'
)
;
console
.
log
(
` Posnic is at http://localhost:
${
PORT
}
/public/login.html`
)
;
console
.
log
(
' ────────────────────────────────────────────────'
)
;
console
.
log
(
''
)
;
console
.
log
(
' First run walks you through setup. Ctrl+C to stop.'
)
;
console
.
log
(
''
)
;
}
,
2500
)
;
/* Leaving mongod running after the terminal closes is how a contributor ends
up with a database they did not know they had. */
const
stop
=
async
(
)
=>
{
console
.
log
(
'\n stopping...'
)
;
try
{
api
.
kill
(
)
;
}
catch
(
e
)
{
/* already gone */
}
try
{
await
mongo
.
stop
(
)
;
}
catch
(
e
)
{
/* already gone */
}
process
.
exit
(
0
)
;
}
;
process
.
on
(
'SIGINT'
,
stop
)
;
process
.
on
(
'SIGTERM'
,
stop
)
;
}
)
(
)
.
catch
(
(
e
)
=>
die
(
e
.
message
)
)
;
You can’t perform that action at this time.