jsii-rosetta 5.0.1-dev.6 → 5.0.1-dev.8

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/lib/json.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /// <reference types="node" />
2
+ import { Readable, Writable } from 'node:stream';
3
+ /**
4
+ * Asynchronously parses a single JSON value from the provided reader. The JSON
5
+ * text might be longer than what could fit in a single string value, since the
6
+ * processing is done in a streaming manner.
7
+ *
8
+ * Prefer using JSON.parse if you know the entire JSON text is always small
9
+ * enough to fit in a string value, as this would have better performance.
10
+ *
11
+ * @param reader the reader from which to consume JSON text.
12
+ *
13
+ * @returns the parse JSON value as a Javascript value.
14
+ */
15
+ export declare function parse(reader: Readable): Promise<any>;
16
+ /**
17
+ * Serializes a possibly large object into the provided writer. The object may
18
+ * be large enough that the JSON text cannot fit in a single string value.
19
+ *
20
+ * Prefer using JSON.stringify if you know the object is always small enough
21
+ * that the JSON text can fit in a single string value, as this would have
22
+ * better performance.
23
+ *
24
+ * @param value the value to be serialized.
25
+ * @param writer the write in which to write the JSON text.
26
+ */
27
+ export declare function stringify(value: any, writer: Writable): Promise<void>;
28
+ //# sourceMappingURL=json.d.ts.map
package/lib/json.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stringify = exports.parse = void 0;
4
+ const node_stream_1 = require("node:stream");
5
+ const node_util_1 = require("node:util");
6
+ const stream_json_1 = require("stream-json");
7
+ const Assembler = require("stream-json/Assembler");
8
+ const Disassembler_1 = require("stream-json/Disassembler");
9
+ const Stringer_1 = require("stream-json/Stringer");
10
+ // NB: In node 15+, there is a node:stream.promises object that has this built-in.
11
+ const asyncPipeline = (0, node_util_1.promisify)(node_stream_1.pipeline);
12
+ /**
13
+ * Asynchronously parses a single JSON value from the provided reader. The JSON
14
+ * text might be longer than what could fit in a single string value, since the
15
+ * processing is done in a streaming manner.
16
+ *
17
+ * Prefer using JSON.parse if you know the entire JSON text is always small
18
+ * enough to fit in a string value, as this would have better performance.
19
+ *
20
+ * @param reader the reader from which to consume JSON text.
21
+ *
22
+ * @returns the parse JSON value as a Javascript value.
23
+ */
24
+ function parse(reader) {
25
+ const assembler = new Assembler();
26
+ const jsonParser = (0, stream_json_1.parser)();
27
+ assembler.connectTo(jsonParser);
28
+ return asyncPipeline(reader, jsonParser).then(() => assembler.current);
29
+ }
30
+ exports.parse = parse;
31
+ /**
32
+ * Serializes a possibly large object into the provided writer. The object may
33
+ * be large enough that the JSON text cannot fit in a single string value.
34
+ *
35
+ * Prefer using JSON.stringify if you know the object is always small enough
36
+ * that the JSON text can fit in a single string value, as this would have
37
+ * better performance.
38
+ *
39
+ * @param value the value to be serialized.
40
+ * @param writer the write in which to write the JSON text.
41
+ */
42
+ function stringify(value, writer) {
43
+ const reader = new node_stream_1.Readable({ objectMode: true });
44
+ reader.push(value);
45
+ reader.push(null);
46
+ return asyncPipeline(reader, (0, Disassembler_1.disassembler)(), (0, Stringer_1.stringer)(), writer);
47
+ }
48
+ exports.stringify = stringify;
49
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;AAAA,6CAA2D;AAC3D,yCAAsC;AACtC,6CAAqC;AACrC,mDAAmD;AACnD,2DAAwD;AACxD,mDAAgD;AAEhD,kFAAkF;AAClF,MAAM,aAAa,GAAG,IAAA,qBAAS,EAAC,sBAAQ,CAAC,CAAC;AAE1C;;;;;;;;;;;GAWG;AACH,SAAgB,KAAK,CAAC,MAAgB;IACpC,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE,CAAC;IAClC,MAAM,UAAU,GAAG,IAAA,oBAAM,GAAE,CAAC;IAC5B,SAAS,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAChC,OAAO,aAAa,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AACzE,CAAC;AALD,sBAKC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,SAAS,CAAC,KAAU,EAAE,MAAgB;IACpD,MAAM,MAAM,GAAG,IAAI,sBAAQ,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;IAClD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAElB,OAAO,aAAa,CAAC,MAAM,EAAE,IAAA,2BAAY,GAAE,EAAE,IAAA,mBAAQ,GAAE,EAAE,MAAM,CAAC,CAAC;AACnE,CAAC;AAND,8BAMC","sourcesContent":["import { Readable, Writable, pipeline } from 'node:stream';\nimport { promisify } from 'node:util';\nimport { parser } from 'stream-json';\nimport * as Assembler from 'stream-json/Assembler';\nimport { disassembler } from 'stream-json/Disassembler';\nimport { stringer } from 'stream-json/Stringer';\n\n// NB: In node 15+, there is a node:stream.promises object that has this built-in.\nconst asyncPipeline = promisify(pipeline);\n\n/**\n * Asynchronously parses a single JSON value from the provided reader. The JSON\n * text might be longer than what could fit in a single string value, since the\n * processing is done in a streaming manner.\n *\n * Prefer using JSON.parse if you know the entire JSON text is always small\n * enough to fit in a string value, as this would have better performance.\n *\n * @param reader the reader from which to consume JSON text.\n *\n * @returns the parse JSON value as a Javascript value.\n */\nexport function parse(reader: Readable): Promise<any> {\n const assembler = new Assembler();\n const jsonParser = parser();\n assembler.connectTo(jsonParser);\n return asyncPipeline(reader, jsonParser).then(() => assembler.current);\n}\n\n/**\n * Serializes a possibly large object into the provided writer. The object may\n * be large enough that the JSON text cannot fit in a single string value.\n *\n * Prefer using JSON.stringify if you know the object is always small enough\n * that the JSON text can fit in a single string value, as this would have\n * better performance.\n *\n * @param value the value to be serialized.\n * @param writer the write in which to write the JSON text.\n */\nexport function stringify(value: any, writer: Writable): Promise<void> {\n const reader = new Readable({ objectMode: true });\n reader.push(value);\n reader.push(null);\n\n return asyncPipeline(reader, disassembler(), stringer(), writer);\n}\n"]}
@@ -6,6 +6,7 @@ const path = require("node:path");
6
6
  const zlib = require("node:zlib");
7
7
  const key_1 = require("./key");
8
8
  const schema_1 = require("./schema");
9
+ const json_1 = require("../json");
9
10
  const logging = require("../logging");
10
11
  const snippet_1 = require("../snippet");
11
12
  const util_1 = require("../util");
@@ -124,14 +125,17 @@ class LanguageTablet {
124
125
  * compressed and decompress accordingly.
125
126
  */
126
127
  async load(filename) {
127
- let data = await node_fs_1.promises.readFile(filename);
128
- // Gzip objects start with 1f 8b 08
129
- if (data[0] === 0x1f && data[1] === 0x8b && data[2] === 0x08) {
130
- // This is a gz object, so we decompress it now...
131
- data = zlib.gunzipSync(data);
128
+ let readStream;
129
+ if (await isGzipped(filename)) {
130
+ const gunzip = zlib.createGunzip();
131
+ (0, node_fs_1.createReadStream)(filename).pipe(gunzip, { end: true });
132
+ readStream = gunzip;
132
133
  this.compressedSource = true;
133
134
  }
134
- const obj = JSON.parse(data.toString('utf-8'));
135
+ else {
136
+ readStream = (0, node_fs_1.createReadStream)(filename);
137
+ }
138
+ const obj = await (0, json_1.parse)(readStream);
135
139
  if (!obj.toolVersion || !obj.snippets) {
136
140
  throw new Error(`File '${filename}' does not seem to be a Tablet file`);
137
141
  }
@@ -154,11 +158,10 @@ class LanguageTablet {
154
158
  */
155
159
  async save(filename, compress = false) {
156
160
  await node_fs_1.promises.mkdir(path.dirname(filename), { recursive: true });
157
- let schema = Buffer.from(JSON.stringify(this.toSchema(), null, 2));
158
- if (compress) {
159
- schema = zlib.gzipSync(schema);
160
- }
161
- await node_fs_1.promises.writeFile(filename, schema);
161
+ const writeStream = (0, node_fs_1.createWriteStream)(filename, { flags: 'w' });
162
+ const gzip = compress ? zlib.createGzip() : undefined;
163
+ gzip?.pipe(writeStream, { end: true });
164
+ return (0, json_1.stringify)(this.toSchema(), gzip ?? writeStream);
162
165
  }
163
166
  toSchema() {
164
167
  return {
@@ -262,4 +265,15 @@ class TranslatedSnippet {
262
265
  }
263
266
  }
264
267
  exports.TranslatedSnippet = TranslatedSnippet;
268
+ async function isGzipped(filename) {
269
+ const openFile = await node_fs_1.promises.open(filename, 'r');
270
+ try {
271
+ // Assumes that we can always read 3 bytes if there's that many in the file...
272
+ const { bytesRead, buffer } = await openFile.read(Buffer.alloc(4), 0, 3, 0);
273
+ return bytesRead >= 3 && buffer[0] === 0x1f && buffer[1] === 0x8b && buffer[2] === 0x08;
274
+ }
275
+ finally {
276
+ await openFile.close();
277
+ }
278
+ }
265
279
  //# sourceMappingURL=tablets.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tablets.js","sourceRoot":"","sources":["../../src/tablets/tablets.ts"],"names":[],"mappings":";;;AAAA,qCAAqD;AACrD,kCAAkC;AAClC,kCAAkC;AAElC,+BAAmC;AACnC,qCAAuF;AAEvF,sCAAsC;AACtC,wCAAgF;AAChF,kCAA6C;AAE7C,oGAAoG;AACpG,MAAM,YAAY,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,CAAC;AAE3D;;GAEG;AACU,QAAA,mBAAmB,GAAG,iBAAiB,CAAC;AAErD;;GAEG;AACU,QAAA,8BAA8B,GAAG,oBAAoB,CAAC;AAEtD,QAAA,sBAAsB,GAAG,GAAG,CAAC;AAE1C;;GAEG;AACH,MAAa,cAAc;IAA3B;QA2BE;;;WAGG;QACI,qBAAgB,GAAG,KAAK,CAAC;QAEf,aAAQ,GAAsC,EAAE,CAAC;IAwIpE,CAAC;IAxKC;;OAEG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAgB;QAC3C,MAAM,GAAG,GAAG,IAAI,cAAc,EAAE,CAAC;QACjC,MAAM,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzB,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,QAAgB;QACnD,MAAM,GA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AC,KAAwB;QAC/C,OAAO,IAAI,iBAAiB,CAAC;YAC3B,GAAG,IAAI,CAAC,OAAO;YACf,YAAY,EAAE,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,EAAE;SAC9E,CAAC,CAAC;IACL,CAAC;IAEM,eAAe,CAAC,EAAU;QAC/B,OAAO,IAAI,iBAAiB,CAAC;YAC3B,GAAG,IAAI,CAAC,OAAO;YACf,eAAe,EAAE,EAAE;SACpB,CAAC,CAAC;IACL,CAAC;IAEM,YAAY,CAAC,QAAyB;QAC3C,OAAO,IAAI,iBAAiB,CAAC;YAC3B,GAAG,IAAI,CAAC,OAAO;YACf,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAEO,mBAAmB;QACzB,OAAO;YACL,aAAa,EAAE,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,6BAAoB,CAAC,CAAC,MAAM;YACrE,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ;SAChC,CAAC;IACJ,CAAC;CACF;AA5GD,8CA4GC","sourcesContent":["import { existsSync, promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport * as zlib from 'node:zlib';\n\nimport { snippetKey } from './key';\nimport { TabletSchema, TranslatedSnippetSchema, ORIGINAL_SNIPPET_KEY } from './schema';\nimport { TargetLanguage } from '../languages';\nimport * as logging from '../logging';\nimport { TypeScriptSnippet, SnippetLocation, completeSource } from '../snippet';\nimport { mapValues, Mutable } from '../util';\n\n// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires\nconst TOOL_VERSION = require('../../package.json').version;\n\n/**\n * The default name of the tablet file\n */\nexport const DEFAULT_TABLET_NAME = '.jsii.tabl.json';\n\n/**\n * The default name of the compressed tablet file\n */\nexport const DEFAULT_TABLET_NAME_COMPRESSED = '.jsii.tabl.json.gz';\n\nexport const CURRENT_SCHEMA_VERSION = '2';\n\n/**\n * A tablet containing various snippets in multiple languages\n */\nexport class LanguageTablet {\n /**\n * Load a tablet from a file\n */\n public static async fromFile(filename: string) {\n const ret = new LanguageTablet();\n await ret.load(filename);\n return ret;\n }\n\n /**\n * Load a tablet from a file that may not exist\n *\n * Will return an empty tablet if the file does not exist\n */\n public static async fromOptionalFile(filename: string) {\n const ret = new LanguageTablet();\n if (existsSync(filename)) {\n try {\n await ret.load(filename);\n } catch (e: any) {\n logging.warn(`${filename}: ${e}`);\n }\n }\n return ret;\n }\n\n /**\n * Whether or not the LanguageTablet was loaded with a compressed source.\n * This gets used to determine if it should be compressed when saved.\n */\n public compressedSource = false;\n\n private readonly snippets: Record<string, TranslatedSnippet> = {};\n\n /**\n * Add one or more snippets to this tablet\n */\n public addSnippets(...snippets: TranslatedSnippet[]) {\n for (const snippet of snippets) {\n const existingSnippet = this.snippets[snippet.key];\n this.snippets[snippet.key] = existingSnippet ? existingSnippet.mergeTranslations(snippet) : snippet;\n }\n }\n\n /**\n * Add one snippet to this tablet\n *\n * @deprecated use addSnippets instead\n */\n public addSnippet(snippet: TranslatedSnippet) {\n this.addSnippets(snippet);\n }\n\n public get snippetKeys() {\n return Object.keys(this.snippets);\n }\n\n /**\n * Add all snippets from the given tablets into this one\n */\n public addTablets(...tablets: LanguageTablet[]) {\n for (const tablet of tablets) {\n for (const snippet of Object.values(tablet.snippets)) {\n this.addSnippet(snippet);\n }\n }\n }\n\n /**\n * Add all snippets from the given tablet into this one\n *\n * @deprecated Use `addTablets()` instead.\n */\n public addTablet(tablet: LanguageTablet) {\n this.addTablets(tablet);\n }\n\n public tryGetSnippet(key: string): TranslatedSnippet | undefined {\n return this.snippets[key];\n }\n\n /**\n * Look up a single translation of a source snippet\n *\n * @deprecated Use `lookupTranslationBySource` instead.\n */\n public lookup(typeScriptSource: TypeScriptSnippet, language: TargetLanguage): Translation | undefined {\n return this.lookupTranslationBySource(typeScriptSource, language);\n }\n\n /**\n * Look up a single translation of a source snippet\n */\n public lookupTranslationBySource(\n typeScriptSource: TypeScriptSnippet,\n language: TargetLanguage,\n ): Translation | undefined {\n const snippet = this.snippets[snippetKey(typeScriptSource)];\n return snippet?.get(language);\n }\n\n /**\n * Lookup the translated verion of a TypeScript snippet\n */\n public lookupBySource(typeScriptSource: TypeScriptSnippet): TranslatedSnippet | undefined {\n return this.snippets[snippetKey(typeScriptSource)];\n }\n\n /**\n * Load the tablet from a file. Will automatically detect if the file is\n * compressed and decompress accordingly.\n */\n public async load(filename: string) {\n let data = await fs.readFile(filename);\n // Gzip objects start with 1f 8b 08\n if (data[0] === 0x1f && data[1] === 0x8b && data[2] === 0x08) {\n // This is a gz object, so we decompress it now...\n data = zlib.gunzipSync(data);\n this.compressedSource = true;\n }\n\n const obj: TabletSchema = JSON.parse(data.toString('utf-8'));\n\n if (!obj.toolVersion || !obj.snippets) {\n throw new Error(`File '${filename}' does not seem to be a Tablet file`);\n }\n\n if (obj.version !== CURRENT_SCHEMA_VERSION) {\n // If we're ever changing the schema version in a backwards incompatible way,\n // do upconversion here.\n throw new Error(\n `Tablet file '${filename}' has schema version '${obj.version}', this program expects '${CURRENT_SCHEMA_VERSION}'`,\n );\n }\n\n Object.assign(this.snippets, mapValues(obj.snippets, TranslatedSnippet.fromSchema));\n }\n\n public get count() {\n return Object.keys(this.snippets).length;\n }\n\n public get translatedSnippets() {\n return Object.values(this.snippets);\n }\n\n /**\n * Saves the tablet schema to a file. If the compress option is passed, then\n * the schema will be gzipped before writing to the file.\n */\n public async save(filename: string, compress = false) {\n await fs.mkdir(path.dirname(filename), { recursive: true });\n\n let schema = Buffer.from(JSON.stringify(this.toSchema(), null, 2));\n if (compress) {\n schema = zlib.gzipSync(schema);\n }\n\n await fs.writeFile(filename, schema);\n }\n\n private toSchema(): TabletSchema {\n return {\n version: CURRENT_SCHEMA_VERSION,\n toolVersion: TOOL_VERSION,\n snippets: mapValues(this.snippets, (s) => s.snippet),\n };\n }\n}\n\n/**\n * Mutable operations on an underlying TranslatedSnippetSchema\n */\nexport class TranslatedSnippet {\n public static fromSchema(schema: TranslatedSnippetSchema) {\n if (!schema.translations[ORIGINAL_SNIPPET_KEY]) {\n throw new Error(`Input schema must have '${ORIGINAL_SNIPPET_KEY}' key set in translations`);\n }\n return new TranslatedSnippet(schema);\n }\n\n public static fromTypeScript(original: TypeScriptSnippet, didCompile?: boolean) {\n return new TranslatedSnippet({\n translations: {\n [ORIGINAL_SNIPPET_KEY]: { source: original.visibleSource, version: '0' },\n },\n didCompile: didCompile,\n location: original.location,\n fullSource: completeSource(original),\n });\n }\n\n public readonly snippet: TranslatedSnippetSchema;\n\n private readonly _snippet: Mutable<TranslatedSnippetSchema>;\n private _key?: string;\n\n private constructor(snippet: TranslatedSnippetSchema) {\n this._snippet = { ...snippet };\n this.snippet = this._snippet;\n }\n\n public get key() {\n if (this._key === undefined) {\n this._key = snippetKey(this.asTypescriptSnippet());\n }\n return this._key;\n }\n\n public get originalSource(): Translation {\n return {\n source: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,\n language: 'typescript',\n didCompile: this.snippet.didCompile,\n };\n }\n\n public addTranslation(language: TargetLanguage, translation: string, version: string): Translation {\n this.snippet.translations[language] = { source: translation, version };\n\n return {\n source: translation,\n language,\n didCompile: this.snippet.didCompile,\n };\n }\n\n public fqnsReferenced() {\n return this._snippet.fqnsReferenced ?? [];\n }\n\n public addSyntaxKindCounter(syntaxKindCounter: Record<string, number>) {\n if (!this._snippet.syntaxKindCounter) {\n this._snippet.syntaxKindCounter = {};\n }\n for (const [key, value] of Object.entries(syntaxKindCounter)) {\n const x = this._snippet.syntaxKindCounter[key] ?? 0;\n this._snippet.syntaxKindCounter[key] = value + x;\n }\n }\n\n public get languages(): TargetLanguage[] {\n return Object.keys(this.snippet.translations).filter((x) => x !== ORIGINAL_SNIPPET_KEY) as TargetLanguage[];\n }\n\n public get(language: TargetLanguage): Translation | undefined {\n const t = this.snippet.translations[language];\n return t && { source: t.source, language, didCompile: this.snippet.didCompile };\n }\n\n public mergeTranslations(other: TranslatedSnippet) {\n return new TranslatedSnippet({\n ...this.snippet,\n translations: { ...this.snippet.translations, ...other.snippet.translations },\n });\n }\n\n public withFingerprint(fp: string) {\n return new TranslatedSnippet({\n ...this.snippet,\n fqnsFingerprint: fp,\n });\n }\n\n public withLocation(location: SnippetLocation) {\n return new TranslatedSnippet({\n ...this.snippet,\n location,\n });\n }\n\n public toJSON() {\n return this._snippet;\n }\n\n private asTypescriptSnippet(): TypeScriptSnippet {\n return {\n visibleSource: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,\n location: this.snippet.location,\n };\n }\n}\n\nexport interface Translation {\n source: string;\n language: string;\n didCompile?: boolean;\n}\n"]}
1
+ {"version":3,"file":"tablets.js","sourceRoot":"","sources":["../../src/tablets/tablets.ts"],"names":[],"mappings":";;;AAAA,qCAA0F;AAC1F,kCAAkC;AAElC,kCAAkC;AAElC,+BAAmC;AACnC,qCAAuF;AACvF,kCAA2C;AAE3C,sCAAsC;AACtC,wCAAgF;AAChF,kCAA6C;AAE7C,oGAAoG;AACpG,MAAM,YAAY,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,CAAC;AAE3D;;GAEG;AACU,QAAA,mBAAmB,GAAG,iBAAiB,CAAC;AAErD;;GAEG;AACU,QAAA,8BAA8B,GAAG,oBAAoB,CAAC;AAEtD,QAAA,sBAAsB,GAAG,GAAG,CAAC;AAE1C;;GAEG;AACH,MAAa,cAAc;IAA3B;QA2BE;;;WAGG;QACI,qBAAgB,GAAG,KAAK,CAAC;QAEf,aAAQ,GAAsC,EAAE,CAAC;IAyIpE,CAAC;IAzKC;;OAEG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAgB;QAC3C,MAAM,GAAG,GAAG,IAAI,cAAc,EAAE,CAAC;QACjC,MAAM,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzB,OAAO,GAAG,CAAC;I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AC;QAC5E,OAAO,SAAS,IAAI,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;KACzF;YAAS;QACR,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC;KACxB;AACH,CAAC","sourcesContent":["import { createReadStream, createWriteStream, existsSync, promises as fs } from 'node:fs';\nimport * as path from 'node:path';\nimport { Readable, Writable } from 'node:stream';\nimport * as zlib from 'node:zlib';\n\nimport { snippetKey } from './key';\nimport { TabletSchema, TranslatedSnippetSchema, ORIGINAL_SNIPPET_KEY } from './schema';\nimport { parse, stringify } from '../json';\nimport { TargetLanguage } from '../languages';\nimport * as logging from '../logging';\nimport { TypeScriptSnippet, SnippetLocation, completeSource } from '../snippet';\nimport { mapValues, Mutable } from '../util';\n\n// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires\nconst TOOL_VERSION = require('../../package.json').version;\n\n/**\n * The default name of the tablet file\n */\nexport const DEFAULT_TABLET_NAME = '.jsii.tabl.json';\n\n/**\n * The default name of the compressed tablet file\n */\nexport const DEFAULT_TABLET_NAME_COMPRESSED = '.jsii.tabl.json.gz';\n\nexport const CURRENT_SCHEMA_VERSION = '2';\n\n/**\n * A tablet containing various snippets in multiple languages\n */\nexport class LanguageTablet {\n /**\n * Load a tablet from a file\n */\n public static async fromFile(filename: string) {\n const ret = new LanguageTablet();\n await ret.load(filename);\n return ret;\n }\n\n /**\n * Load a tablet from a file that may not exist\n *\n * Will return an empty tablet if the file does not exist\n */\n public static async fromOptionalFile(filename: string) {\n const ret = new LanguageTablet();\n if (existsSync(filename)) {\n try {\n await ret.load(filename);\n } catch (e: any) {\n logging.warn(`${filename}: ${e}`);\n }\n }\n return ret;\n }\n\n /**\n * Whether or not the LanguageTablet was loaded with a compressed source.\n * This gets used to determine if it should be compressed when saved.\n */\n public compressedSource = false;\n\n private readonly snippets: Record<string, TranslatedSnippet> = {};\n\n /**\n * Add one or more snippets to this tablet\n */\n public addSnippets(...snippets: TranslatedSnippet[]) {\n for (const snippet of snippets) {\n const existingSnippet = this.snippets[snippet.key];\n this.snippets[snippet.key] = existingSnippet ? existingSnippet.mergeTranslations(snippet) : snippet;\n }\n }\n\n /**\n * Add one snippet to this tablet\n *\n * @deprecated use addSnippets instead\n */\n public addSnippet(snippet: TranslatedSnippet) {\n this.addSnippets(snippet);\n }\n\n public get snippetKeys() {\n return Object.keys(this.snippets);\n }\n\n /**\n * Add all snippets from the given tablets into this one\n */\n public addTablets(...tablets: LanguageTablet[]) {\n for (const tablet of tablets) {\n for (const snippet of Object.values(tablet.snippets)) {\n this.addSnippet(snippet);\n }\n }\n }\n\n /**\n * Add all snippets from the given tablet into this one\n *\n * @deprecated Use `addTablets()` instead.\n */\n public addTablet(tablet: LanguageTablet) {\n this.addTablets(tablet);\n }\n\n public tryGetSnippet(key: string): TranslatedSnippet | undefined {\n return this.snippets[key];\n }\n\n /**\n * Look up a single translation of a source snippet\n *\n * @deprecated Use `lookupTranslationBySource` instead.\n */\n public lookup(typeScriptSource: TypeScriptSnippet, language: TargetLanguage): Translation | undefined {\n return this.lookupTranslationBySource(typeScriptSource, language);\n }\n\n /**\n * Look up a single translation of a source snippet\n */\n public lookupTranslationBySource(\n typeScriptSource: TypeScriptSnippet,\n language: TargetLanguage,\n ): Translation | undefined {\n const snippet = this.snippets[snippetKey(typeScriptSource)];\n return snippet?.get(language);\n }\n\n /**\n * Lookup the translated verion of a TypeScript snippet\n */\n public lookupBySource(typeScriptSource: TypeScriptSnippet): TranslatedSnippet | undefined {\n return this.snippets[snippetKey(typeScriptSource)];\n }\n\n /**\n * Load the tablet from a file. Will automatically detect if the file is\n * compressed and decompress accordingly.\n */\n public async load(filename: string) {\n let readStream: Readable;\n if (await isGzipped(filename)) {\n const gunzip = zlib.createGunzip();\n createReadStream(filename).pipe(gunzip, { end: true });\n readStream = gunzip;\n this.compressedSource = true;\n } else {\n readStream = createReadStream(filename);\n }\n\n const obj: TabletSchema = await parse(readStream);\n\n if (!obj.toolVersion || !obj.snippets) {\n throw new Error(`File '${filename}' does not seem to be a Tablet file`);\n }\n\n if (obj.version !== CURRENT_SCHEMA_VERSION) {\n // If we're ever changing the schema version in a backwards incompatible way,\n // do upconversion here.\n throw new Error(\n `Tablet file '${filename}' has schema version '${obj.version}', this program expects '${CURRENT_SCHEMA_VERSION}'`,\n );\n }\n\n Object.assign(this.snippets, mapValues(obj.snippets, TranslatedSnippet.fromSchema));\n }\n\n public get count() {\n return Object.keys(this.snippets).length;\n }\n\n public get translatedSnippets() {\n return Object.values(this.snippets);\n }\n\n /**\n * Saves the tablet schema to a file. If the compress option is passed, then\n * the schema will be gzipped before writing to the file.\n */\n public async save(filename: string, compress = false) {\n await fs.mkdir(path.dirname(filename), { recursive: true });\n\n const writeStream: Writable = createWriteStream(filename, { flags: 'w' });\n const gzip = compress ? zlib.createGzip() : undefined;\n gzip?.pipe(writeStream, { end: true });\n\n return stringify(this.toSchema(), gzip ?? writeStream);\n }\n\n private toSchema(): TabletSchema {\n return {\n version: CURRENT_SCHEMA_VERSION,\n toolVersion: TOOL_VERSION,\n snippets: mapValues(this.snippets, (s) => s.snippet),\n };\n }\n}\n\n/**\n * Mutable operations on an underlying TranslatedSnippetSchema\n */\nexport class TranslatedSnippet {\n public static fromSchema(schema: TranslatedSnippetSchema) {\n if (!schema.translations[ORIGINAL_SNIPPET_KEY]) {\n throw new Error(`Input schema must have '${ORIGINAL_SNIPPET_KEY}' key set in translations`);\n }\n return new TranslatedSnippet(schema);\n }\n\n public static fromTypeScript(original: TypeScriptSnippet, didCompile?: boolean) {\n return new TranslatedSnippet({\n translations: {\n [ORIGINAL_SNIPPET_KEY]: { source: original.visibleSource, version: '0' },\n },\n didCompile: didCompile,\n location: original.location,\n fullSource: completeSource(original),\n });\n }\n\n public readonly snippet: TranslatedSnippetSchema;\n\n private readonly _snippet: Mutable<TranslatedSnippetSchema>;\n private _key?: string;\n\n private constructor(snippet: TranslatedSnippetSchema) {\n this._snippet = { ...snippet };\n this.snippet = this._snippet;\n }\n\n public get key() {\n if (this._key === undefined) {\n this._key = snippetKey(this.asTypescriptSnippet());\n }\n return this._key;\n }\n\n public get originalSource(): Translation {\n return {\n source: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,\n language: 'typescript',\n didCompile: this.snippet.didCompile,\n };\n }\n\n public addTranslation(language: TargetLanguage, translation: string, version: string): Translation {\n this.snippet.translations[language] = { source: translation, version };\n\n return {\n source: translation,\n language,\n didCompile: this.snippet.didCompile,\n };\n }\n\n public fqnsReferenced() {\n return this._snippet.fqnsReferenced ?? [];\n }\n\n public addSyntaxKindCounter(syntaxKindCounter: Record<string, number>) {\n if (!this._snippet.syntaxKindCounter) {\n this._snippet.syntaxKindCounter = {};\n }\n for (const [key, value] of Object.entries(syntaxKindCounter)) {\n const x = this._snippet.syntaxKindCounter[key] ?? 0;\n this._snippet.syntaxKindCounter[key] = value + x;\n }\n }\n\n public get languages(): TargetLanguage[] {\n return Object.keys(this.snippet.translations).filter((x) => x !== ORIGINAL_SNIPPET_KEY) as TargetLanguage[];\n }\n\n public get(language: TargetLanguage): Translation | undefined {\n const t = this.snippet.translations[language];\n return t && { source: t.source, language, didCompile: this.snippet.didCompile };\n }\n\n public mergeTranslations(other: TranslatedSnippet) {\n return new TranslatedSnippet({\n ...this.snippet,\n translations: { ...this.snippet.translations, ...other.snippet.translations },\n });\n }\n\n public withFingerprint(fp: string) {\n return new TranslatedSnippet({\n ...this.snippet,\n fqnsFingerprint: fp,\n });\n }\n\n public withLocation(location: SnippetLocation) {\n return new TranslatedSnippet({\n ...this.snippet,\n location,\n });\n }\n\n public toJSON() {\n return this._snippet;\n }\n\n private asTypescriptSnippet(): TypeScriptSnippet {\n return {\n visibleSource: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,\n location: this.snippet.location,\n };\n }\n}\n\nexport interface Translation {\n source: string;\n language: string;\n didCompile?: boolean;\n}\n\nasync function isGzipped(filename: string) {\n const openFile = await fs.open(filename, 'r');\n try {\n // Assumes that we can always read 3 bytes if there's that many in the file...\n const { bytesRead, buffer } = await openFile.read(Buffer.alloc(4), 0, 3, 0);\n return bytesRead >= 3 && buffer[0] === 0x1f && buffer[1] === 0x8b && buffer[2] === 0x08;\n } finally {\n await openFile.close();\n }\n}\n"]}
package/package.json CHANGED
@@ -38,6 +38,7 @@
38
38
  "@types/jest": "^29.5.0",
39
39
  "@types/mock-fs": "^4.13.1",
40
40
  "@types/node": "^14",
41
+ "@types/stream-json": "^1.7.3",
41
42
  "@types/tar": "^6.1.4",
42
43
  "@types/workerpool": "^6.4.0",
43
44
  "@typescript-eslint/eslint-plugin": "^5",
@@ -45,7 +46,7 @@
45
46
  "eslint": "^8",
46
47
  "eslint-config-prettier": "^8.8.0",
47
48
  "eslint-import-resolver-node": "^0.3.7",
48
- "eslint-import-resolver-typescript": "^3.5.3",
49
+ "eslint-import-resolver-typescript": "^3.5.4",
49
50
  "eslint-plugin-import": "^2.27.5",
50
51
  "eslint-plugin-prettier": "^4.2.1",
51
52
  "eslint-plugin-unicorn": "^46.0.0",
@@ -53,7 +54,7 @@
53
54
  "mock-fs": "^5.2.0",
54
55
  "npm-check-updates": "^16",
55
56
  "prettier": "^2.8.7",
56
- "projen": "^0.69.11",
57
+ "projen": "^0.70.1",
57
58
  "tar": "^6.1.13",
58
59
  "ts-jest": "^29.0.5",
59
60
  "ts-node": "^10.9.1"
@@ -65,10 +66,11 @@
65
66
  "chalk": "^4",
66
67
  "commonmark": "^0.30.0",
67
68
  "fast-glob": "^3.2.12",
68
- "jsii": "v5.0-next",
69
+ "jsii": "5.0.x",
69
70
  "semver": "^7.3.8",
70
71
  "semver-intersect": "^1.4.0",
71
- "typescript": "~5.0.2",
72
+ "stream-json": "^1.7.5",
73
+ "typescript": "~5.0.3",
72
74
  "workerpool": "^6.4.0",
73
75
  "yargs": "^17.7.1"
74
76
  },
@@ -78,7 +80,7 @@
78
80
  "main": "lib/index.js",
79
81
  "license": "Apache-2.0",
80
82
  "homepage": "https://aws.github.io/jsii",
81
- "version": "5.0.1-dev.6",
83
+ "version": "5.0.1-dev.8",
82
84
  "types": "lib/index.d.ts",
83
85
  "exports": {
84
86
  ".": "./lib/index.js",
package/releases.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "current": "4.9",
2
+ "current": "5.0",
3
3
  "maintenance": {},
4
4
  "//": "~~ Generated by projen. To modify, edit .projenrc.ts and run \"npx projen\"."
5
5
  }