Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@based/server

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@based/server - npm Package Compare versions

Comparing version 5.0.3 to 6.0.0

dist/functions/genVersion.d.ts

7

dist/authorize.d.ts
import { BasedServer } from './server';
import { BasedRoute, BasedSpec } from './functions';
import { HttpSession, Context, WebSocketSession } from '@based/functions';
import { HttpSession, Context, WebSocketSession, BasedRoute, BasedFunctionConfig } from '@based/functions';
type ClientSession = HttpSession | WebSocketSession;
export type IsAuthorizedHandler<S extends ClientSession = ClientSession, R extends BasedRoute = BasedRoute, P = any> = (route: R, spec: BasedSpec<R>, server: BasedServer, ctx: Context<S>, payload: P, id?: number, checksum?: number) => void;
export type IsAuthorizedHandler<S extends ClientSession = ClientSession, R extends BasedRoute = BasedRoute, P = any> = (route: R, spec: BasedFunctionConfig<R['type']>, server: BasedServer, ctx: Context<S>, payload: P, id?: number, checksum?: number) => void;
export type AuthErrorHandler<S extends ClientSession = ClientSession, R extends BasedRoute = BasedRoute, P = any> = (route: R, server: BasedServer, ctx: Context<S>, payload: P, id?: number, checksum?: number, err?: Error) => true | void;
export declare const defaultAuthError: AuthErrorHandler;
export declare const authorize: <S extends ClientSession = ClientSession, R extends BasedRoute = BasedRoute, P = any>(route: R, server: BasedServer, ctx: Context<S>, payload: P, isAuthorized: IsAuthorizedHandler<S, R, P>, id?: number, checksum?: number, isPublic?: boolean, authError?: AuthErrorHandler<S, R, P>) => void;
export declare const authorize: <S extends ClientSession = ClientSession, R extends BasedRoute<import("@based/functions").BasedFunctionTypes, "type" | "name"> = BasedRoute<import("@based/functions").BasedFunctionTypes, "type" | "name">, P = any>(route: R, server: BasedServer, ctx: Context<S>, payload: P, isAuthorized: IsAuthorizedHandler<S, R, P>, id?: number, checksum?: number, isPublic?: boolean, authError?: AuthErrorHandler<S, R, P>) => void;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.authorize = exports.defaultAuthError = void 0;
const functions_1 = require("./functions");
const sendError_1 = require("./sendError");
const error_1 = require("./error");
const functions_1 = require("@based/functions");
const installFn_1 = require("./installFn");

@@ -12,3 +12,3 @@ const defaultAuthError = (route, server, ctx, payload, id, checksum, err) => {

: error_1.BasedErrorCode.AuthorizeRejectedError;
if (id && (0, functions_1.isChannelFunctionRoute)(route)) {
if (id && (0, functions_1.isBasedRoute)('channel', route)) {
(0, sendError_1.sendError)(server, ctx, code, {

@@ -21,3 +21,3 @@ route,

}
if (id && (0, functions_1.isQueryFunctionRoute)(route)) {
if (id && (0, functions_1.isBasedRoute)('query', route)) {
(0, sendError_1.sendError)(server, ctx, code, {

@@ -24,0 +24,0 @@ route,

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destroyChannel = exports.updateDestroyTimer = void 0;
const functions_1 = require("../functions");
const functions_1 = require("@based/functions");
const cleanup_1 = require("./cleanup");
const updateDestroyTimer = (server, channel) => {
const spec = server.functions.specs[channel.name];
if (spec && !(0, functions_1.isChannelFunctionSpec)(spec)) {
if (spec && !(0, functions_1.isBasedFunctionConfig)('channel', spec)) {
console.error('channel updateDestroyTimer - Not channel spec!', spec, channel.name);

@@ -10,0 +10,0 @@ return;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.startChannel = void 0;
const functions_1 = require("../functions");
const protocol_1 = require("../protocol");
const error_1 = require("../error");
const functions_1 = require("@based/functions");
const updateChannelListener = (server, channel, msg) => {

@@ -33,2 +33,3 @@ if (channel.clients.size) {

name: channel.name,
type: 'channel',
},

@@ -62,3 +63,3 @@ })

}
if (!spec || !(0, functions_1.isChannelFunctionSpec)(spec)) {
if (!spec || !(0, functions_1.isBasedFunctionConfig)('channel', spec)) {
console.warn('Start channel - cannot find channel function spec', channel.name);

@@ -68,3 +69,3 @@ return;

if (spec.relay) {
const client = server.clients[spec.relay];
const client = server.clients[spec.relay.client];
if (!client) {

@@ -85,3 +86,3 @@ errorChannelListener(server, channel, new Error(`Relay client ${spec.relay} does not exist`));

try {
channel.closeFunction = spec.function(server.client, payload, id, (msg) => updateChannelListener(server, channel, msg), (err) => errorChannelListener(server, channel, err));
channel.closeFunction = spec.subscriber(server.client, payload, id, (msg) => updateChannelListener(server, channel, msg), (err) => errorChannelListener(server, channel, err));
}

@@ -88,0 +89,0 @@ catch (err) {

@@ -13,4 +13,5 @@ import { BasedServer } from '../server';

path: string;
type: "function" | "channel" | "query" | "stream";
};
};
export declare function createError<T extends BasedErrorCode>(server: BasedServer, context: Context, code: T, payload: ErrorPayload[T]): BasedErrorData<T>;

@@ -18,3 +18,3 @@ "use strict";

exports.createError = exports.createErrorData = void 0;
const functions_1 = require("../functions");
const functions_1 = require("@based/functions");
const types_1 = require("./types");

@@ -27,3 +27,3 @@ const errorTypeHandlers_1 = require("./errorTypeHandlers");

? types_1.EMPTY_ROUTE
: (0, functions_1.isRoute)(payload)
: (0, functions_1.isAnyBasedRoute)(payload)
? payload

@@ -41,2 +41,3 @@ : 'route' in payload

path: route.path,
type: route.type,
},

@@ -43,0 +44,0 @@ };

@@ -1,6 +0,3 @@

import { BasedRoute } from '../functions';
export declare const EMPTY_ROUTE: {
name: string;
path: string;
};
import { BasedRoute } from '@based/functions';
export declare const EMPTY_ROUTE: BasedRoute;
export declare enum BasedErrorCode {

@@ -7,0 +4,0 @@ FunctionError = 50001,

@@ -7,2 +7,3 @@ "use strict";

path: '',
type: 'function',
};

@@ -9,0 +10,0 @@ var BasedErrorCode;

@@ -8,3 +8,3 @@ "use strict";

const callFunction = async (server, name, ctx, payload) => {
const route = (0, verifyRoute_1.verifyRoute)(server, server.client.ctx, 'fn', server.functions.route(name), name);
const route = (0, verifyRoute_1.verifyRoute)(server, server.client.ctx, 'function', server.functions.route(name), name);
if (route === null) {

@@ -20,3 +20,3 @@ return;

try {
return fn.function(server.client, payload, ctx);
return fn.fn(server.client, payload, ctx);
}

@@ -23,0 +23,0 @@ catch (err) {

@@ -5,3 +5,3 @@ "use strict";

const functions_1 = require("@based/functions");
const observable_1 = require("../../observable");
const query_1 = require("../../query");
const publish_1 = require("../publish");

@@ -14,3 +14,3 @@ const channelSubscribe_1 = require("../channelSubscribe");

this.payload = payload;
this.id = (0, observable_1.genObservableId)(name, payload);
this.id = (0, query_1.genObservableId)(name, payload);
this.name = name;

@@ -17,0 +17,0 @@ }

@@ -1,2 +0,2 @@

import { ObservableUpdateFunction, ObserveErrorListener } from '../../observable';
import { ObservableUpdateFunction, ObserveErrorListener } from '../../query';
import { BasedQuery as BasedQueryAbstract, Context, InternalSessionClient } from '@based/functions';

@@ -3,0 +3,0 @@ export declare class BasedQuery extends BasedQueryAbstract {

@@ -5,7 +5,7 @@ "use strict";

const error_1 = require("../error");
const observable_1 = require("../observable");
const query_1 = require("../query");
const verifyRoute_1 = require("../verifyRoute");
const installFn_1 = require("../installFn");
const getObsData = (resolve, reject, server, id, ctx, route) => {
const obs = (0, observable_1.getObsAndStopRemove)(server, id);
const obs = (0, query_1.getObsAndStopRemove)(server, id);
if (obs.error) {

@@ -23,4 +23,4 @@ reject((0, error_1.createError)(server, ctx, error_1.BasedErrorCode.FunctionError, {

}
(0, observable_1.subscribeNext)(obs, (err) => {
(0, observable_1.destroyObs)(server, id);
(0, query_1.subscribeNext)(obs, (err) => {
(0, query_1.destroyObs)(server, id);
if (err) {

@@ -48,4 +48,4 @@ reject(err);

}
const id = (0, observable_1.genObservableId)(name, payload);
if (!(0, observable_1.hasObs)(server, id)) {
const id = (0, query_1.genObservableId)(name, payload);
if (!(0, query_1.hasObs)(server, id)) {
(0, installFn_1.installFn)(server, server.client.ctx, route).then((spec) => {

@@ -58,6 +58,6 @@ if (!spec) {

}
if (!(0, observable_1.hasObs)(server, id)) {
(0, observable_1.createObs)(server, name, id, payload, true);
if (!(0, query_1.hasObs)(server, id)) {
(0, query_1.createObs)(server, name, id, payload, true);
getObsData(resolve, reject, server, id, ctx, route);
(0, observable_1.start)(server, id);
(0, query_1.start)(server, id);
}

@@ -64,0 +64,0 @@ else {

import { BasedServer } from '../server';
import { Context } from '@based/functions';
import { ObservableUpdateFunction, ObserveErrorListener } from '../observable';
import { ObservableUpdateFunction, ObserveErrorListener } from '../query';
export declare const observe: (server: BasedServer, name: string, ctx: Context, payload: any, update: ObservableUpdateFunction, error: ObserveErrorListener) => (() => void);

@@ -6,6 +6,6 @@ "use strict";

const verifyRoute_1 = require("../verifyRoute");
const observable_1 = require("../observable");
const query_1 = require("../query");
const installFn_1 = require("../installFn");
const observe = (server, name, ctx, payload, update, error) => {
const id = (0, observable_1.genObservableId)(name, payload);
const id = (0, query_1.genObservableId)(name, payload);
const route = (0, verifyRoute_1.verifyRoute)(server, server.client.ctx, 'query', server.functions.route(name), name, id);

@@ -21,6 +21,6 @@ if (route === null) {

isClosed = true;
(0, observable_1.unsubscribeFunction)(server, id, update);
(0, query_1.unsubscribeFunction)(server, id, update);
};
if ((0, observable_1.hasObs)(server, id)) {
(0, observable_1.subscribeFunction)(server, id, update);
if ((0, query_1.hasObs)(server, id)) {
(0, query_1.subscribeFunction)(server, id, update);
return close;

@@ -38,6 +38,6 @@ }

}
if (!(0, observable_1.hasObs)(server, id)) {
(0, observable_1.createObs)(server, name, id, payload);
if (!(0, query_1.hasObs)(server, id)) {
(0, query_1.createObs)(server, name, id, payload);
}
(0, observable_1.subscribeFunction)(server, id, update);
(0, query_1.subscribeFunction)(server, id, update);
});

@@ -44,0 +44,0 @@ return close;

@@ -17,3 +17,3 @@ "use strict";

try {
return fn.publish(server.client, payload, msg, id, ctx);
return fn.publisher(server.client, payload, msg, id, ctx);
}

@@ -20,0 +20,0 @@ catch (err) {

@@ -58,3 +58,3 @@ "use strict";

try {
return fn.function(server.client, file, ctx);
return fn.fn(server.client, file, ctx);
}

@@ -61,0 +61,0 @@ catch (err) {

/// <reference types="node" />
import type { BasedServer } from '../server';
import { BasedRoute, BasedSpec, FunctionConfig, BasedSpecs, BasedRoutes } from './types';
import { Optional } from 'utility-types';
import { BasedRoute, BasedFunctionConfig, BasedFunctionConfigs, BasedRoutes, BasedFunctionConfigComplete, BasedRouteComplete } from '@based/functions';
import { FunctionConfig } from './types';
export * from './types';

@@ -23,12 +25,6 @@ export declare class BasedFunctions {

specs: {
[name: string]: BasedSpec & {
maxPayloadSize: number;
rateLimitTokens: number;
};
[name: string]: BasedFunctionConfigComplete;
};
routes: {
[name: string]: BasedRoute & {
maxPayloadSize: number;
rateLimitTokens: number;
};
[name: string]: BasedRouteComplete;
};

@@ -40,35 +36,20 @@ beingUninstalled: {

updateConfig(fullConfig: FunctionConfig): void;
route(name?: string, path?: string): BasedRoute | null;
install(name: string): Promise<BasedSpec | null>;
addSpecs(specs: BasedSpecs): void;
route(name?: string, path?: string): BasedRouteComplete | null;
install(name: string): Promise<BasedFunctionConfigComplete | null>;
add(specs: BasedFunctionConfigs): void;
addRoutes(routes: BasedRoutes): void;
completeSpec(spec: Partial<BasedSpec & {
maxPayloadSize?: number;
rateLimitTokens?: number;
}>, name?: string): null | (BasedSpec & {
completeSpec(spec: Optional<BasedFunctionConfig, 'name'>, name?: string): null | BasedFunctionConfigComplete;
completeRoute(route: Optional<BasedRoute, 'name'>, name?: string): null | BasedRouteComplete;
updateRoute(route: BasedRouteComplete, name?: string): null | (BasedRoute & {
maxPayloadSize: number;
rateLimitTokens: number;
});
completeRoute(route: Partial<BasedRoute & {
maxPayloadSize?: number;
rateLimitTokens?: number;
}>, name?: string): null | (BasedRoute & {
maxPayloadSize: number;
rateLimitTokens: number;
});
updateRoute(route: BasedRoute & {
maxPayloadSize?: number;
rateLimitTokens?: number;
}, name?: string): null | (BasedRoute & {
maxPayloadSize: number;
rateLimitTokens: number;
});
updateInternal(spec: BasedSpec): boolean;
updateInternal(spec: BasedFunctionConfigComplete): boolean;
uninstallLoop(): void;
update(name: string, checksum: number): Promise<void>;
uninstall(name: string, spec?: BasedSpec | false): Promise<boolean>;
uninstall(name: string, spec?: BasedFunctionConfig | BasedFunctionConfigComplete | null): Promise<boolean>;
private installGaurdedFromConfig;
getNameFromPath(path: string): string;
getFromStore(name: string): BasedSpec | null;
getFromStore(name: string): BasedFunctionConfigComplete | null;
remove(name: string): boolean;
}

@@ -18,7 +18,8 @@ "use strict";

exports.BasedFunctions = void 0;
const types_1 = require("./types");
const functions_1 = require("@based/functions");
const utils_1 = require("@saulx/utils");
const timeout_1 = require("./timeout");
const observable_1 = require("../observable");
const query_1 = require("../query");
const channel_1 = require("../channel");
const genVersion_1 = require("./genVersion");
__exportStar(require("./types"), exports);

@@ -45,3 +46,3 @@ class BasedFunctions {

updateConfig(fullConfig) {
const { routes, specs, ...config } = fullConfig;
const { routes, configs, ...config } = fullConfig;
if (this.config) {

@@ -92,4 +93,4 @@ (0, utils_1.deepMerge)(this.config, config);

}
if (specs) {
this.addSpecs(specs);
if (configs) {
this.add(configs);
}

@@ -116,3 +117,3 @@ if (this.unregisterTimeout) {

}
addSpecs(specs) {
add(specs) {
for (const key in specs) {

@@ -142,7 +143,6 @@ const s = this.completeSpec(specs[key], key);

}
// if (!spec.function) {
// if channel
// channel publish
// }
// spec.function
if (!spec.version) {
// @ts-ignore added name allready
spec.version = (0, genVersion_1.genVersion)(spec);
}
const nSpec = spec;

@@ -153,5 +153,9 @@ return nSpec;

const nRoute = route;
if (!nRoute.type) {
console.error('Type is required for based-routes', name);
return null;
}
if (!nRoute.name) {
if (!name) {
console.error('No route name!', route);
console.error('No based-route name', route);
return null;

@@ -162,14 +166,3 @@ }

if (!route.maxPayloadSize) {
if ((0, types_1.isChannelFunctionRoute)(nRoute)) {
route.maxPayloadSize = this.maxPayLoadSizeDefaults.channel;
}
else if ((0, types_1.isQueryFunctionRoute)(nRoute)) {
route.maxPayloadSize = this.maxPayLoadSizeDefaults.query;
}
else if ((0, types_1.isStreamFunctionRoute)(nRoute)) {
route.maxPayloadSize = this.maxPayLoadSizeDefaults.stream;
}
else {
route.maxPayloadSize = this.maxPayLoadSizeDefaults.function;
}
route.maxPayloadSize = this.maxPayLoadSizeDefaults[route.type];
}

@@ -214,7 +207,7 @@ if (nRoute.rateLimitTokens === undefined) {

}
const previousChecksum = this.specs[spec.name]?.checksum ?? -1;
const previousChecksum = this.specs[spec.name]?.version ?? -1;
// @ts-ignore maxpayload and rlimit tokens added....
this.specs[spec.name] = spec;
if (this.specs[spec.name] && this.server.activeChannels[spec.name]) {
if (!(0, types_1.isChannelFunctionSpec)(spec)) {
if (!(0, functions_1.isBasedFunctionConfig)('channel', spec)) {
for (const [id] of this.server.activeChannels[spec.name]) {

@@ -225,3 +218,3 @@ (0, channel_1.destroyChannel)(this.server, id);

else {
if (previousChecksum !== spec.checksum) {
if (previousChecksum !== spec.version) {
for (const [id] of this.server.activeChannels[spec.name]) {

@@ -235,11 +228,11 @@ (0, channel_1.startChannel)(this.server, id, true);

this.server.activeObservables[spec.name]) {
if (!(0, types_1.isQueryFunctionSpec)(spec)) {
if (!(0, functions_1.isBasedFunctionConfig)('query', spec)) {
for (const [id] of this.server.activeObservables[spec.name]) {
(0, observable_1.destroyObs)(this.server, id);
(0, query_1.destroyObs)(this.server, id);
}
}
else {
if (previousChecksum !== spec.checksum) {
if (previousChecksum !== spec.version) {
for (const [id] of this.server.activeObservables[spec.name]) {
(0, observable_1.start)(this.server, id);
(0, query_1.start)(this.server, id);
}

@@ -256,6 +249,7 @@ }

const spec = this.specs[name];
if ((0, types_1.isQueryFunctionSpec)(spec) && this.server.activeObservables[name]) {
if ((0, functions_1.isBasedFunctionConfig)('query', spec) &&
this.server.activeObservables[name]) {
(0, timeout_1.updateTimeoutCounter)(spec);
}
else if ((0, types_1.isChannelFunctionSpec)(spec) &&
else if ((0, functions_1.isBasedFunctionConfig)('channel', spec) &&
this.server.activeChannels[name]) {

@@ -274,3 +268,3 @@ (0, timeout_1.updateTimeoutCounter)(spec);

const prevSpec = this.specs[name];
if (prevSpec && prevSpec.checksum !== checksum) {
if (prevSpec && prevSpec.version !== checksum) {
if (this.beingUninstalled[name]) {

@@ -282,3 +276,3 @@ delete this.beingUninstalled[name];

server: this.server,
function: prevSpec,
config: prevSpec,
name,

@@ -295,3 +289,3 @@ });

}
if (!spec && spec !== false) {
if (!spec && spec !== null) {
spec = this.specs[name];

@@ -308,3 +302,3 @@ }

server: this.server,
function: spec,
config: spec,
name,

@@ -353,7 +347,7 @@ })) {

}
if ((0, types_1.isQueryFunctionRoute)(spec)) {
if ((0, functions_1.isBasedFunctionConfig)('query', spec)) {
const activeObs = this.server.activeObservables[name];
if (activeObs) {
for (const [id] of activeObs) {
(0, observable_1.destroyObs)(this.server, id);
(0, query_1.destroyObs)(this.server, id);
}

@@ -363,3 +357,3 @@ delete this.server.activeObservables[name];

}
if ((0, types_1.isChannelFunctionSpec)(spec)) {
if ((0, functions_1.isBasedFunctionConfig)('channel', spec)) {
const activeChannel = this.server.activeChannels[name];

@@ -366,0 +360,0 @@ if (activeChannel) {

@@ -1,3 +0,3 @@

import type { BasedSpec } from './types';
export declare const fnIsTimedOut: (spec: BasedSpec) => boolean;
export declare const updateTimeoutCounter: (spec: BasedSpec) => void;
import type { BasedFunctionConfigComplete } from '@based/functions';
export declare const fnIsTimedOut: (spec: BasedFunctionConfigComplete) => boolean;
export declare const updateTimeoutCounter: (spec: BasedFunctionConfigComplete) => void;

@@ -1,2 +0,2 @@

import { BasedQueryFunction, BasedFunction, BasedChannelFunction, HttpResponse, UninstallFunction, BasedStreamFunction, BasedChannelPublishFunction, Authorize } from '@based/functions';
import { BasedFunctionConfig, BasedRoutes, BasedRouteComplete, BasedFunctionConfigs, BasedFunctionConfigComplete } from '@based/functions';
import { BasedServer } from '../server';

@@ -22,143 +22,15 @@ export type FunctionConfig = {

path?: string;
}) => null | (BasedFunctionRoute & {
maxPayloadSize: number;
rateLimitTokens: number;
});
}) => null | BasedRouteComplete;
install?: (opts: {
server: BasedServer;
name: string;
function?: BasedSpec;
}) => Promise<null | BasedSpec>;
config?: BasedFunctionConfig | BasedFunctionConfigComplete;
}) => Promise<null | BasedFunctionConfigComplete>;
uninstall?: (opts: {
server: BasedServer;
name: string;
function: BasedSpec;
config: BasedFunctionConfig | BasedFunctionConfigComplete;
}) => Promise<boolean>;
specs?: BasedSpecs;
configs?: BasedFunctionConfigs;
routes?: BasedRoutes;
};
type Route = {
/** Function name */
name: string;
/** In addition to the name, a function can have a custom path for HTTP requests.
* For example: `path: 'my/custom/path'` will result in the function being
* available with a request to `edgeurl.based.io/my/custom/path`
*/
path?: string;
/** In bytes. `-1` indicates no size limit */
maxPayloadSize?: number;
/** Cost in tokens for this function call. */
rateLimitTokens?: number;
/** A function marked as `public` will skip the call to authorize. */
public?: boolean;
/** Array of headers that this function expects to receive. */
headers?: string[];
/** A function marked as `internalOnly` will only be accessible from other server side functions,
* and not through the public internet.
*/
internalOnly?: boolean;
/** Can hold extra information about a spec */
data?: any;
};
export type BasedFunctionRoute = Route;
export type BasedQueryFunctionRoute = Route & {
query: true;
};
export type BasedStreamFunctionRoute = Route & {
stream: true;
};
export type BasedChannelFunctionRoute = Route & {
channel: true;
publisher?: {
public?: true;
};
};
export type BasedInstallableFunctionSpec = {
/** Version hash of the BasedFunction */
checksum: number;
/** Uinstall function after it has been idle, in ms */
uninstallAfterIdleTime?: number;
/** In ms */
timeoutCounter?: number;
/** Hook that fires on uninstall of the function e.g. to clean up database connections */
uninstall?: UninstallFunction;
/** Specific authorize for this function */
authorize?: Authorize;
};
export type BasedQueryFunctionSpec = ({
relay?: false;
function: BasedQueryFunction;
/** How long should the query function remain active after all subscribers are gone, in ms */
closeAfterIdleTime?: number;
/** When in an HTTP context, this function is called to wrap the return value of the BasedFunction, and inject headers and a status code */
httpResponse?: HttpResponse;
} | {
relay: string;
function?: BasedQueryFunction;
closeAfterIdleTime?: number;
httpResponse?: HttpResponse;
}) & BasedQueryFunctionRoute & BasedInstallableFunctionSpec;
export type BasedStreamFunctionSpec = {
function: BasedStreamFunction;
/** Maximum allowed execution time, in ms */
maxExecTime?: number;
} & BasedStreamFunctionRoute & BasedInstallableFunctionSpec;
export type BasedFunctionSpec = ({
relay: string;
function?: BasedFunction;
/** When in an HTTP context, this function is called to wrap the return value of the BasedFunction, and inject headers and a status code */
httpResponse?: HttpResponse;
/** Maximum allowed execution time, in ms */
maxExecTime?: number;
} | {
relay?: false;
function: BasedFunction;
/** When in an HTTP context, this function is called to wrap the return value of the BasedFunction, and inject headers and a status code */
httpResponse?: HttpResponse;
/** Maximum allowed execution time, in ms */
maxExecTime?: number;
}) & BasedFunctionRoute & BasedInstallableFunctionSpec;
export type BasedChannelFunctionSpec = ({
relay?: false;
function: BasedChannelFunction;
/** Publish allows publishing to channels */
publish: BasedChannelPublishFunction;
/** How long should the channel remain active after all subscribers ae gone, in ms */
closeAfterIdleTime?: number;
/** When in an HTTP context, this function is called to wrap the return value of the publish endpoint, and inject headers and a status code */
httpResponse?: HttpResponse;
} | {
relay: string;
function?: BasedChannelFunction;
/** Publish allows publishing to channels */
publish?: BasedChannelPublishFunction;
/** How long should the channel remain active after all subscribers ae gone, in ms */
closeAfterIdleTime?: number;
/** When in an HTTP context, this function is called to wrap the return value of the publish endpoint, and inject headers and a status code */
httpResponse?: HttpResponse;
}) & BasedChannelFunctionRoute & BasedInstallableFunctionSpec;
export type BasedRoute = BasedFunctionRoute | BasedQueryFunctionRoute | BasedStreamFunctionRoute | BasedChannelFunctionRoute;
export type BasedSpec<R extends BasedRoute = BasedRoute> = R extends BasedChannelFunctionRoute ? BasedChannelFunctionSpec : R extends BasedQueryFunctionRoute ? BasedQueryFunctionSpec : R extends BasedStreamFunctionRoute ? BasedStreamFunctionSpec : BasedFunctionSpec;
export declare function isChannelFunctionSpec(fn: BasedSpec): fn is BasedChannelFunctionSpec;
export declare function isQueryFunctionSpec(fn: BasedSpec): fn is BasedQueryFunctionSpec;
export declare function isStreamFunctionSpec(fn: BasedSpec): fn is BasedQueryFunctionSpec;
export declare function isFunctionSpec(fn: BasedSpec): fn is BasedSpec;
export declare function isSpec(fn: any): fn is BasedSpec;
export declare function isQueryFunctionRoute(fn: BasedRoute): fn is BasedQueryFunctionRoute;
export declare function isStreamFunctionRoute(fn: BasedRoute): fn is BasedStreamFunctionRoute;
export declare function isChannelFunctionRoute(fn: BasedRoute): fn is BasedChannelFunctionRoute;
export declare function isFunctionRoute(fn: BasedRoute): fn is BasedFunctionRoute;
export declare function isRoute(route: any): route is BasedRoute;
export type BasedRoutes = {
[name: string]: Partial<BasedRoute & {
maxPayloadSize?: number;
rateLimitTokens?: number;
}>;
};
export type BasedSpecs = {
[name: string]: Partial<BasedSpec & {
maxPayloadSize?: number;
rateLimitTokens?: number;
}>;
};
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRoute = exports.isFunctionRoute = exports.isChannelFunctionRoute = exports.isStreamFunctionRoute = exports.isQueryFunctionRoute = exports.isSpec = exports.isFunctionSpec = exports.isStreamFunctionSpec = exports.isQueryFunctionSpec = exports.isChannelFunctionSpec = void 0;
// ---------- specs -------------
function isChannelFunctionSpec(fn) {
return fn.channel === true;
}
exports.isChannelFunctionSpec = isChannelFunctionSpec;
function isQueryFunctionSpec(fn) {
return fn.query === true;
}
exports.isQueryFunctionSpec = isQueryFunctionSpec;
function isStreamFunctionSpec(fn) {
return fn.stream === true;
}
exports.isStreamFunctionSpec = isStreamFunctionSpec;
function isFunctionSpec(fn) {
return !('stream' in fn) && !('query' in fn);
}
exports.isFunctionSpec = isFunctionSpec;
function isSpec(fn) {
return (fn &&
typeof fn === 'object' &&
(isFunctionSpec(fn) ||
isStreamFunctionSpec(fn) ||
isQueryFunctionSpec(fn) ||
isChannelFunctionSpec(fn)));
}
exports.isSpec = isSpec;
// ---------- routes -------------
function isQueryFunctionRoute(fn) {
return fn.query === true;
}
exports.isQueryFunctionRoute = isQueryFunctionRoute;
function isStreamFunctionRoute(fn) {
return fn.stream === true;
}
exports.isStreamFunctionRoute = isStreamFunctionRoute;
function isChannelFunctionRoute(fn) {
return fn.channel === true;
}
exports.isChannelFunctionRoute = isChannelFunctionRoute;
function isFunctionRoute(fn) {
return !('stream' in fn) && !('query' in fn) && !('channel' in fn);
}
exports.isFunctionRoute = isFunctionRoute;
function isRoute(route) {
if (route && typeof route === 'object' && 'name' in route) {
return true;
}
return false;
}
exports.isRoute = isRoute;
//# sourceMappingURL=types.js.map

@@ -9,3 +9,3 @@ "use strict";

spec
.function(server.client, payload, ctx)
.fn(server.client, payload, ctx)
.then(async (result) => {

@@ -12,0 +12,0 @@ if (!ctx.session) {

@@ -7,5 +7,5 @@ "use strict";

exports.httpHandler = void 0;
const functions_1 = require("@based/functions");
const function_1 = require("./function");
const streamFunction_1 = require("./streamFunction");
const functions_1 = require("../../functions");
const query_1 = require("./query");

@@ -58,4 +58,4 @@ const readBody_1 = require("./readBody");

}, error_1.BasedErrorCode.FunctionNotFound, path[1]
? { route: { name: path[1] } }
: { route: { name: '', path: url } });
? { route: { name: path[1], type: 'function' } }
: { route: { name: '', path: url, type: 'function' } });
return;

@@ -129,3 +129,3 @@ }

}
if ((0, functions_1.isQueryFunctionRoute)(route)) {
if ((0, functions_1.isBasedRoute)('query', route)) {
// Handle HEAD

@@ -144,3 +144,3 @@ if (method !== 'post' && method !== 'get') {

}
if ((0, functions_1.isStreamFunctionRoute)(route)) {
if ((0, functions_1.isBasedRoute)('stream', route)) {
if (method === 'options') {

@@ -162,3 +162,3 @@ (0, sendHttpResponse_1.end)(ctx);

}
if ((0, functions_1.isChannelFunctionRoute)(route)) {
if ((0, functions_1.isBasedRoute)('channel', route)) {
if (method !== 'post' && method !== 'get') {

@@ -169,7 +169,7 @@ (0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.MethodNotAllowed, route);

handleRequest(server, method, ctx, route, (payload) => {
(0, authorize_1.authorize)(route, server, ctx, payload, publish_1.httpPublish, undefined, undefined, route.publisher?.public || route.public);
(0, authorize_1.authorize)(route, server, ctx, payload, publish_1.httpPublish, undefined, undefined, route.publicPublisher || route.public);
});
return;
}
if ((0, functions_1.isFunctionRoute)(route)) {
if ((0, functions_1.isBasedRoute)('function', route)) {
if (method !== 'post' && method !== 'get') {

@@ -176,0 +176,0 @@ (0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.MethodNotAllowed, route);

@@ -8,3 +8,3 @@ "use strict";

const installFn_1 = require("../../installFn");
const observable_1 = require("../../observable");
const query_1 = require("../../query");
const channel_1 = require("../../channel");

@@ -47,3 +47,3 @@ const httpPublish = async (route, spec, server, ctx, payload) => {

const name = route.name;
const id = (0, observable_1.genObservableId)(name, channelPayload);
const id = (0, query_1.genObservableId)(name, channelPayload);
(0, installFn_1.installFn)(server, ctx, route).then(async (spec) => {

@@ -61,3 +61,3 @@ if (spec === null) {

try {
spec.publish(server.client, channelPayload, msg, id, ctx);
spec.publisher(server.client, channelPayload, msg, id, ctx);
if (spec.httpResponse) {

@@ -64,0 +64,0 @@ const send = (responseData, headers, status) => {

import { BasedServer } from '../../server';
import { HttpSession, Context } from '@based/functions';
import { BasedQueryFunctionRoute } from '../../functions';
export declare const httpGet: (route: BasedQueryFunctionRoute, payload: any, ctx: Context<HttpSession>, server: BasedServer, checksum: number) => void;
import { HttpSession, Context, BasedRoute } from '@based/functions';
export declare const httpGet: (route: BasedRoute<'query'>, payload: any, ctx: Context<HttpSession>, server: BasedServer, checksum: number) => void;

@@ -9,3 +9,3 @@ "use strict";

const compress_1 = require("../../compress");
const observable_1 = require("../../observable");
const query_1 = require("../../query");
const node_zlib_1 = __importDefault(require("node:zlib"));

@@ -65,3 +65,3 @@ const utils_1 = require("@saulx/utils");

if (!ctx.session) {
(0, observable_1.destroyObs)(server, id);
(0, query_1.destroyObs)(server, id);
return;

@@ -74,3 +74,3 @@ }

observableId: id,
route: { name: obs.name },
route: { name: obs.name, type: 'query' },
});

@@ -93,3 +93,3 @@ }

}
(0, observable_1.destroyObs)(server, id);
(0, query_1.destroyObs)(server, id);
};

@@ -108,5 +108,5 @@ const sendGetResponse = (route, spec, server, id, obs, checksum, ctx) => {

const getFromExisting = (server, id, ctx, route, spec, checksum) => {
const obs = (0, observable_1.getObsAndStopRemove)(server, id);
const obs = (0, query_1.getObsAndStopRemove)(server, id);
if (obs.error) {
(0, observable_1.sendObsGetError)(server, ctx, obs.id, obs.error);
(0, query_1.sendObsGetError)(server, ctx, obs.id, obs.error);
return;

@@ -118,3 +118,3 @@ }

}
(0, observable_1.subscribeNext)(obs, (err) => {
(0, query_1.subscribeNext)(obs, (err) => {
if (!ctx.session) {

@@ -124,3 +124,3 @@ return;

if (err) {
(0, observable_1.sendObsGetError)(server, ctx, obs.id, err);
(0, query_1.sendObsGetError)(server, ctx, obs.id, err);
}

@@ -134,10 +134,10 @@ else {

const name = route.name;
if ((0, observable_1.hasObs)(server, id)) {
if ((0, query_1.hasObs)(server, id)) {
getFromExisting(server, id, ctx, route, spec, checksum);
return;
}
const obs = (0, observable_1.createObs)(server, name, id, payload, true);
(0, observable_1.subscribeNext)(obs, (err) => {
const obs = (0, query_1.createObs)(server, name, id, payload, true);
(0, query_1.subscribeNext)(obs, (err) => {
if (err) {
(0, observable_1.sendObsGetError)(server, ctx, obs.id, err);
(0, query_1.sendObsGetError)(server, ctx, obs.id, err);
}

@@ -148,3 +148,3 @@ else {

});
(0, observable_1.start)(server, id);
(0, query_1.start)(server, id);
};

@@ -161,5 +161,5 @@ const httpGet = (route, payload, ctx, server, checksum) => {

}
(0, authorize_1.authorize)(route, server, ctx, payload, isAuthorized, (0, observable_1.genObservableId)(route.name, payload), checksum);
(0, authorize_1.authorize)(route, server, ctx, payload, isAuthorized, (0, query_1.genObservableId)(route.name, payload), checksum);
};
exports.httpGet = httpGet;
//# sourceMappingURL=query.js.map

@@ -1,5 +0,4 @@

import { HttpSession, Context } from '@based/functions';
import { BasedFunctionRoute, BasedQueryFunctionRoute } from '../../functions';
import { HttpSession, Context, BasedRoute } from '@based/functions';
import { BasedServer } from '../../server';
export declare const parseHttpPayload: (server: BasedServer, ctx: Context<HttpSession>, data: Uint8Array, route: BasedFunctionRoute | BasedQueryFunctionRoute) => any;
export declare const readBody: (server: BasedServer, ctx: Context<HttpSession>, onData: (data: any | void) => void, route: BasedFunctionRoute | BasedQueryFunctionRoute) => void;
export declare const parseHttpPayload: (server: BasedServer, ctx: Context<HttpSession>, data: Uint8Array, route: BasedRoute) => any;
export declare const readBody: (server: BasedServer, ctx: Context<HttpSession>, onData: (data: any | void) => void, route: BasedRoute) => void;
import { BasedServer } from '../../../server';
import { BasedStreamFunctionRoute } from '../../../functions';
import { HttpSession, Context } from '@based/functions';
export declare const httpStreamFunction: (server: BasedServer, ctx: Context<HttpSession>, route: BasedStreamFunctionRoute) => void;
import { HttpSession, Context, BasedRoute } from '@based/functions';
export declare const httpStreamFunction: (server: BasedServer, ctx: Context<HttpSession>, route: BasedRoute<'stream'>) => void;

@@ -1,4 +0,3 @@

import { HttpSession, Context } from '@based/functions';
import { HttpSession, Context, BasedRoute } from '@based/functions';
import { BasedServer } from '../../../../server';
import { BasedStreamFunctionRoute } from '../../../../functions';
export declare const multiPart: (server: BasedServer, ctx: Context<HttpSession>, route: BasedStreamFunctionRoute) => void;
export declare const multiPart: (server: BasedServer, ctx: Context<HttpSession>, route: BasedRoute<'stream'>) => void;

@@ -39,3 +39,3 @@ "use strict";

try {
const value = await spec.function(server.client, file, ctx);
const value = await spec.fn(server.client, file, ctx);
return { value };

@@ -42,0 +42,0 @@ }

@@ -1,4 +0,3 @@

import { HttpSession, Context, StreamPayload } from '@based/functions';
import { HttpSession, Context, StreamPayload, BasedRoute } from '@based/functions';
import { BasedServer } from '../../../../server';
import { BasedFunctionRoute } from '../../../../functions';
export type FileOptions = {

@@ -11,3 +10,3 @@ name?: string;

};
declare const _default: (ctx: Context<HttpSession>, server: BasedServer, route: BasedFunctionRoute, onFile: (payload: StreamPayload) => void, isReady: () => void) => void;
declare const _default: (ctx: Context<HttpSession>, server: BasedServer, route: BasedRoute<'stream'>, onFile: (payload: StreamPayload) => void, isReady: () => void) => void;
export default _default;

@@ -1,5 +0,4 @@

import { HttpSession, Context, BasedDataStream } from '@based/functions';
import { BasedFunctionRoute } from '../../../../functions';
import { HttpSession, Context, BasedDataStream, BasedRoute } from '@based/functions';
import { BasedServer } from '../../../../server';
declare const _default: (server: BasedServer, route: BasedFunctionRoute, ctx: Context<HttpSession>, size: number) => BasedDataStream;
declare const _default: (server: BasedServer, route: BasedRoute<'stream'>, ctx: Context<HttpSession>, size: number) => BasedDataStream;
export default _default;
import { BasedServer } from '../../../../server';
import { BasedStreamFunctionRoute } from '../../../../functions';
import { HttpSession, Context } from '@based/functions';
export declare const singleStream: (server: BasedServer, ctx: Context<HttpSession>, route: BasedStreamFunctionRoute, type: string, size: number) => void;
import { HttpSession, Context, BasedRoute } from '@based/functions';
export declare const singleStream: (server: BasedServer, ctx: Context<HttpSession>, route: BasedRoute<'stream'>, type: string, size: number) => void;

@@ -47,3 +47,3 @@ "use strict";

}
const fn = spec.function;
const fn = spec.fn;
fn(server.client, streamPayload, ctx)

@@ -50,0 +50,0 @@ .catch((err) => {

@@ -9,3 +9,3 @@ "use strict";

const ws_1 = require("./ws");
const observable_1 = require("../observable");
const query_1 = require("../query");
const channel_1 = require("../channel");

@@ -70,3 +70,3 @@ const http_1 = require("./http");

session.obs.forEach((id) => {
if ((0, observable_1.unsubscribeWsIgnoreClient)(server, id, session.c)) {
if ((0, query_1.unsubscribeWsIgnoreClient)(server, id, session.c)) {
// This is here for channels so we do not need to keep a seperate obs set on clients

@@ -73,0 +73,0 @@ (0, channel_1.unsubscribeChannelIgnoreClient)(server, id, session.c);

@@ -5,3 +5,3 @@ "use strict";

const protocol_1 = require("../../protocol");
const observable_1 = require("./observable");
const query_1 = require("./query");
const security_1 = require("../../security");

@@ -44,7 +44,7 @@ const channelSubscribe_1 = require("./channelSubscribe");

name,
query: true,
type: 'query',
};
(0, installFn_1.installFn)(server, ctx, route, id).then((spec) => {
if (spec) {
(0, observable_1.enableSubscribe)(route, spec, server, ctx, payload, id, checksum);
(0, query_1.enableSubscribe)(route, spec, server, ctx, payload, id, checksum);
}

@@ -63,3 +63,3 @@ else {

name,
channel: true,
type: 'channel',
};

@@ -66,0 +66,0 @@ (0, installFn_1.installFn)(server, ctx, route, id).then((spec) => {

@@ -18,7 +18,7 @@ "use strict";

if (spec.relay) {
const client = server.clients[spec.relay];
const client = server.clients[spec.relay.client];
client.channel(channel.name, channel.payload).publish(payload);
}
else {
spec.publish(server.client, channel.payload, payload, channel.id, ctx);
spec.publisher(server.client, channel.payload, payload, channel.id, ctx);
}

@@ -63,3 +63,3 @@ }

: (0, protocol_1.parsePayload)((0, protocol_1.decodePayload)(new Uint8Array(arr.slice(start + 12, start + len)), isDeflate));
(0, authorize_1.authorize)(route, server, ctx, payload, publish, id, undefined, route.publisher?.public);
(0, authorize_1.authorize)(route, server, ctx, payload, publish, id, undefined, route.publicPublisher);
return true;

@@ -66,0 +66,0 @@ };

@@ -1,7 +0,6 @@

import { BasedChannelFunctionRoute } from '../../functions';
import { WebSocketSession } from '@based/functions';
import { WebSocketSession, BasedRoute } from '@based/functions';
import { IsAuthorizedHandler } from '../../authorize';
import { BinaryMessageHandler } from './types';
export declare const enableChannelSubscribe: IsAuthorizedHandler<WebSocketSession, BasedChannelFunctionRoute>;
export declare const enableChannelSubscribe: IsAuthorizedHandler<WebSocketSession, BasedRoute<'channel'>>;
export declare const channelSubscribeMessage: BinaryMessageHandler;
export declare const unsubscribeChannelMessage: BinaryMessageHandler;

@@ -53,3 +53,3 @@ "use strict";

name,
channel: true,
type: 'channel',
};

@@ -56,0 +56,0 @@ if ((0, security_1.rateLimitRequest)(server, ctx,

@@ -15,3 +15,3 @@ "use strict";

if (spec.relay) {
const client = server.clients[spec.relay];
const client = server.clients[spec.relay.client];
if (!client) {

@@ -40,3 +40,3 @@ (0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionError, {

spec
.function(server.client, payload, ctx)
.fn(server.client, payload, ctx)
.then(async (v) => {

@@ -65,3 +65,3 @@ // TODO: allow chunked reply!

}
const route = (0, verifyRoute_1.verifyRoute)(server, ctx, 'fn', server.functions.route(name), name, requestId);
const route = (0, verifyRoute_1.verifyRoute)(server, ctx, 'function', server.functions.route(name), name, requestId);
// TODO: add strictness setting - if strict return false here

@@ -68,0 +68,0 @@ if (route === null) {

@@ -5,3 +5,3 @@ "use strict";

const protocol_1 = require("../../protocol");
const observable_1 = require("../../observable");
const query_1 = require("../../query");
const error_1 = require("../../error");

@@ -14,7 +14,7 @@ const sendError_1 = require("../../sendError");

if (!ctx.session) {
(0, observable_1.destroyObs)(server, id);
(0, query_1.destroyObs)(server, id);
return;
}
if (checksum === 0) {
(0, observable_1.sendObsWs)(ctx, obs.cache, obs);
(0, query_1.sendObsWs)(ctx, obs.cache, obs);
}

@@ -25,13 +25,13 @@ else if (checksum === obs.checksum) {

else if (obs.diffCache && obs.previousChecksum === checksum) {
(0, observable_1.sendObsWs)(ctx, obs.diffCache, obs);
(0, query_1.sendObsWs)(ctx, obs.diffCache, obs);
}
else {
(0, observable_1.sendObsWs)(ctx, obs.cache, obs);
(0, query_1.sendObsWs)(ctx, obs.cache, obs);
}
(0, observable_1.destroyObs)(server, id);
(0, query_1.destroyObs)(server, id);
};
const getFromExisting = (server, id, ctx, checksum) => {
const obs = (0, observable_1.getObsAndStopRemove)(server, id);
const obs = (0, query_1.getObsAndStopRemove)(server, id);
if (obs.error) {
(0, observable_1.sendObsGetError)(server, ctx, id, obs.error);
(0, query_1.sendObsGetError)(server, ctx, id, obs.error);
return;

@@ -43,5 +43,5 @@ }

}
(0, observable_1.subscribeNext)(obs, (err) => {
(0, query_1.subscribeNext)(obs, (err) => {
if (err) {
(0, observable_1.sendObsGetError)(server, ctx, id, err);
(0, query_1.sendObsGetError)(server, ctx, id, err);
}

@@ -54,3 +54,3 @@ else {

const isAuthorized = (route, spec, server, ctx, payload, id, checksum) => {
if ((0, observable_1.hasObs)(server, id)) {
if ((0, query_1.hasObs)(server, id)) {
getFromExisting(server, id, ctx, checksum);

@@ -63,11 +63,11 @@ return;

}
if ((0, observable_1.hasObs)(server, id)) {
if ((0, query_1.hasObs)(server, id)) {
getFromExisting(server, id, ctx, checksum);
return;
}
const obs = (0, observable_1.createObs)(server, route.name, id, payload, true);
const obs = (0, query_1.createObs)(server, route.name, id, payload, true);
if (!session.obs.has(id)) {
(0, observable_1.subscribeNext)(obs, (err) => {
(0, query_1.subscribeNext)(obs, (err) => {
if (err) {
(0, observable_1.sendObsGetError)(server, ctx, id, err);
(0, query_1.sendObsGetError)(server, ctx, id, err);
}

@@ -79,3 +79,3 @@ else {

}
(0, observable_1.start)(server, id);
(0, query_1.start)(server, id);
};

@@ -82,0 +82,0 @@ const isNotAuthorized = (route, server, ctx, payload, id, checksum) => {

@@ -6,3 +6,3 @@ "use strict";

const function_1 = require("./function");
const observable_1 = require("./observable");
const query_1 = require("./query");
const auth_1 = require("./auth");

@@ -21,3 +21,3 @@ const get_1 = require("./get");

// type 1 = subscribe
if (type === 1 && (0, observable_1.subscribeMessage)(arr, start, len, isDeflate, ctx, server)) {
if (type === 1 && (0, query_1.subscribeMessage)(arr, start, len, isDeflate, ctx, server)) {
return next;

@@ -27,3 +27,3 @@ }

if (type === 2 &&
(0, observable_1.unsubscribeMessage)(arr, start, len, isDeflate, ctx, server)) {
(0, query_1.unsubscribeMessage)(arr, start, len, isDeflate, ctx, server)) {
return next;

@@ -30,0 +30,0 @@ }

@@ -8,3 +8,3 @@ export { compress } from './compress';

export * from './functionApi';
export * from './observable/types';
export * from './query/types';
export * from './protocol';

@@ -28,4 +28,4 @@ "use strict";

__exportStar(require("./functionApi"), exports);
__exportStar(require("./observable/types"), exports);
__exportStar(require("./query/types"), exports);
__exportStar(require("./protocol"), exports);
//# sourceMappingURL=index.js.map

@@ -1,4 +0,3 @@

import { BasedRoute, BasedSpec } from './functions';
import { BasedServer } from './server';
import { Context } from '@based/functions';
export declare const installFn: <R extends BasedRoute>(server: BasedServer, ctx: Context, route: R, id?: number) => Promise<BasedSpec<R>>;
import { Context, BasedRoute, BasedFunctionConfig, BasedFunctionTypes } from '@based/functions';
export declare const installFn: <R extends BasedRoute<BasedFunctionTypes, "type" | "name">>(server: BasedServer, ctx: Context, route: R, id?: number) => Promise<BasedFunctionConfig<R["type"]>>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installFn = void 0;
const functions_1 = require("./functions");
const functions_2 = require("@based/functions");
const functions_1 = require("@based/functions");
const sendError_1 = require("./sendError");
const error_1 = require("./error");
const functionNotFound = (server, ctx, route, isQuery, isChannel, id) => {
if (!(0, functions_2.isClientContext)(ctx)) {
const functionNotFound = (server, ctx, route, type, id) => {
if (!(0, functions_1.isClientContext)(ctx)) {
return;
}
if (isQuery) {
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, {
route,
observableId: id,
});
return;
}
if (isChannel) {
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, {
route,
channelId: id,
});
return;
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, {
route,
requestId: id,
});
(0, sendError_1.sendSimpleError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, { type: route.type, name: route.name }, id);
};
const installFn = async (server, ctx, route, id) => {
const isQuery = (0, functions_1.isQueryFunctionRoute)(route);
const isStream = !isQuery && (0, functions_1.isStreamFunctionRoute)(route);
const isChannel = !isStream && !isQuery && (0, functions_1.isChannelFunctionRoute)(route);
if (!route) {
return null;
}
const { type, name } = route;
try {
const spec = await server.functions.install(route.name);
const spec = await server.functions.install(name);
if (!ctx.session) {

@@ -41,44 +24,12 @@ return null;

if (spec === null) {
functionNotFound(server, ctx, route, isQuery, isChannel, id);
functionNotFound(server, ctx, route, type, id);
return null;
}
if (isChannel && !(0, functions_1.isChannelFunctionSpec)(spec)) {
if (!(0, functions_2.isClientContext)(ctx)) {
if (!(0, functions_1.isBasedFunctionConfig)(type, route)) {
if (!(0, functions_1.isClientContext)(ctx)) {
return null;
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
observableId: id,
});
}
if (isQuery && !(0, functions_1.isQueryFunctionSpec)(spec)) {
if (!(0, functions_2.isClientContext)(ctx)) {
return null;
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
observableId: id,
});
(0, sendError_1.sendSimpleError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, { name, type }, id);
return null;
}
if (isStream && !(0, functions_1.isStreamFunctionSpec)(spec)) {
if (!(0, functions_2.isClientContext)(ctx)) {
return null;
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
observableId: id,
});
return null;
}
if (!isStream && !isQuery && !isChannel && !(0, functions_1.isFunctionSpec)(spec)) {
if (!(0, functions_2.isClientContext)(ctx)) {
return null;
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
observableId: id,
});
return null;
}
// @ts-ignore Fixed by chekcing the specs

@@ -88,3 +39,3 @@ return spec;

catch (err) {
functionNotFound(server, ctx, route, isQuery, isChannel, id);
functionNotFound(server, ctx, route, type, id);
}

@@ -91,0 +42,0 @@ return null;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destroyObs = exports.updateDestroyTimer = void 0;
const functions_1 = require("../functions");
const cleanup_1 = require("./cleanup");
const functions_1 = require("@based/functions");
const updateDestroyTimer = (server, channel) => {
const spec = server.functions.specs[channel.name];
if (!spec || !(0, functions_1.isQueryFunctionSpec)(spec)) {
if (!spec || !(0, functions_1.isBasedFunctionConfig)('query', spec)) {
console.warn('destroyObs - Cannot find obs function spec -', channel.name);

@@ -10,0 +10,0 @@ return;

@@ -22,2 +22,3 @@ "use strict";

name: obs.name,
type: 'query',
},

@@ -24,0 +25,0 @@ })

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.start = void 0;
const functions_1 = require("../../functions");
const update_1 = require("./update");
const error_1 = require("./error");
const relay_1 = require("./relay");
const functions_1 = require("@based/functions");
const start = (server, id) => {

@@ -15,3 +15,3 @@ const obs = server.activeObservablesById.get(id);

const spec = server.functions.specs[obs.name];
if (!spec || !(0, functions_1.isQueryFunctionSpec)(spec)) {
if (!spec || !(0, functions_1.isBasedFunctionConfig)('query', spec)) {
console.warn('Cannot find observable function spec!', obs.name);

@@ -30,3 +30,3 @@ return;

if (spec.relay) {
const client = server.clients[spec.relay];
const client = server.clients[spec.relay.client];
if (!client) {

@@ -40,3 +40,3 @@ (0, error_1.errorListener)(server, obs, new Error(`Relay client ${spec.relay} does not exist`));

try {
const r = spec.function(server.client, payload, update, (err) => {
const r = spec.fn(server.client, payload, update, (err) => {
(0, error_1.errorListener)(server, obs, err);

@@ -43,0 +43,0 @@ });

import { BasedServer } from './server';
import { HttpSession, WebSocketSession, Context } from '@based/functions';
import { HttpSession, WebSocketSession, Context, BasedRoute } from '@based/functions';
import { BasedErrorCode, ErrorPayload, BasedErrorData } from './error';

@@ -7,1 +7,2 @@ export declare function sendHttpError<T extends BasedErrorCode>(server: BasedServer, ctx: Context<HttpSession>, basedCode: T, payload: ErrorPayload[T]): void;

export declare function sendError<T extends BasedErrorCode>(server: BasedServer, ctx: Context<WebSocketSession | HttpSession>, basedCode: T, payload: ErrorPayload[T]): void;
export declare function sendSimpleError<T extends BasedErrorCode>(server: BasedServer, ctx: Context<WebSocketSession | HttpSession>, basedCode: T, route: BasedRoute, id?: number, payload?: ErrorPayload[T]): void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendError = exports.sendErrorData = exports.sendHttpError = void 0;
exports.sendSimpleError = exports.sendError = exports.sendErrorData = exports.sendHttpError = void 0;
const sendHttpResponse_1 = require("./sendHttpResponse");

@@ -54,2 +54,34 @@ const functions_1 = require("@based/functions");

exports.sendError = sendError;
function sendSimpleError(server, ctx, basedCode, route, id, payload) {
if (!ctx.session) {
return;
}
if (!payload) {
// @ts-ignore tmp for now
payload = id
? route.type === 'query'
? {
route,
observableId: id,
}
: route.type === 'channel'
? {
route,
channelId: id,
}
: {
route,
requestId: id,
}
: { route };
}
if ((0, functions_1.isHttpContext)(ctx)) {
return sendHttpError(server, ctx, basedCode, payload);
}
else if ((0, functions_1.isWsSession)(ctx.session)) {
const errorData = (0, error_1.createError)(server, ctx, basedCode, payload);
ctx.session.ws.send((0, protocol_1.encodeErrorResponse)((0, protocol_1.valueToBuffer)(errorData)), true, false);
}
}
exports.sendSimpleError = sendSimpleError;
//# sourceMappingURL=sendError.js.map
/// <reference types="node" />
/// <reference types="node" />
import { Context, BasedFunctionClient, WebSocketSession, HttpSession, Geo } from '@based/functions';
import type { ActiveObservable } from './observable';
import type { ActiveObservable } from './query';
import uws from '@based/uws';

@@ -56,9 +56,10 @@ import { BasedFunctions, FunctionConfig } from './functions';

functions: {
specs: {
configs: {
hello: {
function: async () => 'hello'
type: 'function',
fn: async () => 'hello'
},
counter: {
type: 'query',
function: () => () => {}
fn: () => () => {}
}

@@ -69,8 +70,8 @@ }

server.functions.addSpecs({
hello: { function: async () => 'hello' }
server.functions.add({
hello: { type: 'function', fn: async () => 'hello' }
})
server.functions.addRoutes({
bla: { type: 'query }
bla: { type: 'query' }
})

@@ -77,0 +78,0 @@

@@ -23,9 +23,10 @@ "use strict";

functions: {
specs: {
configs: {
hello: {
function: async () => 'hello'
type: 'function',
fn: async () => 'hello'
},
counter: {
type: 'query',
function: () => () => {}
fn: () => () => {}
}

@@ -36,8 +37,8 @@ }

server.functions.addSpecs({
hello: { function: async () => 'hello' }
server.functions.add({
hello: { type: 'function', fn: async () => 'hello' }
})
server.functions.addRoutes({
bla: { type: 'query }
bla: { type: 'query' }
})

@@ -44,0 +45,0 @@

@@ -1,15 +0,3 @@

import { Context } from '@based/functions';
import { BasedFunctionRoute, BasedQueryFunctionRoute, BasedRoute, BasedStreamFunctionRoute, BasedChannelFunctionRoute } from './functions';
import { Context, BasedRoute, BasedFunctionTypes } from '@based/functions';
import { BasedServer } from './server';
type FnType = 'query' | 'stream' | 'fn' | 'channel';
export declare const verifyRoute: <T extends FnType>(server: BasedServer, ctx: Context, type: T, route: BasedRoute | null, name: string, id?: number) => T extends "channel" ? BasedChannelFunctionRoute : T extends "query" ? BasedQueryFunctionRoute : T extends "stream" ? BasedStreamFunctionRoute : {
name: string;
path?: string;
maxPayloadSize?: number;
rateLimitTokens?: number;
public?: boolean;
headers?: string[];
internalOnly?: boolean;
data?: any;
};
export {};
export declare const verifyRoute: <T extends BasedFunctionTypes>(server: BasedServer, ctx: Context, type: T, route: BasedRoute | null, name: string, id?: number) => BasedRoute<T, "type" | "name">;

@@ -5,3 +5,2 @@ "use strict";

const functions_1 = require("@based/functions");
const functions_2 = require("./functions");
const sendError_1 = require("./sendError");

@@ -18,89 +17,21 @@ const error_1 = require("./error");

name,
type: 'function',
},
});
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, type === 'channel'
? {
route: { name },
channelId: id,
}
: type === 'query'
? {
route: { name },
observableId: id,
}
: {
route: { name },
requestId: id,
});
(0, sendError_1.sendSimpleError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, { name, type }, id);
return null;
}
if (route.internalOnly === true && (0, functions_1.isClientContext)(ctx)) {
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, type === 'query'
? {
route: { name },
observableId: id,
}
: {
route: { name },
requestId: id,
});
(0, sendError_1.sendSimpleError)(server, ctx, error_1.BasedErrorCode.FunctionNotFound, { name, type }, id);
return null;
}
if (type === 'query') {
if (!(0, functions_2.isQueryFunctionRoute)(route)) {
if (!(0, functions_1.isClientContext)(ctx)) {
throw (0, error_1.createError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
observableId: id,
});
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route: { name },
observableId: id,
});
return null;
}
}
if (type === 'stream') {
if (!(0, functions_2.isStreamFunctionRoute)(route)) {
if (!(0, functions_1.isClientContext)(ctx)) {
throw (0, error_1.createError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
requestId: id,
});
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route: { name },
requestId: id,
});
return null;
}
}
if (type === 'channel') {
if (!(0, functions_2.isChannelFunctionRoute)(route)) {
if (!(0, functions_1.isClientContext)(ctx)) {
throw (0, error_1.createError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
channelId: id,
});
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route: { name },
channelId: id,
});
return null;
}
}
if (type === 'fn' && !(0, functions_2.isFunctionRoute)(route)) {
if (!(0, functions_1.isBasedRoute)(type, route)) {
if (!(0, functions_1.isClientContext)(ctx)) {
throw (0, error_1.createError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
requestId: id,
observableId: id,
});
}
(0, sendError_1.sendError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, {
route,
requestId: id,
});
(0, sendError_1.sendSimpleError)(server, ctx, error_1.BasedErrorCode.FunctionIsWrongType, { name, type }, id);
return null;

@@ -107,0 +38,0 @@ }

{
"name": "@based/server",
"version": "5.0.3",
"version": "6.0.0",
"license": "MIT",

@@ -22,2 +22,3 @@ "main": "./dist/index.js",

"dependencies": {
"utility-types": "^3.10.0",
"@based/uws": "^4.0.0",

@@ -31,3 +32,3 @@ "@saulx/diff": "^1.1.4",

"mime-types": "^2.1.31",
"@based/functions": "^1.2.2"
"@based/functions": "^2.0.0"
},

@@ -34,0 +35,0 @@ "devDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc