@cloud-cli/d0 1.1.0 → 1.2.0
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/.dockerignore +1 -0
- package/Dockerfile +4 -0
- package/README.md +5 -3
- package/dist/index.d.ts +9 -1
- package/dist/index.js +25 -27
- package/package.json +2 -2
package/.dockerignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.github/
|
package/Dockerfile
ADDED
package/README.md
CHANGED
|
@@ -9,9 +9,11 @@ SQLite server over HTTP
|
|
|
9
9
|
Run a prepared SQLite statement.
|
|
10
10
|
Accepts a JSON with these properties:
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
| Property | Description | Required |
|
|
13
|
+
|-|:-:|-|
|
|
14
|
+
|`s` | string with the statement | **yes** |
|
|
15
|
+
|`d` | data to bind on a statement | no |
|
|
16
|
+
|`m` | method to execute: `all`, `run` or `get`. Run is the default | no |
|
|
15
17
|
|
|
16
18
|
```js
|
|
17
19
|
// select all items using fetch
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
2
|
import { Database } from 'better-sqlite3';
|
|
3
|
+
type Q = {
|
|
4
|
+
db: Database;
|
|
5
|
+
request: IncomingMessage;
|
|
6
|
+
response: ServerResponse;
|
|
7
|
+
args: Record<string, string>;
|
|
8
|
+
};
|
|
4
9
|
export declare function getDatabase(): Database;
|
|
5
10
|
export declare function serve(): import("http").Server<typeof IncomingMessage, typeof ServerResponse>;
|
|
11
|
+
export declare function handleRequest(request: any, response: any, db: any): Promise<void>;
|
|
12
|
+
export declare function onQuery({ db, request, response }: Q): Promise<void>;
|
|
13
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -9,34 +9,40 @@ export function getDatabase() {
|
|
|
9
9
|
}
|
|
10
10
|
export function serve() {
|
|
11
11
|
const db = getDatabase();
|
|
12
|
-
const server = createServer((
|
|
13
|
-
const url = new URL(request.url, 'http://localhost');
|
|
14
|
-
const route = `${request.method} ${url.pathname}`.trim();
|
|
15
|
-
const args = Object.fromEntries(url.searchParams.entries());
|
|
16
|
-
const q = { db, request, response, args };
|
|
17
|
-
switch (route) {
|
|
18
|
-
case 'GET /index.mjs':
|
|
19
|
-
return onEsModule(q);
|
|
20
|
-
case 'POST /query':
|
|
21
|
-
return onQuery(q);
|
|
22
|
-
default:
|
|
23
|
-
response.writeHead(404).end();
|
|
24
|
-
}
|
|
25
|
-
});
|
|
12
|
+
const server = createServer((req, res) => handleRequest(req, res, db));
|
|
26
13
|
server.listen(process.env.PORT);
|
|
27
14
|
return server;
|
|
28
15
|
}
|
|
29
|
-
|
|
30
|
-
const
|
|
16
|
+
export function handleRequest(request, response, db) {
|
|
17
|
+
const url = new URL(request.url, 'http://localhost');
|
|
18
|
+
const route = `${request.method} ${url.pathname}`.trim();
|
|
19
|
+
const args = Object.fromEntries(url.searchParams.entries());
|
|
20
|
+
const q = { db, request, response, args };
|
|
21
|
+
switch (route) {
|
|
22
|
+
case 'GET /index.mjs':
|
|
23
|
+
return onEsModule(q);
|
|
24
|
+
case 'POST /query':
|
|
25
|
+
return onQuery(q);
|
|
26
|
+
default:
|
|
27
|
+
response.writeHead(404).end();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export async function onQuery({ db, request, response }) {
|
|
31
|
+
const query = Buffer.concat(await request.toArray());
|
|
31
32
|
if (!query.length) {
|
|
32
33
|
response.writeHead(400).end();
|
|
33
34
|
return;
|
|
34
35
|
}
|
|
35
36
|
try {
|
|
36
|
-
const { s, d, m = 'run' } = JSON.parse(query);
|
|
37
|
+
const { s = '', d, m = 'run' } = JSON.parse(query.toString('utf-8'));
|
|
38
|
+
if (!s.trim()) {
|
|
39
|
+
throw new Error('Invalid statement.');
|
|
40
|
+
}
|
|
41
|
+
if (!methods.includes(m)) {
|
|
42
|
+
throw new Error('Invalid method. Must be one of ' + methods.join(', '));
|
|
43
|
+
}
|
|
37
44
|
const runner = db.prepare(s);
|
|
38
|
-
const
|
|
39
|
-
const result = d ? runner[method](d) : runner[method]();
|
|
45
|
+
const result = d ? runner[m](d) : runner[m]();
|
|
40
46
|
response.end(JSON.stringify(result));
|
|
41
47
|
}
|
|
42
48
|
catch (error) {
|
|
@@ -61,11 +67,3 @@ async function onEsModule({ request, response }) {
|
|
|
61
67
|
})
|
|
62
68
|
.end(code.replace('__API_URL__', hostname));
|
|
63
69
|
}
|
|
64
|
-
function readStream(stream) {
|
|
65
|
-
return new Promise((resolve, reject) => {
|
|
66
|
-
const all = [];
|
|
67
|
-
stream.on('data', (c) => all.push(c));
|
|
68
|
-
stream.on('end', () => resolve(Buffer.concat(all).toString('utf8')));
|
|
69
|
-
stream.on('error', reject);
|
|
70
|
-
});
|
|
71
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-cli/d0",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"better-sqlite3": "^
|
|
21
|
+
"better-sqlite3": "^11.0.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@cloud-cli/prettier-config": "^1.0.0",
|