@bare-ts/lib 0.3.0 → 0.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.
Files changed (56) hide show
  1. package/LICENSE +21 -201
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +10 -12
  4. package/dist/codec/float-array.d.ts +9 -9
  5. package/dist/codec/float-array.js +51 -43
  6. package/dist/codec/i16-array.d.ts +5 -5
  7. package/dist/codec/i16-array.js +34 -26
  8. package/dist/codec/i32-array.d.ts +5 -5
  9. package/dist/codec/i32-array.js +34 -26
  10. package/dist/codec/i64-array.d.ts +5 -5
  11. package/dist/codec/i64-array.js +34 -26
  12. package/dist/codec/i8-array.d.ts +1 -1
  13. package/dist/codec/i8-array.js +12 -14
  14. package/dist/codec/primitive.d.ts +3 -1
  15. package/dist/codec/primitive.js +225 -147
  16. package/dist/codec/string.d.ts +1 -1
  17. package/dist/codec/string.js +45 -31
  18. package/dist/codec/u16-array.d.ts +5 -5
  19. package/dist/codec/u16-array.js +34 -26
  20. package/dist/codec/u32-array.d.ts +5 -5
  21. package/dist/codec/u32-array.js +34 -26
  22. package/dist/codec/u64-array.d.ts +5 -5
  23. package/dist/codec/u64-array.js +34 -26
  24. package/dist/codec/u8-array.d.ts +7 -1
  25. package/dist/codec/u8-array.js +22 -17
  26. package/dist/codec/u8-clamped-array.d.ts +1 -1
  27. package/dist/codec/u8-clamped-array.js +13 -15
  28. package/dist/core/bare-error.d.ts +4 -1
  29. package/dist/core/bare-error.js +4 -8
  30. package/dist/core/byte-cursor.d.ts +22 -28
  31. package/dist/core/byte-cursor.js +33 -29
  32. package/dist/core/config.d.ts +3 -7
  33. package/dist/core/config.js +14 -20
  34. package/dist/env/dev.d.ts +1 -0
  35. package/dist/env/dev.development.d.ts +1 -0
  36. package/dist/env/dev.development.js +2 -0
  37. package/dist/env/dev.js +2 -0
  38. package/dist/env/dev.node.d.ts +1 -0
  39. package/dist/env/dev.node.js +2 -0
  40. package/dist/index.cjs +504 -286
  41. package/dist/index.d.cts +16 -0
  42. package/dist/index.d.ts +16 -2
  43. package/dist/index.js +17 -4
  44. package/dist/util/assert.d.ts +6 -8
  45. package/dist/util/assert.js +10 -13
  46. package/dist/util/constants.d.ts +4 -7
  47. package/dist/util/constants.js +11 -30
  48. package/dist/util/validator.d.ts +1 -1
  49. package/dist/util/validator.js +12 -24
  50. package/package.json +31 -24
  51. package/dist/codec/index.d.ts +0 -13
  52. package/dist/codec/index.js +0 -15
  53. package/dist/core/index.d.ts +0 -3
  54. package/dist/core/index.js +0 -5
  55. package/dist/util/util.d.ts +0 -1
  56. package/dist/util/util.js +0 -6
@@ -1,42 +1,46 @@
1
- // src/core/byte-cursor.ts
1
+ "use strict";
2
+ import { DEV, assert } from "../util/assert.js";
2
3
  import { TOO_LARGE_BUFFER } from "../util/constants.js";
4
+ import { isU32 } from "../util/validator.js";
3
5
  import { BareError } from "./bare-error.js";
4
- var ByteCursor = class {
6
+ export class ByteCursor {
7
+ /**
8
+ * @throws {BareError} Buffer exceeds `config.maxBufferLength`
9
+ */
5
10
  constructor(bytes, config) {
11
+ /**
12
+ * Read and write Offset in {@link view} and {@link bytes}
13
+ */
14
+ this.offset = 0;
6
15
  if (bytes.length > config.maxBufferLength) {
7
16
  throw new BareError(0, TOO_LARGE_BUFFER);
8
17
  }
9
18
  this.bytes = bytes;
10
19
  this.config = config;
11
- this.offset = 0;
12
20
  this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
13
21
  }
14
- check(min) {
15
- if (this.offset + min > this.bytes.length) {
16
- throw new BareError(this.offset, "missing bytes");
17
- }
22
+ }
23
+ export function check(bc, min) {
24
+ if (DEV) {
25
+ assert(isU32(min));
18
26
  }
19
- reserve(min) {
20
- const minLen = this.offset + min | 0;
21
- if (minLen > this.bytes.length) {
22
- if (minLen > this.config.maxBufferLength) {
23
- throw new BareError(0, TOO_LARGE_BUFFER);
24
- }
25
- const newLen = Math.min(minLen << 1, this.config.maxBufferLength);
26
- const newBytes = new Uint8Array(newLen);
27
- newBytes.set(this.bytes);
28
- this.bytes = newBytes;
29
- this.view = new DataView(newBytes.buffer);
30
- }
27
+ if (bc.offset + min > bc.bytes.length) {
28
+ throw new BareError(bc.offset, "missing bytes");
31
29
  }
32
- read(len) {
33
- this.check(len);
34
- const offset = this.offset;
35
- this.offset += len;
36
- return this.bytes.subarray(offset, offset + len);
30
+ }
31
+ export function reserve(bc, min) {
32
+ if (DEV) {
33
+ assert(isU32(min));
34
+ }
35
+ const minLen = bc.offset + min | 0;
36
+ if (minLen > bc.bytes.length) {
37
+ if (minLen > bc.config.maxBufferLength) {
38
+ throw new BareError(0, TOO_LARGE_BUFFER);
39
+ }
40
+ const newLen = Math.min(minLen << 1, bc.config.maxBufferLength);
41
+ const newBytes = new Uint8Array(newLen);
42
+ newBytes.set(bc.bytes);
43
+ bc.bytes = newBytes;
44
+ bc.view = new DataView(newBytes.buffer);
37
45
  }
38
- };
39
- export {
40
- ByteCursor
41
- };
42
- //# sourceMappingURL=byte-cursor.js.map
46
+ }
@@ -1,12 +1,8 @@
1
- export interface Config {
1
+ export type Config = {
2
2
  readonly initialBufferLength: number;
3
3
  readonly maxBufferLength: number;
4
- readonly textDecoderThreshold: number;
5
- readonly textEncoderThreshold: number;
6
- }
7
- export declare function Config({ initialBufferLength, maxBufferLength, textDecoderThreshold, textEncoderThreshold, }: {
4
+ };
5
+ export declare function Config({ initialBufferLength, maxBufferLength, }: {
8
6
  initialBufferLength?: number | undefined;
9
7
  maxBufferLength?: number | undefined;
10
- textDecoderThreshold?: number | undefined;
11
- textEncoderThreshold?: number | undefined;
12
8
  }): Config;
@@ -1,27 +1,21 @@
1
- // src/core/config.ts
2
- import { assert } from "../util/assert.js";
1
+ "use strict";
2
+ import { DEV, assert } from "../util/assert.js";
3
3
  import { TOO_LARGE_NUMBER } from "../util/constants.js";
4
4
  import { isU32 } from "../util/validator.js";
5
- function Config({
5
+ export function Config({
6
6
  initialBufferLength = 1024,
7
- maxBufferLength = 1024 * 1024 * 32,
8
- textDecoderThreshold = 256,
9
- textEncoderThreshold = 256
7
+ maxBufferLength = 1024 * 1024 * 32
10
8
  }) {
11
- const config = {
9
+ if (DEV) {
10
+ assert(isU32(initialBufferLength), TOO_LARGE_NUMBER);
11
+ assert(isU32(maxBufferLength), TOO_LARGE_NUMBER);
12
+ assert(
13
+ initialBufferLength <= maxBufferLength,
14
+ "initialBufferLength must be lower than or equal to maxBufferLength"
15
+ );
16
+ }
17
+ return {
12
18
  initialBufferLength,
13
- maxBufferLength,
14
- textDecoderThreshold,
15
- textEncoderThreshold
19
+ maxBufferLength
16
20
  };
17
- assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
18
- assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
19
- assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
20
- assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
21
- assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
22
- return config;
23
21
  }
24
- export {
25
- Config
26
- };
27
- //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ export declare const DEV = false;
@@ -0,0 +1 @@
1
+ export declare const DEV = true;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ export const DEV = true;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ export const DEV = false;
@@ -0,0 +1 @@
1
+ export declare const DEV: boolean;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ export const DEV = process.env.NODE_ENV === "development";