Blob.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const BlobPropertyBag = require("./BlobPropertyBag.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const interfaceName = "Blob";
  8. exports.is = value => {
  9. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  10. };
  11. exports.isImpl = value => {
  12. return utils.isObject(value) && value instanceof Impl.implementation;
  13. };
  14. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  15. if (exports.is(value)) {
  16. return utils.implForWrapper(value);
  17. }
  18. throw new globalObject.TypeError(`${context} is not of type 'Blob'.`);
  19. };
  20. function makeWrapper(globalObject, newTarget) {
  21. let proto;
  22. if (newTarget !== undefined) {
  23. proto = newTarget.prototype;
  24. }
  25. if (!utils.isObject(proto)) {
  26. proto = globalObject[ctorRegistrySymbol]["Blob"].prototype;
  27. }
  28. return Object.create(proto);
  29. }
  30. exports.create = (globalObject, constructorArgs, privateData) => {
  31. const wrapper = makeWrapper(globalObject);
  32. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  33. };
  34. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  35. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  36. return utils.implForWrapper(wrapper);
  37. };
  38. exports._internalSetup = (wrapper, globalObject) => {};
  39. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  40. privateData.wrapper = wrapper;
  41. exports._internalSetup(wrapper, globalObject);
  42. Object.defineProperty(wrapper, implSymbol, {
  43. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  44. configurable: true
  45. });
  46. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  47. if (Impl.init) {
  48. Impl.init(wrapper[implSymbol]);
  49. }
  50. return wrapper;
  51. };
  52. exports.new = (globalObject, newTarget) => {
  53. const wrapper = makeWrapper(globalObject, newTarget);
  54. exports._internalSetup(wrapper, globalObject);
  55. Object.defineProperty(wrapper, implSymbol, {
  56. value: Object.create(Impl.implementation.prototype),
  57. configurable: true
  58. });
  59. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  60. if (Impl.init) {
  61. Impl.init(wrapper[implSymbol]);
  62. }
  63. return wrapper[implSymbol];
  64. };
  65. const exposed = new Set(["Window", "Worker"]);
  66. exports.install = (globalObject, globalNames) => {
  67. if (!globalNames.some(globalName => exposed.has(globalName))) {
  68. return;
  69. }
  70. const ctorRegistry = utils.initCtorRegistry(globalObject);
  71. class Blob {
  72. constructor() {
  73. const args = [];
  74. {
  75. let curArg = arguments[0];
  76. if (curArg !== undefined) {
  77. if (!utils.isObject(curArg)) {
  78. throw new globalObject.TypeError("Failed to construct 'Blob': parameter 1" + " is not an iterable object.");
  79. } else {
  80. const V = [];
  81. const tmp = curArg;
  82. for (let nextItem of tmp) {
  83. if (exports.is(nextItem)) {
  84. nextItem = utils.implForWrapper(nextItem);
  85. } else if (utils.isArrayBuffer(nextItem)) {
  86. nextItem = conversions["ArrayBuffer"](nextItem, {
  87. context: "Failed to construct 'Blob': parameter 1" + "'s element",
  88. globals: globalObject
  89. });
  90. } else if (ArrayBuffer.isView(nextItem)) {
  91. nextItem = conversions["ArrayBufferView"](nextItem, {
  92. context: "Failed to construct 'Blob': parameter 1" + "'s element",
  93. globals: globalObject
  94. });
  95. } else {
  96. nextItem = conversions["USVString"](nextItem, {
  97. context: "Failed to construct 'Blob': parameter 1" + "'s element",
  98. globals: globalObject
  99. });
  100. }
  101. V.push(nextItem);
  102. }
  103. curArg = V;
  104. }
  105. }
  106. args.push(curArg);
  107. }
  108. {
  109. let curArg = arguments[1];
  110. curArg = BlobPropertyBag.convert(globalObject, curArg, { context: "Failed to construct 'Blob': parameter 2" });
  111. args.push(curArg);
  112. }
  113. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  114. }
  115. slice() {
  116. const esValue = this !== null && this !== undefined ? this : globalObject;
  117. if (!exports.is(esValue)) {
  118. throw new globalObject.TypeError("'slice' called on an object that is not a valid instance of Blob.");
  119. }
  120. const args = [];
  121. {
  122. let curArg = arguments[0];
  123. if (curArg !== undefined) {
  124. curArg = conversions["long long"](curArg, {
  125. context: "Failed to execute 'slice' on 'Blob': parameter 1",
  126. globals: globalObject,
  127. clamp: true
  128. });
  129. }
  130. args.push(curArg);
  131. }
  132. {
  133. let curArg = arguments[1];
  134. if (curArg !== undefined) {
  135. curArg = conversions["long long"](curArg, {
  136. context: "Failed to execute 'slice' on 'Blob': parameter 2",
  137. globals: globalObject,
  138. clamp: true
  139. });
  140. }
  141. args.push(curArg);
  142. }
  143. {
  144. let curArg = arguments[2];
  145. if (curArg !== undefined) {
  146. curArg = conversions["DOMString"](curArg, {
  147. context: "Failed to execute 'slice' on 'Blob': parameter 3",
  148. globals: globalObject
  149. });
  150. }
  151. args.push(curArg);
  152. }
  153. return utils.tryWrapperForImpl(esValue[implSymbol].slice(...args));
  154. }
  155. text() {
  156. try {
  157. const esValue = this !== null && this !== undefined ? this : globalObject;
  158. if (!exports.is(esValue)) {
  159. throw new globalObject.TypeError("'text' called on an object that is not a valid instance of Blob.");
  160. }
  161. return utils.tryWrapperForImpl(esValue[implSymbol].text());
  162. } catch (e) {
  163. return globalObject.Promise.reject(e);
  164. }
  165. }
  166. arrayBuffer() {
  167. try {
  168. const esValue = this !== null && this !== undefined ? this : globalObject;
  169. if (!exports.is(esValue)) {
  170. throw new globalObject.TypeError("'arrayBuffer' called on an object that is not a valid instance of Blob.");
  171. }
  172. return utils.tryWrapperForImpl(esValue[implSymbol].arrayBuffer());
  173. } catch (e) {
  174. return globalObject.Promise.reject(e);
  175. }
  176. }
  177. bytes() {
  178. try {
  179. const esValue = this !== null && this !== undefined ? this : globalObject;
  180. if (!exports.is(esValue)) {
  181. throw new globalObject.TypeError("'bytes' called on an object that is not a valid instance of Blob.");
  182. }
  183. return utils.tryWrapperForImpl(esValue[implSymbol].bytes());
  184. } catch (e) {
  185. return globalObject.Promise.reject(e);
  186. }
  187. }
  188. get size() {
  189. const esValue = this !== null && this !== undefined ? this : globalObject;
  190. if (!exports.is(esValue)) {
  191. throw new globalObject.TypeError("'get size' called on an object that is not a valid instance of Blob.");
  192. }
  193. return esValue[implSymbol]["size"];
  194. }
  195. get type() {
  196. const esValue = this !== null && this !== undefined ? this : globalObject;
  197. if (!exports.is(esValue)) {
  198. throw new globalObject.TypeError("'get type' called on an object that is not a valid instance of Blob.");
  199. }
  200. return esValue[implSymbol]["type"];
  201. }
  202. }
  203. Object.defineProperties(Blob.prototype, {
  204. slice: { enumerable: true },
  205. text: { enumerable: true },
  206. arrayBuffer: { enumerable: true },
  207. bytes: { enumerable: true },
  208. size: { enumerable: true },
  209. type: { enumerable: true },
  210. [Symbol.toStringTag]: { value: "Blob", configurable: true }
  211. });
  212. ctorRegistry[interfaceName] = Blob;
  213. Object.defineProperty(globalObject, interfaceName, {
  214. configurable: true,
  215. writable: true,
  216. value: Blob
  217. });
  218. };
  219. const Impl = require("../../jsdom/living/file-api/Blob-impl.js");