ng-talkback 21.0.16 → 21.0.17
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.
- package/browser/package.json +1 -1
- package/browser-prod/package.json +1 -1
- package/lib/build-info._auto-generated_.d.ts +1 -1
- package/lib/build-info._auto-generated_.js +1 -1
- package/lib/package.json +1 -1
- package/lib-prod/build-info._auto-generated_.js +14 -0
- package/lib-prod/env/env.angular-node-app.js +130 -0
- package/lib-prod/env/env.docs-webapp.js +130 -0
- package/lib-prod/env/env.electron-app.js +130 -0
- package/lib-prod/env/env.mobile-app.js +130 -0
- package/lib-prod/env/env.npm-lib-and-cli-tool.js +130 -0
- package/lib-prod/env/env.vscode-plugin.js +130 -0
- package/lib-prod/env/index.js +6 -0
- package/lib-prod/es6.backend.js +5 -0
- package/lib-prod/features/error-rate.backend.js +25 -0
- package/lib-prod/features/latency.backend.js +30 -0
- package/lib-prod/index._auto-generated_.js +0 -0
- package/lib-prod/index.js +21 -0
- package/lib-prod/logger.backend.js +25 -0
- package/lib-prod/migrations/index.js +1 -0
- package/lib-prod/migrations/migrations_index._auto-generated_.js +0 -0
- package/lib-prod/options.backend.js +95 -0
- package/lib-prod/package.json +1 -1
- package/lib-prod/request-handler.backend.js +131 -0
- package/lib-prod/server.backend.js +82 -0
- package/lib-prod/summary.backend.js +24 -0
- package/lib-prod/talkback-factory.backend.js +19 -0
- package/lib-prod/tape-matcher.backend.js +95 -0
- package/lib-prod/tape-renderer.backend.js +93 -0
- package/lib-prod/tape-store.backend.js +94 -0
- package/lib-prod/tape.backend.js +75 -0
- package/lib-prod/types.backend.js +0 -0
- package/lib-prod/utils/content-encoding.backend.js +40 -0
- package/lib-prod/utils/headers.backend.js +16 -0
- package/lib-prod/utils/media-type.backend.js +54 -0
- package/package.json +1 -1
- package/websql/package.json +1 -1
- package/websql-prod/package.json +1 -1
- package/lib-prod/build-info._auto-generated_.ts +0 -27
- package/lib-prod/env/env.angular-node-app.ts +0 -66
- package/lib-prod/env/env.docs-webapp.ts +0 -66
- package/lib-prod/env/env.electron-app.ts +0 -66
- package/lib-prod/env/env.mobile-app.ts +0 -66
- package/lib-prod/env/env.npm-lib-and-cli-tool.ts +0 -66
- package/lib-prod/env/env.vscode-plugin.ts +0 -66
- package/lib-prod/env/index.ts +0 -6
- package/lib-prod/es6.backend.ts +0 -3
- package/lib-prod/features/error-rate.backend.ts +0 -31
- package/lib-prod/features/latency.backend.ts +0 -38
- package/lib-prod/index._auto-generated_.ts +0 -5
- package/lib-prod/index.ts +0 -27
- package/lib-prod/lib-info.md +0 -8
- package/lib-prod/logger.backend.ts +0 -26
- package/lib-prod/migrations/index.ts +0 -2
- package/lib-prod/migrations/migrations-info.md +0 -6
- package/lib-prod/migrations/migrations_index._auto-generated_.ts +0 -5
- package/lib-prod/options.backend.ts +0 -140
- package/lib-prod/request-handler.backend.ts +0 -165
- package/lib-prod/server.backend.ts +0 -100
- package/lib-prod/summary.backend.ts +0 -26
- package/lib-prod/talkback-factory.backend.ts +0 -18
- package/lib-prod/tape-matcher.backend.ts +0 -113
- package/lib-prod/tape-renderer.backend.ts +0 -118
- package/lib-prod/tape-store.backend.ts +0 -111
- package/lib-prod/tape.backend.ts +0 -93
- package/lib-prod/types.backend.ts +0 -55
- package/lib-prod/utils/content-encoding.backend.ts +0 -53
- package/lib-prod/utils/headers.backend.ts +0 -14
- package/lib-prod/utils/media-type.backend.ts +0 -62
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { Options } from './options.backend';
|
|
2
|
-
import { fse, path, json5, mkdirp } from 'tnp-core/lib-prod';
|
|
3
|
-
|
|
4
|
-
import Tape from './tape.backend';
|
|
5
|
-
import TapeMatcher from './tape-matcher.backend';
|
|
6
|
-
import TapeRenderer from './tape-renderer.backend';
|
|
7
|
-
|
|
8
|
-
export default class TapeStore {
|
|
9
|
-
private readonly path: string
|
|
10
|
-
private readonly options: Options
|
|
11
|
-
tapes: Tape[]
|
|
12
|
-
|
|
13
|
-
constructor(options: Options) {
|
|
14
|
-
this.path = path.normalize(options.path + "/")
|
|
15
|
-
this.options = options
|
|
16
|
-
this.tapes = []
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async load() {
|
|
20
|
-
mkdirp.sync(this.path)
|
|
21
|
-
|
|
22
|
-
await this.loadTapesAtDir(this.path)
|
|
23
|
-
console.log(`Loaded ${this.tapes.length} tapes`)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async loadTapesAtDir(directory: string) {
|
|
27
|
-
const items = fse.readdirSync(directory) as string[]
|
|
28
|
-
for (let i = 0; i < items.length; i++) {
|
|
29
|
-
const filename = items[i]
|
|
30
|
-
const fullPath = `${directory}${filename}`
|
|
31
|
-
const stat = fse.statSync(fullPath)
|
|
32
|
-
if (!stat.isDirectory()) {
|
|
33
|
-
try {
|
|
34
|
-
const data = fse.readFileSync(fullPath, "utf8")
|
|
35
|
-
const raw = json5.parse(data)
|
|
36
|
-
const tape = await Tape.fromStore(raw, this.options)
|
|
37
|
-
tape.path = filename
|
|
38
|
-
this.tapes.push(tape)
|
|
39
|
-
} catch (e) {
|
|
40
|
-
console.log(`Error reading tape ${fullPath}`, e.message)
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
this.loadTapesAtDir(fullPath + "/")
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
find(newTape: Tape) {
|
|
49
|
-
const foundTape = this.tapes.find(t => {
|
|
50
|
-
this.options.logger.debug(`Comparing against tape ${t.path}`)
|
|
51
|
-
return new TapeMatcher(t, this.options).sameAs(newTape)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
if (foundTape) {
|
|
55
|
-
foundTape.used = true
|
|
56
|
-
this.options.logger.log(`Found matching tape for ${newTape.req.url} at ${foundTape.path}`)
|
|
57
|
-
return foundTape
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async save(tape: Tape) {
|
|
62
|
-
tape.new = true
|
|
63
|
-
tape.used = true
|
|
64
|
-
|
|
65
|
-
const tapePath = tape.path
|
|
66
|
-
let fullFilename
|
|
67
|
-
|
|
68
|
-
if (tapePath) {
|
|
69
|
-
fullFilename = path.join(this.path, tapePath)
|
|
70
|
-
} else {
|
|
71
|
-
// If the tape doesn't have a path then it's new
|
|
72
|
-
this.tapes.push(tape)
|
|
73
|
-
|
|
74
|
-
fullFilename = this.createTapePath(tape)
|
|
75
|
-
tape.path = path.relative(this.path, fullFilename)
|
|
76
|
-
}
|
|
77
|
-
this.options.logger.log(`Saving request ${tape.req.url} at ${tape.path}`)
|
|
78
|
-
|
|
79
|
-
const tapeRenderer = new TapeRenderer(tape)
|
|
80
|
-
const toSave = await tapeRenderer.render()
|
|
81
|
-
fse.writeFileSync(fullFilename, json5.stringify(toSave, null, 4))
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
currentTapeId() {
|
|
85
|
-
return this.tapes.length
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
hasTapeBeenUsed(tapeName: string) {
|
|
89
|
-
return this.tapes.some(t => t.used && t.path === tapeName)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
resetTapeUsage() {
|
|
93
|
-
return this.tapes.forEach(t => t.used = false)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
createTapePath(tape: Tape) {
|
|
97
|
-
const currentTapeId = this.currentTapeId()
|
|
98
|
-
let tapePath = `unnamed-${currentTapeId}.json5`
|
|
99
|
-
if (this.options.tapeNameGenerator) {
|
|
100
|
-
tapePath = this.options.tapeNameGenerator(currentTapeId, tape)
|
|
101
|
-
}
|
|
102
|
-
let result = path.normalize(path.join(this.options.path, tapePath))
|
|
103
|
-
if (!result.endsWith(".json5")) {
|
|
104
|
-
result = `${result}.json5`
|
|
105
|
-
}
|
|
106
|
-
const dir = path.dirname(result)
|
|
107
|
-
mkdirp.sync(dir)
|
|
108
|
-
|
|
109
|
-
return result
|
|
110
|
-
}
|
|
111
|
-
}
|
package/lib-prod/tape.backend.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import MediaType from './utils/media-type.backend';
|
|
2
|
-
import TapeRenderer from './tape-renderer.backend';
|
|
3
|
-
import ContentEncoding from './utils/content-encoding.backend';
|
|
4
|
-
import { Options } from './options.backend';
|
|
5
|
-
import { Metadata, Req, Res } from './types.backend';
|
|
6
|
-
import* as URL from 'url';
|
|
7
|
-
import * as querystring from 'querystring';
|
|
8
|
-
// const URL = require('url')
|
|
9
|
-
// const querystring = require('querystring')
|
|
10
|
-
|
|
11
|
-
export class Tape {
|
|
12
|
-
req: Req
|
|
13
|
-
res?: Res
|
|
14
|
-
options: Options
|
|
15
|
-
queryParamsToIgnore: string[]
|
|
16
|
-
meta: Metadata
|
|
17
|
-
path?: string
|
|
18
|
-
|
|
19
|
-
new: boolean = false
|
|
20
|
-
used: boolean = false
|
|
21
|
-
|
|
22
|
-
constructor(req: Req, options: Options) {
|
|
23
|
-
this.req = { ...req }
|
|
24
|
-
this.options = options
|
|
25
|
-
|
|
26
|
-
// This needs to happen before we erase headers since we could lose information
|
|
27
|
-
this.normalizeBody()
|
|
28
|
-
|
|
29
|
-
this.cleanupHeaders()
|
|
30
|
-
|
|
31
|
-
this.queryParamsToIgnore = this.options.ignoreQueryParams
|
|
32
|
-
this.cleanupQueryParams()
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.meta = {
|
|
36
|
-
createdAt: new Date(),
|
|
37
|
-
host: this.options.host
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
static async fromStore(raw: any, options: Options) {
|
|
42
|
-
return TapeRenderer.fromStore(raw, options)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
cleanupHeaders() {
|
|
46
|
-
const newHeaders = { ...this.req.headers }
|
|
47
|
-
this.options.ignoreHeaders.forEach(h => delete newHeaders[h])
|
|
48
|
-
this.req = {
|
|
49
|
-
...this.req,
|
|
50
|
-
headers: newHeaders
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
cleanupQueryParams() {
|
|
55
|
-
if (this.queryParamsToIgnore.length === 0) {
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const url = URL.parse(this.req.url, true)
|
|
60
|
-
if (!url.search) {
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const query = { ...url.query }
|
|
65
|
-
this.queryParamsToIgnore.forEach(q => delete query[q])
|
|
66
|
-
|
|
67
|
-
const newQuery = querystring.stringify(query)
|
|
68
|
-
if (newQuery) {
|
|
69
|
-
url.query = query
|
|
70
|
-
url.search = "?" + newQuery
|
|
71
|
-
} else {
|
|
72
|
-
url.query = null
|
|
73
|
-
url.search = null
|
|
74
|
-
}
|
|
75
|
-
this.req.url = URL.format(url)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
normalizeBody() {
|
|
79
|
-
const mediaType = new MediaType(this.req)
|
|
80
|
-
const contentEncoding = new ContentEncoding(this.req)
|
|
81
|
-
if (contentEncoding.isUncompressed() && mediaType.isJSON() && this.req.body.length > 0) {
|
|
82
|
-
this.req.body = Buffer.from(JSON.stringify(JSON.parse(this.req.body.toString()), null, 2))
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async clone() {
|
|
87
|
-
const tapeRenderer = new TapeRenderer(this)
|
|
88
|
-
const raw = await tapeRenderer.render()
|
|
89
|
-
return Tape.fromStore(raw, this.options)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export default Tape;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import {FallbackMode, Options, RecordMode} from './options.backend';
|
|
2
|
-
import TalkbackServer from './server.backend';
|
|
3
|
-
import RequestHandler from './request-handler.backend';
|
|
4
|
-
|
|
5
|
-
export {}
|
|
6
|
-
|
|
7
|
-
export interface More {
|
|
8
|
-
[key: string]: any
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface MatchingContext extends More {
|
|
12
|
-
id: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface ReqRes {
|
|
16
|
-
headers: any,
|
|
17
|
-
body: Buffer
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface Req extends ReqRes {
|
|
21
|
-
url: string,
|
|
22
|
-
method: string
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type HttpRequest = Req
|
|
26
|
-
|
|
27
|
-
export interface Res extends ReqRes {
|
|
28
|
-
status: number
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type HttpResponse = Res
|
|
32
|
-
|
|
33
|
-
export interface Metadata extends More {
|
|
34
|
-
createdAt: Date,
|
|
35
|
-
host: string,
|
|
36
|
-
tag?: string,
|
|
37
|
-
errorRate?: number
|
|
38
|
-
latency?: number | number[],
|
|
39
|
-
reqUncompressed?: boolean,
|
|
40
|
-
resUncompressed?: boolean,
|
|
41
|
-
reqHumanReadable?: boolean,
|
|
42
|
-
resHumanReadable?: boolean
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type TalkbackBase = (options: Partial<Options>) => TalkbackServer
|
|
46
|
-
|
|
47
|
-
export interface Talkback extends TalkbackBase {
|
|
48
|
-
Options: {
|
|
49
|
-
Default: Options,
|
|
50
|
-
FallbackMode: typeof FallbackMode,
|
|
51
|
-
RecordMode: typeof RecordMode
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
requestHandler(options: Partial<Options>): RequestHandler
|
|
55
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {ReqRes} from '../types.backend';
|
|
2
|
-
|
|
3
|
-
const zlib = require('zlib')
|
|
4
|
-
import Headers from './headers.backend';
|
|
5
|
-
|
|
6
|
-
const ALGORITHMS = {
|
|
7
|
-
gzip: {compress: zlib.gzipSync, uncompress: zlib.gunzipSync},
|
|
8
|
-
deflate: {compress: zlib.deflateSync, uncompress: zlib.inflateSync}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
type SupportedAlgorithms = keyof typeof ALGORITHMS
|
|
12
|
-
|
|
13
|
-
export default class ContentEncoding {
|
|
14
|
-
private reqRes: ReqRes
|
|
15
|
-
|
|
16
|
-
constructor(reqRes: ReqRes) {
|
|
17
|
-
this.reqRes = reqRes
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
isUncompressed() {
|
|
21
|
-
const contentEncoding = this.contentEncoding()
|
|
22
|
-
return !contentEncoding || contentEncoding === "identity"
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
supportedAlgorithm() {
|
|
26
|
-
const contentEncoding = this.contentEncoding()
|
|
27
|
-
return Object.keys(ALGORITHMS).includes(contentEncoding)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
contentEncoding() {
|
|
31
|
-
return Headers.read(this.reqRes.headers, "content-encoding")
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async uncompressedBody(body: Buffer) {
|
|
35
|
-
const contentEncoding = this.contentEncoding()
|
|
36
|
-
|
|
37
|
-
if (!this.supportedAlgorithm()) {
|
|
38
|
-
throw new Error(`Unsupported content-encoding ${contentEncoding}`)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return ALGORITHMS[contentEncoding as SupportedAlgorithms].uncompress(body)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async compressedBody(body: string) {
|
|
45
|
-
const contentEncoding = this.contentEncoding()
|
|
46
|
-
|
|
47
|
-
if (!this.supportedAlgorithm()) {
|
|
48
|
-
throw new Error(`Unsupported content-encoding ${contentEncoding}`)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return ALGORITHMS[contentEncoding as SupportedAlgorithms].compress(body)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export default class Headers {
|
|
2
|
-
static read(headers: any, headerName: string) {
|
|
3
|
-
const value = headers[headerName]
|
|
4
|
-
if (Array.isArray(value)) {
|
|
5
|
-
return value[0]
|
|
6
|
-
} else {
|
|
7
|
-
return value
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
static write(headers: any, headerName: string, value: string, type: "req"|"res") {
|
|
12
|
-
headers[headerName] = type === "req" ? value : [value]
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { ___NS__add, ___NS__after, ___NS__ary, ___NS__assign, ___NS__assignIn, ___NS__assignInWith, ___NS__assignWith, ___NS__at, ___NS__attempt, ___NS__before, ___NS__bind, ___NS__bindAll, ___NS__bindKey, ___NS__camelCase, ___NS__capitalize, ___NS__castArray, ___NS__ceil, ___NS__chain, ___NS__chunk, ___NS__clamp, ___NS__clone, ___NS__cloneDeep, ___NS__cloneDeepWith, ___NS__cloneWith, ___NS__compact, ___NS__concat, ___NS__cond, ___NS__conforms, ___NS__conformsTo, ___NS__constant, ___NS__countBy, ___NS__create, ___NS__curry, ___NS__curryRight, ___NS__debounce, ___NS__deburr, ___NS__defaults, ___NS__defaultsDeep, ___NS__defaultTo, ___NS__defer, ___NS__delay, ___NS__difference, ___NS__differenceBy, ___NS__differenceWith, ___NS__divide, ___NS__drop, ___NS__dropRight, ___NS__dropRightWhile, ___NS__dropWhile, ___NS__each, ___NS__eachRight, ___NS__endsWith, ___NS__entries, ___NS__entriesIn, ___NS__eq, ___NS__escape, ___NS__escapeRegExp, ___NS__every, ___NS__extend, ___NS__extendWith, ___NS__fill, ___NS__filter, ___NS__find, ___NS__findIndex, ___NS__findKey, ___NS__findLast, ___NS__findLastIndex, ___NS__findLastKey, ___NS__first, ___NS__flatMap, ___NS__flatMapDeep, ___NS__flatMapDepth, ___NS__flatten, ___NS__flattenDeep, ___NS__flattenDepth, ___NS__flip, ___NS__floor, ___NS__flow, ___NS__flowRight, ___NS__forEach, ___NS__forEachRight, ___NS__forIn, ___NS__forInRight, ___NS__forOwn, ___NS__forOwnRight, ___NS__fromPairs, ___NS__functions, ___NS__functionsIn, ___NS__get, ___NS__groupBy, ___NS__gt, ___NS__gte, ___NS__has, ___NS__hasIn, ___NS__head, ___NS__identity, ___NS__includes, ___NS__indexOf, ___NS__initial, ___NS__inRange, ___NS__intersection, ___NS__intersectionBy, ___NS__intersectionWith, ___NS__invert, ___NS__invertBy, ___NS__invoke, ___NS__invokeMap, ___NS__isArguments, ___NS__isArray, ___NS__isArrayBuffer, ___NS__isArrayLike, ___NS__isArrayLikeObject, ___NS__isBoolean, ___NS__isBuffer, ___NS__isDate, ___NS__isElement, ___NS__isEmpty, ___NS__isEqual, ___NS__isEqualWith, ___NS__isError, ___NS__isFinite, ___NS__isFunction, ___NS__isInteger, ___NS__isLength, ___NS__isMap, ___NS__isMatch, ___NS__isMatchWith, ___NS__isNaN, ___NS__isNative, ___NS__isNil, ___NS__isNull, ___NS__isNumber, ___NS__isObject, ___NS__isObjectLike, ___NS__isPlainObject, ___NS__isRegExp, ___NS__isSafeInteger, ___NS__isSet, ___NS__isString, ___NS__isSymbol, ___NS__isTypedArray, ___NS__isUndefined, ___NS__isWeakMap, ___NS__isWeakSet, ___NS__iteratee, ___NS__join, ___NS__kebabCase, ___NS__keyBy, ___NS__keys, ___NS__keysIn, ___NS__last, ___NS__lastIndexOf, ___NS__lowerCase, ___NS__lowerFirst, ___NS__lt, ___NS__lte, ___NS__map, ___NS__mapKeys, ___NS__mapValues, ___NS__matches, ___NS__matchesProperty, ___NS__max, ___NS__maxBy, ___NS__mean, ___NS__meanBy, ___NS__memoize, ___NS__merge, ___NS__mergeWith, ___NS__method, ___NS__methodOf, ___NS__min, ___NS__minBy, ___NS__mixin, ___NS__multiply, ___NS__negate, ___NS__noop, ___NS__now, ___NS__nth, ___NS__nthArg, ___NS__omit, ___NS__omitBy, ___NS__once, ___NS__orderBy, ___NS__over, ___NS__overArgs, ___NS__overEvery, ___NS__overSome, ___NS__pad, ___NS__padEnd, ___NS__padStart, ___NS__parseInt, ___NS__partial, ___NS__partialRight, ___NS__partition, ___NS__pick, ___NS__pickBy, ___NS__property, ___NS__propertyOf, ___NS__pull, ___NS__pullAll, ___NS__pullAllBy, ___NS__pullAllWith, ___NS__pullAt, ___NS__random, ___NS__range, ___NS__rangeRight, ___NS__rearg, ___NS__reduce, ___NS__reduceRight, ___NS__reject, ___NS__remove, ___NS__repeat, ___NS__replace, ___NS__rest, ___NS__result, ___NS__reverse, ___NS__round, ___NS__sample, ___NS__sampleSize, ___NS__set, ___NS__setWith, ___NS__shuffle, ___NS__size, ___NS__slice, ___NS__snakeCase, ___NS__some, ___NS__sortBy, ___NS__sortedIndex, ___NS__sortedIndexBy, ___NS__sortedIndexOf, ___NS__sortedLastIndex, ___NS__sortedLastIndexBy, ___NS__sortedLastIndexOf, ___NS__sortedUniq, ___NS__sortedUniqBy, ___NS__split, ___NS__spread, ___NS__startCase, ___NS__startsWith, ___NS__stubArray, ___NS__stubFalse, ___NS__stubObject, ___NS__stubString, ___NS__stubTrue, ___NS__subtract, ___NS__sum, ___NS__sumBy, ___NS__tail, ___NS__take, ___NS__takeRight, ___NS__takeRightWhile, ___NS__takeWhile, ___NS__tap, ___NS__template, ___NS__templateSettings, ___NS__throttle, ___NS__thru, ___NS__times, ___NS__toArray, ___NS__toFinite, ___NS__toInteger, ___NS__toLength, ___NS__toLower, ___NS__toNumber, ___NS__toPairs, ___NS__toPairsIn, ___NS__toPath, ___NS__toPlainObject, ___NS__toSafeInteger, ___NS__toString, ___NS__toUpper, ___NS__transform, ___NS__trim, ___NS__trimEnd, ___NS__trimStart, ___NS__truncate, ___NS__unary, ___NS__unescape, ___NS__union, ___NS__unionBy, ___NS__unionWith, ___NS__uniq, ___NS__uniqBy, ___NS__uniqueId, ___NS__uniqWith, ___NS__unset, ___NS__unzip, ___NS__unzipWith, ___NS__update, ___NS__updateWith, ___NS__upperCase, ___NS__upperFirst, ___NS__values, ___NS__valuesIn, ___NS__without, ___NS__words, ___NS__wrap, ___NS__xor, ___NS__xorBy, ___NS__xorWith, ___NS__zip, ___NS__zipObject, ___NS__zipObjectDeep, ___NS__zipWith } from 'tnp-core/lib-prod';
|
|
2
|
-
import { ReqRes } from '../types.backend';
|
|
3
|
-
import Headers from './headers.backend';
|
|
4
|
-
|
|
5
|
-
const contentTypeParser = require('content-type')
|
|
6
|
-
|
|
7
|
-
const equals = (to: string) => (contentType: string) => to == contentType
|
|
8
|
-
|
|
9
|
-
export const jsonTypes = [
|
|
10
|
-
equals("application/json"),
|
|
11
|
-
(contentType: string) => contentType.startsWith("application/") && contentType.endsWith("+json")
|
|
12
|
-
]
|
|
13
|
-
|
|
14
|
-
const humanReadableContentTypes = [
|
|
15
|
-
equals("application/javascript"),
|
|
16
|
-
equals("text/css"),
|
|
17
|
-
equals("text/html"),
|
|
18
|
-
equals("text/javascript"),
|
|
19
|
-
equals("text/plain"),
|
|
20
|
-
...jsonTypes
|
|
21
|
-
]
|
|
22
|
-
|
|
23
|
-
export default class MediaType {
|
|
24
|
-
private htmlReqRes: ReqRes
|
|
25
|
-
|
|
26
|
-
constructor(htmlReqRes: ReqRes) {
|
|
27
|
-
this.htmlReqRes = htmlReqRes
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
isHumanReadable() {
|
|
31
|
-
const contentType = this.contentType()
|
|
32
|
-
if (!contentType) {
|
|
33
|
-
return false
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return humanReadableContentTypes.some(comparator => comparator(contentType))
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
isJSON() {
|
|
40
|
-
const contentType = this.contentType();
|
|
41
|
-
if (!contentType) {
|
|
42
|
-
return false
|
|
43
|
-
}
|
|
44
|
-
const result = jsonTypes.some(comparator => comparator('application/json'));
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
contentType() {
|
|
49
|
-
const contentTypeHeader = Headers.read(this.headers(), "content-type");
|
|
50
|
-
if (!contentTypeHeader) {
|
|
51
|
-
return null
|
|
52
|
-
}
|
|
53
|
-
const parsedContentType = contentTypeParser.parse(contentTypeHeader)
|
|
54
|
-
return parsedContentType.type as string
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
headers() {
|
|
58
|
-
return ___NS__merge(this.htmlReqRes.headers, {
|
|
59
|
-
'content-type': 'application/json'
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
}
|