Skip to content

Commit

Permalink
Merge branch 'origin-dev' into namesty/rust-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
dOrgJelli authored Dec 16, 2022
2 parents d510ad5 + 17a71cb commit cb62c3a
Show file tree
Hide file tree
Showing 56 changed files with 1,432 additions and 343 deletions.
8 changes: 8 additions & 0 deletions packages/cli/src/lib/test-env/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export function getTestEnvClientConfig(): Partial<PolywrapClientConfig> {
testnet: new Connection({
provider: ethProvider,
}),
mainnet: new Connection({
provider:
"https://mainnet.infura.io/v3/b00b2c2cc09c487685e9fb061256d6a6",
}),
goerli: new Connection({
provider:
"https://goerli.infura.io/v3/b00b2c2cc09c487685e9fb061256d6a6",
}),
},
}),
}),
Expand Down
3 changes: 2 additions & 1 deletion packages/js/client-config-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@polywrap/ipfs-resolver-plugin-js": "0.10.0-pre.5",
"@polywrap/logger-plugin-js": "0.10.0",
"@polywrap/uri-resolver-extensions-js": "0.10.0-pre.5",
"@polywrap/uri-resolvers-js": "0.10.0-pre.5"
"@polywrap/uri-resolvers-js": "0.10.0-pre.5",
"concurrent-plugin-js": "0.1.1"
},
"devDependencies": {
"@types/jest": "26.0.8",
Expand Down
10 changes: 10 additions & 0 deletions packages/js/client-config-builder/src/bundles/getDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { httpResolverPlugin } from "@polywrap/http-resolver-plugin-js";
import { fileSystemPlugin } from "@polywrap/fs-plugin-js";
import { loggerPlugin } from "@polywrap/logger-plugin-js";
import { fileSystemResolverPlugin } from "@polywrap/fs-resolver-plugin-js";
import { concurrentPromisePlugin } from "concurrent-plugin-js";

export const getDefaultConfig = (): ClientConfig<Uri> => {
return {
Expand Down Expand Up @@ -66,6 +67,10 @@ export const getDefaultConfig = (): ClientConfig<Uri> => {
interface: new Uri("wrap://ens/wrappers.polywrap.eth:logger@1.0.0"),
implementations: [new Uri("wrap://plugin/logger")],
},
{
interface: new Uri(defaultWrappers.concurrentInterface),
implementations: [new Uri("wrap://plugin/concurrent")],
},
],
packages: getDefaultPlugins(),
wrappers: [],
Expand All @@ -82,6 +87,7 @@ export const defaultWrappers = {
sha3: "wrap://ens/goerli/sha3.wrappers.eth",
uts46: "wrap://ens/goerli/uts46-lite.wrappers.eth",
graphNode: "wrap://ens/goerli/graph-node.wrappers.eth",
concurrentInterface: "wrap://ens/goerli/interface.concurrent.wrappers.eth",
};

export const getDefaultPlugins = (): IUriPackage<Uri>[] => {
Expand Down Expand Up @@ -137,5 +143,9 @@ export const getDefaultPlugins = (): IUriPackage<Uri>[] => {
uri: new Uri("wrap://ens/ipfs-resolver.polywrap.eth"),
package: ipfsResolverPlugin({}),
},
{
uri: new Uri("wrap://plugin/concurrent"),
package: concurrentPromisePlugin({}),
},
];
};
173 changes: 173 additions & 0 deletions packages/js/client/src/__tests__/core/embedded-package.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import fs from "fs";
import path from "path";
import { buildWrapper } from "@polywrap/test-env-js";
import { GetPathToTestWrappers } from "@polywrap/test-cases";
import { UriResolver } from "@polywrap/uri-resolvers-js";
import { InMemoryFileReader, WasmPackage } from "@polywrap/wasm-js";
import { IWrapPackage, Uri } from "@polywrap/core-js";
import { Result, ResultErr, ResultOk } from "@polywrap/result";
import { PolywrapClient } from "../../PolywrapClient";

jest.setTimeout(200000);

const simpleWrapperPath = `${GetPathToTestWrappers()}/wasm-as/simple`;
const simpleWrapperUri = new Uri(`fs/${simpleWrapperPath}/build`);

describe("Embedded package", () => {
beforeAll(async () => {
await buildWrapper(simpleWrapperPath);
});

it("can invoke an embedded package", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))

let wrapPackage = WasmPackage.from(manifestBuffer, wasmModuleBuffer);

const client = new PolywrapClient({
packages: [
{
uri: simpleWrapperUri,
package: wrapPackage
}
]
});

const result = await client.invoke<string>({
uri: simpleWrapperUri.uri,
method: "simpleMethod",
args: {
arg: "test",
},
});

if (!result.ok) fail(result.error);
expect(result.value).toBeTruthy();
expect(typeof result.value).toBe("string");
expect(result.value).toEqual("test");
});

it("can get a file from wrapper", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from(manifestBuffer, wasmModuleBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});

it("can add embedded wrapper through file reader", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from({
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.info") {
return ResultOk(manifestBuffer);
} else if (filePath === "wrap.wasm") {
return ResultOk(wasmModuleBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});

it("can add embedded wrapper with async wrap manifest", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from(
InMemoryFileReader.fromWasmModule(wasmModuleBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.info") {
return ResultOk(manifestBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
})
);

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});

it("can add embedded wrapper with async wasm module", async () => {
const manifestBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info"))
const wasmModuleBuffer = fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
const testFilePath = "hello.txt";
const testFileText = "Hello Test!";

const wrapPackage = WasmPackage.from(manifestBuffer, {
readFile: async (filePath): Promise<Result<Uint8Array, Error>> => {
if (filePath === testFilePath) {
return ResultOk(Buffer.from(testFileText, "utf-8"));
} else if (filePath === "wrap.wasm") {
return ResultOk(wasmModuleBuffer);
} else {
return ResultErr(new Error(`File not found: ${filePath}`));
}
}
});

await testEmbeddedPackageWithFile(wrapPackage, testFilePath, testFileText);
});
});

const testEmbeddedPackageWithFile = async (wrapPackage: IWrapPackage, filePath: string, fileText: string) => {
const client = new PolywrapClient({
packages: [
{
uri: simpleWrapperUri,
package: wrapPackage
}
]
});

const expectedManifest =
await fs.promises.readFile(`${simpleWrapperPath}/build/wrap.info`);
const receivedManifestResult = await client.getFile(simpleWrapperUri, {
path: "wrap.info",
});
if (!receivedManifestResult.ok) fail(receivedManifestResult.error);
const receivedManifest = receivedManifestResult.value as Uint8Array;
expect(receivedManifest).toEqual(expectedManifest);

const expectedWasmModule =
await fs.promises.readFile(`${simpleWrapperPath}/build/wrap.wasm`);
const receivedWasmModuleResult = await client.getFile(simpleWrapperUri, {
path: "wrap.wasm",
});
if (!receivedWasmModuleResult.ok) fail(receivedWasmModuleResult.error);
const receivedWasmModule = receivedWasmModuleResult.value as Uint8Array;
expect(receivedWasmModule).toEqual(expectedWasmModule);

const receivedHelloFileResult = await client.getFile(simpleWrapperUri, {
path: filePath,
encoding: "utf-8",
});
if (!receivedHelloFileResult.ok) fail(receivedHelloFileResult.error);
const receivedHelloFile = receivedHelloFileResult.value as Uint8Array;

expect(receivedHelloFile).toEqual(fileText);
};
Loading

0 comments on commit cb62c3a

Please sign in to comment.