Skip to content

Commit 5bb7654

Browse files
authored
refactor(bigquery): drop the apache-arrow version check
apache-arrow is a regular ^21.0.0 dependency, so the install already guarantees its version. Its runtime check also needed loadPackageJson, because apache-arrow's exports map blocks require('apache-arrow/ package.json'). Load it with a plain lazy require and remove loadPackageJson. Keep the @google-cloud/bigquery-storage check. Package managers only warn when an installed version is outside an optional peer range, and an older version's protos silently drop newer request fields such as arrowSerializationOptions.
1 parent f378c1f commit 5bb7654

2 files changed

Lines changed: 38 additions & 70 deletions

File tree

‎handwritten/bigquery/src/arrow.ts‎

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import {ResourceStream} from '@google-cloud/paginator';
18-
import * as path from 'path';
1918
import type {RecordBatch, Schema} from 'apache-arrow';
2019
import type {BigQuery, Query} from './bigquery';
2120
import type bigquery from './types';
@@ -36,24 +35,6 @@ export type ArrowRecordBatchStream = ResourceStream<RecordBatch> & {
3635
jobReference?: bigquery.IJobReference;
3736
};
3837

39-
/**
40-
* Loads `package.json` for an installed module, falling back to resolving the
41-
* package root directory when package `exports` block `./package.json`.
42-
*
43-
* @private
44-
*/
45-
function loadPackageJson(
46-
requireFn: NodeRequire,
47-
pkgName: string,
48-
): {version?: string} {
49-
try {
50-
return requireFn(`${pkgName}/package.json`);
51-
} catch {
52-
const entryPath = (requireFn.resolve || require.resolve)(pkgName);
53-
return requireFn(path.join(path.dirname(entryPath), 'package.json'));
54-
}
55-
}
56-
5738
/**
5839
* Requires a dependency and verifies its minimum major.minor version.
5940
*
@@ -70,7 +51,7 @@ function requireWithMinVersion<T = unknown>(
7051
let pkg: {version?: string};
7152
try {
7253
mod = requireFn(pkgName);
73-
pkg = loadPackageJson(requireFn, pkgName);
54+
pkg = requireFn(`${pkgName}/package.json`);
7455
} catch (err) {
7556
throw new Error(
7657
`Arrow support requires the "${pkgName}" package (>= ${minVersion}), ` +
@@ -98,20 +79,21 @@ function requireWithMinVersion<T = unknown>(
9879
}
9980

10081
/**
101-
* Verifies that `apache-arrow` (>= 21.0.0) and the optional
102-
* `@google-cloud/bigquery-storage` (>= 6.1.0) package are installed and meet
103-
* minimum version requirements.
82+
* Loads `apache-arrow` and verifies that the optional
83+
* `@google-cloud/bigquery-storage` (>= 6.1.0) package is installed.
84+
*
85+
* `apache-arrow` is a regular dependency, so its version is guaranteed by
86+
* `package.json`. It is loaded lazily so that only Arrow callers pay for it.
10487
*
10588
* @private
10689
*/
10790
export function checkArrowDependencies(
10891
requireFn: NodeRequire = require,
10992
): ArrowModule {
110-
const arrow = requireWithMinVersion<ArrowModule>(
111-
requireFn,
112-
'apache-arrow',
113-
21,
114-
);
93+
const arrow: ArrowModule = requireFn('apache-arrow');
94+
// Package managers only warn when an installed version is outside an
95+
// optional peer range, and an older version's protos silently drop newer
96+
// request fields, so check the version here.
11597
// TODO: Bump minimum version (here and in package.json) once
11698
// @google-cloud/bigquery-storage is released with
11799
// @google-cloud/bigquery-storage-api >= 0.5.0.

‎handwritten/bigquery/test/arrow.ts‎

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -106,55 +106,41 @@ describe('Arrow Support', () => {
106106
});
107107

108108
describe('checkArrowDependencies', () => {
109-
it('should succeed when apache-arrow >= 21.0.0 and @google-cloud/bigquery-storage >= 6.1.0 are installed', () => {
109+
it('should succeed when @google-cloud/bigquery-storage >= 6.1.0 is installed', () => {
110110
const arrowMod = checkArrowDependencies();
111111
// Verify the returned module exposes the Arrow RecordBatchReader API.
112112
assert.strictEqual(typeof arrowMod.RecordBatchReader.from, 'function');
113113
});
114114

115-
it('should throw an actionable error when a required package is missing', () => {
116-
for (const pkg of ['apache-arrow', '@google-cloud/bigquery-storage']) {
117-
const fakeRequire = ((id: string) => {
118-
if (id.startsWith(pkg)) {
119-
throw new Error(`Cannot find module '${id}'`);
120-
}
121-
return require(id);
122-
}) as NodeRequire;
123-
124-
// Verify the error tells the user how to install the missing package.
125-
assert.throws(
126-
() => checkArrowDependencies(fakeRequire),
127-
new RegExp(`npm install ${pkg.replace('/', '\\/')}`),
128-
);
129-
}
115+
it('should throw an actionable error when @google-cloud/bigquery-storage is missing', () => {
116+
const pkg = '@google-cloud/bigquery-storage';
117+
const fakeRequire = ((id: string) => {
118+
if (id.startsWith(pkg)) {
119+
throw new Error(`Cannot find module '${id}'`);
120+
}
121+
return require(id);
122+
}) as NodeRequire;
123+
124+
// Verify the error tells the user how to install the missing package.
125+
assert.throws(
126+
() => checkArrowDependencies(fakeRequire),
127+
/npm install @google-cloud\/bigquery-storage/,
128+
);
130129
});
131130

132-
it('should throw an actionable error when a required package is below the minimum version', () => {
133-
const cases = [
134-
{
135-
pkgJson: 'apache-arrow/package.json',
136-
oldVersion: '20.0.0',
137-
expected: /requires "apache-arrow" >= 21\.0\.0 \(found 20\.0\.0\)/,
138-
},
139-
{
140-
pkgJson: '@google-cloud/bigquery-storage/package.json',
141-
oldVersion: '6.0.0',
142-
expected:
143-
/requires "@google-cloud\/bigquery-storage" >= 6\.1\.0 \(found 6\.0\.0\)/,
144-
},
145-
];
146-
147-
for (const {pkgJson, oldVersion, expected} of cases) {
148-
const fakeRequire = ((id: string) => {
149-
if (id === pkgJson) {
150-
return {version: oldVersion};
151-
}
152-
return require(id);
153-
}) as NodeRequire;
154-
155-
// Verify outdated package versions are rejected with upgrade instructions.
156-
assert.throws(() => checkArrowDependencies(fakeRequire), expected);
157-
}
131+
it('should throw an actionable error when @google-cloud/bigquery-storage is below the minimum version', () => {
132+
const fakeRequire = ((id: string) => {
133+
if (id === '@google-cloud/bigquery-storage/package.json') {
134+
return {version: '6.0.0'};
135+
}
136+
return require(id);
137+
}) as NodeRequire;
138+
139+
// Verify outdated package versions are rejected with upgrade instructions.
140+
assert.throws(
141+
() => checkArrowDependencies(fakeRequire),
142+
/requires "@google-cloud\/bigquery-storage" >= 6\.1\.0 \(found 6\.0\.0\)/,
143+
);
158144
});
159145

160146
it('should include the underlying error when an installed package fails to load', () => {

0 commit comments

Comments
 (0)