-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunsnapshottable.js
300 lines (268 loc) · 9.81 KB
/
unsnapshottable.js
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const util = require('node:util');
const { EventEmitter } = require('node:events');
const { Writable, Transform } = require('node:stream');
const Agent = require('agent-base');
const TransportStream = require('winston-transport/modern');
// Dynamically make the `target` class extend from `base`. Differences with
// util.inherits():
// - Unlike extendClass, util.inherits() defines the `super_` property
// - Unlike extendClass, util.inherits() does not allow inheriting static
// properties
function extendClass(target, base) {
Object.setPrototypeOf(target, base);
Object.setPrototypeOf(target.prototype, base.prototype);
}
// Make require('glob').Glob inherit from EventEmitter. This needs to be done
// here because EventEmitter is not a part of the default V8 context which is
// used to generate the V8 snapshot.
{
const glob = require('glob');
util.inherits(glob.Glob, EventEmitter);
}
// Set minimatch.sep with path.sep. This needs to be done here because the
// 'node:path' module is not a part of the default V8 context which is
// used to generate the V8 snapshot.
{
const minimatch = require('minimatch');
const path = require('node:path');
minimatch.sep = path.sep
}
// Since `util.debuglog` and 'signal-exit' are not a part of the V8 snapshot,
// the code from the 'lockfile' module that uses these have been moved here.
{
const debug = util.debuglog('LOCKFILE')
const onExit = require('signal-exit')
onExit(function () {
const { locks, unlockSync } = require('lockfile');
debug('exit listener')
// cleanup
Object.keys(locks).forEach(unlockSync)
})
}
// Make require('binary-search-tree').AVLTree._AVLTree inherit from
// require('binary-search-tree').BinaryTree. This needs to be done here because
// util.inherits() is not a part of the default V8 context which is used to
// generate the V8 snapshot.
{
const { BinarySearchTree, AVLTree } = require('binary-search-tree');
util.inherits(AVLTree._AVLTree, BinarySearchTree);
}
// Move the code for attaching the fs module methods from 'nedb/lib/storage' to
// here because the fs module is not available in the V8 snapshot.
{
const fs = require('node:fs');
const storage = require('nedb/lib/storage');
storage.exists = fs.exists;
storage.rename = fs.rename;
storage.writeFile = fs.writeFile;
storage.unlink = fs.unlink;
storage.appendFile = fs.appendFile;
storage.readFile = fs.readFile;
}
// Make require('nedb/lib/datastore') inherit from
// require('events').EventEmitter. This needs to be done here because
// util.inherits() is not a part of the default V8 context which is used to
// generate the V8 snapshot.
{
const Datastore = require('nedb/lib/datastore');
util.inherits(Datastore, EventEmitter);
}
// Move code to capture process.exit and process.env and delete
// process.env.OLDPWD from 'shelljs' to here because these are not available in
// the V8 snapshot.
{
const sh = require('shelljs');
sh.exit = process.exit;
sh.env = process.env;
delete process.env.OLDPWD; // initially, there's no previous directory
}
// Make require('agent-base') inherit from EventEmitter. This needs to be done
// here because EventEmitter is not a part of the default V8 context which is
// used to generate the V8 snapshot. Also, runs 'agent-base/patch-core'.
{
require('agent-base/patch-core')
util.inherits(Agent, EventEmitter);
}
// Make require('https-proxy-agent') inherit from Agent. This needs to be done
// here because util.inherits() is not a part of the default V8 context which is
// used to generate the V8 snapshot.
{
const HttpsProxyAgent = require('https-proxy-agent');
util.inherits(HttpsProxyAgent, Agent);
}
// Move the code to the require the 'console' module from
// 'node_modules/@sentry/node/dist/integrations/console.js to here because the
// 'console' module is not available in the context of the V8 snapshot.
{
// special case: since console is built-in and app-level code won't require() it, do that here
require('console');
}
// Move the code for making AbstractUpdater from
// '@postman/app-updater/lib/AbstractUpdater' inherit from EventEmitter here
// because EventEmitter is not a part of the V8 snapshot.
{
const AbstractUpdater = require('@postman/app-updater/lib/AbstractUpdater');
extendClass(AbstractUpdater, EventEmitter);
}
// Move the code for making LinuxAutoUpdater from
// '@postman/app-updater/lib/autoUpdater/LinuxAutoUpdater' inherit from
// EventEmitter here because EventEmitter is not a part of the V8 snapshot.
{
const LinuxAutoUpdater = require('@postman/app-updater/lib/autoUpdater/LinuxAutoUpdater');
extendClass(LinuxAutoUpdater, EventEmitter);
}
// Move the code for making Receiver from 'ws/lib/receiver' inherit from
// stream.Writable here because Writable is not a part of the V8 snapshot.
{
const { Writable } = require('node:stream');
const Receiver = require('./node_modules/ws/lib/receiver');
extendClass(Receiver, Writable);
}
// Move the code for making WebSocket from 'ws/lib/websocket' inherit from
// EventEmitter here because EventEmitter is not a part of the V8 snapshot.
{
const WebSocket = require('./node_modules/ws/lib/websocket');
extendClass(WebSocket, EventEmitter);
}
// Move the code for making WebSocket from 'ws/lib/websocket-server' inherit from
// EventEmitter here because EventEmitter is not a part of the V8 snapshot.
{
const WebSocketServer = require('./node_modules/ws/lib/websocket-server');
extendClass(WebSocketServer, EventEmitter);
}
// Monkey-patch the 'node-ipc' module to export an IPCModule instance instead of
// the IPCModule class because that is what the original module does. It has
// been patched to return the class, so that it can be snapshotted.
{
const IPCModule = require('node-ipc');
require.cache[require.resolve('node-ipc')] = new IPCModule();
}
// Move the code for assigning nextTick, setImmediate and globalScope to the
// 'node-forge/lib/util' module here because those variables are not available
// in the default context of the V8 snapshot.
{
const nodeForgeUtil = require('node-forge/lib/util');
nodeForgeUtil.nextTick = process.nextTick;
nodeForgeUtil.setImmediate = setImmediate;
nodeForgeUtil.globalScope = global;
}
// Move the code for creating the default PRNG context out of
// 'node-forge/lib/random.js' because that requires the usage of the 'crypto'
// module which is not available in the default context of the V8 snapshot.
{
const { forge, spawnPrng } = require('node-forge/lib/random');
forge.random = spawnPrng();
// expose spawn PRNG
forge.random.createInstance = spawnPrng;
require.cache[require.resolve('node-forge/lib/random')] = forge.random;
}
// Move the code from 'winston/lib/winston/exception-stream.js' for extending
// ExceptionStream from stream.Writable here because the 'stream' module is not
// available in the V8 snapshot.
{
const ExceptionStream = require('winston/lib/winston/exception-stream');
extendClass(ExceptionStream, Writable);
}
// Move the code from 'winston-transport/modern.js' for extending
// TransportStream from stream.Writable here because the 'stream' module is not
// available in the V8 snapshot.
{
util.inherits(TransportStream, Writable);
}
// Move the code from 'winston-transport/legacy.js' for extending
// LegacyTransportStream from TransportStream here because the 'util' module
// is not available in the V8 snapshot.
{
const LegacyTransportStream = require('winston-transport/legacy');
util.inherits(LegacyTransportStream, TransportStream);
}
// Move the code from 'winston/lib/winston/logger.js' for extending
// Logger from stream.Transform here because the 'stream' module is not
// available in the V8 snapshot.
{
const Logger = require('winston/lib/winston/logger');
extendClass(Logger, Transform);
}
// Move the code from 'winston' for creating a logger and assigning dependent
// fields on the winston exports object here because the logger creation depends
// on the Node.js stream module which is not a part of the V8 snapshot.
{
const winston = require('winston');
/**
* We create and expose a 'defaultLogger' so that the programmer may do the
* following without the need to create an instance of winston.Logger directly:
* @example
* const winston = require('winston');
* winston.log('info', 'some message');
* winston.error('some error');
*/
const defaultLogger = winston.createLogger();
// Pass through the target methods onto `winston.
Object.keys(winston.config.npm.levels).concat([
'log',
'query',
'stream',
'add',
'remove',
'clear',
'profile',
'startTimer',
'handleExceptions',
'unhandleExceptions',
'configure'
]).forEach(method => (
winston[method] = (...args) => defaultLogger[method](...args)
));
/**
* Define getter / setter for the default logger level which need to be exposed
* by winston.
* @type {string}
*/
Object.defineProperty(winston, 'level', {
get() {
return defaultLogger.level;
},
set(val) {
defaultLogger.level = val;
}
});
/**
* Define getter for `exceptions` which replaces `handleExceptions` and
* `unhandleExceptions`.
* @type {Object}
*/
Object.defineProperty(winston, 'exceptions', {
get() {
return defaultLogger.exceptions;
}
});
/**
* Define getters / setters for appropriate properties of the default logger
* which need to be exposed by winston.
* @type {Logger}
*/
[
'exitOnError'
].forEach(prop => {
Object.defineProperty(winston, prop, {
get() {
return defaultLogger[prop];
},
set(val) {
defaultLogger[prop] = val;
}
});
});
/**
* The default transports and exceptionHandlers for the default winston logger.
* @type {Object}
*/
Object.defineProperty(winston, 'default', {
get() {
return {
exceptionHandlers: defaultLogger.exceptionHandlers,
transports: defaultLogger.transports
};
}
});
}