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
@@ -0,0 +1,236 @@
1
+ import { Constant } from "../../Constant.js";
2
+ import { Type } from "../../Type.js";
3
+ import { EmptyBuffer, parseBoundedInteger } from "../Buffer.js";
4
+ function encodeIP(value) {
5
+ const prefixLengthParts = value.split("/");
6
+ if (prefixLengthParts.length > 2) {
7
+ throw new Error(`Invalid IP address: ${value}`);
8
+ }
9
+ const zeroCompressionParts = prefixLengthParts[0].split("::");
10
+ if (zeroCompressionParts.length > 2) {
11
+ throw new Error(`Invalid IP address: ${value}`);
12
+ }
13
+ const zeroCompressionFlag = zeroCompressionParts.length === 2;
14
+ const beginParts = zeroCompressionParts[0] ? zeroCompressionParts[0].split(":") : [];
15
+ if (!zeroCompressionFlag && beginParts.length === 1) {
16
+ const prefix = prefixLengthParts.length === 2 ? parseBoundedInteger(prefixLengthParts[1], 10, 0, 32) : undefined;
17
+ const ipv4Parts = prefixLengthParts[0].split(".");
18
+ if (ipv4Parts.length !== 4) {
19
+ throw new Error(`Invalid IPv4 address: ${value}`);
20
+ }
21
+ let dv;
22
+ if (prefix != null) {
23
+ dv = new DataView(new ArrayBuffer(5));
24
+ dv.setUint8(4, prefix);
25
+ }
26
+ else {
27
+ dv = new DataView(new ArrayBuffer(4));
28
+ }
29
+ for (let i = 0; i < 4; ++i) {
30
+ const octet = parseBoundedInteger(ipv4Parts[i], 10, 0, 255);
31
+ dv.setUint8(i, octet);
32
+ }
33
+ return dv.buffer;
34
+ }
35
+ const prefix = prefixLengthParts.length === 2 ? parseBoundedInteger(prefixLengthParts[1], 10, 0, 128) : undefined;
36
+ let dv;
37
+ if (prefix != null) {
38
+ dv = new DataView(new ArrayBuffer(17));
39
+ dv.setUint8(16, prefix);
40
+ }
41
+ else {
42
+ dv = new DataView(new ArrayBuffer(16));
43
+ }
44
+ const endParts = zeroCompressionFlag && zeroCompressionParts[1] ? zeroCompressionParts[1].split(":") : [];
45
+ const lastPart = zeroCompressionFlag ? endParts[endParts.length - 1] : beginParts[beginParts.length - 1];
46
+ const ipv4TailFlag = lastPart?.includes(".");
47
+ if (ipv4TailFlag) {
48
+ if (zeroCompressionFlag && beginParts.length + endParts.length > 6
49
+ || !zeroCompressionFlag && beginParts.length + endParts.length !== 7) {
50
+ throw new Error(`Invalid IP address: ${value}`);
51
+ }
52
+ const ipv4Parts = lastPart.split(".");
53
+ if (ipv4Parts.length !== 4) {
54
+ throw new Error(`Invalid IP address: ${value}`);
55
+ }
56
+ for (let i = 0; i < 4; ++i) {
57
+ const octet = parseBoundedInteger(ipv4Parts[i], 10, 0, 255);
58
+ dv.setUint8(12 + i, octet);
59
+ }
60
+ }
61
+ else {
62
+ if (zeroCompressionFlag && beginParts.length + endParts.length > 7
63
+ || !zeroCompressionFlag && beginParts.length + endParts.length !== 8) {
64
+ throw new Error(`Invalid IP address: ${value}`);
65
+ }
66
+ }
67
+ if (beginParts.length > 0) {
68
+ let length = beginParts.length;
69
+ if (ipv4TailFlag && !zeroCompressionFlag) {
70
+ --length;
71
+ }
72
+ for (let i = 0; i < length; ++i) {
73
+ const hextet = parseBoundedInteger(beginParts[i], 16, 0, 0xffff);
74
+ dv.setUint16(i << 1, hextet);
75
+ }
76
+ }
77
+ if (endParts.length > 0) {
78
+ let length = endParts.length;
79
+ let index = 8 - endParts.length;
80
+ if (ipv4TailFlag) {
81
+ --length;
82
+ --index;
83
+ }
84
+ for (let i = 0; i < length; ++i) {
85
+ const hextet = parseBoundedInteger(endParts[i], 16, 0, 0xffff);
86
+ dv.setUint16(index + i << 1, hextet);
87
+ }
88
+ }
89
+ return dv.buffer;
90
+ }
91
+ function decodeIP(value) {
92
+ const dv = new DataView(value);
93
+ if (dv.byteLength === 4 || dv.byteLength === 5) {
94
+ const prefix = dv.byteLength === 5 ? dv.getUint8(4) : undefined;
95
+ if (prefix != null && prefix > 32) {
96
+ throw new Error(`Invalid IPv4 prefix length: ${prefix}`);
97
+ }
98
+ const address = Array.from({ length: 4 }, (_value, i) => dv.getUint8(i)).join(".");
99
+ return prefix == null ? address : `${address}/${prefix}`;
100
+ }
101
+ if (dv.byteLength === 16 || dv.byteLength === 17) {
102
+ const prefix = dv.byteLength === 17 ? dv.getUint8(16) : undefined;
103
+ if (prefix != null && prefix > 128) {
104
+ throw new Error(`Invalid IPv6 prefix length: ${prefix}`);
105
+ }
106
+ const parts = Array.from({ length: 8 }, (_value, i) => dv.getUint16(i << 1));
107
+ let longestStart = -1;
108
+ let longestLength = 0;
109
+ for (let start = 0; start < parts.length;) {
110
+ if (parts[start] !== 0) {
111
+ ++start;
112
+ continue;
113
+ }
114
+ let end = start + 1;
115
+ while (end < parts.length && parts[end] === 0) {
116
+ ++end;
117
+ }
118
+ if (end - start > longestLength) {
119
+ longestStart = start;
120
+ longestLength = end - start;
121
+ }
122
+ start = end;
123
+ }
124
+ let address;
125
+ if (longestLength > 1) {
126
+ const begin = parts.slice(0, longestStart).map((part) => part.toString(16)).join(":");
127
+ const end = parts.slice(longestStart + longestLength).map((part) => part.toString(16)).join(":");
128
+ address = `${begin}::${end}`;
129
+ }
130
+ else {
131
+ address = parts.map((part) => part.toString(16)).join(":");
132
+ }
133
+ return prefix == null ? address : `${address}/${prefix}`;
134
+ }
135
+ throw new Error(`Invalid IP buffer length: ${dv.byteLength}`);
136
+ }
137
+ class IPSet {
138
+ _ipv4Sets = Array(33).fill(undefined);
139
+ _ipv6Sets = Array(129).fill(undefined);
140
+ add(...values) {
141
+ for (const value of values.flat()) {
142
+ const dv = new DataView(encodeIP(value));
143
+ const { array, prefix, shift } = this._select(dv);
144
+ const set = array[prefix];
145
+ const key = shift(prefix);
146
+ if (set) {
147
+ set.add(key);
148
+ }
149
+ else {
150
+ array[prefix] = new Set([key]);
151
+ }
152
+ }
153
+ return this;
154
+ }
155
+ has(value) {
156
+ const dv = new DataView(encodeIP(value));
157
+ const { array, prefix, shift } = this._select(dv);
158
+ for (let i = 0; i <= prefix; ++i) {
159
+ const set = array[i];
160
+ if (set) {
161
+ const key = shift(i);
162
+ if (set.has(key)) {
163
+ return true;
164
+ }
165
+ }
166
+ }
167
+ return false;
168
+ }
169
+ clear() {
170
+ this._ipv4Sets.fill(undefined);
171
+ this._ipv6Sets.fill(undefined);
172
+ }
173
+ _select(dv) {
174
+ const prefix = dv.byteLength & 1 ? dv.getUint8(dv.byteLength - 1) : dv.byteLength << 3;
175
+ if (dv.byteLength < 16) {
176
+ const address = BigInt(dv.getUint32(0));
177
+ const shift = (p) => address >> BigInt(32 - p);
178
+ return {
179
+ array: this._ipv4Sets,
180
+ prefix,
181
+ shift,
182
+ };
183
+ }
184
+ else {
185
+ const address = dv.getBigUint64(0) << 64n | dv.getBigUint64(8);
186
+ const shift = (p) => address >> BigInt(128 - p);
187
+ return {
188
+ array: this._ipv6Sets,
189
+ prefix,
190
+ shift,
191
+ };
192
+ }
193
+ }
194
+ }
195
+ const funcMatchIP = new Constant((values, ...search) => {
196
+ try {
197
+ const ipset = new IPSet().add(values);
198
+ for (const ip of search.flat()) {
199
+ try {
200
+ if (ipset.has(ip)) {
201
+ return true;
202
+ }
203
+ }
204
+ catch { }
205
+ }
206
+ }
207
+ catch { }
208
+ return false;
209
+ }, Type.functionType(Type.Boolean, [Type.Array, Type.union(Type.String, Type.Array)], true));
210
+ const funcEncodeIP = new Constant((value) => {
211
+ if (value == null) {
212
+ return EmptyBuffer;
213
+ }
214
+ try {
215
+ return encodeIP(value);
216
+ }
217
+ catch {
218
+ return EmptyBuffer;
219
+ }
220
+ }, Type.functionType(Type.Buffer, [Type.OptionalString]));
221
+ const funcDecodeIP = new Constant((value) => {
222
+ if (value == null) {
223
+ return undefined;
224
+ }
225
+ try {
226
+ return decodeIP(value);
227
+ }
228
+ catch {
229
+ return undefined;
230
+ }
231
+ }, Type.functionType(Type.OptionalString, [Type.OptionalBuffer]));
232
+ export const constIP = Object.assign(Object.create(null), {
233
+ Match: funcMatchIP,
234
+ Encode: funcEncodeIP,
235
+ Decode: funcDecodeIP,
236
+ });
@@ -1,7 +1,3 @@
1
- import { Constant } from "../../Constant.js";
2
1
  import { Value } from "../../Value.js";
3
2
  export declare const formatJSON: (value: Value, whitespace?: string) => string;
4
- export declare const constJSON: {
5
- Format: Constant;
6
- Parse: Constant;
7
- };
3
+ export declare const constJSON: any;
@@ -1,10 +1,10 @@
1
1
  import { Constant } from "../../Constant.js";
2
2
  import { Type } from "../../Type.js";
3
3
  const typeJson = Type.union(Type.Void, Type.Boolean, Type.Timestamp, Type.Float, Type.Integer, Type.String, Type.Array, Type.Object);
4
- export const formatJSON = (value, whitespace) => JSON.stringify(value, (_key, value) => typeof value === "bigint" ? `"${value.toString()}"` : value, whitespace);
5
- const funcFormatJSON = new Constant((value, whitespace) => formatJSON(value ?? null, whitespace), Type.functionType(Type.String, [typeJson, Type.OptionalString]));
6
- const funcParseJSON = new Constant((value) => JSON.parse(value), Type.functionType(typeJson, [Type.String]));
7
- export const constJSON = {
4
+ export const formatJSON = (value, whitespace) => value == null ? "" : JSON.stringify(value, (_key, value) => typeof value === "bigint" ? `"${value.toString()}"` : value, whitespace);
5
+ const funcFormatJSON = new Constant((value, whitespace) => formatJSON(value, whitespace), Type.functionType(Type.String, [typeJson, Type.OptionalString]));
6
+ const funcParseJSON = new Constant((value) => value == null ? undefined : JSON.parse(value), Type.functionType(typeJson, [Type.OptionalString]));
7
+ export const constJSON = Object.assign(Object.create(null), {
8
8
  Format: funcFormatJSON,
9
9
  Parse: funcParseJSON,
10
- };
10
+ });
@@ -30,7 +30,7 @@ export class ObjectNode extends Node {
30
30
  return this;
31
31
  }
32
32
  evaluate() {
33
- const result = {};
33
+ const result = Object.create(null);
34
34
  for (const [key, value] of this._subnodes) {
35
35
  result[key.evaluate()] = value.evaluate();
36
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "affinirum",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Affinirum Scripting Language",
5
5
  "main": "dst/cjs/index.js",
6
6
  "module": "dst/index.js",