Skip to content

Commit

Permalink
fix cookies, subscriber cache reset
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Nov 20, 2023
1 parent f81ecd5 commit dd91195
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 76 deletions.
17 changes: 16 additions & 1 deletion init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,22 @@ export async function init() {

// init persistent subscriber cache
(async () => {
Runtime.subscriber_cache = (await Storage.loadOrCreate("Datex.Runtime.SUBSCRIBER_CACHE", ()=>new Map())).setAutoDefault(Set);
try {
Runtime.subscriber_cache = (await Storage.loadOrCreate(
"Datex.Runtime.SUBSCRIBER_CACHE",
() => new Map(),
{onlyLocalPointers: true}
)).setAutoDefault(Set);
}
catch (e) {
logger.debug("resetting subscriber cache (" + e?.message + ")")
Runtime.subscriber_cache = (await Storage.loadOrCreate(
"Datex.Runtime.SUBSCRIBER_CACHE",
() => new Map(),
undefined,
true
)).setAutoDefault(Set);
}
})()


Expand Down
6 changes: 5 additions & 1 deletion network/supranet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export class Supranet {
return `@@${buffer2hex(new Uint8Array(id.buffer))}`;
}


public static async getLocalEndpointAndKeys():Promise<[Endpoint|UnresolvedEndpointProperty, Crypto.ExportedKeySet]> {
let endpoint: Endpoint|UnresolvedEndpointProperty;

Expand All @@ -339,8 +340,11 @@ export class Supranet {
return [endpoint, await this.getKeysOrGenerateNew()];
}

/**
* Create new anonymous endpoint or load from "datex-endpoint" cookie
*/
private static createAndSaveNewEndpoint(){
const endpoint = <Endpoint> Endpoint.get(this.createEndpointId());
const endpoint = Endpoint.getFromCookie() ?? <Endpoint> Endpoint.get(Endpoint.createNewID());
endpoint_config.endpoint = endpoint;
endpoint_config.save();
return endpoint;
Expand Down
14 changes: 11 additions & 3 deletions runtime/pointers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,14 @@ export class Pointer<T = any> extends Ref<T> {

const id_string = Pointer.normalizePointerId(id);

// only load if local pointer id
if (SCOPE?.exec_conditions?.onlyLocalPointers) {
const origin = Pointer.getOriginFromPointerId(id_string);
if (origin !== LOCAL_ENDPOINT && origin !== Runtime.endpoint) {
throw new Error("Tried to load non-local pointer");
}
}

if (SCOPE) {
// recursive pointer loading! TODO
if (this.loading_pointers.get(id_string)?.scopeList.has(SCOPE)) {
Expand Down Expand Up @@ -1205,10 +1213,11 @@ export class Pointer<T = any> extends Ref<T> {
let priority:number;
for ([source,priority] of this.#pointer_sources) {
try {
stored = await source.getPointer(pointer.id, !SCOPE);
stored = await source.getPointer(pointer.id, !SCOPE, SCOPE?.exec_conditions?.onlyLocalPointers??false);
}
catch (e) {
logger.error("pointer source error:",e)
this.loading_pointers.delete(id_string);
throw e;
}
if (stored != NOT_EXISTING) break;
}
Expand Down Expand Up @@ -1320,7 +1329,6 @@ export class Pointer<T = any> extends Ref<T> {
// intentionally not loaded
else if (only_load_local) {
this.loading_pointers.delete(id_string);
pointer.delete();
throw new PointerError("Pointer $"+id_string+" was not found locally", SCOPE);
}

Expand Down
21 changes: 16 additions & 5 deletions runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { Function as DatexFunction } from "../types/function.ts";
import { Storage } from "../runtime/storage.ts";
import { Observers } from "../utils/observers.ts";
import { BinaryCode } from "../compiler/binary_codes.ts";
import type { compile_info, datex_meta, datex_scope, dxb_header, routing_info } from "../utils/global_types.ts";
import type { ExecConditions, compile_info, datex_meta, datex_scope, dxb_header, routing_info } from "../utils/global_types.ts";
import { Markdown } from "../types/markdown.ts";
import { Type } from "../types/type.ts";
import { Tuple } from "../types/tuple.ts";
Expand Down Expand Up @@ -73,6 +73,7 @@ import type { Blockchain } from "../network/blockchain_adapter.ts";
import { AutoMap } from "../utils/auto_map.ts";
import { Supranet } from "../network/supranet.ts";
import { sendDatexViaHTTPChannel } from "../network/datex-http-channel.ts";
import { setCookie } from "../utils/cookies.ts";

const mime = client_type === "deno" ? (await import("https://deno.land/x/mimetypes@v1.0.0/mod.ts")).mime : null;

Expand Down Expand Up @@ -744,10 +745,11 @@ export class Runtime {
* @param dxb_base64 DATEX Data Binary (without header) as Base64 string
* @returns evaluated DATEX result
*/
public static decodeValueBase64<T=unknown>(dxb_base64:string, outer_serialized=false):Promise<T> {
public static decodeValueBase64<T=unknown>(dxb_base64:string, outer_serialized=false, conditions?:ExecConditions):Promise<T> {
// create scope
const scope = Runtime.createNewInitialScope();
scope.outer_serialized = outer_serialized;
scope.exec_conditions = conditions;
// set dxb as scope buffer
Runtime.updateScope(scope, base64ToArrayBuffer(dxb_base64), {end_of_scope:true, sender:Runtime.endpoint})
// execute scope
Expand All @@ -759,10 +761,11 @@ export class Runtime {
* @param dxb DATEX Data Binary (without header)
* @returns evaluated DATEX result
*/
public static decodeValue(dxb:ArrayBuffer, outer_serialized=false):Promise<any> {
public static decodeValue(dxb:ArrayBuffer, outer_serialized=false, conditions?:ExecConditions):Promise<any> {
// create scope
const scope = Runtime.createNewInitialScope();
scope.outer_serialized = outer_serialized;
scope.exec_conditions = conditions;
// set dxb as scope buffer
Runtime.updateScope(scope, dxb, {end_of_scope:true, sender:Runtime.endpoint})
// execute scope
Expand Down Expand Up @@ -1077,7 +1080,7 @@ export class Runtime {
* Handles beforeunload (sending GOODBYE)
* @param endpoint
*/
static addActiveEndpoint(endpoint:Endpoint) {
static setActiveEndpoint(endpoint:Endpoint) {
let endpoints:string[] = [];
try {
endpoints = JSON.parse(localStorage['active_endpoints']) as string[]
Expand Down Expand Up @@ -1122,6 +1125,11 @@ export class Runtime {
}

localStorage['active_endpoints'] = JSON.stringify(endpoints)

// update endpoint cookie
const endpointName = endpoint.toString();
// TODO: store signed endpoint validation cookie
if (client_type == "browser") setCookie("datex-endpoint", endpointName);
}

static getActiveLocalStorageEndpoints() {
Expand All @@ -1144,7 +1152,7 @@ export class Runtime {
public static init(endpoint?:Endpoint) {

// save all currently active endpoints for shared local storage (multiple tabs)
if (endpoint && endpoint != LOCAL_ENDPOINT && client_type == "browser") this.addActiveEndpoint(endpoint)
if (endpoint && endpoint != LOCAL_ENDPOINT && client_type == "browser") this.setActiveEndpoint(endpoint)


if (endpoint) Runtime.endpoint = endpoint;
Expand Down Expand Up @@ -1941,6 +1949,9 @@ export class Runtime {
else if (header.type == ProtocolDataType.UPDATE) {
// ignore
}
else if (header.type == ProtocolDataType.GOODBYE) {
console.error("Error in GOODBYE message:",e)
}
else {
logger.error("Invalid proctocol data type: " + ProtocolDataTypesMap[header.type]??header.type)
}
Expand Down
9 changes: 5 additions & 4 deletions runtime/storage-locations/deno-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AsyncStorageLocation } from "../storage.ts";
import { ptr_cache_path } from "../cache_path.ts";
import { client_type } from "../../utils/constants.ts";
import { normalizePath } from "../../utils/normalize-path.ts";
import { ExecConditions } from "../../utils/global_types.ts";

const denoKvDir = new URL("./deno-kv/", ptr_cache_path);
// @ts-ignore global Deno
Expand Down Expand Up @@ -37,10 +38,10 @@ export class DenoKVStorageLocation extends AsyncStorageLocation {
await this.set(itemDB!, key, Compiler.encodeValue(value));
return true;
}
async getItem(key: string): Promise<unknown> {
async getItem(key: string, conditions?: ExecConditions): Promise<unknown> {
const result = await this.get(itemDB!, key);
if (result == null) return NOT_EXISTING;
else return Runtime.decodeValue(result);
else return Runtime.decodeValue(result, false, conditions);
}

hasItem(key:string) {
Expand Down Expand Up @@ -81,10 +82,10 @@ export class DenoKVStorageLocation extends AsyncStorageLocation {
await this.set(pointerDB!, pointer.id, Compiler.encodeValue(pointer, inserted_ptrs, true, false, true));
return inserted_ptrs;
}
async getPointerValue(pointerId: string, outer_serialized: boolean): Promise<unknown> {
async getPointerValue(pointerId: string, outer_serialized: boolean, conditions?: ExecConditions): Promise<unknown> {
const result = await this.get(pointerDB!, pointerId);
if (result == null) return NOT_EXISTING;
else return Runtime.decodeValue(result, outer_serialized);
else return Runtime.decodeValue(result, outer_serialized, conditions);
}
async removePointer(pointerId: string): Promise<void> {
await pointerDB!.delete([pointerId]);
Expand Down
12 changes: 7 additions & 5 deletions runtime/storage-locations/indexed-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NOT_EXISTING } from "../constants.ts";
import { AsyncStorageLocation, site_suffix } from "../storage.ts";

import localforage from "../../lib/localforage/localforage.js";
import { ExecConditions } from "../../utils/global_types.ts";

// db based storage for DATEX value caching (IndexDB in the browser)
const datex_item_storage = <globalThis.Storage><unknown> localforage.createInstance({name: "dxitem::"+site_suffix});
Expand All @@ -14,9 +15,10 @@ const datex_pointer_storage = <globalThis.Storage><unknown> localforage.createIn

export class IndexedDBStorageLocation extends AsyncStorageLocation {


name = "INDEXED_DB"

supportsExecConditions = true

isSupported() {
return !!globalThis.indexedDB;
}
Expand All @@ -25,10 +27,10 @@ export class IndexedDBStorageLocation extends AsyncStorageLocation {
await datex_item_storage.setItem(key, <any>Compiler.encodeValue(value)); // value to buffer (no header)
return true;
}
async getItem(key: string): Promise<unknown> {
async getItem(key: string, conditions: ExecConditions): Promise<unknown> {
const buffer = <ArrayBuffer><any>await datex_item_storage.getItem(key);
if (buffer == null) return NOT_EXISTING;
else return Runtime.decodeValue(buffer);
else return Runtime.decodeValue(buffer, false, conditions);
}

async hasItem(key:string) {
Expand Down Expand Up @@ -68,10 +70,10 @@ export class IndexedDBStorageLocation extends AsyncStorageLocation {
await datex_pointer_storage.setItem(pointer.id, <any>Compiler.encodeValue(pointer, inserted_ptrs, true, false, true));
return inserted_ptrs;
}
async getPointerValue(pointerId: string, outer_serialized: boolean): Promise<unknown> {
async getPointerValue(pointerId: string, outer_serialized: boolean, conditions: ExecConditions): Promise<unknown> {
const buffer = <ArrayBuffer><any>await datex_pointer_storage.getItem(pointerId);
if (buffer == null) return NOT_EXISTING;
return Runtime.decodeValue(buffer, outer_serialized);
return Runtime.decodeValue(buffer, outer_serialized, conditions);
}
async removePointer(pointerId: string): Promise<void> {
await datex_pointer_storage.removeItem(pointerId);
Expand Down
10 changes: 6 additions & 4 deletions runtime/storage-locations/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { NOT_EXISTING } from "../constants.ts";
import { base64ToArrayBuffer } from "../../utils/utils.ts";
import { arrayBufferToBase64 } from "../../datex_all.ts";
import { localStorage } from "./local-storage-compat.ts";
import type { ExecConditions } from "../../utils/global_types.ts";

export class LocalStorageLocation extends SyncStorageLocation {
name = "LOCAL_STORAGE"

supportsExecConditions = true

isSupported() {
return !!localStorage;
Expand All @@ -25,10 +27,10 @@ export class LocalStorageLocation extends SyncStorageLocation {
return true;
}

getItem(key: string) {
getItem(key: string, conditions?: ExecConditions) {
const base64 = localStorage.getItem(Storage.item_prefix+key);
if (base64==null) return NOT_EXISTING;
else return Runtime.decodeValueBase64(base64);
else return Runtime.decodeValueBase64(base64, false, conditions);
}

*getItemKeys() {
Expand Down Expand Up @@ -68,10 +70,10 @@ export class LocalStorageLocation extends SyncStorageLocation {
localStorage.setItem(Storage.pointer_prefix+pointer.id, Compiler.encodeValueBase64(pointer, inserted_ptrs, true, false, true)); // serialized pointer
return inserted_ptrs;
}
async getPointerValue(pointerId: string, outer_serialized: boolean): Promise<unknown> {
async getPointerValue(pointerId: string, outer_serialized: boolean, conditions?: ExecConditions): Promise<unknown> {
const base64 = localStorage.getItem(Storage.pointer_prefix+pointerId);
if (base64 == null) return NOT_EXISTING;
return await Runtime.decodeValueBase64(base64, outer_serialized);
return await Runtime.decodeValueBase64(base64, outer_serialized, conditions);
}
removePointer(pointerId: string): void {
localStorage.removeItem(Storage.pointer_prefix+pointerId);
Expand Down
Loading

0 comments on commit dd91195

Please sign in to comment.