core-3nweb-client-lib 0.41.4 → 0.41.6

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.
@@ -1,12 +1,11 @@
1
- import { Caller } from "../../ipc-via-protobuf/connector";
1
+ import { Caller, EnvelopeBody } from "../../ipc-via-protobuf/connector";
2
+ import { FindObjectRef, FindReferencedObj } from "./json-n-binary";
2
3
  type Observer<T> = web3n.Observer<T>;
3
- interface PassedDatum {
4
- bytes?: Uint8Array;
5
- passedByReference?: any[];
6
- }
7
4
  export interface TransformOpts {
8
- unpackReply?: ((reply: PassedDatum | undefined) => any) | 'noop';
9
- packRequest?: ((args: any[]) => PassedDatum | undefined) | 'noop';
5
+ unpackReply?: ((reply: EnvelopeBody) => any) | 'noop';
6
+ packRequest?: ((args: any[]) => EnvelopeBody) | 'noop';
7
+ findRefOf?: FindObjectRef;
8
+ findReferencedObj?: FindReferencedObj;
10
9
  }
11
10
  export declare function makeReqRepFuncCaller<F extends Function>(clientSide: Caller, path: string[], transforms?: TransformOpts): F;
12
11
  export declare function makeReqRepObjCaller<T, M extends keyof T>(clientSide: Caller, objPath: string[], method: M, transforms?: TransformOpts): T[M];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2022 - 2024 3NSoft Inc.
3
+ Copyright (C) 2022 - 2025 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -21,50 +21,44 @@ exports.makeReqRepObjCaller = makeReqRepObjCaller;
21
21
  exports.makeObservableFuncCaller = makeObservableFuncCaller;
22
22
  const rxjs_1 = require("rxjs");
23
23
  const json_n_binary_1 = require("./json-n-binary");
24
- function replyFromPassedDatum(data, unpack) {
25
- if (!data) {
24
+ function replyFromPassedDatum(bytes, transforms) {
25
+ if (!bytes) {
26
26
  return;
27
27
  }
28
- if (unpack) {
29
- if (unpack === 'noop') {
30
- return [data.bytes];
28
+ if (transforms === null || transforms === void 0 ? void 0 : transforms.unpackReply) {
29
+ if (transforms.unpackReply === 'noop') {
30
+ return [bytes];
31
31
  }
32
32
  else {
33
- return unpack(data);
33
+ return transforms.unpackReply(bytes);
34
34
  }
35
35
  }
36
36
  else {
37
- const { bytes, passedByReference } = data;
38
- return (bytes ? (0, json_n_binary_1.deserializeArgs)(bytes, passedByReference)[0] : undefined);
37
+ return (bytes ? (0, json_n_binary_1.deserializeArgs)(bytes, transforms === null || transforms === void 0 ? void 0 : transforms.findReferencedObj)[0] : undefined);
39
38
  }
40
39
  }
41
- function argsToPassedDatum(args, pack) {
40
+ function argsToPassedDatum(args, transforms) {
42
41
  if (args === undefined) {
43
42
  return;
44
43
  }
45
- if (pack) {
46
- if (pack === 'noop') {
44
+ if (transforms === null || transforms === void 0 ? void 0 : transforms.packRequest) {
45
+ if (transforms.packRequest === 'noop') {
47
46
  if (!ArrayBuffer.isView(args[0])) {
48
47
  throw new Error(`Method returned non-binary, while no serialization is set`);
49
48
  }
50
- return { bytes: args[0] };
49
+ return args[0];
51
50
  }
52
- return pack(args);
51
+ return transforms.packRequest(args);
53
52
  }
54
53
  else {
55
- return (0, json_n_binary_1.serializeArgs)(args);
54
+ return (0, json_n_binary_1.serializeArgs)(args, transforms === null || transforms === void 0 ? void 0 : transforms.findRefOf);
56
55
  }
57
56
  }
58
57
  function makeReqRepFuncCaller(clientSide, path, transforms) {
59
58
  return (async (...args) => {
60
- const req = argsToPassedDatum(args, transforms === null || transforms === void 0 ? void 0 : transforms.packRequest);
61
- if (req === null || req === void 0 ? void 0 : req.passedByReference) {
62
- throw new Error(`Passing by reference is notimplemented, yet.`);
63
- }
64
- const reply = await clientSide.startPromiseCall(path, req === null || req === void 0 ? void 0 : req.bytes);
65
- return replyFromPassedDatum({
66
- bytes: reply
67
- }, transforms === null || transforms === void 0 ? void 0 : transforms.unpackReply);
59
+ const bytes = argsToPassedDatum(args, transforms);
60
+ const reply = await clientSide.startPromiseCall(path, bytes);
61
+ return replyFromPassedDatum(reply, transforms);
68
62
  });
69
63
  }
70
64
  function makeReqRepObjCaller(clientSide, objPath, method, transforms) {
@@ -72,15 +66,15 @@ function makeReqRepObjCaller(clientSide, objPath, method, transforms) {
72
66
  }
73
67
  function makeObservableFuncCaller(clientSide, path, transforms) {
74
68
  return (obs, ...args) => {
75
- const req = argsToPassedDatum(args, transforms === null || transforms === void 0 ? void 0 : transforms.packRequest);
69
+ const bytes = argsToPassedDatum(args, transforms);
76
70
  const s = new rxjs_1.Subject();
77
- const unsub = clientSide.startObservableCall(path, req === null || req === void 0 ? void 0 : req.bytes, s);
71
+ const unsub = clientSide.startObservableCall(path, bytes, s);
78
72
  s.subscribe({
79
73
  next: data => {
80
74
  if (!obs.next) {
81
75
  return;
82
76
  }
83
- const ev = replyFromPassedDatum({ bytes: data }, transforms === null || transforms === void 0 ? void 0 : transforms.unpackReply);
77
+ const ev = replyFromPassedDatum(data, transforms);
84
78
  obs.next(ev);
85
79
  },
86
80
  complete: () => { var _a; return (_a = obs.complete) === null || _a === void 0 ? void 0 : _a.call(obs); },
@@ -1,5 +1,5 @@
1
- export declare function serializeArgs(args: any[]): {
2
- bytes: Uint8Array;
3
- passedByReference?: any[];
4
- };
5
- export declare function deserializeArgs(bytes: Uint8Array, passedByReference: any[] | undefined): any[];
1
+ import { ObjectReference } from '../../ipc-via-protobuf/protobuf-msg';
2
+ export type FindObjectRef = (o: any) => ObjectReference<any> | undefined;
3
+ export type FindReferencedObj = (ref: ObjectReference<any>) => any;
4
+ export declare function serializeArgs(args: any[], findRefOf?: FindObjectRef): Buffer;
5
+ export declare function deserializeArgs(bytes: Uint8Array, findReferencedObj?: FindReferencedObj): any[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2022 - 2023 3NSoft Inc.
3
+ Copyright (C) 2022 - 2023, 2025 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -22,13 +22,12 @@ const protobuf_type_1 = require("../../lib-client/protobuf-type");
22
22
  const json_utils_1 = require("../../lib-common/json-utils");
23
23
  const json_ipc_proto_1 = require("../../protos/json-ipc.proto");
24
24
  const valuesType = protobuf_type_1.ProtoType.for(json_ipc_proto_1.json_ipc.ValuesSequence);
25
- function serializeArgs(args) {
26
- const { seq, passedByReference } = argsToValuesSequence(args);
27
- return { bytes: valuesType.pack(seq), passedByReference };
25
+ function serializeArgs(args, findRefOf) {
26
+ const seq = argsToValuesSequence(args, findRefOf);
27
+ return valuesType.pack(seq);
28
28
  }
29
- function argsToValuesSequence(args) {
29
+ function argsToValuesSequence(args, findRefOf) {
30
30
  const seq = { values: [] };
31
- const passedByReference = [];
32
31
  for (const arg of args) {
33
32
  if (arg && (typeof arg === 'object')) {
34
33
  if (ArrayBuffer.isView(arg)) {
@@ -37,13 +36,19 @@ function argsToValuesSequence(args) {
37
36
  });
38
37
  }
39
38
  else if (arg._isObjectFromCore) {
40
- const indexInPassed = addToArray(passedByReference, arg);
39
+ if (!findRefOf) {
40
+ throw new Error(`Function to find reference for object from core is not given`);
41
+ }
42
+ const objRef = findRefOf(arg);
43
+ if (!objRef) {
44
+ throw new Error(`Reference for object from core wasn't found`);
45
+ }
41
46
  seq.values.push({
42
- transferred: { indexInPassed, objLocation: [] }
47
+ transferred: { objLocation: [], objRef }
43
48
  });
44
49
  }
45
50
  else {
46
- seq.values.push(turnToJsonExtractingBinaryAndTransferable(arg, passedByReference));
51
+ seq.values.push(turnToJsonExtractingBinaryAndTransferable(arg, findRefOf));
47
52
  }
48
53
  }
49
54
  else {
@@ -52,24 +57,10 @@ function argsToValuesSequence(args) {
52
57
  });
53
58
  }
54
59
  }
55
- return {
56
- seq,
57
- passedByReference: ((passedByReference.length > 0) ?
58
- passedByReference : undefined)
59
- };
60
- }
61
- function addToArray(arr, o) {
62
- let foundIndex = arr.indexOf(o);
63
- if (foundIndex < 0) {
64
- arr.push(o);
65
- return arr.length - 1;
66
- }
67
- else {
68
- return foundIndex;
69
- }
60
+ return seq;
70
61
  }
71
- function turnToJsonExtractingBinaryAndTransferable(arg, passedByReference) {
72
- const parts = extractNonJsonableFrom(arg, passedByReference);
62
+ function turnToJsonExtractingBinaryAndTransferable(arg, findRefOf) {
63
+ const parts = extractNonJsonableFrom(arg, findRefOf);
73
64
  if (parts) {
74
65
  const { copy, binaryInJson, transferredInJson } = parts;
75
66
  return {
@@ -83,7 +74,7 @@ function turnToJsonExtractingBinaryAndTransferable(arg, passedByReference) {
83
74
  return { json: JSON.stringify(arg) };
84
75
  }
85
76
  }
86
- function extractNonJsonableFrom(arg, passedByReference) {
77
+ function extractNonJsonableFrom(arg, findRefOf) {
87
78
  const nonJsonLocations = findAllNonJsonable(arg);
88
79
  if (!nonJsonLocations) {
89
80
  return;
@@ -95,8 +86,14 @@ function extractNonJsonableFrom(arg, passedByReference) {
95
86
  const nonJson = getValueAtObjLocation(arg, objLocation);
96
87
  setNewValueAtObjLocation(copy, objLocation, null);
97
88
  if (nonJson._isObjectFromCore) {
98
- const indexInPassed = addToArray(passedByReference, nonJson);
99
- transferredInJson.push({ indexInPassed, objLocation });
89
+ if (!findRefOf) {
90
+ throw new Error(`Function to find reference for object from core is not given`);
91
+ }
92
+ const objRef = findRefOf(arg);
93
+ if (!objRef) {
94
+ throw new Error(`Reference for object from core wasn't found`);
95
+ }
96
+ transferredInJson.push({ objLocation, objRef });
100
97
  }
101
98
  else {
102
99
  binaryInJson.push({ arr: nonJson, objLocation });
@@ -155,17 +152,7 @@ function setNewValueAtObjLocation(o, objLocation, newValue) {
155
152
  o[objLocation[0]] = newValue;
156
153
  }
157
154
  }
158
- function getInitAndSetNewValueAt(o, objLocation, newValue) {
159
- const value = o[objLocation[0]];
160
- if (objLocation.length > 1) {
161
- return getInitAndSetNewValueAt(value, objLocation.slice(1), newValue);
162
- }
163
- else {
164
- o[objLocation[0]] = newValue;
165
- return value;
166
- }
167
- }
168
- function deserializeArgs(bytes, passedByReference) {
155
+ function deserializeArgs(bytes, findReferencedObj) {
169
156
  const values = valuesType.unpack(bytes);
170
157
  const args = [];
171
158
  for (const val of values.values) {
@@ -174,15 +161,21 @@ function deserializeArgs(bytes, passedByReference) {
174
161
  args.push(arr.arr);
175
162
  }
176
163
  else if (transferred) {
177
- args.push(getTransferred(transferred.indexInPassed, passedByReference));
164
+ if (!findReferencedObj) {
165
+ throw new Error(`Function to find referenced object is not given`);
166
+ }
167
+ args.push(findReferencedObj(transferred.objRef));
178
168
  }
179
169
  else if ((typeof json === 'string') && (json.length > 0)) {
180
170
  const arg = JSON.parse(json);
181
171
  if (binaryInJson) {
182
172
  attachBinaryArrays(arg, binaryInJson);
183
173
  }
184
- if (transferredInJson) {
185
- attachTransferred(arg, transferredInJson, passedByReference);
174
+ if (Array.isArray(transferredInJson) && (transferredInJson.length > 0)) {
175
+ if (!findReferencedObj) {
176
+ throw new Error(`Function to find referenced object is not given`);
177
+ }
178
+ attachTransferred(arg, transferredInJson, findReferencedObj);
186
179
  }
187
180
  args.push(arg);
188
181
  }
@@ -197,20 +190,8 @@ function attachBinaryArrays(arg, binaryInJson) {
197
190
  setNewValueAtObjLocation(arg, objLocation, arr);
198
191
  }
199
192
  }
200
- function getTransferred(indexInPassed, passedByReference) {
201
- if (!passedByReference) {
202
- // XXX throw error here
203
- throw new Error(`need better error`);
204
- }
205
- const o = passedByReference[indexInPassed];
206
- if (!o || !o._isObjectFromCore) {
207
- // XXX throw error here
208
- throw new Error(`need better error`);
209
- }
210
- return o;
211
- }
212
- function attachTransferred(arg, transferredInJson, passedByReference) {
213
- for (const { indexInPassed, objLocation } of transferredInJson) {
214
- setNewValueAtObjLocation(arg, objLocation, getTransferred(indexInPassed, passedByReference));
193
+ function attachTransferred(arg, transferredInJson, findReferencedObj) {
194
+ for (const { objRef, objLocation } of transferredInJson) {
195
+ setNewValueAtObjLocation(arg, objLocation, findReferencedObj(objRef));
215
196
  }
216
197
  }
@@ -1,14 +1,13 @@
1
- import { ExposedFn } from "../../ipc-via-protobuf/connector";
1
+ import { EnvelopeBody, ExposedFn } from "../../ipc-via-protobuf/connector";
2
+ import { FindObjectRef, FindReferencedObj } from "./json-n-binary";
2
3
  type Observer<T> = web3n.Observer<T>;
3
- interface PassedDatum {
4
- bytes?: Uint8Array;
5
- passedByReference?: any[];
6
- }
7
4
  export type HandleObservingCall<TEvent> = (obs: Observer<TEvent>, ...requestArgs: any[]) => (() => void);
8
5
  export type HandleReqReplyCall = (...requestArgs: any[]) => Promise<any>;
9
6
  export interface TransformOpts {
10
- unpackRequest?: ((req: PassedDatum | undefined) => any[]) | 'noop';
11
- packReply?: ((reply: any) => PassedDatum | undefined) | 'noop';
7
+ unpackRequest?: ((req: EnvelopeBody) => any[]) | 'noop';
8
+ packReply?: ((reply: any) => EnvelopeBody) | 'noop';
9
+ findRefOf?: FindObjectRef;
10
+ findReferencedObj?: FindReferencedObj;
12
11
  }
13
12
  export declare function wrapReqReplySrvMethod<T extends object, M extends keyof T>(srv: T, method: M, transforms?: TransformOpts): ExposedFn;
14
13
  export declare function wrapReqReplyFunc(srv: object, func: HandleReqReplyCall, transforms?: TransformOpts): ExposedFn;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- Copyright (C) 2022 - 2024 3NSoft Inc.
3
+ Copyright (C) 2022 - 2025 3NSoft Inc.
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify it under
6
6
  the terms of the GNU General Public License as published by the Free Software
@@ -37,7 +37,7 @@ function wrapReqReplyFunc(srvOrFn, funcOrTransforms, transforms) {
37
37
  func = funcOrTransforms;
38
38
  }
39
39
  return buf => {
40
- const args = argsFromBuffer(buf, transforms);
40
+ const args = argsFromPassedDatum(buf, transforms);
41
41
  let promise = (args ?
42
42
  func.call(srv, ...args) :
43
43
  func.call(srv));
@@ -53,42 +53,34 @@ function wrapReqReplyFunc(srvOrFn, funcOrTransforms, transforms) {
53
53
  return { promise };
54
54
  };
55
55
  }
56
- function argsFromBuffer(buf, transforms) {
57
- return argsFromPassedDatum({ bytes: buf }, transforms === null || transforms === void 0 ? void 0 : transforms.unpackRequest);
58
- }
59
- function resultToBuffer(data, transforms) {
60
- const sequencePack = toPassedDatum(data, transforms === null || transforms === void 0 ? void 0 : transforms.packReply);
61
- return sequencePack === null || sequencePack === void 0 ? void 0 : sequencePack.bytes;
62
- }
63
- function argsFromPassedDatum(data, unpack) {
64
- if (!data) {
56
+ function argsFromPassedDatum(bytes, transforms) {
57
+ if (!bytes) {
65
58
  return;
66
59
  }
67
- if (unpack) {
68
- if (unpack === 'noop') {
69
- return [data.bytes];
60
+ if (transforms === null || transforms === void 0 ? void 0 : transforms.unpackRequest) {
61
+ if (transforms.unpackRequest === 'noop') {
62
+ return [bytes];
70
63
  }
71
64
  else {
72
- return unpack(data);
65
+ return transforms.unpackRequest(bytes);
73
66
  }
74
67
  }
75
68
  else {
76
- const { bytes, passedByReference } = data;
77
- return (bytes ? (0, json_n_binary_1.deserializeArgs)(bytes, passedByReference) : undefined);
69
+ return (bytes ? (0, json_n_binary_1.deserializeArgs)(bytes) : undefined);
78
70
  }
79
71
  }
80
- function toPassedDatum(data, pack) {
72
+ function resultToBuffer(data, transforms) {
81
73
  if (data === undefined) {
82
74
  return;
83
75
  }
84
- if (pack) {
85
- if (pack === 'noop') {
76
+ if (transforms === null || transforms === void 0 ? void 0 : transforms.packReply) {
77
+ if (transforms.packReply === 'noop') {
86
78
  if (!ArrayBuffer.isView(data)) {
87
79
  throw new Error(`Method returned non-binary, while no serialization is set`);
88
80
  }
89
- return { bytes: data };
81
+ return data;
90
82
  }
91
- return pack(data);
83
+ return transforms.packReply(data);
92
84
  }
93
85
  else {
94
86
  return (0, json_n_binary_1.serializeArgs)([data]);
@@ -107,7 +99,7 @@ function wrapObservingFunc(srvOrFn, funcOrTransforms, transforms) {
107
99
  func = funcOrTransforms;
108
100
  }
109
101
  return buf => {
110
- const args = argsFromBuffer(buf, transforms);
102
+ const args = argsFromPassedDatum(buf, transforms);
111
103
  const s = new rxjs_1.Subject();
112
104
  const obs = s.asObservable().pipe((0, rxjs_1.map)(ev => resultToBuffer(ev, transforms)));
113
105
  const onCancel = (args ?
@@ -38,6 +38,7 @@ export declare class DeviceFS implements WritableFS, Linkable {
38
38
  static makeWritable(root: string, create?: boolean, exclusive?: boolean): Promise<WritableFS>;
39
39
  private static makeAndWrapWrFS;
40
40
  private static makeAndWrapRoFS;
41
+ static pathOf(item: FS | File): string | undefined;
41
42
  static makeReadonly(root: string): Promise<ReadonlyFS>;
42
43
  readonlySubRoot(folder: string): Promise<ReadonlyFS>;
43
44
  writableSubRoot(folder: string, flags?: web3n.files.FileFlags): Promise<WritableFS>;
@@ -153,6 +153,7 @@ const WRITE_NONEXCL_FLAGS = {
153
153
  exclusive: false,
154
154
  truncate: true
155
155
  };
156
+ const itemToPathMap = new WeakMap();
156
157
  class DeviceFS {
157
158
  constructor(root, writable, name = '') {
158
159
  this.root = root;
@@ -243,12 +244,17 @@ class DeviceFS {
243
244
  }
244
245
  static makeAndWrapWrFS(root, name) {
245
246
  const wrFS = (0, files_1.wrapWritableFS)(new DeviceFS(root, true, name));
247
+ itemToPathMap.set(wrFS, root);
246
248
  return wrFS;
247
249
  }
248
250
  static makeAndWrapRoFS(root, name) {
249
251
  const roFS = (0, files_1.wrapReadonlyFS)(new DeviceFS(root, false, name));
252
+ itemToPathMap.set(roFS, root);
250
253
  return roFS;
251
254
  }
255
+ static pathOf(item) {
256
+ return itemToPathMap.get(item);
257
+ }
252
258
  static async makeReadonly(root) {
253
259
  await checkFolderPresence(root);
254
260
  const folderName = pathMod.basename(root);
@@ -693,7 +699,9 @@ class DeviceFS {
693
699
  }
694
700
  async readonlyFile(path) {
695
701
  await this.checkFilePresence(path, true);
696
- return (0, files_1.wrapReadonlyFile)(new FileObject(this, path, true, false));
702
+ const roFile = (0, files_1.wrapReadonlyFile)(new FileObject(this, path, true, false));
703
+ itemToPathMap.set(roFile, this.fullPath(path));
704
+ return roFile;
697
705
  }
698
706
  async writableFile(path, flags = WRITE_NONEXCL_FLAGS) {
699
707
  const exists = await this.checkFilePresence(path);
@@ -703,7 +711,9 @@ class DeviceFS {
703
711
  if (!exists && !flags.create) {
704
712
  throw (0, file_1.makeFileException)('notFound', path);
705
713
  }
706
- return (0, files_1.wrapWritableFile)(new FileObject(this, path, exists, true));
714
+ const wrFile = (0, files_1.wrapWritableFile)(new FileObject(this, path, exists, true));
715
+ itemToPathMap.set(wrFile, this.fullPath(path));
716
+ return wrFile;
707
717
  }
708
718
  watchFolder(path, observer) {
709
719
  const fullFolderPath = this.fullPath(path, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-3nweb-client-lib",
3
- "version": "0.41.4",
3
+ "version": "0.41.6",
4
4
  "description": "3NWeb client core library, embeddable into different environments",
5
5
  "main": "build/lib-index.js",
6
6
  "types": "build/lib-index.d.ts",