Skip to content

Commit 2cd09dd

Browse files
committed
chore: reusing existing package parser routines
1 parent 23b64fa commit 2cd09dd

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

lib/npa.js

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function resolve (name, spec, where, arg) {
106106
} else if (isAliasSpec(spec)) {
107107
return fromAlias(res, where)
108108
} else if (isJsrSpec(spec)) {
109-
return fromJsr(res)
109+
return fromJsr(res, where)
110110
}
111111

112112
const hosted = HostedGit.fromUrl(spec, {
@@ -462,58 +462,42 @@ function fromAlias (res, where) {
462462
return res
463463
}
464464

465-
function fromJsr (res) {
465+
function fromJsr (res, where) {
466466
// Remove 'jsr:' prefix
467467
const jsrSpec = res.rawSpec.substr(4)
468468

469469
// Parse the JSR specifier to extract name and version
470470
// JSR format: @scope/name or @scope/name@version
471-
const nameEnd = jsrSpec.indexOf('@', 1) // Skip the leading @ in @scope
472-
const jsrName = nameEnd > 0 ? jsrSpec.slice(0, nameEnd) : jsrSpec
473-
const versionSpec = nameEnd > 0 ? jsrSpec.slice(nameEnd + 1) : ''
471+
const versionIndex = jsrSpec.indexOf('@', 1)
472+
const packagePart = versionIndex > 0 ? jsrSpec.slice(0, versionIndex) : jsrSpec
474473

475474
// Validate that JSR package is scoped
476-
if (!jsrName.startsWith('@') || !jsrName.includes('/')) {
475+
if (!packagePart.startsWith('@') || !packagePart.includes('/')) {
477476
throw new Error(`JSR packages must be scoped (e.g., jsr:@scope/name): ${res.raw}`)
478477
}
479478

480-
// Validate the package name
481-
const valid = validatePackageName(jsrName)
482-
if (!valid.validForOldPackages) {
483-
throw invalidPackageName(jsrName, valid, res.raw)
479+
const subSpec = npa(jsrSpec, where)
480+
481+
// Validate that it was parsed as a registry dependency
482+
if (!subSpec.registry) {
483+
throw new Error('JSR packages must be registry dependencies')
484484
}
485485

486486
// Transform @scope/name to @jsr/scope__name
487487
// Extract scope and package name
488-
const scopeEnd = jsrName.indexOf('/')
489-
const scope = jsrName.slice(1, scopeEnd) // Remove leading @ from scope
490-
const packageName = jsrName.slice(scopeEnd + 1)
491-
const transformedName = `@jsr/${scope}__${packageName}`
488+
const originalScope = subSpec.scope.slice(1) // Remove leading @ from scope
489+
const packageName = subSpec.name.slice(subSpec.scope.length + 1)
490+
const transformedName = `@jsr/${originalScope}__${packageName}`
492491

493-
// Set the transformed name
492+
// Set the transformed name and copy properties from subSpec
494493
res.setName(transformedName)
495494
res.registry = true
495+
res.type = subSpec.type
496+
res.fetchSpec = subSpec.fetchSpec
497+
res.rawSpec = subSpec.rawSpec
496498

497-
// Preserve the original JSR spec for saving
498-
res.saveSpec = `jsr:${jsrName}${versionSpec ? '@' + versionSpec : ''}`
499-
500-
// Determine the type based on version specifier
501-
const spec = versionSpec || '*'
502-
res.rawSpec = spec
503-
res.fetchSpec = spec
504-
505-
const version = semver.valid(spec, true)
506-
const range = semver.validRange(spec, true)
507-
if (version) {
508-
res.type = 'version'
509-
} else if (range) {
510-
res.type = 'range'
511-
} else {
512-
if (encodeURIComponent(spec) !== spec) {
513-
throw invalidTagName(spec, res.raw)
514-
}
515-
res.type = 'tag'
516-
}
499+
// Preserve original JSR spec for saving
500+
res.saveSpec = res.raw
517501

518502
return res
519503
}

test/jsr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ t.test('JSR validation errors', t => {
136136
t.test('invalid package name characters', t => {
137137
t.throws(
138138
() => npa('jsr:@scope/in valid'),
139-
/Invalid package name/,
140-
'throws error for invalid package name with spaces'
139+
/JSR packages must be registry dependencies/,
140+
'throws error when package is parsed as non-registry (e.g., directory)'
141141
)
142142
t.end()
143143
})

0 commit comments

Comments
 (0)