Skip to content

Commit

Permalink
Merge pull request #1847 from polywrap/pileks/bugfix/ipfs-deployer-su…
Browse files Browse the repository at this point in the history
…bdirectory-duplication

Fix IPFS deployer subdirectory duplication issue
  • Loading branch information
pileks authored Aug 8, 2023
2 parents 796a880 + 46719d7 commit 766035e
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 84 deletions.
33 changes: 33 additions & 0 deletions packages/cli/src/__tests__/unit/ipfs-deployer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { GetPathToCliTestFiles } from "@polywrap/test-cases";
import { readDirContents } from "../../lib/defaults/deploy-modules/ipfs/utils";
import path from "path";

describe("IPFS Deployer", () => {
const sampleBuildDir = path.join(
GetPathToCliTestFiles(),
"lib",
"deployers",
"mock-build-output"
);

it("properly reads build dir contents and builds out an IPFS-compatible directory entry", async () => {
const dirents = await readDirContents(sampleBuildDir, "");

expect(dirents).toMatchObject({
name: "",
files: [{ name: "wrap.info" }, { name: "wrap.wasm" }],
directories: [
{
name: "docs",
files: [{ name: "polywrap.docs.json" }],
directories: [
{
name: "pages",
files: [{ name: "readme.md" }],
},
],
},
],
});
});
});
63 changes: 2 additions & 61 deletions packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,15 @@
import { DeployModule } from "../../../deploy";
import { AddResult, ArgsAddDir, DirectoryEntry } from "./types";
import { readDirContents } from "./utils";

import { Uri } from "@polywrap/core-js";
import { PolywrapClient } from "@polywrap/client-js";
import * as Sys from "@polywrap/sys-config-bundle-js";
import { PolywrapClientConfigBuilder } from "@polywrap/client-config-builder-js";
import fs from "fs";

const isValidUri = (uri: Uri) =>
uri.authority === "fs" || uri.authority === "file";

interface FileEntry {
name: string;
data: Uint8Array;
}

interface DirectoryEntry {
name: string;
directories?: DirectoryEntry[];
files?: FileEntry[];
}

interface AddOptions {
pin?: boolean;
onlyHash?: boolean;
wrapWithDirectory?: boolean;
}

interface AddResult {
name: string;
hash: string;
size: string;
}

interface ArgsAddDir {
data: DirectoryEntry;
ipfsProvider: string;
timeout?: number;
addOptions?: AddOptions;
}

const readDirContents = async (
path: string,
dirName: string
): Promise<DirectoryEntry> => {
const dirents: fs.Dirent[] = await fs.promises.readdir(path, {
withFileTypes: true,
});
const data: DirectoryEntry = { name: dirName };

for (const dirent of dirents) {
if (dirent.isDirectory()) {
const subDir = await readDirContents(
`${path}/${dirent.name}`,
`${dirName}/${dirent.name}`
);
data.directories = data.directories ?? [];
data.directories?.push(subDir);
} else {
const fileData = await fs.promises.readFile(`${path}/${dirent.name}`);
data.files = data.files ?? [];
data.files?.push({
name: dirent.name,
data: fileData,
});
}
}

return data;
};

class IPFSDeployer implements DeployModule {
async execute(uri: Uri, config?: { gatewayUri: string }): Promise<Uri> {
if (!isValidUri(uri)) {
Expand Down
29 changes: 29 additions & 0 deletions packages/cli/src/lib/defaults/deploy-modules/ipfs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export interface FileEntry {
name: string;
data: Uint8Array;
}

export interface DirectoryEntry {
name: string;
directories?: DirectoryEntry[];
files?: FileEntry[];
}

export interface AddOptions {
pin?: boolean;
onlyHash?: boolean;
wrapWithDirectory?: boolean;
}

export interface AddResult {
name: string;
hash: string;
size: string;
}

export interface ArgsAddDir {
data: DirectoryEntry;
ipfsProvider: string;
timeout?: number;
addOptions?: AddOptions;
}
33 changes: 33 additions & 0 deletions packages/cli/src/lib/defaults/deploy-modules/ipfs/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DirectoryEntry } from "./types";

import fs from "fs";

export const readDirContents = async (
path: string,
dirName: string
): Promise<DirectoryEntry> => {
const dirents: fs.Dirent[] = await fs.promises.readdir(path, {
withFileTypes: true,
});
const data: DirectoryEntry = { name: dirName };

for (const dirent of dirents) {
if (dirent.isDirectory()) {
const subDir = await readDirContents(
`${path}/${dirent.name}`,
`${dirent.name}`
);
data.directories = data.directories ?? [];
data.directories?.push(subDir);
} else {
const fileData = await fs.promises.readFile(`${path}/${dirent.name}`);
data.files = data.files ?? [];
data.files?.push({
name: dirent.name,
data: fileData,
});
}
}

return data;
};
Empty file.
Empty file.
Empty file.
Empty file.
46 changes: 23 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3370,13 +3370,13 @@ browser-process-hrtime@^1.0.0:
integrity sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==

browserslist@^4.21.9:
version "4.21.9"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.21.9.tgz#e11bdd3c313d7e2a9e87e8b4b0c7872b13897635"
integrity sha512-M0MFoZzbUrRU4KNfCrDLnvyE7gub+peetoTid3TBIqtunaDJyXlwhakT+/VkvSXcfIzFfK/nkCs4nmyTmxdNSg==
version "4.21.10"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.21.10.tgz#dbbac576628c13d3b2231332cb2ec5a46e015bb0"
integrity sha512-bipEBdZfVH5/pwrvqc+Ub0kUPVfGUhlKxbvfD+z1BDnPEO/X98ruXGA1WP5ASpAFKan7Qr6j736IacbZQuAlKQ==
dependencies:
caniuse-lite "^1.0.30001503"
electron-to-chromium "^1.4.431"
node-releases "^2.0.12"
caniuse-lite "^1.0.30001517"
electron-to-chromium "^1.4.477"
node-releases "^2.0.13"
update-browserslist-db "^1.0.11"

bs-logger@0.x:
Expand Down Expand Up @@ -3507,10 +3507,10 @@ camelcase@^6.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a"
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

caniuse-lite@^1.0.30001503:
version "1.0.30001517"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001517.tgz#90fabae294215c3495807eb24fc809e11dc2f0a8"
integrity sha512-Vdhm5S11DaFVLlyiKu4hiUTkpZu+y1KA/rZZqVQfOD5YdDT/eQKlkt7NaE0WGOFgX32diqt9MiP9CAiFeRklaA==
caniuse-lite@^1.0.30001517:
version "1.0.30001518"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001518.tgz#b3ca93904cb4699c01218246c4d77a71dbe97150"
integrity sha512-rup09/e3I0BKjncL+FesTayKtPrdwKhUufQFd3riFw1hHg8JmIFoInYfB102cFcY/pPgGmdyl/iy+jgiDi2vdA==

capture-exit@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -4284,10 +4284,10 @@ ecc-jsbn@~0.1.1:
jsbn "~0.1.0"
safer-buffer "^2.1.0"

electron-to-chromium@^1.4.431:
version "1.4.473"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.473.tgz#4853de13a335c70fe1f9df8d4029be54068767d1"
integrity sha512-aVfC8+440vGfl06l8HKKn8/PD5jRfSnLkTTD65EFvU46igbpQRri1gxSzW9/+TeUlwYzrXk1sw867T96zlyECA==
electron-to-chromium@^1.4.477:
version "1.4.480"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.480.tgz#40e32849ca50bc23ce29c1516c5adb3fddac919d"
integrity sha512-IXTgg+bITkQv/FLP9FjX6f9KFCs5hQWeh5uNSKxB9mqYj/JXhHDbu+ekS43LVvbkL3eW6/oZy4+r9Om6lan1Uw==

elliptic@6.5.4:
version "6.5.4"
Expand Down Expand Up @@ -4332,9 +4332,9 @@ end-of-stream@^1.1.0:
once "^1.4.0"

enquirer@^2.3.5:
version "2.4.0"
resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.4.0.tgz#4f36f6c644137cc4fd2891da407ede2b1fea904a"
integrity sha512-ehu97t6FTYK2I3ZYtnp0BZ9vt0mvEL/cnHBds7Ct6jo9VX1VIkiFhOvVRWh6eblQqd7KOoICIQV+syZ3neXO/Q==
version "2.4.1"
resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.4.1.tgz#93334b3fbd74fc7097b224ab4a8fb7e40bf4ae56"
integrity sha512-rRqJg/6gd538VHvR3PSrdRBb/1Vy2YfzHqzvbhGIQpDRKIa4FgV/54b5Q1xYSxOOwKvjXweS26E0Q+nAMwp2pQ==
dependencies:
ansi-colors "^4.1.1"
strip-ansi "^6.0.1"
Expand Down Expand Up @@ -5353,12 +5353,12 @@ growly@^1.3.0:
integrity sha512-+xGQY0YyAWCnqy7Cd++hc2JqMYzlm0dG30Jd0beaA64sROr8C4nt8Yc9V5Ro3avlSUDTN0ulqP/VBKi1/lLygw==

handlebars@^4.7.7:
version "4.7.7"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"
integrity sha512-aAcXm5OAfE/8IXkcZvCepKU3VzW1/39Fb5ZuqMtgI/hT8X2YgoMvBY5dLhq/cpOvw7Lk1nK/UF71aLG/ZnVYRA==
version "4.7.8"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.8.tgz#41c42c18b1be2365439188c77c6afae71c0cd9e9"
integrity sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==
dependencies:
minimist "^1.2.5"
neo-async "^2.6.0"
neo-async "^2.6.2"
source-map "^0.6.1"
wordwrap "^1.0.0"
optionalDependencies:
Expand Down Expand Up @@ -7415,7 +7415,7 @@ negotiator@^0.6.2:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

neo-async@^2.6.0:
neo-async@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==
Expand Down Expand Up @@ -7487,7 +7487,7 @@ node-notifier@^8.0.0:
uuid "^8.3.0"
which "^2.0.2"

node-releases@^2.0.12:
node-releases@^2.0.13:
version "2.0.13"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.13.tgz#d5ed1627c23e3461e819b02e57b75e4899b1c81d"
integrity sha512-uYr7J37ae/ORWdZeQ1xxMJe3NtdmqMC/JZK+geofDrkLUApKRHPd18/TxtBOJ4A0/+uUIliorNrfYV6s1b02eQ==
Expand Down

0 comments on commit 766035e

Please sign in to comment.