Skip to content

Commit

Permalink
Bring back string as allowed large blob type
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLane committed Nov 2, 2023
1 parent be2ebd2 commit 3a79744
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
9 changes: 7 additions & 2 deletions packages/core/cache/src/FSCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ export class FSCache implements Cache {
return Buffer.concat(await Promise.all(buffers));
}
async setLargeBlob(key: string, contents: Buffer): Promise<void> {
async setLargeBlob(key: string, contents: Buffer | string): Promise<void> {
const chunks = Math.ceil(contents.length / WRITE_LIMIT_CHUNK);
const writePromises: Promise<void>[] = [];
for (let i = 0; i < chunks; i += 1) {
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,
),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/cache/src/IDBCache.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class IDBCache implements Cache {
return this.getBlob(key);
}

setLargeBlob(key: string, contents: Buffer): Promise<void> {
setLargeBlob(key: string, contents: Buffer | string): Promise<void> {
return this.setBlob(key, contents);
}

Expand Down
9 changes: 7 additions & 2 deletions packages/core/cache/src/LMDBCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,20 @@ export class LMDBCache implements Cache {
return Buffer.concat(await Promise.all(buffers));
}

async setLargeBlob(key: string, contents: Buffer): Promise<void> {
async setLargeBlob(key: string, contents: Buffer | string): Promise<void> {
const chunks = Math.ceil(contents.length / WRITE_LIMIT_CHUNK);

const writePromises: Promise<void>[] = [];
for (let i = 0; i < chunks; i += 1) {
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,
),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/cache/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface Cache {
setBlob(key: string, contents: Buffer | string): Promise<void>;
hasLargeBlob(key: string): Promise<boolean>;
getLargeBlob(key: string): Promise<Buffer>;
setLargeBlob(key: string, contents: Buffer): Promise<void>;
setLargeBlob(key: string, contents: Buffer | string): Promise<void>;
getBuffer(key: string): Promise<?Buffer>;
/**
* In a multi-threaded environment, where there are potentially multiple Cache
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/bundle-stats-cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions packages/dev/query/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand All @@ -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,
);
}
}
Expand All @@ -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)),
Expand Down

0 comments on commit 3a79744

Please sign in to comment.