Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d30424b

Browse files
remove eval calls introduced by depd wrapped functions (#410)
Co-authored-by: Victor Berchet <[email protected]>
1 parent ff2dd55 commit d30424b

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

.changeset/long-items-poke.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
remove `eval` calls introduced by `depd` wrapped functions
6+
7+
Some dependencies of Next.js (`raw-body` and `send`) use `depd` to deprecate some of their functions,
8+
`depd` uses `eval` to generate a deprecated version of such functions, this causes `eval` warnings in
9+
the terminal even if these functions are never called, the changes here by patching the depd `wrapfunction`
10+
function so that it still retains the same type of behavior but without using `eval`

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { inlineFindDir } from "./patches/plugins/find-dir.js";
1717
import { patchLoadInstrumentation } from "./patches/plugins/load-instrumentation.js";
1818
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
1919
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
20+
import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
2021
import { fixRequire } from "./patches/plugins/require.js";
2122
import { shimRequireHook } from "./patches/plugins/require-hook.js";
2223
import { inlineRequirePage } from "./patches/plugins/require-page.js";
@@ -97,6 +98,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
9798
inlineFindDir(updater, buildOpts),
9899
inlineLoadManifest(updater, buildOpts),
99100
inlineBuildId(updater),
101+
patchDepdDeprecations(updater),
100102
// Apply updater updaters, must be the last plugin
101103
updater.plugin,
102104
],
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, test } from "vitest";
2+
3+
import { patchCode } from "../ast/util.js";
4+
import { rule } from "./patch-depd-deprecations.js";
5+
6+
describe("patchDepdDeprecations", () => {
7+
test("patch", () => {
8+
const code = `
9+
function prepareObjectStackTrace(e,t){
10+
return t
11+
}
12+
function wrapfunction(fn,message){
13+
if(typeof fn!=="function"){
14+
throw new TypeError("argument fn must be a function")
15+
}
16+
var args=createArgumentsString(fn.length);
17+
var deprecate=this;
18+
var stack=getStack();
19+
var site=callSiteLocation(stack[1]);
20+
site.name=fn.name;
21+
var deprecatedfn=eval("(function ("+args+") {\\n"+'"use strict"\\n'+"log.call(deprecate, message, site)\\n"+"return fn.apply(this, arguments)\\n"+"})");
22+
return deprecatedfn;
23+
}`;
24+
25+
expect(patchCode(code, rule)).toMatchInlineSnapshot(`
26+
"function prepareObjectStackTrace(e,t){
27+
return t
28+
}
29+
function wrapfunction(fn, message) { if(typeof fn !== 'function') throw new Error("argument fn must be a function"); return function deprecated_fn(...args) { console.warn(message); return fn(...args); } }"
30+
`);
31+
});
32+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { patchCode } from "../ast/util.js";
2+
import type { ContentUpdater } from "./content-updater.js";
3+
4+
/**
5+
* Some dependencies of Next.js use depd to deprecate some of their functions, depd uses `eval` to generate
6+
* a deprecated version of such functions, this causes `eval` warnings in the terminal even if these functions
7+
* are never called, this function fixes that by patching the depd `wrapfunction` function so that it still
8+
* retains the same type of behavior but without using `eval`
9+
*/
10+
export function patchDepdDeprecations(updater: ContentUpdater) {
11+
return updater.updateContent(
12+
"patch-depd-deprecations",
13+
{ filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /argument fn must be a function/ },
14+
({ contents }) => patchCode(contents, rule)
15+
);
16+
}
17+
18+
export const rule = `
19+
rule:
20+
kind: function_declaration
21+
pattern: function wrapfunction($FN, $MESSAGE) { $$$ }
22+
all:
23+
- has:
24+
kind: variable_declarator
25+
stopBy: end
26+
has:
27+
field: name
28+
pattern: deprecatedfn
29+
- has:
30+
kind: call_expression
31+
stopBy: end
32+
has:
33+
kind: identifier
34+
pattern: eval
35+
fix:
36+
function wrapfunction($FN, $MESSAGE) {
37+
if(typeof $FN !== 'function') throw new Error("argument fn must be a function");
38+
return function deprecated_$FN(...args) {
39+
console.warn($MESSAGE);
40+
return $FN(...args);
41+
}
42+
}
43+
`;

0 commit comments

Comments
 (0)