affinirum 1.4.0 → 1.4.1

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.
@@ -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]));
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
44
  exports.constAN = {
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 = {
236
+ Match: funcMatchIP,
237
+ Encode: funcEncodeIP,
238
+ Decode: funcDecodeIP,
239
+ };
@@ -4,10 +4,10 @@ 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]));
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
11
  exports.constJSON = {
12
12
  Format: funcFormatJSON,
13
13
  Parse: funcParseJSON,
@@ -5,14 +5,17 @@ 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]));
18
+ }, Type.functionType(Type.OptionalBoolean, [Type.OptionalString]));
16
19
  export const constBoolean = {
17
20
  Or: funcOr,
18
21
  And: funcAnd,
@@ -1,9 +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
11
  export declare const constBuffer: {
@@ -1,5 +1,36 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
+ export const EmptyBuffer = new ArrayBuffer(0);
4
+ const ZERO = "0".charCodeAt(0), NINE = "9".charCodeAt(0);
5
+ const UPPER_A = "A".charCodeAt(0), UPPER_F = "F".charCodeAt(0);
6
+ const LOWER_A = "a".charCodeAt(0), LOWER_F = "f".charCodeAt(0);
7
+ export const parseBoundedInteger = (input, base, min, max) => {
8
+ if (input.length === 0) {
9
+ throw new Error(`Invalid IP component: ${input}`);
10
+ }
11
+ let value = 0;
12
+ for (let i = 0; i < input.length; i++) {
13
+ const code = input.charCodeAt(i);
14
+ let digit;
15
+ if (code >= ZERO && code <= NINE) {
16
+ digit = code - ZERO;
17
+ }
18
+ else if (base === 16 && code >= UPPER_A && code <= UPPER_F) {
19
+ digit = code - UPPER_A + 10;
20
+ }
21
+ else if (base === 16 && code >= LOWER_A && code <= LOWER_F) {
22
+ digit = code - LOWER_A + 10;
23
+ }
24
+ else {
25
+ throw new Error(`Invalid IP component: ${input}`);
26
+ }
27
+ value = value * base + digit;
28
+ }
29
+ if (value < min || value > max) {
30
+ throw new Error(`Invalid IP component: ${input}`);
31
+ }
32
+ return value;
33
+ };
3
34
  export const equateBuffers = (value1, value2) => {
4
35
  if (value1.byteLength !== value2.byteLength) {
5
36
  return false;
@@ -67,9 +98,6 @@ export const concatBuffers = (value1, value2) => {
67
98
  return bytes.buffer;
68
99
  };
69
100
  export const formatBuffer = (value, formatting = "hex") => {
70
- if (value == null) {
71
- return "";
72
- }
73
101
  if (formatting === "base64") {
74
102
  let binary = "";
75
103
  const bytes = new Uint8Array(value);
@@ -80,12 +108,15 @@ export const formatBuffer = (value, formatting = "hex") => {
80
108
  }
81
109
  return btoa(binary);
82
110
  }
83
- const bytes = new Uint8Array(value);
84
- let str = "";
85
- for (let i = 0; i < bytes.byteLength; ++i) {
86
- str += bytes[i].toString(16).padStart(2, "0");
111
+ else if (formatting === "hex") {
112
+ const bytes = new Uint8Array(value);
113
+ let str = "";
114
+ for (let i = 0; i < bytes.byteLength; ++i) {
115
+ str += bytes[i].toString(16).padStart(2, "0");
116
+ }
117
+ return str;
87
118
  }
88
- return str;
119
+ throw new Error(`${formatting} formatting not supported`);
89
120
  };
90
121
  export const parseBuffer = (value) => {
91
122
  if (value == null) {
@@ -93,7 +124,7 @@ export const parseBuffer = (value) => {
93
124
  }
94
125
  const bytes = new Uint8Array(Math.ceil(value.length / 2));
95
126
  for (let i = 0, c = 0; c < value.length; ++i) {
96
- bytes[i] = Number.parseInt(value.slice(c, c += 2), 16);
127
+ bytes[i] = parseBoundedInteger(value.slice(c, c += 2), 16, 0, 0xff);
97
128
  }
98
129
  return bytes.buffer;
99
130
  };
@@ -101,7 +132,7 @@ export const funcByte = new Constant((value, pos) => value == null
101
132
  ? undefined
102
133
  : value.slice(Number(pos), Number(pos) + 1), Type.functionType(Type.OptionalBuffer, [Type.Buffer, Type.Integer]));
103
134
  const funcRandomBuffer = new Constant((value) => value == null || value <= 0n ? new Uint8Array(0).buffer : crypto.getRandomValues(new Uint8Array(Number(value))).buffer, Type.functionType(Type.Buffer, [Type.Integer]), false);
104
- const funcParseBuffer = new Constant((value) => parseBuffer(value), Type.functionType(Type.OptionalBuffer, [Type.String]));
135
+ const funcParseBuffer = new Constant((value) => parseBuffer(value), Type.functionType(Type.OptionalBuffer, [Type.OptionalString]));
105
136
  export const constBuffer = {
106
137
  Random: funcRandomBuffer,
107
138
  Parse: funcParseBuffer,
@@ -1,6 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  export type FloatEncoding = "f32" | "f32le" | "f64" | "f64le";
3
- export declare const encodeFloat: (value?: number, encoding?: FloatEncoding) => ArrayBuffer;
3
+ export declare const encodeFloat: (value: number, encoding?: FloatEncoding) => ArrayBuffer;
4
4
  export declare const constFloat: {
5
5
  NAN: Constant;
6
6
  PositiveInfinity: Constant;
@@ -1,9 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  export const encodeFloat = (value, encoding = "f64") => {
4
- if (value == null) {
5
- return new Uint8Array(0).buffer;
6
- }
7
4
  let bits = "";
8
5
  for (let i = 0; i < encoding.length; ++i) {
9
6
  const c = encoding[i];
@@ -56,8 +53,8 @@ const funcFloor = new Constant((value) => Math.floor(Number(value)), typeTransfo
56
53
  const funcRound = new Constant((value) => Math.round(Number(value)), typeTransform);
57
54
  const funcTruncate = new Constant((value) => Math.trunc(Number(value)), typeTransform);
58
55
  const funcRandomFloat = new Constant((value) => value == null ? undefined : Math.random() * Number(value), Type.functionType(Type.Float, [Type.Float]), false);
59
- const funcDecodeFloat = new Constant((value, encoding = "f64", byteOffset) => decodeFloat(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalFloat, [Type.Buffer, Type.OptionalString, Type.OptionalInteger]));
60
- const funcParseFloat = new Constant((value) => value ? Number.parseFloat(value) : undefined, Type.functionType(Type.OptionalFloat, [Type.String]));
56
+ const funcDecodeFloat = new Constant((value, encoding = "f64", byteOffset) => decodeFloat(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalFloat, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger]));
57
+ const funcParseFloat = new Constant((value) => value ? Number.parseFloat(value) : undefined, Type.functionType(Type.OptionalFloat, [Type.OptionalString]));
61
58
  export const constFloat = {
62
59
  NAN: new Constant(Number.NaN),
63
60
  PositiveInfinity: new Constant(Number.POSITIVE_INFINITY),
@@ -1,6 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  export type IntegerEncoding = "i8" | "i16" | "i16le" | "i32" | "i32le" | "i64" | "i64le" | "n8" | "n16" | "n16le" | "n32" | "n32le" | "n64" | "n64le";
3
- export declare const encodeInteger: (value?: bigint, encoding?: IntegerEncoding) => ArrayBuffer;
3
+ export declare const encodeInteger: (value: bigint, encoding?: IntegerEncoding) => ArrayBuffer;
4
4
  export declare const constInteger: {
5
5
  Sum: Constant;
6
6
  Min: Constant;
@@ -1,9 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  export const encodeInteger = (value, encoding = "i64") => {
4
- if (value == null) {
5
- return new Uint8Array(0).buffer;
6
- }
7
4
  let bits = "";
8
5
  for (let i = 0; i < encoding.length; ++i) {
9
6
  const c = encoding[i];
@@ -88,8 +85,14 @@ const funcSum = new Constant((...values) => BigInt.asIntN(64, values.flat().redu
88
85
  const funcMin = new Constant((...values) => BigInt.asIntN(64, values.flat().reduce((min, val) => val < min ? val : min)), typeAggregator);
89
86
  const funcMax = new Constant((...values) => BigInt.asIntN(64, values.flat().reduce((max, val) => val > max ? val : max)), typeAggregator);
90
87
  const funcRandomInteger = new Constant((value) => value == null ? undefined : BigInt.asIntN(64, BigInt(Math.floor(Math.random() * Number(value)))), Type.functionType(Type.Integer, [Type.Integer]), false);
91
- const funcDecodeInteger = new Constant((value, encoding = "i64", byteOffset) => decodeInteger(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalInteger, [Type.Buffer, Type.OptionalString, Type.OptionalInteger]));
92
- const funcParseInteger = new Constant((value) => value ? BigInt.asIntN(64, BigInt(value)) : undefined, Type.functionType(Type.OptionalInteger, [Type.String]));
88
+ const funcDecodeInteger = new Constant((value, encoding = "i64", byteOffset) => decodeInteger(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalInteger, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger]));
89
+ const funcParseInteger = new Constant((value) => {
90
+ try {
91
+ return value ? BigInt.asIntN(64, BigInt(value)) : undefined;
92
+ }
93
+ catch { }
94
+ return undefined;
95
+ }, Type.functionType(Type.OptionalInteger, [Type.OptionalString]));
93
96
  export const constInteger = {
94
97
  Sum: funcSum,
95
98
  Min: funcMin,
@@ -1,5 +1,5 @@
1
1
  import { Constant } from "../Constant.js";
2
- export type StringEncoding = "utf8" | "ucs2" | "ucs2le";
2
+ export type StringEncoding = "utf8" | "sbcs" | "ucs2" | "ucs2le";
3
3
  export declare const isSignSymbol: (c: string) => c is "+" | "-";
4
4
  export declare const isNumericSymbol: (c: string) => boolean;
5
5
  export declare const isAlphanumericSymbol: (c: string) => boolean;
@@ -16,7 +16,7 @@ export declare const equateStrings: (value1?: string, value2?: string, ignoreCas
16
16
  export declare const containsString: (value?: string, search?: string, startPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
17
17
  export declare const startsWithString: (value?: string, search?: string, startPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
18
18
  export declare const endsWithString: (value?: string, search?: string, endPos?: number, ignoreCaseSpaceEtc?: boolean) => boolean;
19
- export declare const encodeString: (value?: string, encoding?: StringEncoding) => ArrayBuffer;
19
+ export declare const encodeString: (value: string, encoding?: StringEncoding) => ArrayBuffer;
20
20
  export declare const funcLike: Constant;
21
21
  export declare const funcUnlike: Constant;
22
22
  export declare const funcStartsWith: Constant;
@@ -143,16 +143,24 @@ export const endsWithString = (value, search, endPos, ignoreCaseSpaceEtc) => {
143
143
  return true;
144
144
  };
145
145
  export const encodeString = (value, encoding = "utf8") => {
146
- if (value == null) {
147
- return new Uint8Array(0).buffer;
148
- }
149
- if (encoding === "utf8") {
150
- return new TextEncoder().encode(value).buffer;
146
+ switch (encoding) {
147
+ case "utf8": return new TextEncoder().encode(value).buffer;
148
+ case "sbcs": break;
149
+ case "ucs2": break;
150
+ case "ucs2le": break;
151
+ default: throw new Error(`${encoding} encoding not supported`);
152
+ }
153
+ if (encoding === "sbcs") {
154
+ const dv = new Uint8Array(value.length);
155
+ for (let i = 0; i < value.length; ++i) {
156
+ dv[i] = value.charCodeAt(i);
157
+ }
158
+ return dv.buffer;
151
159
  }
152
160
  const dv = new DataView(new Uint16Array(value.length).buffer);
153
- const lessOrEqual = encoding.endsWith("le");
161
+ const littleEndian = encoding.endsWith("le");
154
162
  for (let i = 0; i < value.length; ++i) {
155
- dv.setUint16(i << 1, value.charCodeAt(i), lessOrEqual);
163
+ dv.setUint16(i << 1, value.charCodeAt(i), littleEndian);
156
164
  }
157
165
  return dv.buffer;
158
166
  };
@@ -160,18 +168,27 @@ const decodeString = (value, encoding = "utf8", byteOffset, byteLength) => {
160
168
  if (value == null) {
161
169
  return undefined;
162
170
  }
163
- if (encoding === "utf8") {
164
- return new TextDecoder().decode(new DataView(value, byteOffset, byteLength));
171
+ switch (encoding) {
172
+ case "utf8": return new TextDecoder().decode(new DataView(value, byteOffset, byteLength));
173
+ case "sbcs": break;
174
+ case "ucs2": break;
175
+ case "ucs2le": break;
176
+ default: throw new Error(`${encoding} encoding not supported`);
165
177
  }
166
- else {
167
- const dv = new DataView(value, byteOffset, byteLength);
168
- const lessOrEqual = encoding.endsWith("le");
178
+ const dv = new DataView(value, byteOffset, byteLength);
179
+ if (encoding === "sbcs") {
169
180
  let str = "";
170
- for (let i = 0; i < dv.byteLength; i += 2) {
171
- str += String.fromCharCode(dv.getUint16(i, lessOrEqual));
181
+ for (let i = 0; i < dv.byteLength; ++i) {
182
+ str += String.fromCharCode(dv.getUint8(i));
172
183
  }
173
184
  return str;
174
185
  }
186
+ const littleEndian = encoding.endsWith("le");
187
+ let str = "";
188
+ for (let i = 0; i < dv.byteLength; i += 2) {
189
+ str += String.fromCharCode(dv.getUint16(i, littleEndian));
190
+ }
191
+ return str;
175
192
  };
176
193
  const typeStringEquator = Type.functionType(Type.Boolean, [Type.String, Type.String]);
177
194
  const typeStringComparator = Type.functionType(Type.Boolean, [Type.String, Type.String, Type.OptionalInteger, Type.OptionalBoolean]);
@@ -215,7 +232,7 @@ const funcRandomString = new Constant((value) => {
215
232
  }
216
233
  return str.slice(0, length);
217
234
  }, Type.functionType(Type.String, [Type.Integer]), false);
218
- const funcDecodeString = new Constant((value, encoding = "utf8", byteOffset, byteLength) => decodeString(value, encoding, byteOffset == null ? undefined : Number(byteOffset), byteLength == null ? undefined : Number(byteLength)), Type.functionType(Type.OptionalString, [Type.Buffer, Type.OptionalString, Type.OptionalInteger, Type.OptionalInteger]));
235
+ const funcDecodeString = new Constant((value, encoding = "utf8", byteOffset, byteLength) => decodeString(value, encoding, byteOffset == null ? undefined : Number(byteOffset), byteLength == null ? undefined : Number(byteLength)), Type.functionType(Type.OptionalString, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger, Type.OptionalInteger]));
219
236
  export const constString = {
220
237
  Alphanum: funcAlphanum,
221
238
  Random: funcRandomString,
@@ -1,8 +1,8 @@
1
1
  import { Constant } from "../Constant.js";
2
- export type TimestampEncoding = "int64" | "int64le";
3
- export declare const encodeTimestamp: (value?: Date, encoding?: TimestampEncoding) => ArrayBuffer;
2
+ export type TimestampEncoding = "i64" | "i64le";
3
+ export declare const encodeTimestamp: (value: Date, encoding?: TimestampEncoding) => ArrayBuffer;
4
4
  export declare const decodeTimestamp: (value?: ArrayBuffer, encoding?: TimestampEncoding, byteOffset?: bigint) => Date | undefined;
5
- export declare const formatTimestamp: (value?: Date, template?: string) => string;
5
+ export declare const formatTimestamp: (value: Date, template?: string) => string;
6
6
  export declare const funcYear: Constant;
7
7
  export declare const funcMonth: Constant;
8
8
  export declare const funcMonthIndex: Constant;
@@ -2,17 +2,31 @@ import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  const typeTimestampPart = Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalBoolean]);
4
4
  const typeTimestampSince = Type.functionType(Type.Float, [Type.Timestamp, Type.Timestamp]);
5
- export const encodeTimestamp = (value, encoding = "int64") => {
6
- const buf = new ArrayBuffer(8);
7
- const dv = new DataView(buf);
8
- dv.setBigInt64(0, BigInt(value?.getTime() ?? 0), encoding === "int64le");
9
- return buf;
5
+ export const encodeTimestamp = (value, encoding = "i64") => {
6
+ switch (encoding) {
7
+ case "i64": break;
8
+ case "i64le": break;
9
+ default: throw new Error(`${encoding} encoding not supported`);
10
+ }
11
+ const dv = new DataView(new BigInt64Array(1).buffer);
12
+ const littleEndian = encoding.endsWith("le");
13
+ dv.setBigInt64(0, BigInt(value?.getTime() ?? 0), littleEndian);
14
+ return dv.buffer;
10
15
  };
11
- export const decodeTimestamp = (value, encoding = "int64", byteOffset) => value ? new Date(Number(new DataView(value).getBigInt64(byteOffset == null ? 0 : Number(byteOffset), encoding === "int64le"))) : undefined;
12
- export const formatTimestamp = (value, template) => {
13
- if (value == null) {
14
- return "";
16
+ export const decodeTimestamp = (value, encoding = "i64", byteOffset) => {
17
+ switch (encoding) {
18
+ case "i64": break;
19
+ case "i64le": break;
20
+ default: throw new Error(`${encoding} encoding not supported`);
15
21
  }
22
+ const offset = byteOffset == null ? 0 : Number(byteOffset);
23
+ if (!value || offset < 0 || value.byteLength < offset + 8) {
24
+ return undefined;
25
+ }
26
+ const littleEndian = encoding.endsWith("le");
27
+ return new Date(Number(new DataView(value).getBigInt64(offset, littleEndian)));
28
+ };
29
+ export const formatTimestamp = (value, template) => {
16
30
  if (!template || typeof template !== "string") {
17
31
  return value.toISOString();
18
32
  }
@@ -70,8 +84,8 @@ export const funcHoursSince = new Constant((value1, value2) => (value1?.getTime(
70
84
  export const funcMinutesSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 60000.0, typeTimestampSince);
71
85
  export const funcSecondsSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 1000.0, typeTimestampSince);
72
86
  const funcEpochTimestamp = new Constant((value, epoch = new Date(0)) => new Date(Number(value ?? 0) + epoch?.getTime()), Type.functionType(Type.Timestamp, [Type.Number, Type.OptionalTimestamp]));
73
- const funcDecodeTimestamp = new Constant((value, encoding = "int64", byteOffset) => decodeTimestamp(value, encoding, byteOffset), Type.functionType(Type.OptionalTimestamp, [Type.Buffer, Type.OptionalString, Type.OptionalInteger]));
74
- const funcParseTimestamp = new Constant((value) => parseTimestamp(value), Type.functionType(Type.OptionalTimestamp, [Type.String]));
87
+ const funcDecodeTimestamp = new Constant((value, encoding = "i64", byteOffset) => decodeTimestamp(value, encoding, byteOffset), Type.functionType(Type.OptionalTimestamp, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger]));
88
+ const funcParseTimestamp = new Constant((value) => parseTimestamp(value), Type.functionType(Type.OptionalTimestamp, [Type.OptionalString]));
75
89
  export const constTimestamp = {
76
90
  Now: funcNow,
77
91
  Epoch: funcEpochTimestamp,