"Argument str must be a string" error in torrent streaming app [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I'm trying to build a torrent streaming api-thing, that streams you video files you download with torrents.
Here's my code:
import torrentStream from 'torrent-stream';
import rangeParser from 'range-parser';
import express from 'express';
const app = express();
const magnetURI = 'magnet:?xt=urn:btih:331B74F12A1CE8D00F0F8BE0844F5CC6471C925E&dn=The.Last.of.Us.S01E04.1080p.HMAX.WEBRip.DDP5.1.Atmos.x264-SMURF%5BTGx%5D&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&tr=udp%3A%2F%2Fipv4.tracker.harry.lu%3A80%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.birkenwald.de%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.moeking.me%3A6969%2Fannounce&tr=udp%3A%2F%2Fopentor.org%3A2710%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2970%2Fannounce&tr=https%3A%2F%2Ftracker.foreverpirates.co%3A443%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce';
const engine = torrentStream(magnetURI, {
path: './downloads', // location to save the downloaded file
});
engine.on('ready', () => {
const files = engine.files;
// Only select the first video file (you may need to modify this depending on your torrent)
const videoFile = files.find((file) => {
return file.name.endsWith('.mp4') || file.name.endsWith('.mkv') || file.name.endsWith('.avi');
});
if (!videoFile) {
console.log('No video files found in the torrent.');
return;
}
console.log(`Streaming ${videoFile.name} (${videoFile.length} bytes)`);
app.get('/', (req, res) => {
// Set the content type of the response to the video MIME type
res.contentType(videoFile.name.split('.').pop());
// Get the range of bytes to stream from the request headers
const range = req.headers.range;
const parts = rangeParser(videoFile.length, range);
// Set the content range of the response to the range being sent
res.set('Content-Range', `bytes ${parts[0].start}-${parts[0].end}/${videoFile.length}`);
// Stream the file to the response
const stream = videoFile.createReadStream(parts[0]);
stream.pipe(res);
});
app.listen(3000, () => {
console.log('Server listening on port 3000.');
});
});
engine.on('download', (pieceIndex) => {
console.log(`Downloaded piece ${pieceIndex}.`);
});
engine.on('idle', () => {
console.log('Download complete.');
});
but it gave me this error:
TypeError: argument str must be a string
at rangeParser (G:\STREAMING-THING\node_modules\range-parser\index.js:29:11)
at file:///G:/STREAMING-THING/what.js:33:19
at Layer.handle [as handle_request] (G:\STREAMING-THING\node_modules\express\lib\router\layer.js:95:5)
at next (G:\STREAMING-THING\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (G:\STREAMING-THING\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (G:\STREAMING-THING\node_modules\express\lib\router\layer.js:95:5)
at G:\STREAMING-THING\node_modules\express\lib\router\index.js:284:15
at Function.process_params (G:\STREAMING-THING\node_modules\express\lib\router\index.js:346:12)
at next (G:\STREAMING-THING\node_modules\express\lib\router\index.js:280:10)
at expressInit (G:\STREAMING-THING\node_modules\express\lib\middleware\init.js:40:5)

Related

ReferenceError: File is not defined - express

I`m trying to convert some base64 string to a image file and pass it to firebase via express.
Everything works fine on front end, except this part:
const convertBase64ToFile = (base64String, fileName) => {
let arr = base64String.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let uint8Array = new Uint8Array(n);
while (n--) {
uint8Array[n] = bstr.charCodeAt(n);
}
const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
return file
}
Which library i have to import?
Error:
const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
^
ReferenceError: File is not defined
at convertBase64ToFile (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:19:16)
at conversor (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:33:16)
at C:\Users\rahto\devel\new\maissaudeapi\mainServer.js:31:21
at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:280:10)
Node.js v18.6.0
[nodemon] app crashed - waiting for file changes before starting...
Then, i changed to this:
const convertBase64ToFile = (base64String, fileName) => {
let arr = base64String.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let uint8Array = new Uint8Array(n);
while (n--) {
uint8Array[n] = bstr.charCodeAt(n);
}
const file = fs.writeFileSync(fileName, uint8Array)
let fiz = fs.readFileSync(fileName, file);
// const file = new File([uint8Array], fileName, { type: mime });
return fiz
}
And got this error:
C:\Users\rahto\devel\new\maissaudeapi\node_modules\#firebase\storage\dist\index.node.cjs.js:3036
const newPath = child(ref._location.path, childPath);
^
TypeError: Cannot read properties of undefined (reading 'path')
It seems you are using node runtime. You can use fs module to access file system.
fs.writeFile( file, data, options, callback )
Parameters: This method accept four parameters as mentioned above and described below:
file: It is a string, Buffer, URL or file description integer that denotes the path of the file where it has to be written. Using a file descriptor will make it behave similar to fs.write() method.
data: It is a string, Buffer, TypedArray or DataView that will be written to the file.
options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter:
encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’.
mode: It is an integer value that specifies the file mode. The default value is 0o666.
flag: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.
callback: It is the function that would be called when the method is executed.
err: It is an error that would be thrown if the operation fails.
Usage:
var fs = require('fs');
fs.writeFile('file.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Also check more here and here for usage.
And also check here for documentation.

Alert & Prompt in Firebase Functions (index.js)

I am trying to make a simple website with Firebase Functions.
I have to display a prompt to ask their import id.
I will then set the answer as a variable.
Then import their data with the import code
Following is that code in index.js
async function getFirestore() {
var importcode = prompt("What is the Import Code?");
const firestore_con = await admin.firestore();
const writeResult = firestore_con
.collection("Exports")
.doc(importcode)
.get()
.then((doc) => {
if (!doc.exists) {
console.log("No such document!");
} else {
return doc.data();
}
})
.catch((err) => {
console.log("Error getting document", err);
});
return writeResult;
}
and I get this in the Debug Log.
i functions: Beginning execution of "us-central1-app"
> /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:19
> var importcode = prompt("What is the Import Code?");
> ^
>
> ReferenceError: prompt is not defined
> at getFirestore (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:19:22)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/index.js:39:27
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/route.js:137:13)
> at Route.dispatch (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/route.js:112:3)
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:281:22
> at Function.process_params (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:335:12)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:275:10)
> at expressInit (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/middleware/init.js:40:5)
> at Layer.handle [as handle_request] (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/layer.js:95:5)
> at trim_prefix (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:317:13)
> at /Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:284:7
> at Function.process_params (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:335:12)
> at next (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/router/index.js:275:10)
> at query (/Users/randomkindleofficial/Desktop/monopoly-dynamic/functions/node_modules/express/lib/middleware/query.js:45:5)
I think this happens because index.js runs on the server-side, but are there any alternatives to achieve this?
PS: I only have very basic knowledge in firebase, so this may be a foolish question but still please help :)
The prompt() method can be used in a browser and not on server side i.e. in the Cloud function. I am not sure what your frontend looks like, but you should prompt the user to enter ID in your web app and then pass it in the Cloud function. A simple example using callable functions:
var importcode = prompt("What is the Import Code?");
var getData = firebase.functions().httpsCallable('getData');
getData({ code: importCode })
.then((result) => {
// Read result of the Cloud Function.
var result = result.data;
console.log(result)
});
Then you can read this code in your Cloud function like this:
exports.getData = functions.https.onCall((data, context) => {
console.log(data)
// data contains the import code
// run your Firestore query here
});

Hyperledger Sawtooth: Transaction processor in Javascript

I am trying to implement a transaction processor in javascript SDK based on the following example
https://github.com/hyperledger/sawtooth-core/blob/master/sdk/examples/intkey_javascript/index.js
Here is my code to run a transaction processor in javascript SDK
//validator public key
const validatorAddress = '024c512a6d66917d7d00f52fa299a88594915dab27bddbcd2a80154984d7948c3c';
const IntegerKeyHandler = require('./handler');
const startProcessor = function startProcessor(){
const transactionProcessor = new TransactionProcessor(validatorAddress);
transactionProcessor.addHandler(new IntegerKeyHandler())
transactionProcessor.start()
}
But i am getting invalid argument error
Error: Invalid argument
at exports.Socket.Socket.connect (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/zeromq/lib/index.js:510:13)
at Stream.connect (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/sawtooth-sdk/messaging/stream.js:85:18)
at TransactionProcessor.start (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/sawtooth-sdk/processor/index.js:72:18)
at Object.startProcessor (/var/accubits-workspace/hypeerledger-sawtooth/tuts/helpers/transaction-processor.js:15:26)
at app.get (/var/accubits-workspace/hypeerledger-sawtooth/tuts/index.js:62:26)
at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
at next (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
at /var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/index.js:281:22
Change the validator address to the url of the validation which can be either tcp://validator:4004 or tcp://localhost:4004
Here's the full code:
'use strict'
const { TransactionProcessor } = require('sawtooth-sdk/processor')
const IntegerKeyHandler = require('./integer_key_handler')
const address = 'tcp://validator:4004' // If you are not running it inside docker container then change the address to this tcp://localhost:4004
const transactionProcessor = new TransactionProcessor(address);
transactionProcessor.addHandler(new IntegerKeyHandler());
transactionProcessor.start();

Monkeypatch res.send

From a college suggestion and following this answer
I'm trying to monkey patch res.send but I get the following error:
TypeError: Cannot read property 'req' of undefined
This is my code
const express = require('express')
const app = express()
app.use((req, res, next ) => {
const oldSend = res.send;
res.send = (data) => {
console.log(data.length);
oldSend(data);
}
next();
})
app.get('/', (req, res) => res.send('Hello World!'))
Full stacktrace:
Example app listening on port 3000!
undefined
TypeError: Cannot read property 'req' of undefined
at send (/Users/code/js/hello/node_modules/express/lib/response.js:110:17)
at ServerResponse.res.send (/Users/code/js/hello/index.js:8:9)
at app.get (/Users/code/js/hello/index.js:12:32)
at Layer.handle [as handle_request] (/Users/code/js/hello/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/code/js/hello/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/code/js/hello/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/code/js/hello/node_modules/express/lib/router/layer.js:95:5)
at /Users/code/js/hello/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/code/js/hello/node_modules/express/lib/router/index.js:335:12)
at next (/Users/code/js/hello/node_modules/express/lib/router/index.js:275:10)
Where line 110 is:
res.send = function send(body) {
var chunk = body;
var encoding;
var req = this.req; //<-- THIS
var type;
// settings
var app
....
It's because methods are not tied to their instances in JavaScript.
If you call a.fun(), inside the function code, this will initially be set to a. But this happens only because a appears in the method call: fun is otherwise an arbitrary function that has no relationship with a.
In your case, this should work:
oldSend.call(res, data);
The point of call() is to set this.
Found the solution, the oldSend loses it's context(?)
If I add bind(res):
const oldSend = res.send.bind(res);
The problem is gone.

TypeError thrown when library is included in express

I've come across a very peculiar bug that I can't seem to solve. In Node.js, I have an API wrapper library called Markitable:
var request = require('request');
var Q = require('q');
/**
* Markit on Demand API Client
* #constructor
* #author Chandler Freeman <chandler.freeman#gmail.com>
*/
var Markitable = function() {
};
module.exports = Markitable;
endpoints = {
lookup: 'http://dev.markitondemand.com/Api/v2/Lookup',
quote: 'http://dev.markitondemand.com/Api/v2/Quote',
interactivechart: 'http://dev.markitondemand.com/Api/v2/InteractiveChart'
};
/**
* Company lookup
* #httpMethod GET
* #param {ticker} String A string containing the ticker for the stock
* #param {callback} callback (Optional) If callback is passed, than the function will use callbacks
* #promises yes By default, this function uses promises
*/
Markitable.prototype.lookup = function(ticker, callback) {
var options = {
url: endpoints.lookup,
headers: this.headers,
qs: { 'input': ticker.toUpperCase() },
method: 'GET',
json: true
};
if (!callback) {
var d = Q.defer();
request(options, function(err, httpResponse, body) {
if (err) return d.reject(err);
d.resolve(body);
});
return d.promise;
}
else
request(options, callback);
};
As you can see, all it does is fetch data from the Markit on Demand API and return a Q promise. However, every time I include the
var Markitable = require('markitable');
statement anywhere in my code, when I navigate to the / route of my application I receive
Failure on / route: TypeError: Cannot read property 'user' of undefined
I am using Express I have absolutely no idea at all why this is happening; I've reread the source several times, checked source control changes, everything, yet I can't find the root of the issue. This behavior only persists when the library is included; as soon as I remove that statement, everything works perfectly. I don't understand because the code for this library was the exact same code I used from another library I wrote, and the first one looks great. Here is the code for my routes file:
var User = require('../app/models/user');
var Robinhood = require('marian');
var util = require('util');
var Q = require('q');
module.exports = function(app, passport) {
// =========================================
// Table of Contents(Search by name)
//
// 1. Root
//
// =========================================
// ********************* 1. Root *********************
app.get('/', isLoggedIn, asyncCallback(function*(req, res) {
rh = new Robinhood(req.user.local.rhtoken);
var viewData = { user : req.user };
try {
// e: <Cannot read property user of undefined>
viewData.rhUser = yield rh.getUser();
viewData.rhUser.basic_info = yield rh.getBasicInfo();
viewData.rhPortfolio = yield rh.getPortfolio();
viewData.getOrders = yield rh.getOrders();
viewData.rhAccount = yield rh.getAccount();
viewData.active = { page : 'dashboard' };
res.render('main_pages/maindashboard.ejs', viewData);
}
catch(e) {
console.log("Failure on / route: " + e);
res.render('meta/500.ejs');
}
}));
};
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/login');
}
// Code originally from James Long
// http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaScript-Generators
function asyncCallback(gen) {
return function() {
return Q.async(gen).apply(null, arguments).done();
};
}
Any ideas about why this strange behavior may be occurring? I can't imagine why importing a library would affect the 'req' object, but somehow it does. Could this be a bug in Express?
EDIT:
Forgot the stack trace:
TypeError: Cannot read property 'user' of undefined
at Robinhood.getUser (/vagrant/StockFire/node_modules/marian/index.js:110:26)
at /vagrant/StockFire/app/routes.js:28:40
at next (native)
at Function.continuer (/vagrant/StockFire/node_modules/q/q.js:1278:45)
at /vagrant/StockFire/node_modules/q/q.js:1305:16
at /vagrant/StockFire/app/routes.js:226:29
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at next (/vagrant/StockFire/node_modules/express/lib/router/route.js:131:13)
at isLoggedIn (/vagrant/StockFire/app/routes.js:217:16)
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at next (/vagrant/StockFire/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/vagrant/StockFire/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at /vagrant/StockFire/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/vagrant/StockFire/node_modules/express/lib/router/index.js:330:12)
at next (/vagrant/StockFire/node_modules/express/lib/router/index.js:271:10)
Solved the issue. Simple bug, but very hard to find. I discovered that a variable named endpoints existed in both libraries, but neither was declared with var, meaning that the variable was overwritten since they both existed in the javascript global scope. Lessons learned: Always check variable scope.

Categories