wao 0.2.3 → 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/tao.js CHANGED
@@ -46,8 +46,10 @@ var AO = /*#__PURE__*/function (_MAO) {
46
46
  message = _connect.message,
47
47
  spawn = _connect.spawn,
48
48
  dryrun = _connect.dryrun,
49
+ monitor = _connect.monitor,
50
+ unmonitor = _connect.unmonitor,
49
51
  txs = _connect.txs;
50
- _this.module = modules.aos_2_0_1;
52
+ _this.module = modules.aos2_0_1;
51
53
  _this.assign = assign;
52
54
  _this.result = result;
53
55
  _this.results = results;
@@ -55,6 +57,8 @@ var AO = /*#__PURE__*/function (_MAO) {
55
57
  _this.spawn = spawn;
56
58
  _this.dryrun = dryrun;
57
59
  _this.ar.txs = txs;
60
+ _this.monitor = monitor;
61
+ _this.unmonitor = unmonitor;
58
62
  _this.accounts = accounts;
59
63
  return _this;
60
64
  }
package/esm/ao.js CHANGED
@@ -11,6 +11,8 @@ import {
11
11
  message,
12
12
  spawn,
13
13
  dryrun,
14
+ monitor,
15
+ unmonitor,
14
16
  } from "@permaweb/aoconnect"
15
17
 
16
18
  import {
@@ -88,14 +90,24 @@ class AO {
88
90
  }
89
91
  if (in_memory) {
90
92
  } else if (aoconnect) {
91
- const { results, assign, result, message, spawn, dryrun } =
92
- connect(aoconnect)
93
+ const {
94
+ results,
95
+ assign,
96
+ result,
97
+ message,
98
+ spawn,
99
+ dryrun,
100
+ monitor,
101
+ unmonitor,
102
+ } = connect(aoconnect)
93
103
  this.assign = assign
94
104
  this.result = result
95
105
  this.results = results
96
106
  this.message = message
97
107
  this.spawn = spawn
98
108
  this.dryrun = dryrun
109
+ this.monitor = monitor
110
+ this.unmonitor = unmonitor
99
111
  } else {
100
112
  this.assign = assign
101
113
  this.result = result
@@ -103,6 +115,8 @@ class AO {
103
115
  this.message = message
104
116
  this.spawn = spawn
105
117
  this.dryrun = dryrun
118
+ this.monitor = monitor
119
+ this.unmonitor = unmonitor
106
120
  }
107
121
  this.module = module
108
122
  this.scheduler = scheduler
@@ -648,7 +662,11 @@ class AO {
648
662
  }
649
663
  fns.push({ fn: this.wait, then: { "args.pid": "pid" } })
650
664
  let i = 0
651
- for (const v of !loads ? [{ data: src_data, src, fills }] : loads) {
665
+ for (const v of !loads
666
+ ? src_data
667
+ ? [{ data: src_data, src, fills }]
668
+ : []
669
+ : loads) {
652
670
  if (!isBoot || i !== 0) {
653
671
  fns.push({ fn: this.load, args: v, then: { "args.pid": "pid" } })
654
672
  }
package/esm/aoconnect.js CHANGED
@@ -3,7 +3,7 @@ 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) {
@@ -37,6 +37,8 @@ const dirname = async () =>
37
37
 
38
38
  export const acc = accounts.users
39
39
  export const mu = accounts.mu
40
+ const scheduler = "GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA"
41
+ let env = {}
40
42
 
41
43
  export const connect = () => {
42
44
  let wasms = {
@@ -54,12 +56,12 @@ export const connect = () => {
54
56
  },
55
57
  }
56
58
  let modules = {
57
- aos_2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
58
- aos_1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
59
+ aos2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
60
+ aos1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
59
61
  sqlite: "ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw",
60
62
  }
61
63
  let modmap = {}
62
- let env = { msgs: {} }
64
+ let msgs = {}
63
65
  const transform = input => {
64
66
  const output = { Tags: [] }
65
67
  if (input.Data) output.Data = input.Data
@@ -146,12 +148,148 @@ export const connect = () => {
146
148
  return id
147
149
  }
148
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
+
149
288
  const message = async opt => {
150
289
  const p = env[opt.process]
151
290
  let ex = false
152
- for (let v of opt.tags) {
153
- if (v.name === "Type") ex = true
154
- }
291
+ for (let v of opt.tags) if (v.name === "Type") ex = true
292
+
155
293
  if (!ex) opt.tags.push({ name: "Type", value: "Message" })
156
294
  const { id, owner } = await parse(opt)
157
295
  try {
@@ -174,7 +312,7 @@ export const connect = () => {
174
312
  p.res[id] = res
175
313
  p.results.push(id)
176
314
  p.txs.unshift({ id: id, ...opt })
177
- env.msgs[id] = opt
315
+ msgs[id] = opt
178
316
  for (const v of res.Messages ?? []) {
179
317
  if (env[v.Target]) {
180
318
  await message({
@@ -186,120 +324,66 @@ export const connect = () => {
186
324
  })
187
325
  }
188
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
+ }
189
348
  return id
190
349
  } catch (e) {
191
350
  console.log(e)
192
351
  }
193
352
  return null
194
353
  }
195
-
196
354
  return {
197
- scheduler: "GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA",
355
+ scheduler,
198
356
  modules,
199
357
  accounts: acc,
358
+ mu,
200
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
+ },
201
380
  txs: async pid => {
202
381
  let _txs = []
203
382
  for (let v of env[pid].txs) _txs.push({ tags: v.tags, id: v.id })
204
383
  return _txs
205
384
  },
206
- spawn: async (opt = {}) => {
207
- if (!opt.module) throw Error("module.missing")
208
- if (!opt.scheduler) throw Error("scheduler.missing")
209
- let mod = opt.module ?? modules["aos_2_0_1"]
210
- if (!modmap[mod] && wasms[mod]) {
211
- const __dirname = await dirname()
212
- const wasm = readFileSync(
213
- resolve(__dirname, `lua/${wasms[mod].file}.wasm`),
214
- )
215
- const handle = await AoLoader(wasm, { format: wasms[mod].format })
216
- modmap[mod] = { handle, id: mod }
217
- }
218
- if (!mod) throw Error("module not found")
219
- const _module = modmap[mod]
220
- let ex = false
221
- opt.tags ??= []
222
- for (let v of opt.tags) if (v.name === "Type") ex = true
223
- if (!ex) opt.tags.push({ name: "Type", value: "Process" })
224
- const { id, owner } = await parse(opt)
225
- const _tags = tags(opt.tags)
226
- let res = null
227
- let memory = null
228
- let p = {
229
- id: id,
230
- handle: _module.handle,
231
- module: _module.id,
232
- memory,
233
- owner,
234
- height: 0,
235
- res: { [id]: res },
236
- results: [id],
237
- txs: [],
238
- }
239
- if (_tags["On-Boot"]) {
240
- let data = ""
241
- if (_tags["On-Boot"] === "Data") data = opt.data ?? ""
242
- else data = env.msgs[_tags["On-Boot"]]?.data ?? ""
243
- const msg = genMsg(p, data, opt.tags, owner, mu.addr, true)
244
- const _env = genEnv({
245
- pid: p.id,
246
- owner: p.owner,
247
- module: p.module,
248
- auth: mu.addr,
249
- })
250
- res = await _module.handle(null, msg, _env)
251
- memory = res.Memory
252
- delete res.Memory
253
- p.res[id] = res
254
- } else {
255
- p.height += 1
256
- }
257
- env.msgs[id] = opt
258
- env[id] = p
259
- return id
260
- },
261
- assign: async opt => {
262
- const p = env[opt.process]
263
- let _opt = clone(env.msgs[opt.message])
264
- const { id, owner } = await parse(_opt)
265
- try {
266
- const msg = genMsg(
267
- p,
268
- _opt.data ?? "",
269
- _opt.tags,
270
- _opt.from ?? owner,
271
- mu.addr,
272
- )
273
- const _env = genEnv({
274
- pid: p.id,
275
- owner: p.owner,
276
- module: p.module,
277
- auth: mu.addr,
278
- })
279
- const res = await p.handle(p.memory, msg, _env)
280
- p.memory = res.Memory
281
- delete res.Memory
282
- p.res[id] = res
283
- p.results.push(id)
284
- p.txs.unshift({ id: id, ..._opt })
285
- env.msgs[id] = _opt
286
- for (const v of res.Messages ?? []) {
287
- if (env[v.Target]) {
288
- await message({
289
- process: v.Target,
290
- tags: v.Tags,
291
- data: v.Data,
292
- signer: mu.signer,
293
- from: opt.process,
294
- })
295
- }
296
- }
297
- return id
298
- } catch (e) {
299
- console.log(e)
300
- }
301
- return null
302
- },
385
+ spawn,
386
+ assign,
303
387
  result: async opt => env[opt.process].res[opt.message],
304
388
  results: async opt => {
305
389
  const p = env[opt.process]
@@ -337,5 +421,6 @@ export const connect = () => {
337
421
  }
338
422
  return null
339
423
  },
424
+ getProcesses: () => env,
340
425
  }
341
426
  }
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.aos_2_0_1
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wao",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",