Skip to content

Commit cf4adfe

Browse files
authoredNov 4, 2021
Merge pull request #1 from konomi-network/feature/KON-201
+ add decentralized-fs module that support save and find like db orm
2 parents aa42ebe + 0107c27 commit cf4adfe

34 files changed

+36371
-1
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ typings/
8080

8181
# Nuxt.js build / generate output
8282
.nuxt
83-
dist
83+
dist/
84+
lib/
8485

8586
# Gatsby files
8687
.cache/

‎README.md

+30
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
11
# typescript-common
22
Typescript common util library for both Frontend and Backend usage
3+
4+
### Installation
5+
6+
1. Install yarn globally (needed to resolve dependencies correctly when working in a monorepo)
7+
8+
```shell
9+
npm install -g yarn
10+
```
11+
12+
2. Install NPM packages
13+
14+
```shell
15+
yarn install
16+
```
17+
18+
3. Run all the examples
19+
20+
```shell
21+
yarn run test
22+
```
23+
24+
4. before run ipfs daemon
25+
```
26+
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST", "OPTIONS"]'
27+
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
28+
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
29+
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
30+
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
31+
ipfs daemon
32+
```

‎build.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"target": "es6",
5+
"allowJs": false,
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"esModuleInterop": true,
9+
"declaration": true,
10+
"declarationMap": true,
11+
"removeComments": false,
12+
"strict": true,
13+
"noImplicitAny": true,
14+
"noImplicitReturns": true,
15+
"noFallthroughCasesInSwitch": true,
16+
"noUnusedParameters": true,
17+
"noUnusedLocals": true,
18+
"skipLibCheck": true,
19+
"forceConsistentCasingInFileNames": true,
20+
"allowSyntheticDefaultImports": true,
21+
"experimentalDecorators": true,
22+
"lib": ["es2018", "esnext.asynciterable", "DOM"],
23+
"baseUrl": "."
24+
},
25+
"exclude": [
26+
"node_modules",
27+
"**/__mocks__/*",
28+
"**/__tests__/*",
29+
"**/*.spec.ts",
30+
"**/*.test.ts"
31+
]
32+
}

‎configure-references.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
// @ts-check
4+
/* eslint-disable */
5+
6+
const fs = require('fs');
7+
const util = require('util');
8+
const exec = util.promisify(require('child_process').exec);
9+
const path = require('path');
10+
const isCI = require('is-ci');
11+
12+
const config = JSON.parse(fs.readFileSync('tsconfig.json').toString());
13+
config.files = [];
14+
config.references = [];
15+
16+
(async function() {
17+
if (isCI) {
18+
// dont run it on CI
19+
return;
20+
}
21+
22+
const { stdout, stderr } = await exec('yarn workspaces info --json');
23+
24+
const lines = stdout.split('\n');
25+
const depthTree = lines.slice(1, lines.length - 2).join('\n');
26+
const workspaces = JSON.parse(depthTree);
27+
28+
for (const name in workspaces) {
29+
const workspace = workspaces[name];
30+
const location = path.resolve(process.cwd(), workspace.location);
31+
const tsconfigPath = path.resolve(location, 'tsconfig.json');
32+
if (fs.existsSync(tsconfigPath)) {
33+
config.references.push({
34+
path: workspace.location,
35+
});
36+
const workspaceConfig = JSON.parse(
37+
fs.readFileSync(tsconfigPath).toString(),
38+
);
39+
workspaceConfig.compilerOptions.composite = true;
40+
workspaceConfig.references = [];
41+
for (const dependency of workspace.workspaceDependencies) {
42+
const dependecyLocation = path.resolve(
43+
process.cwd(),
44+
workspaces[dependency].location,
45+
);
46+
if (
47+
fs.existsSync(
48+
path.resolve(dependecyLocation, 'tsconfig.json'),
49+
)
50+
) {
51+
workspaceConfig.references.push({
52+
path: path.relative(location, dependecyLocation),
53+
});
54+
}
55+
}
56+
fs.writeFileSync(
57+
tsconfigPath,
58+
JSON.stringify(workspaceConfig, undefined, 4),
59+
);
60+
}
61+
}
62+
fs.writeFileSync('tsconfig.json', JSON.stringify(config, undefined, 4));
63+
})();
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

‎examples/browser-nextjs/.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel

‎examples/browser-nextjs/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
```
12+
13+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
15+
You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.
16+
17+
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.
18+
19+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { useState, useEffect } from "react";
2+
import DecentralizedFileStorage from "@konomi/decentralized-fs/dist/decentralized-fs";
3+
4+
const IpfsComponent = () => {
5+
const [id, setId] = useState(null);
6+
const [ipfs, setIpfs] = useState(null);
7+
const [version, setVersion] = useState(null);
8+
const [isOnline, setIsOnline] = useState(false);
9+
const [cid, setCID] = useState(null);
10+
const [content, setContent] = useState(null);
11+
12+
useEffect(() => {
13+
const init = async () => {
14+
if (ipfs) return;
15+
16+
const dfs = new DecentralizedFileStorage("http://localhost:5002");
17+
18+
const dfsId = await dfs.id();
19+
const dfsVersion = await dfs.version();
20+
const dfsIsOnline = dfs.isOnline;
21+
22+
setIpfs(dfs);
23+
setId(dfsId.id);
24+
setVersion(dfsVersion.version);
25+
setIsOnline(dfsIsOnline);
26+
27+
const mockData = {
28+
symbol: "kono",
29+
slug: "konomi",
30+
client: 0,
31+
aggregationStrategy: 1,
32+
sources: [
33+
{
34+
type: 3, // for uniswap
35+
detail: {
36+
address: "0x...",
37+
},
38+
},
39+
{
40+
type: 2, // coinmarcketcap
41+
detail: {
42+
coinId: "2",
43+
},
44+
},
45+
],
46+
};
47+
48+
const cid = await dfs.save(JSON.stringify(mockData));
49+
setCID(cid);
50+
51+
const content = await dfs.find(cid);
52+
setContent(Uint8ArrayToString(content.split(",")));
53+
};
54+
55+
init();
56+
}, [ipfs]);
57+
58+
const Uint8ArrayToString = (u8aStr: number[]) => {
59+
var dataString = "";
60+
for (const v of u8aStr) {
61+
dataString += String.fromCharCode(v);
62+
}
63+
return dataString;
64+
};
65+
66+
if (!ipfs) {
67+
return "<h4>Connecting to IPFS...</h4>";
68+
}
69+
70+
return (
71+
<div className="hello">
72+
<div className="greeting">
73+
<h4 data-test="id">Id: {id}</h4>
74+
<h4 data-test="version">Version: {version}</h4>
75+
<h4 data-test="status">Status: {isOnline ? "Online" : "Offline"}</h4>
76+
<h4 data-test="cid">CID: {cid}</h4>
77+
<h4 data-test="content">content: {content}</h4>
78+
</div>
79+
</div>
80+
);
81+
};
82+
83+
export default IpfsComponent;

‎examples/browser-nextjs/next-env.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/types/global" />
3+
/// <reference types="next/image-types/global" />
4+
5+
// NOTE: This file should not be edited
6+
// see https://nextjs.org/docs/basic-features/typescript for more information.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @type {import('next').NextConfig} */
2+
module.exports = {
3+
reactStrictMode: true,
4+
// https://github.com/vercel/next.js/issues/21079
5+
// Remove the workaround the issue is fixed
6+
images: {
7+
loader: "imgix",
8+
path: "",
9+
}
10+
}

0 commit comments

Comments
 (0)