mirror of
https://github.com/SunnyQjm/test-typed-ndn-js.git
synced 2026-10-05 01:15:39 +08:00
init
This commit is contained in:
1 parent
efa881511f
commit
d8949b23f6
12 files changed
+400
No files matched your search
@@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
ts/*.js
|
||||
package-lock.json
|
||||
@@ -0,0 +1,10 @@
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.content {
|
||||
/*max-width: 800px;*/
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello World!</title>
|
||||
<!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
|
||||
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
|
||||
<link rel="stylesheet" href="index.css" type="text/css">
|
||||
<script>
|
||||
/**
|
||||
* 以下脚本解决在Electron中不能使用jQuery的问题
|
||||
*/
|
||||
window.nodeRequire = require;
|
||||
delete window.require;
|
||||
delete window.exports;
|
||||
delete window.module;
|
||||
</script>
|
||||
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
|
||||
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
|
||||
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
|
||||
crossorigin="anonymous"></script>
|
||||
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';"/>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content">
|
||||
<h2>Producer</h2>
|
||||
<input id="responseContent" type="text" class="form-control" placeholder="在此处要返回的Data中包含的字符串" value="hello"/>
|
||||
<div class="input-group" style="margin-top: 20px">
|
||||
<input id="registerPrefix" type="text" class="form-control" placeholder="prefix, eg. /test" value="/test">
|
||||
<span class="input-group-btn">
|
||||
<button class="btn btn-default" type="button" onclick="doRegister(this)">注册</button>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<h2>Consumer</h2>
|
||||
|
||||
<div class="input-group" style="margin-top: 20px">
|
||||
<input id="expressPrefix" type="text" class="form-control" placeholder="prefix, eg. /test" value="/test">
|
||||
<span class="input-group-btn">
|
||||
<button class="btn btn-default" type="button" onclick="expressInterest(this)">expressInterest</button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
const {
|
||||
Face,
|
||||
KeyChain,
|
||||
PibMemory,
|
||||
TpmBackEndMemory,
|
||||
Data,
|
||||
SafeBag,
|
||||
Blob,
|
||||
Name
|
||||
} = nodeRequire('typed-ndn-js');
|
||||
const fs = nodeRequire('fs');
|
||||
|
||||
function doRegister(element) {
|
||||
const face = new Face({
|
||||
host: "localhost",
|
||||
port: 6363
|
||||
});
|
||||
const content = $('#responseContent').val();
|
||||
const keyChain = new KeyChain(new PibMemory(), new TpmBackEndMemory());
|
||||
// import safe bag
|
||||
keyChain.importSafeBag(new SafeBag(new Blob(fs.readFileSync("./qjm.safebag"))));
|
||||
face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
|
||||
face.registerPrefix(new Name($('#registerPrefix').val()), (prefix, interest, face, interestFilterId, filter) => {
|
||||
console.log(`receive interest: ` + interest.toUri());
|
||||
const data = new Data(interest.getName());
|
||||
data.setContent(content);
|
||||
console.log(`put content: ` + content, data.getContent().toString());
|
||||
keyChain.sign(data);
|
||||
face.putData(data);
|
||||
}, prefix => {
|
||||
alert(`注册前缀失败: ${prefix}`)
|
||||
console.log("register failed");
|
||||
}, (prefix, registeredPrefixId) => {
|
||||
alert(`注册前缀成功: ${prefix}`)
|
||||
console.log("register success");
|
||||
})
|
||||
}
|
||||
|
||||
function expressInterest() {
|
||||
// expressPrefix
|
||||
const {
|
||||
Face,
|
||||
Interest,
|
||||
Data,
|
||||
Name
|
||||
} = nodeRequire('typed-ndn-js');
|
||||
|
||||
const face = new Face({
|
||||
host: "127.0.0.1",
|
||||
port: 6363
|
||||
});
|
||||
|
||||
const interest = new Interest(new Name($('#expressPrefix').val()));
|
||||
console.log(`expressInterest: ${interest.toUri()}`);
|
||||
face.expressInterest(interest, (interest, data) => { // onData
|
||||
console.log(`Receive data: ${data.getContent().toString()}`);
|
||||
alert(`Receive data: ${data.getContent().toString()}`);
|
||||
face.close();
|
||||
}, interest => { // onTimeout
|
||||
alert(`expressInterest timeout: ${interest.toUri()}`)
|
||||
console.log('onTimeout');
|
||||
}, (interest, networkNack) => { // onNack
|
||||
console.log(`onNack: ${networkNack.reason}`);
|
||||
alert(`onNack: ${networkNack.reason}`);
|
||||
})
|
||||
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
@@ -0,0 +1,22 @@
|
||||
const {
|
||||
Face,
|
||||
Interest,
|
||||
Data,
|
||||
Name
|
||||
} = require('typed-ndn-js');
|
||||
|
||||
const face = new Face({
|
||||
host: "127.0.0.1",
|
||||
port: 6363
|
||||
});
|
||||
|
||||
const interest = new Interest(new Name('/test'));
|
||||
face.expressInterest(interest, (interest, data) => { // onData
|
||||
console.log(`Receive data: ${data.getContent().toString()}`);
|
||||
face.close();
|
||||
}, interest => { // onTimeout
|
||||
console.log('onTimeout');
|
||||
}, (interest, networkNack) => { // onNack
|
||||
console.log(`onNack: ${networkNack.reason}`);
|
||||
})
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const {
|
||||
Face,
|
||||
KeyChain,
|
||||
PibMemory,
|
||||
TpmBackEndMemory,
|
||||
Data,
|
||||
SafeBag,
|
||||
Blob,
|
||||
Name
|
||||
} = require('typed-ndn-js');
|
||||
const fs = require('fs');
|
||||
|
||||
const face = new Face({
|
||||
host: "localhost",
|
||||
port: 6363
|
||||
});
|
||||
const keyChain = new KeyChain(new PibMemory(), new TpmBackEndMemory());
|
||||
|
||||
// import safe bag
|
||||
keyChain.importSafeBag(new SafeBag(new Blob(fs.readFileSync("./qjm.safebag"))));
|
||||
|
||||
face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
|
||||
face.registerPrefix(new Name('/test'), (prefix, interest, face, interestFilterId, filter) => {
|
||||
console.log(`receive interest: ` + interest.toUri());
|
||||
const data = new Data(interest.getName());
|
||||
data.setContent("hello");
|
||||
keyChain.sign(data);
|
||||
face.putData(data);
|
||||
}, prefix => {
|
||||
console.log("register failed");
|
||||
}, (prefix, registeredPrefixId) => {
|
||||
console.log("register success");
|
||||
})
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
const {
|
||||
app, BrowserWindow
|
||||
} = require('electron');
|
||||
// require("./js/producer")
|
||||
|
||||
function createWindow() {
|
||||
// 创建浏览器窗口
|
||||
let win = new BrowserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
webPreferences: {
|
||||
nodeIntegration: true
|
||||
}
|
||||
})
|
||||
|
||||
// 加载index.html文件
|
||||
win.loadFile('index.html')
|
||||
|
||||
// 打开开发者工具
|
||||
win.webContents.openDevTools()
|
||||
}
|
||||
|
||||
// Electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法
|
||||
// 部分 API 在 ready 事件触发后才能使用。
|
||||
app.whenReady().then(createWindow)
|
||||
|
||||
//当所有窗口都被关闭后退出
|
||||
app.on('window-all-closed', () => {
|
||||
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
|
||||
// 否则绝大部分应用及其菜单栏会保持激活。
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit()
|
||||
}
|
||||
})
|
||||
|
||||
app.on('activate', () => {
|
||||
// 在macOS上,当单击dock图标并且没有其他窗口打开时,
|
||||
// 通常在应用程序中重新创建一个窗口。
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow()
|
||||
}
|
||||
})
|
||||
|
||||
// 您可以把应用程序其他的流程写在在此文件中
|
||||
// 代码 也可以拆分成几个文件,然后用 require 导入。
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "test-ndn-js2",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"producer_ts": "tsc ts/producer.ts && node ts/producer.js",
|
||||
"consumer_ts": "tsc ts/consumer.ts && node ts/consumer.js",
|
||||
"build": "webpack",
|
||||
"start": "electron ."
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"rsa-keypair": "^2.0.2",
|
||||
"sqlite3": "^5.0.0",
|
||||
"typed-ndn-js": "^0.19.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"electron": "^9.1.1",
|
||||
"ts-loader": "^8.0.1",
|
||||
"typescript": "^3.9.7",
|
||||
"webpack": "^4.44.0",
|
||||
"webpack-cli": "^3.3.12"
|
||||
}
|
||||
}
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,19 @@
|
||||
import {
|
||||
Face,
|
||||
Interest,
|
||||
Name
|
||||
} from 'typed-ndn-js';
|
||||
|
||||
const face = new Face({
|
||||
host: '127.0.0.1'
|
||||
});
|
||||
|
||||
const interest = new Interest(new Name('/test'));
|
||||
face.expressInterest(interest, (interest1, data) => {
|
||||
console.log(`Receive data: ${data.getContent().toString()}`);
|
||||
face.close();
|
||||
}, interest1 => {
|
||||
console.log('onTimeout');
|
||||
}, (interest1, nack) => {
|
||||
console.log(`onNack: ${nack.getReason()}`);
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
Face,
|
||||
KeyChain, PibMemory, SafeBag, TpmBackEndMemory,
|
||||
Blob,
|
||||
Name,
|
||||
Data
|
||||
} from 'typed-ndn-js';
|
||||
|
||||
import * as fs from 'fs';
|
||||
|
||||
const face = new Face({
|
||||
host: '127.0.0.1'
|
||||
});
|
||||
|
||||
const keyChain = new KeyChain(new PibMemory(), new TpmBackEndMemory());
|
||||
keyChain.importSafeBag(new SafeBag(new Blob(fs.readFileSync('./qjm.safebag'))));
|
||||
|
||||
face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
|
||||
|
||||
face.registerPrefix(new Name('/test'), (prefix, interest, face1, filterId, filter) => {
|
||||
console.log(`receive interest: ` + interest.toUri());
|
||||
const data = new Data(interest.getName());
|
||||
data.setContent("hello");
|
||||
keyChain.sign(data);
|
||||
face.putData(data);
|
||||
}, prefix => {
|
||||
console.log("register failed");
|
||||
}, (prefix, registeredPrefixId) => {
|
||||
console.log("register success");
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Basic Options */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
|
||||
"module": "es6", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
// "checkJs": true, /* Report errors in .js files. */
|
||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||
"sourceMap": true, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||
// "composite": true, /* Enable project compilation */
|
||||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||
// "removeComments": true, /* Do not emit comments to output. */
|
||||
// "noEmit": true, /* Do not emit outputs. */
|
||||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
// "strict": true, /* Enable all strict type-checking options. */
|
||||
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||
|
||||
/* Additional Checks */
|
||||
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||
|
||||
/* Module Resolution Options */
|
||||
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||
// "types": [], /* Type declaration files to be included in compilation. */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
|
||||
/* Source Map Options */
|
||||
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||
|
||||
/* Experimental Options */
|
||||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||
"lib": [
|
||||
"dom",
|
||||
"es5",
|
||||
"scripthost",
|
||||
"es2015.promise"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
mode: 'production',
|
||||
entry: 'test.ts',
|
||||
devtool: "inline-source-map",
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.ts?$/,
|
||||
use: 'ts-loader',
|
||||
exclude: /node_modules/
|
||||
}
|
||||
]
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts', '.tsx', '.js']
|
||||
},
|
||||
output: {
|
||||
filename: "test-ndn-js.bundle.js",
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
libraryTarget: "umd",
|
||||
},
|
||||
target: "node",
|
||||
};
|
||||
Reference in new issue
Block a user