instaserve 1.1.14 → 1.1.16

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 (2) hide show
  1. package/instaserve +106 -5
  2. package/package.json +1 -1
package/instaserve CHANGED
@@ -4,19 +4,21 @@ import chalk from 'chalk'
4
4
  import fs from 'node:fs'
5
5
  import path from 'node:path'
6
6
  import { pathToFileURL, fileURLToPath } from 'node:url'
7
+ import { execSync } from 'node:child_process'
7
8
 
8
9
  console.log(chalk.cyan('\nInstaserve - Instant Web Stack\n'))
9
10
  console.log(chalk.yellow('Usage:'))
10
11
  console.log(chalk.green(' npx instaserve [options]'))
11
12
  console.log(chalk.green(' npx instaserve generate-routes\n'))
12
13
  console.log(chalk.yellow('Commands:'))
13
- console.log(chalk.green(' generate-routes') + ' Create a sample routes.js file in the current directory\n')
14
+ console.log(chalk.green(' generate-routes') + ' Create a sample routes.js file in the current directory')
15
+ console.log(chalk.green(' generate-certs') + ' Generate self-signed SSL certificates\n')
14
16
  console.log(chalk.yellow('Options:'))
15
17
  console.log(chalk.green(' -port <number>') + ' Port to listen on (default: 3000)')
16
18
  console.log(chalk.green(' -ip <address>') + ' IP address to bind to (default: 127.0.0.1)')
17
19
  console.log(chalk.green(' -public <path>') + ' Public directory path (default: ./public)')
18
20
  console.log(chalk.green(' -api <file>') + ' Path to routes file (default: ./routes.js)')
19
- console.log(chalk.green(' -secure') + ' Enable HTTPS (requires cert.pem and key.pem: run generate-certs.sh)')
21
+ console.log(chalk.green(' -secure') + ' Enable HTTPS (requires cert.pem and key.pem: run "npx instaserve generate-certs")')
20
22
  console.log(chalk.green(' -help') + ' Show this help message\n')
21
23
 
22
24
  if (process.argv.includes('-help')) {
@@ -76,16 +78,115 @@ if (args[0] === 'generate-routes') {
76
78
  console.log(chalk.green(`✓ Created routes.js`))
77
79
 
78
80
  const __dirname = path.dirname(fileURLToPath(import.meta.url))
81
+
82
+ // Copy routes-full.js
83
+ const sourceRoutesFull = path.join(__dirname, 'routes-full.js')
84
+ if (fs.existsSync(sourceRoutesFull)) {
85
+ const destRoutesFull = path.join(process.cwd(), 'routes-full.js')
86
+ if (!fs.existsSync(destRoutesFull)) {
87
+ fs.copyFileSync(sourceRoutesFull, destRoutesFull)
88
+ console.log(chalk.green(`✓ Created routes-full.js`))
89
+ }
90
+ }
91
+
79
92
  const sourceLib = path.join(__dirname, 'lib')
80
93
 
81
94
  if (fs.existsSync(sourceLib)) {
82
95
  const destLib = path.join(process.cwd(), 'lib')
83
- if (!fs.existsSync(destLib)) {
96
+ try {
84
97
  fs.cpSync(sourceLib, destLib, { recursive: true })
85
- console.log(chalk.green(`✓ Created lib/`))
98
+ console.log(chalk.green(`✓ Created/Updated lib/`))
99
+ } catch (e) {
100
+ console.error(chalk.red(`! Failed to copy lib/: ${e.message}`))
101
+ }
102
+ }
103
+
104
+ process.exit(0)
105
+ }
106
+
107
+ // Handle generate-certs command
108
+ if (args[0] === 'generate-certs') {
109
+ console.log(chalk.cyan('Generating self-signed certificates for Instaserve...'))
110
+
111
+ const openSslConfig = `[req]
112
+ distinguished_name = req_distinguished_name
113
+ req_extensions = v3_req
114
+ prompt = no
115
+
116
+ [req_distinguished_name]
117
+ C = US
118
+ ST = State
119
+ L = City
120
+ O = Instaserve
121
+ OU = Development
122
+ CN = localhost
123
+
124
+ [v3_req]
125
+ basicConstraints = CA:FALSE
126
+ keyUsage = nonRepudiation, digitalSignature, keyEncipherment
127
+ extendedKeyUsage = serverAuth, clientAuth
128
+ subjectAltName = @alt_names
129
+
130
+ [alt_names]
131
+ DNS.1 = localhost
132
+ DNS.2 = *.localhost
133
+ IP.1 = 127.0.0.1
134
+ IP.2 = ::1
135
+ `
136
+
137
+ try {
138
+ fs.writeFileSync('openssl.conf', openSslConfig)
139
+
140
+ // Generate private key
141
+ try {
142
+ execSync('openssl genrsa -out key.pem 2048', { stdio: 'inherit' })
143
+ } catch (e) {
144
+ console.error(chalk.red('Error running openssl genrsa. Ensure openssl is installed.'))
145
+ throw e
146
+ }
147
+
148
+ // Generate certificate
149
+ try {
150
+ execSync('openssl req -new -x509 -key key.pem -out cert.pem -days 365 -config openssl.conf -extensions v3_req', { stdio: 'inherit' })
151
+ } catch (e) {
152
+ console.error(chalk.red('Error running openssl req.'))
153
+ throw e
154
+ }
155
+
156
+ // Cleanup
157
+ if (fs.existsSync('openssl.conf')) fs.unlinkSync('openssl.conf')
158
+
159
+ console.log(chalk.green('Certificates generated: cert.pem and key.pem'))
160
+
161
+ // Add to trust store
162
+ if (process.platform === 'darwin') {
163
+ console.log(chalk.yellow('Adding certificate to macOS trust store...'))
164
+ try {
165
+ execSync('sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert.pem', { stdio: 'inherit' })
166
+ console.log(chalk.green('Certificate added to macOS trust store'))
167
+ } catch (e) {
168
+ console.log(chalk.red('Failed to add to trust store (sudo skipped or failed). You can add it manually.'))
169
+ }
170
+ } else if (process.platform === 'linux') {
171
+ console.log(chalk.yellow('Adding certificate to Linux trust store...'))
172
+ try {
173
+ execSync('sudo cp cert.pem /usr/local/share/ca-certificates/instaserve.crt', { stdio: 'inherit' })
174
+ execSync('sudo update-ca-certificates', { stdio: 'inherit' })
175
+ console.log(chalk.green('Certificate added to Linux trust store'))
176
+ } catch (e) {
177
+ console.log(chalk.red('Failed to add to trust store. Manual addition required.'))
178
+ }
86
179
  } else {
87
- console.log(chalk.yellow(`! lib/ directory already exists, skipping copy`))
180
+ console.log(chalk.yellow("Please manually add cert.pem to your system's trust store"))
88
181
  }
182
+
183
+ console.log(chalk.green("HTTPS certificates ready! Use 'npx instaserve -secure' to enable HTTPS"))
184
+
185
+ } catch (error) {
186
+ console.error(chalk.red('Error generating certificates:'), error.message)
187
+ // Cleanup if failed
188
+ if (fs.existsSync('openssl.conf')) fs.unlinkSync('openssl.conf')
189
+ process.exit(1)
89
190
  }
90
191
 
91
192
  process.exit(0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instaserve",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "description": "Instant web stack",
5
5
  "main": "module.mjs",
6
6
  "bin": "./instaserve",