worker.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.getSha1 = getSha1;
  6. exports.worker = worker;
  7. function _crypto() {
  8. const data = require('crypto');
  9. _crypto = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function path() {
  15. const data = _interopRequireWildcard(require('path'));
  16. path = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. function fs() {
  22. const data = _interopRequireWildcard(require('graceful-fs'));
  23. fs = function () {
  24. return data;
  25. };
  26. return data;
  27. }
  28. function _jestUtil() {
  29. const data = require('jest-util');
  30. _jestUtil = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. var _blacklist = _interopRequireDefault(require('./blacklist'));
  36. var _constants = _interopRequireDefault(require('./constants'));
  37. var _dependencyExtractor = require('./lib/dependencyExtractor');
  38. function _interopRequireDefault(obj) {
  39. return obj && obj.__esModule ? obj : {default: obj};
  40. }
  41. function _getRequireWildcardCache(nodeInterop) {
  42. if (typeof WeakMap !== 'function') return null;
  43. var cacheBabelInterop = new WeakMap();
  44. var cacheNodeInterop = new WeakMap();
  45. return (_getRequireWildcardCache = function (nodeInterop) {
  46. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  47. })(nodeInterop);
  48. }
  49. function _interopRequireWildcard(obj, nodeInterop) {
  50. if (!nodeInterop && obj && obj.__esModule) {
  51. return obj;
  52. }
  53. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  54. return {default: obj};
  55. }
  56. var cache = _getRequireWildcardCache(nodeInterop);
  57. if (cache && cache.has(obj)) {
  58. return cache.get(obj);
  59. }
  60. var newObj = {};
  61. var hasPropertyDescriptor =
  62. Object.defineProperty && Object.getOwnPropertyDescriptor;
  63. for (var key in obj) {
  64. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  65. var desc = hasPropertyDescriptor
  66. ? Object.getOwnPropertyDescriptor(obj, key)
  67. : null;
  68. if (desc && (desc.get || desc.set)) {
  69. Object.defineProperty(newObj, key, desc);
  70. } else {
  71. newObj[key] = obj[key];
  72. }
  73. }
  74. }
  75. newObj.default = obj;
  76. if (cache) {
  77. cache.set(obj, newObj);
  78. }
  79. return newObj;
  80. }
  81. /**
  82. * Copyright (c) Meta Platforms, Inc. and affiliates.
  83. *
  84. * This source code is licensed under the MIT license found in the
  85. * LICENSE file in the root directory of this source tree.
  86. */
  87. const PACKAGE_JSON = `${path().sep}package.json`;
  88. let hasteImpl = null;
  89. let hasteImplModulePath = null;
  90. function sha1hex(content) {
  91. return (0, _crypto().createHash)('sha1').update(content).digest('hex');
  92. }
  93. async function worker(data) {
  94. if (
  95. data.hasteImplModulePath &&
  96. data.hasteImplModulePath !== hasteImplModulePath
  97. ) {
  98. if (hasteImpl) {
  99. throw new Error('jest-haste-map: hasteImplModulePath changed');
  100. }
  101. hasteImplModulePath = data.hasteImplModulePath;
  102. hasteImpl = require(hasteImplModulePath);
  103. }
  104. let content;
  105. let dependencies;
  106. let id;
  107. let module;
  108. let sha1;
  109. const {computeDependencies, computeSha1, rootDir, filePath} = data;
  110. const getContent = () => {
  111. if (content === undefined) {
  112. content = fs().readFileSync(filePath, 'utf8');
  113. }
  114. return content;
  115. };
  116. if (filePath.endsWith(PACKAGE_JSON)) {
  117. // Process a package.json that is returned as a PACKAGE type with its name.
  118. try {
  119. const fileData = JSON.parse(getContent());
  120. if (fileData.name) {
  121. const relativeFilePath = path().relative(rootDir, filePath);
  122. id = fileData.name;
  123. module = [relativeFilePath, _constants.default.PACKAGE];
  124. }
  125. } catch (err) {
  126. throw new Error(`Cannot parse ${filePath} as JSON: ${err.message}`);
  127. }
  128. } else if (
  129. !_blacklist.default.has(filePath.substring(filePath.lastIndexOf('.')))
  130. ) {
  131. // Process a random file that is returned as a MODULE.
  132. if (hasteImpl) {
  133. id = hasteImpl.getHasteName(filePath);
  134. }
  135. if (computeDependencies) {
  136. const content = getContent();
  137. const extractor = data.dependencyExtractor
  138. ? await (0, _jestUtil().requireOrImportModule)(
  139. data.dependencyExtractor,
  140. false
  141. )
  142. : _dependencyExtractor.extractor;
  143. dependencies = Array.from(
  144. extractor.extract(
  145. content,
  146. filePath,
  147. _dependencyExtractor.extractor.extract
  148. )
  149. );
  150. }
  151. if (id) {
  152. const relativeFilePath = path().relative(rootDir, filePath);
  153. module = [relativeFilePath, _constants.default.MODULE];
  154. }
  155. }
  156. // If a SHA-1 is requested on update, compute it.
  157. if (computeSha1) {
  158. sha1 = sha1hex(content || fs().readFileSync(filePath));
  159. }
  160. return {
  161. dependencies,
  162. id,
  163. module,
  164. sha1
  165. };
  166. }
  167. async function getSha1(data) {
  168. const sha1 = data.computeSha1
  169. ? sha1hex(fs().readFileSync(data.filePath))
  170. : null;
  171. return {
  172. dependencies: undefined,
  173. id: undefined,
  174. module: undefined,
  175. sha1
  176. };
  177. }