*Initial version

This commit is contained in:
Derrick Hammer 2022-08-05 09:34:50 -04:00
parent 758b4b1be0
commit 0ecd526fa0
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 134 additions and 1 deletions

View File

@ -1,6 +1,6 @@
MIT License 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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# kernel-ipfs-http-client
IPFS client interface to @lumeweb/kernel-ipfs-http module

16
package.json Normal file
View File

@ -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"
}
}

92
src/index.ts Normal file
View File

@ -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);
}

23
tsconfig.json Normal file
View File

@ -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"
]
}