wao 0.1.3 → 0.1.5
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/index.js +0 -6
- package/esm/index.js +1 -2
- package/package.json +9 -4
- package/test/.cache/opt.json +0 -1
- package/test/accounts.js +0 -91
- package/test/ao.js +0 -737
- package/test/aoconnect.js +0 -318
- package/test/ar.js +0 -243
- package/test/dirname.js +0 -1
- package/test/helpers.js +0 -161
- package/test/index.js +0 -5
- package/test/lua/aos2_0_1.wasm +0 -0
- package/test/package.json +0 -31
- package/test/test.js +0 -94
- package/test/utils.js +0 -324
package/test/ao.js
DELETED
|
@@ -1,737 +0,0 @@
|
|
|
1
|
-
import * as WarpArBundles from "warp-arbundles"
|
|
2
|
-
const pkg = WarpArBundles.default ? WarpArBundles.default : WarpArBundles
|
|
3
|
-
const { createData, ArweaveSigner } = pkg
|
|
4
|
-
import AR from "./ar.js"
|
|
5
|
-
import {
|
|
6
|
-
createDataItemSigner,
|
|
7
|
-
connect,
|
|
8
|
-
assign,
|
|
9
|
-
result,
|
|
10
|
-
results,
|
|
11
|
-
message,
|
|
12
|
-
spawn,
|
|
13
|
-
dryrun,
|
|
14
|
-
} from "@permaweb/aoconnect"
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
concat,
|
|
18
|
-
is,
|
|
19
|
-
mergeLeft,
|
|
20
|
-
o,
|
|
21
|
-
uniqBy,
|
|
22
|
-
prop,
|
|
23
|
-
isNil,
|
|
24
|
-
includes,
|
|
25
|
-
map,
|
|
26
|
-
} from "ramda"
|
|
27
|
-
|
|
28
|
-
import {
|
|
29
|
-
searchTag,
|
|
30
|
-
checkTag,
|
|
31
|
-
wait,
|
|
32
|
-
isLocalhost,
|
|
33
|
-
tag,
|
|
34
|
-
ltags,
|
|
35
|
-
action,
|
|
36
|
-
isData,
|
|
37
|
-
getTagVal,
|
|
38
|
-
srcs,
|
|
39
|
-
buildTags,
|
|
40
|
-
isRegExp,
|
|
41
|
-
mergeChecks,
|
|
42
|
-
isCheckComplete,
|
|
43
|
-
mergeOut,
|
|
44
|
-
isOutComplete,
|
|
45
|
-
jsonToStr,
|
|
46
|
-
} from "./utils.js"
|
|
47
|
-
|
|
48
|
-
function createDataItemSigner2(wallet) {
|
|
49
|
-
const signer = async ({ data, tags, target, anchor }) => {
|
|
50
|
-
const signer = new ArweaveSigner(wallet)
|
|
51
|
-
const dataItem = createData(data, signer, { tags, target, anchor })
|
|
52
|
-
return dataItem.sign(signer).then(async () => ({
|
|
53
|
-
id: await dataItem.id,
|
|
54
|
-
raw: await dataItem.getRaw(),
|
|
55
|
-
}))
|
|
56
|
-
}
|
|
57
|
-
return signer
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
class AO {
|
|
61
|
-
constructor({
|
|
62
|
-
authority = srcs.authority,
|
|
63
|
-
module,
|
|
64
|
-
module_type = "aos2",
|
|
65
|
-
scheduler = srcs.scheduler,
|
|
66
|
-
aoconnect,
|
|
67
|
-
in_memory,
|
|
68
|
-
ar = {},
|
|
69
|
-
} = {}) {
|
|
70
|
-
if (!module) {
|
|
71
|
-
switch (module_type) {
|
|
72
|
-
case "sqlite":
|
|
73
|
-
module = srcs.module_sqlite
|
|
74
|
-
break
|
|
75
|
-
case "aos2":
|
|
76
|
-
module = srcs.module_aos2
|
|
77
|
-
break
|
|
78
|
-
default:
|
|
79
|
-
module = srcs.module
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
this.__type__ = "ao"
|
|
83
|
-
|
|
84
|
-
if (ar?.__type__ === "ar") this.ar = ar
|
|
85
|
-
else {
|
|
86
|
-
let _ar = typeof ar === "object" ? ar : {}
|
|
87
|
-
this.ar = new AR(ar)
|
|
88
|
-
}
|
|
89
|
-
this.in_memory = in_memory
|
|
90
|
-
if (aoconnect) {
|
|
91
|
-
if (in_memory) {
|
|
92
|
-
} else {
|
|
93
|
-
const { results, assign, result, message, spawn, dryrun } =
|
|
94
|
-
connect(aoconnect)
|
|
95
|
-
this.assign = assign
|
|
96
|
-
this.result = result
|
|
97
|
-
this.results = results
|
|
98
|
-
this.message = message
|
|
99
|
-
this.spawn = spawn
|
|
100
|
-
this.dryrun = dryrun
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
this.assign = assign
|
|
104
|
-
this.result = result
|
|
105
|
-
this.results = results
|
|
106
|
-
this.message = message
|
|
107
|
-
this.spawn = spawn
|
|
108
|
-
this.dryrun = dryrun
|
|
109
|
-
}
|
|
110
|
-
this.module = module
|
|
111
|
-
this.scheduler = scheduler
|
|
112
|
-
this.authority = authority
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async init(jwk) {
|
|
116
|
-
if (this.in_memory) {
|
|
117
|
-
const v = await import("./aoconnect.js")
|
|
118
|
-
const {
|
|
119
|
-
accounts,
|
|
120
|
-
modules,
|
|
121
|
-
results,
|
|
122
|
-
assign,
|
|
123
|
-
result,
|
|
124
|
-
message,
|
|
125
|
-
spawn,
|
|
126
|
-
dryrun,
|
|
127
|
-
txs,
|
|
128
|
-
} = v.connect()
|
|
129
|
-
this.module = modules.aos_2_0_1
|
|
130
|
-
this.assign = assign
|
|
131
|
-
this.result = result
|
|
132
|
-
this.results = results
|
|
133
|
-
this.message = message
|
|
134
|
-
this.spawn = spawn
|
|
135
|
-
this.dryrun = dryrun
|
|
136
|
-
this.ar.txs = txs
|
|
137
|
-
jwk ??= accounts[0].jwk
|
|
138
|
-
}
|
|
139
|
-
await this.ar.init(jwk)
|
|
140
|
-
return this
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
toSigner(wallet) {
|
|
144
|
-
if (wallet?.n && typeof window !== "undefined") {
|
|
145
|
-
return createDataItemSigner2(wallet)
|
|
146
|
-
} else if (wallet.test) {
|
|
147
|
-
return createDataItemSigner(wallet.jwk)
|
|
148
|
-
} else {
|
|
149
|
-
return createDataItemSigner(wallet)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async pipe({ jwk, fns = [], cb }) {
|
|
154
|
-
let [_, out, res, i] = [{}, {}, [], 0]
|
|
155
|
-
let nextArgs = fns[0].args ?? {}
|
|
156
|
-
let [ret, err, pid, mid, id] = [null, null, null, null, null]
|
|
157
|
-
if (!jwk) fns.unshift({ fn: "checkWallet" })
|
|
158
|
-
const copy = ({ _, inp, out, args, from, to, pid, mid, id }) => {
|
|
159
|
-
const _from = from.split(".")
|
|
160
|
-
const _to = to.split(".")
|
|
161
|
-
let [field, val, target] = [null, null, null]
|
|
162
|
-
if (_to[0] === "_") {
|
|
163
|
-
target = _
|
|
164
|
-
field = _to.slice(1).join(".")
|
|
165
|
-
} else if (_to[0] === "args") {
|
|
166
|
-
target = args
|
|
167
|
-
field = _to.slice(1).join(".")
|
|
168
|
-
} else if (_to[0] === "out") {
|
|
169
|
-
target = out
|
|
170
|
-
field = _to.slice(1).join(".")
|
|
171
|
-
} else {
|
|
172
|
-
target = out
|
|
173
|
-
field = _to.join(".")
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (from === "inp") val = inp
|
|
177
|
-
else if (from === "mid") val = mid
|
|
178
|
-
else if (from === "id") val = id
|
|
179
|
-
else if (from === "pid") val = pid
|
|
180
|
-
else if (_from[0] === "args") val = args[_from.slice(1).join(".")] ?? null
|
|
181
|
-
else if (_from[0] === "out") val = out[_from.slice(1).join(".")] ?? null
|
|
182
|
-
else if (_from[0] === "inp") val = inp[_from.slice(1).join(".")] ?? null
|
|
183
|
-
else val = inp[from] ?? null
|
|
184
|
-
|
|
185
|
-
if (target) target[field] = val
|
|
186
|
-
}
|
|
187
|
-
const binds = {
|
|
188
|
-
post: this.ar,
|
|
189
|
-
bundle: this.ar,
|
|
190
|
-
postTx: this.ar,
|
|
191
|
-
checkWallet: this.ar,
|
|
192
|
-
transfer: this.ar,
|
|
193
|
-
}
|
|
194
|
-
for (let v of fns) {
|
|
195
|
-
let bind = null
|
|
196
|
-
if (typeof v.fn === "string" && !v.bind) bind = binds[v.fn] ?? this
|
|
197
|
-
else bind = v.bind ?? this
|
|
198
|
-
|
|
199
|
-
const name =
|
|
200
|
-
(v.name ?? typeof !v.fn)
|
|
201
|
-
? "msg"
|
|
202
|
-
: typeof v.fn === "string"
|
|
203
|
-
? v.fn
|
|
204
|
-
: null
|
|
205
|
-
const fn = typeof v.fn === "string" ? bind[v.fn] : (v.fn ?? this.msg)
|
|
206
|
-
|
|
207
|
-
const _fn = fn.bind(bind)
|
|
208
|
-
const _res = await _fn({ jwk, ...nextArgs })
|
|
209
|
-
res.push(_res)
|
|
210
|
-
if (_res.err) {
|
|
211
|
-
err = _res.err
|
|
212
|
-
break
|
|
213
|
-
}
|
|
214
|
-
if (typeof v.err === "function") {
|
|
215
|
-
const _err = await v.err({
|
|
216
|
-
name,
|
|
217
|
-
_,
|
|
218
|
-
jwk,
|
|
219
|
-
res: _res.res,
|
|
220
|
-
args: nextArgs,
|
|
221
|
-
out,
|
|
222
|
-
pid,
|
|
223
|
-
inp: _res.out,
|
|
224
|
-
mid,
|
|
225
|
-
id,
|
|
226
|
-
})
|
|
227
|
-
if (_err) {
|
|
228
|
-
err = _err
|
|
229
|
-
break
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
if (_res.pid) pid = _res.pid
|
|
233
|
-
if (_res.mid) mid = _res.mid
|
|
234
|
-
if (_res.id) id = _res.id
|
|
235
|
-
if (_res.jwk) jwk = _res.jwk
|
|
236
|
-
nextArgs = fns[i + 1]?.args ?? {}
|
|
237
|
-
if (typeof v.then === "function") {
|
|
238
|
-
const _ret = await v.then({
|
|
239
|
-
name,
|
|
240
|
-
_,
|
|
241
|
-
jwk,
|
|
242
|
-
res: _res.res,
|
|
243
|
-
args: nextArgs,
|
|
244
|
-
out,
|
|
245
|
-
pid,
|
|
246
|
-
inp: _res.out,
|
|
247
|
-
mid,
|
|
248
|
-
id,
|
|
249
|
-
})
|
|
250
|
-
if (typeof _ret !== "undefined") {
|
|
251
|
-
ret = _ret
|
|
252
|
-
break
|
|
253
|
-
}
|
|
254
|
-
} else if (is(Object, v.then)) {
|
|
255
|
-
for (const k in v.then) {
|
|
256
|
-
copy({
|
|
257
|
-
name,
|
|
258
|
-
_,
|
|
259
|
-
inp: _res.out,
|
|
260
|
-
out,
|
|
261
|
-
args: nextArgs,
|
|
262
|
-
pid,
|
|
263
|
-
mid,
|
|
264
|
-
from: v.then[k],
|
|
265
|
-
to: k,
|
|
266
|
-
id,
|
|
267
|
-
})
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
if (typeof cb === "function") {
|
|
271
|
-
cb({
|
|
272
|
-
name,
|
|
273
|
-
fns,
|
|
274
|
-
i: i + 1,
|
|
275
|
-
_,
|
|
276
|
-
jwk,
|
|
277
|
-
res: _res.res,
|
|
278
|
-
args: nextArgs,
|
|
279
|
-
out,
|
|
280
|
-
pid,
|
|
281
|
-
inp: _res.out,
|
|
282
|
-
mid,
|
|
283
|
-
id,
|
|
284
|
-
})
|
|
285
|
-
}
|
|
286
|
-
i++
|
|
287
|
-
}
|
|
288
|
-
return ret ?? { jwk, err, res, out, pid, mid, id, ..._ }
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
async postModule({ data, jwk, tags = {}, overwrite = false }) {
|
|
292
|
-
const _tags = mergeLeft(tags, {
|
|
293
|
-
"Data-Protocol": "ao",
|
|
294
|
-
Variant: "ao.TN.1",
|
|
295
|
-
Type: "Module",
|
|
296
|
-
"Module-Format": "wasm64-unknown-emscripten-draft_2024_02_15",
|
|
297
|
-
"Input-Encoding": "JSON-V1",
|
|
298
|
-
"Output-Encoding": "JSON-V1",
|
|
299
|
-
"Memory-Limit": "1-gb",
|
|
300
|
-
"Compute-Limit": "9000000000000",
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
const fns = [
|
|
304
|
-
{
|
|
305
|
-
fn: "post",
|
|
306
|
-
args: { data, tags: _tags },
|
|
307
|
-
then: ({ id }) => {
|
|
308
|
-
if (!this.module || overwrite) this.module = id
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
]
|
|
312
|
-
return await this.pipe({ jwk, fns })
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
async postScheduler({ jwk, url, tags = {}, overwrite = false }) {
|
|
316
|
-
const _tags = mergeLeft(tags, {
|
|
317
|
-
"Data-Protocol": "ao",
|
|
318
|
-
Variant: "ao.TN.1",
|
|
319
|
-
Type: "Scheduler-Location",
|
|
320
|
-
Url: url,
|
|
321
|
-
"Time-To-Live": "3600000",
|
|
322
|
-
})
|
|
323
|
-
const then = async ({ jwk, out, _ }) => {
|
|
324
|
-
_.scheduler = await this.ar.toAddr(jwk)
|
|
325
|
-
if (!this.scheduler || overwrite) this.scheduler = _.scheduler
|
|
326
|
-
}
|
|
327
|
-
const fns = [{ fn: "post", args: { tags: _tags }, then }]
|
|
328
|
-
return await this.pipe({ jwk, fns })
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
async spwn({
|
|
332
|
-
boot,
|
|
333
|
-
module = this.module,
|
|
334
|
-
scheduler = this.scheduler,
|
|
335
|
-
jwk,
|
|
336
|
-
tags = {},
|
|
337
|
-
data,
|
|
338
|
-
auth,
|
|
339
|
-
} = {}) {
|
|
340
|
-
let err = null
|
|
341
|
-
;({ jwk, err } = await this.ar.checkWallet({ jwk }))
|
|
342
|
-
if (err) return { err }
|
|
343
|
-
let pid = null
|
|
344
|
-
try {
|
|
345
|
-
if (boot) tags["On-Boot"] = boot
|
|
346
|
-
if (auth) tags.Authority = auth
|
|
347
|
-
if (!tags.Authority && this.authority) tags.Authority = this.authority
|
|
348
|
-
let _tags = buildTags(null, tags)
|
|
349
|
-
pid = await this.spawn({
|
|
350
|
-
module,
|
|
351
|
-
scheduler,
|
|
352
|
-
signer: this.toSigner(jwk),
|
|
353
|
-
tags: _tags,
|
|
354
|
-
data: jsonToStr(data),
|
|
355
|
-
})
|
|
356
|
-
} catch (e) {
|
|
357
|
-
err = e
|
|
358
|
-
}
|
|
359
|
-
return { err, pid }
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
async msg({
|
|
363
|
-
pid,
|
|
364
|
-
jwk,
|
|
365
|
-
data,
|
|
366
|
-
act = "Eval",
|
|
367
|
-
tags = {},
|
|
368
|
-
check = [],
|
|
369
|
-
get,
|
|
370
|
-
timeout = 10000,
|
|
371
|
-
}) {
|
|
372
|
-
let err = null
|
|
373
|
-
;({ jwk, err } = await this.ar.checkWallet({ jwk }))
|
|
374
|
-
if (err) return { err }
|
|
375
|
-
let [res, out, mid, results] = [null, null, null, []]
|
|
376
|
-
let _tags = buildTags(act, tags)
|
|
377
|
-
let start = Date.now()
|
|
378
|
-
try {
|
|
379
|
-
mid = await this.message({
|
|
380
|
-
process: pid,
|
|
381
|
-
signer: this.toSigner(jwk),
|
|
382
|
-
tags: _tags,
|
|
383
|
-
data: jsonToStr(data),
|
|
384
|
-
})
|
|
385
|
-
|
|
386
|
-
const exRef = (ref, txs) => {
|
|
387
|
-
for (const v2 of txs ?? []) {
|
|
388
|
-
const t = ltags(v2.tags)
|
|
389
|
-
if (t.type === "Message" && t["x-reference"] === ref) return true
|
|
390
|
-
}
|
|
391
|
-
return false
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
const getRef = async (ref, txs = []) => {
|
|
395
|
-
let ex = exRef(ref, txs)
|
|
396
|
-
if (!ex) {
|
|
397
|
-
if (!this.local) await wait(1000)
|
|
398
|
-
txs = await this.ar.txs(pid)
|
|
399
|
-
ex = exRef(ref, txs)
|
|
400
|
-
}
|
|
401
|
-
if (ex) return txs
|
|
402
|
-
if (this.local) await wait(1)
|
|
403
|
-
return Date.now() - start < (this.local ? timeout / 1000 : timeout)
|
|
404
|
-
? await getRef(ref)
|
|
405
|
-
: []
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
let [cache, checks, isOK] = [[], [], false]
|
|
409
|
-
const getResult = async mid => {
|
|
410
|
-
const res = await this.result({ process: pid, message: mid })
|
|
411
|
-
results.push({ mid, res })
|
|
412
|
-
let err = null
|
|
413
|
-
if (res.Error) err = res.Error
|
|
414
|
-
else {
|
|
415
|
-
if (!is(Array, check)) check = [check]
|
|
416
|
-
let i = 0
|
|
417
|
-
for (const v of check) {
|
|
418
|
-
let _checks = checks[i] ?? null
|
|
419
|
-
if (isRegExp(v) || includes(typeof v)(["string", "boolean"])) {
|
|
420
|
-
_checks = mergeChecks(_checks, isData(v, res), v)
|
|
421
|
-
} else {
|
|
422
|
-
const checks2 = {}
|
|
423
|
-
for (const k in v ?? {}) {
|
|
424
|
-
checks2[k] = checkTag(res, k, v[k])
|
|
425
|
-
}
|
|
426
|
-
_checks = mergeChecks(_checks, checks2, v)
|
|
427
|
-
}
|
|
428
|
-
checks[i] = _checks
|
|
429
|
-
i++
|
|
430
|
-
}
|
|
431
|
-
if (isCheckComplete(checks, check)) isOK = true
|
|
432
|
-
if (!isNil(get) && !isOutComplete(out, get)) {
|
|
433
|
-
out = mergeOut(out, getTagVal(get, res), get)
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
if ((!isOutComplete(out, get) || !isOK) && !err) {
|
|
437
|
-
let refs = []
|
|
438
|
-
for (const v of res.Messages) {
|
|
439
|
-
const _ltags = ltags(v.Tags)
|
|
440
|
-
if (_ltags.type === "Message" && _ltags.reference) {
|
|
441
|
-
refs.push(_ltags.reference)
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
for (const v of res.Messages) {
|
|
446
|
-
const _ltags = ltags(v.Tags)
|
|
447
|
-
if (_ltags.type === "Message" && _ltags.reference) {
|
|
448
|
-
const txs = await getRef(_ltags.reference, cache)
|
|
449
|
-
cache = o(uniqBy(prop("id")), concat(cache))(txs)
|
|
450
|
-
for (const v2 of txs) {
|
|
451
|
-
const _ltags2 = ltags(v2.tags)
|
|
452
|
-
if (
|
|
453
|
-
_ltags2.type === "Message" &&
|
|
454
|
-
_ltags2["x-reference"] === _ltags.reference
|
|
455
|
-
) {
|
|
456
|
-
const {
|
|
457
|
-
res: _res,
|
|
458
|
-
out: _out,
|
|
459
|
-
err: _err,
|
|
460
|
-
ok: _ok,
|
|
461
|
-
} = await getResult(v2.id)
|
|
462
|
-
if (_err) {
|
|
463
|
-
err = _err
|
|
464
|
-
break
|
|
465
|
-
}
|
|
466
|
-
if (!isOutComplete(out, get) && _out)
|
|
467
|
-
out = mergeOut(out, _out, get)
|
|
468
|
-
if (isOutComplete(out, get) && isOK) break
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
if (isOutComplete(out, get) && isOK) break
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
return { res, err }
|
|
476
|
-
}
|
|
477
|
-
;({ res, err } = await getResult(mid))
|
|
478
|
-
if (!isOK && !err) err = "something went wrong!"
|
|
479
|
-
} catch (e) {
|
|
480
|
-
err = e
|
|
481
|
-
}
|
|
482
|
-
return { mid, res, err, out, results }
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
async asgn({ pid, mid, jwk, check, get }) {
|
|
486
|
-
let err = null
|
|
487
|
-
;({ jwk, err } = await this.ar.checkWallet({ jwk }))
|
|
488
|
-
if (err) return { err }
|
|
489
|
-
|
|
490
|
-
let [res, out] = [null, null]
|
|
491
|
-
try {
|
|
492
|
-
mid = await this.assign({
|
|
493
|
-
process: pid,
|
|
494
|
-
message: mid,
|
|
495
|
-
signer: this.toSigner(jwk),
|
|
496
|
-
})
|
|
497
|
-
res = await this.result({ process: pid, message: mid })
|
|
498
|
-
if (!res) err = "something went wrong"
|
|
499
|
-
|
|
500
|
-
if (res.Error) err = res.Error
|
|
501
|
-
else {
|
|
502
|
-
let checks = []
|
|
503
|
-
if (!is(Array, check)) check = [check]
|
|
504
|
-
let i = 0
|
|
505
|
-
for (const v of check) {
|
|
506
|
-
let _checks = checks[i] ?? null
|
|
507
|
-
if (isRegExp(v) || includes(typeof v)(["string", "boolean"])) {
|
|
508
|
-
_checks = mergeChecks(_checks, isData(v, res), v)
|
|
509
|
-
} else {
|
|
510
|
-
const checks2 = {}
|
|
511
|
-
for (const k in v ?? {}) {
|
|
512
|
-
checks2[k] = checkTag(res, k, v[k])
|
|
513
|
-
}
|
|
514
|
-
_checks = mergeChecks(_checks, checks2, v)
|
|
515
|
-
}
|
|
516
|
-
checks[i] = _checks
|
|
517
|
-
i++
|
|
518
|
-
}
|
|
519
|
-
if (!isCheckComplete(checks, check)) err = "something went wrong"
|
|
520
|
-
if (!err && !isNil(get)) out = getTagVal(get, res)
|
|
521
|
-
}
|
|
522
|
-
} catch (e) {
|
|
523
|
-
err = e
|
|
524
|
-
}
|
|
525
|
-
return { mid, res, err, out }
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
async dry({
|
|
529
|
-
pid,
|
|
530
|
-
jwk,
|
|
531
|
-
data,
|
|
532
|
-
act = "Eval",
|
|
533
|
-
tags = {},
|
|
534
|
-
check,
|
|
535
|
-
get,
|
|
536
|
-
timeout = 10000,
|
|
537
|
-
}) {
|
|
538
|
-
let err = null
|
|
539
|
-
;({ jwk, err } = await this.ar.checkWallet({ jwk }))
|
|
540
|
-
if (err) return { err }
|
|
541
|
-
let [res, out] = [null, null]
|
|
542
|
-
let _tags = buildTags(act, tags)
|
|
543
|
-
try {
|
|
544
|
-
const _res = await this.dryrun({
|
|
545
|
-
process: pid,
|
|
546
|
-
signer: this.toSigner(jwk),
|
|
547
|
-
tags: _tags,
|
|
548
|
-
data: jsonToStr(data),
|
|
549
|
-
})
|
|
550
|
-
res = _res
|
|
551
|
-
|
|
552
|
-
let checks = []
|
|
553
|
-
if (!is(Array, check)) check = [check]
|
|
554
|
-
let i = 0
|
|
555
|
-
for (const v of check) {
|
|
556
|
-
let _checks = checks[i] ?? null
|
|
557
|
-
if (isRegExp(v) || includes(typeof v)(["string", "boolean"])) {
|
|
558
|
-
_checks = mergeChecks(_checks, isData(v, res), v)
|
|
559
|
-
} else {
|
|
560
|
-
const checks2 = {}
|
|
561
|
-
for (const k in v ?? {}) {
|
|
562
|
-
checks2[k] = checkTag(res, k, v[k])
|
|
563
|
-
}
|
|
564
|
-
_checks = mergeChecks(_checks, checks2, v)
|
|
565
|
-
}
|
|
566
|
-
checks[i] = _checks
|
|
567
|
-
i++
|
|
568
|
-
}
|
|
569
|
-
if (!isCheckComplete(checks, check)) err = "something went wrong"
|
|
570
|
-
if (!err && !isNil(get)) out = getTagVal(get, res)
|
|
571
|
-
} catch (e) {
|
|
572
|
-
err = e
|
|
573
|
-
}
|
|
574
|
-
return { res, err, out }
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
async eval({ pid, jwk, data }) {
|
|
578
|
-
const fns = [
|
|
579
|
-
{
|
|
580
|
-
args: { pid, data, act: "Eval" },
|
|
581
|
-
err: ({ res }) => {
|
|
582
|
-
return (
|
|
583
|
-
typeof res?.Output?.data !== "object" &&
|
|
584
|
-
!(
|
|
585
|
-
typeof res?.Output?.prompt === "string" &&
|
|
586
|
-
/aos\-/.test(res?.Output?.prompt)
|
|
587
|
-
)
|
|
588
|
-
)
|
|
589
|
-
},
|
|
590
|
-
},
|
|
591
|
-
]
|
|
592
|
-
return await this.pipe({ jwk, fns })
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
async transform({ src, data, fills }) {
|
|
596
|
-
let [err, out] = [null, null]
|
|
597
|
-
let _data = data ?? (await this.ar.data(src, true))
|
|
598
|
-
|
|
599
|
-
if (!_data) err = "data doesn't exist"
|
|
600
|
-
else {
|
|
601
|
-
for (const k in fills ?? {}) {
|
|
602
|
-
let text = fills[k]
|
|
603
|
-
if (typeof text === "number") text = Number(text).toString()
|
|
604
|
-
_data = _data.replace(
|
|
605
|
-
new RegExp(`\<${k}\>`, "g"),
|
|
606
|
-
text.replace(/'/g, "\\'"),
|
|
607
|
-
)
|
|
608
|
-
}
|
|
609
|
-
out = _data
|
|
610
|
-
}
|
|
611
|
-
return { err, out }
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
async load({ src, data, fills, pid, jwk }) {
|
|
615
|
-
let fns = [
|
|
616
|
-
{
|
|
617
|
-
fn: this.transform,
|
|
618
|
-
args: { src, fills, data },
|
|
619
|
-
then: { "args.data": "inp" },
|
|
620
|
-
},
|
|
621
|
-
{ fn: this.eval, args: { pid } },
|
|
622
|
-
]
|
|
623
|
-
return await this.pipe({ jwk, fns })
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
async wait({ pid, attempts = 5 }) {
|
|
627
|
-
let exist = false
|
|
628
|
-
let err = null
|
|
629
|
-
while (attempts > 0) {
|
|
630
|
-
await wait(1000)
|
|
631
|
-
const { res, err: _err } = await this.dry({ pid, data: "#Inbox" })
|
|
632
|
-
if (typeof res?.Output === "object") break
|
|
633
|
-
attempts -= 1
|
|
634
|
-
if (attempts === 0) err = "timeout"
|
|
635
|
-
}
|
|
636
|
-
return { err, pid }
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
async deploy({
|
|
640
|
-
boot,
|
|
641
|
-
loads,
|
|
642
|
-
src,
|
|
643
|
-
src_data,
|
|
644
|
-
fills = {},
|
|
645
|
-
module = this.module,
|
|
646
|
-
scheduler = this.scheduler,
|
|
647
|
-
jwk,
|
|
648
|
-
tags = {},
|
|
649
|
-
data,
|
|
650
|
-
}) {
|
|
651
|
-
let [fns, isBoot] = [[], false]
|
|
652
|
-
if (boot === true && !data) {
|
|
653
|
-
isBoot = true
|
|
654
|
-
fns = [
|
|
655
|
-
{
|
|
656
|
-
fn: this.transform,
|
|
657
|
-
args: { src, fills, data: src_data },
|
|
658
|
-
then: { "args.data": "inp" },
|
|
659
|
-
},
|
|
660
|
-
{
|
|
661
|
-
fn: this.spwn,
|
|
662
|
-
args: { boot: "Data", module, scheduler, tags },
|
|
663
|
-
then: { "args.pid": "pid" },
|
|
664
|
-
},
|
|
665
|
-
]
|
|
666
|
-
} else {
|
|
667
|
-
fns = [
|
|
668
|
-
{
|
|
669
|
-
fn: this.spwn,
|
|
670
|
-
args: { boot, module, scheduler, tags, data },
|
|
671
|
-
then: { "args.pid": "pid" },
|
|
672
|
-
},
|
|
673
|
-
]
|
|
674
|
-
}
|
|
675
|
-
fns.push({ fn: this.wait, then: { "args.pid": "pid" } })
|
|
676
|
-
let i = 0
|
|
677
|
-
for (const v of !loads ? [{ data: src_data, src, fills }] : loads) {
|
|
678
|
-
if (!isBoot || i !== 0) {
|
|
679
|
-
fns.push({ fn: this.load, args: v, then: { "args.pid": "pid" } })
|
|
680
|
-
}
|
|
681
|
-
i++
|
|
682
|
-
}
|
|
683
|
-
let result = await this.pipe({ jwk, fns })
|
|
684
|
-
if (result.pid) result.p = this.p(result.pid)
|
|
685
|
-
return result
|
|
686
|
-
}
|
|
687
|
-
p(pid) {
|
|
688
|
-
return new Process(pid, this)
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
const getParams = (tags, opts) => {
|
|
693
|
-
let _tags = tags
|
|
694
|
-
let _opts = opts
|
|
695
|
-
|
|
696
|
-
if (
|
|
697
|
-
(!isNil(tags?.get) ||
|
|
698
|
-
!isNil(tags?.check) ||
|
|
699
|
-
is(Boolean, tags) ||
|
|
700
|
-
is(String, tags)) &&
|
|
701
|
-
isNil(opts)
|
|
702
|
-
) {
|
|
703
|
-
_opts = tags
|
|
704
|
-
_tags = null
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
if (isNil(_opts)) _opts = { get: true }
|
|
708
|
-
else if (is(Boolean, _opts) || is(String, _opts)) _opts = { get: _opts }
|
|
709
|
-
return { _tags, _opts }
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
class Process {
|
|
713
|
-
constructor(pid, ao) {
|
|
714
|
-
this.ao = ao
|
|
715
|
-
this.pid = pid
|
|
716
|
-
}
|
|
717
|
-
async msg(act, tags, opts) {
|
|
718
|
-
const { _tags, _opts } = getParams(tags, opts)
|
|
719
|
-
return await this.ao.msg({ pid: this.pid, act, tags: _tags, ..._opts })
|
|
720
|
-
}
|
|
721
|
-
async dry(act, tags, opts) {
|
|
722
|
-
const { _tags, _opts } = getParams(tags, opts)
|
|
723
|
-
return await this.ao.dry({ pid: this.pid, act, tags: _tags, ..._opts })
|
|
724
|
-
}
|
|
725
|
-
async m(...opt) {
|
|
726
|
-
const res = await this.msg(...opt)
|
|
727
|
-
if (res.err) throw Error(res.err)
|
|
728
|
-
return res.out
|
|
729
|
-
}
|
|
730
|
-
async d(...opt) {
|
|
731
|
-
const res = await this.dry(...opt)
|
|
732
|
-
if (res.err) throw Error(res.err)
|
|
733
|
-
return res.out
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
export default AO
|