chunk-env-node.ceb43f1c.mjs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import { Console } from 'console';
  2. import { importModule } from 'local-pkg';
  3. var node = {
  4. name: "node",
  5. async setup(global) {
  6. global.console.Console = Console;
  7. return {
  8. teardown(global2) {
  9. delete global2.console.Console;
  10. }
  11. };
  12. }
  13. };
  14. const LIVING_KEYS = [
  15. "DOMException",
  16. "URL",
  17. "URLSearchParams",
  18. "EventTarget",
  19. "NamedNodeMap",
  20. "Node",
  21. "Attr",
  22. "Element",
  23. "DocumentFragment",
  24. "DOMImplementation",
  25. "Document",
  26. "XMLDocument",
  27. "CharacterData",
  28. "Text",
  29. "CDATASection",
  30. "ProcessingInstruction",
  31. "Comment",
  32. "DocumentType",
  33. "NodeList",
  34. "HTMLCollection",
  35. "HTMLOptionsCollection",
  36. "DOMStringMap",
  37. "DOMTokenList",
  38. "StyleSheetList",
  39. "HTMLElement",
  40. "HTMLHeadElement",
  41. "HTMLTitleElement",
  42. "HTMLBaseElement",
  43. "HTMLLinkElement",
  44. "HTMLMetaElement",
  45. "HTMLStyleElement",
  46. "HTMLBodyElement",
  47. "HTMLHeadingElement",
  48. "HTMLParagraphElement",
  49. "HTMLHRElement",
  50. "HTMLPreElement",
  51. "HTMLUListElement",
  52. "HTMLOListElement",
  53. "HTMLLIElement",
  54. "HTMLMenuElement",
  55. "HTMLDListElement",
  56. "HTMLDivElement",
  57. "HTMLAnchorElement",
  58. "HTMLAreaElement",
  59. "HTMLBRElement",
  60. "HTMLButtonElement",
  61. "HTMLCanvasElement",
  62. "HTMLDataElement",
  63. "HTMLDataListElement",
  64. "HTMLDetailsElement",
  65. "HTMLDialogElement",
  66. "HTMLDirectoryElement",
  67. "HTMLFieldSetElement",
  68. "HTMLFontElement",
  69. "HTMLFormElement",
  70. "HTMLHtmlElement",
  71. "HTMLImageElement",
  72. "HTMLInputElement",
  73. "HTMLLabelElement",
  74. "HTMLLegendElement",
  75. "HTMLMapElement",
  76. "HTMLMarqueeElement",
  77. "HTMLMediaElement",
  78. "HTMLMeterElement",
  79. "HTMLModElement",
  80. "HTMLOptGroupElement",
  81. "HTMLOptionElement",
  82. "HTMLOutputElement",
  83. "HTMLPictureElement",
  84. "HTMLProgressElement",
  85. "HTMLQuoteElement",
  86. "HTMLScriptElement",
  87. "HTMLSelectElement",
  88. "HTMLSlotElement",
  89. "HTMLSourceElement",
  90. "HTMLSpanElement",
  91. "HTMLTableCaptionElement",
  92. "HTMLTableCellElement",
  93. "HTMLTableColElement",
  94. "HTMLTableElement",
  95. "HTMLTimeElement",
  96. "HTMLTableRowElement",
  97. "HTMLTableSectionElement",
  98. "HTMLTemplateElement",
  99. "HTMLTextAreaElement",
  100. "HTMLUnknownElement",
  101. "HTMLFrameElement",
  102. "HTMLFrameSetElement",
  103. "HTMLIFrameElement",
  104. "HTMLEmbedElement",
  105. "HTMLObjectElement",
  106. "HTMLParamElement",
  107. "HTMLVideoElement",
  108. "HTMLAudioElement",
  109. "HTMLTrackElement",
  110. "SVGElement",
  111. "SVGGraphicsElement",
  112. "SVGSVGElement",
  113. "SVGTitleElement",
  114. "SVGAnimatedString",
  115. "SVGNumber",
  116. "SVGStringList",
  117. "Event",
  118. "CloseEvent",
  119. "CustomEvent",
  120. "MessageEvent",
  121. "ErrorEvent",
  122. "HashChangeEvent",
  123. "PopStateEvent",
  124. "StorageEvent",
  125. "ProgressEvent",
  126. "PageTransitionEvent",
  127. "UIEvent",
  128. "FocusEvent",
  129. "InputEvent",
  130. "MouseEvent",
  131. "KeyboardEvent",
  132. "TouchEvent",
  133. "CompositionEvent",
  134. "WheelEvent",
  135. "BarProp",
  136. "External",
  137. "Location",
  138. "History",
  139. "Screen",
  140. "Performance",
  141. "Navigator",
  142. "PluginArray",
  143. "MimeTypeArray",
  144. "Plugin",
  145. "MimeType",
  146. "FileReader",
  147. "Blob",
  148. "File",
  149. "FileList",
  150. "ValidityState",
  151. "DOMParser",
  152. "XMLSerializer",
  153. "FormData",
  154. "XMLHttpRequestEventTarget",
  155. "XMLHttpRequestUpload",
  156. "XMLHttpRequest",
  157. "WebSocket",
  158. "NodeFilter",
  159. "NodeIterator",
  160. "TreeWalker",
  161. "AbstractRange",
  162. "Range",
  163. "StaticRange",
  164. "Selection",
  165. "Storage",
  166. "CustomElementRegistry",
  167. "ShadowRoot",
  168. "MutationObserver",
  169. "MutationRecord",
  170. "Headers",
  171. "AbortController",
  172. "AbortSignal",
  173. "Image",
  174. "Audio",
  175. "Option"
  176. ];
  177. const OTHER_KEYS = [
  178. "addEventListener",
  179. "alert",
  180. "atob",
  181. "blur",
  182. "btoa",
  183. "cancelAnimationFrame",
  184. "close",
  185. "confirm",
  186. "createPopup",
  187. "dispatchEvent",
  188. "document",
  189. "focus",
  190. "frames",
  191. "getComputedStyle",
  192. "history",
  193. "innerHeight",
  194. "innerWidth",
  195. "length",
  196. "location",
  197. "matchMedia",
  198. "moveBy",
  199. "moveTo",
  200. "name",
  201. "navigator",
  202. "open",
  203. "outerHeight",
  204. "outerWidth",
  205. "pageXOffset",
  206. "pageYOffset",
  207. "parent",
  208. "postMessage",
  209. "print",
  210. "prompt",
  211. "removeEventListener",
  212. "requestAnimationFrame",
  213. "resizeBy",
  214. "resizeTo",
  215. "screen",
  216. "screenLeft",
  217. "screenTop",
  218. "screenX",
  219. "screenY",
  220. "scroll",
  221. "scrollBy",
  222. "scrollLeft",
  223. "scrollTo",
  224. "scrollTop",
  225. "scrollX",
  226. "scrollY",
  227. "self",
  228. "stop",
  229. "top",
  230. "Window",
  231. "window"
  232. ];
  233. const KEYS = LIVING_KEYS.concat(OTHER_KEYS);
  234. const allowRewrite = [
  235. "Event",
  236. "EventTarget",
  237. "MessageEvent",
  238. "ArrayBuffer"
  239. ];
  240. const skipKeys = [
  241. "window",
  242. "self",
  243. "top",
  244. "parent"
  245. ];
  246. function getWindowKeys(global, win) {
  247. const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win)).filter((k) => {
  248. if (skipKeys.includes(k))
  249. return false;
  250. if (k in global)
  251. return allowRewrite.includes(k);
  252. return true;
  253. }));
  254. return keys;
  255. }
  256. function isClassLikeName(name) {
  257. return name[0] === name[0].toUpperCase();
  258. }
  259. function populateGlobal(global, win, options = {}) {
  260. const { bindFunctions = false } = options;
  261. const keys = getWindowKeys(global, win);
  262. const originals = new Map(
  263. allowRewrite.filter((key) => key in global).map((key) => [key, global[key]])
  264. );
  265. const overrideObject = /* @__PURE__ */ new Map();
  266. for (const key of keys) {
  267. const boundFunction = bindFunctions && typeof win[key] === "function" && !isClassLikeName(key) && win[key].bind(win);
  268. Object.defineProperty(global, key, {
  269. get() {
  270. if (overrideObject.has(key))
  271. return overrideObject.get(key);
  272. if (boundFunction)
  273. return boundFunction;
  274. return win[key];
  275. },
  276. set(v) {
  277. overrideObject.set(key, v);
  278. },
  279. configurable: true
  280. });
  281. }
  282. global.window = global;
  283. global.self = global;
  284. global.top = global;
  285. global.parent = global;
  286. if (global.global)
  287. global.global = global;
  288. skipKeys.forEach((k) => keys.add(k));
  289. return {
  290. keys,
  291. skipKeys,
  292. originals
  293. };
  294. }
  295. var jsdom = {
  296. name: "jsdom",
  297. async setup(global, { jsdom = {} }) {
  298. const {
  299. CookieJar,
  300. JSDOM,
  301. ResourceLoader,
  302. VirtualConsole
  303. } = await importModule("jsdom");
  304. const {
  305. html = "<!DOCTYPE html>",
  306. userAgent,
  307. url = "http://localhost:3000",
  308. contentType = "text/html",
  309. pretendToBeVisual = true,
  310. includeNodeLocations = false,
  311. runScripts = "dangerously",
  312. resources,
  313. console = false,
  314. cookieJar = false,
  315. ...restOptions
  316. } = jsdom;
  317. const dom = new JSDOM(
  318. html,
  319. {
  320. pretendToBeVisual,
  321. resources: resources ?? (userAgent ? new ResourceLoader({ userAgent }) : void 0),
  322. runScripts,
  323. url,
  324. virtualConsole: console && global.console ? new VirtualConsole().sendTo(global.console) : void 0,
  325. cookieJar: cookieJar ? new CookieJar() : void 0,
  326. includeNodeLocations,
  327. contentType,
  328. userAgent,
  329. ...restOptions
  330. }
  331. );
  332. const { keys, originals } = populateGlobal(global, dom.window, { bindFunctions: true });
  333. return {
  334. teardown(global2) {
  335. keys.forEach((key) => delete global2[key]);
  336. originals.forEach((v, k) => global2[k] = v);
  337. }
  338. };
  339. }
  340. };
  341. var happy = {
  342. name: "happy-dom",
  343. async setup(global) {
  344. const { Window, GlobalWindow } = await importModule("happy-dom");
  345. const win = new (GlobalWindow || Window)();
  346. const { keys, originals } = populateGlobal(global, win, { bindFunctions: true });
  347. return {
  348. teardown(global2) {
  349. win.happyDOM.cancelAsync();
  350. keys.forEach((key) => delete global2[key]);
  351. originals.forEach((v, k) => global2[k] = v);
  352. }
  353. };
  354. }
  355. };
  356. var edge = {
  357. name: "edge-runtime",
  358. async setup(global) {
  359. const { EdgeVM } = await importModule("@edge-runtime/vm");
  360. const vm = new EdgeVM({
  361. extend: (context) => {
  362. context.global = context;
  363. context.Buffer = Buffer;
  364. return context;
  365. }
  366. });
  367. const { keys, originals } = populateGlobal(global, vm.context, { bindFunctions: true });
  368. return {
  369. teardown(global2) {
  370. keys.forEach((key) => delete global2[key]);
  371. originals.forEach((v, k) => global2[k] = v);
  372. }
  373. };
  374. }
  375. };
  376. const environments = {
  377. node,
  378. jsdom,
  379. "happy-dom": happy,
  380. "edge-runtime": edge
  381. };
  382. const envs = Object.keys(environments);
  383. const envPackageNames = {
  384. "jsdom": "jsdom",
  385. "happy-dom": "happy-dom",
  386. "edge-runtime": "@edge-runtime/vm"
  387. };
  388. const getEnvPackageName = (env) => {
  389. if (env === "node")
  390. return null;
  391. if (env in envPackageNames)
  392. return envPackageNames[env];
  393. return `vitest-environment-${env}`;
  394. };
  395. export { envs as a, environments as e, getEnvPackageName as g, populateGlobal as p };