-
Notifications
You must be signed in to change notification settings - Fork 20
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 #55 from msywulak/header-forward
Handle custom header additions without requiring "Upstash-Forward-*" as a prefix.
- Loading branch information
Showing
4 changed files
with
81 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { Client, Receiver } = require("@upstash/qstash"); | ||
require("isomorphic-fetch"); | ||
|
||
async function main() { | ||
const q = new Client({ | ||
token: "", | ||
}); | ||
|
||
const res = await q.publish({ | ||
url: "https://qstash-prod-andreas.requestcatcher.com/test", | ||
body: "Hello World", | ||
headers: { | ||
"Upstash-Forward-Custom-Header-1": "Value 1", // Handle already prefixed headers | ||
"Custom-Header-2": "Value 2", // Handle non-prefixed headers | ||
}, | ||
}); | ||
console.log(res); | ||
|
||
// Validating a signature | ||
const receiver = new Receiver({ | ||
currentSigningKey: "sig_3nj4aiyJ2JojDnQ1RRodpYubZAZxAJxNfQcRSKPwVUNbueYk2o", | ||
nextSigningKey: "sig_31zVqmL3s7Eo1vpu1jRSMpaetJXvAT3RvNcfoGUp1Toii8fsQEE", | ||
}); | ||
|
||
const isValid = await receiver | ||
.verify({ | ||
signature: | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIiLCJib2R5IjoicFpHbTFBdjBJRUJLQVJjeno3ZXhrTllzWmI4THphTXJWN0ozMmEyZkZHND0iLCJleHAiOjE2NTc1MzA3NTYsImlhdCI6MTY1NzUzMDQ1NiwiaXNzIjoiVXBzdGFzaCIsImp0aSI6Imp3dF83QXFHbkRLV3dLTmY2dEdUNExnRjhqdENEQjhqIiwibmJmIjoxNjU3NTMwNDU2LCJzdWIiOiJodHRwczovL3FzdGFzaC1wcm9kLWFuZHJlYXMucmVxdWVzdGNhdGNoZXIuY29tL3Rlc3QifQ.GzDXaBRUAqx0KPE-WxfZVVceJll3T1RgxdTRbWPZw8s", | ||
body: "Hello World", | ||
url: "https://qstash-prod-andreas.requestcatcher.com/test", | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
return false; | ||
}); | ||
|
||
console.log({ isValid }); | ||
} | ||
|
||
main(); |
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,25 @@ | ||
export function prefixHeaders(headers: Headers) { | ||
const isIgnoredHeader = (header: string) => { | ||
const lowerCaseHeader = header.toLowerCase(); | ||
return ( | ||
lowerCaseHeader.startsWith("content-type") || | ||
lowerCaseHeader.startsWith("upstash-") | ||
); | ||
}; | ||
|
||
// Get keys of headers that need to be prefixed | ||
const keysToBePrefixed = Array.from(headers.keys()).filter( | ||
(key) => !isIgnoredHeader(key) | ||
); | ||
|
||
// Add the prefixed headers | ||
for (const key of keysToBePrefixed) { | ||
const value = headers.get(key); | ||
if (value !== null) { | ||
headers.set(`Upstash-Forward-${key}`, value); | ||
} | ||
headers.delete(key); // clean up non-prefixed headers | ||
} | ||
|
||
return headers; | ||
} |