@tothalex/nulljs 0.0.40

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 (42) hide show
  1. package/package.json +45 -0
  2. package/scripts/install-server.js +132 -0
  3. package/src/commands/api.ts +16 -0
  4. package/src/commands/auth.ts +54 -0
  5. package/src/commands/create.ts +43 -0
  6. package/src/commands/deploy.ts +160 -0
  7. package/src/commands/dev/function/index.ts +221 -0
  8. package/src/commands/dev/function/utils.ts +99 -0
  9. package/src/commands/dev/index.tsx +126 -0
  10. package/src/commands/dev/logging-manager.ts +87 -0
  11. package/src/commands/dev/server/index.ts +48 -0
  12. package/src/commands/dev/server/utils.ts +37 -0
  13. package/src/commands/dev/ui/components/scroll-area.tsx +141 -0
  14. package/src/commands/dev/ui/components/tab-bar.tsx +67 -0
  15. package/src/commands/dev/ui/index.tsx +71 -0
  16. package/src/commands/dev/ui/logging-context.tsx +76 -0
  17. package/src/commands/dev/ui/tabs/functions-tab.tsx +35 -0
  18. package/src/commands/dev/ui/tabs/server-tab.tsx +36 -0
  19. package/src/commands/dev/ui/tabs/vite-tab.tsx +35 -0
  20. package/src/commands/dev/ui/use-logging.tsx +34 -0
  21. package/src/commands/dev/vite/index.ts +54 -0
  22. package/src/commands/dev/vite/utils.ts +71 -0
  23. package/src/commands/host.ts +339 -0
  24. package/src/commands/index.ts +8 -0
  25. package/src/commands/profile.ts +189 -0
  26. package/src/commands/secret.ts +79 -0
  27. package/src/index.ts +346 -0
  28. package/src/lib/api.ts +189 -0
  29. package/src/lib/bundle/external.ts +23 -0
  30. package/src/lib/bundle/function/index.ts +46 -0
  31. package/src/lib/bundle/index.ts +2 -0
  32. package/src/lib/bundle/react/index.ts +2 -0
  33. package/src/lib/bundle/react/spa.ts +77 -0
  34. package/src/lib/bundle/react/ssr/client.ts +93 -0
  35. package/src/lib/bundle/react/ssr/config.ts +77 -0
  36. package/src/lib/bundle/react/ssr/index.ts +4 -0
  37. package/src/lib/bundle/react/ssr/props.ts +71 -0
  38. package/src/lib/bundle/react/ssr/server.ts +83 -0
  39. package/src/lib/bundle/types.ts +4 -0
  40. package/src/lib/config.ts +347 -0
  41. package/src/lib/deployment.ts +244 -0
  42. package/src/lib/update-server.ts +180 -0
@@ -0,0 +1,180 @@
1
+ import { existsSync, createWriteStream, chmodSync, unlinkSync, mkdirSync } from 'node:fs'
2
+ import { join, dirname } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+ import https from 'node:https'
5
+ import * as tar from 'tar'
6
+ import chalk from 'chalk'
7
+
8
+ interface PlatformInfo {
9
+ target: string
10
+ extension: string
11
+ binaryName: string
12
+ }
13
+
14
+ function getPlatformInfo(): PlatformInfo {
15
+ const platform = process.platform
16
+ const arch = process.arch
17
+
18
+ const platformMap: Record<string, Record<string, string>> = {
19
+ linux: {
20
+ x64: 'x86_64-unknown-linux-gnu',
21
+ arm64: 'aarch64-unknown-linux-gnu'
22
+ },
23
+ darwin: {
24
+ x64: 'x86_64-apple-darwin',
25
+ arm64: 'aarch64-apple-darwin'
26
+ }
27
+ }
28
+
29
+ const target = platformMap[platform]?.[arch]
30
+ if (!target) {
31
+ throw new Error(`Unsupported platform: ${platform}-${arch}`)
32
+ }
33
+
34
+ const extension = '.tar.gz'
35
+ const binaryName = 'server'
36
+
37
+ return { target, extension, binaryName }
38
+ }
39
+
40
+ async function downloadFile(url: string, destination: string): Promise<void> {
41
+ return new Promise((resolve, reject) => {
42
+ const file = createWriteStream(destination)
43
+
44
+ https
45
+ .get(url, (response) => {
46
+ if (response.statusCode === 302 || response.statusCode === 301) {
47
+ // Handle redirect
48
+ return downloadFile(response.headers.location!, destination).then(resolve).catch(reject)
49
+ }
50
+
51
+ if (response.statusCode !== 200) {
52
+ reject(new Error(`Failed to download: ${response.statusCode}`))
53
+ return
54
+ }
55
+
56
+ response.pipe(file)
57
+
58
+ file.on('finish', () => {
59
+ file.close()
60
+ resolve()
61
+ })
62
+
63
+ file.on('error', reject)
64
+ })
65
+ .on('error', reject)
66
+ })
67
+ }
68
+
69
+ async function extractArchive(
70
+ archivePath: string,
71
+ extractPath: string,
72
+ binaryName: string
73
+ ): Promise<void> {
74
+ // Extract tar.gz
75
+ await tar.x({
76
+ file: archivePath,
77
+ cwd: extractPath
78
+ })
79
+
80
+ // Make binary executable
81
+ const binaryPath = join(extractPath, binaryName)
82
+ chmodSync(binaryPath, '755')
83
+ }
84
+
85
+ async function getLatestReleaseUrl(target: string, extension: string): Promise<string> {
86
+ const baseUrl = 'https://nulljs.s3.eu-north-1.amazonaws.com/releases'
87
+ return `${baseUrl}/nulljs-server-${target}/nulljs-server-${target}${extension}`
88
+ }
89
+
90
+ function getServerBinPath(): string {
91
+ // Try to find the server binary relative to this module
92
+ // This works both in development and when installed globally
93
+ const currentFile = fileURLToPath(import.meta.url)
94
+ const moduleRoot = join(dirname(currentFile), '../..')
95
+ return join(moduleRoot, 'bin', 'server')
96
+ }
97
+
98
+ export async function updateServer(): Promise<void> {
99
+ try {
100
+ console.log(chalk.blue('🔄 Updating nulljs server binary...'))
101
+
102
+ const { target, extension, binaryName } = getPlatformInfo()
103
+
104
+ const serverBinPath = getServerBinPath()
105
+ const binDir = dirname(serverBinPath)
106
+ const archivePath = join(binDir, `server${extension}`)
107
+ const backupPath = join(binDir, 'server.backup')
108
+
109
+ // Create bin directory if it doesn't exist
110
+ if (!existsSync(binDir)) {
111
+ mkdirSync(binDir, { recursive: true })
112
+ }
113
+
114
+ // Backup existing binary if it exists
115
+ if (existsSync(serverBinPath)) {
116
+ console.log(chalk.blue('💾 Backing up existing binary...'))
117
+
118
+ // Remove old backup if it exists
119
+ if (existsSync(backupPath)) {
120
+ unlinkSync(backupPath)
121
+ }
122
+
123
+ // Create backup
124
+ const fs = await import('node:fs/promises')
125
+ await fs.copyFile(serverBinPath, backupPath)
126
+ console.log(chalk.green('✅ Backup created'))
127
+ }
128
+
129
+ console.log(chalk.blue(`🔍 Downloading latest server for ${target}...`))
130
+
131
+ // Get download URL from latest release
132
+ const downloadUrl = await getLatestReleaseUrl(target, extension)
133
+
134
+ try {
135
+ // Download the archive
136
+ await downloadFile(downloadUrl, archivePath)
137
+ console.log(chalk.green('✅ Download completed'))
138
+
139
+ // Remove existing binary before extracting new one
140
+ if (existsSync(serverBinPath)) {
141
+ unlinkSync(serverBinPath)
142
+ }
143
+
144
+ // Extract the binary
145
+ console.log(chalk.blue('📂 Extracting binary...'))
146
+ await extractArchive(archivePath, binDir, binaryName)
147
+
148
+ // Clean up archive
149
+ unlinkSync(archivePath)
150
+
151
+ // Remove backup since update was successful
152
+ if (existsSync(backupPath)) {
153
+ unlinkSync(backupPath)
154
+ }
155
+
156
+ console.log(chalk.green('✅ nulljs server updated successfully'))
157
+ } catch (error) {
158
+ // If update failed and we have a backup, restore it
159
+ if (existsSync(backupPath)) {
160
+ console.log(chalk.yellow('⚠️ Update failed, restoring backup...'))
161
+
162
+ if (existsSync(serverBinPath)) {
163
+ unlinkSync(serverBinPath)
164
+ }
165
+
166
+ const fs = await import('node:fs/promises')
167
+ await fs.copyFile(backupPath, serverBinPath)
168
+ unlinkSync(backupPath)
169
+
170
+ console.log(chalk.green('✅ Backup restored'))
171
+ }
172
+
173
+ throw error
174
+ }
175
+ } catch (error) {
176
+ console.error(chalk.red('❌ Failed to update server binary:'), error.message)
177
+ throw error
178
+ }
179
+ }
180
+