@dxos/codec-protobuf 2.33.9-dev.e605934d → 2.33.9-dev.eb69ac10

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.
Files changed (57) hide show
  1. package/README.md +39 -0
  2. package/dist/{index.js → browser.js} +3983 -197
  3. package/dist/src/buffer-patch.d.ts +1 -0
  4. package/dist/src/buffer-patch.d.ts.map +1 -1
  5. package/dist/src/buffer-patch.js +15 -0
  6. package/dist/src/buffer-patch.js.map +1 -0
  7. package/dist/src/codec.js +53 -0
  8. package/dist/src/codec.js.map +1 -0
  9. package/dist/src/common.js +6 -0
  10. package/dist/src/common.js.map +1 -0
  11. package/dist/src/encoding.js +35 -0
  12. package/dist/src/encoding.js.map +1 -0
  13. package/dist/src/index.d.ts +2 -0
  14. package/dist/src/index.d.ts.map +1 -1
  15. package/dist/src/index.js +31 -0
  16. package/dist/src/index.js.map +1 -0
  17. package/dist/src/interface.js +14 -0
  18. package/dist/src/interface.js.map +1 -0
  19. package/dist/src/mapping.d.ts +3 -0
  20. package/dist/src/mapping.d.ts.map +1 -1
  21. package/dist/src/mapping.js +72 -0
  22. package/dist/src/mapping.js.map +1 -0
  23. package/dist/src/precompiled-mapping/codegen.js +86 -0
  24. package/dist/src/precompiled-mapping/codegen.js.map +1 -0
  25. package/dist/src/precompiled-mapping/create-message-mapper.js +132 -0
  26. package/dist/src/precompiled-mapping/create-message-mapper.js.map +1 -0
  27. package/dist/src/sanitizer.js +53 -0
  28. package/dist/src/sanitizer.js.map +1 -0
  29. package/dist/src/schema.js +86 -0
  30. package/dist/src/schema.js.map +1 -0
  31. package/dist/src/service.d.ts +2 -2
  32. package/dist/src/service.d.ts.map +1 -1
  33. package/dist/src/service.js +113 -0
  34. package/dist/src/service.js.map +1 -0
  35. package/dist/src/stream.d.ts +31 -0
  36. package/dist/src/stream.d.ts.map +1 -1
  37. package/dist/src/stream.js +186 -0
  38. package/dist/src/stream.js.map +1 -0
  39. package/dist/src/stream.test.js +66 -0
  40. package/dist/src/stream.test.js.map +1 -0
  41. package/dist/src/substitutions/any.js +30 -0
  42. package/dist/src/substitutions/any.js.map +1 -0
  43. package/dist/src/substitutions/index.js +22 -0
  44. package/dist/src/substitutions/index.js.map +1 -0
  45. package/dist/src/substitutions/timestamp.js +19 -0
  46. package/dist/src/substitutions/timestamp.js.map +1 -0
  47. package/dist/test/codec.test.js +67 -0
  48. package/dist/test/codec.test.js.map +1 -0
  49. package/dist/tsconfig.tsbuildinfo +1 -1
  50. package/package.json +10 -7
  51. package/src/encoding.ts +9 -0
  52. package/src/index.ts +2 -0
  53. package/src/mapping.ts +60 -0
  54. package/src/precompiled-mapping/create-message-mapper.ts +41 -3
  55. package/src/service.ts +11 -18
  56. package/src/stream.test.ts +3 -1
  57. package/src/stream.ts +72 -1
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2021 DXOS.org
4
+ //
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
18
+ }) : function(o, v) {
19
+ o["default"] = v;
20
+ });
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
27
+ };
28
+ var __importDefault = (this && this.__importDefault) || function (mod) {
29
+ return (mod && mod.__esModule) ? mod : { "default": mod };
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.Schema = void 0;
33
+ const lodash_merge_1 = __importDefault(require("lodash.merge"));
34
+ const protobufjs_1 = __importStar(require("protobufjs"));
35
+ const codec_1 = require("./codec");
36
+ const mapping_1 = require("./mapping");
37
+ const service_1 = require("./service");
38
+ class Schema {
39
+ constructor(_typesRoot, substitutions) {
40
+ this._typesRoot = _typesRoot;
41
+ this._codecCache = {};
42
+ this._mapping = (0, mapping_1.createMappingDescriptors)(substitutions);
43
+ }
44
+ static fromJson(schema, substitutions = {}) {
45
+ const root = protobufjs_1.default.Root.fromJSON(schema);
46
+ return new Schema(root, substitutions);
47
+ }
48
+ getCodecForType(typeName) {
49
+ var _a;
50
+ var _b, _c;
51
+ if (typeof typeName !== 'string') {
52
+ throw new TypeError('Expected `typeName` argument to be a string');
53
+ }
54
+ const type = this._typesRoot.lookupType(typeName);
55
+ (_a = (_b = this._codecCache)[_c = type.fullName]) !== null && _a !== void 0 ? _a : (_b[_c] = new codec_1.ProtoCodec(type, this._mapping, this));
56
+ return this._codecCache[type.fullName];
57
+ }
58
+ tryGetCodecForType(typeName) {
59
+ var _a;
60
+ var _b, _c;
61
+ if (typeof typeName !== 'string') {
62
+ throw new TypeError('Expected `typeName` argument to be a string');
63
+ }
64
+ const type = this._typesRoot.lookupType(typeName);
65
+ (_a = (_b = this._codecCache)[_c = type.fullName]) !== null && _a !== void 0 ? _a : (_b[_c] = new codec_1.ProtoCodec(type, this._mapping, this));
66
+ return this._codecCache[type.fullName];
67
+ }
68
+ getService(name) {
69
+ if (typeof name !== 'string') {
70
+ throw new TypeError('Expected `name` argument to be a string');
71
+ }
72
+ const service = this._typesRoot.lookupService(name);
73
+ return new service_1.ServiceDescriptor(service, this);
74
+ }
75
+ /**
76
+ * Dynamically add new definitions to this schema.
77
+ */
78
+ addJson(schema) {
79
+ if (!schema.nested) {
80
+ throw new Error('Invalid schema: missing nested object');
81
+ }
82
+ this._typesRoot = protobufjs_1.Root.fromJSON((0, lodash_merge_1.default)(this._typesRoot.toJSON(), schema));
83
+ }
84
+ }
85
+ exports.Schema = Schema;
86
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEF,gEAAiC;AACjC,yDAA8C;AAE9C,mCAAqC;AAErC,uCAAqF;AACrF,uCAA8C;AAE9C,MAAa,MAAM;IAUjB,YACU,UAA2B,EACnC,aAA4B;QADpB,eAAU,GAAV,UAAU,CAAiB;QAHpB,gBAAW,GAA+B,EAAE,CAAA;QAM3D,IAAI,CAAC,QAAQ,GAAG,IAAA,kCAAwB,EAAC,aAAa,CAAC,CAAC;IAC1D,CAAC;IAdD,MAAM,CAAC,QAAQ,CAAqE,MAAW,EAAE,gBAA+B,EAAE;QAChI,MAAM,IAAI,GAAG,oBAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC9C,OAAO,IAAI,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;IACzC,CAAC;IAaD,eAAe,CAA8B,QAAW;;;QACtD,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;YAChC,MAAM,IAAI,SAAS,CAAC,6CAA6C,CAAC,CAAC;SACpE;QACD,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QAClD,YAAA,IAAI,CAAC,WAAW,OAAC,IAAI,CAAC,QAAQ,8CAAM,IAAI,kBAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAC;QAC9E,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAEzC,CAAC;IAED,kBAAkB,CAAE,QAAgB;;;QAClC,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;YAChC,MAAM,IAAI,SAAS,CAAC,6CAA6C,CAAC,CAAC;SACpE;QACD,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QAClD,YAAA,IAAI,CAAC,WAAW,OAAC,IAAI,CAAC,QAAQ,8CAAM,IAAI,kBAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAC;QAC9E,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzC,CAAC;IAED,UAAU,CAA8B,IAAO;QAC7C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YAC5B,MAAM,IAAI,SAAS,CAAC,yCAAyC,CAAC,CAAC;SAChE;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QACpD,OAAO,IAAI,2BAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC9C,CAAC;IAED;;OAEG;IACH,OAAO,CAAE,MAAW;QAClB,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;SAC1D;QACD,IAAI,CAAC,UAAU,GAAG,iBAAI,CAAC,QAAQ,CAAC,IAAA,sBAAK,EAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;IAC3E,CAAC;CACF;AArDD,wBAqDC"}
@@ -2,8 +2,8 @@ import pb from 'protobufjs';
2
2
  import type { Schema } from './schema';
3
3
  import { Stream } from './stream';
4
4
  export interface Any {
5
- 'type_url'?: string;
6
- value?: Uint8Array;
5
+ 'type_url': string;
6
+ value: Uint8Array;
7
7
  }
8
8
  export interface ServiceBackend {
9
9
  call(method: string, request: Any): Promise<Any>;
@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AAO,EAAE,cAAc,EACvB,OAAO,EAAE,EAAE,CAAC,OAAO,EACnB,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC;CAqCtB;AAED,qBAAa,cAAc,CAAC,CAAC,GAAG,EAAE,CAAE,YAAW,cAAc;IAEzD,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,SAAS;gBAFT,QAAQ,EAAE,EAAE,CAAC,OAAO,EACpB,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,EACpB,SAAS,EAAE,CAAC;IAGzB,IAAI,CAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAoB3D,UAAU,CAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;IAkB1D,OAAO,CAAC,cAAc;CAavB"}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2021 DXOS.org
4
+ //
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ServiceHandler = exports.Service = exports.ServiceDescriptor = void 0;
10
+ const node_assert_1 = __importDefault(require("node:assert"));
11
+ const stream_1 = require("./stream");
12
+ class ServiceDescriptor {
13
+ constructor(_service, _schema) {
14
+ this._service = _service;
15
+ this._schema = _schema;
16
+ }
17
+ get serviceProto() {
18
+ return this._service;
19
+ }
20
+ createClient(backend) {
21
+ return new Service(backend, this._service, this._schema);
22
+ }
23
+ createServer(handlers) {
24
+ return new ServiceHandler(this._service, this._schema, handlers);
25
+ }
26
+ }
27
+ exports.ServiceDescriptor = ServiceDescriptor;
28
+ class Service {
29
+ constructor(backend, service, schema) {
30
+ for (const method of service.methodsArray) {
31
+ method.resolve();
32
+ (0, node_assert_1.default)(method.resolvedRequestType);
33
+ (0, node_assert_1.default)(method.resolvedResponseType);
34
+ (0, node_assert_1.default)(!method.requestStream, 'Streaming RPC requests are not supported.');
35
+ // TODO(marik-d): What about primitive types.
36
+ const requestCodec = schema.tryGetCodecForType(method.resolvedRequestType.fullName);
37
+ const responseCodec = schema.tryGetCodecForType(method.resolvedResponseType.fullName);
38
+ const methodName = mapRpcMethodName(method.name);
39
+ if (!method.responseStream) {
40
+ this[methodName] = async (request) => {
41
+ const encoded = requestCodec.encode(request);
42
+ const response = await backend.call(method.name, {
43
+ value: encoded,
44
+ type_url: method.resolvedRequestType.fullName
45
+ });
46
+ return responseCodec.decode(response.value);
47
+ };
48
+ }
49
+ else {
50
+ this[methodName] = (request) => {
51
+ const encoded = requestCodec.encode(request);
52
+ const stream = backend.callStream(method.name, {
53
+ value: encoded,
54
+ type_url: method.resolvedRequestType.fullName
55
+ });
56
+ return stream_1.Stream.map(stream, data => responseCodec.decode(data.value));
57
+ };
58
+ }
59
+ // Set function name so that is properly named in stack traces.
60
+ Object.defineProperty(this[methodName], 'name', { value: methodName });
61
+ }
62
+ }
63
+ }
64
+ exports.Service = Service;
65
+ class ServiceHandler {
66
+ constructor(_service, _schema, _handlers) {
67
+ this._service = _service;
68
+ this._schema = _schema;
69
+ this._handlers = _handlers;
70
+ }
71
+ async call(methodName, request) {
72
+ const { method, requestCodec, responseCodec } = this._getMethodInfo(methodName);
73
+ (0, node_assert_1.default)(!method.requestStream, 'Invalid RPC method call: request streaming mismatch.');
74
+ (0, node_assert_1.default)(!method.responseStream, `Invalid RPC method call: response streaming mismatch. ${methodName}`);
75
+ const mappedMethodName = mapRpcMethodName(methodName);
76
+ const handler = this._handlers[mappedMethodName];
77
+ (0, node_assert_1.default)(handler, `Handler is missing: ${mappedMethodName}`);
78
+ const requestDecoded = requestCodec.decode(request.value);
79
+ const response = await handler.bind(this._handlers)(requestDecoded);
80
+ const responseEncoded = responseCodec.encode(response);
81
+ return {
82
+ value: responseEncoded,
83
+ type_url: method.resolvedResponseType.fullName
84
+ };
85
+ }
86
+ callStream(methodName, request) {
87
+ const { method, requestCodec, responseCodec } = this._getMethodInfo(methodName);
88
+ (0, node_assert_1.default)(!method.requestStream, 'Invalid RPC method call: request streaming mismatch.');
89
+ (0, node_assert_1.default)(method.responseStream, `Invalid RPC method call: response streaming mismatch., ${methodName}`);
90
+ const mappedMethodName = mapRpcMethodName(methodName);
91
+ const handler = this._handlers[mappedMethodName];
92
+ (0, node_assert_1.default)(handler, `Handler is missing: ${mappedMethodName}`);
93
+ const requestDecoded = requestCodec.decode(request.value);
94
+ const responseStream = handler.bind(this._handlers)(requestDecoded);
95
+ return stream_1.Stream.map(responseStream, (data) => ({
96
+ value: responseCodec.encode(data),
97
+ type_url: method.resolvedResponseType.fullName
98
+ }));
99
+ }
100
+ _getMethodInfo(methodName) {
101
+ const method = this._service.methods[methodName];
102
+ (0, node_assert_1.default)(!!method, `Method not found: ${methodName}`);
103
+ method.resolve();
104
+ (0, node_assert_1.default)(method.resolvedRequestType);
105
+ (0, node_assert_1.default)(method.resolvedResponseType);
106
+ const requestCodec = this._schema.tryGetCodecForType(method.resolvedRequestType.fullName);
107
+ const responseCodec = this._schema.tryGetCodecForType(method.resolvedResponseType.fullName);
108
+ return { method, requestCodec, responseCodec };
109
+ }
110
+ }
111
+ exports.ServiceHandler = ServiceHandler;
112
+ const mapRpcMethodName = (name) => name[0].toLocaleLowerCase() + name.substring(1);
113
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/service.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;;;;AAEF,8DAAiC;AAIjC,qCAAkC;AAYlC,MAAa,iBAAiB;IAC5B,YACmB,QAAoB,EACpB,OAAoB;QADpB,aAAQ,GAAR,QAAQ,CAAY;QACpB,YAAO,GAAP,OAAO,CAAa;IACpC,CAAC;IAEJ,IAAI,YAAY;QACd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Ac,GAAI,OAAe,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,cAAc,CAAoB,CAAC;QAChG,OAAO,eAAM,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC,IAAI,EAAO,EAAE,CAAC,CAAC;YAChD,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC;YACjC,QAAQ,EAAE,MAAM,CAAC,oBAAqB,CAAC,QAAQ;SAChD,CAAC,CAAC,CAAC;IACN,CAAC;IAEO,cAAc,CAAE,UAAkB;QACxC,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACjD,IAAA,qBAAM,EAAC,CAAC,CAAC,MAAM,EAAE,qBAAqB,UAAU,EAAE,CAAC,CAAC;QAEpD,MAAM,CAAC,OAAO,EAAE,CAAC;QACjB,IAAA,qBAAM,EAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QACnC,IAAA,qBAAM,EAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;QAEpC,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,kBAAkB,CAAC,MAAM,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;QAC1F,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,kBAAkB,CAAC,MAAM,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QAE5F,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC;IACjD,CAAC;CACF;AA1DD,wCA0DC;AAED,MAAM,gBAAgB,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,iBAAiB,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC"}
@@ -1,8 +1,22 @@
1
1
  declare type Producer<T> = (callbacks: {
2
+ /**
3
+ * Advises that the producer is ready to stream the data.
4
+ * Called automatically with the first call to `next`.
5
+ */
6
+ ready: () => void;
7
+ /**
8
+ * Sends a message into the stream.
9
+ */
2
10
  next: (message: T) => void;
11
+ /**
12
+ * Closes the stream.
13
+ * Optional error can be provided.
14
+ */
3
15
  close: (error?: Error) => void;
4
16
  }) => (() => void) | void;
5
17
  export declare type StreamItem<T> = {
18
+ ready: true;
19
+ } | {
6
20
  data: T;
7
21
  } | {
8
22
  closed: true;
@@ -20,17 +34,34 @@ export declare class Stream<T> {
20
34
  * Consumes the entire stream to the end until it closes and returns a promise with the resulting items.
21
35
  */
22
36
  static consume<T>(stream: Stream<T>): Promise<StreamItem<T>[]>;
37
+ /**
38
+ * Maps all data coming through the stream.
39
+ */
40
+ static map<T, U>(source: Stream<T>, map: (data: T) => U): Stream<U>;
23
41
  private _messageHandler?;
24
42
  private _closeHandler?;
43
+ private _readyHandler?;
25
44
  private _isClosed;
26
45
  private _closeError;
27
46
  private _dispose;
47
+ private _readyPromise;
48
+ private _resolveReadyPromise;
49
+ private _isReady;
28
50
  /**
29
51
  * Buffer messages before subscription. Set to null when buffer is no longer needed.
30
52
  */
31
53
  private _buffer;
32
54
  constructor(producer: Producer<T>);
55
+ private _markAsReady;
33
56
  subscribe(onMessage: (msg: T) => void, onClose?: (error?: Error) => void): void;
57
+ /**
58
+ * Resolves when stream is ready.
59
+ */
60
+ waitUntilReady(): Promise<void>;
61
+ /**
62
+ * Registers a callback to be called when stream is ready.
63
+ */
64
+ onReady(onReady: () => void): void;
34
65
  /**
35
66
  * Close the stream and dispose of any resources.
36
67
  */
@@ -1 +1 @@
1
- {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AASA,aAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,EAAE;IAC7B,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,IAAI,CAAA;IAC1B,KAAK,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,CAAA;CAC/B,KAAK,CAAC,MAAM,IAAI,CAAC,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ACH,KAAK;CAcN"}
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2021 DXOS.org
4
+ //
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Stream = void 0;
10
+ const debug_1 = __importDefault(require("debug"));
11
+ const node_assert_1 = __importDefault(require("node:assert"));
12
+ const log = (0, debug_1.default)('dxos:codec-protobuf:stream');
13
+ /**
14
+ * Represents a typed stream of data.
15
+ *
16
+ * Can only have one subscriber.
17
+ *
18
+ * `close` must be called to clean-up the resources.
19
+ */
20
+ class Stream {
21
+ constructor(producer) {
22
+ this._isClosed = false;
23
+ this._isReady = false;
24
+ /**
25
+ * Buffer messages before subscription. Set to null when buffer is no longer needed.
26
+ */
27
+ this._buffer = [];
28
+ this._readyPromise = new Promise(resolve => {
29
+ this._resolveReadyPromise = resolve;
30
+ });
31
+ const disposeCallback = producer({
32
+ ready: () => {
33
+ this._markAsReady();
34
+ },
35
+ next: msg => {
36
+ if (this._isClosed) {
37
+ log('Stream is closed, dropping message.');
38
+ return;
39
+ }
40
+ this._markAsReady();
41
+ if (this._messageHandler) {
42
+ try {
43
+ this._messageHandler(msg);
44
+ }
45
+ catch (error) {
46
+ // Stop error propagation.
47
+ throwUnhandledRejection(error);
48
+ }
49
+ }
50
+ else {
51
+ (0, node_assert_1.default)(this._buffer);
52
+ this._buffer.push(msg);
53
+ }
54
+ },
55
+ close: err => {
56
+ var _a, _b;
57
+ if (this._isClosed) {
58
+ return;
59
+ }
60
+ this._isClosed = true;
61
+ this._closeError = err;
62
+ (_a = this._dispose) === null || _a === void 0 ? void 0 : _a.call(this);
63
+ try {
64
+ (_b = this._closeHandler) === null || _b === void 0 ? void 0 : _b.call(this, err);
65
+ }
66
+ catch (error) {
67
+ // Stop error propagation.
68
+ throwUnhandledRejection(error);
69
+ }
70
+ }
71
+ });
72
+ if (disposeCallback) {
73
+ this._dispose = disposeCallback;
74
+ }
75
+ }
76
+ /**
77
+ * Consumes the entire stream to the end until it closes and returns a promise with the resulting items.
78
+ */
79
+ static consume(stream) {
80
+ return new Promise(resolve => {
81
+ const items = [];
82
+ stream.onReady(() => {
83
+ items.push({ ready: true });
84
+ });
85
+ stream.subscribe(data => {
86
+ items.push({ data });
87
+ }, error => {
88
+ if (error) {
89
+ items.push({ closed: true, error });
90
+ }
91
+ else {
92
+ items.push({ closed: true });
93
+ }
94
+ resolve(items);
95
+ });
96
+ });
97
+ }
98
+ /**
99
+ * Maps all data coming through the stream.
100
+ */
101
+ static map(source, map) {
102
+ return new Stream(({ ready, next, close }) => {
103
+ source.onReady(ready);
104
+ source.subscribe(data => next(map(data)), close);
105
+ return () => source.close();
106
+ });
107
+ }
108
+ _markAsReady() {
109
+ var _a;
110
+ if (!this._isReady) {
111
+ this._isReady = true;
112
+ (_a = this._readyHandler) === null || _a === void 0 ? void 0 : _a.call(this);
113
+ this._resolveReadyPromise();
114
+ }
115
+ }
116
+ subscribe(onMessage, onClose) {
117
+ (0, node_assert_1.default)(!this._messageHandler, 'Stream is already subscribed to.');
118
+ (0, node_assert_1.default)(!this._closeHandler, 'Stream is already subscribed to.');
119
+ (0, node_assert_1.default)(this._buffer); // Must be not-null.
120
+ for (const message of this._buffer) {
121
+ try {
122
+ onMessage(message);
123
+ }
124
+ catch (error) {
125
+ // Stop error propagation.
126
+ throwUnhandledRejection(error);
127
+ }
128
+ }
129
+ this._buffer = null;
130
+ // Stream might have allready been closed.
131
+ if (this._isClosed) {
132
+ onClose === null || onClose === void 0 ? void 0 : onClose(this._closeError);
133
+ return;
134
+ }
135
+ this._messageHandler = onMessage;
136
+ this._closeHandler = onClose;
137
+ }
138
+ /**
139
+ * Resolves when stream is ready.
140
+ */
141
+ waitUntilReady() {
142
+ return this._readyPromise;
143
+ }
144
+ /**
145
+ * Registers a callback to be called when stream is ready.
146
+ */
147
+ onReady(onReady) {
148
+ (0, node_assert_1.default)(!this._readyHandler, 'Stream already has a handler for the ready event.');
149
+ this._readyHandler = onReady;
150
+ if (this._isReady) {
151
+ onReady();
152
+ }
153
+ }
154
+ /**
155
+ * Close the stream and dispose of any resources.
156
+ */
157
+ close() {
158
+ var _a, _b;
159
+ if (this._isClosed) {
160
+ return;
161
+ }
162
+ this._isClosed = true;
163
+ (_a = this._dispose) === null || _a === void 0 ? void 0 : _a.call(this);
164
+ (_b = this._closeHandler) === null || _b === void 0 ? void 0 : _b.call(this, undefined);
165
+ // Clear function pointers.
166
+ this._messageHandler = undefined;
167
+ this._closeHandler = undefined;
168
+ this._dispose = undefined;
169
+ }
170
+ }
171
+ exports.Stream = Stream;
172
+ /**
173
+ * Asynchronously produces an unhandled rejection.
174
+ *
175
+ * Will terminate the node process with an error.
176
+ * In browser results in an error message in the console.
177
+ * In mocha tests it fails the currently running test.
178
+ *
179
+ * NOTE: Copied from @dxos/debug to avoid circular dependency.
180
+ */
181
+ const throwUnhandledRejection = (error) => {
182
+ setTimeout(() => {
183
+ throw error;
184
+ });
185
+ };
186
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;;;;AAEF,kDAA0B;AAC1B,8DAAiC;AAEjC,MAAM,GAAG,GAAG,IAAA,eAAK,EAAC,4BAA4B,CAAC,CAAC;AA0BhD;;;;;;GAMG;AACH,MAAa,MAAM;IAuDjB,YAAa,QAAqB;QAZ1B,cAAS,GAAG,KAAK,CAAC;QAKlB,aAAQ,GAAG,KAAK,CAAC;QAEzB;;WAEG;QACK,YAAO,GAAe,EAAE,CAAC;QAG/B,IAAI,CAAC,aAAa,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;YACzC,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC;QACtC,CAAC,CAAC,CAAC;QAEH,MAAM,eAAe,G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S,CAAC,CAAC;QAEhC,2BAA2B;QAC3B,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG,SAAS,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,CAAC;CACF;AAlLD,wBAkLC;AAED;;;;;;;;GAQG;AACH,MAAM,uBAAuB,GAAG,CAAC,KAAY,EAAE,EAAE;IAC/C,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,KAAK,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2021 DXOS.org
4
+ //
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const stream_1 = require("./stream");
7
+ describe('Stream', () => {
8
+ test.only('can consume a stream that immediately closes', async () => {
9
+ const stream = new stream_1.Stream(({ next, close }) => {
10
+ next('foo');
11
+ next('bar');
12
+ next('baz');
13
+ close();
14
+ });
15
+ expect(await stream_1.Stream.consume(stream)).toEqual([
16
+ { ready: true },
17
+ { data: 'foo' },
18
+ { data: 'bar' },
19
+ { data: 'baz' },
20
+ { closed: true }
21
+ ]);
22
+ });
23
+ test('can consume a stream that produces items over time', async () => {
24
+ const stream = new stream_1.Stream(({ next, close }) => {
25
+ setImmediate(async () => {
26
+ await sleep(5);
27
+ next('foo');
28
+ await sleep(5);
29
+ next('bar');
30
+ await sleep(5);
31
+ next('baz');
32
+ await sleep(5);
33
+ close();
34
+ });
35
+ });
36
+ expect(await stream_1.Stream.consume(stream)).toEqual([
37
+ { ready: true },
38
+ { data: 'foo' },
39
+ { data: 'bar' },
40
+ { data: 'baz' },
41
+ { closed: true }
42
+ ]);
43
+ });
44
+ test('close error is buffered', async () => {
45
+ const stream = new stream_1.Stream(({ close }) => {
46
+ close(new Error('test'));
47
+ });
48
+ expect(await stream_1.Stream.consume(stream)).toEqual([
49
+ { closed: true, error: new Error('test') }
50
+ ]);
51
+ });
52
+ test('subscribe gets all updates', async () => {
53
+ let nextCb = () => { };
54
+ const stream = new stream_1.Stream(({ next }) => {
55
+ nextCb = next;
56
+ });
57
+ nextCb('first');
58
+ const received = [];
59
+ stream.subscribe(msg => received.push(msg), () => { });
60
+ nextCb('second');
61
+ expect(received).toEqual(['first', 'second']);
62
+ });
63
+ });
64
+ // To not introduce a dependency on @dxos/async.
65
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
66
+ //# sourceMappingURL=stream.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.test.js","sourceRoot":"","sources":["../../src/stream.test.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;AAEF,qCAAkC;AAElC,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,IAAI,CAAC,IAAI,CAAC,8CAA8C,EAAE,KAAK,IAAI,EAAE;QACnE,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE;YAC5C,IAAI,CAAC,KAAK,CAAC,CAAC;YACZ,IAAI,CAAC,KAAK,CAAC,CAAC;YACZ,IAAI,CAAC,KAAK,CAAC,CAAC;YACZ,KAAK,EAAE,CAAC;QACV,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,eAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;YAC3C,EAAE,KAAK,EAAE,IAAI,EAAE;YACf,EAAE,IAAI,EAAE,KAAK,EAAE;YACf,EAAE,IAAI,EAAE,KAAK,EAAE;YACf,EAAE,IAAI,EAAE,KAAK,EAAE;YACf,EAAE,MAAM,EAAE,IAAI,EAAE;SACjB,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,oDAAoD,EAAE,KAAK,IAAI,EAAE;QACpE,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EA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,CAAC;QAC/C,MAAM,MAAM,GAAG,IAAI,eAAM,CAAS,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;YAC7C,MAAM,GAAG,IAAI,CAAC;QAChB,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,OAAO,CAAC,CAAC;QAChB,MAAM,QAAQ,GAAa,EAAE,CAAC;QAC9B,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QACtD,MAAM,CAAC,QAAQ,CAAC,CAAC;QACjB,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,gDAAgD;AAChD,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2021 DXOS.org
4
+ //
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.anySubstitutions = void 0;
7
+ exports.anySubstitutions = {
8
+ 'google.protobuf.Any': {
9
+ encode: (value, schema) => {
10
+ if (typeof value['@type'] !== 'string') {
11
+ throw new Error('Cannot encode google.protobuf.Any without @type string field');
12
+ }
13
+ const codec = schema.tryGetCodecForType(value['@type']);
14
+ const data = codec.encode(value);
15
+ return {
16
+ type_url: value['@type'],
17
+ value: data
18
+ };
19
+ },
20
+ decode: (value, schema) => {
21
+ const codec = schema.tryGetCodecForType(value.type_url);
22
+ const data = codec.decode(value.value);
23
+ return {
24
+ ...data,
25
+ '@type': value.type_url
26
+ };
27
+ }
28
+ }
29
+ };
30
+ //# sourceMappingURL=any.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"any.js","sourceRoot":"","sources":["../../../src/substitutions/any.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;AAOW,QAAA,gBAAgB,GAAG;IAC9B,qBAAqB,EAAE;QACrB,MAAM,EAAE,CAAC,KAAsB,EAAE,MAAmB,EAAO,EAAE;YAC3D,IAAI,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,QAAQ,EAAE;gBACtC,MAAM,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAC;aACjF;YAED,MAAM,KAAK,GAAG,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;YACxD,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACjC,OAAO;gBACL,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC;gBACxB,KAAK,EAAE,IAAI;aACZ,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,CAAC,KAAU,EAAE,MAAmB,EAAoB,EAAE;YAC5D,MAAM,KAAK,GAAG,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YACxD,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACvC,OAAO;gBACL,GAAG,IAAI;gBACP,OAAO,EAAE,KAAK,CAAC,QAAQ;aACxB,CAAC;QACJ,CAAC;KACF;CACF,CAAC"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2022 DXOS.org
4
+ //
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ __exportStar(require("./any"), exports);
21
+ __exportStar(require("./timestamp"), exports);
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/substitutions/index.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;;;;;;;;;;;;;;AAEF,wCAAsB;AACtB,8CAA4B"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2022 DXOS.org
4
+ //
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.timestampSubstitutions = void 0;
7
+ exports.timestampSubstitutions = {
8
+ 'google.protobuf.Timestamp': {
9
+ encode: (value) => {
10
+ const unixMilliseconds = value.getTime();
11
+ return {
12
+ seconds: Math.floor(unixMilliseconds / 1000).toString(),
13
+ nanos: (unixMilliseconds % 1000) * 1e6
14
+ };
15
+ },
16
+ decode: (value) => { var _a, _b; return new Date(parseInt((_a = value.seconds) !== null && _a !== void 0 ? _a : '0') * 1000 + ((_b = value.nanos) !== null && _b !== void 0 ? _b : 0) / 1e6); }
17
+ }
18
+ };
19
+ //# sourceMappingURL=timestamp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timestamp.js","sourceRoot":"","sources":["../../../src/substitutions/timestamp.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;AAEW,QAAA,sBAAsB,GAAG;IACpC,2BAA2B,EAAE;QAC3B,MAAM,EAAE,CAAC,KAAW,EAAO,EAAE;YAC3B,MAAM,gBAAgB,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;YACzC,OAAO;gBACL,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC,QAAQ,EAAE;gBACvD,KAAK,EAAE,CAAC,gBAAgB,GAAG,IAAI,CAAC,GAAG,GAAG;aACvC,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,CAAC,KAAU,EAAQ,EAAE,eAAC,OAAA,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAA,KAAK,CAAC,OAAO,mCAAI,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,MAAA,KAAK,CAAC,KAAK,mCAAI,CAAC,CAAC,GAAG,GAAG,CAAC,CAAA,EAAA;KACzG;CACF,CAAC"}