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

clooneyjs

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clooneyjs - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

5

clooney.bundle.js

@@ -329,2 +329,6 @@ var Clooney = (function (exports) {

/**
* `asRemoteValue` marks a value. If a marked value is used as an parameter or return value, it will not be transferred but instead proxied.
*/
const asRemoteValue = Comlink.proxyValue;
/**
* `defaultWorkerSrc` is the path passed to the `new Worker()` call. It’s recommended to not change this variable but instead overload `newWorkerFunc`.

@@ -398,2 +402,3 @@ */

exports.asRemoteValue = asRemoteValue;
exports.defaultWorkerSrc = defaultWorkerSrc;

@@ -400,0 +405,0 @@ exports.defaultOpts = defaultOpts;

2

clooney.bundle.min.js

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

var Clooney=function(a){'use strict';async function b(a,b={}){return h.spawn(a,b)}function c(a=self){d.expose({async spawn(a){const b=new Function(`return ${a};`)();return d.proxyValue(new b)}},a)}const d=function(){function a(a){if(n(a)&&(a=h(a)),!i(a))throw Error('endpoint does not have all of addEventListener, removeEventListener and postMessage defined');return j(a),p(async(b)=>{let c=[];('APPLY'===b.type||'CONSTRUCT'===b.type)&&(c=b.argumentsList.map(d));const f=await o(a,Object.assign({},b,{argumentsList:c}),r(c)),g=f.data;return e(g.value)})}function b(a){return a[w]=!0,a}function c(a,c){if(n(c)&&(c=h(c)),!i(c))throw Error('endpoint does not have all of addEventListener, removeEventListener and postMessage defined');j(c),k(c,async function(d){if(!d.data.id)return;const f=d.data;let g=await f.callPath.slice(0,-1).reduce((a,b)=>a[b],a),h=await f.callPath.reduce((a,b)=>a[b],a),i=h,j=[];if(('APPLY'===f.type||'CONSTRUCT'===f.type)&&(j=f.argumentsList.map(e)),'APPLY'===f.type)try{i=await h.apply(g,j)}catch(a){i=a,i[x]=!0}if('CONSTRUCT'===f.type)try{i=new h(...j),i=b(i)}catch(a){i=a,i[x]=!0}return'SET'===f.type&&(h[f.property]=f.value,i=!0),i=t(i),i.id=f.id,c.postMessage(i,r([i]))})}function d(a){for(const[b,c]of y.entries())if(c.canHandle(a))return{type:b,value:c.serialize(a)};let b=[];for(const c of s(a))for(const[a,d]of y.entries())d.canHandle(c.value)&&b.push({path:c.path,wrappedValue:{type:a,value:c.value}});return{type:'RAW',value:a,wrappedChildren:b}}function e(a){if(y.has(a.type)){const b=y.get(a.type);return b.deserialize(a.value)}if(g(a)){for(const b of a.wrappedChildren||[]){if(!y.has(b.wrappedValue.type))throw Error(`Unknown value type "${a.type}" at ${b.path.join('.')}`);const c=y.get(b.wrappedValue.type),d=c.deserialize(b.wrappedValue.value);f(a.value,b.path,d)}return a.value}throw Error(`Unknown value type "${a.type}"`)}function f(a,b,c){const d=b.slice(-1)[0],e=b.slice(0,-1).reduce((a,b)=>a[b],a);e[d]=c}function g(a){return'RAW'===a.type}function h(a){if('Window'!==self.constructor.name)throw Error('self is not a window');return{addEventListener:self.addEventListener.bind(self),removeEventListener:self.removeEventListener.bind(self),postMessage:(b,c)=>a.postMessage(b,'*',c)}}function i(a){return'addEventListener'in a&&'removeEventListener'in a&&'postMessage'in a}function j(a){m(a)&&a.start()}function k(a,b){a.addEventListener('message',b)}function l(a,b){a.removeEventListener('message',b)}function m(a){return'MessagePort'===a.constructor.name}function n(a){return['window','length','location','parent','opener'].every((b)=>b in a)}function o(a,b,c){const d=`${v}-${z++}`;return new Promise((e)=>{k(a,function b(c){c.data.id!==d||(l(a,b),e(c))}),b=Object.assign({},b,{id:d}),a.postMessage(b,c)})}function p(a,b=[]){return new Proxy(function(){},{construct(c,d){return a({type:'CONSTRUCT',callPath:b,argumentsList:d})},apply(c,d,e){return'bind'===b[b.length-1]?p(a,b.slice(0,-1)):a({type:'APPLY',callPath:b,argumentsList:e})},get(c,d,e){if('then'===d&&0===b.length)return{then:()=>e};if('then'===d){const c=a({type:'GET',callPath:b});return Promise.resolve(c).then.bind(c)}return p(a,b.concat(d))},set(c,d,e){return a({type:'SET',callPath:b,property:d,value:e})}})}function q(a){return u.some((b)=>a instanceof b)}function*s(a,b=[],c=null){if(a&&(c||(c=new WeakSet),!c.has(a))&&'string'!=typeof a){'object'==typeof a&&c.add(a),yield{value:a,path:b};let d=Object.keys(a);for(const e of d)yield*s(a[e],[...b,e],c)}}function r(a){const b=[];for(const c of s(a))q(c.value)&&b.push(c.value);return b}function t(a){for(const[b,c]of y.entries())if(c.canHandle(a)){const d=c.serialize(a);return{value:{type:b,value:d}}}return{value:{type:'RAW',value:a}}}const u=[ArrayBuffer,MessagePort],v=Math.floor(Math.random()*Number.MAX_SAFE_INTEGER),w=Symbol('proxyValue'),x=Symbol('throw'),y=new Map([['PROXY',{canHandle:(a)=>a&&a[w],serialize:(a)=>{const{port1:b,port2:d}=new MessageChannel;return c(a,b),d},deserialize:(b)=>{return a(b)}}],['THROW',{canHandle:(a)=>a&&a[x],serialize:(a)=>a.toString()+'\n'+a.stack,deserialize:(a)=>{throw Error(a)}}]]);let z=0;return{proxy:a,proxyValue:b,transferHandlers:y,expose:c}}();let e='document'in self?document.currentScript&&document.currentScript.src:'';const f={maxNumContainers:1,newWorkerFunc:async()=>new Worker(e)};class g{constructor(a={}){this._nextIndex=0,this._options=Object.assign({},f,a),this._containers=Array(this._options.maxNumContainers).fill(null)}async _initOrGetContainer(a){if(a>=this._containers.length)throw Error('No worker available');if(!this._containers[a]){const c=await this._options.newWorkerFunc(),e=d.proxy(c);this._containers[a]={spawn:e.spawn.bind(b),terminate:c.terminate.bind(c)}}return this._containers[a]}async _getNextContainer(){const a=await this._initOrGetContainer(this._nextIndex);return this._nextIndex=(this._nextIndex+1)%this._options.maxNumContainers,a}async spawn(a,b={}){const c=await this._getNextContainer(b);return await c.spawn(a.toString(),b)}async terminate(){this._containers.filter((a)=>a).forEach((a)=>a.terminate()),this._containers.length=0}get terminated(){return 0>=this._containers.length}}let h=new g;return function(){const a=self.WorkerGlobalScope;return a&&self instanceof a}()&&c(),a.defaultWorkerSrc=e,a.defaultOpts=f,a.RoundRobinStrategy=g,a.defaultStrategy=h,a.spawn=b,a.makeContainer=c,a.Comlink=d,a}({});
var Clooney=function(a){'use strict';async function b(a,b={}){return i.spawn(a,b)}function c(a=self){d.expose({async spawn(a){const b=new Function(`return ${a};`)();return d.proxyValue(new b)}},a)}const d=function(){function a(a){if(n(a)&&(a=h(a)),!i(a))throw Error('endpoint does not have all of addEventListener, removeEventListener and postMessage defined');return j(a),p(async(b)=>{let c=[];('APPLY'===b.type||'CONSTRUCT'===b.type)&&(c=b.argumentsList.map(d));const f=await o(a,Object.assign({},b,{argumentsList:c}),r(c)),g=f.data;return e(g.value)})}function b(a){return a[w]=!0,a}function c(a,c){if(n(c)&&(c=h(c)),!i(c))throw Error('endpoint does not have all of addEventListener, removeEventListener and postMessage defined');j(c),k(c,async function(d){if(!d.data.id)return;const f=d.data;let g=await f.callPath.slice(0,-1).reduce((a,b)=>a[b],a),h=await f.callPath.reduce((a,b)=>a[b],a),i=h,j=[];if(('APPLY'===f.type||'CONSTRUCT'===f.type)&&(j=f.argumentsList.map(e)),'APPLY'===f.type)try{i=await h.apply(g,j)}catch(a){i=a,i[x]=!0}if('CONSTRUCT'===f.type)try{i=new h(...j),i=b(i)}catch(a){i=a,i[x]=!0}return'SET'===f.type&&(h[f.property]=f.value,i=!0),i=t(i),i.id=f.id,c.postMessage(i,r([i]))})}function d(a){for(const[b,c]of y.entries())if(c.canHandle(a))return{type:b,value:c.serialize(a)};let b=[];for(const c of s(a))for(const[a,d]of y.entries())d.canHandle(c.value)&&b.push({path:c.path,wrappedValue:{type:a,value:c.value}});return{type:'RAW',value:a,wrappedChildren:b}}function e(a){if(y.has(a.type)){const b=y.get(a.type);return b.deserialize(a.value)}if(g(a)){for(const b of a.wrappedChildren||[]){if(!y.has(b.wrappedValue.type))throw Error(`Unknown value type "${a.type}" at ${b.path.join('.')}`);const c=y.get(b.wrappedValue.type),d=c.deserialize(b.wrappedValue.value);f(a.value,b.path,d)}return a.value}throw Error(`Unknown value type "${a.type}"`)}function f(a,b,c){const d=b.slice(-1)[0],e=b.slice(0,-1).reduce((a,b)=>a[b],a);e[d]=c}function g(a){return'RAW'===a.type}function h(a){if('Window'!==self.constructor.name)throw Error('self is not a window');return{addEventListener:self.addEventListener.bind(self),removeEventListener:self.removeEventListener.bind(self),postMessage:(b,c)=>a.postMessage(b,'*',c)}}function i(a){return'addEventListener'in a&&'removeEventListener'in a&&'postMessage'in a}function j(a){m(a)&&a.start()}function k(a,b){a.addEventListener('message',b)}function l(a,b){a.removeEventListener('message',b)}function m(a){return'MessagePort'===a.constructor.name}function n(a){return['window','length','location','parent','opener'].every((b)=>b in a)}function o(a,b,c){const d=`${v}-${z++}`;return new Promise((e)=>{k(a,function b(c){c.data.id!==d||(l(a,b),e(c))}),b=Object.assign({},b,{id:d}),a.postMessage(b,c)})}function p(a,b=[]){return new Proxy(function(){},{construct(c,d){return a({type:'CONSTRUCT',callPath:b,argumentsList:d})},apply(c,d,e){return'bind'===b[b.length-1]?p(a,b.slice(0,-1)):a({type:'APPLY',callPath:b,argumentsList:e})},get(c,d,e){if('then'===d&&0===b.length)return{then:()=>e};if('then'===d){const c=a({type:'GET',callPath:b});return Promise.resolve(c).then.bind(c)}return p(a,b.concat(d))},set(c,d,e){return a({type:'SET',callPath:b,property:d,value:e})}})}function q(a){return u.some((b)=>a instanceof b)}function*s(a,b=[],c=null){if(a&&(c||(c=new WeakSet),!c.has(a))&&'string'!=typeof a){'object'==typeof a&&c.add(a),yield{value:a,path:b};let d=Object.keys(a);for(const e of d)yield*s(a[e],[...b,e],c)}}function r(a){const b=[];for(const c of s(a))q(c.value)&&b.push(c.value);return b}function t(a){for(const[b,c]of y.entries())if(c.canHandle(a)){const d=c.serialize(a);return{value:{type:b,value:d}}}return{value:{type:'RAW',value:a}}}const u=[ArrayBuffer,MessagePort],v=Math.floor(Math.random()*Number.MAX_SAFE_INTEGER),w=Symbol('proxyValue'),x=Symbol('throw'),y=new Map([['PROXY',{canHandle:(a)=>a&&a[w],serialize:(a)=>{const{port1:b,port2:d}=new MessageChannel;return c(a,b),d},deserialize:(b)=>{return a(b)}}],['THROW',{canHandle:(a)=>a&&a[x],serialize:(a)=>a.toString()+'\n'+a.stack,deserialize:(a)=>{throw Error(a)}}]]);let z=0;return{proxy:a,proxyValue:b,transferHandlers:y,expose:c}}(),e=d.proxyValue;let f='document'in self?document.currentScript&&document.currentScript.src:'';const g={maxNumContainers:1,newWorkerFunc:async()=>new Worker(f)};class h{constructor(a={}){this._nextIndex=0,this._options=Object.assign({},g,a),this._containers=Array(this._options.maxNumContainers).fill(null)}async _initOrGetContainer(a){if(a>=this._containers.length)throw Error('No worker available');if(!this._containers[a]){const c=await this._options.newWorkerFunc(),e=d.proxy(c);this._containers[a]={spawn:e.spawn.bind(b),terminate:c.terminate.bind(c)}}return this._containers[a]}async _getNextContainer(){const a=await this._initOrGetContainer(this._nextIndex);return this._nextIndex=(this._nextIndex+1)%this._options.maxNumContainers,a}async spawn(a,b={}){const c=await this._getNextContainer(b);return await c.spawn(a.toString(),b)}async terminate(){this._containers.filter((a)=>a).forEach((a)=>a.terminate()),this._containers.length=0}get terminated(){return 0>=this._containers.length}}let i=new h;return function(){const a=self.WorkerGlobalScope;return a&&self instanceof a}()&&c(),a.asRemoteValue=e,a.defaultWorkerSrc=f,a.defaultOpts=g,a.RoundRobinStrategy=h,a.defaultStrategy=i,a.spawn=b,a.makeContainer=c,a.Comlink=d,a}({});

@@ -16,2 +16,6 @@ /**

/**
* `asRemoteValue` marks a value. If a marked value is used as an parameter or return value, it will not be transferred but instead proxied.
*/
export declare const asRemoteValue: <T>(x: T) => T;
/**
* `defaultWorkerSrc` is the path passed to the `new Worker()` call. It’s recommended to not change this variable but instead overload `newWorkerFunc`.

@@ -18,0 +22,0 @@ */

@@ -16,2 +16,6 @@ /**

/**
* `asRemoteValue` marks a value. If a marked value is used as an parameter or return value, it will not be transferred but instead proxied.
*/
export const asRemoteValue = Comlink.proxyValue;
/**
* `defaultWorkerSrc` is the path passed to the `new Worker()` call. It’s recommended to not change this variable but instead overload `newWorkerFunc`.

@@ -18,0 +22,0 @@ */

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

import{Comlink}from'comlink';export{Comlink}from'comlink';export let defaultWorkerSrc='document'in self?document.currentScript&&document.currentScript.src:'';export const defaultOpts={maxNumContainers:1,newWorkerFunc:async()=>new Worker(defaultWorkerSrc)};export class RoundRobinStrategy{constructor(a={}){this._nextIndex=0,this._options=Object.assign({},defaultOpts,a),this._containers=Array(this._options.maxNumContainers).fill(null)}async _initOrGetContainer(a){if(a>=this._containers.length)throw Error('No worker available');if(!this._containers[a]){const b=await this._options.newWorkerFunc(),c=Comlink.proxy(b);this._containers[a]={spawn:c.spawn.bind(spawn),terminate:b.terminate.bind(b)}}return this._containers[a]}async _getNextContainer(){const a=await this._initOrGetContainer(this._nextIndex);return this._nextIndex=(this._nextIndex+1)%this._options.maxNumContainers,a}async spawn(a,b={}){const c=await this._getNextContainer(b);return await c.spawn(a.toString(),b)}async terminate(){this._containers.filter((a)=>a).forEach((a)=>a.terminate()),this._containers.length=0}get terminated(){return 0>=this._containers.length}}export let defaultStrategy=new RoundRobinStrategy;export async function spawn(a,b={}){return defaultStrategy.spawn(a,b)}export function makeContainer(a=self){Comlink.expose({async spawn(a){const b=new Function(`return ${a};`)();return Comlink.proxyValue(new b)}},a)}function isWorker(){const a=self.WorkerGlobalScope;return a&&self instanceof a}isWorker()&&makeContainer();
import{Comlink}from'comlink';export{Comlink}from'comlink';export const asRemoteValue=Comlink.proxyValue;export let defaultWorkerSrc='document'in self?document.currentScript&&document.currentScript.src:'';export const defaultOpts={maxNumContainers:1,newWorkerFunc:async()=>new Worker(defaultWorkerSrc)};export class RoundRobinStrategy{constructor(a={}){this._nextIndex=0,this._options=Object.assign({},defaultOpts,a),this._containers=Array(this._options.maxNumContainers).fill(null)}async _initOrGetContainer(a){if(a>=this._containers.length)throw Error('No worker available');if(!this._containers[a]){const b=await this._options.newWorkerFunc(),c=Comlink.proxy(b);this._containers[a]={spawn:c.spawn.bind(spawn),terminate:b.terminate.bind(b)}}return this._containers[a]}async _getNextContainer(){const a=await this._initOrGetContainer(this._nextIndex);return this._nextIndex=(this._nextIndex+1)%this._options.maxNumContainers,a}async spawn(a,b={}){const c=await this._getNextContainer(b);return await c.spawn(a.toString(),b)}async terminate(){this._containers.filter((a)=>a).forEach((a)=>a.terminate()),this._containers.length=0}get terminated(){return 0>=this._containers.length}}export let defaultStrategy=new RoundRobinStrategy;export async function spawn(a,b={}){return defaultStrategy.spawn(a,b)}export function makeContainer(a=self){Comlink.expose({async spawn(a){const b=new Function(`return ${a};`)();return Comlink.proxyValue(new b)}},a)}function isWorker(){const a=self.WorkerGlobalScope;return a&&self instanceof a}isWorker()&&makeContainer();
{
"name": "clooneyjs",
"version": "0.3.1",
"version": "0.4.0",
"description": "Clooney is an actor library for the web",

@@ -34,3 +34,3 @@ "main": "clooney.js",

"chai": "4.1.2",
"comlinkjs": "^2.3.1",
"comlinkjs": "^2.3.2",
"eslint": "4.13.1",

@@ -37,0 +37,0 @@ "eslint-config-google": "0.9.1",

@@ -54,2 +54,17 @@ # Clooney

### `Clooney.asRemoteValue(obj)`
`asRemoteValue` marks a value. If a marked value is used as an parameter or return value, it will not be transferred but instead proxied. This is necessary, for example, for callbacks.
```js
class MyActor {
async callCallback(cb) {
await cb('ohai');
}
}
const actor = await Clooney.spawn(MyActor);
await actor.callCallback(msg => console.log(msg)); // logs 'ohai'
```
## CDN

@@ -59,3 +74,3 @@ If you want to use Clooney from a CDN, you need to work around the same-origin restrictions that workers have:

```html
<script src="https://cdn.jsdelivr.net/npm/clooneyjs@0.3.1/clooney.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clooneyjs@0.4.0/clooney.bundle.min.js"></script>
<script>

@@ -62,0 +77,0 @@ async function newWorkerFunc() {

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