Skip to content

Commit

Permalink
feat: remove ucan-storage dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed May 3, 2022
1 parent ed63deb commit 1586260
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 263 deletions.
64 changes: 3 additions & 61 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"@ipld/dag-ucan": "^1.2.4-beta",
"it-all": "^1.0.6",
"multiformats": "^9.6.4",
"ucan-storage": "^1.2.0",
"ucanto": "0.1.0-beta",
"uint8arrays": "^3.0.0"
"uint8arrays": "^3.0.0",
"@noble/ed25519": "^1.6.0"
},
"files": [
"./bin/*",
Expand Down
117 changes: 58 additions & 59 deletions src/http.spec.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,123 @@
import * as assert from "assert";
import { describe, it } from "mocha";
import { service } from "./service.js";
import * as Server from "ucanto/src/server.js";
import * as Transport from "ucanto/src/transport.js";
import * as HTTP from "ucanto/src/transport/http.js";
import * as Client from "ucanto/src/client.js";
import { createIssuer } from "./issuer.js";
import { KeyPair } from "ucan-storage/keypair";
import * as http from "http";
import { AddressInfo } from "net";
import { server } from "./server.js";
import { blob } from "stream/consumers";
import polyfillFetch from "@web-std/fetch";
import all from "it-all";
import * as uint8arrays from "uint8arrays";
import * as assert from "assert"
import { describe, it } from "mocha"
import { service } from "./service.js"
import * as Server from "ucanto/src/server.js"
import * as Transport from "ucanto/src/transport.js"
import * as HTTP from "ucanto/src/transport/http.js"
import * as Client from "ucanto/src/client.js"
import * as Issuer from "./actor/issuer.js"
import * as http from "http"
import { AddressInfo } from "net"
import { server } from "./server.js"
import { blob } from "stream/consumers"
import polyfillFetch from "@web-std/fetch"
import all from "it-all"
import * as uint8arrays from "uint8arrays"

// 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;
typeof globalThis.fetch !== "undefined" ? globalThis.fetch : polyfillFetch

describe("http name server", () => {
it("can be invoked via Client with HTTP transport", async () => {
const alice = createIssuer(await KeyPair.create());
const alice = await Issuer.generate()
const server = Server.create({
service,
decoder: Transport.CAR,
encoder: Transport.CBOR,
});
})
await withUcantoServer(server, async (url: URL) => {
const connection = Client.connect({
encoder: Transport.CAR, // encode as CAR because server decodes from car
decoder: Transport.CBOR, // decode as CBOR because server encodes as CBOR
/** @type {Transport.Channel<typeof service>} */
channel: HTTP.open<typeof service>({ fetch, url }), // simple `fetch` wrapper
});
})
const publish = Client.invoke({
issuer: alice,
audience: alice,
capability: {
can: "name/resolve",
with: alice.did(),
},
});
await publish.execute(connection);
});
});
});
})
await publish.execute(connection)
})
})
})

function toHeadersRecord(
headers: http.IncomingHttpHeaders
): Readonly<Record<string, string>> {
const headersRecord: Record<string, string> = {};
const headersRecord: Record<string, string> = {}
for (const [key, valueOrValues] of Object.entries(headers)) {
const value = Array.isArray(valueOrValues)
? valueOrValues[0]
: valueOrValues;
: valueOrValues
if (typeof value !== "string") {
console.warn("got unexpected non-string header value. ignoring", value);
continue;
console.warn("got unexpected non-string header value. ignoring", value)
continue
}
headersRecord[key] = value;
headersRecord[key] = value
}
return headersRecord;
return headersRecord
}

async function withUcantoServer<T>(
ucantoServer: Client.ServerView<T>,
useServer: (baseUrl: URL) => Promise<void>
) {
const httpServer = http.createServer((req, res) => {
all(req).then(async (httpRequestBodyChunks) => {
all(req).then(async httpRequestBodyChunks => {
const httpRequestBodyArrayBuffer = uint8arrays.concat(
httpRequestBodyChunks
);
let ucantoResponse;
const headers = toHeadersRecord(req.headers);
)
let ucantoResponse
const headers = toHeadersRecord(req.headers)
try {
ucantoResponse = await ucantoServer.request({
body: httpRequestBodyArrayBuffer,
headers,
});
})
} catch (error) {
console.warn("ucantoServer error", error);
res.writeHead(500);
console.warn("ucantoServer error", error)
res.writeHead(500)
res.end(
JSON.stringify({
message: error instanceof Error ? error.message : undefined,
})
);
return;
)
return
}
res.writeHead(200, ucantoResponse?.headers);
res.end(ucantoResponse?.body);
});
});
res.writeHead(200, ucantoResponse?.headers)
res.end(ucantoResponse?.body)
})
})
await new Promise((resolve, _reject) => {
httpServer.listen(0, () => {
resolve(true);
});
});
const baseUrl = addressUrl(httpServer.address());
resolve(true)
})
})
const baseUrl = addressUrl(httpServer.address())
if (!baseUrl) {
throw new Error(`failed to determine baseUrl from ucantoServer`);
throw new Error(`failed to determine baseUrl from ucantoServer`)
}
try {
await useServer(baseUrl);
await useServer(baseUrl)
} finally {
await new Promise((resolve, _reject) => {
httpServer.close(resolve);
});
httpServer.close(resolve)
})
}
return;
return
}

function addressUrl(addressInfo: string | AddressInfo | null): URL | null {
if (addressInfo === null) return null;
if (typeof addressInfo === "string") return new URL(addressInfo);
const { address, port } = addressInfo;
const host = address === "::" ? "127.0.0.1" : address;
const urlString = `http://${host}:${port}`;
return new URL(urlString);
if (addressInfo === null) return null
if (typeof addressInfo === "string") return new URL(addressInfo)
const { address, port } = addressInfo
const host = address === "::" ? "127.0.0.1" : address
const urlString = `http://${host}:${port}`
return new URL(urlString)
}
8 changes: 0 additions & 8 deletions src/issuer.ts

This file was deleted.

Loading

0 comments on commit 1586260

Please sign in to comment.