child.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. *
  8. */
  9. 'use strict';
  10. var _types;
  11. function _load_types() {
  12. return _types = require('./types');
  13. }
  14. let file = null;
  15. /**
  16. * This file is a small bootstrapper for workers. It sets up the communication
  17. * between the worker and the parent process, interpreting parent messages and
  18. * sending results back.
  19. *
  20. * The file loaded will be lazily initialized the first time any of the workers
  21. * is called. This is done for optimal performance: if the farm is initialized,
  22. * but no call is made to it, child Node processes will be consuming the least
  23. * possible amount of memory.
  24. *
  25. * If an invalid message is detected, the child will exit (by throwing) with a
  26. * non-zero exit code.
  27. */
  28. process.on('message', (request /* Should be ChildMessage */) => {
  29. switch (request[0]) {
  30. case (_types || _load_types()).CHILD_MESSAGE_INITIALIZE:
  31. file = request[2];
  32. break;
  33. case (_types || _load_types()).CHILD_MESSAGE_CALL:
  34. execMethod(request[2], request[3]);
  35. break;
  36. case (_types || _load_types()).CHILD_MESSAGE_END:
  37. process.exit(0);
  38. break;
  39. default:
  40. throw new TypeError('Unexpected request from parent process: ' + request[0]);
  41. }
  42. });
  43. function reportSuccess(result) {
  44. if (!process || !process.send) {
  45. throw new Error('Child can only be used on a forked process');
  46. }
  47. process.send([(_types || _load_types()).PARENT_MESSAGE_OK, result]);
  48. }
  49. function reportError(error) {
  50. if (!process || !process.send) {
  51. throw new Error('Child can only be used on a forked process');
  52. }
  53. if (error == null) {
  54. error = new Error('"null" or "undefined" thrown');
  55. }
  56. process.send([(_types || _load_types()).PARENT_MESSAGE_ERROR, error.constructor && error.constructor.name, error.message, error.stack,
  57. // $FlowFixMe: this is safe to just inherit from Object.
  58. typeof error === 'object' ? Object.assign({}, error) : error]);
  59. }
  60. function execMethod(method, args) {
  61. // $FlowFixMe: This has to be a dynamic require.
  62. const main = require(file);
  63. let result;
  64. try {
  65. if (method === 'default') {
  66. result = (main.__esModule ? main['default'] : main).apply(global, args);
  67. } else {
  68. result = main[method].apply(main, args);
  69. }
  70. } catch (err) {
  71. reportError(err);
  72. return;
  73. }
  74. if (result && typeof result.then === 'function') {
  75. result.then(reportSuccess, reportError);
  76. } else {
  77. reportSuccess(result);
  78. }
  79. }