Skip to content

Commit

Permalink
Merge pull request #3 from humanloop/fern-patch-readable-stream
Browse files Browse the repository at this point in the history
Add "readable-stream" for use with web-based streaming clients
  • Loading branch information
fern-support authored Aug 27, 2024
2 parents 94a70da + 73aede9 commit 2a956c9
Show file tree
Hide file tree
Showing 9 changed files with 887 additions and 423 deletions.
6 changes: 6 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# Specify files that shouldn't be modified by Fern
src/core/form-data-utils/FormDataWrapper.ts
tests/unit/fetcher/stream-wrappers/chooseStreamWrapper.test.ts
tests/unit/fetcher/stream-wrappers/Node18UniversalStreamWrapper.test.ts
tests/unit/fetcher/stream-wrappers/NodePre18StreamWrapper.test.ts
tests/unit/form-data-utils/formDataWrapper.test.ts
tests/unit/fetcher/webpack.test.ts
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "humanloop",
"version": "0.8.0-beta7",
"version": "0.8.0-beta8",
"private": false,
"repository": "https://github.com/humanloop/humanloop-node-beta",
"main": "./index.js",
Expand All @@ -12,25 +12,29 @@
"test": "jest"
},
"dependencies": {
"url-join": "4.0.1",
"form-data": "^4.0.0",
"form-data-encoder": "^4.0.2",
"formdata-node": "^6.0.3",
"node-fetch": "2.7.0",
"qs": "6.11.2",
"form-data-encoder": "^4.0.2"
"readable-stream": "^4.5.2",
"url-join": "4.0.1"
},
"devDependencies": {
"@types/url-join": "4.0.1",
"@types/qs": "6.9.8",
"@types/jest": "29.5.5",
"@types/node": "17.0.33",
"@types/node-fetch": "2.6.9",
"@types/qs": "6.9.8",
"@types/readable-stream": "^4.0.15",
"@types/url-join": "4.0.1",
"fetch-mock-jest": "^1.5.1",
"jest": "29.7.0",
"@types/jest": "29.5.5",
"ts-jest": "29.1.1",
"jest-environment-jsdom": "29.7.0",
"@types/node": "17.0.33",
"prettier": "2.7.1",
"typescript": "4.6.4"
"ts-jest": "29.1.1",
"ts-loader": "^9.5.1",
"typescript": "4.6.4",
"webpack": "^5.94.0"
},
"browser": {
"fs": false,
Expand Down
6 changes: 3 additions & 3 deletions src/core/form-data-utils/FormDataWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Node18FormData implements CrossPlatformFormData {
fileName = value.name;
}

if (value instanceof (await import("stream")).Readable) {
if (value instanceof (await import("readable-stream")).Readable) {
this.fd?.append(key, {
type: undefined,
name: fileName,
Expand All @@ -77,7 +77,7 @@ export class Node18FormData implements CrossPlatformFormData {
public async getRequest(): Promise<FormDataRequest<unknown>> {
const encoder = new (await import("form-data-encoder")).FormDataEncoder(this.fd as any);
return {
body: await (await import("stream")).Readable.from(encoder),
body: await (await import("readable-stream")).Readable.from(encoder),
headers: encoder.headers,
duplex: "half",
};
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Node16FormData implements CrossPlatformFormData {
}

let bufferedValue;
if (!(value instanceof (await import("stream")).Readable)) {
if (!(value instanceof (await import("readable-stream")).Readable)) {
bufferedValue = Buffer.from(await (value as any).arrayBuffer());
} else {
bufferedValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("Node18UniversalStreamWrapper", () => {
},
});
const stream = new Node18UniversalStreamWrapper(rawStream);
const dest = new (await import("stream")).Writable({
const dest = new (await import("readable-stream")).Writable({
write(chunk, encoding, callback) {
expect(chunk.toString()).toEqual("test");
callback();
Expand Down
26 changes: 13 additions & 13 deletions tests/unit/fetcher/stream-wrappers/NodePre18StreamWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NodePre18StreamWrapper } from "../../../../src/core/fetcher/stream-wrap

describe("NodePre18StreamWrapper", () => {
it("should set encoding to utf-8", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const setEncodingSpy = jest.spyOn(stream, "setEncoding");

Expand All @@ -12,7 +12,7 @@ describe("NodePre18StreamWrapper", () => {
});

it("should register an event listener for readable", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const onSpy = jest.spyOn(stream, "on");

Expand All @@ -22,7 +22,7 @@ describe("NodePre18StreamWrapper", () => {
});

it("should remove an event listener for data", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const offSpy = jest.spyOn(stream, "off");

Expand All @@ -34,9 +34,9 @@ describe("NodePre18StreamWrapper", () => {
});

it("should write to dest when calling pipe to node writable stream", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const dest = new (await import("stream")).Writable({
const dest = new (await import("readable-stream")).Writable({
write(chunk, encoding, callback) {
expect(chunk.toString()).toEqual("test");
callback();
Expand All @@ -47,10 +47,10 @@ describe("NodePre18StreamWrapper", () => {
});

it("should write nothing when calling pipe and unpipe", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const buffer: Uint8Array[] = [];
const dest = new (await import("stream")).Writable({
const dest = new (await import("readable-stream")).Writable({
write(chunk, encoding, callback) {
buffer.push(chunk);
callback();
Expand All @@ -63,7 +63,7 @@ describe("NodePre18StreamWrapper", () => {
});

it("should destroy the stream", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const destroySpy = jest.spyOn(stream, "destroy");

Expand All @@ -73,7 +73,7 @@ describe("NodePre18StreamWrapper", () => {
});

it("should pause the stream and resume", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);
const pauseSpy = jest.spyOn(stream, "pause");

Expand All @@ -86,15 +86,15 @@ describe("NodePre18StreamWrapper", () => {
});

it("should read the stream", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);

expect(await stream.read()).toEqual("test");
expect(await stream.read()).toEqual("test");
});

it("should read the stream as text", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
const stream = new NodePre18StreamWrapper(rawStream);

const data = await stream.text();
Expand All @@ -103,7 +103,7 @@ describe("NodePre18StreamWrapper", () => {
});

it("should read the stream as json", async () => {
const rawStream = (await import("stream")).Readable.from([JSON.stringify({ test: "test" })]);
const rawStream = (await import("readable-stream")).Readable.from([JSON.stringify({ test: "test" })]);
const stream = new NodePre18StreamWrapper(rawStream);

const data = await stream.json();
Expand All @@ -112,7 +112,7 @@ describe("NodePre18StreamWrapper", () => {
});

it("should allow use with async iteratable stream", async () => {
const rawStream = (await import("stream")).Readable.from(["test", "test"]);
const rawStream = (await import("readable-stream")).Readable.from(["test", "test"]);
let data = "";
const stream = new NodePre18StreamWrapper(rawStream);
for await (const chunk of stream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("chooseStreamWrapper", () => {
});

it('should return a NodePre18StreamWrapper when RUNTIME.type is "node" and RUNTIME.parsedVersion is not null and RUNTIME.parsedVersion is less than 18', async () => {
const stream = await import("stream");
const stream = await import("readable-stream");
const expected = new NodePre18StreamWrapper(new stream.Readable());

RUNTIME.type = "node";
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/fetcher/webpack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import webpack from 'webpack';

describe("test env compatibility", () => {
test("webpack", (done) => {
webpack({
mode: "production",
entry: "./src/index.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},

}, (err, stats) => {
expect(err).toBe(null);
expect(stats?.hasErrors()).toBe(false);
done();
})
}, 20_000)
})
4 changes: 2 additions & 2 deletions tests/unit/form-data-utils/formDataWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("CrossPlatformFormData", () => {
});

it("should append a Readable stream with a specified filename", async () => {
const value = (await import("stream")).Readable.from(["file content"]);
const value = (await import("readable-stream")).Readable.from(["file content"]);
const filename = "testfile.txt";

await formData.appendFile("file", value, filename);
Expand Down Expand Up @@ -82,7 +82,7 @@ describe("CrossPlatformFormData", () => {
});

it("should append a Readable stream with a specified filename", async () => {
const value = (await import("stream")).Readable.from(["file content"]);
const value = (await import("readable-stream")).Readable.from(["file content"]);
const filename = "testfile.txt";

await formData.appendFile("file", value, filename);
Expand Down
Loading

0 comments on commit 2a956c9

Please sign in to comment.