-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of middleware for javascript
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 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,57 @@ | ||
#!/usr/bin/env node | ||
const readline = require("readline"); | ||
const StringDecoder = require("string_decoder").StringDecoder | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin | ||
}); | ||
|
||
var ignoreIds = new Set(); | ||
var ignoreAddresses = "/api"; | ||
const decoder = new StringDecoder("utf8"); | ||
|
||
function convertHexString(hex) { | ||
var bytes = []; | ||
for (var i = 0; i < hex.length - 1; i += 2) { | ||
bytes.push(parseInt(hex.substr(i, 2), 16)); | ||
} | ||
return decoder.write(Buffer.from(bytes)); | ||
} | ||
|
||
function log(output) { | ||
console.error("==================="); | ||
console.error(output); | ||
} | ||
|
||
function shouldOutputLine(request) { | ||
const components = request.split("\n"); | ||
const header = components[0].split(" "); | ||
const type = parseInt(header[0]); | ||
const tag = header[1]; | ||
|
||
if (type === 3) { | ||
return true; | ||
} | ||
if (type === 1) { | ||
// Check if it's oauth | ||
const endpoint = components[1].split(" ")[1]; | ||
if (!endpoint.startsWith(ignoreAddresses)) { | ||
ignoreIds.add(tag); | ||
return false; | ||
} | ||
} else if (type === 2) { | ||
if (ignoreIds.has(tag)) { | ||
ignoreIds.delete(tag); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
rl.on("line", (input) => { | ||
const str = convertHexString(input); | ||
console.log(input); | ||
if (shouldOutputLine(str)) { | ||
log(str); | ||
} | ||
}); |