Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ containing all the sources needed for the given package manager.
If you're on npm or pnpm and you don't want to include devel dependencies, pass `--no-devel`.
For npm, also pass `--production` to `npm install` itself.

For pnpm v12, pass `--pnpm-store-version v11` and run `pnpm install --offline --frozen-lockfile --trust-lockfile`.
Without `--trust-lockfile`, pnpm checks the lockfile against the registry and fails offline.

If you're using npm, you must run this script when the `node_modules` directory is **NOT** present.
If you generate the `generated-sources.json` in CI, you can do this by passing `--package-lock-only`
to `npm install`.
Expand Down
35 changes: 34 additions & 1 deletion node/flatpak_node_generator/populate_pnpm_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@

_SANITIZE_RE = re.compile(r'[\\/:*?"<>|]')
_MAX_LENGTH_WITHOUT_HASH = 120
# These are package.json fields pnpm keeps in its store index (normalizeBundledManifest.ts)
_MANIFEST_FIELDS = (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the source of these fields? Is there any documentation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're the package.json fields pnpm keeps in its store index, from normalizeBundledManifest.ts: https://github.com/pnpm/pnpm/blob/27be1a9a5aa7e7728443545ce1ccfce619212074/pnpm11/store/cafs/src/normalizeBundledManifest.ts

I synced the list with it and added a comment explaining where they come from.

'bin',
'bundledDependencies',
'bundleDependencies',
'cpu',
'dependencies',
'devDependencies',
'directories',
'engines',
'gypfile',
'libc',
'optionalDependencies',
'os',
'peerDependencies',
'peerDependenciesMeta',
)
_MANIFEST_LIFECYCLE_SCRIPTS = ('preinstall', 'install', 'postinstall')


def _msgpack_pack(obj: object) -> bytes:
Expand Down Expand Up @@ -120,7 +138,7 @@ def _requires_prepare(

def _pack_v11_store_entry(
files: dict[str, dict[str, object]],
manifest: dict[str, str] | None = None,
manifest: Mapping[str, object] | None = None,
requires_prepare: bool | None = None,
) -> bytes:
"""Encode a store v11 entry using msgpackr-compatible record extensions.
Expand Down Expand Up @@ -250,6 +268,7 @@ def _process_tarball(
real_pkg_version = pkg_version
pkg_scripts: Mapping[str, object] | None = None
pkg_main: str | None = None
pkg_manifest: dict[str, object] = {}
rel_names: set[str] = set()

with tarfile.open(tarball_path, 'r:gz') as tf:
Expand All @@ -275,6 +294,19 @@ def _process_tarball(
pkg_scripts = pkg_data['scripts']
if isinstance(pkg_data.get('main'), str):
pkg_main = pkg_data['main']
pkg_manifest = {
k: pkg_data[k]
for k in _MANIFEST_FIELDS
if pkg_data.get(k) is not None
}
if pkg_scripts is not None:
lifecycle_scripts = {
k: pkg_scripts[k]
for k in _MANIFEST_LIFECYCLE_SCRIPTS
if pkg_scripts.get(k)
}
if lifecycle_scripts:
pkg_manifest['scripts'] = lifecycle_scripts

digest = hashlib.sha512(data).digest()
file_hex = digest.hex()
Expand Down Expand Up @@ -326,6 +358,7 @@ def _process_tarball(
manifest = None
if real_pkg_name or real_pkg_version:
manifest = {
**pkg_manifest,
'name': real_pkg_name,
'version': real_pkg_version,
}
Expand Down
71 changes: 71 additions & 0 deletions node/tests/test_populate_pnpm_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,77 @@ def test_process_tarball_v11(tmp_path: Path) -> None:
db.close()


def test_process_tarball_v11_manifest_fields(tmp_path: Path) -> None:
tar_path = tmp_path / 'pkg.tgz'
store_dir = tmp_path / 'store' / 'v11'
pkg_json = json.dumps(
{
'name': 'real-pkg',
'version': '1.2.3',
'bin': {'real-pkg': 'bin/cli.js'},
'dependencies': {'dep': '^1.0.0'},
'engines': {'node': '>=18'},
'gypfile': True,
'os': None,
'description': 'not kept',
'scripts': {'postinstall': 'node install.js', 'test': 'not kept'},
}
)

_create_tarball(
tar_path,
{'package/package.json': pkg_json, 'package/bin/cli.js': "console.log('hi');"},
)

store_dir.mkdir(parents=True, exist_ok=True)
db = sqlite3.connect(str(store_dir / 'index.db'))
db.execute(
'CREATE TABLE IF NOT EXISTS package_index ('
' key TEXT PRIMARY KEY,'
' data BLOB NOT NULL'
') WITHOUT ROWID'
)

integrity = Integrity(
'sha256', 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2'
)

try:
_process_tarball(
tarball_path=str(tar_path),
pkg_name='real-pkg',
pkg_version='1.2.3',
integrity=integrity.to_base64(),
integrity_digest=integrity.digest,
integrity_algo=integrity.algorithm,
store=str(store_dir),
now=1234567890,
store_version='v11',
index_db=db,
)
db.commit()

expected_key = f'{integrity.algorithm}-{integrity.to_base64()}\treal-pkg@1.2.3'
row = db.execute(
'SELECT data FROM package_index WHERE key = ?', (expected_key,)
).fetchone()
assert row is not None

# pnpm v12 links bins from the manifest in the store index
data = _decode_v11(row[0])
assert data['manifest'] == {
'name': 'real-pkg',
'version': '1.2.3',
'bin': {'real-pkg': 'bin/cli.js'},
'dependencies': {'dep': '^1.0.0'},
'engines': {'node': '>=18'},
'gypfile': True,
'scripts': {'postinstall': 'node install.js'},
}
finally:
db.close()


def test_process_tarball_v11_no_package_json(tmp_path: Path) -> None:
tar_path = tmp_path / 'pkg.tgz'
store_dir = tmp_path / 'store' / 'v11'
Expand Down
Loading