wao 0.6.0 → 0.6.2

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/esm/ao.js CHANGED
@@ -46,6 +46,7 @@ import {
46
46
  mergeOut,
47
47
  isOutComplete,
48
48
  jsonToStr,
49
+ optAO,
49
50
  } from "./utils.js"
50
51
 
51
52
  function createDataItemSigner2(wallet) {
@@ -61,15 +62,26 @@ function createDataItemSigner2(wallet) {
61
62
  }
62
63
 
63
64
  class AO {
64
- constructor({
65
- authority = srcs.authority,
66
- module,
67
- module_type = "aos2",
68
- scheduler = srcs.scheduler,
69
- aoconnect,
70
- ar = {},
71
- in_memory = false,
72
- } = {}) {
65
+ constructor(opt = {}) {
66
+ let _port = null
67
+ if (typeof opt === "number") {
68
+ _port = opt
69
+ opt = {}
70
+ }
71
+ let {
72
+ authority = srcs.authority,
73
+ module,
74
+ module_type = "aos2",
75
+ scheduler = srcs.scheduler,
76
+ aoconnect,
77
+ ar,
78
+ in_memory = false,
79
+ port,
80
+ } = opt
81
+
82
+ if (!_port && port) _port = port
83
+ if (!aoconnect && _port) aoconnect = optAO(_port)
84
+ if (!ar && _port) ar = { port: _port }
73
85
  if (!module) {
74
86
  switch (module_type) {
75
87
  case "sqlite":
@@ -83,14 +95,13 @@ class AO {
83
95
  }
84
96
  }
85
97
  this.__type__ = "ao"
86
-
87
98
  if (ar?.__type__ === "ar") this.ar = ar
88
99
  else {
89
100
  let _ar = typeof ar === "object" ? ar : {}
90
- this.ar = new AR(ar)
101
+ this.ar = new AR(_ar)
91
102
  }
92
- if (in_memory) {
93
- } else if (aoconnect) {
103
+
104
+ if (!in_memory && aoconnect) {
94
105
  const {
95
106
  results,
96
107
  assign,
@@ -620,6 +631,7 @@ class AO {
620
631
  while (attempts > 0) {
621
632
  if (!this.in_memory) await wait(1000)
622
633
  const { res, err: _err } = await this.dry({ pid, data: "#Inbox" })
634
+ if (res?.error) return { err: res.error, pid }
623
635
  if (typeof res?.Output === "object") break
624
636
  attempts -= 1
625
637
  if (attempts === 0) err = "timeout"
package/esm/aoconnect.js CHANGED
@@ -23,7 +23,7 @@ import { scheduler, mu, su, cu, acc } from "./test.js"
23
23
  import { is, clone, fromPairs, map, mergeLeft, isNil } from "ramda"
24
24
  import AR from "./tar.js"
25
25
 
26
- export const connect = (mem, log = false) => {
26
+ export const connect = (mem, { log = false, extensions = {} }) => {
27
27
  const isMem = mem?.__type__ === "mem"
28
28
  if (!isMem) {
29
29
  let args = {}
@@ -34,7 +34,8 @@ export const connect = (mem, log = false) => {
34
34
  mem = new ArMem(args)
35
35
  }
36
36
  const ar = new AR({ mem, log })
37
- const WeaveDrive = new weavedrive(ar).drive
37
+ for (const k in extensions) extensions[k] = new extensions[k](ar).ext
38
+ extensions.WeaveDrive = new weavedrive(ar).ext
38
39
 
39
40
  const transform = input => {
40
41
  const output = { Tags: [] }
@@ -149,9 +150,10 @@ export const connect = (mem, log = false) => {
149
150
  if (opt.item) opt.data = base64url.decode(item.data)
150
151
  await ar.postItems(item, su.jwk)
151
152
  const now = Date.now
153
+ const t = tags(opt.tags)
152
154
  const handle = await AoLoader(wasm, {
153
155
  format,
154
- WeaveDrive,
156
+ WeaveDrive: extensions[t.Extension],
155
157
  spawn: item,
156
158
  module: mem.txs[mod],
157
159
  blockHeight: "100",
@@ -378,7 +380,6 @@ export const connect = (mem, log = false) => {
378
380
  target: opt.process,
379
381
  }))
380
382
  }
381
- //await ar.postItems(item, su.jwk)
382
383
  mem.msgs[id] = opt
383
384
  await assign({
384
385
  message_item: item,
@@ -458,11 +459,11 @@ export const connect = (mem, log = false) => {
458
459
  auth: mu.addr,
459
460
  })
460
461
  function cloneMemory(memory) {
461
- const buffer = memory.buffer.slice(0) // Clone the ArrayBuffer
462
+ const buffer = memory.buffer.slice(0)
462
463
  return new WebAssembly.Memory({
463
- initial: memory.buffer.byteLength / 65536, // Memory size in pages (64KB per page)
464
+ initial: memory.buffer.byteLength / 65536,
464
465
  maximum: memory.maximum || undefined,
465
- shared: memory.shared || false, // Retain sharing if applicable
466
+ shared: memory.shared || false,
466
467
  })
467
468
  }
468
469
  const res = await p.handle(p.memory, msg, _env)
package/esm/helpers.js CHANGED
@@ -3,6 +3,7 @@ import assert from "assert"
3
3
  import { createDataItemSigner, connect } from "@permaweb/aoconnect"
4
4
  import { dirname as _dirname, resolve } from "path"
5
5
  import { mkdirSync, existsSync, writeFileSync, readFileSync } from "fs"
6
+ import { optAO } from "./utils.js"
6
7
  import yargs from "yargs"
7
8
 
8
9
  let {
@@ -45,12 +46,7 @@ export class Testnet {
45
46
  constructor({ port = 4000, arweave, aoconnect, docker = false } = {}) {
46
47
  this.docker = docker
47
48
  this.arweave = arweave ?? { port }
48
- this.aoconnect = aoconnect ?? {
49
- MU_URL: `http://localhost:${port + 2}`,
50
- SU_URL: `http://localhost:${port + 3}`,
51
- CU_URL: `http://localhost:${port + 4}`,
52
- GATEWAY_URL: `http://localhost:${port}`,
53
- }
49
+ this.aoconnect = aoconnect ?? optAO(port)
54
50
  this.ar = new AR(this.arweave)
55
51
  }
56
52
  async init(jwk) {
@@ -60,7 +56,7 @@ export class Testnet {
60
56
  this.src = await new Src({ ar: this.ar }).init()
61
57
  await this.ar.init(jwk)
62
58
  this.jwk = this.ar.jwk
63
- this.addr = this.ar.a
59
+ this.addr = this.ar.addr
64
60
  this.gql = this.ar.gql
65
61
  this.module_src = await this.src.upload("aos2_0_1", "wasm")
66
62
  this.ao = await new AO({
@@ -119,11 +115,7 @@ export const setup = async ({
119
115
 
120
116
  // ar
121
117
  arweave ??= { port: 4000 }
122
- aoconnect ??= {
123
- MU_URL: "http://localhost:4002",
124
- CU_URL: "http://localhost:4004",
125
- GATEWAY_URL: "http://localhost:4000",
126
- }
118
+ aoconnect ??= optAO(4000)
127
119
  const ar = new AR(arweave)
128
120
  await ar.gen("10")
129
121
  const src = new Src({ ar, dir })
@@ -0,0 +1,43 @@
1
+ local json = require("json")
2
+ function rollup (id)
3
+ local file = io.open("/rollup/" .. id)
4
+ local data = nil
5
+ if file then data = file:read(file:seek('end')) end
6
+ file:close()
7
+ return data
8
+ end
9
+
10
+ function get (col, doc)
11
+ local file = io.open("/data/" .. col .. "/" .. doc)
12
+ local data = nil
13
+ if file then data = file:read(file:seek('end')) end
14
+ file:close()
15
+ return data
16
+ end
17
+
18
+ Handlers.add(
19
+ "Rollup",
20
+ "Rollup",
21
+ function (msg)
22
+ msg.reply({ Data = "committed!" })
23
+ end
24
+ )
25
+
26
+ Handlers.add(
27
+ "Finalize",
28
+ "Finalize",
29
+ function (msg)
30
+ local data = json.decode(rollup(msg.TXID))
31
+ msg.reply({ Data = "finalized!" })
32
+ end
33
+ )
34
+
35
+
36
+ Handlers.add(
37
+ "Get",
38
+ "Get",
39
+ function (msg)
40
+ msg.reply({ Data = get(msg.col, msg.doc) })
41
+ end
42
+ )
43
+
package/esm/server.js CHANGED
@@ -27,7 +27,7 @@ class Server {
27
27
  mem,
28
28
  monitor,
29
29
  unmonitor,
30
- } = connect(aoconnect, log)
30
+ } = connect(aoconnect, { log })
31
31
  this.monitor = monitor
32
32
  this.unmonitor = unmonitor
33
33
  this.spawn = spawn
package/esm/tao.js CHANGED
@@ -64,7 +64,7 @@ class AO extends MAO {
64
64
  monitor,
65
65
  unmonitor,
66
66
  mem,
67
- } = connect(opt.mem)
67
+ } = connect(opt.mem, { extensions: opt.extensions })
68
68
  this.module = mem.modules.aos2_0_1
69
69
  this.assign = assign
70
70
  this.result = async (...opt) => {
package/esm/utils.js CHANGED
@@ -519,7 +519,21 @@ const toGraphObj = ({ query, variables }) => {
519
519
  }
520
520
  return { tar, args }
521
521
  }
522
+ const optAO = port => {
523
+ return {
524
+ MU_URL: `http://localhost:${port + 2}`,
525
+ SU_URL: `http://localhost:${port + 3}`,
526
+ CU_URL: `http://localhost:${port + 4}`,
527
+ GATEWAY_URL: `http://localhost:${port}`,
528
+ }
529
+ }
530
+ const optServer = port => {
531
+ return { ar: port, mu: port + 2, su: port + 3, cu: port + 4 }
532
+ }
533
+
522
534
  export {
535
+ optAO,
536
+ optServer,
523
537
  toGraphObj,
524
538
  jsonToStr,
525
539
  mergeChecks,
package/esm/weavedb.js ADDED
@@ -0,0 +1,309 @@
1
+ import { resolve } from "path"
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
3
+ const KB = 1024
4
+ const MB = KB * 1024
5
+ const CACHE_SZ = 32 * KB
6
+ const CHUNK_SZ = 128 * MB
7
+ const NOTIFY_SZ = 512 * MB
8
+ const log = console.log
9
+ const rand = Math.floor(Math.random() * 1000000).toString()
10
+
11
+ export default class WeaveDB {
12
+ constructor(ar, dir) {
13
+ dir ??= resolve(import.meta.dirname, ".db")
14
+ const kv_dir = resolve(dir, rand)
15
+ const data_dir = resolve(kv_dir, "data")
16
+ const rollup_dir = resolve(kv_dir, "rollup")
17
+ for (const v of [dir, kv_dir, rollup_dir, data_dir]) {
18
+ if (!existsSync(v)) mkdirSync(v)
19
+ }
20
+ this.ext = (mod, FS) => {
21
+ let cache = { data: {}, rollup: {} }
22
+ return {
23
+ async create(id) {
24
+ let properties = { isDevice: false, contents: null }
25
+ if (!FS.analyzePath("/rollup/").exists) FS.mkdir("/rollup/")
26
+ let node = FS.createFile("/", "rollup/" + id, properties, true, false)
27
+
28
+ // Set initial parame
29
+ let data = await ar.data(id)
30
+ const bytesLength = data?.length ?? 0
31
+ node.total_size = Number(bytesLength)
32
+ node.cache = new Uint8Array(0)
33
+ node.position = 0
34
+
35
+ // Add a function that defers querying the file size until it is asked the first time.
36
+ Object.defineProperties(node, {
37
+ usedBytes: { get: () => bytesLength },
38
+ })
39
+
40
+ // Now we have created the file in the emscripten FS, we can open it as a stream
41
+ let stream = FS.open("/rollup/" + id, "r")
42
+
43
+ //console.log("JS: Created file: ", id, " fd: ", stream.fd);
44
+ return stream
45
+ },
46
+ async createData(col, doc, val) {
47
+ let properties = { isDevice: false, contents: null }
48
+ if (!FS.analyzePath("/data/").exists) FS.mkdir("/data/")
49
+ if (!FS.analyzePath(`/data/${col}`).exists) FS.mkdir(`/data/${col}`)
50
+ let node = FS.createFile(
51
+ "/",
52
+ `data/${col}/${doc}`,
53
+ properties,
54
+ true,
55
+ false,
56
+ )
57
+
58
+ // Set initial parame
59
+ let data = val
60
+ if (!val) {
61
+ const col_dir = resolve(data_dir, col)
62
+ const _data =
63
+ readFileSync(resolve(col_dir, `${doc}.json`), "utf8") ?? ""
64
+ data = Buffer.from(_data, "utf8")
65
+ }
66
+ const bytesLength = data?.length ?? 0
67
+ node.total_size = Number(bytesLength)
68
+ node.cache = new Uint8Array(0)
69
+ node.position = 0
70
+
71
+ // Add a function that defers querying the file size until it is asked the first time.
72
+ Object.defineProperties(node, {
73
+ usedBytes: { get: () => bytesLength },
74
+ })
75
+
76
+ // Now we have created the file in the emscripten FS, we can open it as a stream
77
+ let stream = FS.open("/data/" + `${col}/${doc}`, "r")
78
+
79
+ //console.log("JS: Created file: ", id, " fd: ", stream.fd);
80
+ return stream
81
+ },
82
+ async open(filename, val) {
83
+ const pathCategory = filename.split("/")[1]
84
+ if (pathCategory === "rollup") {
85
+ //log("JS: Opening ID: ", id)
86
+ const id = filename.split("/")[2]
87
+ if (FS.analyzePath(filename).exists) {
88
+ let stream = FS.open(filename, "r")
89
+ if (stream.fd) return stream.fd
90
+ console.log("JS: File not found: ", filename)
91
+ return 0
92
+ } else {
93
+ //console.log("JS: Open => Creating file: ", id);
94
+ const stream = await this.create(id)
95
+ //console.log("JS: Open => Created file: ", id, " fd: ", stream.fd);
96
+ return stream.fd
97
+ }
98
+ } else if (pathCategory === "data") {
99
+ //log("JS: Opening ID: ", id)
100
+ const col = filename.split("/")[2]
101
+ const doc = filename.split("/")[3]
102
+ if (FS.analyzePath(filename).exists) {
103
+ let stream = FS.open(filename, "r")
104
+ if (stream.fd) return stream.fd
105
+ console.log("JS: File not found: ", filename)
106
+ return 0
107
+ } else {
108
+ //console.log("JS: Open => Creating file: ", id);
109
+ const stream = await this.createData(col, doc, val)
110
+ //console.log("JS: Open => Created file: ", id, " fd: ", stream.fd);
111
+ return stream.fd
112
+ }
113
+ } else {
114
+ console.log("JS: Invalid path category: ", pathCategory)
115
+ return 0
116
+ }
117
+ },
118
+ async read(fd, raw_dst_ptr, raw_length, val) {
119
+ let to_read = Number(raw_length)
120
+ let dst_ptr = Number(raw_dst_ptr)
121
+ let stream = 0
122
+ for (let i = 0; i < FS.streams.length; i++) {
123
+ if (FS.streams[i].fd === fd) stream = FS.streams[i]
124
+ }
125
+ // Satisfy what we can with the cache first
126
+ let bytes_read = this.readFromCache(stream, dst_ptr, to_read)
127
+ stream.position += bytes_read
128
+ stream.lastReadPosition = stream.position
129
+ dst_ptr += bytes_read
130
+ to_read -= bytes_read
131
+
132
+ // Return if we have satisfied the request
133
+
134
+ //console.log("KV: Satisfied request with cache. Returning...")
135
+ if (to_read === 0) return bytes_read
136
+
137
+ //console.log("KV: Read from cache: ", bytes_read, " Remaining to read: ", to_read)
138
+
139
+ const chunk_download_sz = Math.max(to_read, CACHE_SZ)
140
+ const to = Math.min(
141
+ stream.node.total_size,
142
+ stream.position + chunk_download_sz,
143
+ )
144
+ let data = val
145
+ if (!data) {
146
+ const sp = stream.path.split("/")
147
+ if (sp[1] === "data") {
148
+ const col_dir = resolve(data_dir, sp[2])
149
+ const _data =
150
+ readFileSync(resolve(col_dir, `${sp[3]}.json`), "utf8") ?? ""
151
+ data = Buffer.from(_data, "utf8")
152
+ } else {
153
+ data = await ar.data(stream.node.name)
154
+ try {
155
+ const json = JSON.parse(Buffer.from(data).toString())
156
+ for (const v of json.diffs) {
157
+ const val = Buffer.from(JSON.stringify(v.data))
158
+ const _fd = await this.open(
159
+ `/data/${v.collection}/${v.doc}`,
160
+ val,
161
+ )
162
+ const col_dir = resolve(data_dir, v.collection)
163
+ if (!existsSync(col_dir)) mkdirSync(col_dir)
164
+ writeFileSync(
165
+ resolve(col_dir, `${v.doc}.json`),
166
+ JSON.stringify(v.data),
167
+ )
168
+ await this.read(_fd, _fd, val.length, val)
169
+ }
170
+ } catch (e) {
171
+ log(e)
172
+ }
173
+ }
174
+ }
175
+ // Extract the Range header to determine the start and end of the requested chunk
176
+ const start = 0
177
+ const end = data.length
178
+ // Create a ReadableStream for the requested chunk
179
+ const chunk = data.subarray(start, end)
180
+ const response = new Response(
181
+ new ReadableStream({
182
+ start(controller) {
183
+ controller.enqueue(chunk) // Push the chunk to the stream
184
+ controller.close() // Close the stream when done
185
+ },
186
+ }),
187
+ {
188
+ headers: { "Content-Length": chunk.length.toString() },
189
+ },
190
+ )
191
+ const reader = response.body.getReader()
192
+ let bytes_until_cache = CHUNK_SZ
193
+ let bytes_until_notify = NOTIFY_SZ
194
+ let downloaded_bytes = 0
195
+ let cache_chunks = []
196
+
197
+ try {
198
+ while (true) {
199
+ const { done, value: chunk_bytes } = await reader.read()
200
+ if (done) break
201
+ // Update the number of downloaded bytes to be _all_, not just the write length
202
+ downloaded_bytes += chunk_bytes.length
203
+ bytes_until_cache -= chunk_bytes.length
204
+ bytes_until_notify -= chunk_bytes.length
205
+
206
+ // Write bytes from the chunk and update the pointer if necessary
207
+ const write_length = Math.min(chunk_bytes.length, to_read)
208
+ if (write_length > 0) {
209
+ //console.log("KV: Writing: ", write_length, " bytes to: ", dst_ptr)
210
+ mod.HEAP8.set(chunk_bytes.subarray(0, write_length), dst_ptr)
211
+ dst_ptr += write_length
212
+ bytes_read += write_length
213
+ stream.position += write_length
214
+ to_read -= write_length
215
+ }
216
+
217
+ if (to_read == 0) {
218
+ // Add excess bytes to our cache
219
+ const chunk_to_cache = chunk_bytes.subarray(write_length)
220
+ //console.log("KV: Cacheing excess: ", chunk_to_cache.length)
221
+ cache_chunks.push(chunk_to_cache)
222
+ }
223
+ if (bytes_until_cache <= 0) {
224
+ console.log("KV: Chunk size reached. Compressing cache...")
225
+ stream.node.cache = this.addChunksToCache(
226
+ stream.node.cache,
227
+ cache_chunks,
228
+ )
229
+ cache_chunks = []
230
+ bytes_until_cache = CHUNK_SZ
231
+ }
232
+
233
+ if (bytes_until_notify <= 0) {
234
+ console.log(
235
+ "KV: Downloaded: ",
236
+ (downloaded_bytes / stream.node.total_size) * 100,
237
+ "%",
238
+ )
239
+ bytes_until_notify = NOTIFY_SZ
240
+ }
241
+ }
242
+ } catch (error) {
243
+ console.error("KV: Error reading the stream: ", error)
244
+ } finally {
245
+ reader.releaseLock()
246
+ }
247
+ // If we have no cache, or we have not satisfied the full request, we need to download the rest
248
+ // Rebuild the cache from the new cache chunks
249
+ stream.node.cache = this.addChunksToCache(
250
+ stream.node.cache,
251
+ cache_chunks,
252
+ )
253
+
254
+ // Update the last read position
255
+ stream.lastReadPosition = stream.position
256
+ return bytes_read
257
+ },
258
+ close(fd) {
259
+ let stream = 0
260
+ for (let i = 0; i < FS.streams.length; i++) {
261
+ if (FS.streams[i].fd === fd) stream = FS.streams[i]
262
+ }
263
+ FS.close(stream)
264
+ },
265
+
266
+ readFromCache(stream, dst_ptr, length) {
267
+ // Check if the cache has been invalidated by a seek
268
+ if (stream.lastReadPosition !== stream.position) {
269
+ //console.log("KV: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.position, " Last read pos: ", stream.lastReadPosition)
270
+ stream.node.cache = new Uint8Array(0)
271
+ return 0
272
+ }
273
+ // Calculate the bytes of the request that can be satisfied with the cache
274
+ let cache_part_length = Math.min(length, stream.node.cache.length)
275
+ let cache_part = stream.node.cache.subarray(0, cache_part_length)
276
+ mod.HEAP8.set(cache_part, dst_ptr)
277
+ // Set the new cache to the remainder of the unused cache and update pointers
278
+ stream.node.cache = stream.node.cache.subarray(cache_part_length)
279
+
280
+ return cache_part_length
281
+ },
282
+
283
+ addChunksToCache(old_cache, chunks) {
284
+ // Make a new cache array of the old cache length + the sum of the chunk lengths, capped by the max cache size
285
+ let new_cache_length = Math.min(
286
+ old_cache.length +
287
+ chunks.reduce((acc, chunk) => acc + chunk.length, 0),
288
+ CACHE_SZ,
289
+ )
290
+ let new_cache = new Uint8Array(new_cache_length)
291
+ // Copy the old cache to the new cache
292
+ new_cache.set(old_cache, 0)
293
+ // Load the cache chunks into the new cache
294
+ let current_offset = old_cache.length
295
+ for (let chunk of chunks) {
296
+ if (current_offset < new_cache_length) {
297
+ new_cache.set(
298
+ chunk.subarray(0, new_cache_length - current_offset),
299
+ current_offset,
300
+ )
301
+ current_offset += chunk.length
302
+ }
303
+ }
304
+ return new_cache
305
+ },
306
+ }
307
+ }
308
+ }
309
+ }