From db229018520a81740b30efe8e4031ad2dd71431c Mon Sep 17 00:00:00 2001 From: namesty Date: Thu, 7 Jul 2022 18:20:23 +0200 Subject: [PATCH 1/6] (chore): allow custom cache object to be passed to client config --- packages/js/client/src/PolywrapClient.ts | 4 +++- packages/js/core/src/types/Client.ts | 2 ++ packages/js/core/src/types/Wrapper.ts | 2 -- packages/js/core/src/types/WrapperCache.ts | 11 +++++++++++ packages/js/core/src/types/index.ts | 1 + todos.txt | 5 +++++ 6 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 packages/js/core/src/types/WrapperCache.ts create mode 100644 todos.txt diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index 0ca72f7a5c..ef775684e5 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -61,7 +61,7 @@ export class PolywrapClient implements Client { // It should help us keep track of what URI's map to what Wrappers, // and handle cases where the are multiple jumps. For example, if // A => B => C, then the cache should have A => C, and B => C. - private _wrapperCache: WrapperCache = new Map(); + private _wrapperCache: WrapperCache; private _config: PolywrapClientConfig = { redirects: [], plugins: [], @@ -98,6 +98,8 @@ export class PolywrapClient implements Client { uriResolvers: config.uriResolvers ?? [], tracingEnabled: !!config.tracingEnabled, }; + + this._wrapperCache = config.wrapperCache ?? new Map(); } if (!options?.noDefaults) { diff --git a/packages/js/core/src/types/Client.ts b/packages/js/core/src/types/Client.ts index bd9a7cfa86..3afc060cfc 100644 --- a/packages/js/core/src/types/Client.ts +++ b/packages/js/core/src/types/Client.ts @@ -11,6 +11,7 @@ import { } from "./"; import { UriResolver } from "../uri-resolution/core"; import { UriResolverHandler } from "./UriResolver"; +import { WrapperCache } from "./WrapperCache"; import { WrapManifest } from "@polywrap/wrap-manifest-types-js"; @@ -20,6 +21,7 @@ export interface ClientConfig { interfaces: InterfaceImplementations[]; envs: Env[]; uriResolvers: UriResolver[]; + wrapperCache?: WrapperCache; } export interface Contextualized { diff --git a/packages/js/core/src/types/Wrapper.ts b/packages/js/core/src/types/Wrapper.ts index b11e46a6c1..9604f2a741 100644 --- a/packages/js/core/src/types/Wrapper.ts +++ b/packages/js/core/src/types/Wrapper.ts @@ -61,5 +61,3 @@ export abstract class Wrapper implements Invocable { */ public abstract getSchema(client: Client): Promise; } - -export type WrapperCache = Map; diff --git a/packages/js/core/src/types/WrapperCache.ts b/packages/js/core/src/types/WrapperCache.ts new file mode 100644 index 0000000000..34e08696ca --- /dev/null +++ b/packages/js/core/src/types/WrapperCache.ts @@ -0,0 +1,11 @@ +import { Wrapper } from "./Wrapper"; + +export interface WrapperCache { + readonly size: number; + + clear(): void; + delete(key: string): boolean; + get(key: string): Wrapper | undefined; + has(key: string): boolean; + set(key: string, value: Wrapper): void; +} diff --git a/packages/js/core/src/types/index.ts b/packages/js/core/src/types/index.ts index 5b604d3c3c..91329a83f7 100644 --- a/packages/js/core/src/types/index.ts +++ b/packages/js/core/src/types/index.ts @@ -12,3 +12,4 @@ export * from "./InterfaceImplementations"; export * from "./PluginRegistration"; export * from "./UriResolver"; export * from "./Workflow"; +export * from "./WrapperCache"; diff --git a/todos.txt b/todos.txt new file mode 100644 index 0000000000..008e71238b --- /dev/null +++ b/todos.txt @@ -0,0 +1,5 @@ +specify their own cache + +define a cache interface + +check env URI resolution stack method, it's a bit weird \ No newline at end of file From 239c66554713eab9ea917b8f27bd18c3c95de009 Mon Sep 17 00:00:00 2001 From: namesty Date: Thu, 7 Jul 2022 18:21:33 +0200 Subject: [PATCH 2/6] (chore): remove todo file --- todos.txt | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 todos.txt diff --git a/todos.txt b/todos.txt deleted file mode 100644 index 008e71238b..0000000000 --- a/todos.txt +++ /dev/null @@ -1,5 +0,0 @@ -specify their own cache - -define a cache interface - -check env URI resolution stack method, it's a bit weird \ No newline at end of file From 83d8400fa05102f87d640268701655660b004fb1 Mon Sep 17 00:00:00 2001 From: namesty Date: Thu, 14 Jul 2022 12:58:38 +0200 Subject: [PATCH 3/6] (fix): cache instantiation --- packages/js/client/src/PolywrapClient.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index ef775684e5..caa3deeeaa 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -61,7 +61,7 @@ export class PolywrapClient implements Client { // It should help us keep track of what URI's map to what Wrappers, // and handle cases where the are multiple jumps. For example, if // A => B => C, then the cache should have A => C, and B => C. - private _wrapperCache: WrapperCache; + private _wrapperCache: WrapperCache = new Map();; private _config: PolywrapClientConfig = { redirects: [], plugins: [], @@ -99,7 +99,9 @@ export class PolywrapClient implements Client { tracingEnabled: !!config.tracingEnabled, }; - this._wrapperCache = config.wrapperCache ?? new Map(); + if (config.wrapperCache) { + this._wrapperCache = config.wrapperCache; + } } if (!options?.noDefaults) { From cb09621805224187965d59ee31cacad56807d9ad Mon Sep 17 00:00:00 2001 From: namesty Date: Thu, 14 Jul 2022 13:03:24 +0200 Subject: [PATCH 4/6] (lint): minor lint fix --- packages/js/client/src/PolywrapClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index caa3deeeaa..ef421335d8 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -61,7 +61,7 @@ export class PolywrapClient implements Client { // It should help us keep track of what URI's map to what Wrappers, // and handle cases where the are multiple jumps. For example, if // A => B => C, then the cache should have A => C, and B => C. - private _wrapperCache: WrapperCache = new Map();; + private _wrapperCache: WrapperCache = new Map(); private _config: PolywrapClientConfig = { redirects: [], plugins: [], From a2c2ef6437de5f2ef0b761f23b5700e2d4032ddd Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Tue, 23 Aug 2022 00:48:06 -0700 Subject: [PATCH 5/6] modify WrapperCache interface --- packages/js/client/src/PolywrapClient.ts | 14 +++++------ .../js/client/src/cache/SimpleWrapperCache.ts | 23 +++++++++++++++++++ packages/js/client/src/cache/index.ts | 1 + packages/js/client/src/index.ts | 2 ++ packages/js/client/src/plugin/index.ts | 1 + packages/js/core/src/types/WrapperCache.ts | 13 ++++------- .../resolvers/cache/CacheResolver.ts | 2 +- .../extendable/ExtendableUriResolver.ts | 4 ++-- 8 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 packages/js/client/src/cache/SimpleWrapperCache.ts create mode 100644 packages/js/client/src/cache/index.ts create mode 100644 packages/js/client/src/plugin/index.ts diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index fccb409922..bef4a10987 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -1,4 +1,5 @@ import { getDefaultClientConfig } from "./default-client-config"; +import { SimpleCache } from "./cache/SimpleWrapperCache"; import { v4 as uuid } from "uuid"; import { @@ -56,11 +57,7 @@ export interface PolywrapClientConfig } export class PolywrapClient implements Client { - // TODO: the Wrapper cache needs to be more like a routing table. - // It should help us keep track of what URI's map to what Wrappers, - // and handle cases where the are multiple jumps. For example, if - // A => B => C, then the cache should have A => C, and B => C. - private _wrapperCache: WrapperCache = new Map(); + private _wrapperCache: WrapperCache; private _config: PolywrapClientConfig = { redirects: [], plugins: [], @@ -100,6 +97,8 @@ export class PolywrapClient implements Client { if (config.wrapperCache) { this._wrapperCache = config.wrapperCache; + } else { + this._wrapperCache = new SimpleCache(); } } @@ -490,9 +489,8 @@ export class PolywrapClient implements Client { // Update cache for all URIs in the chain if (cacheWrite && wrapper) { - for (const item of uriHistory.getResolutionPath().stack) { - this._wrapperCache.set(item.sourceUri.uri, wrapper); - } + const uris = uriHistory.getResolutionPath().stack.map((x) => x.sourceUri); + this._wrapperCache.set(uris, wrapper); } if (shouldClearContext) { diff --git a/packages/js/client/src/cache/SimpleWrapperCache.ts b/packages/js/client/src/cache/SimpleWrapperCache.ts new file mode 100644 index 0000000000..72d2e00368 --- /dev/null +++ b/packages/js/client/src/cache/SimpleWrapperCache.ts @@ -0,0 +1,23 @@ +import { Uri, Wrapper, WrapperCache } from "@polywrap/core-js"; + +export class SimpleCache implements WrapperCache { + private _map: Map = new Map(); + + get(uri: Uri): Wrapper | undefined { + return this._map.get(uri.uri); + } + + has(uri: Uri): boolean { + return this._map.has(uri.uri); + } + + set(uris: Uri | Uri[], wrapper: Wrapper): void { + if (Array.isArray(uris)) { + for (const uri of uris) { + this._map.set(uri.uri, wrapper); + } + } else { + this._map.set(uris.uri, wrapper); + } + } +} diff --git a/packages/js/client/src/cache/index.ts b/packages/js/client/src/cache/index.ts new file mode 100644 index 0000000000..e3f5694e94 --- /dev/null +++ b/packages/js/client/src/cache/index.ts @@ -0,0 +1 @@ +export * from "./SimpleWrapperCache"; diff --git a/packages/js/client/src/index.ts b/packages/js/client/src/index.ts index f9784b93f9..91b5c1339a 100644 --- a/packages/js/client/src/index.ts +++ b/packages/js/client/src/index.ts @@ -2,4 +2,6 @@ export * from "./PolywrapClient"; export * from "./createPolywrapClient"; export * from "./default-client-config"; export * from "./wasm"; +export * from "./plugin"; +export * from "./cache"; export * from "@polywrap/core-js"; diff --git a/packages/js/client/src/plugin/index.ts b/packages/js/client/src/plugin/index.ts new file mode 100644 index 0000000000..b279540405 --- /dev/null +++ b/packages/js/client/src/plugin/index.ts @@ -0,0 +1 @@ +export * from "./PluginWrapper"; diff --git a/packages/js/core/src/types/WrapperCache.ts b/packages/js/core/src/types/WrapperCache.ts index 34e08696ca..05e5a79e76 100644 --- a/packages/js/core/src/types/WrapperCache.ts +++ b/packages/js/core/src/types/WrapperCache.ts @@ -1,11 +1,8 @@ -import { Wrapper } from "./Wrapper"; +import { Uri, Wrapper } from "."; export interface WrapperCache { - readonly size: number; - - clear(): void; - delete(key: string): boolean; - get(key: string): Wrapper | undefined; - has(key: string): boolean; - set(key: string, value: Wrapper): void; + get(uri: Uri): Wrapper | undefined; + has(uri: Uri): boolean; + set(uris: Uri[], wrapper: Wrapper): void; + set(uri: Uri, wrapper: Wrapper): void; } diff --git a/packages/js/core/src/uri-resolution/resolvers/cache/CacheResolver.ts b/packages/js/core/src/uri-resolution/resolvers/cache/CacheResolver.ts index ac556fd118..b2e6aa3bb2 100644 --- a/packages/js/core/src/uri-resolution/resolvers/cache/CacheResolver.ts +++ b/packages/js/core/src/uri-resolution/resolvers/cache/CacheResolver.ts @@ -11,7 +11,7 @@ export class CacheResolver implements UriResolver { client: Client, cache: WrapperCache ): Promise { - const wrapper = cache.get(uri.uri); + const wrapper = cache.get(uri); return Promise.resolve({ uri: uri, diff --git a/packages/js/core/src/uri-resolution/resolvers/extendable/ExtendableUriResolver.ts b/packages/js/core/src/uri-resolution/resolvers/extendable/ExtendableUriResolver.ts index a4e6b47065..e18eaca513 100644 --- a/packages/js/core/src/uri-resolution/resolvers/extendable/ExtendableUriResolver.ts +++ b/packages/js/core/src/uri-resolution/resolvers/extendable/ExtendableUriResolver.ts @@ -105,7 +105,7 @@ export class ExtendableUriResolver implements UriResolver { const implementationsToLoad = new Queue(); for (const implementationUri of implementationUris) { - if (!cache.has(implementationUri.uri)) { + if (!cache.has(implementationUri)) { implementationsToLoad.enqueue(implementationUri); } } @@ -148,7 +148,7 @@ export class ExtendableUriResolver implements UriResolver { }; } } else { - cache.set(implementationUri.uri, wrapper); + cache.set(implementationUri, wrapper); loadedImplementations.push(implementationUri.uri); failedAttempts = 0; } From 8cb0142723b60a7d1f9bac581234a33cb9bf5441 Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Tue, 23 Aug 2022 01:25:55 -0700 Subject: [PATCH 6/6] fix tests --- packages/js/client/src/PolywrapClient.ts | 8 +++++--- packages/js/client/src/index.ts | 1 - .../js/core/src/__tests__/resolveUri.spec.ts | 20 ++++++++++--------- .../src/cache/SimpleWrapperCache.ts | 2 +- .../js/{client => core}/src/cache/index.ts | 0 packages/js/core/src/index.ts | 1 + 6 files changed, 18 insertions(+), 14 deletions(-) rename packages/js/{client => core}/src/cache/SimpleWrapperCache.ts (88%) rename packages/js/{client => core}/src/cache/index.ts (100%) diff --git a/packages/js/client/src/PolywrapClient.ts b/packages/js/client/src/PolywrapClient.ts index bef4a10987..d9940f43af 100644 --- a/packages/js/client/src/PolywrapClient.ts +++ b/packages/js/client/src/PolywrapClient.ts @@ -1,5 +1,4 @@ import { getDefaultClientConfig } from "./default-client-config"; -import { SimpleCache } from "./cache/SimpleWrapperCache"; import { v4 as uuid } from "uuid"; import { @@ -46,6 +45,7 @@ import { PluginPackage, RunOptions, GetManifestOptions, + SimpleCache, } from "@polywrap/core-js"; import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js"; import { WrapManifest } from "@polywrap/wrap-manifest-types-js"; @@ -97,11 +97,13 @@ export class PolywrapClient implements Client { if (config.wrapperCache) { this._wrapperCache = config.wrapperCache; - } else { - this._wrapperCache = new SimpleCache(); } } + if (!this._wrapperCache) { + this._wrapperCache = new SimpleCache(); + } + if (!options?.noDefaults) { this._addDefaultConfig(); } diff --git a/packages/js/client/src/index.ts b/packages/js/client/src/index.ts index 91b5c1339a..8f11efe203 100644 --- a/packages/js/client/src/index.ts +++ b/packages/js/client/src/index.ts @@ -3,5 +3,4 @@ export * from "./createPolywrapClient"; export * from "./default-client-config"; export * from "./wasm"; export * from "./plugin"; -export * from "./cache"; export * from "@polywrap/core-js"; diff --git a/packages/js/core/src/__tests__/resolveUri.spec.ts b/packages/js/core/src/__tests__/resolveUri.spec.ts index 839a95892c..2d6d06a96b 100644 --- a/packages/js/core/src/__tests__/resolveUri.spec.ts +++ b/packages/js/core/src/__tests__/resolveUri.spec.ts @@ -23,6 +23,8 @@ import { SubscribeOptions, Subscription, PluginPackage, + SimpleCache, + GetManifestOptions, } from ".."; import { WrapManifest } from "@polywrap/wrap-manifest-types-js"; @@ -106,7 +108,7 @@ describe("resolveUri", () => { encoded: false }), getFile: (options: GetFileOptions, client: Client) => Promise.resolve(""), - getManifest: (client: Client) => Promise.resolve({} as WrapManifest) + getManifest: (options: GetManifestOptions, client: Client) => Promise.resolve({} as WrapManifest) }; }; @@ -238,7 +240,7 @@ describe("resolveUri", () => { new Uri("ens/test.eth"), uriResolvers, client(wrappers, plugins, interfaces), - new Map(), + new SimpleCache(), ); expect(result.wrapper).toBeTruthy(); @@ -260,7 +262,7 @@ describe("resolveUri", () => { new Uri("my/something-different"), uriResolvers, client(wrappers, plugins, interfaces), - new Map(), + new SimpleCache(), ); expect(result.wrapper).toBeTruthy(); @@ -282,7 +284,7 @@ describe("resolveUri", () => { new Uri("ens/ens"), uriResolvers, client(wrappers, plugins, interfaces), - new Map(), + new SimpleCache(), ); expect(result.wrapper).toBeTruthy(); @@ -304,7 +306,7 @@ describe("resolveUri", () => { new Uri("my/something-different"), uriResolvers, client(wrappers, plugins, interfaces), - new Map(), + new SimpleCache(), ); expect(result.wrapper).toBeTruthy(); @@ -339,7 +341,7 @@ describe("resolveUri", () => { new Uri("some/wrapper"), uriResolvers, client(wrappers, plugins, interfaces, circular), - new Map(), + new SimpleCache(), ).catch((e: Error) => expect(e.message).toMatch(/Infinite loop while resolving URI/) ); @@ -363,7 +365,7 @@ describe("resolveUri", () => { new Uri("some/wrapper"), uriResolvers, client(wrappers, plugins, interfaces, missingFromProperty), - new Map(), + new SimpleCache(), ).catch((e: Error) => expect(e.message).toMatch( "Redirect missing the from property.\nEncountered while resolving wrap://some/wrapper" @@ -387,7 +389,7 @@ describe("resolveUri", () => { new Uri("some/wrapper"), uriResolvers, client(wrappers, pluginRegistrations, interfaces), - new Map(), + new SimpleCache(), ); expect(result.wrapper).toBeTruthy(); @@ -425,7 +427,7 @@ describe("resolveUri", () => { plugins, interfaces ), - new Map(), + new SimpleCache(), ); expect(resolvedUri).toEqual(uri); diff --git a/packages/js/client/src/cache/SimpleWrapperCache.ts b/packages/js/core/src/cache/SimpleWrapperCache.ts similarity index 88% rename from packages/js/client/src/cache/SimpleWrapperCache.ts rename to packages/js/core/src/cache/SimpleWrapperCache.ts index 72d2e00368..71948bea40 100644 --- a/packages/js/client/src/cache/SimpleWrapperCache.ts +++ b/packages/js/core/src/cache/SimpleWrapperCache.ts @@ -1,4 +1,4 @@ -import { Uri, Wrapper, WrapperCache } from "@polywrap/core-js"; +import { Uri, Wrapper, WrapperCache } from "../types"; export class SimpleCache implements WrapperCache { private _map: Map = new Map(); diff --git a/packages/js/client/src/cache/index.ts b/packages/js/core/src/cache/index.ts similarity index 100% rename from packages/js/client/src/cache/index.ts rename to packages/js/core/src/cache/index.ts diff --git a/packages/js/core/src/index.ts b/packages/js/core/src/index.ts index 774ca2e6fe..b40a7aeb26 100644 --- a/packages/js/core/src/index.ts +++ b/packages/js/core/src/index.ts @@ -1,5 +1,6 @@ export * from "./types"; export * from "./algorithms"; +export * from "./cache"; export * from "./interfaces"; export * from "./uri-resolution/core"; export * from "./uri-resolution/resolvers";