|
| 1 | +const MODULE_KEY = '__module_obj', // why not use `module`? |
| 2 | + MODULE_WRAPPER = [ |
| 3 | + '(function (exports, module) {\n', |
| 4 | + `\n})(${MODULE_KEY}.exports, ${MODULE_KEY});` |
| 5 | + ]; |
| 6 | + |
| 7 | +/** |
| 8 | + * Cache of all files that are available to be required. |
| 9 | + * |
| 10 | + * @typedef {Object.<string, { data: string } | { error: string }>} FileCache |
| 11 | + */ |
| 12 | + |
| 13 | +class PostmanRequireStore { |
| 14 | + /** |
| 15 | + * @param {FileCache} fileCache - fileCache |
| 16 | + */ |
| 17 | + constructor (fileCache) { |
| 18 | + if (!fileCache) { |
| 19 | + throw new Error('File cache is required'); |
| 20 | + } |
| 21 | + |
| 22 | + this.fileCache = fileCache; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Check if the file is available in the cache. |
| 27 | + * |
| 28 | + * @param {string} path - path |
| 29 | + * @returns {boolean} |
| 30 | + */ |
| 31 | + hasFile (path) { |
| 32 | + return Boolean(this.getFile(path)); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the file from the cache. |
| 37 | + * |
| 38 | + * @param {string} path - path |
| 39 | + * @returns {Object|undefined} - file |
| 40 | + */ |
| 41 | + getFile (path) { |
| 42 | + return this.fileCache[path]; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the resolved path for the file. |
| 47 | + * |
| 48 | + * @param {string} path - path |
| 49 | + * @returns {string|undefined} - resolved path |
| 50 | + */ |
| 51 | + getResolvedPath (path) { |
| 52 | + if (this.hasFile(path)) { |
| 53 | + return path; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the file data. |
| 59 | + * |
| 60 | + * @param {string} path - path |
| 61 | + * @returns {string|undefined} |
| 62 | + */ |
| 63 | + getFileData (path) { |
| 64 | + return this.hasFile(path) && this.getFile(path).data; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Check if the file has an error. |
| 69 | + * |
| 70 | + * @param {string} path - path |
| 71 | + * @returns {boolean} |
| 72 | + */ |
| 73 | + hasError (path) { |
| 74 | + return this.hasFile(path) && Boolean(this.getFile(path).error); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the file error. |
| 79 | + * |
| 80 | + * @param {string} path - path |
| 81 | + * @returns {string|undefined} |
| 82 | + */ |
| 83 | + getFileError (path) { |
| 84 | + return this.hasError(path) && this.getFile(path).error; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param {FileCache} fileCache - fileCache |
| 90 | + * @param {Object} scope - scope |
| 91 | + * @returns {Function} - postmanRequire |
| 92 | + * @example |
| 93 | + * const fileCache = { |
| 94 | + * 'path/to/file.js': { |
| 95 | + * data: 'module.exports = { foo: "bar" };' |
| 96 | + * } |
| 97 | + * }; |
| 98 | + * |
| 99 | + * const postmanRequire = createPostmanRequire(fileCache, scope); |
| 100 | + * |
| 101 | + * const module = postmanRequire('path/to/file.js'); |
| 102 | + * console.log(module.foo); // bar |
| 103 | + */ |
| 104 | +function createPostmanRequire (fileCache, scope) { |
| 105 | + const store = new PostmanRequireStore(fileCache || {}), |
| 106 | + cache = {}; |
| 107 | + |
| 108 | + /** |
| 109 | + * @param {string} name - name |
| 110 | + * @returns {any} - module |
| 111 | + */ |
| 112 | + function postmanRequire (name) { |
| 113 | + const path = store.getResolvedPath(name); |
| 114 | + |
| 115 | + if (!path) { |
| 116 | + // Error should contain the name exactly as the user specified, |
| 117 | + // and not the resolved path. |
| 118 | + throw new Error(`Cannot find module '${name}'`); |
| 119 | + } |
| 120 | + |
| 121 | + if (store.hasError(path)) { |
| 122 | + throw new Error(`Error while loading module '${name}': ${store.getFileError(path)}`); |
| 123 | + } |
| 124 | + |
| 125 | + // Any module should not be evaluated twice, so we use it from the |
| 126 | + // cache. If there's a circular dependency, the partially evaluated |
| 127 | + // module will be returned from the cache. |
| 128 | + if (cache[path]) { |
| 129 | + // Always use the resolved path as the ID of the module. This |
| 130 | + // ensures that relative paths are handled correctly. |
| 131 | + return cache[path].exports; |
| 132 | + } |
| 133 | + |
| 134 | + /* eslint-disable-next-line one-var */ |
| 135 | + const file = store.getFileData(path), |
| 136 | + moduleObj = { |
| 137 | + id: path, |
| 138 | + exports: {} |
| 139 | + }; |
| 140 | + |
| 141 | + // Add to cache before executing. This ensures that any dependency |
| 142 | + // that tries to import it's parent/ancestor gets the cached |
| 143 | + // version and not end up in infinite loop. |
| 144 | + cache[moduleObj.id] = moduleObj; |
| 145 | + |
| 146 | + /* eslint-disable-next-line one-var */ |
| 147 | + const wrappedModule = MODULE_WRAPPER[0] + file + MODULE_WRAPPER[1]; |
| 148 | + |
| 149 | + scope.import({ |
| 150 | + [MODULE_KEY]: moduleObj |
| 151 | + }); |
| 152 | + |
| 153 | + // Note: We're executing the code in the same scope as the one |
| 154 | + // which called the `pm.require` function. This is because we want |
| 155 | + // to share the global scope across all the required modules. Any |
| 156 | + // locals are available inside the required modules and any locals |
| 157 | + // created inside the required modules are available to the parent. |
| 158 | + // |
| 159 | + // Why `async` = true? |
| 160 | + // - We want to allow execution of async code like setTimeout etc. |
| 161 | + scope.exec(wrappedModule, true, (err) => { |
| 162 | + // Bubble up the error to be caught as execution error |
| 163 | + if (err) { |
| 164 | + throw err; |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + scope.unset(MODULE_KEY); |
| 169 | + |
| 170 | + return moduleObj.exports; |
| 171 | + } |
| 172 | + |
| 173 | + return postmanRequire; |
| 174 | +} |
| 175 | + |
| 176 | +module.exports = createPostmanRequire; |
0 commit comments