forked from cory/tildefriends
Compare commits
1 Commits
tasiaiso-0
...
tasiaiso-h
Author | SHA1 | Date | |
---|---|---|---|
5e72c9caf4
|
1
.husky/pre-commit
Normal file
1
.husky/pre-commit
Normal file
@ -0,0 +1 @@
|
|||||||
|
make format || exit 0
|
@ -3,9 +3,9 @@
|
|||||||
MAKEFLAGS += --warn-undefined-variables
|
MAKEFLAGS += --warn-undefined-variables
|
||||||
MAKEFLAGS += --no-builtin-rules
|
MAKEFLAGS += --no-builtin-rules
|
||||||
|
|
||||||
VERSION_CODE := 21
|
VERSION_CODE := 20
|
||||||
VERSION_NUMBER := 0.0.21-wip
|
VERSION_NUMBER := 0.0.20-wip
|
||||||
VERSION_NAME := Psst. Look behind you.
|
VERSION_NAME := One word all lowercase four words all uppercase.
|
||||||
|
|
||||||
SQLITE_URL := https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip
|
SQLITE_URL := https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip
|
||||||
LIBUV_URL := https://dist.libuv.org/dist/v1.48.0/libuv-v1.48.0.tar.gz
|
LIBUV_URL := https://dist.libuv.org/dist/v1.48.0/libuv-v1.48.0.tar.gz
|
||||||
@ -902,7 +902,7 @@ dist-test: dist
|
|||||||
@rm -rf tildefriends-$(VERSION_NUMBER)
|
@rm -rf tildefriends-$(VERSION_NUMBER)
|
||||||
.PHONY: dist-test
|
.PHONY: dist-test
|
||||||
|
|
||||||
format:
|
format: prettier
|
||||||
@clang-format -i $(wildcard src/*.c src/*.h src/*.m)
|
@clang-format -i $(wildcard src/*.c src/*.h src/*.m)
|
||||||
.PHONY: format
|
.PHONY: format
|
||||||
|
|
||||||
|
6
apps/blog/lit-all.min.js
vendored
6
apps/blog/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
6
apps/issues/lit-all.min.js
vendored
6
apps/issues/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
6
apps/journal/lit-all.min.js
vendored
6
apps/journal/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
6
apps/sneaker/lit-all.min.js
vendored
6
apps/sneaker/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"type": "tildefriends-app",
|
"type": "tildefriends-app",
|
||||||
"emoji": "🐌",
|
"emoji": "🐌",
|
||||||
"previous": "&TqpkOAi38Oi6gW6guh95KIvWY2M/vjBE8NLLNHK+M00=.sha256"
|
"previous": "&zRv7YNZBT/NoliiTS7Jn/Q+3przdFZljUl8yPBIpSSE=.sha256"
|
||||||
}
|
}
|
||||||
|
6
apps/ssb/lit-all.min.js
vendored
6
apps/ssb/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -76,9 +76,15 @@ class TfComposeElement extends LitElement {
|
|||||||
let preview = this.renderRoot.getElementById('preview');
|
let preview = this.renderRoot.getElementById('preview');
|
||||||
preview.innerHTML = this.process_text(edit.innerText);
|
preview.innerHTML = this.process_text(edit.innerText);
|
||||||
let content_warning = this.renderRoot.getElementById('content_warning');
|
let content_warning = this.renderRoot.getElementById('content_warning');
|
||||||
|
let content_warning_preview = this.renderRoot.getElementById(
|
||||||
|
'content_warning_preview'
|
||||||
|
);
|
||||||
|
if (content_warning && content_warning_preview) {
|
||||||
|
content_warning_preview.innerText = content_warning.value;
|
||||||
|
}
|
||||||
let draft = this.get_draft();
|
let draft = this.get_draft();
|
||||||
draft.text = edit.innerText;
|
draft.text = edit.innerText;
|
||||||
draft.content_warning = content_warning?.value;
|
draft.content_warning = content_warning?.innerText;
|
||||||
setTimeout(() => this.notify(draft), 0);
|
setTimeout(() => this.notify(draft), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,8 +221,12 @@ class TfComposeElement extends LitElement {
|
|||||||
console.log('encrypted as', message);
|
console.log('encrypted as', message);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await tfrpc.rpc.appendMessage(this.whoami, message);
|
await tfrpc.rpc.appendMessage(this.whoami, message).then(function () {
|
||||||
|
edit.innerText = '';
|
||||||
|
self.input();
|
||||||
self.notify(undefined);
|
self.notify(undefined);
|
||||||
|
self.requestUpdate();
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error.message);
|
alert(error.message);
|
||||||
}
|
}
|
||||||
@ -243,9 +253,9 @@ class TfComposeElement extends LitElement {
|
|||||||
try {
|
try {
|
||||||
let rows = await tfrpc.rpc.query(
|
let rows = await tfrpc.rpc.query(
|
||||||
`
|
`
|
||||||
SELECT json(messages.content) AS content FROM messages_fts(?)
|
SELECT json(messages.content) FROM messages_fts(?)
|
||||||
JOIN messages ON messages.rowid = messages_fts.rowid
|
JOIN messages ON messages.rowid = messages_fts.rowid
|
||||||
WHERE json(messages.content) LIKE ?
|
WHERE messages.content LIKE ?
|
||||||
ORDER BY timestamp DESC LIMIT 10
|
ORDER BY timestamp DESC LIMIT 10
|
||||||
`,
|
`,
|
||||||
['"' + text.replace('"', '""') + '"', `%%`]
|
['"' + text.replace('"', '""') + '"', `%%`]
|
||||||
@ -281,7 +291,6 @@ class TfComposeElement extends LitElement {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let tribute = new Tribute({
|
let tribute = new Tribute({
|
||||||
iframe: this.shadowRoot,
|
|
||||||
collection: [
|
collection: [
|
||||||
{
|
{
|
||||||
values: values,
|
values: values,
|
||||||
@ -316,7 +325,6 @@ class TfComposeElement extends LitElement {
|
|||||||
let encrypt = this.renderRoot.getElementById('encrypt_to');
|
let encrypt = this.renderRoot.getElementById('encrypt_to');
|
||||||
if (encrypt) {
|
if (encrypt) {
|
||||||
let tribute = new Tribute({
|
let tribute = new Tribute({
|
||||||
iframe: this.shadowRoot,
|
|
||||||
values: Object.entries(this.users).map((x) => ({
|
values: Object.entries(this.users).map((x) => ({
|
||||||
key: x[1].name,
|
key: x[1].name,
|
||||||
value: x[0],
|
value: x[0],
|
||||||
@ -449,7 +457,7 @@ class TfComposeElement extends LitElement {
|
|||||||
<input type="checkbox" class="w3-check w3-theme-d1" id="cw" @change=${() => self.set_content_warning(undefined)} checked="checked"></input>
|
<input type="checkbox" class="w3-check w3-theme-d1" id="cw" @change=${() => self.set_content_warning(undefined)} checked="checked"></input>
|
||||||
<label for="cw">CW</label>
|
<label for="cw">CW</label>
|
||||||
</p>
|
</p>
|
||||||
<input type="text" class="w3-input w3-border w3-theme-d1" id="content_warning" placeholder="Enter a content warning here." @input=${self.input} value=${draft.content_warning}></input>
|
<input type="text" class="w3-input w3-border w3-theme-d1" id="content_warning" placeholder="Enter a content warning here." @input=${this.input} @change=${this.change} value=${draft.content_warning}></input>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
} else {
|
} else {
|
||||||
|
@ -482,7 +482,16 @@ class TributeRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getDocument() {
|
getDocument() {
|
||||||
return document;
|
let iframe;
|
||||||
|
if (this.tribute.current.collection) {
|
||||||
|
iframe = this.tribute.current.collection.iframe;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!iframe) {
|
||||||
|
return document
|
||||||
|
}
|
||||||
|
|
||||||
|
return iframe.contentWindow.document
|
||||||
}
|
}
|
||||||
|
|
||||||
positionMenuAtCaret(scrollTo) {
|
positionMenuAtCaret(scrollTo) {
|
||||||
@ -644,8 +653,8 @@ class TributeRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getWindowSelection() {
|
getWindowSelection() {
|
||||||
if (this.tribute.collection[0].iframe?.getSelection) {
|
if (this.tribute.collection.iframe) {
|
||||||
return this.tribute.collection[0].iframe.getSelection()
|
return this.tribute.collection.iframe.contentWindow.getSelection()
|
||||||
}
|
}
|
||||||
|
|
||||||
return window.getSelection()
|
return window.getSelection()
|
||||||
|
6
apps/wiki/lit-all.min.js
vendored
6
apps/wiki/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -83,10 +83,10 @@ App.prototype.send = function (message) {
|
|||||||
* @param {*} response
|
* @param {*} response
|
||||||
* @param {*} client
|
* @param {*} client
|
||||||
*/
|
*/
|
||||||
async function socket(request, response, client) {
|
function socket(request, response, client) {
|
||||||
let process;
|
let process;
|
||||||
let options = {};
|
let options = {};
|
||||||
let credentials = await httpd.auth_query(request.headers);
|
let credentials = httpd.auth_query(request.headers);
|
||||||
|
|
||||||
response.onClose = async function () {
|
response.onClose = async function () {
|
||||||
if (process && process.task) {
|
if (process && process.task) {
|
||||||
@ -222,7 +222,7 @@ async function socket(request, response, client) {
|
|||||||
} else if (message.action == 'setActiveIdentity') {
|
} else if (message.action == 'setActiveIdentity') {
|
||||||
process.setActiveIdentity(message.identity);
|
process.setActiveIdentity(message.identity);
|
||||||
} else if (message.action == 'createIdentity') {
|
} else if (message.action == 'createIdentity') {
|
||||||
await process.createIdentity();
|
process.createIdentity();
|
||||||
} else if (message.message == 'tfrpc') {
|
} else if (message.message == 'tfrpc') {
|
||||||
if (message.id && g_calls[message.id]) {
|
if (message.id && g_calls[message.id]) {
|
||||||
if (message.error !== undefined) {
|
if (message.error !== undefined) {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>Tilde Friends Sign-in</title>
|
<title>Tilde Friends Sign-in</title>
|
||||||
<link type="text/css" rel="stylesheet" href="/static/style.css" />
|
<link type="text/css" rel="stylesheet" href="/static/style.css" />
|
||||||
<link type="image/svg+xml" rel="icon" href="/static/tildefriends.svg" />
|
<link type="image/png" rel="shortcut icon" href="/static/favicon.png" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
@ -118,6 +118,28 @@ class TfNavigationElement extends LitElement {
|
|||||||
return this.spark_lines[key];
|
return this.spark_lines[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODOC
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
render_login() {
|
||||||
|
if (this?.credentials?.session?.name) {
|
||||||
|
return html`<a
|
||||||
|
class="w3-bar-item w3-right"
|
||||||
|
id="login"
|
||||||
|
href="/login/logout?return=${url() + hash()}"
|
||||||
|
>logout ${this.credentials.session.name}</a
|
||||||
|
>`;
|
||||||
|
} else {
|
||||||
|
return html`<a
|
||||||
|
class="w3-bar-item w3-right"
|
||||||
|
id="login"
|
||||||
|
href="/login?return=${url() + hash()}"
|
||||||
|
>login</a
|
||||||
|
>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
set_active_identity(id) {
|
set_active_identity(id) {
|
||||||
send({action: 'setActiveIdentity', identity: id});
|
send({action: 'setActiveIdentity', identity: id});
|
||||||
this.renderRoot.getElementById('id_dropdown').classList.remove('w3-show');
|
this.renderRoot.getElementById('id_dropdown').classList.remove('w3-show');
|
||||||
@ -137,14 +159,8 @@ class TfNavigationElement extends LitElement {
|
|||||||
window.location.href = '/~core/ssb/#' + this.identity;
|
window.location.href = '/~core/ssb/#' + this.identity;
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
|
||||||
window.location.href = `/login/logout?return=${encodeURIComponent(url() + hash())}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
render_identity() {
|
render_identity() {
|
||||||
let self = this;
|
let self = this;
|
||||||
|
|
||||||
if (this?.credentials?.session?.name) {
|
|
||||||
if (this.identities?.length) {
|
if (this.identities?.length) {
|
||||||
return html`
|
return html`
|
||||||
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
||||||
@ -152,15 +168,18 @@ class TfNavigationElement extends LitElement {
|
|||||||
<button
|
<button
|
||||||
class="w3-button w3-rest w3-cyan"
|
class="w3-button w3-rest w3-cyan"
|
||||||
style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; max-width: 100%"
|
style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; max-width: 100%"
|
||||||
id="identity"
|
|
||||||
@click=${self.toggle_id_dropdown}
|
@click=${self.toggle_id_dropdown}
|
||||||
>
|
>
|
||||||
${self.names[this.identity]}▾
|
${self.names[this.identity]}${self.names[this.identity] ===
|
||||||
|
this.identity
|
||||||
|
? ''
|
||||||
|
: html` - ${this.identity}`}
|
||||||
|
▾
|
||||||
</button>
|
</button>
|
||||||
<div
|
<div
|
||||||
id="id_dropdown"
|
id="id_dropdown"
|
||||||
class="w3-dropdown-content w3-bar-block w3-card-4"
|
class="w3-dropdown-content w3-bar-block w3-card-4"
|
||||||
style="max-width: 100%; right: 0"
|
style="max-width: 100%"
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
class="w3-bar-item w3-button w3-border"
|
class="w3-bar-item w3-button w3-border"
|
||||||
@ -187,13 +206,6 @@ class TfNavigationElement extends LitElement {
|
|||||||
</button>
|
</button>
|
||||||
`
|
`
|
||||||
)}
|
)}
|
||||||
<button
|
|
||||||
class="w3-bar-item w3-button w3-border"
|
|
||||||
id="logout"
|
|
||||||
@click=${self.logout}
|
|
||||||
>
|
|
||||||
Logout ${this.credentials.session.name}
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
@ -203,39 +215,14 @@ class TfNavigationElement extends LitElement {
|
|||||||
) {
|
) {
|
||||||
return html`
|
return html`
|
||||||
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
||||||
<button
|
|
||||||
class="w3-bar-item w3-button w3-right w3-cyan"
|
|
||||||
id="logout"
|
|
||||||
@click=${self.logout}
|
|
||||||
>
|
|
||||||
Logout ${this.credentials.session.name}
|
|
||||||
</button>
|
|
||||||
<button
|
<button
|
||||||
id="create_identity"
|
id="create_identity"
|
||||||
@click=${this.create_identity}
|
@click=${this.create_identity}
|
||||||
class="w3-button w3-mobile w3-red w3-right"
|
class="w3-button w3-mobile w3-blue w3-right"
|
||||||
>
|
>
|
||||||
Create an Identity
|
Create an Identity
|
||||||
</button>
|
</button>
|
||||||
`;
|
`;
|
||||||
} else {
|
|
||||||
return html`
|
|
||||||
<button
|
|
||||||
class="w3-bar-item w3-button w3-right w3-cyan"
|
|
||||||
id="logout"
|
|
||||||
@click=${self.logout}
|
|
||||||
>
|
|
||||||
Logout ${this.credentials.session.name}
|
|
||||||
</button>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return html`<a
|
|
||||||
class="w3-bar-item w3-cyan w3-right"
|
|
||||||
id="login"
|
|
||||||
href="/login?return=${url() + hash()}"
|
|
||||||
>login</a
|
|
||||||
>`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -375,7 +362,7 @@ class TfNavigationElement extends LitElement {
|
|||||||
${Object.keys(this.spark_lines)
|
${Object.keys(this.spark_lines)
|
||||||
.sort()
|
.sort()
|
||||||
.map((x) => this.spark_lines[x])}
|
.map((x) => this.spark_lines[x])}
|
||||||
${this.render_identity()}
|
${this.render_login()} ${this.render_identity()}
|
||||||
</div>
|
</div>
|
||||||
${this.status?.is_error
|
${this.status?.is_error
|
||||||
? html`
|
? html`
|
||||||
|
106
core/core.js
106
core/core.js
@ -206,7 +206,7 @@ function getUser(caller, process) {
|
|||||||
* @param {*} process
|
* @param {*} process
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async function getApps(user, process) {
|
function getApps(user, process) {
|
||||||
if (
|
if (
|
||||||
process.credentials &&
|
process.credentials &&
|
||||||
process.credentials.session &&
|
process.credentials.session &&
|
||||||
@ -221,12 +221,10 @@ async function getApps(user, process) {
|
|||||||
if (user) {
|
if (user) {
|
||||||
let db = new Database(user);
|
let db = new Database(user);
|
||||||
try {
|
try {
|
||||||
let names = JSON.parse(await db.get('apps'));
|
let names = JSON.parse(db.get('apps'));
|
||||||
let result = {};
|
return Object.fromEntries(
|
||||||
for (let name of names) {
|
names.map((name) => [name, db.get('path:' + name)])
|
||||||
result[name] = await db.get('path:' + name);
|
);
|
||||||
}
|
|
||||||
return result;
|
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
@ -322,9 +320,9 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
user: getUser(process, process),
|
user: getUser(process, process),
|
||||||
users: async function () {
|
users: function () {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(await new Database('auth').get('users'));
|
return JSON.parse(new Database('auth').get('users'));
|
||||||
} catch {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -472,7 +470,7 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
process.credentials.session.name &&
|
process.credentials.session.name &&
|
||||||
process.credentials.session.name !== 'guest'
|
process.credentials.session.name !== 'guest'
|
||||||
) {
|
) {
|
||||||
let id = await ssb.createIdentity(process.credentials.session.name);
|
let id = ssb.createIdentity(process.credentials.session.name);
|
||||||
await process.sendIdentities();
|
await process.sendIdentities();
|
||||||
broadcastAppEventToUser(
|
broadcastAppEventToUser(
|
||||||
process?.credentials?.session?.name,
|
process?.credentials?.session?.name,
|
||||||
@ -511,20 +509,25 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
setGlobalSettings(gGlobalSettings);
|
setGlobalSettings(gGlobalSettings);
|
||||||
print('Done.');
|
print('Done.');
|
||||||
};
|
};
|
||||||
imports.core.deleteUser = async function (user) {
|
imports.core.deleteUser = function (user) {
|
||||||
await imports.core.permissionTest('delete_user');
|
return Promise.resolve(
|
||||||
|
imports.core.permissionTest('delete_user')
|
||||||
|
).then(function () {
|
||||||
let db = new Database('auth');
|
let db = new Database('auth');
|
||||||
|
|
||||||
db.remove('user:' + user);
|
db.remove('user:' + user);
|
||||||
|
|
||||||
let users = new Set();
|
let users = new Set();
|
||||||
let users_original = await db.get('users');
|
let users_original = db.get('users');
|
||||||
try {
|
try {
|
||||||
users = new Set(JSON.parse(users_original));
|
users = new Set(JSON.parse(users_original));
|
||||||
} catch {}
|
} catch {}
|
||||||
users.delete(user);
|
users.delete(user);
|
||||||
users = JSON.stringify([...users].sort());
|
users = JSON.stringify([...users].sort());
|
||||||
if (users !== users_original) {
|
if (users !== users_original) {
|
||||||
await db.set('users', users);
|
db.set('users', users);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (options.api) {
|
if (options.api) {
|
||||||
@ -749,7 +752,7 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
};
|
};
|
||||||
process.task.setImports(imports);
|
process.task.setImports(imports);
|
||||||
process.task.activate();
|
process.task.activate();
|
||||||
let source = await ssb.blobGet(blobId);
|
let source = await getBlobOrContent(blobId);
|
||||||
let appSourceName = blobId;
|
let appSourceName = blobId;
|
||||||
let appSource = utf8Decode(source);
|
let appSource = utf8Decode(source);
|
||||||
try {
|
try {
|
||||||
@ -757,7 +760,7 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
if (appObject.type == 'tildefriends-app') {
|
if (appObject.type == 'tildefriends-app') {
|
||||||
appSourceName = options?.script ?? 'app.js';
|
appSourceName = options?.script ?? 'app.js';
|
||||||
let id = appObject.files[appSourceName];
|
let id = appObject.files[appSourceName];
|
||||||
let blob = await ssb.blobGet(id);
|
let blob = await getBlobOrContent(id);
|
||||||
appSource = utf8Decode(blob);
|
appSource = utf8Decode(blob);
|
||||||
await process.task.loadFile([
|
await process.task.loadFile([
|
||||||
'/tfrpc.js',
|
'/tfrpc.js',
|
||||||
@ -767,7 +770,7 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
Object.keys(appObject.files).map(async function (f) {
|
Object.keys(appObject.files).map(async function (f) {
|
||||||
await process.task.loadFile([
|
await process.task.loadFile([
|
||||||
f,
|
f,
|
||||||
await ssb.blobGet(appObject.files[f]),
|
await getBlobOrContent(appObject.files[f]),
|
||||||
]);
|
]);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -803,15 +806,33 @@ async function getProcessBlob(blobId, key, options) {
|
|||||||
* @param {*} settings
|
* @param {*} settings
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async function setGlobalSettings(settings) {
|
function setGlobalSettings(settings) {
|
||||||
gGlobalSettings = settings;
|
gGlobalSettings = settings;
|
||||||
try {
|
try {
|
||||||
return await new Database('core').set('settings', JSON.stringify(settings));
|
return new Database('core').set('settings', JSON.stringify(settings));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
print('Error storing settings:', error);
|
print('Error storing settings:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODOC
|
||||||
|
* @param {*} data
|
||||||
|
* @param {*} bytes
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
function startsWithBytes(data, bytes) {
|
||||||
|
if (data.byteLength >= bytes.length) {
|
||||||
|
let dataBytes = new Uint8Array(data.slice(0, bytes.length));
|
||||||
|
for (let i = 0; i < bytes.length; i++) {
|
||||||
|
if (dataBytes[i] !== bytes[i] && bytes[i] !== null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODOC
|
* TODOC
|
||||||
* @param {*} response
|
* @param {*} response
|
||||||
@ -851,6 +872,21 @@ function sendData(response, data, type, headers, status_code) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODOC
|
||||||
|
* @param {*} id
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async function getBlobOrContent(id) {
|
||||||
|
if (!id) {
|
||||||
|
return;
|
||||||
|
} else if (id.startsWith('&')) {
|
||||||
|
return ssb.blobGet(id);
|
||||||
|
} else if (id.startsWith('%')) {
|
||||||
|
return ssb.messageContentGet(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let g_handler_index = 0;
|
let g_handler_index = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -893,7 +929,7 @@ async function useAppHandler(
|
|||||||
},
|
},
|
||||||
respond: do_resolve,
|
respond: do_resolve,
|
||||||
},
|
},
|
||||||
credentials: await httpd.auth_query(headers),
|
credentials: httpd.auth_query(headers),
|
||||||
packageOwner: packageOwner,
|
packageOwner: packageOwner,
|
||||||
packageName: packageName,
|
packageName: packageName,
|
||||||
}
|
}
|
||||||
@ -978,7 +1014,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
response.writeHead(304, headers);
|
response.writeHead(304, headers);
|
||||||
response.end();
|
response.end();
|
||||||
} else {
|
} else {
|
||||||
data = await ssb.blobGet(id);
|
data = await getBlobOrContent(id);
|
||||||
if (match[3]) {
|
if (match[3]) {
|
||||||
let appObject = JSON.parse(data);
|
let appObject = JSON.parse(data);
|
||||||
data = appObject.files[match[3]];
|
data = appObject.files[match[3]];
|
||||||
@ -1010,7 +1046,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
response.writeHead(304, headers);
|
response.writeHead(304, headers);
|
||||||
response.end();
|
response.end();
|
||||||
} else {
|
} else {
|
||||||
data = await ssb.blobGet(blobId);
|
data = await getBlobOrContent(blobId);
|
||||||
sendData(
|
sendData(
|
||||||
response,
|
response,
|
||||||
data,
|
data,
|
||||||
@ -1024,7 +1060,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
if ((match = /^\/\~(\w+)\/(\w+)$/.exec(blobId))) {
|
if ((match = /^\/\~(\w+)\/(\w+)$/.exec(blobId))) {
|
||||||
let user = match[1];
|
let user = match[1];
|
||||||
let appName = match[2];
|
let appName = match[2];
|
||||||
let credentials = await httpd.auth_query(request.headers);
|
let credentials = httpd.auth_query(request.headers);
|
||||||
if (
|
if (
|
||||||
credentials &&
|
credentials &&
|
||||||
credentials.session &&
|
credentials.session &&
|
||||||
@ -1034,7 +1070,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
let database = new Database(user);
|
let database = new Database(user);
|
||||||
|
|
||||||
let app_object = JSON.parse(utf8Decode(request.body));
|
let app_object = JSON.parse(utf8Decode(request.body));
|
||||||
let previous_id = await database.get('path:' + appName);
|
let previous_id = database.get('path:' + appName);
|
||||||
if (previous_id) {
|
if (previous_id) {
|
||||||
try {
|
try {
|
||||||
let previous_object = JSON.parse(
|
let previous_object = JSON.parse(
|
||||||
@ -1055,7 +1091,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
let newBlobId = await ssb.blobStore(JSON.stringify(app_object));
|
let newBlobId = await ssb.blobStore(JSON.stringify(app_object));
|
||||||
|
|
||||||
let apps = new Set();
|
let apps = new Set();
|
||||||
let apps_original = await database.get('apps');
|
let apps_original = database.get('apps');
|
||||||
try {
|
try {
|
||||||
apps = new Set(JSON.parse(apps_original));
|
apps = new Set(JSON.parse(apps_original));
|
||||||
} catch {}
|
} catch {}
|
||||||
@ -1064,9 +1100,9 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
}
|
}
|
||||||
apps = JSON.stringify([...apps].sort());
|
apps = JSON.stringify([...apps].sort());
|
||||||
if (apps != apps_original) {
|
if (apps != apps_original) {
|
||||||
await database.set('apps', apps);
|
database.set('apps', apps);
|
||||||
}
|
}
|
||||||
await database.set('path:' + appName, newBlobId);
|
database.set('path:' + appName, newBlobId);
|
||||||
response.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
|
response.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
|
||||||
response.end('/' + newBlobId);
|
response.end('/' + newBlobId);
|
||||||
} else {
|
} else {
|
||||||
@ -1087,7 +1123,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
if ((match = /^\/\~(\w+)\/(\w+)$/.exec(blobId))) {
|
if ((match = /^\/\~(\w+)\/(\w+)$/.exec(blobId))) {
|
||||||
let user = match[1];
|
let user = match[1];
|
||||||
let appName = match[2];
|
let appName = match[2];
|
||||||
let credentials = await httpd.auth_query(request.headers);
|
let credentials = httpd.auth_query(request.headers);
|
||||||
if (
|
if (
|
||||||
credentials &&
|
credentials &&
|
||||||
credentials.session &&
|
credentials.session &&
|
||||||
@ -1097,10 +1133,10 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
let database = new Database(user);
|
let database = new Database(user);
|
||||||
let apps = new Set();
|
let apps = new Set();
|
||||||
try {
|
try {
|
||||||
apps = new Set(JSON.parse(await database.get('apps')));
|
apps = new Set(JSON.parse(database.get('apps')));
|
||||||
} catch {}
|
} catch {}
|
||||||
if (apps.delete(appName)) {
|
if (apps.delete(appName)) {
|
||||||
await database.set('apps', JSON.stringify([...apps].sort()));
|
database.set('apps', JSON.stringify([...apps].sort()));
|
||||||
}
|
}
|
||||||
database.remove('path:' + appName);
|
database.remove('path:' + appName);
|
||||||
} else {
|
} else {
|
||||||
@ -1126,9 +1162,9 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
app_id = await db.get('path:' + match[2]);
|
app_id = await db.get('path:' + match[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let app_object = JSON.parse(utf8Decode(await ssb.blobGet(app_id)));
|
let app_object = JSON.parse(utf8Decode(await getBlobOrContent(app_id)));
|
||||||
id = app_object?.files[uri.substring(1)];
|
id = app_object.files[uri.substring(1)];
|
||||||
if (!id && app_object?.files['handler.js']) {
|
if (!id && app_object.files['handler.js']) {
|
||||||
let answer;
|
let answer;
|
||||||
try {
|
try {
|
||||||
answer = await useAppHandler(
|
answer = await useAppHandler(
|
||||||
@ -1182,7 +1218,7 @@ async function blobHandler(request, response, blobId, uri) {
|
|||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
'Content-Security-Policy': k_content_security_policy,
|
'Content-Security-Policy': k_content_security_policy,
|
||||||
};
|
};
|
||||||
data = await ssb.blobGet(id);
|
data = await getBlobOrContent(id);
|
||||||
let type =
|
let type =
|
||||||
httpd.mime_type_from_extension(uri) ||
|
httpd.mime_type_from_extension(uri) ||
|
||||||
httpd.mime_type_from_magic_bytes(data);
|
httpd.mime_type_from_magic_bytes(data);
|
||||||
@ -1212,7 +1248,7 @@ ssb.addEventListener('connections', function () {
|
|||||||
async function loadSettings() {
|
async function loadSettings() {
|
||||||
let data = {};
|
let data = {};
|
||||||
try {
|
try {
|
||||||
let settings = await new Database('core').get('settings');
|
let settings = new Database('core').get('settings');
|
||||||
if (settings) {
|
if (settings) {
|
||||||
data = JSON.parse(settings);
|
data = JSON.parse(settings);
|
||||||
}
|
}
|
||||||
|
BIN
core/favicon.png
Normal file
BIN
core/favicon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 320 B |
@ -4,7 +4,7 @@
|
|||||||
<title>Tilde Friends</title>
|
<title>Tilde Friends</title>
|
||||||
<link type="text/css" rel="stylesheet" href="/static/style.css" />
|
<link type="text/css" rel="stylesheet" href="/static/style.css" />
|
||||||
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
||||||
<link type="image/svg+xml" rel="icon" href="/static/tildefriends.svg" />
|
<link type="image/png" rel="shortcut icon" href="/static/favicon.png" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<script>
|
<script>
|
||||||
function set_access_key_title(event) {
|
function set_access_key_title(event) {
|
||||||
|
@ -1 +0,0 @@
|
|||||||
<svg width="65" height="65" viewBox="0 0 61 65" fill="none" xmlns="http://www.w3.org/2000/svg"><path style="fill:#0af;stroke-width:.712717;fill-opacity:1" d="M6 0h49a8 8 45 0 1 8 8v49a8 8 135 0 1-8 8H6a8 8 45 0 1-8-8V8a8 8 135 0 1 8-8Z"/><text xml:space="preserve" style="font-style:normal;font-weight:400;font-size:40px;line-height:1.25;font-family:sans-serif;fill:#000;fill-opacity:1;stroke:none" x="-.023" y="47.568"><tspan x="-.023" y="47.568" style="font-style:normal;font-variant:normal;font-weight:400;font-stretch:normal;font-size:40px;font-family:Arial;-inkscape-font-specification:'Arial, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal">~</tspan></text><g transform="translate(16.213 5.975) scale(.72923)"><circle cx="36" cy="36" r="23" fill="#fcea2b"/><path fill="#3f3f3f" d="M45.331 38.564c3.963 0 7.178-2.862 7.178-6.389 0-1.765.448-3.53-.852-4.685-1.299-1.156-4.345-1.704-6.326-1.704-2.357 0-5.143.143-6.451 1.704-.894 1.065-.727 3.253-.727 4.685 0 3.527 3.213 6.389 7.178 6.389zM25.738 38.564c3.963 0 7.179-2.862 7.179-6.389 0-1.765.447-3.53-.852-4.685-1.3-1.156-4.345-1.704-6.327-1.704-2.356 0-5.142.143-6.451 1.704-.893 1.065-.727 3.253-.727 4.685 0 3.527 3.213 6.389 7.178 6.389z"/></g><g stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2" transform="translate(16.213 5.975) scale(.72923)"><circle cx="35.887" cy="36.056" r="23"/><path d="M45.702 44.862c-6.574 3.525-14.045 3.658-19.63 0M18.883 30.464s-.953 8.55 6.86 7.918c2.62-.212 7.817-.65 7.867-8.342.005-.698-.007-1.6-.81-2.63-1.065-1.367-3.572-1.971-9.945-1.422 0 0-3.446-.1-3.972 4.476z"/><path d="m18.953 29.931-.433-3.372 3.833-.527M52.741 30.464s.953 8.55-6.86 7.918c-2.62-.212-7.817-.65-7.868-8.342-.004-.698.008-1.6.811-2.63 1.065-1.367 3.572-1.971 9.945-1.422 0 0 3.446-.1 3.972 4.476z"/><path d="M31.505 26.416s4.124 2.534 8.657 0M33.536 31.318s2.202-3.751 4.536 0M52.664 29.933l.433-3.371-3.833-.528"/><path d="M33.955 30.027s1.795-3.75 3.699 0"/></g></svg>
|
|
Before Width: | Height: | Size: 2.0 KiB |
@ -21,26 +21,24 @@
|
|||||||
}:
|
}:
|
||||||
pkgs.stdenv.mkDerivation rec {
|
pkgs.stdenv.mkDerivation rec {
|
||||||
pname = "tildefriends";
|
pname = "tildefriends";
|
||||||
version = "0.0.20";
|
version = "0.0.19";
|
||||||
|
|
||||||
src = pkgs.fetchFromGitea {
|
src = pkgs.fetchFromGitea {
|
||||||
domain = "dev.tildefriends.net";
|
domain = "dev.tildefriends.net";
|
||||||
owner = "cory";
|
owner = "cory";
|
||||||
repo = "tildefriends";
|
repo = "tildefriends";
|
||||||
rev = "v${version}";
|
rev = "v${version}";
|
||||||
hash = "sha256-q7PQS/OnfPyU74FBsTmuwWn+G8XTJ11ulvTxf1sgUQk=";
|
hash = "sha256-ttqL2wz06Jvn2f6kKIAGpF0nSSle+g4nSlj4jL0D+Fk=";
|
||||||
fetchSubmodules = true;
|
fetchSubmodules = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
nativeBuildInputs = with pkgs; [
|
nativeBuildInputs = with pkgs; [
|
||||||
glibc
|
|
||||||
gnumake
|
gnumake
|
||||||
openssl
|
openssl
|
||||||
which
|
which
|
||||||
];
|
];
|
||||||
|
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
glibc
|
|
||||||
openssl
|
openssl
|
||||||
which
|
which
|
||||||
];
|
];
|
||||||
|
2
deps/codemirror/cm6.js
vendored
2
deps/codemirror/cm6.js
vendored
File diff suppressed because one or more lines are too long
56
deps/codemirror_src/package-lock.json
generated
vendored
56
deps/codemirror_src/package-lock.json
generated
vendored
@ -19,9 +19,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@codemirror/autocomplete": {
|
"node_modules/@codemirror/autocomplete": {
|
||||||
"version": "6.16.2",
|
"version": "6.16.0",
|
||||||
"resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.16.2.tgz",
|
"resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.16.0.tgz",
|
||||||
"integrity": "sha512-MjfDrHy0gHKlPWsvSsikhO1+BOh+eBHNgfH1OXs1+DAf30IonQldgMM3kxLDTG9ktE7kDLaA1j/l7KMPA4KNfw==",
|
"integrity": "sha512-P/LeCTtZHRTCU4xQsa89vSKWecYv1ZqwzOd5topheGRf+qtacFgBeIMQi3eL8Kt/BUNvxUWkx+5qP2jlGoARrg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@codemirror/language": "^6.0.0",
|
"@codemirror/language": "^6.0.0",
|
||||||
"@codemirror/state": "^6.0.0",
|
"@codemirror/state": "^6.0.0",
|
||||||
@ -36,13 +36,13 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@codemirror/commands": {
|
"node_modules/@codemirror/commands": {
|
||||||
"version": "6.6.0",
|
"version": "6.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/@codemirror/commands/-/commands-6.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/@codemirror/commands/-/commands-6.5.0.tgz",
|
||||||
"integrity": "sha512-qnY+b7j1UNcTS31Eenuc/5YJB6gQOzkUoNmJQc0rznwqSRpeaWWpjkWy2C/MPTcePpsKJEM26hXrOXl1+nceXg==",
|
"integrity": "sha512-rK+sj4fCAN/QfcY9BEzYMgp4wwL/q5aj/VfNSoH1RWPF9XS/dUwBkvlL3hpWgEjOqlpdN1uLC9UkjJ4tmyjJYg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@codemirror/language": "^6.0.0",
|
"@codemirror/language": "^6.0.0",
|
||||||
"@codemirror/state": "^6.4.0",
|
"@codemirror/state": "^6.4.0",
|
||||||
"@codemirror/view": "^6.27.0",
|
"@codemirror/view": "^6.0.0",
|
||||||
"@lezer/common": "^1.1.0"
|
"@lezer/common": "^1.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -98,9 +98,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@codemirror/language": {
|
"node_modules/@codemirror/language": {
|
||||||
"version": "6.10.2",
|
"version": "6.10.1",
|
||||||
"resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.10.2.tgz",
|
"resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.10.1.tgz",
|
||||||
"integrity": "sha512-kgbTYTo0Au6dCSc/TFy7fK3fpJmgHDv1sG1KNQKJXVi+xBTEeBPY/M30YXiU6mMXeH+YIDLsbrT4ZwNRdtF+SA==",
|
"integrity": "sha512-5GrXzrhq6k+gL5fjkAwt90nYDmjlzTIJV8THnxNFtNKWotMIlzzN+CpqxqwXOECnUdOndmSeWntVrVcv5axWRQ==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@codemirror/state": "^6.0.0",
|
"@codemirror/state": "^6.0.0",
|
||||||
"@codemirror/view": "^6.23.0",
|
"@codemirror/view": "^6.23.0",
|
||||||
@ -147,9 +147,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@codemirror/view": {
|
"node_modules/@codemirror/view": {
|
||||||
"version": "6.28.1",
|
"version": "6.26.3",
|
||||||
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.28.1.tgz",
|
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.26.3.tgz",
|
||||||
"integrity": "sha512-BUWr+zCJpMkA/u69HlJmR+YkV4yPpM81HeMkOMZuwFa8iM5uJdEPKAs1icIRZKkKmy0Ub1x9/G3PQLTXdpBxrQ==",
|
"integrity": "sha512-gmqxkPALZjkgSxIeeweY/wGQXBfwTUaLs8h7OKtSwfbj9Ct3L11lD+u1sS7XHppxFQoMDiMDp07P9f3I2jWOHw==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@codemirror/state": "^6.4.0",
|
"@codemirror/state": "^6.4.0",
|
||||||
"style-mod": "^4.1.0",
|
"style-mod": "^4.1.0",
|
||||||
@ -238,9 +238,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@lezer/html": {
|
"node_modules/@lezer/html": {
|
||||||
"version": "1.3.10",
|
"version": "1.3.9",
|
||||||
"resolved": "https://registry.npmjs.org/@lezer/html/-/html-1.3.10.tgz",
|
"resolved": "https://registry.npmjs.org/@lezer/html/-/html-1.3.9.tgz",
|
||||||
"integrity": "sha512-dqpT8nISx/p9Do3AchvYGV3qYc4/rKr3IBZxlHmpIKam56P47RSHkSF5f13Vu9hebS1jM0HmtJIwLbWz1VIY6w==",
|
"integrity": "sha512-MXxeCMPyrcemSLGaTQEZx0dBUH0i+RPl8RN5GwMAzo53nTsd/Unc/t5ZxACeQoyPUM5/GkPLRUs2WliOImzkRA==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lezer/common": "^1.2.0",
|
"@lezer/common": "^1.2.0",
|
||||||
"@lezer/highlight": "^1.0.0",
|
"@lezer/highlight": "^1.0.0",
|
||||||
@ -248,9 +248,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@lezer/javascript": {
|
"node_modules/@lezer/javascript": {
|
||||||
"version": "1.4.17",
|
"version": "1.4.16",
|
||||||
"resolved": "https://registry.npmjs.org/@lezer/javascript/-/javascript-1.4.17.tgz",
|
"resolved": "https://registry.npmjs.org/@lezer/javascript/-/javascript-1.4.16.tgz",
|
||||||
"integrity": "sha512-bYW4ctpyGK+JMumDApeUzuIezX01H76R1foD6LcRX224FWfyYit/HYxiPGDjXXe/wQWASjCvVGoukTH68+0HIA==",
|
"integrity": "sha512-84UXR3N7s11MPQHWgMnjb9571fr19MmXnr5zTv2XX0gHXXUvW3uPJ8GCjKrfTXmSdfktjRK0ayKklw+A13rk4g==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lezer/common": "^1.2.0",
|
"@lezer/common": "^1.2.0",
|
||||||
"@lezer/highlight": "^1.1.3",
|
"@lezer/highlight": "^1.1.3",
|
||||||
@ -268,9 +268,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@lezer/lr": {
|
"node_modules/@lezer/lr": {
|
||||||
"version": "1.4.1",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.0.tgz",
|
||||||
"integrity": "sha512-CHsKq8DMKBf9b3yXPDIU4DbH+ZJd/sJdYOW2llbW/HudP5u0VS6Bfq1hLYfgU7uAYGFIyGGQIsSOXGPEErZiJw==",
|
"integrity": "sha512-Wst46p51km8gH0ZUmeNrtpRYmdlRHUpN1DQd3GFAyKANi8WVz8c2jHYTf1CVScFaCjQw1iO3ZZdqGDxQPRErTg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lezer/common": "^1.0.0"
|
"@lezer/common": "^1.0.0"
|
||||||
}
|
}
|
||||||
@ -545,9 +545,9 @@
|
|||||||
"integrity": "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q=="
|
"integrity": "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q=="
|
||||||
},
|
},
|
||||||
"node_modules/acorn": {
|
"node_modules/acorn": {
|
||||||
"version": "8.12.0",
|
"version": "8.11.3",
|
||||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.0.tgz",
|
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz",
|
||||||
"integrity": "sha512-RTvkC4w+KNXrM39/lWCUaG0IbRkWdCv7W/IOW9oU6SawyxulvkQy5HQPVTKxEjczcUvapcrw3cFx/60VN/NRNw==",
|
"integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"bin": {
|
"bin": {
|
||||||
"acorn": "bin/acorn"
|
"acorn": "bin/acorn"
|
||||||
@ -819,9 +819,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/terser": {
|
"node_modules/terser": {
|
||||||
"version": "5.31.1",
|
"version": "5.31.0",
|
||||||
"resolved": "https://registry.npmjs.org/terser/-/terser-5.31.1.tgz",
|
"resolved": "https://registry.npmjs.org/terser/-/terser-5.31.0.tgz",
|
||||||
"integrity": "sha512-37upzU1+viGvuFtBo9NPufCb9dwM0+l9hMxYyWfBA+fbwrPqNJAhbZ6W47bBFnZHKHTUBnMvi87434qq+qnxOg==",
|
"integrity": "sha512-Q1JFAoUKE5IMfI4Z/lkE/E6+SwgzO+x4tq4v1AyBLRj8VSYvRO6A/rQrPg1yud4g0En9EKI1TvFRF2tQFcoUkg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@jridgewell/source-map": "^0.3.3",
|
"@jridgewell/source-map": "^0.3.3",
|
||||||
|
2
deps/libbacktrace
vendored
2
deps/libbacktrace
vendored
Submodule deps/libbacktrace updated: 4ead348bb4...11427f31a6
6
deps/lit/lit-all.min.js
vendored
6
deps/lit/lit-all.min.js
vendored
File diff suppressed because one or more lines are too long
2
deps/lit/lit-all.min.js.map
vendored
2
deps/lit/lit-all.min.js.map
vendored
File diff suppressed because one or more lines are too long
8
flake.lock
generated
8
flake.lock
generated
@ -20,16 +20,16 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1717281328,
|
"lastModified": 1715395895,
|
||||||
"narHash": "sha256-evZPzpf59oNcDUXxh2GHcxHkTEG4fjae2ytWP85jXRo=",
|
"narHash": "sha256-DreMqi6+qa21ffLQqhMQL2XRUkAGt3N7iVB5FhJKie4=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "b3b2b28c1daa04fe2ae47c21bb76fd226eac4ca1",
|
"rev": "71bae31b7dbc335528ca7e96f479ec93462323ff",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"ref": "nixos-24.05",
|
"ref": "nixos-23.11",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
description = "Tilde Friends is a platform for making, running, and sharing web applications.";
|
description = "Tilde Friends is a platform for making, running, and sharing web applications.";
|
||||||
|
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
|
||||||
flake-utils.url = "github:numtide/flake-utils";
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -31,8 +31,6 @@
|
|||||||
openssl
|
openssl
|
||||||
llvmPackages_17.clang-unwrapped
|
llvmPackages_17.clang-unwrapped
|
||||||
unzip
|
unzip
|
||||||
doxygen
|
|
||||||
graphviz
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
21
package-lock.json
generated
21
package-lock.json
generated
@ -6,12 +6,29 @@
|
|||||||
"": {
|
"": {
|
||||||
"name": "tildefriends",
|
"name": "tildefriends",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"devDependencies": {
|
||||||
"prettier": "^3.2.5"
|
"husky": "9.0.11",
|
||||||
|
"prettier": "3.2.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/husky": {
|
||||||
|
"version": "9.0.11",
|
||||||
|
"resolved": "https://registry.npmjs.org/husky/-/husky-9.0.11.tgz",
|
||||||
|
"integrity": "sha512-AB6lFlbwwyIqMdHYhwPe+kjOC3Oc5P3nThEoW/AaO2BX3vJDjWPFxYLxokUZOo6RNX20He3AaT8sESs9NJcmEw==",
|
||||||
|
"dev": true,
|
||||||
|
"bin": {
|
||||||
|
"husky": "bin.mjs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/typicode"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/prettier": {
|
"node_modules/prettier": {
|
||||||
"version": "3.2.5",
|
"version": "3.2.5",
|
||||||
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bin": {
|
"bin": {
|
||||||
"prettier": "bin/prettier.cjs"
|
"prettier": "bin/prettier.cjs"
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "tildefriends",
|
"name": "tildefriends",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prettier": "prettier . --check --cache --write"
|
"prettier": "prettier . --check --cache --write",
|
||||||
|
"prepare": "husky"
|
||||||
},
|
},
|
||||||
"author": "Cory McWilliams",
|
"author": "Cory McWilliams",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"devDependencies": {
|
||||||
"prettier": "^3.2.5"
|
"prettier": "3.2.5",
|
||||||
|
"husky": "9.0.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.unprompted.tildefriends"
|
package="com.unprompted.tildefriends"
|
||||||
android:versionCode="21"
|
android:versionCode="20"
|
||||||
android:versionName="0.0.21-wip">
|
android:versionName="0.0.20-wip">
|
||||||
<uses-sdk android:minSdkVersion="24" android:targetSdkVersion="34"/>
|
<uses-sdk android:minSdkVersion="24" android:targetSdkVersion="34"/>
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
<application
|
<application
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
#include "ssb.h"
|
#include "ssb.h"
|
||||||
#include "task.h"
|
#include "task.h"
|
||||||
#include "util.js.h"
|
|
||||||
|
|
||||||
#include "sqlite3.h"
|
#include "sqlite3.h"
|
||||||
|
|
||||||
@ -51,7 +50,7 @@ void tf_database_register(JSContext* context)
|
|||||||
JS_SetPropertyStr(context, global, "Database", constructor);
|
JS_SetPropertyStr(context, global, "Database", constructor);
|
||||||
JSValue databases = JS_NewObject(context);
|
JSValue databases = JS_NewObject(context);
|
||||||
JS_SetPropertyStr(context, global, "databases", databases);
|
JS_SetPropertyStr(context, global, "databases", databases);
|
||||||
JS_SetPropertyStr(context, databases, "list", JS_NewCFunctionData(context, _databases_list, 1, 0, 0, NULL));
|
JS_SetPropertyStr(context, databases, "list", JS_NewCFunctionData(context, _databases_list, 0, 0, 0, NULL));
|
||||||
JS_FreeValue(context, global);
|
JS_FreeValue(context, global);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,455 +91,159 @@ static void _database_finalizer(JSRuntime* runtime, JSValue value)
|
|||||||
--_database_count;
|
--_database_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct _database_get_t
|
static JSValue _database_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
{
|
{
|
||||||
const char* id;
|
JSValue entry = JS_UNDEFINED;
|
||||||
const char* key;
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
||||||
size_t key_length;
|
if (database)
|
||||||
char* out_value;
|
|
||||||
size_t out_length;
|
|
||||||
JSValue promise[2];
|
|
||||||
} database_get_t;
|
|
||||||
|
|
||||||
static void _database_get_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
{
|
||||||
database_get_t* work = user_data;
|
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
||||||
sqlite3_stmt* statement;
|
sqlite3_stmt* statement;
|
||||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||||
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, work->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, work->key, work->key_length, NULL) == SQLITE_OK &&
|
size_t length;
|
||||||
|
const char* keyString = JS_ToCStringLen(context, &length, argv[0]);
|
||||||
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, keyString, length, NULL) == SQLITE_OK &&
|
||||||
sqlite3_step(statement) == SQLITE_ROW)
|
sqlite3_step(statement) == SQLITE_ROW)
|
||||||
{
|
{
|
||||||
size_t length = sqlite3_column_bytes(statement, 0);
|
entry = JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0));
|
||||||
char* data = tf_malloc(length + 1);
|
|
||||||
memcpy(data, sqlite3_column_text(statement, 0), length);
|
|
||||||
data[length] = '\0';
|
|
||||||
work->out_value = data;
|
|
||||||
work->out_length = length;
|
|
||||||
}
|
}
|
||||||
|
JS_FreeCString(context, keyString);
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
tf_ssb_release_db_reader(ssb, db);
|
tf_ssb_release_db_reader(ssb, db);
|
||||||
}
|
}
|
||||||
|
return entry;
|
||||||
static void _database_get_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
database_get_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = JS_UNDEFINED;
|
|
||||||
if (work->out_value)
|
|
||||||
{
|
|
||||||
result = JS_NewStringLen(context, work->out_value, work->out_length);
|
|
||||||
}
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
tf_free(work->out_value);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
|
||||||
|
|
||||||
static JSValue _database_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
||||||
{
|
|
||||||
JSValue result = JS_UNDEFINED;
|
|
||||||
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
|
||||||
if (database)
|
|
||||||
{
|
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
|
||||||
|
|
||||||
size_t length;
|
|
||||||
const char* key = JS_ToCStringLen(context, &length, argv[0]);
|
|
||||||
database_get_t* work = tf_malloc(sizeof(database_get_t) + strlen(database->id) + 1 + length + 1);
|
|
||||||
*work = (database_get_t) {
|
|
||||||
.id = (const char*)(work + 1),
|
|
||||||
.key = (const char*)(work + 1) + strlen(database->id) + 1,
|
|
||||||
.key_length = length,
|
|
||||||
};
|
|
||||||
memcpy((char*)work->id, database->id, strlen(database->id) + 1);
|
|
||||||
memcpy((char*)work->key, key, length + 1);
|
|
||||||
JS_FreeCString(context, key);
|
|
||||||
|
|
||||||
tf_ssb_run_work(ssb, _database_get_work, _database_get_after_work, work);
|
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _database_set_t
|
|
||||||
{
|
|
||||||
const char* id;
|
|
||||||
const char* key;
|
|
||||||
size_t key_length;
|
|
||||||
const char* value;
|
|
||||||
size_t value_length;
|
|
||||||
bool result;
|
|
||||||
JSValue promise[2];
|
|
||||||
} database_set_t;
|
|
||||||
|
|
||||||
static void _database_set_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
database_set_t* work = user_data;
|
|
||||||
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
||||||
sqlite3_stmt* statement;
|
|
||||||
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES (?1, ?2, ?3)", -1, &statement, NULL) == SQLITE_OK)
|
|
||||||
{
|
|
||||||
if (sqlite3_bind_text(statement, 1, work->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, work->key, work->key_length, NULL) == SQLITE_OK &&
|
|
||||||
sqlite3_bind_text(statement, 3, work->value, work->value_length, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_DONE)
|
|
||||||
{
|
|
||||||
work->result = true;
|
|
||||||
}
|
|
||||||
sqlite3_finalize(statement);
|
|
||||||
}
|
|
||||||
tf_ssb_release_db_writer(ssb, db);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _database_set_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
database_set_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = work->result ? JS_TRUE : JS_UNDEFINED;
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSValue _database_set(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
static JSValue _database_set(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
{
|
{
|
||||||
JSValue result = JS_UNDEFINED;
|
|
||||||
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
||||||
if (database)
|
if (database)
|
||||||
{
|
{
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
|
||||||
|
|
||||||
size_t key_length = 0;
|
|
||||||
const char* key = JS_ToCStringLen(context, &key_length, argv[0]);
|
|
||||||
size_t value_length = 0;
|
|
||||||
const char* value = JS_ToCStringLen(context, &value_length, argv[1]);
|
|
||||||
|
|
||||||
database_set_t* work = tf_malloc(sizeof(database_set_t) + strlen(database->id) + 1 + key_length + 1 + value_length + 1);
|
|
||||||
*work = (database_set_t) {
|
|
||||||
.id = (const char*)(work + 1),
|
|
||||||
.key = (const char*)(work + 1) + strlen(database->id) + 1,
|
|
||||||
.value = (const char*)(work + 1) + strlen(database->id) + 1 + key_length + 1,
|
|
||||||
.key_length = key_length,
|
|
||||||
.value_length = value_length,
|
|
||||||
};
|
|
||||||
|
|
||||||
memcpy((char*)work->id, database->id, strlen(database->id) + 1);
|
|
||||||
memcpy((char*)work->key, key, key_length + 1);
|
|
||||||
memcpy((char*)work->value, value, value_length + 1);
|
|
||||||
|
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
|
||||||
tf_ssb_run_work(ssb, _database_set_work, _database_set_after_work, work);
|
|
||||||
JS_FreeCString(context, key);
|
|
||||||
JS_FreeCString(context, value);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _database_exchange_t
|
|
||||||
{
|
|
||||||
const char* id;
|
|
||||||
const char* key;
|
|
||||||
size_t key_length;
|
|
||||||
const char* expected;
|
|
||||||
size_t expected_length;
|
|
||||||
const char* value;
|
|
||||||
size_t value_length;
|
|
||||||
bool result;
|
|
||||||
JSValue promise[2];
|
|
||||||
} database_exchange_t;
|
|
||||||
|
|
||||||
static void _database_exchange_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
database_exchange_t* work = user_data;
|
|
||||||
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
||||||
sqlite3_stmt* statement;
|
sqlite3_stmt* statement;
|
||||||
if (!work->expected)
|
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
||||||
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
||||||
|
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES (?1, ?2, ?3)", -1, &statement, NULL) == SQLITE_OK)
|
||||||
|
{
|
||||||
|
size_t keyLength;
|
||||||
|
const char* keyString = JS_ToCStringLen(context, &keyLength, argv[0]);
|
||||||
|
size_t valueLength;
|
||||||
|
const char* valueString = JS_ToCStringLen(context, &valueLength, argv[1]);
|
||||||
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, keyString, keyLength, NULL) == SQLITE_OK &&
|
||||||
|
sqlite3_bind_text(statement, 3, valueString, valueLength, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_OK)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
JS_FreeCString(context, keyString);
|
||||||
|
JS_FreeCString(context, valueString);
|
||||||
|
sqlite3_finalize(statement);
|
||||||
|
}
|
||||||
|
tf_ssb_release_db_writer(ssb, db);
|
||||||
|
}
|
||||||
|
return JS_UNDEFINED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSValue _database_exchange(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
|
{
|
||||||
|
JSValue exchanged = JS_UNDEFINED;
|
||||||
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
||||||
|
if (database)
|
||||||
|
{
|
||||||
|
sqlite3_stmt* statement;
|
||||||
|
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
||||||
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
||||||
|
if (JS_IsNull(argv[1]) || JS_IsUndefined(argv[1]))
|
||||||
{
|
{
|
||||||
if (sqlite3_prepare(db, "INSERT INTO properties (id, key, value) VALUES (?1, ?2, ?3) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK)
|
if (sqlite3_prepare(db, "INSERT INTO properties (id, key, value) VALUES (?1, ?2, ?3) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, work->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, work->key, work->key_length, NULL) == SQLITE_OK &&
|
size_t key_length;
|
||||||
sqlite3_bind_text(statement, 3, work->value, work->value_length, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_DONE)
|
size_t set_length;
|
||||||
|
const char* key = JS_ToCStringLen(context, &key_length, argv[0]);
|
||||||
|
const char* set = JS_ToCStringLen(context, &set_length, argv[2]);
|
||||||
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, key, key_length, NULL) == SQLITE_OK &&
|
||||||
|
sqlite3_bind_text(statement, 3, set, set_length, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_DONE)
|
||||||
{
|
{
|
||||||
work->result = sqlite3_changes(db) != 0;
|
exchanged = sqlite3_changes(db) != 0 ? JS_TRUE : JS_FALSE;
|
||||||
}
|
}
|
||||||
|
JS_FreeCString(context, key);
|
||||||
|
JS_FreeCString(context, set);
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (sqlite3_prepare(db, "UPDATE properties SET value = ?1 WHERE id = ?2 AND key = ?3 AND value = ?4", -1, &statement, NULL) == SQLITE_OK)
|
else if (sqlite3_prepare(db, "UPDATE properties SET value = ?1 WHERE id = ?2 AND key = ?3 AND value = ?4", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, work->value, work->value_length, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, work->id, -1, NULL) == SQLITE_OK &&
|
|
||||||
sqlite3_bind_text(statement, 3, work->key, work->key_length, NULL) == SQLITE_OK &&
|
|
||||||
sqlite3_bind_text(statement, 4, work->expected, work->expected_length, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_DONE)
|
|
||||||
{
|
|
||||||
work->result = sqlite3_changes(db) != 0;
|
|
||||||
}
|
|
||||||
sqlite3_finalize(statement);
|
|
||||||
}
|
|
||||||
tf_ssb_release_db_writer(ssb, db);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _database_exchange_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
database_exchange_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = work->result ? JS_TRUE : JS_UNDEFINED;
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
JS_FreeCString(context, work->key);
|
|
||||||
JS_FreeCString(context, work->expected);
|
|
||||||
JS_FreeCString(context, work->value);
|
|
||||||
tf_free((char*)work->id);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
|
||||||
|
|
||||||
static JSValue _database_exchange(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
||||||
{
|
|
||||||
JSValue result = JS_UNDEFINED;
|
|
||||||
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
|
||||||
if (database)
|
|
||||||
{
|
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
|
||||||
database_exchange_t* work = tf_malloc(sizeof(database_exchange_t));
|
|
||||||
*work = (database_exchange_t) {
|
|
||||||
.id = tf_strdup(database->id),
|
|
||||||
};
|
|
||||||
work->key = JS_ToCStringLen(context, &work->key_length, argv[0]);
|
|
||||||
work->expected = (JS_IsNull(argv[1]) || JS_IsUndefined(argv[1])) ? NULL : JS_ToCStringLen(context, &work->expected_length, argv[1]);
|
|
||||||
work->value = JS_ToCStringLen(context, &work->value_length, argv[2]);
|
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
|
||||||
tf_ssb_run_work(ssb, _database_exchange_work, _database_exchange_after_work, work);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _database_remove_t
|
|
||||||
{
|
|
||||||
const char* id;
|
|
||||||
size_t key_length;
|
size_t key_length;
|
||||||
JSValue promise[2];
|
size_t expected_length;
|
||||||
char key[];
|
size_t set_length;
|
||||||
} database_remove_t;
|
const char* key = JS_ToCStringLen(context, &key_length, argv[0]);
|
||||||
|
const char* expected = JS_ToCStringLen(context, &expected_length, argv[1]);
|
||||||
static void _database_remove_work(tf_ssb_t* ssb, void* user_data)
|
const char* set = JS_ToCStringLen(context, &set_length, argv[2]);
|
||||||
{
|
if (sqlite3_bind_text(statement, 1, set, set_length, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, database->id, -1, NULL) == SQLITE_OK &&
|
||||||
database_remove_t* work = user_data;
|
sqlite3_bind_text(statement, 3, key, key_length, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 4, expected, expected_length, NULL) == SQLITE_OK &&
|
||||||
sqlite3_stmt* statement;
|
sqlite3_step(statement) == SQLITE_DONE)
|
||||||
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
||||||
if (sqlite3_prepare(db, "DELETE FROM properties WHERE id = ?1 AND key = ?2", -1, &statement, NULL) == SQLITE_OK)
|
|
||||||
{
|
|
||||||
if (sqlite3_bind_text(statement, 1, work->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, work->key, work->key_length, NULL) == SQLITE_OK &&
|
|
||||||
sqlite3_step(statement) == SQLITE_OK)
|
|
||||||
{
|
{
|
||||||
|
exchanged = sqlite3_changes(db) != 0 ? JS_TRUE : JS_FALSE;
|
||||||
}
|
}
|
||||||
|
JS_FreeCString(context, key);
|
||||||
|
JS_FreeCString(context, expected);
|
||||||
|
JS_FreeCString(context, set);
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
tf_ssb_release_db_writer(ssb, db);
|
tf_ssb_release_db_writer(ssb, db);
|
||||||
}
|
}
|
||||||
|
return exchanged;
|
||||||
static void _database_remove_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
database_remove_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = JS_UNDEFINED;
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
tf_free((char*)work->id);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSValue _database_remove(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
static JSValue _database_remove(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
{
|
{
|
||||||
JSValue result = JS_UNDEFINED;
|
|
||||||
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
||||||
if (database)
|
if (database)
|
||||||
{
|
{
|
||||||
size_t key_length = 0;
|
|
||||||
const char* key = JS_ToCStringLen(context, &key_length, argv[0]);
|
|
||||||
|
|
||||||
database_remove_t* work = tf_malloc(sizeof(database_remove_t) + key_length + 1);
|
|
||||||
*work = (database_remove_t) {
|
|
||||||
.id = tf_strdup(database->id),
|
|
||||||
.key_length = key_length,
|
|
||||||
};
|
|
||||||
memcpy(work->key, key, key_length + 1);
|
|
||||||
JS_FreeCString(context, key);
|
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
|
||||||
tf_ssb_run_work(tf_task_get_ssb(database->task), _database_remove_work, _database_remove_after_work, work);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _database_get_all_t
|
|
||||||
{
|
|
||||||
const char* id;
|
|
||||||
const char* key;
|
|
||||||
size_t key_length;
|
|
||||||
char** out_values;
|
|
||||||
size_t* out_lengths;
|
|
||||||
int out_values_length;
|
|
||||||
JSValue promise[2];
|
|
||||||
} database_get_all_t;
|
|
||||||
|
|
||||||
static void _database_get_all_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
database_get_all_t* work = user_data;
|
|
||||||
sqlite3_stmt* statement;
|
sqlite3_stmt* statement;
|
||||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
||||||
if (sqlite3_prepare(db, "SELECT key FROM properties WHERE id = ?", -1, &statement, NULL) == SQLITE_OK)
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
||||||
|
if (sqlite3_prepare(db, "DELETE FROM properties WHERE id = ?1 AND key = ?2", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, work->id, -1, NULL) == SQLITE_OK)
|
size_t keyLength;
|
||||||
|
const char* keyString = JS_ToCStringLen(context, &keyLength, argv[0]);
|
||||||
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, keyString, keyLength, NULL) == SQLITE_OK &&
|
||||||
|
sqlite3_step(statement) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
while (sqlite3_step(statement) == SQLITE_ROW)
|
|
||||||
{
|
|
||||||
work->out_values = tf_resize_vec(work->out_values, sizeof(char*) * (work->out_values_length + 1));
|
|
||||||
work->out_lengths = tf_resize_vec(work->out_lengths, sizeof(size_t) * (work->out_values_length + 1));
|
|
||||||
size_t length = sqlite3_column_bytes(statement, 0);
|
|
||||||
char* data = tf_malloc(length + 1);
|
|
||||||
memcpy(data, sqlite3_column_text(statement, 0), length);
|
|
||||||
data[length] = '\0';
|
|
||||||
work->out_values[work->out_values_length] = data;
|
|
||||||
work->out_lengths[work->out_values_length] = length;
|
|
||||||
work->out_values_length++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
JS_FreeCString(context, keyString);
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
tf_ssb_release_db_reader(ssb, db);
|
tf_ssb_release_db_writer(ssb, db);
|
||||||
}
|
}
|
||||||
|
return JS_UNDEFINED;
|
||||||
static void _database_get_all_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
database_get_all_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = JS_NewArray(context);
|
|
||||||
;
|
|
||||||
for (int i = 0; i < work->out_values_length; i++)
|
|
||||||
{
|
|
||||||
JS_SetPropertyUint32(context, result, i, JS_NewStringLen(context, work->out_values[i], work->out_lengths[i]));
|
|
||||||
tf_free((void*)work->out_values[i]);
|
|
||||||
}
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
tf_free(work->out_values);
|
|
||||||
tf_free(work->out_lengths);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSValue _database_get_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
static JSValue _database_get_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
{
|
{
|
||||||
JSValue result = JS_UNDEFINED;
|
JSValue array = JS_UNDEFINED;
|
||||||
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
||||||
if (database)
|
if (database)
|
||||||
{
|
{
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
|
||||||
|
|
||||||
size_t length;
|
|
||||||
const char* key = JS_ToCStringLen(context, &length, argv[0]);
|
|
||||||
database_get_all_t* work = tf_malloc(sizeof(database_get_all_t) + strlen(database->id) + 1 + length + 1);
|
|
||||||
*work = (database_get_all_t) {
|
|
||||||
.id = (const char*)(work + 1),
|
|
||||||
.key = (const char*)(work + 1) + strlen(database->id) + 1,
|
|
||||||
.key_length = length,
|
|
||||||
};
|
|
||||||
memcpy((char*)work->id, database->id, strlen(database->id) + 1);
|
|
||||||
memcpy((char*)work->key, key, length + 1);
|
|
||||||
JS_FreeCString(context, key);
|
|
||||||
|
|
||||||
tf_ssb_run_work(ssb, _database_get_all_work, _database_get_all_after_work, work);
|
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _key_value_t
|
|
||||||
{
|
|
||||||
char* key;
|
|
||||||
size_t key_length;
|
|
||||||
char* value;
|
|
||||||
size_t value_length;
|
|
||||||
} key_value_t;
|
|
||||||
|
|
||||||
typedef struct _database_get_like_t
|
|
||||||
{
|
|
||||||
const char* id;
|
|
||||||
const char* pattern;
|
|
||||||
key_value_t* results;
|
|
||||||
int results_length;
|
|
||||||
JSValue promise[2];
|
|
||||||
} database_get_like_t;
|
|
||||||
|
|
||||||
static void _database_get_like_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
database_get_like_t* work = user_data;
|
|
||||||
sqlite3_stmt* statement;
|
sqlite3_stmt* statement;
|
||||||
|
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
||||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||||
if (sqlite3_prepare(db, "SELECT key, value FROM properties WHERE id = ? AND KEY LIKE ?", -1, &statement, NULL) == SQLITE_OK)
|
if (sqlite3_prepare(db, "SELECT key, value FROM properties WHERE id = ?1", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, work->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, work->pattern, -1, NULL) == SQLITE_OK)
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
|
array = JS_NewArray(context);
|
||||||
|
uint32_t index = 0;
|
||||||
while (sqlite3_step(statement) == SQLITE_ROW)
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
||||||
{
|
{
|
||||||
work->results = tf_resize_vec(work->results, sizeof(key_value_t) * (work->results_length + 1));
|
JS_SetPropertyUint32(context, array, index++, JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0)));
|
||||||
key_value_t* out = &work->results[work->results_length];
|
|
||||||
*out = (key_value_t) {
|
|
||||||
.key_length = sqlite3_column_bytes(statement, 0),
|
|
||||||
.value_length = sqlite3_column_bytes(statement, 1),
|
|
||||||
};
|
|
||||||
out->key = tf_malloc(out->key_length + 1);
|
|
||||||
memcpy(out->key, sqlite3_column_text(statement, 0), out->key_length + 1);
|
|
||||||
out->value = tf_malloc(out->value_length + 1);
|
|
||||||
memcpy(out->value, sqlite3_column_text(statement, 1), out->value_length + 1);
|
|
||||||
work->results_length++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
tf_ssb_release_db_reader(ssb, db);
|
tf_ssb_release_db_reader(ssb, db);
|
||||||
}
|
}
|
||||||
|
return array;
|
||||||
static void _database_get_like_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
database_get_like_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = JS_NewObject(context);
|
|
||||||
for (int i = 0; i < work->results_length; i++)
|
|
||||||
{
|
|
||||||
const key_value_t* row = &work->results[i];
|
|
||||||
JS_SetPropertyStr(context, result, row->key, JS_NewStringLen(context, row->value, row->value_length));
|
|
||||||
tf_free(row->key);
|
|
||||||
tf_free(row->value);
|
|
||||||
}
|
|
||||||
JS_FreeCString(context, work->pattern);
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
tf_free((void*)work->id);
|
|
||||||
tf_free(work->results);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
@ -549,77 +252,51 @@ static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int
|
|||||||
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
database_t* database = JS_GetOpaque(this_val, _database_class_id);
|
||||||
if (database)
|
if (database)
|
||||||
{
|
{
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
|
||||||
database_get_like_t* work = tf_malloc(sizeof(database_get_like_t));
|
|
||||||
*work = (database_get_like_t) {
|
|
||||||
.id = tf_strdup(database->id),
|
|
||||||
.pattern = JS_ToCString(context, argv[0]),
|
|
||||||
};
|
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
|
||||||
tf_ssb_run_work(ssb, _database_get_like_work, _database_get_like_after_work, work);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _databases_list_t
|
|
||||||
{
|
|
||||||
const char* pattern;
|
|
||||||
char** names;
|
|
||||||
int names_length;
|
|
||||||
JSValue promise[2];
|
|
||||||
} databases_list_t;
|
|
||||||
|
|
||||||
static void _databases_list_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
databases_list_t* work = user_data;
|
|
||||||
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
||||||
sqlite3_stmt* statement;
|
sqlite3_stmt* statement;
|
||||||
if (sqlite3_prepare(db, "SELECT DISTINCT id FROM properties WHERE id LIKE ?", -1, &statement, NULL) == SQLITE_OK)
|
tf_ssb_t* ssb = tf_task_get_ssb(database->task);
|
||||||
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||||
|
if (sqlite3_prepare(db, "SELECT key, value FROM properties WHERE id = ? AND KEY LIKE ?", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, work->pattern, -1, NULL) == SQLITE_OK)
|
const char* pattern = JS_ToCString(context, argv[0]);
|
||||||
|
if (sqlite3_bind_text(statement, 1, database->id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, pattern, -1, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
|
result = JS_NewObject(context);
|
||||||
while (sqlite3_step(statement) == SQLITE_ROW)
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
||||||
{
|
{
|
||||||
work->names = tf_resize_vec(work->names, sizeof(char*) * (work->names_length + 1));
|
JS_SetPropertyStr(context, result, (const char*)sqlite3_column_text(statement, 0),
|
||||||
work->names[work->names_length] = tf_strdup((const char*)sqlite3_column_text(statement, 0));
|
JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 1), sqlite3_column_bytes(statement, 1)));
|
||||||
work->names_length++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
JS_FreeCString(context, pattern);
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
tf_ssb_release_db_reader(ssb, db);
|
tf_ssb_release_db_reader(ssb, db);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
static void _databases_list_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
databases_list_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue result = JS_NewArray(context);
|
|
||||||
for (int i = 0; i < work->names_length; i++)
|
|
||||||
{
|
|
||||||
JS_SetPropertyUint32(context, result, i, JS_NewString(context, work->names[i]));
|
|
||||||
tf_free(work->names[i]);
|
|
||||||
}
|
|
||||||
JS_FreeCString(context, work->pattern);
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, result);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
tf_free(work->names);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
|
static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
|
||||||
{
|
{
|
||||||
tf_task_t* task = tf_task_get(context);
|
tf_task_t* task = tf_task_get(context);
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(task);
|
tf_ssb_t* ssb = tf_task_get_ssb(task);
|
||||||
databases_list_t* work = tf_malloc(sizeof(databases_list_t));
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
||||||
*work = (databases_list_t) {
|
JSValue array = JS_UNDEFINED;
|
||||||
.pattern = JS_ToCString(context, argv[0]),
|
sqlite3_stmt* statement;
|
||||||
};
|
if (sqlite3_prepare(db, "SELECT DISTINCT id FROM properties WHERE id LIKE ?", -1, &statement, NULL) == SQLITE_OK)
|
||||||
JSValue result = JS_NewPromiseCapability(context, work->promise);
|
{
|
||||||
tf_ssb_run_work(ssb, _databases_list_work, _databases_list_after_work, work);
|
const char* pattern = JS_ToCString(context, argv[0]);
|
||||||
return result;
|
if (sqlite3_bind_text(statement, 1, pattern, -1, NULL) == SQLITE_OK)
|
||||||
|
{
|
||||||
|
array = JS_NewArray(context);
|
||||||
|
uint32_t index = 0;
|
||||||
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
||||||
|
{
|
||||||
|
JS_SetPropertyUint32(context, array, index++, JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JS_FreeCString(context, pattern);
|
||||||
|
sqlite3_finalize(statement);
|
||||||
|
}
|
||||||
|
tf_ssb_release_db_reader(ssb, db);
|
||||||
|
return array;
|
||||||
}
|
}
|
||||||
|
@ -1019,7 +1019,7 @@ void tf_http_request_unref(tf_http_request_t* request)
|
|||||||
tf_free(request);
|
tf_free(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection && --connection->ref_count == 0)
|
if (--connection->ref_count == 0)
|
||||||
{
|
{
|
||||||
if (connection->http->is_shutting_down)
|
if (connection->http->is_shutting_down)
|
||||||
{
|
{
|
||||||
|
306
src/httpd.js.c
306
src/httpd.js.c
@ -37,7 +37,7 @@
|
|||||||
|
|
||||||
const int64_t k_refresh_interval = 1ULL * 7 * 24 * 60 * 60 * 1000;
|
const int64_t k_refresh_interval = 1ULL * 7 * 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
static JSValue _authenticate_jwt(tf_ssb_t* ssb, JSContext* context, const char* jwt);
|
static JSValue _authenticate_jwt(JSContext* context, const char* jwt);
|
||||||
static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
||||||
static const char* _make_session_jwt(tf_ssb_t* ssb, const char* name);
|
static const char* _make_session_jwt(tf_ssb_t* ssb, const char* name);
|
||||||
static const char* _make_set_session_cookie_header(tf_http_request_t* request, const char* session_cookie);
|
static const char* _make_set_session_cookie_header(tf_http_request_t* request, const char* session_cookie);
|
||||||
@ -330,7 +330,7 @@ static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_va
|
|||||||
|
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(tf_task_get(context));
|
tf_ssb_t* ssb = tf_task_get_ssb(tf_task_get(context));
|
||||||
const char* session = tf_http_get_cookie(tf_http_request_get_header(request, "cookie"), "session");
|
const char* session = tf_http_get_cookie(tf_http_request_get_header(request, "cookie"), "session");
|
||||||
JSValue jwt = _authenticate_jwt(ssb, context, session);
|
JSValue jwt = _authenticate_jwt(context, session);
|
||||||
tf_free((void*)session);
|
tf_free((void*)session);
|
||||||
JSValue name = !JS_IsUndefined(jwt) ? JS_GetPropertyStr(context, jwt, "name") : JS_UNDEFINED;
|
JSValue name = !JS_IsUndefined(jwt) ? JS_GetPropertyStr(context, jwt, "name") : JS_UNDEFINED;
|
||||||
const char* name_string = !JS_IsUndefined(name) ? JS_ToCString(context, name) : NULL;
|
const char* name_string = !JS_IsUndefined(name) ? JS_ToCString(context, name) : NULL;
|
||||||
@ -446,55 +446,6 @@ static JSValue _httpd_set_http_redirect(JSContext* context, JSValueConst this_va
|
|||||||
return JS_UNDEFINED;
|
return JS_UNDEFINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct _auth_query_work_t
|
|
||||||
{
|
|
||||||
const char* settings;
|
|
||||||
JSValue entry;
|
|
||||||
JSValue result;
|
|
||||||
JSValue promise[2];
|
|
||||||
} auth_query_work_t;
|
|
||||||
|
|
||||||
static void _httpd_auth_query_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
auth_query_work_t* work = user_data;
|
|
||||||
work->settings = tf_ssb_db_get_property(ssb, "core", "settings");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _httpd_auth_query_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
auth_query_work_t* work = user_data;
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
JSValue name = JS_GetPropertyStr(context, work->entry, "name");
|
|
||||||
const char* name_string = JS_ToCString(context, name);
|
|
||||||
JSValue settings_value = work->settings ? JS_ParseJSON(context, work->settings, strlen(work->settings), NULL) : JS_UNDEFINED;
|
|
||||||
JSValue out_permissions = JS_NewObject(context);
|
|
||||||
JS_SetPropertyStr(context, work->result, "permissions", out_permissions);
|
|
||||||
JSValue permissions = !JS_IsUndefined(settings_value) ? JS_GetPropertyStr(context, settings_value, "permissions") : JS_UNDEFINED;
|
|
||||||
JSValue user_permissions = !JS_IsUndefined(permissions) ? JS_GetPropertyStr(context, permissions, name_string) : JS_UNDEFINED;
|
|
||||||
int length = !JS_IsUndefined(user_permissions) ? tf_util_get_length(context, user_permissions) : 0;
|
|
||||||
for (int i = 0; i < length; i++)
|
|
||||||
{
|
|
||||||
JSValue permission = JS_GetPropertyUint32(context, user_permissions, i);
|
|
||||||
const char* permission_string = JS_ToCString(context, permission);
|
|
||||||
JS_SetPropertyStr(context, out_permissions, permission_string, JS_TRUE);
|
|
||||||
JS_FreeCString(context, permission_string);
|
|
||||||
JS_FreeValue(context, permission);
|
|
||||||
}
|
|
||||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &work->result);
|
|
||||||
JS_FreeValue(context, work->result);
|
|
||||||
tf_util_report_error(context, error);
|
|
||||||
JS_FreeValue(context, error);
|
|
||||||
JS_FreeValue(context, work->promise[0]);
|
|
||||||
JS_FreeValue(context, work->promise[1]);
|
|
||||||
JS_FreeValue(context, user_permissions);
|
|
||||||
JS_FreeValue(context, permissions);
|
|
||||||
JS_FreeValue(context, settings_value);
|
|
||||||
tf_free((void*)work->settings);
|
|
||||||
JS_FreeCString(context, name_string);
|
|
||||||
JS_FreeValue(context, name);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
|
||||||
|
|
||||||
static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||||
{
|
{
|
||||||
tf_task_t* task = tf_task_get(context);
|
tf_task_t* task = tf_task_get(context);
|
||||||
@ -508,7 +459,7 @@ static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int
|
|||||||
JSValue cookie = JS_GetPropertyStr(context, headers, "cookie");
|
JSValue cookie = JS_GetPropertyStr(context, headers, "cookie");
|
||||||
const char* cookie_string = JS_ToCString(context, cookie);
|
const char* cookie_string = JS_ToCString(context, cookie);
|
||||||
const char* session = tf_http_get_cookie(cookie_string, "session");
|
const char* session = tf_http_get_cookie(cookie_string, "session");
|
||||||
JSValue entry = _authenticate_jwt(ssb, context, session);
|
JSValue entry = _authenticate_jwt(context, session);
|
||||||
tf_free((void*)session);
|
tf_free((void*)session);
|
||||||
JS_FreeCString(context, cookie_string);
|
JS_FreeCString(context, cookie_string);
|
||||||
JS_FreeValue(context, cookie);
|
JS_FreeValue(context, cookie);
|
||||||
@ -516,16 +467,33 @@ static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int
|
|||||||
JSValue result = JS_UNDEFINED;
|
JSValue result = JS_UNDEFINED;
|
||||||
if (!JS_IsUndefined(entry))
|
if (!JS_IsUndefined(entry))
|
||||||
{
|
{
|
||||||
JSValue value = JS_NewObject(context);
|
result = JS_NewObject(context);
|
||||||
JS_SetPropertyStr(context, value, "session", entry);
|
JS_SetPropertyStr(context, result, "session", entry);
|
||||||
|
JSValue out_permissions = JS_NewObject(context);
|
||||||
|
JS_SetPropertyStr(context, result, "permissions", out_permissions);
|
||||||
|
|
||||||
auth_query_work_t* work = tf_malloc(sizeof(auth_query_work_t));
|
JSValue name = JS_GetPropertyStr(context, entry, "name");
|
||||||
*work = (auth_query_work_t) {
|
const char* name_string = JS_ToCString(context, name);
|
||||||
.entry = entry,
|
|
||||||
.result = value,
|
const char* settings = tf_ssb_db_get_property(ssb, "core", "settings");
|
||||||
};
|
JSValue settings_value = settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED;
|
||||||
result = JS_NewPromiseCapability(context, work->promise);
|
JSValue permissions = !JS_IsUndefined(settings_value) ? JS_GetPropertyStr(context, settings_value, "permissions") : JS_UNDEFINED;
|
||||||
tf_ssb_run_work(ssb, _httpd_auth_query_work, _httpd_auth_query_after_work, work);
|
JSValue user_permissions = !JS_IsUndefined(permissions) ? JS_GetPropertyStr(context, permissions, name_string) : JS_UNDEFINED;
|
||||||
|
int length = !JS_IsUndefined(user_permissions) ? tf_util_get_length(context, user_permissions) : 0;
|
||||||
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
JSValue permission = JS_GetPropertyUint32(context, user_permissions, i);
|
||||||
|
const char* permission_string = JS_ToCString(context, permission);
|
||||||
|
JS_SetPropertyStr(context, out_permissions, permission_string, JS_TRUE);
|
||||||
|
JS_FreeCString(context, permission_string);
|
||||||
|
JS_FreeValue(context, permission);
|
||||||
|
}
|
||||||
|
JS_FreeValue(context, user_permissions);
|
||||||
|
JS_FreeValue(context, permissions);
|
||||||
|
JS_FreeValue(context, settings_value);
|
||||||
|
tf_free((void*)settings);
|
||||||
|
JS_FreeCString(context, name_string);
|
||||||
|
JS_FreeValue(context, name);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -884,7 +852,7 @@ static void _httpd_endpoint_static(tf_http_request_t* request)
|
|||||||
const char* k_static_files[] = {
|
const char* k_static_files[] = {
|
||||||
"index.html",
|
"index.html",
|
||||||
"client.js",
|
"client.js",
|
||||||
"tildefriends.svg",
|
"favicon.png",
|
||||||
"jszip.min.js",
|
"jszip.min.js",
|
||||||
"style.css",
|
"style.css",
|
||||||
"tfrpc.js",
|
"tfrpc.js",
|
||||||
@ -1094,17 +1062,13 @@ const char* _form_data_get(const char** form_data, const char* key)
|
|||||||
typedef struct _login_request_t
|
typedef struct _login_request_t
|
||||||
{
|
{
|
||||||
tf_http_request_t* request;
|
tf_http_request_t* request;
|
||||||
|
const char* session_cookie;
|
||||||
|
JSValue jwt;
|
||||||
const char* name;
|
const char* name;
|
||||||
const char* error;
|
const char* error;
|
||||||
const char* settings;
|
|
||||||
const char* code_of_conduct;
|
const char* code_of_conduct;
|
||||||
bool have_administrator;
|
bool have_administrator;
|
||||||
bool session_is_new;
|
bool session_is_new;
|
||||||
|
|
||||||
char location_header[1024];
|
|
||||||
const char* set_cookie_header;
|
|
||||||
|
|
||||||
int pending;
|
|
||||||
} login_request_t;
|
} login_request_t;
|
||||||
|
|
||||||
static const char* _make_set_session_cookie_header(tf_http_request_t* request, const char* session_cookie)
|
static const char* _make_set_session_cookie_header(tf_http_request_t* request, const char* session_cookie)
|
||||||
@ -1119,29 +1083,18 @@ static const char* _make_set_session_cookie_header(tf_http_request_t* request, c
|
|||||||
return cookie;
|
return cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _login_release(login_request_t* login)
|
|
||||||
{
|
|
||||||
int ref_count = --login->pending;
|
|
||||||
if (ref_count == 0)
|
|
||||||
{
|
|
||||||
tf_free((void*)login->name);
|
|
||||||
tf_free((void*)login->code_of_conduct);
|
|
||||||
tf_free((void*)login->set_cookie_header);
|
|
||||||
tf_free(login);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _httpd_endpoint_login_file_read_callback(tf_task_t* task, const char* path, int result, const void* data, void* user_data)
|
static void _httpd_endpoint_login_file_read_callback(tf_task_t* task, const char* path, int result, const void* data, void* user_data)
|
||||||
{
|
{
|
||||||
login_request_t* login = user_data;
|
login_request_t* login = user_data;
|
||||||
tf_http_request_t* request = login->request;
|
tf_http_request_t* request = login->request;
|
||||||
if (result >= 0)
|
if (result >= 0)
|
||||||
{
|
{
|
||||||
|
const char* cookie = _make_set_session_cookie_header(request, login->session_cookie);
|
||||||
const char* headers[] = {
|
const char* headers[] = {
|
||||||
"Content-Type",
|
"Content-Type",
|
||||||
"text/html; charset=utf-8",
|
"text/html; charset=utf-8",
|
||||||
"Set-Cookie",
|
"Set-Cookie",
|
||||||
login->set_cookie_header ? login->set_cookie_header : "",
|
cookie ? cookie : "",
|
||||||
};
|
};
|
||||||
const char* replace_me = "$AUTH_DATA";
|
const char* replace_me = "$AUTH_DATA";
|
||||||
const char* auth = strstr(data, replace_me);
|
const char* auth = strstr(data, replace_me);
|
||||||
@ -1175,6 +1128,7 @@ static void _httpd_endpoint_login_file_read_callback(tf_task_t* task, const char
|
|||||||
{
|
{
|
||||||
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, data, result);
|
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, data, result);
|
||||||
}
|
}
|
||||||
|
tf_free((void*)cookie);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1182,7 +1136,10 @@ static void _httpd_endpoint_login_file_read_callback(tf_task_t* task, const char
|
|||||||
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
|
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
|
||||||
}
|
}
|
||||||
tf_http_request_unref(request);
|
tf_http_request_unref(request);
|
||||||
_login_release(login);
|
tf_free((void*)login->name);
|
||||||
|
tf_free((void*)login->code_of_conduct);
|
||||||
|
tf_free((void*)login->session_cookie);
|
||||||
|
tf_free(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _string_property_equals(JSContext* context, JSValue object, const char* name, const char* value)
|
static bool _string_property_equals(JSContext* context, JSValue object, const char* name, const char* value)
|
||||||
@ -1195,7 +1152,12 @@ static bool _string_property_equals(JSContext* context, JSValue object, const ch
|
|||||||
return equals;
|
return equals;
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSValue _authenticate_jwt(tf_ssb_t* ssb, JSContext* context, const char* jwt)
|
static void _public_key_visit(const char* identity, void* user_data)
|
||||||
|
{
|
||||||
|
snprintf(user_data, k_id_base64_len, "%s", identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSValue _authenticate_jwt(JSContext* context, const char* jwt)
|
||||||
{
|
{
|
||||||
if (!jwt)
|
if (!jwt)
|
||||||
{
|
{
|
||||||
@ -1236,8 +1198,10 @@ static JSValue _authenticate_jwt(tf_ssb_t* ssb, JSContext* context, const char*
|
|||||||
return JS_UNDEFINED;
|
return JS_UNDEFINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tf_task_t* task = tf_task_get(context);
|
||||||
|
tf_ssb_t* ssb = tf_task_get_ssb(task);
|
||||||
char public_key_b64[k_id_base64_len] = { 0 };
|
char public_key_b64[k_id_base64_len] = { 0 };
|
||||||
tf_ssb_whoami(ssb, public_key_b64, sizeof(public_key_b64));
|
tf_ssb_db_identity_visit(ssb, ":admin", _public_key_visit, public_key_b64);
|
||||||
|
|
||||||
const char* payload = jwt + dot[0] + 1;
|
const char* payload = jwt + dot[0] + 1;
|
||||||
size_t payload_length = dot[1] - dot[0] - 1;
|
size_t payload_length = dot[1] - dot[0] - 1;
|
||||||
@ -1296,6 +1260,25 @@ static bool _is_name_valid(const char* name)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _visit_auth_identity(const char* identity, void* user_data)
|
||||||
|
{
|
||||||
|
if (!*(char*)user_data)
|
||||||
|
{
|
||||||
|
snprintf((char*)user_data, k_id_base64_len, "%s", identity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _get_auth_private_key(tf_ssb_t* ssb, uint8_t* out_private_key)
|
||||||
|
{
|
||||||
|
char id[k_id_base64_len] = { 0 };
|
||||||
|
tf_ssb_db_identity_visit(ssb, ":admin", _visit_auth_identity, id);
|
||||||
|
if (*id)
|
||||||
|
{
|
||||||
|
return tf_ssb_db_identity_get_private_key(ssb, ":admin", id, out_private_key, crypto_sign_SECRETKEYBYTES);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static const char* _make_session_jwt(tf_ssb_t* ssb, const char* name)
|
static const char* _make_session_jwt(tf_ssb_t* ssb, const char* name)
|
||||||
{
|
{
|
||||||
if (!name || !*name)
|
if (!name || !*name)
|
||||||
@ -1326,8 +1309,8 @@ static const char* _make_session_jwt(tf_ssb_t* ssb, const char* name)
|
|||||||
char signature_base64[256] = { 0 };
|
char signature_base64[256] = { 0 };
|
||||||
|
|
||||||
uint8_t private_key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
uint8_t private_key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
||||||
tf_ssb_get_private_key(ssb, private_key, sizeof(private_key));
|
if (_get_auth_private_key(ssb, private_key))
|
||||||
|
{
|
||||||
if (crypto_sign_detached(signature, &signature_length, (const uint8_t*)payload_base64, strlen(payload_base64), private_key) == 0)
|
if (crypto_sign_detached(signature, &signature_length, (const uint8_t*)payload_base64, strlen(payload_base64), private_key) == 0)
|
||||||
{
|
{
|
||||||
sodium_bin2base64(signature_base64, sizeof(signature_base64), signature, sizeof(signature), sodium_base64_VARIANT_URLSAFE_NO_PADDING);
|
sodium_bin2base64(signature_base64, sizeof(signature_base64), signature, sizeof(signature), sodium_base64_VARIANT_URLSAFE_NO_PADDING);
|
||||||
@ -1336,6 +1319,7 @@ static const char* _make_session_jwt(tf_ssb_t* ssb, const char* name)
|
|||||||
snprintf(result, size, "%s.%s.%s", header_base64, payload_base64, signature_base64);
|
snprintf(result, size, "%s.%s.%s", header_base64, payload_base64, signature_base64);
|
||||||
}
|
}
|
||||||
sodium_memzero(private_key, sizeof(private_key));
|
sodium_memzero(private_key, sizeof(private_key));
|
||||||
|
}
|
||||||
|
|
||||||
JS_FreeCString(context, payload_string);
|
JS_FreeCString(context, payload_string);
|
||||||
JS_FreeValue(context, payload_json);
|
JS_FreeValue(context, payload_json);
|
||||||
@ -1351,10 +1335,26 @@ static bool _verify_password(const char* password, const char* hash)
|
|||||||
return out_hash && strcmp(hash, out_hash) == 0;
|
return out_hash && strcmp(hash, out_hash) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _make_administrator_if_first(tf_ssb_t* ssb, JSContext* context, const char* account_name_copy, bool may_become_first_admin)
|
static const char* _get_code_of_conduct(tf_ssb_t* ssb)
|
||||||
{
|
{
|
||||||
|
JSContext* context = tf_ssb_get_context(ssb);
|
||||||
const char* settings = tf_ssb_db_get_property(ssb, "core", "settings");
|
const char* settings = tf_ssb_db_get_property(ssb, "core", "settings");
|
||||||
JSValue settings_value = settings && *settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED;
|
JSValue settings_value = settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED;
|
||||||
|
JSValue code_of_conduct_value = JS_GetPropertyStr(context, settings_value, "code_of_conduct");
|
||||||
|
const char* code_of_conduct = JS_ToCString(context, code_of_conduct_value);
|
||||||
|
const char* result = tf_strdup(code_of_conduct);
|
||||||
|
JS_FreeCString(context, code_of_conduct);
|
||||||
|
JS_FreeValue(context, code_of_conduct_value);
|
||||||
|
JS_FreeValue(context, settings_value);
|
||||||
|
tf_free((void*)settings);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _make_administrator_if_first(tf_ssb_t* ssb, const char* account_name_copy, bool may_become_first_admin)
|
||||||
|
{
|
||||||
|
JSContext* context = tf_ssb_get_context(ssb);
|
||||||
|
const char* settings = tf_ssb_db_get_property(ssb, "core", "settings");
|
||||||
|
JSValue settings_value = settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED;
|
||||||
if (JS_IsUndefined(settings_value))
|
if (JS_IsUndefined(settings_value))
|
||||||
{
|
{
|
||||||
settings_value = JS_NewObject(context);
|
settings_value = JS_NewObject(context);
|
||||||
@ -1423,32 +1423,30 @@ static bool _make_administrator_if_first(tf_ssb_t* ssb, JSContext* context, cons
|
|||||||
return have_administrator;
|
return have_administrator;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _httpd_endpoint_login_work(tf_ssb_t* ssb, void* user_data)
|
static void _httpd_endpoint_login(tf_http_request_t* request)
|
||||||
{
|
{
|
||||||
login_request_t* login = user_data;
|
tf_task_t* task = request->user_data;
|
||||||
tf_http_request_t* request = login->request;
|
JSContext* context = tf_task_get_context(task);
|
||||||
|
tf_ssb_t* ssb = tf_task_get_ssb(task);
|
||||||
JSMallocFunctions funcs = { 0 };
|
|
||||||
tf_get_js_malloc_functions(&funcs);
|
|
||||||
JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL);
|
|
||||||
JSContext* context = JS_NewContext(runtime);
|
|
||||||
|
|
||||||
const char* session = tf_http_get_cookie(tf_http_request_get_header(request, "cookie"), "session");
|
const char* session = tf_http_get_cookie(tf_http_request_get_header(request, "cookie"), "session");
|
||||||
const char** form_data = _form_data_decode(request->query, request->query ? strlen(request->query) : 0);
|
const char** form_data = _form_data_decode(request->query, request->query ? strlen(request->query) : 0);
|
||||||
const char* account_name_copy = NULL;
|
const char* account_name_copy = NULL;
|
||||||
JSValue jwt = _authenticate_jwt(ssb, context, session);
|
JSValue jwt = _authenticate_jwt(context, session);
|
||||||
|
|
||||||
if (_session_is_authenticated_as_user(context, jwt))
|
if (_session_is_authenticated_as_user(context, jwt))
|
||||||
{
|
{
|
||||||
const char* return_url = _form_data_get(form_data, "return");
|
const char* return_url = _form_data_get(form_data, "return");
|
||||||
if (return_url)
|
char url[1024];
|
||||||
|
if (!return_url)
|
||||||
{
|
{
|
||||||
snprintf(login->location_header, sizeof(login->location_header), "%s", return_url);
|
snprintf(url, sizeof(url), "%s%s/", request->is_tls ? "https://" : "http://", tf_http_request_get_header(request, "host"));
|
||||||
}
|
return_url = url;
|
||||||
else
|
|
||||||
{
|
|
||||||
snprintf(login->location_header, sizeof(login->location_header), "%s%s/", request->is_tls ? "https://" : "http://", tf_http_request_get_header(request, "host"));
|
|
||||||
}
|
}
|
||||||
|
const char* headers[] = {
|
||||||
|
"Location",
|
||||||
|
return_url,
|
||||||
|
};
|
||||||
|
tf_http_respond(request, 303, headers, tf_countof(headers) / 2, NULL, 0);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1522,51 +1520,47 @@ static void _httpd_endpoint_login_work(tf_ssb_t* ssb, void* user_data)
|
|||||||
tf_free(post_form_data);
|
tf_free(post_form_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool have_administrator = _make_administrator_if_first(ssb, context, account_name_copy, may_become_first_admin);
|
bool have_administrator = _make_administrator_if_first(ssb, account_name_copy, may_become_first_admin);
|
||||||
|
|
||||||
if (session_is_new && _form_data_get(form_data, "return") && !login_error)
|
if (session_is_new && _form_data_get(form_data, "return") && !login_error)
|
||||||
{
|
{
|
||||||
const char* return_url = _form_data_get(form_data, "return");
|
const char* return_url = _form_data_get(form_data, "return");
|
||||||
if (return_url)
|
char url[1024];
|
||||||
|
if (!return_url)
|
||||||
{
|
{
|
||||||
snprintf(login->location_header, sizeof(login->location_header), "%s", return_url);
|
snprintf(url, sizeof(url), "%s%s/", request->is_tls ? "https://" : "http://", tf_http_request_get_header(request, "host"));
|
||||||
|
return_url = url;
|
||||||
}
|
}
|
||||||
else
|
const char* cookie = _make_set_session_cookie_header(request, send_session);
|
||||||
{
|
const char* headers[] = {
|
||||||
snprintf(login->location_header, sizeof(login->location_header), "%s%s/", request->is_tls ? "https://" : "http://", tf_http_request_get_header(request, "host"));
|
"Location",
|
||||||
}
|
return_url,
|
||||||
login->set_cookie_header = _make_set_session_cookie_header(request, send_session);
|
"Set-Cookie",
|
||||||
|
cookie ? cookie : "",
|
||||||
|
};
|
||||||
|
tf_http_respond(request, 303, headers, tf_countof(headers) / 2, NULL, 0);
|
||||||
|
tf_free((void*)cookie);
|
||||||
tf_free((void*)send_session);
|
tf_free((void*)send_session);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
login->name = account_name_copy;
|
|
||||||
login->error = login_error;
|
|
||||||
login->set_cookie_header = _make_set_session_cookie_header(request, send_session);
|
|
||||||
tf_free((void*)send_session);
|
|
||||||
login->session_is_new = session_is_new;
|
|
||||||
login->have_administrator = have_administrator;
|
|
||||||
login->settings = tf_ssb_db_get_property(ssb, "core", "settings");
|
|
||||||
|
|
||||||
if (login->settings)
|
|
||||||
{
|
|
||||||
JSValue settings_value = JS_ParseJSON(context, login->settings, strlen(login->settings), NULL);
|
|
||||||
JSValue code_of_conduct_value = JS_GetPropertyStr(context, settings_value, "code_of_conduct");
|
|
||||||
const char* code_of_conduct = JS_ToCString(context, code_of_conduct_value);
|
|
||||||
const char* result = tf_strdup(code_of_conduct);
|
|
||||||
JS_FreeCString(context, code_of_conduct);
|
|
||||||
JS_FreeValue(context, code_of_conduct_value);
|
|
||||||
JS_FreeValue(context, settings_value);
|
|
||||||
tf_free((void*)login->settings);
|
|
||||||
login->settings = NULL;
|
|
||||||
login->code_of_conduct = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
login->pending++;
|
|
||||||
tf_http_request_ref(request);
|
tf_http_request_ref(request);
|
||||||
tf_file_read(login->request->user_data, "core/auth.html", _httpd_endpoint_login_file_read_callback, login);
|
|
||||||
|
|
||||||
|
login_request_t* login = tf_malloc(sizeof(login_request_t));
|
||||||
|
const char* code_of_conduct = _get_code_of_conduct(ssb);
|
||||||
|
*login = (login_request_t) {
|
||||||
|
.request = request,
|
||||||
|
.name = account_name_copy,
|
||||||
|
.jwt = jwt,
|
||||||
|
.error = login_error,
|
||||||
|
.session_cookie = send_session,
|
||||||
|
.session_is_new = session_is_new,
|
||||||
|
.code_of_conduct = code_of_conduct,
|
||||||
|
.have_administrator = have_administrator,
|
||||||
|
};
|
||||||
|
|
||||||
|
tf_file_read(request->user_data, "core/auth.html", _httpd_endpoint_login_file_read_callback, login);
|
||||||
|
jwt = JS_UNDEFINED;
|
||||||
account_name_copy = NULL;
|
account_name_copy = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1575,44 +1569,6 @@ done:
|
|||||||
tf_free(form_data);
|
tf_free(form_data);
|
||||||
tf_free((void*)account_name_copy);
|
tf_free((void*)account_name_copy);
|
||||||
JS_FreeValue(context, jwt);
|
JS_FreeValue(context, jwt);
|
||||||
|
|
||||||
JS_FreeContext(context);
|
|
||||||
JS_FreeRuntime(runtime);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _httpd_endpoint_login_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
login_request_t* login = user_data;
|
|
||||||
tf_http_request_t* request = login->request;
|
|
||||||
if (login->pending == 1)
|
|
||||||
{
|
|
||||||
if (*login->location_header)
|
|
||||||
{
|
|
||||||
const char* headers[] = {
|
|
||||||
"Location",
|
|
||||||
login->location_header,
|
|
||||||
"Set-Cookie",
|
|
||||||
login->set_cookie_header ? login->set_cookie_header : "",
|
|
||||||
};
|
|
||||||
tf_http_respond(request, 303, headers, tf_countof(headers) / 2, NULL, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tf_http_request_unref(request);
|
|
||||||
_login_release(login);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _httpd_endpoint_login(tf_http_request_t* request)
|
|
||||||
{
|
|
||||||
tf_task_t* task = request->user_data;
|
|
||||||
tf_http_request_ref(request);
|
|
||||||
|
|
||||||
tf_ssb_t* ssb = tf_task_get_ssb(task);
|
|
||||||
login_request_t* login = tf_malloc(sizeof(login_request_t));
|
|
||||||
*login = (login_request_t) {
|
|
||||||
.request = request,
|
|
||||||
};
|
|
||||||
login->pending++;
|
|
||||||
tf_ssb_run_work(ssb, _httpd_endpoint_login_work, _httpd_endpoint_login_after_work, login);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _httpd_endpoint_logout(tf_http_request_t* request)
|
static void _httpd_endpoint_logout(tf_http_request_t* request)
|
||||||
|
@ -381,7 +381,6 @@ static int _tf_run_task(const tf_run_args_t* args, int index)
|
|||||||
tf_ssb_import(tf_task_get_ssb(task), "core", "apps");
|
tf_ssb_import(tf_task_get_ssb(task), "core", "apps");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tf_ssb_set_main_thread(tf_task_get_ssb(task), true);
|
|
||||||
if (tf_task_execute(task, args->script))
|
if (tf_task_execute(task, args->script))
|
||||||
{
|
{
|
||||||
tf_task_run(task);
|
tf_task_run(task);
|
||||||
|
34
src/ssb.c
34
src/ssb.c
@ -1674,6 +1674,10 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t
|
|||||||
tf_trace_end(connection->ssb->trace);
|
tf_trace_end(connection->ssb->trace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tf_printf("No request callback for %p %d\n", connection, -request_number);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (close_connection)
|
if (close_connection)
|
||||||
@ -1780,28 +1784,18 @@ static bool _tf_ssb_connection_box_stream_recv(tf_ssb_connection_t* connection)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message, const char* previous_id, int64_t previous_sequence)
|
JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message)
|
||||||
{
|
{
|
||||||
char actual_previous_id[crypto_hash_sha256_BYTES * 2];
|
char previous_id[crypto_hash_sha256_BYTES * 2];
|
||||||
int64_t actual_previous_sequence = 0;
|
int64_t previous_sequence = 0;
|
||||||
bool have_previous = false;
|
bool have_previous = tf_ssb_db_get_latest_message_by_author(ssb, author, &previous_sequence, previous_id, sizeof(previous_id));
|
||||||
if (previous_id)
|
|
||||||
{
|
|
||||||
have_previous = *previous_id && previous_sequence > 0;
|
|
||||||
snprintf(actual_previous_id, sizeof(actual_previous_id), "%s", previous_id);
|
|
||||||
actual_previous_sequence = previous_sequence;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
have_previous = tf_ssb_db_get_latest_message_by_author(ssb, author, &actual_previous_sequence, actual_previous_id, sizeof(actual_previous_id));
|
|
||||||
}
|
|
||||||
|
|
||||||
JSContext* context = ssb->context;
|
JSContext* context = ssb->context;
|
||||||
JSValue root = JS_NewObject(context);
|
JSValue root = JS_NewObject(context);
|
||||||
|
|
||||||
JS_SetPropertyStr(context, root, "previous", have_previous ? JS_NewString(context, actual_previous_id) : JS_NULL);
|
JS_SetPropertyStr(context, root, "previous", have_previous ? JS_NewString(context, previous_id) : JS_NULL);
|
||||||
JS_SetPropertyStr(context, root, "author", JS_NewString(context, author));
|
JS_SetPropertyStr(context, root, "author", JS_NewString(context, author));
|
||||||
JS_SetPropertyStr(context, root, "sequence", JS_NewInt64(context, actual_previous_sequence + 1));
|
JS_SetPropertyStr(context, root, "sequence", JS_NewInt64(context, previous_sequence + 1));
|
||||||
|
|
||||||
int64_t now = (int64_t)time(NULL);
|
int64_t now = (int64_t)time(NULL);
|
||||||
JS_SetPropertyStr(context, root, "timestamp", JS_NewInt64(context, now * 1000LL));
|
JS_SetPropertyStr(context, root, "timestamp", JS_NewInt64(context, now * 1000LL));
|
||||||
@ -2266,7 +2260,6 @@ static void _tf_ssb_assert_not_main_thread(tf_ssb_t* ssb)
|
|||||||
const char* bt = tf_util_backtrace_string();
|
const char* bt = tf_util_backtrace_string();
|
||||||
tf_printf("Acquiring DB from the main thread:\n%s\n", bt);
|
tf_printf("Acquiring DB from the main thread:\n%s\n", bt);
|
||||||
tf_free((void*)bt);
|
tf_free((void*)bt);
|
||||||
abort();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3756,12 +3749,7 @@ void tf_ssb_ref(tf_ssb_t* ssb)
|
|||||||
|
|
||||||
void tf_ssb_unref(tf_ssb_t* ssb)
|
void tf_ssb_unref(tf_ssb_t* ssb)
|
||||||
{
|
{
|
||||||
int new_count = --ssb->ref_count;
|
ssb->ref_count--;
|
||||||
if (new_count < 0)
|
|
||||||
{
|
|
||||||
tf_printf("tf_ssb_unref past 0: %d\n", new_count);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tf_ssb_set_main_thread(tf_ssb_t* ssb, bool main_thread)
|
void tf_ssb_set_main_thread(tf_ssb_t* ssb, bool main_thread)
|
||||||
|
59
src/ssb.db.c
59
src/ssb.db.c
@ -281,7 +281,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
|||||||
tf_ssb_release_db_writer(ssb, db);
|
tf_ssb_release_db_writer(ssb, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _tf_ssb_db_previous_message_exists(sqlite3* db, const char* author, int64_t sequence, const char* previous, bool* out_id_mismatch)
|
static bool _tf_ssb_db_previous_message_exists(sqlite3* db, const char* author, int64_t sequence, const char* previous)
|
||||||
{
|
{
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
if (sequence == 1)
|
if (sequence == 1)
|
||||||
@ -291,13 +291,12 @@ static bool _tf_ssb_db_previous_message_exists(sqlite3* db, const char* author,
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
sqlite3_stmt* statement;
|
sqlite3_stmt* statement;
|
||||||
if (sqlite3_prepare(db, "SELECT COUNT(*), id != ?3 AS is_mismatch FROM messages WHERE author = ?1 AND sequence = ?2", -1, &statement, NULL) == SQLITE_OK)
|
if (sqlite3_prepare(db, "SELECT COUNT(*) FROM messages WHERE author = ?1 AND sequence = ?2 AND id = ?3", -1, &statement, NULL) == SQLITE_OK)
|
||||||
{
|
{
|
||||||
if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, sequence - 1) == SQLITE_OK &&
|
if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, sequence - 1) == SQLITE_OK &&
|
||||||
sqlite3_bind_text(statement, 3, previous, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW)
|
sqlite3_bind_text(statement, 3, previous, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW)
|
||||||
{
|
{
|
||||||
exists = sqlite3_column_int(statement, 0) != 0;
|
exists = sqlite3_column_int(statement, 0) != 0;
|
||||||
*out_id_mismatch = sqlite3_column_int(statement, 1) != 0;
|
|
||||||
}
|
}
|
||||||
sqlite3_finalize(statement);
|
sqlite3_finalize(statement);
|
||||||
}
|
}
|
||||||
@ -310,9 +309,8 @@ static int64_t _tf_ssb_db_store_message_raw(tf_ssb_t* ssb, const char* id, const
|
|||||||
{
|
{
|
||||||
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
||||||
int64_t last_row_id = -1;
|
int64_t last_row_id = -1;
|
||||||
bool id_mismatch = false;
|
|
||||||
|
|
||||||
if (_tf_ssb_db_previous_message_exists(db, author, sequence, previous, &id_mismatch))
|
if (_tf_ssb_db_previous_message_exists(db, author, sequence, previous))
|
||||||
{
|
{
|
||||||
const char* query = "INSERT INTO messages (id, previous, author, sequence, timestamp, content, hash, signature, flags) VALUES (?, ?, ?, ?, ?, jsonb(?), "
|
const char* query = "INSERT INTO messages (id, previous, author, sequence, timestamp, content, hash, signature, flags) VALUES (?, ?, ?, ?, ?, jsonb(?), "
|
||||||
"?, ?, ?) ON CONFLICT DO NOTHING";
|
"?, ?, ?) ON CONFLICT DO NOTHING";
|
||||||
@ -347,14 +345,8 @@ static int64_t _tf_ssb_db_store_message_raw(tf_ssb_t* ssb, const char* id, const
|
|||||||
tf_printf("%s: prepare failed: %s\n", __FUNCTION__, sqlite3_errmsg(db));
|
tf_printf("%s: prepare failed: %s\n", __FUNCTION__, sqlite3_errmsg(db));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (id_mismatch)
|
else
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
** Only warn if we find a previous message with the wrong ID.
|
|
||||||
** If a feed is forked, we would otherwise warn on every
|
|
||||||
** message when trying to receive what we don't have, and
|
|
||||||
** that's not helping anybody.
|
|
||||||
*/
|
|
||||||
tf_printf("%p: Previous message doesn't exist for author=%s sequence=%" PRId64 " previous=%s.\n", db, author, sequence, previous);
|
tf_printf("%p: Previous message doesn't exist for author=%s sequence=%" PRId64 " previous=%s.\n", db, author, sequence, previous);
|
||||||
}
|
}
|
||||||
tf_ssb_release_db_writer(ssb, db);
|
tf_ssb_release_db_writer(ssb, db);
|
||||||
@ -643,44 +635,6 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct _blob_get_async_t
|
|
||||||
{
|
|
||||||
tf_ssb_t* ssb;
|
|
||||||
char id[k_blob_id_len];
|
|
||||||
tf_ssb_db_blob_get_callback_t* callback;
|
|
||||||
void* user_data;
|
|
||||||
|
|
||||||
bool out_found;
|
|
||||||
uint8_t* out_data;
|
|
||||||
size_t out_size;
|
|
||||||
} blob_get_async_t;
|
|
||||||
|
|
||||||
static void _tf_ssb_db_blob_get_async_work(tf_ssb_t* ssb, void* user_data)
|
|
||||||
{
|
|
||||||
blob_get_async_t* async = user_data;
|
|
||||||
async->out_found = tf_ssb_db_blob_get(ssb, async->id, &async->out_data, &async->out_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _tf_ssb_db_blob_get_async_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
|
||||||
{
|
|
||||||
blob_get_async_t* async = user_data;
|
|
||||||
async->callback(async->out_found, async->out_data, async->out_size, async->user_data);
|
|
||||||
tf_free(async->out_data);
|
|
||||||
tf_free(async);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tf_ssb_db_blob_get_async(tf_ssb_t* ssb, const char* id, tf_ssb_db_blob_get_callback_t* callback, void* user_data)
|
|
||||||
{
|
|
||||||
blob_get_async_t* async = tf_malloc(sizeof(blob_get_async_t));
|
|
||||||
*async = (blob_get_async_t) {
|
|
||||||
.ssb = ssb,
|
|
||||||
.callback = callback,
|
|
||||||
.user_data = user_data,
|
|
||||||
};
|
|
||||||
snprintf(async->id, sizeof(async->id), "%s", id);
|
|
||||||
tf_ssb_run_work(ssb, _tf_ssb_db_blob_get_async_work, _tf_ssb_db_blob_get_async_after_work, async);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct _blob_store_work_t
|
typedef struct _blob_store_work_t
|
||||||
{
|
{
|
||||||
const uint8_t* blob;
|
const uint8_t* blob;
|
||||||
@ -694,11 +648,8 @@ typedef struct _blob_store_work_t
|
|||||||
static void _tf_ssb_db_blob_store_work(tf_ssb_t* ssb, void* user_data)
|
static void _tf_ssb_db_blob_store_work(tf_ssb_t* ssb, void* user_data)
|
||||||
{
|
{
|
||||||
blob_store_work_t* blob_work = user_data;
|
blob_store_work_t* blob_work = user_data;
|
||||||
if (!tf_ssb_is_shutting_down(ssb))
|
|
||||||
{
|
|
||||||
tf_ssb_db_blob_store(ssb, blob_work->blob, blob_work->size, blob_work->id, sizeof(blob_work->id), &blob_work->is_new);
|
tf_ssb_db_blob_store(ssb, blob_work->blob, blob_work->size, blob_work->id, sizeof(blob_work->id), &blob_work->is_new);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void _tf_ssb_db_blob_store_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
static void _tf_ssb_db_blob_store_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||||
{
|
{
|
||||||
@ -1095,8 +1046,6 @@ bool tf_ssb_db_identity_create(tf_ssb_t* ssb, const char* user, uint8_t* out_pub
|
|||||||
if (out_private_key)
|
if (out_private_key)
|
||||||
{
|
{
|
||||||
tf_ssb_id_str_to_bin(out_private_key, private);
|
tf_ssb_id_str_to_bin(out_private_key, private);
|
||||||
/* HACK: tf_ssb_id_str_to_bin only produces 32 bytes even though the full private key is 32 + 32. */
|
|
||||||
tf_ssb_id_str_to_bin(out_private_key + crypto_sign_PUBLICKEYBYTES, public);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
18
src/ssb.db.h
18
src/ssb.db.h
@ -55,24 +55,6 @@ bool tf_ssb_db_blob_has(tf_ssb_t* ssb, const char* id);
|
|||||||
*/
|
*/
|
||||||
bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size);
|
bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size);
|
||||||
|
|
||||||
/**
|
|
||||||
** A function called when a blob is retrieved from the database.
|
|
||||||
** @param found Whether the blob was found.
|
|
||||||
** @param data The blob data if found.
|
|
||||||
** @param size The size of the blob data if found, in bytes.
|
|
||||||
** @param user_data The user data.
|
|
||||||
*/
|
|
||||||
typedef void(tf_ssb_db_blob_get_callback_t)(bool found, const uint8_t* data, size_t size, void* user_data);
|
|
||||||
|
|
||||||
/**
|
|
||||||
** Retrieve a blob from the database asynchronously.
|
|
||||||
** @param ssb The SSB instance.
|
|
||||||
** @param id The blob identifier.
|
|
||||||
** @param callback Callback called with the result.
|
|
||||||
** @param user_data The user data.
|
|
||||||
*/
|
|
||||||
void tf_ssb_db_blob_get_async(tf_ssb_t* ssb, const char* id, tf_ssb_db_blob_get_callback_t* callback, void* user_data);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
** A function called when a message is stored in the database.
|
** A function called when a message is stored in the database.
|
||||||
** @param id The message identifier.
|
** @param id The message identifier.
|
||||||
|
@ -278,11 +278,9 @@ void tf_ssb_run(tf_ssb_t* ssb);
|
|||||||
** @param author The author's public key.
|
** @param author The author's public key.
|
||||||
** @param private_key The author's private key.
|
** @param private_key The author's private key.
|
||||||
** @param message The message to sign.
|
** @param message The message to sign.
|
||||||
** @param previous_id The ID of the previous message in the feed. Optional.
|
|
||||||
** @param previous_sequence The sequence number of the previous message in the feed. Optional.
|
|
||||||
** @return The signed message.
|
** @return The signed message.
|
||||||
*/
|
*/
|
||||||
JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message, const char* previous_id, int64_t previous_sequence);
|
JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
** Get the server's identity.
|
** Get the server's identity.
|
||||||
|
1009
src/ssb.js.c
1009
src/ssb.js.c
File diff suppressed because it is too large
Load Diff
127
src/ssb.rpc.c
127
src/ssb.rpc.c
@ -67,40 +67,6 @@ static void _tf_ssb_rpc_blobs_get_callback(
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct _blobs_get_work_t
|
|
||||||
{
|
|
||||||
int64_t request_number;
|
|
||||||
char id[k_id_base64_len];
|
|
||||||
bool found;
|
|
||||||
uint8_t* blob;
|
|
||||||
size_t size;
|
|
||||||
} blobs_get_work_t;
|
|
||||||
|
|
||||||
static void _tf_ssb_rpc_blobs_get_work(tf_ssb_connection_t* connection, void* user_data)
|
|
||||||
{
|
|
||||||
blobs_get_work_t* work = user_data;
|
|
||||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
|
||||||
work->found = tf_ssb_db_blob_get(ssb, work->id, &work->blob, &work->size);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _tf_ssb_rpc_blobs_get_after_work(tf_ssb_connection_t* connection, int status, void* user_data)
|
|
||||||
{
|
|
||||||
blobs_get_work_t* work = user_data;
|
|
||||||
if (work->found)
|
|
||||||
{
|
|
||||||
const size_t k_send_max = 8192;
|
|
||||||
for (size_t offset = 0; offset < work->size; offset += k_send_max)
|
|
||||||
{
|
|
||||||
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream, -work->request_number, NULL, work->blob + offset,
|
|
||||||
offset + k_send_max <= work->size ? k_send_max : (work->size - offset), NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
tf_free(work->blob);
|
|
||||||
}
|
|
||||||
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error | k_ssb_rpc_flag_stream, -work->request_number, NULL,
|
|
||||||
(const uint8_t*)(work->found ? "true" : "false"), strlen(work->found ? "true" : "false"), NULL, NULL, NULL);
|
|
||||||
tf_free(work);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||||
{
|
{
|
||||||
if (flags & k_ssb_rpc_flag_end_error)
|
if (flags & k_ssb_rpc_flag_end_error)
|
||||||
@ -109,10 +75,11 @@ static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags
|
|||||||
}
|
}
|
||||||
|
|
||||||
tf_ssb_connection_add_request(connection, -request_number, "blobs.get", _tf_ssb_rpc_blobs_get_callback, NULL, NULL, NULL);
|
tf_ssb_connection_add_request(connection, -request_number, "blobs.get", _tf_ssb_rpc_blobs_get_callback, NULL, NULL, NULL);
|
||||||
|
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||||
JSContext* context = tf_ssb_connection_get_context(connection);
|
JSContext* context = tf_ssb_connection_get_context(connection);
|
||||||
JSValue ids = JS_GetPropertyStr(context, args, "args");
|
JSValue ids = JS_GetPropertyStr(context, args, "args");
|
||||||
int length = tf_util_get_length(context, ids);
|
int length = tf_util_get_length(context, ids);
|
||||||
|
bool success = false;
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
JSValue arg = JS_GetPropertyUint32(context, ids, i);
|
JSValue arg = JS_GetPropertyUint32(context, ids, i);
|
||||||
@ -127,18 +94,25 @@ static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags
|
|||||||
id = JS_ToCString(context, key);
|
id = JS_ToCString(context, key);
|
||||||
JS_FreeValue(context, key);
|
JS_FreeValue(context, key);
|
||||||
}
|
}
|
||||||
|
uint8_t* blob = NULL;
|
||||||
blobs_get_work_t* work = tf_malloc(sizeof(blobs_get_work_t));
|
size_t size = 0;
|
||||||
*work = (blobs_get_work_t) {
|
const size_t k_send_max = 8192;
|
||||||
.request_number = request_number,
|
if (tf_ssb_db_blob_get(ssb, id, &blob, &size))
|
||||||
};
|
{
|
||||||
snprintf(work->id, sizeof(work->id), "%s", id);
|
for (size_t offset = 0; offset < size; offset += k_send_max)
|
||||||
tf_ssb_connection_run_work(connection, _tf_ssb_rpc_blobs_get_work, _tf_ssb_rpc_blobs_get_after_work, work);
|
{
|
||||||
|
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream, -request_number, NULL, blob + offset,
|
||||||
|
offset + k_send_max <= size ? k_send_max : (size - offset), NULL, NULL, NULL);
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
tf_free(blob);
|
||||||
|
}
|
||||||
JS_FreeCString(context, id);
|
JS_FreeCString(context, id);
|
||||||
JS_FreeValue(context, arg);
|
JS_FreeValue(context, arg);
|
||||||
}
|
}
|
||||||
JS_FreeValue(context, ids);
|
JS_FreeValue(context, ids);
|
||||||
|
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error | k_ssb_rpc_flag_stream, -request_number, NULL,
|
||||||
|
(const uint8_t*)(success ? "true" : "false"), strlen(success ? "true" : "false"), NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _tf_ssb_rpc_blobs_has(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
static void _tf_ssb_rpc_blobs_has(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||||
@ -506,45 +480,6 @@ static void _tf_ssb_rpc_connection_blobs_get(tf_ssb_connection_t* connection, co
|
|||||||
JS_FreeValue(context, message);
|
JS_FreeValue(context, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct _blob_create_wants_work_t
|
|
||||||
{
|
|
||||||
tf_ssb_connection_t* connection;
|
|
||||||
char blob_id[k_blob_id_len];
|
|
||||||
bool out_result;
|
|
||||||
int64_t size;
|
|
||||||
size_t out_size;
|
|
||||||
} blob_create_wants_work_t;
|
|
||||||
|
|
||||||
static void _tf_ssb_rpc_connection_blobs_create_wants_work(tf_ssb_connection_t* connection, void* user_data)
|
|
||||||
{
|
|
||||||
blob_create_wants_work_t* work = user_data;
|
|
||||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(work->connection);
|
|
||||||
work->out_result = tf_ssb_db_blob_get(ssb, work->blob_id, NULL, &work->out_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _tf_ssb_rpc_connection_blobs_create_wants_after_work(tf_ssb_connection_t* connection, int result, void* user_data)
|
|
||||||
{
|
|
||||||
blob_create_wants_work_t* work = user_data;
|
|
||||||
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(work->connection);
|
|
||||||
tf_ssb_blob_wants_t* blob_wants = tf_ssb_connection_get_blob_wants_state(connection);
|
|
||||||
JSContext* context = tf_ssb_get_context(ssb);
|
|
||||||
if (work->out_result)
|
|
||||||
{
|
|
||||||
JSValue message = JS_NewObject(context);
|
|
||||||
JS_SetPropertyStr(context, message, work->blob_id, JS_NewInt64(context, work->out_size));
|
|
||||||
tf_ssb_connection_rpc_send_json(work->connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL);
|
|
||||||
JS_FreeValue(context, message);
|
|
||||||
}
|
|
||||||
else if (work->size == -1LL)
|
|
||||||
{
|
|
||||||
JSValue message = JS_NewObject(context);
|
|
||||||
JS_SetPropertyStr(context, message, work->blob_id, JS_NewInt64(context, -2));
|
|
||||||
tf_ssb_connection_rpc_send_json(work->connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL);
|
|
||||||
JS_FreeValue(context, message);
|
|
||||||
}
|
|
||||||
tf_free(work);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _tf_ssb_rpc_connection_blobs_createWants_callback(
|
static void _tf_ssb_rpc_connection_blobs_createWants_callback(
|
||||||
tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
||||||
{
|
{
|
||||||
@ -554,6 +489,7 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
||||||
JSContext* context = tf_ssb_connection_get_context(connection);
|
JSContext* context = tf_ssb_connection_get_context(connection);
|
||||||
|
|
||||||
JSValue name = JS_GetPropertyStr(context, args, "name");
|
JSValue name = JS_GetPropertyStr(context, args, "name");
|
||||||
@ -588,13 +524,21 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback(
|
|||||||
}
|
}
|
||||||
if (size < 0)
|
if (size < 0)
|
||||||
{
|
{
|
||||||
blob_create_wants_work_t* work = tf_malloc(sizeof(blob_create_wants_work_t));
|
size_t blob_size = 0;
|
||||||
*work = (blob_create_wants_work_t) {
|
if (tf_ssb_db_blob_get(ssb, blob_id, NULL, &blob_size))
|
||||||
.connection = connection,
|
{
|
||||||
.size = size,
|
JSValue message = JS_NewObject(context);
|
||||||
};
|
JS_SetPropertyStr(context, message, blob_id, JS_NewInt64(context, blob_size));
|
||||||
snprintf(work->blob_id, sizeof(work->blob_id), "%s", blob_id);
|
tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL);
|
||||||
tf_ssb_connection_run_work(connection, _tf_ssb_rpc_connection_blobs_create_wants_work, _tf_ssb_rpc_connection_blobs_create_wants_after_work, work);
|
JS_FreeValue(context, message);
|
||||||
|
}
|
||||||
|
else if (size == -1LL)
|
||||||
|
{
|
||||||
|
JSValue message = JS_NewObject(context);
|
||||||
|
JS_SetPropertyStr(context, message, blob_id, JS_NewInt64(context, -2));
|
||||||
|
tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL);
|
||||||
|
JS_FreeValue(context, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -832,10 +776,6 @@ static void _tf_ssb_connection_send_history_stream_callback(tf_ssb_connection_t*
|
|||||||
{
|
{
|
||||||
tf_ssb_connection_run_work(connection, _tf_ssb_connection_send_history_stream_work, _tf_ssb_connection_send_history_stream_after_work, user_data);
|
tf_ssb_connection_run_work(connection, _tf_ssb_connection_send_history_stream_work, _tf_ssb_connection_send_history_stream_after_work, user_data);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
_tf_ssb_connection_send_history_stream_after_work(connection, -1, user_data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _tf_ssb_connection_send_history_stream(tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys, bool live)
|
static void _tf_ssb_connection_send_history_stream(tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys, bool live)
|
||||||
@ -1285,6 +1225,7 @@ static void _tf_ssb_rpc_delete_blobs_work(tf_ssb_t* ssb, void* user_data)
|
|||||||
|
|
||||||
static void _tf_ssb_rpc_delete_blobs_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
static void _tf_ssb_rpc_delete_blobs_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||||
{
|
{
|
||||||
|
tf_ssb_unref(ssb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _tf_ssb_rpc_start_delete_callback(tf_ssb_t* ssb, void* user_data)
|
static void _tf_ssb_rpc_start_delete_callback(tf_ssb_t* ssb, void* user_data)
|
||||||
|
@ -16,11 +16,6 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
#define WIFEXITED(x) 1
|
|
||||||
#define WEXITSTATUS(x) (x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void tf_ssb_test_id_conversion(const tf_test_options_t* options)
|
void tf_ssb_test_id_conversion(const tf_test_options_t* options)
|
||||||
{
|
{
|
||||||
tf_printf("Testing id conversion.\n");
|
tf_printf("Testing id conversion.\n");
|
||||||
@ -197,7 +192,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
|
|||||||
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
||||||
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!"));
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!"));
|
||||||
bool stored = false;
|
bool stored = false;
|
||||||
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
|
||||||
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
||||||
JS_FreeValue(context0, signed_message);
|
JS_FreeValue(context0, signed_message);
|
||||||
_wait_stored(ssb0, &stored);
|
_wait_stored(ssb0, &stored);
|
||||||
@ -207,7 +202,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
|
|||||||
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
||||||
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post."));
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post."));
|
||||||
stored = false;
|
stored = false;
|
||||||
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
|
||||||
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
||||||
JS_FreeValue(context0, signed_message);
|
JS_FreeValue(context0, signed_message);
|
||||||
_wait_stored(ssb0, &stored);
|
_wait_stored(ssb0, &stored);
|
||||||
@ -222,7 +217,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
|
|||||||
JS_SetPropertyUint32(context0, mentions, 0, mention);
|
JS_SetPropertyUint32(context0, mentions, 0, mention);
|
||||||
JS_SetPropertyStr(context0, obj, "mentions", mentions);
|
JS_SetPropertyStr(context0, obj, "mentions", mentions);
|
||||||
stored = false;
|
stored = false;
|
||||||
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
|
||||||
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
||||||
JS_FreeValue(context0, signed_message);
|
JS_FreeValue(context0, signed_message);
|
||||||
_wait_stored(ssb0, &stored);
|
_wait_stored(ssb0, &stored);
|
||||||
@ -281,7 +276,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
|
|||||||
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
|
||||||
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self."));
|
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self."));
|
||||||
stored = false;
|
stored = false;
|
||||||
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
|
||||||
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
||||||
JS_FreeValue(context0, signed_message);
|
JS_FreeValue(context0, signed_message);
|
||||||
_wait_stored(ssb0, &stored);
|
_wait_stored(ssb0, &stored);
|
||||||
@ -554,7 +549,7 @@ void tf_ssb_test_following(const tf_test_options_t* options)
|
|||||||
JS_SetPropertyStr(context, message, "contact", JS_NewString(context, contact)); \
|
JS_SetPropertyStr(context, message, "contact", JS_NewString(context, contact)); \
|
||||||
JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \
|
JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \
|
||||||
JS_SetPropertyStr(context, message, "blocking", block ? JS_TRUE : JS_FALSE); \
|
JS_SetPropertyStr(context, message, "blocking", block ? JS_TRUE : JS_FALSE); \
|
||||||
signed_message = tf_ssb_sign_message(ssb0, id, priv, message, NULL, 0); \
|
signed_message = tf_ssb_sign_message(ssb0, id, priv, message); \
|
||||||
stored = false; \
|
stored = false; \
|
||||||
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); \
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored); \
|
||||||
_wait_stored(ssb0, &stored); \
|
_wait_stored(ssb0, &stored); \
|
||||||
@ -613,7 +608,7 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
|
|||||||
for (int i = 0; i < k_messages; i++)
|
for (int i = 0; i < k_messages; i++)
|
||||||
{
|
{
|
||||||
bool stored = false;
|
bool stored = false;
|
||||||
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj, NULL, 0);
|
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
|
||||||
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
|
||||||
JS_FreeValue(tf_ssb_get_context(ssb0), signed_message);
|
JS_FreeValue(tf_ssb_get_context(ssb0), signed_message);
|
||||||
_wait_stored(ssb0, &stored);
|
_wait_stored(ssb0, &stored);
|
||||||
@ -824,45 +819,3 @@ void tf_ssb_test_go_ssb_room(const tf_test_options_t* options)
|
|||||||
|
|
||||||
uv_loop_close(&loop);
|
uv_loop_close(&loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !TARGET_OS_IPHONE
|
|
||||||
static void _write_file(const char* path, const char* contents)
|
|
||||||
{
|
|
||||||
FILE* file = fopen(path, "w");
|
|
||||||
if (!file)
|
|
||||||
{
|
|
||||||
printf("Unable to write %s: %s.\n", path, strerror(errno));
|
|
||||||
fflush(stdout);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
fputs(contents, file);
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define TEST_ARGS " --ssb-port=0 --http-port=0 --https-port=0"
|
|
||||||
|
|
||||||
void tf_ssb_test_encrypt(const tf_test_options_t* options)
|
|
||||||
{
|
|
||||||
_write_file("out/test.js",
|
|
||||||
"async function main() {\n"
|
|
||||||
" let a = await ssb.createIdentity('test');\n"
|
|
||||||
" let b = await ssb.createIdentity('test');\n"
|
|
||||||
" let c = await ssb.privateMessageEncrypt('test', a, [a, b], \"{'foo': 1}\");\n"
|
|
||||||
" if (!c.endsWith('.box')) {\n"
|
|
||||||
" exit(1);\n"
|
|
||||||
" }\n"
|
|
||||||
" print(await ssb.privateMessageDecrypt('test', a, c));\n"
|
|
||||||
"}\n"
|
|
||||||
"main().catch(() => exit(2));\n");
|
|
||||||
|
|
||||||
unlink("out/testdb.sqlite");
|
|
||||||
char command[256];
|
|
||||||
snprintf(command, sizeof(command), "%s run --db-path=out/testdb.sqlite -s out/test.js" TEST_ARGS, options->exe_path);
|
|
||||||
tf_printf("%s\n", command);
|
|
||||||
int result = system(command);
|
|
||||||
(void)result;
|
|
||||||
assert(WIFEXITED(result));
|
|
||||||
printf("returned %d\n", WEXITSTATUS(result));
|
|
||||||
assert(WEXITSTATUS(result) == 0);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
@ -47,10 +47,4 @@ void tf_ssb_test_bench(const tf_test_options_t* options);
|
|||||||
*/
|
*/
|
||||||
void tf_ssb_test_go_ssb_room(const tf_test_options_t* options);
|
void tf_ssb_test_go_ssb_room(const tf_test_options_t* options);
|
||||||
|
|
||||||
/**
|
|
||||||
** Test encrypting a private message.
|
|
||||||
** @param options The test options.
|
|
||||||
*/
|
|
||||||
void tf_ssb_test_encrypt(const tf_test_options_t* options);
|
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
30
src/tests.c
30
src/tests.c
@ -266,29 +266,19 @@ static void _test_promise_remote_reject(const tf_test_options_t* options)
|
|||||||
static void _test_database(const tf_test_options_t* options)
|
static void _test_database(const tf_test_options_t* options)
|
||||||
{
|
{
|
||||||
_write_file("out/test.js",
|
_write_file("out/test.js",
|
||||||
"async function main() {\n"
|
|
||||||
"var db = new Database('testdb');\n"
|
"var db = new Database('testdb');\n"
|
||||||
" if (await db.get('a')) {\n"
|
"if (db.get('a')) {\n"
|
||||||
" exit(1);\n"
|
" exit(1);\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
" await db.set('a', 1);\n"
|
"db.set('a', 1);\n"
|
||||||
" if (await db.get('a') != 1) {\n"
|
"if (db.get('a') != 1) {\n"
|
||||||
" exit(2);\n"
|
" exit(2);\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
" await db.exchange('b', null, 1);\n"
|
"db.set('b', 2);\n"
|
||||||
" await db.exchange('b', 1, 2);\n"
|
"db.set('c', 3);\n"
|
||||||
" if (await db.get('b') != 2) {\n"
|
|
||||||
" exit(5);\n"
|
|
||||||
" }\n"
|
|
||||||
" await db.set('c', 3);\n"
|
|
||||||
" await db.set('d', 3);\n"
|
|
||||||
" await db.remove('d', 3);\n"
|
|
||||||
" if (JSON.stringify(await db.getLike('b%')) != '{\"b\":\"2\"}') {\n"
|
|
||||||
" exit(6);\n"
|
|
||||||
" }\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"var expected = ['a', 'b', 'c'];\n"
|
"var expected = ['a', 'b', 'c'];\n"
|
||||||
" var have = await db.getAll();\n"
|
"var have = db.getAll();\n"
|
||||||
"for (var i = 0; i < have.length; i++) {\n"
|
"for (var i = 0; i < have.length; i++) {\n"
|
||||||
" var item = have[i];\n"
|
" var item = have[i];\n"
|
||||||
" if (expected.indexOf(item) == -1) {\n"
|
" if (expected.indexOf(item) == -1) {\n"
|
||||||
@ -301,12 +291,7 @@ static void _test_database(const tf_test_options_t* options)
|
|||||||
"if (expected.length) {\n"
|
"if (expected.length) {\n"
|
||||||
" print('Expected but did not find: ' + JSON.stringify(expected));\n"
|
" print('Expected but did not find: ' + JSON.stringify(expected));\n"
|
||||||
" exit(4);\n"
|
" exit(4);\n"
|
||||||
" }\n"
|
"}\n");
|
||||||
" if (JSON.stringify(await databases.list('%')) != '[\"testdb\"]') {\n"
|
|
||||||
" exit(7);\n"
|
|
||||||
" }\n"
|
|
||||||
"}\n"
|
|
||||||
"main();");
|
|
||||||
|
|
||||||
char command[256];
|
char command[256];
|
||||||
unlink("out/test_db0.sqlite");
|
unlink("out/test_db0.sqlite");
|
||||||
@ -914,7 +899,6 @@ void tf_tests(const tf_test_options_t* options)
|
|||||||
_tf_test_run(options, "bench", tf_ssb_test_bench, false);
|
_tf_test_run(options, "bench", tf_ssb_test_bench, false);
|
||||||
_tf_test_run(options, "auto", _test_auto, false);
|
_tf_test_run(options, "auto", _test_auto, false);
|
||||||
_tf_test_run(options, "go-ssb-room", tf_ssb_test_go_ssb_room, true);
|
_tf_test_run(options, "go-ssb-room", tf_ssb_test_go_ssb_room, true);
|
||||||
_tf_test_run(options, "encrypt", tf_ssb_test_encrypt, false);
|
|
||||||
tf_printf("Tests completed.\n");
|
tf_printf("Tests completed.\n");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -435,16 +435,6 @@ const char* tf_util_backtrace_string()
|
|||||||
return tf_util_backtrace_to_string(buffer, count);
|
return tf_util_backtrace_to_string(buffer, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tf_util_print_backtrace()
|
|
||||||
{
|
|
||||||
const char* bt = tf_util_backtrace_string();
|
|
||||||
if (bt)
|
|
||||||
{
|
|
||||||
tf_printf("%s\n", bt);
|
|
||||||
}
|
|
||||||
tf_free((void*)bt);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__ANDROID__)
|
#if defined(__ANDROID__)
|
||||||
typedef struct _android_backtrace_t
|
typedef struct _android_backtrace_t
|
||||||
{
|
{
|
||||||
|
@ -126,11 +126,6 @@ const char* tf_util_backtrace_to_string(void* const* buffer, int count);
|
|||||||
*/
|
*/
|
||||||
const char* tf_util_backtrace_string();
|
const char* tf_util_backtrace_string();
|
||||||
|
|
||||||
/**
|
|
||||||
** Print a stack backtrace of the calling thread.
|
|
||||||
*/
|
|
||||||
void tf_util_print_backtrace();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
** Convert a function pointer to its name, if possible.
|
** Convert a function pointer to its name, if possible.
|
||||||
** @return The function name or null.
|
** @return The function name or null.
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
#define VERSION_NUMBER "0.0.21-wip"
|
#define VERSION_NUMBER "0.0.20-wip"
|
||||||
#define VERSION_NAME "Psst. Look behind you."
|
#define VERSION_NAME "One word all lowercase four words all uppercase."
|
||||||
|
@ -42,19 +42,20 @@ try:
|
|||||||
|
|
||||||
driver.switch_to.frame(driver.find_element(By.ID, 'document'))
|
driver.switch_to.frame(driver.find_element(By.ID, 'document'))
|
||||||
wait.until(expected_conditions.presence_of_element_located((By.LINK_TEXT, 'identity'))).click()
|
wait.until(expected_conditions.presence_of_element_located((By.LINK_TEXT, 'identity'))).click()
|
||||||
|
driver.switch_to.default_content()
|
||||||
|
|
||||||
|
wait.until(expected_conditions.presence_of_element_located((By.ID, 'content')))
|
||||||
|
|
||||||
# StaleElementReferenceException
|
# StaleElementReferenceException
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
driver.switch_to.default_content()
|
|
||||||
wait.until(expected_conditions.presence_of_element_located((By.ID, 'content')))
|
|
||||||
driver.switch_to.frame(wait.until(expected_conditions.presence_of_element_located((By.ID, 'document'))))
|
driver.switch_to.frame(wait.until(expected_conditions.presence_of_element_located((By.ID, 'document'))))
|
||||||
wait.until(expected_conditions.presence_of_element_located((By.ID, 'create_id'))).click()
|
|
||||||
driver.switch_to.alert.accept()
|
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
wait.until(expected_conditions.presence_of_element_located((By.ID, 'create_id'))).click()
|
||||||
|
driver.switch_to.alert.accept()
|
||||||
# StaleElementReferenceException
|
# StaleElementReferenceException
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -125,8 +126,7 @@ try:
|
|||||||
driver.switch_to.default_content()
|
driver.switch_to.default_content()
|
||||||
driver.find_element(By.ID, 'allow').click()
|
driver.find_element(By.ID, 'allow').click()
|
||||||
|
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click()
|
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.LINK_TEXT, 'logout testuser').click()
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click()
|
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'password').send_keys('test_password')
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'password').send_keys('test_password')
|
||||||
@ -134,8 +134,13 @@ try:
|
|||||||
|
|
||||||
wait.until(expected_conditions.presence_of_element_located((By.ID, 'content')))
|
wait.until(expected_conditions.presence_of_element_located((By.ID, 'content')))
|
||||||
|
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click()
|
# NoSuchElementException
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click()
|
while True:
|
||||||
|
try:
|
||||||
|
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.LINK_TEXT, 'logout testuser').click()
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
pass
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guest_label').click()
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guest_label').click()
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guestButton').click()
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'guestButton').click()
|
||||||
|
|
||||||
@ -144,7 +149,7 @@ try:
|
|||||||
wait.until(expected_conditions.presence_of_element_located((By.TAG_NAME, 'tf-app'))).shadow_root
|
wait.until(expected_conditions.presence_of_element_located((By.TAG_NAME, 'tf-app'))).shadow_root
|
||||||
driver.switch_to.default_content()
|
driver.switch_to.default_content()
|
||||||
|
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click()
|
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.LINK_TEXT, 'logout guest').click()
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
|
||||||
|
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')
|
||||||
@ -185,8 +190,7 @@ try:
|
|||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'confirm').send_keys('new_password')
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'confirm').send_keys('new_password')
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'loginButton').click()
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'loginButton').click()
|
||||||
wait.until(expected_conditions.presence_of_element_located((By.ID, 'document')))
|
wait.until(expected_conditions.presence_of_element_located((By.ID, 'document')))
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'identity').click()
|
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.LINK_TEXT, 'logout testuser').click()
|
||||||
driver.find_element(By.TAG_NAME, 'tf-navigation').shadow_root.find_element(By.ID, 'logout').click()
|
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'login_label').click()
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'name').send_keys('testuser')
|
||||||
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'password').send_keys('test_password')
|
driver.find_element(By.TAG_NAME, 'tf-auth').shadow_root.find_element(By.ID, 'password').send_keys('test_password')
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
if [ -z $ANDROID_NDK_ROOT ]; then
|
if [ -z $ANDROID_NDK_ROOT ]; then
|
||||||
ANDROID_NDK_ROOT=~/Android/Sdk/ndk/26.1.10909125
|
ANDROID_NDK_ROOT=~/Android/Sdk/ndk/26.1.10909125
|
||||||
fi
|
fi
|
||||||
OPENSSL_VERSION=3.3.1
|
OPENSSL_VERSION=3.3.0
|
||||||
|
|
||||||
API_LEVEL=24
|
API_LEVEL=24
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
OPENSSL_VERSION=3.3.1
|
OPENSSL_VERSION=3.3.0
|
||||||
|
|
||||||
API_LEVEL=28
|
API_LEVEL=28
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ if [ ! -d out/openssl-${OPENSSL_VERSION} ]
|
|||||||
then
|
then
|
||||||
if [ ! -f out/openssl-${OPENSSL_VERSION}.tar.gz ]
|
if [ ! -f out/openssl-${OPENSSL_VERSION}.tar.gz ]
|
||||||
then
|
then
|
||||||
curl -L https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz -o out/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
|
curl https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz -o out/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
|
||||||
fi
|
fi
|
||||||
tar -C out/ -xzf out/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
|
tar -C out/ -xzf out/openssl-${OPENSSL_VERSION}.tar.gz || exit 128
|
||||||
fi
|
fi
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
OPENSSL_VERSION=3.3.1
|
OPENSSL_VERSION=3.3.0
|
||||||
|
|
||||||
API_LEVEL=24
|
API_LEVEL=24
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
VERSION=3.1.4
|
VERSION=3.1.3
|
||||||
wget https://cdn.jsdelivr.net/gh/lit/dist@$VERSION/all/lit-all.min.js -O deps/lit/lit-all.min.js
|
wget https://cdn.jsdelivr.net/gh/lit/dist@$VERSION/all/lit-all.min.js -O deps/lit/lit-all.min.js
|
||||||
wget https://cdn.jsdelivr.net/gh/lit/dist@$VERSION/all/lit-all.min.js.map -O deps/lit/lit-all.min.js.map
|
wget https://cdn.jsdelivr.net/gh/lit/dist@$VERSION/all/lit-all.min.js.map -O deps/lit/lit-all.min.js.map
|
||||||
cp -fv deps/lit/* apps/blog/
|
cp -fv deps/lit/* apps/blog/
|
||||||
|
Reference in New Issue
Block a user