Urgent.News

What's breaking now, across thousands of outlets.

Tech

Your CSP blocks eval. So I wrote a parser.

I needed a graphing calculator that plots sin(2x) + x^2 while you type. The obvious implementation is one line: const y = eval ( expression . replace ( / x / g , String ( x ) ) ) ; It is also the one line that cannot ship, because the page sends this header: Content - Security - Policy : script - src 'self' ; object - src 'none' ; No 'unsafe-eval' , so eval and new Function throw. That is not an…

The author wanted a graphing calculator that could plot expressions like sin(2x) + x^2 while the user typed them in. The obvious solution was to create a one-line function using eval: const y = eval(expression.replace(/x/g, String(x))); However, this approach was not viable due to the Content-Security-Policy headers that disallow script-src self and object-src none, thereby preventing eval from executing. The author needed to implement a proper expression parser instead, which proved to be a challenging task.

The parser consists of four stages: tokenization, conversion to Reverse Polish Notation (RPN), and evaluation. The first stage, tokenization, involves breaking down the input string into individual tokens such as numbers, identifiers, operators, and parentheses. The author created utility functions isDigit and isAlpha to identify digits and alphabetic characters in the input string. The tokenize function iterates through the input character by character, building up tokens based on the identified characters and operators.

The second stage, conversion to RPN, involves transforming the token list into a reverse polish notation queue. RPN is a mathematical notation in which operators follow their operands, removing the need for parentheses. This stage enables the expression to be evaluated more easily, as the order of operations is already determined.

The third stage is the evaluation of the RPN queue, which involves performing the mathematical operations in the correct order. The author aimed to make this process straightforward, as the primary challenge was determining the meaning of expressions like -3^2 and whether 2x should be treated as multiplication.

In summary, the author wrote an expression parser to circumvent the limitations imposed by the page's Content-Security-Policy headers and create a functional graphing calculator that could plot complex mathematical expressions as the user typed them in. The parser consisted of four stages: tokenization, conversion to RPN, and evaluation.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Cómo saber si un proceso mejoró de verdad (y qué indicadores lo demuestran)

Un proceso mejoró si el mismo indicador, medido igual antes y después, se movió en la dirección que buscabas y se sostiene varias semanas sin que nadie esté empujando.

  • Process improvement measured by stable indicator after change
  • Key indicators: process time, error rate, manual steps, cost per transaction
  • Baseline established using existing records to compare improvements

Inside the AWS Default VPC (and Why You'll Want Your Own)

Introduction: A guided tour of the default VPC that comes with every AWS account: subnets, route table, internet gateway, NACL and security group. Then how to create your own VPC.

  • Default AWS VPC includes subnets, route table, internet gateway, NACLs, and security groups.
  • Default VPC IPv4 CIDR block is 172.31.0.0/16, providing 65,536 private IP addresses.
  • Default VPC has three subnets in different Availability Zones, each with public IP addresses.

"infinite recursion detected in policy for relation" in a Supabase app

You added sharing, or teams, or an admin role. Now a page that worked yesterday loads nothing at all, and the console shows this: infinite recursion detected in policy for relation "profiles" with the…

  • Infinite recursion error in Supabase app's RLS policy
  • Subquery referencing 'profiles' table caused issue
  • Security definer function resolved policy errors

More from Sunday 15 November →