ts-graphviz 1.4.1-dev.4c0d71c18 → 1.4.1-dev.5525f3630

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.
@@ -20,9 +20,29 @@ const pipeline = node_util.promisify(node_stream.pipeline);
20
20
  */
21
21
  async function toStream(dot, options) {
22
22
  const [command, args] = index_js.commandBuilder(options ?? {});
23
- const p = node_child_process.spawn(command, args, { stdio: 'pipe' });
24
- await pipeline(node_stream.Readable.from([dot]), p.stdin);
25
- return p.stdout;
23
+ return new Promise(async function toStreamInternal(resolve, reject) {
24
+ const p = node_child_process.spawn(command, args, { stdio: 'pipe' });
25
+ // error handling
26
+ p.on('error', (e) => {
27
+ reject(
28
+ new Error(`Command "${command}" failed.\nMESSAGE:${e.message}`, {
29
+ cause: e,
30
+ }),
31
+ );
32
+ });
33
+ const stderrChunks = [];
34
+ p.stderr.on('data', (chunk) => stderrChunks.push(chunk));
35
+ const dist = p.stdout.pipe(new node_stream.PassThrough());
36
+ p.on('close', async (code, signal) => {
37
+ if (code === 0) {
38
+ resolve(dist);
39
+ } else {
40
+ const message = Buffer.concat(stderrChunks).toString();
41
+ reject(new Error(`Command "${command}" failed.\nCODE: ${code}\nSIGNAL: ${signal}\nMESSAGE: ${message}`));
42
+ }
43
+ });
44
+ await pipeline(node_stream.Readable.from([dot]), p.stdin);
45
+ });
26
46
  }
27
47
 
28
48
  /**
@@ -1,4 +1,4 @@
1
- import { pipeline as pipeline$1, Readable } from 'node:stream';
1
+ import { pipeline as pipeline$1, PassThrough, Readable } from 'node:stream';
2
2
  import { spawn } from 'node:child_process';
3
3
  import { commandBuilder } from '../utils/index.js';
4
4
  import { promisify } from 'node:util';
@@ -16,9 +16,29 @@ const pipeline = promisify(pipeline$1);
16
16
  */
17
17
  async function toStream(dot, options) {
18
18
  const [command, args] = commandBuilder(options ?? {});
19
- const p = spawn(command, args, { stdio: 'pipe' });
20
- await pipeline(Readable.from([dot]), p.stdin);
21
- return p.stdout;
19
+ return new Promise(async function toStreamInternal(resolve, reject) {
20
+ const p = spawn(command, args, { stdio: 'pipe' });
21
+ // error handling
22
+ p.on('error', (e) => {
23
+ reject(
24
+ new Error(`Command "${command}" failed.\nMESSAGE:${e.message}`, {
25
+ cause: e,
26
+ }),
27
+ );
28
+ });
29
+ const stderrChunks = [];
30
+ p.stderr.on('data', (chunk) => stderrChunks.push(chunk));
31
+ const dist = p.stdout.pipe(new PassThrough());
32
+ p.on('close', async (code, signal) => {
33
+ if (code === 0) {
34
+ resolve(dist);
35
+ } else {
36
+ const message = Buffer.concat(stderrChunks).toString();
37
+ reject(new Error(`Command "${command}" failed.\nCODE: ${code}\nSIGNAL: ${signal}\nMESSAGE: ${message}`));
38
+ }
39
+ });
40
+ await pipeline(Readable.from([dot]), p.stdin);
41
+ });
22
42
  }
23
43
 
24
44
  /**
@@ -5,7 +5,7 @@ import {
5
5
  SubgraphAttributesObject,
6
6
  } from '../../common/index.js';
7
7
 
8
- type Format = 'png' | 'svg' | 'json' | 'jpg' | 'pdf' | 'xdot' | 'plain' | 'dot_json';
8
+ type Format = 'png' | 'svg' | 'json' | 'jpg' | 'pdf' | 'xdot' | 'dot' | 'plain' | 'dot_json';
9
9
  type Layout = 'dot' | 'neato' | 'fdp' | 'sfdp' | 'circo' | 'twopi' | 'nop' | 'nop2' | 'osage' | 'patchwork';
10
10
  interface NeatoOptions {
11
11
  layout: 'neato';
@@ -2,26 +2,33 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function escapeValue(value) {
6
+ if (value !== true) {
7
+ if (typeof value === 'string' && /\s/g.test(value)) {
8
+ return `="${value}"`;
9
+ } else {
10
+ return `=${value}`;
11
+ }
12
+ }
13
+ return '';
14
+ }
5
15
  function* args(options) {
6
16
  const { suppressWarnings = true, format = 'svg', attributes = {}, library = [], y = false, scale } = options;
7
17
  if (suppressWarnings) yield '-q';
8
18
  yield `-T${format}`;
9
19
  if (attributes.graph) {
10
20
  for (const [key, value] of Object.entries(attributes.graph)) {
11
- if (value === true) yield `-G${key}`;
12
- else yield `-G${key}="${value}"`;
21
+ yield `-G${key}${escapeValue(value)}`;
13
22
  }
14
23
  }
15
24
  if (attributes.node) {
16
25
  for (const [key, value] of Object.entries(attributes.node)) {
17
- if (value === true) yield `-N${key}`;
18
- else yield `-N${key}="${value}"`;
26
+ yield `-N${key}${escapeValue(value)}`;
19
27
  }
20
28
  }
21
29
  if (attributes.edge) {
22
30
  for (const [key, value] of Object.entries(attributes.edge)) {
23
- if (value === true) yield `-E${key}`;
24
- else yield `-E${key}="${value}"`;
31
+ yield `-E${key}${escapeValue(value)}`;
25
32
  }
26
33
  }
27
34
  if (typeof scale === 'number' && !Number.isNaN(scale)) yield `-s${scale}`;
@@ -1,23 +1,30 @@
1
+ function escapeValue(value) {
2
+ if (value !== true) {
3
+ if (typeof value === 'string' && /\s/g.test(value)) {
4
+ return `="${value}"`;
5
+ } else {
6
+ return `=${value}`;
7
+ }
8
+ }
9
+ return '';
10
+ }
1
11
  function* args(options) {
2
12
  const { suppressWarnings = true, format = 'svg', attributes = {}, library = [], y = false, scale } = options;
3
13
  if (suppressWarnings) yield '-q';
4
14
  yield `-T${format}`;
5
15
  if (attributes.graph) {
6
16
  for (const [key, value] of Object.entries(attributes.graph)) {
7
- if (value === true) yield `-G${key}`;
8
- else yield `-G${key}="${value}"`;
17
+ yield `-G${key}${escapeValue(value)}`;
9
18
  }
10
19
  }
11
20
  if (attributes.node) {
12
21
  for (const [key, value] of Object.entries(attributes.node)) {
13
- if (value === true) yield `-N${key}`;
14
- else yield `-N${key}="${value}"`;
22
+ yield `-N${key}${escapeValue(value)}`;
15
23
  }
16
24
  }
17
25
  if (attributes.edge) {
18
26
  for (const [key, value] of Object.entries(attributes.edge)) {
19
- if (value === true) yield `-E${key}`;
20
- else yield `-E${key}="${value}"`;
27
+ yield `-E${key}${escapeValue(value)}`;
21
28
  }
22
29
  }
23
30
  if (typeof scale === 'number' && !Number.isNaN(scale)) yield `-s${scale}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-graphviz",
3
- "version": "1.4.1-dev.4c0d71c18",
3
+ "version": "1.4.1-dev.5525f3630",
4
4
  "author": "kamiazya <yuki@kamiazya.tech>",
5
5
  "description": "Graphviz library for TypeScript.",
6
6
  "homepage": "https://ts-graphviz.github.io/ts-graphviz/",