-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove
eval
calls introduced by depd
wrapped functions
- Loading branch information
1 parent
12d385d
commit 76b6a44
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
"@opennextjs/cloudflare": patch | ||
--- | ||
|
||
remove `eval` calls introduced by `depd` wrapped functions | ||
|
||
Some dependencies of Next.js (`raw-body` and `send`) use `depd` to deprecate some of their functions, | ||
`depd` uses `eval` to generate a deprecated version of such functions, this causes `eval` warnings in | ||
the terminal even if these functions are never called, the changes here by patching the depd `wrapfunction` | ||
function so that it still retains the same type of behavior but without using `eval` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/cloudflare/src/cli/build/patches/plugins/patch-depd-deprecations.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { patchCode } from "../ast/util.js"; | ||
import { rule } from "./patch-depd-deprecations.js"; | ||
|
||
describe("patchDepdDeprecations", () => { | ||
test("patch", () => { | ||
const code = ` | ||
function prepareObjectStackTrace(e,t){ | ||
return t | ||
} | ||
function wrapfunction(fn,message){ | ||
if(typeof fn!=="function"){ | ||
throw new TypeError("argument fn must be a function") | ||
} | ||
var args=createArgumentsString(fn.length); | ||
var deprecate=this; | ||
var stack=getStack(); | ||
var site=callSiteLocation(stack[1]); | ||
site.name=fn.name; | ||
var deprecatedfn=eval("(function ("+args+") {\\n"+'"use strict"\\n'+"log.call(deprecate, message, site)\\n"+"return fn.apply(this, arguments)\\n"+"})"); | ||
return deprecatedfn; | ||
}`; | ||
|
||
expect(patchCode(code, rule)).toMatchInlineSnapshot(` | ||
"function prepareObjectStackTrace(e,t){ | ||
return t | ||
} | ||
function wrapfunction(fn, message) { if(typeof fn !== 'function') throw new Error("argument fn must be a function"); return (...args) => { console.warn(message); return fn(...args); } }" | ||
`); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/cloudflare/src/cli/build/patches/plugins/patch-depd-deprecations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { patchCode } from "../ast/util.js"; | ||
import type { ContentUpdater } from "./content-updater.js"; | ||
|
||
/** | ||
* Some dependencies of Next.js use depd to deprecate some of their functions, depd uses `eval` to generate | ||
* a deprecated version of such functions, this causes `eval` warnings in the terminal even if these functions | ||
* are never called, this function fixes that by patching the depd `wrapfunction` function so that it still | ||
* retains the same type of behavior but without using `eval` | ||
*/ | ||
export function patchDepdDeprecations(updater: ContentUpdater) { | ||
return updater.updateContent( | ||
"patch-depd-deprecations", | ||
{ filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /argument fn must be a function/ }, | ||
({ contents }) => patchCode(contents, rule) | ||
); | ||
} | ||
|
||
export const rule = ` | ||
rule: | ||
kind: function_declaration | ||
pattern: function wrapfunction($FN, $MESSAGE) { $$$ } | ||
all: | ||
- has: | ||
kind: variable_declarator | ||
stopBy: end | ||
has: | ||
field: name | ||
pattern: deprecatedfn | ||
- has: | ||
kind: call_expression | ||
stopBy: end | ||
has: | ||
kind: identifier | ||
pattern: eval | ||
fix: | ||
function wrapfunction($FN, $MESSAGE) { | ||
if(typeof $FN !== 'function') throw new Error("argument fn must be a function"); | ||
return (...args) => { | ||
console.warn($MESSAGE); | ||
return $FN(...args); | ||
} | ||
} | ||
`; |