@hazae41/webpack 1.0.0 → 1.0.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.
package/README.md CHANGED
@@ -8,11 +8,6 @@ npm install @hazae41/webpack
8
8
 
9
9
  [**📦 NPM**](https://www.npmjs.com/package/@hazae41/webpack)
10
10
 
11
- ## Features
12
-
13
- - WebAssembly-compatible
14
- - Fails on self-reference
15
-
16
11
  ## Usage
17
12
 
18
13
  ```tsx
@@ -23,7 +18,7 @@ type Packable =
23
18
  | string
24
19
  | bigint
25
20
  | Array<Packable>
26
- | { [k: string]: Packable }
21
+ | Record<string, Packable>
27
22
  ```
28
23
 
29
24
  ```tsx
@@ -1,18 +1,15 @@
1
1
  import type { Cursor } from "@hazae41/cursor";
2
- export type Packable = null | number | Uint8Array | string | bigint | Array<Packable> | {
3
- [k: string]: Packable;
4
- };
5
2
  export declare class Packed {
6
- readonly value: Packable;
7
- constructor(value: Packable);
3
+ readonly data: unknown;
4
+ constructor(data: unknown);
8
5
  size(): number;
9
6
  write(cursor: Cursor): void;
10
7
  encode(): Uint8Array<ArrayBuffer>;
11
8
  }
12
9
  export declare namespace Packed {
13
- function encode(value: Packable): Uint8Array<ArrayBuffer>;
14
- function decode(bytes: Uint8Array): Packable;
15
- function read(cursor: Cursor): Packable;
16
- function size(value: Packable): number;
17
- function write(value: Packable, cursor: Cursor): void;
10
+ function encode(data: unknown): Uint8Array<ArrayBuffer>;
11
+ function decode(blob: Uint8Array): unknown;
12
+ function read(cursor: Cursor): unknown;
13
+ function size(data: unknown): number;
14
+ function write(data: unknown, cursor: Cursor): void;
18
15
  }
@@ -1,27 +1,27 @@
1
1
  // deno-lint-ignore-file no-namespace
2
2
  import { Readable, Writable } from "@hazae41/binary";
3
3
  export class Packed {
4
- value;
5
- constructor(value) {
6
- this.value = value;
4
+ data;
5
+ constructor(data) {
6
+ this.data = data;
7
7
  }
8
8
  size() {
9
- return Packed.size(this.value);
9
+ return Packed.size(this.data);
10
10
  }
11
11
  write(cursor) {
12
- return Packed.write(this.value, cursor);
12
+ return Packed.write(this.data, cursor);
13
13
  }
14
14
  encode() {
15
- return Packed.encode(this.value);
15
+ return Packed.encode(this.data);
16
16
  }
17
17
  }
18
18
  (function (Packed) {
19
- function encode(value) {
20
- return Writable.writeToBytes(new Packed(value));
19
+ function encode(data) {
20
+ return Writable.writeToBytes(new Packed(data));
21
21
  }
22
22
  Packed.encode = encode;
23
- function decode(bytes) {
24
- return Readable.readFromBytes(Packed, bytes);
23
+ function decode(blob) {
24
+ return Readable.readFromBytes(Packed, blob);
25
25
  }
26
26
  Packed.decode = decode;
27
27
  function read(cursor) {
@@ -32,20 +32,20 @@ export class Packed {
32
32
  return cursor.readFloat64();
33
33
  if (type === 2) {
34
34
  const size = cursor.readUint32();
35
- const data = cursor.read(size);
36
- return data;
35
+ const blob = cursor.read(size);
36
+ return blob;
37
37
  }
38
38
  if (type === 3) {
39
39
  const size = cursor.readUint32();
40
- const data = cursor.read(size);
41
- const text = new TextDecoder().decode(data);
40
+ const blob = cursor.read(size);
41
+ const text = new TextDecoder().decode(blob);
42
42
  return text;
43
43
  }
44
44
  if (type === 4) {
45
45
  const sign = cursor.readUint8();
46
46
  const size = cursor.readUint32();
47
- const data = cursor.read(size);
48
- const absolute = BigInt("0x" + data.toHex());
47
+ const blob = cursor.read(size);
48
+ const absolute = BigInt("0x" + blob.toHex());
49
49
  const relative = sign ? -absolute : absolute;
50
50
  return relative;
51
51
  }
@@ -61,8 +61,8 @@ export class Packed {
61
61
  const record = {};
62
62
  for (let i = 0; i < length; i++) {
63
63
  const subsize = cursor.readUint32();
64
- const subdata = cursor.read(subsize);
65
- const subtext = new TextDecoder().decode(subdata);
64
+ const subblob = cursor.read(subsize);
65
+ const subtext = new TextDecoder().decode(subblob);
66
66
  record[subtext] = read(cursor);
67
67
  }
68
68
  return record;
@@ -70,97 +70,98 @@ export class Packed {
70
70
  throw new Error("Non-decodable type");
71
71
  }
72
72
  Packed.read = read;
73
- function size(value) {
74
- if (value == null)
73
+ function size(data) {
74
+ if (data == null)
75
75
  return 1;
76
- if (typeof value === "number")
76
+ if (typeof data === "number")
77
77
  return 1 + 8;
78
- if (value instanceof Uint8Array)
79
- return 1 + 4 + value.length;
80
- if (typeof value === "string")
81
- return 1 + 4 + new TextEncoder().encode(value).length;
82
- if (typeof value === "bigint") {
83
- const absolute = value < 0n ? -value : value;
78
+ if (data instanceof Uint8Array)
79
+ return 1 + 4 + data.length;
80
+ if (typeof data === "string")
81
+ return 1 + 4 + new TextEncoder().encode(data).length;
82
+ if (typeof data === "bigint") {
83
+ const absolute = data < 0n ? -data : data;
84
84
  const text = absolute.toString(16);
85
- const data = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
86
- return 1 + 1 + 4 + data.length;
85
+ const blob = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
86
+ return 1 + 1 + 4 + blob.length;
87
87
  }
88
- if (Array.isArray(value)) {
88
+ if (Array.isArray(data)) {
89
89
  let total = 0;
90
90
  total += 1;
91
91
  total += 4;
92
- for (const subvalue of value)
93
- total += size(subvalue);
92
+ for (const subdata of data)
93
+ total += size(subdata);
94
94
  return total;
95
95
  }
96
- if (typeof value === "object") {
96
+ if (typeof data === "object") {
97
+ const entries = Object.entries(data);
97
98
  let total = 0;
98
99
  total += 1;
99
100
  total += 4;
100
- for (const subtext in value) {
101
- const subdata = new TextEncoder().encode(subtext);
101
+ for (const [subtext, subdata] of entries) {
102
+ const subblob = new TextEncoder().encode(subtext);
102
103
  total += 4;
103
- total += subdata.length;
104
- total += size(value[subtext]);
104
+ total += subblob.length;
105
+ total += size(subdata);
105
106
  }
106
107
  return total;
107
108
  }
108
- throw new Error("Non-encodable value");
109
+ throw new Error("Non-encodable data");
109
110
  }
110
111
  Packed.size = size;
111
- function write(value, cursor) {
112
- if (value == null) {
112
+ function write(data, cursor) {
113
+ if (data == null) {
113
114
  cursor.writeUint8(0);
114
115
  return;
115
116
  }
116
- if (typeof value === "number") {
117
+ if (typeof data === "number") {
117
118
  cursor.writeUint8(1);
118
- cursor.writeFloat64(value);
119
+ cursor.writeFloat64(data);
119
120
  return;
120
121
  }
121
- if (value instanceof Uint8Array) {
122
+ if (data instanceof Uint8Array) {
122
123
  cursor.writeUint8(2);
123
- cursor.writeUint32(value.length);
124
- cursor.write(value);
124
+ cursor.writeUint32(data.length);
125
+ cursor.write(data);
125
126
  return;
126
127
  }
127
- if (typeof value === "string") {
128
+ if (typeof data === "string") {
128
129
  cursor.writeUint8(3);
129
- const data = new TextEncoder().encode(value);
130
- cursor.writeUint32(data.length);
131
- cursor.write(data);
130
+ const blob = new TextEncoder().encode(data);
131
+ cursor.writeUint32(blob.length);
132
+ cursor.write(blob);
132
133
  return;
133
134
  }
134
- if (typeof value === "bigint") {
135
+ if (typeof data === "bigint") {
135
136
  cursor.writeUint8(4);
136
- const [negative, absolute] = value < 0n ? [1, -value] : [0, value];
137
+ const [negative, absolute] = data < 0n ? [1, -data] : [0, data];
137
138
  const text = absolute.toString(16);
138
- const data = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
139
+ const blob = Uint8Array.fromHex(text.length % 2 === 1 ? "0" + text : text);
139
140
  cursor.writeUint8(negative);
140
- cursor.writeUint32(data.length);
141
- cursor.write(data);
141
+ cursor.writeUint32(blob.length);
142
+ cursor.write(blob);
142
143
  return;
143
144
  }
144
- if (Array.isArray(value)) {
145
+ if (Array.isArray(data)) {
145
146
  cursor.writeUint8(5);
146
- cursor.writeUint32(value.length);
147
- for (const subvalue of value)
148
- write(subvalue, cursor);
147
+ cursor.writeUint32(data.length);
148
+ for (const subdata of data)
149
+ write(subdata, cursor);
149
150
  return;
150
151
  }
151
- if (typeof value === "object") {
152
- const keys = Object.keys(value);
152
+ if (typeof data === "object") {
153
+ const entries = Object.entries(data);
153
154
  cursor.writeUint8(6);
154
- cursor.writeUint32(keys.length);
155
- for (const subtext of keys) {
156
- const subdata = new TextEncoder().encode(subtext);
157
- cursor.writeUint32(subdata.length);
158
- cursor.write(subdata);
159
- write(value[subtext], cursor);
155
+ cursor.writeUint32(entries.length);
156
+ for (const [subtext, subdata] of entries) {
157
+ const subblob = new TextEncoder().encode(subtext);
158
+ cursor.writeUint32(subblob.length);
159
+ cursor.write(subblob);
160
+ write(subdata, cursor);
160
161
  }
161
162
  return;
162
163
  }
163
- throw new Error("Non-encodable value");
164
+ throw new Error("Non-encodable data");
164
165
  }
165
166
  Packed.write = write;
166
167
  })(Packed || (Packed = {}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@hazae41/webpack",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "Wire format for JavaScript primitives",
6
6
  "homepage": "https://github.com/hazae41/webpack",
7
7
  "repository": "github:hazae41/webpack",