From 3a797447136812e9785ad7524adfda6078b884ac Mon Sep 17 00:00:00 2001 From: Jake Lane Date: Thu, 2 Nov 2023 14:59:26 +1100 Subject: [PATCH] Bring back string as allowed large blob type --- packages/core/cache/src/FSCache.js | 9 +++++++-- packages/core/cache/src/IDBCache.browser.js | 2 +- packages/core/cache/src/LMDBCache.js | 9 +++++++-- packages/core/cache/src/types.js | 2 +- packages/dev/bundle-stats-cli/src/cli.js | 4 ++-- packages/dev/query/src/index.js | 8 ++++---- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/core/cache/src/FSCache.js b/packages/core/cache/src/FSCache.js index 48577ae803c..cc4b85e33e6 100644 --- a/packages/core/cache/src/FSCache.js +++ b/packages/core/cache/src/FSCache.js @@ -102,7 +102,7 @@ export class FSCache implements Cache { return Buffer.concat(await Promise.all(buffers)); } - async setLargeBlob(key: string, contents: Buffer): Promise { + async setLargeBlob(key: string, contents: Buffer | string): Promise { const chunks = Math.ceil(contents.length / WRITE_LIMIT_CHUNK); const writePromises: Promise[] = []; @@ -110,7 +110,12 @@ export class FSCache implements Cache { writePromises.push( this.fs.writeFile( this.#getFilePath(key, i), - contents.subarray(i * WRITE_LIMIT_CHUNK, (i + 1) * WRITE_LIMIT_CHUNK), + typeof contents === 'string' + ? contents.slice(i * WRITE_LIMIT_CHUNK, (i + 1) * WRITE_LIMIT_CHUNK) + : contents.subarray( + i * WRITE_LIMIT_CHUNK, + (i + 1) * WRITE_LIMIT_CHUNK, + ), ), ); } diff --git a/packages/core/cache/src/IDBCache.browser.js b/packages/core/cache/src/IDBCache.browser.js index 6de017c5906..15fbf65adbb 100644 --- a/packages/core/cache/src/IDBCache.browser.js +++ b/packages/core/cache/src/IDBCache.browser.js @@ -114,7 +114,7 @@ export class IDBCache implements Cache { return this.getBlob(key); } - setLargeBlob(key: string, contents: Buffer): Promise { + setLargeBlob(key: string, contents: Buffer | string): Promise { return this.setBlob(key, contents); } diff --git a/packages/core/cache/src/LMDBCache.js b/packages/core/cache/src/LMDBCache.js index bfe6428dae9..01cf6f74351 100644 --- a/packages/core/cache/src/LMDBCache.js +++ b/packages/core/cache/src/LMDBCache.js @@ -111,7 +111,7 @@ export class LMDBCache implements Cache { return Buffer.concat(await Promise.all(buffers)); } - async setLargeBlob(key: string, contents: Buffer): Promise { + async setLargeBlob(key: string, contents: Buffer | string): Promise { const chunks = Math.ceil(contents.length / WRITE_LIMIT_CHUNK); const writePromises: Promise[] = []; @@ -119,7 +119,12 @@ export class LMDBCache implements Cache { writePromises.push( this.fs.writeFile( this.#getFilePath(key, i), - contents.subarray(i * WRITE_LIMIT_CHUNK, (i + 1) * WRITE_LIMIT_CHUNK), + typeof contents === 'string' + ? contents.slice(i * WRITE_LIMIT_CHUNK, (i + 1) * WRITE_LIMIT_CHUNK) + : contents.subarray( + i * WRITE_LIMIT_CHUNK, + (i + 1) * WRITE_LIMIT_CHUNK, + ), ), ); } diff --git a/packages/core/cache/src/types.js b/packages/core/cache/src/types.js index 55063964bbd..163b3d57644 100644 --- a/packages/core/cache/src/types.js +++ b/packages/core/cache/src/types.js @@ -12,7 +12,7 @@ export interface Cache { setBlob(key: string, contents: Buffer | string): Promise; hasLargeBlob(key: string): Promise; getLargeBlob(key: string): Promise; - setLargeBlob(key: string, contents: Buffer): Promise; + setLargeBlob(key: string, contents: Buffer | string): Promise; getBuffer(key: string): Promise; /** * In a multi-threaded environment, where there are potentially multiple Cache diff --git a/packages/dev/bundle-stats-cli/src/cli.js b/packages/dev/bundle-stats-cli/src/cli.js index c4deed1549c..fd60930f27b 100644 --- a/packages/dev/bundle-stats-cli/src/cli.js +++ b/packages/dev/bundle-stats-cli/src/cli.js @@ -18,9 +18,9 @@ import {getBundleStats} from '@parcel/reporter-bundle-stats/src/BundleStatsRepor import {PackagedBundle as PackagedBundleClass} from '@parcel/core/src/public/Bundle'; import type {commander$Command} from 'commander'; -function run({cacheDir, outDir}) { +async function run({cacheDir, outDir}) { // 1. load bundle graph and info via parcel~query - let {bundleGraph, bundleInfo} = loadGraphs(cacheDir); + let {bundleGraph, bundleInfo} = await loadGraphs(cacheDir); if (bundleGraph == null) { console.error('Bundle Graph could not be found'); diff --git a/packages/dev/query/src/index.js b/packages/dev/query/src/index.js index 75630136639..1746fd010db 100644 --- a/packages/dev/query/src/index.js +++ b/packages/dev/query/src/index.js @@ -94,7 +94,7 @@ export async function loadGraphs(cacheDir: string): Promise<{| ); if (bundleGraphRequestNode != null) { bundleGraph = BundleGraph.deserialize( - (await loadLargeBlobRequestRequestSync(cache, bundleGraphRequestNode)) + (await loadLargeBlobRequestRequest(cache, bundleGraphRequestNode)) .bundleGraph.value, ); @@ -103,8 +103,8 @@ export async function loadGraphs(cacheDir: string): Promise<{| ).find(n => n.type === 'request' && n.value.type === 'asset_graph_request'); if (assetGraphRequest != null) { assetGraph = AssetGraph.deserialize( - (await loadLargeBlobRequestRequestSync(cache, assetGraphRequest)) - .assetGraph.value, + (await loadLargeBlobRequestRequest(cache, assetGraphRequest)).assetGraph + .value, ); } } @@ -124,7 +124,7 @@ export async function loadGraphs(cacheDir: string): Promise<{| return {assetGraph, bundleGraph, requestTracker, bundleInfo}; } -async function loadLargeBlobRequestRequestSync(cache, node) { +async function loadLargeBlobRequestRequest(cache, node) { invariant(node.type === 'request'); return v8.deserialize( await cache.getLargeBlob(nullthrows(node.value.resultCacheKey)),