wao 0.24.0 → 0.24.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/cjs/utils.js CHANGED
@@ -606,6 +606,7 @@ var toGraphObj = exports.toGraphObj = function toGraphObj(_ref12) {
606
606
  if (fields) args.fields = fields;
607
607
  if (args.sort && args.sort === "HEIGHT_ASC") args.asc = true;
608
608
  delete args.sort;
609
+ if (!Array.isArray(args.tags)) args.tags = [args.tags];
609
610
  if (args.tags) {
610
611
  var _tags = {};
611
612
  var _iterator9 = _createForOfIteratorHelper(args.tags),
package/esm/hb.js CHANGED
@@ -17,41 +17,50 @@ class HB {
17
17
  this.dev.hyperbuddy = {
18
18
  metrics: async (args = {}) => {
19
19
  return this.parseMetrics(
20
- await this.fetch(this.path("hyperbuddy", "metrics", false), false)
20
+ await this.fetch(
21
+ this.path({ dev: "hyperbuddy", path: "metrics", json: false }),
22
+ false
23
+ )
21
24
  )
22
25
  },
23
26
  }
24
27
 
25
28
  this.dev.json = {
26
29
  commit: async args => {
27
- return await this.send({ path: "/~json@1.0/commit", ...args })
30
+ return await this.post({ path: "/~json@1.0/commit", ...args })
28
31
  },
29
32
  verify: async args => {
30
- return await this.send({ path: "/~json@1.0/verify", ...args })
33
+ return await this.post({ path: "/~json@1.0/verify", ...args })
31
34
  },
32
35
  deserialize: async args => {
33
- return await this.send({ path: "/~json@1.0/deserialize", ...args })
36
+ return await this.post({ path: "/~json@1.0/deserialize", ...args })
34
37
  },
35
38
  serialize: async args => {
36
- return await this.send({ path: "/~json@1.0/serialize", ...args })
39
+ return await this.post({ path: "/~json@1.0/serialize", ...args })
37
40
  },
38
41
  }
39
42
  this.dev.meta = {
40
43
  info: async (args = {}) => {
41
44
  let { method = "GET", json = true, key } = args
42
45
  if (method.toLowerCase() === "post") {
43
- return await this.send({ path: "/~meta@1.0/info", ...args })
46
+ return await this.post({ path: "/~meta@1.0/info", ...args })
44
47
  } else {
45
48
  return key
46
49
  ? await this.fetch(
47
- this.path("meta", `info/${key}`, args.json ?? false),
50
+ this.path({
51
+ dev: "meta",
52
+ path: `info/${key}`,
53
+ json: args.json ?? false,
54
+ }),
48
55
  args.json ?? false
49
56
  )
50
- : await this.fetch(this.path("meta", "info", json))
57
+ : await this.fetch(
58
+ this.path({ dev: "meta", path: "info", json: json })
59
+ )
51
60
  }
52
61
  },
53
62
  build: async () => {
54
- return await this.fetch(this.path("meta", "build"))
63
+ return await this.fetch(this.path({ dev: "meta", path: "build" }))
55
64
  },
56
65
  }
57
66
  if (jwk) this._init(jwk)
@@ -60,7 +69,7 @@ class HB {
60
69
  _init(jwk) {
61
70
  this.signer = createSigner(jwk, this.url)
62
71
  this.addr = toAddr(jwk.n)
63
- this._request2 = createRequest({ signer: this.signer, url: this.url })
72
+ this.sign = createRequest({ signer: this.signer, url: this.url })
64
73
 
65
74
  const { request } = connect({
66
75
  MODE: "mainnet",
@@ -78,8 +87,8 @@ class HB {
78
87
  return this
79
88
  }
80
89
 
81
- async send(args) {
82
- return await _send(await this._request2(args))
90
+ async send(msg) {
91
+ return await _send(msg)
83
92
  }
84
93
 
85
94
  async getImage() {
@@ -91,15 +100,6 @@ class HB {
91
100
  return id
92
101
  }
93
102
 
94
- async getLegacy() {
95
- const wasm = readFileSync(
96
- resolve(import.meta.dirname, "./lua/aos2_0_6.wasm")
97
- )
98
- const id = await this.cacheModule(wasm, "application/wasm")
99
- this.legacy ??= id
100
- return id
101
- }
102
-
103
103
  async getLua() {
104
104
  const lua = readFileSync(
105
105
  resolve(import.meta.dirname, "./lua/hyper-aos.lua")
@@ -108,25 +108,35 @@ class HB {
108
108
  this.lua ??= id
109
109
  return id
110
110
  }
111
-
112
- async messageAOS({ pid, action = "Eval", tags = {}, data }) {
111
+ async scheduleAOS({ pid, action = "Eval", tags = {}, data }) {
113
112
  pid ??= this.pid
114
113
  let _tags = mergeLeft(tags, {
115
114
  device: "process@1.0",
116
115
  method: "POST",
117
- path: `/${pid}/schedule`,
116
+ path: `/${pid}~process@1.0/schedule`,
118
117
  scheduler: this.scheduler,
119
118
  Type: "Message",
120
119
  Action: action,
121
120
  Target: pid,
122
121
  })
123
122
  if (data) _tags.data = data
124
- let res = await this.send(_tags)
125
- const slot = res.headers.get("slot")
123
+ let res = await this.post(_tags)
124
+ const slot = res.headers.slot
125
+ return { slot, res, pid }
126
+ }
127
+ async messageAOS(args) {
128
+ const { slot, pid } = await this.scheduleAOS(args)
126
129
  return { slot, outbox: await this.computeAOS({ pid, slot }) }
127
130
  }
128
131
 
129
- path(dev = "message", path, json = true, params = {}, pid = "", tail = "") {
132
+ path({
133
+ dev = "message",
134
+ path,
135
+ json = true,
136
+ params = {},
137
+ pid = "",
138
+ tail = "",
139
+ }) {
130
140
  if (!/@/.test(dev)) dev += "@1.0"
131
141
  let _params = ""
132
142
  if (!isEmpty(params)) {
@@ -136,7 +146,8 @@ class HB {
136
146
  i++
137
147
  }
138
148
  }
139
- return `${this.url}/${pid}~${dev}${path ? `/${path}` : ""}${tail}${json ? "/~json@1.0/serialize" : ""}${_params}`
149
+ if (path && !/^\//.test(path)) path = "/" + path
150
+ return `/${pid}~${dev}${path ?? ""}${tail}${json ? "/~json@1.0/serialize" : ""}${_params}`
140
151
  }
141
152
 
142
153
  async text(dev, path, params = {}, tail) {
@@ -146,7 +157,7 @@ class HB {
146
157
  dev = "process"
147
158
  }
148
159
  return await this.fetch(
149
- this.path(dev, path, false, params, pid, tail),
160
+ this.path({ dev, path, json: false, params, pid, tail }),
150
161
  false
151
162
  )
152
163
  }
@@ -157,29 +168,26 @@ class HB {
157
168
  pid = dev
158
169
  dev = "process"
159
170
  }
160
- return await this.fetch(this.path(dev, path, true, params, pid, tail))
171
+ return await this.fetch(
172
+ this.path({ dev, path, json: true, params, pid, tail })
173
+ )
161
174
  }
162
175
 
163
- async fetch(url, json = true) {
164
- return await fetch(url).then(r => (json ? r.json() : r.text()))
176
+ async fetch(path, json = true) {
177
+ return await fetch(this.url + path).then(r => (json ? r.json() : r.text()))
165
178
  }
166
179
 
167
180
  async computeAOS({ pid, slot }) {
168
- return await fetch(
169
- `${this.url}/${pid}/compute/results/outbox/~json@1.0/serialize?slot=${slot}`
170
- ).then(r => r.json())
181
+ return await this.getJSON({ path: `/${pid}/compute/results/outbox`, slot })
171
182
  }
172
183
 
173
184
  async computeLua({ pid, slot }) {
174
- return await fetch(
175
- `${this.url}/${pid}/compute/results/~json@1.0/serialize?slot=${slot}`
176
- ).then(r => r.json())
185
+ return await this.getJSON({ path: `/${pid}/compute/results`, slot })
177
186
  }
178
187
 
179
- async compute({ pid, slot }) {
180
- return await fetch(
181
- `${this.url}/${pid}/compute/~json@1.0/serialize?slot=${slot}`
182
- ).then(r => r.json())
188
+ async compute({ pid, slot, path = "" }) {
189
+ if (path && !/^\//.test(path)) path = "/" + path
190
+ return await this.getJSON({ path: `/${pid}/compute${path}`, slot })
183
191
  }
184
192
 
185
193
  async computeLegacy({ pid, slot }) {
@@ -190,26 +198,35 @@ class HB {
190
198
  async spawn(tags = {}) {
191
199
  const addr = await this.dev.meta.info({ key: "address" })
192
200
  this.scheduler ??= addr
193
- const res = await this.send(
194
- mergeLeft(tags, {
195
- device: "process@1.0",
196
- path: "/schedule",
197
- scheduler: this.scheduler,
198
- "random-seed": seed(16),
199
- Type: "Process",
200
- "scheduler-device": "scheduler@1.0",
201
- "execution-device": "test-device@1.0",
202
- })
203
- )
204
- return { res, pid: res.headers.get("process") }
201
+ const _tags = mergeLeft(tags, {
202
+ device: "process@1.0",
203
+ path: "/schedule",
204
+ scheduler: this.scheduler,
205
+ "random-seed": seed(16),
206
+ Type: "Process",
207
+ "execution-device": "test-device@1.0",
208
+ })
209
+ const res = await this.post(_tags)
210
+ return { res, pid: res.headers.process }
205
211
  }
206
- async cacheModule(data, type) {
207
- const res = await this.send({
208
- path: "/~wao@1.0/cache_module",
212
+
213
+ async cacheModule2(data, type) {
214
+ if (!this.cache) {
215
+ const { pid } = await this.spawn({})
216
+ this.cache = pid
217
+ }
218
+ const { slot } = await this.schedule({
209
219
  data,
210
- type,
220
+ pid: this.cache,
221
+ "content-type": type,
211
222
  })
212
- return res.headers.get("id")
223
+ const msgs = await this.messages({ pid: this.cache, from: slot, limit: 1 })
224
+ return msgs.edges[0].node.message.Id
225
+ }
226
+
227
+ async cacheModule(data, type) {
228
+ const res = await this.post({ path: "/~wao@1.0/cache_module", data, type })
229
+ return res.headers.id
213
230
  }
214
231
  async message(args) {
215
232
  const pid = args.pid
@@ -230,26 +247,24 @@ class HB {
230
247
  async scheduleLua(...args) {
231
248
  return await this.scheduleLegacy(...args)
232
249
  }
233
- async schedule({ pid, tags = {}, data, scheduler } = {}) {
250
+ async schedule({ pid, tags = {}, data } = {}) {
234
251
  pid ??= this.pid
235
- scheduler ??= this.scheduler
236
252
  let _tags = mergeLeft(tags, {
237
253
  method: "POST",
238
254
  path: `/${pid}/schedule`,
239
- scheduler,
240
255
  Type: "Message",
241
256
  Target: pid,
242
257
  })
243
258
  if (data) _tags.data = data
244
- let res = await this.send(_tags)
245
- return { slot: res.headers.get("slot"), res }
259
+ let res = await this.post(_tags)
260
+ return { slot: res.headers.slot, res }
246
261
  }
247
262
 
248
263
  async spawnAOS(image) {
249
264
  const addr = await this.dev.meta.info({ key: "address" })
250
265
  this.scheduler ??= addr
251
266
  image ??= this.image ?? (await this.getImage())
252
- const res = await this.send({
267
+ const res = await this.post({
253
268
  device: "process@1.0",
254
269
  path: "/schedule",
255
270
  scheduler: this.scheduler,
@@ -260,12 +275,12 @@ class HB {
260
275
  "random-seed": seed(16),
261
276
  Type: "Process",
262
277
  image,
263
- "scheduler-device": "scheduler@1.0",
264
278
  "execution-device": "stack@1.0",
265
279
  "device-stack": [
266
280
  "wasi@1.0",
267
281
  "json-iface@1.0",
268
282
  "wasm-64@1.0",
283
+ "patch@1.0",
269
284
  "multipass@1.0",
270
285
  ],
271
286
  "output-prefix": "wasm",
@@ -273,7 +288,7 @@ class HB {
273
288
  "stack-keys": ["init", "compute", "snapshot", "normalize"],
274
289
  passes: 2,
275
290
  })
276
- const pid = res.headers.get("process")
291
+ const pid = res.headers.process
277
292
  this.pid ??= pid
278
293
  return { pid, res }
279
294
  }
@@ -282,7 +297,7 @@ class HB {
282
297
  const addr = await this.dev.meta.info({ key: "address" })
283
298
  this.scheduler ??= addr
284
299
  lua ??= this.lua ?? (await this.getLua())
285
- const res = await this.send({
300
+ const res = await this.post({
286
301
  device: "process@1.0",
287
302
  path: "/schedule",
288
303
  scheduler: this.scheduler,
@@ -293,11 +308,10 @@ class HB {
293
308
  "random-seed": seed(16),
294
309
  Type: "Process",
295
310
  module: lua,
296
- "scheduler-device": "scheduler@1.0",
297
311
  "execution-device": "lua@5.3a",
298
312
  "patch-from": "/results/outbox",
299
313
  })
300
- const pid = res.headers.get("process")
314
+ const pid = res.headers.process
301
315
  this.pid ??= pid
302
316
  return { pid, res }
303
317
  }
@@ -333,6 +347,10 @@ class HB {
333
347
  }
334
348
  return _metrics
335
349
  }
350
+ async now({ pid, path = "" }) {
351
+ if (path && !/^\//.test(path)) path = "/" + path
352
+ return await this.getJSON({ path: `/${pid}/now${path}` })
353
+ }
336
354
 
337
355
  async messages({ pid, from, to, limit } = {}) {
338
356
  let params = `target=${pid}`
@@ -373,7 +391,7 @@ class HB {
373
391
  if (typeof action === "string") tags.Action = action
374
392
  let json = { Tags: buildTags(tags) }
375
393
  if (data) json.Data = data
376
- const res = await this.send({
394
+ const res = await this.post({
377
395
  path: "/~relay@1.0/call",
378
396
  "relay-method": "POST",
379
397
  "relay-path": `/dry-run?process-id=${pid}`,
@@ -383,18 +401,40 @@ class HB {
383
401
  return JSON.parse(res.body)
384
402
  }
385
403
 
386
- async get({ device, path = "~meta@1.0/info" }) {
387
- return (await this.request({ device, path, method: "GET" })).body
404
+ async post(obj, json) {
405
+ const _json = json ? "/~json@1.0/serialize" : ""
406
+ obj.path += _json
407
+ return await this.send(await this.sign(obj))
388
408
  }
389
-
390
- async post({ device, tags, path = "/schedule" }) {
391
- return await this.request({ tags, device, path, method: "POST" })
409
+ async get({ path, ...params }, json = false) {
410
+ const _json = json ? "/~json@1.0/serialize" : ""
411
+ path ??= "/~message@1.0"
412
+ if (!/^\//.test(path)) path = "/" + path
413
+ let _params = ""
414
+ if (!isEmpty(params)) {
415
+ let i = 0
416
+ for (const k in params) {
417
+ _params += `${i === 0 ? "?" : "&"}${k}=${params[k]}`
418
+ i++
419
+ }
420
+ }
421
+ const response = await fetch(`${this.url}${path}${_json}${_params}`)
422
+ let headers = {}
423
+ response.headers.forEach((v, k) => (headers[k] = v))
424
+ return {
425
+ response,
426
+ headers,
427
+ body: await response.text(),
428
+ status: response.status,
429
+ }
392
430
  }
393
-
394
- async request({ device, tags, method = "POST", path = "/schedule" }) {
395
- let _tags = mergeLeft(tags, { path, method })
396
- if (device) _tags.device = device
397
- return await this._request(_tags)
431
+ async postJSON(args) {
432
+ const res = await this.post(args, true)
433
+ return JSON.parse(res.body)
434
+ }
435
+ async getJSON(args) {
436
+ const res = await this.get(args, true)
437
+ return JSON.parse(res.body)
398
438
  }
399
439
  }
400
440
 
package/esm/hyperbeam.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { spawn } from "child_process"
2
2
  import { resolve } from "path"
3
3
  import { isNil, map } from "ramda"
4
- import { readFileSync } from "fs"
4
+ import { rmSync, readFileSync, readdirSync } from "fs"
5
+
5
6
  export default class HyperBEAM {
6
7
  constructor({
7
8
  port = 10001,
@@ -9,6 +10,7 @@ export default class HyperBEAM {
9
10
  bundler,
10
11
  gateway,
11
12
  wallet,
13
+ clearCache,
12
14
  cwd = "./HyperBEAM",
13
15
  c,
14
16
  cmake,
@@ -20,9 +22,22 @@ export default class HyperBEAM {
20
22
  store_prefix,
21
23
  operator,
22
24
  } = {}) {
25
+ if (clearCache) {
26
+ const dirname = resolve(process.cwd(), cwd)
27
+ for (let v of readdirSync(dirname)) {
28
+ if (/^cache-/.test(v)) {
29
+ try {
30
+ rmSync(resolve(dirname, v), { recursive: true, force: true })
31
+ } catch (e) {
32
+ console.log(e)
33
+ }
34
+ }
35
+ }
36
+ }
23
37
  this.cu = cu
24
- this.store_prefix =
25
- store_prefix ?? "cache-mainnet-" + Math.floor(Math.random() * 10000000)
38
+ this.store_prefix = store_prefix
39
+ ? "cache-mainnet-" + Math.floor(Math.random() * 10000000)
40
+ : "cache-mainnet"
26
41
  this.p4_lua = p4_lua
27
42
  this.simplePay = simplePay
28
43
  this.spp = simplePayPrice
@@ -66,6 +81,8 @@ export default class HyperBEAM {
66
81
  const _gateway = gateway
67
82
  ? `, gateway => <<"http://localhost:${gateway}">>`
68
83
  : ""
84
+
85
+ // store option will be overwritten by hb.erl
69
86
  const _store = this.store_prefix
70
87
  ? `, store => [#{ <<"store-module">> => hb_store_fs, <<"prefix">> => <<"${this.store_prefix}">> }, #{ <<"store-module">> => hb_store_gateway, <<"subindex">> => [#{ <<"name">> => <<"Data-Protocol">>, <<"value">> => <<"ao">> }], <<"store">> => [#{ <<"store-module">> => hb_store_fs, <<"prefix">> => <<"${this.store_prefix}">> }] }, #{ <<"store-module">> => hb_store_gateway, <<"store">> => [#{ <<"store-module">> => hb_store_fs, <<"prefix">> => <<"${this.store_prefix}">> }] }]`
71
88
  : ""
@@ -102,7 +119,7 @@ export default class HyperBEAM {
102
119
  : !isNil(this.faff)
103
120
  ? `, on => #{ <<"request">> => #{ <<"device">> => <<"p4@1.0">>, <<"pricing-device">> => <<"faff@1.0">>, <<"ledger-device">> => <<"faff@1.0">> }, <<"response">> => #{ <<"device">> => <<"p4@1.0">>, <<"pricing-device">> => <<"faff@1.0">>, <<"ledger-device">> => <<"faff@1.0">> } }`
104
121
  : ""
105
- const start = `hb:start_mainnet(#{ ${_port}${_gateway}${_wallet}${_store}${_faff}${_bundler}${_routes}${_on}${_p4_non_chargable}${_operator}${_spp}${_node_processes} }).`
122
+ const start = `hb:start_mainnet(#{ ${_port}${_gateway}${_wallet}${_faff}${_bundler}${_routes}${_on}${_p4_non_chargable}${_operator}${_spp}${_node_processes}}).`
106
123
  return start
107
124
  }
108
125