Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ec5c2ec

Browse files
committed
Log worker's uncompressed size on reload, warn over 1MiB, closes #23
1 parent 606060b commit ec5c2ec

File tree

12 files changed

+171
-6
lines changed

12 files changed

+171
-6
lines changed

src/helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ export class MiniflareError extends Error {
77
this.name = new.target.name;
88
}
99
}
10+
11+
export function formatSize(bytes: number): string {
12+
if (bytes >= 524_288) return `${(bytes / 1_048_576).toFixed(2)}MiB`;
13+
if (bytes >= 512) return `${(bytes / 1_024).toFixed(2)}KiB`;
14+
return `${bytes}B`;
15+
}

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import cron from "node-cron";
1313
import sourceMap, { UrlAndMap } from "source-map-support";
1414
import StandardWebSocket from "ws";
1515
import Youch from "youch";
16-
import { MiniflareError } from "./helpers";
16+
import { MiniflareError, formatSize } from "./helpers";
1717
import { Cache, KVStorageNamespace } from "./kv";
1818
import { ConsoleLog, Log, NoOpLog, logResponse } from "./log";
1919
import { ResponseWaitUntil } from "./modules";
@@ -146,6 +146,9 @@ export class Miniflare {
146146
// processedModulesRules are always set in this
147147
assert(this.#options?.scripts && this.#options.processedModulesRules);
148148

149+
// Keep track of the size in bytes of all scripts
150+
let size = 0;
151+
149152
// Build modules linker maintaining set of referenced paths for watching
150153
const linker = new ScriptLinker(this.#options.processedModulesRules);
151154
this.#extraSourceMaps = linker.extraSourceMaps;
@@ -170,6 +173,7 @@ export class Miniflare {
170173
this.log.debug(`Reloading ${path.relative("", script.fileName)}...`);
171174

172175
// Parse script and build instance
176+
size += Buffer.byteLength(script.code, "utf8");
173177
let instance: ScriptScriptInstance | ModuleScriptInstance<ModuleExports>;
174178
try {
175179
instance = this.#options.modules
@@ -240,7 +244,15 @@ export class Miniflare {
240244
ws.close(1012, "Service Restart");
241245
}
242246

243-
this.log.info("Worker reloaded!");
247+
// Log total size of worker with warning if required
248+
size += linker.referencedPathsTotalSize;
249+
this.log.info(`Worker reloaded! (${formatSize(size)})`);
250+
if (size > 1_048_576)
251+
this.log.warn(
252+
"Worker's uncompressed size exceeds 1MiB!" +
253+
"Note that your worker will be compressed during upload " +
254+
"so you may still be able to deploy it."
255+
);
244256
}
245257

246258
/** @deprecated Since 1.2.0, this is just an alias for reloadOptions() */

src/scripts.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,21 @@ const commonJsCompilerOptions: CompilerOptions = {
7878

7979
export class ScriptLinker {
8080
readonly referencedPaths = new Set<string>();
81+
private _referencedPathsSizes = new Map<string, number>();
8182
readonly extraSourceMaps = new Map<string, string>();
8283
readonly linker: ModuleLinker;
8384

8485
constructor(private moduleRules: ProcessedModuleRule[]) {
8586
this.linker = this._linker.bind(this);
8687
}
8788

89+
get referencedPathsTotalSize(): number {
90+
// Make sure we only include each module once, even if it's referenced
91+
// from multiple scripts
92+
const sizes = Array.from(this._referencedPathsSizes.values());
93+
return sizes.reduce((total, size) => total + size, 0);
94+
}
95+
8896
private async _linker(
8997
specifier: string,
9098
referencingModule: vm.Module
@@ -118,6 +126,7 @@ export class ScriptLinker {
118126
// Load module based on rule type
119127
this.referencedPaths.add(modulePath);
120128
const data = await fs.readFile(modulePath);
129+
this._referencedPathsSizes.set(modulePath, data.byteLength);
121130
const moduleOptions = {
122131
identifier: modulePath,
123132
context: referencingModule.context,

test/fixtures/limits/a.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import s from "./shared.mjs";
2+
export default s + "a";

test/fixtures/limits/b.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import s from "./shared.mjs";
2+
export default s + "bb";

test/fixtures/limits/entry.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
fetch() {
3+
return new Response();
4+
},
5+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import a from "./a.mjs";
2+
import b from "./b.mjs";
3+
4+
export default {
5+
fetch() {
6+
return new Response(a + b);
7+
},
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import s from "./shared.mjs";
2+
3+
export default {
4+
fetch() {
5+
return new Response(s);
6+
},
7+
};

test/fixtures/limits/object.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class TestObject {
2+
async fetch() {
3+
return new Response("test");
4+
}
5+
}

test/fixtures/limits/shared.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "shared";

0 commit comments

Comments
 (0)