Skip to content

Commit 96104e1

Browse files
committed
prettier
1 parent 9227a18 commit 96104e1

2 files changed

Lines changed: 24 additions & 34 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ $ curl -H "Content-Type: application/json" -d '{"sql":"select DATETIME(?) AS UTC
121121

122122
## BLOB Handling
123123

124-
Blobs via http POST/GET can be treated as byte arrays or base64-encoded text. This is handled via the blobtype variable in the HTTP GET request, or a blobtype object member in the request body for an HTTP POST. Allowable values for blobtype are "base64" and "array". If blobtype is omitted, "base64" is the default.
124+
Blobs via http POST/GET can be treated as byte arrays or base64-encoded text. This is handled via the blobtype variable in the HTTP GET request, or a blobtype object member in the request body for an HTTP POST. Allowable values for blobtype are "base64" and "array". If blobtype is omitted, "base64" is the default.
125125

126126
This also affects how your parameterized SQLite statements are sent to the server. BLOB query parameters must be structured as {"data": value}, whereas other parameter types (e.g. text, numberic) are treated as primitives in the params array (see exables below).
127127

@@ -140,7 +140,7 @@ GET http://localhost:2048?sql=select BLOB_FIELD from BLOB_TABLE&blobtype=array
140140
###############################################################################
141141
142142
POST (application/json) http://localhost:2048
143-
BODY:
143+
BODY:
144144
{
145145
"blobtype": "base64",
146146
"sql": "insert into BLOB_TABLE(KEY,BLOB_FIELD) values (?,?)",
@@ -151,7 +151,7 @@ BODY:
151151
###############################################################################
152152
153153
POST (application/json) http://localhost:2048
154-
BODY:
154+
BODY:
155155
{
156156
"blobtype": "array",
157157
"sql": "insert into BLOB_TABLE(KEY,BLOB_FIELD) values (?,?)",

main.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ console.log("db", "=", flags.get("db"));
1212
console.log("readonly", "=", flags.get("readonly"));
1313
console.log("port", "=", flags.get("port"));
1414
console.log("cors", "=", flags.get("cors").join(", ") || "false");
15-
console.log("requestlimit", "=", flags.get("requestlimit"))
15+
console.log("requestlimit", "=", flags.get("requestlimit"));
1616

1717
const Database = require("better-sqlite3");
1818

@@ -21,7 +21,9 @@ const bodyParser = require("body-parser");
2121

2222
const app = express();
2323
app.use(require("compression")());
24-
app.use(bodyParser.urlencoded({ extended: false, limit: flags.get("requestlimit") }));
24+
app.use(
25+
bodyParser.urlencoded({ extended: false, limit: flags.get("requestlimit") })
26+
);
2527
app.use(bodyParser.json({ limit: "1mb" }));
2628
app.use(function (req, res, next) {
2729
req.connection.setTimeout(2 * 60 * 1000); // 2 minutes
@@ -47,23 +49,19 @@ if (flags.get("cors").length > 0) {
4749

4850
function getSqlExecutor(httpRequestFieldName) {
4951
return function (req, res) {
50-
5152
let blobtype = req[httpRequestFieldName].blobtype;
52-
if (blobtype == undefined || blobtype == null)
53-
{
53+
if (blobtype == undefined || blobtype == null) {
5454
blobtype = "base64"; //default http blob handling to base64 if blobtype queryparam/bodyparam is missing
5555
}
56-
if ((typeof blobtype) != "string")
57-
{
56+
if (typeof blobtype != "string") {
5857
res.status(400);
5958
res.send(
6059
`${res.statusCode}: 'blobtype' element mandatory in http request ${httpRequestFieldName} must be string value 'base64' or 'array'!\n`
6160
);
6261
return;
6362
}
6463
blobtype = blobtype.toLowerCase().trim();
65-
if (blobtype != "base64" && blobtype != "array")
66-
{
64+
if (blobtype != "base64" && blobtype != "array") {
6765
res.status(400);
6866
res.send(
6967
`${res.statusCode}: 'blobtype' element mandatory in http request ${httpRequestFieldName} must be string value 'base64' or 'array'!\n`
@@ -86,7 +84,7 @@ function getSqlExecutor(httpRequestFieldName) {
8684
);
8785
return;
8886
}
89-
87+
9088
if (blobtype === "base64") {
9189
/**********************************************************************************
9290
Enumerate through sqlite parameters and if of them is a blob param
@@ -97,26 +95,22 @@ function getSqlExecutor(httpRequestFieldName) {
9795
for (let i = 0; i < params.length; i++) {
9896
let param = params[i];
9997
//if the parameter is an object, assume it's a blob parameter
100-
if (typeof param === 'object' && param !== null) {
101-
102-
if (param.hasOwnProperty("data"))
103-
{
98+
if (typeof param === "object" && param !== null) {
99+
if (param.hasOwnProperty("data")) {
104100
var data = param.data;
105101
let buff = null;
106102
if (typeof Buffer.from === "function") {
107103
// Node 5.10+
108-
buff = Buffer.from(data, 'base64');
109-
}
110-
else {
104+
buff = Buffer.from(data, "base64");
105+
} else {
111106
// older Node versions, now deprecated
112-
buff = new Buffer(data, 'base64');
107+
buff = new Buffer(data, "base64");
113108
}
114109
params[i] = buff;
115110
}
116111
}
117112
}
118-
}
119-
else if (blobtype === "array") {
113+
} else if (blobtype === "array") {
120114
/**********************************************************************************
121115
Enumerate through sqlite parameters and if of them is a blob param
122116
then convert the param to a buffer.
@@ -126,16 +120,14 @@ function getSqlExecutor(httpRequestFieldName) {
126120
for (let i = 0; i < params.length; i++) {
127121
let param = params[i];
128122
//if the parameter is an object, assume it's a blob parameter
129-
if (typeof param === 'object' && param !== null) {
130-
if (param.hasOwnProperty("data"))
131-
{
123+
if (typeof param === "object" && param !== null) {
124+
if (param.hasOwnProperty("data")) {
132125
var data = param.data;
133126
let buff = null;
134127
if (typeof Buffer.from === "function") {
135128
// Node 5.10+
136129
buff = Buffer.from(data);
137-
}
138-
else {
130+
} else {
139131
// older Node versions, now deprecated
140132
buff = new Buffer(data);
141133
}
@@ -187,26 +179,24 @@ function getSqlExecutor(httpRequestFieldName) {
187179
db.close();
188180

189181
//if blobtype = base64, enumerate through the rows/fields and convert any buffer fields found to base64 string
190-
if (blobtype == "base64")
191-
{
182+
if (blobtype == "base64") {
192183
for (let i = 0; i < rows.length; i++) {
193184
let row = rows[i];
194185

195186
//get field count for row
196187
let fieldcount = 0;
197188
for (var prop in row) {
198189
if (Object.prototype.hasOwnProperty.call(row, prop)) {
199-
fieldcount ++;
190+
fieldcount++;
200191
}
201192
}
202193

203194
//enumerate through fields
204-
for (let j = 0; j < fieldcount; j++)
205-
{
195+
for (let j = 0; j < fieldcount; j++) {
206196
let fieldname = Object.keys(row)[j];
207197
let fielddata = row[fieldname];
208198
if (Buffer.isBuffer(fielddata)) {
209-
let base64data = fielddata.toString('base64');
199+
let base64data = fielddata.toString("base64");
210200
rows[i][fieldname] = base64data;
211201
}
212202
}

0 commit comments

Comments
 (0)