@leofcoin/peernet 0.16.7 → 0.17.1

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,225 @@
1
+ import { join } from 'path';
2
+ import { homedir, platform } from 'os';
3
+ import { readdirSync } from 'fs';
4
+ import { execSync } from 'child_process';
5
+ import { ClassicLevel } from 'classic-level';
6
+
7
+ // import base32 from '@vandeurenglenn/base32'
8
+ // import base58 from '@vandeurenglenn/base58'
9
+
10
+ // export const encodings = {
11
+ // base58,
12
+ // base32
13
+ // }
14
+
15
+ const encode$1 = (string, encoding = 'utf-8') => {
16
+ if (typeof string === 'string') {
17
+ let encoded;
18
+
19
+ // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
20
+ encoded = new TextEncoder().encode(string);
21
+ return encoded
22
+ }
23
+ throw Error(`expected typeof String instead got ${string}`)
24
+ };
25
+
26
+ const decode$1 = (uint8Array, encoding) => {
27
+ if (uint8Array instanceof Uint8Array) {
28
+ let decoded;
29
+ // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
30
+ decoded = new TextDecoder().decode(uint8Array);
31
+
32
+ return decoded
33
+ }
34
+ throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
35
+ };
36
+
37
+ class KeyValue {
38
+
39
+ /**
40
+ * @param {string | Uint8Array} input
41
+ */
42
+ constructor(input) {
43
+ if (typeof input === 'string') {
44
+ this.uint8Array = encode$1(input);
45
+ } else if (input instanceof Uint8Array) {
46
+ this.uint8Array = input;
47
+ } else if (input instanceof KeyValue) {
48
+ this.uint8Array = input.uint8Array;
49
+ } else {
50
+ throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue')
51
+ }
52
+ }
53
+
54
+ isKeyValue() {
55
+ return true
56
+ }
57
+
58
+ /**
59
+ * Convert to the string representation
60
+ *
61
+ * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
62
+ * @returns {string}
63
+ */
64
+ toString(encoding = 'utf8') {
65
+ return decode$1(this.uint8Array)
66
+ }
67
+
68
+ }
69
+
70
+ const mkdirp = path => execSync(`mkdir "${platform() === 'win32' ? path.replace(/\//g, '\\') : path}"`);
71
+
72
+ const init = (root, home = true) => {
73
+ let _root;
74
+ if (home) _root = join(homedir(), root);
75
+ if (readdirSync) try {
76
+ readdirSync(_root);
77
+ } catch (e) {
78
+ mkdirp(_root);
79
+ }
80
+
81
+ return _root
82
+ };
83
+
84
+ // import base32 from '@vandeurenglenn/base32'
85
+ // import base58 from '@vandeurenglenn/base58'
86
+
87
+ // export const encodings = {
88
+ // base58,
89
+ // base32
90
+ // }
91
+
92
+ const encode = (string, encoding = 'utf-8') => {
93
+ if (typeof string === 'string') {
94
+ let encoded;
95
+
96
+ // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
97
+ encoded = new TextEncoder().encode(string);
98
+ return encoded
99
+ }
100
+ throw Error(`expected typeof String instead got ${string}`)
101
+ };
102
+
103
+ const decode = (uint8Array, encoding) => {
104
+ if (uint8Array instanceof Uint8Array) {
105
+ let decoded;
106
+ // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
107
+ decoded = new TextDecoder().decode(uint8Array);
108
+
109
+ return decoded
110
+ }
111
+ throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
112
+ };
113
+
114
+ const pathSepS = '/';
115
+ class KeyPath {
116
+
117
+ /**
118
+ * @param {string | Uint8Array} input
119
+ */
120
+ constructor(input) {
121
+ if (typeof input === 'string') {
122
+ this.uint8Array = encode(input);
123
+ } else if (input instanceof Uint8Array) {
124
+ this.uint8Array = input;
125
+ } else if (input instanceof KeyPath) {
126
+ this.uint8Array = input.uint8Array;
127
+ } else {
128
+ throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath')
129
+ }
130
+ }
131
+
132
+ isKeyPath() {
133
+ return true
134
+ }
135
+
136
+ /**
137
+ * Convert to the string representation
138
+ *
139
+ * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
140
+ * @returns {string}
141
+ */
142
+ toString(encoding = 'hex') {
143
+ return decode(this.uint8Array)
144
+ }
145
+
146
+ /**
147
+ * Returns the `list` representation of this path.
148
+ *
149
+ * @returns string[]
150
+ *
151
+ * @example
152
+ * ```js
153
+ * new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
154
+ * // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
155
+ * ```
156
+ */
157
+ list() {
158
+ return this.toString().split(pathSepS).slice(1)
159
+ }
160
+
161
+ }
162
+
163
+ class Store {
164
+ constructor(name = 'storage', root, version = 'v1.0.0') {
165
+ this.name = name;
166
+ this.root = init(root);
167
+ this.version = version;
168
+
169
+ this.db = new ClassicLevel(join(this.root, this.name), { valueEncoding: 'view'});
170
+ }
171
+
172
+ toKeyPath(key) {
173
+ if (!key.isKeyPath()) key = new KeyPath(key);
174
+ return key.toString('base32')
175
+ }
176
+
177
+ toKeyValue(value) {
178
+ if (!value.isKeyValue()) value = new KeyValue(value);
179
+ return value.uint8Array
180
+ }
181
+
182
+ async get(key) {
183
+ return this.db.get(this.toKeyPath(key))
184
+ }
185
+
186
+ async put(key, value) {
187
+ return this.db.put(this.toKeyPath(key), this.toKeyValue(value))
188
+ }
189
+
190
+ async delete(key) {
191
+ return this.db.del(this.toKeyPath(key))
192
+ }
193
+
194
+ async clear() {
195
+ return this.db.clear()
196
+ }
197
+
198
+ async values(limit = -1) {
199
+ const values = [];
200
+ for await (const value of this.db.values({limit})) {
201
+ values.push(value);
202
+ }
203
+ return values
204
+ }
205
+
206
+ async keys(limit = -1) {
207
+ const keys = [];
208
+ for await (const key of this.db.keys({limit})) {
209
+ keys.push(key);
210
+ }
211
+ return keys
212
+ }
213
+
214
+ /**
215
+ *
216
+ * @param {object} options { limit, gt, lt, reverse }
217
+ * @returns
218
+ */
219
+ iterate(options) {
220
+ return this.db.iterator(options)
221
+ }
222
+
223
+ }
224
+
225
+ export { Store as default };
package/index.html CHANGED
@@ -9,9 +9,9 @@
9
9
  </script> -->
10
10
 
11
11
  <script type="module">
12
- import Peernet from './dist/browser/peernet.mjs';
12
+ import Peernet from './dist/peernet.js';
13
13
  (async () => {
14
- const peernet = await new Peernet()
14
+ const peernet = await new Peernet({network: 'leofcoin:peach', networkVersion: 'peach'})
15
15
  peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: new TextEncoder().encode(100)}))
16
16
  })()
17
17
  </script>
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.16.7",
3
+ "version": "0.17.1",
4
4
  "description": "",
5
5
  "main": "src/peernet.js",
6
+ "exports": {
7
+ ".": "./exports/peernet.js",
8
+ "./browser": "./exports/browser/peernet.js"
9
+ },
6
10
  "type": "module",
7
11
  "engines": {
8
12
  "node": ">=19.0.0"
9
13
  },
10
14
  "scripts": {
15
+ "build": "rollup -c",
16
+ "watch": "rollup -c -w",
11
17
  "set-flags": "set NODE_OPTIONS=--openssl-legacy-provider",
12
18
  "test": "npm run set-flags && node test/index.js",
13
19
  "server": "discovery-swarm-webrtc --port=4000",
@@ -19,20 +25,29 @@
19
25
  "license": "MIT",
20
26
  "dependencies": {
21
27
  "@leofcoin/codec-format-interface": "^1.4.0",
22
- "@leofcoin/generate-account": "^1.0.4",
23
- "@leofcoin/multi-wallet": "^2.1.2",
28
+ "@leofcoin/generate-account": "^1.2.0",
29
+ "@leofcoin/multi-wallet": "^3.0.1",
24
30
  "@leofcoin/peernet-swarm": "^0.5.0",
25
31
  "@leofcoin/storage": "^3.0.0",
32
+ "@types/node": "^18.11.18",
26
33
  "@vandeurenglenn/base32": "^1.1.0",
27
34
  "@vandeurenglenn/base58": "^1.1.0",
28
35
  "@vandeurenglenn/debug": "^1.0.0",
29
36
  "@vandeurenglenn/is-hex": "^1.0.0",
30
37
  "@vandeurenglenn/little-pubsub": "^1.3.1",
38
+ "multi-signature": "^1.3.1",
31
39
  "socket-request-client": "^1.5.0",
32
40
  "socket-request-server": "^1.5.0"
33
41
  },
34
42
  "devDependencies": {
43
+ "@rollup/plugin-json": "^6.0.0",
44
+ "@rollup/plugin-node-resolve": "^15.0.1",
45
+ "@rollup/plugin-wasm": "^6.1.1",
46
+ "@types/bs58check": "^2.1.0",
47
+ "@types/secp256k1": "^4.0.3",
48
+ "@types/varint": "^6.0.1",
35
49
  "eslint": "^8.29.0",
36
- "eslint-plugin-unicorn": "^45.0.1"
50
+ "eslint-plugin-unicorn": "^45.0.1",
51
+ "rollup": "^3.9.0"
37
52
  }
38
53
  }
@@ -0,0 +1,30 @@
1
+ import resolve from '@rollup/plugin-node-resolve'
2
+ import commonjs from '@rollup/plugin-commonjs'
3
+ import json from '@rollup/plugin-json'
4
+ import wasm from '@rollup/plugin-wasm'
5
+ import rimraf from 'rimraf'
6
+
7
+ rimraf.sync('./exports/**')
8
+
9
+
10
+ export default [{
11
+ input: ['./src/peernet.ts', './node_modules/@leofcoin/storage/exports/browser-store.js'],
12
+ output: {
13
+ format: 'es',
14
+ dir: './exports/browser'
15
+ },
16
+ plugins: [
17
+ json(),
18
+ wasm(),
19
+ commonjs(),
20
+ resolve({
21
+ mainFields: ["browser", "module", "main"]
22
+ })
23
+ ]
24
+ }, {
25
+ input: ['./src/peernet.ts', './node_modules/@leofcoin/storage/exports/store.js'],
26
+ output: {
27
+ format: 'es',
28
+ dir: './exports'
29
+ }
30
+ }]
@@ -190,7 +190,7 @@ export default class Peernet {
190
190
 
191
191
  const {daemon, environment} = await target()
192
192
  this.hasDaemon = daemon
193
-
193
+
194
194
  for (const store of this.defaultStores) {
195
195
  await this.addStore(store, options.storePrefix, options.root)
196
196
  }
@@ -253,7 +253,7 @@ export default class Peernet {
253
253
  * @type {PeernetClient}
254
254
  */
255
255
  this.client = new importee.default(this.id, this.networkVersion, this.stars)
256
- if (globalThis.onbeforeunload) {
256
+ if (globalThis.navigator) {
257
257
  globalThis.addEventListener('beforeunload', async () => this.client.close());
258
258
  } else {
259
259
  process.on('SIGTERM', async () => {
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "ES2022",
4
+ "target": "es2022",
5
+ // "noImplicitAny": true,
6
+ "removeComments": true,
7
+ "preserveConstEnums": true,
8
+ "outDir": ".build/",
9
+ "moduleResolution":"NodeNext",
10
+ "sourceMap": true,
11
+ "allowJs": true
12
+ },
13
+ "include": [
14
+ "./src/**/*",
15
+ "@vandeurenglenn/little-pubsub",
16
+ "@leofcoin/storage"
17
+ ],
18
+ "exclude": ["node_modules", "**/*.spec.ts"]
19
+ }
package/src/server.js DELETED
@@ -1,24 +0,0 @@
1
- import server from 'socket-request-server/server';
2
- import socketResponse from 'socket-request-server/response';
3
-
4
- globalThis.peerMap = new Map()
5
-
6
- server({
7
- peernet: (params, response) => {
8
- if (params.join) {
9
- peerMap.set(params.peerId, params.address)
10
- response.send([...peerMap.values()])
11
- for (const connection of connections) {
12
- console.log(connection);
13
- socketResponse(connection, 'peernet', 'peernet').send({
14
- discovered: params.address,
15
- })
16
- }
17
- return
18
- }
19
- if (!params.join) {
20
- peerMap.delete(params.peerId)
21
- return response.send()
22
- }
23
- },
24
- })