rads-db 0.1.86 → 0.1.87

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/dist/index.d.ts CHANGED
@@ -191,6 +191,8 @@ interface GetRestRoutesOptions {
191
191
  db: RadsDb;
192
192
  /** Prefix to use for generated routes. Defaults to "/api/" */
193
193
  prefix?: string;
194
+ /** By default, rest endpoints for manipulating local files are exposed when NODE_ENV === 'development'. Set to true or false to override. */
195
+ exposeFilesystem?: boolean;
194
196
  }
195
197
  interface GetRestRoutesArgs {
196
198
  body?: any;
@@ -22,7 +22,7 @@ function getRestRoutes(options) {
22
22
  })
23
23
  };
24
24
  routes[`${prefix}radsTunnel`] = {
25
- POST: getRadsTunnelHandler(db)
25
+ POST: getRadsTunnelHandler(options)
26
26
  };
27
27
  routes[`${prefix}uploadFile`] = {
28
28
  POST: getUploadFileHandler(db)
@@ -70,7 +70,9 @@ function getBlobFromDataUrl(dataUrl) {
70
70
  type
71
71
  });
72
72
  }
73
- function getRadsTunnelHandler(db) {
73
+ function getRadsTunnelHandler(options) {
74
+ const db = options.db;
75
+ const exposeFilesystem = options.exposeFilesystem ?? process.env.NODE_ENV === "development";
74
76
  return async function radsTunnelHandler(request, ctx) {
75
77
  const {
76
78
  method,
@@ -78,6 +80,10 @@ function getRadsTunnelHandler(db) {
78
80
  args,
79
81
  uploadArgs
80
82
  } = request?.body || {};
83
+ if (!method) throw (0, _h.createError)({
84
+ statusCode: 400,
85
+ message: "Malformed request - missing method"
86
+ });
81
87
  if (method === "uploadFile") {
82
88
  const blob = getBlobFromDataUrl(uploadArgs?.blobDataUrl);
83
89
  if (!blob) throw (0, _h.createError)({
@@ -92,9 +98,10 @@ function getRadsTunnelHandler(db) {
92
98
  if (method === "_schema") {
93
99
  return db._schema;
94
100
  }
95
- if (process.env.NODE_ENV === "development") {
96
- const devHandler = _restEndpointsDev.default[method];
97
- if (devHandler) return devHandler(args);
101
+ if (exposeFilesystem && method.startsWith("fs:")) {
102
+ const fsMethod = method.slice(3);
103
+ const devHandler = _restEndpointsDev.default[fsMethod];
104
+ if (devHandler) return devHandler(...args);
98
105
  }
99
106
  if (!method || !entity) throw (0, _h.createError)({
100
107
  statusCode: 400,
@@ -1,3 +1,3 @@
1
- import type { GetRestRoutesOptions, RadsDb } from '@/types';
1
+ import type { GetRestRoutesOptions } from '@/types';
2
2
  export declare function getRestRoutes(options: GetRestRoutesOptions): GetRestRoutesResponse;
3
- export declare function getRadsTunnelHandler(db: RadsDb): (request: GetRestRoutesArgs, ctx?: any) => Promise<any>;
3
+ export declare function getRadsTunnelHandler(options: GetRestRoutesOptions): (request: GetRestRoutesArgs, ctx?: any) => Promise<any>;
@@ -7,7 +7,7 @@ export function getRestRoutes(options) {
7
7
  const schema = db._schema;
8
8
  routes[`${prefix}me`] = { GET: async (args, ctx) => ({ userId: ctx?.getUser?.()?.id }) };
9
9
  routes[`${prefix}radsTunnel`] = {
10
- POST: getRadsTunnelHandler(db)
10
+ POST: getRadsTunnelHandler(options)
11
11
  };
12
12
  routes[`${prefix}uploadFile`] = { POST: getUploadFileHandler(db) };
13
13
  for (const key in schema) {
@@ -49,9 +49,13 @@ function getBlobFromDataUrl(dataUrl) {
49
49
  throw createError({ statusCode: 400, message: "Malformed request - bad dataUrl" });
50
50
  return new Blob([Buffer.from(b64string, "base64")], { type });
51
51
  }
52
- export function getRadsTunnelHandler(db) {
52
+ export function getRadsTunnelHandler(options) {
53
+ const db = options.db;
54
+ const exposeFilesystem = options.exposeFilesystem ?? process.env.NODE_ENV === "development";
53
55
  return async function radsTunnelHandler(request, ctx) {
54
56
  const { method, entity, args, uploadArgs } = request?.body || {};
57
+ if (!method)
58
+ throw createError({ statusCode: 400, message: "Malformed request - missing method" });
55
59
  if (method === "uploadFile") {
56
60
  const blob = getBlobFromDataUrl(uploadArgs?.blobDataUrl);
57
61
  if (!blob)
@@ -61,10 +65,11 @@ export function getRadsTunnelHandler(db) {
61
65
  if (method === "_schema") {
62
66
  return db._schema;
63
67
  }
64
- if (process.env.NODE_ENV === "development") {
65
- const devHandler = restEndpointsDev[method];
68
+ if (exposeFilesystem && method.startsWith("fs:")) {
69
+ const fsMethod = method.slice(3);
70
+ const devHandler = restEndpointsDev[fsMethod];
66
71
  if (devHandler)
67
- return devHandler(args);
72
+ return devHandler(...args);
68
73
  }
69
74
  if (!method || !entity)
70
75
  throw createError({ statusCode: 400, message: 'Malformed request - missing "method" or "entity"' });
@@ -5,51 +5,26 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  module.exports = void 0;
7
7
  var _promises = _interopRequireDefault(require("node:fs/promises"));
8
- var _nodePath = _interopRequireDefault(require("node:path"));
9
- var _klaw = _interopRequireDefault(require("klaw"));
10
- var _ignore = _interopRequireDefault(require("ignore"));
11
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
- let gitignore;
13
9
  var _default = {
14
- async fsGetFileTree() {
15
- const results = [];
16
- await ensureGitignoreCreated();
17
- const klawIterator = await (0, _klaw.default)(".", {
18
- depthLimit: 15,
19
- preserveSymlinks: true,
20
- filter: p => {
21
- const rp = _nodePath.default.relative(".", p);
22
- if (rp.startsWith(".git")) return false;
23
- return !gitignore.ignores(rp);
24
- }
25
- });
26
- for await (const file of klawIterator) {
27
- if (file.stats.isSymbolicLink()) continue;
28
- const rp = _nodePath.default.relative(".", file.path);
29
- results.push({
30
- sha: rp,
31
- mode: `${file.stats.mode}`,
32
- path: rp,
33
- type: file.stats.isDirectory() ? "tree" : "blob",
34
- size: file.stats.size
35
- });
36
- }
37
- return results;
10
+ async mkdir(filepath) {
11
+ return await _promises.default.mkdir(filepath);
38
12
  },
39
- async fsGetBlobContent(args) {
40
- const text = await _promises.default.readFile(args.path, "utf-8");
41
- return {
42
- text
43
- };
13
+ async rmdir(filepath) {
14
+ return await _promises.default.rmdir(filepath);
44
15
  },
45
- async fsPutFile(args) {
46
- await _promises.default.writeFile(args.path, args.content, "utf-8");
47
- return {};
16
+ async readdir(filepath) {
17
+ return await _promises.default.readdir(filepath);
18
+ },
19
+ async writeFile(filepath, data, encoding) {
20
+ return await _promises.default.writeFile(filepath, data, encoding);
21
+ },
22
+ async readFile(filepath, encoding) {
23
+ return await _promises.default.readFile(filepath, encoding);
24
+ },
25
+ /** Delete a file */
26
+ async unlink(filepath) {
27
+ return await _promises.default.unlink(filepath);
48
28
  }
49
29
  };
50
- module.exports = _default;
51
- async function ensureGitignoreCreated() {
52
- if (gitignore) return;
53
- const gitignoreStr = await _promises.default.readFile("./.gitignore", "utf-8");
54
- gitignore = (0, _ignore.default)().add(gitignoreStr.split("\n").filter(x => x));
55
- }
30
+ module.exports = _default;
@@ -1,14 +1,10 @@
1
- import path from 'node:path';
2
1
  declare const _default: {
3
- fsGetFileTree(): Promise<GithubTreeItem[]>;
4
- fsGetBlobContent(args: {
5
- path: string;
6
- }): Promise<{
7
- text: any;
8
- }>;
9
- fsPutFile(args: {
10
- path: string;
11
- content: string;
12
- }): Promise<{}>;
2
+ mkdir(filepath: string): Promise<any>;
3
+ rmdir(filepath: string): Promise<any>;
4
+ readdir(filepath: string): Promise<any>;
5
+ writeFile(filepath: string, data: string | Uint8Array, encoding?: string): Promise<any>;
6
+ readFile(filepath: string, encoding?: string): Promise<any>;
7
+ /** Delete a file */
8
+ unlink(filepath: string): Promise<any>;
13
9
  };
14
10
  export default _default;
@@ -1,48 +1,22 @@
1
1
  import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import klaw from "klaw";
4
- import ignore from "ignore";
5
- let gitignore;
6
2
  export default {
7
- async fsGetFileTree() {
8
- const results = [];
9
- await ensureGitignoreCreated();
10
- const klawIterator = await klaw(".", {
11
- depthLimit: 15,
12
- preserveSymlinks: true,
13
- filter: (p) => {
14
- const rp = path.relative(".", p);
15
- if (rp.startsWith(".git"))
16
- return false;
17
- return !gitignore.ignores(rp);
18
- }
19
- });
20
- for await (const file of klawIterator) {
21
- if (file.stats.isSymbolicLink())
22
- continue;
23
- const rp = path.relative(".", file.path);
24
- results.push({
25
- sha: rp,
26
- mode: `${file.stats.mode}`,
27
- path: rp,
28
- type: file.stats.isDirectory() ? "tree" : "blob",
29
- size: file.stats.size
30
- });
31
- }
32
- return results;
3
+ async mkdir(filepath) {
4
+ return await fs.mkdir(filepath);
33
5
  },
34
- async fsGetBlobContent(args) {
35
- const text = await fs.readFile(args.path, "utf-8");
36
- return { text };
6
+ async rmdir(filepath) {
7
+ return await fs.rmdir(filepath);
37
8
  },
38
- async fsPutFile(args) {
39
- await fs.writeFile(args.path, args.content, "utf-8");
40
- return {};
9
+ async readdir(filepath) {
10
+ return await fs.readdir(filepath);
11
+ },
12
+ async writeFile(filepath, data, encoding) {
13
+ return await fs.writeFile(filepath, data, encoding);
14
+ },
15
+ async readFile(filepath, encoding) {
16
+ return await fs.readFile(filepath, encoding);
17
+ },
18
+ /** Delete a file */
19
+ async unlink(filepath) {
20
+ return await fs.unlink(filepath);
41
21
  }
42
22
  };
43
- async function ensureGitignoreCreated() {
44
- if (gitignore)
45
- return;
46
- const gitignoreStr = await fs.readFile("./.gitignore", "utf-8");
47
- gitignore = ignore().add(gitignoreStr.split("\n").filter((x) => x));
48
- }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ module.exports = void 0;
7
+ var _promises = _interopRequireDefault(require("node:fs/promises"));
8
+ var _nodePath = _interopRequireDefault(require("node:path"));
9
+ var _klaw = _interopRequireDefault(require("klaw"));
10
+ var _ignore = _interopRequireDefault(require("ignore"));
11
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+ let gitignore;
13
+ var _default = {
14
+ async fsGetFileTree() {
15
+ const results = [];
16
+ await ensureGitignoreCreated();
17
+ const klawIterator = await (0, _klaw.default)(".", {
18
+ depthLimit: 15,
19
+ preserveSymlinks: true,
20
+ filter: p => {
21
+ const rp = _nodePath.default.relative(".", p);
22
+ if (rp.startsWith(".git")) return false;
23
+ return !gitignore.ignores(rp);
24
+ }
25
+ });
26
+ for await (const file of klawIterator) {
27
+ if (file.stats.isSymbolicLink()) continue;
28
+ const rp = _nodePath.default.relative(".", file.path);
29
+ results.push({
30
+ sha: rp,
31
+ mode: `${file.stats.mode}`,
32
+ path: rp,
33
+ type: file.stats.isDirectory() ? "tree" : "blob",
34
+ size: file.stats.size
35
+ });
36
+ }
37
+ return results;
38
+ },
39
+ async fsGetBlobContent(args) {
40
+ const text = await _promises.default.readFile(args.path, "utf-8");
41
+ return {
42
+ text
43
+ };
44
+ },
45
+ async fsPutFile(args) {
46
+ await _promises.default.writeFile(args.path, args.content, "utf-8");
47
+ return {};
48
+ }
49
+ };
50
+ module.exports = _default;
51
+ async function ensureGitignoreCreated() {
52
+ if (gitignore) return;
53
+ const gitignoreStr = await _promises.default.readFile("./.gitignore", "utf-8");
54
+ gitignore = (0, _ignore.default)().add(gitignoreStr.split("\n").filter(x => x));
55
+ }
@@ -0,0 +1,14 @@
1
+ import path from 'node:path';
2
+ declare const _default: {
3
+ fsGetFileTree(): Promise<GithubTreeItem[]>;
4
+ fsGetBlobContent(args: {
5
+ path: string;
6
+ }): Promise<{
7
+ text: any;
8
+ }>;
9
+ fsPutFile(args: {
10
+ path: string;
11
+ content: string;
12
+ }): Promise<{}>;
13
+ };
14
+ export default _default;
@@ -0,0 +1,48 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import klaw from "klaw";
4
+ import ignore from "ignore";
5
+ let gitignore;
6
+ export default {
7
+ async fsGetFileTree() {
8
+ const results = [];
9
+ await ensureGitignoreCreated();
10
+ const klawIterator = await klaw(".", {
11
+ depthLimit: 15,
12
+ preserveSymlinks: true,
13
+ filter: (p) => {
14
+ const rp = path.relative(".", p);
15
+ if (rp.startsWith(".git"))
16
+ return false;
17
+ return !gitignore.ignores(rp);
18
+ }
19
+ });
20
+ for await (const file of klawIterator) {
21
+ if (file.stats.isSymbolicLink())
22
+ continue;
23
+ const rp = path.relative(".", file.path);
24
+ results.push({
25
+ sha: rp,
26
+ mode: `${file.stats.mode}`,
27
+ path: rp,
28
+ type: file.stats.isDirectory() ? "tree" : "blob",
29
+ size: file.stats.size
30
+ });
31
+ }
32
+ return results;
33
+ },
34
+ async fsGetBlobContent(args) {
35
+ const text = await fs.readFile(args.path, "utf-8");
36
+ return { text };
37
+ },
38
+ async fsPutFile(args) {
39
+ await fs.writeFile(args.path, args.content, "utf-8");
40
+ return {};
41
+ }
42
+ };
43
+ async function ensureGitignoreCreated() {
44
+ if (gitignore)
45
+ return;
46
+ const gitignoreStr = await fs.readFile("./.gitignore", "utf-8");
47
+ gitignore = ignore().add(gitignoreStr.split("\n").filter((x) => x));
48
+ }
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "require": "./features/*.cjs"
41
41
  }
42
42
  },
43
- "version": "0.1.86",
43
+ "version": "0.1.87",
44
44
  "description": "Say goodbye to boilerplate code and hello to efficient and elegant syntax.",
45
45
  "keywords": [],
46
46
  "author": "",