@tothalex/nulljs 0.0.43 → 0.0.45

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tothalex/nulljs",
3
3
  "module": "index.ts",
4
- "version": "0.0.43",
4
+ "version": "0.0.45",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "nulljs": "./src/index.ts"
@@ -6,6 +6,12 @@ const path = require('path')
6
6
  const { createWriteStream } = require('fs')
7
7
  const tar = require('tar')
8
8
 
9
+ // --- Configuration ---
10
+ const S3_BASE_URL = 'https://nulljs.s3.eu-north-1.amazonaws.com'
11
+ const S3_PREFIX = 'releases/'
12
+ const DOWNLOAD_BASE_URL = `${S3_BASE_URL}/${S3_PREFIX}`
13
+ // ---------------------
14
+
9
15
  function getPlatformInfo() {
10
16
  const platform = process.platform
11
17
  const arch = process.arch
@@ -32,6 +38,67 @@ function getPlatformInfo() {
32
38
  return { target, extension, binaryName }
33
39
  }
34
40
 
41
+ // Helper to fetch XML and return response data
42
+ async function fetchXml(url) {
43
+ return new Promise((resolve, reject) => {
44
+ https
45
+ .get(url, (res) => {
46
+ if (res.statusCode !== 200) {
47
+ reject(
48
+ new Error(`S3 Request Failed: ${res.statusCode}. Check if public listing is enabled.`)
49
+ )
50
+ return
51
+ }
52
+ let data = ''
53
+ res.on('data', (chunk) => (data += chunk))
54
+ res.on('end', () => resolve(data))
55
+ })
56
+ .on('error', reject)
57
+ })
58
+ }
59
+
60
+ /**
61
+ * Lists the S3 prefix, parses the XML, and determines the latest version tag.
62
+ */
63
+ async function getLatestVersionFromS3() {
64
+ // Query S3 for folders using delimiter and prefix
65
+ const listUrl = `${S3_BASE_URL}/?delimiter=/&prefix=${S3_PREFIX}`
66
+ const xmlData = await fetchXml(listUrl)
67
+
68
+ // Regex to find all CommonPrefixes entries (the version folders)
69
+ const prefixRegex = /<Prefix>(releases\/v[^<]+)\/<\/Prefix>/g
70
+ const versionFolders = []
71
+ let match
72
+
73
+ while ((match = prefixRegex.exec(xmlData)) !== null) {
74
+ // Extract version part: "releases/v1.0.0/" -> "v1.0.0"
75
+ const fullPrefix = match[1]
76
+ const version = fullPrefix.substring(S3_PREFIX.length)
77
+ versionFolders.push(version)
78
+ }
79
+
80
+ if (versionFolders.length === 0) {
81
+ throw new Error('No version folders found in S3 bucket.')
82
+ }
83
+
84
+ // Sort versions to find the latest (using semantic versioning logic)
85
+ const sortedVersions = versionFolders.sort((a, b) => {
86
+ // Strips 'v', splits by '.', and converts to number arrays for comparison
87
+ const aParts = a.replace('v', '').split('.').map(Number)
88
+ const bParts = b.replace('v', '').split('.').map(Number)
89
+
90
+ // Compare major, minor, and patch numbers
91
+ for (let i = 0; i < 3; i++) {
92
+ if (aParts[i] > bParts[i]) return 1
93
+ if (aParts[i] < bParts[i]) return -1
94
+ }
95
+ return 0
96
+ })
97
+
98
+ // The latest version is the last one after sorting
99
+ return sortedVersions[sortedVersions.length - 1]
100
+ }
101
+
35
102
  async function downloadFile(url, destination) {
36
103
  return new Promise((resolve, reject) => {
37
104
  const file = createWriteStream(destination)
@@ -70,12 +137,9 @@ async function extractArchive(archivePath, extractPath, binaryName) {
70
137
 
71
138
  // Make binary executable
72
139
  const binaryPath = path.join(extractPath, binaryName)
73
- fs.chmodSync(binaryPath, '755')
74
- }
75
-
76
- async function getLatestReleaseUrl(target, extension) {
77
- const baseUrl = 'https://nulljs.s3.eu-north-1.amazonaws.com/releases'
78
- return `${baseUrl}/nulljs-server-${target}/nulljs-server-${target}${extension}`
140
+ if (fs.existsSync(binaryPath)) {
141
+ fs.chmodSync(binaryPath, '755')
142
+ }
79
143
  }
80
144
 
81
145
  async function installServer() {
@@ -83,7 +147,6 @@ async function installServer() {
83
147
  console.log('📦 Installing nulljs server binary...')
84
148
 
85
149
  const { target, extension, binaryName } = getPlatformInfo()
86
-
87
150
  const binDir = path.join(__dirname, '..', 'bin')
88
151
  const archivePath = path.join(binDir, `server${extension}`)
89
152
  const binaryPath = path.join(binDir, 'server')
@@ -93,33 +156,38 @@ async function installServer() {
93
156
  fs.mkdirSync(binDir, { recursive: true })
94
157
  }
95
158
 
96
- // Check if binary already exists
159
+ // Check if binary already exists (optional optimization)
97
160
  if (fs.existsSync(binaryPath)) {
98
161
  console.log('✅ Server binary already installed')
99
162
  return
100
163
  }
101
164
 
102
- console.log(`🔍 Downloading server for ${target}...`)
165
+ // 1. Determine Version from S3
166
+ console.log('🔍 Checking for latest version on S3...')
167
+ const version = await getLatestVersionFromS3()
168
+ console.log(` Found version: ${version}`)
103
169
 
104
- // Get download URL from latest release
105
- const downloadUrl = await getLatestReleaseUrl(target, extension)
170
+ // 2. Construct URL
171
+ // URL format: https://.../releases/VERSION/nulljs-server-TARGET.tar.gz
172
+ const downloadUrl = `${DOWNLOAD_BASE_URL}${version}/nulljs-server-${target}${extension}`
173
+ console.log(` Downloading from: ${downloadUrl}`)
106
174
 
107
- // Download the archive
175
+ // 3. Download
108
176
  await downloadFile(downloadUrl, archivePath)
109
177
  console.log('✅ Download completed')
110
178
 
111
- // Extract the binary
179
+ // 4. Extract
112
180
  console.log('📂 Extracting binary...')
113
181
  await extractArchive(archivePath, binDir, binaryName)
114
182
 
115
- // Clean up archive
183
+ // 5. Clean up archive
116
184
  fs.unlinkSync(archivePath)
117
185
 
118
186
  console.log('✅ nulljs server installed successfully')
119
187
  } catch (error) {
120
188
  console.error('❌ Failed to install server binary:', error.message)
121
189
  console.error('⚠️ You may need to build the server manually')
122
- process.exit(0) // Don't fail the installation
190
+ process.exit(0)
123
191
  }
124
192
  }
125
193
 
@@ -129,4 +197,3 @@ if (require.main === module) {
129
197
  }
130
198
 
131
199
  module.exports = { installServer }
132
-