@tothalex/nulljs 0.0.45 → 0.0.46

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.45",
4
+ "version": "0.0.46",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "nulljs": "./src/index.ts"
@@ -5,12 +5,25 @@ import https from 'node:https'
5
5
  import * as tar from 'tar'
6
6
  import chalk from 'chalk'
7
7
 
8
+ // --- Configuration ---
9
+ const S3_BASE_URL = 'https://nulljs.s3.eu-north-1.amazonaws.com'
10
+ const S3_PREFIX = 'releases/'
11
+ const DOWNLOAD_BASE_URL = `${S3_BASE_URL}/${S3_PREFIX}`
12
+ // ---------------------
13
+
8
14
  interface PlatformInfo {
9
15
  target: string
10
16
  extension: string
11
17
  binaryName: string
12
18
  }
13
19
 
20
+ interface S3DownloadInfo {
21
+ downloadUrl: string
22
+ version: string
23
+ }
24
+
25
+ // --- Core Helper Functions (Unchanged) ---
26
+
14
27
  function getPlatformInfo(): PlatformInfo {
15
28
  const platform = process.platform
16
29
  const arch = process.arch
@@ -82,19 +95,87 @@ async function extractArchive(
82
95
  chmodSync(binaryPath, '755')
83
96
  }
84
97
 
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
98
  function getServerBinPath(): string {
91
99
  // Try to find the server binary relative to this module
92
- // This works both in development and when installed globally
93
100
  const currentFile = fileURLToPath(import.meta.url)
94
101
  const moduleRoot = join(dirname(currentFile), '../..')
95
102
  return join(moduleRoot, 'bin', 'server')
96
103
  }
97
104
 
105
+ // --- S3 LISTING Functions (New) ---
106
+
107
+ /**
108
+ * Fetches the raw XML listing from S3.
109
+ */
110
+ async function fetchXml(url: string): Promise<string> {
111
+ return new Promise((resolve, reject) => {
112
+ https
113
+ .get(url, (res) => {
114
+ if (res.statusCode !== 200) {
115
+ // This likely indicates a 403 Access Denied error
116
+ reject(
117
+ new Error(`S3 Request Failed (${res.statusCode}): Check public 's3:ListBucket' policy.`)
118
+ )
119
+ return
120
+ }
121
+ let data = ''
122
+ res.on('data', (chunk) => (data += chunk))
123
+ res.on('end', () => resolve(data))
124
+ })
125
+ .on('error', reject)
126
+ })
127
+ }
128
+
129
+ /**
130
+ * Lists the S3 prefix, parses the XML, and determines the latest version tag.
131
+ */
132
+ async function getLatestVersionFromS3(target: string, extension: string): Promise<S3DownloadInfo> {
133
+ // Query S3 for folders using delimiter and prefix
134
+ const listUrl = `${S3_BASE_URL}/?delimiter=/&prefix=${S3_PREFIX}`
135
+ const xmlData = await fetchXml(listUrl)
136
+
137
+ // Regex to find all CommonPrefixes entries (the version folders)
138
+ // Example: <Prefix>releases/v1.0.0/</Prefix>
139
+ const prefixRegex = /<Prefix>(releases\/v[^<]+)\/<\/Prefix>/g
140
+ const versionFolders: string[] = []
141
+ let match: RegExpExecArray | null
142
+
143
+ while ((match = prefixRegex.exec(xmlData)) !== null) {
144
+ // Extract version part: "releases/v1.0.0/" -> "v1.0.0"
145
+ const fullPrefix = match[1]
146
+ const version = fullPrefix.substring(S3_PREFIX.length)
147
+ versionFolders.push(version)
148
+ }
149
+
150
+ if (versionFolders.length === 0) {
151
+ throw new Error('No version folders found in S3 bucket. Has the CI/CD run?')
152
+ }
153
+
154
+ // Sort versions to find the latest (using semantic versioning logic)
155
+ const sortedVersions = versionFolders.sort((a, b) => {
156
+ // Strips 'v', splits by '.', and converts to number arrays for comparison
157
+ const aParts = a.replace('v', '').split('.').map(Number)
158
+ const bParts = b.replace('v', '').split('.').map(Number)
159
+
160
+ // Compare major, minor, and patch numbers (assuming vX.Y.Z)
161
+ for (let i = 0; i < 3; i++) {
162
+ if (aParts[i] > bParts[i]) return 1
163
+ if (aParts[i] < bParts[i]) return -1
164
+ }
165
+ return 0
166
+ })
167
+
168
+ // The latest version is the last one after sorting
169
+ const latestVersion = sortedVersions[sortedVersions.length - 1]
170
+
171
+ // Construct the full download URL
172
+ const downloadUrl = `${DOWNLOAD_BASE_URL}${latestVersion}/nulljs-server-${target}${extension}`
173
+
174
+ return { downloadUrl, version: latestVersion }
175
+ }
176
+
177
+ // --- Main Update Function (Modified) ---
178
+
98
179
  export async function updateServer(): Promise<void> {
99
180
  try {
100
181
  console.log(chalk.blue('🔄 Updating nulljs server binary...'))
@@ -115,21 +196,21 @@ export async function updateServer(): Promise<void> {
115
196
  if (existsSync(serverBinPath)) {
116
197
  console.log(chalk.blue('💾 Backing up existing binary...'))
117
198
 
118
- // Remove old backup if it exists
119
199
  if (existsSync(backupPath)) {
120
200
  unlinkSync(backupPath)
121
201
  }
122
202
 
123
- // Create backup
124
203
  const fs = await import('node:fs/promises')
125
204
  await fs.copyFile(serverBinPath, backupPath)
126
205
  console.log(chalk.green('✅ Backup created'))
127
206
  }
128
207
 
129
- console.log(chalk.blue(`🔍 Downloading latest server for ${target}...`))
208
+ console.log(chalk.blue(`🔍 Searching for latest server version...`))
130
209
 
131
- // Get download URL from latest release
132
- const downloadUrl = await getLatestReleaseUrl(target, extension)
210
+ // 1. Get download URL and version from S3 listing (NEW LOGIC)
211
+ const { downloadUrl, version } = await getLatestVersionFromS3(target, extension)
212
+ console.log(chalk.blue(` Found latest version: ${version} for ${target}`))
213
+ console.log(chalk.blue(` Downloading from: ${downloadUrl}`))
133
214
 
134
215
  try {
135
216
  // Download the archive
@@ -157,7 +238,7 @@ export async function updateServer(): Promise<void> {
157
238
  } catch (error) {
158
239
  // If update failed and we have a backup, restore it
159
240
  if (existsSync(backupPath)) {
160
- console.log(chalk.yellow('⚠️ Update failed, restoring backup...'))
241
+ console.log(chalk.yellow('⚠️ Update failed, restoring backup...'))
161
242
 
162
243
  if (existsSync(serverBinPath)) {
163
244
  unlinkSync(serverBinPath)
@@ -173,8 +254,9 @@ export async function updateServer(): Promise<void> {
173
254
  throw error
174
255
  }
175
256
  } catch (error) {
176
- console.error(chalk.red('❌ Failed to update server binary:'), error.message)
257
+ // Ensure the error message is available if it's not a standard Error object
258
+ const errorMessage = error instanceof Error ? error.message : String(error)
259
+ console.error(chalk.red('❌ Failed to update server binary:'), errorMessage)
177
260
  throw error
178
261
  }
179
262
  }
180
-