Skip to content

Conversation

@indexzero
Copy link

@indexzero indexzero commented Sep 29, 2025

Howdy @jdalton 👋 been a few years. Hope that you've been doing well 🙏

What is the issue?

The simplest possible repro via node -pe:

pnpm install @socketregistry/packageurl-js@1.1.2

node -pe "(new (require('@socketregistry/packageurl-js').PackageURL)('npm', '@babel', 'runtime', '7.18.6', null, 'helpers/typeof.js').toString())"

yields

./node_modules/.pnpm/@socketregistry+packageurl-js@1.1.2/node_modules/@socketregistry/packageurl-js/dist/purl-type.js:316
            if ((0, encode_js_1.encodeComponent)(name) !== name) {
                                                ^

TypeError: (0 , encode_js_1.encodeComponent) is not a function
    at Object.npm [as validate] (./node_modules/.pnpm/@socketregistry+packageurl-js@1.1.2/node_modules/@socketregistry/packageurl-js/dist/purl-type.js:316:49)
    at new PackageURL (./node_modules/.pnpm/@socketregistry+packageurl-js@1.1.2/node_modules/@socketregistry/packageurl-js/dist/package-url.js:103:40)
    at [eval]:1:2
    at runScriptInThisContext (node:internal/vm:209:10)
    at node:internal/process/execution:449:12
    at [eval]-wrapper:6:24
    at runScriptInContext (node:internal/process/execution:447:60)
    at evalFunction (node:internal/process/execution:87:30)
    at evalScript (node:internal/process/execution:99:3)
    at node:internal/main/eval_string:74:3

The 1-liner above is just the node -pe equivalent to the example from README.md:

import { PackageURL } from '@socketregistry/packageurl-js';

const subpathPurl = new PackageURL('npm', '@babel', 'runtime', '7.18.6', null, 'helpers/typeof.js')

console.log(subpathPurl.toString());

Why does this fix the issue?

There appears to be a subtle bug in tsgo that is causing the destructured version of this assignment to transpile to:

exports.encodeComponent = void 0;

I'm far from an expert in that domain, but this does resolve the issue:

# Checkout the branch

# Blow away any existing `tsconfig.tsbuildinfo`
rm -f tsconfig.tsbuildinfo

# Rebuild via tsgo
pnpm build

# Get the result expected
node -pe "(new (require('./dist/index.js').PackageURL)('npm', '@babel', 'runtime', '7.18.6', null, 'helpers/typeof.js').toString())"

which yields

pkg:npm/%40babel/runtime@7.18.6#helpers/typeof.js

…t = void 0;`

Signed-off-by: indexzero <charlie.robbins@gmail.com>
@jdalton jdalton added the bug Something isn't working label Sep 29, 2025
@jdalton
Copy link
Collaborator

jdalton commented Sep 29, 2025

Thank you @indexzero! I'll add some commits around this and clear caches as part of builds... interesting the tests didn't catch this...

@jdalton jdalton merged commit 9cf70e9 into SocketDev:main Sep 29, 2025
4 checks passed
@jdalton
Copy link
Collaborator

jdalton commented Sep 29, 2025

@indexzero Thank you for the kind greeting by the way! I am doing good! Hope you're doing well too.

jdalton pushed a commit that referenced this pull request Sep 29, 2025
…t = void 0;` (#3)

Signed-off-by: indexzero <charlie.robbins@gmail.com>
jdalton added a commit that referenced this pull request Sep 29, 2025
Document why we avoid destructuring from global objects due to a tsgo
transpilation bug that causes runtime errors. References issue #3.
jdalton added a commit to SocketDev/socket-registry that referenced this pull request Sep 29, 2025
Replace destructuring from global objects with direct property access
to work around tsgo transpilation bug that causes runtime errors.
References: SocketDev/socket-packageurl-js#3
@indexzero
Copy link
Author

indexzero commented Sep 29, 2025

Thank you @indexzero! I'll add some commits around this and clear caches as part of builds... interesting the tests didn't catch this...

It looks like vitest uses esbuild in an automagic way under the covers? A separate transpiler toolchain would def explain how this bug snuck through

@indexzero Thank you for the kind greeting by the way! I am doing good! Hope you're doing well too.

Thanks 🫶 it is fun being back in the saddle with Node & JS again. Spent a few years in different worlds (energy & climate are mostly Python) which were educational though 🤓

@jdalton
Copy link
Collaborator

jdalton commented Sep 29, 2025

I'm following up with commits to have tests run both /src coverage, and /dist regular testing to catch issues in both sides of things

@indexzero
Copy link
Author

That's a solid plan @jdalton. Thanks for creating / maintaining this functionality! It's been really useful separate from other libraries for purl (packageurl-js) and purl(ish) (npm-package-arg) strings 🥇

jdalton added a commit to SocketDev/socket-registry that referenced this pull request Sep 29, 2025
Replace destructuring from global objects with direct property access
to work around tsgo transpilation bug that causes runtime errors.
References: SocketDev/socket-packageurl-js#3
jdalton added a commit to SocketDev/socket-registry that referenced this pull request Sep 30, 2025
Replace destructuring from global objects with direct property access
to work around tsgo transpilation bug that causes runtime errors.
References: SocketDev/socket-packageurl-js#3
jdalton added a commit to SocketDev/socket-registry that referenced this pull request Oct 22, 2025
Replace destructuring from global objects with direct property access
to work around tsgo transpilation bug that causes runtime errors.
References: SocketDev/socket-packageurl-js#3
jdalton pushed a commit that referenced this pull request Oct 22, 2025
…t = void 0;` (#3)

Signed-off-by: indexzero <charlie.robbins@gmail.com>
jdalton added a commit that referenced this pull request Oct 22, 2025
Document why we avoid destructuring from global objects due to a tsgo
transpilation bug that causes runtime errors. References issue #3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants