Skip to content

Commit 7a72863

Browse files
chore: convert xhrs js to ts (#30935)
* Convert xhrs js to ts * empty commit * Clean up types and leftover todos * install mime in net-stubbing package * Update packages/net-stubbing/lib/server/util.ts Co-authored-by: Bill Glesias <[email protected]> * Update packages/net-stubbing/lib/server/util.ts Co-authored-by: Bill Glesias <[email protected]> * Remove internal function to clean up intention of code --------- Co-authored-by: Bill Glesias <[email protected]>
1 parent 87a845a commit 7a72863

File tree

6 files changed

+73
-77
lines changed

6 files changed

+73
-77
lines changed

packages/net-stubbing/lib/server/util.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash'
22
import Debug from 'debug'
3+
import mime from 'mime'
34
import isHtml from 'is-html'
45
import { IncomingMessage } from 'http'
56
import {
@@ -18,12 +19,39 @@ import type { CypressIncomingRequest } from '@packages/proxy'
1819
import type { InterceptedRequest } from './intercepted-request'
1920
import { caseInsensitiveGet, caseInsensitiveHas } from '../util'
2021

21-
// TODO: move this into net-stubbing once cy.route is removed
22-
import { parseContentType } from '@packages/server/lib/controllers/xhrs'
2322
import type { CyHttpMessages } from '../external-types'
2423
import { getEncoding } from 'istextorbinary'
2524

2625
const debug = Debug('cypress:net-stubbing:server:util')
26+
const htmlLikeRe = /<.+>[\s\S]+<\/.+>/
27+
28+
const isValidJSON = function (text: unknown) {
29+
if (_.isObject(text)) {
30+
return true
31+
}
32+
33+
try {
34+
const o = JSON.parse(text as string)
35+
36+
return _.isObject(o)
37+
} catch (error) {
38+
false
39+
}
40+
41+
return false
42+
}
43+
44+
export function parseContentType (response?: string) {
45+
if (isValidJSON(response)) {
46+
return mime.getType('json')
47+
}
48+
49+
if (response && htmlLikeRe.test(response)) {
50+
return mime.getType('html')
51+
}
52+
53+
return mime.getType('text')
54+
}
2755

2856
export function emit (socket: CyServer.Socket, eventName: string, data: object) {
2957
if (debug.enabled) {

packages/net-stubbing/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"is-html": "^2.0.0",
1717
"istextorbinary": "6.0.0",
1818
"lodash": "^4.17.15",
19+
"mime": "^2.6.0",
1920
"mime-types": "2.1.27",
2021
"minimatch": "^3.1.2",
2122
"throttle": "^1.0.3"

packages/net-stubbing/test/unit/util-spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1-
import { getBodyEncoding } from '../../lib/server/util'
1+
import { getBodyEncoding, parseContentType } from '../../lib/server/util'
22
import { expect } from 'chai'
33
import { join } from 'path'
44
import { readFileSync } from 'fs'
55

66
const imageBuffer = readFileSync(join(__dirname, '..', 'fixtures', 'cypress-logo.png'))
77

88
describe('net-stubbing util', () => {
9+
describe('parseContentType', () => {
10+
it('returns application/json', () => {
11+
const str = JSON.stringify({ foo: 'bar' })
12+
13+
expect(parseContentType(str)).to.eq('application/json')
14+
})
15+
16+
it('returns text/html', () => {
17+
const str = `\
18+
<html>
19+
<body>foobarbaz</body>
20+
</html>\
21+
`
22+
23+
expect(parseContentType(str)).to.eq('text/html')
24+
})
25+
26+
it('returns text/plain', () => {
27+
const str = 'foobar<p>baz'
28+
29+
expect(parseContentType(str)).to.eq('text/plain')
30+
})
31+
32+
it('returns text/plain by default', () => {
33+
expect(parseContentType()).to.eq('text/plain')
34+
})
35+
})
36+
937
context('getBodyEncoding', () => {
1038
it('returns null without data', () => {
1139
expect(getBodyEncoding(null)).to.equal(null)

packages/server/lib/controllers/xhrs.js renamed to packages/server/lib/controllers/xhrs.ts

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
1-
const _ = require('lodash')
2-
const mime = require('mime')
3-
const Promise = require('bluebird')
4-
const fixture = require('../fixture')
1+
import { parseContentType } from '@packages/net-stubbing/lib/server/util'
2+
import _ from 'lodash'
3+
import Promise from 'bluebird'
4+
import fixture from '../fixture'
55

66
const fixturesRe = /^(fx:|fixture:)/
7-
const htmlLikeRe = /<.+>[\s\S]+<\/.+>/
87

9-
const isValidJSON = function (text) {
10-
if (_.isObject(text)) {
11-
return true
12-
}
13-
14-
try {
15-
const o = JSON.parse(text)
16-
17-
return _.isObject(o)
18-
} catch (error) {
19-
false
20-
}
21-
22-
return false
23-
}
24-
25-
module.exports = {
8+
export = {
269
handle (req, res, config, next) {
27-
const get = function (val, def) {
10+
const get = function (val, def?) {
2811
return decodeURI(req.get(val) || def)
2912
}
3013

@@ -37,7 +20,7 @@ module.exports = {
3720
// figure out the stream interface and pipe these
3821
// chunks to the response
3922
return this.getResponse(response, config)
40-
.then((resp = {}) => {
23+
.then((resp: { data: any, encoding?: BufferEncoding }) => {
4124
let { data, encoding } = resp
4225

4326
// grab content-type from x-cypress-headers if present
@@ -73,7 +56,7 @@ module.exports = {
7356
.set(headers)
7457
.status(status)
7558
.end(chunk)
76-
}).catch((err) => {
59+
}).catch((err: Error) => {
7760
return res
7861
.status(400)
7962
.send({ __error: err.stack })
@@ -87,8 +70,8 @@ module.exports = {
8770
return respond()
8871
},
8972

90-
_get (resp, config) {
91-
const options = {}
73+
_get (resp: string, config: { fixturesFolder: string }): Promise<{ data: any, encoding?: string }> {
74+
const options: { encoding?: string } = {}
9275

9376
const file = resp.replace(fixturesRe, '')
9477

@@ -99,7 +82,7 @@ module.exports = {
9982
}
10083

10184
return fixture.get(config.fixturesFolder, filePath, options)
102-
.then((bytes) => {
85+
.then((bytes: any) => {
10386
return {
10487
data: bytes,
10588
encoding,
@@ -115,22 +98,6 @@ module.exports = {
11598
return Promise.resolve({ data: resp })
11699
},
117100

118-
parseContentType (response) {
119-
const ret = (type) => {
120-
return mime.getType(type) //+ "; charset=utf-8"
121-
}
122-
123-
if (isValidJSON(response)) {
124-
return ret('json')
125-
}
126-
127-
if (htmlLikeRe.test(response)) {
128-
return ret('html')
129-
}
130-
131-
return ret('text')
132-
},
133-
134101
parseHeaders (headers, response) {
135102
try {
136103
headers = JSON.parse(headers)
@@ -141,7 +108,7 @@ module.exports = {
141108
}
142109

143110
if (headers['content-type'] == null) {
144-
headers['content-type'] = this.parseContentType(response)
111+
headers['content-type'] = parseContentType(response)
145112
}
146113

147114
return headers

packages/server/test/unit/xhrs_spec.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,6 @@ require('../spec_helper')
33
const xhrs = require(`../../lib/controllers/xhrs`)
44

55
describe('lib/controllers/xhr', () => {
6-
describe('#parseContentType', () => {
7-
it('returns application/json', () => {
8-
const str = JSON.stringify({ foo: 'bar' })
9-
10-
expect(xhrs.parseContentType(str)).to.eq('application/json')
11-
})
12-
13-
it('returns text/html', () => {
14-
const str = `\
15-
<html>
16-
<body>foobarbaz</body>
17-
</html>\
18-
`
19-
20-
expect(xhrs.parseContentType(str)).to.eq('text/html')
21-
})
22-
23-
it('returns text/plain', () => {
24-
const str = 'foobar<p>baz'
25-
26-
expect(xhrs.parseContentType(str)).to.eq('text/plain')
27-
})
28-
29-
it('returns text/plain by default', () => {
30-
expect(xhrs.parseContentType()).to.eq('text/plain')
31-
})
32-
})
33-
346
describe('#parseHeaders', () => {
357
it('returns object literal on undefined', () => {
368
const obj = xhrs.parseHeaders(undefined)

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22392,7 +22392,7 @@ [email protected], mime@^1.3.4, mime@^1.4.1, mime@^1.6.0:
2239222392
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
2239322393
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
2239422394

22395-
[email protected], mime@^2.4.6, mime@^2.5.2:
22395+
[email protected], mime@^2.4.6, mime@^2.5.2, mime@^2.6.0:
2239622396
version "2.6.0"
2239722397
resolved "https://registry.yarnpkg.com/mime/-/mime-2.6.0.tgz#a2a682a95cd4d0cb1d6257e28f83da7e35800367"
2239822398
integrity sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==

0 commit comments

Comments
 (0)