1
+ import fs from 'node:fs' ;
2
+ import os from 'node:os' ;
3
+
4
+ import iconv from 'iconv-lite' ;
5
+
1
6
import { database as db } from '../common/database' ;
2
7
import * as models from '../models' ;
3
8
import type { Request as DBRequest } from '../models/request' ;
4
9
import type { RequestGroup } from '../models/request-group' ;
10
+ import type { Response } from '../models/response' ;
5
11
import type { Workspace } from '../models/workspace' ;
6
12
import { fetchRequestData , sendCurlAndWriteTimeline , tryToInterpolateRequest } from '../network/network' ;
7
-
8
13
export const resolveDbByKey = async ( request : Request ) => {
9
14
const url = new URL ( request . url ) ;
10
- let result ;
11
15
const body = await request . json ( ) ;
12
- if ( url . host === 'request.getById' . toLowerCase ( ) ) {
13
- result = await models . request . getById ( body . id ) ;
14
- }
15
- if ( url . host === 'request.getAncestors' . toLowerCase ( ) ) {
16
- result = await db . withAncestors < DBRequest | RequestGroup | Workspace > ( body . request , body . types ) ;
17
- }
18
- if ( url . host === 'workspace.getById' . toLowerCase ( ) ) {
19
- result = await models . workspace . getById ( body . id ) ;
20
- }
21
- if ( url . host === 'oAuth2Token.getByRequestId' . toLowerCase ( ) ) {
22
- result = await models . oAuth2Token . getByParentId ( body . parentId ) ;
23
- }
24
- if ( url . host === 'cookieJar.getOrCreateForParentId' . toLowerCase ( ) ) {
25
- result = await models . cookieJar . getOrCreateForParentId ( body . parentId ) ;
26
- }
27
- if ( url . host === 'response.getLatestForRequestId' . toLowerCase ( ) ) {
28
- result = await models . response . getLatestForRequest ( body . requestId , body . environmentId ) ;
29
- }
30
- if ( url . host === 'response.getBodyBuffer' . toLowerCase ( ) ) {
31
- result = await models . response . getBodyBuffer ( body . response , body . readFailureValue ) ;
32
- }
33
- if ( url . host === 'pluginData.hasItem' . toLowerCase ( ) ) {
16
+ // url get normalized to lowercase, so we need to normalize the keys to lower case as well
17
+ const withLowercasedKeys = Object . fromEntries (
18
+ Object . entries ( pluginToMainAPI ) . map ( ( [ key , value ] ) => [ key . toLowerCase ( ) , value ] ) ,
19
+ ) ;
20
+ const result = await withLowercasedKeys [ url . host . toLowerCase ( ) ] ( body ) ;
21
+ return new Response ( JSON . stringify ( result ) ) ;
22
+ } ;
23
+
24
+ // These are exposed to the templating worker and can be used by plugins from context.util
25
+ const pluginToMainAPI = {
26
+ 'readFile' : async ( body : { path : string ; encoding : 'utf8' } ) => {
27
+ return await fs . promises . readFile ( body . path , { encoding : body . encoding || 'utf8' } ) ;
28
+ } ,
29
+ 'nodeOS' : async ( ) => {
30
+ return {
31
+ arch : os . arch ( ) ,
32
+ platform : os . platform ( ) ,
33
+ release : os . release ( ) ,
34
+ cpus : os . cpus ( ) ,
35
+ hostname : os . hostname ( ) ,
36
+ freemem : os . freemem ( ) ,
37
+ userInfo : os . userInfo ( ) ,
38
+ } ;
39
+ } ,
40
+ 'decode' : async ( body : { buffer : Buffer ; encoding : 'utf8' } ) => {
41
+ return iconv . decode ( body . buffer , body . encoding || 'utf8' ) ;
42
+ } ,
43
+ 'request.getById' : async ( body : { id : string } ) => {
44
+ return await models . request . getById ( body . id ) ;
45
+ } ,
46
+ 'request.getAncestors' : async ( body : { request : DBRequest | RequestGroup | Workspace ; types : string [ ] } ) => {
47
+ return await db . withAncestors < DBRequest | RequestGroup | Workspace > ( body . request , body . types ) ;
48
+ } ,
49
+ 'workspace.getById' : async ( body : { id : string } ) => {
50
+ return await models . workspace . getById ( body . id ) ;
51
+ } ,
52
+ 'oAuth2Token.getByRequestId' : async ( body : { parentId : string } ) => {
53
+ return await models . oAuth2Token . getByParentId ( body . parentId ) ;
54
+ } ,
55
+ 'cookieJar.getOrCreateForParentId' : async ( body : { parentId : string } ) => {
56
+ return await models . cookieJar . getOrCreateForParentId ( body . parentId ) ;
57
+ } ,
58
+ 'response.getLatestForRequestId' : async ( body : { requestId : string ; environmentId : string } ) => {
59
+ return await models . response . getLatestForRequest ( body . requestId , body . environmentId ) ;
60
+ } ,
61
+ 'response.getBodyBuffer' : async ( body : { response : Response ; readFailureValue : string } ) => {
62
+ return await models . response . getBodyBuffer ( body . response , body . readFailureValue ) ;
63
+ } ,
64
+ 'pluginData.hasItem' : async ( body : { pluginName : string ; key : string } ) => {
34
65
const doc = await models . pluginData . getByKey ( body . pluginName , body . key ) ;
35
- result = doc !== null ;
36
- }
37
- if ( url . host === 'pluginData.setItem' . toLowerCase ( ) ) {
38
- result = models . pluginData . upsertByKey ( body . pluginName , body . key , String ( body . value ) ) ;
39
- }
40
- if ( url . host === 'pluginData.getItem' . toLowerCase ( ) ) {
66
+ return doc !== null ;
67
+ } ,
68
+ 'pluginData.setItem' : async ( body : { pluginName : string ; key : string ; value : string } ) => {
69
+ return models . pluginData . upsertByKey ( body . pluginName , body . key , String ( body . value ) ) ;
70
+ } ,
71
+ 'pluginData.getItem' : async ( body : { pluginName : string ; key : string } ) => {
41
72
const doc = await models . pluginData . getByKey ( body . pluginName , body . key ) ;
42
- result = doc ? doc . value : null ;
43
- }
44
- if ( url . host === 'pluginData.removeItem' . toLowerCase ( ) ) {
45
- result = models . pluginData . removeByKey ( body . pluginName , body . key ) ;
46
- }
47
- if ( url . host === 'pluginData.clear' . toLowerCase ( ) ) {
48
- result = models . pluginData . removeAll ( body . pluginName ) ;
49
- }
50
- if ( url . host === 'pluginData.all' . toLowerCase ( ) ) {
73
+ return doc ? doc . value : null ;
74
+ } ,
75
+ 'pluginData.removeItem' : async ( body : { pluginName : string ; key : string } ) => {
76
+ return models . pluginData . removeByKey ( body . pluginName , body . key ) ;
77
+ } ,
78
+ 'pluginData.clear' : async ( body : { pluginName : string } ) => {
79
+ return models . pluginData . removeAll ( body . pluginName ) ;
80
+ } ,
81
+ 'pluginData.all' : async ( body : { pluginName : string } ) => {
51
82
const docs = ( await models . pluginData . all ( body . pluginName ) ) || [ ] ;
52
- result = docs . map ( d => ( {
83
+ return docs . map ( d => ( {
53
84
value : d . value ,
54
85
key : d . key ,
55
86
} ) ) ;
56
- }
57
- if ( url . host === 'network.sendRequest' . toLowerCase ( ) ) {
87
+ } ,
88
+ 'network.sendRequest' : async ( body : { request : DBRequest ; extraInfo ?: { requestChain : string [ ] } } ) => {
58
89
const { request, environment, settings, clientCertificates, caCert, timelinePath, responseId } =
59
90
await fetchRequestData ( body . request . _id ) ;
60
91
@@ -72,8 +103,6 @@ export const resolveDbByKey = async (request: Request) => {
72
103
timelinePath ,
73
104
responseId ,
74
105
) ;
75
- result = await models . response . create ( { ...response , bodyCompression : null } , settings . maxHistoryResponses ) ;
76
- }
77
-
78
- return new Response ( JSON . stringify ( result ) ) ;
106
+ return await models . response . create ( { ...response , bodyCompression : null } , settings . maxHistoryResponses ) ;
107
+ } ,
79
108
} ;
0 commit comments