Skip to content

Commit 56e8fc6

Browse files
fix tests
1 parent 5321999 commit 56e8fc6

File tree

5 files changed

+278
-30
lines changed

5 files changed

+278
-30
lines changed

Diff for: main.js

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
"use strict";
2+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7+
var _, done = false;
8+
for (var i = decorators.length - 1; i >= 0; i--) {
9+
var context = {};
10+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14+
if (kind === "accessor") {
15+
if (result === void 0) continue;
16+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17+
if (_ = accept(result.get)) descriptor.get = _;
18+
if (_ = accept(result.set)) descriptor.set = _;
19+
if (_ = accept(result.init)) initializers.push(_);
20+
}
21+
else if (_ = accept(result)) {
22+
if (kind === "field") initializers.push(_);
23+
else descriptor[key] = _;
24+
}
25+
}
26+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
27+
done = true;
28+
};
29+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30+
var useValue = arguments.length > 2;
31+
for (var i = 0; i < initializers.length; i++) {
32+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33+
}
34+
return useValue ? value : void 0;
35+
};
36+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38+
return new (P || (P = Promise))(function (resolve, reject) {
39+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42+
step((generator = generator.apply(thisArg, _arguments || [])).next());
43+
});
44+
};
45+
var __generator = (this && this.__generator) || function (thisArg, body) {
46+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48+
function verb(n) { return function (v) { return step([n, v]); }; }
49+
function step(op) {
50+
if (f) throw new TypeError("Generator is already executing.");
51+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
52+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53+
if (y = 0, t) op = [op[0] & 2, t.value];
54+
switch (op[0]) {
55+
case 0: case 1: t = op; break;
56+
case 4: _.label++; return { value: op[1], done: false };
57+
case 5: _.label++; y = op[1]; op = [0]; continue;
58+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
59+
default:
60+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64+
if (t[2]) _.ops.pop();
65+
_.trys.pop(); continue;
66+
}
67+
op = body.call(thisArg, _);
68+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70+
}
71+
};
72+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
73+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
74+
if (ar || !(i in from)) {
75+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
76+
ar[i] = from[i];
77+
}
78+
}
79+
return to.concat(ar || Array.prototype.slice.call(from));
80+
};
81+
var _this = this;
82+
Object.defineProperty(exports, "__esModule", { value: true });
83+
var promises_1 = require("timers/promises");
84+
function loggedMethod(originalMethod, _context) {
85+
console.error(_context);
86+
console.error(String(_context.name));
87+
function replacementMethod() {
88+
var args = [];
89+
for (var _i = 0; _i < arguments.length; _i++) {
90+
args[_i] = arguments[_i];
91+
}
92+
console.log("LOG: Entering method.");
93+
var result = originalMethod.call.apply(originalMethod, __spreadArray([this], args, false));
94+
console.log("LOG: Exiting method.");
95+
return result;
96+
}
97+
return replacementMethod;
98+
}
99+
function lock(originalMethod, _context) {
100+
var cachedPromise = null;
101+
function replacementMethod() {
102+
var args = [];
103+
for (var _i = 0; _i < arguments.length; _i++) {
104+
args[_i] = arguments[_i];
105+
}
106+
return __awaiter(this, void 0, void 0, function () {
107+
var returnValue;
108+
return __generator(this, function (_a) {
109+
switch (_a.label) {
110+
case 0:
111+
console.error({ cachedPromise: cachedPromise });
112+
cachedPromise !== null && cachedPromise !== void 0 ? cachedPromise : (cachedPromise = originalMethod.call.apply(originalMethod, __spreadArray([this], args, false)));
113+
return [4 /*yield*/, cachedPromise];
114+
case 1:
115+
returnValue = _a.sent();
116+
cachedPromise = null;
117+
return [2 /*return*/, returnValue];
118+
}
119+
});
120+
});
121+
}
122+
return replacementMethod;
123+
}
124+
function lazyInit(_a, _b) {
125+
var get = _a.get, set = _a.set;
126+
var name = _b.name, kind = _b.kind;
127+
if (kind === 'accessor') {
128+
var initializedValue_1 = null;
129+
return {
130+
get: function () {
131+
!initializedValue_1 && (initializedValue_1 = get.call(this));
132+
return initializedValue_1;
133+
}
134+
};
135+
}
136+
}
137+
function number(value, _context) {
138+
return function replacement() {
139+
var returnValue = value.call(this);
140+
return Number(returnValue);
141+
};
142+
}
143+
var ExampleClass = function () {
144+
var _a;
145+
var _instanceExtraInitializers = [];
146+
var _fooBar_decorators;
147+
var _promiseReturns_decorators;
148+
var _get_myField_decorators;
149+
return _a = /** @class */ (function () {
150+
function ExampleClass() {
151+
__runInitializers(this, _instanceExtraInitializers);
152+
}
153+
ExampleClass.prototype.fooBar = function () {
154+
console.log('evaluating method');
155+
};
156+
ExampleClass.prototype.promiseReturns = function () {
157+
return __awaiter(this, void 0, void 0, function () {
158+
return __generator(this, function (_a) {
159+
switch (_a.label) {
160+
case 0:
161+
console.log('starting execution');
162+
return [4 /*yield*/, (0, promises_1.setTimeout)(1000)];
163+
case 1:
164+
_a.sent();
165+
console.log('finishing execution');
166+
return [2 /*return*/, 3];
167+
}
168+
});
169+
});
170+
};
171+
Object.defineProperty(ExampleClass.prototype, "myField", {
172+
get: function () {
173+
// @ts-expect-error expected error
174+
return '2';
175+
},
176+
enumerable: false,
177+
configurable: true
178+
});
179+
return ExampleClass;
180+
}()),
181+
(function () {
182+
_fooBar_decorators = [loggedMethod];
183+
_promiseReturns_decorators = [lock];
184+
_get_myField_decorators = [number];
185+
__esDecorate(_a, null, _fooBar_decorators, { kind: "method", name: "fooBar", static: false, private: false, access: { has: function (obj) { return "fooBar" in obj; }, get: function (obj) { return obj.fooBar; } } }, null, _instanceExtraInitializers);
186+
__esDecorate(_a, null, _promiseReturns_decorators, { kind: "method", name: "promiseReturns", static: false, private: false, access: { has: function (obj) { return "promiseReturns" in obj; }, get: function (obj) { return obj.promiseReturns; } } }, null, _instanceExtraInitializers);
187+
__esDecorate(_a, null, _get_myField_decorators, { kind: "getter", name: "myField", static: false, private: false, access: { has: function (obj) { return "myField" in obj; }, get: function (obj) { return obj.myField; } } }, null, _instanceExtraInitializers);
188+
})(),
189+
_a;
190+
}();
191+
var x = new ExampleClass();
192+
// x.promiseReturns();
193+
console.log('instantiated class', typeof x.myField);
194+
x.myField;

Diff for: main.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { setTimeout } from "timers/promises";
2+
3+
function loggedMethod(originalMethod: any, _context: ClassMethodDecoratorContext) {
4+
console.error(_context);
5+
console.error(String(_context.name));
6+
function replacementMethod(this: any, ...args: any[]) {
7+
console.log("LOG: Entering method.")
8+
const result = originalMethod.call(this, ...args);
9+
console.log("LOG: Exiting method.")
10+
return result;
11+
}
12+
13+
return replacementMethod;
14+
}
15+
16+
function lock<This, Args extends any[], Return>(
17+
originalMethod: (this: This, args: Args) => Promise<Return>,
18+
_context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Promise<Return>>) {
19+
let cachedPromise: Promise<Return> | null = null;
20+
async function replacementMethod(this: This, ...args: Args): Promise<Return> {
21+
console.error({ cachedPromise })
22+
cachedPromise ??= originalMethod.call(this, ...args);
23+
const returnValue = await cachedPromise;
24+
cachedPromise = null;
25+
return returnValue;
26+
}
27+
28+
return replacementMethod;
29+
}
30+
31+
function lazyInit({ get, set }, { name, kind }) {
32+
if (kind === 'accessor') {
33+
let initializedValue = null;
34+
return {
35+
get() {
36+
!initializedValue && (initializedValue = get.call(this));
37+
return initializedValue;
38+
}
39+
};
40+
}
41+
}
42+
43+
type ValidTypes = 'int' | 'long' | 'string';
44+
45+
function number(value, _context: ClassGetterDecoratorContext) {
46+
return function replacement(this) {
47+
const returnValue = value.call(this);
48+
49+
return Number(returnValue);
50+
}
51+
}
52+
53+
class ExampleClass {
54+
@loggedMethod
55+
fooBar() {
56+
console.log('evaluating method');
57+
}
58+
59+
@lock
60+
async promiseReturns(): Promise<number> {
61+
console.log('starting execution')
62+
await setTimeout(1000);
63+
console.log('finishing execution')
64+
return 3;
65+
}
66+
67+
@number
68+
get myField(): number {
69+
// @ts-expect-error expected error
70+
return '2';
71+
}
72+
}
73+
74+
const x = new ExampleClass();
75+
// x.promiseReturns();
76+
77+
console.log('instantiated class', typeof x.myField);
78+
x.myField;

Diff for: src/client-side-encryption/providers/aws.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { getAwsCredentialProvider } from '../../deps';
2-
import { type KMSProviders } from '.';
31
import { AWSSDKCredentialProvider } from '../../cmap/auth/aws_temporary_credentials';
2+
import { type KMSProviders } from '.';
43

54
/**
65
* @internal

Diff for: test/integration/auth/mongodb_aws.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { performance } from 'perf_hooks';
66
import * as sinon from 'sinon';
77

88
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
9-
import { KMSCredentialProvider, refreshKMSCredentials } from '../../../src/client-side-encryption/providers';
9+
import { refreshKMSCredentials } from '../../../src/client-side-encryption/providers';
1010
import {
1111
AWSTemporaryCredentialProvider,
1212
MongoAWSError,
@@ -17,7 +17,7 @@ import {
1717
setDifference
1818
} from '../../mongodb';
1919

20-
const isMongoDBAWSAuthEnvironment = (process.env.MONGODB_URI ?? '').includes('MONGODB_AWS');
20+
const isMongoDBAWSAuthEnvironment = (process.env.MONGODB_URI ?? '').includes('MONGODB-AWS');
2121

2222
describe('MONGODB-AWS', function () {
2323
let awsSdkPresent;
@@ -337,7 +337,7 @@ describe('AWS KMS Credential Fetching', function () {
337337
this.currentTest?.skipReason && this.skip();
338338
});
339339
it('fetching AWS KMS credentials throws an error', async function () {
340-
const error = await new KMSCredentialProvider({ aws: {} }).refreshCredentials().catch(e => e);
340+
const error = await refreshKMSCredentials({ aws: {} }).catch(e => e);
341341
expect(error).to.be.instanceOf(MongoAWSError);
342342
});
343343
});

Diff for: test/unit/client-side-encryption/providers/credentialsProvider.test.ts

+2-25
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {
2020
} from '../../../../src/client-side-encryption/providers/azure';
2121
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
2222
import * as utils from '../../../../src/client-side-encryption/providers/utils';
23-
import * as requirements from '../requirements.helper';
23+
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
2424
import { AWSSDKCredentialProvider } from '../../../../src/cmap/auth/aws_temporary_credentials';
25-
import { MongoAWSError } from '../../../../src/error';
25+
import * as requirements from '../requirements.helper';
2626

2727
const originalAccessKeyId = process.env.AWS_ACCESS_KEY_ID;
2828
const originalSecretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
@@ -156,29 +156,6 @@ describe('#refreshKMSCredentials', function () {
156156
});
157157
});
158158

159-
context('when the sdk is not installed', function () {
160-
const kmsProviders = {
161-
local: {
162-
key: Buffer.alloc(96)
163-
},
164-
aws: {}
165-
};
166-
167-
before(function () {
168-
if (requirements.credentialProvidersInstalled.aws && this.currentTest) {
169-
this.currentTest.skipReason = 'Credentials will be loaded when sdk present';
170-
this.currentTest.skip();
171-
return;
172-
}
173-
});
174-
175-
it('throws a MongoAWSError', async function () {
176-
const error = await refreshKMSCredentials(kmsProviders).catch(e => e);
177-
const expectedErrorMessage = 'Optional module `@aws-sdk/credential-providers` not found';
178-
expect(error).to.be.instanceOf(MongoAWSError).to.match(new RegExp(expectedErrorMessage, 'i'));
179-
});
180-
});
181-
182159
context('when the AWS SDK returns unknown fields', function () {
183160
beforeEach(() => {
184161
sinon.stub(AWSSDKCredentialProvider.prototype, 'getCredentials').resolves({

0 commit comments

Comments
 (0)