wao 0.6.0 → 0.6.1
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/cjs/ao.js +102 -81
- package/cjs/aoconnect.js +36 -32
- package/cjs/helpers.js +4 -12
- package/cjs/kv.js +335 -0
- package/cjs/utils.js +17 -1
- package/cjs/weavedrive.js +236 -588
- package/esm/ao.js +25 -13
- package/esm/aoconnect.js +8 -5
- package/esm/helpers.js +4 -12
- package/esm/kv.js +226 -0
- package/esm/utils.js +14 -0
- package/esm/weavedrive.js +67 -293
- package/package.json +1 -1
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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(
|
|
101
|
+
this.ar = new AR(_ar)
|
|
91
102
|
}
|
|
92
|
-
|
|
93
|
-
|
|
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
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
} from "./utils.js"
|
|
17
17
|
import ArMem from "./armem.js"
|
|
18
18
|
import weavedrive from "./weavedrive.js"
|
|
19
|
+
import kv from "./kv.js"
|
|
19
20
|
import AoLoader from "@permaweb/ao-loader"
|
|
20
21
|
import { readFileSync } from "fs"
|
|
21
22
|
import { resolve } from "path"
|
|
@@ -35,6 +36,7 @@ export const connect = (mem, log = false) => {
|
|
|
35
36
|
}
|
|
36
37
|
const ar = new AR({ mem, log })
|
|
37
38
|
const WeaveDrive = new weavedrive(ar).drive
|
|
39
|
+
const KV = new kv(ar).kv
|
|
38
40
|
|
|
39
41
|
const transform = input => {
|
|
40
42
|
const output = { Tags: [] }
|
|
@@ -149,9 +151,11 @@ export const connect = (mem, log = false) => {
|
|
|
149
151
|
if (opt.item) opt.data = base64url.decode(item.data)
|
|
150
152
|
await ar.postItems(item, su.jwk)
|
|
151
153
|
const now = Date.now
|
|
154
|
+
const t = tags(opt.tags)
|
|
155
|
+
const wd = t.Extension === "KV" ? KV : WeaveDrive
|
|
152
156
|
const handle = await AoLoader(wasm, {
|
|
153
157
|
format,
|
|
154
|
-
WeaveDrive,
|
|
158
|
+
WeaveDrive: wd,
|
|
155
159
|
spawn: item,
|
|
156
160
|
module: mem.txs[mod],
|
|
157
161
|
blockHeight: "100",
|
|
@@ -378,7 +382,6 @@ export const connect = (mem, log = false) => {
|
|
|
378
382
|
target: opt.process,
|
|
379
383
|
}))
|
|
380
384
|
}
|
|
381
|
-
//await ar.postItems(item, su.jwk)
|
|
382
385
|
mem.msgs[id] = opt
|
|
383
386
|
await assign({
|
|
384
387
|
message_item: item,
|
|
@@ -458,11 +461,11 @@ export const connect = (mem, log = false) => {
|
|
|
458
461
|
auth: mu.addr,
|
|
459
462
|
})
|
|
460
463
|
function cloneMemory(memory) {
|
|
461
|
-
const buffer = memory.buffer.slice(0)
|
|
464
|
+
const buffer = memory.buffer.slice(0)
|
|
462
465
|
return new WebAssembly.Memory({
|
|
463
|
-
initial: memory.buffer.byteLength / 65536,
|
|
466
|
+
initial: memory.buffer.byteLength / 65536,
|
|
464
467
|
maximum: memory.maximum || undefined,
|
|
465
|
-
shared: memory.shared || false,
|
|
468
|
+
shared: memory.shared || false,
|
|
466
469
|
})
|
|
467
470
|
}
|
|
468
471
|
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.
|
|
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 })
|
package/esm/kv.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import Arweave from "arweave"
|
|
2
|
+
import { toGraphObj } from "./utils.js"
|
|
3
|
+
import { map } from "ramda"
|
|
4
|
+
|
|
5
|
+
const KB = 1024
|
|
6
|
+
const MB = KB * 1024
|
|
7
|
+
const CACHE_SZ = 32 * KB
|
|
8
|
+
const CHUNK_SZ = 128 * MB
|
|
9
|
+
const NOTIFY_SZ = 512 * MB
|
|
10
|
+
const log = console.log
|
|
11
|
+
|
|
12
|
+
export default class KV {
|
|
13
|
+
constructor(ar) {
|
|
14
|
+
this.kv = function WeaveDrive(mod, FS) {
|
|
15
|
+
return {
|
|
16
|
+
async create(id) {
|
|
17
|
+
let properties = { isDevice: false, contents: null }
|
|
18
|
+
if (!FS.analyzePath("/data/").exists) FS.mkdir("/data/")
|
|
19
|
+
let node = FS.createFile("/", "data/" + id, properties, true, false)
|
|
20
|
+
// Set initial parameters
|
|
21
|
+
const bytesLength = (await ar.data(id))?.length ?? 0
|
|
22
|
+
node.total_size = Number(bytesLength)
|
|
23
|
+
node.cache = new Uint8Array(0)
|
|
24
|
+
node.position = 0
|
|
25
|
+
|
|
26
|
+
// Add a function that defers querying the file size until it is asked the first time.
|
|
27
|
+
Object.defineProperties(node, {
|
|
28
|
+
usedBytes: {
|
|
29
|
+
get: () => bytesLength,
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
// Now we have created the file in the emscripten FS, we can open it as a stream
|
|
34
|
+
let stream = FS.open("/data/" + id, "r")
|
|
35
|
+
|
|
36
|
+
//console.log("JS: Created file: ", id, " fd: ", stream.fd);
|
|
37
|
+
return stream
|
|
38
|
+
},
|
|
39
|
+
async open(filename) {
|
|
40
|
+
const pathCategory = filename.split("/")[1]
|
|
41
|
+
const id = filename.split("/")[2]
|
|
42
|
+
//log("JS: Opening ID: ", id)
|
|
43
|
+
if (pathCategory === "data") {
|
|
44
|
+
if (FS.analyzePath(filename).exists) {
|
|
45
|
+
let stream = FS.open(filename, "r")
|
|
46
|
+
if (stream.fd) return stream.fd
|
|
47
|
+
console.log("JS: File not found: ", filename)
|
|
48
|
+
return 0
|
|
49
|
+
} else {
|
|
50
|
+
//console.log("JS: Open => Creating file: ", id);
|
|
51
|
+
const stream = await this.create(id)
|
|
52
|
+
//console.log("JS: Open => Created file: ", id, " fd: ", stream.fd);
|
|
53
|
+
return stream.fd
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
console.log("JS: Invalid path category: ", pathCategory)
|
|
57
|
+
return 0
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
async read(fd, raw_dst_ptr, raw_length) {
|
|
61
|
+
let to_read = Number(raw_length)
|
|
62
|
+
let dst_ptr = Number(raw_dst_ptr)
|
|
63
|
+
let stream = 0
|
|
64
|
+
for (let i = 0; i < FS.streams.length; i++) {
|
|
65
|
+
if (FS.streams[i].fd === fd) stream = FS.streams[i]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Satisfy what we can with the cache first
|
|
69
|
+
let bytes_read = this.readFromCache(stream, dst_ptr, to_read)
|
|
70
|
+
stream.position += bytes_read
|
|
71
|
+
stream.lastReadPosition = stream.position
|
|
72
|
+
dst_ptr += bytes_read
|
|
73
|
+
to_read -= bytes_read
|
|
74
|
+
|
|
75
|
+
// Return if we have satisfied the request
|
|
76
|
+
if (to_read === 0) {
|
|
77
|
+
//console.log("WeaveDrive: Satisfied request with cache. Returning...")
|
|
78
|
+
return bytes_read
|
|
79
|
+
}
|
|
80
|
+
//console.log("WeaveDrive: Read from cache: ", bytes_read, " Remaining to read: ", to_read)
|
|
81
|
+
|
|
82
|
+
const chunk_download_sz = Math.max(to_read, CACHE_SZ)
|
|
83
|
+
const to = Math.min(
|
|
84
|
+
stream.node.total_size,
|
|
85
|
+
stream.position + chunk_download_sz,
|
|
86
|
+
)
|
|
87
|
+
const data = await ar.data(stream.node.name)
|
|
88
|
+
// Extract the Range header to determine the start and end of the requested chunk
|
|
89
|
+
const start = 0
|
|
90
|
+
const end = data.length
|
|
91
|
+
|
|
92
|
+
// Create a ReadableStream for the requested chunk
|
|
93
|
+
const chunk = data.subarray(start, end)
|
|
94
|
+
const response = new Response(
|
|
95
|
+
new ReadableStream({
|
|
96
|
+
start(controller) {
|
|
97
|
+
controller.enqueue(chunk) // Push the chunk to the stream
|
|
98
|
+
controller.close() // Close the stream when done
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
{
|
|
102
|
+
headers: { "Content-Length": chunk.length.toString() },
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
const reader = response.body.getReader()
|
|
106
|
+
let bytes_until_cache = CHUNK_SZ
|
|
107
|
+
let bytes_until_notify = NOTIFY_SZ
|
|
108
|
+
let downloaded_bytes = 0
|
|
109
|
+
let cache_chunks = []
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
while (true) {
|
|
113
|
+
const { done, value: chunk_bytes } = await reader.read()
|
|
114
|
+
if (done) break
|
|
115
|
+
// Update the number of downloaded bytes to be _all_, not just the write length
|
|
116
|
+
downloaded_bytes += chunk_bytes.length
|
|
117
|
+
bytes_until_cache -= chunk_bytes.length
|
|
118
|
+
bytes_until_notify -= chunk_bytes.length
|
|
119
|
+
|
|
120
|
+
// Write bytes from the chunk and update the pointer if necessary
|
|
121
|
+
const write_length = Math.min(chunk_bytes.length, to_read)
|
|
122
|
+
if (write_length > 0) {
|
|
123
|
+
//console.log("WeaveDrive: Writing: ", write_length, " bytes to: ", dst_ptr)
|
|
124
|
+
mod.HEAP8.set(chunk_bytes.subarray(0, write_length), dst_ptr)
|
|
125
|
+
dst_ptr += write_length
|
|
126
|
+
bytes_read += write_length
|
|
127
|
+
stream.position += write_length
|
|
128
|
+
to_read -= write_length
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (to_read == 0) {
|
|
132
|
+
// Add excess bytes to our cache
|
|
133
|
+
const chunk_to_cache = chunk_bytes.subarray(write_length)
|
|
134
|
+
//console.log("WeaveDrive: Cacheing excess: ", chunk_to_cache.length)
|
|
135
|
+
cache_chunks.push(chunk_to_cache)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (bytes_until_cache <= 0) {
|
|
139
|
+
console.log(
|
|
140
|
+
"WeaveDrive: Chunk size reached. Compressing cache...",
|
|
141
|
+
)
|
|
142
|
+
stream.node.cache = this.addChunksToCache(
|
|
143
|
+
stream.node.cache,
|
|
144
|
+
cache_chunks,
|
|
145
|
+
)
|
|
146
|
+
cache_chunks = []
|
|
147
|
+
bytes_until_cache = CHUNK_SZ
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (bytes_until_notify <= 0) {
|
|
151
|
+
console.log(
|
|
152
|
+
"WeaveDrive: Downloaded: ",
|
|
153
|
+
(downloaded_bytes / stream.node.total_size) * 100,
|
|
154
|
+
"%",
|
|
155
|
+
)
|
|
156
|
+
bytes_until_notify = NOTIFY_SZ
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} catch (error) {
|
|
160
|
+
console.error("WeaveDrive: Error reading the stream: ", error)
|
|
161
|
+
} finally {
|
|
162
|
+
reader.releaseLock()
|
|
163
|
+
}
|
|
164
|
+
// If we have no cache, or we have not satisfied the full request, we need to download the rest
|
|
165
|
+
// Rebuild the cache from the new cache chunks
|
|
166
|
+
stream.node.cache = this.addChunksToCache(
|
|
167
|
+
stream.node.cache,
|
|
168
|
+
cache_chunks,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
// Update the last read position
|
|
172
|
+
stream.lastReadPosition = stream.position
|
|
173
|
+
return bytes_read
|
|
174
|
+
},
|
|
175
|
+
close(fd) {
|
|
176
|
+
let stream = 0
|
|
177
|
+
for (let i = 0; i < FS.streams.length; i++) {
|
|
178
|
+
if (FS.streams[i].fd === fd) stream = FS.streams[i]
|
|
179
|
+
}
|
|
180
|
+
FS.close(stream)
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
readFromCache(stream, dst_ptr, length) {
|
|
184
|
+
// Check if the cache has been invalidated by a seek
|
|
185
|
+
if (stream.lastReadPosition !== stream.position) {
|
|
186
|
+
//console.log("WeaveDrive: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.position, " Last read pos: ", stream.lastReadPosition)
|
|
187
|
+
stream.node.cache = new Uint8Array(0)
|
|
188
|
+
return 0
|
|
189
|
+
}
|
|
190
|
+
// Calculate the bytes of the request that can be satisfied with the cache
|
|
191
|
+
let cache_part_length = Math.min(length, stream.node.cache.length)
|
|
192
|
+
let cache_part = stream.node.cache.subarray(0, cache_part_length)
|
|
193
|
+
mod.HEAP8.set(cache_part, dst_ptr)
|
|
194
|
+
// Set the new cache to the remainder of the unused cache and update pointers
|
|
195
|
+
stream.node.cache = stream.node.cache.subarray(cache_part_length)
|
|
196
|
+
|
|
197
|
+
return cache_part_length
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
addChunksToCache(old_cache, chunks) {
|
|
201
|
+
// Make a new cache array of the old cache length + the sum of the chunk lengths, capped by the max cache size
|
|
202
|
+
let new_cache_length = Math.min(
|
|
203
|
+
old_cache.length +
|
|
204
|
+
chunks.reduce((acc, chunk) => acc + chunk.length, 0),
|
|
205
|
+
CACHE_SZ,
|
|
206
|
+
)
|
|
207
|
+
let new_cache = new Uint8Array(new_cache_length)
|
|
208
|
+
// Copy the old cache to the new cache
|
|
209
|
+
new_cache.set(old_cache, 0)
|
|
210
|
+
// Load the cache chunks into the new cache
|
|
211
|
+
let current_offset = old_cache.length
|
|
212
|
+
for (let chunk of chunks) {
|
|
213
|
+
if (current_offset < new_cache_length) {
|
|
214
|
+
new_cache.set(
|
|
215
|
+
chunk.subarray(0, new_cache_length - current_offset),
|
|
216
|
+
current_offset,
|
|
217
|
+
)
|
|
218
|
+
current_offset += chunk.length
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return new_cache
|
|
222
|
+
},
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
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,
|