15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
'use strict';
 
require('bytenode');
const convert = require('koa-convert');
const is = require('is-type-of');
const path = require('path');
const fs = require('fs');
const co = require('../../../utils/co');
const BuiltinModule = require('module');
 
// Guard against poorly mocked module constructors.
const Module = module.constructor.length > 1
  ? module.constructor
  /* istanbul ignore next */
  : BuiltinModule;
 
// Module._extensions:
// '.js': [Function (anonymous)],
// '.json': [Function (anonymous)],
// '.node': [Function: func],
// '.jsc': [Function (anonymous)]
 
module.exports = {
  extensions: Module._extensions,
 
  loadFile(filepath) {
    try {
      // if not js module, just return content buffer
      const extname = path.extname(filepath);
      if (extname && !Module._extensions[extname]) {
        return fs.readFileSync(filepath);
      }
 
      // require js module
      const obj = require(filepath);
      if (!obj) return obj;
      // it's es module
      if (obj.__esModule) return 'default' in obj ? obj.default : obj;
      return obj;
    } catch (err) {
      err.message = `[ee-core] load file: ${filepath}, error: ${err.message}`;
      throw err;
    }
  },
 
  methods: [ 'head', 'options', 'get', 'put', 'patch', 'post', 'delete' ],
 
  async callFn(fn, args, ctx) {
    args = args || [];
    if (!is.function(fn)) return;
    if (is.generatorFunction(fn)) fn = co.wrap(fn);
    return ctx ? fn.call(ctx, ...args) : fn(...args);
  },
 
  middleware(fn) {
    return is.generatorFunction(fn) ? convert(fn) : fn;
  },
 
  getCalleeFromStack(withLine, stackIndex) {
    stackIndex = stackIndex === undefined ? 2 : stackIndex;
    const limit = Error.stackTraceLimit;
    const prep = Error.prepareStackTrace;
 
    Error.prepareStackTrace = prepareObjectStackTrace;
    Error.stackTraceLimit = 5;
 
    // capture the stack
    const obj = {};
    Error.captureStackTrace(obj);
    let callSite = obj.stack[stackIndex];
    let fileName;
    /* istanbul ignore else */
    if (callSite) {
      fileName = callSite.getFileName();
      /* istanbul ignore if */
      if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
        // TODO: add test
        callSite = obj.stack[stackIndex + 1];
        fileName = callSite.getFileName();
      }
    }
 
    Error.prepareStackTrace = prep;
    Error.stackTraceLimit = limit;
 
    /* istanbul ignore if */
    if (!callSite || !fileName) return '<anonymous>';
    if (!withLine) return fileName;
    return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
  },
 
  getResolvedFilename(filepath, baseDir) {
    const reg = /[/\\]/g;
    return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
  },
 
  /**
   * 字节码类
   */
  isBytecodeClass (exports) {
    let isClass = false;
 
    // 标识
    if (exports.toString().indexOf('[class') != -1) {
      isClass = true;
    }
    // TODO 更严谨的判断,应该加上文件名和路径
    
    return isClass;
  },
 
  /**
   * 文件类型
   */
  filePatterns () {
    const files = (process.env.EE_TYPESCRIPT === 'true' && Module._extensions['.ts'])
    ? [ '**/*.(js|ts)', '!**/*.d.ts' ]
    : [ '**/*.js','**/*.jsc' ];
 
    return files;
  }
 
};
 
 
/**
 * Capture call site stack from v8.
 * https://github.com/v8/v8/wiki/Stack-Trace-API
 */
 
function prepareObjectStackTrace(obj, stack) {
  return stack;
}