Skip to content

Commit 1e61ba2

Browse files
feat: Create node process polyfill
1 parent 976a6c3 commit 1e61ba2

File tree

1 file changed

+238
-0
lines changed

1 file changed

+238
-0
lines changed
+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/* eslint-disable prefer-rest-params */
2+
/* eslint-disable no-use-before-define */
3+
/* eslint-disable no-shadow */
4+
/* eslint-disable func-names */
5+
6+
/*
7+
* Copyright Azion
8+
* Licensed under the MIT license. See LICENSE file for details.
9+
*
10+
* Portions of this file Copyright Roman Shtylman, licensed under the MIT license.
11+
* npmjs: https://www.npmjs.com/package/process
12+
* repo: https://github.com/defunctzombie/node-process
13+
*/
14+
15+
// shim for using process in browser
16+
const process = {};
17+
18+
// cached from whatever global is present so that test runners that stub it
19+
// don't break things. But we need to wrap it in a try catch in case it is
20+
// wrapped in strict mode code which doesn't define any globals. It's inside a
21+
// function because try/catches deoptimize in certain engines.
22+
23+
let cachedSetTimeout;
24+
let cachedClearTimeout;
25+
26+
/**
27+
*
28+
*/
29+
function defaultSetTimout() {
30+
throw new Error('setTimeout has not been defined');
31+
}
32+
/**
33+
*
34+
*/
35+
function defaultClearTimeout() {
36+
throw new Error('clearTimeout has not been defined');
37+
}
38+
(function () {
39+
try {
40+
if (typeof setTimeout === 'function') {
41+
cachedSetTimeout = setTimeout;
42+
} else {
43+
cachedSetTimeout = defaultSetTimout;
44+
}
45+
} catch (e) {
46+
cachedSetTimeout = defaultSetTimout;
47+
}
48+
try {
49+
if (typeof clearTimeout === 'function') {
50+
cachedClearTimeout = clearTimeout;
51+
} else {
52+
cachedClearTimeout = defaultClearTimeout;
53+
}
54+
} catch (e) {
55+
cachedClearTimeout = defaultClearTimeout;
56+
}
57+
})();
58+
/**
59+
/**
60+
* @param {Function} fun - The function to be executed.
61+
* @returns {void} Returns nothing.
62+
*/
63+
function runTimeout(fun) {
64+
if (cachedSetTimeout === setTimeout) {
65+
// normal enviroments in sane situations
66+
return setTimeout(fun, 0);
67+
}
68+
// if setTimeout wasn't available but was latter defined
69+
if (
70+
(cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) &&
71+
setTimeout
72+
) {
73+
cachedSetTimeout = setTimeout;
74+
return setTimeout(fun, 0);
75+
}
76+
try {
77+
// when when somebody has screwed with setTimeout but no I.E. maddness
78+
return cachedSetTimeout(fun, 0);
79+
} catch (e) {
80+
try {
81+
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
82+
return cachedSetTimeout.call(null, fun, 0);
83+
} catch (e) {
84+
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
85+
return cachedSetTimeout.call(this, fun, 0);
86+
}
87+
}
88+
}
89+
/**
90+
/**
91+
* @param {any} marker - A marker that was returned from calling setTimeout().
92+
* @returns {void}
93+
*/
94+
function runClearTimeout(marker) {
95+
if (cachedClearTimeout === clearTimeout) {
96+
// normal enviroments in sane situations
97+
return clearTimeout(marker);
98+
}
99+
// if clearTimeout wasn't available but was latter defined
100+
if (
101+
(cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) &&
102+
clearTimeout
103+
) {
104+
cachedClearTimeout = clearTimeout;
105+
return clearTimeout(marker);
106+
}
107+
try {
108+
// when when somebody has screwed with setTimeout but no I.E. maddness
109+
return cachedClearTimeout(marker);
110+
} catch (e) {
111+
try {
112+
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
113+
return cachedClearTimeout.call(null, marker);
114+
} catch (e) {
115+
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
116+
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
117+
return cachedClearTimeout.call(this, marker);
118+
}
119+
}
120+
}
121+
let queue = [];
122+
let draining = false;
123+
let currentQueue;
124+
let queueIndex = -1;
125+
126+
/**
127+
*
128+
*/
129+
function drainQueue() {
130+
if (draining) {
131+
return;
132+
}
133+
const timeout = runTimeout(cleanUpNextTick);
134+
draining = true;
135+
136+
let len = queue.length;
137+
while (len) {
138+
currentQueue = queue;
139+
queue = [];
140+
while (++queueIndex < len) {
141+
if (currentQueue) {
142+
currentQueue[queueIndex].run();
143+
}
144+
}
145+
queueIndex = -1;
146+
len = queue.length;
147+
}
148+
currentQueue = null;
149+
draining = false;
150+
runClearTimeout(timeout);
151+
}
152+
153+
/**
154+
*
155+
*/
156+
function cleanUpNextTick() {
157+
if (!draining || !currentQueue) {
158+
return;
159+
}
160+
draining = false;
161+
if (currentQueue.length) {
162+
queue = currentQueue.concat(queue);
163+
} else {
164+
queueIndex = -1;
165+
}
166+
if (queue.length) {
167+
drainQueue();
168+
}
169+
}
170+
171+
/**
172+
*
173+
*/
174+
175+
process.nextTick = function (fun) {
176+
const args = new Array(arguments.length - 1);
177+
if (arguments.length > 1) {
178+
for (let i = 1; i < arguments.length; i++) {
179+
args[i - 1] = arguments[i];
180+
}
181+
}
182+
queue.push(new Item(fun, args));
183+
if (queue.length === 1 && !draining) {
184+
runTimeout(drainQueue);
185+
}
186+
};
187+
188+
// v8 likes predictible objects
189+
/**
190+
* @param {Function} fun - return a item
191+
* @param {Array} array - return a array
192+
*/
193+
function Item(fun, array) {
194+
this.fun = fun;
195+
this.array = array;
196+
}
197+
Item.prototype.run = function () {
198+
this.fun.apply(null, this.array);
199+
};
200+
process.title = 'browser';
201+
process.browser = true;
202+
process.env = {};
203+
process.argv = [];
204+
process.version = ''; // empty string to avoid regexp issues
205+
process.versions = { node: '18.3.1' }; // fake version
206+
207+
/**
208+
*
209+
*/
210+
function noop() {}
211+
212+
process.on = noop;
213+
process.addListener = noop;
214+
process.once = noop;
215+
process.off = noop;
216+
process.removeListener = noop;
217+
process.removeAllListeners = noop;
218+
process.emit = noop;
219+
process.prependListener = noop;
220+
process.prependOnceListener = noop;
221+
222+
process.listeners = function () {
223+
return [];
224+
};
225+
226+
process.binding = function () {
227+
throw new Error('process.binding is not supported');
228+
};
229+
230+
process.cwd = function () {
231+
return '/';
232+
};
233+
process.chdir = function () {
234+
throw new Error('process.chdir is not supported');
235+
};
236+
process.umask = function () {
237+
return 0;
238+
};

0 commit comments

Comments
 (0)