{"id":6054,"date":"2018-06-20T18:03:28","date_gmt":"2018-06-20T09:03:28","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=6054"},"modified":"2019-05-28T12:49:19","modified_gmt":"2019-05-28T03:49:19","slug":"fetch-api%e3%81%aestream%e6%a9%9f%e8%83%bd%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e8%aa%ad%e3%81%bf%e8%be%bc%e3%81%bf%e3%81%aa%e3%81%8c%e3%82%89%e5%9c%b0%e5%9b%b3","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/javascript\/6054","title":{"rendered":"Fetch API\u306eStream\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u307f\u306a\u304c\u3089\u5730\u56f3\u3092\u63cf\u753b\u3059\u308b"},"content":{"rendered":"<p>\u5730\u7406\u7a7a\u9593\u30c7\u30fc\u30bf\u306f\u3069\u3046\u3057\u3066\u3082\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u304c\u5927\u304d\u304f\u306a\u308a\u304c\u3061\u3067\u3059\u3002<br \/>\n\u901a\u5e38\u306e\u30c7\u30fc\u30bf\u8aad\u307f\u8fbc\u307f\u3067\u306f\u3001\u8aad\u307f\u8fbc\u307f\u304c\u7d42\u308f\u308b\u307e\u3067\u5730\u56f3\u306e\u63cf\u753b\u3092\u59cb\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u306e\u3067\u3059\u304c\u3001Fetch API\u306eReadableStream\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u300c\u4e00\u90e8\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3093\u3067\u306f\u5730\u56f3\u306e\u4e00\u90e8\u5206\u3092\u63cf\u753b\u3059\u308b\u300d\u3068\u3044\u3046\u5206\u5272\u3057\u305f\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h3>\n<p><a href=\"http:\/\/shimz.me\/example\/js\/fetch\/streaming_canvas\/\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2018\/06\/geodataFeachStream.png\" alt=\"GeoData Feach Stream\" width=\"1845\" height=\"1505\" class=\"aligncenter size-full wp-image-6119 border\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2018\/06\/geodataFeachStream.png 1845w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2018\/06\/geodataFeachStream-184x150.png 184w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2018\/06\/geodataFeachStream-768x626.png 768w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2018\/06\/geodataFeachStream-1024x835.png 1024w\" sizes=\"auto, (max-width: 1845px) 100vw, 1845px\" \/><\/a><\/p>\n<p>\u7d0450MB\u306e\u30dd\u30ea\u30b4\u30f3\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u307f\u306a\u304c\u3089\u9010\u6b21\u63cf\u753b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/shimz.me\/example\/js\/fetch\/streaming_canvas\/\">DEMO<\/a><\/p>\n<pre class=\"lang:js decode:true \" >\n\/\/\u5730\u7406\u30c7\u30fc\u30bf\u3092stream\u3092\u4f7f\u3063\u3066\u8aad\u307f\u8fbc\u3080\nfetch(\"city.txt\").then((response) =&gt; {    \n    const reader = response.body.getReader();\n    const stream = new ReadableStream({\n        start(controller) {\n            function push() {\n                reader.read().then(({ done, value }) =&gt; {\n                    if (done) {            \n                        controller.close();                        \n                        draw(\"\");\n                        console.log(\"Stream End\")\n                        return;\n                    }\n          \n                  \n                    const chunk = new TextDecoder(\"utf-8\").decode(value);\n                    draw(chunk);\n                    controller.enqueue(value);\n                    push();\n                });\n            };          \n            push();\n        }\n    });\n\n    return new Response(stream, { headers: { \"Content-Type\": \"text\/html\" } });\n});\n\n\/\/chank\u3092\u6e9c\u3081\u3068\u304f\u30d0\u30c3\u30d5\u30a1\nlet buffer = \"\";\n\n\/\/\u5730\u56f3\u3092\u63cf\u753b\u3059\u308b\u30ad\u30e3\u30f3\u30d0\u30b9    \nconst canvas = d3.select(\"canvas\")\nconst ctx = canvas.node().getContext('2d');\n\n\/\/\u6295\u5f71\u6cd5\u306e\u8a2d\u5b9a\nconst projection = d3.geoMercator()\n    .scale(4000) \n    .translate([960,500*2])\n    .center([139.0032936, 36.3219088]); \n    \n\/\/\u30d1\u30b9\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u30fc\u3092\u4f5c\u6210        \nconst  geoPath = d3.geoPath(projection)\n  .context(ctx);\n\n\n\/\/\u5730\u56f3\u63cf\u753b\nfunction draw(chunk){\n  buffer += chunk; \n  \n  \/\/\u6539\u884c\u30b3\u30fc\u30c9\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u6b21\u306b\u56de\u3059\n  if(buffer.indexOf(\"n\") &lt; 1) return; \n  \n  \/\/\u6539\u884c\u30b3\u30fc\u30c9\u3092\u5143\u306b\u30d0\u30c3\u30d5\u30a1\u3092\u5206\u5272\n  const block = buffer.split(\"n\");\n  \n   \/\/\u5206\u5272\u3057\u305f\u30c7\u30fc\u30bf\u306e\u672b\u5c3e\u3092\u53d6\u308a\u51fa\u3057\u3002\n  const last = block.pop();\n  \n  \/\/\u5206\u5272\u3057\u305f\u30d0\u30c3\u30d5\u30a1\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\n  const jsons = block.map(JSON.parse);\n  \n  \/\/\u672b\u5c3e\u30c7\u30fc\u30bf\u306b\u6539\u884c\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\n  if(last.indexOf(\"n\") &lt; 0){\n    buffer = last; \/\/\u6539\u884c\u30b3\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u306f\u6b21\u306b\u56de\u3059\n  }else{\n    if(last) jsons.push(JSON.parse(last)); \/\/\u6539\u884c\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\n  }\n  \n  \n  \/\/\u5909\u63db\u3057\u305fJ\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066canvas\u306b\u5730\u56f3\u3092\u63cf\u753b\u3059\u308b\n  jsons.forEach(d =&gt; {\n    ctx.beginPath();\n    geoPath(d);\n    ctx.stroke();\n    ctx.closePath();\n  });\n  \n\n}<\/pre>\n<h4>\u30d6\u30e9\u30a6\u30b6\u5bfe\u5fdc<\/h4>\n<p>\u63a8\u5968\u74b0\u5883<\/p>\n<ul>\n<li>\u6700\u65b0\u7248 Chrome<\/li>\n<li>\u6700\u65b0\u7248 Safari Mobiled<\/li>\n<\/ul>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Fetch_API#Browser_compatibility\">Fetch API &#45; Web API \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9 &#124; MDN<\/a><\/p>\n<p><del datetime=\"2019-05-28T03:48:32+00:00\">\u3068\u308a\u3042\u3048\u305a\u3001IE\u3001Firefox\u3067\u306f\u52d5\u304d\u307e\u305b\u3093\u3002IE\u306f\u305d\u3082\u305d\u3082Fetch API\u306b\u672a\u5bfe\u5fdc\u3001Firefox\u306fReadableStream\u306eResponse body\u304c\u672a\u30b5\u30dd\u30fc\u30c8\u306e\u305f\u3081\u3067\u3059\u3002<\/del><br \/>\n* \u6700\u65b0\u7248\u306eFirefox\u3067\u306f\u52d5\u4f5c\u3059\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<br \/>\nchrome\u306f\u554f\u984c\u7121\u3057\u3002\u307e\u305f\u3001MDN\u306e\u30da\u30fc\u30b8\u3067\u306fSafari, Safari Mobile\u304c\u672a\u30b5\u30dd\u30fc\u30c8\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u624b\u5143\u306e\u74b0\u5883\uff08Safari 11.1 \/ iOS 11.3.1\uff09\u3067\u78ba\u8a8d\u3057\u305f\u3068\u3053\u308d\u3069\u3061\u3089\u3082\u554f\u984c\u306a\u304f\u52d5\u4f5c\u3057\u307e\u3057\u305f\u3002<\/p>\n<h3>\u4e00\u90e8\u89e3\u8aac<\/h3>\n<h4>GeoJSON\u30d5\u30a1\u30a4\u30eb\u306e\u5206\u5272<\/h4>\n<p>\u30b9\u30c8\u30ea\u30fc\u30e0\u3067\u4f7f\u3044\u3084\u3059\u3044\u3088\u3046\u306b\u3001GeoJSON\u3092features\u5358\u4f4d\u3067\u5206\u5272\u3057\u4e00\u884c\u3054\u3068\u306eJSON\u3068\u3057\u3066\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"geojson2txt.js\" >const fs = require('fs');\nconst converter =  require('json-2-csv');\n\n\nconst file = process.argv[2];\n\n\nconst geojson = JSON.parse(fs.readFileSync(file, 'utf8')); \n\n\nlet features = [];\n\nconst len = geojson.features.length;\nfor(let i=0; i &lt; len; i++){\n    features.push(JSON.stringify(geojson.features[i]))\n}\n\nconsole.log(features.join(\"n\"));\n<\/pre>\n<pre class=\"lang:sh decode:true \" >node geojson2txt.js japan_ver81.geojson &gt; japan.txt<\/pre>\n<h4>Streeming API\u3067\u8aad\u307f\u8fbc\u3080<\/h4>\n<p>Stream\u304b\u3089\u53d7\u3051\u3063\u3068\u305fchunk\u3092buffer\u306b\u6e9c\u3081\u3001\u6539\u884c\u30b3\u30fc\u30c9\u3092\u76ee\u5370\u306b\u5206\u5272\u3057\u3066JSON\u3092\u8aad\u307f\u8fbc\u307f\u5143\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/ReadableStream\">ReadableStream &#45; Web APIs &#124; MDN<\/a><\/p>\n<pre class=\"lang:js decode:true \" >fetch(\"japan.txt\").then(response =&gt; {\n    \n  const reader = response.body.getReader();\n  const stream = new ReadableStream({\n    start(controller) {\n      function push() {\n          reader.read().then(({ done, value }) =&gt; {\n              if (done) {            \n                  controller.close();                        \n                  draw(\"\");\n                  console.log(\"Stream End\")\n                  return;\n              }\n\n              \/\/\u6587\u5b57\u5217\u306b\u5909\u63db  \n              const chunk = new TextDecoder(\"utf-8\").decode(value);\n              draw(chunk);\n              controller.enqueue(value);\n              push();\n          });\n      };          \n      push();\n      }\n    });\n\n  return new Response(stream, { headers: { \"Content-Type\": \"text\/html\" } });\n});\n<\/pre>\n<h4>\u5730\u56f3\u3092\u63cf\u753b\u3059\u308b(SVG)<\/h4>\n<p>chunk\u304b\u3089\u53d6\u308a\u51fa\u3057\u305fJSON\u3092D3\u3067\u63cf\u753b\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\n\u3057\u304b\u3057\u30c7\u30fc\u30bf\u91cf\u304c\u591a\u3044\u3068SVG\u30a8\u30ec\u30e1\u30f3\u30c8\u6570\u3082\u6bd4\u4f8b\u3057\u3066\u591a\u304f\u306a\u308b\u306e\u3067\u3001\u3053\u306e\u65b9\u6cd5\u306f\u975e\u63a8\u5968\u3067\u3059\u3002<br \/>\n\u8a73\u3057\u304f\u306f\u300c\u5730\u56f3\u3092\u63cf\u753b\u3059\u308b(Canvas)\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"lang:js decode:true \"  >let buffer = \"\";\nconst projection = d3.geoMercator()\n  .center([138, 35])\n  .scale(2000)\n\nconst geoPath = d3.geoPath(projection);    \n    \nconst map = d3.select(\"svg\").append(\"g\");\n\nmap.attr(\"transform\", \"translate(300, 300)\");\n\n\nfunction draw(chunk){\n  buffer += chunk;\n  \n  \/\/\u6539\u884c\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\n  if(buffer.indexOf(\"n\") &lt; 1) return;  \n  \n  \/\/\u6539\u884c\u30b3\u30fc\u30c9\u3067\u6587\u5b57\u5217\u3092\u5206\u5272\u3059\u308b\n  const block = buffer.split(\"n\");\n  \n  \/\/\u6587\u5b57\u5217\u306e\u672b\u5c3e\u3092\u53d6\u308a\u51fa\u3057\n  const last = block.pop();\n  \n  \/\/\u672b\u5c3e\u4ee5\u5916\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\n  const jsons = block.map(JSON.parse);\n  \n  \/\/\u672b\u5c3e\u306b\u6539\u884c\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30af\n  if(last.indexOf(\"n\") &lt; 0){\n    buffer = last; \/\/\u6539\u884c\u30b3\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u306f\u6b21\u306echank\u3092\u5f85\u3064\n  }else{\n    if(last) jsons.push(JSON.parse(last)); \/\/\u6539\u884c\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\u3057\u3066\u8ffd\u52a0\n  }\n  \n\n  \/\/svg\u3067\u5730\u56f3\u3092\u63cf\u753b\n  jsons.forEach(d =&gt; {\n    map.append(\"path\").attr(\"d\", geoPath(d));  \n  });\n  \n\n}<\/pre>\n<h4>\u5730\u56f3\u3092\u63cf\u753b\u3059\u308b(canvas)<\/h4>\n<p>chunk\u304b\u3089\u53d6\u308a\u51fa\u3057\u305fJSON\u3092D3\u3067canvas\u306b\u63cf\u753b\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\n\u3053\u306e\u65b9\u6cd5\u3060\u3068\u3001\u30c7\u30fc\u30bf\u91cf\u306e\u591a\u3044\u5730\u7406\u60c5\u5831\u3082\u3042\u308b\u7a0b\u5ea6\u307e\u3067\u63cf\u753b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3053\u308c\u4ee5\u4e0a\u306e\u30b5\u30a4\u30ba\u306b\u306a\u308b\u3068WebGL\u306a\u3069\u3092\u5229\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >const canvas = d3.select(\"canvas\")\nconst ctx = canvas.node().getContext('2d');\n\nconst projection = d3.geoMercator()     \/\/\u6295\u5f71\u6cd5\u306e\u6307\u5b9a\n    .scale(4000)    \/\/\u30b9\u30b1\u30fc\u30eb\uff08\u30ba\u30fc\u30e0\uff09\u306e\u6307\u5b9a\n    .translate([960,500*2]) \/\/\u8868\u793a\u4f4d\u7f6e\u8abf\u6574\n    .center([139.0032936, 36.3219088]); \/\/\u4e2d\u5fc3\u306e\u5ea7\u6a19\u3092\u6307\u5b9a\n    \n        \nconst  geoPath = d3.geoPath(projection)\n  .context(ctx);\n\n\nfunction draw(chunk){\n  buffer += chunk;\n  \n  if(buffer.indexOf(\"n\") &lt; 1) return; \n  \n  const block = buffer.split(\"n\");\n  \n  const last = block.pop();\n  \n  const jsons = block.map(JSON.parse);\n  \n  if(last.indexOf(\"n\") &lt; 0){\n    buffer = last;\n  }else{\n    if(last) jsons.push(JSON.parse(last));\n  }\n  \n  \n  jsons.forEach(d =&gt; {\n    ctx.beginPath();\n    geoPath(d);\n    ctx.stroke();\n    ctx.closePath();\n  });\n  \n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5730\u7406\u7a7a\u9593\u30c7\u30fc\u30bf\u306f\u3069\u3046\u3057\u3066\u3082\u30c7\u30fc\u30bf\u30b5\u30a4\u30ba\u304c\u5927\u304d\u304f\u306a\u308a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6119,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,6,66],"tags":[],"class_list":["post-6054","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-d3v4","category-javascript","category-map","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/6054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=6054"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/6054\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/6119"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=6054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=6054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=6054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}