https://github.com/kumavis/iframe-stream/blob/master/test/rebundle.js

iframe-stream-其实就是将iframe打包成流

Create a stream around an iframe via post-message-stream

const IframeStream = require('iframe-stream').IframeStream

var iframe = createIframe()
var iframeStream = IframeStream(iframe)

Note

  • Setup the stream immediately, so we don't miss the iframe's load event.
  • The IframeStream will buffer all input until the childstream is ready.
  • This is an object stream, and buffers will not be handled gracefully.

Example

Here is an example using dnode to create a callback based API with a javascript context inside an iframe.

Parent:

const IframeStream = require('iframe-stream').IframeStream
const Dnode = require('dnode') var iframe = createIframe()
var iframeStream = IframeStream(iframe)
var dnode = Dnode()
dnode.on('remote', function(child){
child.doAsyncSomething(function(){
console.log('child finished doing the thing!')
})
}) dnode.pipe(iframeStream).pipe(dnode)

Child:

const ParentStream = require('iframe-stream').ParentStream
const Dnode = require('dnode') var parentStream = ParentStream(iframe)
var dnode = Dnode({
doAsyncSomething: function (cb) {
console.log('doing something async...')
cb()
}
}) dnode.pipe(parentStream).pipe(dnode)
 
源码实现:
var PostMessageStream = require('post-message-stream')

/*
IframeStream starts corked until it gets a message from the iframe
ParentStream starts uncorked
*/ module.exports = {
IframeStream: IframeStream,
ParentStream: ParentStream,
} function IframeStream(iframe) {
if (this instanceof IframeStream) throw Error('IframeStream - Dont construct via the "new" keyword.')
return new PostMessageStream({
name: 'iframe-parent',
target: 'iframe-child',
targetWindow: iframe.contentWindow,
})
} //
// Parent Stream
// function ParentStream() {
if (this instanceof ParentStream) throw Error('ParentStream - Dont construct via the "new" keyword.')
return new PostMessageStream({
name: 'iframe-child',
target: 'iframe-parent',
targetWindow: frames.parent,
})
}
 
下面实例实现:
先将插件安装下来:
npm install iframe --save
npm install iframe-stream --save
npm install dnode --save
 
index.js
const test = require('tape')
const Iframe = require('iframe')
const from = require('from')
const pipe = require('mississippi').pipe
const IframeStream = require('../').IframeStream
const rebundle = require('./rebundle')
const htmlWrap = require('./htmlWrap')
const iframeContent = htmlWrap(rebundle(require('./frame'))) // setup iframe
var iframe = Iframe({
body: iframeContent,
container: document.body,
}).iframe
var iframeStream = IframeStream(iframe) test(function(t) {
var data = [, , , , , , , , ] t.plan(data.length) pipe(
from(data),
iframeStream
) // create a test for each datum
// this is because these streams never close
eachOutput(iframeStream, data.map(function(datum){
return function onResult(result){ t.equal(result, datum * , 'datum was doubled correctly') }
}), function onDone(){
t.end()
}) }) function eachOutput(stream, handlers, cb) {
var index =
if (!handlers.length) return cb()
stream.once('data', handleChunk)
function handleChunk(data){
var fn = handlers[index]
fn(data)
index++
if (!handlers[index]) return cb()
stream.once('data', handleChunk)
}
}

htmlWrap.js

module.exports = htmlWrap

function htmlWrap(src){
return '<'+'script type="text/javascript"'+'>'+src+'<'+'/script'+'>'
}

rebundle.js

//
// Extraced from WebWorkify
// https://github.com/substack/webworkify/blob/master/index.js
// var bundleFn = arguments[];
var sources = arguments[];
var cache = arguments[]; var stringify = JSON.stringify; module.exports = function (fn, options) {
var wkey;
var cacheKeys = Object.keys(cache); for (var i = , l = cacheKeys.length; i < l; i++) {
var key = cacheKeys[i];
var exp = cache[key].exports;
// Using babel as a transpiler to use esmodule, the export will always
// be an object with the default export as a property of it. To ensure
// the existing api and babel esmodule exports are both supported we
// check for both
if (exp === fn || exp && exp.default === fn) {
wkey = key;
break;
}
} if (!wkey) {
wkey = Math.floor(Math.pow(, ) * Math.random()).toString();
var wcache = {};
for (var i = , l = cacheKeys.length; i < l; i++) {
var key = cacheKeys[i];
wcache[key] = key;
}
sources[wkey] = [
Function(['require','module','exports'], '(' + fn + ')(self)'),
wcache
];
}
var skey = Math.floor(Math.pow(, ) * Math.random()).toString(); var scache = {}; scache[wkey] = wkey;
sources[skey] = [
Function(['require'], (
// try to call default if defined to also support babel esmodule
// exports
'var f = require(' + stringify(wkey) + ');' +
'(f.default ? f.default : f)(self);'
)),
scache
]; var workerSources = {};
resolveSources(skey); function resolveSources(key) {
workerSources[key] = true; for (var depPath in sources[key][]) {
var depKey = sources[key][][depPath];
if (!workerSources[depKey]) {
resolveSources(depKey);
}
}
} var src = '(' + bundleFn + ')({'
+ Object.keys(workerSources).map(function (key) {
return stringify(key) + ':['
+ sources[key][]
+ ',' + stringify(sources[key][]) + ']'
;
}).join(',')
+ '},{},[' + stringify(skey) + '])'
; return src
}

frame.js

const BrowserStdout = require('browser-stdout')
const ParentStream = require('../index.js').ParentStream
const transform = require('mississippi').through
const pipe = require('mississippi').pipe module.exports = setupStream setupStream() function setupStream(opts) {
var iframeStream = ParentStream()
pipe(
iframeStream,
transform({ objectMode: true }, doubleIt),
iframeStream
)
} function doubleIt(chunk, encoding, cb){
cb(null, chunk * )
}

上面的例子是iframe-stream作者写的一个测试test

metamask-iframe-stream,没成功的更多相关文章

  1. LODOP判断没成功发送任务-重打一下

    一般情况下打印执行了PRINT()或PRINTA(),就会加入打印机队列,如果打印机脱机,就会在队列里排队,当打印机连上并取消脱机的时候,正在排队的任务就会打出,所以一般建议用是否加入队列来判断打印成 ...

  2. 宝塔https部署没成功的原因排查

    今天ytkah在迁移一个客户网站的时候出了点问题,网站从旧的服务器(windows)换到新的服务器(阿里云centos 7,已经安装了宝塔面板),网站之前有用comodo的ssl证书,因为快要过期了, ...

  3. Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功 - -!)

                              A complete click-by-click, step-by-step video of this article is available ...

  4. [archlinux][plasma][screensaver] plasma5配置屏保程序,没成功(-_-#)

    plamsa用了好久,一直没有屏保.我想要玄酷的屏保! 用xscreensaver, 之前用FVWM2的时候,就用过了,很玄酷. 一,安装 pacman -S xscreensaver 二,配置 xs ...

  5. Ubuntu 16.04安装Mac OS 12虚拟机资源(没成功,但资源还是可以用)

    整理的Mac OS 12虚拟机资源.装虚拟机基本是按这样的套路: 1.先装VM 2.破解VM使其支持Mac OS 12,这个脚本基本是全平台支持,可以看里面的教程文档. 3.用镜像安装系统. 资源: ...

  6. MetaMask/zero-client

    https://github.com/MetaMask/zero-client MetaMask ZeroClient and backing iframe service architecture ...

  7. iframe高度自动随着子页面的高度变化而变化(不止要在iframe标签里加上this.height=this.contentWindow.document.body.scrollHeight)

    最近使用iframe整合页面遇到一些难题,走了很多弯路才解决,借此记录一下: 1 <!-- 页面主体内容 --> 2 <div class="iframe-wrapper& ...

  8. 原生XMLHTTPResponse,jQuery-Ajax 上传文件;iframe上传图片&预览;图片验证码小案例

    原生AJAX Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件) 1.Xml ...

  9. iframe编程的一些问题

    前几天做一个用iframe显示曲线图的demo,发现对iframe的contentDocument绑定 onclick事件都无效,而在页面中对iframe.contentDocument的onclic ...

随机推荐

  1. java多线程框架

    JDK5中的一个亮点就是将Doug Lea的并发库引入到Java标准库中.Doug Lea确实是一个牛人,能教书,能出书,能编码,不过这在国外还是比较普遍的,而国内的教授们就相差太远了. 一般的服务器 ...

  2. python 系列文章汇总(持续更新...)

    引言 不知不觉已经写了好几篇 python 相关的随笔了,从刚开始的门外汉到现在已经对 python 有一些入门了,时间也已经过去了一个多月. 写博客真是好处多多,不仅能提供整理自己学习的知识点,梳理 ...

  3. PHP文件上传,下载,Sql工具类!

    PHP文件上传,下载,Sql工具类! 对文件大小,文件类型 同名覆盖 中文转码的操作,可直接使用 前台 upload.html <!DOCTYPE html> <html> & ...

  4. JSz中的静态方法和实例方法的分析

    我又回来了,最近忙着喝枸杞,没来写博客感觉很有负罪感,今晚我来写一点小小的知识点 可能我们在用形如Array.of()的方法时会产生一些疑问,为什么我们能不实例化直接使用Array上的of()方法呢, ...

  5. java中System.currentTimeMillis()

    System.curentTimeMillis();会产生一个当前的毫秒. 1.计算某个方法的耗时 long curTime = System.currentTimeMillis(); resourc ...

  6. postman获取全局

    1.获取token接口时,在test里面输入如下语句 postman.clearGlobalVariable("token"); var jsonData = JSON.parse ...

  7. 语义SLAM的数据关联和语义定位(三)

    与现有方法的异同 特征点SLAM中的数据关联 先回忆一下特征点SLAM中,我们是如何处理数据关联的.下面以ORBSLAM为例. 在初始化部分,我们通过特征描述子的相似性,建立两帧之间的特征点关联,然后 ...

  8. Bootstrap源码分析系列之核心CSS

    本节主要介绍核心CSS,从整体架构中的7个Less文件对应的源码分别进行分析 scaffolding.less 这个文件编译后的css文件(886~989行)其作用就像定义全局样式. //调整css盒 ...

  9. go语言练习:接口

    package main import ( "fmt" ) type Run interface { //这个接口的名字命名成Car更直观一点,除了distance方法外,后面可以 ...

  10. IIS日志导致磁盘被占满

    某服务器只部署了个IIS,应用目录都在D盘,可C盘97.5GB空间却被占满了. 将系统文件,隐藏文件全部显示,再选中所有的C盘文件及文件夹查看容量只有19GB. 既然只部署了IIS,那自然就怀疑到了I ...