summaryrefslogtreecommitdiff
path: root/lib/internal/loader/Loader.js
blob: 57c70188d66b70b2f3d84a03ba6f30822375952e (plain)
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
'use strict';

const { getURLFromFilePath } = require('internal/url');

const {
  getNamespaceOfModuleWrap,
  createDynamicModule
} = require('internal/loader/ModuleWrap');

const ModuleMap = require('internal/loader/ModuleMap');
const ModuleJob = require('internal/loader/ModuleJob');
const ModuleRequest = require('internal/loader/ModuleRequest');
const errors = require('internal/errors');
const debug = require('util').debuglog('esm');

function getBase() {
  try {
    return getURLFromFilePath(`${process.cwd()}/`).href;
  } catch (e) {
    e.stack;
    // If the current working directory no longer exists.
    if (e.code === 'ENOENT') {
      return undefined;
    }
    throw e;
  }
}

class Loader {
  constructor(base = getBase()) {
    this.moduleMap = new ModuleMap();
    if (typeof base !== 'string') {
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'base', 'string');
    }
    this.base = base;
    this.resolver = ModuleRequest.resolve.bind(null);
    this.dynamicInstantiate = undefined;
  }

  hook({ resolve = ModuleRequest.resolve, dynamicInstantiate }) {
    this.resolver = resolve.bind(null);
    this.dynamicInstantiate = dynamicInstantiate;
  }

  async resolve(specifier, parentURL = this.base) {
    if (typeof parentURL !== 'string') {
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
                                 'parentURL', 'string');
    }
    const { url, format } = await this.resolver(specifier, parentURL,
                                                ModuleRequest.resolve);

    if (typeof format !== 'string') {
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'format',
                                 ['esm', 'cjs', 'builtin', 'addon', 'json']);
    }
    if (typeof url !== 'string') {
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'url', 'string');
    }

    if (format === 'builtin') {
      return { url: `node:${url}`, format };
    }

    if (format !== 'dynamic') {
      if (!ModuleRequest.loaders.has(format)) {
        throw new errors.Error('ERR_UNKNOWN_MODULE_FORMAT', format);
      }
      if (!url.startsWith('file:')) {
        throw new errors.Error('ERR_INVALID_PROTOCOL', url, 'file:');
      }
    }

    return { url, format };
  }

  async getModuleJob(specifier, parentURL = this.base) {
    const { url, format } = await this.resolve(specifier, parentURL);
    let job = this.moduleMap.get(url);
    if (job === undefined) {
      let loaderInstance;
      if (format === 'dynamic') {
        loaderInstance = async (url) => {
          const { exports, execute } = await this.dynamicInstantiate(url);
          return createDynamicModule(exports, url, (reflect) => {
            debug(`Loading custom loader ${url}`);
            execute(reflect.exports);
          });
        };
      } else {
        loaderInstance = ModuleRequest.loaders.get(format);
      }
      job = new ModuleJob(this, url, loaderInstance);
      this.moduleMap.set(url, job);
    }
    return job;
  }

  async import(specifier, parentURL = this.base) {
    const job = await this.getModuleJob(specifier, parentURL);
    const module = await job.run();
    return getNamespaceOfModuleWrap(module);
  }
}
Object.setPrototypeOf(Loader.prototype, null);
module.exports = Loader;