transferables
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -12,2 +12,18 @@ export declare const TypedArray: any; | ||
}; | ||
/** | ||
* Let's you know which transferable objects to actually exist in the js runtime the library is running in | ||
*/ | ||
export declare const AVAILABLE_TRANSFERABLE_OBJECTS: { | ||
ReadableStreamExists: boolean; | ||
WritableStreamExists: boolean; | ||
TransformStreamExists: boolean; | ||
MessageChannelExists: boolean; | ||
MessagePortExists: boolean; | ||
ArrayBufferExists: boolean; | ||
AudioDataExists: boolean; | ||
ImageBitmapExists: boolean; | ||
VideoFrameExists: boolean; | ||
OffscreenCanvasExists: boolean; | ||
RTCDataChannelExists: boolean; | ||
}; | ||
export declare type TypeTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; | ||
@@ -14,0 +30,0 @@ export declare type TypeTransferable = ArrayBuffer | MessagePort | ReadableStream | WritableStream | TransformStream | ImageBitmap | OffscreenCanvas | RTCDataChannel; |
@@ -1,1 +0,1 @@ | ||
(function(s,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(s=typeof globalThis<"u"?globalThis:s||self,d(s.Transferables={}))})(this,function(s){"use strict";const d=Object.getPrototypeOf(Int8Array),p=globalThis.AudioData,T=globalThis.VideoFrame,b=globalThis.OffscreenCanvas,w=globalThis.RTCDataChannel;async function C(){const n=await(async()=>{try{const l=new MessageChannel,a={port1:l.port1},r=structuredClone(a,{transfer:[l.port1]}),t=new MessageChannel,e={port1:r.port1};await new Promise(o=>{t.port1.postMessage(e,[e.port1]),t.port1.onmessage=()=>{o()},t.port2.onmessage=({data:i})=>{t.port2.postMessage(i,[i.port1])}}),t.port1.close()}catch(l){return console.warn(l),!1}return!0})(),u=await(async()=>{try{const l={readonly:new ReadableStream,writeonly:new WritableStream,tranformonly:new TransformStream},a=structuredClone(l,{transfer:[l.readonly,l.writeonly,l.tranformonly]}),r=new MessageChannel,t=a;await new Promise(e=>{r.port1.postMessage(t,[t.readonly,t.writeonly,t.tranformonly]),r.port1.onmessage=()=>{e()},r.port2.onmessage=({data:o})=>{r.port2.postMessage(o,[o.readonly,o.writeonly,o.tranformonly].filter(i=>i!=null))}}),r.port1.close()}catch(l){return console.warn(l),!1}return!0})();return{channel:n,streams:u}}function h(n){return typeof n=="object"&&n!==null||typeof n=="function"}function g(n){return n instanceof d||n instanceof DataView}function f(n){return"ReadableStream"in globalThis&&n instanceof ReadableStream||"WritableStream"in globalThis&&n instanceof WritableStream||"TransformStream"in globalThis&&n instanceof TransformStream}function y(n){return"MessageChannel"in globalThis&&n instanceof MessageChannel}function m(n){return"ArrayBuffer"in globalThis&&n instanceof ArrayBuffer||"MessagePort"in globalThis&&n instanceof MessagePort||"AudioData"in globalThis&&n instanceof p||"ImageBitmap"in globalThis&&n instanceof ImageBitmap||"VideoFrame"in globalThis&&n instanceof T||"OffscreenCanvas"in globalThis&&n instanceof b||"RTCDataChannel"in globalThis&&n instanceof w}function A(n){return Array.from(new Set(n))}function S(n,u=!1,l=1e4){const a=new Set([]);let r=[],t=[n];for(;t.length>0&&l>0;){for(let e of t)if(g(e))a.add(e.buffer);else if(m(e))a.add(e);else if(y(e))a.add(e.port1),a.add(e.port2);else if(u&&f(e))a.add(e);else if(!f(e)&&h(e)){const o=Array.isArray(e)?e:Object.values(e),i=o.length;for(let c=0;c<i;c++)r.push(o[c])}t=r,r=[],l--}return Array.from(a)}function*O(n,u=!1,l=1e4){const a=new Set([]);let r=[n],t=[];for(;r.length>0&&l>0;){for(let e of r)if(!a.has(e)){if(g(e)){a.add(e);const{buffer:o}=e;if(a.has(o))continue;yield o,a.add(o)}else if(m(e))yield e,a.add(e);else if(y(e)){if(a.add(e),a.has(e.port1)||a.has(e.port2))continue;yield e.port1,yield e.port2,a.add(e.port1),a.add(e.port2)}else if(u&&f(e))yield e,a.add(e);else if(!f(e)&&h(e)){const o=Array.isArray(e)?e:Object.values(e),i=o.length;for(let c=0;c<i;c++)t.push(o[c])}}r=t,t=[],l--}return a.clear(),r=null,t=null,null}function M(n,u=!1,l=1e4){let a=[n],r=[];for(;a.length>0&&l>0;){for(let t of a){if(g(t))return!0;if(m(t))return!0;if(y(t))return!0;if(u&&f(t))return!0;if(h(t)&&!f(t)){const e=Array.isArray(t)?t:Object.values(t),o=e.length;for(let i=0;i<o;i++)r.push(e[i])}}a=r,r=[],l--}return a=null,r=null,!1}s.AudioData=p,s.OffscreenCanvas=b,s.RTCDataChannel=w,s.TypedArray=d,s.VideoFrame=T,s.filterOutDuplicates=A,s.getTransferable=O,s.getTransferables=S,s.hasTransferables=M,s.isMessageChannel=y,s.isObject=h,s.isStream=f,s.isSupported=C,s.isTransferable=m,s.isTypedArray=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}); | ||
(function(a,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(a=typeof globalThis<"u"?globalThis:a||self,d(a.Transferables={}))})(this,function(a){"use strict";const d=Object.getPrototypeOf(Int8Array),C=globalThis.AudioData,w=globalThis.VideoFrame,S=globalThis.OffscreenCanvas,E=globalThis.RTCDataChannel,T="ReadableStream"in globalThis,p="WritableStream"in globalThis,b="TransformStream"in globalThis,A="MessageChannel"in globalThis,O="MessagePort"in globalThis,M="ArrayBuffer"in globalThis,v="AudioData"in globalThis,D="ImageBitmap"in globalThis,R="VideoFrame"in globalThis,B="OffscreenCanvas"in globalThis,V="RTCDataChannel"in globalThis,F={ReadableStreamExists:T,WritableStreamExists:p,TransformStreamExists:b,MessageChannelExists:A,MessagePortExists:O,ArrayBufferExists:M,AudioDataExists:v,ImageBitmapExists:D,VideoFrameExists:R,OffscreenCanvasExists:B,RTCDataChannelExists:V};async function P(){const n=await(async()=>{try{if(!A)return!1;const l=new MessageChannel,s={port1:l.port1},r=structuredClone(s,{transfer:[l.port1]}),t=new MessageChannel,e={port1:r.port1};await new Promise(o=>{t.port1.postMessage(e,[e.port1]),t.port1.onmessage=()=>{o()},t.port2.onmessage=({data:i})=>{t.port2.postMessage(i,[i.port1])}}),t.port1.close()}catch(l){return console.warn(l),!1}return!0})(),c=await(async()=>{try{if(!T||!p||!b)return!1;const l={readonly:new ReadableStream,writeonly:new WritableStream,tranformonly:new TransformStream},s=structuredClone(l,{transfer:[l.readonly,l.writeonly,l.tranformonly]}),r=new MessageChannel,t=s;await new Promise(e=>{r.port1.postMessage(t,[t.readonly,t.writeonly,t.tranformonly]),r.port1.onmessage=()=>{e()},r.port2.onmessage=({data:o})=>{r.port2.postMessage(o,[o.readonly,o.writeonly,o.tranformonly].filter(i=>i!==void 0))}}),r.port1.close()}catch(l){return console.warn(l),!1}return!0})();return{channel:n,streams:c}}function h(n){return typeof n=="object"&&n!==null||typeof n=="function"}function g(n){return n instanceof d||n instanceof DataView}function f(n){return T&&n instanceof ReadableStream||p&&n instanceof WritableStream||b&&n instanceof TransformStream}function m(n){return A&&n instanceof MessageChannel}function y(n){return M&&n instanceof ArrayBuffer||O&&n instanceof MessagePort||v&&n instanceof C||D&&n instanceof ImageBitmap||R&&n instanceof w||B&&n instanceof S||V&&n instanceof E}function I(n){return Array.from(new Set(n))}function L(n,c=!1,l=1e4){const s=new Set([]);let r=[],t=[n];for(;t.length>0&&l>0;){for(let e of t)if(y(e))s.add(e);else if(g(e))s.add(e.buffer);else if(m(e))s.add(e.port1),s.add(e.port2);else if(c&&f(e))s.add(e);else if(!f(e)&&h(e)){const o=Array.isArray(e)?e:Object.values(e),i=o.length;for(let u=0;u<i;u++)r.push(o[u])}t=r,r=[],l--}return Array.from(s)}function*j(n,c=!1,l=1e4){const s=new Set([]);let r=[n],t=[];for(;r.length>0&&l>0;){for(let e of r)if(!s.has(e)){if(g(e)){s.add(e);const{buffer:o}=e;if(s.has(o))continue;yield o,s.add(o)}else if(y(e))yield e,s.add(e);else if(m(e)){if(s.add(e),s.has(e.port1)||s.has(e.port2))continue;yield e.port1,yield e.port2,s.add(e.port1),s.add(e.port2)}else if(c&&f(e))yield e,s.add(e);else if(!f(e)&&h(e)){const o=Array.isArray(e)?e:Object.values(e),i=o.length;for(let u=0;u<i;u++)t.push(o[u])}}r=t,t=[],l--}return s.clear(),r=null,t=null,null}function W(n,c=!1,l=1e4){let s=[n],r=[];for(;s.length>0&&l>0;){for(let t of s){if(g(t))return!0;if(y(t))return!0;if(m(t))return!0;if(c&&f(t))return!0;if(h(t)&&!f(t)){const e=Array.isArray(t)?t:Object.values(t),o=e.length;for(let i=0;i<o;i++)r.push(e[i])}}s=r,r=[],l--}return s=null,r=null,!1}a.AVAILABLE_TRANSFERABLE_OBJECTS=F,a.AudioData=C,a.OffscreenCanvas=S,a.RTCDataChannel=E,a.TypedArray=d,a.VideoFrame=w,a.filterOutDuplicates=I,a.getTransferable=j,a.getTransferables=L,a.hasTransferables=W,a.isMessageChannel=m,a.isObject=h,a.isStream=f,a.isSupported=P,a.isTransferable=y,a.isTypedArray=g,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}); |
{ | ||
"name": "transferables", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"type": "module", | ||
@@ -149,3 +149,3 @@ "sideEffects": false, | ||
"@commitlint/config-conventional": "^17.3.0", | ||
"@playwright/test": "^1.29.0", | ||
"@playwright/test": "^1.29.1", | ||
"@semantic-release/changelog": "^6.0.2", | ||
@@ -156,5 +156,5 @@ "@semantic-release/git": "^10.0.1", | ||
"@types/offscreencanvas": "^2019.7.0", | ||
"@types/web": "^0.0.85", | ||
"@types/web": "^0.0.86", | ||
"comlink": "^4.3.1", | ||
"happy-dom": "^8.1.0", | ||
"happy-dom": "^8.1.1", | ||
"husky": "^8.0.2", | ||
@@ -164,3 +164,3 @@ "markdown-table": "^3.0.3", | ||
"micromark-extension-gfm": "^2.0.1", | ||
"pnpm": "^7.18.2", | ||
"pnpm": "^7.19.0", | ||
"pretty-bytes": "^6.0.0", | ||
@@ -170,6 +170,6 @@ "semantic-release": "^19.0.5", | ||
"ultra-runner": "^3.10.5", | ||
"vite": "^4.0.2", | ||
"vite": "^4.0.3", | ||
"vite-plugin-dts": "^1.7.1", | ||
"vitest": "^0.26.0" | ||
"vitest": "^0.26.2" | ||
} | ||
} |
@@ -7,2 +7,34 @@ export const TypedArray = Object.getPrototypeOf(Int8Array); | ||
const ReadableStreamExists = "ReadableStream" in globalThis; | ||
const WritableStreamExists = "WritableStream" in globalThis; | ||
const TransformStreamExists = "TransformStream" in globalThis; | ||
const MessageChannelExists = "MessageChannel" in globalThis; | ||
const MessagePortExists = "MessagePort" in globalThis; | ||
const ArrayBufferExists = "ArrayBuffer" in globalThis; | ||
const AudioDataExists = "AudioData" in globalThis; | ||
const ImageBitmapExists = "ImageBitmap" in globalThis; | ||
const VideoFrameExists = "VideoFrame" in globalThis; | ||
const OffscreenCanvasExists = "OffscreenCanvas" in globalThis; | ||
const RTCDataChannelExists = "RTCDataChannel" in globalThis; | ||
/** | ||
* Let's you know which transferable objects to actually exist in the js runtime the library is running in | ||
*/ | ||
export const AVAILABLE_TRANSFERABLE_OBJECTS = { | ||
ReadableStreamExists, | ||
WritableStreamExists, | ||
TransformStreamExists, | ||
MessageChannelExists, | ||
MessagePortExists, | ||
ArrayBufferExists, | ||
AudioDataExists, | ||
ImageBitmapExists, | ||
VideoFrameExists, | ||
OffscreenCanvasExists, | ||
RTCDataChannelExists, | ||
} | ||
export type TypeTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; | ||
@@ -17,2 +49,5 @@ export type TypeTransferable = ArrayBuffer | MessagePort | ReadableStream | WritableStream | TransformStream /* | typeof AudioData */ | ImageBitmap /* | typeof VideoFrame */ | OffscreenCanvas | RTCDataChannel; | ||
try { | ||
if (!MessageChannelExists) | ||
return false; | ||
const msgChanl = new MessageChannel(); | ||
@@ -52,2 +87,5 @@ const obj = { port1: msgChanl.port1 } | ||
try { | ||
if (!ReadableStreamExists || !WritableStreamExists || !TransformStreamExists) | ||
return false; | ||
const streams = { | ||
@@ -58,2 +96,3 @@ readonly: new ReadableStream(), | ||
} | ||
const clonedObj = structuredClone(streams, { | ||
@@ -83,3 +122,3 @@ transfer: [ | ||
data.tranformonly as unknown as Transferable, | ||
].filter(x => x != undefined)); | ||
].filter(x => x !== undefined)); | ||
} | ||
@@ -122,5 +161,5 @@ }) | ||
return ( | ||
("ReadableStream" in globalThis && obj instanceof ReadableStream) || | ||
("WritableStream" in globalThis && obj instanceof WritableStream) || | ||
("TransformStream" in globalThis && obj instanceof TransformStream) | ||
(ReadableStreamExists && obj instanceof ReadableStream) || | ||
(WritableStreamExists && obj instanceof WritableStream) || | ||
(TransformStreamExists && obj instanceof TransformStream) | ||
); | ||
@@ -134,3 +173,3 @@ } | ||
return ( | ||
("MessageChannel" in globalThis && obj instanceof MessageChannel) | ||
(MessageChannelExists && obj instanceof MessageChannel) | ||
); | ||
@@ -144,9 +183,9 @@ } | ||
return ( | ||
("ArrayBuffer" in globalThis && obj instanceof ArrayBuffer) || | ||
("MessagePort" in globalThis && obj instanceof MessagePort) || | ||
("AudioData" in globalThis && obj instanceof AudioData) || | ||
("ImageBitmap" in globalThis && obj instanceof ImageBitmap) || | ||
("VideoFrame" in globalThis && obj instanceof VideoFrame) || | ||
("OffscreenCanvas" in globalThis && obj instanceof OffscreenCanvas) || | ||
("RTCDataChannel" in globalThis && obj instanceof RTCDataChannel) | ||
(ArrayBufferExists && obj instanceof ArrayBuffer) || | ||
(MessagePortExists && obj instanceof MessagePort) || | ||
(AudioDataExists && obj instanceof AudioData) || | ||
(ImageBitmapExists && obj instanceof ImageBitmap) || | ||
(VideoFrameExists && obj instanceof VideoFrame) || | ||
(OffscreenCanvasExists && obj instanceof OffscreenCanvas) || | ||
(RTCDataChannelExists && obj instanceof RTCDataChannel) | ||
); | ||
@@ -179,6 +218,6 @@ } | ||
for (let item of queue) { | ||
if (isTypedArray(item)) { | ||
if (isTransferable(item)) { | ||
result.add(item); | ||
} else if (isTypedArray(item)) { | ||
result.add(item.buffer); | ||
} else if (isTransferable(item)) { | ||
result.add(item); | ||
} else if (isMessageChannel(item)) { | ||
@@ -262,3 +301,3 @@ result.add(item.port1); | ||
else if (!isStream(item) && isObject(item)) { | ||
const values = (Array.isArray(item) ? item : Object.values(item)); | ||
const values = Array.isArray(item) ? item : Object.values(item); | ||
const len = values.length; | ||
@@ -265,0 +304,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54684
697