Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions handwritten/firestore/bundle-budget.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"serverless_minimal_bundle": {
"entrypoint": "./build/src/index.js",
"target_minified_bytes": 2293760,
"max_minified_bytes_deduped": 2500000,
"max_minified_bytes_undeduped": 5000000,
"description": "2.19 MB minified target budget for standard serverless Cloud Function deployment"
}
}
2 changes: 2 additions & 0 deletions handwritten/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"system-test:emulator": "concurrently -p \"[{name}]\" -n \"grpc,rest,enterprise-grpc,enterprise-rest\" -c \"cyan,magenta,blue,yellow\" \"npm:system-test:emulator:grpc\" \"npm:system-test:emulator:rest\" \"npm:system-test:enterprise:emulator:grpc\" \"npm:system-test:enterprise:emulator:rest\"",
"presystem-test": "npm run compile",
"conformance": "mocha build/conformance",
"precheck-bundle-size": "npm run compile",
"check-bundle-size": "node scripts/check-bundle-size.js",
Comment thread
MarkDuckworth marked this conversation as resolved.
"test-only": "c8 mocha build/test",
"pretest": "npm run compile",
"test": "npm run test-only && npm run conformance",
Expand Down
34 changes: 34 additions & 0 deletions handwritten/firestore/scripts/check-bundle-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
const budget = require('../bundle-budget.json');
Comment on lines +1 to +5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To support creating a secure and collision-free temporary directory using fs.mkdtempSync in the OS temp directory, we should import the native os module.

Suggested change
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const budget = require('../bundle-budget.json');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
const budget = require('../bundle-budget.json');


const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'firestore-bundle-'));
const entrypoint = path.join(tmpDir, 'test_bundle_input.js');
const outfile = path.join(tmpDir, 'test_bundle_out.min.js');
const buildIndexPath = path.resolve(__dirname, '../build/src/index');

fs.writeFileSync(
entrypoint,
'\nconst { Firestore } = require(' +
JSON.stringify(buildIndexPath) +
');\nconst f = new Firestore();\nconsole.log(f);\n',
'utf8'
);

try {
execSync('npx --yes esbuild --bundle --platform=node --target=node18 --minify "' + entrypoint + '" --outfile="' + outfile + '"', {
stdio: 'inherit',
});
const stats = fs.statSync(outfile);
const size = stats.size;
console.log(`[test-bundle-size] Compiled serverless bundle size: ${size} bytes`);
if (size > budget.maxBundleSize) {
console.error(`[test-bundle-size] ERROR: Bundle size (${size} bytes) exceeds budget (${budget.maxBundleSize} bytes)`);
process.exit(1);
}
console.log('[test-bundle-size] SUCCESS: Bundle size is within budget.');
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
Loading