Skip to content

Commit e3c3827

Browse files
committed
Avoid calling require('lz4') if it's really not required
This changes the utlity module to return a function instead of the module directly. This way, caller can control when the module is actually tried to be resolved.
1 parent 00f55c7 commit e3c3827

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

lib/DBSQLSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default class DBSQLSession implements IDBSQLSession {
232232
}
233233

234234
if (ProtocolVersion.supportsArrowCompression(this.serverProtocolVersion) && request.canDownloadResult !== true) {
235-
request.canDecompressLZ4Result = (options.useLZ4Compression ?? clientConfig.useLZ4Compression) && Boolean(LZ4);
235+
request.canDecompressLZ4Result = (options.useLZ4Compression ?? clientConfig.useLZ4Compression) && Boolean(LZ4());
236236
}
237237

238238
const operationPromise = driver.executeStatement(request);

lib/result/ArrowResultHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class ArrowResultHandler implements IResultsProvider<ArrowBatch>
2626
this.arrowSchema = arrowSchema ?? hiveSchemaToArrowSchema(schema);
2727
this.isLZ4Compressed = lz4Compressed ?? false;
2828

29-
if (this.isLZ4Compressed && !LZ4) {
29+
if (this.isLZ4Compressed && !LZ4()) {
3030
throw new HiveDriverError('Cannot handle LZ4 compressed result: module `lz4` not installed');
3131
}
3232
}
@@ -52,7 +52,7 @@ export default class ArrowResultHandler implements IResultsProvider<ArrowBatch>
5252
let totalRowCount = 0;
5353
rowSet?.arrowBatches?.forEach(({ batch, rowCount }) => {
5454
if (batch) {
55-
batches.push(this.isLZ4Compressed ? LZ4!.decode(batch) : batch);
55+
batches.push(this.isLZ4Compressed ? LZ4()!.decode(batch) : batch);
5656
totalRowCount += rowCount.toNumber(true);
5757
}
5858
});

lib/result/CloudFetchResultHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
2727
this.source = source;
2828
this.isLZ4Compressed = lz4Compressed ?? false;
2929

30-
if (this.isLZ4Compressed && !LZ4) {
30+
if (this.isLZ4Compressed && !LZ4()) {
3131
throw new HiveDriverError('Cannot handle LZ4 compressed result: module `lz4` not installed');
3232
}
3333
}
@@ -64,7 +64,7 @@ export default class CloudFetchResultHandler implements IResultsProvider<ArrowBa
6464
}
6565

6666
if (this.isLZ4Compressed) {
67-
batch.batches = batch.batches.map((buffer) => LZ4!.decode(buffer));
67+
batch.batches = batch.batches.map((buffer) => LZ4()!.decode(buffer));
6868
}
6969
return batch;
7070
}

lib/utils/lz4.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ function tryLoadLZ4Module(): LZ4Module | undefined {
2626
}
2727
}
2828

29-
export default tryLoadLZ4Module();
29+
// The null is already tried resolving that failed
30+
let resolvedModule: LZ4Module | null | undefined;
31+
32+
function getResolvedModule() {
33+
if (resolvedModule === undefined) {
34+
resolvedModule = tryLoadLZ4Module() ?? null;
35+
}
36+
return resolvedModule ?? undefined;
37+
}
38+
39+
export default getResolvedModule;

0 commit comments

Comments
 (0)