New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@zlepper/rpc

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zlepper/rpc - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

esm/shared/normalized-event-target.d.ts

0

esm/client/worker-client-connection.d.ts

@@ -0,0 +0,0 @@ import { CrossInvocation, CrossInvocationResult } from '../shared/cross-invocation.js';

export {};
//# sourceMappingURL=worker-client-connection.js.map

9

esm/client/worker-consumer.d.ts
import { WorkerClientConnection } from './worker-client-connection.js';
export declare type AsyncProperty<T> = T extends (...args: infer TArgs) => infer TResult ? (...args: TArgs) => TResult extends Promise<any> ? TResult : Promise<TResult> : Promise<T>;
export declare type WrappedObject<T> = {
import { IEventDispatcher, InferEvent, NormalizedEventTarget } from "../shared/normalized-event-target";
export type AsyncProperty<T> = T extends (...args: infer TArgs) => infer TResult ? (...args: TArgs) => TResult extends Promise<any> ? TResult : Promise<TResult> : Promise<T>;
export type WrappedObject<T> = T extends IEventDispatcher<infer TEvent> ? {
readonly [property in keyof Omit<T, 'dispatchEvent'> as T[property] extends Function ? property : never]: AsyncProperty<T[property]>;
} & NormalizedEventTarget<TEvent> : {
readonly [property in keyof T as T[property] extends Function ? property : never]: AsyncProperty<T[property]>;
};
export declare function wrapBackgroundService<T extends object>(workerConnection: WorkerClientConnection): WrappedObject<T>;
export declare function wrapBackgroundService<T extends object | IEventDispatcher<TEvent>, TEvent extends object = InferEvent<T>>(workerConnection: WorkerClientConnection): WrappedObject<T>;
class EventPipe {
constructor() {
this.listeners = new Map();
}
listeners = new Map();
addListener(refId, listener) {

@@ -12,13 +10,48 @@ this.listeners.set(refId, listener);

emit(result) {
const listener = this.listeners.get(result.refId);
if (result.kind === 'message') {
const listener = this.listeners.get(result.refId);
if (listener) {
listener(result);
}
}
else {
const eventListeners = this._eventListeners.get(result.type);
if (eventListeners) {
for (let listener of eventListeners) {
listener(result.data);
}
}
}
}
_eventListeners = new Map();
addEventListener(type, listener, _options) {
const listeners = this._eventListeners.get(type);
if (!listeners) {
const newListeners = new Set();
newListeners.add(listener);
this._eventListeners.set(type, newListeners);
}
else {
listeners.add(listener);
}
}
removeEventListener(type, listener) {
const listeners = this._eventListeners.get(type);
if (!listeners) {
return;
}
if (listener) {
listener(result);
listeners.delete(listener);
}
else {
this._eventListeners.delete(type);
}
}
}
class BackgroundWrapper {
workerConnection;
refId = 1;
pipe = new EventPipe();
constructor(workerConnection) {
this.workerConnection = workerConnection;
this.refId = 1;
this.pipe = new EventPipe();
workerConnection.addListener(data => {

@@ -29,2 +62,12 @@ this.pipe.emit(data);

get(_target, propertyName) {
if (propertyName === 'addEventListener') {
return (type, listener, options) => {
this.pipe.addEventListener(type, listener, options);
};
}
else if (propertyName === 'removeEventListener') {
return (type, listener) => {
this.pipe.removeEventListener(type, listener);
};
}
return (...args) => {

@@ -38,9 +81,14 @@ return new Promise((resolve, reject) => {

const cleanup = this.pipe.addListener(message.refId, result => {
if (result.success) {
resolve(result.result);
if (result.kind === 'message') {
if (result.success) {
resolve(result.result);
}
else {
reject(result.error);
}
cleanup();
}
else {
reject(result.error);
throw new Error('Got event result for a non-event invocation');
}
cleanup();
});

@@ -47,0 +95,0 @@ this.workerConnection.send(message);

@@ -0,0 +0,0 @@ export * from './client/worker-consumer.js';

@@ -0,0 +0,0 @@ export * from './client/worker-consumer.js';

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

export declare type FunctionArguments<TProperty> = TProperty extends (...args: infer TArgs) => any ? TArgs : `Property is not a functions`[];
export type FunctionArguments<TProperty> = TProperty extends (...args: infer TArgs) => any ? TArgs : `Property is not a function`[];
export interface CrossInvocation<T extends object = any, TPropertyName extends keyof T = any> {

@@ -8,2 +8,3 @@ readonly refId: number;

export interface FailedCrossInvocationResult {
kind: 'message';
readonly refId: number;

@@ -14,2 +15,3 @@ readonly success: false;

export interface SuccessfulCrossInvocationResult<T extends object, TPropertyName extends keyof T> {
kind: 'message';
readonly refId: number;

@@ -19,2 +21,8 @@ readonly success: true;

}
export declare type CrossInvocationResult<T extends object = any, TPropertyName extends keyof T = any> = FailedCrossInvocationResult | SuccessfulCrossInvocationResult<T, TPropertyName>;
export interface CrossEvent<TEvent extends object, TEventName extends keyof TEvent> {
kind: 'event';
readonly refId: -1;
readonly type: TEventName;
readonly data: TEvent[TEventName];
}
export type CrossInvocationResult<T extends object = any, TPropertyName extends keyof T = any, TEvent extends object = any, TEventName extends keyof TEvent = any> = FailedCrossInvocationResult | SuccessfulCrossInvocationResult<T, TPropertyName> | CrossEvent<TEvent, TEventName>;
export {};
//# sourceMappingURL=cross-invocation.js.map

@@ -0,0 +0,0 @@ import { WorkerClientConnection } from './client/worker-client-connection.js';

class Pipe {
clientCallback;
serverCallback;
sendToServer(message) {

@@ -24,2 +26,3 @@ const raw = JSON.stringify(message);

export class TestClientConnection {
pipe;
constructor(pipe) {

@@ -36,2 +39,3 @@ this.pipe = pipe;

export class TestServerConnection {
pipe;
constructor(pipe) {

@@ -38,0 +42,0 @@ this.pipe = pipe;

import { WorkerServerConnection } from "./worker-server-connection.js";
import { IEventDispatcher } from "../shared/normalized-event-target";
interface WorkerProviderRef {

@@ -8,2 +9,5 @@ stop(): void;

export declare function createWorkerProvider<T extends object>(target: T, serverConnection: WorkerServerConnection): WorkerProviderRef;
export declare abstract class MyEventDispatcher<TEvent extends object> implements IEventDispatcher<TEvent> {
dispatchEvent<K extends keyof TEvent>(type: K, data: TEvent[K]): void;
}
export {};

@@ -0,5 +1,14 @@

// @ts-ignore
function isEventDispatcher(target) {
return '__initializeEventDispatcher' in target;
}
class WorkerProvider {
target;
serverConnection;
constructor(target, serverConnection) {
this.target = target;
this.serverConnection = serverConnection;
if (isEventDispatcher(target)) {
target.__initializeEventDispatcher(this);
}
}

@@ -14,2 +23,3 @@ stop() {

const errorMessage = {
kind: 'message',
refId: invocation.refId,

@@ -25,2 +35,3 @@ success: false,

const message = {
kind: 'message',
refId: invocation.refId,

@@ -32,2 +43,11 @@ result,

}
sendEvent(type, data) {
const event = {
refId: -1,
kind: 'event',
type,
data
};
this.serverConnection.send(event);
}
handleInvocation(invocation) {

@@ -37,3 +57,3 @@ try {

if (typeof prop !== 'function') {
this.sendErrorResponse(invocation, new Error(`Property ${invocation.propertyName} is not a function on the underlying worker objects. Did you use the correct type in both the Worker and main code?`));
this.sendErrorResponse(invocation, new Error(`Property ${String(invocation.propertyName)} is not a function on the underlying worker objects. Did you use the correct type in both the Worker and main code?`));
return;

@@ -63,2 +83,20 @@ }

}
export class MyEventDispatcher {
/**
* @internal
*/
__workerProvider = null;
/**
* @internal
*/
__initializeEventDispatcher(provider) {
this.__workerProvider = provider;
}
dispatchEvent(type, data) {
if (!this.__workerProvider) {
throw new Error('Worker provider has not been initialized. Did you call dispatchEvent before passing the worker to the provider?');
}
this.__workerProvider.sendEvent(type, data);
}
}
//# sourceMappingURL=worker-provider.js.map

@@ -0,0 +0,0 @@ import { CrossInvocation, CrossInvocationResult } from '../shared/cross-invocation.js';

export {};
//# sourceMappingURL=worker-server-connection.js.map
{
"name": "@zlepper/rpc",
"version": "0.0.5",
"version": "0.0.6",
"description": "Allows RPC from the main thread to a background worker thread (Of any kind), using ES6 classes.",
"scripts": {
"test": "yarn run build-tests && mocha 'tests/build/tests/*.spec.js'",
"test": "yarn run build-tests && mocha 'tests/build/tests/*.spec.js' --async-stack-traces --full-trace",
"build": "tsc",

@@ -15,8 +15,8 @@ "build-tests": "tsc -p tests"

"devDependencies": {
"@types/chai": "^4.2.15",
"@types/mocha": "^8.2.2",
"@types/chai": "^4.3.6",
"@types/mocha": "^10.0.1",
"@zlepper/testing": "0.0.1",
"chai": "^4.3.4",
"mocha": "^8.3.2",
"typescript": "^4.2.3"
"chai": "^4.3.8",
"mocha": "^10.2.0",
"typescript": "^5.1.5"
},

@@ -23,0 +23,0 @@ "author": "Zlepper",

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