@yume-chan/struct 0.0.5 → 0.0.10

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 (121) hide show
  1. package/CHANGELOG.json +23 -0
  2. package/CHANGELOG.md +16 -0
  3. package/LICENSE +21 -21
  4. package/README.md +720 -643
  5. package/dts/basic/context.d.ts +10 -17
  6. package/dts/basic/context.d.ts.map +1 -1
  7. package/dts/basic/definition.d.ts +23 -23
  8. package/dts/basic/definition.d.ts.map +1 -1
  9. package/dts/basic/field-value.d.ts +38 -0
  10. package/dts/basic/field-value.d.ts.map +1 -0
  11. package/dts/basic/index.d.ts +2 -2
  12. package/dts/basic/index.d.ts.map +1 -1
  13. package/dts/basic/struct-value.d.ts +24 -0
  14. package/dts/basic/struct-value.d.ts.map +1 -0
  15. package/dts/index.d.ts +1 -0
  16. package/dts/index.d.ts.map +1 -1
  17. package/dts/struct.d.ts +44 -47
  18. package/dts/struct.d.ts.map +1 -1
  19. package/dts/syncbird.d.ts +20 -0
  20. package/dts/syncbird.d.ts.map +1 -0
  21. package/dts/types/array-buffer.d.ts +22 -20
  22. package/dts/types/array-buffer.d.ts.map +1 -1
  23. package/dts/types/fixed-length-array-buffer.d.ts +1 -3
  24. package/dts/types/fixed-length-array-buffer.d.ts.map +1 -1
  25. package/dts/types/index.d.ts +0 -1
  26. package/dts/types/index.d.ts.map +1 -1
  27. package/dts/types/number.d.ts +11 -10
  28. package/dts/types/number.d.ts.map +1 -1
  29. package/dts/types/variable-length-array-buffer.d.ts +21 -19
  30. package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
  31. package/dts/{types/utils.d.ts → utils.d.ts} +3 -0
  32. package/dts/utils.d.ts.map +1 -0
  33. package/esm/basic/context.js +1 -1
  34. package/esm/basic/context.js.map +1 -1
  35. package/esm/basic/definition.js +9 -11
  36. package/esm/basic/definition.js.map +1 -1
  37. package/esm/basic/field-value.js +35 -0
  38. package/esm/basic/field-value.js.map +1 -0
  39. package/esm/basic/index.js +2 -2
  40. package/esm/basic/index.js.map +1 -1
  41. package/esm/basic/struct-value.js +36 -0
  42. package/esm/basic/struct-value.js.map +1 -0
  43. package/esm/index.js +1 -0
  44. package/esm/index.js.map +1 -1
  45. package/esm/struct.js +103 -176
  46. package/esm/struct.js.map +1 -1
  47. package/esm/syncbird.js +25 -0
  48. package/esm/syncbird.js.map +1 -0
  49. package/esm/types/array-buffer.js +83 -107
  50. package/esm/types/array-buffer.js.map +1 -1
  51. package/esm/types/fixed-length-array-buffer.js +5 -15
  52. package/esm/types/fixed-length-array-buffer.js.map +1 -1
  53. package/esm/types/index.js +0 -1
  54. package/esm/types/index.js.map +1 -1
  55. package/esm/types/number.js +35 -53
  56. package/esm/types/number.js.map +1 -1
  57. package/esm/types/variable-length-array-buffer.js +57 -68
  58. package/esm/types/variable-length-array-buffer.js.map +1 -1
  59. package/esm/utils.js +17 -0
  60. package/esm/utils.js.map +1 -0
  61. package/package.json +47 -50
  62. package/src/basic/context.ts +32 -40
  63. package/src/basic/definition.ts +72 -69
  64. package/src/basic/{runtime-value.ts → field-value.ts} +67 -72
  65. package/src/basic/index.ts +4 -4
  66. package/src/basic/struct-value.ts +39 -0
  67. package/src/index.ts +5 -4
  68. package/src/struct.ts +595 -579
  69. package/src/syncbird.ts +53 -0
  70. package/src/types/array-buffer.ts +194 -171
  71. package/src/types/fixed-length-array-buffer.ts +17 -27
  72. package/src/types/index.ts +4 -5
  73. package/src/types/number.ts +114 -101
  74. package/src/types/variable-length-array-buffer.ts +142 -121
  75. package/src/{types/utils.ts → utils.ts} +62 -47
  76. package/cjs/basic/context.js +0 -7
  77. package/cjs/basic/context.js.map +0 -1
  78. package/cjs/basic/definition.js +0 -26
  79. package/cjs/basic/definition.js.map +0 -1
  80. package/cjs/basic/index.js +0 -8
  81. package/cjs/basic/index.js.map +0 -1
  82. package/cjs/basic/runtime-object.js +0 -34
  83. package/cjs/basic/runtime-object.js.map +0 -1
  84. package/cjs/basic/runtime-value.js +0 -41
  85. package/cjs/basic/runtime-value.js.map +0 -1
  86. package/cjs/index.js +0 -10
  87. package/cjs/index.js.map +0 -1
  88. package/cjs/struct.js +0 -259
  89. package/cjs/struct.js.map +0 -1
  90. package/cjs/types/array-buffer.js +0 -137
  91. package/cjs/types/array-buffer.js.map +0 -1
  92. package/cjs/types/fixed-length-array-buffer.js +0 -21
  93. package/cjs/types/fixed-length-array-buffer.js.map +0 -1
  94. package/cjs/types/index.js +0 -9
  95. package/cjs/types/index.js.map +0 -1
  96. package/cjs/types/number.js +0 -68
  97. package/cjs/types/number.js.map +0 -1
  98. package/cjs/types/utils.js +0 -11
  99. package/cjs/types/utils.js.map +0 -1
  100. package/cjs/types/variable-length-array-buffer.js +0 -85
  101. package/cjs/types/variable-length-array-buffer.js.map +0 -1
  102. package/dts/basic/runtime-object.d.ts +0 -17
  103. package/dts/basic/runtime-object.d.ts.map +0 -1
  104. package/dts/basic/runtime-value.d.ts +0 -39
  105. package/dts/basic/runtime-value.d.ts.map +0 -1
  106. package/dts/types/utils.d.ts.map +0 -1
  107. package/esm/basic/runtime-object.js +0 -28
  108. package/esm/basic/runtime-object.js.map +0 -1
  109. package/esm/basic/runtime-value.js +0 -38
  110. package/esm/basic/runtime-value.js.map +0 -1
  111. package/esm/types/utils.js +0 -7
  112. package/esm/types/utils.js.map +0 -1
  113. package/src/basic/context.spec.ts +0 -9
  114. package/src/basic/definition.spec.ts +0 -26
  115. package/src/basic/runtime-object.spec.ts +0 -38
  116. package/src/basic/runtime-object.ts +0 -36
  117. package/src/basic/runtime-value.spec.ts +0 -71
  118. package/src/types/array-buffer.spec.ts +0 -88
  119. package/src/types/number.spec.ts +0 -262
  120. package/tsconfig.cjs.json +0 -11
  121. package/tsconfig.esm.json +0 -15
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VariableLengthArrayBufferLikeFieldDefinition = void 0;
4
- var tslib_1 = require("tslib");
5
- var basic_1 = require("../basic");
6
- var array_buffer_1 = require("./array-buffer");
7
- var VariableLengthArrayBufferLikeFieldDefinition = /** @class */ (function (_super) {
8
- tslib_1.__extends(VariableLengthArrayBufferLikeFieldDefinition, _super);
9
- function VariableLengthArrayBufferLikeFieldDefinition() {
10
- return _super !== null && _super.apply(this, arguments) || this;
11
- }
12
- VariableLengthArrayBufferLikeFieldDefinition.prototype.getSize = function () {
13
- return 0;
14
- };
15
- VariableLengthArrayBufferLikeFieldDefinition.prototype.getDeserializeSize = function (object) {
16
- var value = object[this.options.lengthField];
17
- if (typeof value === 'string') {
18
- value = Number.parseInt(value, 10);
19
- }
20
- return value;
21
- };
22
- VariableLengthArrayBufferLikeFieldDefinition.prototype.createValue = function (options, context, object, value) {
23
- return new VariableLengthArrayBufferLikeFieldRuntimeValue(this, options, context, object, value);
24
- };
25
- return VariableLengthArrayBufferLikeFieldDefinition;
26
- }(array_buffer_1.ArrayBufferLikeFieldDefinition));
27
- exports.VariableLengthArrayBufferLikeFieldDefinition = VariableLengthArrayBufferLikeFieldDefinition;
28
- var VariableLengthArrayBufferLikeLengthFieldRuntimeValue = /** @class */ (function (_super) {
29
- tslib_1.__extends(VariableLengthArrayBufferLikeLengthFieldRuntimeValue, _super);
30
- function VariableLengthArrayBufferLikeLengthFieldRuntimeValue(originalValue, arrayBufferValue) {
31
- var _this = _super.call(this, originalValue.definition, originalValue.options, originalValue.context, originalValue.object, 0) || this;
32
- _this.originalValue = originalValue;
33
- _this.arrayBufferValue = arrayBufferValue;
34
- return _this;
35
- }
36
- VariableLengthArrayBufferLikeLengthFieldRuntimeValue.prototype.getSize = function () {
37
- return this.originalValue.getSize();
38
- };
39
- VariableLengthArrayBufferLikeLengthFieldRuntimeValue.prototype.get = function () {
40
- // TODO: originalValue might be a `string` type, now it always returns `number`.
41
- return this.arrayBufferValue.getSize();
42
- };
43
- VariableLengthArrayBufferLikeLengthFieldRuntimeValue.prototype.set = function () { };
44
- VariableLengthArrayBufferLikeLengthFieldRuntimeValue.prototype.serialize = function (dataView, offset, context) {
45
- this.originalValue.set(this.get());
46
- this.originalValue.serialize(dataView, offset, context);
47
- };
48
- return VariableLengthArrayBufferLikeLengthFieldRuntimeValue;
49
- }(basic_1.FieldRuntimeValue));
50
- var VariableLengthArrayBufferLikeFieldRuntimeValue = /** @class */ (function (_super) {
51
- tslib_1.__extends(VariableLengthArrayBufferLikeFieldRuntimeValue, _super);
52
- function VariableLengthArrayBufferLikeFieldRuntimeValue(definition, options, context, object, value) {
53
- var _this = _super.call(this, definition, options, context, object, value) || this;
54
- // Patch the associated length field.
55
- var lengthField = _this.definition.options.lengthField;
56
- var originalValue = basic_1.getRuntimeValue(object, lengthField);
57
- _this.lengthFieldValue = new VariableLengthArrayBufferLikeLengthFieldRuntimeValue(originalValue, _this);
58
- basic_1.setRuntimeValue(object, lengthField, _this.lengthFieldValue);
59
- return _this;
60
- }
61
- VariableLengthArrayBufferLikeFieldRuntimeValue.getSize = function () {
62
- return 0;
63
- };
64
- VariableLengthArrayBufferLikeFieldRuntimeValue.prototype.getSize = function () {
65
- if (this.length === undefined) {
66
- if (this.arrayBuffer !== undefined) {
67
- this.length = this.arrayBuffer.byteLength;
68
- }
69
- else {
70
- this.length = this.definition.type.getSize(this.value);
71
- if (this.length === -1) {
72
- this.arrayBuffer = this.definition.type.toArrayBuffer(this.value, this.context);
73
- this.length = this.arrayBuffer.byteLength;
74
- }
75
- }
76
- }
77
- return this.length;
78
- };
79
- VariableLengthArrayBufferLikeFieldRuntimeValue.prototype.set = function (value) {
80
- _super.prototype.set.call(this, value);
81
- this.length = undefined;
82
- };
83
- return VariableLengthArrayBufferLikeFieldRuntimeValue;
84
- }(array_buffer_1.ArrayBufferLikeFieldRuntimeValue));
85
- //# sourceMappingURL=variable-length-array-buffer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"variable-length-array-buffer.js","sourceRoot":"","sources":["../../src/types/variable-length-array-buffer.ts"],"names":[],"mappings":";;;;AAAA,kCAA0H;AAC1H,+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AAO,CAAC,CAAC;oBAChF,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC;iBAC7C;aACJ;SACJ;QAED,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAEM,4DAAG,GAAV,UAAW,KAAc;QACrB,iBAAM,GAAG,YAAC,KAAK,CAAC,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IAC5B,CAAC;IACL,qDAAC;AAAD,CAAC,AAhDD,CAGc,+CAAgC,GA6C7C"}
@@ -1,17 +0,0 @@
1
- import { FieldRuntimeValue } from './runtime-value';
2
- declare const RuntimeValues: unique symbol;
3
- export interface RuntimeObject {
4
- [RuntimeValues]: Record<PropertyKey, FieldRuntimeValue>;
5
- }
6
- /** Creates a new runtime object that can be used with `getRuntimeValue` and `setRuntimeValue` */
7
- export declare function createRuntimeObject(): RuntimeObject;
8
- /** Gets the previously set `RuntimeValue` for specified `key` on `object` */
9
- export declare function getRuntimeValue(object: RuntimeObject, key: PropertyKey): FieldRuntimeValue;
10
- /**
11
- * Sets the `RuntimeValue` for specified `key` on `object`,
12
- * also sets up property accessors so reads/writes to `object`'s `key` will be forwarded to
13
- * the underlying `RuntimeValue`
14
- */
15
- export declare function setRuntimeValue(object: RuntimeObject, key: PropertyKey, runtimeValue: FieldRuntimeValue): void;
16
- export {};
17
- //# sourceMappingURL=runtime-object.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime-object.d.ts","sourceRoot":"","sources":["../../src/basic/runtime-object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,QAAA,MAAM,aAAa,eAA0B,CAAC;AAE9C,MAAM,WAAW,aAAa;IAC1B,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;CAC3D;AAED,iGAAiG;AACjG,wBAAgB,mBAAmB,IAAI,aAAa,CAInD;AAED,6EAA6E;AAC7E,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,WAAW,GAAG,iBAAiB,CAE1F;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,WAAW,EAAE,YAAY,EAAE,iBAAiB,GAAG,IAAI,CAU9G"}
@@ -1,39 +0,0 @@
1
- import type { StructOptions, StructSerializationContext } from './context';
2
- import type { FieldDefinition } from './definition';
3
- /**
4
- * Field runtime value manages one field of one `Struct` instance.
5
- *
6
- * If one `FieldDefinition` needs to change other field's semantics
7
- * It can override other fields' `FieldRuntimeValue` in its own `FieldRuntimeValue`'s constructor
8
- */
9
- export declare abstract class FieldRuntimeValue<TDefinition extends FieldDefinition<any, any, any> = FieldDefinition<any, any, any>> {
10
- /** Gets the definition associated with this runtime value */
11
- readonly definition: TDefinition;
12
- /** Gets the options of the associated `Struct` */
13
- readonly options: Readonly<StructOptions>;
14
- /** Gets the serialization context of the associated `Struct` instance */
15
- readonly context: StructSerializationContext;
16
- /** Gets the associated `Struct` instance */
17
- readonly object: any;
18
- protected value: TDefinition['valueType'];
19
- constructor(definition: TDefinition, options: Readonly<StructOptions>, context: StructSerializationContext, object: any, value: TDefinition['valueType']);
20
- /**
21
- * Gets the actual size of this field. By default, the return value of its `definition.getSize()`
22
- *
23
- * When overridden in derived classes, can have custom logic to calculate the actual size.
24
- */
25
- getSize(): number;
26
- /**
27
- * When implemented in derived classes, returns the current value of this field
28
- */
29
- get(): TDefinition['valueType'];
30
- /**
31
- * When implemented in derived classes, update the current value of this field
32
- */
33
- set(value: TDefinition['valueType']): void;
34
- /**
35
- * When implemented in derived classes, serializes this field into `dataView` at `offset`
36
- */
37
- abstract serialize(dataView: DataView, offset: number, context: StructSerializationContext): void;
38
- }
39
- //# sourceMappingURL=runtime-value.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime-value.d.ts","sourceRoot":"","sources":["../../src/basic/runtime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AEG;AACH,oBAAY,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,KAAK,CAAC;AAE/E;;GAEG;AACH,oBAAY,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,IAC3D,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;AAE7C;;GAEG;AACH,oBAAY,SAAS,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,GAAG,CAAC;CAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAEpG;;GAEG;AACH,oBAAY,UAAU,CAAC,CAAC,EAAE,MAAM,IAC5B;KAAG,IAAI,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,KAAK;CAAE,CAAC,MAAM,CAAC,CAAC,CAAC;AAE1E;;GAEG;AACH,wBAAgB,WAAW,CAAC,CAAC,KAAK,CAAC,CAElC"}
@@ -1,28 +0,0 @@
1
- var RuntimeValues = Symbol('RuntimeValues');
2
- /** Creates a new runtime object that can be used with `getRuntimeValue` and `setRuntimeValue` */
3
- export function createRuntimeObject() {
4
- var _a;
5
- return _a = {},
6
- _a[RuntimeValues] = {},
7
- _a;
8
- }
9
- /** Gets the previously set `RuntimeValue` for specified `key` on `object` */
10
- export function getRuntimeValue(object, key) {
11
- return object[RuntimeValues][key];
12
- }
13
- /**
14
- * Sets the `RuntimeValue` for specified `key` on `object`,
15
- * also sets up property accessors so reads/writes to `object`'s `key` will be forwarded to
16
- * the underlying `RuntimeValue`
17
- */
18
- export function setRuntimeValue(object, key, runtimeValue) {
19
- delete object[key];
20
- object[RuntimeValues][key] = runtimeValue;
21
- Object.defineProperty(object, key, {
22
- configurable: true,
23
- enumerable: true,
24
- get: function () { return runtimeValue.get(); },
25
- set: function (value) { runtimeValue.set(value); },
26
- });
27
- }
28
- //# sourceMappingURL=runtime-object.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime-object.js","sourceRoot":"","sources":["../../src/basic/runtime-object.ts"],"names":[],"mappings":"AAEA,IAAM,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAM9C,iGAAiG;AACjG,MAAM,UAAU,mBAAmB;;IAC/B;QACI,GAAC,aAAa,IAAG,EAAE;WACrB;AACN,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,eAAe,CAAC,MAAqB,EAAE,GAAgB;IACnE,OAAO,MAAM,CAAC,aAAa,CAAC,CAAC,GAAU,CAAsB,CAAC;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,MAAqB,EAAE,GAAgB,EAAE,YAA+B;IACpG,OAAQ,MAAc,CAAC,GAAG,CAAC,CAAC;IAE5B,MAAM,CAAC,aAAa,CAAC,CAAC,GAAU,CAAC,GAAG,YAAY,CAAC;IACjD,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE;QAC/B,YAAY,EAAE,IAAI;QAClB,UAAU,EAAE,IAAI;QAChB,GAAG,gBAAK,OAAO,YAAY,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACpC,GAAG,YAAC,KAAK,IAAI,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC1C,CAAC,CAAC;AACP,CAAC"}
@@ -1,38 +0,0 @@
1
- /**
2
- * Field runtime value manages one field of one `Struct` instance.
3
- *
4
- * If one `FieldDefinition` needs to change other field's semantics
5
- * It can override other fields' `FieldRuntimeValue` in its own `FieldRuntimeValue`'s constructor
6
- */
7
- var FieldRuntimeValue = /** @class */ (function () {
8
- function FieldRuntimeValue(definition, options, context, object, value) {
9
- this.definition = definition;
10
- this.options = options;
11
- this.context = context;
12
- this.object = object;
13
- this.value = value;
14
- }
15
- /**
16
- * Gets the actual size of this field. By default, the return value of its `definition.getSize()`
17
- *
18
- * When overridden in derived classes, can have custom logic to calculate the actual size.
19
- */
20
- FieldRuntimeValue.prototype.getSize = function () {
21
- return this.definition.getSize();
22
- };
23
- /**
24
- * When implemented in derived classes, returns the current value of this field
25
- */
26
- FieldRuntimeValue.prototype.get = function () {
27
- return this.value;
28
- };
29
- /**
30
- * When implemented in derived classes, update the current value of this field
31
- */
32
- FieldRuntimeValue.prototype.set = function (value) {
33
- this.value = value;
34
- };
35
- return FieldRuntimeValue;
36
- }());
37
- export { FieldRuntimeValue };
38
- //# sourceMappingURL=runtime-value.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime-value.js","sourceRoot":"","sources":["../../src/basic/runtime-value.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH;IAiBI,2BACI,UAAuB,EACvB,OAAgC,EAChC,OAAmC,EACnC,MAAW,EACX,KAA+B;QAE/B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACI,mCAAO,GAAd;QACI,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,+BAAG,GAAV;QACI,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED;;OAEG;IACI,+BAAG,GAAV,UAAW,KAA+B;QACtC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;IAUL,wBAAC;AAAD,CAAC,AA9DD,IA8DC"}
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a (fake) value of the given type.
3
- */
4
- export function placeholder() {
5
- return undefined;
6
- }
7
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/types/utils.ts"],"names":[],"mappings":"AAyCA;;GAEG;AACH,MAAM,UAAU,WAAW;IACvB,OAAO,SAAyB,CAAC;AACrC,CAAC"}
@@ -1,9 +0,0 @@
1
- import { StructDefaultOptions } from './context';
2
-
3
- describe('Runtime', () => {
4
- describe('StructDefaultOptions', () => {
5
- it('should have `littleEndian` that equals to `false`', () => {
6
- expect(StructDefaultOptions).toHaveProperty('littleEndian', false);
7
- });
8
- });
9
- });
@@ -1,26 +0,0 @@
1
- import { StructOptions, StructDeserializationContext, StructSerializationContext } from './context';
2
- import { FieldDefinition } from './definition';
3
- import { FieldRuntimeValue } from './runtime-value';
4
-
5
- describe('FieldDefinition', () => {
6
- describe('new', () => {
7
- it('should save the `options` parameter', () => {
8
- class MockFieldDefinition extends FieldDefinition<number>{
9
- public constructor(options: number) {
10
- super(options);
11
- }
12
- public getSize(): number {
13
- throw new Error('Method not implemented.');
14
- }
15
- public deserialize(options: Readonly<StructOptions>, context: StructDeserializationContext, object: any): FieldRuntimeValue<FieldDefinition<number, unknown, never>> | Promise<FieldRuntimeValue<FieldDefinition<number, unknown, never>>> {
16
- throw new Error('Method not implemented.');
17
- }
18
- public createValue(options: Readonly<StructOptions>, context: StructSerializationContext, object: any, value: unknown): FieldRuntimeValue<FieldDefinition<number, unknown, never>> {
19
- throw new Error('Method not implemented.');
20
- }
21
- }
22
-
23
- expect(new MockFieldDefinition(42)).toHaveProperty('options', 42);
24
- });
25
- });
26
- });
@@ -1,38 +0,0 @@
1
- import { createRuntimeObject, getRuntimeValue, setRuntimeValue } from './runtime-object';
2
-
3
- describe('RuntimeObject', () => {
4
- describe('createRuntimeObject', () => {
5
- it('should create a special object', () => {
6
- const object = createRuntimeObject();
7
- expect(Object.getOwnPropertySymbols(object)).toHaveLength(1);
8
- });
9
- });
10
-
11
- describe('getRuntimeValue', () => {
12
- it('should return previously set value', () => {
13
- const object = createRuntimeObject();
14
- const field = 'foo';
15
- const value = {} as any;
16
- setRuntimeValue(object, field, value);
17
- expect(getRuntimeValue(object, field)).toBe(value);
18
- });
19
- });
20
-
21
- describe('setRuntimeValue', () => {
22
- it('should define a proxy property to underlying `RuntimeValue`', () => {
23
- const object = createRuntimeObject();
24
- const field = 'foo';
25
- const getter = jest.fn(() => 42);
26
- const setter = jest.fn((value: number) => { });
27
- const value = { get: getter, set: setter } as any;
28
- setRuntimeValue(object, field, value);
29
-
30
- expect((object as any)[field]).toBe(42);
31
- expect(getter).toBeCalledTimes(1);
32
-
33
- (object as any)[field] = 100;
34
- expect(setter).toBeCalledTimes(1);
35
- expect(setter).lastCalledWith(100);
36
- });
37
- });
38
- });
@@ -1,36 +0,0 @@
1
- import { FieldRuntimeValue } from './runtime-value';
2
-
3
- const RuntimeValues = Symbol('RuntimeValues');
4
-
5
- export interface RuntimeObject {
6
- [RuntimeValues]: Record<PropertyKey, FieldRuntimeValue>;
7
- }
8
-
9
- /** Creates a new runtime object that can be used with `getRuntimeValue` and `setRuntimeValue` */
10
- export function createRuntimeObject(): RuntimeObject {
11
- return {
12
- [RuntimeValues]: {},
13
- };
14
- }
15
-
16
- /** Gets the previously set `RuntimeValue` for specified `key` on `object` */
17
- export function getRuntimeValue(object: RuntimeObject, key: PropertyKey): FieldRuntimeValue {
18
- return object[RuntimeValues][key as any] as FieldRuntimeValue;
19
- }
20
-
21
- /**
22
- * Sets the `RuntimeValue` for specified `key` on `object`,
23
- * also sets up property accessors so reads/writes to `object`'s `key` will be forwarded to
24
- * the underlying `RuntimeValue`
25
- */
26
- export function setRuntimeValue(object: RuntimeObject, key: PropertyKey, runtimeValue: FieldRuntimeValue): void {
27
- delete (object as any)[key];
28
-
29
- object[RuntimeValues][key as any] = runtimeValue;
30
- Object.defineProperty(object, key, {
31
- configurable: true,
32
- enumerable: true,
33
- get() { return runtimeValue.get(); },
34
- set(value) { runtimeValue.set(value); },
35
- });
36
- }
@@ -1,71 +0,0 @@
1
- import { StructDeserializationContext, StructOptions, StructSerializationContext } from './context';
2
- import { FieldDefinition } from './definition';
3
- import { FieldRuntimeValue } from './runtime-value';
4
-
5
- describe('FieldRuntimeValue', () => {
6
- describe('.constructor', () => {
7
- it('should save parameters', () => {
8
- class MockFieldRuntimeValue extends FieldRuntimeValue {
9
- public serialize(dataView: DataView, offset: number, context: StructSerializationContext): void {
10
- throw new Error('Method not implemented.');
11
- }
12
- }
13
-
14
- const definition = 1 as any;
15
- const options = 2 as any;
16
- const context = 3 as any;
17
- const object = 4 as any;
18
- const value = 5 as any;
19
-
20
- const fieldRuntimeValue = new MockFieldRuntimeValue(definition, options, context, object, value);
21
- expect(fieldRuntimeValue).toHaveProperty('definition', definition);
22
- expect(fieldRuntimeValue).toHaveProperty('options', options);
23
- expect(fieldRuntimeValue).toHaveProperty('context', context);
24
- expect(fieldRuntimeValue).toHaveProperty('object', object);
25
- expect(fieldRuntimeValue.get()).toBe(value);
26
- });
27
- });
28
-
29
- describe('#getSize', () => {
30
- it('should return same value as definition\'s', () => {
31
- class MockFieldDefinition extends FieldDefinition {
32
- public getSize(): number {
33
- return 42;
34
- }
35
- public deserialize(options: Readonly<StructOptions>, context: StructDeserializationContext, object: any): FieldRuntimeValue<FieldDefinition<void, unknown, never>> | Promise<FieldRuntimeValue<FieldDefinition<void, unknown, never>>> {
36
- throw new Error('Method not implemented.');
37
- }
38
- public createValue(options: Readonly<StructOptions>, context: StructSerializationContext, object: any, value: unknown): FieldRuntimeValue<FieldDefinition<void, unknown, never>> {
39
- throw new Error('Method not implemented.');
40
- }
41
- }
42
-
43
- class MockFieldRuntimeValue extends FieldRuntimeValue {
44
- public serialize(dataView: DataView, offset: number, context: StructSerializationContext): void {
45
- throw new Error('Method not implemented.');
46
- }
47
- }
48
-
49
- const fieldDefinition = new MockFieldDefinition();
50
- const fieldRuntimeValue = new MockFieldRuntimeValue(fieldDefinition, undefined as any, undefined as any, undefined as any, undefined as any);
51
- expect(fieldRuntimeValue.getSize()).toBe(42);
52
- });
53
- });
54
-
55
- describe('#set', () => {
56
- it('should update interval value', () => {
57
- class MockFieldRuntimeValue extends FieldRuntimeValue {
58
- public serialize(dataView: DataView, offset: number, context: StructSerializationContext): void {
59
- throw new Error('Method not implemented.');
60
- }
61
- }
62
-
63
- const fieldRuntimeValue = new MockFieldRuntimeValue(undefined as any, undefined as any, undefined as any, undefined as any, undefined as any);
64
- fieldRuntimeValue.set(1);
65
- expect(fieldRuntimeValue.get()).toBe(1);
66
-
67
- fieldRuntimeValue.set(2);
68
- expect(fieldRuntimeValue.get()).toBe(2);
69
- });
70
- });
71
- });
@@ -1,88 +0,0 @@
1
- import { StructDeserializationContext, StructSerializationContext } from '../basic';
2
- import { ArrayBufferFieldType, StringFieldType, Uint8ClampedArrayFieldType } from './array-buffer';
3
-
4
- describe('Types', () => {
5
- describe('ArrayBufferFieldType', () => {
6
- it('should have a static instance', () => {
7
- expect(ArrayBufferFieldType.instance).toBeInstanceOf(ArrayBufferFieldType);
8
- });
9
-
10
- it('`toArrayBuffer` should return the same `ArrayBuffer`', () => {
11
- const arrayBuffer = new ArrayBuffer(10);
12
- expect(ArrayBufferFieldType.instance.toArrayBuffer(arrayBuffer)).toBe(arrayBuffer);
13
- });
14
-
15
- it('`fromArrayBuffer` should return the same `ArrayBuffer`', () => {
16
- const arrayBuffer = new ArrayBuffer(10);
17
- expect(ArrayBufferFieldType.instance.fromArrayBuffer(arrayBuffer)).toBe(arrayBuffer);
18
- });
19
-
20
- it('`getSize` should return the `byteLength` of the `ArrayBuffer`', () => {
21
- const arrayBuffer = new ArrayBuffer(10);
22
- expect(ArrayBufferFieldType.instance.getSize(arrayBuffer)).toBe(10);
23
- });
24
- });
25
-
26
- describe('Uint8ClampedArrayFieldType', () => {
27
- it('should have a static instance', () => {
28
- expect(Uint8ClampedArrayFieldType.instance).toBeInstanceOf(Uint8ClampedArrayFieldType);
29
- });
30
-
31
- it('`toArrayBuffer` should return its `buffer`', () => {
32
- const array = new Uint8ClampedArray(10);
33
- const buffer = array.buffer;
34
- expect(Uint8ClampedArrayFieldType.instance.toArrayBuffer(array)).toBe(buffer);
35
- });
36
-
37
- it('`fromArrayBuffer` should return a view of the `ArrayBuffer`', () => {
38
- const arrayBuffer = new ArrayBuffer(10);
39
- const array = Uint8ClampedArrayFieldType.instance.fromArrayBuffer(arrayBuffer);
40
- expect(array).toHaveProperty('buffer', arrayBuffer);
41
- expect(array).toHaveProperty('byteOffset', 0);
42
- expect(array).toHaveProperty('byteLength', 10);
43
- });
44
-
45
- it('`getSize` should return the `byteLength` of the `ArrayBuffer`', () => {
46
- const array = new Uint8ClampedArray(10);
47
- expect(Uint8ClampedArrayFieldType.instance.getSize(array)).toBe(10);
48
- });
49
- });
50
-
51
- describe('StringFieldType', () => {
52
- it('should have a static instance', () => {
53
- expect(StringFieldType.instance).toBeInstanceOf(StringFieldType);
54
- });
55
-
56
- it('`toArrayBuffer` should return the decoded string', () => {
57
- const text = 'foo';
58
- const arrayBuffer = Buffer.from(text, 'utf-8');
59
- const context: StructSerializationContext = {
60
- encodeUtf8(input) {
61
- return Buffer.from(input, 'utf-8');
62
- },
63
- };
64
- expect(StringFieldType.instance.toArrayBuffer(text, context)).toEqual(arrayBuffer);
65
- });
66
-
67
- it('`fromArrayBuffer` should return the encoded ArrayBuffer', () => {
68
- const text = 'foo';
69
- const arrayBuffer = Buffer.from(text, 'utf-8');
70
- const context: StructDeserializationContext = {
71
- decodeUtf8(arrayBuffer: ArrayBuffer): string {
72
- return Buffer.from(arrayBuffer).toString('utf-8');
73
- },
74
- encodeUtf8(input) {
75
- throw new Error('Method not implemented.');
76
- },
77
- read(length) {
78
- throw new Error('Method not implemented.');
79
- },
80
- };
81
- expect(StringFieldType.instance.fromArrayBuffer(arrayBuffer, context)).toBe(text);
82
- });
83
-
84
- it('`getSize` should return -1', () => {
85
- expect(StringFieldType.instance.getSize()).toBe(-1);
86
- });
87
- });
88
- });