From 0ecd526fa04c1ccc73ac3b8b7d41a64c73680953 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 5 Aug 2022 09:34:50 -0400 Subject: [PATCH] *Initial version --- LICENSE | 2 +- README.md | 2 ++ package.json | 16 +++++++++ src/index.ts | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 23 +++++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/LICENSE b/LICENSE index 3f80f0d..8995c8b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Derrick Hammer +Copyright (c) 2022 Hammer Technologies LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md new file mode 100644 index 0000000..d099b0b --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# kernel-ipfs-http-client +IPFS client interface to @lumeweb/kernel-ipfs-http module diff --git a/package.json b/package.json new file mode 100644 index 0000000..2b6180f --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "@lumeweb/kernel-ipfs-client", + "version": "0.1.0", + "type": "module", + "main": "dist/index.js", + "dependencies": { + "is-ipfs": "^6.0.2", + "libkernel": "^0.1.43", + "libkmodule": "^0.2.44" + }, + "devDependencies": { + "@types/node": "^18.0.6", + "libskynet": "^0.0.62", + "prettier": "^2.7.1" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..25953cb --- /dev/null +++ b/src/index.ts @@ -0,0 +1,92 @@ +import type { DataFn } from "libskynet"; +import { ipnsPath, ipfsPath } from "is-ipfs"; + +const IPFS_MODULE = "AQDr2iGYEiMKIdb14w7dxwxFYBo3LaYc0mAuRKXsF2w9OQ"; + +let callModule: any, connectModule: any; + +async function loadLibs() { + if (callModule && connectModule) { + return; + } + if (typeof window !== "undefined" && window?.document) { + const pkg = await import("libkernel"); + callModule = pkg.callModule; + connectModule = pkg.connectModule; + } else { + const pkg = await import("libkmodule"); + callModule = pkg.callModule; + connectModule = pkg.connectModule; + } +} + +export async function refreshGatewayList() { + const [resp, err] = await doCall("refreshGatewayList"); + + if (err) { + throw new Error(err); + } + + return resp; +} + +export async function fetchIpfs( + hash: string, + path = "", + headers = {}, + receiveUpdate: DataFn +) { + if (!ipfsPath(`/ipfs/{${hash}`)) { + throw new Error("Invalid hash"); + } + return doFetch("fetchIpfs", { hash, path, headers }, receiveUpdate); +} + +export async function statIpfs(hash: string, path = "", headers = {}) { + if (!ipfsPath(`/ipfs/{${hash}`)) { + throw new Error("Invalid hash"); + } + return doFetch("statIpfs", { hash, path, headers }); +} + +export async function fetchIpns( + hash: string, + path = "", + headers = {}, + receiveUpdate: DataFn +) { + if (!ipnsPath(`/ipns/{${hash}`)) { + throw new Error("Invalid hash"); + } + return doFetch("fetchIpns", { hash, path, headers }, receiveUpdate); +} + +export async function statIpns(hash: string, path = "", headers = {}) { + if (!ipnsPath(`/ipns/{${hash}`)) { + throw new Error("Invalid hash"); + } + return doFetch("statIpns", { hash, path, headers }); +} + +async function doFetch(method: string, data: any, receiveUpdate?: DataFn) { + let [resp, err] = await doCall(method, data, receiveUpdate); + + if (typeof err?.then === "function") { + [resp, err] = await err; + } + + if (err) { + throw new Error(err); + } + + return resp; +} + +async function doCall(method: string, data?: any, receiveUpdate?: DataFn) { + await loadLibs(); + if (receiveUpdate) { + return connectModule(IPFS_MODULE, method, data, receiveUpdate); + } + + return callModule(IPFS_MODULE, method, data); +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a4d5cd4 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "declaration": true, + "strict": true, + "module": "esnext", + "target": "esnext", + "esModuleInterop": true, + "sourceMap": false, + "rootDir": "src", + "outDir": "dist", + "typeRoots": [ + "node_modules/@types", + ], + "moduleResolution": "node", + "declarationMap": true, + "declarationDir": "dist", + "emitDeclarationOnly": false, + "allowJs": true + }, + "include": [ + "src" + ] +}