@salesforce/core 4.3.2 → 4.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,15 +1,10 @@
1
1
  [![NPM](https://img.shields.io/npm/v/@salesforce/core.svg)](https://www.npmjs.com/package/@salesforce/core)
2
- [![CircleCI](https://circleci.com/gh/forcedotcom/sfdx-core.svg?style=svg&circle-token=2377ca31221869e9d13448313620486da80e595f)](https://circleci.com/gh/forcedotcom/sfdx-core)
3
2
 
4
3
  - [Description](#description)
5
4
  - [Usage](#usage)
6
5
  - [Contributing](#contributing)
7
6
  - [Using TestSetup](#using-testsetup)
8
- - [Mocking Authorizations](#mocking-authorizations)
9
- - [Mocking Config Files](#mocking-config-files)
10
- - [Using the Built-in Sinon Sandboxes](#using-the-built-in-sinon-sandboxes)
11
- - [Testing Expected Failures](#testing-expected-failures)
12
- - [Testing Log Lines](#testing-log-lines)
7
+ - [Message Transformer](#message-transformer)
13
8
 
14
9
  # Description
15
10
 
@@ -21,213 +16,21 @@ See the [API documentation](https://forcedotcom.github.io/sfdx-core/).
21
16
 
22
17
  ## Contributing
23
18
 
24
- If you are interested in contributing, please take a look at the [CONTRIBUTING](CONTRIBUTING.md) guide.
19
+ If you're interested in contributing, take a look at the [CONTRIBUTING](CONTRIBUTING.md) guide.
25
20
 
26
21
  ## Issues
27
22
 
28
- Please report all issues to [issues only repository](https://github.com/forcedotcom/cli/issues).
23
+ Report all issues to the [issues only repository](https://github.com/forcedotcom/cli/issues).
29
24
 
30
25
  # Using TestSetup
31
26
 
32
27
  The Salesforce DX Core Library provides a unit testing utility to help with mocking and sand-boxing core components. This feature allows unit tests to execute without needing to make API calls to salesforce.com.
33
28
 
34
- ## Mocking Authorizations
35
-
36
- Here you can mock authorization for a Salesforce scratch org.
37
-
38
- ```typescript
39
- import { strictEqual } from 'assert';
40
- import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
41
- import { AuthInfo } from '@salesforce/core';
42
-
43
- describe('Mocking Auth data', () => {
44
- const $$ = new TestContext();
45
- it('example', async () => {
46
- const testData = new MockTestOrgData();
47
- await $$.stubAuths(testData);
48
- const auth = await AuthInfo.create({ username: testData.username });
49
- strictEqual(auth.getUsername(), testData.username);
50
- });
51
- });
52
- ```
53
-
54
- After having a valid AuthInfo object you can then create fake connections to a Salesforce.com scratch org. This allows for writing tests that can validate result responses for SOQL queries and REST endpoints.
55
-
56
- ```typescript
57
- import { AuthInfo, Connection, SfError } from '@salesforce/core';
58
- import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
59
- import { AnyJson, ensureJsonMap, JsonMap } from '@salesforce/ts-types';
60
- import { ensureString } from '@salesforce/ts-types';
61
- import { deepStrictEqual } from 'assert';
62
- import { QueryResult } from 'jsforce';
63
-
64
- describe('Mocking a force server call', () => {
65
- const $$ = new TestContext();
66
- it('example', async () => {
67
- const records: AnyJson = { records: ['123456', '234567'] };
68
- const testData = new MockTestOrgData();
69
- await $$.stubAuths(testData);
70
- $$.fakeConnectionRequest = (request: AnyJson): Promise<AnyJson> => {
71
- const _request = ensureJsonMap(request);
72
- if (request && ensureString(_request.url).includes('Account')) {
73
- return Promise.resolve(records);
74
- } else {
75
- return Promise.reject(new SfError(`Unexpected request: ${_request.url}`));
76
- }
77
- };
78
- const connection = await Connection.create({
79
- authInfo: await AuthInfo.create({ username: testData.username }),
80
- });
81
- const result = await connection.query('select Id From Account');
82
- deepStrictEqual(result, records);
83
- });
84
- });
85
- ```
86
-
87
- ## Mocking Config Files
88
-
89
- You can mock the contents of various config files
90
-
91
- ```typescript
92
- import { strictEqual } from 'assert';
93
- import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
94
- import { StateAggregator, OrgConfigProperties } from '@salesforce/core';
95
-
96
- describe('Mocking Aliases', () => {
97
- const $$ = new TestContext();
98
- it('example', async () => {
99
- const testData = new MockTestOrgData();
100
- await $$.stubAliases({ myAlias: testData.username });
101
- const alias = (await StateAggregator.getInstance()).aliases.get(testData.username);
102
- strictEqual(alias, 'myAlias');
103
- });
104
- });
105
-
106
- describe('Mocking Config', () => {
107
- it('example', async () => {
108
- const testData = new MockTestOrgData();
109
- await $$.stubConfig({ [OrgConfigProperties.TARGET_ORG]: testData.username });
110
- const { value } = (await ConfigAggregator.create()).getInfo(OrgConfigProperties.TARGET_ORG);
111
- strictEqual(value, testData.username);
112
- });
113
- });
114
-
115
- describe('Mocking Arbitrary Config Files', () => {
116
- it('example', async () => {
117
- // MyConfigFile must extend the ConfigFile class in order for this to work properly.
118
- $$.setConfigStubContents('MyConfigFile', { contents: { foo: 'bar' } });
119
- });
120
- });
121
- ```
122
-
123
- ## Using the Built-in Sinon Sandboxes
124
-
125
- sfdx-core uses Sinon as its underlying mocking system. If you're unfamiliar with Sinon and its sandboxing concept you can find more information here:
126
- https://sinonjs.org/
127
- Sinon `stub`s and `spy`s must be cleaned up after test invocations. To ease the use of Sinon with sfdx core we've exposed our sandbox in TestSetup. After adding your own `stub`s and/or `spy`s they will automatically be cleaned up after each test using mocha's afterEach method.
128
-
129
- ```typescript
130
- import { strictEqual } from 'assert';
131
-
132
- import { TestContext } from '@salesforce/core/lib/testSetup';
133
- import * as os from 'os';
134
-
135
- describe('Using the built in Sinon sandbox.', () => {
136
- const $$ = new TestContext();
137
- it('example', async () => {
138
- const unsupportedOS = 'LEO';
139
- $$.SANDBOX.stub(os, 'platform').returns(unsupportedOS);
140
- strictEqual(os.platform(), unsupportedOS);
141
- });
142
- });
143
- ```
144
-
145
- ## Testing Expected Failures
146
-
147
- It's important to have negative tests that ensure proper error handling. With `shouldThrow` it's easy to test for expected async rejections.
148
-
149
- ```typescript
150
- import { SfError } from '@salesforce/core';
151
- import { shouldThrow } from '@salesforce/core/lib/testSetup';
152
- import { strictEqual } from 'assert';
153
-
154
- class TestObject {
155
- public static async method() {
156
- throw new SfError('Error', 'ExpectedError');
157
- }
158
- }
159
-
160
- describe('Testing for expected errors', () => {
161
- it('example', async () => {
162
- try {
163
- await shouldThrow(TestObject.method());
164
- } catch (e) {
165
- strictEqual(e.name, 'ExpectedError');
166
- }
167
- });
168
- });
169
- ```
170
-
171
- You can also use `shouldThrowSync` for syncrhonous functions you expect to fail
172
-
173
- ```typescript
174
- import { SfError } from '@salesforce/core';
175
- import { shouldThrowSync } from '@salesforce/core/lib/testSetup';
176
- import { strictEqual } from 'assert';
177
-
178
- class TestObject {
179
- public static method() {
180
- throw new SfError('Error', 'ExpectedError');
181
- }
182
- }
183
-
184
- describe('Testing for expected errors', () => {
185
- it('example', async () => {
186
- try {
187
- shouldThrowSync(() => TestObject.method());
188
- } catch (e) {
189
- strictEqual(e.name, 'ExpectedError');
190
- }
191
- });
192
- });
193
- ```
194
-
195
- ## Testing Log Lines
196
-
197
- It's also useful to check expected values and content from log lines. TestSetup configures the sfdx-core logger to use an in memory LogLine storage structure. These can be easily accessed from tests.
198
-
199
- ```typescript
200
- import { Logger, LogLine } from '@salesforce/core';
201
- import { TestContext } from '@salesforce/core/lib/testSetup';
202
- import { strictEqual } from 'assert';
203
-
204
- const TEST_STRING = 'foo was here';
205
-
206
- class TestObject {
207
- constructor(private logger: Logger) {
208
- this.logger = logger.child('TestObject');
209
- }
210
-
211
- public method() {
212
- this.logger.error(TEST_STRING);
213
- }
214
- }
215
-
216
- describe('Testing log lines', () => {
217
- const $$ = new TestContext();
218
- it('example', async () => {
219
- const obj = new TestObject($$.TEST_LOGGER);
220
- obj.method();
221
- const records = $$.TEST_LOGGER.getBufferedRecords();
222
- strictEqual(records.length, 1);
223
- strictEqual(records[0].msg, TEST_STRING);
224
- });
225
- });
226
- ```
29
+ See the [Test Setup documentation](TEST_SETUP.md).
227
30
 
228
31
  ## Message Transformer
229
32
 
230
- the Messages class, by default, will load message text during run time. It's optimized to do this only per file.
33
+ The Messages class, by default, loads message text during run time. It's optimized to do this only per file.
231
34
 
232
35
  If you're using @salesforce/core or other code that uses its Messages class in a bundler (webpack, esbuild, etc) it may struggle with these runtime references.
233
36
 
@@ -147,24 +147,25 @@ class ConfigFile extends configStore_1.BaseConfigStore {
147
147
  // internally and updated persistently via write().
148
148
  if (!this.hasRead || force) {
149
149
  this.logger.info(`Reading config file: ${this.getPath()}`);
150
- const obj = (0, kit_1.parseJsonMap)(await fs.promises.readFile(this.getPath(), 'utf8'));
150
+ const obj = (0, kit_1.parseJsonMap)(await fs.promises.readFile(this.getPath(), 'utf8'), this.getPath());
151
151
  this.setContentsFromObject(obj);
152
152
  }
153
+ // Necessarily set this even when an error happens to avoid infinite re-reading.
154
+ // To attempt another read, pass `force=true`.
155
+ this.hasRead = true;
153
156
  return this.getContents();
154
157
  }
155
158
  catch (err) {
159
+ this.hasRead = true;
156
160
  if (err.code === 'ENOENT') {
157
161
  if (!throwOnNotFound) {
158
162
  this.setContents();
159
163
  return this.getContents();
160
164
  }
161
165
  }
162
- throw err;
163
- }
164
- finally {
165
166
  // Necessarily set this even when an error happens to avoid infinite re-reading.
166
167
  // To attempt another read, pass `force=true`.
167
- this.hasRead = true;
168
+ throw err;
168
169
  }
169
170
  }
170
171
  /**
@@ -1,6 +1,6 @@
1
1
  import { AsyncOptionalCreatable } from '@salesforce/kit';
2
2
  import { Nullable } from '@salesforce/ts-types';
3
- import { JwtOAuth2Config, OAuth2 } from 'jsforce';
3
+ import { OAuth2Config, OAuth2 } from 'jsforce';
4
4
  import { Connection } from './connection';
5
5
  import { Org } from './org';
6
6
  /**
@@ -67,6 +67,13 @@ export type AuthSideEffects = {
67
67
  setDefaultDevHub: boolean;
68
68
  setTracksSource?: boolean;
69
69
  };
70
+ export type JwtOAuth2Config = OAuth2Config & {
71
+ privateKey?: string;
72
+ privateKeyFile?: string;
73
+ authCode?: string;
74
+ refreshToken?: string;
75
+ username?: string;
76
+ };
70
77
  /**
71
78
  * A function to update a refresh token when the access token is expired.
72
79
  */
@@ -702,10 +702,12 @@ class AuthInfo extends kit_1.AsyncOptionalCreatable {
702
702
  }, privateKeyContents, {
703
703
  algorithm: 'RS256',
704
704
  });
705
- const oauth2 = new jsforce_1.JwtOAuth2({ loginUrl });
706
- // jsforce has it types as any
707
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
708
- return (0, ts_types_1.ensureJsonMap)(await oauth2.jwtAuthorize(jwtToken));
705
+ const oauth2 = new jsforce_1.OAuth2({ loginUrl });
706
+ return (0, ts_types_1.ensureJsonMap)(await oauth2.requestToken({
707
+ // eslint-disable-next-line camelcase
708
+ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
709
+ assertion: jwtToken,
710
+ }));
709
711
  }
710
712
  // Build OAuth config for a refresh token auth flow
711
713
  async buildRefreshTokenConfig(options) {
@@ -3,7 +3,7 @@ import { ScratchOrgInfo } from './scratchOrgTypes';
3
3
  export declare const scratchOrgLifecycleEventName = "scratchOrgLifecycleEvent";
4
4
  export declare const scratchOrgLifecycleStages: readonly ["prepare request", "send request", "wait for org", "available", "authenticate", "deploy settings", "done"];
5
5
  export interface ScratchOrgLifecycleEvent {
6
- stage: typeof scratchOrgLifecycleStages[number];
6
+ stage: (typeof scratchOrgLifecycleStages)[number];
7
7
  scratchOrgInfo?: ScratchOrgInfo;
8
8
  }
9
9
  export declare const emit: (event: ScratchOrgLifecycleEvent) => Promise<void>;
@@ -14,6 +14,7 @@ const authInfoConfig_1 = require("../../config/authInfoConfig");
14
14
  const global_1 = require("../../global");
15
15
  const logger_1 = require("../../logger");
16
16
  const messages_1 = require("../../messages");
17
+ const lifecycleEvents_1 = require("../../lifecycleEvents");
17
18
  function chunk(array, chunkSize) {
18
19
  const final = [];
19
20
  for (let i = 0, len = array.length; i < len; i += chunkSize)
@@ -40,6 +41,9 @@ class BaseOrgAccessor extends kit_1.AsyncOptionalCreatable {
40
41
  return this.get(username, decrypt);
41
42
  }
42
43
  catch (err) {
44
+ if (err instanceof Error && err.name === 'JsonParseError') {
45
+ throw err;
46
+ }
43
47
  return null;
44
48
  }
45
49
  }
@@ -53,8 +57,13 @@ class BaseOrgAccessor extends kit_1.AsyncOptionalCreatable {
53
57
  for (const fileChunk of fileChunks) {
54
58
  const promises = fileChunk.map(async (f) => {
55
59
  const username = this.parseUsername(f);
56
- const config = await this.initAuthFile(username);
57
- this.configs.set(username, config);
60
+ try {
61
+ const config = await this.initAuthFile(username);
62
+ this.configs.set(username, config);
63
+ }
64
+ catch (e) {
65
+ await lifecycleEvents_1.Lifecycle.getInstance().emitWarning(`The auth file for ${username} is invalid.`);
66
+ }
58
67
  });
59
68
  // eslint-disable-next-line no-await-in-loop
60
69
  await Promise.all(promises);
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  import * as http from 'http';
3
- import { JwtOAuth2Config } from 'jsforce';
4
3
  import { AsyncCreatable } from '@salesforce/kit';
5
4
  import { AuthInfo } from './org';
5
+ import { JwtOAuth2Config } from './org/authInfo';
6
6
  /**
7
7
  * Handles the creation of a web server for web based login flows.
8
8
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/core",
3
- "version": "4.3.2",
3
+ "version": "4.3.4",
4
4
  "description": "Core libraries to interact with SFDX projects, orgs, and APIs.",
5
5
  "main": "lib/exported",
6
6
  "types": "lib/exported.d.ts",
@@ -49,7 +49,7 @@
49
49
  "faye": "^1.4.0",
50
50
  "form-data": "^4.0.0",
51
51
  "js2xmlparser": "^4.0.1",
52
- "jsforce": "^2.0.0-beta.24",
52
+ "jsforce": "^2.0.0-beta.27",
53
53
  "jsonwebtoken": "9.0.0",
54
54
  "jszip": "3.10.1",
55
55
  "proper-lockfile": "^4.1.2",