text-decoder-stream.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Polyfill for TextEncoderStream and TextDecoderStream
  15. // Modified by Sukka (https://skk.moe) to increase compatibility and performance with Bun.
  16. export class PolyfillTextDecoderStream extends TransformStream<Uint8Array, string> {
  17. readonly encoding: string;
  18. readonly fatal: boolean;
  19. readonly ignoreBOM: boolean;
  20. constructor(
  21. encoding: Encoding = 'utf-8',
  22. {
  23. fatal = false,
  24. ignoreBOM = false,
  25. }: ConstructorParameters<typeof TextDecoder>[1] = {},
  26. ) {
  27. const decoder = new TextDecoder(encoding, { fatal, ignoreBOM });
  28. super({
  29. transform(chunk: Uint8Array, controller: TransformStreamDefaultController<string>) {
  30. const decoded = decoder.decode(chunk);
  31. if (decoded.length > 0) {
  32. controller.enqueue(decoded);
  33. }
  34. },
  35. flush(controller: TransformStreamDefaultController<string>) {
  36. // If {fatal: false} is in options (the default), then the final call to
  37. // decode() can produce extra output (usually the unicode replacement
  38. // character 0xFFFD). When fatal is true, this call is just used for its
  39. // side-effect of throwing a TypeError exception if the input is
  40. // incomplete.
  41. const output = decoder.decode();
  42. if (output.length > 0) {
  43. controller.enqueue(output);
  44. }
  45. }
  46. });
  47. this.encoding = encoding;
  48. this.fatal = fatal;
  49. this.ignoreBOM = ignoreBOM;
  50. }
  51. }