V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
whiteball
V2EX  ›  JavaScript

[已解决] 将二进制文件传到七牛云, fetch 下来却异常?

  •  
  •   whiteball · 2017-07-03 04:04:55 +08:00 · 5580 次点击
    这是一个创建于 2682 天前的主题,其中的信息可能已经有所发展或是发生改变。

    发生什么事情了?

    小弟今天学习webassmbly遇到了这样一件事情。

    我在把我 弄好wasm模块文件上传到七牛云,再在本地通过fetch来获取这个wasm模块文件进行使用的时候,总是报告编译错误,如下

    CompileError: WasmCompile: Compiling WASM function #32 failed:: return[0] expected type i32, found f32.load of type f32 @+3
    

    后来通过断点发现是fetch下来的wasm模块文件有问题,内部的二进制编码变乱了,而且添加了额外的内容,甚至大小从原本的147byte,变成了19Kb

    // 环境检测
    var root = this,
        isBrowserSide = false;
    if (typeof window !== "undefined" && root === window) {
        isBrowserSide = true;
    }
    
    // 获取 wasm 并且导入
    function fetchAndInstantiateWasm(url, imports) {
        return fetch(url) // url could be your .wasm file
            .then(res => {
                if (res.ok) {
                    if (this.isBrowserSide) return res.arrayBuffer();
                    else return res.buffer();
                }
                throw new Error(`Unable to fetch Web Assembly file ${url}.`);
            })
            .then(bytes => {
          		// --- 测试用 START ---
          
                console.log(bytes) // 在此打印出加载的文件的 bytes 信息
                // 当使用本地文件时 // `bytes`为长度为 147 的二进制数组,并且顺利执行程序。
                // 而使用上传文件时 // `bytes`为长度为 18670 的二进制数组,并且在下一步`WebAssembly.compile(bytes)`报错,
                
                // --- 测试用 END ---
                return WebAssembly.compile(bytes);
            })
            .then(module => WebAssembly.instantiate(module, imports || {}))
            .then(instance => instance.exports);
    }
    
    // 调用函数
    fetchAndInstantiateWasm("http://osgc6x3up.bkt.clouddn.com/test.wasm")
        .then(ex => {
            console.log(ex.add(1, 2))
        });
    

    本地文件 test.wasm(147 byte)

    00000000: 00 61 73 6d 01 00 00 00  01 0a 02 60 00 00 60 02     .asm.......`..`.
    00000010: 7f 7f 01 7f 03 03 02 00  01 05 03 01 00 01 06 0b     ................
    00000020: 02 7f 01 41 00 0b 7f 01  41 00 0b 07 10 02 03 61     ...A....A......a
    00000030: 64 64 00 01 06 6d 65 6d  6f 72 79 02 00 08 01 00     dd...memory.....
    00000040: 0a 11 02 06 00 23 00 24  01 0b 08 00 20 00 20 01     .....#.$........
    00000050: 6a 0f 0b 0b 0e 01 00 41  08 0b 08 00 00 00 00 00     j......A........
    00000060: 00 00 00 00 2e 04 6e 61  6d 65 01 1a 02 00 12 5f     ......name....._
    00000070: 5f 57 41 53 4d 5f 49 4e  49 54 49 41 4c 49 5a 45     _WASM_INITIALIZE
    00000080: 52 01 03 61 64 64 02 0b  02 00 00 01 02 00 01 61     R..add.........a
    00000090: 01 01 62 ......
    ...
    ...
    Total 18670 Byte
    

    通过浏览器下载的文件 test.wasm(有省略) (19kb)

    00000000: 00 61 73 6d 01 00 00 00  01 3d 0a 60 03 7f 7f 7f     .asm.....=.`....
    00000010: 01 7f 60 02 7f 7f 01 7f  60 01 7f 01 7f 60 02 7f     ..`.....`....`..
    00000020: 7f 00 60 04 7f 7f 7f 7f  01 7f 60 05 7f 7f 7f 7f     ..`.......`.....
    00000030: 7f 01 7f 60 03 7f 7f 7f  00 60 00 00 60 01 7f 00     ...`.....`..`...
    00000040: 60 03 7d 7d 7d 01 7f 03  26 25 00 00 00 01 00 01     `.}}}...&%......
    00000050: 02 01 03 00 00 00 00 00  04 05 04 00 01 02 02 02     ................
    00000060: 01 02 01 02 06 07 02 08  09 01 02 02 09 01 08 05     ................
    00000070: 03 01 00 01 06 0b 02 7f  01 41 00 0b 7f 01 41 00     .........A....A.
    00000080: 0b 07 97 04 1f 06 6d 65  6d 6f 72 79 02 00 06 6d     ......memory...m
    00000090: 65 6d 63 6d 70 00 00 06  6d 65 6d 63 70 79 00 01     emcmp...memcpy..
    000000a0: 06 6d 65 6d 73 65 74 00  02 0d 63 72 65 61 74 65     .memset...create
    000000b0: 5f 6d 73 70 61 63 65 00  03 17 63 72 65 61 74 65     _mspace...create
    000000c0: 5f 6d 73 70 61 63 65 5f  77 69 74 68 5f 62 61 73     _mspace_with_bas
    000000d0: 65 00 04 19 6d 73 70 61  63 65 5f 74 72 61 63 6b     e...mspace_track
    000000e0: 5f 6c 61 72 67 65 5f 63  68 75 6e 6b 73 00 05 0e     _large_chunks...
    000000f0: 64 65 73 74 72 6f 79 5f  6d 73 70 61 63 65 00 06     destroy_mspace..
    00000100: 0d 6d 73 70 61 63 65 5f  6d 61 6c 6c 6f 63 00 07     .mspace_malloc..
    00000110: 0b 6d 73 70 61 63 65 5f  66 72 65 65 00 08 0d 6d     .mspace_free...m
    00000120: 73 70 61 63 65 5f 63 61  6c 6c 6f 63 00 09 0e 6d     space_calloc...m
    00000130: 73 70 61 63 65 5f 72 65  61 6c 6c 6f 63 00 0a 17     space_realloc...
    00000140: 6d 73 70 61 63 65 5f 72  65 61 6c 6c 6f 63 5f 69     mspace_realloc_i
    00000150: 6e 5f 70 6c 61 63 65 00  0c 0f 6d 73 70 61 63 65     n_place...mspace
    00000160: 5f 6d 65 6d 61 6c 69 67  6e 00 0d 19 6d 73 70 61     _memalign...mspa
    00000170: 63 65 5f 69 6e 64 65 70  65 6e 64 65 6e 74 5f 63     ce_independent_c
    00000180: 61 6c 6c 6f 63
    

    十年后

    不,并没有十年后,但也是费劲了小弟的一番努力才在写到这的时候,我突然有了头绪。

    因为一直都是上传同一个名字的本地文件test.wasm,覆盖的也是七牛云空间上的test.wasm,而正巧之前第一次传入的是一个没有弄好的 19kb 的test.wasm,也就因此造成了这个问题。

    简单来说——没有覆盖成功。于是小弟以新的文件名再此上传试了试,竟然就成功了!

    开心的诡异

    但是为什么没有覆盖成功呐,

    小弟确实按照文档来进行了覆盖上传的操作了啊。

    // ...other things above...
    var overwrite = true;
    
    let options = {};
    if (overwrite) {
    	options.scope = bucket + ":" + fileName
    } else {
    	options.scope = bucket
    }
    
    var putPolicy = new qiniu.rs.PutPolicy(options);
    // ...other things following...
    

    而且在七牛云的工作台上,文件的时间和大小在进行覆盖上传之后也都发生了变化。

    工作台

    就很气

    在之后的测试中

    在后来的测试中,发现还是无法覆盖上传成功。

    就算在控制台中下载文件能够下载到新上传的文件

    下载

    但是通过外链下载的文件仍然是第一次上传的文件

    外联

    就如下图所示

    不一样!

    我的心情是

    坑爹啊这是

    BTW,已交工单。

    16 条回复    2017-07-03 22:51:57 +08:00
    mkeith
        1
    mkeith  
       2017-07-03 04:11:19 +08:00 via iPhone   ❤️ 1
    CDN 没更新?
    whiteball
        2
    whiteball  
    OP
       2017-07-03 04:20:38 +08:00
    @mkeith 这个怎么操作=。=
    whiteball
        3
    whiteball  
    OP
       2017-07-03 04:20:47 +08:00
    ```
    116.24.235.38 MISS 186 [02/Jul/2017:16:18:32 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19676 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 MISS 140 [02/Jul/2017:16:18:52 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19680 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 83 [02/Jul/2017:16:18:53 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19635 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 57 [02/Jul/2017:16:58:41 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19680 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 210 [02/Jul/2017:16:18:32 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19715 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 MISS 283 [02/Jul/2017:16:18:20 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm?imageView2/2/w/308/h/210/interlace/1/q/100 HTTP/1.1" 400 936 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 4 [02/Jul/2017:16:57:49 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19684 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 20 [02/Jul/2017:16:56:52 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19650 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 295 [02/Jul/2017:16:18:19 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm?imageView2/2/w/308/h/210/interlace/1/q/100 HTTP/1.1" 400 973 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 15 [02/Jul/2017:16:58:42 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19611 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 162 [02/Jul/2017:17:06:06 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm?imageView2/2/w/308/h/210/interlace/1/q/100 HTTP/1.1" 400 909 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 MISS 183 [02/Jul/2017:17:06:16 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm?imageView2/2/w/308/h/210/interlace/1/q/100 HTTP/1.1" 400 888 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 11 [02/Jul/2017:17:33:52 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19684 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 0 [02/Jul/2017:17:06:19 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 206 19150 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 7 [02/Jul/2017:17:23:04 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19684 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 150 [02/Jul/2017:17:16:36 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wast HTTP/1.1" 200 1602 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 MISS 189 [02/Jul/2017:17:16:36 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wast HTTP/1.1" 200 1647 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 55 [02/Jul/2017:17:08:05 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19652 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 145 [02/Jul/2017:17:06:16 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm?imageView2/2/w/308/h/210/interlace/1/q/100 HTTP/1.1" 400 844 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 51 [02/Jul/2017:17:09:01 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19722 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 62 [02/Jul/2017:17:05:01 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19650 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 0 [02/Jul/2017:17:06:19 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 206 1026 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    116.24.235.38 HIT 130 [02/Jul/2017:17:08:05 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19721 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 0 [02/Jul/2017:17:05:53 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19684 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 0 [02/Jul/2017:17:14:13 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19650 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 49 [02/Jul/2017:17:15:32 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19652 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 41 [02/Jul/2017:17:06:54 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19638 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 0 [02/Jul/2017:17:09:01 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19652 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 72 [02/Jul/2017:17:15:31 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19723 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 13 [02/Jul/2017:17:11:52 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19650 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 HIT 21 [02/Jul/2017:17:32:43 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm HTTP/1.1" 200 19682 "-" "node-fetch/1.0+(+https://github.com/bitinn/node-fetch)"
    116.24.235.38 MISS 237 [02/Jul/2017:17:06:06 +0800] "GET http://osgc6x3up.bkt.clouddn.com/test.wasm?imageView2/2/w/308/h/210/interlace/1/q/100 HTTP/1.1" 400 953 "-" "Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_12_1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/59.0.3071.115+Safari/537.36"
    ```
    CDN 的日志
    ovear
        4
    ovear  
       2017-07-03 04:22:12 +08:00 via Android   ❤️ 1
    在后台刷新缓存
    whiteball
        5
    whiteball  
    OP
       2017-07-03 04:30:34 +08:00
    @ovear
    不是很懂,是这样么...不过弄完还是不行诶。
    ![是不是回复里面就不能用图片了=。=]( http://7xp7u3.com1.z0.glb.clouddn.com/o_1bk2frlg91mu41bsg1ge0gvv1sr7f.png)
    libook
        6
    libook  
       2017-07-03 09:23:18 +08:00 via Android   ❤️ 1
    Cdn 存在的意义之一 , 就是能让任何地区的任何量级的用户 都能流畅地访问到资源, 所以 cdn 在很多地区都会有大量的分布式节点服务器。覆盖老文件,cdn 需要同步这些节点, 受限于速度、成本和任务量等因素,cdn 通常难以做到及时更新。七牛官方的建议是尽可能不修改已经上传的文件,但如果一定要修改,就要等待七牛每七日一次的节点同步,或者手动去七牛后台刷新,刷新文件数量和频率都是有限制的。所以 cdn 的最佳实践是,修改文件要同时修改文件名(如 abc-1.0.bin ),以新文件的形式上传 cdn,客户端动态从应用服务器上获取最新的 url,访问七牛 cdn 上的最新上传的文件。
    另外七牛上也可以配置客户端缓存的时长,如果是浏览器或任何严格遵循 http 标准的客户端都会按照数据头的缓存时常进行本地缓存。
    xmadi
        7
    xmadi  
       2017-07-03 09:50:47 +08:00 via iPhone   ❤️ 1
    cdn 就是这样的 缓存很多 每次上传改名字吧
    whiteball
        8
    whiteball  
    OP
       2017-07-03 11:42:18 +08:00
    @libook 谢谢,学习了!
    whiteball
        9
    whiteball  
    OP
       2017-07-03 11:42:55 +08:00
    @xmadi 嗯,果然不能偷懒呐。
    indooorsman
        10
    indooorsman  
       2017-07-03 11:45:53 +08:00 via Android   ❤️ 1
    看到标题第一反应就是没刷新 cdn 缓存…
    七牛提供刷新缓存的接口,也有 web 界面可以刷新的

    每次上传改名是个好主意
    whiteball
        11
    whiteball  
    OP
       2017-07-03 11:53:05 +08:00
    @indooorsman
    老司机就是有经验啊。
    =,=突然感觉有点丢人...果然这锅还是得自己来背。
    文档写得也挺清楚的: https://developer.qiniu.com/fusion/kb/1325/refresh-the-cache-and-the-effect-of-time
    codingkiller
        12
    codingkiller  
       2017-07-03 12:05:55 +08:00
    CDN,基本功。
    anyele
        13
    anyele  
       2017-07-03 12:12:01 +08:00 via Android
    十年后
    SourceMan
        14
    SourceMan  
       2017-07-03 12:32:02 +08:00 via iPhone
    哈哈哈,缓存问题
    ty89
        15
    ty89  
       2017-07-03 13:38:05 +08:00
    废话写了一大堆
    所以楼主你 http 的基础知识是看 a 片学的吗

    10 年后:

    《震惊!修改了 css 文件后显示效果竟然没有丝毫变化,原因让所有人不瞪口呆...》
    mingyun
        16
    mingyun  
       2017-07-03 22:51:57 +08:00
    已解决 v2 可编辑了?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1778 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:34 · PVG 00:34 · LAX 08:34 · JFK 11:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.