-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [x] Add IPFS service - [x] Add Cron job integration - [x] Use esm for jest
- Loading branch information
Showing
19 changed files
with
3,847 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"testTimeout": 900000, | ||
"moduleFileExtensions": ["js", "json", "ts"], | ||
"rootDir": ".", | ||
"roots": ["<rootDir>/ts", "<rootDir>/tests"], | ||
"testRegex": ".*\\.test\\.ts$", | ||
"transform": { | ||
"^.+\\.(t|j)s$": [ | ||
"ts-jest", | ||
{ | ||
"useESM": true | ||
} | ||
] | ||
}, | ||
"preset": "ts-jest/presets/default-esm", | ||
"moduleNameMapper": { | ||
"^(\\.{1,2}/.*)\\.[jt]s$": "$1" | ||
}, | ||
"collectCoverageFrom": [ | ||
"**/*.(t|j)s", | ||
"!<rootDir>/ts/main.ts", | ||
"!<rootDir>/ts/jest/*.js", | ||
"!<rootDir>/hardhat.config.js" | ||
], | ||
"coveragePathIgnorePatterns": ["<rootDir>/ts/sessionKeys/__tests__/utils.ts"], | ||
"coverageDirectory": "<rootDir>/coverage", | ||
"testEnvironment": "node" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { IpfsService } from "../ipfs.service"; | ||
|
||
describe("IpfsService", () => { | ||
const defaultData = { hello: "world" }; | ||
|
||
test("should not add or get data if adapter is not initialized", async () => { | ||
const service = new IpfsService(); | ||
|
||
await expect(service.add(defaultData)).rejects.toThrow("IPFS adapter is not initialized"); | ||
await expect(service.get("cid")).rejects.toThrow("IPFS adapter is not initialized"); | ||
}); | ||
|
||
test("should add data and get data properly", async () => { | ||
const service = new IpfsService(); | ||
await service.init(); | ||
|
||
const cid = await service.add(defaultData); | ||
const data = await service.get(cid); | ||
|
||
expect(data).toStrictEqual(defaultData); | ||
}); | ||
}); |
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,9 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { IpfsService } from "./ipfs.service"; | ||
|
||
@Module({ | ||
exports: [IpfsService], | ||
providers: [IpfsService], | ||
}) | ||
export class IpfsModule {} |
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,70 @@ | ||
import { Injectable, Logger } from "@nestjs/common"; | ||
|
||
import type { JSON as JsonAdapter } from "@helia/json"; | ||
|
||
/** | ||
* IpfsService is responsible for saving data to ipfs | ||
*/ | ||
@Injectable() | ||
export class IpfsService { | ||
/** | ||
* Logger | ||
*/ | ||
private readonly logger: Logger = new Logger(IpfsService.name); | ||
|
||
/** | ||
* IPFS adapter | ||
*/ | ||
private adapter?: JsonAdapter; | ||
|
||
/** | ||
* Initialize IpfsService | ||
*/ | ||
async init(): Promise<void> { | ||
if (!this.adapter) { | ||
const { createHelia } = await import("helia"); | ||
const { json } = await import("@helia/json"); | ||
|
||
const helia = await createHelia(); | ||
this.adapter = json(helia); | ||
} | ||
} | ||
|
||
/** | ||
* Add data to IPFS and return the CID | ||
* | ||
* @param data data to be added to IPFS | ||
* @returns cid | ||
*/ | ||
async add<T>(data: T): Promise<string> { | ||
this.checkAdapter(); | ||
|
||
return this.adapter!.add(data).then((cid) => cid.toString()); | ||
} | ||
|
||
/** | ||
* Get data from IPFS | ||
* | ||
* @param cid CID of the data to be fetched from IPFS | ||
* @returns data | ||
*/ | ||
async get<T>(cid: string): Promise<T> { | ||
this.checkAdapter(); | ||
|
||
const { CID } = await import("multiformats"); | ||
|
||
return this.adapter!.get<T>(CID.parse(cid)); | ||
} | ||
|
||
/** | ||
* Check if IPFS adapter is initialized | ||
* | ||
* @throws Error if IPFS adapter is not initialized | ||
*/ | ||
private checkAdapter(): void { | ||
if (!this.adapter) { | ||
this.logger.error("IPFS adapter is not initialized"); | ||
throw new Error("IPFS adapter is not initialized"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.