Incorrect TypeScript definition for request / request-promise: option "time" is missing - node.js

On NodeJS with TypeScript and using npm request-promise (which wraps npm request).
I want to use the "time" option to time the round-trip as documented here "time - If true, the request-response cycle (including all redirects) is timed at millisecond resolution, and the result provided on the response's elapsedTime property" and which is available within the npm as evidenced here
if (options.time) {
self.timing = true
self.elapsedTime = self.elapsedTime || 0
}
But this option doesn't appear to be supported in any of the request definitions I can find.
So my first question is - am I reading things wrong?
If not, my second question is - is there a reason it's not there?
Finally, if it should be there - I can modify my local copy of the definitions but, of course, the better way is to get the repository definitions fixed and I'm just not exactly sure the best way to go about doing that, so who can point me in the right direction?

The definition for the time option is missing from the typings definition of the request package: https://github.com/louy/typed-request/blob/master/index.d.ts
The typings definition are for version 2.69 of the request package (https://github.com/typings/registry/blob/master/npm/request.json) which probably didn't have that time option
To get the repository definition fixed, you can ask the author of the type-request repository to fix it, or you can fork the main repository, do your updates and issue a pull request to the original author.
In the mean time, you can still install the definitions you updated into your project with:
typings install --save github:<YourGithubUsername>/<RepositoryName>#<CommitSha>

Related

Tensorflow-node not recognized with cocoSsd on node.js

I'm using #tensorflow-models/coco-ssd and #tensorflow/tfjs-node to do some object detection. It's working, but apparently could be faster. It's honestly not even that slow, it bangs through an image in about a second or two, but it just bugs me when something isn't working as well as it could.
You can find a live version of this at https://01014.org/wall-of-cats
Most current code at https://github.com/qozle/wall-of-cats
I get this on the first call to the model.detect():
============================
Hi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up
dramatically, install our node backend, which binds to TensorFlow C++, by running
npm i #tensorflow/tfjs-node, or npm i #tensorflow/tfjs-node-gpu if you have CUDA.
Then call require('#tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of
your program. Visit https://github.com/tensorflow/tfjs-node for more details.
============================
I'm on a linux ubuntu 20 LTS server. I tried downgrading tfjs-node, I saw some folks had a problem with the versions not matching for the faceAPI example, so I tried that.
"#tensorflow-models/coco-ssd": "^2.1.0",
"#tensorflow/tfjs-node": "^2.1.0",
I tried deleting node_modules and doing
npm install
so that it would rebuild the bindings. No beans. Tried making sure I have python installed- I'm running python3. EDIT tried making sure that I have 2.7 installed instead and that I'm using it as default. No beans.
EDIT I've also tried adding #tensorflow/tfjs-backend-cpu to the mix, and rebuilding bindings again by deleting node_modules and doing npm install. No beans.
Here's some of the code:
const tf = require("#tensorflow/tfjs-node");
const cocoSsd = require("#tensorflow-models/coco-ssd");
tf.enableProdMode();
preloading the model:
catModel = await cocoSsd.load();
Then later on, when I get some data:
const image = await tf.node.decodeImage(resp.body, 3);
const predictions = await nsfwModel.classify(image);
const catObjects = await catModel.detect(image);
image.dispose();
This is for a project that interfaces with the twitter API, pulls filtered data of all posts with images that have #cat or cat or kitten in the post, checks it against a NSFW model, and then does object detection to make sure there are cats in the pictures (I got a lot of random images and couldn't really refine the twitter API filter rules).
I'm out of beans and out of ideas.

Angular universal server error: When using the default fallback, a fallback language must be provided in the config

I have installed angular universal on my app.
Running npm run build:ssr - DONE. WORKS.
Running npm run server:ssr - DONE.WORKS.
After accessing the server URL (localhost:4000), the page is not fully loaded and the following error is raised on the Terminal:
I also faced the same problem, so I would just like to share my findings for the same.
For me, there were two plausible causes/solutions for it:
First, in my project's I18N default JSON file that was en.json, I was having a problem In the structure of the JSON file.
For example, I had the below mistake. I missed the comma after the second label 'FINISH' :
{
"COMMON": {
"EDIT": "Edit",
"FINISH": "Finish"
"QUIT": "Quit",
}
}
So after correcting the structure, the application ran fine without an error.
Secondly, another cause of the issue could be, at runtime transloco was not able to find the correct label in the selected language, so it looked for a fallback language and it could not even find that in the transloco-root.module.ts so after adding my fallback language, it tried to find the same in the fallback language as specified in the transloco-root.module.ts.
So it found out that label and the issue got resolved.
BUT in the second solution provided, you need to have that incorrect label in at least that fallback language's json file in correct format.
I added the fallback language like below:
useValue: translocoConfig({
availableLangs: ['fr', 'en'],
defaultLang: 'en',
reRenderOnLangChange: true,
fallbackLang: 'fr',
prodMode: environment.production,
missingHandler: {
logMissingKey: true
}
})
i18n Transloco wasn't fully configured on the module file.

How to set a default/fallback scope on package names with Node require()

I am using a particular module in my Node code:
const example = require('example');
However this module is slow to be updated, so I have forked it and published it with my updates under my own scope on npmjs.com. However now to use my own module, I must change every use in my code:
const example = require('#my-username/example');
The problem with this is that I will have to commit a bunch of changes throughout many files to rename the module, then when upstream merges my changes into the official version I will have to update my code again to remove the scope operator from require() across all those files, then add it back if I have more changes that are slow to be accepted, and so on.
Is there a way that I can tell Node or NPM that if require() can't find a module with no scope in the name, to then check all the #scope folders in node_modules to see if there's a match there?
If that were possible then I would only need to update package.json with the relevant package version and the code itself could remain unchanged as I switch between my fork and the official version.
you can implement it using module-alias
This will slow down your startup, but let you write all this logic for every requires you make in your application.
const moduleAlias = require('module-alias')
// Custom handler function (starting from v2.1)
moduleAlias.addAlias('request', (fromPath, request, alias) => {
console.log({
fromPath,
request,
alias,
});
return __dirname + '/my-custom-request'
})
require('request')

Resetting or overwriting the lifespan of a context in DialogFlow

I'm trying to manage the context of my Google Assistant agent (in DialogFlow), using the ApiAi class in the npm package actions-on-google.
The problem is this:
How can I reset the lifespan / delete a context using the npm package?
I can easily set the lifespan of a new context, and it works.
However:
How do I delete a context?
Setting the context to a different number does not seem to work. That is, if I set app.setContext('myContext',10) and then, 2 intents later, when the lifespan in 8, I call app.setContext('myContext',10) again, in the next intent, the lifespan is still 7. If I could answer (1) and delete a context, I'd just delete it and set it again.
I don't think there is a way to delete or overwrite the duration of a context. Instead, if you know that a certain context must not be active at a certain point, set a context that lasts for 1 or 2 turns and do this after each turn. This will also give you more control over the conversation, so you won't have contexts that last for 10 turns that you suddenly don't need anymore.
To delete a lifespan of context you just set it to ZERO or 0 like app.setContext('your_context',0)
Make sure you do this before calling the app.ask or app.tell
or
if not using client you could write a function that simply sets
this.contexts_[your_context] = {}
The first option is definitely working for me. I have not tried the second option. Try and see if you are not setting it in the Dialogflow. Also, you can remove context setting in webhook and put lifespan as 0 in Dialogflow. That will put a line (like deprecated method) over your context.
I know this question is old, at least old enough so that we now have a v2 API and library overhaul, so I'll answer anyway with today's solution :) .
1 - To delete a context, you can use conv.contexts.delete('context1'); as specified on the Node.js library reference docs.
2- If conv.contexts.set('context1', 1); doesn't change the context's lifespan then you can easily delete it and recreate it with these two calls.
My experience is that you cannot overwrite context data.
You can create a new context though:
agent.setContext({
name: contextName,
lifespan: newLifeSpan,
// Note: Parameters are not visible until the context is passed
// console.log() won't show them now.
parameters: {
// Previously saved using getContext()
param_name : paramValue,
}
});

React FacebookAuth Error: FB.login() called before FB.init()

I'm using the admin-on-rest npm package starter project and trying to plug in a simple SSO Facebook login button using the FacebookAuth npm package. Every time I try to click the "Login" button, I get the following error:
FB.login() called before FB.init()
I'm using an .env file with the following variable: REACT_APP_FACEBOOK_APP_ID and setting it to the right value. I even did console.log() within my app and can see it output.
I checked and I'm only loading the FB SDK once, not multiple times (this was a common issue reported on other threads).
Ok, it turned out to be something pretty dumb, but something to point out nonetheless!
In my .env file, I had accidentally placed a semicolon (;) at the end of the declaration, like this:
REACT_APP_FACEBOOK_APP_ID = XXXXXXXXXXXX;
Apparently .env files do NOT like semi-colons. This was just very difficult to figure out from the error above.
So if any of you want to pull your hair out because of this issue, and you're using similar tech, check to make sure you're syntactically kosher EVERYWHERE variables are being declared.
the funny thing was i forgot to replace your-app-id with my app id:
<script>
FB.init({
appId: 'your-app-id',
autoLogAppEvents: true,
xfbml: true,
version: 'v8.0'
});
</script>

Resources