mapbox-gl 1.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (460) hide show
  1. package/.flowconfig +61 -0
  2. package/CHANGELOG.md +2485 -0
  3. package/LICENSE.txt +84 -0
  4. package/README.md +34 -0
  5. package/build/banner.js +4 -0
  6. package/build/check-bundle-size.js +140 -0
  7. package/build/diff-tarball.js +18 -0
  8. package/build/generate-access-token-script.js +11 -0
  9. package/build/generate-flow-typed-style-spec.js +188 -0
  10. package/build/generate-release-list.js +21 -0
  11. package/build/generate-struct-arrays.js +243 -0
  12. package/build/generate-style-code.js +159 -0
  13. package/build/mapbox-gl.js.flow +3 -0
  14. package/build/print-release-url.js +6 -0
  15. package/build/rollup_plugin_minify_style_spec.js +24 -0
  16. package/build/rollup_plugins.js +80 -0
  17. package/build/run-node +3 -0
  18. package/build/run-tap +8 -0
  19. package/build/test/build-tape.js +19 -0
  20. package/dist/mapbox-gl-csp-worker.js +2 -0
  21. package/dist/mapbox-gl-csp-worker.js.map +1 -0
  22. package/dist/mapbox-gl-csp.js +2 -0
  23. package/dist/mapbox-gl-csp.js.map +1 -0
  24. package/dist/mapbox-gl-dev.js +65889 -0
  25. package/dist/mapbox-gl-dev.js.flow +3 -0
  26. package/dist/mapbox-gl-unminified.js +42889 -0
  27. package/dist/mapbox-gl-unminified.js.map +1 -0
  28. package/dist/mapbox-gl.css +1 -0
  29. package/dist/mapbox-gl.js +42 -0
  30. package/dist/mapbox-gl.js.flow +3 -0
  31. package/dist/mapbox-gl.js.map +1 -0
  32. package/dist/style-spec/index.es.js +15032 -0
  33. package/dist/style-spec/index.es.js.map +1 -0
  34. package/dist/style-spec/index.js +15058 -0
  35. package/dist/style-spec/index.js.map +1 -0
  36. package/flow-typed/gl.js +5 -0
  37. package/flow-typed/jsdom.js +18 -0
  38. package/flow-typed/mapbox-gl-supported.js +9 -0
  39. package/flow-typed/mapbox-unitbezier.js +14 -0
  40. package/flow-typed/offscreen-canvas.js +9 -0
  41. package/flow-typed/pbf.js +25 -0
  42. package/flow-typed/point-geometry.js +44 -0
  43. package/flow-typed/potpack.js +12 -0
  44. package/flow-typed/sinon.js +28 -0
  45. package/flow-typed/vector-tile.js +41 -0
  46. package/package.json +173 -0
  47. package/src/css/mapbox-gl.css +812 -0
  48. package/src/css/svg/mapboxgl-ctrl-attrib.svg +3 -0
  49. package/src/css/svg/mapboxgl-ctrl-compass.svg +4 -0
  50. package/src/css/svg/mapboxgl-ctrl-fullscreen.svg +3 -0
  51. package/src/css/svg/mapboxgl-ctrl-geolocate.svg +5 -0
  52. package/src/css/svg/mapboxgl-ctrl-logo.svg +20 -0
  53. package/src/css/svg/mapboxgl-ctrl-shrink.svg +3 -0
  54. package/src/css/svg/mapboxgl-ctrl-zoom-in.svg +3 -0
  55. package/src/css/svg/mapboxgl-ctrl-zoom-out.svg +3 -0
  56. package/src/data/array_types.js +1135 -0
  57. package/src/data/bucket/circle_attributes.js +9 -0
  58. package/src/data/bucket/circle_bucket.js +201 -0
  59. package/src/data/bucket/fill_attributes.js +9 -0
  60. package/src/data/bucket/fill_bucket.js +229 -0
  61. package/src/data/bucket/fill_extrusion_attributes.js +10 -0
  62. package/src/data/bucket/fill_extrusion_bucket.js +283 -0
  63. package/src/data/bucket/heatmap_bucket.js +17 -0
  64. package/src/data/bucket/line_attributes.js +10 -0
  65. package/src/data/bucket/line_attributes_ext.js +10 -0
  66. package/src/data/bucket/line_bucket.js +594 -0
  67. package/src/data/bucket/pattern_attributes.js +12 -0
  68. package/src/data/bucket/pattern_bucket_features.js +60 -0
  69. package/src/data/bucket/symbol_attributes.js +117 -0
  70. package/src/data/bucket/symbol_bucket.js +937 -0
  71. package/src/data/bucket.js +123 -0
  72. package/src/data/dem_data.js +125 -0
  73. package/src/data/evaluation_feature.js +25 -0
  74. package/src/data/extent.js +18 -0
  75. package/src/data/feature_index.js +322 -0
  76. package/src/data/feature_position_map.js +131 -0
  77. package/src/data/index_array_type.js +16 -0
  78. package/src/data/load_geometry.js +46 -0
  79. package/src/data/pos_attributes.js +6 -0
  80. package/src/data/program_configuration.js +708 -0
  81. package/src/data/raster_bounds_attributes.js +7 -0
  82. package/src/data/segment.js +76 -0
  83. package/src/geo/edge_insets.js +102 -0
  84. package/src/geo/lng_lat.js +168 -0
  85. package/src/geo/lng_lat_bounds.js +276 -0
  86. package/src/geo/mercator_coordinate.js +150 -0
  87. package/src/geo/transform.js +834 -0
  88. package/src/gl/color_mode.js +34 -0
  89. package/src/gl/context.js +302 -0
  90. package/src/gl/cull_face_mode.js +26 -0
  91. package/src/gl/depth_mode.js +29 -0
  92. package/src/gl/framebuffer.js +44 -0
  93. package/src/gl/index_buffer.js +55 -0
  94. package/src/gl/stencil_mode.js +30 -0
  95. package/src/gl/types.js +84 -0
  96. package/src/gl/value.js +520 -0
  97. package/src/gl/vertex_buffer.js +119 -0
  98. package/src/index.js +230 -0
  99. package/src/render/draw_background.js +57 -0
  100. package/src/render/draw_circle.js +113 -0
  101. package/src/render/draw_collision_debug.js +172 -0
  102. package/src/render/draw_custom.js +49 -0
  103. package/src/render/draw_debug.js +127 -0
  104. package/src/render/draw_fill.js +124 -0
  105. package/src/render/draw_fill_extrusion.js +95 -0
  106. package/src/render/draw_heatmap.js +133 -0
  107. package/src/render/draw_hillshade.js +107 -0
  108. package/src/render/draw_line.js +125 -0
  109. package/src/render/draw_raster.js +125 -0
  110. package/src/render/draw_symbol.js +392 -0
  111. package/src/render/glyph_atlas.js +71 -0
  112. package/src/render/glyph_manager.js +182 -0
  113. package/src/render/image_atlas.js +149 -0
  114. package/src/render/image_manager.js +306 -0
  115. package/src/render/line_atlas.js +210 -0
  116. package/src/render/painter.js +654 -0
  117. package/src/render/program/background_program.js +103 -0
  118. package/src/render/program/circle_program.js +69 -0
  119. package/src/render/program/clipping_mask_program.js +20 -0
  120. package/src/render/program/collision_program.js +76 -0
  121. package/src/render/program/debug_program.js +35 -0
  122. package/src/render/program/fill_extrusion_program.js +122 -0
  123. package/src/render/program/fill_program.js +126 -0
  124. package/src/render/program/heatmap_program.js +83 -0
  125. package/src/render/program/hillshade_program.js +119 -0
  126. package/src/render/program/line_program.js +211 -0
  127. package/src/render/program/pattern.js +102 -0
  128. package/src/render/program/program_uniforms.js +42 -0
  129. package/src/render/program/raster_program.js +92 -0
  130. package/src/render/program/symbol_program.js +224 -0
  131. package/src/render/program.js +188 -0
  132. package/src/render/texture.js +122 -0
  133. package/src/render/uniform_binding.js +147 -0
  134. package/src/render/vertex_array_object.js +163 -0
  135. package/src/shaders/README.md +42 -0
  136. package/src/shaders/_prelude.fragment.glsl +17 -0
  137. package/src/shaders/_prelude.vertex.glsl +73 -0
  138. package/src/shaders/background.fragment.glsl +10 -0
  139. package/src/shaders/background.vertex.glsl +7 -0
  140. package/src/shaders/background_pattern.fragment.glsl +28 -0
  141. package/src/shaders/background_pattern.vertex.glsl +20 -0
  142. package/src/shaders/circle.fragment.glsl +39 -0
  143. package/src/shaders/circle.vertex.glsl +64 -0
  144. package/src/shaders/clipping_mask.fragment.glsl +3 -0
  145. package/src/shaders/clipping_mask.vertex.glsl +7 -0
  146. package/src/shaders/collision_box.fragment.glsl +21 -0
  147. package/src/shaders/collision_box.vertex.glsl +27 -0
  148. package/src/shaders/collision_circle.fragment.glsl +17 -0
  149. package/src/shaders/collision_circle.vertex.glsl +59 -0
  150. package/src/shaders/debug.fragment.glsl +9 -0
  151. package/src/shaders/debug.vertex.glsl +12 -0
  152. package/src/shaders/encode_attribute.js +17 -0
  153. package/src/shaders/fill.fragment.glsl +13 -0
  154. package/src/shaders/fill.vertex.glsl +13 -0
  155. package/src/shaders/fill_extrusion.fragment.glsl +9 -0
  156. package/src/shaders/fill_extrusion.vertex.glsl +66 -0
  157. package/src/shaders/fill_extrusion_pattern.fragment.glsl +45 -0
  158. package/src/shaders/fill_extrusion_pattern.vertex.glsl +79 -0
  159. package/src/shaders/fill_outline.fragment.glsl +17 -0
  160. package/src/shaders/fill_outline.vertex.glsl +17 -0
  161. package/src/shaders/fill_outline_pattern.fragment.glsl +43 -0
  162. package/src/shaders/fill_outline_pattern.vertex.glsl +44 -0
  163. package/src/shaders/fill_pattern.fragment.glsl +36 -0
  164. package/src/shaders/fill_pattern.vertex.glsl +39 -0
  165. package/src/shaders/heatmap.fragment.glsl +22 -0
  166. package/src/shaders/heatmap.vertex.glsl +54 -0
  167. package/src/shaders/heatmap_texture.fragment.glsl +14 -0
  168. package/src/shaders/heatmap_texture.vertex.glsl +11 -0
  169. package/src/shaders/hillshade.fragment.glsl +52 -0
  170. package/src/shaders/hillshade.vertex.glsl +11 -0
  171. package/src/shaders/hillshade_prepare.fragment.glsl +75 -0
  172. package/src/shaders/hillshade_prepare.vertex.glsl +15 -0
  173. package/src/shaders/index.js +20 -0
  174. package/src/shaders/line.fragment.glsl +30 -0
  175. package/src/shaders/line.vertex.glsl +85 -0
  176. package/src/shaders/line_gradient.fragment.glsl +34 -0
  177. package/src/shaders/line_gradient.vertex.glsl +88 -0
  178. package/src/shaders/line_pattern.fragment.glsl +74 -0
  179. package/src/shaders/line_pattern.vertex.glsl +99 -0
  180. package/src/shaders/line_sdf.fragment.glsl +45 -0
  181. package/src/shaders/line_sdf.vertex.glsl +98 -0
  182. package/src/shaders/raster.fragment.glsl +52 -0
  183. package/src/shaders/raster.vertex.glsl +21 -0
  184. package/src/shaders/shaders.js +185 -0
  185. package/src/shaders/symbol_icon.fragment.glsl +17 -0
  186. package/src/shaders/symbol_icon.vertex.glsl +94 -0
  187. package/src/shaders/symbol_sdf.fragment.glsl +52 -0
  188. package/src/shaders/symbol_sdf.vertex.glsl +115 -0
  189. package/src/shaders/symbol_text_and_icon.fragment.glsl +68 -0
  190. package/src/shaders/symbol_text_and_icon.vertex.glsl +116 -0
  191. package/src/source/canvas_source.js +238 -0
  192. package/src/source/geojson_source.js +370 -0
  193. package/src/source/geojson_worker_source.js +366 -0
  194. package/src/source/geojson_wrapper.js +94 -0
  195. package/src/source/image_source.js +307 -0
  196. package/src/source/load_tilejson.js +39 -0
  197. package/src/source/pixels_to_tile_units.js +21 -0
  198. package/src/source/query_features.js +208 -0
  199. package/src/source/raster_dem_tile_source.js +138 -0
  200. package/src/source/raster_dem_tile_worker_source.js +62 -0
  201. package/src/source/raster_tile_source.js +169 -0
  202. package/src/source/rtl_text_plugin.js +143 -0
  203. package/src/source/source.js +137 -0
  204. package/src/source/source_cache.js +953 -0
  205. package/src/source/source_state.js +159 -0
  206. package/src/source/tile.js +458 -0
  207. package/src/source/tile_bounds.js +38 -0
  208. package/src/source/tile_cache.js +212 -0
  209. package/src/source/tile_id.js +199 -0
  210. package/src/source/vector_tile_source.js +259 -0
  211. package/src/source/vector_tile_worker_source.js +216 -0
  212. package/src/source/video_source.js +203 -0
  213. package/src/source/worker.js +240 -0
  214. package/src/source/worker_source.js +107 -0
  215. package/src/source/worker_tile.js +224 -0
  216. package/src/style/create_style_layer.js +36 -0
  217. package/src/style/evaluation_parameters.js +62 -0
  218. package/src/style/format_section_override.js +56 -0
  219. package/src/style/light.js +130 -0
  220. package/src/style/load_glyph_range.js +38 -0
  221. package/src/style/load_sprite.js +67 -0
  222. package/src/style/parse_glyph_pbf.js +44 -0
  223. package/src/style/pauseable_placement.js +132 -0
  224. package/src/style/properties.js +753 -0
  225. package/src/style/query_utils.js +43 -0
  226. package/src/style/style.js +1374 -0
  227. package/src/style/style_glyph.js +17 -0
  228. package/src/style/style_image.js +137 -0
  229. package/src/style/style_layer/background_style_layer.js +21 -0
  230. package/src/style/style_layer/background_style_layer_properties.js +40 -0
  231. package/src/style/style_layer/circle_style_layer.js +98 -0
  232. package/src/style/style_layer/circle_style_layer_properties.js +63 -0
  233. package/src/style/style_layer/custom_style_layer.js +223 -0
  234. package/src/style/style_layer/fill_extrusion_style_layer.js +224 -0
  235. package/src/style/style_layer/fill_extrusion_style_layer_properties.js +50 -0
  236. package/src/style/style_layer/fill_style_layer.js +67 -0
  237. package/src/style/style_layer/fill_style_layer_properties.js +55 -0
  238. package/src/style/style_layer/heatmap_style_layer.js +73 -0
  239. package/src/style/style_layer/heatmap_style_layer_properties.js +44 -0
  240. package/src/style/style_layer/hillshade_style_layer.js +25 -0
  241. package/src/style/style_layer/hillshade_style_layer_properties.js +46 -0
  242. package/src/style/style_layer/layer_properties.js.ejs +69 -0
  243. package/src/style/style_layer/line_style_layer.js +150 -0
  244. package/src/style/style_layer/line_style_layer_properties.js +71 -0
  245. package/src/style/style_layer/raster_style_layer.js +21 -0
  246. package/src/style/style_layer/raster_style_layer_properties.js +50 -0
  247. package/src/style/style_layer/symbol_style_layer.js +190 -0
  248. package/src/style/style_layer/symbol_style_layer_properties.js +153 -0
  249. package/src/style/style_layer/typed_style_layer.js +17 -0
  250. package/src/style/style_layer.js +283 -0
  251. package/src/style/style_layer_index.js +80 -0
  252. package/src/style/validate_style.js +42 -0
  253. package/src/style/zoom_history.js +44 -0
  254. package/src/style-spec/.eslintrc +5 -0
  255. package/src/style-spec/CHANGELOG.md +468 -0
  256. package/src/style-spec/README.md +59 -0
  257. package/src/style-spec/bin/gl-style-composite +9 -0
  258. package/src/style-spec/bin/gl-style-format +22 -0
  259. package/src/style-spec/bin/gl-style-migrate +9 -0
  260. package/src/style-spec/bin/gl-style-validate +50 -0
  261. package/src/style-spec/composite.js +50 -0
  262. package/src/style-spec/declass.js +42 -0
  263. package/src/style-spec/deref.js +52 -0
  264. package/src/style-spec/diff.js +393 -0
  265. package/src/style-spec/dist/.gitkeep +0 -0
  266. package/src/style-spec/dist/index.es.js +15032 -0
  267. package/src/style-spec/dist/index.es.js.map +1 -0
  268. package/src/style-spec/dist/index.js +15058 -0
  269. package/src/style-spec/dist/index.js.map +1 -0
  270. package/src/style-spec/empty.js +29 -0
  271. package/src/style-spec/error/parsing_error.js +16 -0
  272. package/src/style-spec/error/validation_error.js +18 -0
  273. package/src/style-spec/expression/compound_expression.js +162 -0
  274. package/src/style-spec/expression/definitions/assertion.js +130 -0
  275. package/src/style-spec/expression/definitions/at.js +70 -0
  276. package/src/style-spec/expression/definitions/case.js +85 -0
  277. package/src/style-spec/expression/definitions/coalesce.js +93 -0
  278. package/src/style-spec/expression/definitions/coercion.js +133 -0
  279. package/src/style-spec/expression/definitions/collator.js +78 -0
  280. package/src/style-spec/expression/definitions/comparison.js +184 -0
  281. package/src/style-spec/expression/definitions/format.js +144 -0
  282. package/src/style-spec/expression/definitions/image.js +52 -0
  283. package/src/style-spec/expression/definitions/in.js +72 -0
  284. package/src/style-spec/expression/definitions/index.js +565 -0
  285. package/src/style-spec/expression/definitions/index_of.js +89 -0
  286. package/src/style-spec/expression/definitions/interpolate.js +267 -0
  287. package/src/style-spec/expression/definitions/length.js +61 -0
  288. package/src/style-spec/expression/definitions/let.js +72 -0
  289. package/src/style-spec/expression/definitions/literal.js +77 -0
  290. package/src/style-spec/expression/definitions/match.js +158 -0
  291. package/src/style-spec/expression/definitions/number_format.js +142 -0
  292. package/src/style-spec/expression/definitions/slice.js +86 -0
  293. package/src/style-spec/expression/definitions/step.js +120 -0
  294. package/src/style-spec/expression/definitions/var.js +46 -0
  295. package/src/style-spec/expression/definitions/within.js +342 -0
  296. package/src/style-spec/expression/evaluation_context.js +59 -0
  297. package/src/style-spec/expression/expression.js +27 -0
  298. package/src/style-spec/expression/index.js +392 -0
  299. package/src/style-spec/expression/is_constant.js +59 -0
  300. package/src/style-spec/expression/parsing_context.js +233 -0
  301. package/src/style-spec/expression/parsing_error.js +13 -0
  302. package/src/style-spec/expression/runtime_error.js +17 -0
  303. package/src/style-spec/expression/scope.js +36 -0
  304. package/src/style-spec/expression/stops.js +39 -0
  305. package/src/style-spec/expression/types/collator.js +61 -0
  306. package/src/style-spec/expression/types/formatted.js +73 -0
  307. package/src/style-spec/expression/types/resolved_image.js +29 -0
  308. package/src/style-spec/expression/types.js +126 -0
  309. package/src/style-spec/expression/values.js +123 -0
  310. package/src/style-spec/feature_filter/README.md +55 -0
  311. package/src/style-spec/feature_filter/convert.js +208 -0
  312. package/src/style-spec/feature_filter/index.js +175 -0
  313. package/src/style-spec/format.js +51 -0
  314. package/src/style-spec/function/convert.js +270 -0
  315. package/src/style-spec/function/index.js +262 -0
  316. package/src/style-spec/group_by_layout.js +75 -0
  317. package/src/style-spec/migrate/expressions.js +39 -0
  318. package/src/style-spec/migrate/v8.js +203 -0
  319. package/src/style-spec/migrate/v9.js +26 -0
  320. package/src/style-spec/migrate.js +36 -0
  321. package/src/style-spec/package.json +41 -0
  322. package/src/style-spec/read_style.js +14 -0
  323. package/src/style-spec/reference/latest.js +3 -0
  324. package/src/style-spec/reference/v8.json +5914 -0
  325. package/src/style-spec/rollup.config.js +65 -0
  326. package/src/style-spec/style-spec.js +124 -0
  327. package/src/style-spec/types.js +432 -0
  328. package/src/style-spec/util/color.js +95 -0
  329. package/src/style-spec/util/color_spaces.js +139 -0
  330. package/src/style-spec/util/deep_equal.js +28 -0
  331. package/src/style-spec/util/extend.js +10 -0
  332. package/src/style-spec/util/get_type.js +17 -0
  333. package/src/style-spec/util/interpolate.js +22 -0
  334. package/src/style-spec/util/properties.js +15 -0
  335. package/src/style-spec/util/ref_properties.js +2 -0
  336. package/src/style-spec/util/result.js +19 -0
  337. package/src/style-spec/util/unbundle_jsonlint.js +24 -0
  338. package/src/style-spec/validate/latest.js +11 -0
  339. package/src/style-spec/validate/validate.js +75 -0
  340. package/src/style-spec/validate/validate_array.js +52 -0
  341. package/src/style-spec/validate/validate_boolean.js +15 -0
  342. package/src/style-spec/validate/validate_color.js +20 -0
  343. package/src/style-spec/validate/validate_constants.js +13 -0
  344. package/src/style-spec/validate/validate_enum.js +21 -0
  345. package/src/style-spec/validate/validate_expression.js +43 -0
  346. package/src/style-spec/validate/validate_filter.js +117 -0
  347. package/src/style-spec/validate/validate_formatted.js +11 -0
  348. package/src/style-spec/validate/validate_function.js +207 -0
  349. package/src/style-spec/validate/validate_glyphs_url.js +21 -0
  350. package/src/style-spec/validate/validate_image.js +11 -0
  351. package/src/style-spec/validate/validate_layer.js +134 -0
  352. package/src/style-spec/validate/validate_layout_property.js +6 -0
  353. package/src/style-spec/validate/validate_light.js +47 -0
  354. package/src/style-spec/validate/validate_number.js +29 -0
  355. package/src/style-spec/validate/validate_object.js +61 -0
  356. package/src/style-spec/validate/validate_paint_property.js +6 -0
  357. package/src/style-spec/validate/validate_property.js +64 -0
  358. package/src/style-spec/validate/validate_source.js +111 -0
  359. package/src/style-spec/validate/validate_string.js +15 -0
  360. package/src/style-spec/validate_mapbox_api_supported.js +171 -0
  361. package/src/style-spec/validate_style.js +39 -0
  362. package/src/style-spec/validate_style.min.js +78 -0
  363. package/src/style-spec/visit.js +77 -0
  364. package/src/symbol/anchor.js +26 -0
  365. package/src/symbol/check_max_angle.js +81 -0
  366. package/src/symbol/clip_line.js +71 -0
  367. package/src/symbol/collision_feature.js +109 -0
  368. package/src/symbol/collision_index.js +373 -0
  369. package/src/symbol/cross_tile_symbol_index.js +301 -0
  370. package/src/symbol/get_anchors.js +167 -0
  371. package/src/symbol/grid_index.js +335 -0
  372. package/src/symbol/mergelines.js +82 -0
  373. package/src/symbol/one_em.js +4 -0
  374. package/src/symbol/opacity_state.js +27 -0
  375. package/src/symbol/path_interpolator.js +61 -0
  376. package/src/symbol/placement.js +1124 -0
  377. package/src/symbol/projection.js +451 -0
  378. package/src/symbol/quads.js +334 -0
  379. package/src/symbol/shaping.js +816 -0
  380. package/src/symbol/symbol_layout.js +796 -0
  381. package/src/symbol/symbol_size.js +113 -0
  382. package/src/symbol/transform_text.js +29 -0
  383. package/src/types/callback.js +17 -0
  384. package/src/types/cancelable.js +3 -0
  385. package/src/types/tilejson.js +17 -0
  386. package/src/types/transferable.js +3 -0
  387. package/src/types/window.js +172 -0
  388. package/src/ui/anchor.js +32 -0
  389. package/src/ui/camera.js +1277 -0
  390. package/src/ui/control/attribution_control.js +212 -0
  391. package/src/ui/control/fullscreen_control.js +147 -0
  392. package/src/ui/control/geolocate_control.js +707 -0
  393. package/src/ui/control/logo_control.js +92 -0
  394. package/src/ui/control/navigation_control.js +257 -0
  395. package/src/ui/control/scale_control.js +142 -0
  396. package/src/ui/default_locale.js +22 -0
  397. package/src/ui/events.js +1301 -0
  398. package/src/ui/handler/box_zoom.js +170 -0
  399. package/src/ui/handler/click_zoom.js +52 -0
  400. package/src/ui/handler/handler_util.js +12 -0
  401. package/src/ui/handler/keyboard.js +208 -0
  402. package/src/ui/handler/map_event.js +156 -0
  403. package/src/ui/handler/mouse.js +171 -0
  404. package/src/ui/handler/scroll_zoom.js +350 -0
  405. package/src/ui/handler/shim/dblclick_zoom.js +62 -0
  406. package/src/ui/handler/shim/drag_pan.js +88 -0
  407. package/src/ui/handler/shim/drag_rotate.js +67 -0
  408. package/src/ui/handler/shim/touch_zoom_rotate.js +108 -0
  409. package/src/ui/handler/tap_drag_zoom.js +103 -0
  410. package/src/ui/handler/tap_recognizer.js +133 -0
  411. package/src/ui/handler/tap_zoom.js +93 -0
  412. package/src/ui/handler/touch_pan.js +101 -0
  413. package/src/ui/handler/touch_zoom_rotate.js +305 -0
  414. package/src/ui/handler_inertia.js +158 -0
  415. package/src/ui/handler_manager.js +531 -0
  416. package/src/ui/hash.js +148 -0
  417. package/src/ui/map.js +2874 -0
  418. package/src/ui/marker.js +672 -0
  419. package/src/ui/popup.js +640 -0
  420. package/src/util/actor.js +212 -0
  421. package/src/util/ajax.js +388 -0
  422. package/src/util/browser/web_worker.js +10 -0
  423. package/src/util/browser/window.js +6 -0
  424. package/src/util/browser.js +70 -0
  425. package/src/util/classify_rings.js +52 -0
  426. package/src/util/color_ramp.js +61 -0
  427. package/src/util/config.js +30 -0
  428. package/src/util/debug.js +28 -0
  429. package/src/util/dictionary_coder.js +30 -0
  430. package/src/util/dispatcher.js +70 -0
  431. package/src/util/dom.js +142 -0
  432. package/src/util/evented.js +174 -0
  433. package/src/util/find_pole_of_inaccessibility.js +129 -0
  434. package/src/util/global_worker_pool.js +35 -0
  435. package/src/util/image.js +142 -0
  436. package/src/util/intersection_tests.js +208 -0
  437. package/src/util/is_char_in_unicode_block.js +311 -0
  438. package/src/util/mapbox.js +491 -0
  439. package/src/util/offscreen_canvas_supported.js +14 -0
  440. package/src/util/performance.js +112 -0
  441. package/src/util/primitives.js +145 -0
  442. package/src/util/resolve_tokens.js +16 -0
  443. package/src/util/script_detection.js +328 -0
  444. package/src/util/sku_token.js +42 -0
  445. package/src/util/smart_wrap.js +55 -0
  446. package/src/util/struct_array.js +243 -0
  447. package/src/util/struct_array.js.ejs +112 -0
  448. package/src/util/struct_array_layout.js.ejs +98 -0
  449. package/src/util/task_queue.js +68 -0
  450. package/src/util/throttle.js +27 -0
  451. package/src/util/throttled_invoker.js +46 -0
  452. package/src/util/tile_request_cache.js +172 -0
  453. package/src/util/util.js +524 -0
  454. package/src/util/vectortile_to_geojson.js +50 -0
  455. package/src/util/verticalize_punctuation.js +114 -0
  456. package/src/util/web_worker.js +91 -0
  457. package/src/util/web_worker_transfer.js +266 -0
  458. package/src/util/webp_supported.js +69 -0
  459. package/src/util/window.js +102 -0
  460. package/src/util/worker_pool.js +57 -0
@@ -0,0 +1,491 @@
1
+ // @flow
2
+
3
+ /***** START WARNING - IF YOU USE THIS CODE WITH MAPBOX MAPPING APIS, REMOVAL OR
4
+ * MODIFICATION OF THE FOLLOWING CODE VIOLATES THE MAPBOX TERMS OF SERVICE ******
5
+ * The following code is used to access Mapbox's Mapping APIs. Removal or modification
6
+ * of this code when used with Mapbox's Mapping APIs can result in higher fees and/or
7
+ * termination of your account with Mapbox.
8
+ *
9
+ * Under the Mapbox Terms of Service, you may not use this code to access Mapbox
10
+ * Mapping APIs other than through Mapbox SDKs.
11
+ *
12
+ * The Mapping APIs documentation is available at https://docs.mapbox.com/api/maps/#maps
13
+ * and the Mapbox Terms of Service are available at https://www.mapbox.com/tos/
14
+ ******************************************************************************/
15
+
16
+ import config from './config';
17
+
18
+ import browser from './browser';
19
+ import window from './window';
20
+ import webpSupported from './webp_supported';
21
+ import {createSkuToken, SKU_ID} from './sku_token';
22
+ import {version as sdkVersion} from '../../package.json';
23
+ import {uuid, validateUuid, storageAvailable, b64DecodeUnicode, b64EncodeUnicode, warnOnce, extend} from './util';
24
+ import {postData, ResourceType} from './ajax';
25
+
26
+ import type {RequestParameters} from './ajax';
27
+ import type {Cancelable} from '../types/cancelable';
28
+ import type {TileJSON} from '../types/tilejson';
29
+
30
+ type ResourceTypeEnum = $Keys<typeof ResourceType>;
31
+ export type RequestTransformFunction = (url: string, resourceType?: ResourceTypeEnum) => RequestParameters;
32
+
33
+ type UrlObject = {|
34
+ protocol: string,
35
+ authority: string,
36
+ path: string,
37
+ params: Array<string>
38
+ |};
39
+
40
+ export class RequestManager {
41
+ _skuToken: string;
42
+ _skuTokenExpiresAt: number;
43
+ _transformRequestFn: ?RequestTransformFunction;
44
+ _customAccessToken: ?string;
45
+
46
+ constructor(transformRequestFn?: RequestTransformFunction, customAccessToken?: string) {
47
+ this._transformRequestFn = transformRequestFn;
48
+ this._customAccessToken = customAccessToken;
49
+ this._createSkuToken();
50
+ }
51
+
52
+ _createSkuToken() {
53
+ const skuToken = createSkuToken();
54
+ this._skuToken = skuToken.token;
55
+ this._skuTokenExpiresAt = skuToken.tokenExpiresAt;
56
+ }
57
+
58
+ _isSkuTokenExpired(): boolean {
59
+ return Date.now() > this._skuTokenExpiresAt;
60
+ }
61
+
62
+ transformRequest(url: string, type: ResourceTypeEnum) {
63
+ if (this._transformRequestFn) {
64
+ return this._transformRequestFn(url, type) || {url};
65
+ }
66
+
67
+ return {url};
68
+ }
69
+
70
+ normalizeStyleURL(url: string, accessToken?: string): string {
71
+ if (!isMapboxURL(url)) return url;
72
+ const urlObject = parseUrl(url);
73
+ urlObject.path = `/styles/v1${urlObject.path}`;
74
+ return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
75
+ }
76
+
77
+ normalizeGlyphsURL(url: string, accessToken?: string): string {
78
+ if (!isMapboxURL(url)) return url;
79
+ const urlObject = parseUrl(url);
80
+ urlObject.path = `/fonts/v1${urlObject.path}`;
81
+ return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
82
+ }
83
+
84
+ normalizeSourceURL(url: string, accessToken?: string): string {
85
+ if (!isMapboxURL(url)) return url;
86
+ const urlObject = parseUrl(url);
87
+ urlObject.path = `/v4/${urlObject.authority}.json`;
88
+ // TileJSON requests need a secure flag appended to their URLs so
89
+ // that the server knows to send SSL-ified resource references.
90
+ urlObject.params.push('secure');
91
+ return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
92
+ }
93
+
94
+ normalizeSpriteURL(url: string, format: string, extension: string, accessToken?: string): string {
95
+ const urlObject = parseUrl(url);
96
+ if (!isMapboxURL(url)) {
97
+ urlObject.path += `${format}${extension}`;
98
+ return formatUrl(urlObject);
99
+ }
100
+ urlObject.path = `/styles/v1${urlObject.path}/sprite${format}${extension}`;
101
+ return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
102
+ }
103
+
104
+ normalizeTileURL(tileURL: string, tileSize?: ?number): string {
105
+ if (this._isSkuTokenExpired()) {
106
+ this._createSkuToken();
107
+ }
108
+
109
+ if (tileURL && !isMapboxURL(tileURL)) return tileURL;
110
+
111
+ const urlObject = parseUrl(tileURL);
112
+ const imageExtensionRe = /(\.(png|jpg)\d*)(?=$)/;
113
+ const tileURLAPIPrefixRe = /^.+\/v4\//;
114
+
115
+ // The v4 mapbox tile API supports 512x512 image tiles only when @2x
116
+ // is appended to the tile URL. If `tileSize: 512` is specified for
117
+ // a Mapbox raster source force the @2x suffix even if a non hidpi device.
118
+ const suffix = browser.devicePixelRatio >= 2 || tileSize === 512 ? '@2x' : '';
119
+ const extension = webpSupported.supported ? '.webp' : '$1';
120
+ urlObject.path = urlObject.path.replace(imageExtensionRe, `${suffix}${extension}`);
121
+ urlObject.path = urlObject.path.replace(tileURLAPIPrefixRe, '/');
122
+ urlObject.path = `/v4${urlObject.path}`;
123
+
124
+ const accessToken = this._customAccessToken || getAccessToken(urlObject.params) || config.ACCESS_TOKEN;
125
+ if (config.REQUIRE_ACCESS_TOKEN && accessToken && this._skuToken) {
126
+ urlObject.params.push(`sku=${this._skuToken}`);
127
+ }
128
+
129
+ return this._makeAPIURL(urlObject, accessToken);
130
+ }
131
+
132
+ canonicalizeTileURL(url: string, removeAccessToken: boolean) {
133
+ const version = "/v4/";
134
+ // matches any file extension specified by a dot and one or more alphanumeric characters
135
+ const extensionRe = /\.[\w]+$/;
136
+
137
+ const urlObject = parseUrl(url);
138
+ // Make sure that we are dealing with a valid Mapbox tile URL.
139
+ // Has to begin with /v4/, with a valid filename + extension
140
+ if (!urlObject.path.match(/(^\/v4\/)/) || !urlObject.path.match(extensionRe)) {
141
+ // Not a proper Mapbox tile URL.
142
+ return url;
143
+ }
144
+ // Reassemble the canonical URL from the parts we've parsed before.
145
+ let result = "mapbox://tiles/";
146
+ result += urlObject.path.replace(version, '');
147
+
148
+ // Append the query string, minus the access token parameter.
149
+ let params = urlObject.params;
150
+ if (removeAccessToken) {
151
+ params = params.filter(p => !p.match(/^access_token=/));
152
+ }
153
+ if (params.length) result += `?${params.join('&')}`;
154
+ return result;
155
+ }
156
+
157
+ canonicalizeTileset(tileJSON: TileJSON, sourceURL?: string) {
158
+ const removeAccessToken = sourceURL ? isMapboxURL(sourceURL) : false;
159
+ const canonical = [];
160
+ for (const url of tileJSON.tiles || []) {
161
+ if (isMapboxHTTPURL(url)) {
162
+ canonical.push(this.canonicalizeTileURL(url, removeAccessToken));
163
+ } else {
164
+ canonical.push(url);
165
+ }
166
+ }
167
+ return canonical;
168
+ }
169
+
170
+ _makeAPIURL(urlObject: UrlObject, accessToken: string | null | void): string {
171
+ const help = 'See https://www.mapbox.com/api-documentation/#access-tokens-and-token-scopes';
172
+ const apiUrlObject = parseUrl(config.API_URL);
173
+ urlObject.protocol = apiUrlObject.protocol;
174
+ urlObject.authority = apiUrlObject.authority;
175
+
176
+ if (urlObject.protocol === 'http') {
177
+ const i = urlObject.params.indexOf('secure');
178
+ if (i >= 0) urlObject.params.splice(i, 1);
179
+ }
180
+
181
+ if (apiUrlObject.path !== '/') {
182
+ urlObject.path = `${apiUrlObject.path}${urlObject.path}`;
183
+ }
184
+
185
+ if (!config.REQUIRE_ACCESS_TOKEN) return formatUrl(urlObject);
186
+
187
+ accessToken = accessToken || config.ACCESS_TOKEN;
188
+ if (!accessToken)
189
+ throw new Error(`An API access token is required to use Mapbox GL. ${help}`);
190
+ if (accessToken[0] === 's')
191
+ throw new Error(`Use a public access token (pk.*) with Mapbox GL, not a secret access token (sk.*). ${help}`);
192
+
193
+ urlObject.params = urlObject.params.filter((d) => d.indexOf('access_token') === -1);
194
+ urlObject.params.push(`access_token=${accessToken}`);
195
+ return formatUrl(urlObject);
196
+ }
197
+ }
198
+
199
+ function isMapboxURL(url: string) {
200
+ return url.indexOf('mapbox:') === 0;
201
+ }
202
+
203
+ const mapboxHTTPURLRe = /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/|\?|$)/i;
204
+ function isMapboxHTTPURL(url: string): boolean {
205
+ return mapboxHTTPURLRe.test(url);
206
+ }
207
+
208
+ function hasCacheDefeatingSku(url: string) {
209
+ return url.indexOf('sku=') > 0 && isMapboxHTTPURL(url);
210
+ }
211
+
212
+ function getAccessToken(params: Array<string>): string | null {
213
+ for (const param of params) {
214
+ const match = param.match(/^access_token=(.*)$/);
215
+ if (match) {
216
+ return match[1];
217
+ }
218
+ }
219
+ return null;
220
+ }
221
+
222
+ const urlRe = /^(\w+):\/\/([^/?]*)(\/[^?]+)?\??(.+)?/;
223
+
224
+ function parseUrl(url: string): UrlObject {
225
+ const parts = url.match(urlRe);
226
+ if (!parts) {
227
+ throw new Error('Unable to parse URL object');
228
+ }
229
+ return {
230
+ protocol: parts[1],
231
+ authority: parts[2],
232
+ path: parts[3] || '/',
233
+ params: parts[4] ? parts[4].split('&') : []
234
+ };
235
+ }
236
+
237
+ function formatUrl(obj: UrlObject): string {
238
+ const params = obj.params.length ? `?${obj.params.join('&')}` : '';
239
+ return `${obj.protocol}://${obj.authority}${obj.path}${params}`;
240
+ }
241
+
242
+ export {isMapboxURL, isMapboxHTTPURL, hasCacheDefeatingSku};
243
+
244
+ const telemEventKey = 'mapbox.eventData';
245
+
246
+ function parseAccessToken(accessToken: ?string) {
247
+ if (!accessToken) {
248
+ return null;
249
+ }
250
+
251
+ const parts = accessToken.split('.');
252
+ if (!parts || parts.length !== 3) {
253
+ return null;
254
+ }
255
+
256
+ try {
257
+ const jsonData = JSON.parse(b64DecodeUnicode(parts[1]));
258
+ return jsonData;
259
+ } catch (e) {
260
+ return null;
261
+ }
262
+ }
263
+
264
+ type TelemetryEventType = 'appUserTurnstile' | 'map.load';
265
+
266
+ class TelemetryEvent {
267
+ eventData: any;
268
+ anonId: ?string;
269
+ queue: Array<any>;
270
+ type: TelemetryEventType;
271
+ pendingRequest: ?Cancelable;
272
+ _customAccessToken: ?string;
273
+
274
+ constructor(type: TelemetryEventType) {
275
+ this.type = type;
276
+ this.anonId = null;
277
+ this.eventData = {};
278
+ this.queue = [];
279
+ this.pendingRequest = null;
280
+ }
281
+
282
+ getStorageKey(domain: ?string) {
283
+ const tokenData = parseAccessToken(config.ACCESS_TOKEN);
284
+ let u = '';
285
+ if (tokenData && tokenData['u']) {
286
+ u = b64EncodeUnicode(tokenData['u']);
287
+ } else {
288
+ u = config.ACCESS_TOKEN || '';
289
+ }
290
+ return domain ?
291
+ `${telemEventKey}.${domain}:${u}` :
292
+ `${telemEventKey}:${u}`;
293
+ }
294
+
295
+ fetchEventData() {
296
+ const isLocalStorageAvailable = storageAvailable('localStorage');
297
+ const storageKey = this.getStorageKey();
298
+ const uuidKey = this.getStorageKey('uuid');
299
+
300
+ if (isLocalStorageAvailable) {
301
+ //Retrieve cached data
302
+ try {
303
+ const data = window.localStorage.getItem(storageKey);
304
+ if (data) {
305
+ this.eventData = JSON.parse(data);
306
+ }
307
+
308
+ const uuid = window.localStorage.getItem(uuidKey);
309
+ if (uuid) this.anonId = uuid;
310
+ } catch (e) {
311
+ warnOnce('Unable to read from LocalStorage');
312
+ }
313
+ }
314
+ }
315
+
316
+ saveEventData() {
317
+ const isLocalStorageAvailable = storageAvailable('localStorage');
318
+ const storageKey = this.getStorageKey();
319
+ const uuidKey = this.getStorageKey('uuid');
320
+ if (isLocalStorageAvailable) {
321
+ try {
322
+ window.localStorage.setItem(uuidKey, this.anonId);
323
+ if (Object.keys(this.eventData).length >= 1) {
324
+ window.localStorage.setItem(storageKey, JSON.stringify(this.eventData));
325
+ }
326
+ } catch (e) {
327
+ warnOnce('Unable to write to LocalStorage');
328
+ }
329
+ }
330
+
331
+ }
332
+
333
+ processRequests(_: ?string) {}
334
+
335
+ /*
336
+ * If any event data should be persisted after the POST request, the callback should modify eventData`
337
+ * to the values that should be saved. For this reason, the callback should be invoked prior to the call
338
+ * to TelemetryEvent#saveData
339
+ */
340
+ postEvent(timestamp: number, additionalPayload: {[_: string]: any}, callback: (err: ?Error) => void, customAccessToken?: ?string) {
341
+ if (!config.EVENTS_URL) return;
342
+ const eventsUrlObject: UrlObject = parseUrl(config.EVENTS_URL);
343
+ eventsUrlObject.params.push(`access_token=${customAccessToken || config.ACCESS_TOKEN || ''}`);
344
+
345
+ const payload: Object = {
346
+ event: this.type,
347
+ created: new Date(timestamp).toISOString(),
348
+ sdkIdentifier: 'mapbox-gl-js',
349
+ sdkVersion,
350
+ skuId: SKU_ID,
351
+ userId: this.anonId
352
+ };
353
+
354
+ const finalPayload = additionalPayload ? extend(payload, additionalPayload) : payload;
355
+ const request: RequestParameters = {
356
+ url: formatUrl(eventsUrlObject),
357
+ headers: {
358
+ 'Content-Type': 'text/plain' //Skip the pre-flight OPTIONS request
359
+ },
360
+ body: JSON.stringify([finalPayload])
361
+ };
362
+
363
+ this.pendingRequest = postData(request, (error) => {
364
+ this.pendingRequest = null;
365
+ callback(error);
366
+ this.saveEventData();
367
+ this.processRequests(customAccessToken);
368
+ });
369
+ }
370
+
371
+ queueRequest(event: number | {id: number, timestamp: number}, customAccessToken?: ?string) {
372
+ this.queue.push(event);
373
+ this.processRequests(customAccessToken);
374
+ }
375
+ }
376
+
377
+ export class MapLoadEvent extends TelemetryEvent {
378
+ +success: {[_: number]: boolean};
379
+ skuToken: string;
380
+
381
+ constructor() {
382
+ super('map.load');
383
+ this.success = {};
384
+ this.skuToken = '';
385
+ }
386
+
387
+ postMapLoadEvent(tileUrls: Array<string>, mapId: number, skuToken: string, customAccessToken: string) {
388
+ //Enabled only when Mapbox Access Token is set and a source uses
389
+ // mapbox tiles.
390
+ this.skuToken = skuToken;
391
+
392
+ if (config.EVENTS_URL &&
393
+ customAccessToken || config.ACCESS_TOKEN &&
394
+ Array.isArray(tileUrls) &&
395
+ tileUrls.some(url => isMapboxURL(url) || isMapboxHTTPURL(url))) {
396
+ this.queueRequest({id: mapId, timestamp: Date.now()}, customAccessToken);
397
+ }
398
+ }
399
+
400
+ processRequests(customAccessToken?: ?string) {
401
+ if (this.pendingRequest || this.queue.length === 0) return;
402
+ const {id, timestamp} = this.queue.shift();
403
+
404
+ // Only one load event should fire per map
405
+ if (id && this.success[id]) return;
406
+
407
+ if (!this.anonId) {
408
+ this.fetchEventData();
409
+ }
410
+
411
+ if (!validateUuid(this.anonId)) {
412
+ this.anonId = uuid();
413
+ }
414
+
415
+ this.postEvent(timestamp, {skuToken: this.skuToken}, (err) => {
416
+ if (!err) {
417
+ if (id) this.success[id] = true;
418
+ }
419
+ }, customAccessToken);
420
+ }
421
+ }
422
+
423
+ export class TurnstileEvent extends TelemetryEvent {
424
+ constructor(customAccessToken?: ?string) {
425
+ super('appUserTurnstile');
426
+ this._customAccessToken = customAccessToken;
427
+ }
428
+
429
+ postTurnstileEvent(tileUrls: Array<string>, customAccessToken?: ?string) {
430
+ //Enabled only when Mapbox Access Token is set and a source uses
431
+ // mapbox tiles.
432
+ if (config.EVENTS_URL &&
433
+ config.ACCESS_TOKEN &&
434
+ Array.isArray(tileUrls) &&
435
+ tileUrls.some(url => isMapboxURL(url) || isMapboxHTTPURL(url))) {
436
+ this.queueRequest(Date.now(), customAccessToken);
437
+ }
438
+ }
439
+
440
+ processRequests(customAccessToken?: ?string) {
441
+ if (this.pendingRequest || this.queue.length === 0) {
442
+ return;
443
+ }
444
+
445
+ if (!this.anonId || !this.eventData.lastSuccess || !this.eventData.tokenU) {
446
+ //Retrieve cached data
447
+ this.fetchEventData();
448
+ }
449
+
450
+ const tokenData = parseAccessToken(config.ACCESS_TOKEN);
451
+ const tokenU = tokenData ? tokenData['u'] : config.ACCESS_TOKEN;
452
+ //Reset event data cache if the access token owner changed.
453
+ let dueForEvent = tokenU !== this.eventData.tokenU;
454
+
455
+ if (!validateUuid(this.anonId)) {
456
+ this.anonId = uuid();
457
+ dueForEvent = true;
458
+ }
459
+
460
+ const nextUpdate = this.queue.shift();
461
+ // Record turnstile event once per calendar day.
462
+ if (this.eventData.lastSuccess) {
463
+ const lastUpdate = new Date(this.eventData.lastSuccess);
464
+ const nextDate = new Date(nextUpdate);
465
+ const daysElapsed = (nextUpdate - this.eventData.lastSuccess) / (24 * 60 * 60 * 1000);
466
+ dueForEvent = dueForEvent || daysElapsed >= 1 || daysElapsed < -1 || lastUpdate.getDate() !== nextDate.getDate();
467
+ } else {
468
+ dueForEvent = true;
469
+ }
470
+
471
+ if (!dueForEvent) {
472
+ return this.processRequests();
473
+ }
474
+
475
+ this.postEvent(nextUpdate, {"enabled.telemetry": false}, (err) => {
476
+ if (!err) {
477
+ this.eventData.lastSuccess = nextUpdate;
478
+ this.eventData.tokenU = tokenU;
479
+ }
480
+ }, customAccessToken);
481
+ }
482
+ }
483
+
484
+ const turnstileEvent_ = new TurnstileEvent();
485
+ export const postTurnstileEvent = turnstileEvent_.postTurnstileEvent.bind(turnstileEvent_);
486
+
487
+ const mapLoadEvent_ = new MapLoadEvent();
488
+ export const postMapLoadEvent = mapLoadEvent_.postMapLoadEvent.bind(mapLoadEvent_);
489
+
490
+ /***** END WARNING - REMOVAL OR MODIFICATION OF THE
491
+ PRECEDING CODE VIOLATES THE MAPBOX TERMS OF SERVICE ******/
@@ -0,0 +1,14 @@
1
+ // @flow
2
+ import window from './window';
3
+
4
+ let supportsOffscreenCanvas: ?boolean;
5
+
6
+ export default function offscreenCanvasSupported(): boolean {
7
+ if (supportsOffscreenCanvas == null) {
8
+ supportsOffscreenCanvas = window.OffscreenCanvas &&
9
+ new window.OffscreenCanvas(1, 1).getContext('2d') &&
10
+ typeof window.createImageBitmap === 'function';
11
+ }
12
+
13
+ return supportsOffscreenCanvas;
14
+ }
@@ -0,0 +1,112 @@
1
+ // @flow
2
+
3
+ import window from '../util/window';
4
+ import type {RequestParameters} from '../util/ajax';
5
+
6
+ const performance = window.performance;
7
+
8
+ export type PerformanceMetrics = {
9
+ loadTime: number,
10
+ fullLoadTime: number,
11
+ fps: number,
12
+ percentDroppedFrames: number
13
+ }
14
+
15
+ export const PerformanceMarkers = {
16
+ create: 'create',
17
+ load: 'load',
18
+ fullLoad: 'fullLoad'
19
+ };
20
+
21
+ let lastFrameTime = null;
22
+ let frameTimes = [];
23
+
24
+ const minFramerateTarget = 30;
25
+ const frameTimeTarget = 1000 / minFramerateTarget;
26
+
27
+ export const PerformanceUtils = {
28
+ mark(marker: $Keys<typeof PerformanceMarkers>) {
29
+ performance.mark(marker);
30
+ },
31
+ frame(timestamp: number) {
32
+ const currTimestamp = timestamp;
33
+ if (lastFrameTime != null) {
34
+ const frameTime = currTimestamp - lastFrameTime;
35
+ frameTimes.push(frameTime);
36
+ }
37
+ lastFrameTime = currTimestamp;
38
+ },
39
+ clearMetrics() {
40
+ lastFrameTime = null;
41
+ frameTimes = [];
42
+ performance.clearMeasures('loadTime');
43
+ performance.clearMeasures('fullLoadTime');
44
+
45
+ for (const marker in PerformanceMarkers) {
46
+ performance.clearMarks(PerformanceMarkers[marker]);
47
+ }
48
+ },
49
+ getPerformanceMetrics(): PerformanceMetrics {
50
+ const loadTime = performance.measure('loadTime', PerformanceMarkers.create, PerformanceMarkers.load).duration;
51
+ const fullLoadTime = performance.measure('fullLoadTime', PerformanceMarkers.create, PerformanceMarkers.fullLoad).duration;
52
+ const totalFrames = frameTimes.length;
53
+
54
+ const avgFrameTime = frameTimes.reduce((prev, curr) => prev + curr, 0) / totalFrames / 1000;
55
+ const fps = 1 / avgFrameTime;
56
+
57
+ // count frames that missed our framerate target
58
+ const droppedFrames = frameTimes
59
+ .filter((frameTime) => frameTime > frameTimeTarget)
60
+ .reduce((acc, curr) => {
61
+ return acc + (curr - frameTimeTarget) / frameTimeTarget;
62
+ }, 0);
63
+ const percentDroppedFrames = (droppedFrames / (totalFrames + droppedFrames)) * 100;
64
+
65
+ return {
66
+ loadTime,
67
+ fullLoadTime,
68
+ fps,
69
+ percentDroppedFrames
70
+ };
71
+ }
72
+ };
73
+
74
+ /**
75
+ * Safe wrapper for the performance resource timing API in web workers with graceful degradation
76
+ *
77
+ * @param {RequestParameters} request
78
+ * @private
79
+ */
80
+ export class RequestPerformance {
81
+ _marks: {start: string, end: string, measure: string};
82
+
83
+ constructor (request: RequestParameters) {
84
+ this._marks = {
85
+ start: [request.url, 'start'].join('#'),
86
+ end: [request.url, 'end'].join('#'),
87
+ measure: request.url.toString()
88
+ };
89
+
90
+ performance.mark(this._marks.start);
91
+ }
92
+
93
+ finish() {
94
+ performance.mark(this._marks.end);
95
+ let resourceTimingData = performance.getEntriesByName(this._marks.measure);
96
+
97
+ // fallback if web worker implementation of perf.getEntriesByName returns empty
98
+ if (resourceTimingData.length === 0) {
99
+ performance.measure(this._marks.measure, this._marks.start, this._marks.end);
100
+ resourceTimingData = performance.getEntriesByName(this._marks.measure);
101
+
102
+ // cleanup
103
+ performance.clearMarks(this._marks.start);
104
+ performance.clearMarks(this._marks.end);
105
+ performance.clearMeasures(this._marks.measure);
106
+ }
107
+
108
+ return resourceTimingData;
109
+ }
110
+ }
111
+
112
+ export default performance;