Skip to content

Commit c848076

Browse files
authored
feat: support building in CJS and ESM formats (#2296)
* feat: support ESM and CJS formats * update imports to use .js extension * various fixes to get dual formats working * update internal tooling * add .js to imports, fix typing * add gapic-tools to dev deps * rename .jsdoc.js to .cjs * add files under cjs to output * fix merge issue * add files to owlbot ignore * add module tracking string to library version * merge and fix conflicts * revert parse function * add pack-n-play tests * fix merge conflicts * import cleanups in transfer manager tests * create separate packageJSON helper in CJS to avoid breaking esbuild * fix merge conflicts
1 parent ba3f046 commit c848076

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+670
-452
lines changed

Diff for: .jsdoc.js

-52
This file was deleted.

Diff for: .jsdoc.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"opts": {
3+
"readme": "./README.md",
4+
"package": "./package.json",
5+
"template": "./node_modules/jsdoc-fresh",
6+
"recurse": true,
7+
"verbose": true,
8+
"destination": "./docs/"
9+
},
10+
"plugins": ["plugins/markdown", "jsdoc-region-tag"],
11+
"source": {
12+
"excludePattern": "(^|\\/|\\\\)[._]",
13+
"include": ["build/cjs/src"],
14+
"includePattern": "\\.js$"
15+
},
16+
"templates": {
17+
"copyright": "Copyright 2019 Google, LLC.",
18+
"includeDate": false,
19+
"sourceFiles": false,
20+
"systemName": "@google-cloud/storage",
21+
"theme": "lumen",
22+
"default": {
23+
"outputSourceFiles": false
24+
}
25+
},
26+
"markdown": {
27+
"idInHeadings": true
28+
}
29+
}

Diff for: .mocharc.js renamed to .mocharc.cjs

File renamed without changes.

Diff for: .prettierrc.js renamed to .prettierrc.cjs

File renamed without changes.

Diff for: conformance-test/libraryMethods.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
deleteTestFile,
2222
} from './testBenchUtil';
2323
import * as uuid from 'uuid';
24+
import {getDirName} from '../src/util.js';
2425

2526
const FILE_SIZE_BYTES = 9 * 1024 * 1024;
2627
const CHUNK_SIZE_BYTES = 2 * 1024 * 1024;
@@ -390,7 +391,10 @@ export async function bucketSetStorageClass(options: ConformanceTestOptions) {
390391
export async function bucketUploadResumableInstancePrecondition(
391392
options: ConformanceTestOptions
392393
) {
393-
const filePath = path.join(__dirname, `test-data/tmp-${uuid.v4()}.txt`);
394+
const filePath = path.join(
395+
getDirName(),
396+
`../conformance-test/test-data/tmp-${uuid.v4()}.txt`
397+
);
394398
createTestFileFromBuffer(FILE_SIZE_BYTES, filePath);
395399
if (options.bucket!.instancePreconditionOpts) {
396400
options.bucket!.instancePreconditionOpts.ifGenerationMatch = 0;
@@ -405,7 +409,10 @@ export async function bucketUploadResumableInstancePrecondition(
405409
}
406410

407411
export async function bucketUploadResumable(options: ConformanceTestOptions) {
408-
const filePath = path.join(__dirname, `test-data/tmp-${uuid.v4()}.txt`);
412+
const filePath = path.join(
413+
getDirName(),
414+
`../conformance-test/test-data/tmp-${uuid.v4()}.txt`
415+
);
409416
createTestFileFromBuffer(FILE_SIZE_BYTES, filePath);
410417
if (options.preconditionRequired) {
411418
await options.bucket!.upload(filePath, {
@@ -433,8 +440,8 @@ export async function bucketUploadMultipartInstancePrecondition(
433440
}
434441
await options.bucket!.upload(
435442
path.join(
436-
__dirname,
437-
'../../conformance-test/test-data/retryStrategyTestData.json'
443+
getDirName(),
444+
'../../../conformance-test/test-data/retryStrategyTestData.json'
438445
),
439446
{resumable: false}
440447
);
@@ -448,16 +455,16 @@ export async function bucketUploadMultipart(options: ConformanceTestOptions) {
448455
if (options.preconditionRequired) {
449456
await options.bucket!.upload(
450457
path.join(
451-
__dirname,
452-
'../../conformance-test/test-data/retryStrategyTestData.json'
458+
getDirName(),
459+
'../../../conformance-test/test-data/retryStrategyTestData.json'
453460
),
454461
{resumable: false, preconditionOpts: {ifGenerationMatch: 0}}
455462
);
456463
} else {
457464
await options.bucket!.upload(
458465
path.join(
459-
__dirname,
460-
'../../conformance-test/test-data/retryStrategyTestData.json'
466+
getDirName(),
467+
'../../../conformance-test/test-data/retryStrategyTestData.json'
461468
),
462469
{resumable: false}
463470
);

Diff for: conformance-test/scenarios/scenarioFive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 5;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/scenarios/scenarioFour.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 4;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/scenarios/scenarioOne.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 1;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/scenarios/scenarioSeven.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 7;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/scenarios/scenarioSix.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 6;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/scenarios/scenarioThree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 3;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/scenarios/scenarioTwo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as testFile from '../test-data/retryStrategyTestData.json';
1717
import {executeScenario, RetryTestCase} from '../conformanceCommon';
18-
import * as assert from 'assert';
18+
import assert from 'assert';
1919

2020
const SCENARIO_NUMBER_TO_TEST = 2;
2121
const retryTestCase: RetryTestCase | undefined = testFile.retryTests.find(

Diff for: conformance-test/v4SignedUrl.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
GenerateSignedPostPolicyV4Options,
2828
} from '../src/';
2929
import * as url from 'url';
30+
import {getDirName} from '../src/util.js';
3031

3132
export enum UrlStyle {
3233
PATH_STYLE = 'PATH_STYLE',
@@ -88,7 +89,10 @@ interface BucketAction {
8889
}
8990

9091
const testFile = fs.readFileSync(
91-
path.join(__dirname, '../../conformance-test/test-data/v4SignedUrl.json'),
92+
path.join(
93+
getDirName(),
94+
'../../../conformance-test/test-data/v4SignedUrl.json'
95+
),
9296
'utf-8'
9397
);
9498

@@ -99,8 +103,8 @@ const v4SignedPolicyCases: V4SignedPolicyTestCase[] =
99103
testCases.postPolicyV4Tests;
100104

101105
const SERVICE_ACCOUNT = path.join(
102-
__dirname,
103-
'../../conformance-test/fixtures/signing-service-account.json'
106+
getDirName(),
107+
'../../../conformance-test/fixtures/signing-service-account.json'
104108
);
105109

106110
const storage = new Storage({keyFilename: SERVICE_ACCOUNT});
@@ -192,7 +196,7 @@ describe('v4 conformance test', () => {
192196
expires,
193197
};
194198

195-
const conditions = [];
199+
const conditions: (string | number)[][] = [];
196200
if (input.conditions) {
197201
if (input.conditions.startsWith) {
198202
const variable = input.conditions.startsWith[0];

Diff for: internal-tooling/helpers/package.cjs.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

Diff for: internal-tooling/performApplicationPerformanceTest.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
const yargs = require('yargs');
17+
import yargs from 'yargs';
1818
import {promises as fsp, rmSync} from 'fs';
1919
import {
2020
Bucket,
2121
DownloadOptions,
2222
DownloadResponse,
2323
UploadOptions,
2424
UploadResponse,
25-
} from '../src';
25+
} from '../src/index.js';
2626
import {performance} from 'perf_hooks';
27-
// eslint-disable-next-line node/no-unsupported-features/node-builtins
2827
import {parentPort} from 'worker_threads';
2928
import {
3029
NODE_DEFAULT_HIGHWATER_MARK_BYTES,
@@ -35,7 +34,7 @@ import {
3534
performanceTestCommand,
3635
getLowHighFileSize,
3736
PERFORMANCE_TEST_TYPES,
38-
} from './performanceUtils';
37+
} from './performanceUtils.js';
3938

4039
const TEST_NAME_STRING = 'nodejs-perf-metrics-application';
4140

@@ -54,7 +53,10 @@ const argv = yargs(process.argv.slice(2))
5453
async function main() {
5554
let result: TestResult | undefined = undefined;
5655

57-
({bucket} = await performanceTestSetup(argv.project!, argv.bucket!));
56+
({bucket} = await performanceTestSetup(
57+
argv.project! as string,
58+
argv.bucket! as string
59+
));
5860

5961
switch (argv.test_type) {
6062
case PERFORMANCE_TEST_TYPES.APPLICATION_UPLOAD_MULTIPLE_OBJECTS:
@@ -107,9 +109,9 @@ async function downloadInParallel(bucket: Bucket, options: DownloadOptions) {
107109
async function performWriteTest(): Promise<TestResult> {
108110
await bucket.deleteFiles(); //start clean
109111

110-
const fileSizeRange = getLowHighFileSize(argv.object_size);
112+
const fileSizeRange = getLowHighFileSize(argv.object_size as string);
111113
const creationInfo = generateRandomDirectoryStructure(
112-
argv.num_objects,
114+
argv.num_objects as number,
113115
TEST_NAME_STRING,
114116
fileSizeRange.low,
115117
fileSizeRange.high
@@ -133,7 +135,7 @@ async function performWriteTest(): Promise<TestResult> {
133135
cpuTimeUs: -1,
134136
status: 'OK',
135137
chunkSize: creationInfo.totalSizeInBytes,
136-
workers: argv.workers,
138+
workers: argv.workers as number,
137139
library: 'nodejs',
138140
transferSize: creationInfo.totalSizeInBytes,
139141
transferOffset: 0,
@@ -148,10 +150,10 @@ async function performWriteTest(): Promise<TestResult> {
148150
* @returns {Promise<TestResult>} Promise that resolves to an array of test results for the iteration.
149151
*/
150152
async function performReadTest(): Promise<TestResult> {
151-
const fileSizeRange = getLowHighFileSize(argv.object_size);
153+
const fileSizeRange = getLowHighFileSize(argv.object_size as string);
152154
await bucket.deleteFiles(); // start clean
153155
const creationInfo = generateRandomDirectoryStructure(
154-
argv.num_objects,
156+
argv.num_objects as number,
155157
TEST_NAME_STRING,
156158
fileSizeRange.low,
157159
fileSizeRange.high
@@ -173,7 +175,7 @@ async function performReadTest(): Promise<TestResult> {
173175
cpuTimeUs: -1,
174176
status: 'OK',
175177
chunkSize: creationInfo.totalSizeInBytes,
176-
workers: argv.workers,
178+
workers: argv.workers as number,
177179
library: 'nodejs',
178180
transferSize: creationInfo.totalSizeInBytes,
179181
transferOffset: 0,

0 commit comments

Comments
 (0)