Skip to content

Commit

Permalink
Add option to route emoji kitchen through a proxy (#6)
Browse files Browse the repository at this point in the history
The emoji kitchen uses Tenor, owned by Google.
In China, Google's services are blocked, and tenor can be
accessed only through a proxy.

With this change, we use the newly added env var
`FAMOS_PROXY` to configure this proxy.
  • Loading branch information
someok authored Jan 6, 2025
1 parent d7ac5e4 commit 62ef31c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ apple_icons
emoji-to-icon-filename.json
node_modules
package-lock.json
pnpm-lock.yaml
build-scripts/package.json
icons
wfbuild
emoji-kitchen.bin
Expand Down
2 changes: 1 addition & 1 deletion build-scripts/mkkitchen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cd emoji-kitchen
npm install
cd ..

pkg --targets node12-macos-x64 ./emoji-kitchen --output emoji-kitchen.bin
pkg --targets node14-macos-x64 ./emoji-kitchen --output emoji-kitchen.bin
27 changes: 18 additions & 9 deletions emoji-kitchen/emoji-kitchen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const { HttpsProxyAgent } = require('https-proxy-agent');

const { spawn, spawnSync } = require("child_process");
const {lookup} = require('lookup-dns-cache');
const { lookup } = require('lookup-dns-cache');
const { log } = require('console');

const PORT = 36363;

let agent = undefined;
if (process.env.FAMOS_PROXY && process.env.FAMOS_PROXY.startsWith('http')) {
agent = new HttpsProxyAgent(process.env.FAMOS_PROXY);
console.log('Using proxy agent: ', process.env.FAMOS_PROXY);
}

// https://nodejs.org/api/http.html#http_http_get_options_callback
const REQUEST_OPTS = {
family: 4,
lookup: lookup,
agent: agent,
};

String.prototype.title = function() {
String.prototype.title = function () {
return this.replace(/(^|\s)\S/g, (t) => t.toUpperCase());
};

// Negative indices for arrays
Array.prototype.get = function(i) {
Array.prototype.get = function (i) {
return this[(i + this.length) % this.length];
};

Array.prototype.makeMap = function(keyfunc) {
Array.prototype.makeMap = function (keyfunc) {
return this.reduce(
(dict, item) => {
dict[keyfunc(item)] = item;
Expand Down Expand Up @@ -62,7 +71,7 @@ const emojiToInfoMap =
// the emoji fridge for easy later access.
function updateFridge(alfredItems) {
let fridgePath = `${KITCHEN_DATA_DIR}/fridge.json`;
var fridge = {'items': []};
var fridge = { 'items': [] };
if (fs.existsSync(fridgePath)) {
fridge = JSON.parse(fs.readFileSync(fridgePath));
}
Expand Down Expand Up @@ -130,13 +139,13 @@ function parseTenorDataAndRespondToAlfred(tenorData, res, emoji, emoji2) {
}

// Busy-wait for downloads to finish
var timeout = setInterval(function() {
var timeout = setInterval(function () {
if (downloadMonitor.size == tenorData.results.length) {
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify({'items': responseItems}));
res.write(JSON.stringify({ 'items': responseItems }));
res.end();
console.log('Responded to alfred');
clearInterval(timeout);
clearInterval(timeout);
}
}, 50);

Expand Down Expand Up @@ -229,7 +238,7 @@ http.createServer(function (req, res) {
} else if (!/^application\/json/.test(contentType)) {
error = new Error(
'Invalid content-type.\n' +
`Expected application/json but received ${contentType}`
`Expected application/json but received ${contentType}`
);
}

Expand Down
1 change: 1 addition & 0 deletions emoji-kitchen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "MIT",
"dependencies": {
"https-proxy-agent": "^7.0.6",
"lookup-dns-cache": "^2.1.0"
}
}

0 comments on commit 62ef31c

Please sign in to comment.