@tscircuit/cli 0.0.123 → 0.0.124

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/bun.lockb CHANGED
Binary file
Binary file
@@ -15,7 +15,7 @@
15
15
  "typescript": "^5.0.0"
16
16
  },
17
17
  "dependencies": {
18
- "level": "^8.0.1",
18
+ "memory-level": "^1.0.0",
19
19
  "redaxios": "^0.5.1",
20
20
  "winterspec": "0.0.81",
21
21
  "zod": "^3.22.4"
@@ -16,12 +16,14 @@ export default withWinterSpec({
16
16
  }),
17
17
  auth: "none",
18
18
  })(async (req, ctx) => {
19
+ console.log("putting file")
19
20
  const export_file = await ctx.db.put("export_file", {
20
21
  export_request_id: req.jsonBody.export_request_id,
21
22
  file_name: req.jsonBody.file_name,
22
23
  file_content_base64: req.jsonBody.file_content_base64,
23
24
  created_at: new Date().toISOString(),
24
25
  })
26
+ console.log("done putting file")
25
27
 
26
28
  return ctx.json({
27
29
  export_file,
@@ -0,0 +1,126 @@
1
+ import {
2
+ AbstractLevel,
3
+ AbstractIterator,
4
+ AbstractKeyIterator,
5
+ AbstractValueIterator,
6
+ } from "abstract-level"
7
+ import { promises as fs } from "fs"
8
+ import path from "path"
9
+
10
+ interface JSONLevelOptions {}
11
+
12
+ /**
13
+ * This is not totally tested yet, but is basically a NodeJS level that doesn't
14
+ * use C-bindings. It could be used as a replacement for memory-level to store
15
+ * to disk.
16
+ */
17
+ class GenericJsonLevel extends AbstractLevel<string, any> {
18
+ private location: string
19
+
20
+ constructor(location: string, options?: JSONLevelOptions) {
21
+ super({ ...options, encodings: { utf8: true } })
22
+ this.location = location
23
+ }
24
+
25
+ async _open(): Promise<void> {
26
+ await fs.mkdir(this.location, { recursive: true })
27
+ }
28
+
29
+ async _put(key: string, value: any): Promise<void> {
30
+ const filePath = path.join(this.location, `${key}.json`)
31
+ console.log("writing file", filePath)
32
+ await fs.writeFile(filePath, JSON.stringify(value))
33
+ }
34
+
35
+ async _get(key: string): Promise<any> {
36
+ const filePath = path.join(this.location, `${key}.json`)
37
+ try {
38
+ const data = await fs.readFile(filePath, "utf8")
39
+ return JSON.parse(data)
40
+ } catch (error) {
41
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
42
+ throw new Error("NotFoundError")
43
+ }
44
+ throw error
45
+ }
46
+ }
47
+
48
+ async _del(key: string): Promise<void> {
49
+ const filePath = path.join(this.location, `${key}.json`)
50
+ try {
51
+ await fs.unlink(filePath)
52
+ } catch (error) {
53
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
54
+ throw error
55
+ }
56
+ }
57
+ }
58
+
59
+ async _batch(
60
+ operations: Array<{ type: "put" | "del"; key: string; value?: any }>
61
+ ): Promise<void> {
62
+ for (const op of operations) {
63
+ if (op.type === "put") {
64
+ await this._put(op.key, op.value)
65
+ } else if (op.type === "del") {
66
+ await this._del(op.key)
67
+ }
68
+ }
69
+ }
70
+
71
+ async _clear(): Promise<void> {
72
+ const files = await fs.readdir(this.location)
73
+ for (const file of files) {
74
+ if (file.endsWith(".json")) {
75
+ await fs.unlink(path.join(this.location, file))
76
+ }
77
+ }
78
+ }
79
+
80
+ _iterator(): AbstractIterator<this, string, any> {
81
+ let files: string[] = []
82
+ let index = 0
83
+
84
+ const nextFile = async (): Promise<{ key: string; value: any } | null> => {
85
+ if (index >= files.length) {
86
+ return null
87
+ }
88
+
89
+ const file = files[index++]
90
+ const key = path.basename(file, ".json")
91
+ const filePath = path.join(this.location, file)
92
+ const data = await fs.readFile(filePath, "utf8")
93
+ const value = JSON.parse(data)
94
+
95
+ return { key, value }
96
+ }
97
+
98
+ return {
99
+ async next() {
100
+ if (files.length === 0) {
101
+ files = (await fs.readdir(this.db.location)).filter((file) =>
102
+ file.endsWith(".json")
103
+ )
104
+ }
105
+
106
+ const entry = await nextFile()
107
+ if (entry === null) {
108
+ return undefined
109
+ }
110
+
111
+ return [entry.key, entry.value]
112
+ },
113
+ async seek(target: string) {
114
+ index = files.findIndex((file) => file.startsWith(target))
115
+ if (index === -1) {
116
+ index = files.length
117
+ }
118
+ },
119
+ async end() {
120
+ // No resources to clean up
121
+ },
122
+ }
123
+ }
124
+ }
125
+
126
+ export { GenericJsonLevel }
@@ -1,13 +1,15 @@
1
- import { Level } from "level"
1
+ // import { Level } from "level"
2
+ // import { GenericJsonLevel } from "./generic-json-level"
3
+ import { MemoryLevel } from "memory-level"
2
4
  import { z } from "zod"
3
5
  import { DBSchema, type DBSchemaType, type DBInputSchemaType } from "./schema"
4
6
 
5
7
  // Create a wrapper class for Level with Zod validation
6
8
  export class ZodLevelDatabase {
7
- private db: Level<string, any>
9
+ private db: MemoryLevel<string, any>
8
10
 
9
11
  constructor(location: string) {
10
- this.db = new Level(location)
12
+ this.db = new MemoryLevel() // new GenericJsonLevel(location)
11
13
  }
12
14
 
13
15
  async open() {
@@ -35,11 +37,10 @@ export class ZodLevelDatabase {
35
37
  const valueLoose: any = value
36
38
  if (!valueLoose[idkey]) {
37
39
  // generate an id using the "count" key
38
- let count = await this.db
39
- .get(`${collection}.count`, { valueEncoding: "json" })
40
- .catch(() => 1)
40
+ let count = await this.db.get(`${collection}.count`).catch(() => 1)
41
+ if (typeof count === "string") count = parseInt(count)
41
42
  ;(value as any)[idkey] = count
42
- await this.db.put(`${collection}.count`, count + 1)
43
+ await this.db.put(`${collection}.count`, (count + 1).toString())
43
44
  }
44
45
  const key = `${collection}:${valueLoose[idkey]}`
45
46
  const validatedData = DBSchema.shape[collection].parse(value)