Skip to content

feat!: add http transport and remove axios #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"@typescript-eslint/parser": "^4.29.0",
"ajv-cli": "^5.0.0",
"ajv-formats": "^2.1.1",
"axios": "^0.21.3",
"axios": "^0.26.1",
"chai": "~4.2.0",
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ValidationError } from "./event/validation";
import { CloudEventV1, CloudEventV1Attributes } from "./event/interfaces";

import { Options, TransportFunction, EmitterFunction, emitterFor, Emitter } from "./transport/emitter";
import { httpTransport } from "./transport/http";
import {
Headers, Mode, Binding, HTTP, Kafka, KafkaEvent, KafkaMessage, Message, MQTT, MQTTMessage, MQTTMessageFactory,
Serializer, Deserializer } from "./message";
Expand All @@ -25,6 +26,7 @@ export {
MQTT,
MQTTMessageFactory,
emitterFor,
httpTransport,
Emitter,
// From Constants
CONSTANTS
Expand Down
65 changes: 53 additions & 12 deletions src/transport/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,61 @@
SPDX-License-Identifier: Apache-2.0
*/

import { Socket } from "net";
import http, { OutgoingHttpHeaders } from "http";
import https, { RequestOptions } from "https";

import { Message, Options } from "../..";
import axios from "axios";
import { TransportFunction } from "../emitter";

/**
* httpTransport provides a simple HTTP Transport function, which can send a CloudEvent,
* encoded as a Message to the endpoint. The returned function can be used with emitterFor()
* to provide an event emitter, for example:
*
* const emitter = emitterFor(httpTransport("http://example.com"));
* emitter.emit(myCloudEvent)
* .then(resp => console.log(resp));
*
* @param {string|URL} sink the destination endpoint for the event
* @returns {TransportFunction} a function which can be used to send CloudEvents to _sink_
*/
export function httpTransport(sink: string | URL): TransportFunction {
const url = new URL(sink);
let base: any;
if (url.protocol === "https:") {
base = https;
} else if (url.protocol === "http:") {
base = http;
} else {
throw new TypeError(`unsupported protocol ${url.protocol}`);
}
return function(message: Message, options?: Options): Promise<unknown> {
return new Promise((resolve, reject) => {
options = { ...options };

export function axiosEmitter(sink: string) {
return function (message: Message, options?: Options): Promise<unknown> {
options = { ...options };
const headers = {
...message.headers,
...(options.headers as Record<string, string>),
};
delete options.headers;
return axios.post(sink, message.body, {
headers: headers,
...options,
// TODO: Callers should be able to set any Node.js RequestOptions
const opts: RequestOptions = {
method: "POST",
headers: {...message.headers, ...options.headers as OutgoingHttpHeaders},
};
try {
const response = {
body: "",
headers: {},
};
const req = base.request(url, opts, (res: Socket) => {
res.setEncoding("utf-8");
response.headers = (res as any).headers;
res.on("data", (chunk) => response.body += chunk);
res.on("end", () => { resolve(response); });
});
req.on("error", reject);
req.write(message.body);
req.end();
} catch (err) {
reject(err);
}
});
};
}
59 changes: 39 additions & 20 deletions test/integration/emitter_factory_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import "mocha";
import { expect } from "chai";
import nock from "nock";
import axios from "axios";
import axios, { AxiosRequestHeaders } from "axios";
import request from "superagent";
import got from "got";

import CONSTANTS from "../../src/constants";
import { CloudEvent, emitterFor, HTTP, Mode, Message, Options, TransportFunction } from "../../src";
import { CloudEvent, HTTP, Message, Mode, Options, TransportFunction, emitterFor, httpTransport }
from "../../src";

const DEFAULT_CE_CONTENT_TYPE = CONSTANTS.DEFAULT_CE_CONTENT_TYPE;
const sink = "https://cloudevents.io/";
Expand All @@ -38,7 +39,7 @@ export const fixture = new CloudEvent({
});

function axiosEmitter(message: Message, options?: Options): Promise<unknown> {
return axios.post(sink, message.body, { headers: message.headers, ...options });
return axios.post(sink, message.body, { headers: message.headers as AxiosRequestHeaders, ...options });
}

function superagentEmitter(message: Message, options?: Options): Promise<unknown> {
Expand Down Expand Up @@ -83,7 +84,6 @@ describe("emitterFor() defaults", () => {

it("Supports HTTP binding, structured mode", () => {
function transport(message: Message): Promise<unknown> {
console.error(message);
// A structured message will have the application/cloudevents+json header
expect(message.headers["content-type"]).to.equal(CONSTANTS.DEFAULT_CE_CONTENT_TYPE);
const body = JSON.parse(message.body as string);
Expand All @@ -101,33 +101,50 @@ describe("emitterFor() defaults", () => {
});
});

function setupMock(uri: string) {
nock(uri)
.post("/")
.reply(function (uri: string, body: nock.Body) {
// return the request body and the headers so they can be
// examined in the test
if (typeof body === "string") {
body = JSON.parse(body);
}
const returnBody = { ...(body as Record<string, unknown>), ...this.req.headers };
return [201, returnBody];
});
}

describe("HTTP Transport Binding for emitterFactory", () => {
beforeEach(() => {
nock(sink)
.post("/")
.reply(function (uri: string, body: nock.Body) {
// return the request body and the headers so they can be
// examined in the test
if (typeof body === "string") {
body = JSON.parse(body);
}
const returnBody = { ...(body as Record<string, unknown>), ...this.req.headers };
return [201, returnBody];
});
beforeEach(() => { setupMock(sink); });

describe("HTTPS builtin", () => {
testEmitterBinary(httpTransport(sink), "body");
});

describe("HTTP builtin", () => {
setupMock("http://cloudevents.io");
testEmitterBinary(httpTransport("http://cloudevents.io"), "body");
setupMock("http://cloudevents.io");
testEmitterStructured(httpTransport("http://cloudevents.io"), "body");
});

describe("Axios", () => {
testEmitter(axiosEmitter, "data");
testEmitterBinary(axiosEmitter, "data");
testEmitterStructured(axiosEmitter, "data");
});
describe("SuperAgent", () => {
testEmitter(superagentEmitter, "body");
testEmitterBinary(superagentEmitter, "body");
testEmitterStructured(superagentEmitter, "body");
});

describe("Got", () => {
testEmitter(gotEmitter, "body");
testEmitterBinary(gotEmitter, "body");
testEmitterStructured(gotEmitter, "body");
});
});

function testEmitter(fn: TransportFunction, bodyAttr: string) {
function testEmitterBinary(fn: TransportFunction, bodyAttr: string) {
it("Works as a binary event emitter", async () => {
const emitter = emitterFor(fn);
const response = (await emitter(fixture)) as Record<string, Record<string, string>>;
Expand All @@ -137,7 +154,9 @@ function testEmitter(fn: TransportFunction, bodyAttr: string) {
}
assertBinary(body);
});
}

function testEmitterStructured(fn: TransportFunction, bodyAttr: string) {
it("Works as a structured event emitter", async () => {
const emitter = emitterFor(fn, { binding: HTTP, mode: Mode.STRUCTURED });
const response = (await emitter(fixture)) as Record<string, Record<string, Record<string, string>>>;
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module.exports = {
},
resolve: {
fallback: {
util: require.resolve("util/")
util: require.resolve("util/"),
http: false,
https: false
},
},
output: {
Expand Down