a zip code crypto-currency system good for red ONLY

index.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright 2015 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. (function() {
  18. var nativeAddAll = Cache.prototype.addAll;
  19. var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/);
  20. // Has nice behavior of `var` which everyone hates
  21. if (userAgent) {
  22. var agent = userAgent[1];
  23. var version = parseInt(userAgent[2]);
  24. }
  25. if (
  26. nativeAddAll && (!userAgent ||
  27. (agent === 'Firefox' && version >= 46) ||
  28. (agent === 'Chrome' && version >= 50)
  29. )
  30. ) {
  31. return;
  32. }
  33. Cache.prototype.addAll = function addAll(requests) {
  34. var cache = this;
  35. // Since DOMExceptions are not constructable:
  36. function NetworkError(message) {
  37. this.name = 'NetworkError';
  38. this.code = 19;
  39. this.message = message;
  40. }
  41. NetworkError.prototype = Object.create(Error.prototype);
  42. return Promise.resolve().then(function() {
  43. if (arguments.length < 1) throw new TypeError();
  44. // Simulate sequence<(Request or USVString)> binding:
  45. var sequence = [];
  46. requests = requests.map(function(request) {
  47. if (request instanceof Request) {
  48. return request;
  49. }
  50. else {
  51. return String(request); // may throw TypeError
  52. }
  53. });
  54. return Promise.all(
  55. requests.map(function(request) {
  56. if (typeof request === 'string') {
  57. request = new Request(request);
  58. }
  59. var scheme = new URL(request.url).protocol;
  60. if (scheme !== 'http:' && scheme !== 'https:') {
  61. throw new NetworkError("Invalid scheme");
  62. }
  63. return fetch(request.clone());
  64. })
  65. );
  66. }).then(function(responses) {
  67. // If some of the responses has not OK-eish status,
  68. // then whole operation should reject
  69. if (responses.some(function(response) {
  70. return !response.ok;
  71. })) {
  72. throw new NetworkError('Incorrect response status');
  73. }
  74. // TODO: check that requests don't overwrite one another
  75. // (don't think this is possible to polyfill due to opaque responses)
  76. return Promise.all(
  77. responses.map(function(response, i) {
  78. return cache.put(requests[i], response);
  79. })
  80. );
  81. }).then(function() {
  82. return undefined;
  83. });
  84. };
  85. Cache.prototype.add = function add(request) {
  86. return this.addAll([request]);
  87. };
  88. }());