writableStreamDispatcher.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.WritableStreamDispatcher = void 0;
  6. var _dispatcher = require("./dispatcher");
  7. var fs = _interopRequireWildcard(require("fs"));
  8. var _utils = require("../../utils");
  9. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  10. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the 'License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class WritableStreamDispatcher extends _dispatcher.Dispatcher {
  27. constructor(scope, stream, lastModifiedMs) {
  28. super(scope, {
  29. guid: 'writableStream@' + (0, _utils.createGuid)(),
  30. stream
  31. }, 'WritableStream', {});
  32. this._type_WritableStream = true;
  33. this._lastModifiedMs = void 0;
  34. this._lastModifiedMs = lastModifiedMs;
  35. }
  36. async write(params) {
  37. const stream = this._object.stream;
  38. await new Promise((fulfill, reject) => {
  39. stream.write(params.binary, error => {
  40. if (error) reject(error);else fulfill();
  41. });
  42. });
  43. }
  44. async close() {
  45. const stream = this._object.stream;
  46. await new Promise(fulfill => stream.end(fulfill));
  47. if (this._lastModifiedMs) await fs.promises.utimes(this.path(), new Date(this._lastModifiedMs), new Date(this._lastModifiedMs));
  48. }
  49. path() {
  50. return this._object.stream.path;
  51. }
  52. }
  53. exports.WritableStreamDispatcher = WritableStreamDispatcher;