@simplysf/simply-data 2.3.25 → 2.3.27
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 +12 -4
- package/lib/common/contentVersionUtils.d.ts +10 -3
- package/lib/common/contentVersionUtils.js +65 -19
- package/lib/common/contentVersionUtils.js.map +1 -1
- package/lib/common/multipart.d.ts +58 -0
- package/lib/common/multipart.js +92 -0
- package/lib/common/multipart.js.map +1 -0
- package/messages/simply.data.file.upload.md +3 -1
- package/messages/simply.data.files.upload.md +2 -0
- package/package.json +2 -4
package/README.md
CHANGED
|
@@ -37,7 +37,8 @@ FLAGS
|
|
|
37
37
|
-o, --target-org=<value> (required) Username or alias of the target org. Not required if the
|
|
38
38
|
`target-org` configuration variable is already set.
|
|
39
39
|
--api-version=<value> Override the api version used for api requests made by this command
|
|
40
|
-
--file-path=<value> (required) Path to the file to upload.
|
|
40
|
+
--file-path=<value> (required) Path to the file to upload. May be relative or absolute; only the
|
|
41
|
+
file's name is sent to the org.
|
|
41
42
|
--first-publish-location-id=<value> Specify a record Id that the file should be linked to.
|
|
42
43
|
--title=<value> Specify the title for the file being uploaded.
|
|
43
44
|
|
|
@@ -50,13 +51,16 @@ DESCRIPTION
|
|
|
50
51
|
|
|
51
52
|
Uploads a file to a Salesforce org.
|
|
52
53
|
|
|
54
|
+
Only the file's name is sent to the org — Salesforce stores it as the ContentVersion's PathOnClient and derives
|
|
55
|
+
FileExtension and FileType from it — so the local directory the file came from is never uploaded.
|
|
56
|
+
|
|
53
57
|
EXAMPLES
|
|
54
58
|
$ sf simply data file upload --file-path fileToUpload.txt --target-org myTargetOrg
|
|
55
59
|
|
|
56
60
|
$ sf simply data file upload --file-path fileToUpload.txt --first-publish-location-id 0019000000DmehK --target-org myTargetOrg
|
|
57
61
|
```
|
|
58
62
|
|
|
59
|
-
_See code: [lib/commands/simply/data/file/upload.js](https://github.com/SimplySF/simply-node/blob/@simplysf/simply-data@2.3.
|
|
63
|
+
_See code: [lib/commands/simply/data/file/upload.js](https://github.com/SimplySF/simply-node/blob/@simplysf/simply-data@2.3.27/packages/simply-data/lib/commands/simply/data/file/upload.js)_
|
|
60
64
|
|
|
61
65
|
## `sf simply data files download`
|
|
62
66
|
|
|
@@ -101,7 +105,7 @@ FLAG DESCRIPTIONS
|
|
|
101
105
|
Provide a WHERE clause to allow the plugin to specify which ContentVersion records should be downloaded.
|
|
102
106
|
```
|
|
103
107
|
|
|
104
|
-
_See code: [lib/commands/simply/data/files/download.js](https://github.com/SimplySF/simply-node/blob/@simplysf/simply-data@2.3.
|
|
108
|
+
_See code: [lib/commands/simply/data/files/download.js](https://github.com/SimplySF/simply-node/blob/@simplysf/simply-data@2.3.27/packages/simply-data/lib/commands/simply/data/files/download.js)_
|
|
105
109
|
|
|
106
110
|
## `sf simply data files upload`
|
|
107
111
|
|
|
@@ -140,13 +144,17 @@ FLAG DESCRIPTIONS
|
|
|
140
144
|
The csv file must specify the columns PathOnClient and Title. Optionally, a FirstPublishLocationId can be specified
|
|
141
145
|
to have it linked directly to a Salesforce record after upload.
|
|
142
146
|
|
|
147
|
+
PathOnClient is the local path each file is read from, and may be relative or absolute. Only the file's name is sent
|
|
148
|
+
to the org — Salesforce stores it as the ContentVersion's PathOnClient and derives FileExtension and FileType from
|
|
149
|
+
it — so the local directory the file came from is never uploaded.
|
|
150
|
+
|
|
143
151
|
--max-parallel-jobs=<value> Maximum number of parallel jobs.
|
|
144
152
|
|
|
145
153
|
By default the plugin will only process a single file upload at a time. You can increase this value to allow for
|
|
146
154
|
quasi concurrent uploads. Please note that setting this value too high can cause performance issues.
|
|
147
155
|
```
|
|
148
156
|
|
|
149
|
-
_See code: [lib/commands/simply/data/files/upload.js](https://github.com/SimplySF/simply-node/blob/@simplysf/simply-data@2.3.
|
|
157
|
+
_See code: [lib/commands/simply/data/files/upload.js](https://github.com/SimplySF/simply-node/blob/@simplysf/simply-data@2.3.27/packages/simply-data/lib/commands/simply/data/files/upload.js)_
|
|
150
158
|
<!-- commandsstop -->
|
|
151
159
|
|
|
152
160
|
## License
|
|
@@ -8,16 +8,23 @@ import { ContentVersion, ContentVersionDownload } from './contentVersionTypes.js
|
|
|
8
8
|
* `ContentDocumentId`, and `FileExtension` are used to build the output file path.
|
|
9
9
|
* @param downloadDirectory - The local directory to write the file into.
|
|
10
10
|
* @returns The local path the file was written to.
|
|
11
|
+
* @throws {SfError} If the org returns a non-2xx response, before any file is created.
|
|
11
12
|
*/
|
|
12
13
|
export declare function downloadContentVersion(targetOrgConnection: Connection, contentVersionDownload: ContentVersionDownload, downloadDirectory: string): Promise<string>;
|
|
13
14
|
/**
|
|
14
15
|
* Upload a local file as a new `ContentVersion`.
|
|
15
16
|
*
|
|
17
|
+
* Only the file's name is sent to the org as `PathOnClient`, not the local path it was read from.
|
|
18
|
+
* Salesforce derives `FileExtension` and `FileType` from that value, so the name is all it needs —
|
|
19
|
+
* and sending the rest would publish the uploader's local directory layout into the org, where it
|
|
20
|
+
* is visible to anyone who can query `ContentVersion`.
|
|
21
|
+
*
|
|
16
22
|
* @param targetOrgConnection - The org connection to upload to.
|
|
17
|
-
* @param
|
|
18
|
-
* @param title - The `ContentVersion` title. Defaults to the file's
|
|
23
|
+
* @param filePath - The local filesystem path of the file to upload. Stays local.
|
|
24
|
+
* @param title - The `ContentVersion` title. Defaults to the file's name.
|
|
19
25
|
* @param firstPublishLocationId - The ID of the record/library to attach the resulting
|
|
20
26
|
* `ContentDocument` to, if any.
|
|
21
27
|
* @returns The created `ContentVersion`, re-queried to include `ContentDocumentId`.
|
|
28
|
+
* @throws {SfError} If the org returns a non-2xx response.
|
|
22
29
|
*/
|
|
23
|
-
export declare function uploadContentVersion(targetOrgConnection: Connection,
|
|
30
|
+
export declare function uploadContentVersion(targetOrgConnection: Connection, filePath: string, title?: string, firstPublishLocationId?: string): Promise<ContentVersion>;
|
|
@@ -15,9 +15,32 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import fs from 'node:fs';
|
|
17
17
|
import path from 'node:path';
|
|
18
|
-
import stream from 'node:stream';
|
|
19
|
-
import
|
|
20
|
-
import
|
|
18
|
+
import stream, { Readable } from 'node:stream';
|
|
19
|
+
import { SfError } from '@salesforce/core';
|
|
20
|
+
import { contentVersionMultipart } from './multipart.js';
|
|
21
|
+
/** How much of an error response body to include in a thrown error before truncating. */
|
|
22
|
+
const MAX_ERROR_BODY_LENGTH = 2000;
|
|
23
|
+
/**
|
|
24
|
+
* Turn a non-2xx response into an `SfError`.
|
|
25
|
+
*
|
|
26
|
+
* `fetch` — unlike the `got` client this replaced — resolves successfully for 4xx and 5xx, so every
|
|
27
|
+
* call site has to check `response.ok` itself. Missing one is how an HTML error page ends up
|
|
28
|
+
* written to disk with a `.pdf` extension.
|
|
29
|
+
*
|
|
30
|
+
* @param response - The failed response. Its body is consumed.
|
|
31
|
+
* @param context - What was being attempted, used as the error message prefix.
|
|
32
|
+
* @returns An `SfError` carrying the status and as much of the body as is useful.
|
|
33
|
+
*/
|
|
34
|
+
async function responseError(response, context) {
|
|
35
|
+
let body = '';
|
|
36
|
+
try {
|
|
37
|
+
body = (await response.text()).slice(0, MAX_ERROR_BODY_LENGTH);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// A body we can't read is not worth failing differently over; the status still tells the story.
|
|
41
|
+
}
|
|
42
|
+
return new SfError(`${context} failed with HTTP ${response.status} ${response.statusText}${body ? `: ${body}` : ''}`, 'ContentVersionRequestError');
|
|
43
|
+
}
|
|
21
44
|
/**
|
|
22
45
|
* Download a `ContentVersion`'s file data to a local directory.
|
|
23
46
|
*
|
|
@@ -26,46 +49,69 @@ import got from 'got';
|
|
|
26
49
|
* `ContentDocumentId`, and `FileExtension` are used to build the output file path.
|
|
27
50
|
* @param downloadDirectory - The local directory to write the file into.
|
|
28
51
|
* @returns The local path the file was written to.
|
|
52
|
+
* @throws {SfError} If the org returns a non-2xx response, before any file is created.
|
|
29
53
|
*/
|
|
30
54
|
export async function downloadContentVersion(targetOrgConnection, contentVersionDownload, downloadDirectory) {
|
|
31
55
|
const filePath = `${downloadDirectory}/${contentVersionDownload.ContentDocumentId}_${contentVersionDownload.Title.replaceAll(' ', '_')}.${contentVersionDownload.FileExtension}`;
|
|
32
|
-
await
|
|
56
|
+
const response = await fetch(`${targetOrgConnection.baseUrl()}/sobjects/ContentVersion/${contentVersionDownload.Id}/VersionData`, {
|
|
33
57
|
headers: {
|
|
34
58
|
Authorization: `Bearer ${targetOrgConnection.accessToken}`,
|
|
35
59
|
},
|
|
36
|
-
})
|
|
60
|
+
});
|
|
61
|
+
// Checked before the write stream is opened, so a failed download leaves nothing behind.
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw await responseError(response, `Download of ContentVersion ${contentVersionDownload.Id}`);
|
|
64
|
+
}
|
|
65
|
+
if (!response.body) {
|
|
66
|
+
throw new SfError(`Download of ContentVersion ${contentVersionDownload.Id} returned an empty response body.`, 'ContentVersionRequestError');
|
|
67
|
+
}
|
|
68
|
+
await stream.promises.pipeline(Readable.fromWeb(response.body), fs.createWriteStream(filePath));
|
|
37
69
|
return filePath;
|
|
38
70
|
}
|
|
39
71
|
/**
|
|
40
72
|
* Upload a local file as a new `ContentVersion`.
|
|
41
73
|
*
|
|
74
|
+
* Only the file's name is sent to the org as `PathOnClient`, not the local path it was read from.
|
|
75
|
+
* Salesforce derives `FileExtension` and `FileType` from that value, so the name is all it needs —
|
|
76
|
+
* and sending the rest would publish the uploader's local directory layout into the org, where it
|
|
77
|
+
* is visible to anyone who can query `ContentVersion`.
|
|
78
|
+
*
|
|
42
79
|
* @param targetOrgConnection - The org connection to upload to.
|
|
43
|
-
* @param
|
|
44
|
-
* @param title - The `ContentVersion` title. Defaults to the file's
|
|
80
|
+
* @param filePath - The local filesystem path of the file to upload. Stays local.
|
|
81
|
+
* @param title - The `ContentVersion` title. Defaults to the file's name.
|
|
45
82
|
* @param firstPublishLocationId - The ID of the record/library to attach the resulting
|
|
46
83
|
* `ContentDocument` to, if any.
|
|
47
84
|
* @returns The created `ContentVersion`, re-queried to include `ContentDocumentId`.
|
|
85
|
+
* @throws {SfError} If the org returns a non-2xx response.
|
|
48
86
|
*/
|
|
49
|
-
export async function uploadContentVersion(targetOrgConnection,
|
|
87
|
+
export async function uploadContentVersion(targetOrgConnection, filePath, title, firstPublishLocationId) {
|
|
50
88
|
// Check that we have access to the file
|
|
51
|
-
await fs.promises.access(
|
|
89
|
+
await fs.promises.access(filePath, fs.constants.F_OK);
|
|
90
|
+
const fileName = path.basename(filePath);
|
|
52
91
|
const contentVersionCreateRequest = {
|
|
53
92
|
FirstPublishLocationId: firstPublishLocationId,
|
|
54
|
-
PathOnClient:
|
|
55
|
-
Title: title ??
|
|
93
|
+
PathOnClient: fileName,
|
|
94
|
+
Title: title ?? fileName,
|
|
56
95
|
};
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
96
|
+
const { contentType, createBody } = contentVersionMultipart({
|
|
97
|
+
entity: contentVersionCreateRequest,
|
|
98
|
+
filePath,
|
|
99
|
+
filename: fileName,
|
|
100
|
+
});
|
|
101
|
+
const response = await fetch(`${targetOrgConnection.baseUrl()}/sobjects/ContentVersion`, {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
// `duplex: 'half'` is required by undici whenever the body is a stream.
|
|
104
|
+
body: Readable.toWeb(createBody()),
|
|
105
|
+
duplex: 'half',
|
|
62
106
|
headers: {
|
|
63
107
|
Authorization: `Bearer ${targetOrgConnection.accessToken}`,
|
|
64
|
-
'Content-Type':
|
|
108
|
+
'Content-Type': contentType,
|
|
65
109
|
},
|
|
66
|
-
resolveBodyOnly: true,
|
|
67
|
-
responseType: 'json',
|
|
68
110
|
});
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
throw await responseError(response, `Upload of "${filePath}"`);
|
|
113
|
+
}
|
|
114
|
+
const data = (await response.json());
|
|
69
115
|
const queryResult = await targetOrgConnection.singleRecordQuery(`SELECT ContentDocumentId, FileExtension, Id, Title FROM ContentVersion WHERE Id='${data.id}'`);
|
|
70
116
|
return queryResult;
|
|
71
117
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contentVersionUtils.js","sourceRoot":"","sources":["../../src/common/contentVersionUtils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,MAAM,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"contentVersionUtils.js","sourceRoot":"","sources":["../../src/common/contentVersionUtils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAc,OAAO,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAQzD,yFAAyF;AACzF,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAEnC;;;;;;;;;;GAUG;AACH,KAAK,UAAU,aAAa,CAAC,QAAkB,EAAE,OAAe;IAC9D,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,CAAC;QACH,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,qBAAqB,CAAC,CAAC;IACjE,CAAC;IAAC,MAAM,CAAC;QACP,gGAAgG;IAClG,CAAC;IAED,OAAO,IAAI,OAAO,CAChB,GAAG,OAAO,qBAAqB,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EACjG,4BAA4B,CAC7B,CAAC;AACJ,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IAAI,CAAC,CAAC;IAEtD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAEzC,MAAM,2BAA2B,GAAgC;QAC/D,sBAAsB,EAAE,sBAAsB;QAC9C,YAAY,EAAE,QAAQ;QACtB,KAAK,EAAE,KAAK,IAAI,QAAQ;KACzB,CAAC;IAEF,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,uBAAuB,CAAC;QAC1D,MAAM,EAAE,2BAA2B;QACnC,QAAQ;QACR,QAAQ,EAAE,QAAQ;KACnB,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,mBAAmB,CAAC,OAAO,EAAE,0BAA0B,EAAE;QACvF,MAAM,EAAE,MAAM;QACd,wEAAwE;QACxE,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,UAAU,EAAE,CAA+B;QAChE,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,aAAa,EAAE,UAAU,mBAAmB,CAAC,WAAqB,EAAE;YACpE,cAAc,EAAE,WAAW;SAC5B;KACF,CAAC,CAAC;IAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,MAAM,aAAa,CAAC,QAAQ,EAAE,cAAc,QAAQ,GAAG,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA+B,CAAC;IAEnE,MAAM,WAAW,GAAG,MAAM,mBAAmB,CAAC,iBAAiB,CAC7D,oFAAoF,IAAI,CAAC,EAAE,GAAG,CAC/F,CAAC;IAEF,OAAO,WAA6B,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
/**
|
|
3
|
+
* Generate a multipart boundary.
|
|
4
|
+
*
|
|
5
|
+
* The boundary must not appear anywhere in the encoded body. Rather than scanning the file to
|
|
6
|
+
* prove that, we use 128 bits of randomness, which makes an accidental collision less likely than
|
|
7
|
+
* a disk error corrupting the upload anyway.
|
|
8
|
+
*
|
|
9
|
+
* @returns A boundary string, comfortably under the 70-character limit.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createBoundary(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Escape a filename for inclusion in a `Content-Disposition` header.
|
|
14
|
+
*
|
|
15
|
+
* Applies the same normalization the WHATWG `FormData` serializer does. Without it, a file named
|
|
16
|
+
* `my"file.pdf` would close the quoted string early, and one containing a newline could inject a
|
|
17
|
+
* header. The `form-data` package this replaced did neither.
|
|
18
|
+
*
|
|
19
|
+
* @param filename - The raw filename.
|
|
20
|
+
* @returns The filename with `"`, CR, and LF percent-encoded.
|
|
21
|
+
*/
|
|
22
|
+
export declare function escapeHeaderFilename(filename: string): string;
|
|
23
|
+
/** A multipart request body, plus the `Content-Type` header value that describes it. */
|
|
24
|
+
export type ContentVersionMultipart = {
|
|
25
|
+
/** The generated (or supplied) boundary. */
|
|
26
|
+
boundary: string;
|
|
27
|
+
/** The value for the request's `Content-Type` header, including the boundary. */
|
|
28
|
+
contentType: string;
|
|
29
|
+
/**
|
|
30
|
+
* Build the request body.
|
|
31
|
+
*
|
|
32
|
+
* This is a factory rather than a stream because a stream can only be consumed once, and a
|
|
33
|
+
* retried upload needs a fresh body. Each call re-opens the file.
|
|
34
|
+
*/
|
|
35
|
+
createBody: () => Readable;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Build the `multipart/form-data` body for a `ContentVersion` create request.
|
|
39
|
+
*
|
|
40
|
+
* The shape here matches Salesforce's documented example exactly, including the trailing semicolon
|
|
41
|
+
* after `name="entity_content"` and the absence of a `filename` on that part. That absence is the
|
|
42
|
+
* reason this is hand-built rather than delegated to the platform's `FormData`: a `FormData` part
|
|
43
|
+
* carrying `Content-Type: application/json` is necessarily a `Blob`, and a `Blob` part always
|
|
44
|
+
* serializes with a filename.
|
|
45
|
+
*
|
|
46
|
+
* @param options - The entity metadata, the file to attach, and an optional fixed boundary.
|
|
47
|
+
* @param options.entity - Serialized as the JSON `entity_content` part.
|
|
48
|
+
* @param options.filePath - Path of the file streamed as the `VersionData` part.
|
|
49
|
+
* @param options.filename - Filename advertised for the `VersionData` part.
|
|
50
|
+
* @param options.boundary - Boundary to use. Defaults to a random one; pass a fixed value in tests.
|
|
51
|
+
* @returns The boundary, the `Content-Type` header value, and a body factory.
|
|
52
|
+
*/
|
|
53
|
+
export declare function contentVersionMultipart(options: {
|
|
54
|
+
entity: unknown;
|
|
55
|
+
filePath: string;
|
|
56
|
+
filename: string;
|
|
57
|
+
boundary?: string;
|
|
58
|
+
}): ContentVersionMultipart;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026, Clay Chipps.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import crypto from 'node:crypto';
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import { Readable } from 'node:stream';
|
|
19
|
+
/**
|
|
20
|
+
* Prefix for generated boundaries. Salesforce caps a boundary at 70 characters, so this plus the
|
|
21
|
+
* 32 hex characters appended to it has to stay under that.
|
|
22
|
+
*/
|
|
23
|
+
const BOUNDARY_PREFIX = 'simplyBoundary';
|
|
24
|
+
/** CRLF, which is what RFC 2046 requires between multipart headers and parts — not a bare `\n`. */
|
|
25
|
+
const CRLF = '\r\n';
|
|
26
|
+
/**
|
|
27
|
+
* Generate a multipart boundary.
|
|
28
|
+
*
|
|
29
|
+
* The boundary must not appear anywhere in the encoded body. Rather than scanning the file to
|
|
30
|
+
* prove that, we use 128 bits of randomness, which makes an accidental collision less likely than
|
|
31
|
+
* a disk error corrupting the upload anyway.
|
|
32
|
+
*
|
|
33
|
+
* @returns A boundary string, comfortably under the 70-character limit.
|
|
34
|
+
*/
|
|
35
|
+
export function createBoundary() {
|
|
36
|
+
return `${BOUNDARY_PREFIX}${crypto.randomBytes(16).toString('hex')}`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Escape a filename for inclusion in a `Content-Disposition` header.
|
|
40
|
+
*
|
|
41
|
+
* Applies the same normalization the WHATWG `FormData` serializer does. Without it, a file named
|
|
42
|
+
* `my"file.pdf` would close the quoted string early, and one containing a newline could inject a
|
|
43
|
+
* header. The `form-data` package this replaced did neither.
|
|
44
|
+
*
|
|
45
|
+
* @param filename - The raw filename.
|
|
46
|
+
* @returns The filename with `"`, CR, and LF percent-encoded.
|
|
47
|
+
*/
|
|
48
|
+
export function escapeHeaderFilename(filename) {
|
|
49
|
+
return filename.replaceAll('\r', '%0D').replaceAll('\n', '%0A').replaceAll('"', '%22');
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Build the `multipart/form-data` body for a `ContentVersion` create request.
|
|
53
|
+
*
|
|
54
|
+
* The shape here matches Salesforce's documented example exactly, including the trailing semicolon
|
|
55
|
+
* after `name="entity_content"` and the absence of a `filename` on that part. That absence is the
|
|
56
|
+
* reason this is hand-built rather than delegated to the platform's `FormData`: a `FormData` part
|
|
57
|
+
* carrying `Content-Type: application/json` is necessarily a `Blob`, and a `Blob` part always
|
|
58
|
+
* serializes with a filename.
|
|
59
|
+
*
|
|
60
|
+
* @param options - The entity metadata, the file to attach, and an optional fixed boundary.
|
|
61
|
+
* @param options.entity - Serialized as the JSON `entity_content` part.
|
|
62
|
+
* @param options.filePath - Path of the file streamed as the `VersionData` part.
|
|
63
|
+
* @param options.filename - Filename advertised for the `VersionData` part.
|
|
64
|
+
* @param options.boundary - Boundary to use. Defaults to a random one; pass a fixed value in tests.
|
|
65
|
+
* @returns The boundary, the `Content-Type` header value, and a body factory.
|
|
66
|
+
*/
|
|
67
|
+
export function contentVersionMultipart(options) {
|
|
68
|
+
const boundary = options.boundary ?? createBoundary();
|
|
69
|
+
const entityJson = JSON.stringify(options.entity);
|
|
70
|
+
const filename = escapeHeaderFilename(options.filename);
|
|
71
|
+
const header = Buffer.from(`--${boundary}${CRLF}` +
|
|
72
|
+
`Content-Disposition: form-data; name="entity_content";${CRLF}` +
|
|
73
|
+
`Content-Type: application/json${CRLF}${CRLF}` +
|
|
74
|
+
`${entityJson}${CRLF}` +
|
|
75
|
+
`--${boundary}${CRLF}` +
|
|
76
|
+
`Content-Type: application/octet-stream${CRLF}` +
|
|
77
|
+
`Content-Disposition: form-data; name="VersionData"; filename="${filename}"${CRLF}${CRLF}`, 'utf8');
|
|
78
|
+
const footer = Buffer.from(`${CRLF}--${boundary}--${CRLF}`, 'utf8');
|
|
79
|
+
const createBody = () => Readable.from((async function* body() {
|
|
80
|
+
yield header;
|
|
81
|
+
yield* fs.createReadStream(options.filePath);
|
|
82
|
+
yield footer;
|
|
83
|
+
})());
|
|
84
|
+
return {
|
|
85
|
+
boundary,
|
|
86
|
+
// Unquoted, matching what `form-data` and jsforce emit. RFC 2046 permits quoting, but the
|
|
87
|
+
// unquoted form is the one known to work against this endpoint.
|
|
88
|
+
contentType: `multipart/form-data; boundary=${boundary}`,
|
|
89
|
+
createBody,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=multipart.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.js","sourceRoot":"","sources":["../../src/common/multipart.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,MAAM,MAAM,aAAa,CA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CAAC,GAAG,IAAI,KAAK,QAAQ,KAAK,IAAI,EAAE,EAAE,MAAM,CAAC,CAAC;IAEpE,MAAM,UAAU,GAAG,GAAa,EAAE,CAChC,QAAQ,CAAC,IAAI,CACX,CAAC,KAAK,SAAS,CAAC,CAAC,IAAI;QACnB,MAAM,MAAM,CAAC;QACb,KAAK,CAAC,CAAC,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC7C,MAAM,MAAM,CAAC;IACf,CAAC,CAAC,EAAE,CACL,CAAC;IAEJ,OAAO;QACL,QAAQ;QACR,0FAA0F;QAC1F,gEAAgE;QAChE,WAAW,EAAE,iCAAiC,QAAQ,EAAE;QACxD,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -6,9 +6,11 @@ Upload a file to a Salesforce org.
|
|
|
6
6
|
|
|
7
7
|
Uploads a file to a Salesforce org.
|
|
8
8
|
|
|
9
|
+
Only the file's name is sent to the org — Salesforce stores it as the ContentVersion's PathOnClient and derives FileExtension and FileType from it — so the local directory the file came from is never uploaded.
|
|
10
|
+
|
|
9
11
|
# flags.file-path.summary
|
|
10
12
|
|
|
11
|
-
Path to the file to upload.
|
|
13
|
+
Path to the file to upload. May be relative or absolute; only the file's name is sent to the org.
|
|
12
14
|
|
|
13
15
|
# flags.first-publish-location-id.summary
|
|
14
16
|
|
|
@@ -14,6 +14,8 @@ Path to the csv file that specifies the upload.
|
|
|
14
14
|
|
|
15
15
|
The csv file must specify the columns PathOnClient and Title. Optionally, a FirstPublishLocationId can be specified to have it linked directly to a Salesforce record after upload.
|
|
16
16
|
|
|
17
|
+
PathOnClient is the local path each file is read from, and may be relative or absolute. Only the file's name is sent to the org — Salesforce stores it as the ContentVersion's PathOnClient and derives FileExtension and FileType from it — so the local directory the file came from is never uploaded.
|
|
18
|
+
|
|
17
19
|
# flags.max-parallel-jobs.summary
|
|
18
20
|
|
|
19
21
|
Maximum number of parallel jobs.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysf/simply-data",
|
|
3
3
|
"description": "Utilities for working with data",
|
|
4
|
-
"version": "2.3.
|
|
4
|
+
"version": "2.3.27",
|
|
5
5
|
"author": "@ClayChipps",
|
|
6
6
|
"homepage": "https://github.com/SimplySF/simply-node",
|
|
7
7
|
"bugs": "https://github.com/SimplySF/simply-node/issues",
|
|
@@ -68,8 +68,6 @@
|
|
|
68
68
|
"@simplysf/simply-core": "^1.3.0",
|
|
69
69
|
"@simplysf/simply-plugin-kit": "^1.0.4",
|
|
70
70
|
"csv-parse": "^7.0.2",
|
|
71
|
-
"form-data": "^4.0.5",
|
|
72
|
-
"got": "14.6.6",
|
|
73
71
|
"p-queue": "^9.2.0"
|
|
74
72
|
},
|
|
75
73
|
"devDependencies": {
|
|
@@ -216,5 +214,5 @@
|
|
|
216
214
|
"output": []
|
|
217
215
|
}
|
|
218
216
|
},
|
|
219
|
-
"gitHead": "
|
|
217
|
+
"gitHead": "5227ae680ff4f42e4842b090e50826930dcc67b1"
|
|
220
218
|
}
|