@ms-cloudpack/json-utilities 0.1.4 → 0.1.5

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.
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.39.4"
8
+ "packageVersion": "7.47.0"
9
9
  }
10
10
  ]
11
11
  }
@@ -8,6 +8,9 @@ export interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> {
8
8
  }
9
9
  /**
10
10
  * Writes json to a path. Ensures the path exists.
11
+ *
12
+ * If the JSON is very large (causing a RangeError "Invalid string length"), it will fall back to
13
+ * stringifying and writing the object using a stream, provided that `update` is false/unset.
11
14
  */
12
15
  export declare function writeJson(filePath: string, data: unknown, options?: WriteJsonOptions): Promise<void>;
13
16
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"writeJson.d.ts","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC;IACrE;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,wBAAsB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAM9G;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,IAAI,CAMnG"}
1
+ {"version":3,"file":"writeJson.d.ts","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC;IACrE;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAmB9G;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,IAAI,CAMnG"}
package/lib/writeJson.js CHANGED
@@ -1,16 +1,33 @@
1
1
  import fs from 'fs';
2
- import fsPromises from 'fs/promises';
3
- import path from 'path';
4
2
  import fsExtra from 'fs-extra';
3
+ import fsPromises from 'fs/promises';
5
4
  import jju from 'jju';
5
+ import { JsonStreamStringify } from 'json-stream-stringify';
6
+ import path from 'path';
7
+ import { pipeline } from 'stream/promises';
6
8
  /**
7
9
  * Writes json to a path. Ensures the path exists.
10
+ *
11
+ * If the JSON is very large (causing a RangeError "Invalid string length"), it will fall back to
12
+ * stringifying and writing the object using a stream, provided that `update` is false/unset.
8
13
  */
9
14
  export async function writeJson(filePath, data, options = {}) {
10
15
  const folderPath = path.dirname(filePath);
11
16
  await fsExtra.ensureDir(folderPath);
12
- const contents = stringify(filePath, data, options);
13
- await fsPromises.writeFile(filePath, contents, 'utf8');
17
+ let contents;
18
+ try {
19
+ contents = stringify(filePath, data, options);
20
+ await fsPromises.writeFile(filePath, contents, 'utf8');
21
+ }
22
+ catch (err) {
23
+ if (!contents && err instanceof RangeError && !options.update) {
24
+ // It's a giant object, so try a stream
25
+ await pipeline(new JsonStreamStringify(data, undefined, 2, true), fs.createWriteStream(filePath, { encoding: 'utf8' }));
26
+ }
27
+ else {
28
+ throw err;
29
+ }
30
+ }
14
31
  }
15
32
  /**
16
33
  * Synchronously writes json to a path. Ensures the path exists.
@@ -24,7 +41,6 @@ export function writeJsonSync(filePath, data, options = {}) {
24
41
  function stringify(filePath, data, options) {
25
42
  const { update, mode } = options;
26
43
  if (update && fs.existsSync(filePath)) {
27
- // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem
28
44
  const originalContent = fs.readFileSync(filePath, 'utf8');
29
45
  return jju.update(originalContent, data, {
30
46
  mode: mode === 'permissive' ? 'json5' : 'json',
@@ -1 +1 @@
1
- {"version":3,"file":"writeJson.js","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,GAAG,MAAM,KAAK,CAAC;AAWtB;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC7F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,MAAM,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAEpC,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,MAAM,UAAU,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC3F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;IAElC,MAAM,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,EAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,OAAyB;IAC3E,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;IAEjC,IAAI,MAAM,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACtC,qEAAqE;QACrE,MAAM,eAAe,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC1D,OAAO,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,IAAI,EAAE;YACvC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM;YAC9C,MAAM,EAAE,CAAC;YACT,KAAK,EAAE,GAAG;YACV,UAAU,EAAE,IAAI;YAChB,iBAAiB,EAAE,IAAI;SACxB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC;AAC9C,CAAC","sourcesContent":["import fs from 'fs';\nimport fsPromises from 'fs/promises';\nimport path from 'path';\nimport fsExtra from 'fs-extra';\nimport jju from 'jju';\nimport type { ReadJsonOptions } from './readJson.js';\n\nexport interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> {\n /**\n * If true, read the file (if it exists) and preserve as much of the original formatting as possible.\n * (For files with comments, `mode: 'permissive'` must also be set.)\n */\n update?: boolean;\n}\n\n/**\n * Writes json to a path. Ensures the path exists.\n */\nexport async function writeJson(filePath: string, data: unknown, options: WriteJsonOptions = {}): Promise<void> {\n const folderPath = path.dirname(filePath);\n await fsExtra.ensureDir(folderPath);\n\n const contents = stringify(filePath, data, options);\n await fsPromises.writeFile(filePath, contents, 'utf8');\n}\n\n/**\n * Synchronously writes json to a path. Ensures the path exists.\n */\nexport function writeJsonSync(filePath: string, data: unknown, options: WriteJsonOptions = {}): void {\n const folderPath = path.dirname(filePath);\n fsExtra.ensureDirSync(folderPath);\n\n const contents = stringify(filePath, data, options);\n fs.writeFileSync(filePath, contents, 'utf8');\n}\n\nfunction stringify(filePath: string, data: unknown, options: WriteJsonOptions): string {\n const { update, mode } = options;\n\n if (update && fs.existsSync(filePath)) {\n // eslint-disable-next-line @ms-cloudpack/internal/no-sync-filesystem\n const originalContent = fs.readFileSync(filePath, 'utf8');\n return jju.update(originalContent, data, {\n mode: mode === 'permissive' ? 'json5' : 'json',\n indent: 2,\n quote: '\"',\n quote_keys: true,\n no_trailing_comma: true,\n });\n }\n\n return JSON.stringify(data, null, 2) + '\\n';\n}\n"]}
1
+ {"version":3,"file":"writeJson.js","sourceRoot":"","sources":["../src/writeJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAW3C;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,QAAgB,EAAE,IAAa,EAAE,UAA4B,EAAE;IAC7F,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C,MAAM,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IAEpC,IAAI,QAA4B,CAAC;IACjC,IAAI,CAAC;QACH,QAAQ,GAAG,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAC9C,MAAM,UAAU,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IACzD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC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ChB,iBAAiB,EAAE,IAAI;SACxB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC;AAC9C,CAAC","sourcesContent":["import fs from 'fs';\nimport fsExtra from 'fs-extra';\nimport fsPromises from 'fs/promises';\nimport jju from 'jju';\nimport { JsonStreamStringify } from 'json-stream-stringify';\nimport path from 'path';\nimport { pipeline } from 'stream/promises';\nimport type { ReadJsonOptions } from './readJson.js';\n\nexport interface WriteJsonOptions extends Pick<ReadJsonOptions, 'mode'> {\n /**\n * If true, read the file (if it exists) and preserve as much of the original formatting as possible.\n * (For files with comments, `mode: 'permissive'` must also be set.)\n */\n update?: boolean;\n}\n\n/**\n * Writes json to a path. Ensures the path exists.\n *\n * If the JSON is very large (causing a RangeError \"Invalid string length\"), it will fall back to\n * stringifying and writing the object using a stream, provided that `update` is false/unset.\n */\nexport async function writeJson(filePath: string, data: unknown, options: WriteJsonOptions = {}): Promise<void> {\n const folderPath = path.dirname(filePath);\n await fsExtra.ensureDir(folderPath);\n\n let contents: string | undefined;\n try {\n contents = stringify(filePath, data, options);\n await fsPromises.writeFile(filePath, contents, 'utf8');\n } catch (err) {\n if (!contents && err instanceof RangeError && !options.update) {\n // It's a giant object, so try a stream\n await pipeline(\n new JsonStreamStringify(data, undefined, 2, true),\n fs.createWriteStream(filePath, { encoding: 'utf8' }),\n );\n } else {\n throw err;\n }\n }\n}\n\n/**\n * Synchronously writes json to a path. Ensures the path exists.\n */\nexport function writeJsonSync(filePath: string, data: unknown, options: WriteJsonOptions = {}): void {\n const folderPath = path.dirname(filePath);\n fsExtra.ensureDirSync(folderPath);\n\n const contents = stringify(filePath, data, options);\n fs.writeFileSync(filePath, contents, 'utf8');\n}\n\nfunction stringify(filePath: string, data: unknown, options: WriteJsonOptions): string {\n const { update, mode } = options;\n\n if (update && fs.existsSync(filePath)) {\n const originalContent = fs.readFileSync(filePath, 'utf8');\n return jju.update(originalContent, data, {\n mode: mode === 'permissive' ? 'json5' : 'json',\n indent: 2,\n quote: '\"',\n quote_keys: true,\n no_trailing_comma: true,\n });\n }\n\n return JSON.stringify(data, null, 2) + '\\n';\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/json-utilities",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Helpers for reading/writing json files.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -15,11 +15,12 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "fs-extra": "^11.2.0",
18
- "jju": "^1.4.0"
18
+ "jju": "^1.4.0",
19
+ "json-stream-stringify": "^3.1.4"
19
20
  },
20
21
  "devDependencies": {
21
- "@ms-cloudpack/eslint-plugin-internal": "*",
22
- "@ms-cloudpack/scripts": "*",
22
+ "@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
23
+ "@ms-cloudpack/scripts": "^0.0.1",
23
24
  "@types/jju": "^1.4.4"
24
25
  },
25
26
  "scripts": {