@gjsify/node-gi 0.26.1 → 0.27.0

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.
@@ -22,8 +22,8 @@
22
22
  // Truncate a byte array at the first NUL, per gjs's ZERO_TERMINATED handling
23
23
  // (gjs/text-encoding.cpp zero_terminated_length).
24
24
  function zeroTerminated(u8) {
25
- const nul = u8.indexOf(0);
26
- return nul === -1 ? u8 : u8.subarray(0, nul);
25
+ const nul = u8.indexOf(0);
26
+ return nul === -1 ? u8 : u8.subarray(0, nul);
27
27
  }
28
28
 
29
29
  // gjs routes non-UTF-8 encodings through iconv; Node's TextDecoder (decode
@@ -32,33 +32,33 @@ function zeroTerminated(u8) {
32
32
  // Node — map the labels gjs consumers actually use onto Buffer encodings and
33
33
  // throw a clear error for the rest.
34
34
  const ENCODE_LABELS = new Map([
35
- ['latin1', 'latin1'],
36
- ['iso-8859-1', 'latin1'],
37
- ['iso8859-1', 'latin1'],
38
- ['ascii', 'ascii'],
39
- ['us-ascii', 'ascii'],
40
- ['utf-16', 'utf16le'],
41
- ['utf-16le', 'utf16le'],
42
- ['utf16le', 'utf16le'],
35
+ ['latin1', 'latin1'],
36
+ ['iso-8859-1', 'latin1'],
37
+ ['iso8859-1', 'latin1'],
38
+ ['ascii', 'ascii'],
39
+ ['us-ascii', 'ascii'],
40
+ ['utf-16', 'utf16le'],
41
+ ['utf-16le', 'utf16le'],
42
+ ['utf16le', 'utf16le'],
43
43
  ]);
44
44
 
45
45
  function isUtf8Label(encoding) {
46
- const label = String(encoding).toLowerCase();
47
- return label === 'utf-8' || label === 'utf8';
46
+ const label = String(encoding).toLowerCase();
47
+ return label === 'utf-8' || label === 'utf8';
48
48
  }
49
49
 
50
50
  // The legacy own `toString(encoding)` gjs defines on arrays returned by
51
51
  // fromString/fromGBytes/fromArray (byteArray.cpp define_legacy_tostring).
52
52
  function defineLegacyToString(u8, toString) {
53
- Object.defineProperty(u8, 'toString', {
54
- value(encoding = 'utf-8') {
55
- return toString(u8, encoding);
56
- },
57
- writable: true,
58
- enumerable: false,
59
- configurable: true,
60
- });
61
- return u8;
53
+ Object.defineProperty(u8, 'toString', {
54
+ value(encoding = 'utf-8') {
55
+ return toString(u8, encoding);
56
+ },
57
+ writable: true,
58
+ enumerable: false,
59
+ configurable: true,
60
+ });
61
+ return u8;
62
62
  }
63
63
 
64
64
  /**
@@ -67,131 +67,131 @@ function defineLegacyToString(u8, toString) {
67
67
  * @returns {{ ByteArray: any, fromArray: Function, fromGBytes: Function, fromString: Function, toGBytes: Function, toString: Function }}
68
68
  */
69
69
  export function createByteArray(requireGi) {
70
- /**
71
- * @param {Uint8Array} byteArray bytes to decode
72
- * @param {string} [encoding] a WHATWG/iconv label, default UTF-8
73
- * @returns {string} the decoded string, stopping at the first NUL byte
74
- */
75
- function toString(byteArray, encoding = 'utf-8') {
76
- // Plain Error, not TypeError — gjs raises these argument failures via
77
- // gjs_throw (Error); only the GBytes typecheck below is a TypeError.
78
- if (!(byteArray instanceof Uint8Array)) throw new Error('Argument to decode() must be a Uint8Array');
79
- const data = zeroTerminated(byteArray);
80
- if (data.length === 0) return '';
81
- // fatal:true mirrors gjs's fatal decode (invalid sequences throw).
82
- return new TextDecoder(encoding, { fatal: true }).decode(data);
83
- }
84
-
85
- /**
86
- * @param {string} string the string to encode
87
- * @param {string} [encoding] a WHATWG/iconv label, default UTF-8
88
- * @returns {Uint8Array} the encoded bytes, truncated at the first NUL byte
89
- */
90
- function fromString(string, encoding = 'utf-8') {
91
- if (typeof string !== 'string') throw new Error('Argument to fromString() must be a string');
92
- let encoded;
93
- if (isUtf8Label(encoding)) {
94
- encoded = new TextEncoder().encode(string);
95
- } else {
96
- const bufferEncoding = ENCODE_LABELS.get(String(encoding).toLowerCase());
97
- if (!bufferEncoding) throw new Error(`Unsupported encoding for fromString(): ${encoding}`);
98
- const buf = Buffer.from(string, bufferEncoding);
99
- encoded = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
100
- }
101
- const data = zeroTerminated(encoded);
102
- // Copy into a fresh plain Uint8Array (gjs returns a standalone buffer, and
103
- // Buffer.from may hand out a view into Node's shared slab).
104
- const out = new Uint8Array(data.length);
105
- out.set(data);
106
- return defineLegacyToString(out, toString);
107
- }
108
-
109
- /**
110
- * @param {any} bytes a GLib.Bytes boxed handle
111
- * @returns {Uint8Array} a copy of the bytes as a plain Uint8Array
112
- */
113
- function fromGBytes(bytes) {
114
- // The engine's boxed GBytes proxy carries `toArray()` (gi.js wrapBoxed) —
115
- // the same convenience gjs adds to GLib.Bytes.prototype. Its absence means
116
- // the value is not a GBytes, matching gjs's G_TYPE_BYTES typecheck throw.
117
- if (!bytes || typeof bytes.toArray !== 'function') {
118
- throw new TypeError('Argument to fromGBytes() must be a GLib.Bytes');
119
- }
120
- const data = bytes.toArray();
121
- // toArray() already returns a copy of the GBytes data; re-view it as a
122
- // PLAIN Uint8Array (gjs returns Uint8Array, not Node's Buffer subclass).
123
- const out = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
124
- return defineLegacyToString(out, toString);
125
- }
126
-
127
- /**
128
- * @param {Uint8Array} array the Uint8Array to convert to GLib.Bytes
129
- * @returns {any} a GLib.Bytes boxed handle
130
- */
131
- function toGBytes(array) {
132
- if (!(array instanceof Uint8Array)) throw new Error('Argument to ByteArray.toGBytes() must be a Uint8Array');
133
- const GLib = requireGi('GLib', '2.0');
134
- return new GLib.Bytes(array);
135
- }
136
-
137
- // The legacy ByteArray wrapper class, ported verbatim from
138
- // refs/gjs/modules/script/byteArray.js (backwards compatibility only).
139
- const ByteArray = class ByteArray {
140
- constructor(arg = 0) {
141
- if (arg instanceof Uint8Array) this._array = arg;
142
- else this._array = new Uint8Array(arg);
143
- return new Proxy(this, ByteArray);
70
+ /**
71
+ * @param {Uint8Array} byteArray bytes to decode
72
+ * @param {string} [encoding] a WHATWG/iconv label, default UTF-8
73
+ * @returns {string} the decoded string, stopping at the first NUL byte
74
+ */
75
+ function toString(byteArray, encoding = 'utf-8') {
76
+ // Plain Error, not TypeError — gjs raises these argument failures via
77
+ // gjs_throw (Error); only the GBytes typecheck below is a TypeError.
78
+ if (!(byteArray instanceof Uint8Array)) throw new Error('Argument to decode() must be a Uint8Array');
79
+ const data = zeroTerminated(byteArray);
80
+ if (data.length === 0) return '';
81
+ // fatal:true mirrors gjs's fatal decode (invalid sequences throw).
82
+ return new TextDecoder(encoding, { fatal: true }).decode(data);
144
83
  }
145
84
 
146
- static get(target, prop, receiver) {
147
- if (!Number.isNaN(Number.parseInt(prop))) return Reflect.get(target._array, prop);
148
- return Reflect.get(target, prop, receiver);
85
+ /**
86
+ * @param {string} string the string to encode
87
+ * @param {string} [encoding] a WHATWG/iconv label, default UTF-8
88
+ * @returns {Uint8Array} the encoded bytes, truncated at the first NUL byte
89
+ */
90
+ function fromString(string, encoding = 'utf-8') {
91
+ if (typeof string !== 'string') throw new Error('Argument to fromString() must be a string');
92
+ let encoded;
93
+ if (isUtf8Label(encoding)) {
94
+ encoded = new TextEncoder().encode(string);
95
+ } else {
96
+ const bufferEncoding = ENCODE_LABELS.get(String(encoding).toLowerCase());
97
+ if (!bufferEncoding) throw new Error(`Unsupported encoding for fromString(): ${encoding}`);
98
+ const buf = Buffer.from(string, bufferEncoding);
99
+ encoded = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
100
+ }
101
+ const data = zeroTerminated(encoded);
102
+ // Copy into a fresh plain Uint8Array (gjs returns a standalone buffer, and
103
+ // Buffer.from may hand out a view into Node's shared slab).
104
+ const out = new Uint8Array(data.length);
105
+ out.set(data);
106
+ return defineLegacyToString(out, toString);
149
107
  }
150
108
 
151
- static set(target, prop, val, receiver) {
152
- const ix = Number.parseInt(prop);
153
- if (!Number.isNaN(ix)) {
154
- if (ix >= target._array.length) {
155
- const newArray = new Uint8Array(ix + 1);
156
- newArray.set(target._array);
157
- target._array = newArray;
109
+ /**
110
+ * @param {any} bytes a GLib.Bytes boxed handle
111
+ * @returns {Uint8Array} a copy of the bytes as a plain Uint8Array
112
+ */
113
+ function fromGBytes(bytes) {
114
+ // The engine's boxed GBytes proxy carries `toArray()` (gi.js wrapBoxed) —
115
+ // the same convenience gjs adds to GLib.Bytes.prototype. Its absence means
116
+ // the value is not a GBytes, matching gjs's G_TYPE_BYTES typecheck throw.
117
+ if (!bytes || typeof bytes.toArray !== 'function') {
118
+ throw new TypeError('Argument to fromGBytes() must be a GLib.Bytes');
158
119
  }
159
- return Reflect.set(target._array, prop, val);
160
- }
161
- return Reflect.set(target, prop, val, receiver);
120
+ const data = bytes.toArray();
121
+ // toArray() already returns a copy of the GBytes data; re-view it as a
122
+ // PLAIN Uint8Array (gjs returns Uint8Array, not Node's Buffer subclass).
123
+ const out = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
124
+ return defineLegacyToString(out, toString);
162
125
  }
163
126
 
164
- get length() {
165
- return this._array.length;
127
+ /**
128
+ * @param {Uint8Array} array the Uint8Array to convert to GLib.Bytes
129
+ * @returns {any} a GLib.Bytes boxed handle
130
+ */
131
+ function toGBytes(array) {
132
+ if (!(array instanceof Uint8Array)) throw new Error('Argument to ByteArray.toGBytes() must be a Uint8Array');
133
+ const GLib = requireGi('GLib', '2.0');
134
+ return new GLib.Bytes(array);
166
135
  }
167
136
 
168
- set length(newLength) {
169
- if (newLength === this._array.length) return;
170
- if (newLength < this._array.length) {
171
- this._array = new Uint8Array(this._array.buffer, 0, newLength);
172
- return;
173
- }
174
- const newArray = new Uint8Array(newLength);
175
- newArray.set(this._array);
176
- this._array = newArray;
177
- }
137
+ // The legacy ByteArray wrapper class, ported verbatim from
138
+ // refs/gjs/modules/script/byteArray.js (backwards compatibility only).
139
+ const ByteArray = class ByteArray {
140
+ constructor(arg = 0) {
141
+ if (arg instanceof Uint8Array) this._array = arg;
142
+ else this._array = new Uint8Array(arg);
143
+ return new Proxy(this, ByteArray);
144
+ }
178
145
 
179
- toString(encoding = 'UTF-8') {
180
- return toString(this._array, encoding);
181
- }
146
+ static get(target, prop, receiver) {
147
+ if (!Number.isNaN(Number.parseInt(prop))) return Reflect.get(target._array, prop);
148
+ return Reflect.get(target, prop, receiver);
149
+ }
182
150
 
183
- toGBytes() {
184
- return toGBytes(this._array);
185
- }
186
- };
151
+ static set(target, prop, val, receiver) {
152
+ const ix = Number.parseInt(prop);
153
+ if (!Number.isNaN(ix)) {
154
+ if (ix >= target._array.length) {
155
+ const newArray = new Uint8Array(ix + 1);
156
+ newArray.set(target._array);
157
+ target._array = newArray;
158
+ }
159
+ return Reflect.set(target._array, prop, val);
160
+ }
161
+ return Reflect.set(target, prop, val, receiver);
162
+ }
187
163
 
188
- /**
189
- * @param {Iterable<number>} array an iterable to convert into a ByteArray wrapper
190
- * @returns {InstanceType<ByteArray>}
191
- */
192
- function fromArray(array) {
193
- return new ByteArray(Uint8Array.from(array));
194
- }
164
+ get length() {
165
+ return this._array.length;
166
+ }
167
+
168
+ set length(newLength) {
169
+ if (newLength === this._array.length) return;
170
+ if (newLength < this._array.length) {
171
+ this._array = new Uint8Array(this._array.buffer, 0, newLength);
172
+ return;
173
+ }
174
+ const newArray = new Uint8Array(newLength);
175
+ newArray.set(this._array);
176
+ this._array = newArray;
177
+ }
178
+
179
+ toString(encoding = 'UTF-8') {
180
+ return toString(this._array, encoding);
181
+ }
182
+
183
+ toGBytes() {
184
+ return toGBytes(this._array);
185
+ }
186
+ };
187
+
188
+ /**
189
+ * @param {Iterable<number>} array an iterable to convert into a ByteArray wrapper
190
+ * @returns {InstanceType<ByteArray>}
191
+ */
192
+ function fromArray(array) {
193
+ return new ByteArray(Uint8Array.from(array));
194
+ }
195
195
 
196
- return { ByteArray, fromArray, fromGBytes, fromString, toGBytes, toString };
196
+ return { ByteArray, fromArray, fromGBytes, fromString, toGBytes, toString };
197
197
  }