Skip to content
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

Bump lockfiles #516

Closed
wants to merge 9 commits into from
Closed
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
8 changes: 2 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ COPY package.json yarn.lock lerna.json ./
COPY patches patches/
# Install lerna first
RUN yarn --frozen-lockfile --non-interactive --ignore-scripts --ignore-optional
COPY packages/admin-ui/package.json \
packages/admin-ui/yarn.lock \
packages/admin-ui/
COPY packages/dappmanager/package.json \
packages/dappmanager/yarn.lock \
packages/dappmanager/
COPY packages/admin-ui/package.json packages/admin-ui/
COPY packages/dappmanager/package.json packages/dappmanager/
RUN yarn bootstrap --production

# Build UI
Expand Down
14,217 changes: 0 additions & 14,217 deletions packages/admin-ui/yarn.lock

This file was deleted.

4 changes: 2 additions & 2 deletions packages/dappmanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"express": "^4.17.1",
"express-fileupload": "^1.1.5",
"http-proxy": "^1.18.0",
"ipfs-http-client": "^44.0.1",
"ipfs-http-client": "^48.1.2",
"is-ip": "^3.0.0",
"is-ipfs": "^1.0.3",
"it-to-stream": "^0.1.1",
Expand All @@ -74,7 +74,7 @@
"mime-types": "^2.1.27",
"monero-rpc": "^0.5.0",
"multicodec": "^1.0.1",
"multihashes": "^0.4.18",
"multihashes": "^3.0.0",
"node-fetch": "^2.6.0",
"node-os-utils": "^1.2.2",
"semver": "^5.5.0",
Expand Down
34 changes: 4 additions & 30 deletions packages/dappmanager/src/ethForward/utils/decodeContentHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CID from "cids";
import multicodec from "multicodec";
import { Content } from "../types";
import { isEmpty } from "./isEmpty";
const multihash: Multihash = require("multihashes");
import multihash from "multihashes";

/**
* Used in the CONTENTHASH_INTERFACE_ID = "0xbc1c58d1"
Expand All @@ -17,45 +17,19 @@ export function decodeContentHash(contenthash: string): Content {

if (contentCodec.startsWith("ipfs")) {
const value = multicodec.rmPrefix(contentHashEncoded);
const cid = new CID(value);
const cid = new CID(value as Buffer);
return {
location: "ipfs",
hash: multihash.toB58String(cid.multihash)
};
} else if (contentCodec.startsWith("swarm")) {
const value = multicodec.rmPrefix(contentHashEncoded);
const cid = new CID(value);
const cid = new CID(value as Buffer);
return {
location: "swarm",
hash: multihash.decode(cid.multihash).digest.toString("hex")
hash: Buffer.from(multihash.decode(cid.multihash).digest).toString("hex")
};
} else {
throw Error(`Unsupported coded: ${contentCodec}`);
}
}

/**
* multihashes is not typed, nor @types/multihashes exists
*/
interface Multihash {
/**
* Convert the given multihash to a base58 encoded string.
* @param hash
* @returns
*/
toB58String(hash: Buffer): string;

/**
* Decode a hash from the given multihash.
* @param buf
* @returns result
*/
decode(
buf: Buffer
): {
code: number;
name: string;
length: number;
digest: Buffer;
};
}
6 changes: 3 additions & 3 deletions packages/dappmanager/src/modules/ipfs/ipfsSetup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ipfsClient = require("ipfs-http-client");
import Ipfs from "ipfs-http-client";
import params from "../../params";

export const timeoutMs = 30 * 1000;
Expand All @@ -20,7 +20,7 @@ export const TimeoutErrorKy = class TimeoutError extends Error {
*/
const IPFS_HOST = params.IPFS_HOST;
const ipfs = process.env.DISABLE_IPFS
? {}
: ipfsClient(IPFS_HOST, { timeout: timeoutMs });
? ({} as ReturnType<typeof Ipfs>)
: Ipfs({ url: IPFS_HOST, timeout: timeoutMs });

export default ipfs;
13 changes: 2 additions & 11 deletions packages/dappmanager/src/modules/ipfs/methods/pinAdd.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import ipfs, { timeoutMs } from "../ipfsSetup";
import { logs } from "../../../logs";

/**
* [ { hash: 'QmNqDvqAyy3pN3PvymB6chM7S1FgYyive8LosVKUuaDdfd' } ]
*/
type IpfsPinAddResult = { hash: string }[];

/**
* Pin a hash
* @param hash "QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD"
*/
export async function pinAdd({
hash
}: {
hash: string;
}): Promise<IpfsPinAddResult> {
return await ipfs.pin.add(hash, { timeout: timeoutMs });
export async function pinAdd({ hash }: { hash: string }): Promise<void> {
await ipfs.pin.add(hash, { timeout: timeoutMs });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/dappmanager/src/utils/asyncFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function runOnlyOneSequentially<A, R>(
1e9);

return function(arg?: A): void {
cargo.push({ arg });
cargo.push({ arg: arg! });
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function uploadManifestRelease(
version: manifest.version,
serviceNames: [manifest.name]
});
const [imageUploadResult] = await ipfsAddFromFs(imagePath);
const imageUploadResult = await ipfsAddFromFs(imagePath);

if (!manifest.image) throw Error("No image in manifest");
manifest.image.hash = imageUploadResult.hash;
Expand Down
61 changes: 61 additions & 0 deletions packages/dappmanager/test/modules/ipfs/index.test.int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from "fs";
import path from "path";
import { cleanTestDir, testDir } from "../../testUtils";
import * as ipfs from "../../../src/modules/ipfs";
import { ipfsAddDirFromFs } from "../../testIpfsUtils";
import { expect } from "chai";
import { pinAdd } from "../../../src/modules/ipfs/methods/pinAdd";
import objectSize from "../../../src/modules/ipfs/methods/objectSize";

describe("ipfs / integration test", function() {
this.timeout(60 * 1000);

const dirPath = path.join(testDir, "ipfs-test-upload");
const filepath = path.join(dirPath, "sample.txt");
const filePathResult = path.join(testDir, "sample-result.txt");
const fileContents = "sample-contents";

type Await<T> = T extends PromiseLike<infer U> ? U : T;

let dirHash: string;
let fileHash: string;
let files: Await<ReturnType<typeof ipfs.ls>>;

before("Prepare directory", () => {
fs.mkdirSync(dirPath, { recursive: true });
fs.writeFileSync(filepath, fileContents);
});

after("Clean test dir", async () => {
await cleanTestDir();
});

it("Upload directory", async () => {
dirHash = await ipfsAddDirFromFs(dirPath);
});

it("List directory files", async () => {
files = await ipfs.ls({ hash: dirHash });
fileHash = files[0].hash;
});

it("Download file to FS", async () => {
await ipfs.catStreamToFs({ hash: fileHash, path: filePathResult });
const result = fs.readFileSync(filePathResult, "utf8");
expect(result).to.equal(fileContents, "Wrong downloaded file contents");
});

it("Download file to memory", async () => {
const result = await ipfs.catString({ hash: fileHash });
expect(result).to.equal(fileContents, "Wrong downloaded file contents");
});

it("Pin file", async () => {
await pinAdd({ hash: fileHash });
});

it("objectSize", async () => {
const size = await objectSize(dirHash);
expect(size).to.be.a("number");
});
});
73 changes: 19 additions & 54 deletions packages/dappmanager/test/testIpfsUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from "fs";
import path from "path";
import ipfsRaw from "../src/modules/ipfs/ipfsSetup";
import { Manifest } from "../src/types";
const Ipfs = require("ipfs-http-client");
import Ipfs from "ipfs-http-client";

const globSource = (Ipfs as any).globSource;
const Buffer = (Ipfs as any).Buffer;

/**
* Util, IPFS wrapper with type info
Expand All @@ -12,22 +14,19 @@ type IpfsAddResult = {
path: string;
hash: string;
size: number;
}[];
};

/**
* Wrapper to abstract converting the return values of ipfs.add
* @param content
*/
async function ipfsAdd(content: any): Promise<IpfsAddResult> {
const files = [];
for await (const file of ipfsRaw.add(content)) {
files.push(file);
}
return files.map(file => ({
function parseAddResult(file: any): IpfsAddResult {
return {
path: file.path,
hash: file.cid.toString(),
size: file.size
}));
};
}

async function ipfsAddSingle(content: any): Promise<IpfsAddResult> {
const file = await ipfsRaw.add(content);
return parseAddResult(file);
}

/**
Expand All @@ -40,54 +39,20 @@ export async function ipfsAddFromFs(
): Promise<IpfsAddResult> {
if (!fs.existsSync(path))
throw Error(`ipfs.addFromFs error: no file found at: ${path}`);
return await ipfsAdd(Ipfs.globSource(path, options));
return await ipfsAddSingle(globSource(path, options));
}

export async function ipfsAddDirFromFs(path: string): Promise<string> {
return ipfsAddFromFs(path, { recursive: true }).then(findRootHash);
const result = await ipfsAddFromFs(path, { recursive: true });
return result.hash;
}

/**
* Uploads a manifest from memory
* This should be part of the `DAppNodeSDK`
*/
export async function ipfsAddManifest(manifest: Manifest): Promise<string> {
const content = Ipfs.Buffer.from(JSON.stringify(manifest, null, 2));
const results = await ipfsAdd(content);
return results[0].hash;
}

/**
* Returns the root IPFS hash of a directory upload
*
* Sample @param uploadedFiles: [
* { path: 'release-directory-docker-compose/dappnode_package-no-hashes.json',
* hash: 'QmZ5sKqDtgV4J8DM8D1RUziNrsC2Sx8hRw5NXFU8LctJRN',
* size: 338 },
* { path: 'release-directory-docker-compose/docker-compose-mock-test.yml',
* hash: 'QmTp5Rb3k2cyzN7gZpUe4zQ6cMV3FoWJDxJUVfLZDsXhfo',
* size: 135 },
* { path: 'release-directory-docker-compose/mock-test.public.dappnode.eth_0.0.1.tar.xz',
* hash: 'QmP1CbEd5WTUqqKeDxvaDg9noPQNtcpKmcXj3zsqZyKKo8',
* size: 637642 },
* { path: 'release-directory-docker-compose',
* hash: 'QmaRXWSyst18BPyjKiMMKzn94krYEKZaoyVsyoPxh8PzjG',
* size: 638350 }
* ]
*
* Sample return of `path.parse`
* > path.parse("test/a.json")
* { root: '', dir: 'test', base: 'a.json', ext: '.json', name: 'a' }
* > path.parse("a.json")
* { root: '', dir: '', base: 'a.json', ext: '.json', name: 'a' }
* > path.parse("test")
* { root: '', dir: '', base: 'test', ext: '', name: 'test' }
*/
function findRootHash(uploadedFiles: IpfsAddResult): string {
const rootEntries = uploadedFiles.filter(e => !path.parse(e.path).dir);
if (rootEntries.length === 1) return rootEntries[0].hash;
else {
console.log(uploadedFiles);
throw Error("No releaseEntry found in uploaded release files");
}
const content = Buffer.from(JSON.stringify(manifest, null, 2));
const result = await ipfsAddSingle(content);
return result.hash;
}
Loading