forked from unjs/unenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_process.ts
228 lines (207 loc) · 5.84 KB
/
_process.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// @ts-nocheck
// Source: https://github.com/defunctzombie/node-process/blob/77caa43cdaee4ea710aa14d11cea1705293c0ef3/browser.js
export const process = {} as typeof globalThis.process;
// Cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.
let cachedSetTimeout: typeof globalThis.setTimeout;
let cachedClearTimeout: typeof global.clearTimeout;
function defaultSetTimeout() {
throw new Error("setTimeout has not been defined");
}
function defaultClearTimeout() {
throw new Error("clearTimeout has not been defined");
}
(function () {
try {
cachedSetTimeout =
typeof setTimeout === "function" ? setTimeout : defaultSetTimeout;
} catch {
cachedSetTimeout = defaultSetTimeout;
}
try {
cachedClearTimeout =
typeof clearTimeout === "function" ? clearTimeout : defaultClearTimeout;
} catch {
cachedClearTimeout = defaultClearTimeout;
}
})();
function runTimeout(fun: () => void) {
if (cachedSetTimeout === setTimeout) {
// normal enviroments in sane situations
return setTimeout(fun, 0);
}
// if setTimeout wasn't available but was latter defined
if (
(cachedSetTimeout === defaultSetTimeout || !cachedSetTimeout) &&
setTimeout
) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch {
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
// eslint-disable-next-line no-useless-call
return cachedSetTimeout.call(null, fun, 0);
} catch {
// 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
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
// normal enviroments in sane situations
return clearTimeout(marker);
}
// if clearTimeout wasn't available but was latter defined
if (
(cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) &&
clearTimeout
) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch {
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
// eslint-disable-next-line no-useless-call
return cachedClearTimeout.call(null, marker);
} catch {
// 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.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}
let queue = [];
let draining = false;
let currentQueue;
let queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length > 0) {
queue = [...currentQueue, ...queue];
} else {
queueIndex = -1;
}
if (queue.length > 0) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
const timeout = runTimeout(cleanUpNextTick);
draining = true;
let len = queue.length;
while (len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
const args = Array.from({ length: arguments.length - 1 });
if (arguments.length > 1) {
for (let i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = "unenv";
const _envShim = Object.create(null);
const _processEnv = globalThis.process?.env;
const _getEnv = (useShim: boolean) =>
_processEnv || globalThis.__env__ || (useShim ? _envShim : globalThis);
process.env = new Proxy(_envShim, {
get(_, prop) {
const env = _getEnv();
return env[prop] ?? _envShim[prop];
},
has(_, prop) {
const env = _getEnv();
return prop in env || prop in _envShim;
},
set(_, prop, value) {
const env = _getEnv(true);
env[prop] = value;
return true;
},
deleteProperty(_, prop) {
const env = _getEnv(true);
delete env[prop];
},
ownKeys() {
const env = _getEnv();
return Object.keys(env);
},
});
process.argv = [];
// @ts-ignore
process.version = ""; // empty string to avoid regexp issues
// @ts-ignore
process.versions = {};
function noop() {
return process;
}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
// @ts-ignore
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) {
return [];
};
// @ts-ignore
process.binding = function (name) {
throw new Error("[unenv] process.binding is not supported");
};
let cwd = "/";
process.cwd = function () {
return cwd;
};
process.chdir = function (dir) {
cwd = dir;
};
process.umask = function () {
return 0;
};