-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(deps): replace ipfs-http-client with kubo-rpc-client
- Replace deprecated ipfs-http-client with kubo-rpc-client. - kubo-rpc-client must be imported dynamically since it's ESM-only and we still use CJS. Peter's additional changes: --------------------------- build(typescript): project-wide fixes to allow us to use ESM-only deps Apologies for the huge diff, this can't be broken down to smaller changes that would still compile because of cross-package dependencies. I realize that this change is not exactly the optimal solution, but it is probably a step in the right direction. If I somehow found the time to submit pull requests to the libraries that I needed to fork and re-publish (see details below) and then get the changes onto the upstream and get them released as the official packages, then we could (in theory) arrive at a solution that is the recommended way of fixing these problems (apart from going full ESM-only) This work stands on the shoulders of the previous commits from @outSH and takes a slightly different direction compared to what we've been talking about earlier on account of the problem that the eval-import workaround causes crashes in Jest. Based on the above I went through the following adventures: 1. I migrated the build system of kubo-rpc-client myself so that it correctly exports CJS and ESM and typings for both of those as well, I put that code on my fork [1] and then published it onto npm as well [2] After this, I was hoping that now we could just import the package in our CJS code without issues, but what really happened is that instead of crashing at the require call that pull in kubo itself, it started crashing deeper in the require stack where kubo itself was requiring it's own ESM only dependencies (of which there seem to be at least 10 or 15). At this point I realized that me migrating and self-publishing all of these additional packages might not be worth the effort and started looking for something easier. 2. I gave dynamic imports + moduleResultion=Node16 as my next attempt to get our build back to working order. With this, the kubo-rpc-client can now be imported dynamically without issues in packages that declare themselves as resolving modules as "Node16" in their tsconfig.json Other issues here were encountered because not all of our ESM only packages are used in a way that they can be imported dynamically (for example if their types are part of our own types or are being re-exported). The two libraries with this problem were `run-time-error` and `socket.io-client` for both of which I ended up going through the same treatment as for kubo-rpc-client above (but this time my effort wasn't) in vain. They work and so I did some search and replace in the entire codebase to use these re-published packages with the correct types: [3] [4] [5] [6] 3. After this the project build was working, but Jest was still failing with compiler errors which I determined to happen because it uses the root tsconfig.json file for it's internal TS compilation and that root tsconfig.json file was not setting module resolution to Node16. 4. After fixing that the final hurdle (hopefully) was to ensure that jest gets execued with the custom node option as below: NODE_OPTIONS=--experimental-vm-modules yarn jest [1] https://github.com/petermetz/js-kubo-rpc-client-esm-cjs [2] https://www.npmjs.com/package/kubo-rpc-client-esm-cjs [3] https://github.com/petermetz/socket.io-client [4] https://www.npmjs.com/package/socket.io-client-fixed-types [5] https://github.com/petermetz/RuntimeError [6] https://www.npmjs.com/package/run-time-error-cjs Huge thanks for https://arethetypeswrong.github.io/ a tool I used extensively to create the fixes for the libraries above. One more thing that I tried just to collect more data points was to set the moduleResultion project-wide to Node16 via setting it in the root tsconfig.base.json but this broke the compiler itself, as in, there is a bug in the Typescript compiler in v4.x as seen here: microsoft/TypeScript#51221 So this is one more reason for us to upgrade to 5.x as soon as possible. I also needed to add "run-time-error" to the root package.json as a dependency because it was accidentally providing that to some sub-packages and when we moved to "run-time-error-cjs" the tests that install plugins from npm started failing (because those releases are still using "run-time-error" and not "run-time-error-cjs") ------------------------------------ Fixes #2807 Fixes #2852 Depends on: #2821 Co-authored-by: Peter Somogyvari <[email protected]> Signed-off-by: Peter Somogyvari <[email protected]> Signed-off-by: Michal Bajer <[email protected]>
- Loading branch information
Showing
110 changed files
with
487 additions
and
454 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
extensions/cactus-plugin-object-store-ipfs/src/main/typescript/i-ipfs-http-client.ts
This file was deleted.
Oops, something went wrong.
133 changes: 133 additions & 0 deletions
133
extensions/cactus-plugin-object-store-ipfs/src/main/typescript/kubo-rpc-client-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Since kubo-rpc-client uses ESM only, we can't import it to get types (since we use CJS). | ||
* To fix this we define required types here, based on their counterparts in kubo-rpc-client. | ||
*/ | ||
|
||
// @ts-ignore | ||
import type { Multiaddr } from "@multiformats/multiaddr"; | ||
// @ts-ignore | ||
import type { MultihashHasher } from "multiformats/hashes/interface"; | ||
import type { Agent as HttpAgent } from "http"; | ||
import type { Agent as HttpsAgent } from "https"; | ||
// @ts-ignore | ||
import type { CID } from "multiformats/cid"; | ||
// @ts-ignore | ||
import type { Mtime } from "ipfs-unixfs"; | ||
|
||
///////////////////////////////////// | ||
// Types from kubo-rpc-client | ||
///////////////////////////////////// | ||
// Some are simplified when details are not needed | ||
|
||
export type MultibaseCodec<Prefix extends string = any> = | ||
// @ts-ignore | ||
import("multiformats/bases/interface").MultibaseCodec<Prefix>; | ||
export type BlockCodec< | ||
T1 = any, | ||
T2 = any, | ||
// @ts-ignore | ||
> = import("multiformats/codecs/interface").BlockCodec<T1, T2>; | ||
|
||
export interface LoadBaseFn { | ||
(codeOrName: number | string): Promise<MultibaseCodec<any>>; | ||
} | ||
export interface LoadCodecFn { | ||
(codeOrName: number | string): Promise<BlockCodec<any, any>>; | ||
} | ||
export interface LoadHasherFn { | ||
(codeOrName: number | string): Promise<MultihashHasher>; | ||
} | ||
|
||
export interface IPLDOptions { | ||
loadBase: LoadBaseFn; | ||
loadCodec: LoadCodecFn; | ||
loadHasher: LoadHasherFn; | ||
bases: Array<MultibaseCodec<any>>; | ||
codecs: Array<BlockCodec<any, any>>; | ||
hashers: MultihashHasher[]; | ||
} | ||
|
||
export interface Options { | ||
host?: string; | ||
port?: number; | ||
protocol?: string; | ||
headers?: Headers | Record<string, string>; | ||
timeout?: number | string; | ||
apiPath?: string; | ||
url?: URL | string | Multiaddr; | ||
ipld?: Partial<IPLDOptions>; | ||
agent?: HttpAgent | HttpsAgent; | ||
} | ||
|
||
export type IPFSPath = CID | string; | ||
|
||
export interface StatResult { | ||
cid: CID; | ||
size: number; | ||
cumulativeSize: number; | ||
type: "directory" | "file"; | ||
blocks: number; | ||
withLocality: boolean; | ||
local?: boolean; | ||
sizeLocal?: number; | ||
mode?: number; | ||
mtime?: Mtime; | ||
} | ||
|
||
///////////////////////////////////////////////////////// | ||
// LikeIpfsHttpClient instead of full IpfsHttpClient | ||
///////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Connector only needs these methods to work. | ||
* More methods can be added in the future. | ||
*/ | ||
export interface LikeIpfsHttpClientFile { | ||
read: ( | ||
ipfsPath: IPFSPath, | ||
options?: Record<string, unknown>, | ||
) => AsyncIterable<Uint8Array>; | ||
|
||
write: ( | ||
ipfsPath: string, | ||
content: | ||
| string | ||
| Uint8Array | ||
| Blob | ||
| AsyncIterable<Uint8Array> | ||
| Iterable<Uint8Array>, | ||
options?: Record<string, unknown>, | ||
) => Promise<void>; | ||
|
||
stat: ( | ||
ipfsPath: IPFSPath, | ||
options?: Record<string, unknown>, | ||
) => Promise<StatResult>; | ||
} | ||
|
||
export function isLikeIpfsHttpClientFile( | ||
x: unknown, | ||
): x is LikeIpfsHttpClientFile { | ||
if (!x) { | ||
return false; | ||
} | ||
return ( | ||
typeof (x as LikeIpfsHttpClientFile).read === "function" && | ||
typeof (x as LikeIpfsHttpClientFile).write === "function" && | ||
typeof (x as LikeIpfsHttpClientFile).stat === "function" | ||
); | ||
} | ||
|
||
/** | ||
* Only files API is used | ||
*/ | ||
export interface LikeIpfsHttpClient { | ||
files: LikeIpfsHttpClientFile; | ||
} | ||
|
||
export function isLikeIpfsHttpClient(x: unknown): x is LikeIpfsHttpClient { | ||
if (!x) { | ||
return false; | ||
} | ||
return isLikeIpfsHttpClientFile((x as LikeIpfsHttpClient).files); | ||
} |
Oops, something went wrong.