Skip to content

Commit c7d54bf

Browse files
node: Fix pnpm incompatible handling of non-lowercase / long package names
1 parent 5c73dc4 commit c7d54bf

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

node/flatpak_node_generator/populate_pnpm_store.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import time
1212

1313
_SANITIZE_RE = re.compile(r'[\\/:*?"<>|]')
14+
_MAX_LENGTH_WITHOUT_HASH = 120
1415

1516

1617
def populate_store(manifest_path: str, tarball_dir: str, store_dir: str) -> None:
@@ -134,6 +135,11 @@ def _process_tarball(
134135
json.dump(index_data, out)
135136
else:
136137
url_dir_name = re.sub(r'[:/]', '+', tarball_url)
138+
if (
139+
len(url_dir_name) > _MAX_LENGTH_WITHOUT_HASH
140+
or url_dir_name != url_dir_name.lower()
141+
):
142+
url_dir_name = f'{url_dir_name[: _MAX_LENGTH_WITHOUT_HASH - 33]}_{hashlib.sha256(url_dir_name.encode()).hexdigest()[:32]}'
137143
url_idx_dir = os.path.join(store, url_dir_name)
138144
os.makedirs(url_idx_dir, exist_ok=True)
139145
url_idx_path = os.path.join(url_idx_dir, 'integrity.json')

node/tests/test_populate_pnpm_store.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
import json
23
import re
34
import tarfile
@@ -128,3 +129,53 @@ def test_process_tarball_with_tarball_url_v6(tmp_path: Path) -> None:
128129
url_idx_file = store_dir / url_dir_name / 'integrity.json'
129130

130131
assert url_idx_file.exists()
132+
133+
134+
def test_process_tarball_with_uppercase_path(tmp_path: Path) -> None:
135+
tar_path = tmp_path / 'pkg.tgz'
136+
store_dir = tmp_path / 'store'
137+
tarball_url = 'https://example.com/PKG.tgz'
138+
139+
_create_tarball(tar_path, {'package/index.js': "console.log('hello');"})
140+
141+
_process_tarball(
142+
tarball_path=str(tar_path),
143+
pkg_name='pkg',
144+
pkg_version='1.0.0',
145+
integrity_hex='a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2',
146+
store=str(store_dir),
147+
now=1234567890,
148+
tarball_url=tarball_url,
149+
store_version='v6',
150+
)
151+
152+
sanitized_tarball_url = re.sub(r'[:/]', '+', tarball_url)
153+
normalized_tarball_url = f'{sanitized_tarball_url}_{hashlib.sha256(sanitized_tarball_url.encode()).hexdigest()[:32]}'
154+
url_idx_file = store_dir / normalized_tarball_url / 'integrity.json'
155+
156+
assert url_idx_file.exists()
157+
158+
159+
def test_process_tarball_with_long_path(tmp_path: Path) -> None:
160+
tar_path = tmp_path / 'pkg.tgz'
161+
store_dir = tmp_path / 'store'
162+
tarball_url = f'https://example.com{"pkg" * 50}.tgz'
163+
164+
_create_tarball(tar_path, {'package/index.js': "console.log('hello');"})
165+
166+
_process_tarball(
167+
tarball_path=str(tar_path),
168+
pkg_name='pkg',
169+
pkg_version='1.0.0',
170+
integrity_hex='a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2',
171+
store=str(store_dir),
172+
now=1234567890,
173+
tarball_url=tarball_url,
174+
store_version='v6',
175+
)
176+
177+
sanitized_tarball_url = re.sub(r'[:/]', '+', tarball_url)
178+
normalized_tarball_url = f'{sanitized_tarball_url[:87]}_{hashlib.sha256(sanitized_tarball_url.encode()).hexdigest()[:32]}'
179+
url_idx_file = store_dir / normalized_tarball_url / 'integrity.json'
180+
181+
assert url_idx_file.exists()

0 commit comments

Comments
 (0)