Compare commits
43 commits
develop
...
jryans/p2p
Author | SHA1 | Date | |
---|---|---|---|
|
7fd26c3af9 | ||
|
9d1bed29b2 | ||
|
1e051365b1 | ||
|
13251a4d6c | ||
|
b28708fbf3 | ||
|
46d659a936 | ||
|
9e19d910c1 | ||
|
92fe2b8935 | ||
|
7e15090364 | ||
|
d83c475bb7 | ||
|
b4c7d54fc0 | ||
|
e10d5eb0be | ||
|
ff6c4ac837 | ||
|
bcba891df1 | ||
|
708a7146d5 | ||
|
e4c67f39a6 | ||
|
a5ba31e127 | ||
|
f6aceba566 | ||
|
35bcc152ea | ||
|
ec45bb7976 | ||
|
b0fb043c2b | ||
|
4828869515 | ||
|
671315c36e | ||
|
9994c9ea65 | ||
|
9328519c29 | ||
|
330aa285e6 | ||
|
5c3f11a7c7 | ||
|
cdd32590fe | ||
|
52641c5674 | ||
|
6f43a14c43 | ||
|
d80285d971 | ||
|
3d74d336bf | ||
|
1f1f0f1264 | ||
|
bf13ec2285 | ||
|
4d1f969c4d | ||
|
0935ccd737 | ||
|
d2635bd282 | ||
|
462fa91938 | ||
|
836236ea9b | ||
|
3fcc3c8bfd | ||
|
349d3e3d47 | ||
|
8a41f956f6 | ||
|
80411d38f7 |
10 changed files with 1253 additions and 4 deletions
|
@ -60,6 +60,7 @@
|
|||
"highlight.js": "^10.5.0",
|
||||
"jsrsasign": "^10.2.0",
|
||||
"katex": "^0.12.0",
|
||||
"localforage": "^1.7.3",
|
||||
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop",
|
||||
"matrix-react-sdk": "github:matrix-org/matrix-react-sdk#develop",
|
||||
"matrix-widget-api": "^0.1.0-beta.15",
|
||||
|
@ -67,6 +68,7 @@
|
|||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
"sanitize-html": "^2.3.2",
|
||||
"sql.js": "github:neilalexander/sql.js#252a72bf57b0538cbd49bbd6f70af71e516966ae",
|
||||
"ua-parser-js": "^0.7.24"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -25,7 +25,7 @@ window.React = React;
|
|||
|
||||
import * as sdk from 'matrix-react-sdk';
|
||||
import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg';
|
||||
import { _td, newTranslatableError } from 'matrix-react-sdk/src/languageHandler';
|
||||
import { _t, _td, newTranslatableError } from 'matrix-react-sdk/src/languageHandler';
|
||||
import AutoDiscoveryUtils from 'matrix-react-sdk/src/utils/AutoDiscoveryUtils';
|
||||
import { AutoDiscovery } from "matrix-js-sdk/src/autodiscovery";
|
||||
import * as Lifecycle from "matrix-react-sdk/src/Lifecycle";
|
||||
|
@ -33,6 +33,13 @@ import type MatrixChatType from "matrix-react-sdk/src/components/structures/Matr
|
|||
import { MatrixClientPeg } from 'matrix-react-sdk/src/MatrixClientPeg';
|
||||
import SdkConfig from "matrix-react-sdk/src/SdkConfig";
|
||||
|
||||
// P2P only, probably should be relocated
|
||||
import Modal from 'matrix-react-sdk/src/Modal';
|
||||
import { IDialogProps } from 'matrix-react-sdk/src/components/views/dialogs/IDialogProps';
|
||||
import dis from 'matrix-react-sdk/src/dispatcher/dispatcher';
|
||||
import { getCachedRoomIDForAlias } from 'matrix-react-sdk/src/RoomAliasCache';
|
||||
import { Room } from "matrix-js-sdk/src/models/room";
|
||||
|
||||
import { parseQs, parseQsFromFragment } from './url_utils';
|
||||
import VectorBasePlatform from "./platform/VectorBasePlatform";
|
||||
import { createClient } from "matrix-js-sdk/src/matrix";
|
||||
|
@ -79,6 +86,184 @@ function onNewScreen(screen: string, replaceLast = false) {
|
|||
} else {
|
||||
window.location.assign(hash);
|
||||
}
|
||||
|
||||
if (!window.matrixChat) {
|
||||
return;
|
||||
}
|
||||
let creds = null;
|
||||
if (screen === "register" || screen === "login" || screen === "welcome") {
|
||||
autoRegister().then((newCreds) => {
|
||||
creds = newCreds;
|
||||
return (window.matrixChat as MatrixChatType).onUserCompletedLoginFlow(
|
||||
newCreds, "-",
|
||||
);
|
||||
}, (err) => {
|
||||
console.error("Failed to auto-register:", err);
|
||||
}).then(() => {
|
||||
// first time user
|
||||
if (creds._registered) {
|
||||
p2pFirstTimeSetup();
|
||||
}
|
||||
});
|
||||
} else if (screen.startsWith("room/")) {
|
||||
// room/!foo:bar
|
||||
// room/#foo:bar
|
||||
// if this room is public then make sure it is published.
|
||||
p2pEnsurePublished(screen.split("/")[1]);
|
||||
}
|
||||
}
|
||||
|
||||
const P2PDisplayNameDialog: React.FC<IDialogProps> = ({ onFinished }) => {
|
||||
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
||||
const ChangeDisplayName = sdk.getComponent('settings.ChangeDisplayName');
|
||||
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
|
||||
|
||||
return <BaseDialog
|
||||
onFinished={onFinished}
|
||||
title={_t('Set a display name:')}
|
||||
>
|
||||
<ChangeDisplayName onFinished={onFinished} />
|
||||
<DialogButtons
|
||||
primaryButton={_t('OK')}
|
||||
onPrimaryButtonClick={onFinished}
|
||||
hasCancel={false}
|
||||
/>
|
||||
</BaseDialog>;
|
||||
};
|
||||
|
||||
function p2pFirstTimeSetup() {
|
||||
// Prompt them to set a display name
|
||||
Modal.createDialog(P2PDisplayNameDialog,
|
||||
{
|
||||
onFinished: () => {
|
||||
// View the room directory after display name has been sorted out
|
||||
dis.dispatch({
|
||||
action: 'view_room_directory',
|
||||
});
|
||||
},
|
||||
}, null, /* priority = */ false, /* static = */ true,
|
||||
);
|
||||
}
|
||||
|
||||
async function fetchRoom(roomId: string): Room {
|
||||
const client = MatrixClientPeg.get();
|
||||
let room = client.getRoom(roomId);
|
||||
if (room) {
|
||||
return room;
|
||||
}
|
||||
console.log("p2pEnsurePublished fetchRoom waiting for room... ", roomId);
|
||||
room = await new Promise((resolve, reject) => {
|
||||
let fulfilled = false;
|
||||
const cb = function(room) {
|
||||
if (fulfilled) {
|
||||
return;
|
||||
}
|
||||
const newRoomId = room.roomId;
|
||||
if (roomId === newRoomId) {
|
||||
fulfilled = true;
|
||||
console.log("p2pEnsurePublished fetchRoom found ", roomId);
|
||||
resolve(room);
|
||||
}
|
||||
};
|
||||
client.on("Room", cb);
|
||||
setTimeout(() => {
|
||||
if (fulfilled) {
|
||||
return;
|
||||
}
|
||||
console.log("p2pEnsurePublished fetchRoom timed out ", roomId);
|
||||
fulfilled = true;
|
||||
client.removeListener("Room", cb);
|
||||
reject(new Error("timed out waiting to see room " + roomId));
|
||||
}, 60 * 1000); // wait 60s
|
||||
});
|
||||
return room;
|
||||
}
|
||||
|
||||
async function p2pEnsurePublished(roomIdOrAlias: string) {
|
||||
// If the room has just been created, we need to wait for the join_rules to come down /sync
|
||||
// If the app has just been refreshed, we need to wait for the DB to be loaded.
|
||||
// Since we don't really care when this is done, just sleep a bit.
|
||||
await sleep(3000);
|
||||
console.log("p2pEnsurePublished ", roomIdOrAlias);
|
||||
try {
|
||||
const client = MatrixClientPeg.get();
|
||||
// convert alias to room ID
|
||||
let roomId;
|
||||
let aliasLocalpart;
|
||||
if (roomIdOrAlias.startsWith("!")) {
|
||||
roomId = roomIdOrAlias;
|
||||
} else {
|
||||
roomId = getCachedRoomIDForAlias(roomIdOrAlias);
|
||||
// extract the localpart so we can republish this alias on our server
|
||||
aliasLocalpart = roomIdOrAlias.split(":")[0].substring(1);
|
||||
}
|
||||
|
||||
// fetch the join_rules, check if public
|
||||
const room = await fetchRoom(roomId);
|
||||
if (!room) {
|
||||
throw new Error("No room for room ID: " + roomId);
|
||||
}
|
||||
if (!aliasLocalpart) {
|
||||
const roomName = room.currentState.getStateEvents("m.room.name", "");
|
||||
if (roomName) {
|
||||
aliasLocalpart = roomName.getContent().name;
|
||||
// room alias grammar is poorly defined. Synapse rejects whitespace, Riot barfs on slashes, it's a mess.
|
||||
// so for now, let's just do A-Za-z0-9_-
|
||||
aliasLocalpart = aliasLocalpart.replace(/[^A-Za-z0-9_-]/g, "");
|
||||
} else {
|
||||
// use the random part of the room ID as a fallback.
|
||||
aliasLocalpart = roomId.split(":")[0].substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
const joinRules = room.currentState.getStateEvents("m.room.join_rules", "");
|
||||
if (!joinRules) {
|
||||
throw new Error("No join_rules for room ID: " + roomId);
|
||||
}
|
||||
const isPublic = joinRules.getContent().join_rule === "public";
|
||||
|
||||
if (isPublic) {
|
||||
// make sure that there is an alias mapping
|
||||
try {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const newRoomAlias = `#${aliasLocalpart}:${client.getDomain()}`;
|
||||
let exists = false;
|
||||
let matches = false;
|
||||
try {
|
||||
const aliasResponse = await client.getRoomIdForAlias(newRoomAlias);
|
||||
matches = aliasResponse.room_id === roomId;
|
||||
exists = true;
|
||||
} catch (err) {}
|
||||
console.log(
|
||||
"p2pEnsurePublished: room ID:", roomId, " want alias: ", newRoomAlias,
|
||||
" exists=", exists, " matches=", matches,
|
||||
);
|
||||
if (!exists) {
|
||||
await client.createAlias(newRoomAlias, roomId);
|
||||
break;
|
||||
} else if (!matches) {
|
||||
// clashing room alias, use the room ID.
|
||||
aliasLocalpart = roomId.split(":")[0].substring(1);
|
||||
} else {
|
||||
// exists and matches, do nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("p2pEnsurePublished: problem creating alias: ", err);
|
||||
}
|
||||
|
||||
// publish the room
|
||||
await client.setRoomDirectoryVisibility(roomId, "public");
|
||||
console.log("p2pEnsurePublished: Now published.");
|
||||
} else {
|
||||
// unpublish the room
|
||||
await client.setRoomDirectoryVisibility(roomId, "private");
|
||||
console.log("p2pEnsurePublished: Now hidden.");
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("p2pEnsurePublished encountered an error: ", err);
|
||||
}
|
||||
}
|
||||
|
||||
// We use this to work out what URL the SDK should
|
||||
|
@ -128,6 +313,60 @@ function onTokenLoginCompleted() {
|
|||
window.history.replaceState(null, "", url.href);
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function autoRegister() {
|
||||
console.log("dendrite: Auto-registration in progress");
|
||||
const cli = createClient({
|
||||
baseUrl: window.location.origin,
|
||||
});
|
||||
const password = "this should be really really secure";
|
||||
|
||||
// Make sure the server is up (active service worker)
|
||||
await navigator.serviceWorker.ready;
|
||||
// On Firefox, the ready promise resolves just prior to activation.
|
||||
// On Chrome, the ready promise resolves just after activation.
|
||||
// We need to make requests AFTER we have been activated, else the /register request
|
||||
// will fail.
|
||||
await sleep(10);
|
||||
|
||||
let response = null;
|
||||
let didRegister = false;
|
||||
try {
|
||||
response = await cli.registerRequest({
|
||||
username: "p2p",
|
||||
password,
|
||||
auth: {
|
||||
type: "m.login.dummy",
|
||||
},
|
||||
});
|
||||
console.log("dendrite: Auto-registration done ", response);
|
||||
didRegister = true;
|
||||
} catch (err) {
|
||||
console.error("dendrite: failed to register, trying to login:", err);
|
||||
response = await cli.login("m.login.password", {
|
||||
identifier: {
|
||||
type: "m.id.user",
|
||||
user: "p2p",
|
||||
},
|
||||
password,
|
||||
initial_device_display_name: "p2p-dendrite",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
userId: response.user_id,
|
||||
deviceId: response.device_id,
|
||||
homeserverUrl: cli.getHomeserverUrl(),
|
||||
identityServerUrl: cli.getIdentityServerUrl(),
|
||||
accessToken: response.access_token,
|
||||
guest: cli.isGuest(),
|
||||
_registered: didRegister,
|
||||
};
|
||||
}
|
||||
|
||||
export async function loadApp(fragParams: {}) {
|
||||
// XXX: the way we pass the path to the worker script from webpack via html in body's dataset is a hack
|
||||
// but alternatives seem to require changing the interface to passing Workers to js-sdk
|
||||
|
@ -139,7 +378,46 @@ export async function loadApp(fragParams: {}) {
|
|||
// make sure the indexeddb script is present, so fail hard.
|
||||
throw newTranslatableError(_td("Missing indexeddb worker script!"));
|
||||
}
|
||||
MatrixClientPeg.setIndexedDbWorkerScript(vectorIndexeddbWorkerScript);
|
||||
// MatrixClientPeg.setIndexedDbWorkerScript(vectorIndexeddbWorkerScript);
|
||||
|
||||
// load dendrite, if available
|
||||
const vectorDendriteWorkerScript = document.body.dataset.vectorDendriteWorkerScript;
|
||||
if (vectorDendriteWorkerScript && 'serviceWorker' in navigator) {
|
||||
console.log("dendrite code exec... ", document.readyState);
|
||||
const loadDendriteSw = () => {
|
||||
console.log("Registering dendrite sw...", vectorDendriteWorkerScript);
|
||||
console.log("swjs: invoke navigator.serviceWorker.register");
|
||||
navigator.serviceWorker.register(vectorDendriteWorkerScript, { scope: "/" }).then(function(registration) {
|
||||
console.log("swjs: navigator.serviceWorker.register resolved", registration);
|
||||
// Registration was successful
|
||||
console.log('ServiceWorker sw.js registration successful with scope: ', registration.scope);
|
||||
// periodically check for updates
|
||||
setInterval(function() {
|
||||
console.log("swjs invoke registration.update");
|
||||
registration.update();
|
||||
}, 1000 * 60 * 30); // once every 30 minutes
|
||||
}, (err) => {
|
||||
// registration failed :(
|
||||
console.log('dendrite: ServiceWorker registration failed: ', err);
|
||||
});
|
||||
// First, do a one-off check if there's currently a
|
||||
// service worker in control.
|
||||
if (navigator.serviceWorker.controller) {
|
||||
console.log('dendrite: This page is currently controlled by:', navigator.serviceWorker.controller);
|
||||
}
|
||||
|
||||
// Then, register a handler to detect when a new or
|
||||
// updated service worker takes control.
|
||||
navigator.serviceWorker.oncontrollerchange = function() {
|
||||
console.log('dendrite: This page is now controlled by:', navigator.serviceWorker.controller);
|
||||
};
|
||||
};
|
||||
if (document.readyState === "loading") {
|
||||
window.addEventListener('DOMContentLoaded', loadDendriteSw);
|
||||
} else {
|
||||
loadDendriteSw();
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('hashchange', onHashChange);
|
||||
|
||||
|
|
279
src/vector/dendrite-sw.js
Normal file
279
src/vector/dendrite-sw.js
Normal file
|
@ -0,0 +1,279 @@
|
|||
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||
// Copyright 2020 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Dendrite Service Worker version
|
||||
// Bumping the patch version of this has no side-effects.
|
||||
// Bumping the minor version of this will delete databases.
|
||||
const version = "0.2.0";
|
||||
|
||||
const isVersionBump = function(oldVer, newVer) {
|
||||
oldVer = oldVer || "";
|
||||
const newSegments = newVer.split(".");
|
||||
const oldSegments = oldVer.split(".");
|
||||
if (oldSegments.length != 3) {
|
||||
return true; // brand new
|
||||
}
|
||||
return newSegments[1] > oldSegments[1];
|
||||
}
|
||||
|
||||
const bundle_path = self.location.href.replace("/dendrite_sw.js", "")
|
||||
const id = Math.random();
|
||||
console.log("swjs: ", id," dendrite-sw.js file running...")
|
||||
self.registration.addEventListener('updatefound', () => {
|
||||
console.log("swjs: ", id," updatefound registration event fired")
|
||||
const newWorker = self.registration.installing;
|
||||
if (!newWorker) {
|
||||
console.log("swjs: ", id," updatefound registration event fired, no installing worker")
|
||||
return;
|
||||
}
|
||||
newWorker.addEventListener('statechange', () => {
|
||||
console.log("swjs: ", id," worker statechange: ", newWorker.state)
|
||||
});
|
||||
})
|
||||
|
||||
self.importScripts(`${bundle_path}/wasm_exec.js`,
|
||||
`${bundle_path}/sqlitejs.js`,
|
||||
`${bundle_path}/localforage.js`);
|
||||
|
||||
let isWriting = false;
|
||||
async function writeDatabasesToIndexedDB() {
|
||||
while (true) {
|
||||
await sleep(1000 * 30);
|
||||
try {
|
||||
await syncfs(false);
|
||||
}
|
||||
catch (err) {
|
||||
console.error("syncfs: failed to write to IDB:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function syncfs(isStartup) {
|
||||
return new Promise((resolve, reject) => {
|
||||
global._go_sqlite.FS.syncfs(isStartup, function(err) {
|
||||
if (err) {
|
||||
console.error("syncfs failed:", err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
console.log("syncfs OK");
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function deleteDatabase(name) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
const req = indexedDB.deleteDatabase(name);
|
||||
req.onsuccess = function () {
|
||||
console.log("Deleted database successfully: ", name);
|
||||
resolve();
|
||||
};
|
||||
req.onerror = function (event) {
|
||||
console.log("Couldn't delete database: ", name, event);
|
||||
reject(new Error("failed to delete database"));
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function initDendrite() {
|
||||
console.log(`dendrite-sw.js: v${version} SW init`)
|
||||
// check if we need to purge databases (minor version bump)
|
||||
const prevVer = await global.localforage.getItem("dendrite_version")
|
||||
if (prevVer != version) {
|
||||
const nukeDatabase = isVersionBump(prevVer, version);
|
||||
console.log(`dendrite-sw.js: previous ver ${prevVer} current ${version} nuke databases: ${nukeDatabase}`);
|
||||
if (nukeDatabase) {
|
||||
await deleteDatabase("/idb");
|
||||
}
|
||||
}
|
||||
|
||||
global.process = {
|
||||
pid: 1,
|
||||
env: {
|
||||
DEBUG: "*",
|
||||
}
|
||||
};
|
||||
// we do this for prometheus so it doesn't panic on init()
|
||||
global.fs.stat = function(path, cb) {
|
||||
cb({
|
||||
code: "EINVAL",
|
||||
});
|
||||
}
|
||||
|
||||
const config = {
|
||||
locateFile: filename => `${bundle_path}/../../sql-wasm.wasm`
|
||||
}
|
||||
|
||||
const go = new Go();
|
||||
await sqlitejs.init(config);
|
||||
|
||||
// periodically write databases to disk
|
||||
if (!isWriting) {
|
||||
isWriting = true;
|
||||
// have to do this before we start dendrite for hopefully obvious reasons...
|
||||
console.log("syncfs", global._go_sqlite.IDBFS);
|
||||
global._go_sqlite.FS.mkdir("/idb");
|
||||
//global._go_sqlite.FS.unmount("/");
|
||||
console.log("syncfs at /idb mkdir ok");
|
||||
global._go_sqlite.FS.mount(global._go_sqlite.IDBFS, {}, "/idb");
|
||||
console.log("syncfs mounted");
|
||||
await syncfs(true); // load from IDB
|
||||
writeDatabasesToIndexedDB();
|
||||
}
|
||||
|
||||
console.log(`dendrite-sw.js: v${version} starting dendrite.wasm...`)
|
||||
const result = await WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject)
|
||||
go.run(result.instance).then(() => {
|
||||
console.log(`dendrite-sw.js: v${version} dendrite.wasm terminated, restarting...`);
|
||||
// purge databases and p2p nodes.
|
||||
global._go_js_server = undefined;
|
||||
global._go_sqlite_dbs.clear();
|
||||
initDendritePromise = initDendrite();
|
||||
});
|
||||
// make fetch calls go through this sw - notably if a page registers a sw, it does NOT go through any sw by default
|
||||
// unless you refresh or call this function.
|
||||
console.log(`dendrite-sw.js: v${version} claiming open browser tabs`)
|
||||
console.log("swjs: ", id," invoke self.clients.claim()")
|
||||
self.clients.claim();
|
||||
|
||||
let serverIsUp = false;
|
||||
for (let i = 0; i < 30; i++) { // 3s
|
||||
if (global._go_js_server) {
|
||||
console.log("swjs: ", id," init dendrite promise resolving");
|
||||
serverIsUp = true;
|
||||
break;
|
||||
}
|
||||
await sleep(100);
|
||||
}
|
||||
if (!serverIsUp) {
|
||||
throw new Error("Timed out waiting for _go_js_server to be set.");
|
||||
}
|
||||
// persist the new version
|
||||
await global.localforage.setItem("dendrite_version", version);
|
||||
}
|
||||
|
||||
let initDendritePromise = initDendrite();
|
||||
|
||||
self.addEventListener('install', function(event) {
|
||||
console.log("swjs: ", id," install event fired:", event)
|
||||
console.log(`dendrite-sw.js: v${version} SW install`)
|
||||
// Tell the browser to kill old sw's running in other tabs and replace them with this one
|
||||
// This may cause spontaneous logouts.
|
||||
console.log("swjs: ", id," invoke self.skipWaiting")
|
||||
self.skipWaiting();
|
||||
})
|
||||
|
||||
self.addEventListener('activate', function(event) {
|
||||
console.log("swjs: ", id," activate event fired")
|
||||
console.log(`dendrite-sw.js: v${version} SW activate`)
|
||||
event.waitUntil(initDendritePromise)
|
||||
})
|
||||
|
||||
async function sendRequestToGo(event) {
|
||||
await initDendritePromise; // this sets the global fetch listener
|
||||
if (!global._go_js_server || !global._go_js_server.fetch) {
|
||||
console.log(`dendrite-sw.js: v${version} no fetch listener present for ${event.request.url}`);
|
||||
return
|
||||
}
|
||||
console.log(`dendrite-sw.js: v${version} forwarding ${event.request.url} to Go`);
|
||||
const req = event.request
|
||||
let reqHeaders = ''
|
||||
if (req.headers) {
|
||||
for (const header of req.headers) {
|
||||
// FIXME: is this a safe header encoding?
|
||||
reqHeaders += `${header[0]}: ${header[1]}\n`
|
||||
}
|
||||
}
|
||||
let jj = null;
|
||||
if (req.method === "POST" || req.method === "PUT") {
|
||||
jj = await req.json();
|
||||
jj = JSON.stringify(jj);
|
||||
reqHeaders += `Content-Length: ${new Blob([jj]).size}`; // include utf-8 chars properly
|
||||
}
|
||||
|
||||
if (reqHeaders.length > 0) {
|
||||
reqHeaders = `\r\n${reqHeaders}`
|
||||
}
|
||||
|
||||
// Replace the timeout value for /sync calls to be 20s not 30s because Firefox
|
||||
// will aggressively cull service workers after a 30s idle period. Chrome doesn't.
|
||||
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1378587
|
||||
const fullurl = req.url.replace("timeout=30000", "timeout=20000");
|
||||
|
||||
const reqString = `${req.method} ${fullurl} HTTP/1.0${reqHeaders}\r\n\r\n${jj ? jj : ''}`
|
||||
|
||||
const res = await global._go_js_server.fetch(reqString)
|
||||
if (res.error) {
|
||||
console.error(`dendrite-sw.js: v${version} Error for request: ${event.request.url} => ${res.error}`)
|
||||
return
|
||||
}
|
||||
const respString = res.result;
|
||||
|
||||
const m = respString.match(/^(HTTP\/1.[01]) ((.*?) (.*?))(\r\n([^]*?)?(\r\n\r\n([^]*?)))?$/)
|
||||
if (!m) {
|
||||
console.warn("couldn't parse resp", respString);
|
||||
return;
|
||||
}
|
||||
const response = {
|
||||
"proto": m[1],
|
||||
"status": m[2],
|
||||
"statusCode": parseInt(m[3]),
|
||||
"headers": m[6],
|
||||
"body": m[8],
|
||||
}
|
||||
|
||||
const respHeaders = new Headers()
|
||||
const headerLines = response.headers.split('\r\n')
|
||||
for (const headerLine of headerLines) {
|
||||
// FIXME: is this safe header parsing? Do we need to worry about line-wrapping?
|
||||
const match = headerLine.match(/^(.+?): *(.*?)$/)
|
||||
if (match) {
|
||||
respHeaders.append(match[1], match[2])
|
||||
}
|
||||
else {
|
||||
console.log("couldn't parse headerLine ", headerLine)
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(response.body, {
|
||||
status: response.statusCode,
|
||||
headers: respHeaders,
|
||||
})
|
||||
}
|
||||
|
||||
self.addEventListener('fetch', function(event) {
|
||||
event.respondWith((async () => {
|
||||
/*
|
||||
// If this is a page refresh for the current page, then shunt in the new sw
|
||||
// https://github.com/w3c/ServiceWorker/issues/1238
|
||||
if (event.request.mode === "navigate" && event.request.method === "GET" && registration.waiting && (await clients.matchAll()).length < 2) {
|
||||
console.log("Forcing new sw.js into page")
|
||||
registration.waiting.postMessage('skipWaiting');
|
||||
return new Response("", {headers: {"Refresh": "0"}});
|
||||
} */
|
||||
|
||||
if (event.request.url.match(/\/_matrix\/client/)) {
|
||||
return await sendRequestToGo(event);
|
||||
}
|
||||
else {
|
||||
return fetch(event.request);
|
||||
}
|
||||
})());
|
||||
})
|
BIN
src/vector/dendrite.wasm
Executable file
BIN
src/vector/dendrite.wasm
Executable file
Binary file not shown.
|
@ -62,6 +62,7 @@
|
|||
style="height: 100%; margin: 0;"
|
||||
data-vector-indexeddb-worker-script="<%= htmlWebpackPlugin.files.js.find(entry => entry.includes("indexeddb-worker.js")) %>"
|
||||
data-vector-recorder-worklet-script="<%= htmlWebpackPlugin.files.js.find(entry => entry.includes("recorder-worklet.js")) %>"
|
||||
data-vector-dendrite-worker-script="<%= htmlWebpackPlugin.files.js.find(entry => entry.includes("dendrite_sw.js")) %>"
|
||||
>
|
||||
<noscript>Sorry, Element requires JavaScript to be enabled.</noscript> <!-- TODO: Translate this? -->
|
||||
<section id="matrixchat" style="height: 100%;" class="notranslate"></section>
|
||||
|
|
|
@ -37,7 +37,7 @@ export default class WebPlatform extends VectorBasePlatform {
|
|||
super();
|
||||
// Register service worker if available on this platform
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register('sw.js');
|
||||
// navigator.serviceWorker.register('sw.js');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
22
src/vector/sqlitejs.js
Normal file
22
src/vector/sqlitejs.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import initSqlJs from 'sql.js'
|
||||
|
||||
export function init(config) {
|
||||
return initSqlJs(config).then(SQL => {
|
||||
global._go_sqlite = SQL;
|
||||
console.log("Loaded sqlite")
|
||||
})
|
||||
}
|
626
src/vector/wasm_exec.js
Normal file
626
src/vector/wasm_exec.js
Normal file
|
@ -0,0 +1,626 @@
|
|||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
(() => {
|
||||
// Map multiple JavaScript environments to a single common API,
|
||||
// preferring web standards over Node.js API.
|
||||
//
|
||||
// Environments considered:
|
||||
// - Browsers
|
||||
// - Node.js
|
||||
// - Electron
|
||||
// - Parcel
|
||||
// - Webpack
|
||||
|
||||
if (typeof global !== "undefined") {
|
||||
// global already exists
|
||||
} else if (typeof window !== "undefined") {
|
||||
window.global = window;
|
||||
} else if (typeof self !== "undefined") {
|
||||
self.global = self;
|
||||
} else {
|
||||
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
||||
}
|
||||
|
||||
if (!global.require && typeof require !== "undefined") {
|
||||
global.require = require;
|
||||
}
|
||||
|
||||
if (!global.fs && global.require) {
|
||||
const fs = require("fs");
|
||||
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
|
||||
global.fs = fs;
|
||||
}
|
||||
}
|
||||
|
||||
const enosys = () => {
|
||||
const err = new Error("not implemented");
|
||||
err.code = "ENOSYS";
|
||||
return err;
|
||||
};
|
||||
|
||||
if (!global.fs) {
|
||||
let outputBuf = "";
|
||||
global.fs = {
|
||||
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
||||
writeSync(fd, buf) {
|
||||
outputBuf += decoder.decode(buf);
|
||||
const nl = outputBuf.lastIndexOf("\n");
|
||||
if (nl != -1) {
|
||||
console.log(outputBuf.substr(0, nl));
|
||||
outputBuf = outputBuf.substr(nl + 1);
|
||||
}
|
||||
return buf.length;
|
||||
},
|
||||
write(fd, buf, offset, length, position, callback) {
|
||||
if (offset !== 0 || length !== buf.length || position !== null) {
|
||||
callback(enosys());
|
||||
return;
|
||||
}
|
||||
const n = this.writeSync(fd, buf);
|
||||
callback(null, n);
|
||||
},
|
||||
chmod(path, mode, callback) { callback(enosys()); },
|
||||
chown(path, uid, gid, callback) { callback(enosys()); },
|
||||
close(fd, callback) { callback(enosys()); },
|
||||
fchmod(fd, mode, callback) { callback(enosys()); },
|
||||
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
||||
fstat(fd, callback) { callback(enosys()); },
|
||||
fsync(fd, callback) { callback(null); },
|
||||
ftruncate(fd, length, callback) { callback(enosys()); },
|
||||
lchown(path, uid, gid, callback) { callback(enosys()); },
|
||||
link(path, link, callback) { callback(enosys()); },
|
||||
lstat(path, callback) { callback(enosys()); },
|
||||
mkdir(path, perm, callback) { callback(enosys()); },
|
||||
open(path, flags, mode, callback) { callback(enosys()); },
|
||||
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
||||
readdir(path, callback) { callback(enosys()); },
|
||||
readlink(path, callback) { callback(enosys()); },
|
||||
rename(from, to, callback) { callback(enosys()); },
|
||||
rmdir(path, callback) { callback(enosys()); },
|
||||
stat(path, callback) { callback(enosys()); },
|
||||
symlink(path, link, callback) { callback(enosys()); },
|
||||
truncate(path, length, callback) { callback(enosys()); },
|
||||
unlink(path, callback) { callback(enosys()); },
|
||||
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
||||
};
|
||||
}
|
||||
|
||||
if (!global.process) {
|
||||
global.process = {
|
||||
getuid() { return -1; },
|
||||
getgid() { return -1; },
|
||||
geteuid() { return -1; },
|
||||
getegid() { return -1; },
|
||||
getgroups() { throw enosys(); },
|
||||
pid: -1,
|
||||
ppid: -1,
|
||||
umask() { throw enosys(); },
|
||||
cwd() { throw enosys(); },
|
||||
chdir() { throw enosys(); },
|
||||
}
|
||||
}
|
||||
|
||||
if (!global.crypto && global.require) {
|
||||
const nodeCrypto = require("crypto");
|
||||
global.crypto = {
|
||||
getRandomValues(b) {
|
||||
nodeCrypto.randomFillSync(b);
|
||||
},
|
||||
};
|
||||
}
|
||||
if (!global.crypto) {
|
||||
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
|
||||
}
|
||||
|
||||
if (!global.performance) {
|
||||
global.performance = {
|
||||
now() {
|
||||
const [sec, nsec] = process.hrtime();
|
||||
return sec * 1000 + nsec / 1000000;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!global.TextEncoder && global.require) {
|
||||
global.TextEncoder = require("util").TextEncoder;
|
||||
}
|
||||
if (!global.TextEncoder) {
|
||||
throw new Error("global.TextEncoder is not available, polyfill required");
|
||||
}
|
||||
|
||||
if (!global.TextDecoder && global.require) {
|
||||
global.TextDecoder = require("util").TextDecoder;
|
||||
}
|
||||
if (!global.TextDecoder) {
|
||||
throw new Error("global.TextDecoder is not available, polyfill required");
|
||||
}
|
||||
|
||||
// End of polyfills for common API.
|
||||
|
||||
const encoder = new TextEncoder("utf-8");
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
|
||||
global.Go = class {
|
||||
constructor() {
|
||||
this.argv = ["js"];
|
||||
this.env = {};
|
||||
this.exit = (code) => {
|
||||
if (code !== 0) {
|
||||
console.warn("exit code:", code);
|
||||
}
|
||||
};
|
||||
this._exitPromise = new Promise((resolve) => {
|
||||
this._resolveExitPromise = resolve;
|
||||
});
|
||||
this._pendingEvent = null;
|
||||
this._scheduledTimeouts = new Map();
|
||||
this._nextCallbackTimeoutID = 1;
|
||||
|
||||
const setInt64 = (addr, v) => {
|
||||
this.mem.setUint32(addr + 0, v, true);
|
||||
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
||||
}
|
||||
|
||||
const getInt64 = (addr) => {
|
||||
const low = this.mem.getUint32(addr + 0, true);
|
||||
const high = this.mem.getInt32(addr + 4, true);
|
||||
return low + high * 4294967296;
|
||||
}
|
||||
|
||||
const loadValue = (addr) => {
|
||||
const f = this.mem.getFloat64(addr, true);
|
||||
if (f === 0) {
|
||||
return undefined;
|
||||
}
|
||||
if (!isNaN(f)) {
|
||||
return f;
|
||||
}
|
||||
|
||||
const id = this.mem.getUint32(addr, true);
|
||||
return this._values[id];
|
||||
}
|
||||
|
||||
const storeValue = (addr, v) => {
|
||||
const nanHead = 0x7FF80000;
|
||||
|
||||
if (typeof v === "number" && v !== 0) {
|
||||
if (isNaN(v)) {
|
||||
this.mem.setUint32(addr + 4, nanHead, true);
|
||||
this.mem.setUint32(addr, 0, true);
|
||||
return;
|
||||
}
|
||||
this.mem.setFloat64(addr, v, true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (v === undefined) {
|
||||
this.mem.setFloat64(addr, 0, true);
|
||||
return;
|
||||
}
|
||||
|
||||
let id = this._ids.get(v);
|
||||
if (id === undefined) {
|
||||
id = this._idPool.pop();
|
||||
if (id === undefined) {
|
||||
id = this._values.length;
|
||||
}
|
||||
this._values[id] = v;
|
||||
this._goRefCounts[id] = 0;
|
||||
this._ids.set(v, id);
|
||||
}
|
||||
this._goRefCounts[id]++;
|
||||
let typeFlag = 0;
|
||||
switch (typeof v) {
|
||||
case "object":
|
||||
if (v !== null) {
|
||||
typeFlag = 1;
|
||||
}
|
||||
break;
|
||||
case "string":
|
||||
typeFlag = 2;
|
||||
break;
|
||||
case "symbol":
|
||||
typeFlag = 3;
|
||||
break;
|
||||
case "function":
|
||||
typeFlag = 4;
|
||||
break;
|
||||
}
|
||||
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
|
||||
this.mem.setUint32(addr, id, true);
|
||||
}
|
||||
|
||||
const loadSlice = (addr) => {
|
||||
const array = getInt64(addr + 0);
|
||||
const len = getInt64(addr + 8);
|
||||
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
|
||||
}
|
||||
|
||||
const loadSliceOfValues = (addr) => {
|
||||
const array = getInt64(addr + 0);
|
||||
const len = getInt64(addr + 8);
|
||||
const a = new Array(len);
|
||||
for (let i = 0; i < len; i++) {
|
||||
a[i] = loadValue(array + i * 8);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
const loadString = (addr) => {
|
||||
const saddr = getInt64(addr + 0);
|
||||
const len = getInt64(addr + 8);
|
||||
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
|
||||
}
|
||||
|
||||
const timeOrigin = Date.now() - performance.now();
|
||||
this.importObject = {
|
||||
go: {
|
||||
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
||||
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
||||
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
||||
// This changes the SP, thus we have to update the SP used by the imported function.
|
||||
|
||||
// func wasmExit(code int32)
|
||||
"runtime.wasmExit": (sp) => {
|
||||
sp >>>= 0;
|
||||
const code = this.mem.getInt32(sp + 8, true);
|
||||
this.exited = true;
|
||||
delete this._inst;
|
||||
delete this._values;
|
||||
delete this._goRefCounts;
|
||||
delete this._ids;
|
||||
delete this._idPool;
|
||||
this.exit(code);
|
||||
},
|
||||
|
||||
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
||||
"runtime.wasmWrite": (sp) => {
|
||||
sp >>>= 0;
|
||||
const fd = getInt64(sp + 8);
|
||||
const p = getInt64(sp + 16);
|
||||
const n = this.mem.getInt32(sp + 24, true);
|
||||
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
||||
},
|
||||
|
||||
// func resetMemoryDataView()
|
||||
"runtime.resetMemoryDataView": (sp) => {
|
||||
sp >>>= 0;
|
||||
this.mem = new DataView(this._inst.exports.mem.buffer);
|
||||
},
|
||||
|
||||
// func nanotime1() int64
|
||||
"runtime.nanotime1": (sp) => {
|
||||
sp >>>= 0;
|
||||
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
||||
},
|
||||
|
||||
// func walltime1() (sec int64, nsec int32)
|
||||
"runtime.walltime1": (sp) => {
|
||||
sp >>>= 0;
|
||||
const msec = (new Date).getTime();
|
||||
setInt64(sp + 8, msec / 1000);
|
||||
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
||||
},
|
||||
|
||||
// func scheduleTimeoutEvent(delay int64) int32
|
||||
"runtime.scheduleTimeoutEvent": (sp) => {
|
||||
sp >>>= 0;
|
||||
const id = this._nextCallbackTimeoutID;
|
||||
this._nextCallbackTimeoutID++;
|
||||
this._scheduledTimeouts.set(id, setTimeout(
|
||||
() => {
|
||||
this._resume();
|
||||
while (this._scheduledTimeouts.has(id)) {
|
||||
// for some reason Go failed to register the timeout event, log and try again
|
||||
// (temporary workaround for https://github.com/golang/go/issues/28975)
|
||||
console.warn("scheduleTimeoutEvent: missed timeout event");
|
||||
this._resume();
|
||||
}
|
||||
},
|
||||
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
||||
));
|
||||
this.mem.setInt32(sp + 16, id, true);
|
||||
},
|
||||
|
||||
// func clearTimeoutEvent(id int32)
|
||||
"runtime.clearTimeoutEvent": (sp) => {
|
||||
sp >>>= 0;
|
||||
const id = this.mem.getInt32(sp + 8, true);
|
||||
clearTimeout(this._scheduledTimeouts.get(id));
|
||||
this._scheduledTimeouts.delete(id);
|
||||
},
|
||||
|
||||
// func getRandomData(r []byte)
|
||||
"runtime.getRandomData": (sp) => {
|
||||
sp >>>= 0;
|
||||
crypto.getRandomValues(loadSlice(sp + 8));
|
||||
},
|
||||
|
||||
// func finalizeRef(v ref)
|
||||
"syscall/js.finalizeRef": (sp) => {
|
||||
sp >>>= 0;
|
||||
const id = this.mem.getUint32(sp + 8, true);
|
||||
this._goRefCounts[id]--;
|
||||
if (this._goRefCounts[id] === 0) {
|
||||
const v = this._values[id];
|
||||
this._values[id] = null;
|
||||
this._ids.delete(v);
|
||||
this._idPool.push(id);
|
||||
}
|
||||
},
|
||||
|
||||
// func stringVal(value string) ref
|
||||
"syscall/js.stringVal": (sp) => {
|
||||
sp >>>= 0;
|
||||
storeValue(sp + 24, loadString(sp + 8));
|
||||
},
|
||||
|
||||
// func valueGet(v ref, p string) ref
|
||||
"syscall/js.valueGet": (sp) => {
|
||||
sp >>>= 0;
|
||||
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
|
||||
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
||||
storeValue(sp + 32, result);
|
||||
},
|
||||
|
||||
// func valueSet(v ref, p string, x ref)
|
||||
"syscall/js.valueSet": (sp) => {
|
||||
sp >>>= 0;
|
||||
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
||||
},
|
||||
|
||||
// func valueDelete(v ref, p string)
|
||||
"syscall/js.valueDelete": (sp) => {
|
||||
sp >>>= 0;
|
||||
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
||||
},
|
||||
|
||||
// func valueIndex(v ref, i int) ref
|
||||
"syscall/js.valueIndex": (sp) => {
|
||||
sp >>>= 0;
|
||||
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
||||
},
|
||||
|
||||
// valueSetIndex(v ref, i int, x ref)
|
||||
"syscall/js.valueSetIndex": (sp) => {
|
||||
sp >>>= 0;
|
||||
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
||||
},
|
||||
|
||||
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
||||
"syscall/js.valueCall": (sp) => {
|
||||
sp >>>= 0;
|
||||
try {
|
||||
const v = loadValue(sp + 8);
|
||||
const m = Reflect.get(v, loadString(sp + 16));
|
||||
const args = loadSliceOfValues(sp + 32);
|
||||
const result = Reflect.apply(m, v, args);
|
||||
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
||||
storeValue(sp + 56, result);
|
||||
this.mem.setUint8(sp + 64, 1);
|
||||
} catch (err) {
|
||||
storeValue(sp + 56, err);
|
||||
this.mem.setUint8(sp + 64, 0);
|
||||
}
|
||||
},
|
||||
|
||||
// func valueInvoke(v ref, args []ref) (ref, bool)
|
||||
"syscall/js.valueInvoke": (sp) => {
|
||||
sp >>>= 0;
|
||||
try {
|
||||
const v = loadValue(sp + 8);
|
||||
const args = loadSliceOfValues(sp + 16);
|
||||
const result = Reflect.apply(v, undefined, args);
|
||||
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
||||
storeValue(sp + 40, result);
|
||||
this.mem.setUint8(sp + 48, 1);
|
||||
} catch (err) {
|
||||
storeValue(sp + 40, err);
|
||||
this.mem.setUint8(sp + 48, 0);
|
||||
}
|
||||
},
|
||||
|
||||
// func valueNew(v ref, args []ref) (ref, bool)
|
||||
"syscall/js.valueNew": (sp) => {
|
||||
sp >>>= 0;
|
||||
try {
|
||||
const v = loadValue(sp + 8);
|
||||
const args = loadSliceOfValues(sp + 16);
|
||||
const result = Reflect.construct(v, args);
|
||||
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
||||
storeValue(sp + 40, result);
|
||||
this.mem.setUint8(sp + 48, 1);
|
||||
} catch (err) {
|
||||
storeValue(sp + 40, err);
|
||||
this.mem.setUint8(sp + 48, 0);
|
||||
}
|
||||
},
|
||||
|
||||
// func valueLength(v ref) int
|
||||
"syscall/js.valueLength": (sp) => {
|
||||
sp >>>= 0;
|
||||
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
||||
},
|
||||
|
||||
// valuePrepareString(v ref) (ref, int)
|
||||
"syscall/js.valuePrepareString": (sp) => {
|
||||
sp >>>= 0;
|
||||
const str = encoder.encode(String(loadValue(sp + 8)));
|
||||
storeValue(sp + 16, str);
|
||||
setInt64(sp + 24, str.length);
|
||||
},
|
||||
|
||||
// valueLoadString(v ref, b []byte)
|
||||
"syscall/js.valueLoadString": (sp) => {
|
||||
sp >>>= 0;
|
||||
const str = loadValue(sp + 8);
|
||||
loadSlice(sp + 16).set(str);
|
||||
},
|
||||
|
||||
// func valueInstanceOf(v ref, t ref) bool
|
||||
"syscall/js.valueInstanceOf": (sp) => {
|
||||
sp >>>= 0;
|
||||
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
|
||||
},
|
||||
|
||||
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
||||
"syscall/js.copyBytesToGo": (sp) => {
|
||||
sp >>>= 0;
|
||||
const dst = loadSlice(sp + 8);
|
||||
const src = loadValue(sp + 32);
|
||||
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
||||
this.mem.setUint8(sp + 48, 0);
|
||||
return;
|
||||
}
|
||||
const toCopy = src.subarray(0, dst.length);
|
||||
dst.set(toCopy);
|
||||
setInt64(sp + 40, toCopy.length);
|
||||
this.mem.setUint8(sp + 48, 1);
|
||||
},
|
||||
|
||||
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
||||
"syscall/js.copyBytesToJS": (sp) => {
|
||||
sp >>>= 0;
|
||||
const dst = loadValue(sp + 8);
|
||||
const src = loadSlice(sp + 16);
|
||||
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
||||
this.mem.setUint8(sp + 48, 0);
|
||||
return;
|
||||
}
|
||||
const toCopy = src.subarray(0, dst.length);
|
||||
dst.set(toCopy);
|
||||
setInt64(sp + 40, toCopy.length);
|
||||
this.mem.setUint8(sp + 48, 1);
|
||||
},
|
||||
|
||||
"debug": (value) => {
|
||||
console.log(value);
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async run(instance) {
|
||||
if (!(instance instanceof WebAssembly.Instance)) {
|
||||
throw new Error("Go.run: WebAssembly.Instance expected");
|
||||
}
|
||||
this._inst = instance;
|
||||
this.mem = new DataView(this._inst.exports.mem.buffer);
|
||||
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
||||
NaN,
|
||||
0,
|
||||
null,
|
||||
true,
|
||||
false,
|
||||
global,
|
||||
this,
|
||||
];
|
||||
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
|
||||
this._ids = new Map([ // mapping from JS values to reference ids
|
||||
[0, 1],
|
||||
[null, 2],
|
||||
[true, 3],
|
||||
[false, 4],
|
||||
[global, 5],
|
||||
[this, 6],
|
||||
]);
|
||||
this._idPool = []; // unused ids that have been garbage collected
|
||||
this.exited = false; // whether the Go program has exited
|
||||
|
||||
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
||||
let offset = 4096;
|
||||
|
||||
const strPtr = (str) => {
|
||||
const ptr = offset;
|
||||
const bytes = encoder.encode(str + "\0");
|
||||
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
|
||||
offset += bytes.length;
|
||||
if (offset % 8 !== 0) {
|
||||
offset += 8 - (offset % 8);
|
||||
}
|
||||
return ptr;
|
||||
};
|
||||
|
||||
const argc = this.argv.length;
|
||||
|
||||
const argvPtrs = [];
|
||||
this.argv.forEach((arg) => {
|
||||
argvPtrs.push(strPtr(arg));
|
||||
});
|
||||
argvPtrs.push(0);
|
||||
|
||||
const keys = Object.keys(this.env).sort();
|
||||
keys.forEach((key) => {
|
||||
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
|
||||
});
|
||||
argvPtrs.push(0);
|
||||
|
||||
const argv = offset;
|
||||
argvPtrs.forEach((ptr) => {
|
||||
this.mem.setUint32(offset, ptr, true);
|
||||
this.mem.setUint32(offset + 4, 0, true);
|
||||
offset += 8;
|
||||
});
|
||||
|
||||
this._inst.exports.run(argc, argv);
|
||||
if (this.exited) {
|
||||
this._resolveExitPromise();
|
||||
}
|
||||
await this._exitPromise;
|
||||
}
|
||||
|
||||
_resume() {
|
||||
if (this.exited) {
|
||||
throw new Error("Go program has already exited");
|
||||
}
|
||||
this._inst.exports.resume();
|
||||
if (this.exited) {
|
||||
this._resolveExitPromise();
|
||||
}
|
||||
}
|
||||
|
||||
_makeFuncWrapper(id) {
|
||||
const go = this;
|
||||
return function () {
|
||||
const event = { id: id, this: this, args: arguments };
|
||||
go._pendingEvent = event;
|
||||
go._resume();
|
||||
return event.result;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
typeof module !== "undefined" &&
|
||||
global.require &&
|
||||
global.require.main === module &&
|
||||
global.process &&
|
||||
global.process.versions &&
|
||||
!global.process.versions.electron
|
||||
) {
|
||||
if (process.argv.length < 3) {
|
||||
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const go = new Go();
|
||||
go.argv = process.argv.slice(2);
|
||||
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
|
||||
go.exit = process.exit;
|
||||
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
|
||||
process.on("exit", (code) => { // Node.js exits if no event handler is pending
|
||||
if (code === 0 && !go.exited) {
|
||||
// deadlock, make Go print error and stack traces
|
||||
go._pendingEvent = { id: 0 };
|
||||
go._resume();
|
||||
}
|
||||
});
|
||||
return go.run(result.instance);
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
})();
|
|
@ -50,6 +50,7 @@ module.exports = (env, argv) => {
|
|||
|
||||
node: {
|
||||
// Mock out the NodeFS module: The opus decoder imports this wrongly.
|
||||
// Also needed by sql.js in P2P mode.
|
||||
fs: 'empty',
|
||||
},
|
||||
|
||||
|
@ -61,6 +62,14 @@ module.exports = (env, argv) => {
|
|||
"usercontent": "./node_modules/matrix-react-sdk/src/usercontent/index.js",
|
||||
"recorder-worklet": "./node_modules/matrix-react-sdk/src/voice/RecorderWorklet.ts",
|
||||
|
||||
// P2P
|
||||
"dendrite_sw": "./src/vector/dendrite-sw.js",
|
||||
"sqlitejs": "./src/vector/sqlitejs.js",
|
||||
"localforage": "./node_modules/localforage/dist/localforage.min.js",
|
||||
"sql_wasm": "./node_modules/sql.js/dist/sql-wasm.wasm",
|
||||
"dendrite_wasm": "./src/vector/dendrite.wasm",
|
||||
"wasm_exec": "./src/vector/wasm_exec.js",
|
||||
|
||||
// CSS themes
|
||||
"theme-legacy": "./node_modules/matrix-react-sdk/res/themes/legacy-light/css/legacy-light.scss",
|
||||
"theme-legacy-dark": "./node_modules/matrix-react-sdk/res/themes/legacy-dark/css/legacy-dark.scss",
|
||||
|
@ -268,7 +277,9 @@ module.exports = (env, argv) => {
|
|||
loader: "file-loader",
|
||||
type: "javascript/auto", // https://github.com/webpack/webpack/issues/6725
|
||||
options: {
|
||||
name: '[name].[hash:7].[ext]',
|
||||
// fixme: reintroduce [hash] once we can figure out how to pass it into the
|
||||
// dendrite service worker nicely
|
||||
name: '[name].[ext]',
|
||||
outputPath: '.',
|
||||
},
|
||||
},
|
||||
|
@ -450,6 +461,8 @@ module.exports = (env, argv) => {
|
|||
// chunks even after the app is redeployed.
|
||||
filename: "bundles/[hash]/[name].js",
|
||||
chunkFilename: "bundles/[hash]/[name].js",
|
||||
libraryTarget: "var",
|
||||
library: "[name]",
|
||||
},
|
||||
|
||||
// configuration for the webpack-dev-server
|
||||
|
@ -461,11 +474,16 @@ module.exports = (env, argv) => {
|
|||
// This hides the massive list of modules.
|
||||
stats: 'minimal',
|
||||
|
||||
headers: {
|
||||
"Service-Worker-Allowed": "/",
|
||||
},
|
||||
|
||||
// hot module replacement doesn't work (I think we'd need react-hot-reload?)
|
||||
// so webpack-dev-server reloads the page on every update which is quite
|
||||
// tedious in Riot since that can take a while.
|
||||
hot: false,
|
||||
inline: false,
|
||||
writeToDisk: true,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
23
yarn.lock
23
yarn.lock
|
@ -5761,6 +5761,11 @@ ignore@^5.1.4, ignore@^5.1.8:
|
|||
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
|
||||
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
|
||||
|
||||
immediate@~3.0.5:
|
||||
version "3.0.6"
|
||||
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
|
||||
integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=
|
||||
|
||||
immutable@^3.7.4:
|
||||
version "3.8.2"
|
||||
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"
|
||||
|
@ -7145,6 +7150,13 @@ levn@~0.3.0:
|
|||
prelude-ls "~1.1.2"
|
||||
type-check "~0.3.2"
|
||||
|
||||
lie@3.1.1:
|
||||
version "3.1.1"
|
||||
resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e"
|
||||
integrity sha1-mkNrLMd0bKWd56QfpGmz77dr2H4=
|
||||
dependencies:
|
||||
immediate "~3.0.5"
|
||||
|
||||
lines-and-columns@^1.1.6:
|
||||
version "1.1.6"
|
||||
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
|
||||
|
@ -7176,6 +7188,13 @@ loader-utils@^1.1.0, loader-utils@^1.2.3, loader-utils@^1.4.0:
|
|||
emojis-list "^3.0.0"
|
||||
json5 "^1.0.1"
|
||||
|
||||
localforage@^1.7.3:
|
||||
version "1.9.0"
|
||||
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.9.0.tgz#f3e4d32a8300b362b4634cc4e066d9d00d2f09d1"
|
||||
integrity sha512-rR1oyNrKulpe+VM9cYmcFn6tsHuokyVHFaCM3+osEmxaHTbEk8oQu6eGDfS6DQLWi/N67XRmB8ECG37OES368g==
|
||||
dependencies:
|
||||
lie "3.1.1"
|
||||
|
||||
locate-path@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
|
||||
|
@ -10817,6 +10836,10 @@ sprintf-js@~1.0.2:
|
|||
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
|
||||
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=
|
||||
|
||||
"sql.js@github:neilalexander/sql.js#252a72bf57b0538cbd49bbd6f70af71e516966ae":
|
||||
version "1.5.0"
|
||||
resolved "https://codeload.github.com/neilalexander/sql.js/tar.gz/252a72bf57b0538cbd49bbd6f70af71e516966ae"
|
||||
|
||||
sshpk@^1.7.0:
|
||||
version "1.16.1"
|
||||
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877"
|
||||
|
|
Loading…
Add table
Reference in a new issue