sass-embedded 1.0.0-beta.5 → 1.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/.gitignore +1 -1
  3. package/dist/lib/index.js +11 -4
  4. package/dist/lib/index.js.map +1 -1
  5. package/dist/lib/src/{embedded-compiler/compiler.js → async-compiler.js} +9 -17
  6. package/dist/lib/src/async-compiler.js.map +1 -0
  7. package/dist/lib/src/compile.js +167 -79
  8. package/dist/lib/src/compile.js.map +1 -1
  9. package/dist/lib/src/compiler-path.js +18 -0
  10. package/dist/lib/src/compiler-path.js.map +1 -0
  11. package/dist/lib/src/{embedded-protocol/utils.js → deprotofy-span.js} +16 -18
  12. package/dist/lib/src/deprotofy-span.js.map +1 -0
  13. package/dist/lib/src/{embedded-protocol/dispatcher.js → dispatcher.js} +45 -40
  14. package/dist/lib/src/dispatcher.js.map +1 -0
  15. package/dist/lib/src/exception.js +20 -0
  16. package/dist/lib/src/exception.js.map +1 -0
  17. package/dist/lib/src/function-registry.js +81 -0
  18. package/dist/lib/src/function-registry.js.map +1 -0
  19. package/dist/lib/src/{node-sass/render.js → legacy.js} +45 -39
  20. package/dist/lib/src/legacy.js.map +1 -0
  21. package/dist/lib/src/{embedded-protocol/message-transformer.js → message-transformer.js} +2 -2
  22. package/dist/lib/src/message-transformer.js.map +1 -0
  23. package/dist/lib/src/{embedded-compiler/packet-transformer.js → packet-transformer.js} +0 -0
  24. package/dist/lib/src/packet-transformer.js.map +1 -0
  25. package/dist/lib/src/protofier.js +205 -0
  26. package/dist/lib/src/protofier.js.map +1 -0
  27. package/dist/lib/src/{embedded-protocol/request-tracker.js → request-tracker.js} +0 -0
  28. package/dist/lib/src/request-tracker.js.map +1 -0
  29. package/dist/lib/src/sync-compiler.js +52 -0
  30. package/dist/lib/src/sync-compiler.js.map +1 -0
  31. package/dist/lib/src/{exception/span.js → sync-process/event.js} +2 -2
  32. package/dist/lib/src/sync-process/event.js.map +1 -0
  33. package/dist/lib/src/sync-process/index.js +122 -0
  34. package/dist/lib/src/sync-process/index.js.map +1 -0
  35. package/dist/lib/src/sync-process/sync-message-port.js +128 -0
  36. package/dist/lib/src/sync-process/sync-message-port.js.map +1 -0
  37. package/dist/lib/src/sync-process/worker.js +51 -0
  38. package/dist/lib/src/sync-process/worker.js.map +1 -0
  39. package/dist/lib/src/utils.js +43 -1
  40. package/dist/lib/src/utils.js.map +1 -1
  41. package/dist/lib/src/value/boolean.js +3 -3
  42. package/dist/lib/src/value/boolean.js.map +1 -1
  43. package/dist/lib/src/value/color.js +89 -98
  44. package/dist/lib/src/value/color.js.map +1 -1
  45. package/dist/lib/src/value/{value.js → index.js} +5 -1
  46. package/dist/lib/src/value/index.js.map +1 -0
  47. package/dist/lib/src/value/list.js +15 -18
  48. package/dist/lib/src/value/list.js.map +1 -1
  49. package/dist/lib/src/value/map.js +17 -10
  50. package/dist/lib/src/value/map.js.map +1 -1
  51. package/dist/lib/src/value/null.js +2 -2
  52. package/dist/lib/src/value/number.js +20 -26
  53. package/dist/lib/src/value/number.js.map +1 -1
  54. package/dist/lib/src/value/string.js +14 -9
  55. package/dist/lib/src/value/string.js.map +1 -1
  56. package/dist/lib/src/value/utils.js +7 -1
  57. package/dist/lib/src/value/utils.js.map +1 -1
  58. package/dist/lib/src/vendor/embedded-protocol/embedded_sass_pb.js +1395 -101
  59. package/dist/lib/src/vendor/embedded-protocol/embedded_sass_pb.js.map +1 -1
  60. package/dist/package.json +6 -4
  61. package/dist/tool/utils.js +34 -9
  62. package/dist/tool/utils.js.map +1 -1
  63. package/dist/types/compile.d.ts +152 -0
  64. package/dist/types/exception.d.ts +41 -0
  65. package/dist/types/importer.d.ts +294 -0
  66. package/dist/types/index.d.ts +76 -0
  67. package/dist/types/legacy/exception.d.ts +54 -0
  68. package/dist/types/legacy/function.d.ts +652 -0
  69. package/dist/types/legacy/importer.d.ts +168 -0
  70. package/dist/types/legacy/options.d.ts +642 -0
  71. package/dist/types/legacy/plugin_this.d.ts +70 -0
  72. package/dist/types/legacy/render.d.ts +139 -0
  73. package/dist/types/logger/index.d.ts +94 -0
  74. package/dist/types/logger/source_location.d.ts +21 -0
  75. package/dist/types/logger/source_span.d.ts +34 -0
  76. package/dist/types/options.d.ts +408 -0
  77. package/dist/types/util/promise_or.d.ts +17 -0
  78. package/dist/types/value/argument_list.d.ts +47 -0
  79. package/dist/types/value/boolean.d.ts +29 -0
  80. package/dist/types/value/color.d.ts +107 -0
  81. package/dist/types/value/function.d.ts +22 -0
  82. package/dist/types/value/index.d.ts +173 -0
  83. package/dist/types/value/list.d.ts +54 -0
  84. package/dist/types/value/map.d.ts +41 -0
  85. package/dist/types/value/number.d.ts +305 -0
  86. package/dist/types/value/string.d.ts +84 -0
  87. package/package.json +6 -4
  88. package/dist/lib/index.d.ts +0 -10
  89. package/dist/lib/src/compile.d.ts +0 -22
  90. package/dist/lib/src/embedded-compiler/compiler.d.ts +0 -19
  91. package/dist/lib/src/embedded-compiler/compiler.js.map +0 -1
  92. package/dist/lib/src/embedded-compiler/packet-transformer.d.ts +0 -34
  93. package/dist/lib/src/embedded-compiler/packet-transformer.js.map +0 -1
  94. package/dist/lib/src/embedded-protocol/dispatcher.d.ts +0 -60
  95. package/dist/lib/src/embedded-protocol/dispatcher.js.map +0 -1
  96. package/dist/lib/src/embedded-protocol/message-transformer.d.ts +0 -47
  97. package/dist/lib/src/embedded-protocol/message-transformer.js.map +0 -1
  98. package/dist/lib/src/embedded-protocol/request-tracker.d.ts +0 -20
  99. package/dist/lib/src/embedded-protocol/request-tracker.js.map +0 -1
  100. package/dist/lib/src/embedded-protocol/utils.d.ts +0 -7
  101. package/dist/lib/src/embedded-protocol/utils.js.map +0 -1
  102. package/dist/lib/src/exception/exception.d.ts +0 -19
  103. package/dist/lib/src/exception/exception.js +0 -37
  104. package/dist/lib/src/exception/exception.js.map +0 -1
  105. package/dist/lib/src/exception/location.d.ts +0 -11
  106. package/dist/lib/src/exception/location.js +0 -6
  107. package/dist/lib/src/exception/location.js.map +0 -1
  108. package/dist/lib/src/exception/span.d.ts +0 -29
  109. package/dist/lib/src/exception/span.js.map +0 -1
  110. package/dist/lib/src/node-sass/render.d.ts +0 -61
  111. package/dist/lib/src/node-sass/render.js.map +0 -1
  112. package/dist/lib/src/utils.d.ts +0 -16
  113. package/dist/lib/src/value/boolean.d.ts +0 -25
  114. package/dist/lib/src/value/color.d.ts +0 -89
  115. package/dist/lib/src/value/list.d.ts +0 -41
  116. package/dist/lib/src/value/map.d.ts +0 -26
  117. package/dist/lib/src/value/null.d.ts +0 -12
  118. package/dist/lib/src/value/number.d.ts +0 -189
  119. package/dist/lib/src/value/string.d.ts +0 -56
  120. package/dist/lib/src/value/utils.d.ts +0 -44
  121. package/dist/lib/src/value/value.d.ts +0 -108
  122. package/dist/lib/src/value/value.js.map +0 -1
  123. package/dist/lib/src/vendor/embedded-protocol/embedded_sass_pb.d.ts +0 -1
  124. package/dist/tool/utils.d.ts +0 -32
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ // Copyright 2021 Google Inc. Use of this source code is governed by an
3
+ // MIT-style license that can be found in the LICENSE file or at
4
+ // https://opensource.org/licenses/MIT.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Protofier = void 0;
7
+ const immutable_1 = require("immutable");
8
+ const proto = require("./vendor/embedded-protocol/embedded_sass_pb");
9
+ const utils = require("./utils");
10
+ const color_1 = require("./value/color");
11
+ const list_1 = require("./value/list");
12
+ const map_1 = require("./value/map");
13
+ const number_1 = require("./value/number");
14
+ const string_1 = require("./value/string");
15
+ const null_1 = require("./value/null");
16
+ const boolean_1 = require("./value/boolean");
17
+ /**
18
+ * A class that converts [Value] objects into protobufs.
19
+ *
20
+ * A given [Protofier] instance is valid only within the scope of a single
21
+ * custom function call.
22
+ */
23
+ class Protofier {
24
+ /** Converts `value` to its protocol buffer representation. */
25
+ protofy(value) {
26
+ const result = new proto.Value();
27
+ if (value instanceof string_1.SassString) {
28
+ const string = new proto.Value.String();
29
+ string.setText(value.text);
30
+ string.setQuoted(value.hasQuotes);
31
+ result.setString(string);
32
+ }
33
+ else if (value instanceof number_1.SassNumber) {
34
+ result.setNumber(this.protofyNumber(value));
35
+ }
36
+ else if (value instanceof color_1.SassColor) {
37
+ if (value.hasCalculatedHsl) {
38
+ const color = new proto.Value.HslColor();
39
+ color.setHue(value.hue);
40
+ color.setSaturation(value.saturation);
41
+ color.setLightness(value.lightness);
42
+ color.setAlpha(value.alpha);
43
+ result.setHslColor(color);
44
+ }
45
+ else {
46
+ const color = new proto.Value.RgbColor();
47
+ color.setRed(value.red);
48
+ color.setGreen(value.green);
49
+ color.setBlue(value.blue);
50
+ color.setAlpha(value.alpha);
51
+ result.setRgbColor(color);
52
+ }
53
+ }
54
+ else if (value instanceof list_1.SassList) {
55
+ const list = new proto.Value.List();
56
+ list.setSeparator(this.protofySeparator(value.separator));
57
+ list.setHasBrackets(value.hasBrackets);
58
+ for (const element of value.asList) {
59
+ list.addContents(this.protofy(element));
60
+ }
61
+ result.setList(list);
62
+ }
63
+ else if (value instanceof map_1.SassMap) {
64
+ const map = new proto.Value.Map();
65
+ for (const [key, mapValue] of value.contents) {
66
+ const entry = new proto.Value.Map.Entry();
67
+ entry.setKey(this.protofy(key));
68
+ entry.setValue(this.protofy(mapValue));
69
+ map.addEntries(entry);
70
+ }
71
+ result.setMap(map);
72
+ }
73
+ else if (value === boolean_1.sassTrue) {
74
+ result.setSingleton(proto.SingletonValue.TRUE);
75
+ }
76
+ else if (value === boolean_1.sassFalse) {
77
+ result.setSingleton(proto.SingletonValue.FALSE);
78
+ }
79
+ else if (value === null_1.sassNull) {
80
+ result.setSingleton(proto.SingletonValue.NULL);
81
+ }
82
+ else {
83
+ throw utils.compilerError(`Unknown Value ${value}`);
84
+ }
85
+ return result;
86
+ }
87
+ /** Converts `number` to its protocol buffer representation. */
88
+ protofyNumber(number) {
89
+ const value = new proto.Value.Number();
90
+ value.setValue(number.value);
91
+ for (const unit of number.numeratorUnits) {
92
+ value.addNumerators(unit);
93
+ }
94
+ for (const unit of number.denominatorUnits) {
95
+ value.addDenominators(unit);
96
+ }
97
+ return value;
98
+ }
99
+ /** Converts `separator` to its protocol buffer representation. */
100
+ protofySeparator(separator) {
101
+ switch (separator) {
102
+ case ',':
103
+ return proto.ListSeparator.COMMA;
104
+ case ' ':
105
+ return proto.ListSeparator.SPACE;
106
+ case '/':
107
+ return proto.ListSeparator.SLASH;
108
+ case null:
109
+ return proto.ListSeparator.UNDECIDED;
110
+ default:
111
+ throw utils.compilerError(`Unknown ListSeparator ${separator}`);
112
+ }
113
+ }
114
+ /** Converts `value` to its JS representation. */
115
+ deprotofy(value) {
116
+ switch (value.getValueCase()) {
117
+ case proto.Value.ValueCase.STRING: {
118
+ const string = value.getString();
119
+ return string.getText().length === 0
120
+ ? string_1.SassString.empty({ quotes: string.getQuoted() })
121
+ : new string_1.SassString(string.getText(), { quotes: string.getQuoted() });
122
+ }
123
+ case proto.Value.ValueCase.NUMBER:
124
+ return this.deprotofyNumber(value.getNumber());
125
+ case proto.Value.ValueCase.RGB_COLOR: {
126
+ const color = value.getRgbColor();
127
+ return new color_1.SassColor({
128
+ red: color.getRed(),
129
+ green: color.getGreen(),
130
+ blue: color.getBlue(),
131
+ alpha: color.getAlpha(),
132
+ });
133
+ }
134
+ case proto.Value.ValueCase.HSL_COLOR: {
135
+ const color = value.getHslColor();
136
+ return new color_1.SassColor({
137
+ hue: color.getHue(),
138
+ saturation: color.getSaturation(),
139
+ lightness: color.getLightness(),
140
+ alpha: color.getAlpha(),
141
+ });
142
+ }
143
+ case proto.Value.ValueCase.LIST: {
144
+ const list = value.getList();
145
+ const separator = this.deprotofySeparator(list.getSeparator());
146
+ const contents = list.getContentsList();
147
+ if (separator === null && contents.length > 1) {
148
+ throw utils.compilerError(`Value.List ${list} can't have an undecided separator because it ` +
149
+ `has ${contents.length} elements`);
150
+ }
151
+ return new list_1.SassList(contents.map(element => this.deprotofy(element)), { separator, brackets: list.getHasBrackets() });
152
+ }
153
+ case proto.Value.ValueCase.MAP:
154
+ return new map_1.SassMap((0, immutable_1.OrderedMap)(value
155
+ .getMap()
156
+ .getEntriesList()
157
+ .map(entry => {
158
+ const key = entry.getKey();
159
+ if (!key)
160
+ throw utils.mandatoryError('Value.Map.Entry.key');
161
+ const value = entry.getValue();
162
+ if (!value)
163
+ throw utils.mandatoryError('Value.Map.Entry.value');
164
+ return [this.deprotofy(key), this.deprotofy(value)];
165
+ })));
166
+ case proto.Value.ValueCase.SINGLETON:
167
+ switch (value.getSingleton()) {
168
+ case proto.SingletonValue.TRUE:
169
+ return boolean_1.sassTrue;
170
+ case proto.SingletonValue.FALSE:
171
+ return boolean_1.sassFalse;
172
+ case proto.SingletonValue.NULL:
173
+ return null_1.sassNull;
174
+ default:
175
+ throw utils.compilerError(`Unknown Value.singleton ${value.getSingleton()}`);
176
+ }
177
+ default:
178
+ throw utils.mandatoryError('Value.value');
179
+ }
180
+ }
181
+ /** Converts `number` to its JS representation. */
182
+ deprotofyNumber(number) {
183
+ return new number_1.SassNumber(number.getValue(), {
184
+ numeratorUnits: number.getNumeratorsList(),
185
+ denominatorUnits: number.getDenominatorsList(),
186
+ });
187
+ }
188
+ /** Converts `separator` to its JS representation. */
189
+ deprotofySeparator(separator) {
190
+ switch (separator) {
191
+ case proto.ListSeparator.COMMA:
192
+ return ',';
193
+ case proto.ListSeparator.SPACE:
194
+ return ' ';
195
+ case proto.ListSeparator.SLASH:
196
+ return '/';
197
+ case proto.ListSeparator.UNDECIDED:
198
+ return null;
199
+ default:
200
+ throw utils.compilerError(`Unknown separator ${separator}`);
201
+ }
202
+ }
203
+ }
204
+ exports.Protofier = Protofier;
205
+ //# sourceMappingURL=protofier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protofier.js","sourceRoot":"","sources":["../../../lib/src/protofier.ts"],"names":[],"mappings":";AAAA,uEAAuE;AACvE,gEAAg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AED;;;OAGG;IACH,OAAO,CAAC,EAAU,EAAE,IAAgD;QAClE,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,SAAS,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE;YACjE,MAAM,KAAK,CAAC,eAAe,EAAE,uCAAuC,CAAC,CAAC;SACvE;aAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE;YACrC,MAAM,KAAK,CACT,oBAAoB,EAAE,oDAAoD,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,iBAAiB,IAAI,GAAG,CACpH,CAAC;SACH;QACD,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;IAC3B,CAAC;CACF;AAjDD,wCAiDC"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ // Copyright 2021 Google LLC. Use of this source code is governed by an
3
+ // MIT-style license that can be found in the LICENSE file or at
4
+ // https://opensource.org/licenses/MIT.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SyncEmbeddedCompiler = void 0;
7
+ const rxjs_1 = require("rxjs");
8
+ const sync_process_1 = require("./sync-process");
9
+ const compiler_path_1 = require("./compiler-path");
10
+ /**
11
+ * A synchronous wrapper for the embedded Sass compiler that exposes its stdio
12
+ * streams as Observables.
13
+ */
14
+ class SyncEmbeddedCompiler {
15
+ constructor() {
16
+ /** The underlying process that's being wrapped. */
17
+ this.process = new sync_process_1.SyncProcess(compiler_path_1.compilerPath, { windowsHide: true });
18
+ /** The buffers emitted by the child process's stdout. */
19
+ this.stdout$ = new rxjs_1.Subject();
20
+ /** The buffers emitted by the child process's stderr. */
21
+ this.stderr$ = new rxjs_1.Subject();
22
+ }
23
+ /** Writes `buffer` to the child process's stdin. */
24
+ writeStdin(buffer) {
25
+ this.process.stdin.write(buffer);
26
+ }
27
+ yield() {
28
+ const event = this.process.yield();
29
+ switch (event.type) {
30
+ case 'stdout':
31
+ this.stdout$.next(event.data);
32
+ return true;
33
+ case 'stderr':
34
+ this.stderr$.next(event.data);
35
+ return true;
36
+ case 'exit':
37
+ return false;
38
+ }
39
+ }
40
+ /** Blocks until the underlying process exits. */
41
+ yieldUntilExit() {
42
+ while (this.yield()) {
43
+ // Any events will be handled by `this.yield()`.
44
+ }
45
+ }
46
+ /** Kills the child process, cleaning up all associated Observables. */
47
+ close() {
48
+ this.process.stdin.end();
49
+ }
50
+ }
51
+ exports.SyncEmbeddedCompiler = SyncEmbeddedCompiler;
52
+ //# sourceMappingURL=sync-compiler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-compiler.js","sourceRoot":"","sources":["../../../lib/src/sync-compiler.ts"],"names":[],"mappings":";AAAA,uEAAuE;AACvE,gEAAgE;AAChE,uCAAuC;;;AAEvC,+BAA6B;AAE7B,iDAA2C;AAC3C,mDAA6C;AAE7C;;;GAGG;AACH,MAAa,oBAAoB;IAAjC;QACE,mDAAmD;QAClC,YAAO,GAAG,IAAI,0BAAW,CAAC,4BAAY,EAAE,EAAC,WAAW,EAAE,IAAI,EAAC,CAAC,CAAC;QAE9E,yDAAyD;QAChD,YAAO,GAAG,IAAI,cAAO,EAAU,CAAC;QAEzC,yDAAyD;QAChD,YAAO,GAAG,IAAI,cAAO,EAAU,CAAC;IAkC3C,CAAC;IAhCC,oDAAoD;IACpD,UAAU,CAAC,MAAc;QACvB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,KAAK;QACH,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACnC,QAAQ,KAAK,CAAC,IAAI,EAAE;YAClB,KAAK,QAAQ;gBACX,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBAC9B,OAAO,IAAI,CAAC;YAEd,KAAK,QAAQ;gBACX,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBAC9B,OAAO,IAAI,CAAC;YAEd,KAAK,MAAM;gBACT,OAAO,KAAK,CAAC;SAChB;IACH,CAAC;IAED,iDAAiD;IACjD,cAAc;QACZ,OAAO,IAAI,CAAC,KAAK,EAAE,EAAE;YACnB,gDAAgD;SACjD;IACH,CAAC;IAED,uEAAuE;IACvE,KAAK;QACH,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IAC3B,CAAC;CACF;AA1CD,oDA0CC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
- // Copyright 2020 Google Inc. Use of this source code is governed by an
2
+ // Copyright 2021 Google LLC. Use of this source code is governed by an
3
3
  // MIT-style license that can be found in the LICENSE file or at
4
4
  // https://opensource.org/licenses/MIT.
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- //# sourceMappingURL=span.js.map
6
+ //# sourceMappingURL=event.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.js","sourceRoot":"","sources":["../../../../lib/src/sync-process/event.ts"],"names":[],"mappings":";AAAA,uEAAuE;AACvE,gEAAgE;AAChE,uCAAuC"}
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ // Copyright 2021 Google LLC. Use of this source code is governed by an
3
+ // MIT-style license that can be found in the LICENSE file or at
4
+ // https://opensource.org/licenses/MIT.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SyncProcess = void 0;
7
+ const fs = require("fs");
8
+ const p = require("path");
9
+ const stream = require("stream");
10
+ const worker_threads_1 = require("worker_threads");
11
+ const sync_message_port_1 = require("./sync-message-port");
12
+ // TODO(nex3): Factor this out into its own package.
13
+ /**
14
+ * A child process that runs synchronously while also allowing the user to
15
+ * interact with it before it shuts down.
16
+ */
17
+ class SyncProcess {
18
+ constructor(command, argsOrOptions, options) {
19
+ let args;
20
+ if (Array.isArray(argsOrOptions)) {
21
+ args = argsOrOptions;
22
+ }
23
+ else {
24
+ args = [];
25
+ options = argsOrOptions;
26
+ }
27
+ const { port1, port2 } = sync_message_port_1.SyncMessagePort.createChannel();
28
+ this.port = new sync_message_port_1.SyncMessagePort(port1);
29
+ this.worker = spawnWorker(p.join(p.dirname(__filename), 'worker'), {
30
+ workerData: { port: port2, command, args, options },
31
+ transferList: [port2],
32
+ });
33
+ // The worker shouldn't emit any errors unless it breaks in development.
34
+ this.worker.on('error', console.error);
35
+ this.stdin = new stream.Writable({
36
+ write: (chunk, encoding, callback) => {
37
+ this.port.postMessage({
38
+ type: 'stdin',
39
+ data: chunk,
40
+ }, [chunk.buffer]);
41
+ callback();
42
+ },
43
+ });
44
+ // Unfortunately, there's no built-in event or callback that will reliably
45
+ // *synchronously* notify us that the stdin stream has been closed. (The
46
+ // `final` callback works in Node v16 but not v14.) Instead, we wrap the
47
+ // methods themselves that are used to close the stream.
48
+ const oldEnd = this.stdin.end.bind(this.stdin);
49
+ this.stdin.end = ((a1, a2, a3) => {
50
+ oldEnd(a1, a2, a3);
51
+ this.port.postMessage({ type: 'stdinClosed' });
52
+ });
53
+ const oldDestroy = this.stdin.destroy.bind(this.stdin);
54
+ this.stdin.destroy = ((a1) => {
55
+ oldDestroy(a1);
56
+ this.port.postMessage({ type: 'stdinClosed' });
57
+ });
58
+ }
59
+ /**
60
+ * Blocks until the child process is ready to emit another event, then returns
61
+ * that event.
62
+ *
63
+ * If there's an error running the child process, this will throw that error.
64
+ * This may not be called after it emits an `ExitEvent` or throws an error.
65
+ */
66
+ yield() {
67
+ if (this.stdin.destroyed) {
68
+ throw new Error("Can't call SyncProcess.yield() after the process has exited.");
69
+ }
70
+ const message = this.port.receiveMessage();
71
+ switch (message.type) {
72
+ case 'stdout':
73
+ return { type: 'stdout', data: Buffer.from(message.data.buffer) };
74
+ case 'stderr':
75
+ return { type: 'stderr', data: Buffer.from(message.data.buffer) };
76
+ case 'error':
77
+ this.close();
78
+ throw message.error;
79
+ case 'exit':
80
+ this.close();
81
+ return message;
82
+ }
83
+ }
84
+ // TODO(nex3): Add a non-blocking `yieldIfReady()` function that returns
85
+ // `null` if the worker hasn't queued up an event.
86
+ // TODO(nex3): Add a `yieldAsync()` function that returns a `Promise<Event>`.
87
+ /**
88
+ * Sends a signal (`SIGTERM` by default) to the child process.
89
+ *
90
+ * This has no effect if the process has already exited.
91
+ */
92
+ kill(signal) {
93
+ this.port.postMessage({ type: 'kill', signal });
94
+ }
95
+ /** Closes down the worker thread and the stdin stream. */
96
+ close() {
97
+ this.port.close();
98
+ this.worker.terminate();
99
+ this.stdin.destroy();
100
+ }
101
+ }
102
+ exports.SyncProcess = SyncProcess;
103
+ /**
104
+ * Spawns a worker for the given `fileWithoutExtension` in either a JS or TS
105
+ * worker, depending on which file exists.
106
+ */
107
+ function spawnWorker(fileWithoutExtension, options) {
108
+ // The released version always spawns the JS worker. The TS worker is only
109
+ // used for development.
110
+ const jsFile = fileWithoutExtension + '.js';
111
+ if (fs.existsSync(jsFile))
112
+ return new worker_threads_1.Worker(jsFile, options);
113
+ const tsFile = fileWithoutExtension + '.ts';
114
+ if (fs.existsSync(tsFile)) {
115
+ return new worker_threads_1.Worker(`
116
+ require('ts-node').register();
117
+ require(${JSON.stringify(tsFile)});
118
+ `, { ...options, eval: true });
119
+ }
120
+ throw new Error(`Neither "${jsFile}" nor ".ts" exists.`);
121
+ }
122
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../lib/src/sync-process/index.ts"],"names":[],"mappings":";AAAA,uEAAuE;AACvE,gEAAgE;AAChE,uCAAuC;;;AAEvC,yBAAyB;AACzB,0BAA0B;AAC1B,iCAAiC;AACjC,mDAAqD;AAErD,2DAAoD;AAKpD,oDAAoD;AAEpD;;;GAGG;AACH,MAAa,WAAW;IActB,YACE,OAAe,EACf,aAAkC,EAClC,OAAiB;QAEjB,IAAI,IAAc,CAAC;QACnB,IAAI,KAAK,CAAC,OAAO,CAAC,a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AAkD;IAElD,6EAA6E;IAE7E;;;;OAIG;IACH,IAAI,CAAC,MAAgC;QACnC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,EAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAC,CAAC,CAAC;IAChD,CAAC;IAED,0DAA0D;IAClD,KAAK;QACX,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QAClB,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACxB,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IACvB,CAAC;CACF;AA9HD,kCA8HC;AAED;;;GAGG;AACH,SAAS,WAAW,CAClB,oBAA4B,EAC5B,OAAsB;IAEtB,0EAA0E;IAC1E,wBAAwB;IACxB,MAAM,MAAM,GAAG,oBAAoB,GAAG,KAAK,CAAC;IAC5C,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,uBAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE9D,MAAM,MAAM,GAAG,oBAAoB,GAAG,KAAK,CAAC;IAC5C,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE;QACzB,OAAO,IAAI,uBAAM,CACf;;kBAEY,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;OACjC,EACD,EAAC,GAAG,OAAO,EAAE,IAAI,EAAE,IAAI,EAAC,CACzB,CAAC;KACH;IAED,MAAM,IAAI,KAAK,CAAC,YAAY,MAAM,qBAAqB,CAAC,CAAC;AAC3D,CAAC"}
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ // Copyright 2021 Google LLC. Use of this source code is governed by an
3
+ // MIT-style license that can be found in the LICENSE file or at
4
+ // https://opensource.org/licenses/MIT.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SyncMessagePort = void 0;
7
+ const assert_1 = require("assert");
8
+ const events_1 = require("events");
9
+ const worker_threads_1 = require("worker_threads");
10
+ // TODO(nex3): Make this its own package.
11
+ /**
12
+ * An enum of possible states for the shared buffer that two `SyncMessagePort`s
13
+ * use to communicate.
14
+ */
15
+ var BufferState;
16
+ (function (BufferState) {
17
+ /**
18
+ * The initial state. When an endpoint is ready to receive messages, it'll set
19
+ * the buffer to this state so that it can use `Atomics.wait()` to be notified
20
+ * when it switches to `MessageSent`.
21
+ */
22
+ BufferState[BufferState["AwaitingMessage"] = 0] = "AwaitingMessage";
23
+ /**
24
+ * The state indicating that a message has been sent. Whenever an endpoint
25
+ * sends a message, it'll set the buffer to this state so that the other
26
+ * endpoint's `Atomics.wait()` call terminates.
27
+ */
28
+ BufferState[BufferState["MessageSent"] = 1] = "MessageSent";
29
+ /**
30
+ * The state indicating that the channel has been closed. This never
31
+ * transitions to any other states.
32
+ */
33
+ BufferState[BufferState["Closed"] = 2] = "Closed";
34
+ })(BufferState || (BufferState = {}));
35
+ /**
36
+ * A communication port that can receive messages synchronously from another
37
+ * `SyncMessagePort`.
38
+ *
39
+ * This also emits the same asynchronous events as `MessagePort`.
40
+ */
41
+ class SyncMessagePort extends events_1.EventEmitter {
42
+ /**
43
+ * Creates a new message port. The `port` must be created by
44
+ * `SyncMessagePort.createChannel()` and must connect to a port passed to
45
+ * another `SyncMessagePort` in another worker.
46
+ */
47
+ constructor(port) {
48
+ var _a;
49
+ super();
50
+ this.port = port;
51
+ const buffer = (_a = (0, worker_threads_1.receiveMessageOnPort)(this.port)) === null || _a === void 0 ? void 0 : _a.message;
52
+ if (!buffer) {
53
+ throw new Error('new SyncMessagePort() must be passed a port from ' +
54
+ 'SyncMessagePort.createChannel().');
55
+ }
56
+ this.buffer = new Int32Array(buffer);
57
+ this.on('newListener', (event, listener) => {
58
+ this.port.on(event, listener);
59
+ });
60
+ this.on('removeListener', (event, listener) => this.port.removeListener(event, listener));
61
+ }
62
+ /** Creates a channel whose ports can be passed to `new SyncMessagePort()`. */
63
+ static createChannel() {
64
+ const channel = new worker_threads_1.MessageChannel();
65
+ // Four bytes is the minimum necessary to use `Atomics.wait()`.
66
+ const buffer = new SharedArrayBuffer(4);
67
+ // Queue up messages on each port so the caller doesn't have to explicitly
68
+ // pass the buffer around along with them.
69
+ channel.port1.postMessage(buffer);
70
+ channel.port2.postMessage(buffer);
71
+ return channel;
72
+ }
73
+ /** See `MessagePort.postMesage()`. */
74
+ postMessage(value, transferList) {
75
+ this.port.postMessage(value, transferList);
76
+ // If the other port is waiting for a new message, notify it that the
77
+ // message is ready. Use `Atomics.compareExchange` so that we don't
78
+ // overwrite the "closed" state.
79
+ if (Atomics.compareExchange(this.buffer, 0, BufferState.AwaitingMessage, BufferState.MessageSent) === BufferState.AwaitingMessage) {
80
+ Atomics.notify(this.buffer, 0);
81
+ }
82
+ }
83
+ // TODO(nex3):
84
+ // * Add a non-blocking `receiveMessage()`
85
+ // * Add a timeout option to `receiveMessage()`
86
+ // * Add an option to `receiveMessage()` to return a special value if the
87
+ // channel is closed.
88
+ /**
89
+ * Blocks and returns the next message sent by the other port.
90
+ *
91
+ * This may not be called while this has a listener for the `'message'` event.
92
+ * Throws an error if the channel is closed, including if it closes while this
93
+ * is waiting for a message.
94
+ */
95
+ receiveMessage() {
96
+ if (this.listenerCount('message')) {
97
+ throw new Error('SyncMessageChannel.receiveMessage() may not be called while there ' +
98
+ 'are message listeners.');
99
+ }
100
+ // Set the "new message" indicator to zero before we check for new messages.
101
+ // That way if the other port sets it to 1 between the call to
102
+ // `receiveMessageOnPort` and the call to `Atomics.wait()`, we won't
103
+ // overwrite it. Use `Atomics.compareExchange` so that we don't overwrite
104
+ // the "closed" state.
105
+ if (Atomics.compareExchange(this.buffer, 0, BufferState.MessageSent, BufferState.AwaitingMessage) === BufferState.Closed) {
106
+ throw new Error("The SyncMessagePort's channel is closed.");
107
+ }
108
+ let message = (0, worker_threads_1.receiveMessageOnPort)(this.port);
109
+ if (message)
110
+ return message.message;
111
+ // If there's no new message, wait for the other port to flip the "new
112
+ // message" indicator to 1. If it's been set to 1 since we stored 0, this
113
+ // will terminate immediately.
114
+ Atomics.wait(this.buffer, 0, BufferState.AwaitingMessage);
115
+ message = (0, worker_threads_1.receiveMessageOnPort)(this.port);
116
+ if (message)
117
+ return message.message;
118
+ assert_1.strict.equal(Atomics.load(this.buffer, 0), BufferState.Closed);
119
+ throw new Error("The SyncMessagePort's channel is closed.");
120
+ }
121
+ /** See `MessagePort.close()`. */
122
+ close() {
123
+ Atomics.store(this.buffer, 0, BufferState.Closed);
124
+ this.port.close();
125
+ }
126
+ }
127
+ exports.SyncMessagePort = SyncMessagePort;
128
+ //# sourceMappingURL=sync-message-port.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-message-port.js","sourceRoot":"","sources":["../../../../lib/src/sync-process/sync-message-port.ts"],"names":[],"mappings":";AAAA,uEAAuE;AACvE,gEAAgE;AAChE,uCAAuC;;;AAEvC,mCAAwC;AACxC,mCAAoC;AACpC,mDAKwB;AAExB,yCAAyC;AAEzC;;;GAGG;AACH,IAAK,WAkBJ;AAlBD,WAAK,WAAW;IACd;;;;OAIG;IACH,mEAAe,CAAA;IACf;;;;OAIG;IACH,2DAAW,CAAA;IACX;;;OAGG;IACH,iDAAM,CAAA;AACR,CAAC,EAlBI,WAAW,KAAX,WAAW,QAkBf;AAED;;;;;GAKG;AACH,MAAa,eAAgB,SAAQ,qBAAY;IAwB/C;;;;OAIG;IACH,YAA6B,IAAiB;;QAC5C,KAAK,EAAE,CAAC;QADmB,SAAI,GAAJ,IAAI,CAAa;QAG5C,MAAM,MAAM,GAAG,MAAA,IAAA,qCAAoB,EAAC,IAAI,CAAC,IAAI,CAAC,0CAAE,OAAO,CAAC;QACxD,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,KAAK,CACb,mDAAmD;gBACjD,kCAAkC,CACrC,CAAC;SACH;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,MAA2B,CAAC,CAAC;QAE1D,IAAI,CAAC,EAAE,CAAC,aAAa,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE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;gBAClE,wBAAwB,CAC3B,CAAC;SACH;QAED,4EAA4E;QAC5E,8DAA8D;QAC9D,oEAAoE;QACpE,yEAAyE;QACzE,sBAAsB;QACtB,IACE,OAAO,CAAC,eAAe,CACrB,IAAI,CAAC,MAAM,EACX,CAAC,EACD,WAAW,CAAC,WAAW,EACvB,WAAW,CAAC,eAAe,CAC5B,KAAK,WAAW,CAAC,MAAM,EACxB;YACA,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;SAC7D;QAED,IAAI,OAAO,GAAG,IAAA,qCAAoB,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9C,IAAI,OAAO;YAAE,OAAO,OAAO,CAAC,OAAO,CAAC;QAEpC,sEAAsE;QACtE,yEAAyE;QACzE,8BAA8B;QAC9B,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC,eAAe,CAAC,CAAC;QAC1D,OAAO,GAAG,IAAA,qCAAoB,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1C,IAAI,OAAO;YAAE,OAAO,OAAO,CAAC,OAAO,CAAC;QAEpC,eAAM,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;QAC/D,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;IAED,iCAAiC;IACjC,KAAK;QACH,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC;CACF;AA5HD,0CA4HC"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ // Copyright 2021 Google LLC. Use of this source code is governed by an
3
+ // MIT-style license that can be found in the LICENSE file or at
4
+ // https://opensource.org/licenses/MIT.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const worker_threads_1 = require("worker_threads");
7
+ const child_process_1 = require("child_process");
8
+ const assert_1 = require("assert");
9
+ const sync_message_port_1 = require("./sync-message-port");
10
+ const port = new sync_message_port_1.SyncMessagePort(worker_threads_1.workerData.port);
11
+ /** A more type-safe way to call `port.postMesage()` */
12
+ function emit(event, transferList) {
13
+ port.postMessage(event, transferList);
14
+ }
15
+ const process = (0, child_process_1.spawn)(worker_threads_1.workerData.command, worker_threads_1.workerData.args, worker_threads_1.workerData.options);
16
+ port.on('message', message => {
17
+ if (message.type === 'stdin') {
18
+ process.stdin.write(message.data);
19
+ }
20
+ else if (message.type === 'stdinClosed') {
21
+ process.stdin.end();
22
+ }
23
+ else {
24
+ assert_1.strict.equal(message.type, 'kill');
25
+ process.kill(message.signal);
26
+ }
27
+ });
28
+ process.stdout.on('data', data => {
29
+ emit({ type: 'stdout', data }, [data.buffer]);
30
+ });
31
+ process.stderr.on('data', data => {
32
+ emit({ type: 'stderr', data }, [data.buffer]);
33
+ });
34
+ process.on('error', error => {
35
+ emit({ type: 'error', error });
36
+ process.kill();
37
+ worker_threads_1.parentPort.close();
38
+ port.close();
39
+ });
40
+ process.on('exit', (code, signal) => {
41
+ if (code !== null) {
42
+ emit({ type: 'exit', code });
43
+ }
44
+ else {
45
+ (0, assert_1.strict)(signal);
46
+ emit({ type: 'exit', signal });
47
+ }
48
+ worker_threads_1.parentPort.close();
49
+ port.close();
50
+ });
51
+ //# sourceMappingURL=worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../../lib/src/sync-process/worker.ts"],"names":[],"mappings":";AAAA,uEAAuE;AACvE,gEAAgE;AAChE,uCAAuC;;AAEvC,mDAKwB;AACxB,iDAA8D;AAC9D,mCAAwC;AAExC,2DAAoD;AAGpD,MAAM,IAAI,GAAG,IAAI,mCAAe,CAAC,2BAAU,CAAC,IAAmB,CAAC,CAAC;AAEjE,uDAAu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