Skip to content

Commit 4ad9c28

Browse files
committed
chore: convert random to TypeScript
1 parent f975654 commit 4ad9c28

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

packages/server/lib/cloud/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const url = require('url')
99
const { shell } = require('electron')
1010

1111
const machineId = require('./machine_id')
12-
const random = require('../util/random')
12+
import { id as randomId } from '../util/random'
1313
const user = require('./user')
1414

1515
let app
@@ -29,7 +29,7 @@ const buildFullLoginUrl = (baseLoginUrl, server, utmSource, utmMedium, utmConten
2929
const { port } = server.address()
3030

3131
if (!authState) {
32-
authState = random.id(32)
32+
authState = randomId(32)
3333
}
3434

3535
const authUrl = url.parse(baseLoginUrl)

packages/server/lib/file_server.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from 'path'
77
import send from 'send'
88
import { httpUtils } from '@packages/network'
99
import { allowDestroy } from './util/server_destroy'
10-
import random from './util/random'
10+
import { id as randomId } from './util/random'
1111
import networkFailures from './util/network_failures'
1212
import type { AddressInfo } from 'net'
1313

@@ -59,7 +59,7 @@ const onRequest = function (req: http.IncomingMessage, res: http.ServerResponse,
5959

6060
export const create = (fileServerFolder: string): Promise<FileServer> => {
6161
return new Promise(((resolve) => {
62-
const token = random.id(64)
62+
const token = randomId(64)
6363

6464
const srv = http.createServer(httpUtils.lenientOptions, (req: http.IncomingMessage, res: http.ServerResponse) => {
6565
return onRequest(req, res, token, fileServerFolder)
@@ -68,23 +68,21 @@ export const create = (fileServerFolder: string): Promise<FileServer> => {
6868
allowDestroy(srv)
6969

7070
return srv.listen(0, '127.0.0.1', () => {
71-
return resolve({
71+
const server: FileServer = {
7272
token,
73-
7473
port () {
7574
return (srv.address() as AddressInfo).port
7675
},
77-
7876
address () {
79-
// @ts-expect-error - port is defined on the object context
8077
return `http://localhost:${this.port()}`
8178
},
82-
8379
close () {
8480
// @ts-expect-error - destroyAsync is defined when allowDestroy is called
8581
return srv.destroyAsync()
8682
},
87-
})
83+
}
84+
85+
return resolve(server)
8886
})
8987
}))
9088
}

packages/server/lib/modes/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { setCtx } from '@packages/data-context'
22
import _ from 'lodash'
33

44
import { makeDataContext } from '../makeDataContext'
5-
import random from '../util/random'
5+
import { id as randomId } from '../util/random'
66
import { telemetry } from '@packages/telemetry'
77

88
export = (mode, options) => {
@@ -14,7 +14,7 @@ export = (mode, options) => {
1414

1515
if (mode === 'run') {
1616
_.defaults(options, {
17-
socketId: random.id(10),
17+
socketId: randomId(10),
1818
isTextTerminal: true,
1919
browser: 'electron',
2020
quiet: false,

packages/server/lib/modes/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { fs, getPath } from '../util/fs'
1717
import runEvents from '../plugins/run_events'
1818
import env from '../util/env'
1919
import trash from '../util/trash'
20-
import random from '../util/random'
20+
import { id as randomId } from '../util/random'
2121
import system from '../util/system'
2222
import chromePolicyCheck from '../util/chrome_policy_check'
2323
import type { SpecWithRelativeRoot, SpecFile, TestingType, OpenProjectLaunchOpts, FoundBrowser, BrowserVideoController, VideoRecording, ProcessOptions, ProtocolManagerShape, AutomationCommands } from '@packages/types'
@@ -764,7 +764,7 @@ async function waitForTestsToFinishRunning (options: { project: Project, screens
764764

765765
function screenshotMetadata (data: any, resp: any) {
766766
return {
767-
screenshotId: random.id(),
767+
screenshotId: randomId(),
768768
name: data.name || null,
769769
testId: data.testId,
770770
testAttemptIndex: data.testAttemptIndex,
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
const random = require('randomstring')
1+
import random from 'randomstring'
22

33
// return a random id
4-
const id = (length = 5) => {
4+
export const id = (length = 5): string => {
55
return random.generate({
66
length,
77
capitalization: 'lowercase',
88
})
99
}
10-
11-
module.exports = {
12-
id,
13-
}

packages/server/test/unit/util/random_spec.js renamed to packages/server/test/unit/util/random_spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
require('../../spec_helper')
2-
3-
const randomstring = require('randomstring')
4-
const random = require(`../../../lib/util/random`)
1+
import randomstring from 'randomstring'
2+
import { id as randomId } from '../../../lib/util/random'
53

64
context('.id', () => {
75
it('returns random.generate string with length 5 by default', () => {
86
sinon.spy(randomstring, 'generate')
97

10-
const id = random.id()
8+
const id = randomId()
119

1210
expect(id.length).to.eq(5)
1311

@@ -20,7 +18,7 @@ context('.id', () => {
2018
it('passes the length parameter if supplied', () => {
2119
sinon.spy(randomstring, 'generate')
2220

23-
const id = random.id(32)
21+
const id = randomId(32)
2422

2523
expect(id.length).to.eq(32)
2624

0 commit comments

Comments
 (0)