From 6aea983855c8fe0d808670b6a812163454112cf6 Mon Sep 17 00:00:00 2001 From: shahramdj Date: Fri, 7 Oct 2022 10:29:26 -0400 Subject: [PATCH 01/13] Update 27-twitter.js Update the public search and user look up parts to work with twitter API V2.0 --- social/twitter/27-twitter.js | 637 +++++++++++++++++++++++------------ 1 file changed, 427 insertions(+), 210 deletions(-) diff --git a/social/twitter/27-twitter.js b/social/twitter/27-twitter.js index ac037bee..ffe99c20 100644 --- a/social/twitter/27-twitter.js +++ b/social/twitter/27-twitter.js @@ -1,5 +1,5 @@ - -module.exports = function(RED) { +//new updated code on Oct 3rd +module.exports = function (RED) { "use strict"; var Ntwitter = require('twitter-ng'); var request = require('request'); @@ -11,9 +11,19 @@ module.exports = function(RED) { var localUserCache = {}; var userObjectCache = {}; var userSreenNameToIdCache = {}; + + RED.nodes.registerType("twitter-credentials", TwitterCredentialsNode, { + credentials: { + consumer_key: { type: "password" }, + consumer_secret: { type: "password" }, + access_token: { type: "password" }, + access_token_secret: { type: "password" }, + access_token_bearer: { type: "password" } + } + }); function TwitterCredentialsNode(n) { - RED.nodes.createNode(this,n); + RED.nodes.createNode(this, n); this.screen_name = n.screen_name; if (this.screen_name && this.screen_name[0] === "@") { this.screen_name = this.screen_name.substring(1); @@ -21,67 +31,106 @@ module.exports = function(RED) { if (this.credentials.consumer_key && this.credentials.consumer_secret && this.credentials.access_token && - this.credentials.access_token_secret) { + this.credentials.access_token_secret && + this.credentials.access_token_bearer) { this.oauth = { consumer_key: this.credentials.consumer_key, consumer_secret: this.credentials.consumer_secret, token: this.credentials.access_token, - token_secret: this.credentials.access_token_secret + token_secret: this.credentials.access_token_secret, + token_bearer: this.credentials.access_token_bearer } this.credHash = crypto.createHash('sha1').update( - this.credentials.consumer_key+this.credentials.consumer_secret+ - this.credentials.access_token+this.credentials.access_token_secret + this.credentials.consumer_key + this.credentials.consumer_secret + + this.credentials.access_token + this.credentials.access_token_secret + + this.credentials.access_token_bearer ).digest('base64'); var self = this; - if (localUserCache.hasOwnProperty(self.credHash)) { - this.localIdentityPromise = Promise.resolve(localUserCache[self.credHash]); - } else { - this.localIdentityPromise = this.get("https://api.twitter.com/1.1/account/settings.json").then(function(body) { - if (body.status === 200) { - localUserCache[self.credHash] = body.body.screen_name; - self.screen_name = body.body.screen_name; - } else { - self.warn("Failed to get user profile"); + + const needle = require('needle'); + var credentials = RED.nodes.getCredentials(self); + + // The code below sets the bearer token from your environment variables + // To set environment variables on macOS or Linux, run the export command below from the terminal: + // export BEARER_TOKEN='YOUR-TOKEN' + + const token = this.credentials.access_token_bearer; + + const endpointUserURL = "https://api.twitter.com/2/users/by?usernames=" + + async function getRequest() { + + // These are the parameters for the API request + // specify User names to fetch, and any additional fields that are required + // by default, only the User ID, name and user name are returned + + + const params = { + usernames: `${self.screen_name}`, // Edit usernames to look up + "user.fields": "created_at,description", // Edit optional query parameters here + "expansions": "pinned_tweet_id" + } + + console.log(self.screen_name); + + // this is the HTTP header that adds bearer token authentication + const res = await needle('get', endpointUserURL, params, { + headers: { + "authorization": `Bearer ${token}` } - }); + }) + + if (res.statusCode === 200) { + return res.body; + } else { + node.send("Failed to get user profile"); + } } + + (async () => { + + try { + // Make request + const response = await getRequest(); + // console.dir(response, { + // depth: null + // }); + + } catch (e) { + console.log(e); + } + })(); + } } - RED.nodes.registerType("twitter-credentials",TwitterCredentialsNode,{ - credentials: { - consumer_key: { type: "password"}, - consumer_secret: { type: "password" }, - access_token: {type: "password"}, - access_token_secret: {type:"password"} - } - }); - TwitterCredentialsNode.prototype.get = function(url,opts) { + + TwitterCredentialsNode.prototype.get = function (url, opts) { var node = this; opts = opts || {}; opts.tweet_mode = 'extended'; - return new Promise(function(resolve,reject) { + return new Promise(function (resolve, reject) { request.get({ url: url, oauth: node.oauth, json: true, qs: opts - }, function(err, response,body) { + }, function (err, response, body) { if (err) { reject(err); } else { resolve({ status: response.statusCode, rateLimitRemaining: response.headers['x-rate-limit-remaining'], - rateLimitTimeout: 5000+parseInt(response.headers['x-rate-limit-reset'])*1000 - Date.now(), + rateLimitTimeout: 5000 + parseInt(response.headers['x-rate-limit-reset']) * 1000 - Date.now(), body: body }); } }); }) } - TwitterCredentialsNode.prototype.post = function(url,data,opts,formData) { + TwitterCredentialsNode.prototype.post = function (url, data, opts, formData) { var node = this; opts = opts || {}; var options = { @@ -96,15 +145,15 @@ module.exports = function(RED) { if (formData) { options.formData = formData; } - return new Promise(function(resolve,reject) { - request.post(options, function(err, response,body) { + return new Promise(function (resolve, reject) { + request.post(options, function (err, response, body) { if (err) { reject(err); } else { resolve({ status: response.statusCode, rateLimitRemaining: response.headers['x-rate-limit-remaining'], - rateLimitTimeout: 5000+parseInt(response.headers['x-rate-limit-reset'])*1000 - Date.now(), + rateLimitTimeout: 5000 + parseInt(response.headers['x-rate-limit-reset']) * 1000 - Date.now(), body: body }); } @@ -113,14 +162,14 @@ module.exports = function(RED) { } - TwitterCredentialsNode.prototype.getUsers = function(users,getBy) { + TwitterCredentialsNode.prototype.getUsers = function (users, getBy) { if (users.length === 0) { return Promise.resolve(); } var params = {}; - params[getBy||"user_id"] = users; + params[getBy || "user_id"] = users; - return this.get("https://api.twitter.com/1.1/users/lookup.json",params).then(function(result) { + return this.get("https://api.twitter.com/1.1/users/lookup.json", params).then(function (result) { var res = result.body; if (res.errors) { throw new Error(res.errors[0].message); @@ -159,17 +208,16 @@ module.exports = function(RED) { } function TwitterInNode(n) { - RED.nodes.createNode(this,n); + RED.nodes.createNode(this, n); this.active = true; this.user = n.user; //this.tags = n.tags.replace(/ /g,''); - this.tags = n.tags||""; + this.tags = n.tags || ""; this.twitter = n.twitter; this.topic = "tweets"; this.twitterConfig = RED.nodes.getNode(this.twitter); this.poll_ids = []; this.timeout_ids = []; - var credentials = RED.nodes.getCredentials(this.twitter); this.status({}); @@ -177,16 +225,16 @@ module.exports = function(RED) { var node = this; if (this.user === "true") { // Poll User Home Timeline 1/min - this.poll(60000,"https://api.twitter.com/1.1/statuses/home_timeline.json"); + this.poll(60000, "https://api.twitter.com/1.1/statuses/home_timeline.json"); } else if (this.user === "user") { - var users = node.tags.split(/\s*,\s*/).filter(v=>!!v); + var users = node.tags.split(/\s*,\s*/).filter(v => !!v); if (users.length === 0) { node.error(RED._("twitter.warn.nousers")); return; } // Poll User timeline - users.forEach(function(user) { - node.poll(60000,"https://api.twitter.com/1.1/statuses/user_timeline.json",{screen_name: user}); + users.forEach(function (user) { + node.poll(60000, "https://api.twitter.com/1.1/statuses/user_timeline.json", { screen_name: user }); }) } else if (this.user === "dm") { node.pollDirectMessages(); @@ -198,103 +246,272 @@ module.exports = function(RED) { consumer_key: credentials.consumer_key, consumer_secret: credentials.consumer_secret, access_token_key: credentials.access_token, - access_token_secret: credentials.access_token_secret + access_token_secret: credentials.access_token_secret, + access_token_bearer: credentials.access_token_bearer }); // Stream public tweets - try { - var thing = 'statuses/filter'; - var tags = node.tags; - var st = { track: [tags] }; - var setupStream = function() { - if (node.restart) { - node.status({fill:"green", shape:"dot", text:(tags||" ")}); - twit.stream(thing, st, function(stream) { - //console.log("ST",st); - node.stream = stream; - stream.on('data', function(tweet) { - if (tweet.user !== undefined) { - var where = tweet.user.location; - var la = tweet.lang || tweet.user.lang; - var msg = { topic:node.topic+"/"+tweet.user.screen_name, payload:tweet.text, lang:la, tweet:tweet }; - if (where) { - msg.location = {place:where}; - addLocationToTweet(msg); - } - node.send(msg); - //node.status({fill:"green", shape:"dot", text:(tags||" ")}); - } - }); - stream.on('limit', function(tweet) { - //node.status({fill:"grey", shape:"dot", text:RED._("twitter.errors.limitrate")}); - node.status({fill:"grey", shape:"dot", text:(tags||" ")}); - node.tout2 = setTimeout(function() { node.status({fill:"green", shape:"dot", text:(tags||" ")}); },10000); - }); - stream.on('error', function(tweet,rc) { - //console.log("ERRO",rc,tweet); - if (rc == 420) { - node.status({fill:"red", shape:"ring", text:RED._("twitter.errors.ratelimit")}); - } - else { - node.status({fill:"red", shape:"ring", text:tweet.toString()}); - node.warn(RED._("twitter.errors.streamerror",{error:tweet.toString(),rc:rc})); - } - twitterRateTimeout = Date.now() + retry; - if (node.restart) { - node.tout = setTimeout(function() { setupStream() },retry); - } - }); - stream.on('destroy', function (response) { - //console.log("DEST",response) - twitterRateTimeout = Date.now() + 15000; - if (node.restart) { - node.status({fill:"red", shape:"dot", text:" "}); - node.warn(RED._("twitter.errors.unexpectedend")); - node.tout = setTimeout(function() { setupStream() },15000); - } - }); - }); + const needle = require('needle'); + + // The code below sets the bearer token from your environment variables + // To set environment variables on macOS or Linux, run the export command below from the terminal: + // export BEARER_TOKEN='YOUR-TOKEN' + const token = credentials.access_token_bearer; + + const rulesURL = 'https://api.twitter.com/2/tweets/search/stream/rules'; + const streamURL = 'https://api.twitter.com/2/tweets/search/stream'; + + // this sets up two rules - the value is the search terms to match on, and the tag is an identifier that + // will be applied to the Tweets return to show which rule they matched + // with a standard project with Basic Access, you can add up to 25 concurrent rules to your stream, and + // each rule can be up to 512 characters long + + // Edit rules as desired below + const rules = [{ + 'value': node.tags, + 'tag': node.tags + }]; + + async function getAllRules() { + + const response = await needle('get', rulesURL, { + headers: { + "authorization": `Bearer ${token}` + } + }) + + if (response.statusCode !== 200) { + node.send("Error:", response.statusMessage, response.statusCode) + throw new Error(response.body); + } + + return (response.body); + } + + async function deleteAllRules(rules) { + + if (!Array.isArray(rules.data)) { + return null; + } + + const ids = rules.data.map(rule => rule.id); + + const data = { + "delete": { + "ids": ids } } - // if 4 numeric tags that look like a geo area then set geo area - var bits = node.tags.split(","); - if (bits.length == 4) { - if ((Number(bits[0]) < Number(bits[2])) && (Number(bits[1]) < Number(bits[3]))) { - st = { locations: node.tags }; - node.log(RED._("twitter.status.using-geo",{location:node.tags.toString()})); + const response = await needle('post', rulesURL, data, { + headers: { + "content-type": "application/json", + "authorization": `Bearer ${token}` + } + }) + + if (response.statusCode !== 200) { + throw new Error(response.body); + } + + return (response.body); + + } + + async function setRules() { + + const data = { + "add": rules + } + + const response = await needle('post', rulesURL, data, { + headers: { + "content-type": "application/json", + "authorization": `Bearer ${token}` + } + }) + + if (response.statusCode !== 201) { + throw new Error(response.body); + } + + return (response.body); + + } + + function streamConnect(retryAttempt) { + var flag = false; + const stream = needle.get(streamURL, { + headers: { + "User-Agent": "v2FilterStreamJS", + "Authorization": `Bearer ${token}` + }, + timeout: 20000 + }); + node.stream = stream; + + stream.on('data', data => { + try { + const json = JSON.parse(data); + // console.log(json); + node.status({ fill: "green", shape: "dot", text: (tags || " ") }); + node.send({ topic: "tweet", payload: json.data.text }); + + // A successful connection resets retry count. + retryAttempt = 0; + } catch (e) { + if (data.detail === "This stream is currently at the maximum allowed connection limit.") { + console.log(data.detail) + process.exit(1) + } else { + // Keep alive signal received. Do nothing. + } + } + }).on('err', error => { + if (error.code !== 'ECONNRESET') { + console.log(error.code); + process.exit(1); + } else { + // This reconnection logic will attempt to reconnect when a disconnection is detected. + // To avoid rate limits, this logic implements exponential backoff, so the wait time + // will increase if the client cannot reconnect to the stream. + setTimeout(() => { + console.warn("A connection error occurred. Reconnecting...") + streamConnect(++retryAttempt); + node.status({ fill: "red", shape: "ring", text: RED._("twitter.errors") }); + }, 2 ** retryAttempt) + } + if (node.restart) { + node.tout = setTimeout(function () { setupStream() }, retry); + } + }).on('limit', limit => { + //node.status({fill:"grey", shape:"dot", text:RED._("twitter.errors.limitrate")}); + node.status({ fill: "grey", shape: "dot", text: (tags || " ") }); + }).on('destroy', function (response) { + twitterRateTimeout = Date.now() + 15000; + if (node.restart) { + node.status({ fill: "red", shape: "dot", text: " " }); + node.warn(RED._("twitter.errors.unexpectedend")); + node.tout = setTimeout(function () { setupStream() }, 15000); + } + }); + + return stream; + + } + + try { + var tags = node.tags; + var st = { track: [tags] }; + var setupStream = function () { + if (node.restart) { + (async () => { + let currentRules; + console.warn = ({ topic: "warning", payload: node.restart }) + try { + // Gets the complete list of rules currently applied to the stream + currentRules = await getAllRules(); + // Delete all rules. Comment the line below if you want to keep your existing rules. + await deleteAllRules(currentRules); + // Add rules to the stream. Comment the line below if you don't want to add new rules. + await setRules(); + } catch (e) { + console.error(e); + process.exit(1); + } + // Listen to the stream. + // node.status({fill:"green", shape:"dot", text:(node.tags||" ")}); + console.log("Twitter API is steraming public tweets with search term " + node.tags || " "); + + var flag = false; + const stream = needle.get(streamURL, { + headers: { + "User-Agent": "v2FilterStreamJS", + "Authorization": `Bearer ${token}` + }, + timeout: 20000 + }); + node.stream = stream; + + stream.on('data', data => { + try { + const json = JSON.parse(data); + // console.log(json); + node.status({ fill: "green", shape: "dot", text: (tags || " ") }); + var msg = { topic: "tweet", payload: json.data.text }; + node.send(msg); + + // A successful connection resets retry count. + retryAttempt = 0; + } catch (e) { + if (data.detail === "This stream is currently at the maximum allowed connection limit.") { + console.log(data.detail) + // process.exit(1) + } else { + // Keep alive signal received. Do nothing. + } + } + }).on('err', error => { + if (error.code !== 'ECONNRESET') { + console.log(error.code); + // process.exit(1); + } else { + // This reconnection logic will attempt to reconnect when a disconnection is detected. + // To avoid rate limits, this logic implements exponential backoff, so the wait time + // will increase if the client cannot reconnect to the stream. + setTimeout(() => { + console.warn("A connection error occurred. Reconnecting...") + streamConnect(++retryAttempt); + node.status({ fill: "red", shape: "ring", text: RED._("twitter.errors") }); + }, 2 ** retryAttempt) + } + if (node.restart) { + node.tout = setTimeout(function () { setupStream() }, retry); + } + }).on('limit', limit => { + //node.status({fill:"grey", shape:"dot", text:RED._("twitter.errors.limitrate")}); + node.status({ fill: "grey", shape: "dot", text: (tags || " ") }); + }).on('destroy', function (response) { + twitterRateTimeout = Date.now() + 15000; + if (node.restart) { + node.status({ fill: "red", shape: "dot", text: " " }); + node.warn(RED._("twitter.errors.unexpectedend")); + node.tout = setTimeout(function () { setupStream() }, 15000); + } + }); + + })(); } } // all public tweets if (this.user === "false") { - node.on("input", function(msg) { + node.on("input", function (msg) { if (this.tags === '') { if (node.tout) { clearTimeout(node.tout); } if (node.tout2) { clearTimeout(node.tout2); } if (this.stream) { this.restart = false; node.stream.removeAllListeners(); - this.stream.destroy(); + this.stream.request.abort(); } if ((typeof msg.payload === "string") && (msg.payload !== "")) { - st = { track:[msg.payload] }; + st = { track: [msg.payload] }; tags = msg.payload; this.restart = true; - if ((twitterRateTimeout - Date.now()) > 0 ) { - node.status({fill:"red", shape:"ring", text:tags}); - node.tout = setTimeout(function() { + if ((twitterRateTimeout - Date.now()) > 0) { + node.status({ fill: "red", shape: "ring", text: tags }); + node.tout = setTimeout(function () { setupStream(); - }, twitterRateTimeout - Date.now() ); + }, twitterRateTimeout - Date.now()); } else { setupStream(); } } else { - node.status({fill:"yellow", shape:"ring", text:RED._("twitter.warn.waiting")}); + node.status({ fill: "yellow", shape: "ring", text: RED._("twitter.warn.waiting") }); } } }); @@ -302,7 +519,7 @@ module.exports = function(RED) { // wait for input or start the stream if ((this.user === "false") && (tags === '')) { - node.status({fill:"yellow", shape:"ring", text:RED._("twitter.warn.waiting")}); + node.status({ fill: "yellow", shape: "ring", text: RED._("twitter.warn.waiting") }); } else { this.restart = true; @@ -313,21 +530,21 @@ module.exports = function(RED) { node.error(err); } } - this.on('close', function() { + this.on('close', function () { if (this.tout) { clearTimeout(this.tout); } if (this.tout2) { clearTimeout(this.tout2); } if (this.stream) { this.restart = false; - this.stream.removeAllListeners(); - this.stream.destroy(); + node.stream.removeAllListeners(); + this.stream.request.abort(); } if (this.timeout_ids) { - for (var i=0; i 0) { since = res[0].id_str; } - pollId = setInterval(function() { + pollId = setInterval(function () { opts.since_id = since; - node.twitterConfig.get(url,opts).then(function(result) { + node.twitterConfig.get(url, opts).then(function (result) { if (result.status === 429) { - node.warn("Rate limit hit. Waiting "+Math.floor(result.rateLimitTimeout/1000)+" seconds to try again"); + node.warn("Rate limit hit. Waiting " + Math.floor(result.rateLimitTimeout / 1000) + " seconds to try again"); clearInterval(pollId); - node.timeout_ids.push(setTimeout(function() { - node.poll(interval,url,opts); - },result.rateLimitTimeout)) + node.timeout_ids.push(setTimeout(function () { + node.poll(interval, url, opts); + }, result.rateLimitTimeout)) return; } - node.debug("Twitter Poll, rateLimitRemaining="+result.rateLimitRemaining+" rateLimitTimeout="+Math.floor(result.rateLimitTimeout/1000)+"s"); + node.debug("Twitter Poll, rateLimitRemaining=" + result.rateLimitRemaining + " rateLimitTimeout=" + Math.floor(result.rateLimitTimeout / 1000) + "s"); var res = result.body; if (res.errors) { node.error(res.errors[0].message); @@ -379,77 +596,77 @@ module.exports = function(RED) { delete opts.since_id; } clearInterval(pollId); - node.timeout_ids.push(setTimeout(function() { - node.poll(interval,url,opts); - },interval)) + node.timeout_ids.push(setTimeout(function () { + node.poll(interval, url, opts); + }, interval)) return; } if (res.length > 0) { since = res[0].id_str; var len = res.length; - for (var i = len-1; i >= 0; i--) { + for (var i = len - 1; i >= 0; i--) { var tweet = res[i]; if (tweet.user !== undefined) { var where = tweet.user.location; var la = tweet.lang || tweet.user.lang; tweet.text = tweet.text || tweet.full_text; var msg = { - topic:"tweets/"+tweet.user.screen_name, - payload:tweet.text, - lang:la, - tweet:tweet + topic: "tweets/" + tweet.user.screen_name, + payload: tweet.text, + lang: la, + tweet: tweet }; if (where) { - msg.location = {place:where}; + msg.location = { place: where }; addLocationToTweet(msg); } node.send(msg); } } } - }).catch(function(err) { + }).catch(function (err) { node.error(err); clearInterval(pollId); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { delete opts.since_id; delete opts.count; - node.poll(interval,url,opts); - },interval)) + node.poll(interval, url, opts); + }, interval)) }) - },interval) + }, interval) node.poll_ids.push(pollId); - }).catch(function(err) { + }).catch(function (err) { node.error(err); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { delete opts.since_id; delete opts.count; - node.poll(interval,url,opts); - },interval)) + node.poll(interval, url, opts); + }, interval)) }) } - TwitterInNode.prototype.pollDirectMessages = function() { + TwitterInNode.prototype.pollDirectMessages = function () { var interval = 70000; var node = this; var opts = {}; var url = "https://api.twitter.com/1.1/direct_messages/events/list.json"; var pollId; opts.count = 50; - this.twitterConfig.get(url,opts).then(function(result) { + this.twitterConfig.get(url, opts).then(function (result) { if (result.status === 429) { - node.warn("Rate limit hit. Waiting "+Math.floor(result.rateLimitTimeout/1000)+" seconds to try again"); - node.timeout_ids.push(setTimeout(function() { + node.warn("Rate limit hit. Waiting " + Math.floor(result.rateLimitTimeout / 1000) + " seconds to try again"); + node.timeout_ids.push(setTimeout(function () { node.pollDirectMessages(); - },result.rateLimitTimeout)) + }, result.rateLimitTimeout)) return; } - node.debug("Twitter DM Poll, rateLimitRemaining="+result.rateLimitRemaining+" rateLimitTimeout="+Math.floor(result.rateLimitTimeout/1000)+"s"); + node.debug("Twitter DM Poll, rateLimitRemaining=" + result.rateLimitRemaining + " rateLimitTimeout=" + Math.floor(result.rateLimitTimeout / 1000) + "s"); var res = result.body; if (res.errors) { node.error(res.errors[0].message); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { node.pollDirectMessages(); - },interval)) + }, interval)) return; } var since = "0"; @@ -457,24 +674,24 @@ module.exports = function(RED) { if (messages.length > 0) { since = messages[0].id; } - pollId = setInterval(function() { - node.twitterConfig.get(url,opts).then(function(result) { + pollId = setInterval(function () { + node.twitterConfig.get(url, opts).then(function (result) { if (result.status === 429) { - node.warn("Rate limit hit. Waiting "+Math.floor(result.rateLimitTimeout/1000)+" seconds to try again"); + node.warn("Rate limit hit. Waiting " + Math.floor(result.rateLimitTimeout / 1000) + " seconds to try again"); clearInterval(pollId); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { node.pollDirectMessages(); - },result.rateLimitTimeout)) + }, result.rateLimitTimeout)) return; } - node.debug("Twitter DM Poll, rateLimitRemaining="+result.rateLimitRemaining+" rateLimitTimeout="+Math.floor(result.rateLimitTimeout/1000)+"s"); + node.debug("Twitter DM Poll, rateLimitRemaining=" + result.rateLimitRemaining + " rateLimitTimeout=" + Math.floor(result.rateLimitTimeout / 1000) + "s"); var res = result.body; if (res.errors) { node.error(res.errors[0].message); clearInterval(pollId); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { node.pollDirectMessages(); - },interval)) + }, interval)) return; } var messages = res.events.filter(tweet => tweet.type === 'message_create' && tweet.id > since); @@ -483,7 +700,7 @@ module.exports = function(RED) { var len = messages.length; var missingUsers = {}; var tweets = []; - for (var i = len-1; i >= 0; i--) { + for (var i = len - 1; i >= 0; i--) { var tweet = messages[i]; // node.log(JSON.stringify(tweet," ",4)); var output = { @@ -503,7 +720,7 @@ module.exports = function(RED) { } var missingUsernames = Object.keys(missingUsers).join(","); - return node.twitterConfig.getUsers(missingUsernames).then(function() { + return node.twitterConfig.getUsers(missingUsernames).then(function () { var len = tweets.length; for (var i = 0; i < len; i++) { var tweet = messages[i]; @@ -520,9 +737,9 @@ module.exports = function(RED) { if (output.sender_screen_name !== node.twitterConfig.screen_name) { var msg = { - topic:"tweets/"+output.sender_screen_name, - payload:output.text, - tweet:output + topic: "tweets/" + output.sender_screen_name, + payload: output.text, + tweet: output }; node.send(msg); } @@ -530,25 +747,25 @@ module.exports = function(RED) { }) } - }).catch(function(err) { + }).catch(function (err) { node.error(err); clearInterval(pollId); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { node.pollDirectMessages(); - },interval)) + }, interval)) }) - },interval) + }, interval) node.poll_ids.push(pollId); - }).catch(function(err) { + }).catch(function (err) { node.error(err); - node.timeout_ids.push(setTimeout(function() { + node.timeout_ids.push(setTimeout(function () { node.pollDirectMessages(); - },interval)) + }, interval)) }) } function TwitterOutNode(n) { - RED.nodes.createNode(this,n); + RED.nodes.createNode(this, n); this.topic = n.topic; this.twitter = n.twitter; this.twitterConfig = RED.nodes.getNode(this.twitter); @@ -563,10 +780,10 @@ module.exports = function(RED) { access_token_secret: credentials.access_token_secret }); - node.on("input", function(msg) { + node.on("input", function (msg) { if (msg.hasOwnProperty("payload")) { - node.status({fill:"blue",shape:"dot",text:"twitter.status.tweeting"}); - if (msg.payload.slice(0,2).toLowerCase() === "d ") { + node.status({ fill: "blue", shape: "dot", text: "twitter.status.tweeting" }); + if (msg.payload.slice(0, 2).toLowerCase() === "d ") { var dm_user; // direct message syntax: "D user message" var t = msg.payload.match(/D\s+(\S+)\s+(.*)/).slice(1); @@ -576,38 +793,38 @@ module.exports = function(RED) { if (userSreenNameToIdCache.hasOwnProperty(dm_user)) { lookupPromise = Promise.resolve(); } else { - lookupPromise = node.twitterConfig.getUsers(dm_user,"screen_name") + lookupPromise = node.twitterConfig.getUsers(dm_user, "screen_name") } - lookupPromise.then(function() { + lookupPromise.then(function () { if (userSreenNameToIdCache.hasOwnProperty(dm_user)) { // Send a direct message - node.twitterConfig.post("https://api.twitter.com/1.1/direct_messages/events/new.json",{ + node.twitterConfig.post("https://api.twitter.com/1.1/direct_messages/events/new.json", { event: { type: "message_create", "message_create": { "target": { "recipient_id": userSreenNameToIdCache[dm_user] }, - "message_data": {"text": msg.payload} + "message_data": { "text": msg.payload } } } - }).then(function() { + }).then(function () { node.status({}); - }).catch(function(err) { - node.error(err,msg); - node.status({fill:"red",shape:"ring",text:"twitter.status.failed"}); + }).catch(function (err) { + node.error(err, msg); + node.status({ fill: "red", shape: "ring", text: "twitter.status.failed" }); }); } else { - node.error("Unknown user",msg); - node.status({fill:"red",shape:"ring",text:"twitter.status.failed"}); + node.error("Unknown user", msg); + node.status({ fill: "red", shape: "ring", text: "twitter.status.failed" }); } - }).catch(function(err) { - node.error(err,msg); - node.status({fill:"red",shape:"ring",text:"twitter.status.failed"}); + }).catch(function (err) { + node.error(err, msg); + node.status({ fill: "red", shape: "ring", text: "twitter.status.failed" }); }) } else { if (msg.payload.length > 280) { - msg.payload = msg.payload.slice(0,279); + msg.payload = msg.payload.slice(0, 279); node.warn(RED._("twitter.errors.truncated")); } var mediaPromise; @@ -618,9 +835,9 @@ module.exports = function(RED) { // node.error("msg.media is not a valid media object",msg); // return; // } - mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json",null,null,{ + mediaPromise = node.twitterConfig.post("https://upload.twitter.com/1.1/media/upload.json", null, null, { media: msg.media - }).then(function(result) { + }).then(function (result) { if (result.status === 200) { return result.body.media_id_string; } else { @@ -631,31 +848,31 @@ module.exports = function(RED) { } else { mediaPromise = Promise.resolve(); } - mediaPromise.then(function(mediaId) { + mediaPromise.then(function (mediaId) { var params = msg.params || {}; params.status = msg.payload; if (mediaId) { params.media_ids = mediaId; } - node.twitterConfig.post("https://api.twitter.com/1.1/statuses/update.json",{},params).then(function(result) { + node.twitterConfig.post("https://api.twitter.com/1.1/statuses/update.json", {}, params).then(function (result) { if (result.status === 200) { node.status({}); } else { - node.status({fill:"red",shape:"ring",text:"twitter.status.failed"}); - + node.status({ fill: "red", shape: "ring", text: "twitter.status.failed" }); + if ('error' in result.body && typeof result.body.error === 'string') { - node.error(result.body.error,msg); + node.error(result.body.error, msg); } else { - node.error(result.body.errors[0].message,msg); + node.error(result.body.errors[0].message, msg); } } - }).catch(function(err) { - node.status({fill:"red",shape:"ring",text:"twitter.status.failed"}); - node.error(err,msg); + }).catch(function (err) { + node.status({ fill: "red", shape: "ring", text: "twitter.status.failed" }); + node.error(err, msg); }) - }).catch(function(err) { - node.status({fill:"red",shape:"ring",text:"twitter.status.failed"}); - node.error(err,msg); + }).catch(function (err) { + node.status({ fill: "red", shape: "ring", text: "twitter.status.failed" }); + node.error(err, msg); }); // if (msg.payload.length > 280) { // msg.payload = msg.payload.slice(0,279); @@ -702,5 +919,5 @@ module.exports = function(RED) { this.error(RED._("twitter.errors.missingcredentials")); } } - RED.nodes.registerType("twitter out",TwitterOutNode); + RED.nodes.registerType("twitter out", TwitterOutNode); } From 3c83afdecc9d9a6c564eb636f67838b567766a78 Mon Sep 17 00:00:00 2001 From: shahramdj Date: Fri, 7 Oct 2022 10:30:38 -0400 Subject: [PATCH 02/13] Update 27-twitter.html Added bearer token to support API V2.0 --- social/twitter/27-twitter.html | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/social/twitter/27-twitter.html b/social/twitter/27-twitter.html index 57ea833b..3637abf5 100644 --- a/social/twitter/27-twitter.html +++ b/social/twitter/27-twitter.html @@ -29,6 +29,10 @@ +
+ + +
+ + + + + + + + + + +TomcatUTF-8/EUCȤ + + + + + + +
The Wayback Machine - https://web.archive.org/web/20181003202907/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html
+ + +

TomcatUTF-8/EUC-JPȤ

+

+[Фμ¸ Slackware] +

+

+ : 2004/12/31 +

+
+

+"Фμ¸"θ + + + +

+
+
+ +

+Tomcat֤륭饯åȤξϡhttpd.confΥ롼ȤǻꤷAddDefaultCharsetͤƱˤʤ餷 +Directoryǥ쥯ƥ֤ǻꤷAddDefaultCharset̵뤵äݤ +ĤǤˡmeta̵뤵ߤ +<---Τؤ󡢸ҤSetCharacterEncodingFilterưʤȤ⤢ꡢʤ... +

+ +

+WEBФϥ롼ȤAddDefaultCharsetEUC-JP򤷤ƤꡢƥȥѥʲUTF-8ˤΤǡʤ餫к򤷤ʤʸƤޤ +

+ +

֥åȤξ

+

+response.setContentTypeǥ饯åȤꤹ롣 +EUC-JPѤʤ顢response.setContentType("text/html; charset=EUC-JP") +UTF-8Ѥʤ顢response.setContentType("text/html; charset=UTF-8") +

+
+# HelloWorld.java
+
+import java.io.*;
+import java.text.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class HelloWorld extends HttpServlet {
+
+    public void doGet(HttpServletRequest request,
+                      HttpServletResponse response)
+        throws IOException, ServletException
+    {
+        response.setContentType("text/html; charset=EUC-JP");
+        PrintWriter out = response.getWriter();
+
+        out.println("<html>");
+        out.println("<head>");
+        out.println("<title>HelloWorld</title>");
+        out.println("</head>");
+        out.println("<body>");
+        out.println("<p>");
+        out.println("ˤ");
+        out.println("</p>");
+        out.println("</body>");
+        out.println("</html>");
+    }
+}
+
+

+JAVAUTF-8ǽԤΤǡʳEUC-JPʤɤѤϡѥ뤹Ȥ-encodingĤ뤳ȡ +

+
+# javac -encoding EUC-JP -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar HelloWorld.java
+
+ +

JSPξ

+

+ǥ쥯ƥ֤ǥ饯åȤꤹ롣 +EUC-JPѤʤ顢<%@ page contentType="text/html; charset=EUC-JP" %> +UTF-8Ѥʤ顢<%@ page contentType="text/html; charset=UTF-8" %> +

+
+# hello.jsp
+
+<%@ page contentType="text/html; charset=EUC-JP" %>
+<html>
+<head>
+<title>HelloWorld</title>
+</head>
+<body>
+<p>
+<%
+    out.println("ˤ");
+%>
+</p>
+</body>
+</html>
+
+ +

ŪƥġHTMLˤξ

+

+workers2.propetiesեǡƥȥѥʲΤ٤ƤΥեTomcatϤ褦ꤷƤ硢ŪƥĤˤĤƤ⤳ΥڡƬ˽񤤤褦ʥ饯åȾ֤롣 +HTMLmetacharsetꤷƤ̵뤵Τǡɥȥ롼Ȥȥƥȥѥǰۤʤ륭饯åȤѤȤդɬס +

+
+# workers2.properties
+
+[uri:/hoge/*]    <---٤ƤΥեTomcat˽
+
+

SetCharacterEncodingFilterѤΤʤΤ褦ɤäƤcharset֤Ƥʤ +ʤΤǡweb.xml<mime-mapping>charsetȳĥҤδϢդꤷ +

+
+<!--ʥƥȥѥ/WEB-INF/web.xml-->
+
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!DOCTYPE web-app
+     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+    "http://java.sun.com/dtd/web-app_2_3.dtd">
+<web-app>
+  <mime-mapping>
+    <extension>html</extension>
+    <mime-type>text/html; charset=UTF-8</mime-type>
+  </mime-mapping>
+</web-app>
+
+

+SetCharacterEncodingFilterѤˡ񤤤Ƥȡ$CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes/filtersǥ쥯ȥˤSetCharacterEncodingFilter.java򥳥ѥ뤷ơ +

+
+# cd $CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes
+# javac -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar filters.SetCharacterEncodingFilter.java
+
+

+줿饹եʥƥȥѥ/WEB-INF/classes/filtersǥ쥯ȥ˥ԡơʥƥȥѥ/WEB-INF/web.xml˥ե륿򵭽Ҥ餷 +

+
+<!--ʥƥȥѥ/WEB-INF/web.xml-->
+
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!DOCTYPE web-app
+     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+    "http://java.sun.com/dtd/web-app_2_3.dtd">
+<web-app>
+  <filter>
+      <filter-name>Set Character Encoding</filter-name>
+      <filter-class>filters.SetCharacterEncodingFilter</filter-class>
+      <init-param>
+          <param-name>encoding</param-name>
+          <param-value>UTF-8</param-value>
+      </init-param>
+  </filter>
+  <filter-mapping>
+      <filter-name>Set Character Encoding</filter-name>
+      <url-pattern>/*</url-pattern>
+  </filter-mapping>
+</web-app>
+
+

+... +

+ +
+

+[Фμ¸ Slackware] +

+ + + + + \ No newline at end of file diff --git a/social/twitter/node_modules/needle/test/uri_modifier_spec.js b/social/twitter/node_modules/needle/test/uri_modifier_spec.js new file mode 100644 index 00000000..1a12a36c --- /dev/null +++ b/social/twitter/node_modules/needle/test/uri_modifier_spec.js @@ -0,0 +1,46 @@ +var needle = require('../'), + sinon = require('sinon'), + should = require('should'), + http = require('http'), + helpers = require('./helpers'); + +var port = 3456; + +describe('uri_modifier config parameter function', function() { + + var server, uri; + + function send_request(mw, cb) { + needle.get(uri, { uri_modifier: mw }, cb); + } + + before(function(done){ + server = helpers.server({ port: port }, done); + }) + + after(function(done) { + server.close(done); + }) + + describe('modifies uri', function() { + + var path = '/foo/replace'; + + before(function() { + uri = 'localhost:' + port + path + }); + + it('should modify path', function(done) { + send_request(function(uri) { + return uri.replace('/replace', ''); + }, function(err, res) { + should.not.exist(err); + should(res.req.path).be.exactly('/foo'); + done(); + }); + + }); + + }) + +}) diff --git a/social/twitter/node_modules/needle/test/url_spec.js b/social/twitter/node_modules/needle/test/url_spec.js new file mode 100644 index 00000000..5154d584 --- /dev/null +++ b/social/twitter/node_modules/needle/test/url_spec.js @@ -0,0 +1,155 @@ +var needle = require('../'), + sinon = require('sinon'), + should = require('should'), + http = require('http'), + helpers = require('./helpers'); + +var port = 3456; + +describe('urls', function() { + + var server, url; + + function send_request(cb) { + return needle.get(url, cb); + } + + before(function(done){ + server = helpers.server({ port: port }, done); + }) + + after(function(done) { + server.close(done); + }) + + describe('null URL', function(){ + + it('throws', function(){ + (function() { + send_request() + }).should.throw(); + }) + + }) + + describe('invalid protocol', function(){ + + before(function() { + url = 'foo://google.com/what' + }) + + it('does not throw', function(done) { + (function() { + send_request(function(err) { + done(); + }) + }).should.not.throw() + }) + + it('returns an error', function(done) { + send_request(function(err) { + err.should.be.an.Error; + err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/) + done(); + }) + }) + + }) + + describe('invalid host', function(){ + + before(function() { + url = 'http://s1\\\u0002.com/' + }) + + it('fails', function(done) { + (function() { + send_request(function(){ }) + }.should.throw(TypeError)) + done() + }) + + }) + +/* + describe('invalid path', function(){ + + before(function() { + url = 'http://www.google.com\\\/x\\\ %^&*() /x2.com/' + }) + + it('fails', function(done) { + send_request(function(err) { + err.should.be.an.Error; + done(); + }) + }) + + }) +*/ + + describe('valid protocol and path', function() { + + before(function() { + url = 'http://localhost:' + port + '/foo'; + }) + + it('works', function(done) { + send_request(function(err){ + should.not.exist(err); + done(); + }) + }) + + }) + + describe('no protocol but with slashes and valid path', function() { + + before(function() { + url = '//localhost:' + port + '/foo'; + }) + + it('works', function(done) { + send_request(function(err){ + should.not.exist(err); + done(); + }) + }) + + }) + + describe('no protocol nor slashes and valid path', function() { + + before(function() { + url = 'localhost:' + port + '/foo'; + }) + + it('works', function(done) { + send_request(function(err){ + should.not.exist(err); + done(); + }) + }) + + }) + + describe('double encoding', function() { + + var path = '/foo?email=' + encodeURIComponent('what-ever@Example.Com'); + + before(function() { + url = 'localhost:' + port + path + }); + + it('should not occur', function(done) { + send_request(function(err, res) { + should.not.exist(err); + should(res.req.path).be.exactly(path); + done(); + }); + + }); + + }) + +}) diff --git a/social/twitter/node_modules/needle/test/utils/formidable.js b/social/twitter/node_modules/needle/test/utils/formidable.js new file mode 100644 index 00000000..ba1d983e --- /dev/null +++ b/social/twitter/node_modules/needle/test/utils/formidable.js @@ -0,0 +1,17 @@ +var formidable = require('formidable'), + http = require('http'), + util = require('util'); + +var port = process.argv[2] || 8888; + +http.createServer(function(req, res) { + var form = new formidable.IncomingForm(); + form.parse(req, function(err, fields, files) { + res.writeHead(200, {'content-type': 'text/plain'}); + res.write('received upload:\n\n'); + console.log(util.inspect({fields: fields, files: files})) + res.end(util.inspect({fields: fields, files: files})); + }); +}).listen(port); + +console.log('HTTP server listening on port ' + port); \ No newline at end of file diff --git a/social/twitter/node_modules/needle/test/utils/proxy.js b/social/twitter/node_modules/needle/test/utils/proxy.js new file mode 100644 index 00000000..531bf493 --- /dev/null +++ b/social/twitter/node_modules/needle/test/utils/proxy.js @@ -0,0 +1,62 @@ +var http = require('http'), + https = require('https'), + url = require('url'); + +var port = 1234, + log = true, + request_auth = false; + +http.createServer(function(request, response) { + + console.log(request.headers); + console.log("Got request: " + request.url); + console.log("Forwarding request to " + request.headers['host']); + + if (request_auth) { + if (!request.headers['proxy-authorization']) { + response.writeHead(407, {'Proxy-Authenticate': 'Basic realm="proxy.com"'}) + return response.end('Hello.'); + } + } + + var remote = url.parse(request.url); + var protocol = remote.protocol == 'https:' ? https : http; + + var opts = { + host: request.headers['host'], + port: remote.port || (remote.protocol == 'https:' ? 443 : 80), + method: request.method, + path: remote.pathname, + headers: request.headers + } + + var proxy_request = protocol.request(opts, function(proxy_response){ + + proxy_response.on('data', function(chunk) { + if (log) console.log(chunk.toString()); + response.write(chunk, 'binary'); + }); + proxy_response.on('end', function() { + response.end(); + }); + + response.writeHead(proxy_response.statusCode, proxy_response.headers); + }); + + request.on('data', function(chunk) { + if (log) console.log(chunk.toString()); + proxy_request.write(chunk, 'binary'); + }); + + request.on('end', function() { + proxy_request.end(); + }); + +}).listen(port); + +process.on('uncaughtException', function(err){ + console.log('Uncaught exception!'); + console.log(err); +}); + +console.log("Proxy server listening on port " + port); diff --git a/social/twitter/node_modules/needle/test/utils/test.js b/social/twitter/node_modules/needle/test/utils/test.js new file mode 100644 index 00000000..8d58d70f --- /dev/null +++ b/social/twitter/node_modules/needle/test/utils/test.js @@ -0,0 +1,104 @@ +// TODO: write specs. :) + +var fs = require('fs'), + client = require('./../../'); + +process.env.DEBUG = true; + +var response_callback = function(err, resp, body){ + console.log(err); + if(resp) console.log("Got status code " + resp.statusCode) + console.log(body); +} + +function simple_head(){ + client.head('http://www.amazon.com', response_callback); +} + +function simple_get(){ + client.get('http://www.nodejs.org', response_callback); +} + +function proxy_get(){ + client.get('https://www.google.com/search?q=nodejs', {proxy: 'http://localhost:1234'}, response_callback); +} + +function auth_get(){ + client.get('https://www.twitter.com', {username: 'asd', password: '123'}, response_callback); +} + +function simple_post(url){ + + var data = { + foo: 'bar', + baz: { + nested: 'attribute' + } + } + + client.post(url, data, response_callback); + +} + +function multipart_post(url){ + + var filename = 'test_file.txt'; + var data = 'Plain text data.\nLorem ipsum dolor sit amet.\nBla bla bla.\n'; + fs.writeFileSync(filename, data); + + var black_pixel = Buffer.from("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64"); + + var data = { + foo: 'bar', + bar: 'baz', + nested: { + my_document: { file: filename, content_type: 'text/plain' }, + even: { + more: 'nesting' + } + }, + pixel: { filename: 'black_pixel.gif', buffer: black_pixel, content_type: 'image/gif' }, + field2: {value: JSON.stringify({"json":[ {"one":1}, {"two":2} ]}), content_type: 'application/json' } + } + + client.post(url, data, {multipart: true}, function(err, resp, body){ + + console.log(err); + console.log("Got status code " + resp.statusCode) + console.log(body); + fs.unlink(filename); + + }); + +} + +switch(process.argv[2]){ + case 'head': + simple_head(); + break; + case 'get': + simple_get(); + break; + case 'auth': + auth_get(); + break; + case 'proxy': + proxy_get(); + break; + case 'post': + simple_post(process.argv[3] || 'http://posttestserver.com/post.php'); + break; + case 'multipart': + multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example'); + break; + case 'all': + simple_head(); + simple_get(); + auth_get(); + proxy_get(); + simple_post(process.argv[3] || 'http://posttestserver.com/post.php'); + multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example'); + break; + default: + console.log("Usage: ./test.js [head|get|auth|proxy|multipart]") +} diff --git a/social/twitter/node_modules/safer-buffer/LICENSE b/social/twitter/node_modules/safer-buffer/LICENSE new file mode 100644 index 00000000..4fe9e6f1 --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Nikita Skovoroda + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/social/twitter/node_modules/safer-buffer/Porting-Buffer.md b/social/twitter/node_modules/safer-buffer/Porting-Buffer.md new file mode 100644 index 00000000..68d86bab --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/Porting-Buffer.md @@ -0,0 +1,268 @@ +# Porting to the Buffer.from/Buffer.alloc API + + +## Overview + +- [Variant 1: Drop support for Node.js ≤ 4.4.x and 5.0.0 — 5.9.x.](#variant-1) (*recommended*) +- [Variant 2: Use a polyfill](#variant-2) +- [Variant 3: manual detection, with safeguards](#variant-3) + +### Finding problematic bits of code using grep + +Just run `grep -nrE '[^a-zA-Z](Slow)?Buffer\s*\(' --exclude-dir node_modules`. + +It will find all the potentially unsafe places in your own code (with some considerably unlikely +exceptions). + +### Finding problematic bits of code using Node.js 8 + +If you’re using Node.js ≥ 8.0.0 (which is recommended), Node.js exposes multiple options that help with finding the relevant pieces of code: + +- `--trace-warnings` will make Node.js show a stack trace for this warning and other warnings that are printed by Node.js. +- `--trace-deprecation` does the same thing, but only for deprecation warnings. +- `--pending-deprecation` will show more types of deprecation warnings. In particular, it will show the `Buffer()` deprecation warning, even on Node.js 8. + +You can set these flags using an environment variable: + +```console +$ export NODE_OPTIONS='--trace-warnings --pending-deprecation' +$ cat example.js +'use strict'; +const foo = new Buffer('foo'); +$ node example.js +(node:7147) [DEP0005] DeprecationWarning: The Buffer() and new Buffer() constructors are not recommended for use due to security and usability concerns. Please use the new Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() construction methods instead. + at showFlaggedDeprecation (buffer.js:127:13) + at new Buffer (buffer.js:148:3) + at Object. (/path/to/example.js:2:13) + [... more stack trace lines ...] +``` + +### Finding problematic bits of code using linters + +Eslint rules [no-buffer-constructor](https://eslint.org/docs/rules/no-buffer-constructor) +or +[node/no-deprecated-api](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md) +also find calls to deprecated `Buffer()` API. Those rules are included in some pre-sets. + +There is a drawback, though, that it doesn't always +[work correctly](https://github.com/chalker/safer-buffer#why-not-safe-buffer) when `Buffer` is +overriden e.g. with a polyfill, so recommended is a combination of this and some other method +described above. + + +## Variant 1: Drop support for Node.js ≤ 4.4.x and 5.0.0 — 5.9.x. + +This is the recommended solution nowadays that would imply only minimal overhead. + +The Node.js 5.x release line has been unsupported since July 2016, and the Node.js 4.x release line reaches its End of Life in April 2018 (→ [Schedule](https://github.com/nodejs/Release#release-schedule)). This means that these versions of Node.js will *not* receive any updates, even in case of security issues, so using these release lines should be avoided, if at all possible. + +What you would do in this case is to convert all `new Buffer()` or `Buffer()` calls to use `Buffer.alloc()` or `Buffer.from()`, in the following way: + +- For `new Buffer(number)`, replace it with `Buffer.alloc(number)`. +- For `new Buffer(string)` (or `new Buffer(string, encoding)`), replace it with `Buffer.from(string)` (or `Buffer.from(string, encoding)`). +- For all other combinations of arguments (these are much rarer), also replace `new Buffer(...arguments)` with `Buffer.from(...arguments)`. + +Note that `Buffer.alloc()` is also _faster_ on the current Node.js versions than +`new Buffer(size).fill(0)`, which is what you would otherwise need to ensure zero-filling. + +Enabling eslint rule [no-buffer-constructor](https://eslint.org/docs/rules/no-buffer-constructor) +or +[node/no-deprecated-api](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md) +is recommended to avoid accidential unsafe Buffer API usage. + +There is also a [JSCodeshift codemod](https://github.com/joyeecheung/node-dep-codemod#dep005) +for automatically migrating Buffer constructors to `Buffer.alloc()` or `Buffer.from()`. +Note that it currently only works with cases where the arguments are literals or where the +constructor is invoked with two arguments. + +_If you currently support those older Node.js versions and dropping them would be a semver-major change +for you, or if you support older branches of your packages, consider using [Variant 2](#variant-2) +or [Variant 3](#variant-3) on older branches, so people using those older branches will also receive +the fix. That way, you will eradicate potential issues caused by unguarded Buffer API usage and +your users will not observe a runtime deprecation warning when running your code on Node.js 10._ + + +## Variant 2: Use a polyfill + +Utilize [safer-buffer](https://www.npmjs.com/package/safer-buffer) as a polyfill to support older +Node.js versions. + +You would take exacly the same steps as in [Variant 1](#variant-1), but with a polyfill +`const Buffer = require('safer-buffer').Buffer` in all files where you use the new `Buffer` api. + +Make sure that you do not use old `new Buffer` API — in any files where the line above is added, +using old `new Buffer()` API will _throw_. It will be easy to notice that in CI, though. + +Alternatively, you could use [buffer-from](https://www.npmjs.com/package/buffer-from) and/or +[buffer-alloc](https://www.npmjs.com/package/buffer-alloc) [ponyfills](https://ponyfill.com/) — +those are great, the only downsides being 4 deps in the tree and slightly more code changes to +migrate off them (as you would be using e.g. `Buffer.from` under a different name). If you need only +`Buffer.from` polyfilled — `buffer-from` alone which comes with no extra dependencies. + +_Alternatively, you could use [safe-buffer](https://www.npmjs.com/package/safe-buffer) — it also +provides a polyfill, but takes a different approach which has +[it's drawbacks](https://github.com/chalker/safer-buffer#why-not-safe-buffer). It will allow you +to also use the older `new Buffer()` API in your code, though — but that's arguably a benefit, as +it is problematic, can cause issues in your code, and will start emitting runtime deprecation +warnings starting with Node.js 10._ + +Note that in either case, it is important that you also remove all calls to the old Buffer +API manually — just throwing in `safe-buffer` doesn't fix the problem by itself, it just provides +a polyfill for the new API. I have seen people doing that mistake. + +Enabling eslint rule [no-buffer-constructor](https://eslint.org/docs/rules/no-buffer-constructor) +or +[node/no-deprecated-api](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md) +is recommended. + +_Don't forget to drop the polyfill usage once you drop support for Node.js < 4.5.0._ + + +## Variant 3 — manual detection, with safeguards + +This is useful if you create Buffer instances in only a few places (e.g. one), or you have your own +wrapper around them. + +### Buffer(0) + +This special case for creating empty buffers can be safely replaced with `Buffer.concat([])`, which +returns the same result all the way down to Node.js 0.8.x. + +### Buffer(notNumber) + +Before: + +```js +var buf = new Buffer(notNumber, encoding); +``` + +After: + +```js +var buf; +if (Buffer.from && Buffer.from !== Uint8Array.from) { + buf = Buffer.from(notNumber, encoding); +} else { + if (typeof notNumber === 'number') + throw new Error('The "size" argument must be of type number.'); + buf = new Buffer(notNumber, encoding); +} +``` + +`encoding` is optional. + +Note that the `typeof notNumber` before `new Buffer` is required (for cases when `notNumber` argument is not +hard-coded) and _is not caused by the deprecation of Buffer constructor_ — it's exactly _why_ the +Buffer constructor is deprecated. Ecosystem packages lacking this type-check caused numereous +security issues — situations when unsanitized user input could end up in the `Buffer(arg)` create +problems ranging from DoS to leaking sensitive information to the attacker from the process memory. + +When `notNumber` argument is hardcoded (e.g. literal `"abc"` or `[0,1,2]`), the `typeof` check can +be omitted. + +Also note that using TypeScript does not fix this problem for you — when libs written in +`TypeScript` are used from JS, or when user input ends up there — it behaves exactly as pure JS, as +all type checks are translation-time only and are not present in the actual JS code which TS +compiles to. + +### Buffer(number) + +For Node.js 0.10.x (and below) support: + +```js +var buf; +if (Buffer.alloc) { + buf = Buffer.alloc(number); +} else { + buf = new Buffer(number); + buf.fill(0); +} +``` + +Otherwise (Node.js ≥ 0.12.x): + +```js +const buf = Buffer.alloc ? Buffer.alloc(number) : new Buffer(number).fill(0); +``` + +## Regarding Buffer.allocUnsafe + +Be extra cautious when using `Buffer.allocUnsafe`: + * Don't use it if you don't have a good reason to + * e.g. you probably won't ever see a performance difference for small buffers, in fact, those + might be even faster with `Buffer.alloc()`, + * if your code is not in the hot code path — you also probably won't notice a difference, + * keep in mind that zero-filling minimizes the potential risks. + * If you use it, make sure that you never return the buffer in a partially-filled state, + * if you are writing to it sequentially — always truncate it to the actuall written length + +Errors in handling buffers allocated with `Buffer.allocUnsafe` could result in various issues, +ranged from undefined behaviour of your code to sensitive data (user input, passwords, certs) +leaking to the remote attacker. + +_Note that the same applies to `new Buffer` usage without zero-filling, depending on the Node.js +version (and lacking type checks also adds DoS to the list of potential problems)._ + + +## FAQ + + +### What is wrong with the `Buffer` constructor? + +The `Buffer` constructor could be used to create a buffer in many different ways: + +- `new Buffer(42)` creates a `Buffer` of 42 bytes. Before Node.js 8, this buffer contained + *arbitrary memory* for performance reasons, which could include anything ranging from + program source code to passwords and encryption keys. +- `new Buffer('abc')` creates a `Buffer` that contains the UTF-8-encoded version of + the string `'abc'`. A second argument could specify another encoding: For example, + `new Buffer(string, 'base64')` could be used to convert a Base64 string into the original + sequence of bytes that it represents. +- There are several other combinations of arguments. + +This meant that, in code like `var buffer = new Buffer(foo);`, *it is not possible to tell +what exactly the contents of the generated buffer are* without knowing the type of `foo`. + +Sometimes, the value of `foo` comes from an external source. For example, this function +could be exposed as a service on a web server, converting a UTF-8 string into its Base64 form: + +``` +function stringToBase64(req, res) { + // The request body should have the format of `{ string: 'foobar' }` + const rawBytes = new Buffer(req.body.string) + const encoded = rawBytes.toString('base64') + res.end({ encoded: encoded }) +} +``` + +Note that this code does *not* validate the type of `req.body.string`: + +- `req.body.string` is expected to be a string. If this is the case, all goes well. +- `req.body.string` is controlled by the client that sends the request. +- If `req.body.string` is the *number* `50`, the `rawBytes` would be 50 bytes: + - Before Node.js 8, the content would be uninitialized + - After Node.js 8, the content would be `50` bytes with the value `0` + +Because of the missing type check, an attacker could intentionally send a number +as part of the request. Using this, they can either: + +- Read uninitialized memory. This **will** leak passwords, encryption keys and other + kinds of sensitive information. (Information leak) +- Force the program to allocate a large amount of memory. For example, when specifying + `500000000` as the input value, each request will allocate 500MB of memory. + This can be used to either exhaust the memory available of a program completely + and make it crash, or slow it down significantly. (Denial of Service) + +Both of these scenarios are considered serious security issues in a real-world +web server context. + +when using `Buffer.from(req.body.string)` instead, passing a number will always +throw an exception instead, giving a controlled behaviour that can always be +handled by the program. + + +### The `Buffer()` constructor has been deprecated for a while. Is this really an issue? + +Surveys of code in the `npm` ecosystem have shown that the `Buffer()` constructor is still +widely used. This includes new code, and overall usage of such code has actually been +*increasing*. diff --git a/social/twitter/node_modules/safer-buffer/Readme.md b/social/twitter/node_modules/safer-buffer/Readme.md new file mode 100644 index 00000000..14b08229 --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/Readme.md @@ -0,0 +1,156 @@ +# safer-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![javascript style guide][standard-image]][standard-url] [![Security Responsible Disclosure][secuirty-image]][secuirty-url] + +[travis-image]: https://travis-ci.org/ChALkeR/safer-buffer.svg?branch=master +[travis-url]: https://travis-ci.org/ChALkeR/safer-buffer +[npm-image]: https://img.shields.io/npm/v/safer-buffer.svg +[npm-url]: https://npmjs.org/package/safer-buffer +[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg +[standard-url]: https://standardjs.com +[secuirty-image]: https://img.shields.io/badge/Security-Responsible%20Disclosure-green.svg +[secuirty-url]: https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md + +Modern Buffer API polyfill without footguns, working on Node.js from 0.8 to current. + +## How to use? + +First, port all `Buffer()` and `new Buffer()` calls to `Buffer.alloc()` and `Buffer.from()` API. + +Then, to achieve compatibility with outdated Node.js versions (`<4.5.0` and 5.x `<5.9.0`), use +`const Buffer = require('safer-buffer').Buffer` in all files where you make calls to the new +Buffer API. _Use `var` instead of `const` if you need that for your Node.js version range support._ + +Also, see the +[porting Buffer](https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md) guide. + +## Do I need it? + +Hopefully, not — dropping support for outdated Node.js versions should be fine nowdays, and that +is the recommended path forward. You _do_ need to port to the `Buffer.alloc()` and `Buffer.from()` +though. + +See the [porting guide](https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md) +for a better description. + +## Why not [safe-buffer](https://npmjs.com/safe-buffer)? + +_In short: while `safe-buffer` serves as a polyfill for the new API, it allows old API usage and +itself contains footguns._ + +`safe-buffer` could be used safely to get the new API while still keeping support for older +Node.js versions (like this module), but while analyzing ecosystem usage of the old Buffer API +I found out that `safe-buffer` is itself causing problems in some cases. + +For example, consider the following snippet: + +```console +$ cat example.unsafe.js +console.log(Buffer(20)) +$ ./node-v6.13.0-linux-x64/bin/node example.unsafe.js + +$ standard example.unsafe.js +standard: Use JavaScript Standard Style (https://standardjs.com) + /home/chalker/repo/safer-buffer/example.unsafe.js:2:13: 'Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead. +``` + +This is allocates and writes to console an uninitialized chunk of memory. +[standard](https://www.npmjs.com/package/standard) linter (among others) catch that and warn people +to avoid using unsafe API. + +Let's now throw in `safe-buffer`! + +```console +$ cat example.safe-buffer.js +const Buffer = require('safe-buffer').Buffer +console.log(Buffer(20)) +$ standard example.safe-buffer.js +$ ./node-v6.13.0-linux-x64/bin/node example.safe-buffer.js + +``` + +See the problem? Adding in `safe-buffer` _magically removes the lint warning_, but the behavior +remains identiсal to what we had before, and when launched on Node.js 6.x LTS — this dumps out +chunks of uninitialized memory. +_And this code will still emit runtime warnings on Node.js 10.x and above._ + +That was done by design. I first considered changing `safe-buffer`, prohibiting old API usage or +emitting warnings on it, but that significantly diverges from `safe-buffer` design. After some +discussion, it was decided to move my approach into a separate package, and _this is that separate +package_. + +This footgun is not imaginary — I observed top-downloaded packages doing that kind of thing, +«fixing» the lint warning by blindly including `safe-buffer` without any actual changes. + +Also in some cases, even if the API _was_ migrated to use of safe Buffer API — a random pull request +can bring unsafe Buffer API usage back to the codebase by adding new calls — and that could go +unnoticed even if you have a linter prohibiting that (becase of the reason stated above), and even +pass CI. _I also observed that being done in popular packages._ + +Some examples: + * [webdriverio](https://github.com/webdriverio/webdriverio/commit/05cbd3167c12e4930f09ef7cf93b127ba4effae4#diff-124380949022817b90b622871837d56cR31) + (a module with 548 759 downloads/month), + * [websocket-stream](https://github.com/maxogden/websocket-stream/commit/c9312bd24d08271687d76da0fe3c83493871cf61) + (218 288 d/m, fix in [maxogden/websocket-stream#142](https://github.com/maxogden/websocket-stream/pull/142)), + * [node-serialport](https://github.com/node-serialport/node-serialport/commit/e8d9d2b16c664224920ce1c895199b1ce2def48c) + (113 138 d/m, fix in [node-serialport/node-serialport#1510](https://github.com/node-serialport/node-serialport/pull/1510)), + * [karma](https://github.com/karma-runner/karma/commit/3d94b8cf18c695104ca195334dc75ff054c74eec) + (3 973 193 d/m, fix in [karma-runner/karma#2947](https://github.com/karma-runner/karma/pull/2947)), + * [spdy-transport](https://github.com/spdy-http2/spdy-transport/commit/5375ac33f4a62a4f65bcfc2827447d42a5dbe8b1) + (5 970 727 d/m, fix in [spdy-http2/spdy-transport#53](https://github.com/spdy-http2/spdy-transport/pull/53)). + * And there are a lot more over the ecosystem. + +I filed a PR at +[mysticatea/eslint-plugin-node#110](https://github.com/mysticatea/eslint-plugin-node/pull/110) to +partially fix that (for cases when that lint rule is used), but it is a semver-major change for +linter rules and presets, so it would take significant time for that to reach actual setups. +_It also hasn't been released yet (2018-03-20)._ + +Also, `safer-buffer` discourages the usage of `.allocUnsafe()`, which is often done by a mistake. +It still supports it with an explicit concern barier, by placing it under +`require('safer-buffer/dangereous')`. + +## But isn't throwing bad? + +Not really. It's an error that could be noticed and fixed early, instead of causing havoc later like +unguarded `new Buffer()` calls that end up receiving user input can do. + +This package affects only the files where `var Buffer = require('safer-buffer').Buffer` was done, so +it is really simple to keep track of things and make sure that you don't mix old API usage with that. +Also, CI should hint anything that you might have missed. + +New commits, if tested, won't land new usage of unsafe Buffer API this way. +_Node.js 10.x also deals with that by printing a runtime depecation warning._ + +### Would it affect third-party modules? + +No, unless you explicitly do an awful thing like monkey-patching or overriding the built-in `Buffer`. +Don't do that. + +### But I don't want throwing… + +That is also fine! + +Also, it could be better in some cases when you don't comprehensive enough test coverage. + +In that case — just don't override `Buffer` and use +`var SaferBuffer = require('safer-buffer').Buffer` instead. + +That way, everything using `Buffer` natively would still work, but there would be two drawbacks: + +* `Buffer.from`/`Buffer.alloc` won't be polyfilled — use `SaferBuffer.from` and + `SaferBuffer.alloc` instead. +* You are still open to accidentally using the insecure deprecated API — use a linter to catch that. + +Note that using a linter to catch accidential `Buffer` constructor usage in this case is strongly +recommended. `Buffer` is not overriden in this usecase, so linters won't get confused. + +## «Without footguns»? + +Well, it is still possible to do _some_ things with `Buffer` API, e.g. accessing `.buffer` property +on older versions and duping things from there. You shouldn't do that in your code, probabably. + +The intention is to remove the most significant footguns that affect lots of packages in the +ecosystem, and to do it in the proper way. + +Also, this package doesn't protect against security issues affecting some Node.js versions, so for +usage in your own production code, it is still recommended to update to a Node.js version +[supported by upstream](https://github.com/nodejs/release#release-schedule). diff --git a/social/twitter/node_modules/safer-buffer/dangerous.js b/social/twitter/node_modules/safer-buffer/dangerous.js new file mode 100644 index 00000000..ca41fdc5 --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/dangerous.js @@ -0,0 +1,58 @@ +/* eslint-disable node/no-deprecated-api */ + +'use strict' + +var buffer = require('buffer') +var Buffer = buffer.Buffer +var safer = require('./safer.js') +var Safer = safer.Buffer + +var dangerous = {} + +var key + +for (key in safer) { + if (!safer.hasOwnProperty(key)) continue + dangerous[key] = safer[key] +} + +var Dangereous = dangerous.Buffer = {} + +// Copy Safer API +for (key in Safer) { + if (!Safer.hasOwnProperty(key)) continue + Dangereous[key] = Safer[key] +} + +// Copy those missing unsafe methods, if they are present +for (key in Buffer) { + if (!Buffer.hasOwnProperty(key)) continue + if (Dangereous.hasOwnProperty(key)) continue + Dangereous[key] = Buffer[key] +} + +if (!Dangereous.allocUnsafe) { + Dangereous.allocUnsafe = function (size) { + if (typeof size !== 'number') { + throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) + } + if (size < 0 || size >= 2 * (1 << 30)) { + throw new RangeError('The value "' + size + '" is invalid for option "size"') + } + return Buffer(size) + } +} + +if (!Dangereous.allocUnsafeSlow) { + Dangereous.allocUnsafeSlow = function (size) { + if (typeof size !== 'number') { + throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) + } + if (size < 0 || size >= 2 * (1 << 30)) { + throw new RangeError('The value "' + size + '" is invalid for option "size"') + } + return buffer.SlowBuffer(size) + } +} + +module.exports = dangerous diff --git a/social/twitter/node_modules/safer-buffer/package.json b/social/twitter/node_modules/safer-buffer/package.json new file mode 100644 index 00000000..d452b04a --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/package.json @@ -0,0 +1,34 @@ +{ + "name": "safer-buffer", + "version": "2.1.2", + "description": "Modern Buffer API polyfill without footguns", + "main": "safer.js", + "scripts": { + "browserify-test": "browserify --external tape tests.js > browserify-tests.js && tape browserify-tests.js", + "test": "standard && tape tests.js" + }, + "author": { + "name": "Nikita Skovoroda", + "email": "chalkerx@gmail.com", + "url": "https://github.com/ChALkeR" + }, + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/ChALkeR/safer-buffer.git" + }, + "bugs": { + "url": "https://github.com/ChALkeR/safer-buffer/issues" + }, + "devDependencies": { + "standard": "^11.0.1", + "tape": "^4.9.0" + }, + "files": [ + "Porting-Buffer.md", + "Readme.md", + "tests.js", + "dangerous.js", + "safer.js" + ] +} diff --git a/social/twitter/node_modules/safer-buffer/safer.js b/social/twitter/node_modules/safer-buffer/safer.js new file mode 100644 index 00000000..37c7e1aa --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/safer.js @@ -0,0 +1,77 @@ +/* eslint-disable node/no-deprecated-api */ + +'use strict' + +var buffer = require('buffer') +var Buffer = buffer.Buffer + +var safer = {} + +var key + +for (key in buffer) { + if (!buffer.hasOwnProperty(key)) continue + if (key === 'SlowBuffer' || key === 'Buffer') continue + safer[key] = buffer[key] +} + +var Safer = safer.Buffer = {} +for (key in Buffer) { + if (!Buffer.hasOwnProperty(key)) continue + if (key === 'allocUnsafe' || key === 'allocUnsafeSlow') continue + Safer[key] = Buffer[key] +} + +safer.Buffer.prototype = Buffer.prototype + +if (!Safer.from || Safer.from === Uint8Array.from) { + Safer.from = function (value, encodingOrOffset, length) { + if (typeof value === 'number') { + throw new TypeError('The "value" argument must not be of type number. Received type ' + typeof value) + } + if (value && typeof value.length === 'undefined') { + throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type ' + typeof value) + } + return Buffer(value, encodingOrOffset, length) + } +} + +if (!Safer.alloc) { + Safer.alloc = function (size, fill, encoding) { + if (typeof size !== 'number') { + throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) + } + if (size < 0 || size >= 2 * (1 << 30)) { + throw new RangeError('The value "' + size + '" is invalid for option "size"') + } + var buf = Buffer(size) + if (!fill || fill.length === 0) { + buf.fill(0) + } else if (typeof encoding === 'string') { + buf.fill(fill, encoding) + } else { + buf.fill(fill) + } + return buf + } +} + +if (!safer.kStringMaxLength) { + try { + safer.kStringMaxLength = process.binding('buffer').kStringMaxLength + } catch (e) { + // we can't determine kStringMaxLength in environments where process.binding + // is unsupported, so let's not set it + } +} + +if (!safer.constants) { + safer.constants = { + MAX_LENGTH: safer.kMaxLength + } + if (safer.kStringMaxLength) { + safer.constants.MAX_STRING_LENGTH = safer.kStringMaxLength + } +} + +module.exports = safer diff --git a/social/twitter/node_modules/safer-buffer/tests.js b/social/twitter/node_modules/safer-buffer/tests.js new file mode 100644 index 00000000..7ed2777c --- /dev/null +++ b/social/twitter/node_modules/safer-buffer/tests.js @@ -0,0 +1,406 @@ +/* eslint-disable node/no-deprecated-api */ + +'use strict' + +var test = require('tape') + +var buffer = require('buffer') + +var index = require('./') +var safer = require('./safer') +var dangerous = require('./dangerous') + +/* Inheritance tests */ + +test('Default is Safer', function (t) { + t.equal(index, safer) + t.notEqual(safer, dangerous) + t.notEqual(index, dangerous) + t.end() +}) + +test('Is not a function', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(typeof impl, 'object') + t.equal(typeof impl.Buffer, 'object') + }); + [buffer].forEach(function (impl) { + t.equal(typeof impl, 'object') + t.equal(typeof impl.Buffer, 'function') + }) + t.end() +}) + +test('Constructor throws', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.throws(function () { impl.Buffer() }) + t.throws(function () { impl.Buffer(0) }) + t.throws(function () { impl.Buffer('a') }) + t.throws(function () { impl.Buffer('a', 'utf-8') }) + t.throws(function () { return new impl.Buffer() }) + t.throws(function () { return new impl.Buffer(0) }) + t.throws(function () { return new impl.Buffer('a') }) + t.throws(function () { return new impl.Buffer('a', 'utf-8') }) + }) + t.end() +}) + +test('Safe methods exist', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(typeof impl.Buffer.alloc, 'function', 'alloc') + t.equal(typeof impl.Buffer.from, 'function', 'from') + }) + t.end() +}) + +test('Unsafe methods exist only in Dangerous', function (t) { + [index, safer].forEach(function (impl) { + t.equal(typeof impl.Buffer.allocUnsafe, 'undefined') + t.equal(typeof impl.Buffer.allocUnsafeSlow, 'undefined') + }); + [dangerous].forEach(function (impl) { + t.equal(typeof impl.Buffer.allocUnsafe, 'function') + t.equal(typeof impl.Buffer.allocUnsafeSlow, 'function') + }) + t.end() +}) + +test('Generic methods/properties are defined and equal', function (t) { + ['poolSize', 'isBuffer', 'concat', 'byteLength'].forEach(function (method) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer[method], buffer.Buffer[method], method) + t.notEqual(typeof impl.Buffer[method], 'undefined', method) + }) + }) + t.end() +}) + +test('Built-in buffer static methods/properties are inherited', function (t) { + Object.keys(buffer).forEach(function (method) { + if (method === 'SlowBuffer' || method === 'Buffer') return; + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl[method], buffer[method], method) + t.notEqual(typeof impl[method], 'undefined', method) + }) + }) + t.end() +}) + +test('Built-in Buffer static methods/properties are inherited', function (t) { + Object.keys(buffer.Buffer).forEach(function (method) { + if (method === 'allocUnsafe' || method === 'allocUnsafeSlow') return; + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer[method], buffer.Buffer[method], method) + t.notEqual(typeof impl.Buffer[method], 'undefined', method) + }) + }) + t.end() +}) + +test('.prototype property of Buffer is inherited', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer.prototype, buffer.Buffer.prototype, 'prototype') + t.notEqual(typeof impl.Buffer.prototype, 'undefined', 'prototype') + }) + t.end() +}) + +test('All Safer methods are present in Dangerous', function (t) { + Object.keys(safer).forEach(function (method) { + if (method === 'Buffer') return; + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl[method], safer[method], method) + if (method !== 'kStringMaxLength') { + t.notEqual(typeof impl[method], 'undefined', method) + } + }) + }) + Object.keys(safer.Buffer).forEach(function (method) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer[method], safer.Buffer[method], method) + t.notEqual(typeof impl.Buffer[method], 'undefined', method) + }) + }) + t.end() +}) + +test('Safe methods from Dangerous methods are present in Safer', function (t) { + Object.keys(dangerous).forEach(function (method) { + if (method === 'Buffer') return; + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl[method], dangerous[method], method) + if (method !== 'kStringMaxLength') { + t.notEqual(typeof impl[method], 'undefined', method) + } + }) + }) + Object.keys(dangerous.Buffer).forEach(function (method) { + if (method === 'allocUnsafe' || method === 'allocUnsafeSlow') return; + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer[method], dangerous.Buffer[method], method) + t.notEqual(typeof impl.Buffer[method], 'undefined', method) + }) + }) + t.end() +}) + +/* Behaviour tests */ + +test('Methods return Buffers', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(0))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(0, 10))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(0, 'a'))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(10))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(10, 'x'))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(9, 'ab'))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from(''))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from('string'))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from('string', 'utf-8'))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64'))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from([0, 42, 3]))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from(new Uint8Array([0, 42, 3])))) + t.ok(buffer.Buffer.isBuffer(impl.Buffer.from([]))) + }); + ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { + t.ok(buffer.Buffer.isBuffer(dangerous.Buffer[method](0))) + t.ok(buffer.Buffer.isBuffer(dangerous.Buffer[method](10))) + }) + t.end() +}) + +test('Constructor is buffer.Buffer', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer.alloc(0).constructor, buffer.Buffer) + t.equal(impl.Buffer.alloc(0, 10).constructor, buffer.Buffer) + t.equal(impl.Buffer.alloc(0, 'a').constructor, buffer.Buffer) + t.equal(impl.Buffer.alloc(10).constructor, buffer.Buffer) + t.equal(impl.Buffer.alloc(10, 'x').constructor, buffer.Buffer) + t.equal(impl.Buffer.alloc(9, 'ab').constructor, buffer.Buffer) + t.equal(impl.Buffer.from('').constructor, buffer.Buffer) + t.equal(impl.Buffer.from('string').constructor, buffer.Buffer) + t.equal(impl.Buffer.from('string', 'utf-8').constructor, buffer.Buffer) + t.equal(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64').constructor, buffer.Buffer) + t.equal(impl.Buffer.from([0, 42, 3]).constructor, buffer.Buffer) + t.equal(impl.Buffer.from(new Uint8Array([0, 42, 3])).constructor, buffer.Buffer) + t.equal(impl.Buffer.from([]).constructor, buffer.Buffer) + }); + [0, 10, 100].forEach(function (arg) { + t.equal(dangerous.Buffer.allocUnsafe(arg).constructor, buffer.Buffer) + t.equal(dangerous.Buffer.allocUnsafeSlow(arg).constructor, buffer.SlowBuffer(0).constructor) + }) + t.end() +}) + +test('Invalid calls throw', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.throws(function () { impl.Buffer.from(0) }) + t.throws(function () { impl.Buffer.from(10) }) + t.throws(function () { impl.Buffer.from(10, 'utf-8') }) + t.throws(function () { impl.Buffer.from('string', 'invalid encoding') }) + t.throws(function () { impl.Buffer.from(-10) }) + t.throws(function () { impl.Buffer.from(1e90) }) + t.throws(function () { impl.Buffer.from(Infinity) }) + t.throws(function () { impl.Buffer.from(-Infinity) }) + t.throws(function () { impl.Buffer.from(NaN) }) + t.throws(function () { impl.Buffer.from(null) }) + t.throws(function () { impl.Buffer.from(undefined) }) + t.throws(function () { impl.Buffer.from() }) + t.throws(function () { impl.Buffer.from({}) }) + t.throws(function () { impl.Buffer.alloc('') }) + t.throws(function () { impl.Buffer.alloc('string') }) + t.throws(function () { impl.Buffer.alloc('string', 'utf-8') }) + t.throws(function () { impl.Buffer.alloc('b25ldHdvdGhyZWU=', 'base64') }) + t.throws(function () { impl.Buffer.alloc(-10) }) + t.throws(function () { impl.Buffer.alloc(1e90) }) + t.throws(function () { impl.Buffer.alloc(2 * (1 << 30)) }) + t.throws(function () { impl.Buffer.alloc(Infinity) }) + t.throws(function () { impl.Buffer.alloc(-Infinity) }) + t.throws(function () { impl.Buffer.alloc(null) }) + t.throws(function () { impl.Buffer.alloc(undefined) }) + t.throws(function () { impl.Buffer.alloc() }) + t.throws(function () { impl.Buffer.alloc([]) }) + t.throws(function () { impl.Buffer.alloc([0, 42, 3]) }) + t.throws(function () { impl.Buffer.alloc({}) }) + }); + ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { + t.throws(function () { dangerous.Buffer[method]('') }) + t.throws(function () { dangerous.Buffer[method]('string') }) + t.throws(function () { dangerous.Buffer[method]('string', 'utf-8') }) + t.throws(function () { dangerous.Buffer[method](2 * (1 << 30)) }) + t.throws(function () { dangerous.Buffer[method](Infinity) }) + if (dangerous.Buffer[method] === buffer.Buffer.allocUnsafe) { + t.skip('Skipping, older impl of allocUnsafe coerced negative sizes to 0') + } else { + t.throws(function () { dangerous.Buffer[method](-10) }) + t.throws(function () { dangerous.Buffer[method](-1e90) }) + t.throws(function () { dangerous.Buffer[method](-Infinity) }) + } + t.throws(function () { dangerous.Buffer[method](null) }) + t.throws(function () { dangerous.Buffer[method](undefined) }) + t.throws(function () { dangerous.Buffer[method]() }) + t.throws(function () { dangerous.Buffer[method]([]) }) + t.throws(function () { dangerous.Buffer[method]([0, 42, 3]) }) + t.throws(function () { dangerous.Buffer[method]({}) }) + }) + t.end() +}) + +test('Buffers have appropriate lengths', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.equal(impl.Buffer.alloc(0).length, 0) + t.equal(impl.Buffer.alloc(10).length, 10) + t.equal(impl.Buffer.from('').length, 0) + t.equal(impl.Buffer.from('string').length, 6) + t.equal(impl.Buffer.from('string', 'utf-8').length, 6) + t.equal(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64').length, 11) + t.equal(impl.Buffer.from([0, 42, 3]).length, 3) + t.equal(impl.Buffer.from(new Uint8Array([0, 42, 3])).length, 3) + t.equal(impl.Buffer.from([]).length, 0) + }); + ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { + t.equal(dangerous.Buffer[method](0).length, 0) + t.equal(dangerous.Buffer[method](10).length, 10) + }) + t.end() +}) + +test('Buffers have appropriate lengths (2)', function (t) { + t.equal(index.Buffer.alloc, safer.Buffer.alloc) + t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) + var ok = true; + [ safer.Buffer.alloc, + dangerous.Buffer.allocUnsafe, + dangerous.Buffer.allocUnsafeSlow + ].forEach(function (method) { + for (var i = 0; i < 1e2; i++) { + var length = Math.round(Math.random() * 1e5) + var buf = method(length) + if (!buffer.Buffer.isBuffer(buf)) ok = false + if (buf.length !== length) ok = false + } + }) + t.ok(ok) + t.end() +}) + +test('.alloc(size) is zero-filled and has correct length', function (t) { + t.equal(index.Buffer.alloc, safer.Buffer.alloc) + t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) + var ok = true + for (var i = 0; i < 1e2; i++) { + var length = Math.round(Math.random() * 2e6) + var buf = index.Buffer.alloc(length) + if (!buffer.Buffer.isBuffer(buf)) ok = false + if (buf.length !== length) ok = false + var j + for (j = 0; j < length; j++) { + if (buf[j] !== 0) ok = false + } + buf.fill(1) + for (j = 0; j < length; j++) { + if (buf[j] !== 1) ok = false + } + } + t.ok(ok) + t.end() +}) + +test('.allocUnsafe / .allocUnsafeSlow are fillable and have correct lengths', function (t) { + ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { + var ok = true + for (var i = 0; i < 1e2; i++) { + var length = Math.round(Math.random() * 2e6) + var buf = dangerous.Buffer[method](length) + if (!buffer.Buffer.isBuffer(buf)) ok = false + if (buf.length !== length) ok = false + buf.fill(0, 0, length) + var j + for (j = 0; j < length; j++) { + if (buf[j] !== 0) ok = false + } + buf.fill(1, 0, length) + for (j = 0; j < length; j++) { + if (buf[j] !== 1) ok = false + } + } + t.ok(ok, method) + }) + t.end() +}) + +test('.alloc(size, fill) is `fill`-filled', function (t) { + t.equal(index.Buffer.alloc, safer.Buffer.alloc) + t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) + var ok = true + for (var i = 0; i < 1e2; i++) { + var length = Math.round(Math.random() * 2e6) + var fill = Math.round(Math.random() * 255) + var buf = index.Buffer.alloc(length, fill) + if (!buffer.Buffer.isBuffer(buf)) ok = false + if (buf.length !== length) ok = false + for (var j = 0; j < length; j++) { + if (buf[j] !== fill) ok = false + } + } + t.ok(ok) + t.end() +}) + +test('.alloc(size, fill) is `fill`-filled', function (t) { + t.equal(index.Buffer.alloc, safer.Buffer.alloc) + t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) + var ok = true + for (var i = 0; i < 1e2; i++) { + var length = Math.round(Math.random() * 2e6) + var fill = Math.round(Math.random() * 255) + var buf = index.Buffer.alloc(length, fill) + if (!buffer.Buffer.isBuffer(buf)) ok = false + if (buf.length !== length) ok = false + for (var j = 0; j < length; j++) { + if (buf[j] !== fill) ok = false + } + } + t.ok(ok) + t.deepEqual(index.Buffer.alloc(9, 'a'), index.Buffer.alloc(9, 97)) + t.notDeepEqual(index.Buffer.alloc(9, 'a'), index.Buffer.alloc(9, 98)) + + var tmp = new buffer.Buffer(2) + tmp.fill('ok') + if (tmp[1] === tmp[0]) { + // Outdated Node.js + t.deepEqual(index.Buffer.alloc(5, 'ok'), index.Buffer.from('ooooo')) + } else { + t.deepEqual(index.Buffer.alloc(5, 'ok'), index.Buffer.from('okoko')) + } + t.notDeepEqual(index.Buffer.alloc(5, 'ok'), index.Buffer.from('kokok')) + + t.end() +}) + +test('safer.Buffer.from returns results same as Buffer constructor', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.deepEqual(impl.Buffer.from(''), new buffer.Buffer('')) + t.deepEqual(impl.Buffer.from('string'), new buffer.Buffer('string')) + t.deepEqual(impl.Buffer.from('string', 'utf-8'), new buffer.Buffer('string', 'utf-8')) + t.deepEqual(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64'), new buffer.Buffer('b25ldHdvdGhyZWU=', 'base64')) + t.deepEqual(impl.Buffer.from([0, 42, 3]), new buffer.Buffer([0, 42, 3])) + t.deepEqual(impl.Buffer.from(new Uint8Array([0, 42, 3])), new buffer.Buffer(new Uint8Array([0, 42, 3]))) + t.deepEqual(impl.Buffer.from([]), new buffer.Buffer([])) + }) + t.end() +}) + +test('safer.Buffer.from returns consistent results', function (t) { + [index, safer, dangerous].forEach(function (impl) { + t.deepEqual(impl.Buffer.from(''), impl.Buffer.alloc(0)) + t.deepEqual(impl.Buffer.from([]), impl.Buffer.alloc(0)) + t.deepEqual(impl.Buffer.from(new Uint8Array([])), impl.Buffer.alloc(0)) + t.deepEqual(impl.Buffer.from('string', 'utf-8'), impl.Buffer.from('string')) + t.deepEqual(impl.Buffer.from('string'), impl.Buffer.from([115, 116, 114, 105, 110, 103])) + t.deepEqual(impl.Buffer.from('string'), impl.Buffer.from(impl.Buffer.from('string'))) + t.deepEqual(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64'), impl.Buffer.from('onetwothree')) + t.notDeepEqual(impl.Buffer.from('b25ldHdvdGhyZWU='), impl.Buffer.from('onetwothree')) + }) + t.end() +}) diff --git a/social/twitter/node_modules/sax/LICENSE b/social/twitter/node_modules/sax/LICENSE new file mode 100644 index 00000000..ccffa082 --- /dev/null +++ b/social/twitter/node_modules/sax/LICENSE @@ -0,0 +1,41 @@ +The ISC License + +Copyright (c) Isaac Z. Schlueter and Contributors + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +==== + +`String.fromCodePoint` by Mathias Bynens used according to terms of MIT +License, as follows: + + Copyright Mathias Bynens + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/social/twitter/node_modules/sax/README.md b/social/twitter/node_modules/sax/README.md new file mode 100644 index 00000000..afcd3f3d --- /dev/null +++ b/social/twitter/node_modules/sax/README.md @@ -0,0 +1,225 @@ +# sax js + +A sax-style parser for XML and HTML. + +Designed with [node](http://nodejs.org/) in mind, but should work fine in +the browser or other CommonJS implementations. + +## What This Is + +* A very simple tool to parse through an XML string. +* A stepping stone to a streaming HTML parser. +* A handy way to deal with RSS and other mostly-ok-but-kinda-broken XML + docs. + +## What This Is (probably) Not + +* An HTML Parser - That's a fine goal, but this isn't it. It's just + XML. +* A DOM Builder - You can use it to build an object model out of XML, + but it doesn't do that out of the box. +* XSLT - No DOM = no querying. +* 100% Compliant with (some other SAX implementation) - Most SAX + implementations are in Java and do a lot more than this does. +* An XML Validator - It does a little validation when in strict mode, but + not much. +* A Schema-Aware XSD Thing - Schemas are an exercise in fetishistic + masochism. +* A DTD-aware Thing - Fetching DTDs is a much bigger job. + +## Regarding `Hello, world!').close(); + +// stream usage +// takes the same options as the parser +var saxStream = require("sax").createStream(strict, options) +saxStream.on("error", function (e) { + // unhandled errors will throw, since this is a proper node + // event emitter. + console.error("error!", e) + // clear the error + this._parser.error = null + this._parser.resume() +}) +saxStream.on("opentag", function (node) { + // same object as above +}) +// pipe is supported, and it's readable/writable +// same chunks coming in also go out. +fs.createReadStream("file.xml") + .pipe(saxStream) + .pipe(fs.createWriteStream("file-copy.xml")) +``` + + +## Arguments + +Pass the following arguments to the parser function. All are optional. + +`strict` - Boolean. Whether or not to be a jerk. Default: `false`. + +`opt` - Object bag of settings regarding string formatting. All default to `false`. + +Settings supported: + +* `trim` - Boolean. Whether or not to trim text and comment nodes. +* `normalize` - Boolean. If true, then turn any whitespace into a single + space. +* `lowercase` - Boolean. If true, then lowercase tag names and attribute names + in loose mode, rather than uppercasing them. +* `xmlns` - Boolean. If true, then namespaces are supported. +* `position` - Boolean. If false, then don't track line/col/position. +* `strictEntities` - Boolean. If true, only parse [predefined XML + entities](http://www.w3.org/TR/REC-xml/#sec-predefined-ent) + (`&`, `'`, `>`, `<`, and `"`) + +## Methods + +`write` - Write bytes onto the stream. You don't have to do this all at +once. You can keep writing as much as you want. + +`close` - Close the stream. Once closed, no more data may be written until +it is done processing the buffer, which is signaled by the `end` event. + +`resume` - To gracefully handle errors, assign a listener to the `error` +event. Then, when the error is taken care of, you can call `resume` to +continue parsing. Otherwise, the parser will not continue while in an error +state. + +## Members + +At all times, the parser object will have the following members: + +`line`, `column`, `position` - Indications of the position in the XML +document where the parser currently is looking. + +`startTagPosition` - Indicates the position where the current tag starts. + +`closed` - Boolean indicating whether or not the parser can be written to. +If it's `true`, then wait for the `ready` event to write again. + +`strict` - Boolean indicating whether or not the parser is a jerk. + +`opt` - Any options passed into the constructor. + +`tag` - The current tag being dealt with. + +And a bunch of other stuff that you probably shouldn't touch. + +## Events + +All events emit with a single argument. To listen to an event, assign a +function to `on`. Functions get executed in the this-context of +the parser object. The list of supported events are also in the exported +`EVENTS` array. + +When using the stream interface, assign handlers using the EventEmitter +`on` function in the normal fashion. + +`error` - Indication that something bad happened. The error will be hanging +out on `parser.error`, and must be deleted before parsing can continue. By +listening to this event, you can keep an eye on that kind of stuff. Note: +this happens *much* more in strict mode. Argument: instance of `Error`. + +`text` - Text node. Argument: string of text. + +`doctype` - The ``. Argument: +object with `name` and `body` members. Attributes are not parsed, as +processing instructions have implementation dependent semantics. + +`sgmldeclaration` - Random SGML declarations. Stuff like `` +would trigger this kind of event. This is a weird thing to support, so it +might go away at some point. SAX isn't intended to be used to parse SGML, +after all. + +`opentagstart` - Emitted immediately when the tag name is available, +but before any attributes are encountered. Argument: object with a +`name` field and an empty `attributes` set. Note that this is the +same object that will later be emitted in the `opentag` event. + +`opentag` - An opening tag. Argument: object with `name` and `attributes`. +In non-strict mode, tag names are uppercased, unless the `lowercase` +option is set. If the `xmlns` option is set, then it will contain +namespace binding information on the `ns` member, and will have a +`local`, `prefix`, and `uri` member. + +`closetag` - A closing tag. In loose mode, tags are auto-closed if their +parent closes. In strict mode, well-formedness is enforced. Note that +self-closing tags will have `closeTag` emitted immediately after `openTag`. +Argument: tag name. + +`attribute` - An attribute node. Argument: object with `name` and `value`. +In non-strict mode, attribute names are uppercased, unless the `lowercase` +option is set. If the `xmlns` option is set, it will also contains namespace +information. + +`comment` - A comment node. Argument: the string of the comment. + +`opencdata` - The opening tag of a ``) of a `` tags trigger a `"script"` +event, and their contents are not checked for special xml characters. +If you pass `noscript: true`, then this behavior is suppressed. + +## Reporting Problems + +It's best to write a failing test if you find an issue. I will always +accept pull requests with failing tests if they demonstrate intended +behavior, but it is very hard to figure out what issue you're describing +without a test. Writing a test is also the best way for you yourself +to figure out if you really understand the issue you think you have with +sax-js. diff --git a/social/twitter/node_modules/sax/lib/sax.js b/social/twitter/node_modules/sax/lib/sax.js new file mode 100644 index 00000000..795d607e --- /dev/null +++ b/social/twitter/node_modules/sax/lib/sax.js @@ -0,0 +1,1565 @@ +;(function (sax) { // wrapper for non-node envs + sax.parser = function (strict, opt) { return new SAXParser(strict, opt) } + sax.SAXParser = SAXParser + sax.SAXStream = SAXStream + sax.createStream = createStream + + // When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns. + // When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)), + // since that's the earliest that a buffer overrun could occur. This way, checks are + // as rare as required, but as often as necessary to ensure never crossing this bound. + // Furthermore, buffers are only tested at most once per write(), so passing a very + // large string into write() might have undesirable effects, but this is manageable by + // the caller, so it is assumed to be safe. Thus, a call to write() may, in the extreme + // edge case, result in creating at most one complete copy of the string passed in. + // Set to Infinity to have unlimited buffers. + sax.MAX_BUFFER_LENGTH = 64 * 1024 + + var buffers = [ + 'comment', 'sgmlDecl', 'textNode', 'tagName', 'doctype', + 'procInstName', 'procInstBody', 'entity', 'attribName', + 'attribValue', 'cdata', 'script' + ] + + sax.EVENTS = [ + 'text', + 'processinginstruction', + 'sgmldeclaration', + 'doctype', + 'comment', + 'opentagstart', + 'attribute', + 'opentag', + 'closetag', + 'opencdata', + 'cdata', + 'closecdata', + 'error', + 'end', + 'ready', + 'script', + 'opennamespace', + 'closenamespace' + ] + + function SAXParser (strict, opt) { + if (!(this instanceof SAXParser)) { + return new SAXParser(strict, opt) + } + + var parser = this + clearBuffers(parser) + parser.q = parser.c = '' + parser.bufferCheckPosition = sax.MAX_BUFFER_LENGTH + parser.opt = opt || {} + parser.opt.lowercase = parser.opt.lowercase || parser.opt.lowercasetags + parser.looseCase = parser.opt.lowercase ? 'toLowerCase' : 'toUpperCase' + parser.tags = [] + parser.closed = parser.closedRoot = parser.sawRoot = false + parser.tag = parser.error = null + parser.strict = !!strict + parser.noscript = !!(strict || parser.opt.noscript) + parser.state = S.BEGIN + parser.strictEntities = parser.opt.strictEntities + parser.ENTITIES = parser.strictEntities ? Object.create(sax.XML_ENTITIES) : Object.create(sax.ENTITIES) + parser.attribList = [] + + // namespaces form a prototype chain. + // it always points at the current tag, + // which protos to its parent tag. + if (parser.opt.xmlns) { + parser.ns = Object.create(rootNS) + } + + // mostly just for error reporting + parser.trackPosition = parser.opt.position !== false + if (parser.trackPosition) { + parser.position = parser.line = parser.column = 0 + } + emit(parser, 'onready') + } + + if (!Object.create) { + Object.create = function (o) { + function F () {} + F.prototype = o + var newf = new F() + return newf + } + } + + if (!Object.keys) { + Object.keys = function (o) { + var a = [] + for (var i in o) if (o.hasOwnProperty(i)) a.push(i) + return a + } + } + + function checkBufferLength (parser) { + var maxAllowed = Math.max(sax.MAX_BUFFER_LENGTH, 10) + var maxActual = 0 + for (var i = 0, l = buffers.length; i < l; i++) { + var len = parser[buffers[i]].length + if (len > maxAllowed) { + // Text/cdata nodes can get big, and since they're buffered, + // we can get here under normal conditions. + // Avoid issues by emitting the text node now, + // so at least it won't get any bigger. + switch (buffers[i]) { + case 'textNode': + closeText(parser) + break + + case 'cdata': + emitNode(parser, 'oncdata', parser.cdata) + parser.cdata = '' + break + + case 'script': + emitNode(parser, 'onscript', parser.script) + parser.script = '' + break + + default: + error(parser, 'Max buffer length exceeded: ' + buffers[i]) + } + } + maxActual = Math.max(maxActual, len) + } + // schedule the next check for the earliest possible buffer overrun. + var m = sax.MAX_BUFFER_LENGTH - maxActual + parser.bufferCheckPosition = m + parser.position + } + + function clearBuffers (parser) { + for (var i = 0, l = buffers.length; i < l; i++) { + parser[buffers[i]] = '' + } + } + + function flushBuffers (parser) { + closeText(parser) + if (parser.cdata !== '') { + emitNode(parser, 'oncdata', parser.cdata) + parser.cdata = '' + } + if (parser.script !== '') { + emitNode(parser, 'onscript', parser.script) + parser.script = '' + } + } + + SAXParser.prototype = { + end: function () { end(this) }, + write: write, + resume: function () { this.error = null; return this }, + close: function () { return this.write(null) }, + flush: function () { flushBuffers(this) } + } + + var Stream + try { + Stream = require('stream').Stream + } catch (ex) { + Stream = function () {} + } + + var streamWraps = sax.EVENTS.filter(function (ev) { + return ev !== 'error' && ev !== 'end' + }) + + function createStream (strict, opt) { + return new SAXStream(strict, opt) + } + + function SAXStream (strict, opt) { + if (!(this instanceof SAXStream)) { + return new SAXStream(strict, opt) + } + + Stream.apply(this) + + this._parser = new SAXParser(strict, opt) + this.writable = true + this.readable = true + + var me = this + + this._parser.onend = function () { + me.emit('end') + } + + this._parser.onerror = function (er) { + me.emit('error', er) + + // if didn't throw, then means error was handled. + // go ahead and clear error, so we can write again. + me._parser.error = null + } + + this._decoder = null + + streamWraps.forEach(function (ev) { + Object.defineProperty(me, 'on' + ev, { + get: function () { + return me._parser['on' + ev] + }, + set: function (h) { + if (!h) { + me.removeAllListeners(ev) + me._parser['on' + ev] = h + return h + } + me.on(ev, h) + }, + enumerable: true, + configurable: false + }) + }) + } + + SAXStream.prototype = Object.create(Stream.prototype, { + constructor: { + value: SAXStream + } + }) + + SAXStream.prototype.write = function (data) { + if (typeof Buffer === 'function' && + typeof Buffer.isBuffer === 'function' && + Buffer.isBuffer(data)) { + if (!this._decoder) { + var SD = require('string_decoder').StringDecoder + this._decoder = new SD('utf8') + } + data = this._decoder.write(data) + } + + this._parser.write(data.toString()) + this.emit('data', data) + return true + } + + SAXStream.prototype.end = function (chunk) { + if (chunk && chunk.length) { + this.write(chunk) + } + this._parser.end() + return true + } + + SAXStream.prototype.on = function (ev, handler) { + var me = this + if (!me._parser['on' + ev] && streamWraps.indexOf(ev) !== -1) { + me._parser['on' + ev] = function () { + var args = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments) + args.splice(0, 0, ev) + me.emit.apply(me, args) + } + } + + return Stream.prototype.on.call(me, ev, handler) + } + + // this really needs to be replaced with character classes. + // XML allows all manner of ridiculous numbers and digits. + var CDATA = '[CDATA[' + var DOCTYPE = 'DOCTYPE' + var XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace' + var XMLNS_NAMESPACE = 'http://www.w3.org/2000/xmlns/' + var rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE } + + // http://www.w3.org/TR/REC-xml/#NT-NameStartChar + // This implementation works on strings, a single character at a time + // as such, it cannot ever support astral-plane characters (10000-EFFFF) + // without a significant breaking change to either this parser, or the + // JavaScript language. Implementation of an emoji-capable xml parser + // is left as an exercise for the reader. + var nameStart = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/ + + var nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/ + + var entityStart = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/ + var entityBody = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/ + + function isWhitespace (c) { + return c === ' ' || c === '\n' || c === '\r' || c === '\t' + } + + function isQuote (c) { + return c === '"' || c === '\'' + } + + function isAttribEnd (c) { + return c === '>' || isWhitespace(c) + } + + function isMatch (regex, c) { + return regex.test(c) + } + + function notMatch (regex, c) { + return !isMatch(regex, c) + } + + var S = 0 + sax.STATE = { + BEGIN: S++, // leading byte order mark or whitespace + BEGIN_WHITESPACE: S++, // leading whitespace + TEXT: S++, // general stuff + TEXT_ENTITY: S++, // & and such. + OPEN_WAKA: S++, // < + SGML_DECL: S++, // + SCRIPT: S++, // - - - - - - - - - - -TomcatUTF-8/EUCȤ - - - - - - -
The Wayback Machine - https://web.archive.org/web/20181003202907/http://www.nina.jp:80/server/slackware/webapp/tomcat_charset.html
- - -

TomcatUTF-8/EUC-JPȤ

-

-[Фμ¸ Slackware] -

-

- : 2004/12/31 -

-
-

-"Фμ¸"θ - - - -

-
-
- -

-Tomcat֤륭饯åȤξϡhttpd.confΥ롼ȤǻꤷAddDefaultCharsetͤƱˤʤ餷 -Directoryǥ쥯ƥ֤ǻꤷAddDefaultCharset̵뤵äݤ -ĤǤˡmeta̵뤵ߤ -<---Τؤ󡢸ҤSetCharacterEncodingFilterưʤȤ⤢ꡢʤ... -

- -

-WEBФϥ롼ȤAddDefaultCharsetEUC-JP򤷤ƤꡢƥȥѥʲUTF-8ˤΤǡʤ餫к򤷤ʤʸƤޤ -

- -

֥åȤξ

-

-response.setContentTypeǥ饯åȤꤹ롣 -EUC-JPѤʤ顢response.setContentType("text/html; charset=EUC-JP") -UTF-8Ѥʤ顢response.setContentType("text/html; charset=UTF-8") -

-
-# HelloWorld.java
-
-import java.io.*;
-import java.text.*;
-import java.util.*;
-import javax.servlet.*;
-import javax.servlet.http.*;
-
-public class HelloWorld extends HttpServlet {
-
-    public void doGet(HttpServletRequest request,
-                      HttpServletResponse response)
-        throws IOException, ServletException
-    {
-        response.setContentType("text/html; charset=EUC-JP");
-        PrintWriter out = response.getWriter();
-
-        out.println("<html>");
-        out.println("<head>");
-        out.println("<title>HelloWorld</title>");
-        out.println("</head>");
-        out.println("<body>");
-        out.println("<p>");
-        out.println("ˤ");
-        out.println("</p>");
-        out.println("</body>");
-        out.println("</html>");
-    }
-}
-
-

-JAVAUTF-8ǽԤΤǡʳEUC-JPʤɤѤϡѥ뤹Ȥ-encodingĤ뤳ȡ -

-
-# javac -encoding EUC-JP -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar HelloWorld.java
-
- -

JSPξ

-

-ǥ쥯ƥ֤ǥ饯åȤꤹ롣 -EUC-JPѤʤ顢<%@ page contentType="text/html; charset=EUC-JP" %> -UTF-8Ѥʤ顢<%@ page contentType="text/html; charset=UTF-8" %> -

-
-# hello.jsp
-
-<%@ page contentType="text/html; charset=EUC-JP" %>
-<html>
-<head>
-<title>HelloWorld</title>
-</head>
-<body>
-<p>
-<%
-    out.println("ˤ");
-%>
-</p>
-</body>
-</html>
-
- -

ŪƥġHTMLˤξ

-

-workers2.propetiesեǡƥȥѥʲΤ٤ƤΥեTomcatϤ褦ꤷƤ硢ŪƥĤˤĤƤ⤳ΥڡƬ˽񤤤褦ʥ饯åȾ֤롣 -HTMLmetacharsetꤷƤ̵뤵Τǡɥȥ롼Ȥȥƥȥѥǰۤʤ륭饯åȤѤȤդɬס -

-
-# workers2.properties
-
-[uri:/hoge/*]    <---٤ƤΥեTomcat˽
-
-

SetCharacterEncodingFilterѤΤʤΤ褦ɤäƤcharset֤Ƥʤ -ʤΤǡweb.xml<mime-mapping>charsetȳĥҤδϢդꤷ -

-
-<!--ʥƥȥѥ/WEB-INF/web.xml-->
-
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<!DOCTYPE web-app
-     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-    "http://java.sun.com/dtd/web-app_2_3.dtd">
-<web-app>
-  <mime-mapping>
-    <extension>html</extension>
-    <mime-type>text/html; charset=UTF-8</mime-type>
-  </mime-mapping>
-</web-app>
-
-

-SetCharacterEncodingFilterѤˡ񤤤Ƥȡ$CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes/filtersǥ쥯ȥˤSetCharacterEncodingFilter.java򥳥ѥ뤷ơ -

-
-# cd $CATALINA_HOME/webapps/jsp-examples/WEB-INF/classes
-# javac -classpath .:$CATALINA_HOME/common/lib/servlet-api.jar filters.SetCharacterEncodingFilter.java
-
-

-줿饹եʥƥȥѥ/WEB-INF/classes/filtersǥ쥯ȥ˥ԡơʥƥȥѥ/WEB-INF/web.xml˥ե륿򵭽Ҥ餷 -

-
-<!--ʥƥȥѥ/WEB-INF/web.xml-->
-
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<!DOCTYPE web-app
-     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-    "http://java.sun.com/dtd/web-app_2_3.dtd">
-<web-app>
-  <filter>
-      <filter-name>Set Character Encoding</filter-name>
-      <filter-class>filters.SetCharacterEncodingFilter</filter-class>
-      <init-param>
-          <param-name>encoding</param-name>
-          <param-value>UTF-8</param-value>
-      </init-param>
-  </filter>
-  <filter-mapping>
-      <filter-name>Set Character Encoding</filter-name>
-      <url-pattern>/*</url-pattern>
-  </filter-mapping>
-</web-app>
-
-

-... -

- -
-

-[Фμ¸ Slackware] -

- - - - - \ No newline at end of file diff --git a/social/twitter/node_modules/needle/test/uri_modifier_spec.js b/social/twitter/node_modules/needle/test/uri_modifier_spec.js deleted file mode 100644 index 1a12a36c..00000000 --- a/social/twitter/node_modules/needle/test/uri_modifier_spec.js +++ /dev/null @@ -1,46 +0,0 @@ -var needle = require('../'), - sinon = require('sinon'), - should = require('should'), - http = require('http'), - helpers = require('./helpers'); - -var port = 3456; - -describe('uri_modifier config parameter function', function() { - - var server, uri; - - function send_request(mw, cb) { - needle.get(uri, { uri_modifier: mw }, cb); - } - - before(function(done){ - server = helpers.server({ port: port }, done); - }) - - after(function(done) { - server.close(done); - }) - - describe('modifies uri', function() { - - var path = '/foo/replace'; - - before(function() { - uri = 'localhost:' + port + path - }); - - it('should modify path', function(done) { - send_request(function(uri) { - return uri.replace('/replace', ''); - }, function(err, res) { - should.not.exist(err); - should(res.req.path).be.exactly('/foo'); - done(); - }); - - }); - - }) - -}) diff --git a/social/twitter/node_modules/needle/test/url_spec.js b/social/twitter/node_modules/needle/test/url_spec.js deleted file mode 100644 index 5154d584..00000000 --- a/social/twitter/node_modules/needle/test/url_spec.js +++ /dev/null @@ -1,155 +0,0 @@ -var needle = require('../'), - sinon = require('sinon'), - should = require('should'), - http = require('http'), - helpers = require('./helpers'); - -var port = 3456; - -describe('urls', function() { - - var server, url; - - function send_request(cb) { - return needle.get(url, cb); - } - - before(function(done){ - server = helpers.server({ port: port }, done); - }) - - after(function(done) { - server.close(done); - }) - - describe('null URL', function(){ - - it('throws', function(){ - (function() { - send_request() - }).should.throw(); - }) - - }) - - describe('invalid protocol', function(){ - - before(function() { - url = 'foo://google.com/what' - }) - - it('does not throw', function(done) { - (function() { - send_request(function(err) { - done(); - }) - }).should.not.throw() - }) - - it('returns an error', function(done) { - send_request(function(err) { - err.should.be.an.Error; - err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/) - done(); - }) - }) - - }) - - describe('invalid host', function(){ - - before(function() { - url = 'http://s1\\\u0002.com/' - }) - - it('fails', function(done) { - (function() { - send_request(function(){ }) - }.should.throw(TypeError)) - done() - }) - - }) - -/* - describe('invalid path', function(){ - - before(function() { - url = 'http://www.google.com\\\/x\\\ %^&*() /x2.com/' - }) - - it('fails', function(done) { - send_request(function(err) { - err.should.be.an.Error; - done(); - }) - }) - - }) -*/ - - describe('valid protocol and path', function() { - - before(function() { - url = 'http://localhost:' + port + '/foo'; - }) - - it('works', function(done) { - send_request(function(err){ - should.not.exist(err); - done(); - }) - }) - - }) - - describe('no protocol but with slashes and valid path', function() { - - before(function() { - url = '//localhost:' + port + '/foo'; - }) - - it('works', function(done) { - send_request(function(err){ - should.not.exist(err); - done(); - }) - }) - - }) - - describe('no protocol nor slashes and valid path', function() { - - before(function() { - url = 'localhost:' + port + '/foo'; - }) - - it('works', function(done) { - send_request(function(err){ - should.not.exist(err); - done(); - }) - }) - - }) - - describe('double encoding', function() { - - var path = '/foo?email=' + encodeURIComponent('what-ever@Example.Com'); - - before(function() { - url = 'localhost:' + port + path - }); - - it('should not occur', function(done) { - send_request(function(err, res) { - should.not.exist(err); - should(res.req.path).be.exactly(path); - done(); - }); - - }); - - }) - -}) diff --git a/social/twitter/node_modules/needle/test/utils/formidable.js b/social/twitter/node_modules/needle/test/utils/formidable.js deleted file mode 100644 index ba1d983e..00000000 --- a/social/twitter/node_modules/needle/test/utils/formidable.js +++ /dev/null @@ -1,17 +0,0 @@ -var formidable = require('formidable'), - http = require('http'), - util = require('util'); - -var port = process.argv[2] || 8888; - -http.createServer(function(req, res) { - var form = new formidable.IncomingForm(); - form.parse(req, function(err, fields, files) { - res.writeHead(200, {'content-type': 'text/plain'}); - res.write('received upload:\n\n'); - console.log(util.inspect({fields: fields, files: files})) - res.end(util.inspect({fields: fields, files: files})); - }); -}).listen(port); - -console.log('HTTP server listening on port ' + port); \ No newline at end of file diff --git a/social/twitter/node_modules/needle/test/utils/proxy.js b/social/twitter/node_modules/needle/test/utils/proxy.js deleted file mode 100644 index 531bf493..00000000 --- a/social/twitter/node_modules/needle/test/utils/proxy.js +++ /dev/null @@ -1,62 +0,0 @@ -var http = require('http'), - https = require('https'), - url = require('url'); - -var port = 1234, - log = true, - request_auth = false; - -http.createServer(function(request, response) { - - console.log(request.headers); - console.log("Got request: " + request.url); - console.log("Forwarding request to " + request.headers['host']); - - if (request_auth) { - if (!request.headers['proxy-authorization']) { - response.writeHead(407, {'Proxy-Authenticate': 'Basic realm="proxy.com"'}) - return response.end('Hello.'); - } - } - - var remote = url.parse(request.url); - var protocol = remote.protocol == 'https:' ? https : http; - - var opts = { - host: request.headers['host'], - port: remote.port || (remote.protocol == 'https:' ? 443 : 80), - method: request.method, - path: remote.pathname, - headers: request.headers - } - - var proxy_request = protocol.request(opts, function(proxy_response){ - - proxy_response.on('data', function(chunk) { - if (log) console.log(chunk.toString()); - response.write(chunk, 'binary'); - }); - proxy_response.on('end', function() { - response.end(); - }); - - response.writeHead(proxy_response.statusCode, proxy_response.headers); - }); - - request.on('data', function(chunk) { - if (log) console.log(chunk.toString()); - proxy_request.write(chunk, 'binary'); - }); - - request.on('end', function() { - proxy_request.end(); - }); - -}).listen(port); - -process.on('uncaughtException', function(err){ - console.log('Uncaught exception!'); - console.log(err); -}); - -console.log("Proxy server listening on port " + port); diff --git a/social/twitter/node_modules/needle/test/utils/test.js b/social/twitter/node_modules/needle/test/utils/test.js deleted file mode 100644 index 8d58d70f..00000000 --- a/social/twitter/node_modules/needle/test/utils/test.js +++ /dev/null @@ -1,104 +0,0 @@ -// TODO: write specs. :) - -var fs = require('fs'), - client = require('./../../'); - -process.env.DEBUG = true; - -var response_callback = function(err, resp, body){ - console.log(err); - if(resp) console.log("Got status code " + resp.statusCode) - console.log(body); -} - -function simple_head(){ - client.head('http://www.amazon.com', response_callback); -} - -function simple_get(){ - client.get('http://www.nodejs.org', response_callback); -} - -function proxy_get(){ - client.get('https://www.google.com/search?q=nodejs', {proxy: 'http://localhost:1234'}, response_callback); -} - -function auth_get(){ - client.get('https://www.twitter.com', {username: 'asd', password: '123'}, response_callback); -} - -function simple_post(url){ - - var data = { - foo: 'bar', - baz: { - nested: 'attribute' - } - } - - client.post(url, data, response_callback); - -} - -function multipart_post(url){ - - var filename = 'test_file.txt'; - var data = 'Plain text data.\nLorem ipsum dolor sit amet.\nBla bla bla.\n'; - fs.writeFileSync(filename, data); - - var black_pixel = Buffer.from("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64"); - - var data = { - foo: 'bar', - bar: 'baz', - nested: { - my_document: { file: filename, content_type: 'text/plain' }, - even: { - more: 'nesting' - } - }, - pixel: { filename: 'black_pixel.gif', buffer: black_pixel, content_type: 'image/gif' }, - field2: {value: JSON.stringify({"json":[ {"one":1}, {"two":2} ]}), content_type: 'application/json' } - } - - client.post(url, data, {multipart: true}, function(err, resp, body){ - - console.log(err); - console.log("Got status code " + resp.statusCode) - console.log(body); - fs.unlink(filename); - - }); - -} - -switch(process.argv[2]){ - case 'head': - simple_head(); - break; - case 'get': - simple_get(); - break; - case 'auth': - auth_get(); - break; - case 'proxy': - proxy_get(); - break; - case 'post': - simple_post(process.argv[3] || 'http://posttestserver.com/post.php'); - break; - case 'multipart': - multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example'); - break; - case 'all': - simple_head(); - simple_get(); - auth_get(); - proxy_get(); - simple_post(process.argv[3] || 'http://posttestserver.com/post.php'); - multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example'); - break; - default: - console.log("Usage: ./test.js [head|get|auth|proxy|multipart]") -} diff --git a/social/twitter/node_modules/safer-buffer/LICENSE b/social/twitter/node_modules/safer-buffer/LICENSE deleted file mode 100644 index 4fe9e6f1..00000000 --- a/social/twitter/node_modules/safer-buffer/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 Nikita Skovoroda - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/social/twitter/node_modules/safer-buffer/Porting-Buffer.md b/social/twitter/node_modules/safer-buffer/Porting-Buffer.md deleted file mode 100644 index 68d86bab..00000000 --- a/social/twitter/node_modules/safer-buffer/Porting-Buffer.md +++ /dev/null @@ -1,268 +0,0 @@ -# Porting to the Buffer.from/Buffer.alloc API - - -## Overview - -- [Variant 1: Drop support for Node.js ≤ 4.4.x and 5.0.0 — 5.9.x.](#variant-1) (*recommended*) -- [Variant 2: Use a polyfill](#variant-2) -- [Variant 3: manual detection, with safeguards](#variant-3) - -### Finding problematic bits of code using grep - -Just run `grep -nrE '[^a-zA-Z](Slow)?Buffer\s*\(' --exclude-dir node_modules`. - -It will find all the potentially unsafe places in your own code (with some considerably unlikely -exceptions). - -### Finding problematic bits of code using Node.js 8 - -If you’re using Node.js ≥ 8.0.0 (which is recommended), Node.js exposes multiple options that help with finding the relevant pieces of code: - -- `--trace-warnings` will make Node.js show a stack trace for this warning and other warnings that are printed by Node.js. -- `--trace-deprecation` does the same thing, but only for deprecation warnings. -- `--pending-deprecation` will show more types of deprecation warnings. In particular, it will show the `Buffer()` deprecation warning, even on Node.js 8. - -You can set these flags using an environment variable: - -```console -$ export NODE_OPTIONS='--trace-warnings --pending-deprecation' -$ cat example.js -'use strict'; -const foo = new Buffer('foo'); -$ node example.js -(node:7147) [DEP0005] DeprecationWarning: The Buffer() and new Buffer() constructors are not recommended for use due to security and usability concerns. Please use the new Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() construction methods instead. - at showFlaggedDeprecation (buffer.js:127:13) - at new Buffer (buffer.js:148:3) - at Object. (/path/to/example.js:2:13) - [... more stack trace lines ...] -``` - -### Finding problematic bits of code using linters - -Eslint rules [no-buffer-constructor](https://eslint.org/docs/rules/no-buffer-constructor) -or -[node/no-deprecated-api](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md) -also find calls to deprecated `Buffer()` API. Those rules are included in some pre-sets. - -There is a drawback, though, that it doesn't always -[work correctly](https://github.com/chalker/safer-buffer#why-not-safe-buffer) when `Buffer` is -overriden e.g. with a polyfill, so recommended is a combination of this and some other method -described above. - - -## Variant 1: Drop support for Node.js ≤ 4.4.x and 5.0.0 — 5.9.x. - -This is the recommended solution nowadays that would imply only minimal overhead. - -The Node.js 5.x release line has been unsupported since July 2016, and the Node.js 4.x release line reaches its End of Life in April 2018 (→ [Schedule](https://github.com/nodejs/Release#release-schedule)). This means that these versions of Node.js will *not* receive any updates, even in case of security issues, so using these release lines should be avoided, if at all possible. - -What you would do in this case is to convert all `new Buffer()` or `Buffer()` calls to use `Buffer.alloc()` or `Buffer.from()`, in the following way: - -- For `new Buffer(number)`, replace it with `Buffer.alloc(number)`. -- For `new Buffer(string)` (or `new Buffer(string, encoding)`), replace it with `Buffer.from(string)` (or `Buffer.from(string, encoding)`). -- For all other combinations of arguments (these are much rarer), also replace `new Buffer(...arguments)` with `Buffer.from(...arguments)`. - -Note that `Buffer.alloc()` is also _faster_ on the current Node.js versions than -`new Buffer(size).fill(0)`, which is what you would otherwise need to ensure zero-filling. - -Enabling eslint rule [no-buffer-constructor](https://eslint.org/docs/rules/no-buffer-constructor) -or -[node/no-deprecated-api](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md) -is recommended to avoid accidential unsafe Buffer API usage. - -There is also a [JSCodeshift codemod](https://github.com/joyeecheung/node-dep-codemod#dep005) -for automatically migrating Buffer constructors to `Buffer.alloc()` or `Buffer.from()`. -Note that it currently only works with cases where the arguments are literals or where the -constructor is invoked with two arguments. - -_If you currently support those older Node.js versions and dropping them would be a semver-major change -for you, or if you support older branches of your packages, consider using [Variant 2](#variant-2) -or [Variant 3](#variant-3) on older branches, so people using those older branches will also receive -the fix. That way, you will eradicate potential issues caused by unguarded Buffer API usage and -your users will not observe a runtime deprecation warning when running your code on Node.js 10._ - - -## Variant 2: Use a polyfill - -Utilize [safer-buffer](https://www.npmjs.com/package/safer-buffer) as a polyfill to support older -Node.js versions. - -You would take exacly the same steps as in [Variant 1](#variant-1), but with a polyfill -`const Buffer = require('safer-buffer').Buffer` in all files where you use the new `Buffer` api. - -Make sure that you do not use old `new Buffer` API — in any files where the line above is added, -using old `new Buffer()` API will _throw_. It will be easy to notice that in CI, though. - -Alternatively, you could use [buffer-from](https://www.npmjs.com/package/buffer-from) and/or -[buffer-alloc](https://www.npmjs.com/package/buffer-alloc) [ponyfills](https://ponyfill.com/) — -those are great, the only downsides being 4 deps in the tree and slightly more code changes to -migrate off them (as you would be using e.g. `Buffer.from` under a different name). If you need only -`Buffer.from` polyfilled — `buffer-from` alone which comes with no extra dependencies. - -_Alternatively, you could use [safe-buffer](https://www.npmjs.com/package/safe-buffer) — it also -provides a polyfill, but takes a different approach which has -[it's drawbacks](https://github.com/chalker/safer-buffer#why-not-safe-buffer). It will allow you -to also use the older `new Buffer()` API in your code, though — but that's arguably a benefit, as -it is problematic, can cause issues in your code, and will start emitting runtime deprecation -warnings starting with Node.js 10._ - -Note that in either case, it is important that you also remove all calls to the old Buffer -API manually — just throwing in `safe-buffer` doesn't fix the problem by itself, it just provides -a polyfill for the new API. I have seen people doing that mistake. - -Enabling eslint rule [no-buffer-constructor](https://eslint.org/docs/rules/no-buffer-constructor) -or -[node/no-deprecated-api](https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md) -is recommended. - -_Don't forget to drop the polyfill usage once you drop support for Node.js < 4.5.0._ - - -## Variant 3 — manual detection, with safeguards - -This is useful if you create Buffer instances in only a few places (e.g. one), or you have your own -wrapper around them. - -### Buffer(0) - -This special case for creating empty buffers can be safely replaced with `Buffer.concat([])`, which -returns the same result all the way down to Node.js 0.8.x. - -### Buffer(notNumber) - -Before: - -```js -var buf = new Buffer(notNumber, encoding); -``` - -After: - -```js -var buf; -if (Buffer.from && Buffer.from !== Uint8Array.from) { - buf = Buffer.from(notNumber, encoding); -} else { - if (typeof notNumber === 'number') - throw new Error('The "size" argument must be of type number.'); - buf = new Buffer(notNumber, encoding); -} -``` - -`encoding` is optional. - -Note that the `typeof notNumber` before `new Buffer` is required (for cases when `notNumber` argument is not -hard-coded) and _is not caused by the deprecation of Buffer constructor_ — it's exactly _why_ the -Buffer constructor is deprecated. Ecosystem packages lacking this type-check caused numereous -security issues — situations when unsanitized user input could end up in the `Buffer(arg)` create -problems ranging from DoS to leaking sensitive information to the attacker from the process memory. - -When `notNumber` argument is hardcoded (e.g. literal `"abc"` or `[0,1,2]`), the `typeof` check can -be omitted. - -Also note that using TypeScript does not fix this problem for you — when libs written in -`TypeScript` are used from JS, or when user input ends up there — it behaves exactly as pure JS, as -all type checks are translation-time only and are not present in the actual JS code which TS -compiles to. - -### Buffer(number) - -For Node.js 0.10.x (and below) support: - -```js -var buf; -if (Buffer.alloc) { - buf = Buffer.alloc(number); -} else { - buf = new Buffer(number); - buf.fill(0); -} -``` - -Otherwise (Node.js ≥ 0.12.x): - -```js -const buf = Buffer.alloc ? Buffer.alloc(number) : new Buffer(number).fill(0); -``` - -## Regarding Buffer.allocUnsafe - -Be extra cautious when using `Buffer.allocUnsafe`: - * Don't use it if you don't have a good reason to - * e.g. you probably won't ever see a performance difference for small buffers, in fact, those - might be even faster with `Buffer.alloc()`, - * if your code is not in the hot code path — you also probably won't notice a difference, - * keep in mind that zero-filling minimizes the potential risks. - * If you use it, make sure that you never return the buffer in a partially-filled state, - * if you are writing to it sequentially — always truncate it to the actuall written length - -Errors in handling buffers allocated with `Buffer.allocUnsafe` could result in various issues, -ranged from undefined behaviour of your code to sensitive data (user input, passwords, certs) -leaking to the remote attacker. - -_Note that the same applies to `new Buffer` usage without zero-filling, depending on the Node.js -version (and lacking type checks also adds DoS to the list of potential problems)._ - - -## FAQ - - -### What is wrong with the `Buffer` constructor? - -The `Buffer` constructor could be used to create a buffer in many different ways: - -- `new Buffer(42)` creates a `Buffer` of 42 bytes. Before Node.js 8, this buffer contained - *arbitrary memory* for performance reasons, which could include anything ranging from - program source code to passwords and encryption keys. -- `new Buffer('abc')` creates a `Buffer` that contains the UTF-8-encoded version of - the string `'abc'`. A second argument could specify another encoding: For example, - `new Buffer(string, 'base64')` could be used to convert a Base64 string into the original - sequence of bytes that it represents. -- There are several other combinations of arguments. - -This meant that, in code like `var buffer = new Buffer(foo);`, *it is not possible to tell -what exactly the contents of the generated buffer are* without knowing the type of `foo`. - -Sometimes, the value of `foo` comes from an external source. For example, this function -could be exposed as a service on a web server, converting a UTF-8 string into its Base64 form: - -``` -function stringToBase64(req, res) { - // The request body should have the format of `{ string: 'foobar' }` - const rawBytes = new Buffer(req.body.string) - const encoded = rawBytes.toString('base64') - res.end({ encoded: encoded }) -} -``` - -Note that this code does *not* validate the type of `req.body.string`: - -- `req.body.string` is expected to be a string. If this is the case, all goes well. -- `req.body.string` is controlled by the client that sends the request. -- If `req.body.string` is the *number* `50`, the `rawBytes` would be 50 bytes: - - Before Node.js 8, the content would be uninitialized - - After Node.js 8, the content would be `50` bytes with the value `0` - -Because of the missing type check, an attacker could intentionally send a number -as part of the request. Using this, they can either: - -- Read uninitialized memory. This **will** leak passwords, encryption keys and other - kinds of sensitive information. (Information leak) -- Force the program to allocate a large amount of memory. For example, when specifying - `500000000` as the input value, each request will allocate 500MB of memory. - This can be used to either exhaust the memory available of a program completely - and make it crash, or slow it down significantly. (Denial of Service) - -Both of these scenarios are considered serious security issues in a real-world -web server context. - -when using `Buffer.from(req.body.string)` instead, passing a number will always -throw an exception instead, giving a controlled behaviour that can always be -handled by the program. - - -### The `Buffer()` constructor has been deprecated for a while. Is this really an issue? - -Surveys of code in the `npm` ecosystem have shown that the `Buffer()` constructor is still -widely used. This includes new code, and overall usage of such code has actually been -*increasing*. diff --git a/social/twitter/node_modules/safer-buffer/Readme.md b/social/twitter/node_modules/safer-buffer/Readme.md deleted file mode 100644 index 14b08229..00000000 --- a/social/twitter/node_modules/safer-buffer/Readme.md +++ /dev/null @@ -1,156 +0,0 @@ -# safer-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![javascript style guide][standard-image]][standard-url] [![Security Responsible Disclosure][secuirty-image]][secuirty-url] - -[travis-image]: https://travis-ci.org/ChALkeR/safer-buffer.svg?branch=master -[travis-url]: https://travis-ci.org/ChALkeR/safer-buffer -[npm-image]: https://img.shields.io/npm/v/safer-buffer.svg -[npm-url]: https://npmjs.org/package/safer-buffer -[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg -[standard-url]: https://standardjs.com -[secuirty-image]: https://img.shields.io/badge/Security-Responsible%20Disclosure-green.svg -[secuirty-url]: https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md - -Modern Buffer API polyfill without footguns, working on Node.js from 0.8 to current. - -## How to use? - -First, port all `Buffer()` and `new Buffer()` calls to `Buffer.alloc()` and `Buffer.from()` API. - -Then, to achieve compatibility with outdated Node.js versions (`<4.5.0` and 5.x `<5.9.0`), use -`const Buffer = require('safer-buffer').Buffer` in all files where you make calls to the new -Buffer API. _Use `var` instead of `const` if you need that for your Node.js version range support._ - -Also, see the -[porting Buffer](https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md) guide. - -## Do I need it? - -Hopefully, not — dropping support for outdated Node.js versions should be fine nowdays, and that -is the recommended path forward. You _do_ need to port to the `Buffer.alloc()` and `Buffer.from()` -though. - -See the [porting guide](https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md) -for a better description. - -## Why not [safe-buffer](https://npmjs.com/safe-buffer)? - -_In short: while `safe-buffer` serves as a polyfill for the new API, it allows old API usage and -itself contains footguns._ - -`safe-buffer` could be used safely to get the new API while still keeping support for older -Node.js versions (like this module), but while analyzing ecosystem usage of the old Buffer API -I found out that `safe-buffer` is itself causing problems in some cases. - -For example, consider the following snippet: - -```console -$ cat example.unsafe.js -console.log(Buffer(20)) -$ ./node-v6.13.0-linux-x64/bin/node example.unsafe.js - -$ standard example.unsafe.js -standard: Use JavaScript Standard Style (https://standardjs.com) - /home/chalker/repo/safer-buffer/example.unsafe.js:2:13: 'Buffer()' was deprecated since v6. Use 'Buffer.alloc()' or 'Buffer.from()' (use 'https://www.npmjs.com/package/safe-buffer' for '<4.5.0') instead. -``` - -This is allocates and writes to console an uninitialized chunk of memory. -[standard](https://www.npmjs.com/package/standard) linter (among others) catch that and warn people -to avoid using unsafe API. - -Let's now throw in `safe-buffer`! - -```console -$ cat example.safe-buffer.js -const Buffer = require('safe-buffer').Buffer -console.log(Buffer(20)) -$ standard example.safe-buffer.js -$ ./node-v6.13.0-linux-x64/bin/node example.safe-buffer.js - -``` - -See the problem? Adding in `safe-buffer` _magically removes the lint warning_, but the behavior -remains identiсal to what we had before, and when launched on Node.js 6.x LTS — this dumps out -chunks of uninitialized memory. -_And this code will still emit runtime warnings on Node.js 10.x and above._ - -That was done by design. I first considered changing `safe-buffer`, prohibiting old API usage or -emitting warnings on it, but that significantly diverges from `safe-buffer` design. After some -discussion, it was decided to move my approach into a separate package, and _this is that separate -package_. - -This footgun is not imaginary — I observed top-downloaded packages doing that kind of thing, -«fixing» the lint warning by blindly including `safe-buffer` without any actual changes. - -Also in some cases, even if the API _was_ migrated to use of safe Buffer API — a random pull request -can bring unsafe Buffer API usage back to the codebase by adding new calls — and that could go -unnoticed even if you have a linter prohibiting that (becase of the reason stated above), and even -pass CI. _I also observed that being done in popular packages._ - -Some examples: - * [webdriverio](https://github.com/webdriverio/webdriverio/commit/05cbd3167c12e4930f09ef7cf93b127ba4effae4#diff-124380949022817b90b622871837d56cR31) - (a module with 548 759 downloads/month), - * [websocket-stream](https://github.com/maxogden/websocket-stream/commit/c9312bd24d08271687d76da0fe3c83493871cf61) - (218 288 d/m, fix in [maxogden/websocket-stream#142](https://github.com/maxogden/websocket-stream/pull/142)), - * [node-serialport](https://github.com/node-serialport/node-serialport/commit/e8d9d2b16c664224920ce1c895199b1ce2def48c) - (113 138 d/m, fix in [node-serialport/node-serialport#1510](https://github.com/node-serialport/node-serialport/pull/1510)), - * [karma](https://github.com/karma-runner/karma/commit/3d94b8cf18c695104ca195334dc75ff054c74eec) - (3 973 193 d/m, fix in [karma-runner/karma#2947](https://github.com/karma-runner/karma/pull/2947)), - * [spdy-transport](https://github.com/spdy-http2/spdy-transport/commit/5375ac33f4a62a4f65bcfc2827447d42a5dbe8b1) - (5 970 727 d/m, fix in [spdy-http2/spdy-transport#53](https://github.com/spdy-http2/spdy-transport/pull/53)). - * And there are a lot more over the ecosystem. - -I filed a PR at -[mysticatea/eslint-plugin-node#110](https://github.com/mysticatea/eslint-plugin-node/pull/110) to -partially fix that (for cases when that lint rule is used), but it is a semver-major change for -linter rules and presets, so it would take significant time for that to reach actual setups. -_It also hasn't been released yet (2018-03-20)._ - -Also, `safer-buffer` discourages the usage of `.allocUnsafe()`, which is often done by a mistake. -It still supports it with an explicit concern barier, by placing it under -`require('safer-buffer/dangereous')`. - -## But isn't throwing bad? - -Not really. It's an error that could be noticed and fixed early, instead of causing havoc later like -unguarded `new Buffer()` calls that end up receiving user input can do. - -This package affects only the files where `var Buffer = require('safer-buffer').Buffer` was done, so -it is really simple to keep track of things and make sure that you don't mix old API usage with that. -Also, CI should hint anything that you might have missed. - -New commits, if tested, won't land new usage of unsafe Buffer API this way. -_Node.js 10.x also deals with that by printing a runtime depecation warning._ - -### Would it affect third-party modules? - -No, unless you explicitly do an awful thing like monkey-patching or overriding the built-in `Buffer`. -Don't do that. - -### But I don't want throwing… - -That is also fine! - -Also, it could be better in some cases when you don't comprehensive enough test coverage. - -In that case — just don't override `Buffer` and use -`var SaferBuffer = require('safer-buffer').Buffer` instead. - -That way, everything using `Buffer` natively would still work, but there would be two drawbacks: - -* `Buffer.from`/`Buffer.alloc` won't be polyfilled — use `SaferBuffer.from` and - `SaferBuffer.alloc` instead. -* You are still open to accidentally using the insecure deprecated API — use a linter to catch that. - -Note that using a linter to catch accidential `Buffer` constructor usage in this case is strongly -recommended. `Buffer` is not overriden in this usecase, so linters won't get confused. - -## «Without footguns»? - -Well, it is still possible to do _some_ things with `Buffer` API, e.g. accessing `.buffer` property -on older versions and duping things from there. You shouldn't do that in your code, probabably. - -The intention is to remove the most significant footguns that affect lots of packages in the -ecosystem, and to do it in the proper way. - -Also, this package doesn't protect against security issues affecting some Node.js versions, so for -usage in your own production code, it is still recommended to update to a Node.js version -[supported by upstream](https://github.com/nodejs/release#release-schedule). diff --git a/social/twitter/node_modules/safer-buffer/dangerous.js b/social/twitter/node_modules/safer-buffer/dangerous.js deleted file mode 100644 index ca41fdc5..00000000 --- a/social/twitter/node_modules/safer-buffer/dangerous.js +++ /dev/null @@ -1,58 +0,0 @@ -/* eslint-disable node/no-deprecated-api */ - -'use strict' - -var buffer = require('buffer') -var Buffer = buffer.Buffer -var safer = require('./safer.js') -var Safer = safer.Buffer - -var dangerous = {} - -var key - -for (key in safer) { - if (!safer.hasOwnProperty(key)) continue - dangerous[key] = safer[key] -} - -var Dangereous = dangerous.Buffer = {} - -// Copy Safer API -for (key in Safer) { - if (!Safer.hasOwnProperty(key)) continue - Dangereous[key] = Safer[key] -} - -// Copy those missing unsafe methods, if they are present -for (key in Buffer) { - if (!Buffer.hasOwnProperty(key)) continue - if (Dangereous.hasOwnProperty(key)) continue - Dangereous[key] = Buffer[key] -} - -if (!Dangereous.allocUnsafe) { - Dangereous.allocUnsafe = function (size) { - if (typeof size !== 'number') { - throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) - } - if (size < 0 || size >= 2 * (1 << 30)) { - throw new RangeError('The value "' + size + '" is invalid for option "size"') - } - return Buffer(size) - } -} - -if (!Dangereous.allocUnsafeSlow) { - Dangereous.allocUnsafeSlow = function (size) { - if (typeof size !== 'number') { - throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) - } - if (size < 0 || size >= 2 * (1 << 30)) { - throw new RangeError('The value "' + size + '" is invalid for option "size"') - } - return buffer.SlowBuffer(size) - } -} - -module.exports = dangerous diff --git a/social/twitter/node_modules/safer-buffer/package.json b/social/twitter/node_modules/safer-buffer/package.json deleted file mode 100644 index d452b04a..00000000 --- a/social/twitter/node_modules/safer-buffer/package.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "safer-buffer", - "version": "2.1.2", - "description": "Modern Buffer API polyfill without footguns", - "main": "safer.js", - "scripts": { - "browserify-test": "browserify --external tape tests.js > browserify-tests.js && tape browserify-tests.js", - "test": "standard && tape tests.js" - }, - "author": { - "name": "Nikita Skovoroda", - "email": "chalkerx@gmail.com", - "url": "https://github.com/ChALkeR" - }, - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/ChALkeR/safer-buffer.git" - }, - "bugs": { - "url": "https://github.com/ChALkeR/safer-buffer/issues" - }, - "devDependencies": { - "standard": "^11.0.1", - "tape": "^4.9.0" - }, - "files": [ - "Porting-Buffer.md", - "Readme.md", - "tests.js", - "dangerous.js", - "safer.js" - ] -} diff --git a/social/twitter/node_modules/safer-buffer/safer.js b/social/twitter/node_modules/safer-buffer/safer.js deleted file mode 100644 index 37c7e1aa..00000000 --- a/social/twitter/node_modules/safer-buffer/safer.js +++ /dev/null @@ -1,77 +0,0 @@ -/* eslint-disable node/no-deprecated-api */ - -'use strict' - -var buffer = require('buffer') -var Buffer = buffer.Buffer - -var safer = {} - -var key - -for (key in buffer) { - if (!buffer.hasOwnProperty(key)) continue - if (key === 'SlowBuffer' || key === 'Buffer') continue - safer[key] = buffer[key] -} - -var Safer = safer.Buffer = {} -for (key in Buffer) { - if (!Buffer.hasOwnProperty(key)) continue - if (key === 'allocUnsafe' || key === 'allocUnsafeSlow') continue - Safer[key] = Buffer[key] -} - -safer.Buffer.prototype = Buffer.prototype - -if (!Safer.from || Safer.from === Uint8Array.from) { - Safer.from = function (value, encodingOrOffset, length) { - if (typeof value === 'number') { - throw new TypeError('The "value" argument must not be of type number. Received type ' + typeof value) - } - if (value && typeof value.length === 'undefined') { - throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type ' + typeof value) - } - return Buffer(value, encodingOrOffset, length) - } -} - -if (!Safer.alloc) { - Safer.alloc = function (size, fill, encoding) { - if (typeof size !== 'number') { - throw new TypeError('The "size" argument must be of type number. Received type ' + typeof size) - } - if (size < 0 || size >= 2 * (1 << 30)) { - throw new RangeError('The value "' + size + '" is invalid for option "size"') - } - var buf = Buffer(size) - if (!fill || fill.length === 0) { - buf.fill(0) - } else if (typeof encoding === 'string') { - buf.fill(fill, encoding) - } else { - buf.fill(fill) - } - return buf - } -} - -if (!safer.kStringMaxLength) { - try { - safer.kStringMaxLength = process.binding('buffer').kStringMaxLength - } catch (e) { - // we can't determine kStringMaxLength in environments where process.binding - // is unsupported, so let's not set it - } -} - -if (!safer.constants) { - safer.constants = { - MAX_LENGTH: safer.kMaxLength - } - if (safer.kStringMaxLength) { - safer.constants.MAX_STRING_LENGTH = safer.kStringMaxLength - } -} - -module.exports = safer diff --git a/social/twitter/node_modules/safer-buffer/tests.js b/social/twitter/node_modules/safer-buffer/tests.js deleted file mode 100644 index 7ed2777c..00000000 --- a/social/twitter/node_modules/safer-buffer/tests.js +++ /dev/null @@ -1,406 +0,0 @@ -/* eslint-disable node/no-deprecated-api */ - -'use strict' - -var test = require('tape') - -var buffer = require('buffer') - -var index = require('./') -var safer = require('./safer') -var dangerous = require('./dangerous') - -/* Inheritance tests */ - -test('Default is Safer', function (t) { - t.equal(index, safer) - t.notEqual(safer, dangerous) - t.notEqual(index, dangerous) - t.end() -}) - -test('Is not a function', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(typeof impl, 'object') - t.equal(typeof impl.Buffer, 'object') - }); - [buffer].forEach(function (impl) { - t.equal(typeof impl, 'object') - t.equal(typeof impl.Buffer, 'function') - }) - t.end() -}) - -test('Constructor throws', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.throws(function () { impl.Buffer() }) - t.throws(function () { impl.Buffer(0) }) - t.throws(function () { impl.Buffer('a') }) - t.throws(function () { impl.Buffer('a', 'utf-8') }) - t.throws(function () { return new impl.Buffer() }) - t.throws(function () { return new impl.Buffer(0) }) - t.throws(function () { return new impl.Buffer('a') }) - t.throws(function () { return new impl.Buffer('a', 'utf-8') }) - }) - t.end() -}) - -test('Safe methods exist', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(typeof impl.Buffer.alloc, 'function', 'alloc') - t.equal(typeof impl.Buffer.from, 'function', 'from') - }) - t.end() -}) - -test('Unsafe methods exist only in Dangerous', function (t) { - [index, safer].forEach(function (impl) { - t.equal(typeof impl.Buffer.allocUnsafe, 'undefined') - t.equal(typeof impl.Buffer.allocUnsafeSlow, 'undefined') - }); - [dangerous].forEach(function (impl) { - t.equal(typeof impl.Buffer.allocUnsafe, 'function') - t.equal(typeof impl.Buffer.allocUnsafeSlow, 'function') - }) - t.end() -}) - -test('Generic methods/properties are defined and equal', function (t) { - ['poolSize', 'isBuffer', 'concat', 'byteLength'].forEach(function (method) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer[method], buffer.Buffer[method], method) - t.notEqual(typeof impl.Buffer[method], 'undefined', method) - }) - }) - t.end() -}) - -test('Built-in buffer static methods/properties are inherited', function (t) { - Object.keys(buffer).forEach(function (method) { - if (method === 'SlowBuffer' || method === 'Buffer') return; - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl[method], buffer[method], method) - t.notEqual(typeof impl[method], 'undefined', method) - }) - }) - t.end() -}) - -test('Built-in Buffer static methods/properties are inherited', function (t) { - Object.keys(buffer.Buffer).forEach(function (method) { - if (method === 'allocUnsafe' || method === 'allocUnsafeSlow') return; - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer[method], buffer.Buffer[method], method) - t.notEqual(typeof impl.Buffer[method], 'undefined', method) - }) - }) - t.end() -}) - -test('.prototype property of Buffer is inherited', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer.prototype, buffer.Buffer.prototype, 'prototype') - t.notEqual(typeof impl.Buffer.prototype, 'undefined', 'prototype') - }) - t.end() -}) - -test('All Safer methods are present in Dangerous', function (t) { - Object.keys(safer).forEach(function (method) { - if (method === 'Buffer') return; - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl[method], safer[method], method) - if (method !== 'kStringMaxLength') { - t.notEqual(typeof impl[method], 'undefined', method) - } - }) - }) - Object.keys(safer.Buffer).forEach(function (method) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer[method], safer.Buffer[method], method) - t.notEqual(typeof impl.Buffer[method], 'undefined', method) - }) - }) - t.end() -}) - -test('Safe methods from Dangerous methods are present in Safer', function (t) { - Object.keys(dangerous).forEach(function (method) { - if (method === 'Buffer') return; - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl[method], dangerous[method], method) - if (method !== 'kStringMaxLength') { - t.notEqual(typeof impl[method], 'undefined', method) - } - }) - }) - Object.keys(dangerous.Buffer).forEach(function (method) { - if (method === 'allocUnsafe' || method === 'allocUnsafeSlow') return; - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer[method], dangerous.Buffer[method], method) - t.notEqual(typeof impl.Buffer[method], 'undefined', method) - }) - }) - t.end() -}) - -/* Behaviour tests */ - -test('Methods return Buffers', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(0))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(0, 10))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(0, 'a'))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(10))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(10, 'x'))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.alloc(9, 'ab'))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from(''))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from('string'))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from('string', 'utf-8'))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64'))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from([0, 42, 3]))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from(new Uint8Array([0, 42, 3])))) - t.ok(buffer.Buffer.isBuffer(impl.Buffer.from([]))) - }); - ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { - t.ok(buffer.Buffer.isBuffer(dangerous.Buffer[method](0))) - t.ok(buffer.Buffer.isBuffer(dangerous.Buffer[method](10))) - }) - t.end() -}) - -test('Constructor is buffer.Buffer', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer.alloc(0).constructor, buffer.Buffer) - t.equal(impl.Buffer.alloc(0, 10).constructor, buffer.Buffer) - t.equal(impl.Buffer.alloc(0, 'a').constructor, buffer.Buffer) - t.equal(impl.Buffer.alloc(10).constructor, buffer.Buffer) - t.equal(impl.Buffer.alloc(10, 'x').constructor, buffer.Buffer) - t.equal(impl.Buffer.alloc(9, 'ab').constructor, buffer.Buffer) - t.equal(impl.Buffer.from('').constructor, buffer.Buffer) - t.equal(impl.Buffer.from('string').constructor, buffer.Buffer) - t.equal(impl.Buffer.from('string', 'utf-8').constructor, buffer.Buffer) - t.equal(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64').constructor, buffer.Buffer) - t.equal(impl.Buffer.from([0, 42, 3]).constructor, buffer.Buffer) - t.equal(impl.Buffer.from(new Uint8Array([0, 42, 3])).constructor, buffer.Buffer) - t.equal(impl.Buffer.from([]).constructor, buffer.Buffer) - }); - [0, 10, 100].forEach(function (arg) { - t.equal(dangerous.Buffer.allocUnsafe(arg).constructor, buffer.Buffer) - t.equal(dangerous.Buffer.allocUnsafeSlow(arg).constructor, buffer.SlowBuffer(0).constructor) - }) - t.end() -}) - -test('Invalid calls throw', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.throws(function () { impl.Buffer.from(0) }) - t.throws(function () { impl.Buffer.from(10) }) - t.throws(function () { impl.Buffer.from(10, 'utf-8') }) - t.throws(function () { impl.Buffer.from('string', 'invalid encoding') }) - t.throws(function () { impl.Buffer.from(-10) }) - t.throws(function () { impl.Buffer.from(1e90) }) - t.throws(function () { impl.Buffer.from(Infinity) }) - t.throws(function () { impl.Buffer.from(-Infinity) }) - t.throws(function () { impl.Buffer.from(NaN) }) - t.throws(function () { impl.Buffer.from(null) }) - t.throws(function () { impl.Buffer.from(undefined) }) - t.throws(function () { impl.Buffer.from() }) - t.throws(function () { impl.Buffer.from({}) }) - t.throws(function () { impl.Buffer.alloc('') }) - t.throws(function () { impl.Buffer.alloc('string') }) - t.throws(function () { impl.Buffer.alloc('string', 'utf-8') }) - t.throws(function () { impl.Buffer.alloc('b25ldHdvdGhyZWU=', 'base64') }) - t.throws(function () { impl.Buffer.alloc(-10) }) - t.throws(function () { impl.Buffer.alloc(1e90) }) - t.throws(function () { impl.Buffer.alloc(2 * (1 << 30)) }) - t.throws(function () { impl.Buffer.alloc(Infinity) }) - t.throws(function () { impl.Buffer.alloc(-Infinity) }) - t.throws(function () { impl.Buffer.alloc(null) }) - t.throws(function () { impl.Buffer.alloc(undefined) }) - t.throws(function () { impl.Buffer.alloc() }) - t.throws(function () { impl.Buffer.alloc([]) }) - t.throws(function () { impl.Buffer.alloc([0, 42, 3]) }) - t.throws(function () { impl.Buffer.alloc({}) }) - }); - ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { - t.throws(function () { dangerous.Buffer[method]('') }) - t.throws(function () { dangerous.Buffer[method]('string') }) - t.throws(function () { dangerous.Buffer[method]('string', 'utf-8') }) - t.throws(function () { dangerous.Buffer[method](2 * (1 << 30)) }) - t.throws(function () { dangerous.Buffer[method](Infinity) }) - if (dangerous.Buffer[method] === buffer.Buffer.allocUnsafe) { - t.skip('Skipping, older impl of allocUnsafe coerced negative sizes to 0') - } else { - t.throws(function () { dangerous.Buffer[method](-10) }) - t.throws(function () { dangerous.Buffer[method](-1e90) }) - t.throws(function () { dangerous.Buffer[method](-Infinity) }) - } - t.throws(function () { dangerous.Buffer[method](null) }) - t.throws(function () { dangerous.Buffer[method](undefined) }) - t.throws(function () { dangerous.Buffer[method]() }) - t.throws(function () { dangerous.Buffer[method]([]) }) - t.throws(function () { dangerous.Buffer[method]([0, 42, 3]) }) - t.throws(function () { dangerous.Buffer[method]({}) }) - }) - t.end() -}) - -test('Buffers have appropriate lengths', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.equal(impl.Buffer.alloc(0).length, 0) - t.equal(impl.Buffer.alloc(10).length, 10) - t.equal(impl.Buffer.from('').length, 0) - t.equal(impl.Buffer.from('string').length, 6) - t.equal(impl.Buffer.from('string', 'utf-8').length, 6) - t.equal(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64').length, 11) - t.equal(impl.Buffer.from([0, 42, 3]).length, 3) - t.equal(impl.Buffer.from(new Uint8Array([0, 42, 3])).length, 3) - t.equal(impl.Buffer.from([]).length, 0) - }); - ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { - t.equal(dangerous.Buffer[method](0).length, 0) - t.equal(dangerous.Buffer[method](10).length, 10) - }) - t.end() -}) - -test('Buffers have appropriate lengths (2)', function (t) { - t.equal(index.Buffer.alloc, safer.Buffer.alloc) - t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) - var ok = true; - [ safer.Buffer.alloc, - dangerous.Buffer.allocUnsafe, - dangerous.Buffer.allocUnsafeSlow - ].forEach(function (method) { - for (var i = 0; i < 1e2; i++) { - var length = Math.round(Math.random() * 1e5) - var buf = method(length) - if (!buffer.Buffer.isBuffer(buf)) ok = false - if (buf.length !== length) ok = false - } - }) - t.ok(ok) - t.end() -}) - -test('.alloc(size) is zero-filled and has correct length', function (t) { - t.equal(index.Buffer.alloc, safer.Buffer.alloc) - t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) - var ok = true - for (var i = 0; i < 1e2; i++) { - var length = Math.round(Math.random() * 2e6) - var buf = index.Buffer.alloc(length) - if (!buffer.Buffer.isBuffer(buf)) ok = false - if (buf.length !== length) ok = false - var j - for (j = 0; j < length; j++) { - if (buf[j] !== 0) ok = false - } - buf.fill(1) - for (j = 0; j < length; j++) { - if (buf[j] !== 1) ok = false - } - } - t.ok(ok) - t.end() -}) - -test('.allocUnsafe / .allocUnsafeSlow are fillable and have correct lengths', function (t) { - ['allocUnsafe', 'allocUnsafeSlow'].forEach(function (method) { - var ok = true - for (var i = 0; i < 1e2; i++) { - var length = Math.round(Math.random() * 2e6) - var buf = dangerous.Buffer[method](length) - if (!buffer.Buffer.isBuffer(buf)) ok = false - if (buf.length !== length) ok = false - buf.fill(0, 0, length) - var j - for (j = 0; j < length; j++) { - if (buf[j] !== 0) ok = false - } - buf.fill(1, 0, length) - for (j = 0; j < length; j++) { - if (buf[j] !== 1) ok = false - } - } - t.ok(ok, method) - }) - t.end() -}) - -test('.alloc(size, fill) is `fill`-filled', function (t) { - t.equal(index.Buffer.alloc, safer.Buffer.alloc) - t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) - var ok = true - for (var i = 0; i < 1e2; i++) { - var length = Math.round(Math.random() * 2e6) - var fill = Math.round(Math.random() * 255) - var buf = index.Buffer.alloc(length, fill) - if (!buffer.Buffer.isBuffer(buf)) ok = false - if (buf.length !== length) ok = false - for (var j = 0; j < length; j++) { - if (buf[j] !== fill) ok = false - } - } - t.ok(ok) - t.end() -}) - -test('.alloc(size, fill) is `fill`-filled', function (t) { - t.equal(index.Buffer.alloc, safer.Buffer.alloc) - t.equal(index.Buffer.alloc, dangerous.Buffer.alloc) - var ok = true - for (var i = 0; i < 1e2; i++) { - var length = Math.round(Math.random() * 2e6) - var fill = Math.round(Math.random() * 255) - var buf = index.Buffer.alloc(length, fill) - if (!buffer.Buffer.isBuffer(buf)) ok = false - if (buf.length !== length) ok = false - for (var j = 0; j < length; j++) { - if (buf[j] !== fill) ok = false - } - } - t.ok(ok) - t.deepEqual(index.Buffer.alloc(9, 'a'), index.Buffer.alloc(9, 97)) - t.notDeepEqual(index.Buffer.alloc(9, 'a'), index.Buffer.alloc(9, 98)) - - var tmp = new buffer.Buffer(2) - tmp.fill('ok') - if (tmp[1] === tmp[0]) { - // Outdated Node.js - t.deepEqual(index.Buffer.alloc(5, 'ok'), index.Buffer.from('ooooo')) - } else { - t.deepEqual(index.Buffer.alloc(5, 'ok'), index.Buffer.from('okoko')) - } - t.notDeepEqual(index.Buffer.alloc(5, 'ok'), index.Buffer.from('kokok')) - - t.end() -}) - -test('safer.Buffer.from returns results same as Buffer constructor', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.deepEqual(impl.Buffer.from(''), new buffer.Buffer('')) - t.deepEqual(impl.Buffer.from('string'), new buffer.Buffer('string')) - t.deepEqual(impl.Buffer.from('string', 'utf-8'), new buffer.Buffer('string', 'utf-8')) - t.deepEqual(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64'), new buffer.Buffer('b25ldHdvdGhyZWU=', 'base64')) - t.deepEqual(impl.Buffer.from([0, 42, 3]), new buffer.Buffer([0, 42, 3])) - t.deepEqual(impl.Buffer.from(new Uint8Array([0, 42, 3])), new buffer.Buffer(new Uint8Array([0, 42, 3]))) - t.deepEqual(impl.Buffer.from([]), new buffer.Buffer([])) - }) - t.end() -}) - -test('safer.Buffer.from returns consistent results', function (t) { - [index, safer, dangerous].forEach(function (impl) { - t.deepEqual(impl.Buffer.from(''), impl.Buffer.alloc(0)) - t.deepEqual(impl.Buffer.from([]), impl.Buffer.alloc(0)) - t.deepEqual(impl.Buffer.from(new Uint8Array([])), impl.Buffer.alloc(0)) - t.deepEqual(impl.Buffer.from('string', 'utf-8'), impl.Buffer.from('string')) - t.deepEqual(impl.Buffer.from('string'), impl.Buffer.from([115, 116, 114, 105, 110, 103])) - t.deepEqual(impl.Buffer.from('string'), impl.Buffer.from(impl.Buffer.from('string'))) - t.deepEqual(impl.Buffer.from('b25ldHdvdGhyZWU=', 'base64'), impl.Buffer.from('onetwothree')) - t.notDeepEqual(impl.Buffer.from('b25ldHdvdGhyZWU='), impl.Buffer.from('onetwothree')) - }) - t.end() -}) diff --git a/social/twitter/node_modules/sax/LICENSE b/social/twitter/node_modules/sax/LICENSE deleted file mode 100644 index ccffa082..00000000 --- a/social/twitter/node_modules/sax/LICENSE +++ /dev/null @@ -1,41 +0,0 @@ -The ISC License - -Copyright (c) Isaac Z. Schlueter and Contributors - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR -IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -==== - -`String.fromCodePoint` by Mathias Bynens used according to terms of MIT -License, as follows: - - Copyright Mathias Bynens - - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: - - The above copyright notice and this permission notice shall be - included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/social/twitter/node_modules/sax/README.md b/social/twitter/node_modules/sax/README.md deleted file mode 100644 index afcd3f3d..00000000 --- a/social/twitter/node_modules/sax/README.md +++ /dev/null @@ -1,225 +0,0 @@ -# sax js - -A sax-style parser for XML and HTML. - -Designed with [node](http://nodejs.org/) in mind, but should work fine in -the browser or other CommonJS implementations. - -## What This Is - -* A very simple tool to parse through an XML string. -* A stepping stone to a streaming HTML parser. -* A handy way to deal with RSS and other mostly-ok-but-kinda-broken XML - docs. - -## What This Is (probably) Not - -* An HTML Parser - That's a fine goal, but this isn't it. It's just - XML. -* A DOM Builder - You can use it to build an object model out of XML, - but it doesn't do that out of the box. -* XSLT - No DOM = no querying. -* 100% Compliant with (some other SAX implementation) - Most SAX - implementations are in Java and do a lot more than this does. -* An XML Validator - It does a little validation when in strict mode, but - not much. -* A Schema-Aware XSD Thing - Schemas are an exercise in fetishistic - masochism. -* A DTD-aware Thing - Fetching DTDs is a much bigger job. - -## Regarding `Hello, world!').close(); - -// stream usage -// takes the same options as the parser -var saxStream = require("sax").createStream(strict, options) -saxStream.on("error", function (e) { - // unhandled errors will throw, since this is a proper node - // event emitter. - console.error("error!", e) - // clear the error - this._parser.error = null - this._parser.resume() -}) -saxStream.on("opentag", function (node) { - // same object as above -}) -// pipe is supported, and it's readable/writable -// same chunks coming in also go out. -fs.createReadStream("file.xml") - .pipe(saxStream) - .pipe(fs.createWriteStream("file-copy.xml")) -``` - - -## Arguments - -Pass the following arguments to the parser function. All are optional. - -`strict` - Boolean. Whether or not to be a jerk. Default: `false`. - -`opt` - Object bag of settings regarding string formatting. All default to `false`. - -Settings supported: - -* `trim` - Boolean. Whether or not to trim text and comment nodes. -* `normalize` - Boolean. If true, then turn any whitespace into a single - space. -* `lowercase` - Boolean. If true, then lowercase tag names and attribute names - in loose mode, rather than uppercasing them. -* `xmlns` - Boolean. If true, then namespaces are supported. -* `position` - Boolean. If false, then don't track line/col/position. -* `strictEntities` - Boolean. If true, only parse [predefined XML - entities](http://www.w3.org/TR/REC-xml/#sec-predefined-ent) - (`&`, `'`, `>`, `<`, and `"`) - -## Methods - -`write` - Write bytes onto the stream. You don't have to do this all at -once. You can keep writing as much as you want. - -`close` - Close the stream. Once closed, no more data may be written until -it is done processing the buffer, which is signaled by the `end` event. - -`resume` - To gracefully handle errors, assign a listener to the `error` -event. Then, when the error is taken care of, you can call `resume` to -continue parsing. Otherwise, the parser will not continue while in an error -state. - -## Members - -At all times, the parser object will have the following members: - -`line`, `column`, `position` - Indications of the position in the XML -document where the parser currently is looking. - -`startTagPosition` - Indicates the position where the current tag starts. - -`closed` - Boolean indicating whether or not the parser can be written to. -If it's `true`, then wait for the `ready` event to write again. - -`strict` - Boolean indicating whether or not the parser is a jerk. - -`opt` - Any options passed into the constructor. - -`tag` - The current tag being dealt with. - -And a bunch of other stuff that you probably shouldn't touch. - -## Events - -All events emit with a single argument. To listen to an event, assign a -function to `on`. Functions get executed in the this-context of -the parser object. The list of supported events are also in the exported -`EVENTS` array. - -When using the stream interface, assign handlers using the EventEmitter -`on` function in the normal fashion. - -`error` - Indication that something bad happened. The error will be hanging -out on `parser.error`, and must be deleted before parsing can continue. By -listening to this event, you can keep an eye on that kind of stuff. Note: -this happens *much* more in strict mode. Argument: instance of `Error`. - -`text` - Text node. Argument: string of text. - -`doctype` - The ``. Argument: -object with `name` and `body` members. Attributes are not parsed, as -processing instructions have implementation dependent semantics. - -`sgmldeclaration` - Random SGML declarations. Stuff like `` -would trigger this kind of event. This is a weird thing to support, so it -might go away at some point. SAX isn't intended to be used to parse SGML, -after all. - -`opentagstart` - Emitted immediately when the tag name is available, -but before any attributes are encountered. Argument: object with a -`name` field and an empty `attributes` set. Note that this is the -same object that will later be emitted in the `opentag` event. - -`opentag` - An opening tag. Argument: object with `name` and `attributes`. -In non-strict mode, tag names are uppercased, unless the `lowercase` -option is set. If the `xmlns` option is set, then it will contain -namespace binding information on the `ns` member, and will have a -`local`, `prefix`, and `uri` member. - -`closetag` - A closing tag. In loose mode, tags are auto-closed if their -parent closes. In strict mode, well-formedness is enforced. Note that -self-closing tags will have `closeTag` emitted immediately after `openTag`. -Argument: tag name. - -`attribute` - An attribute node. Argument: object with `name` and `value`. -In non-strict mode, attribute names are uppercased, unless the `lowercase` -option is set. If the `xmlns` option is set, it will also contains namespace -information. - -`comment` - A comment node. Argument: the string of the comment. - -`opencdata` - The opening tag of a ``) of a `` tags trigger a `"script"` -event, and their contents are not checked for special xml characters. -If you pass `noscript: true`, then this behavior is suppressed. - -## Reporting Problems - -It's best to write a failing test if you find an issue. I will always -accept pull requests with failing tests if they demonstrate intended -behavior, but it is very hard to figure out what issue you're describing -without a test. Writing a test is also the best way for you yourself -to figure out if you really understand the issue you think you have with -sax-js. diff --git a/social/twitter/node_modules/sax/lib/sax.js b/social/twitter/node_modules/sax/lib/sax.js deleted file mode 100644 index 795d607e..00000000 --- a/social/twitter/node_modules/sax/lib/sax.js +++ /dev/null @@ -1,1565 +0,0 @@ -;(function (sax) { // wrapper for non-node envs - sax.parser = function (strict, opt) { return new SAXParser(strict, opt) } - sax.SAXParser = SAXParser - sax.SAXStream = SAXStream - sax.createStream = createStream - - // When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns. - // When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)), - // since that's the earliest that a buffer overrun could occur. This way, checks are - // as rare as required, but as often as necessary to ensure never crossing this bound. - // Furthermore, buffers are only tested at most once per write(), so passing a very - // large string into write() might have undesirable effects, but this is manageable by - // the caller, so it is assumed to be safe. Thus, a call to write() may, in the extreme - // edge case, result in creating at most one complete copy of the string passed in. - // Set to Infinity to have unlimited buffers. - sax.MAX_BUFFER_LENGTH = 64 * 1024 - - var buffers = [ - 'comment', 'sgmlDecl', 'textNode', 'tagName', 'doctype', - 'procInstName', 'procInstBody', 'entity', 'attribName', - 'attribValue', 'cdata', 'script' - ] - - sax.EVENTS = [ - 'text', - 'processinginstruction', - 'sgmldeclaration', - 'doctype', - 'comment', - 'opentagstart', - 'attribute', - 'opentag', - 'closetag', - 'opencdata', - 'cdata', - 'closecdata', - 'error', - 'end', - 'ready', - 'script', - 'opennamespace', - 'closenamespace' - ] - - function SAXParser (strict, opt) { - if (!(this instanceof SAXParser)) { - return new SAXParser(strict, opt) - } - - var parser = this - clearBuffers(parser) - parser.q = parser.c = '' - parser.bufferCheckPosition = sax.MAX_BUFFER_LENGTH - parser.opt = opt || {} - parser.opt.lowercase = parser.opt.lowercase || parser.opt.lowercasetags - parser.looseCase = parser.opt.lowercase ? 'toLowerCase' : 'toUpperCase' - parser.tags = [] - parser.closed = parser.closedRoot = parser.sawRoot = false - parser.tag = parser.error = null - parser.strict = !!strict - parser.noscript = !!(strict || parser.opt.noscript) - parser.state = S.BEGIN - parser.strictEntities = parser.opt.strictEntities - parser.ENTITIES = parser.strictEntities ? Object.create(sax.XML_ENTITIES) : Object.create(sax.ENTITIES) - parser.attribList = [] - - // namespaces form a prototype chain. - // it always points at the current tag, - // which protos to its parent tag. - if (parser.opt.xmlns) { - parser.ns = Object.create(rootNS) - } - - // mostly just for error reporting - parser.trackPosition = parser.opt.position !== false - if (parser.trackPosition) { - parser.position = parser.line = parser.column = 0 - } - emit(parser, 'onready') - } - - if (!Object.create) { - Object.create = function (o) { - function F () {} - F.prototype = o - var newf = new F() - return newf - } - } - - if (!Object.keys) { - Object.keys = function (o) { - var a = [] - for (var i in o) if (o.hasOwnProperty(i)) a.push(i) - return a - } - } - - function checkBufferLength (parser) { - var maxAllowed = Math.max(sax.MAX_BUFFER_LENGTH, 10) - var maxActual = 0 - for (var i = 0, l = buffers.length; i < l; i++) { - var len = parser[buffers[i]].length - if (len > maxAllowed) { - // Text/cdata nodes can get big, and since they're buffered, - // we can get here under normal conditions. - // Avoid issues by emitting the text node now, - // so at least it won't get any bigger. - switch (buffers[i]) { - case 'textNode': - closeText(parser) - break - - case 'cdata': - emitNode(parser, 'oncdata', parser.cdata) - parser.cdata = '' - break - - case 'script': - emitNode(parser, 'onscript', parser.script) - parser.script = '' - break - - default: - error(parser, 'Max buffer length exceeded: ' + buffers[i]) - } - } - maxActual = Math.max(maxActual, len) - } - // schedule the next check for the earliest possible buffer overrun. - var m = sax.MAX_BUFFER_LENGTH - maxActual - parser.bufferCheckPosition = m + parser.position - } - - function clearBuffers (parser) { - for (var i = 0, l = buffers.length; i < l; i++) { - parser[buffers[i]] = '' - } - } - - function flushBuffers (parser) { - closeText(parser) - if (parser.cdata !== '') { - emitNode(parser, 'oncdata', parser.cdata) - parser.cdata = '' - } - if (parser.script !== '') { - emitNode(parser, 'onscript', parser.script) - parser.script = '' - } - } - - SAXParser.prototype = { - end: function () { end(this) }, - write: write, - resume: function () { this.error = null; return this }, - close: function () { return this.write(null) }, - flush: function () { flushBuffers(this) } - } - - var Stream - try { - Stream = require('stream').Stream - } catch (ex) { - Stream = function () {} - } - - var streamWraps = sax.EVENTS.filter(function (ev) { - return ev !== 'error' && ev !== 'end' - }) - - function createStream (strict, opt) { - return new SAXStream(strict, opt) - } - - function SAXStream (strict, opt) { - if (!(this instanceof SAXStream)) { - return new SAXStream(strict, opt) - } - - Stream.apply(this) - - this._parser = new SAXParser(strict, opt) - this.writable = true - this.readable = true - - var me = this - - this._parser.onend = function () { - me.emit('end') - } - - this._parser.onerror = function (er) { - me.emit('error', er) - - // if didn't throw, then means error was handled. - // go ahead and clear error, so we can write again. - me._parser.error = null - } - - this._decoder = null - - streamWraps.forEach(function (ev) { - Object.defineProperty(me, 'on' + ev, { - get: function () { - return me._parser['on' + ev] - }, - set: function (h) { - if (!h) { - me.removeAllListeners(ev) - me._parser['on' + ev] = h - return h - } - me.on(ev, h) - }, - enumerable: true, - configurable: false - }) - }) - } - - SAXStream.prototype = Object.create(Stream.prototype, { - constructor: { - value: SAXStream - } - }) - - SAXStream.prototype.write = function (data) { - if (typeof Buffer === 'function' && - typeof Buffer.isBuffer === 'function' && - Buffer.isBuffer(data)) { - if (!this._decoder) { - var SD = require('string_decoder').StringDecoder - this._decoder = new SD('utf8') - } - data = this._decoder.write(data) - } - - this._parser.write(data.toString()) - this.emit('data', data) - return true - } - - SAXStream.prototype.end = function (chunk) { - if (chunk && chunk.length) { - this.write(chunk) - } - this._parser.end() - return true - } - - SAXStream.prototype.on = function (ev, handler) { - var me = this - if (!me._parser['on' + ev] && streamWraps.indexOf(ev) !== -1) { - me._parser['on' + ev] = function () { - var args = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments) - args.splice(0, 0, ev) - me.emit.apply(me, args) - } - } - - return Stream.prototype.on.call(me, ev, handler) - } - - // this really needs to be replaced with character classes. - // XML allows all manner of ridiculous numbers and digits. - var CDATA = '[CDATA[' - var DOCTYPE = 'DOCTYPE' - var XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace' - var XMLNS_NAMESPACE = 'http://www.w3.org/2000/xmlns/' - var rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE } - - // http://www.w3.org/TR/REC-xml/#NT-NameStartChar - // This implementation works on strings, a single character at a time - // as such, it cannot ever support astral-plane characters (10000-EFFFF) - // without a significant breaking change to either this parser, or the - // JavaScript language. Implementation of an emoji-capable xml parser - // is left as an exercise for the reader. - var nameStart = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/ - - var nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/ - - var entityStart = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/ - var entityBody = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/ - - function isWhitespace (c) { - return c === ' ' || c === '\n' || c === '\r' || c === '\t' - } - - function isQuote (c) { - return c === '"' || c === '\'' - } - - function isAttribEnd (c) { - return c === '>' || isWhitespace(c) - } - - function isMatch (regex, c) { - return regex.test(c) - } - - function notMatch (regex, c) { - return !isMatch(regex, c) - } - - var S = 0 - sax.STATE = { - BEGIN: S++, // leading byte order mark or whitespace - BEGIN_WHITESPACE: S++, // leading whitespace - TEXT: S++, // general stuff - TEXT_ENTITY: S++, // & and such. - OPEN_WAKA: S++, // < - SGML_DECL: S++, // - SCRIPT: S++, //