jsii-rosetta 6.0.27 → 6.0.28-dev.1
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.
- package/README.md +14 -0
- package/lib/translate_all.d.ts +1 -1
- package/lib/translate_all.js +8 -2
- package/lib/translate_all.js.map +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -392,6 +392,20 @@ The diagram below shows how data flows through the jsii tools when used together
|
|
|
392
392
|
|
|
393
393
|
## Advanced topics
|
|
394
394
|
|
|
395
|
+
### Bundling jsii-rosetta
|
|
396
|
+
|
|
397
|
+
`jsii-rosetta` translates snippets in worker threads that run the separate script
|
|
398
|
+
`translate_all_worker.js`. It loads that script from the same directory as its own code.
|
|
399
|
+
Bundlers like esbuild don't detect worker scripts, so if you bundle `jsii-rosetta` you need
|
|
400
|
+
to emit the worker as an extra entry point named `translate_all_worker.js`, next to your bundle:
|
|
401
|
+
|
|
402
|
+
```sh
|
|
403
|
+
esbuild index.ts jsii-rosetta/translate_all_worker \
|
|
404
|
+
--bundle --platform=node --outdir=dist '--entry-names=[name]'
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
If the worker script is missing, `translateAll()` throws an error that points to this section.
|
|
408
|
+
|
|
395
409
|
### Hiding code from samples
|
|
396
410
|
|
|
397
411
|
In order to make examples compile, boilerplate code may need to be added that detracts from the example at hand (such as
|
package/lib/translate_all.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { RosettaDiagnostic, SnippetTimingInfo } from './translate';
|
|
|
11
11
|
* Never include 'translate_all_worker' directly, only do TypeScript type references (so that in
|
|
12
12
|
* the script we may assume that 'worker_threads' successfully imports).
|
|
13
13
|
*/
|
|
14
|
-
export declare function translateAll(snippets: TypeScriptSnippet[], includeCompilerDiagnostics: boolean, batchSize?: number): Promise<TranslateAllResult>;
|
|
14
|
+
export declare function translateAll(snippets: TypeScriptSnippet[], includeCompilerDiagnostics: boolean, batchSize?: number, workerScript?: string): Promise<TranslateAllResult>;
|
|
15
15
|
export interface TranslateAllResult {
|
|
16
16
|
translatedSnippets: TranslatedSnippet[];
|
|
17
17
|
diagnostics: RosettaDiagnostic[];
|
package/lib/translate_all.js
CHANGED
|
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.translateAll = translateAll;
|
|
37
|
+
const node_fs_1 = require("node:fs");
|
|
37
38
|
const os = __importStar(require("node:os"));
|
|
38
39
|
const path = __importStar(require("node:path"));
|
|
39
40
|
const workerpool = __importStar(require("workerpool"));
|
|
@@ -50,7 +51,12 @@ const translate_1 = require("./translate");
|
|
|
50
51
|
* Never include 'translate_all_worker' directly, only do TypeScript type references (so that in
|
|
51
52
|
* the script we may assume that 'worker_threads' successfully imports).
|
|
52
53
|
*/
|
|
53
|
-
async function translateAll(snippets, includeCompilerDiagnostics, batchSize) {
|
|
54
|
+
async function translateAll(snippets, includeCompilerDiagnostics, batchSize, workerScript = path.join(__dirname, 'translate_all_worker.js')) {
|
|
55
|
+
if (!(0, node_fs_1.existsSync)(workerScript)) {
|
|
56
|
+
throw new Error(`jsii-rosetta worker script not found: ${workerScript}. ` +
|
|
57
|
+
'If you bundle jsii-rosetta, also bundle its worker as translate_all_worker.js next to your bundle, ' +
|
|
58
|
+
'see https://github.com/aws/jsii-rosetta#bundling-jsii-rosetta');
|
|
59
|
+
}
|
|
54
60
|
// Use about half the advertised cores because hyperthreading doesn't seem to
|
|
55
61
|
// help that much, or we become I/O-bound at some point. On my machine, using
|
|
56
62
|
// more than half the cores actually makes it slower.
|
|
@@ -61,7 +67,7 @@ async function translateAll(snippets, includeCompilerDiagnostics, batchSize) {
|
|
|
61
67
|
const snippetArr = Array.from(snippets);
|
|
62
68
|
const batchesOf = batchSize ? ` (in batches of ${batchSize})` : '';
|
|
63
69
|
logging.info(`Translating ${snippetArr.length} snippets using ${N} workers${batchesOf}`);
|
|
64
|
-
const pool = workerpool.pool(
|
|
70
|
+
const pool = workerpool.pool(workerScript, {
|
|
65
71
|
maxWorkers: N,
|
|
66
72
|
});
|
|
67
73
|
try {
|
package/lib/translate_all.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translate_all.js","sourceRoot":"","sources":["../src/translate_all.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"translate_all.js","sourceRoot":"","sources":["../src/translate_all.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,oCAyDC;AA9ED,qCAAqC;AACrC,4CAA8B;AAC9B,gDAAkC;AAClC,uDAAyC;AAEzC,mDAAqC;AAErC,+CAAsD;AACtD,2CAAsF;AAGtF;;;;;;;;;GASG;AACI,KAAK,UAAU,YAAY,CAChC,QAA6B,EAC7B,0BAAmC,EACnC,SAAkB,EAClB,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,yBAAyB,CAAC;IAE9D,IAAI,CAAC,IAAA,oBAAU,EAAC,YAAY,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CACb,yCAAyC,YAAY,IAAI;YACvD,qGAAqG;YACrG,+DAA+D,CAClE,CAAC;IACJ,CAAC;IACD,6EAA6E;IAC7E,6EAA6E;IAC7E,qDAAqD;IACrD,2FAA2F;IAC3F,MAAM,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,6BAA6B;QACjD,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC;QACrD,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/D,MAAM,UAAU,GAA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C,GAAG,CAAC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS;SAC1D,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,yBAAyB;QACzB,KAAK,IAAI,CAAC,SAAS,EAAE,CAAC;IACxB,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CACpB,QAA6B,EAC7B,0BAAmC,EACnC,SAAS,GAAG,EAAE,EACd,yBAAkC,KAAK;IAEvC,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IACnC,MAAM,GAAG,GAA4B,EAAE,CAAC;IAExC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,SAAS,EAAE,CAAC;QACpD,0EAA0E;QAC1E,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QACtE,GAAG,CAAC,IAAI,CAAC;YACP,UAAU,EAAE,UAAU,QAAQ,EAAE;YAChC,QAAQ,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;YAC1C,0BAA0B;YAC1B,QAAQ;YACR,SAAS,EAAE,sBAAsB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS;SAC1D,CAAC,CAAC;IACL,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { existsSync } from 'node:fs';\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport * as workerpool from 'workerpool';\n\nimport * as logging from './logging';\nimport { TypeScriptSnippet } from './snippet';\nimport { TranslatedSnippet } from './tablets/tablets';\nimport { RosettaDiagnostic, SnippetTimingInfo, extractTimingInfo } from './translate';\nimport type { TranslateBatchRequest, TranslateBatchResponse } from './translate_all_worker';\n\n/**\n * Divide the work evenly over all processors by running 'translate_all_worker' in Worker Threads, then combine results\n *\n * The workers are fed small queues of work each. We used to divide the entire queue into N\n * but since the work is divided unevenly that led to some workers stopping early, idling while\n * waiting for more work.\n *\n * Never include 'translate_all_worker' directly, only do TypeScript type references (so that in\n * the script we may assume that 'worker_threads' successfully imports).\n */\nexport async function translateAll(\n snippets: TypeScriptSnippet[],\n includeCompilerDiagnostics: boolean,\n batchSize?: number,\n workerScript = path.join(__dirname, 'translate_all_worker.js'),\n): Promise<TranslateAllResult> {\n if (!existsSync(workerScript)) {\n throw new Error(\n `jsii-rosetta worker script not found: ${workerScript}. ` +\n 'If you bundle jsii-rosetta, also bundle its worker as translate_all_worker.js next to your bundle, ' +\n 'see https://github.com/aws/jsii-rosetta#bundling-jsii-rosetta',\n );\n }\n // Use about half the advertised cores because hyperthreading doesn't seem to\n // help that much, or we become I/O-bound at some point. On my machine, using\n // more than half the cores actually makes it slower.\n // Cap to a reasonable top-level limit to prevent thrash on machines with many, many cores.\n const N = process.env.JSII_ROSETTA_MAX_WORKER_COUNT\n ? parseInt(process.env.JSII_ROSETTA_MAX_WORKER_COUNT)\n : Math.min(16, Math.max(1, Math.ceil(os.cpus().length / 2)));\n const snippetArr = Array.from(snippets);\n const batchesOf = batchSize ? ` (in batches of ${batchSize})` : '';\n logging.info(`Translating ${snippetArr.length} snippets using ${N} workers${batchesOf}`);\n\n const pool = workerpool.pool(workerScript, {\n maxWorkers: N,\n });\n\n try {\n const shouldBatchCompilation = batchSize != null;\n const requests = batchSnippets(snippetArr, includeCompilerDiagnostics, batchSize, shouldBatchCompilation);\n\n const responses: TranslateBatchResponse[] = await Promise.all(\n requests.map((request) => pool.exec('translateBatch', [request])),\n );\n\n const allDiagnostics = new Array<RosettaDiagnostic>();\n const translatedSnippets = new Array<TranslatedSnippet>();\n\n // Combine results\n for (const response of responses) {\n allDiagnostics.push(...response.diagnostics);\n translatedSnippets.push(...response.translatedSchemas.map(TranslatedSnippet.fromSchema));\n }\n\n // Extract timing info if enabled\n const { timings, diagnostics } = extractTimingInfo(allDiagnostics);\n\n return {\n diagnostics,\n translatedSnippets,\n timings: process.env.TIMING === '1' ? timings : undefined,\n };\n } finally {\n // Not waiting on purpose\n void pool.terminate();\n }\n}\n\nfunction batchSnippets(\n snippets: TypeScriptSnippet[],\n includeCompilerDiagnostics: boolean,\n batchSize = 10,\n shouldBatchCompilation: boolean = false,\n): TranslateBatchRequest[] {\n const logLevel = logging.current();\n const ret: TranslateBatchRequest[] = [];\n\n for (let i = 0; i < snippets.length; i += batchSize) {\n // create a unique worker name, purely for logging so can be pseudo random\n const workerId = Math.random().toString(16).slice(2, 6).toUpperCase();\n ret.push({\n workerName: `Worker#${workerId}`,\n snippets: snippets.slice(i, i + batchSize),\n includeCompilerDiagnostics,\n logLevel,\n batchSize: shouldBatchCompilation ? batchSize : undefined,\n });\n }\n\n return ret;\n}\n\nexport interface TranslateAllResult {\n translatedSnippets: TranslatedSnippet[];\n diagnostics: RosettaDiagnostic[];\n timings?: SnippetTimingInfo[];\n}\n"]}
|
package/package.json
CHANGED
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"jest": "^30",
|
|
53
53
|
"memfs": "^4.80.1",
|
|
54
54
|
"prettier": "^2.8.8",
|
|
55
|
-
"projen": "^0.105.
|
|
55
|
+
"projen": "^0.105.7",
|
|
56
56
|
"tar": "^7.5.23",
|
|
57
57
|
"ts-jest": "^29",
|
|
58
58
|
"ts-node": "^10.9.2"
|
|
@@ -88,12 +88,14 @@
|
|
|
88
88
|
"publishConfig": {
|
|
89
89
|
"access": "public"
|
|
90
90
|
},
|
|
91
|
-
"version": "6.0.
|
|
91
|
+
"version": "6.0.28-dev.1",
|
|
92
92
|
"packageManager": "yarn@4.13.0",
|
|
93
93
|
"types": "lib/index.d.ts",
|
|
94
94
|
"exports": {
|
|
95
95
|
".": "./lib/index.js",
|
|
96
|
+
"./translate_all_worker": "./lib/translate_all_worker.js",
|
|
96
97
|
"./package.json": "./package.json"
|
|
97
98
|
},
|
|
98
|
-
"//": "~~ Generated by projen. To modify, edit .projenrc.ts and run \"yarn projen\"."
|
|
99
|
+
"//": "~~ Generated by projen. To modify, edit .projenrc.ts and run \"yarn projen\".",
|
|
100
|
+
"stableVersion": "0.0.0"
|
|
99
101
|
}
|