wao 0.2.2 → 0.2.4
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 +23 -13
- package/cjs/aoconnect.js +554 -346
- package/cjs/ar.js +63 -60
- package/cjs/helpers.js +6 -27
- package/cjs/tao.js +5 -1
- package/cjs/test.js +32 -0
- package/esm/ao.js +22 -4
- package/esm/aoconnect.js +196 -101
- package/esm/ar.js +2 -0
- package/esm/helpers.js +1 -4
- package/esm/tao.js +5 -1
- package/esm/test.js +3 -0
- package/package.json +1 -1
package/esm/aoconnect.js
CHANGED
|
@@ -3,9 +3,9 @@ import base64url from "base64url"
|
|
|
3
3
|
import AoLoader from "@permaweb/ao-loader"
|
|
4
4
|
import { readFileSync } from "fs"
|
|
5
5
|
import { resolve } from "path"
|
|
6
|
-
import { is, clone, fromPairs, map, mergeLeft } from "ramda"
|
|
6
|
+
import { is, clone, fromPairs, map, mergeLeft, isNil } from "ramda"
|
|
7
7
|
import accounts from "./accounts.js"
|
|
8
|
-
|
|
8
|
+
import { tags, action, tag, buildTags } from "./utils.js"
|
|
9
9
|
function isJSON(obj) {
|
|
10
10
|
if (obj === null || obj === undefined) return false
|
|
11
11
|
if (
|
|
@@ -35,19 +35,10 @@ const dirname = async () =>
|
|
|
35
35
|
? __dirname
|
|
36
36
|
: (await import("./dirname.js")).default
|
|
37
37
|
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
const buildTags = (act, tags) => {
|
|
43
|
-
let _tags = []
|
|
44
|
-
if (act) _tags.push(action(act))
|
|
45
|
-
for (const k in tags) {
|
|
46
|
-
if (is(Array)(tags[k])) for (const v of tags[k]) _tags.push(tag(k, v))
|
|
47
|
-
else _tags.push(tag(k, tags[k]))
|
|
48
|
-
}
|
|
49
|
-
return _tags
|
|
50
|
-
}
|
|
38
|
+
export const acc = accounts.users
|
|
39
|
+
export const mu = accounts.mu
|
|
40
|
+
const scheduler = "GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA"
|
|
41
|
+
let env = {}
|
|
51
42
|
|
|
52
43
|
export const connect = () => {
|
|
53
44
|
let wasms = {
|
|
@@ -65,13 +56,12 @@ export const connect = () => {
|
|
|
65
56
|
},
|
|
66
57
|
}
|
|
67
58
|
let modules = {
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
aos2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
|
|
60
|
+
aos1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
|
|
70
61
|
sqlite: "ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw",
|
|
71
62
|
}
|
|
72
63
|
let modmap = {}
|
|
73
|
-
let
|
|
74
|
-
let mu = accounts.mu
|
|
64
|
+
let msgs = {}
|
|
75
65
|
const transform = input => {
|
|
76
66
|
const output = { Tags: [] }
|
|
77
67
|
if (input.Data) output.Data = input.Data
|
|
@@ -158,12 +148,148 @@ export const connect = () => {
|
|
|
158
148
|
return id
|
|
159
149
|
}
|
|
160
150
|
|
|
151
|
+
const spawn = async (opt = {}) => {
|
|
152
|
+
if (!opt.module) throw Error("module missing")
|
|
153
|
+
if (!opt.scheduler) throw Error("scheduler missing")
|
|
154
|
+
let mod = opt.module ?? modules["aos2_0_1"]
|
|
155
|
+
if (!modmap[mod] && wasms[mod]) {
|
|
156
|
+
const __dirname = await dirname()
|
|
157
|
+
const wasm = readFileSync(
|
|
158
|
+
resolve(__dirname, `lua/${wasms[mod].file}.wasm`),
|
|
159
|
+
)
|
|
160
|
+
const handle = await AoLoader(wasm, { format: wasms[mod].format })
|
|
161
|
+
modmap[mod] = { handle, id: mod }
|
|
162
|
+
}
|
|
163
|
+
if (!mod) throw Error("module not found")
|
|
164
|
+
const _module = modmap[mod]
|
|
165
|
+
let ex = false
|
|
166
|
+
opt.tags ??= []
|
|
167
|
+
for (let v of opt.tags) if (v.name === "Type") ex = true
|
|
168
|
+
if (!ex) opt.tags.push({ name: "Type", value: "Process" })
|
|
169
|
+
const { id, owner } = await parse(opt)
|
|
170
|
+
const _tags = tags(opt.tags)
|
|
171
|
+
let res = null
|
|
172
|
+
let memory = null
|
|
173
|
+
let p = {
|
|
174
|
+
id: id,
|
|
175
|
+
handle: _module.handle,
|
|
176
|
+
module: _module.id,
|
|
177
|
+
memory,
|
|
178
|
+
owner,
|
|
179
|
+
height: 0,
|
|
180
|
+
res: { [id]: res },
|
|
181
|
+
results: [id],
|
|
182
|
+
txs: [],
|
|
183
|
+
opt,
|
|
184
|
+
}
|
|
185
|
+
if (_tags["On-Boot"]) {
|
|
186
|
+
let data = ""
|
|
187
|
+
if (_tags["On-Boot"] === "Data") data = opt.data ?? ""
|
|
188
|
+
else data = msgs[_tags["On-Boot"]]?.data ?? ""
|
|
189
|
+
let msg = genMsg(p, data, opt.tags, owner, mu.addr, true)
|
|
190
|
+
const _env = genEnv({
|
|
191
|
+
pid: p.id,
|
|
192
|
+
owner: p.owner,
|
|
193
|
+
module: p.module,
|
|
194
|
+
auth: mu.addr,
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
const _t = tags(msg.Tags)
|
|
198
|
+
res = await _module.handle(null, msg, _env)
|
|
199
|
+
p.memory = res.Memory
|
|
200
|
+
delete res.Memory
|
|
201
|
+
p.res[id] = res
|
|
202
|
+
} else {
|
|
203
|
+
p.height += 1
|
|
204
|
+
}
|
|
205
|
+
msgs[id] = opt
|
|
206
|
+
env[id] = p
|
|
207
|
+
if (_tags["Cron-Interval"]) {
|
|
208
|
+
let [num, unit] = _tags["Cron-Interval"].split("-")
|
|
209
|
+
let int = 0
|
|
210
|
+
switch (unit.replace(/s$/, "")) {
|
|
211
|
+
case "millisecond":
|
|
212
|
+
int = num
|
|
213
|
+
break
|
|
214
|
+
case "second":
|
|
215
|
+
int = num * 1000
|
|
216
|
+
break
|
|
217
|
+
case "minute":
|
|
218
|
+
int = num * 1000 * 60
|
|
219
|
+
break
|
|
220
|
+
case "hour":
|
|
221
|
+
int = num * 1000 * 60 * 60
|
|
222
|
+
break
|
|
223
|
+
case "day":
|
|
224
|
+
int = num * 1000 * 60 * 60 * 24
|
|
225
|
+
break
|
|
226
|
+
case "month":
|
|
227
|
+
int = num * 1000 * 60 * 60 * 24 * 30
|
|
228
|
+
break
|
|
229
|
+
case "year":
|
|
230
|
+
int = num * 1000 * 60 * 60 * 24 * 365
|
|
231
|
+
break
|
|
232
|
+
}
|
|
233
|
+
let cronTags = []
|
|
234
|
+
for (const k in _tags) {
|
|
235
|
+
if (/^Cron-Tag-/.test(k)) {
|
|
236
|
+
cronTags.push({ name: k.replace(/Cron-Tag-/, ""), value: _tags[k] })
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
env[id].cronTags = cronTags
|
|
240
|
+
env[id].span = int
|
|
241
|
+
}
|
|
242
|
+
return id
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const assign = async opt => {
|
|
246
|
+
const p = env[opt.process]
|
|
247
|
+
let _opt = clone(msgs[opt.message])
|
|
248
|
+
const { id, owner } = await parse(_opt)
|
|
249
|
+
try {
|
|
250
|
+
const msg = genMsg(
|
|
251
|
+
p,
|
|
252
|
+
_opt.data ?? "",
|
|
253
|
+
_opt.tags,
|
|
254
|
+
_opt.from ?? owner,
|
|
255
|
+
mu.addr,
|
|
256
|
+
)
|
|
257
|
+
const _env = genEnv({
|
|
258
|
+
pid: p.id,
|
|
259
|
+
owner: p.owner,
|
|
260
|
+
module: p.module,
|
|
261
|
+
auth: mu.addr,
|
|
262
|
+
})
|
|
263
|
+
const res = await p.handle(p.memory, msg, _env)
|
|
264
|
+
p.memory = res.Memory
|
|
265
|
+
delete res.Memory
|
|
266
|
+
p.res[id] = res
|
|
267
|
+
p.results.push(id)
|
|
268
|
+
p.txs.unshift({ id: id, ..._opt })
|
|
269
|
+
msgs[id] = _opt
|
|
270
|
+
for (const v of res.Messages ?? []) {
|
|
271
|
+
if (env[v.Target]) {
|
|
272
|
+
await message({
|
|
273
|
+
process: v.Target,
|
|
274
|
+
tags: v.Tags,
|
|
275
|
+
data: v.Data,
|
|
276
|
+
signer: mu.signer,
|
|
277
|
+
from: opt.process,
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return id
|
|
282
|
+
} catch (e) {
|
|
283
|
+
console.log(e)
|
|
284
|
+
}
|
|
285
|
+
return null
|
|
286
|
+
}
|
|
287
|
+
|
|
161
288
|
const message = async opt => {
|
|
162
289
|
const p = env[opt.process]
|
|
163
290
|
let ex = false
|
|
164
|
-
for (let v of opt.tags)
|
|
165
|
-
|
|
166
|
-
}
|
|
291
|
+
for (let v of opt.tags) if (v.name === "Type") ex = true
|
|
292
|
+
|
|
167
293
|
if (!ex) opt.tags.push({ name: "Type", value: "Message" })
|
|
168
294
|
const { id, owner } = await parse(opt)
|
|
169
295
|
try {
|
|
@@ -186,7 +312,7 @@ export const connect = () => {
|
|
|
186
312
|
p.res[id] = res
|
|
187
313
|
p.results.push(id)
|
|
188
314
|
p.txs.unshift({ id: id, ...opt })
|
|
189
|
-
|
|
315
|
+
msgs[id] = opt
|
|
190
316
|
for (const v of res.Messages ?? []) {
|
|
191
317
|
if (env[v.Target]) {
|
|
192
318
|
await message({
|
|
@@ -198,98 +324,66 @@ export const connect = () => {
|
|
|
198
324
|
})
|
|
199
325
|
}
|
|
200
326
|
}
|
|
327
|
+
for (const v of res.Spawns ?? []) {
|
|
328
|
+
const _tags = tags(v.Tags)
|
|
329
|
+
await spawn({
|
|
330
|
+
module: _tags.Module,
|
|
331
|
+
scheduler,
|
|
332
|
+
tags: v.Tags,
|
|
333
|
+
data: v.Data,
|
|
334
|
+
from: _tags["From-Process"],
|
|
335
|
+
signer: mu.signer,
|
|
336
|
+
})
|
|
337
|
+
}
|
|
338
|
+
for (const v of res.Assignments ?? []) {
|
|
339
|
+
for (const v2 of v.Processes) {
|
|
340
|
+
await assign({
|
|
341
|
+
message: v.Message,
|
|
342
|
+
process: v2,
|
|
343
|
+
from: opt.process,
|
|
344
|
+
signer: mu.signer,
|
|
345
|
+
})
|
|
346
|
+
}
|
|
347
|
+
}
|
|
201
348
|
return id
|
|
202
349
|
} catch (e) {
|
|
203
350
|
console.log(e)
|
|
204
351
|
}
|
|
205
352
|
return null
|
|
206
353
|
}
|
|
207
|
-
|
|
208
354
|
return {
|
|
209
|
-
scheduler
|
|
355
|
+
scheduler,
|
|
210
356
|
modules,
|
|
211
|
-
accounts:
|
|
357
|
+
accounts: acc,
|
|
358
|
+
mu,
|
|
212
359
|
message,
|
|
360
|
+
unmonitor: async opt => {
|
|
361
|
+
const p = env[opt.process]
|
|
362
|
+
try {
|
|
363
|
+
clearInterval(p.cron)
|
|
364
|
+
p.cron = null
|
|
365
|
+
} catch (e) {}
|
|
366
|
+
},
|
|
367
|
+
monitor: async opt => {
|
|
368
|
+
const p = env[opt.process]
|
|
369
|
+
if (isNil(p.cron)) {
|
|
370
|
+
p.cron = setInterval(async () => {
|
|
371
|
+
await message({
|
|
372
|
+
tags: p.cronTags,
|
|
373
|
+
process: opt.process,
|
|
374
|
+
signer: mu.signer,
|
|
375
|
+
from: mu.addr,
|
|
376
|
+
})
|
|
377
|
+
}, p.span)
|
|
378
|
+
}
|
|
379
|
+
},
|
|
213
380
|
txs: async pid => {
|
|
214
381
|
let _txs = []
|
|
215
382
|
for (let v of env[pid].txs) _txs.push({ tags: v.tags, id: v.id })
|
|
216
383
|
return _txs
|
|
217
384
|
},
|
|
218
|
-
spawn
|
|
219
|
-
|
|
220
|
-
if (!opt.scheduler) throw Error("scheduler.missing")
|
|
221
|
-
let mod = opt.module ?? modules["aos_2_0_1"]
|
|
222
|
-
if (!modmap[mod] && wasms[mod]) {
|
|
223
|
-
const __dirname = await dirname()
|
|
224
|
-
const wasm = readFileSync(
|
|
225
|
-
resolve(__dirname, `lua/${wasms[mod].file}.wasm`),
|
|
226
|
-
)
|
|
227
|
-
const handle = await AoLoader(wasm, { format: wasms[mod].format })
|
|
228
|
-
modmap[mod] = { handle, id: mod }
|
|
229
|
-
}
|
|
230
|
-
if (!mod) throw Error("module not found")
|
|
231
|
-
const _module = modmap[mod]
|
|
232
|
-
let ex = false
|
|
233
|
-
opt.tags ??= []
|
|
234
|
-
for (let v of opt.tags) if (v.name === "Type") ex = true
|
|
235
|
-
if (!ex) opt.tags.push({ name: "Type", value: "Process" })
|
|
236
|
-
const { id, owner } = await parse(opt)
|
|
237
|
-
env.msgs[id] = opt
|
|
238
|
-
env[id] = {
|
|
239
|
-
id: id,
|
|
240
|
-
handle: _module.handle,
|
|
241
|
-
module: _module.id,
|
|
242
|
-
memory: null,
|
|
243
|
-
owner,
|
|
244
|
-
height: 1,
|
|
245
|
-
res: { id: null },
|
|
246
|
-
results: [id],
|
|
247
|
-
txs: [],
|
|
248
|
-
}
|
|
249
|
-
return id
|
|
250
|
-
},
|
|
251
|
-
assign: async opt => {
|
|
252
|
-
const p = env[opt.process]
|
|
253
|
-
let _opt = clone(env.msgs[opt.message])
|
|
254
|
-
const { id, owner } = await parse(_opt)
|
|
255
|
-
try {
|
|
256
|
-
const msg = genMsg(
|
|
257
|
-
p,
|
|
258
|
-
_opt.data ?? "",
|
|
259
|
-
_opt.tags,
|
|
260
|
-
_opt.from ?? owner,
|
|
261
|
-
mu.addr,
|
|
262
|
-
)
|
|
263
|
-
const _env = genEnv({
|
|
264
|
-
pid: p.id,
|
|
265
|
-
owner: p.owner,
|
|
266
|
-
module: p.module,
|
|
267
|
-
auth: mu.addr,
|
|
268
|
-
})
|
|
269
|
-
const res = await p.handle(p.memory, msg, _env)
|
|
270
|
-
p.memory = res.Memory
|
|
271
|
-
delete res.Memory
|
|
272
|
-
p.res[id] = res
|
|
273
|
-
p.results.push(id)
|
|
274
|
-
p.txs.unshift({ id: id, ..._opt })
|
|
275
|
-
env.msgs[id] = _opt
|
|
276
|
-
for (const v of res.Messages ?? []) {
|
|
277
|
-
if (env[v.Target]) {
|
|
278
|
-
await message({
|
|
279
|
-
process: v.Target,
|
|
280
|
-
tags: v.Tags,
|
|
281
|
-
data: v.Data,
|
|
282
|
-
signer: mu.signer,
|
|
283
|
-
from: opt.process,
|
|
284
|
-
})
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return id
|
|
288
|
-
} catch (e) {
|
|
289
|
-
console.log(e)
|
|
290
|
-
}
|
|
291
|
-
return null
|
|
292
|
-
},
|
|
385
|
+
spawn,
|
|
386
|
+
assign,
|
|
293
387
|
result: async opt => env[opt.process].res[opt.message],
|
|
294
388
|
results: async opt => {
|
|
295
389
|
const p = env[opt.process]
|
|
@@ -327,5 +421,6 @@ export const connect = () => {
|
|
|
327
421
|
}
|
|
328
422
|
return null
|
|
329
423
|
},
|
|
424
|
+
getProcesses: () => env,
|
|
330
425
|
}
|
|
331
426
|
}
|
package/esm/ar.js
CHANGED
|
@@ -27,6 +27,7 @@ class AR {
|
|
|
27
27
|
if (!jwk && typeof window === "object") jwk = window.arweaveWallet
|
|
28
28
|
if (!jwk) isGen = true
|
|
29
29
|
else {
|
|
30
|
+
jwk = typeof jwk?.jwk === "object" ? jwk.jwk : jwk
|
|
30
31
|
this.jwk = jwk
|
|
31
32
|
const isWallet = this.isArConnect(this.jwk)
|
|
32
33
|
if (isWallet) {
|
|
@@ -60,6 +61,7 @@ class AR {
|
|
|
60
61
|
}
|
|
61
62
|
|
|
62
63
|
async checkWallet({ jwk } = {}) {
|
|
64
|
+
let start = Date.now()
|
|
63
65
|
if (jwk) return { err: null, jwk }
|
|
64
66
|
let [err, addr, pub] = [null, null, null]
|
|
65
67
|
let existWallet = typeof window === "object" && window.arweaveWallet
|
package/esm/helpers.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { connect as tconnect } from "./aoconnect.js"
|
|
2
1
|
import { AR } from "./index.js"
|
|
3
|
-
import AO from "./tao.js"
|
|
4
2
|
import assert from "assert"
|
|
5
3
|
import { createDataItemSigner, connect } from "@permaweb/aoconnect"
|
|
6
4
|
import { dirname as _dirname, resolve } from "path"
|
|
@@ -110,6 +108,7 @@ export const setup = async ({
|
|
|
110
108
|
url: "http://su",
|
|
111
109
|
overwrite: true,
|
|
112
110
|
})
|
|
111
|
+
|
|
113
112
|
opt.ao = {
|
|
114
113
|
module: module_sqlite,
|
|
115
114
|
scheduler,
|
|
@@ -161,5 +160,3 @@ export const fail = obj => {
|
|
|
161
160
|
assert.notEqual(obj.err, null)
|
|
162
161
|
return obj
|
|
163
162
|
}
|
|
164
|
-
|
|
165
|
-
export { AO, AR, tconnect as connect }
|
package/esm/tao.js
CHANGED
|
@@ -16,9 +16,11 @@ class AO extends MAO {
|
|
|
16
16
|
message,
|
|
17
17
|
spawn,
|
|
18
18
|
dryrun,
|
|
19
|
+
monitor,
|
|
20
|
+
unmonitor,
|
|
19
21
|
txs,
|
|
20
22
|
} = connect()
|
|
21
|
-
this.module = modules.
|
|
23
|
+
this.module = modules.aos2_0_1
|
|
22
24
|
this.assign = assign
|
|
23
25
|
this.result = result
|
|
24
26
|
this.results = results
|
|
@@ -26,6 +28,8 @@ class AO extends MAO {
|
|
|
26
28
|
this.spawn = spawn
|
|
27
29
|
this.dryrun = dryrun
|
|
28
30
|
this.ar.txs = txs
|
|
31
|
+
this.monitor = monitor
|
|
32
|
+
this.unmonitor = unmonitor
|
|
29
33
|
this.accounts = accounts
|
|
30
34
|
}
|
|
31
35
|
|
package/esm/test.js
ADDED