@asyncapi/cli 3.4.0 → 3.4.2

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.
@@ -3154,9 +3154,10 @@
3154
3154
  }
3155
3155
  },
3156
3156
  "node_modules/@npmcli/arborist/node_modules/brace-expansion": {
3157
- "version": "2.0.1",
3158
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
3159
- "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
3157
+ "version": "2.0.2",
3158
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
3159
+ "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
3160
+ "license": "MIT",
3160
3161
  "dependencies": {
3161
3162
  "balanced-match": "^1.0.0"
3162
3163
  }
@@ -3241,9 +3242,10 @@
3241
3242
  }
3242
3243
  },
3243
3244
  "node_modules/@npmcli/map-workspaces/node_modules/brace-expansion": {
3244
- "version": "2.0.1",
3245
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
3246
- "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
3245
+ "version": "2.0.2",
3246
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
3247
+ "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
3248
+ "license": "MIT",
3247
3249
  "dependencies": {
3248
3250
  "balanced-match": "^1.0.0"
3249
3251
  }
@@ -4335,9 +4337,10 @@
4335
4337
  }
4336
4338
  },
4337
4339
  "node_modules/@typescript-eslint/typescript-estree/node_modules/brace-expansion": {
4338
- "version": "2.0.1",
4339
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
4340
- "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
4340
+ "version": "2.0.2",
4341
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
4342
+ "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
4343
+ "license": "MIT",
4341
4344
  "dependencies": {
4342
4345
  "balanced-match": "^1.0.0"
4343
4346
  }
@@ -5162,9 +5165,10 @@
5162
5165
  "integrity": "sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw=="
5163
5166
  },
5164
5167
  "node_modules/brace-expansion": {
5165
- "version": "1.1.11",
5166
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
5167
- "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
5168
+ "version": "1.1.12",
5169
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz",
5170
+ "integrity": "sha512-9T9UjW3r0UW5c1Q7GTwllptXwhvYmEzFhzMfZ9H7FQWt+uZePjZPjBP/W1ZEyZ1twGWom5/56TF4lPcqjnDHcg==",
5171
+ "license": "MIT",
5168
5172
  "dependencies": {
5169
5173
  "balanced-match": "^1.0.0",
5170
5174
  "concat-map": "0.0.1"
@@ -7533,9 +7537,10 @@
7533
7537
  }
7534
7538
  },
7535
7539
  "node_modules/glob/node_modules/brace-expansion": {
7536
- "version": "2.0.1",
7537
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
7538
- "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
7540
+ "version": "2.0.2",
7541
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
7542
+ "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
7543
+ "license": "MIT",
7539
7544
  "dependencies": {
7540
7545
  "balanced-match": "^1.0.0"
7541
7546
  }
@@ -7931,9 +7936,10 @@
7931
7936
  }
7932
7937
  },
7933
7938
  "node_modules/ignore-walk/node_modules/brace-expansion": {
7934
- "version": "2.0.1",
7935
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
7936
- "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
7939
+ "version": "2.0.2",
7940
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
7941
+ "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
7942
+ "license": "MIT",
7937
7943
  "dependencies": {
7938
7944
  "balanced-match": "^1.0.0"
7939
7945
  }
Binary file
@@ -169,6 +169,15 @@ function validationMiddleware(options) {
169
169
  if (Array.isArray(body)) {
170
170
  const results = yield validateListDocuments(body, resolveURL, validationService);
171
171
  const parsedDocuments = results.map((result) => result.document);
172
+ if (!parsedDocuments.every(doc => doc !== undefined)) {
173
+ throw new problem_exception_1.ProblemException({
174
+ type: 'invalid-asyncapi-document-parse',
175
+ title: 'Invalid AsyncAPI Document (Parse Error)',
176
+ status: 422,
177
+ detail: 'One or more provided AsyncAPI documents are invalid.',
178
+ diagnostics: results.flatMap(result => result.diagnostics || []),
179
+ });
180
+ }
172
181
  req.asyncapi.parsedDocuments = parsedDocuments;
173
182
  req.asyncapi.validationResults = results;
174
183
  }
@@ -53,6 +53,7 @@ class Template extends base_1.default {
53
53
  };
54
54
  };
55
55
  }
56
+ // eslint-disable-next-line sonarjs/cognitive-complexity
56
57
  run() {
57
58
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
58
59
  var _a, _b, _c, _d;
@@ -14,6 +14,7 @@ class Models extends base_1.default {
14
14
  super(...arguments);
15
15
  this.validationService = new validation_service_1.ValidationService();
16
16
  }
17
+ // eslint-disable-next-line sonarjs/cognitive-complexity
17
18
  run() {
18
19
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
19
20
  const { args, flags } = yield this.parse(Models);
@@ -3,7 +3,7 @@ export default class PreviewStudio extends Command {
3
3
  static readonly description = "starts a new local instance of Studio in minimal state bundling all the refs of the schema file and with no editing allowed.";
4
4
  static readonly flags: {
5
5
  help: import("@oclif/core/lib/interfaces").BooleanFlag<void>;
6
- port: import("@oclif/core/lib/interfaces").OptionFlag<number | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
6
+ port: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
7
7
  base: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
8
8
  baseDir: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
9
9
  xOrigin: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
@@ -9,10 +9,10 @@ const Preview_1 = require("../../../../domains/models/Preview");
9
9
  class PreviewStudio extends base_1.default {
10
10
  run() {
11
11
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
12
- var _a;
12
+ var _a, _b;
13
13
  const { args, flags } = yield this.parse(PreviewStudio);
14
14
  let filePath = (_a = args['spec-file']) !== null && _a !== void 0 ? _a : flags.file;
15
- const previewPort = flags.port;
15
+ const previewPort = parseInt((_b = flags.port) !== null && _b !== void 0 ? _b : '0', 10);
16
16
  if (!filePath) {
17
17
  filePath = (yield (0, SpecificationFile_1.load)()).getFilePath();
18
18
  this.log(`Loaded the specification from: ${filePath}`);
@@ -26,7 +26,7 @@ class PreviewStudio extends base_1.default {
26
26
  }
27
27
  }
28
28
  this.metricsMetadata.port = previewPort;
29
- (0, Preview_1.startPreview)(filePath, flags.base, flags.baseDir, flags.xOrigin, flags.suppressLogs, previewPort);
29
+ yield (0, Preview_1.startPreview)(filePath, flags.base, flags.baseDir, flags.xOrigin, flags.suppressLogs, previewPort);
30
30
  });
31
31
  }
32
32
  }
@@ -4,7 +4,7 @@ export default class StartStudio extends Command {
4
4
  static flags: {
5
5
  help: import("@oclif/core/lib/interfaces").BooleanFlag<void>;
6
6
  file: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
7
- port: import("@oclif/core/lib/interfaces").OptionFlag<number | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
7
+ port: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
8
8
  'no-interactive': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
9
9
  };
10
10
  static readonly args: {
@@ -10,10 +10,10 @@ const prompts_1 = require("@clack/prompts");
10
10
  class StartStudio extends base_1.default {
11
11
  run() {
12
12
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
13
- var _a;
13
+ var _a, _b;
14
14
  const { args, flags } = yield this.parse(StartStudio);
15
15
  let filePath = (_a = args['spec-file']) !== null && _a !== void 0 ? _a : flags.file;
16
- let port = flags.port;
16
+ let port = parseInt((_b = flags.port) !== null && _b !== void 0 ? _b : '0', 10);
17
17
  if (flags.file) {
18
18
  this.warn('The file flag has been removed and is being replaced by the argument spec-file. Please pass the filename directly like `asyncapi start studio asyncapi.yml`');
19
19
  }
@@ -42,7 +42,7 @@ class StartStudio extends base_1.default {
42
42
  }
43
43
  }
44
44
  this.metricsMetadata.port = port;
45
- (0, Studio_1.start)(filePath, port);
45
+ yield (0, Studio_1.start)(filePath, port);
46
46
  });
47
47
  }
48
48
  parseArgs(args, port) {
@@ -1,6 +1,6 @@
1
1
  export declare const previewFlags: () => {
2
2
  help: import("@oclif/core/lib/interfaces").BooleanFlag<void>;
3
- port: import("@oclif/core/lib/interfaces").OptionFlag<number | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
3
+ port: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
4
4
  base: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
5
5
  baseDir: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
6
6
  xOrigin: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
@@ -5,7 +5,7 @@ const core_1 = require("@oclif/core");
5
5
  const previewFlags = () => {
6
6
  return {
7
7
  help: core_1.Flags.help({ char: 'h' }),
8
- port: core_1.Flags.integer({
8
+ port: core_1.Flags.string({
9
9
  char: 'p',
10
10
  description: 'port in which to start Studio in the preview mode',
11
11
  }),
@@ -1,6 +1,6 @@
1
1
  export declare const studioFlags: () => {
2
2
  help: import("@oclif/core/lib/interfaces").BooleanFlag<void>;
3
3
  file: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
4
- port: import("@oclif/core/lib/interfaces").OptionFlag<number | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
4
+ port: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
5
5
  'no-interactive': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
6
6
  };
@@ -10,7 +10,7 @@ const studioFlags = () => {
10
10
  description: 'path to the AsyncAPI file to link with Studio',
11
11
  deprecated: true,
12
12
  }),
13
- port: core_1.Flags.integer({
13
+ port: core_1.Flags.string({
14
14
  char: 'p',
15
15
  description: 'port in which to start Studio',
16
16
  }),
@@ -1,2 +1,2 @@
1
- export declare const DEFAULT_PORT = 3210;
2
- export declare function startPreview(filePath: string, base: string | undefined, baseDirectory: string | undefined, xOrigin: boolean | undefined, suppressLogs: boolean | undefined, port?: number): void;
1
+ export declare const DEFAULT_PORT = 0;
2
+ export declare function startPreview(filePath: string, base: string | undefined, baseDirectory: string | undefined, xOrigin: boolean | undefined, suppressLogs: boolean | undefined, port?: number): Promise<void>;
@@ -20,155 +20,154 @@ const messageQueue = [];
20
20
  const filePathsToWatch = new Set();
21
21
  const defaultErrorMessage = 'error occured while bundling files. use --detailedLog or -l flag to get more details.';
22
22
  let bundleError = true;
23
- exports.DEFAULT_PORT = 3210;
23
+ exports.DEFAULT_PORT = 0;
24
24
  function isValidFilePath(filePath) {
25
25
  return (0, fs_1.existsSync)(filePath);
26
26
  }
27
27
  // eslint-disable-next-line sonarjs/cognitive-complexity
28
- function startPreview(filePath, base, baseDirectory, xOrigin, suppressLogs, port = exports.DEFAULT_PORT) {
29
- if (filePath && !isValidFilePath(filePath)) {
30
- throw new specification_file_1.SpecificationFileNotFound(filePath);
31
- }
32
- const baseDir = path_1.default.dirname(path_1.default.resolve(filePath));
33
- (0, bundler_1.default)(filePath)
34
- .then((doc) => {
35
- if (doc) {
36
- bundleError = false;
37
- }
38
- })
39
- .catch((err) => {
40
- if (suppressLogs) {
41
- console.log(defaultErrorMessage);
42
- }
43
- else {
44
- console.log(err);
28
+ function startPreview(filePath_1, base_1, baseDirectory_1, xOrigin_1, suppressLogs_1) {
29
+ return tslib_1.__awaiter(this, arguments, void 0, function* (filePath, base, baseDirectory, xOrigin, suppressLogs, port = exports.DEFAULT_PORT) {
30
+ if (filePath && !isValidFilePath(filePath)) {
31
+ throw new specification_file_1.SpecificationFileNotFound(filePath);
45
32
  }
46
- });
47
- const studioPath = path_1.default.dirname(require.resolve('@asyncapi/studio/package.json'));
48
- const app = (0, next_1.default)({
49
- dev: false,
50
- dir: studioPath,
51
- conf: {
52
- distDir: 'build',
53
- },
54
- });
55
- const handle = app.getRequestHandler();
56
- const wsServer = new ws_1.WebSocketServer({ noServer: true });
57
- wsServer.on('connection', (socket) => {
58
- sockets.push(socket);
59
- sendQueuedMessages();
60
- });
61
- wsServer.on('close', (socket) => {
62
- sockets.splice(sockets.findIndex((s) => s === socket));
63
- });
64
- app.prepare().then(() => {
65
- if (filePath && !bundleError) {
66
- messageQueue.push(JSON.stringify({
67
- type: 'preview:connected',
68
- code: 'Preview server connected',
69
- }));
33
+ const baseDir = path_1.default.dirname(path_1.default.resolve(filePath));
34
+ (0, bundler_1.default)(filePath).then((doc) => {
35
+ if (doc) {
36
+ bundleError = false;
37
+ }
38
+ }).catch((err) => {
39
+ if (suppressLogs) {
40
+ console.log(defaultErrorMessage);
41
+ }
42
+ else {
43
+ console.log(err);
44
+ }
45
+ });
46
+ const studioPath = path_1.default.dirname(require.resolve('@asyncapi/studio/package.json'));
47
+ const app = (0, next_1.default)({
48
+ dev: false,
49
+ dir: studioPath,
50
+ conf: {
51
+ distDir: 'build',
52
+ },
53
+ });
54
+ const handle = app.getRequestHandler();
55
+ const wsServer = new ws_1.WebSocketServer({ noServer: true });
56
+ wsServer.on('connection', (socket) => {
57
+ sockets.push(socket);
70
58
  sendQueuedMessages();
71
- findPathsToWatchFromSchemaRef(filePath, baseDir);
72
- filePathsToWatch.add(path_1.default.resolve(baseDir, filePath));
73
- chokidar_1.default.watch([...filePathsToWatch]).on('all', (event) => {
74
- switch (event) {
75
- case 'add':
76
- (0, bundler_1.default)([filePath], {
77
- base,
78
- baseDir: baseDirectory,
79
- xOrigin,
80
- })
81
- .then((intitalDocument) => {
82
- messageQueue.push(JSON.stringify({
83
- type: 'preview:file:added',
84
- code: path_1.default.extname(filePath) === '.yaml' ||
85
- path_1.default.extname(filePath) === '.yml'
86
- ? intitalDocument.yml()
87
- : intitalDocument.string(),
88
- }));
89
- sendQueuedMessages();
90
- })
91
- .catch((e) => {
92
- if (suppressLogs) {
93
- console.log(defaultErrorMessage);
94
- }
95
- else {
96
- console.log(e);
97
- }
98
- });
99
- break;
100
- case 'change':
101
- (0, bundler_1.default)([filePath], {
102
- base,
103
- baseDir: baseDirectory,
104
- xOrigin,
105
- })
106
- .then((modifiedDocument) => {
59
+ });
60
+ wsServer.on('close', (socket) => {
61
+ sockets.splice(sockets.findIndex(s => s === socket));
62
+ });
63
+ app.prepare().then(() => {
64
+ if (filePath && !bundleError) {
65
+ messageQueue.push(JSON.stringify({
66
+ type: 'preview:connected',
67
+ code: 'Preview server connected'
68
+ }));
69
+ sendQueuedMessages();
70
+ findPathsToWatchFromSchemaRef(filePath, baseDir);
71
+ filePathsToWatch.add(path_1.default.resolve(baseDir, filePath));
72
+ chokidar_1.default.watch([...filePathsToWatch]).on('all', (event) => {
73
+ switch (event) {
74
+ case 'add':
75
+ (0, bundler_1.default)([filePath], {
76
+ base,
77
+ baseDir: baseDirectory,
78
+ xOrigin,
79
+ }).then((intitalDocument) => {
80
+ messageQueue.push(JSON.stringify({
81
+ type: 'preview:file:added',
82
+ code: (path_1.default.extname(filePath) === '.yaml' || path_1.default.extname(filePath) === '.yml') ?
83
+ intitalDocument.yml() : intitalDocument.string()
84
+ }));
85
+ sendQueuedMessages();
86
+ }).catch((e) => {
87
+ if (suppressLogs) {
88
+ console.log(defaultErrorMessage);
89
+ }
90
+ else {
91
+ console.log(e);
92
+ }
93
+ });
94
+ break;
95
+ case 'change':
96
+ (0, bundler_1.default)([filePath], {
97
+ base,
98
+ baseDir: baseDirectory,
99
+ xOrigin,
100
+ }).then((modifiedDocument) => {
101
+ messageQueue.push(JSON.stringify({
102
+ type: 'preview:file:changed',
103
+ code: (path_1.default.extname(filePath) === '.yaml' || path_1.default.extname(filePath) === '.yml') ?
104
+ modifiedDocument.yml() : modifiedDocument.string()
105
+ }));
106
+ sendQueuedMessages();
107
+ }).catch((error) => {
108
+ if (suppressLogs) {
109
+ console.log(defaultErrorMessage);
110
+ }
111
+ else {
112
+ console.log(error);
113
+ }
114
+ });
115
+ break;
116
+ case 'unlink':
107
117
  messageQueue.push(JSON.stringify({
108
- type: 'preview:file:changed',
109
- code: path_1.default.extname(filePath) === '.yaml' ||
110
- path_1.default.extname(filePath) === '.yml'
111
- ? modifiedDocument.yml()
112
- : modifiedDocument.string(),
118
+ type: 'preview:file:deleted',
119
+ filePath,
113
120
  }));
114
121
  sendQueuedMessages();
115
- })
116
- .catch((error) => {
117
- if (suppressLogs) {
118
- console.log(defaultErrorMessage);
119
- }
120
- else {
121
- console.log(error);
122
- }
123
- });
124
- break;
125
- case 'unlink':
126
- messageQueue.push(JSON.stringify({
127
- type: 'preview:file:deleted',
128
- filePath,
129
- }));
130
- sendQueuedMessages();
131
- break;
132
- }
133
- });
134
- }
135
- const server = (0, http_1.createServer)((req, res) => handle(req, res));
136
- server.on('upgrade', (request, socket, head) => {
137
- if (request.url === '/preview-server' &&
138
- request.headers['origin'] === `http://localhost:${port}`) {
139
- console.log('🔗 WebSocket connection established for the preview.');
140
- wsServer.handleUpgrade(request, socket, head, (sock) => {
141
- wsServer.emit('connection', sock, request);
122
+ break;
123
+ }
142
124
  });
143
125
  }
144
- else {
145
- console.log('🔗 WebSocket connection not established.');
146
- socket.destroy();
147
- }
148
- });
149
- if (!bundleError) {
150
- server
151
- .listen(port, () => {
152
- const url = `http://localhost:${port}?previewServer=${port}&studio-version=${package_json_1.version}`;
153
- console.log(`🎉 Connected to Preview Server running at ${(0, picocolors_1.blueBright)(url)}.`);
154
- console.log(`🌐 Open this URL in your web browser: ${(0, picocolors_1.blueBright)(url)}`);
155
- console.log(`🛑 If needed, press ${(0, picocolors_1.redBright)('Ctrl + C')} to stop the server.`);
156
- if (filePath) {
157
- for (const entry of filePathsToWatch) {
158
- console.log(`👁️ Watching changes on file ${(0, picocolors_1.blueBright)(entry)}`);
159
- }
126
+ const server = (0, http_1.createServer)((req, res) => handle(req, res));
127
+ server.on('upgrade', (request, socket, head) => {
128
+ if (request.url === '/preview-server' && request.headers['origin'] === `http://localhost:${port}`) {
129
+ console.log('🔗 WebSocket connection established for the preview.');
130
+ wsServer.handleUpgrade(request, socket, head, (sock) => {
131
+ wsServer.emit('connection', sock, request);
132
+ });
160
133
  }
161
134
  else {
162
- console.warn('Warning: No file was provided, and we couldn\'t find a default file (like "asyncapi.yaml" or "asyncapi.json") in the current folder. Starting Studio with a blank workspace.');
163
- }
164
- if (!bundleError) {
165
- (0, open_1.default)(url);
135
+ console.log('🔗 WebSocket connection not established.');
136
+ socket.destroy();
166
137
  }
167
- })
168
- .on('error', (error) => {
169
- console.error(`Failed to start server on port ${port}:`, error.message);
170
138
  });
171
- }
139
+ if (!bundleError) {
140
+ server.listen(port, () => {
141
+ const previewServerAddr = server.address();
142
+ const currentPort = (previewServerAddr && typeof previewServerAddr === 'object' && 'port' in previewServerAddr) ? previewServerAddr.port : port;
143
+ const url = `http://localhost:${currentPort}?previewServer=${currentPort}&studio-version=${package_json_1.version}`;
144
+ console.log(`🎉 Connected to Preview Server running at ${(0, picocolors_1.blueBright)(url)}.`);
145
+ console.log(`🌐 Open this URL in your web browser: ${(0, picocolors_1.blueBright)(url)}`);
146
+ console.log(`🛑 If needed, press ${(0, picocolors_1.redBright)('Ctrl + C')} to stop the server.`);
147
+ if (filePath) {
148
+ for (const entry of filePathsToWatch) {
149
+ console.log(`👁️ Watching changes on file ${(0, picocolors_1.blueBright)(entry)}`);
150
+ }
151
+ }
152
+ else {
153
+ console.warn('Warning: No file was provided, and we couldn\'t find a default file (like "asyncapi.yaml" or "asyncapi.json") in the current folder. Starting Studio with a blank workspace.');
154
+ }
155
+ if (!bundleError) {
156
+ (0, open_1.default)(url);
157
+ }
158
+ }).on('error', (error) => {
159
+ if (error.message.includes('EADDRINUSE')) {
160
+ console.log(error);
161
+ console.error((0, picocolors_1.redBright)(`Error: Port ${port} is already in use.`));
162
+ // eslint-disable-next-line no-process-exit
163
+ process.exit(1);
164
+ }
165
+ else {
166
+ console.error(`Failed to start server on port ${port}:`, 'cause', error.cause, '\n', 'name', error.name, '\n', 'stack', error.stack, '\n', 'message', error.message);
167
+ }
168
+ });
169
+ }
170
+ });
172
171
  });
173
172
  }
174
173
  function sendQueuedMessages() {
@@ -180,12 +179,9 @@ function sendQueuedMessages() {
180
179
  }
181
180
  }
182
181
  function isLocalRefAPath(key, value) {
183
- return (typeof value === 'string' &&
184
- key === '$ref' &&
185
- (value.startsWith('.') ||
186
- value.startsWith('./') ||
187
- value.startsWith('../') ||
188
- !value.startsWith('#')));
182
+ return (typeof value === 'string' && key === '$ref' &&
183
+ (value.startsWith('.') || value.startsWith('./') ||
184
+ value.startsWith('../') || !value.startsWith('#')));
189
185
  }
190
186
  function findPathsToWatchFromSchemaRef(filePath, baseDir) {
191
187
  if (filePath && !isValidFilePath(filePath)) {
@@ -1,2 +1,2 @@
1
- export declare const DEFAULT_PORT = 3210;
2
- export declare function start(filePath: string, port?: number): void;
1
+ export declare const DEFAULT_PORT = 0;
2
+ export declare function start(filePath: string, port?: number): Promise<void>;