Skip to content

Commit

Permalink
feat: improve http transport configuration (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo authored May 3, 2022
1 parent e24f536 commit c99be08
Show file tree
Hide file tree
Showing 3 changed files with 482 additions and 473 deletions.
45 changes: 30 additions & 15 deletions src/transport/http.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
import * as Transport from "./api.js"
import fetch from "@web-std/fetch"
import * as Transport from "./api.js";
import * as CAR from "./car.js";

/**
* @template T
* @param {URL} url
* @param {object} options
* @param {typeof fetch} [options.fetch]
* @param {URL} options.url
* @param {string} [options.method]
* @returns {Transport.Channel<T>}
*/
export const open = url => new Channel({ url })
export const open = ({ url, fetch = globalThis.fetch, method = 'POST', }) => {
if (typeof fetch === 'undefined') {
throw new TypeError(`ucanto HTTP transport got undefined \`fetch\`. Try passing in a \`fetch\` implementation explicitly.`)
}
return new Channel({ fetch, url, method });
}

class Channel {
/**
* @param {object} options
* @param {URL} options.url
* @param {typeof fetch} options.fetch
* @param {string} [options.method]
*/
constructor({ url }) {
this.url = url
constructor({ url, fetch, method }) {
this.fetch = fetch;
this.method = method;
this.url = url;
}
/**
* @param {Transport.HTTPRequest} request
* @returns {Promise<Transport.HTTPResponse>}
*/
async request({ headers, body }) {
const response = await fetch(this.url.href, {
const decodedBody = await CAR.decode({ headers, body });
const response = await this.fetch(this.url.href, {
headers,
body,
})
method: this.method
});

const buffer = response.ok
? await response.arrayBuffer()
: HTTPError.throw("HTTP Request failed", response)
: HTTPError.throw("HTTP Request failed", response);

return {
headers: Object.fromEntries(response.headers.entries()),
body: new Uint8Array(buffer),
}
};
}
}

Expand All @@ -50,16 +65,16 @@ class HTTPError extends Error {
* @returns {never}
*/
static throw(message, options) {
throw new this(message, options)
throw new this(message, options);
}
/**
* @param {string} message
* @param {Options} options
*/
constructor(message, { url, status = 500, statusText = "Server error" }) {
super(message)
this.url = url
this.status = status
this.statusText = statusText
super(message);
this.url = url;
this.status = status;
this.statusText = statusText;
}
}
14 changes: 10 additions & 4 deletions test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import * as Packet from "../src/transport/packet.js"
import { writeCAR, writeCBOR, importActors } from "./util.js"
import { isLink } from "../src/transport/packet.js"
import * as Service from "./service.js"
import polyfillFetch from "@web-std/fetch";

// in node <17.5.0, globalThis.fetch is not defined, so use polyfill
// https://nodejs.org/api/globals.html#fetch
const fetch =
typeof globalThis.fetch !== "undefined" ? globalThis.fetch : polyfillFetch;

describe("delegation", () => {
it("delegation can be transpcoded as ucan", async () => {
Expand Down Expand Up @@ -168,10 +174,10 @@ describe("invoke", () => {
const { alice, web3Storage } = await importActors()
/** @type {Client.ConnectionView<Service.Service>} */
const connection = Client.connect({
channel: HTTP.open(new URL("about:blank")),
channel: HTTP.open({ url: new URL("about:blank"), fetch }),
encoder: Transport.CAR,
decoder: Transport.CBOR,
})
});

const car = await writeCAR([await writeCBOR({ hello: "world " })])
const add = Client.invoke({
Expand Down Expand Up @@ -214,10 +220,10 @@ describe("invoke", () => {

/** @type {Client.ConnectionView<Service.Service>} */
const connection = Client.connect({
channel: HTTP.open(new URL("about:blank")),
channel: HTTP.open({ url: new URL("about:blank"), fetch }),
encoder: Transport.CAR,
decoder: Transport.CBOR,
})
});

const proof = await Client.delegate({
issuer: alice,
Expand Down
Loading

0 comments on commit c99be08

Please sign in to comment.