@peerbit/server 5.4.1 → 5.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/server",
3
- "version": "5.4.1",
3
+ "version": "5.4.2",
4
4
  "author": "dao.xyz",
5
5
  "repository": {
6
6
  "type": "git",
package/src/cli.ts CHANGED
@@ -999,10 +999,23 @@ export const cli = async (args?: string[]) => {
999
999
  handler: async (args) => {
1000
1000
  for (const api of apis) {
1001
1001
  const list = await api.api.program.list();
1002
- api.log(`Running programs (${list.length}):`);
1003
- list.forEach((p) => {
1004
- api.log(chalk.green(p));
1005
- });
1002
+ api.log(
1003
+ `Running programs (${[...Object.keys(list)].length}):`,
1004
+ );
1005
+ for (const key of Object.keys(list)) {
1006
+ let programOpenArgs = list[key];
1007
+ let argsString = "";
1008
+ if (
1009
+ programOpenArgs &&
1010
+ Object.keys(programOpenArgs).length > 0
1011
+ ) {
1012
+ // show but indent
1013
+ argsString =
1014
+ " " + JSON.stringify(programOpenArgs);
1015
+ }
1016
+
1017
+ api.log(chalk.green(key + argsString));
1018
+ }
1006
1019
  }
1007
1020
  },
1008
1021
  })
package/src/client.ts CHANGED
@@ -225,13 +225,13 @@ export const createClient = async (
225
225
  await Promise.all(resp.data.map((address: string) => drop(address)));
226
226
  },
227
227
 
228
- list: async (): Promise<string[]> => {
228
+ list: async (): Promise<Record<string, Record<string, any> | null>> => {
229
229
  const resp = throwIfNot200(
230
230
  await axiosInstance.get(endpoint + PROGRAMS_PATH, {
231
231
  validateStatus,
232
232
  }),
233
233
  );
234
- return resp.data as string[];
234
+ return resp.data as Record<string, Record<string, any> | null>;
235
235
  },
236
236
  variants: async (): Promise<string[]> => {
237
237
  const resp = throwIfNot200(
package/src/server.ts CHANGED
@@ -39,7 +39,7 @@ import {
39
39
  TRUST_PATH,
40
40
  VERSIONS_PATH,
41
41
  } from "./routes.js";
42
- import { Session } from "./session.js";
42
+ import { JSONArgs, Session } from "./session.js";
43
43
  import { getBody, verifyRequest } from "./signed-request.js";
44
44
  import { Trust } from "./trust.js";
45
45
  import type {
@@ -154,10 +154,13 @@ export const startServerWithNode = async (properties: {
154
154
  for (const [string] of await session.imports.all()) {
155
155
  await import(string);
156
156
  }
157
- for (const [address] of await session.programs.all()) {
157
+ for (const [address, args] of await session.programs.all()) {
158
158
  // TODO args
159
159
  try {
160
- await peer.open(address, { timeout: 3000 });
160
+ await peer.open<any>(address, {
161
+ args: args?.length > 0 ? JSONArgs.from(args) : undefined,
162
+ timeout: 1e4,
163
+ });
161
164
  } catch (error) {
162
165
  console.error(error);
163
166
  }
@@ -365,10 +368,19 @@ export const startApiServer = async (
365
368
  try {
366
369
  const ref: any =
367
370
  (client as Peerbit).handler?.items?.keys() || [];
368
- const keys = JSON.stringify([...ref]);
371
+ let args: Record<string, Record<string, any> | null> = {};
372
+ for (const key of ref) {
373
+ const argsBytes =
374
+ await properties.session?.programs.get(key);
375
+ if (argsBytes && argsBytes.length > 0) {
376
+ args[key] = JSONArgs.from(argsBytes).args;
377
+ } else {
378
+ args[key] = null;
379
+ }
380
+ }
369
381
  res.setHeader("Content-Type", "application/json");
370
382
  res.writeHead(200);
371
- res.end(keys);
383
+ res.end(JSON.stringify(args));
372
384
  } catch (error: any) {
373
385
  res.writeHead(404);
374
386
  res.end(error.message);
@@ -489,18 +501,19 @@ export const startApiServer = async (
489
501
  Program,
490
502
  );
491
503
  }
504
+ let extraArgsParsed =
505
+ Object.keys(extraArgs).length > 0 ? extraArgs : undefined;
492
506
  client
493
507
  .open(program, {
494
- args:
495
- Object.keys(extraArgs).length > 0
496
- ? extraArgs
497
- : undefined,
508
+ args: extraArgsParsed,
498
509
  })
499
510
  .then(async (program) => {
500
511
  // TODO what if this is a reopen?
501
512
  await properties?.session?.programs.add(
502
513
  program.address,
503
- new Uint8Array(),
514
+ extraArgsParsed
515
+ ? new JSONArgs(extraArgsParsed).bytes
516
+ : new Uint8Array(),
504
517
  );
505
518
  res.writeHead(200);
506
519
  res.end(program.address.toString());
package/src/session.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { deserialize, field, serialize, variant } from "@dao-xyz/borsh";
1
2
  import type { AbstractLevel } from "abstract-level";
2
3
 
3
4
  export class Session {
@@ -30,6 +31,47 @@ export class Session {
30
31
  }
31
32
  }
32
33
 
34
+ const encoder = new TextEncoder();
35
+ const decoder = new TextDecoder();
36
+
37
+ @variant(0)
38
+ export class JSONArgs {
39
+ @field({ type: Uint8Array })
40
+ private _args: Uint8Array;
41
+
42
+ cache: Record<string, any> | undefined;
43
+ constructor(
44
+ args: Record<
45
+ string,
46
+ string | number | bigint | Uint8Array | boolean | null | undefined
47
+ >,
48
+ ) {
49
+ this.cache = args;
50
+ this._args = this.encode(args);
51
+ }
52
+ private encode(obj: any): Uint8Array {
53
+ return new Uint8Array(encoder.encode(JSON.stringify(obj)));
54
+ }
55
+
56
+ private decode(bytes: Uint8Array) {
57
+ return (this.cache = JSON.parse(decoder.decode(bytes).toString()));
58
+ }
59
+
60
+ get args(): Record<
61
+ string,
62
+ string | number | bigint | Uint8Array | boolean | null | undefined
63
+ > {
64
+ return this.cache || this.decode(this._args);
65
+ }
66
+
67
+ get bytes(): Uint8Array {
68
+ return serialize(this);
69
+ }
70
+
71
+ static from(bytes: Uint8Array): JSONArgs {
72
+ return deserialize(bytes, JSONArgs);
73
+ }
74
+ }
33
75
  export class KV {
34
76
  constructor(
35
77
  readonly level: AbstractLevel<
@@ -39,6 +81,9 @@ export class KV {
39
81
  >,
40
82
  ) {}
41
83
 
84
+ get(key: string): Promise<Uint8Array | undefined> {
85
+ return this.level.get(key);
86
+ }
42
87
  add(key: string, arg: Uint8Array) {
43
88
  return this.level.put(key, arg);
44
89
  }