@jonathanleelx/meddle 0.4.0-beta.4 → 0.4.0-beta.6

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.
@@ -24,6 +24,7 @@ const {
24
24
  downloadBinaryAsset,
25
25
  getAutoUpdate,
26
26
  setAutoUpdate,
27
+ inferChannel,
27
28
  } = require('../lib/update-check')
28
29
 
29
30
  const PACKAGE_NAME = '@jonathanleelx/meddle'
@@ -45,8 +46,9 @@ function showHelp() {
45
46
  meddle update - 检查并升级 meddle
46
47
 
47
48
  用法:
48
- meddle update 检查并升级到最新版本
49
- meddle update --beta 检查并升级到最新的 beta 测试版本
49
+ meddle update 检查并升级(beta 版本默认走 beta 频道)
50
+ meddle update --beta 强制使用 beta 测试频道
51
+ meddle update --stable 强制使用稳定频道
50
52
  meddle update --check 仅检查,提示可用版本 (有新版本时退出码为 2)
51
53
  meddle update --check --beta 仅检查 beta 版本
52
54
  meddle update --version <x.y.z> 安装指定版本
@@ -118,7 +120,7 @@ async function run() {
118
120
  const flag = args.find((a) => a === '--check' || a === 'status' || a === '--help' || a === '-h')
119
121
  const versionFlagIndex = args.indexOf('--version')
120
122
  const autoFlagIndex = args.indexOf('--auto')
121
- const channel = args.includes('--beta') ? 'beta' : 'stable'
123
+ const channel = args.includes('--beta') ? 'beta' : args.includes('--stable') ? 'stable' : inferChannel(current)
122
124
 
123
125
  if (flag === '--help' || flag === '-h') {
124
126
  showHelp()
@@ -22,7 +22,6 @@ const REPO = 'JonathanLee-LX/meddle'
22
22
  const DEFAULT_NPM_REGISTRY_URL = `https://registry.npmjs.org/${PACKAGE_NAME}/latest`
23
23
  const DEFAULT_NPM_BETA_URL = `https://registry.npmjs.org/${PACKAGE_NAME}/beta`
24
24
  const DEFAULT_GITHUB_LATEST_URL = `https://github.com/${REPO}/releases/latest`
25
- const DEFAULT_GITHUB_RELEASES_URL = `https://api.github.com/repos/${REPO}/releases`
26
25
  const DEFAULT_DOWNLOAD_BASE_URL = `https://github.com/${REPO}/releases/download`
27
26
  const DEFAULT_CACHE_TTL_MS = 24 * 60 * 60 * 1000
28
27
  const DEFAULT_TIMEOUT_MS = 5000
@@ -103,8 +102,9 @@ function compareVersions(a, b) {
103
102
  async function getLatestVersionNpm({ fetchImpl, registryUrl, timeoutMs, distTag } = {}) {
104
103
  const doFetch = fetchImpl || fetch
105
104
  const url = registryUrl
106
- || process.env.MEDDLE_NPM_REGISTRY_URL
107
- || (distTag === 'beta' ? DEFAULT_NPM_BETA_URL : DEFAULT_NPM_REGISTRY_URL)
105
+ || (distTag === 'beta'
106
+ ? (process.env.MEDDLE_NPM_BETA_REGISTRY_URL || process.env.MEDDLE_NPM_REGISTRY_URL || DEFAULT_NPM_BETA_URL)
107
+ : (process.env.MEDDLE_NPM_REGISTRY_URL || DEFAULT_NPM_REGISTRY_URL))
108
108
  const response = await doFetch(url, { signal: AbortSignal.timeout(timeoutMs || DEFAULT_TIMEOUT_MS) })
109
109
  if (!response.ok) throw new Error(`npm registry responded ${response.status}`)
110
110
  const data = await response.json()
@@ -114,42 +114,26 @@ async function getLatestVersionNpm({ fetchImpl, registryUrl, timeoutMs, distTag
114
114
  }
115
115
 
116
116
  /**
117
- * Fetch the latest version from GitHub. For stable, follow the releases/latest
118
- * redirect (prereleases excluded). For includePrerelease, list releases via the
119
- * GitHub API and pick the highest version.
120
- * @param {{ fetchImpl?: Function, latestUrl?: string, timeoutMs?: number, includePrerelease?: boolean, releasesUrl?: string }} opts
117
+ * Fetch the latest stable version from the GitHub releases/latest redirect
118
+ * (prereleases are excluded by GitHub). Beta versions resolve through the npm
119
+ * registry `beta` dist-tag instead — see checkForUpdate.
120
+ * @param {{ fetchImpl?: Function, latestUrl?: string, timeoutMs?: number }} opts
121
121
  * @returns {Promise<string>}
122
122
  */
123
- async function getLatestVersionGithub({ fetchImpl, latestUrl, timeoutMs, includePrerelease = false, releasesUrl } = {}) {
123
+ async function getLatestVersionGithub({ fetchImpl, latestUrl, timeoutMs } = {}) {
124
124
  const doFetch = fetchImpl || fetch
125
- if (!includePrerelease) {
126
- const url = latestUrl || process.env.MEDDLE_GITHUB_LATEST_URL || DEFAULT_GITHUB_LATEST_URL
127
- const response = await doFetch(url, {
128
- redirect: 'manual',
129
- signal: AbortSignal.timeout(timeoutMs || DEFAULT_TIMEOUT_MS),
130
- })
131
- if (response.status < 300 || response.status >= 400) {
132
- throw new Error(`GitHub latest release did not redirect (${response.status})`)
133
- }
134
- const location = response.headers.get('location') || ''
135
- const match = /\/tag\/(v?[\d][\w.-]*)$/.exec(location)
136
- if (!match) throw new Error(`Could not resolve latest release tag`)
137
- return match[1].replace(/^v/, '')
138
- }
139
-
140
- const url = releasesUrl || process.env.MEDDLE_GITHUB_RELEASES_URL || DEFAULT_GITHUB_RELEASES_URL
125
+ const url = latestUrl || process.env.MEDDLE_GITHUB_LATEST_URL || DEFAULT_GITHUB_LATEST_URL
141
126
  const response = await doFetch(url, {
127
+ redirect: 'manual',
142
128
  signal: AbortSignal.timeout(timeoutMs || DEFAULT_TIMEOUT_MS),
143
129
  })
144
- if (!response.ok) throw new Error(`GitHub releases API responded ${response.status}`)
145
- const data = await response.json()
146
- const versions = (Array.isArray(data) ? data : [])
147
- .filter((r) => r && typeof r === 'object' && r.prerelease === true && typeof r.tag_name === 'string')
148
- .map((r) => r.tag_name.replace(/^v/, ''))
149
- .filter(isValidVersion)
150
- if (versions.length === 0) throw new Error(`No prerelease found on GitHub releases`)
151
- versions.sort((a, b) => compareVersions(b, a))
152
- return versions[0]
130
+ if (response.status < 300 || response.status >= 400) {
131
+ throw new Error(`GitHub latest release did not redirect (${response.status})`)
132
+ }
133
+ const location = response.headers.get('location') || ''
134
+ const match = /\/tag\/(v?[\d][\w.-]*)$/.exec(location)
135
+ if (!match) throw new Error(`Could not resolve latest release tag`)
136
+ return match[1].replace(/^v/, '')
153
137
  }
154
138
 
155
139
  /**
@@ -171,6 +155,16 @@ function updateCachePath(home) {
171
155
  return path.join(home, '.cache', 'update-check.json')
172
156
  }
173
157
 
158
+ /**
159
+ * Infer the update channel from the current version: prerelease versions
160
+ * (e.g. 0.4.0-beta.4) default to the beta channel, releases to stable.
161
+ * @param {string} current
162
+ * @returns {'stable' | 'beta'}
163
+ */
164
+ function inferChannel(current) {
165
+ return isValidVersion(current) && parseVersion(current).prerelease.length > 0 ? 'beta' : 'stable'
166
+ }
167
+
174
168
  /**
175
169
  * @param {{ home: string, fetchImpl?: Function, installMethod: string, current: string,
176
170
  * now?: number, ttlMs?: number, force?: boolean,
@@ -190,7 +184,7 @@ async function checkForUpdate(opts) {
190
184
  registryUrl,
191
185
  latestUrl,
192
186
  timeoutMs,
193
- channel = 'stable',
187
+ channel = inferChannel(current),
194
188
  } = opts
195
189
 
196
190
  const cacheFile = updateCachePath(home)
@@ -213,9 +207,15 @@ async function checkForUpdate(opts) {
213
207
  }
214
208
  }
215
209
 
216
- const latest = installMethod === 'binary'
217
- ? await getLatestVersionGithub({ fetchImpl, latestUrl, timeoutMs, includePrerelease: channel === 'beta' })
218
- : await getLatestVersionNpm({ fetchImpl, registryUrl, timeoutMs, distTag: channel })
210
+ // Beta channel always resolves via the npm registry `beta` dist-tag — the
211
+ // GitHub releases API is rate-limited (403) for unauthenticated callers.
212
+ // Binary installs still download assets from GitHub, but resolve the
213
+ // version number from npm where it is published in lockstep with releases.
214
+ const latest = channel === 'beta'
215
+ ? await getLatestVersionNpm({ fetchImpl, registryUrl, timeoutMs, distTag: 'beta' })
216
+ : installMethod === 'binary'
217
+ ? await getLatestVersionGithub({ fetchImpl, latestUrl, timeoutMs })
218
+ : await getLatestVersionNpm({ fetchImpl, registryUrl, timeoutMs })
219
219
 
220
220
  const checkedAt = now
221
221
  try {
@@ -452,6 +452,7 @@ module.exports = {
452
452
  compareVersions,
453
453
  isValidVersion,
454
454
  parseVersion,
455
+ inferChannel,
455
456
  getLatestVersionNpm,
456
457
  getLatestVersionGithub,
457
458
  getInstallMethod,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jonathanleelx/meddle",
3
- "version": "0.4.0-beta.4",
3
+ "version": "0.4.0-beta.6",
4
4
  "description": "A simple development proxy server with custom routing rules, mock responses, HTTPS interception, and a plugin system",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",