Skip to content

Commit

Permalink
Merge pull request #122 from webpack/bugfix/stack-overflow
Browse files Browse the repository at this point in the history
avoid generating too depth scope nesting
  • Loading branch information
sokra committed May 15, 2020
2 parents 75fcb00 + a6b539e commit 6bf8b25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/HookCodeFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,17 @@ class HookCodeFactory {
}) {
if (this.options.taps.length === 0) return onDone();
const firstAsync = this.options.taps.findIndex(t => t.type !== "sync");
const somethingReturns = resultReturns || doneReturns || false;
const somethingReturns = resultReturns || doneReturns;
let code = "";
let current = onDone;
let unrollCounter = 0;
for (let j = this.options.taps.length - 1; j >= 0; j--) {
const i = j;
const unroll = current !== onDone && this.options.taps[i].type !== "sync";
const unroll =
current !== onDone &&
(this.options.taps[i].type !== "sync" || unrollCounter++ > 20);
if (unroll) {
unrollCounter = 0;
code += `function _next${i}() {\n`;
code += current();
code += `}\n`;
Expand Down
10 changes: 10 additions & 0 deletions lib/__tests__/AsyncSeriesHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ describe("AsyncSeriesBailHook", () => {

expect(result).toMatchSnapshot();
});

it("should not crash with many plugins", () => {
const hook = new AsyncSeriesBailHook(["x"]);
for (let i = 0; i < 1000; i++) {
hook.tap("Test", () => 42);
}
hook.tapAsync("Test", (x, callback) => callback(null, 42));
hook.tapPromise("Test", x => Promise.resolve(42));
return expect(hook.promise()).resolves.toBe(42);
});
});

describe("AsyncSeriesWaterfallHook", () => {
Expand Down
8 changes: 8 additions & 0 deletions lib/__tests__/SyncBailHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ describe("SyncBailHook", () => {
const hook = new SyncBailHook(["x"]);
expect(() => hook.tapPromise()).toThrow(/tapPromise/);
});

it("should not crash with many plugins", () => {
const hook = new SyncBailHook(["x"]);
for (let i = 0; i < 1000; i++) {
hook.tap("Test", () => 42);
}
expect(hook.call()).toBe(42);
});
});

function pify(fn) {
Expand Down

0 comments on commit 6bf8b25

Please sign in to comment.