platform-types 1.10.686 → 1.10.687

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.
@@ -166,10 +166,28 @@ const previewRecord = (value) => value !== null &&
166
166
  typeof value === 'object' &&
167
167
  !Array.isArray(value) &&
168
168
  (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
169
+ // UTF-8 length without TextEncoder: the contracts compile with no DOM or Node lib, so a
170
+ // runtime global here breaks every program that type-checks them. A lone surrogate
171
+ // counts 3, as TextEncoder's U+FFFD replacement does.
172
+ function previewUtf8Bytes(text) {
173
+ let bytes = 0;
174
+ for (const char of text) {
175
+ const code = char.codePointAt(0) ?? 0;
176
+ if (code < 0x80)
177
+ bytes += 1;
178
+ else if (code < 0x800)
179
+ bytes += 2;
180
+ else if (code < 0x10000)
181
+ bytes += 3;
182
+ else
183
+ bytes += 4;
184
+ }
185
+ return bytes;
186
+ }
169
187
  function previewWithinBytes(value, maximum) {
170
188
  try {
171
189
  const serialized = JSON.stringify(value);
172
- return serialized !== undefined && new TextEncoder().encode(serialized).byteLength <= maximum;
190
+ return serialized !== undefined && previewUtf8Bytes(serialized) <= maximum;
173
191
  }
174
192
  catch {
175
193
  return false;
@@ -159,10 +159,28 @@ const previewRecord = (value) => value !== null &&
159
159
  typeof value === 'object' &&
160
160
  !Array.isArray(value) &&
161
161
  (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
162
+ // UTF-8 length without TextEncoder: the contracts compile with no DOM or Node lib, so a
163
+ // runtime global here breaks every program that type-checks them. A lone surrogate
164
+ // counts 3, as TextEncoder's U+FFFD replacement does.
165
+ function previewUtf8Bytes(text) {
166
+ let bytes = 0;
167
+ for (const char of text) {
168
+ const code = char.codePointAt(0) ?? 0;
169
+ if (code < 0x80)
170
+ bytes += 1;
171
+ else if (code < 0x800)
172
+ bytes += 2;
173
+ else if (code < 0x10000)
174
+ bytes += 3;
175
+ else
176
+ bytes += 4;
177
+ }
178
+ return bytes;
179
+ }
162
180
  function previewWithinBytes(value, maximum) {
163
181
  try {
164
182
  const serialized = JSON.stringify(value);
165
- return serialized !== undefined && new TextEncoder().encode(serialized).byteLength <= maximum;
183
+ return serialized !== undefined && previewUtf8Bytes(serialized) <= maximum;
166
184
  }
167
185
  catch {
168
186
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "platform-types",
3
- "version": "1.10.686",
3
+ "version": "1.10.687",
4
4
  "description": "Shared wire and entity contracts for the platform.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",