affinirum 1.4.0 → 1.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.
Files changed (53) hide show
  1. package/README.md +66 -48
  2. package/dst/Affinirum.js +3 -3
  3. package/dst/Constants.js +2 -0
  4. package/dst/StaticScope.js +1 -1
  5. package/dst/Type.js +1 -1
  6. package/dst/atom/FunctionAtom.js +1 -1
  7. package/dst/atom/ObjectAtom.js +1 -1
  8. package/dst/cjs/Affinirum.js +3 -3
  9. package/dst/cjs/Constants.js +2 -0
  10. package/dst/cjs/StaticScope.js +1 -1
  11. package/dst/cjs/Type.js +1 -1
  12. package/dst/cjs/atom/FunctionAtom.js +1 -1
  13. package/dst/cjs/atom/ObjectAtom.js +1 -1
  14. package/dst/cjs/native/Array.js +3 -3
  15. package/dst/cjs/native/Boolean.js +7 -4
  16. package/dst/cjs/native/Buffer.js +45 -13
  17. package/dst/cjs/native/Float.js +4 -7
  18. package/dst/cjs/native/Integer.js +10 -7
  19. package/dst/cjs/native/Iterable.js +2 -1
  20. package/dst/cjs/native/Object.js +3 -3
  21. package/dst/cjs/native/String.js +34 -17
  22. package/dst/cjs/native/Timestamp.js +27 -13
  23. package/dst/cjs/native/Unknown.js +10 -10
  24. package/dst/cjs/native/notation/AN.js +6 -6
  25. package/dst/cjs/native/notation/IP.js +239 -0
  26. package/dst/cjs/native/notation/JSON.js +5 -5
  27. package/dst/cjs/node/ObjectNode.js +1 -1
  28. package/dst/native/Array.d.ts +1 -7
  29. package/dst/native/Array.js +3 -3
  30. package/dst/native/Boolean.d.ts +1 -7
  31. package/dst/native/Boolean.js +7 -4
  32. package/dst/native/Buffer.d.ts +4 -5
  33. package/dst/native/Buffer.js +43 -12
  34. package/dst/native/Float.d.ts +2 -21
  35. package/dst/native/Float.js +4 -7
  36. package/dst/native/Integer.d.ts +2 -10
  37. package/dst/native/Integer.js +10 -7
  38. package/dst/native/Iterable.js +2 -1
  39. package/dst/native/Object.d.ts +1 -3
  40. package/dst/native/Object.js +3 -3
  41. package/dst/native/String.d.ts +3 -7
  42. package/dst/native/String.js +34 -17
  43. package/dst/native/Timestamp.d.ts +4 -9
  44. package/dst/native/Timestamp.js +27 -13
  45. package/dst/native/Unknown.js +11 -11
  46. package/dst/native/notation/AN.d.ts +1 -4
  47. package/dst/native/notation/AN.js +6 -6
  48. package/dst/native/notation/IP.d.ts +1 -0
  49. package/dst/native/notation/IP.js +236 -0
  50. package/dst/native/notation/JSON.d.ts +1 -5
  51. package/dst/native/notation/JSON.js +5 -5
  52. package/dst/node/ObjectNode.js +1 -1
  53. package/package.json +1 -1
@@ -162,16 +162,24 @@ const endsWithString = (value, search, endPos, ignoreCaseSpaceEtc) => {
162
162
  };
163
163
  exports.endsWithString = endsWithString;
164
164
  const encodeString = (value, encoding = "utf8") => {
165
- if (value == null) {
166
- return new Uint8Array(0).buffer;
167
- }
168
- if (encoding === "utf8") {
169
- return new TextEncoder().encode(value).buffer;
165
+ switch (encoding) {
166
+ case "utf8": return new TextEncoder().encode(value).buffer;
167
+ case "sbcs": break;
168
+ case "ucs2": break;
169
+ case "ucs2le": break;
170
+ default: throw new Error(`${encoding} encoding not supported`);
171
+ }
172
+ if (encoding === "sbcs") {
173
+ const dv = new Uint8Array(value.length);
174
+ for (let i = 0; i < value.length; ++i) {
175
+ dv[i] = value.charCodeAt(i);
176
+ }
177
+ return dv.buffer;
170
178
  }
171
179
  const dv = new DataView(new Uint16Array(value.length).buffer);
172
- const lessOrEqual = encoding.endsWith("le");
180
+ const littleEndian = encoding.endsWith("le");
173
181
  for (let i = 0; i < value.length; ++i) {
174
- dv.setUint16(i << 1, value.charCodeAt(i), lessOrEqual);
182
+ dv.setUint16(i << 1, value.charCodeAt(i), littleEndian);
175
183
  }
176
184
  return dv.buffer;
177
185
  };
@@ -180,18 +188,27 @@ const decodeString = (value, encoding = "utf8", byteOffset, byteLength) => {
180
188
  if (value == null) {
181
189
  return undefined;
182
190
  }
183
- if (encoding === "utf8") {
184
- return new TextDecoder().decode(new DataView(value, byteOffset, byteLength));
191
+ switch (encoding) {
192
+ case "utf8": return new TextDecoder().decode(new DataView(value, byteOffset, byteLength));
193
+ case "sbcs": break;
194
+ case "ucs2": break;
195
+ case "ucs2le": break;
196
+ default: throw new Error(`${encoding} encoding not supported`);
185
197
  }
186
- else {
187
- const dv = new DataView(value, byteOffset, byteLength);
188
- const lessOrEqual = encoding.endsWith("le");
198
+ const dv = new DataView(value, byteOffset, byteLength);
199
+ if (encoding === "sbcs") {
189
200
  let str = "";
190
- for (let i = 0; i < dv.byteLength; i += 2) {
191
- str += String.fromCharCode(dv.getUint16(i, lessOrEqual));
201
+ for (let i = 0; i < dv.byteLength; ++i) {
202
+ str += String.fromCharCode(dv.getUint8(i));
192
203
  }
193
204
  return str;
194
205
  }
206
+ const littleEndian = encoding.endsWith("le");
207
+ let str = "";
208
+ for (let i = 0; i < dv.byteLength; i += 2) {
209
+ str += String.fromCharCode(dv.getUint16(i, littleEndian));
210
+ }
211
+ return str;
195
212
  };
196
213
  const typeStringEquator = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.String, Type_js_1.Type.String]);
197
214
  const typeStringComparator = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.String, Type_js_1.Type.String, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalBoolean]);
@@ -235,9 +252,9 @@ const funcRandomString = new Constant_js_1.Constant((value) => {
235
252
  }
236
253
  return str.slice(0, length);
237
254
  }, Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Integer]), false);
238
- const funcDecodeString = new Constant_js_1.Constant((value, encoding = "utf8", byteOffset, byteLength) => decodeString(value, encoding, byteOffset == null ? undefined : Number(byteOffset), byteLength == null ? undefined : Number(byteLength)), Type_js_1.Type.functionType(Type_js_1.Type.OptionalString, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger]));
239
- exports.constString = {
255
+ const funcDecodeString = new Constant_js_1.Constant((value, encoding = "utf8", byteOffset, byteLength) => decodeString(value, encoding, byteOffset == null ? undefined : Number(byteOffset), byteLength == null ? undefined : Number(byteLength)), Type_js_1.Type.functionType(Type_js_1.Type.OptionalString, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger]));
256
+ exports.constString = Object.assign(Object.create(null), {
240
257
  Alphanum: funcAlphanum,
241
258
  Random: funcRandomString,
242
259
  Decode: funcDecodeString,
243
- };
260
+ });
@@ -5,19 +5,33 @@ const Constant_js_1 = require("../Constant.js");
5
5
  const Type_js_1 = require("../Type.js");
6
6
  const typeTimestampPart = Type_js_1.Type.functionType(Type_js_1.Type.Integer, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalBoolean]);
7
7
  const typeTimestampSince = Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Timestamp, Type_js_1.Type.Timestamp]);
8
- const encodeTimestamp = (value, encoding = "int64") => {
9
- const buf = new ArrayBuffer(8);
10
- const dv = new DataView(buf);
11
- dv.setBigInt64(0, BigInt(value?.getTime() ?? 0), encoding === "int64le");
12
- return buf;
8
+ const encodeTimestamp = (value, encoding = "i64") => {
9
+ switch (encoding) {
10
+ case "i64": break;
11
+ case "i64le": break;
12
+ default: throw new Error(`${encoding} encoding not supported`);
13
+ }
14
+ const dv = new DataView(new BigInt64Array(1).buffer);
15
+ const littleEndian = encoding.endsWith("le");
16
+ dv.setBigInt64(0, BigInt(value?.getTime() ?? 0), littleEndian);
17
+ return dv.buffer;
13
18
  };
14
19
  exports.encodeTimestamp = encodeTimestamp;
15
- const decodeTimestamp = (value, encoding = "int64", byteOffset) => value ? new Date(Number(new DataView(value).getBigInt64(byteOffset == null ? 0 : Number(byteOffset), encoding === "int64le"))) : undefined;
20
+ const decodeTimestamp = (value, encoding = "i64", byteOffset) => {
21
+ switch (encoding) {
22
+ case "i64": break;
23
+ case "i64le": break;
24
+ default: throw new Error(`${encoding} encoding not supported`);
25
+ }
26
+ const offset = byteOffset == null ? 0 : Number(byteOffset);
27
+ if (!value || offset < 0 || value.byteLength < offset + 8) {
28
+ return undefined;
29
+ }
30
+ const littleEndian = encoding.endsWith("le");
31
+ return new Date(Number(new DataView(value).getBigInt64(offset, littleEndian)));
32
+ };
16
33
  exports.decodeTimestamp = decodeTimestamp;
17
34
  const formatTimestamp = (value, template) => {
18
- if (value == null) {
19
- return "";
20
- }
21
35
  if (!template || typeof template !== "string") {
22
36
  return value.toISOString();
23
37
  }
@@ -76,11 +90,11 @@ exports.funcHoursSince = new Constant_js_1.Constant((value1, value2) => (value1?
76
90
  exports.funcMinutesSince = new Constant_js_1.Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 60000.0, typeTimestampSince);
77
91
  exports.funcSecondsSince = new Constant_js_1.Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 1000.0, typeTimestampSince);
78
92
  const funcEpochTimestamp = new Constant_js_1.Constant((value, epoch = new Date(0)) => new Date(Number(value ?? 0) + epoch?.getTime()), Type_js_1.Type.functionType(Type_js_1.Type.Timestamp, [Type_js_1.Type.Number, Type_js_1.Type.OptionalTimestamp]));
79
- const funcDecodeTimestamp = new Constant_js_1.Constant((value, encoding = "int64", byteOffset) => (0, exports.decodeTimestamp)(value, encoding, byteOffset), Type_js_1.Type.functionType(Type_js_1.Type.OptionalTimestamp, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger]));
80
- const funcParseTimestamp = new Constant_js_1.Constant((value) => parseTimestamp(value), Type_js_1.Type.functionType(Type_js_1.Type.OptionalTimestamp, [Type_js_1.Type.String]));
81
- exports.constTimestamp = {
93
+ const funcDecodeTimestamp = new Constant_js_1.Constant((value, encoding = "i64", byteOffset) => (0, exports.decodeTimestamp)(value, encoding, byteOffset), Type_js_1.Type.functionType(Type_js_1.Type.OptionalTimestamp, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalInteger]));
94
+ const funcParseTimestamp = new Constant_js_1.Constant((value) => parseTimestamp(value), Type_js_1.Type.functionType(Type_js_1.Type.OptionalTimestamp, [Type_js_1.Type.OptionalString]));
95
+ exports.constTimestamp = Object.assign(Object.create(null), {
82
96
  Now: funcNow,
83
97
  Epoch: funcEpochTimestamp,
84
98
  Decode: funcDecodeTimestamp,
85
99
  Parse: funcParseTimestamp,
86
- };
100
+ });
@@ -55,7 +55,7 @@ const equate = (value1, value2) => {
55
55
  };
56
56
  exports.equate = equate;
57
57
  const encode = (value, encoding) => value == null
58
- ? new Uint8Array(0).buffer
58
+ ? Buffer_js_1.EmptyBuffer
59
59
  : typeof value === "boolean"
60
60
  ? new Uint8Array([value ? 255 : 0]).buffer
61
61
  : value instanceof Date
@@ -69,15 +69,15 @@ const encode = (value, encoding) => value == null
69
69
  : typeof value === "string"
70
70
  ? (0, String_js_1.encodeString)(value, encoding ?? "utf8")
71
71
  : Array.isArray(value)
72
- ? value.map((i) => (0, exports.encode)(i, encoding)).reduce((acc, val) => (0, Buffer_js_1.concatBuffers)(acc, val))
72
+ ? value.map((i) => (0, exports.encode)(i, encoding)).reduce((acc, val) => (0, Buffer_js_1.concatBuffers)(acc, val), Buffer_js_1.EmptyBuffer)
73
73
  : typeof value === "object"
74
74
  ? Object.entries(value)
75
75
  .map(([k, v]) => (0, Buffer_js_1.concatBuffers)((0, exports.encode)(k, encoding), (0, exports.encode)(v, encoding)))
76
- .reduce((acc, val) => (0, Buffer_js_1.concatBuffers)(acc, val))
77
- : new Uint8Array(0).buffer;
76
+ .reduce((acc, val) => (0, Buffer_js_1.concatBuffers)(acc, val), Buffer_js_1.EmptyBuffer)
77
+ : Buffer_js_1.EmptyBuffer;
78
78
  exports.encode = encode;
79
79
  const format = (value, formatting) => value == null
80
- ? "null"
80
+ ? ""
81
81
  : typeof value === "boolean"
82
82
  ? value.toString()
83
83
  : value instanceof Date
@@ -91,16 +91,16 @@ const format = (value, formatting) => value == null
91
91
  : typeof value === "string"
92
92
  ? value
93
93
  : Array.isArray(value)
94
- ? value.map((i) => (0, exports.format)(i, formatting)).reduce((acc, val) => acc + val)
94
+ ? value.map((i) => (0, exports.format)(i, formatting)).reduce((acc, val) => acc + val, "")
95
95
  : typeof value === "object"
96
96
  ? Object.entries(value)
97
97
  .map(([k, v]) => (0, exports.format)(k, formatting) + (0, exports.format)(v, formatting))
98
- .reduce((acc, val) => acc + val)
99
- : "function";
98
+ .reduce((acc, val) => acc + val, "")
99
+ : "";
100
100
  exports.format = format;
101
101
  const typeEquator = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Unknown, Type_js_1.Type.Unknown]);
102
102
  exports.funcCoalesce = new Constant_js_1.Constant((value, valueOtherwise) => value ?? valueOtherwise, Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Unknown, Type_js_1.Type.Unknown]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalFloat, [Type_js_1.Type.OptionalFloat, Type_js_1.Type.OptionalFloat]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.OptionalBoolean, Type_js_1.Type.OptionalBoolean]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalTimestamp, [Type_js_1.Type.OptionalTimestamp, Type_js_1.Type.OptionalTimestamp]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalBuffer]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalString, [Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalArray, [Type_js_1.Type.OptionalArray, Type_js_1.Type.OptionalArray]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalObject, [Type_js_1.Type.OptionalObject, Type_js_1.Type.OptionalObject]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalFunction, [Type_js_1.Type.OptionalFunction, Type_js_1.Type.OptionalFunction])));
103
103
  exports.funcEqual = new Constant_js_1.Constant((value1, value2) => (0, exports.equate)(value1, value2), typeEquator);
104
104
  exports.funcNotEqual = new Constant_js_1.Constant((value1, value2) => !(0, exports.equate)(value1, value2), typeEquator);
105
- exports.funcEncode = new Constant_js_1.Constant((value, encoding) => (0, exports.encode)(value, encoding), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Boolean]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Float, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Integer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.String, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Array, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Object, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Function])));
106
- exports.funcFormat = new Constant_js_1.Constant((value, formatting) => (0, exports.format)(value, formatting), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Boolean]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Float, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Integer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Array, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Object, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Function])));
105
+ exports.funcEncode = new Constant_js_1.Constant((value, encoding) => (0, exports.encode)(value, encoding), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Void, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Boolean]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Float, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Integer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.String, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Array, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Object, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Function])));
106
+ exports.funcFormat = new Constant_js_1.Constant((value, formatting) => (0, exports.format)(value, formatting), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Void, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Boolean]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Float, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Integer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Array, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Object, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Function])));
@@ -7,7 +7,7 @@ const Number_js_1 = require("../Number.js");
7
7
  const Buffer_js_1 = require("../Buffer.js");
8
8
  const formatAN = (value, whitespace) => {
9
9
  if (value == null) {
10
- return "null";
10
+ return "";
11
11
  }
12
12
  if (typeof value === "boolean") {
13
13
  return value.toString();
@@ -29,18 +29,18 @@ const formatAN = (value, whitespace) => {
29
29
  }
30
30
  if (Array.isArray(value)) {
31
31
  const [prefix, suffix] = whitespace ? ["\n" + whitespace, "\n"] : ["", ""];
32
- const lines = value.map((i) => `${prefix}${(0, exports.formatAN)(i, whitespace).split("\n").join(prefix)}`);
32
+ const lines = value.map((i) => `${prefix}${(0, exports.formatAN)(i, whitespace)?.split("\n").join(prefix) ?? ""}`);
33
33
  return lines.length ? `[${lines.join(",")}${suffix}]` : "[]";
34
34
  }
35
35
  if (typeof value === "object") {
36
36
  const [prefix, suffix, separator] = whitespace ? ["\n" + whitespace, "\n", ": "] : ["", "", ":"];
37
- const lines = Object.entries(value).map(([k, v]) => `${prefix}"${k}"${separator}${(0, exports.formatAN)(v, whitespace).split("\n").join(prefix)}`);
37
+ const lines = Object.entries(value).map(([k, v]) => `${prefix}"${k}"${separator}${(0, exports.formatAN)(v, whitespace).split("\n").join(prefix) ?? ""}`);
38
38
  return lines.length ? `[${lines.join(",")}${suffix}]` : "[:]";
39
39
  }
40
40
  return "function";
41
41
  };
42
42
  exports.formatAN = formatAN;
43
- const funcFormatAN = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatAN)(value ?? null, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Unknown, Type_js_1.Type.OptionalString]));
44
- exports.constAN = {
43
+ const funcFormatAN = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatAN)(value, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Unknown, Type_js_1.Type.OptionalString]));
44
+ exports.constAN = Object.assign(Object.create(null), {
45
45
  Format: funcFormatAN,
46
- };
46
+ });
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.constIP = void 0;
4
+ const Constant_js_1 = require("../../Constant.js");
5
+ const Type_js_1 = require("../../Type.js");
6
+ const Buffer_js_1 = require("../Buffer.js");
7
+ function encodeIP(value) {
8
+ const prefixLengthParts = value.split("/");
9
+ if (prefixLengthParts.length > 2) {
10
+ throw new Error(`Invalid IP address: ${value}`);
11
+ }
12
+ const zeroCompressionParts = prefixLengthParts[0].split("::");
13
+ if (zeroCompressionParts.length > 2) {
14
+ throw new Error(`Invalid IP address: ${value}`);
15
+ }
16
+ const zeroCompressionFlag = zeroCompressionParts.length === 2;
17
+ const beginParts = zeroCompressionParts[0] ? zeroCompressionParts[0].split(":") : [];
18
+ if (!zeroCompressionFlag && beginParts.length === 1) {
19
+ const prefix = prefixLengthParts.length === 2 ? (0, Buffer_js_1.parseBoundedInteger)(prefixLengthParts[1], 10, 0, 32) : undefined;
20
+ const ipv4Parts = prefixLengthParts[0].split(".");
21
+ if (ipv4Parts.length !== 4) {
22
+ throw new Error(`Invalid IPv4 address: ${value}`);
23
+ }
24
+ let dv;
25
+ if (prefix != null) {
26
+ dv = new DataView(new ArrayBuffer(5));
27
+ dv.setUint8(4, prefix);
28
+ }
29
+ else {
30
+ dv = new DataView(new ArrayBuffer(4));
31
+ }
32
+ for (let i = 0; i < 4; ++i) {
33
+ const octet = (0, Buffer_js_1.parseBoundedInteger)(ipv4Parts[i], 10, 0, 255);
34
+ dv.setUint8(i, octet);
35
+ }
36
+ return dv.buffer;
37
+ }
38
+ const prefix = prefixLengthParts.length === 2 ? (0, Buffer_js_1.parseBoundedInteger)(prefixLengthParts[1], 10, 0, 128) : undefined;
39
+ let dv;
40
+ if (prefix != null) {
41
+ dv = new DataView(new ArrayBuffer(17));
42
+ dv.setUint8(16, prefix);
43
+ }
44
+ else {
45
+ dv = new DataView(new ArrayBuffer(16));
46
+ }
47
+ const endParts = zeroCompressionFlag && zeroCompressionParts[1] ? zeroCompressionParts[1].split(":") : [];
48
+ const lastPart = zeroCompressionFlag ? endParts[endParts.length - 1] : beginParts[beginParts.length - 1];
49
+ const ipv4TailFlag = lastPart?.includes(".");
50
+ if (ipv4TailFlag) {
51
+ if (zeroCompressionFlag && beginParts.length + endParts.length > 6
52
+ || !zeroCompressionFlag && beginParts.length + endParts.length !== 7) {
53
+ throw new Error(`Invalid IP address: ${value}`);
54
+ }
55
+ const ipv4Parts = lastPart.split(".");
56
+ if (ipv4Parts.length !== 4) {
57
+ throw new Error(`Invalid IP address: ${value}`);
58
+ }
59
+ for (let i = 0; i < 4; ++i) {
60
+ const octet = (0, Buffer_js_1.parseBoundedInteger)(ipv4Parts[i], 10, 0, 255);
61
+ dv.setUint8(12 + i, octet);
62
+ }
63
+ }
64
+ else {
65
+ if (zeroCompressionFlag && beginParts.length + endParts.length > 7
66
+ || !zeroCompressionFlag && beginParts.length + endParts.length !== 8) {
67
+ throw new Error(`Invalid IP address: ${value}`);
68
+ }
69
+ }
70
+ if (beginParts.length > 0) {
71
+ let length = beginParts.length;
72
+ if (ipv4TailFlag && !zeroCompressionFlag) {
73
+ --length;
74
+ }
75
+ for (let i = 0; i < length; ++i) {
76
+ const hextet = (0, Buffer_js_1.parseBoundedInteger)(beginParts[i], 16, 0, 0xffff);
77
+ dv.setUint16(i << 1, hextet);
78
+ }
79
+ }
80
+ if (endParts.length > 0) {
81
+ let length = endParts.length;
82
+ let index = 8 - endParts.length;
83
+ if (ipv4TailFlag) {
84
+ --length;
85
+ --index;
86
+ }
87
+ for (let i = 0; i < length; ++i) {
88
+ const hextet = (0, Buffer_js_1.parseBoundedInteger)(endParts[i], 16, 0, 0xffff);
89
+ dv.setUint16(index + i << 1, hextet);
90
+ }
91
+ }
92
+ return dv.buffer;
93
+ }
94
+ function decodeIP(value) {
95
+ const dv = new DataView(value);
96
+ if (dv.byteLength === 4 || dv.byteLength === 5) {
97
+ const prefix = dv.byteLength === 5 ? dv.getUint8(4) : undefined;
98
+ if (prefix != null && prefix > 32) {
99
+ throw new Error(`Invalid IPv4 prefix length: ${prefix}`);
100
+ }
101
+ const address = Array.from({ length: 4 }, (_value, i) => dv.getUint8(i)).join(".");
102
+ return prefix == null ? address : `${address}/${prefix}`;
103
+ }
104
+ if (dv.byteLength === 16 || dv.byteLength === 17) {
105
+ const prefix = dv.byteLength === 17 ? dv.getUint8(16) : undefined;
106
+ if (prefix != null && prefix > 128) {
107
+ throw new Error(`Invalid IPv6 prefix length: ${prefix}`);
108
+ }
109
+ const parts = Array.from({ length: 8 }, (_value, i) => dv.getUint16(i << 1));
110
+ let longestStart = -1;
111
+ let longestLength = 0;
112
+ for (let start = 0; start < parts.length;) {
113
+ if (parts[start] !== 0) {
114
+ ++start;
115
+ continue;
116
+ }
117
+ let end = start + 1;
118
+ while (end < parts.length && parts[end] === 0) {
119
+ ++end;
120
+ }
121
+ if (end - start > longestLength) {
122
+ longestStart = start;
123
+ longestLength = end - start;
124
+ }
125
+ start = end;
126
+ }
127
+ let address;
128
+ if (longestLength > 1) {
129
+ const begin = parts.slice(0, longestStart).map((part) => part.toString(16)).join(":");
130
+ const end = parts.slice(longestStart + longestLength).map((part) => part.toString(16)).join(":");
131
+ address = `${begin}::${end}`;
132
+ }
133
+ else {
134
+ address = parts.map((part) => part.toString(16)).join(":");
135
+ }
136
+ return prefix == null ? address : `${address}/${prefix}`;
137
+ }
138
+ throw new Error(`Invalid IP buffer length: ${dv.byteLength}`);
139
+ }
140
+ class IPSet {
141
+ _ipv4Sets = Array(33).fill(undefined);
142
+ _ipv6Sets = Array(129).fill(undefined);
143
+ add(...values) {
144
+ for (const value of values.flat()) {
145
+ const dv = new DataView(encodeIP(value));
146
+ const { array, prefix, shift } = this._select(dv);
147
+ const set = array[prefix];
148
+ const key = shift(prefix);
149
+ if (set) {
150
+ set.add(key);
151
+ }
152
+ else {
153
+ array[prefix] = new Set([key]);
154
+ }
155
+ }
156
+ return this;
157
+ }
158
+ has(value) {
159
+ const dv = new DataView(encodeIP(value));
160
+ const { array, prefix, shift } = this._select(dv);
161
+ for (let i = 0; i <= prefix; ++i) {
162
+ const set = array[i];
163
+ if (set) {
164
+ const key = shift(i);
165
+ if (set.has(key)) {
166
+ return true;
167
+ }
168
+ }
169
+ }
170
+ return false;
171
+ }
172
+ clear() {
173
+ this._ipv4Sets.fill(undefined);
174
+ this._ipv6Sets.fill(undefined);
175
+ }
176
+ _select(dv) {
177
+ const prefix = dv.byteLength & 1 ? dv.getUint8(dv.byteLength - 1) : dv.byteLength << 3;
178
+ if (dv.byteLength < 16) {
179
+ const address = BigInt(dv.getUint32(0));
180
+ const shift = (p) => address >> BigInt(32 - p);
181
+ return {
182
+ array: this._ipv4Sets,
183
+ prefix,
184
+ shift,
185
+ };
186
+ }
187
+ else {
188
+ const address = dv.getBigUint64(0) << 64n | dv.getBigUint64(8);
189
+ const shift = (p) => address >> BigInt(128 - p);
190
+ return {
191
+ array: this._ipv6Sets,
192
+ prefix,
193
+ shift,
194
+ };
195
+ }
196
+ }
197
+ }
198
+ const funcMatchIP = new Constant_js_1.Constant((values, ...search) => {
199
+ try {
200
+ const ipset = new IPSet().add(values);
201
+ for (const ip of search.flat()) {
202
+ try {
203
+ if (ipset.has(ip)) {
204
+ return true;
205
+ }
206
+ }
207
+ catch { }
208
+ }
209
+ }
210
+ catch { }
211
+ return false;
212
+ }, Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Array, Type_js_1.Type.union(Type_js_1.Type.String, Type_js_1.Type.Array)], true));
213
+ const funcEncodeIP = new Constant_js_1.Constant((value) => {
214
+ if (value == null) {
215
+ return Buffer_js_1.EmptyBuffer;
216
+ }
217
+ try {
218
+ return encodeIP(value);
219
+ }
220
+ catch {
221
+ return Buffer_js_1.EmptyBuffer;
222
+ }
223
+ }, Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.OptionalString]));
224
+ const funcDecodeIP = new Constant_js_1.Constant((value) => {
225
+ if (value == null) {
226
+ return undefined;
227
+ }
228
+ try {
229
+ return decodeIP(value);
230
+ }
231
+ catch {
232
+ return undefined;
233
+ }
234
+ }, Type_js_1.Type.functionType(Type_js_1.Type.OptionalString, [Type_js_1.Type.OptionalBuffer]));
235
+ exports.constIP = Object.assign(Object.create(null), {
236
+ Match: funcMatchIP,
237
+ Encode: funcEncodeIP,
238
+ Decode: funcDecodeIP,
239
+ });
@@ -4,11 +4,11 @@ exports.constJSON = exports.formatJSON = void 0;
4
4
  const Constant_js_1 = require("../../Constant.js");
5
5
  const Type_js_1 = require("../../Type.js");
6
6
  const typeJson = Type_js_1.Type.union(Type_js_1.Type.Void, Type_js_1.Type.Boolean, Type_js_1.Type.Timestamp, Type_js_1.Type.Float, Type_js_1.Type.Integer, Type_js_1.Type.String, Type_js_1.Type.Array, Type_js_1.Type.Object);
7
- const formatJSON = (value, whitespace) => JSON.stringify(value, (_key, value) => typeof value === "bigint" ? `"${value.toString()}"` : value, whitespace);
7
+ const formatJSON = (value, whitespace) => value == null ? "" : JSON.stringify(value, (_key, value) => typeof value === "bigint" ? `"${value.toString()}"` : value, whitespace);
8
8
  exports.formatJSON = formatJSON;
9
- const funcFormatJSON = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatJSON)(value ?? null, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [typeJson, Type_js_1.Type.OptionalString]));
10
- const funcParseJSON = new Constant_js_1.Constant((value) => JSON.parse(value), Type_js_1.Type.functionType(typeJson, [Type_js_1.Type.String]));
11
- exports.constJSON = {
9
+ const funcFormatJSON = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatJSON)(value, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [typeJson, Type_js_1.Type.OptionalString]));
10
+ const funcParseJSON = new Constant_js_1.Constant((value) => value == null ? undefined : JSON.parse(value), Type_js_1.Type.functionType(typeJson, [Type_js_1.Type.OptionalString]));
11
+ exports.constJSON = Object.assign(Object.create(null), {
12
12
  Format: funcFormatJSON,
13
13
  Parse: funcParseJSON,
14
- };
14
+ });
@@ -33,7 +33,7 @@ class ObjectNode extends Node_js_1.Node {
33
33
  return this;
34
34
  }
35
35
  evaluate() {
36
- const result = {};
36
+ const result = Object.create(null);
37
37
  for (const [key, value] of this._subnodes) {
38
38
  result[key.evaluate()] = value.evaluate();
39
39
  }
@@ -13,10 +13,4 @@ export declare const funcReduce: Constant;
13
13
  export declare const funcCompose: Constant;
14
14
  export declare const funcPrepend: Constant;
15
15
  export declare const funcAppend: Constant;
16
- export declare const constArray: {
17
- Join: Constant;
18
- Range: Constant;
19
- Unique: Constant;
20
- Intersection: Constant;
21
- Difference: Constant;
22
- };
16
+ export declare const constArray: any;
@@ -50,7 +50,7 @@ export const funcCompose = new Constant((value, callback) => {
50
50
  if (value == null) {
51
51
  return undefined;
52
52
  }
53
- const obj = {};
53
+ const obj = Object.create(null);
54
54
  for (let i = 0; i < value.length; ++i) {
55
55
  const key = value[i];
56
56
  obj[key] = callback(obj, key, BigInt(i), value);
@@ -82,10 +82,10 @@ const funcUnique = new Constant((value) => {
82
82
  }, Type.functionType(Type.Array, [Type.Array]));
83
83
  const funcIntersection = new Constant((value1, value2) => value1?.filter((i) => value2?.some((v) => equate(v, i))) ?? [], typeArrayOperator);
84
84
  const funcDifference = new Constant((value1, value2) => [...value1?.filter((i) => value2?.every((v) => !equate(v, i))) ?? [], ...value2?.filter((i) => value1?.every((v) => !equate(v, i))) ?? []], typeArrayOperator);
85
- export const constArray = {
85
+ export const constArray = Object.assign(Object.create(null), {
86
86
  Join: funcJoin,
87
87
  Range: funcRange,
88
88
  Unique: funcUnique,
89
89
  Intersection: funcIntersection,
90
90
  Difference: funcDifference,
91
- };
91
+ });
@@ -2,10 +2,4 @@ import { Constant } from "../Constant.js";
2
2
  export declare const funcOr: Constant;
3
3
  export declare const funcAnd: Constant;
4
4
  export declare const funcNot: Constant;
5
- export declare const constBoolean: {
6
- Or: Constant;
7
- And: Constant;
8
- Not: Constant;
9
- Decode: Constant;
10
- Parse: Constant;
11
- };
5
+ export declare const constBoolean: any;
@@ -5,18 +5,21 @@ const typeBooleanLogic = Type.functionType(Type.Boolean, [typeBooleanOrArray], t
5
5
  export const funcOr = new Constant((...values) => values.flat().some((v) => v), typeBooleanLogic);
6
6
  export const funcAnd = new Constant((...values) => values.flat().every((v) => v), typeBooleanLogic);
7
7
  export const funcNot = new Constant((value) => !value, Type.functionType(Type.Boolean, [Type.Boolean]));
8
- const funcDecodeBoolean = new Constant((value, byteOffset) => value ? Boolean(new Uint8Array(value)[byteOffset == null ? 0 : Number(byteOffset)]) : undefined, Type.functionType(Type.OptionalBoolean, [Type.Buffer, Type.OptionalInteger]));
8
+ const funcDecodeBoolean = new Constant((value, byteOffset) => {
9
+ const offset = byteOffset == null ? 0 : Number(byteOffset);
10
+ return value && offset >= 0 && value.byteLength > offset ? Boolean(new Uint8Array(value)[offset]) : undefined;
11
+ }, Type.functionType(Type.OptionalBoolean, [Type.OptionalBuffer, Type.OptionalInteger]));
9
12
  const funcParseBoolean = new Constant((value) => {
10
13
  if (value == null) {
11
14
  return undefined;
12
15
  }
13
16
  const v = value.toLowerCase();
14
17
  return v === "true" ? true : v === "false" ? false : undefined;
15
- }, Type.functionType(Type.OptionalBoolean, [Type.String]));
16
- export const constBoolean = {
18
+ }, Type.functionType(Type.OptionalBoolean, [Type.OptionalString]));
19
+ export const constBoolean = Object.assign(Object.create(null), {
17
20
  Or: funcOr,
18
21
  And: funcAnd,
19
22
  Not: funcNot,
20
23
  Decode: funcDecodeBoolean,
21
24
  Parse: funcParseBoolean,
22
- };
25
+ });
@@ -1,12 +1,11 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  export type BufferFormatting = "hex" | "base64";
3
+ export declare const EmptyBuffer: ArrayBuffer;
4
+ export declare const parseBoundedInteger: (input: string, base: 10 | 16, min: number, max: number) => number;
3
5
  export declare const equateBuffers: (value1: ArrayBuffer, value2: ArrayBuffer) => boolean;
4
6
  export declare const containsBuffer: (value: ArrayBuffer, search: ArrayBuffer, startPos?: number) => boolean;
5
7
  export declare const concatBuffers: (value1: ArrayBuffer, value2: ArrayBuffer) => ArrayBuffer;
6
- export declare const formatBuffer: (value?: ArrayBuffer, formatting?: BufferFormatting) => string;
8
+ export declare const formatBuffer: (value: ArrayBuffer, formatting?: BufferFormatting) => string;
7
9
  export declare const parseBuffer: (value?: string) => ArrayBuffer | undefined;
8
10
  export declare const funcByte: Constant;
9
- export declare const constBuffer: {
10
- Random: Constant;
11
- Parse: Constant;
12
- };
11
+ export declare const constBuffer: any;