This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #112 from zapier/z.dehydrateFile
Introduce z.dehydrateFile
- Loading branch information
Showing
10 changed files
with
256 additions
and
56 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
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,58 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
const { DEFAULT_FILE_HYDRATOR_NAME } = require('../constants'); | ||
const { DehydrateError } = require('../errors'); | ||
const resolveMethodPath = require('./resolve-method-path'); | ||
const wrapHydrate = require('./wrap-hydrate'); | ||
|
||
const createFileDehydrator = input => { | ||
const app = _.get(input, '_zapier.app'); | ||
|
||
const dehydrateFileFromRequest = (url, request, meta) => { | ||
url = url || undefined; | ||
request = request || undefined; | ||
meta = meta || undefined; | ||
if (!url && !request) { | ||
throw new DehydrateError( | ||
'You must provide either url or request arguments!' | ||
); | ||
} | ||
return wrapHydrate({ | ||
type: 'file', | ||
method: `hydrators.${DEFAULT_FILE_HYDRATOR_NAME}`, | ||
bundle: { url, request, meta } | ||
}); | ||
}; | ||
|
||
const dehydrateFileFromFunc = (func, inputData) => { | ||
inputData = inputData || {}; | ||
if (inputData.inputData) { | ||
throw new DehydrateError( | ||
'Oops! You passed a full `bundle` - really you should pass what you want under `inputData`!' | ||
); | ||
} | ||
return wrapHydrate({ | ||
type: 'file', | ||
method: resolveMethodPath(app, func), | ||
// inputData vs. bundle is a legacy oddity | ||
bundle: _.omit(inputData, 'environment') // don't leak the environment | ||
}); | ||
}; | ||
|
||
return (...args) => { | ||
const arg0 = args[0]; | ||
if (_.isFunction(arg0)) { | ||
return dehydrateFileFromFunc.apply(this, args); | ||
} | ||
if (arg0 && typeof arg0 !== 'string') { | ||
throw new DehydrateError( | ||
`First argument must be either null, a URL (string), or a hydrator function! We got ${typeof arg0}.` | ||
); | ||
} | ||
return dehydrateFileFromRequest.apply(this, args); | ||
}; | ||
}; | ||
|
||
module.exports = createFileDehydrator; |
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,18 @@ | ||
'use strict'; | ||
|
||
const defaultFileHydrator = (z, bundle) => { | ||
const request = bundle.inputData.request || {}; | ||
request.url = bundle.inputData.url || request.url; | ||
request.raw = true; | ||
|
||
const filePromise = z.request(request); | ||
const meta = bundle.inputData.meta || {}; | ||
return z.stashFile( | ||
filePromise, | ||
meta.knownLength, | ||
meta.filename, | ||
meta.contentType | ||
); | ||
}; | ||
|
||
module.exports = defaultFileHydrator; |
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,36 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
|
||
const { DehydrateError } = require('../errors'); | ||
|
||
const MAX_PAYLOAD_SIZE = 2048; | ||
|
||
const wrapHydrate = payload => { | ||
payload = JSON.stringify(payload); | ||
|
||
if (payload.length > MAX_PAYLOAD_SIZE) { | ||
throw new DehydrateError( | ||
`Oops! You passed too much data (${ | ||
payload.length | ||
} bytes) to your dehydration function - try slimming it down under ${MAX_PAYLOAD_SIZE} bytes (usually by just passing the needed IDs).` | ||
); | ||
} | ||
|
||
if (process.env._ZAPIER_ONE_TIME_SECRET) { | ||
payload = new Buffer(payload).toString('base64'); | ||
|
||
const signature = Buffer.from( | ||
crypto | ||
.createHmac('sha1', process.env._ZAPIER_ONE_TIME_SECRET) | ||
.update(payload) | ||
.digest() | ||
).toString('base64'); | ||
|
||
payload += ':' + signature; | ||
} | ||
|
||
return 'hydrate|||' + payload + '|||hydrate'; | ||
}; | ||
|
||
module.exports = wrapHydrate; |
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.