浏览代码

Phoenix: Support older Node versions in test harness

This brings Phoenix's minimum required version from 20.x down to 16.x.

ReadableStream.from() is deemed experimental, and requires Node 20.x
(or at least, something higher than 18.x). This was the only code that
made us require version 20.x.

ReadableStream and WritableStream are available from Node 16.5, but
require that they be explicitly imported.
Sam Atkins 1 年之前
父节点
当前提交
dc95f2e065
共有 1 个文件被更改,包括 18 次插入2 次删除
  1. 18 2
      packages/phoenix/test/coreutils/harness.js

+ 18 - 2
packages/phoenix/test/coreutils/harness.js

@@ -19,8 +19,9 @@
 import { Context } from "contextlink";
 import { Context } from "contextlink";
 import { SyncLinesReader } from '../../src/ansi-shell/ioutil/SyncLinesReader.js';
 import { SyncLinesReader } from '../../src/ansi-shell/ioutil/SyncLinesReader.js';
 import { CommandStdinDecorator } from '../../src/ansi-shell/pipeline/iowrappers.js';
 import { CommandStdinDecorator } from '../../src/ansi-shell/pipeline/iowrappers.js';
+import { ReadableStream, WritableStream } from 'stream/web'
 
 
-export class WritableStringStream extends WritableStream {
+class WritableStringStream extends WritableStream {
     constructor() {
     constructor() {
         super({
         super({
             write: (chunk) => {
             write: (chunk) => {
@@ -42,8 +43,23 @@ export class WritableStringStream extends WritableStream {
 
 
 // TODO: Flesh this out as needed.
 // TODO: Flesh this out as needed.
 export const MakeTestContext = (command, { positionals = [],  values = {}, stdinInputs = [], env = {} }) => {
 export const MakeTestContext = (command, { positionals = [],  values = {}, stdinInputs = [], env = {} }) => {
+    // This is a replacement to ReadableStream.from() in earlier Node versions
+    // Sourece: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#convert_an_iterator_or_async_iterator_to_a_stream
+    function iteratorToStream(iterator) {
+        return new ReadableStream({
+            async pull(controller) {
+                const { value, done } = await iterator.next();
 
 
-    let in_ = ReadableStream.from(stdinInputs).getReader();
+                if (done) {
+                    controller.close();
+                } else {
+                    controller.enqueue(value);
+                }
+            },
+        });
+    }
+
+    let in_ = iteratorToStream(stdinInputs.values()).getReader();
     if (command.input?.syncLines) {
     if (command.input?.syncLines) {
         in_ = new SyncLinesReader({ delegate: in_ });
         in_ = new SyncLinesReader({ delegate: in_ });
     }
     }