Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
If all you want is "come back after other tasks are done" then just dump whatever you were working on to the bottom of the stack. Node will do everything else and come back around to it. This is not an ideal solution for your case since you are describing intense processing, which should definitely be offloaded out of your main thread.

But in any case, methods for doing so include: `setTimeout(() => { // your code }, 0);`

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

fankey posted:

Pretty simple - every time a new web socket comes in ( via require('ws').Server ) I create a TCP socket and set up handlers on both the socket and ws to shovel data between them. If an error occurs on either one I close down the other side. It all pretty much works but I'm not clear what I need to do with regards to clearing stuff out so the gc can delete the objects.

In my code I'm not storing any references to either the ws or socket - they appear to go out of scope when add() returns. At this point are they eligible for gc and could be yanked out from under me? If so, should I just store them in a collection manually? If they somehow aren't eligible for gc then I have the opposite quesiton - what do I need to do once I'm done with both connections to ensure the gc will clean up memory for me? Or do things just magically work and I don't need to worry about such things - which is hard for someone with years of C++ and C# development to come to terms with.

Everyone's first production Node project leaks memory like a sieve. JavaScript wasn't designed with long running processes in mind. It was supposed to run on a webpage that gets cleared out as soon as you click on anything. JavaScript doesn't gc oftentimes seemingly regardless of whether the object can logically be ever used. It therefore requires a great deal of care to write for Node.

In depth closures understanding will be necessary and at some point you'll have to begin explicitly setting variables to `undefined`.

Nolgthorn fucked around with this message at 02:46 on Jan 29, 2017

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

kiwid posted:

I'm looking for a library/package that allows a user to upload an image from their computer in the original huge as 12 megapixel size, auto-resize the short side of the image with proportions locked and then allow the user to crop the remaining with a locked size so in the end I get a 300px by 300px profile picture. Any suggestions?

Something like this?

https://foliotek.github.io/Croppie/

If you have a bit of JS expertise you might be best off allowing the user to crop their photo before uploading a huge file. This can be done using canvas, loading the image into it, allowing them to move it around. The image can then be extracted from the canvas and sent with a post request to the server.

You'll have to do some reading on canvas to make that happen.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I wanted to say I've had the pleasure of using `js-joda` for dates stuff in a recent project and it's so nice. Terrible name but so nice.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I've had a great experience with TypeScript, but I haven't had any trouble figuring it out from the documentation provided on the official website. Since it's a superset of ES2015+, the massive bulk of TypeScript is just existing valid JavaScript code. It adds a few features but mostly not.

In fact it replaces some things you used to need like Babel.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

IAmKale posted:

So here's my question: How can I break up and organize my code into multiple files, but get tsc to output a single JavaScript file in basic ES5?

This has become a more complicated question after TypeScript developers found out that people were using TypeScript wrong, so they fixed it and you can't really do this with TypeScript alone anymore. They would say that concatenating the files is wrong too.

The simplest and best solution:

Emit "es2015" targeted at "es5" compatibility. This will give you a folder full of your ts files, as es5 files, with es2015 import statements. Then use Rollup because Webpack is the worst thing to have ever happened. Rollup is really small, it will watch, and it will properly output a single file for you.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
When using state, you want to set an initial state in your constructor.

code:
class MyClass extends React.Component {
    constructor(props) {
        super(props);
        this.state = { photos: [...etc...] };
    }
}

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Knifegrab posted:

gently caress yes thank you for recommending postgres I am so sick of people bending over backwards for mongo in situations where it gains them literally no benefit. Postgres is also really really good in so many ways.

I've rediscovered my love for document storage after learning to use views incredibly liberally. It just returns whatever crazy object I want spread out all over my database. But for replication and less of a need for replication reasons, a SQL database is often a better option.

Wish there was a super simple basic SQL database without all the frills of MySQL and Postgres. Like sqlite but not sqlite. Also I would like something better than using blob for file attachments.

Nolgthorn fucked around with this message at 19:31 on Sep 8, 2017

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I've been hearing rumblings that REST is dead. That the concept in practice doesn't work very well, that no consistency exists between APIs and that it's basically a crap shoot where the only value derived from it are descriptive urls. But those urls could be replaced easily by descriptive names, in fact names could be those urls or something even more suitable.

Also a point of aggression against REST is that no protocol supports it at all except HTTP and then even methods DELETE and so on are inconsistently implemented. If you have a REST api and want a Websockets version it is flat out impossible.

People are saying a single API endpoint which accepts json GET or POST is the future.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
But I get it. These transmission frameworks are too complicated, in a perfect world I'd be able to simply run a function on a different server. The closest we can come to something so simple is a single parameter in the shape of an object.

Or, even, an array of parameters.

A single endpoint where I can just send something like

code:
{
  "transportId": 2,
  "type": "post:create",
  "content": "hi from the other server"
}
I sorta agree, there is no reason it shouldn't be about that easy. Where transport ids are sent back when the server finishes doing something.

code:
{
  "transportId": 2,
  "type": "post:created",
  "id": 209
}
An api like this could easily be translated into almost any transport protocol.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Thinking more in the realm of new apis not in upgrading old ones.

Error codes too, it seems like they aren't implemented in any kind of uniform or expected way. You can try and GET a resource that's behind authentication and if you're not authenticated you can expect any of a huge number of possible error codes. I understand there is some agreement about what you should receive but I think it'd be a lot simpler if we all admitted that it is on a per API basis, realistically. Some will return 404 some will return something else.

I'd prefer something like.

code:
{
  "type": "error",
  "error": {
    "name": "restricted",
    "message": "Resource is inaccessible."
  }
}
Where the software I'm interacting with just returns whatever error it has and I as an api developer don't need to translate the error so it matches some kind of server communication holy text. I fully understand that I sound pretty ignorant to a lot of people.

I've been building APIs for a while. I know they're not changing soon. But I also am getting disillusioned.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

ROFLburger posted:

That's more an implementation issue and not necessarily a shortcoming of REST though, right? I mean there's definitely a standard for what should be returned in that scenario

It seems to depend who you ask.

https://docs.microsoft.com/en-us/rest/api/storageservices/common-rest-api-error-codes

https://developer.springcm.com/guides/rest-api-response-and-error-codes

https://www.fusioo.com/guide/api-status-error-codes

https://actionstep.atlassian.net/wiki/spaces/API/pages/13140251/Error+Codes


Best resource I found looked like a flow chart. I think only communication errors should return a error code, if the software is communicating correctly then just let me communicate with the software and leave the code book out of it.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I stopped making web apps long ago, I just set up a user facing phpmyadmin and it's practically the same thing.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I think Typescript is the best thing to happen to Javascript since ES2015 but lately they've been letting me down with the part of it that really mattered to me which was client side compilation. It can compile to ES5 but it cannot compile to a single file.

It used to be able to. That functionality has languished and now you need a second compilation step in order to get the code where it needs to be.

I was using Rollup to do that which was annoying but still manageable. Now that I see support for Vue, or just about anything which isn't Angular falling by the wayside I've had to stop using Typescript. Which is such a shame because it was one of my favourite tools for a long time.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Still no ES6 alternative for `pick`... ?

code:
function pick (obj, ...keys) {
    const result = {};
    for (const key of keys) {
        result[key] = obj[key];
    }
    return result;
}

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Skandranon posted:

How is client side compilation a deal breaker? There are a ton of industry standard tools for bundling files, TypeScript shouldn't be trying to do their own.

Because Typescript is a compiler. You only need another compiler if you wanna do client side compilation, something it used to do and used to do a lot better than Browserify or Webpack. "Industry standard" doesn't generally describe technologies that are only a couple of years old, as well.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

gmq posted:

Apparently CoffeeScript refused to die honorably.

Does this work as an alternative to `decaffeinate`, so that I can get rid of all this coffeescript?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
That worked thanks.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
How is { [key: string]: any } different from { [giraffe: string]: any }?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Anecdotally, yarn didn't work and I had to force install all of my packages more often than it worked at my last job.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
What are the best places to host your basic Nodejs app these days? I have been using Bluemix which is great but I'm starting to get a small stack of apps running there and it's becoming no longer cheap, they seem to have disabled "auto sleep" for my less important apps so I'm always racking up gigabyte hours too.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Yes I need to use my own custom domains, I need SSL, I also have a project coming up that needs to host a (incoming) mail server. I don't know much about the details of receiving mail at a domain if there are special requirements for that.

Are there smaller players than Microsoft, Heroku, and Google?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Cool I'll try it out!

I don't need my own mail server per say I just need to to be able to receive and read emails, no sending emails, no storing emails, nothing like that. Basically this.

https://github.com/Flolagale/mailin

Nolgthorn fucked around with this message at 17:23 on Sep 30, 2017

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Now looks frighteningly competent. All I need is a regular package.json and it'll deploy my app... I can add domains and it automatically gets decorated with ssl and whoisguard for free. I can generate as many deployments as I want.

It's too bad that the free tier open sources your code. That means I can't really try them without using a test app, instead of my actual app.

Something is fishy about the competency.

What if I manage my own domains ssl certs and whoisguard elsewhere because I'm a tinfoil hatter and want to spend more.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Running a bunch of nodes is pretty common.

It's supposed to be, you can have one for each api concern. Docker is big because of the demand for microservices, something that lets me run a bunch of small stuff would make me feel more future proof.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
code:
    var a = password();
    var b = parseAscii(a);
    var c = parseBin(b);
    var d = split(c);

    forEach(d);
You want something like this. To avoid hoisting and save you some sanity, plz use `let` and `const` instead of `var`. Super easier.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Man these classes using old javascript make me feel like Javascript is the new PHP.

With all of these alternate old ways of doing things, that are still supported, that you're not supposed to use, that are taught in classes. I can see learning programming in school to start, but if I had taken a class for everything I wanted to know I'd have never made it anywhere.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
code:
    // This is a very obscure and totally unused feature built in to web browsers that displays a dialog box
  // that allows the user to enter input ([url]https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt[/url]).
  // It will (for some reason) display a 'C' in the dialog box as the default input because of the
  // variable above (intDefLetter).
  intNewLetter = prompt(intQuestion, intDefLetter);

    // This checks whether the user has entered more than one letter in the dialog box mentioned above,
  // and if they did then it opens the same dialog box again.
    if (intNewLetter.length > 1) {
  	prompt(intQuestion, intDefLetter);
  }
What if I enter more than one character 2 times? Also, the second prompt doesn't store the value so it doesn't matter at all what I type.

code:
let newLetter = '';

while (newLetter.length !== 1) {
  newLetter = prompt(question, 'C');
}

Nolgthorn fucked around with this message at 17:48 on Oct 8, 2017

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Dude not even God himself understands bitwise operations.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
The Unity3D game engine used to be a modified version of javascript. But, you can use it for all kinds of cool stuff due to the callstack and singlethreadedness.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Once you get it working move the `throw 'buttes'` into the catch block since you know it'll never get thrown again anyway.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
Mobile safari is very unpredictable. You are defining the city variable twice, you only need to define it the first time.

code:
function getCity() {
    var cityurl = document.referrer;
    var citystart = cityurl.search("city=");
    var city = "0";
    if (citystart > 0) {
        city = cityurl.substring(citystart + 5);
    }
    var link = "(a website url)?City=" + city;
    window.open(link);
}
Not sure that will fix it.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
https://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889011/Using-Safari-for-iPhone-iPad-and-iPod-Touch-Website-Testing.htm

Ooh.

Safari -> Preferences -> Advanced -> Show developer menu -> Right click -> User Agent -> Select any safari you want.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Knifegrab posted:

I quite think the solution is elegant, and flexible and half my coworkers agree but the other half are screeching.

Thoughts?

It is elegant now. But it's also incredibly annoying that any time you want new behaviour from a form field, or a new type of form field, you have to go in and hack someone's API. For example what if you wanted a specific form field that had a specialised popover that's only relevant to one page of the application? Hopefully that is happening fairly often.

With your solution that is a big pain, because you have re-written HTML.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
In my experience in general it's a good idea to leverage existing tools, especially at the lowest levels such as HTML CSS and JavaScript. I see way too often these days new solutions which completely replace those base toolsets. It forces developers then to have to choose between the in house version or another custom thing.

Or combine them.

My solution would be more along the lines of something that eliminates duplicated code, but not a replacement for basic tooling. For example, a function that takes a reference to a form and a set of validation criteria. Or a function that takes a reference to a form and returns the data from that form as you like it.

Doesn't your answer to my question place that new field outside the `form` element?

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

huhu posted:

What am I missing here? In react I have an image with two events...
code:
onMouseEnter={() => this.onHover(true)}
onMouseLeave={() => this.onHover(false)}
and onHover is...

code:
  onHover = (isHovered) => {
    console.log('setting state', isHovered)
    this.setState({ isTileHovered: isHovered });
  }
When I enter the image I get "setting state true", "setting state false" then when I mouse out, I get "setting state false" again.

I remember solving a similar problem by instantiating the events.

code:
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
That way react knows that the attribute didn't change, otherwise it has to de-instantiate and re-instantiate the event on every update which causes weird behaviour.

e- something like...

code:
class MyComponent extends React.Component {
  constructor() {
    super();
    this.state = { isTileHovered: false };
    this.onMouseEnter = this.onMouseEnter.bind(this);
    this.onMouseLeave = this.onMouseLeave.bind(this);
  }

  onMouseEnter() {
    this.setState({ isTileHovered: true });
  }

  onMouseLeave() {
    this.setState({ isTileHovered: false });
  }
}

Nolgthorn fucked around with this message at 20:34 on Dec 22, 2017

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Shrimpy posted:

That's my intention, though part of this will require DOM manipulation and checking browser compatibility, so I'm kind of stuck using a browser or something browser-like at some point in the process.

Testing in multiple browsers is typically slow and also difficult. How many legacy browsers are you supporting? I'd suggest having them run on Jenkins. But, blah. Really I'd rather figure out what parts of the code are browser-sensitive and fix them.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
It's better to bind a function than it is to create a new function on every execution. Especially in React especially on event declarations. React has a couple of failings in this way, Vue you don't have to do bindings like that.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I'm interested in capturing and parsing incoming email. For the time being I'm using a 3rd party service but it would be really nice if I could just very simply receive and parse email myself. With a DNS MX record set up, how difficult is it in a node app to capture an email?

Once I have captured the email, is it difficult to take it apart? I found this module, which may do what I want however it doesn't talk about how to get the email to begin with.

https://github.com/nodemailer/mailparser

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
The same authors offer a SMTP server implementation. oops

https://nodemailer.com/extras/smtp-server/

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply