'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
const stackTrace = require('./stackTrace');
const splitPath = require('./splitPath');
const startWith = require('./startWith');
const endWith = require('./endWith');
const defineProp = require('./defineProp');
const isStr = require('./isStr');
const has = require('./has');
const objToStr = require('./objToStr');
const unique = require('./unique');
const concat = require('./concat');
const keys = require('./keys');
const isArr = require('./isArr');
const toBool = require('./toBool');
 
const path = require('path');
 
exports = function(importFn, dirname) {
    return function(moduleId) {
        if (isRelative(moduleId)) {
            if (!dirname) {
                dirname = findDirName();
            }
            moduleId = path.join(dirname, moduleId);
        }
 
        const { cache } = importFn;
        if (cache) {
            if (cache[moduleId]) {
                return importFn(moduleId);
            }
            if (!endWith(moduleId, '.js') && cache[`${moduleId}.js`]) {
                return importFn(`${moduleId}.js`);
            }
        }
 
        return proxyExports(importFn, moduleId);
    };
};
 
function proxyExports(importFn, moduleId) {
    const fakeExports = function() {};
    let cache;
 
    function doImport() {
        if (cache) {
            return;
        }
        const module = importFn(moduleId);
        cache = Object(module);
 
        const valueOfDescriptor = createDescriptor(0, 0, 1);
        if (isStr(module)) {
            valueOfDescriptor.value = () => String(module.valueOf());
        } else {
            valueOfDescriptor.value = () => Number(module.valueOf());
        }
        defineProp(cache, 'valueOf', valueOfDescriptor);
 
        defineProp(
            cache,
            'toString',
            createDescriptor(0, 0, 1, () => String(module.toString()))
        );
 
        if (!has(cache, Symbol.toStringTag)) {
            const realType = objToStr(module).slice(8, -1);
            Object.defineProperty(cache, Symbol.toStringTag, {
                configurable: true,
                get() {
                    return realType;
                }
            });
        }
    }
 
    return new Proxy(fakeExports, {
        get(target, property) {
            doImport();
            return cache[property];
        },
        set(target, property, value) {
            doImport();
            cache[property] = value;
            return true;
        },
 
        has(target, prop) {
            doImport();
            return prop in cache;
        },
 
        construct(target, argumentsList) {
            doImport();
 
            return new cache(...argumentsList);
        },
 
        apply(target, thisArg, argumentsList) {
            doImport();
            return cache.apply(thisArg, argumentsList);
        },
 
        ownKeys() {
            doImport();
 
            const descriptors = Object.getOwnPropertyDescriptors(cache);
            delete descriptors.valueOf;
            delete descriptors.toString;
 
            return unique(
                concat(
                    [
                        'arguments',
                        'caller',
                        'prototype',
                        'name',
                        'length',
                        Symbol.toStringTag
                    ],
                    keys(descriptors)
                )
            );
        },
        getOwnPropertyDescriptor(target, prop) {
            if (has(cache, prop)) {
                if (isArr(cache) && prop === 'length') {
                    return {
                        configurable: true,
                        enumerable: false,
                        writable: true
                    };
                } else {
                    const descriptor = Object.getOwnPropertyDescriptor(
                        cache,
                        prop
                    );
                    if (descriptor.configurable) {
                        return descriptor;
                    }
                    if (!fakeExports.prop) {
                        defineProp(fakeExports, prop, descriptor);
                    }
                    return descriptor;
                }
            } else {
                switch (prop) {
                    case 'arguments':
                        return createDescriptor(0, 0, 0, null);
                    case 'caller':
                        return createDescriptor(0, 0, 0, null);
                    case 'prototype':
                        return createDescriptor(0, 0, 1, null);
                    case 'length':
                        return createDescriptor(1, 0, 0, 0);
                    case 'name':
                        return createDescriptor(1, 0, 0, '');
                    default:
                        return {
                            configurable: true,
                            enumerable: true,
                            writable: true
                        };
                }
            }
        }
    });
}
 
function createDescriptor(configurable, enumerable, writable, value) {
    return {
        configurable: toBool(configurable),
        enumerable: toBool(enumerable),
        writable: toBool(writable),
        value
    };
}
 
function findDirName() {
    const stack = stackTrace();
    for (const item of stack) {
        const fileName = item.getFileName();
        if (fileName !== module.filename) {
            return splitPath(fileName).dir;
        }
    }
 
    return '';
}
 
function isRelative(moduleId) {
    return startWith(moduleId, './') || startWith(moduleId, '../');
}
 
module.exports = exports;