Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/units/base-device/support/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default syrup.serial()
}
this.url = await this.handlers.start()

if (this.deviceType === DEVICE_TYPE.ANDROID) {
if (!options.connectUrlPattern && this.deviceType === DEVICE_TYPE.ANDROID) {
await db.connect() // TODO: remove db connect
const device = await dbapi.loadDeviceBySerial(this.serial)
if (device.adbPort && this.storageUrl) {
Expand Down
24 changes: 0 additions & 24 deletions lib/units/base-device/support/urlformat.js

This file was deleted.

43 changes: 43 additions & 0 deletions lib/units/base-device/support/urlformat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import syrup from '@devicefarmer/stf-syrup'
import _ from 'lodash'
import * as tr from 'transliteration'

export type UrlFormatter = (
template: string,
port: number,
model?: string | null,
name?: string | null,
) => string

interface UrlFormatOptions {
[key: string]: unknown
}

const createSlug = (model: string, name: string): string =>
(name === '' || model.toLowerCase() === name.toLowerCase()) ?
tr.slugify(model) :
tr.slugify(name + ' ' + model)

export default syrup.serial()
.define((options: UrlFormatOptions): UrlFormatter =>
(template, port, model = null, name = null) => {
const safeModel = model ?? ''
const safeName = name ?? ''
const slug = (safeName || safeModel) ? createSlug(safeModel, safeName) : 'slug'

return _.template(template, {
imports: {
slugify: tr.slugify,
},
})(
Object.assign(
{
model: safeModel,
name: safeName,
slug,
publicPort: port,
},
options,
),
)
})
2 changes: 1 addition & 1 deletion lib/units/device/plugins/screen/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export default syrup.serial()
}
FrameProducer.prototype._readBanner = function(socket) {
log.info('Reading minicap banner')
return bannerutil.read(socket).timeout(2000)
return bannerutil.read(socket, AbortSignal.timeout(2000))
}
FrameProducer.prototype._readFrames = function(socket) {
this.needsReadable = true
Expand Down
104 changes: 0 additions & 104 deletions lib/units/device/plugins/screen/util/banner.js

This file was deleted.

141 changes: 141 additions & 0 deletions lib/units/device/plugins/screen/util/banner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import type {Readable} from 'node:stream'

/**
* See https://github.com/openstf/minicap
*/
export interface Banner {
version: number
length: number
pid: number
realWidth: number
realHeight: number
virtualWidth: number
virtualHeight: number
orientation: number
quirks: {
dumb: boolean
alwaysUpright: boolean
tear: boolean
}
}

type BannerStream = Readable & {banner?: Banner; read: (n: number) => Buffer | null}

export const read = function parseBanner(out: BannerStream, signal?: AbortSignal): Promise<Banner> {
return new Promise<Banner>(function(resolve, reject) {
let readBannerBytes = 0
let needBannerBytes = 2
const banner: Banner = (out.banner = {
version: 0,
length: 0,
pid: 0,
realWidth: 0,
realHeight: 0,
virtualWidth: 0,
virtualHeight: 0,
orientation: 0,
quirks: {
dumb: false,
alwaysUpright: false,
tear: false,
},
})

const cleanup = (): void => {
out.removeListener('readable', tryRead)
if (signal) signal.removeEventListener('abort', onAbort)
}

const onAbort = (): void => {
cleanup()
reject(signal?.reason ?? new Error('Banner read aborted'))
}

const tryRead = function(): void {
let chunk: Buffer | null
while ((chunk = out.read(needBannerBytes - readBannerBytes))) {
for (let cursor = 0, len = chunk.length; cursor < len;) {
if (readBannerBytes < needBannerBytes) {
switch (readBannerBytes) {
case 0:
// version
banner.version = chunk[cursor]
break
case 1:
// length
banner.length = needBannerBytes = chunk[cursor]
break
case 2:
case 3:
case 4:
case 5:
// pid
banner.pid += (chunk[cursor] << ((readBannerBytes - 2) * 8)) >>> 0
break
case 6:
case 7:
case 8:
case 9:
// real width
banner.realWidth += (chunk[cursor] << ((readBannerBytes - 6) * 8)) >>> 0
break
case 10:
case 11:
case 12:
case 13:
// real height
banner.realHeight += (chunk[cursor] << ((readBannerBytes - 10) * 8)) >>> 0
break
case 14:
case 15:
case 16:
case 17:
// virtual width
banner.virtualWidth += (chunk[cursor] << ((readBannerBytes - 14) * 8)) >>> 0
break
case 18:
case 19:
case 20:
case 21:
// virtual height
banner.virtualHeight += (chunk[cursor] << ((readBannerBytes - 18) * 8)) >>> 0
break
case 22:
// orientation
banner.orientation += chunk[cursor] * 90
break
case 23:
// quirks
banner.quirks.dumb = (chunk[cursor] & 1) === 1
banner.quirks.alwaysUpright = (chunk[cursor] & 2) === 2
banner.quirks.tear = (chunk[cursor] & 4) === 4
break
}
cursor += 1
readBannerBytes += 1
if (readBannerBytes === needBannerBytes) {
cleanup()
resolve(banner)
return
}
} else {
cleanup()
reject(new Error('Supposedly impossible error parsing banner'))
return
}
}
}
}

if (signal) {
if (signal.aborted) {
reject(signal.reason ?? new Error('Banner read aborted'))
return
}
signal.addEventListener('abort', onAbort, {once: true})
}

tryRead()
out.on('readable', tryRead)
})
}
2 changes: 1 addition & 1 deletion lib/units/device/plugins/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default syrup.serial()

copy = () => this.getClipboard()

getDisplay = (id: string) =>
getDisplay = (id: number) =>
runServiceCommand(apk.wire.MessageType.GET_DISPLAY, new apk.wire.GetDisplayRequest(id))
.then((data) => {
const response = apk.wire.GetDisplayResponse.decode(data)
Expand Down
Loading
Loading