-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
130 lines (117 loc) · 3.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const BSON = require("bson");
const fs = require("fs");
const os = require("os");
const path = require("path");
const lodash = require("lodash");
let writeAsFile = (buffer) => {
let tmp = os.tmpDir();
let base = path.join(tmp, "insomnia-bson");
if (!fs.existsSync(base)) {
fs.mkdirSync(base);
}
let filePath = path.join(
base,
String(Math.floor(Math.random() * 3000000000))
);
fs.writeFileSync(filePath, buffer);
return filePath;
};
module.exports.templateTags = [
{
name: "buffer_from_string",
displayName: "Buffer (from a string)",
description:
"Given a string, renders that string as a buffer in the BSON body.",
args: [
{
displayName: "Value",
description: "The value to write to a buffer (as a UTF-8 string)",
type: "string",
defaultValue: "pizza",
},
],
async run(context, value) {
return "INSOMNIA-BSON::::buffer_from_string::::" + value;
},
},
{
name: "buffer_from_file",
displayName: "Buffer (from a file)",
description:
"Given a path to a file, includes the contents as a buffer in the BSON body.",
args: [
{
displayName: "File",
description: "The file to include",
type: "file",
},
],
async run(context, path) {
return "INSOMNIA-BSON::::buffer_from_file::::" + path;
},
},
];
let tagOperationDict = {
buffer_from_string: (value) => {
return Buffer.from(value);
},
buffer_from_file: (path) => {
return fs.readFileSync(path);
},
};
let replaceTags = (data) => {
if (typeof data == "string" && data.split("::::").length == 3) {
let [_, name, value] = data.split("::::");
return tagOperationDict[name](value);
} else if (data instanceof Array) {
return data.map(replaceTags);
} else if (data instanceof Object) {
return lodash.mapValues(data, replaceTags);
} else {
return data;
}
};
module.exports.requestHooks = [
(ctx) => {
let req = ctx.request;
let shouldProcess = req.getHeader("x-use-bson") || false;
if (!shouldProcess) {
return;
}
// Set the correct content-type header:
req.setHeader("content-type", "application/bson");
req.removeHeader("x-use-bson");
// Take the JSON body as it is and serialize it to bson.
let { text } = req.getBody();
let parsed = JSON.parse(text);
let replaced = replaceTags(parsed);
let serialized = BSON.serialize(replaced, { ignoreUndefined: true });
let filePath = writeAsFile(serialized);
req.setBody({ mimeType: "application/bson", fileName: filePath });
},
];
let replaceBuffers = (data) => {
if (data instanceof Buffer) {
return data.toString("base64");
} else if (data instanceof Array) {
return data.map(replaceBuffers);
} else if (data instanceof Object) {
return lodash.mapValues(data, replaceBuffers);
} else {
return data;
}
};
module.exports.responseHooks = [
(ctx) => {
let res = ctx.response;
if (res.getHeader("content-type") == "application/bson") {
let body = res.getBody();
let deserialized = BSON.deserialize(body, {
promoteBuffers: true,
promoteValues: true,
promoteLongs: true,
});
res.setBody(JSON.stringify(replaceBuffers(deserialized)));
}
},
];