Skip to content

Commit b4a27bd

Browse files
initial commit for tunnel support in smartUI
1 parent b50bae1 commit b4a27bd

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

src/lib/ctx.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export default (options: Record<string, string>): Context => {
9898
delayedUpload: config.delayedUpload ?? false,
9999
useGlobalCache: config.useGlobalCache ?? false,
100100
ignoreHTTPSErrors: config.ignoreHTTPSErrors ?? false,
101-
skipBuildCreation: config.skipBuildCreation ?? false
101+
skipBuildCreation: config.skipBuildCreation ?? false,
102+
tunnel: config.tunnel ?? false,
103+
tunnelName: config.tunnelName || ''
102104
},
103105
uploadFilePath: '',
104106
webStaticConfig: [],
@@ -116,6 +118,11 @@ export default (options: Record<string, string>): Context => {
116118
url: ''
117119
},
118120
args: {},
121+
tunnelDetails: {
122+
tunnelPort: -1,
123+
tunnelHost: '',
124+
tunnelName: ''
125+
},
119126
options: {
120127
parallel: parallelObj,
121128
force: options.force ? true : false,

src/lib/httpClient.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ export default class httpClient {
169169
}, log);
170170
}
171171

172+
getTunnelDetails(tunnelName: string, log: Logger) {
173+
return this.request({
174+
url: '/tunnel',
175+
method: 'POST',
176+
data: {
177+
tunnelName: tunnelName
178+
}
179+
}, log)
180+
}
181+
182+
172183
ping(buildId: string, log: Logger) {
173184
return this.request({
174185
url: '/build/ping',

src/lib/processSnapshot.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,15 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
306306
}
307307
}
308308

309-
// process for every viewport
309+
if (ctx.config.tunnel) {
310+
if (ctx.tunnelDetails && ctx.tunnelDetails.tunnelPort != -1 && ctx.tunnelDetails.tunnelHost != '') {
311+
const tunnelAddress = `http://${ctx.tunnelDetails.tunnelHost}:${ctx.tunnelDetails.tunnelPort}`;
312+
processedOptions.tunnelAddress = tunnelAddress;
313+
ctx.log.debug(`Tunnel address added to processedOptions: ${tunnelAddress}`);
314+
}
315+
}
316+
317+
// process for every viewport
310318
let navigated: boolean = false;
311319
let previousDeviceType: string | null = null;
312320

@@ -354,7 +362,19 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
354362
ctx.log.debug(`Navigated to ${snapshot.url}`);
355363
} catch (error: any) {
356364
ctx.log.debug(`Navigation to discovery page failed; ${error}`)
357-
throw new Error(error.message)
365+
if (error && error.name && error.name === 'TimeoutError') {
366+
ctx.log.debug(`Payload uploaded tough navigation to discovery page failed; ${error}`)
367+
return {
368+
processedSnapshot: {
369+
name: snapshot.name,
370+
url: snapshot.url,
371+
dom: Buffer.from(snapshot.dom.html).toString('base64'),
372+
resources: cache,
373+
options: processedOptions
374+
},
375+
warnings: [...optionWarnings, ...snapshot.dom.warnings]
376+
};
377+
}
358378
}
359379

360380
}

src/lib/schemaValidation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ const ConfigSchema = {
173173
type: "boolean",
174174
errorMessage: "Invalid config; skipBuildCreation must be true/false"
175175
},
176+
tunnel: {
177+
type: "boolean",
178+
errorMessage: "Invalid config; tunnel must be true/false"
179+
},
180+
tunnelName: {
181+
type: "string",
182+
errorMessage: "Invalid config; tunnelName must be string"
183+
},
176184
},
177185
anyOf: [
178186
{ required: ["web"] },

src/tasks/createBuildExec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
2525
task.output = chalk.gray(`Empty PROJECT_TOKEN and PROJECT_NAME. Skipping Creation of Build!`)
2626
task.title = 'Skipped SmartUI build creation'
2727
}
28+
29+
if (ctx.config.tunnel) {
30+
let tunnelResp = await ctx.client.getTunnelDetails(ctx.config.tunnelName, ctx.log);
31+
ctx.log.debug(`Tunnel Response: ${JSON.stringify(tunnelResp)}`)
32+
if (tunnelResp && tunnelResp.data && tunnelResp.data.host && tunnelResp.data.port && tunnelResp.data.tunnel_name) {
33+
ctx.tunnelDetails = {
34+
tunnelHost: tunnelResp.data.host,
35+
tunnelPort: tunnelResp.data.port,
36+
tunnelName: tunnelResp.data.tunnel_name
37+
}
38+
ctx.log.debug(`Tunnel Details: ${JSON.stringify(ctx.tunnelDetails)}`)
39+
} else if (tunnelResp && tunnelResp.error) {
40+
if (tunnelResp.error.message) {
41+
if (tunnelResp.error.code && tunnelResp.error.code === 400) {
42+
ctx.log.info(chalk.yellow(tunnelResp.error.message))
43+
} else {
44+
ctx.log.info(chalk.yellow(`Error while fetch tunnel details; Either tunnel is not running or tunnel parameters are different`))
45+
}
46+
}
47+
}
48+
}
2849
} catch (error: any) {
2950
ctx.log.debug(error);
3051
task.output = chalk.gray(error.message);

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface Context {
3232
figma?: FigmaWebConfig;
3333
ignoreHTTPSErrors : boolean;
3434
skipBuildCreation?: boolean;
35+
tunnel?: boolean;
36+
tunnelName?: string;
3537
};
3638
uploadFilePath: string;
3739
webStaticConfig: WebStaticConfig;
@@ -40,6 +42,11 @@ export interface Context {
4042
args: {
4143
execCommand?: Array<string>
4244
}
45+
tunnelDetails: {
46+
tunnelPort: number;
47+
tunnelHost: string;
48+
tunnelName: string;
49+
}
4350
options: {
4451
parallel?: number,
4552
force?: boolean,

0 commit comments

Comments
 (0)