wao 0.9.1 → 0.9.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.
@@ -0,0 +1,172 @@
1
+ import _Arweave from "arweave"
2
+ const Arweave = _Arweave.default ?? _Arweave
3
+
4
+ import { last } from "ramda"
5
+ import { buildTags, tags } from "./utils.js"
6
+
7
+ export default class ArMemBase {
8
+ constructor({ MU_URL, CU_URL, SU_URL, GATEWAY_URL, scheduler, cache } = {}) {
9
+ this.__type__ = "mem"
10
+ this.isInit = false
11
+ this.keyInit = false
12
+ this.keys = {}
13
+ this.arweave = Arweave.init()
14
+ this.arweave.transactions.getTransactionAnchor = () => this.getAnchor()
15
+ this.arweave.transactions.getPrice = () => 0
16
+ this.scheduler = scheduler
17
+ this.SU_URL = SU_URL
18
+ }
19
+ getAnchor() {
20
+ return this.blocks.length === 0
21
+ ? "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM"
22
+ : last(this.blockmap[last(this.blocks)].txs)
23
+ }
24
+ async get(key, field) {
25
+ await this.init()
26
+ if (!field) return this[key]
27
+ return this[key]?.[field]
28
+ }
29
+ async set(val, key, field) {
30
+ await this.init()
31
+ if (!field) {
32
+ this[key] = val
33
+ if (this.db) await this.db.put(`${key}`, this[key])
34
+ } else {
35
+ this[key] ??= {}
36
+ this[key][field] = val
37
+ if (this.db) await this.db.put(`${key}.${field}`, this[key][field])
38
+ }
39
+ }
40
+ initSync() {
41
+ this.addrmap = {}
42
+ this.txs = {}
43
+ this.jwks = {}
44
+ this.blocks = []
45
+ this.blockmap = {}
46
+ this.env = {}
47
+ this.modules = {
48
+ aos2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
49
+ aos1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
50
+ sqlite: "ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw",
51
+ }
52
+ this.modmap = {}
53
+ this.msgs = {}
54
+ this.wasms = {
55
+ "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM": {
56
+ file: "aos2_0_1",
57
+ format: "wasm64-unknown-emscripten-draft_2024_02_15",
58
+ },
59
+ cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo: {
60
+ file: "aos_1",
61
+ format: "wasm64-unknown-emscripten-draft_2024_02_15",
62
+ },
63
+ ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw: {
64
+ file: "sqlite",
65
+ format: "wasm64-unknown-emscripten-draft_2024_02_15",
66
+ },
67
+ }
68
+ let txs = []
69
+ for (const k in this.modules) {
70
+ const key = this.modules[k]
71
+ txs.push(key)
72
+ this.txs[key] = {
73
+ id: key,
74
+ block: 0,
75
+ tags: buildTags(null, {
76
+ "Data-Protocol": "ao",
77
+ Variant: "ao.TN.1",
78
+ Type: "Module",
79
+ "Module-Format": "wasm64-unknown-emscripten-draft_2024_02_15",
80
+ "Input-Encoding": "JSON-V1",
81
+ "Output-Encoding": "JSON-V1",
82
+ "Memory-Limit": "1-gb",
83
+ "Compute-Limit": "9000000000000",
84
+ Extension: "WeaveDrive",
85
+ }),
86
+ }
87
+ }
88
+ if (this.scheduler && this.SU_URL) {
89
+ const key = this.scheduler
90
+ txs.push(key)
91
+ this.addrmap[this.scheduler] = { address: this.scheduler }
92
+ this.txs[key] = {
93
+ id: key,
94
+ block: 0,
95
+ owner: this.scheduler,
96
+ tags: buildTags(null, {
97
+ "Data-Protocol": "ao",
98
+ Variant: "ao.TN.1",
99
+ Type: "Scheduler-Location",
100
+ Url: this.SU_URL,
101
+ "Time-To-Live": 1000 * 60 * 60 * 24 * 365 * 10,
102
+ }),
103
+ }
104
+ }
105
+ this.blockmap["0"] = {
106
+ txs,
107
+ timestamp: Date.now(),
108
+ height: 0,
109
+ previous: "",
110
+ id: "0",
111
+ }
112
+ this.blocks.push("0")
113
+ this.height = 1
114
+ }
115
+ async putAll(key) {
116
+ for (let k in this[key]) await this.set(this[key][k], key, k)
117
+ }
118
+ async init() {
119
+ if (this.isInit) return
120
+ this.isInit = true
121
+ if (this.db) {
122
+ for (const v of ["height", "blocks"]) this[v] = await this.get(v)
123
+ for (const v of [
124
+ "txs",
125
+ "jwks",
126
+ "env",
127
+ "modules",
128
+ "wasms",
129
+ "addrmap",
130
+ "blockmap",
131
+ "modmap",
132
+ "msgs",
133
+ ]) {
134
+ this[v] ??= {}
135
+ const items = await this.db.getKeys({ start: v, end: v + "a" })
136
+ for (const v2 of items || []) {
137
+ const key = v2.split(".")[0]
138
+ const field = v2.split(".")[1]
139
+ if (key === v) this[v][field] = await this.db.get(v2)
140
+ }
141
+ }
142
+ } else {
143
+ for (const v of ["height", "blocks"]) await this.set(this[v], v)
144
+ for (const v of ["modules", "wasms", "addrmap", "blockmap"]) {
145
+ await this.putAll(v)
146
+ }
147
+ }
148
+ }
149
+ async getWasm(module) {
150
+ let mod = module ?? this.modules.aos2_0_1
151
+ if (!mod) throw Error("module not found")
152
+ let format = null
153
+ let _wasm = await this.wasms[mod]
154
+ let wasm = _wasm?.data
155
+ if (!wasm) {
156
+ if (_wasm?.file) {
157
+ wasm = await this._getWasm(this.wasms[mod].file)
158
+ format = _wasm.format
159
+ } else {
160
+ const tx = await this.get("txs", mod)
161
+ if (tx) {
162
+ wasm = Buffer.from(tx.data, "base64")
163
+ format = tags(tx.tags)["Module-Format"]
164
+ }
165
+ }
166
+ } else {
167
+ format = _wasm.format
168
+ }
169
+ format ??= "wasm64-unknown-emscripten-draft_2024_02_15"
170
+ return { format, mod, wasm }
171
+ }
172
+ }
package/esm/armem-web.js CHANGED
@@ -1,238 +1,14 @@
1
- import _Arweave from "arweave"
2
- import { is, last, keys } from "ramda"
3
- import { tags, buildTags, dirname } from "./utils.js"
4
- const Arweave = _Arweave.default ?? _Arweave
5
- import lf from "localforage"
6
1
  import wdb from "./lua/weavedb-lite.js"
2
+ import Base from "./armem-base.js"
3
+ import db from "./lfdb.js"
7
4
 
8
- export default class ArMem {
9
- constructor({ MU_URL, CU_URL, SU_URL, GATEWAY_URL, scheduler, cache } = {}) {
10
- this.__type__ = "mem"
11
- this.isInit = false
12
- this.keyInit = false
13
- this.keys = {}
14
- this.db = {
15
- put: async (key, val) => {
16
- let _val = null
17
- if (typeof val === "object" && !is(Array, val)) {
18
- _val = {}
19
- for (let k in val) {
20
- if (typeof val[k] !== "function") {
21
- if (is(Array, val[k])) {
22
- let _arr = []
23
- for (let v of val[k]) {
24
- if (
25
- typeof v === "object" &&
26
- !is(Array, v) &&
27
- !(v instanceof Uint8Array) &&
28
- !(v instanceof ArrayBuffer)
29
- ) {
30
- let __val = {}
31
- for (let k2 in v) {
32
- if (typeof v[k2] !== "function") __val[k2] = v[k2]
33
- }
34
- _arr.push(__val)
35
- } else {
36
- _arr.push(v)
37
- }
38
- }
39
- _val[k] = _arr
40
- } else if (
41
- typeof val[k] === "object" &&
42
- !(val[k] instanceof Uint8Array) &&
43
- !(val[k] instanceof ArrayBuffer)
44
- ) {
45
- let __val = {}
46
- for (let k2 in val[k]) {
47
- if (typeof val[k][k2] !== "function") __val[k2] = val[k][k2]
48
- }
49
- _val[k] = __val
50
- } else {
51
- _val[k] = val[k]
52
- }
53
- }
54
- }
55
- } else _val = val
56
- await lf.setItem(key, _val)
57
- this.keys[key] = true
58
- await lf.setItem("keys", keys(this.keys))
59
- },
60
- get: async key => {
61
- return await lf.getItem(key)
62
- },
63
- getKeys: async ({ start, end }) => {
64
- if (!this.keyInit) {
65
- const _keys = (await lf.getItem("keys")) ?? []
66
- this.keys = {}
67
- for (const v of _keys) this.keys[v] = true
68
- }
69
- let __keys = []
70
- for (const k in this.keys) {
71
- const sp = k.split(".")
72
- if (sp[0] === start) __keys.push(k)
73
- }
74
- return __keys
75
- },
76
- }
77
- this.arweave = Arweave.init()
78
- this.arweave.transactions.getTransactionAnchor = () => this.getAnchor()
79
- this.arweave.transactions.getPrice = () => 0
80
- this.scheduler = scheduler
81
- this.SU_URL = SU_URL
5
+ export default class ArMem extends Base {
6
+ constructor(args = {}) {
7
+ super(args)
8
+ this.db = db(this)
82
9
  this.initSync()
83
10
  }
84
- async putAll(key) {
85
- for (let k in this[key]) await this.set(this[key][k], key, k)
86
- }
87
- async init() {
88
- if (this.isInit) return
89
- this.isInit = true
90
- if (this.db) {
91
- for (const v of ["height", "blocks"]) this[v] = await this.get(v)
92
- for (const v of [
93
- "txs",
94
- "jwks",
95
- "env",
96
- "modules",
97
- "wasms",
98
- "addrmap",
99
- "blockmap",
100
- "modmap",
101
- "msgs",
102
- ]) {
103
- this[v] ??= {}
104
- const items = await this.db.getKeys({ start: v, end: v + "a" })
105
- for (const v2 of items || []) {
106
- const key = v2.split(".")[0]
107
- const field = v2.split(".")[1]
108
- if (key === v) this[v][field] = await this.db.get(v2)
109
- }
110
- }
111
- } else {
112
- for (const v of ["height", "blocks"]) await this.set(this[v], v)
113
- for (const v of ["modules", "wasms", "addrmap", "blockmap"]) {
114
- await this.putAll(v)
115
- }
116
- }
117
- }
118
- async getWasm(module) {
119
- let mod = module ?? this.modules.aos2_0_1
120
- if (!mod) throw Error("module not found")
121
- const __dirname = await dirname()
122
- let format = null
123
- let _wasm = await this.wasms[mod]
124
- let wasm = _wasm?.data
125
- if (!wasm) {
126
- if (_wasm?.file) {
127
- wasm = Buffer.from(wdb, "base64")
128
- format = _wasm.format
129
- } else {
130
- const tx = await this.get("txs", mod)
131
- if (tx) {
132
- wasm = Buffer.from(tx.data, "base64")
133
- format = tags(tx.tags)["Module-Format"]
134
- }
135
- }
136
- } else {
137
- format = _wasm.format
138
- }
139
- format ??= "wasm64-unknown-emscripten-draft_2024_02_15"
140
- return { format, mod, wasm }
141
- }
142
- initSync() {
143
- this.addrmap = {}
144
- this.txs = {}
145
- this.jwks = {}
146
- this.blocks = []
147
- this.blockmap = {}
148
- this.env = {}
149
- this.modules = {
150
- aos2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
151
- aos1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
152
- sqlite: "ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw",
153
- }
154
- this.modmap = {}
155
- this.msgs = {}
156
- this.wasms = {
157
- "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM": {
158
- file: "aos2_0_1",
159
- format: "wasm64-unknown-emscripten-draft_2024_02_15",
160
- },
161
- cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo: {
162
- file: "aos_1",
163
- format: "wasm64-unknown-emscripten-draft_2024_02_15",
164
- },
165
- ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw: {
166
- file: "sqlite",
167
- format: "wasm64-unknown-emscripten-draft_2024_02_15",
168
- },
169
- }
170
- let txs = []
171
- for (const k in this.modules) {
172
- const key = this.modules[k]
173
- txs.push(key)
174
- this.txs[key] = {
175
- id: key,
176
- block: 0,
177
- tags: buildTags(null, {
178
- "Data-Protocol": "ao",
179
- Variant: "ao.TN.1",
180
- Type: "Module",
181
- "Module-Format": "wasm64-unknown-emscripten-draft_2024_02_15",
182
- "Input-Encoding": "JSON-V1",
183
- "Output-Encoding": "JSON-V1",
184
- "Memory-Limit": "1-gb",
185
- "Compute-Limit": "9000000000000",
186
- Extension: "WeaveDrive",
187
- }),
188
- }
189
- }
190
- if (this.scheduler && this.SU_URL) {
191
- const key = this.scheduler
192
- txs.push(key)
193
- this.addrmap[this.scheduler] = { address: this.scheduler }
194
- this.txs[key] = {
195
- id: key,
196
- block: 0,
197
- owner: this.scheduler,
198
- tags: buildTags(null, {
199
- "Data-Protocol": "ao",
200
- Variant: "ao.TN.1",
201
- Type: "Scheduler-Location",
202
- Url: this.SU_URL,
203
- "Time-To-Live": 1000 * 60 * 60 * 24 * 365 * 10,
204
- }),
205
- }
206
- }
207
- this.blockmap["0"] = {
208
- txs,
209
- timestamp: Date.now(),
210
- height: 0,
211
- previous: "",
212
- id: "0",
213
- }
214
- this.blocks.push("0")
215
- this.height = 1
216
- }
217
- getAnchor() {
218
- return this.blocks.length === 0
219
- ? "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM"
220
- : last(this.blockmap[last(this.blocks)].txs)
221
- }
222
- async get(key, field) {
223
- await this.init()
224
- if (!field) return this[key]
225
- return this[key]?.[field]
226
- }
227
- async set(val, key, field) {
228
- await this.init()
229
- if (!field) {
230
- this[key] = val
231
- if (this.db) await this.db.put(`${key}`, this[key])
232
- } else {
233
- this[key] ??= {}
234
- this[key][field] = val
235
- if (this.db) await this.db.put(`${key}.${field}`, this[key][field])
236
- }
11
+ async _getWasm(file) {
12
+ return Buffer.from(wdb, "base64")
237
13
  }
238
14
  }
package/esm/armem.js CHANGED
@@ -4,173 +4,17 @@ import { tags, buildTags, dirname } from "./utils.js"
4
4
  import { open } from "lmdb"
5
5
  import { readFileSync } from "fs"
6
6
  import { resolve } from "path"
7
+ import Base from "./armem-base.js"
7
8
 
8
- export default class ArMem {
9
- constructor({ MU_URL, CU_URL, SU_URL, GATEWAY_URL, scheduler, cache } = {}) {
10
- this.__type__ = "mem"
11
- this.isInit = false
9
+ export default class ArMem extends Base {
10
+ constructor(args = {}) {
11
+ const { cache } = args
12
+ super(args)
12
13
  if (cache) this.db = open({ path: cache, compression: true })
13
- this.arweave = Arweave.init()
14
- this.arweave.transactions.getTransactionAnchor = () => this.getAnchor()
15
- this.arweave.transactions.getPrice = () => 0
16
- this.scheduler = scheduler
17
- this.SU_URL = SU_URL
18
14
  this.initSync()
19
15
  }
20
- async putAll(key) {
21
- for (let k in this[key]) await this.set(this[key][k], key, k)
22
- }
23
- async init() {
24
- if (this.isInit) return
25
- this.isInit = true
26
- if (this.db) {
27
- for (const v of ["height", "blocks"]) this[v] = await this.get(v)
28
- for (const v of [
29
- "txs",
30
- "jwks",
31
- "env",
32
- "modules",
33
- "wasms",
34
- "addrmap",
35
- "blockmap",
36
- "modmap",
37
- "msgs",
38
- ]) {
39
- this[v] ??= {}
40
- const items = await this.db.getKeys({ start: v, end: v + "a" })
41
- for (const v2 of items) {
42
- const key = v2.split(".")[0]
43
- const field = v2.split(".")[1]
44
- if (key === v) this[v][field] = await this.db.get(v2)
45
- }
46
- }
47
- } else {
48
- for (const v of ["height", "blocks"]) await this.set(this[v], v)
49
- for (const v of ["modules", "wasms", "addrmap", "blockmap"]) {
50
- await this.putAll(v)
51
- }
52
- }
53
- }
54
- async getWasm(module) {
55
- let mod = module ?? this.modules.aos2_0_1
56
- if (!mod) throw Error("module not found")
16
+ async _getWasm(file) {
57
17
  const __dirname = await dirname()
58
- let format = null
59
- let _wasm = await this.wasms[mod]
60
- let wasm = _wasm?.data
61
- if (!wasm) {
62
- if (_wasm?.file) {
63
- wasm = readFileSync(
64
- resolve(__dirname, `lua/${this.wasms[mod].file}.wasm`),
65
- )
66
- format = _wasm.format
67
- } else {
68
- const tx = await this.get("txs", mod)
69
- if (tx) {
70
- wasm = Buffer.from(tx.data, "base64")
71
- format = tags(tx.tags)["Module-Format"]
72
- }
73
- }
74
- } else {
75
- format = _wasm.format
76
- }
77
- format ??= "wasm64-unknown-emscripten-draft_2024_02_15"
78
- return { format, mod, wasm }
79
- }
80
- initSync() {
81
- this.addrmap = {}
82
- this.txs = {}
83
- this.jwks = {}
84
- this.blocks = []
85
- this.blockmap = {}
86
- this.env = {}
87
- this.modules = {
88
- aos2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
89
- aos1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
90
- sqlite: "ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw",
91
- }
92
- this.modmap = {}
93
- this.msgs = {}
94
- this.wasms = {
95
- "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM": {
96
- file: "aos2_0_1",
97
- format: "wasm64-unknown-emscripten-draft_2024_02_15",
98
- },
99
- cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo: {
100
- file: "aos_1",
101
- format: "wasm64-unknown-emscripten-draft_2024_02_15",
102
- },
103
- ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw: {
104
- file: "sqlite",
105
- format: "wasm64-unknown-emscripten-draft_2024_02_15",
106
- },
107
- }
108
- let txs = []
109
- for (const k in this.modules) {
110
- const key = this.modules[k]
111
- txs.push(key)
112
- this.txs[key] = {
113
- id: key,
114
- block: 0,
115
- tags: buildTags(null, {
116
- "Data-Protocol": "ao",
117
- Variant: "ao.TN.1",
118
- Type: "Module",
119
- "Module-Format": "wasm64-unknown-emscripten-draft_2024_02_15",
120
- "Input-Encoding": "JSON-V1",
121
- "Output-Encoding": "JSON-V1",
122
- "Memory-Limit": "1-gb",
123
- "Compute-Limit": "9000000000000",
124
- Extension: "WeaveDrive",
125
- }),
126
- }
127
- }
128
- if (this.scheduler && this.SU_URL) {
129
- const key = this.scheduler
130
- txs.push(key)
131
- this.addrmap[this.scheduler] = { address: this.scheduler }
132
- this.txs[key] = {
133
- id: key,
134
- block: 0,
135
- owner: this.scheduler,
136
- tags: buildTags(null, {
137
- "Data-Protocol": "ao",
138
- Variant: "ao.TN.1",
139
- Type: "Scheduler-Location",
140
- Url: this.SU_URL,
141
- "Time-To-Live": 1000 * 60 * 60 * 24 * 365 * 10,
142
- }),
143
- }
144
- }
145
- this.blockmap["0"] = {
146
- txs,
147
- timestamp: Date.now(),
148
- height: 0,
149
- previous: "",
150
- id: "0",
151
- }
152
- this.blocks.push("0")
153
- this.height = 1
154
- }
155
- getAnchor() {
156
- return this.blocks.length === 0
157
- ? "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM"
158
- : last(this.blockmap[last(this.blocks)].txs)
159
- }
160
- async get(key, field) {
161
- await this.init()
162
- if (!field) return this[key]
163
- return this[key]?.[field]
164
- }
165
- async set(val, key, field) {
166
- await this.init()
167
- if (!field) {
168
- this[key] = val
169
- if (this.db) await this.db.put(`${key}`, this[key])
170
- } else {
171
- this[key] ??= {}
172
- this[key][field] = val
173
- if (this.db) await this.db.put(`${key}.${field}`, this[key][field])
174
- }
18
+ return readFileSync(resolve(__dirname, `lua/${file}.wasm`))
175
19
  }
176
20
  }