instrumentation.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.SdkObject = void 0;
  6. exports.createInstrumentation = createInstrumentation;
  7. exports.kTestSdkObjects = void 0;
  8. exports.serverSideCallMetadata = serverSideCallMetadata;
  9. var _events = require("events");
  10. var _utils = require("../utils");
  11. /**
  12. * Copyright (c) Microsoft Corporation. All rights reserved.
  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. const kTestSdkObjects = exports.kTestSdkObjects = new WeakSet();
  27. class SdkObject extends _events.EventEmitter {
  28. constructor(parent, guidPrefix, guid) {
  29. super();
  30. this.guid = void 0;
  31. this.attribution = void 0;
  32. this.instrumentation = void 0;
  33. this.guid = guid || `${guidPrefix || ''}@${(0, _utils.createGuid)()}`;
  34. this.setMaxListeners(0);
  35. this.attribution = {
  36. ...parent.attribution
  37. };
  38. this.instrumentation = parent.instrumentation;
  39. if (process.env._PW_INTERNAL_COUNT_SDK_OBJECTS) kTestSdkObjects.add(this);
  40. }
  41. }
  42. exports.SdkObject = SdkObject;
  43. function createInstrumentation() {
  44. const listeners = new Map();
  45. return new Proxy({}, {
  46. get: (obj, prop) => {
  47. if (typeof prop !== 'string') return obj[prop];
  48. if (prop === 'addListener') return (listener, context) => listeners.set(listener, context);
  49. if (prop === 'removeListener') return listener => listeners.delete(listener);
  50. if (!prop.startsWith('on')) return obj[prop];
  51. return async (sdkObject, ...params) => {
  52. for (const [listener, context] of listeners) {
  53. var _prop, _ref;
  54. if (!context || sdkObject.attribution.context === context) await ((_prop = (_ref = listener)[prop]) === null || _prop === void 0 ? void 0 : _prop.call(_ref, sdkObject, ...params));
  55. }
  56. };
  57. }
  58. });
  59. }
  60. function serverSideCallMetadata() {
  61. return {
  62. id: '',
  63. startTime: 0,
  64. endTime: 0,
  65. wallTime: Date.now(),
  66. type: 'Internal',
  67. method: '',
  68. params: {},
  69. log: [],
  70. isServerSide: true
  71. };
  72. }