@opencode/codemode 0.0.0-dev-1257 → 0.0.0-dev-1263

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.
@@ -85,7 +85,13 @@ const builtinSpecs = () => [
85
85
  { name: "HeadersObj", ctor: HeadersObj, fields: ["headers"], obj: true, constants: { tag: "Headers" } },
86
86
  { name: "URLObj", ctor: URLObj, fields: ["searchParams", "url"], obj: true, constants: { tag: "URL" } },
87
87
  { name: "Bytes", ctor: Bytes, fields: ["bytes"], obj: true, constants: { tag: "Uint8Array" } },
88
- { name: "TextDecoderObj", ctor: TextDecoderObj, fields: ["decoder"], obj: true, constants: { tag: "Object" } },
88
+ {
89
+ name: "TextDecoderObj",
90
+ ctor: TextDecoderObj,
91
+ fields: ["decoder", "ignoreBOM", "queued", "seen"],
92
+ obj: true,
93
+ constants: { tag: "Object" },
94
+ },
89
95
  {
90
96
  name: "AsyncFromSyncIterator",
91
97
  ctor: AsyncFromSyncIterator,
@@ -3,9 +3,12 @@ import type { Interpreter } from "../runtime/interpreter.js";
3
3
  export declare const finishBytes: (bytes: Uint8Array, into: Obj, offset: number) => Value;
4
4
  export declare const uint8ArrayGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
5
5
  export declare const textEncoderGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
6
- /** A `TextDecoder` holding the host decoder its label and options configured. */
7
6
  export declare class TextDecoderObj extends Obj {
8
7
  readonly decoder: TextDecoder;
9
- constructor(proto: Obj, decoder: TextDecoder);
8
+ readonly ignoreBOM: boolean;
9
+ queued: Uint8Array;
10
+ seen: boolean;
11
+ constructor(proto: Obj, decoder: TextDecoder, ignoreBOM: boolean);
12
+ decode(bytes: Uint8Array, stream: boolean): string;
10
13
  }
11
14
  export declare const textDecoderGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -170,12 +170,78 @@ export const textEncoderGlobal = (ctx) => {
170
170
  construct: (_, newTarget) => new Obj(prototypeFrom(newTarget, proto)),
171
171
  });
172
172
  };
173
- /** A `TextDecoder` holding the host decoder its label and options configured. */
173
+ // Replacement mode only needs to identify an incomplete suffix (at most three bytes).
174
+ // Fatal mode also needs the queue position after the first error, which the host doesn't expose.
175
+ const streamBoundary = (bytes, fatal) => {
176
+ let next = fatal ? 0 : Math.max(0, bytes.length - 3);
177
+ while (next < bytes.length) {
178
+ const start = next;
179
+ const lead = bytes[next++];
180
+ if (lead < 0x80)
181
+ continue;
182
+ if (lead < 0xc2 || lead > 0xf4) {
183
+ if (fatal)
184
+ return { end: next, error: true };
185
+ continue;
186
+ }
187
+ const width = lead < 0xe0 ? 2 : lead < 0xf0 ? 3 : 4;
188
+ let lower = lead === 0xe0 ? 0xa0 : lead === 0xf0 ? 0x90 : 0x80;
189
+ let upper = lead === 0xed ? 0x9f : lead === 0xf4 ? 0x8f : 0xbf;
190
+ while (next < start + width) {
191
+ if (next === bytes.length)
192
+ return { end: start, error: false };
193
+ const byte = bytes[next];
194
+ // A bad continuation is reprocessed as a leading byte, even after a fatal error.
195
+ if (byte < lower || byte > upper) {
196
+ if (fatal)
197
+ return { end: next, error: true };
198
+ break;
199
+ }
200
+ next++;
201
+ lower = 0x80;
202
+ upper = 0xbf;
203
+ }
204
+ }
205
+ return { end: next, error: false };
206
+ };
207
+ const empty = new Uint8Array();
174
208
  export class TextDecoderObj extends Obj {
175
209
  decoder;
176
- constructor(proto, decoder) {
210
+ ignoreBOM;
211
+ queued = empty;
212
+ seen = false;
213
+ constructor(proto, decoder, ignoreBOM) {
177
214
  super(proto);
178
215
  this.decoder = decoder;
216
+ this.ignoreBOM = ignoreBOM;
217
+ }
218
+ decode(bytes, stream) {
219
+ if (this.queued.length && bytes.length) {
220
+ checkArrayLength(this.queued.length + bytes.length);
221
+ const joined = new Uint8Array(this.queued.length + bytes.length);
222
+ joined.set(this.queued);
223
+ joined.set(bytes, this.queued.length);
224
+ bytes = joined;
225
+ }
226
+ if (!bytes.length)
227
+ bytes = this.queued;
228
+ const { end, error } = stream ? streamBoundary(bytes, this.decoder.fatal) : { end: bytes.length, error: false };
229
+ this.queued = end < bytes.length ? bytes.slice(end) : empty;
230
+ if (error)
231
+ throw typeError("TextDecoder.decode: the input is not valid utf-8.");
232
+ const stripBOM = !this.ignoreBOM && !this.seen;
233
+ if (!stream)
234
+ this.seen = false;
235
+ try {
236
+ // Keep the host decoder stateless: snapshots own the pending bytes and BOM decision.
237
+ const text = this.decoder.decode(end === bytes.length ? bytes : bytes.subarray(0, end));
238
+ if (stream && text.length)
239
+ this.seen = true;
240
+ return stripBOM && text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
241
+ }
242
+ catch {
243
+ throw typeError("TextDecoder.decode: the input is not valid utf-8.");
244
+ }
179
245
  }
180
246
  }
181
247
  // The WHATWG labels for UTF-8, the only encoding CodeMode decodes.
@@ -186,24 +252,22 @@ export const textDecoderGlobal = (ctx) => {
186
252
  const self = (thisValue, name) => receiver(TextDecoderObj, thisValue, `TextDecoder.prototype.${name}`);
187
253
  defineAccessor(proto, "encoding", (thisValue) => self(thisValue, "encoding").decoder.encoding);
188
254
  defineAccessor(proto, "fatal", (thisValue) => self(thisValue, "fatal").decoder.fatal);
189
- defineAccessor(proto, "ignoreBOM", (thisValue) => self(thisValue, "ignoreBOM").decoder.ignoreBOM);
255
+ defineAccessor(proto, "ignoreBOM", (thisValue) => self(thisValue, "ignoreBOM").ignoreBOM);
190
256
  methods(builtins, proto, [
191
257
  [
192
258
  "decode",
193
259
  0,
194
260
  (thisValue, args) => {
195
- const decoder = self(thisValue, "decode").decoder;
196
- if (args[0] === undefined)
197
- return "";
198
- if (!(args[0] instanceof Bytes)) {
261
+ const decoder = self(thisValue, "decode");
262
+ if (args[0] !== undefined && !(args[0] instanceof Bytes)) {
199
263
  throw typeError(`TextDecoder.decode expects a Uint8Array, received ${describeValue(args[0])}.`);
200
264
  }
201
- try {
202
- return decoder.decode(args[0].bytes);
203
- }
204
- catch {
205
- throw typeError(`TextDecoder.decode: the input is not valid ${decoder.encoding}.`);
265
+ const options = args[1];
266
+ if (options !== undefined && typeof options !== "object") {
267
+ throw typeError("TextDecoder.decode expects an options object.");
206
268
  }
269
+ const stream = options instanceof Obj && Boolean(get(options, "stream"));
270
+ return decoder.decode(args[0]?.bytes ?? empty, stream);
207
271
  },
208
272
  ],
209
273
  ]);
@@ -216,7 +280,7 @@ export const textDecoderGlobal = (ctx) => {
216
280
  throw rangeError(`The "${label}" encoding is not supported; only UTF-8 is.`);
217
281
  const options = args[1] instanceof Obj ? args[1] : undefined;
218
282
  const flag = (name) => options !== undefined && Boolean(get(options, name));
219
- return new TextDecoderObj(prototypeFrom(newTarget, proto), new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: flag("ignoreBOM") }));
283
+ return new TextDecoderObj(prototypeFrom(newTarget, proto), new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: true }), flag("ignoreBOM"));
220
284
  },
221
285
  });
222
286
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode/codemode",
4
- "version": "0.0.0-dev-1257",
4
+ "version": "0.0.0-dev-1263",
5
5
  "description": "Effect-native confined code execution over schema-described tools",
6
6
  "type": "module",
7
7
  "license": "MIT",