convert to typescript
This commit is contained in:
parent
18b7ef774a
commit
afef280a2b
8 changed files with 189 additions and 96 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
dist/main.js
|
dist/main.js
|
||||||
|
src/
|
||||||
|
|
8
build.sh
Executable file
8
build.sh
Executable file
|
@ -0,0 +1,8 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
if [ ! -e ./src ]; then
|
||||||
|
mkdir ./src
|
||||||
|
fi
|
||||||
|
|
||||||
|
npx tsc &&
|
||||||
|
npx webpack
|
31
package-lock.json
generated
31
package-lock.json
generated
|
@ -11,9 +11,11 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nostr-dev-kit/ndk": "^2.7.1",
|
"@nostr-dev-kit/ndk": "^2.7.1",
|
||||||
"bech32": "^2.0.0",
|
"bech32": "^2.0.0",
|
||||||
"nostr-tools": "^2.5.0"
|
"nostr-tools": "^2.5.0",
|
||||||
|
"typescript": "^5.4.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/debug": "^4.1.12",
|
||||||
"webpack": "^5.91.0",
|
"webpack": "^5.91.0",
|
||||||
"webpack-cli": "^5.1.4"
|
"webpack-cli": "^5.1.4"
|
||||||
}
|
}
|
||||||
|
@ -281,6 +283,15 @@
|
||||||
"url": "https://paulmillr.com/funding/"
|
"url": "https://paulmillr.com/funding/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/debug": {
|
||||||
|
"version": "4.1.12",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz",
|
||||||
|
"integrity": "sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@types/ms": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/eslint": {
|
"node_modules/@types/eslint": {
|
||||||
"version": "8.56.10",
|
"version": "8.56.10",
|
||||||
"resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.56.10.tgz",
|
"resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.56.10.tgz",
|
||||||
|
@ -313,6 +324,12 @@
|
||||||
"integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==",
|
"integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/ms": {
|
||||||
|
"version": "0.7.34",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.34.tgz",
|
||||||
|
"integrity": "sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"node_modules/@types/node": {
|
"node_modules/@types/node": {
|
||||||
"version": "20.12.7",
|
"version": "20.12.7",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz",
|
||||||
|
@ -1685,6 +1702,18 @@
|
||||||
"is-typedarray": "^1.0.0"
|
"is-typedarray": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/typescript": {
|
||||||
|
"version": "5.4.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz",
|
||||||
|
"integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==",
|
||||||
|
"bin": {
|
||||||
|
"tsc": "bin/tsc",
|
||||||
|
"tsserver": "bin/tsserver"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.17"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/typescript-lru-cache": {
|
"node_modules/typescript-lru-cache": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/typescript-lru-cache/-/typescript-lru-cache-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/typescript-lru-cache/-/typescript-lru-cache-2.0.0.tgz",
|
||||||
|
|
|
@ -11,9 +11,11 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nostr-dev-kit/ndk": "^2.7.1",
|
"@nostr-dev-kit/ndk": "^2.7.1",
|
||||||
"bech32": "^2.0.0",
|
"bech32": "^2.0.0",
|
||||||
"nostr-tools": "^2.5.0"
|
"nostr-tools": "^2.5.0",
|
||||||
|
"typescript": "^5.4.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/debug": "^4.1.12",
|
||||||
"webpack": "^5.91.0",
|
"webpack": "^5.91.0",
|
||||||
"webpack-cli": "^5.1.4"
|
"webpack-cli": "^5.1.4"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,57 +0,0 @@
|
||||||
import NDK, {NDKPrivateKeySigner} from "@nostr-dev-kit/ndk";
|
|
||||||
import * as nip19 from 'nostr-tools/nip19'
|
|
||||||
import { bech32 } from "bech32";
|
|
||||||
|
|
||||||
let ndk = null;
|
|
||||||
|
|
||||||
function createAdapter() {
|
|
||||||
let adapter = {
|
|
||||||
nickname: "",
|
|
||||||
identity: null,
|
|
||||||
protocol: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
adapter.init = ()=>{};
|
|
||||||
adapter.getInbox = ()=>{};
|
|
||||||
adapter.getFollowers = ()=>{};
|
|
||||||
adapter.getFollowing = ()=>{};
|
|
||||||
adapter.publish = ()=>{};
|
|
||||||
adapter.updateMetadata = ()=>{};
|
|
||||||
adapter.getMetadata = ()=>{};
|
|
||||||
|
|
||||||
return adapter;
|
|
||||||
}
|
|
||||||
|
|
||||||
function toNostrAdapter(adapter, settings) {
|
|
||||||
adapter.identity = { privkey: settings.privkey };
|
|
||||||
adapter.nickname = settings.nickname;
|
|
||||||
adapter.init = ()=> {
|
|
||||||
if (!ndk) {
|
|
||||||
let privkey_raw = nip19.decode(settings.privkey);
|
|
||||||
console.dir(privkey_raw);
|
|
||||||
ndk = new NDK({
|
|
||||||
signer: new NDKPrivateKeySigner(privkey_raw.data),
|
|
||||||
explicitRelayUrls: [ settings.relays ]
|
|
||||||
});
|
|
||||||
ndk.connect();
|
|
||||||
} else {
|
|
||||||
ndk.activeUser = new NDKPrivateKeySigner(settings.privatekey);
|
|
||||||
for (let i of settings.relays) {
|
|
||||||
ndk.addExplicitRelay(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
adapter.getInbox = () => {
|
|
||||||
const sub = ndk.subscribe({ kinds: [1] }); // Get all kind:1s
|
|
||||||
sub.on("event", (event) => console.log(event.content)); // Show the content
|
|
||||||
sub.on("eose", () => console.log("All relays have reached the end of the event stream"));
|
|
||||||
sub.on("close", () => console.log("Subscription closed"));
|
|
||||||
setTimeout(() => sub.stop(), 10000); // Stop the subscription after 10 seconds
|
|
||||||
};
|
|
||||||
|
|
||||||
return adapter;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default { createAdapter, toNostrAdapter }
|
|
||||||
|
|
67
ts/adapter.ts
Normal file
67
ts/adapter.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import NDK, {NDKPrivateKeySigner} from "@nostr-dev-kit/ndk";
|
||||||
|
import * as nip19 from 'nostr-tools/nip19'
|
||||||
|
|
||||||
|
export class Adapter {
|
||||||
|
public nickname: string = "";
|
||||||
|
public protocol: string = "";
|
||||||
|
public identity: any | null;
|
||||||
|
|
||||||
|
public init(): void {};
|
||||||
|
public getInbox(): void {};
|
||||||
|
public publish(): void {};
|
||||||
|
public getFollowers(): any[] { return [] };
|
||||||
|
public getFollowing(): any[] { return [] };
|
||||||
|
public updateMetadata(): void {};
|
||||||
|
public getMetadata(): any { return {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
let ndk: NDK | null = null;
|
||||||
|
|
||||||
|
function createAdapter(): Adapter {
|
||||||
|
let adapter = new Adapter();
|
||||||
|
|
||||||
|
adapter.init = ()=>{};
|
||||||
|
adapter.getInbox = ()=>{};
|
||||||
|
adapter.getFollowers = ()=>[];
|
||||||
|
adapter.getFollowing = ()=>[];
|
||||||
|
adapter.publish = ()=>{};
|
||||||
|
adapter.updateMetadata = ()=>{};
|
||||||
|
adapter.getMetadata = ()=>{return {}};
|
||||||
|
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toNostrAdapter(adapter: Adapter, settings: any): Adapter {
|
||||||
|
adapter.identity = { privkey: settings.privkey };
|
||||||
|
adapter.nickname = settings.nickname;
|
||||||
|
adapter.init = ()=> {
|
||||||
|
if (!ndk) {
|
||||||
|
let privkey_raw = nip19.decode(settings.privkey);
|
||||||
|
ndk = new NDK({
|
||||||
|
signer: new NDKPrivateKeySigner(<string>privkey_raw.data),
|
||||||
|
explicitRelayUrls: [ settings.relays ]
|
||||||
|
});
|
||||||
|
ndk.connect();
|
||||||
|
} else {
|
||||||
|
ndk.signer = new NDKPrivateKeySigner(settings.privatekey);
|
||||||
|
for (let i of settings.relays) {
|
||||||
|
ndk.addExplicitRelay(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
adapter.getInbox = () => {
|
||||||
|
if (ndk) {
|
||||||
|
const sub = ndk.subscribe({ kinds: [1] }); // Get all kind:1s
|
||||||
|
sub.on("event", (event) => console.log(event.content)); // Show the content
|
||||||
|
sub.on("eose", () => console.log("All relays have reached the end of the event stream"));
|
||||||
|
sub.on("close", () => console.log("Subscription closed"));
|
||||||
|
setTimeout(() => sub.stop(), 10000); // Stop the subscription after 10 seconds
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default { createAdapter, toNostrAdapter }
|
||||||
|
|
|
@ -1,49 +1,66 @@
|
||||||
import adapter from "./adapter";
|
import adapter, {Adapter} from "./adapter";
|
||||||
|
|
||||||
window.adapters = [];
|
function _s(key: string, value: any | null) {
|
||||||
|
const x = <any>window;
|
||||||
|
x[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
function $(id) {
|
function _g(key: string): any | null {
|
||||||
|
const x = <any>window;
|
||||||
|
return x[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
function $(id: string): HTMLElement | null {
|
||||||
return document.getElementById(id);
|
return document.getElementById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function main():void {
|
||||||
window.settings = JSON.parse(localStorage.getItem("settings"));;
|
_s("settings", JSON.parse(localStorage.getItem("settings") ?? "{}"));
|
||||||
if (window.settings != null) {
|
_s("adapters", []);
|
||||||
for (let s of window.settings.adapters) {
|
|
||||||
|
const settings = _g("settings");
|
||||||
|
const adapters = _g("adapters");
|
||||||
|
if (settings != null) {
|
||||||
|
for (let s of settings.adapters) {
|
||||||
switch (s.protocol) {
|
switch (s.protocol) {
|
||||||
case "nostr":
|
case "nostr":
|
||||||
let a = adapter.toNostrAdapter(adapter.createAdapter(), s);
|
let a = adapter.toNostrAdapter(adapter.createAdapter(), s);
|
||||||
window.adapters.push(a);
|
adapters.push(a);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log("no settings exist for this client");
|
console.log("no settings exist for this client");
|
||||||
window.settings = { adapters: [] };
|
_s("settings", { adapters: [] });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function showSettings() {
|
function showSettings():void {
|
||||||
// tab bar hidden
|
// tab bar hidden
|
||||||
const tabbar = $("tabbar");
|
const tabbar = $("tabbar");
|
||||||
|
if (tabbar) {
|
||||||
tabbar.style.display = "none";
|
tabbar.style.display = "none";
|
||||||
|
}
|
||||||
|
|
||||||
// tabcontent to show settings ui
|
// tabcontent to show settings ui
|
||||||
const tabcontent = $("tabcontent");
|
const tabcontent = $("tabcontent");
|
||||||
|
|
||||||
|
if (tabcontent) {
|
||||||
let html = "<p>this is our settings dialogue</p>";
|
let html = "<p>this is our settings dialogue</p>";
|
||||||
html += "<button onclick='addAdapter()'>New</button>";
|
html += "<button onclick='addAdapter()'>New</button>";
|
||||||
html += adapters.reduce((self, a) => {
|
html += _g("adapters").reduce((self: string, a: Adapter) => {
|
||||||
self += `<li><a href='#' onclick='editAdapter(${a.nickname})'>${a.nickname}</a></li>`
|
self += `<li><a href='#' onclick='editAdapter(${a.nickname})'>${a.nickname}</a></li>`
|
||||||
return self;
|
return self;
|
||||||
}, "<ul id='settings_adapterlist'>");
|
}, "<ul id='settings_adapterlist'>");
|
||||||
html += "</ul>";
|
html += "</ul>";
|
||||||
html += "<button onclick='saveSettings()'>save</button>";
|
html += "<button onclick='saveSettings()'>save</button>";
|
||||||
tabcontent.innerHTML = html;
|
tabcontent.innerHTML = html;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addAdapter() {
|
function addAdapter(): void {
|
||||||
const tabcontent = $("tabcontent");
|
const tabcontent = $("tabcontent");
|
||||||
|
if (tabcontent) {
|
||||||
// dropdown for protocol
|
// dropdown for protocol
|
||||||
let html = "<select id='settings_newadapter_protocolselect' onchange='fillAdapterProtocolOptions()'>";
|
let html = "<select id='settings_newadapter_protocolselect' onchange='fillAdapterProtocolOptions()'>";
|
||||||
html += [ "nostr" ].reduce((self, p)=>{
|
html += [ "nostr" ].reduce((self, p)=>{
|
||||||
|
@ -64,30 +81,39 @@ function addAdapter() {
|
||||||
html += "<button onclick='showSettings()'>Back</button>";
|
html += "<button onclick='showSettings()'>Back</button>";
|
||||||
|
|
||||||
tabcontent.innerHTML = html;
|
tabcontent.innerHTML = html;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fillAdapterProtocolOptions() {
|
function fillAdapterProtocolOptions(): void {
|
||||||
const proto = $("settings_newadapter_protocolselect");
|
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
|
||||||
console.log(proto.options[proto.selectedIndex]);
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log(proto?.options[proto.selectedIndex] ?? "no proto");
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveSettings() {
|
function saveSettings(): void {
|
||||||
if (window.settings) {
|
const settings = _g("settings");
|
||||||
localStorage.setItem("settings", JSON.stringify(window.settings));
|
if (settings) {
|
||||||
|
localStorage.setItem("settings", JSON.stringify(settings));
|
||||||
}
|
}
|
||||||
// tab bar hidden
|
// tab bar hidden
|
||||||
const tabbar = $("tabbar");
|
const tabbar = $("tabbar");
|
||||||
tabbar.style.display = "block";
|
if (tabbar) {
|
||||||
|
tabbar.style.display = "block";
|
||||||
|
}
|
||||||
|
|
||||||
// tabcontent to show settings ui
|
// tabcontent to show settings ui
|
||||||
const tabcontent = $("tabcontent");
|
const tabcontent = $("tabcontent");
|
||||||
tabcontent.innerHTML = "";
|
if (tabcontent) {
|
||||||
|
tabcontent.innerHTML = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveAdapter() {
|
function saveAdapter(): void {
|
||||||
let self = {};
|
let self: any = {};
|
||||||
// get selected adapter protocol
|
// get selected adapter protocol
|
||||||
const proto = $("settings_newadapter_protocolselect");
|
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
|
||||||
console.log(proto.options[proto.selectedIndex]);
|
console.log(proto.options[proto.selectedIndex]);
|
||||||
|
|
||||||
// switch protocol
|
// switch protocol
|
||||||
|
@ -95,25 +121,29 @@ function saveAdapter() {
|
||||||
// nostr: save privkey
|
// nostr: save privkey
|
||||||
// save relays
|
// save relays
|
||||||
case "nostr":
|
case "nostr":
|
||||||
const privkey = $("settings_newadapter_nostr_privkey").value;
|
const privkey = ($("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? "";
|
||||||
const relays = $("settings_newadapter_nostr_default_relays").value;
|
const relays = ($("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? "";
|
||||||
const nickname = $("settings_newadapter_nickname").value;
|
const nickname = ($("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
|
||||||
self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
|
self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
|
||||||
break;
|
break;
|
||||||
// AP/masto: whatever
|
// AP/masto: whatever
|
||||||
case "ap":
|
case "ap":
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
window.settings.adapters.push(self);
|
const settings = _g("settings");
|
||||||
adapters.push(adapter.toNostrAdapter(adapter.createAdapter(), self));
|
const adapters = _g("adapters");
|
||||||
localStorage.setItem("settings", JSON.stringify(window.settings));
|
if (settings && adapters) {
|
||||||
showSettings();
|
settings.adapters.push(self);
|
||||||
|
adapters.push(adapter.toNostrAdapter(adapter.createAdapter(), self));
|
||||||
|
localStorage.setItem("settings", JSON.stringify(settings));
|
||||||
|
showSettings();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addAdapter = addAdapter;
|
_s("addAdapter", addAdapter);
|
||||||
window.saveAdapter = saveAdapter;
|
_s("saveAdapter", saveAdapter);
|
||||||
window.fillAdapterProtocolOptions = fillAdapterProtocolOptions;
|
_s("fillAdapterProtocolOptions", fillAdapterProtocolOptions);
|
||||||
window.showSettings = showSettings;
|
_s("showSettings", showSettings);
|
||||||
window.saveSettings = saveSettings;
|
_s("saveSettings", saveSettings);
|
||||||
|
|
||||||
main();
|
main();
|
13
tsconfig.json
Normal file
13
tsconfig.json
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "preserve",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"strict": true,
|
||||||
|
"removeComments": false,
|
||||||
|
"preserveConstEnums": true,
|
||||||
|
"sourceMap": false,
|
||||||
|
"outDir": "./src"
|
||||||
|
},
|
||||||
|
"include": [ "./ts/*.ts"]
|
||||||
|
}
|
Loading…
Reference in a new issue