-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #808 from zapier/pde-5125/handle-lambda-payloads
PDE-5125 - feat(core) Handle large response payloads
- Loading branch information
Showing
8 changed files
with
351 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const uploader = require('./uploader'); | ||
const crypto = require('crypto'); | ||
|
||
const withRetry = async (fn, retries = 3, delay = 100, attempt = 0) => { | ||
try { | ||
return await fn(); | ||
} catch (error) { | ||
if (attempt >= retries) { | ||
throw error; | ||
} | ||
|
||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
return withRetry(fn, retries, delay, attempt + 1); | ||
} | ||
}; | ||
|
||
// responseStasher uploads the data and returns the URL that points to that data. | ||
const stashResponse = async (input, response, size) => { | ||
const rpc = _.get(input, '_zapier.rpc'); | ||
|
||
if (!rpc) { | ||
throw new Error('rpc is not available'); | ||
} | ||
const signedPostData = await rpc('get_presigned_upload_post_data'); | ||
return withRetry( | ||
_.partial( | ||
uploader, | ||
signedPostData, | ||
response.toString(), // accept JSON string to send to uploader. | ||
size, | ||
crypto.randomUUID() + '.txt', | ||
'text/plain' | ||
) | ||
); | ||
}; | ||
|
||
module.exports = stashResponse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const path = require('path'); | ||
|
||
const FormData = require('form-data'); | ||
const contentDisposition = require('content-disposition'); | ||
|
||
const request = require('./request-client-internal'); | ||
const LENGTH_ERR_MESSAGE = | ||
'We could not calculate the length of your file - please ' + | ||
'pass a knownLength like z.stashFile(f, knownLength)'; | ||
|
||
const uploader = async ( | ||
signedPostData, | ||
bufferStringStream, | ||
knownLength, | ||
filename, | ||
contentType | ||
) => { | ||
filename = path.basename(filename).replace('"', ''); | ||
|
||
const fields = { | ||
...signedPostData.fields, | ||
'Content-Disposition': contentDisposition(filename), | ||
'Content-Type': contentType, | ||
}; | ||
|
||
const form = new FormData(); | ||
|
||
Object.entries(fields).forEach(([key, value]) => { | ||
form.append(key, value); | ||
}); | ||
|
||
form.append('file', bufferStringStream, { | ||
knownLength, | ||
contentType, | ||
filename, | ||
}); | ||
|
||
// Try to catch the missing length early, before upload to S3 fails. | ||
try { | ||
form.getLengthSync(); | ||
} catch (err) { | ||
throw new Error(LENGTH_ERR_MESSAGE); | ||
} | ||
|
||
// Send to S3 with presigned request. | ||
const response = await request({ | ||
url: signedPostData.url, | ||
method: 'POST', | ||
body: form, | ||
}); | ||
|
||
if (response.status === 204) { | ||
return new URL(signedPostData.fields.key, signedPostData.url).href; | ||
} | ||
|
||
if ( | ||
response.content && | ||
response.content.includes && | ||
response.content.includes( | ||
'You must provide the Content-Length HTTP header.' | ||
) | ||
) { | ||
throw new Error(LENGTH_ERR_MESSAGE); | ||
} | ||
|
||
throw new Error(`Got ${response.status} - ${response.content}`); | ||
}; | ||
|
||
module.exports = uploader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.