packages/reflex-cache: decompress xz

This commit is contained in:
Max Headroom 2023-11-10 00:31:41 +01:00
parent 4c25e003b7
commit bd08dcda97
3 changed files with 23 additions and 7 deletions

View file

@ -3,6 +3,7 @@ from urllib.parse import quote_plus
import requests import requests
import requests_unixsocket import requests_unixsocket
import lzma
class IPFSController: class IPFSController:
@ -16,6 +17,12 @@ class IPFSController:
print(f"Downloading NAR: {nar}") print(f"Downloading NAR: {nar}")
code, _, content = self.__nix.try_all("get", nar, hint) code, _, content = self.__nix.try_all("get", nar, hint)
if code == 200: if code == 200:
if nar.endswith(".nar.xz"):
print(f"Attempt decompression of {nar}")
decompressed = lzma.decompress(content)
print(f"Size diff: {len(content)} -> {len(decompressed)}")
content = decompressed
upload = {"file": ("FILE", content, "application/octet-stream")} upload = {"file": ("FILE", content, "application/octet-stream")}
try: try:
rIpfs = requests_unixsocket.post( rIpfs = requests_unixsocket.post(

View file

@ -30,7 +30,7 @@ class NixCacheFetcher:
caches = self.__caches caches = self.__caches
for cache in caches: for cache in caches:
try: try:
rCache = fn(f"{cache}{path}") rCache = fn(f"{cache}{path}", allow_redirects=True)
if rCache.status_code < bestState: if rCache.status_code < bestState:
bestState = rCache.status_code bestState = rCache.status_code

View file

@ -50,11 +50,7 @@ class ReflexHTTPServiceHandler(BaseHTTPRequestHandler):
if resultHash == None: if resultHash == None:
code, cache, _ = self._nix.try_all("head", self.path) code, cache, _ = self._nix.try_all("head", self.path)
if code == 200: if code != 200:
self.send_response(302)
self.send_header("Location", f"{cache}{self.path}")
self.end_headers()
else:
self.send_response(404) self.send_response(404)
self.end_headers() self.end_headers()
return return
@ -82,6 +78,13 @@ class ReflexHTTPServiceHandler(BaseHTTPRequestHandler):
self._ipfs.ipfs_fetch_task, cb, self.path, cache self._ipfs.ipfs_fetch_task, cb, self.path, cache
) )
self._workSet.add((self.path, f)) self._workSet.add((self.path, f))
_, code, resultHash = f.result()
else:
code = 200
if code != 200:
self.send_response(code)
self.end_headers()
return return
self.send_response(302) self.send_response(302)
@ -113,11 +116,15 @@ class ReflexHTTPServiceHandler(BaseHTTPRequestHandler):
self.send_response(code) self.send_response(code)
self.end_headers() self.end_headers()
if code == 200: if code == 200:
self.wfile.write(content)
if match := re.search( if match := re.search(
"URL: (nar/[a-z0-9]*\\.nar.*)", content.decode("utf-8") "URL: (nar/[a-z0-9]*\\.nar.*)", content.decode("utf-8")
): ):
nar = f"/{match.group(1)}" nar = f"/{match.group(1)}"
# we decompress xz, so tell Nix to except an uncompressed NAR
if nar.endswith(".xz"):
self.wfile.write(content.replace(b"Compression: xz\n", b"Compression: none\n"))
else:
self.wfile.write(content)
if not self._db.get_path(nar): if not self._db.get_path(nar):
with self._workSetLock: with self._workSetLock:
found = False found = False
@ -139,6 +146,8 @@ class ReflexHTTPServiceHandler(BaseHTTPRequestHandler):
self._ipfs.ipfs_fetch_task, cb, nar self._ipfs.ipfs_fetch_task, cb, nar
) )
self._workSet.add((nar, f)) self._workSet.add((nar, f))
else:
self.wfile.write(content)
return return
else: else: