Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@warpjs/warp

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@warpjs/warp - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

33

package.json
{
"name": "@warpjs/warp",
"version": "2.0.0",
"version": "2.0.1",
"author": "ScaleDynamics SAS",

@@ -9,15 +9,26 @@ "license": "MIT",

"type": "git",
"url": "https://github.com/ScaleDynamics/warp.git"
"url": "https://github.com/WarpJS/warp.git"
},
"description": "Warp is an API to run JavaScript functions on server.",
"description": "WarpJS is a serverless platform with front-end integration capabilities. You focus on features with one JavaSscript fullStack codebase, WarpJS deals with the back-end for you: communication, routing, API, versioning, error management, deployment, scaling and more. Check-out warpjs.com to understand how you can develop new features faster than ever.",
"keywords": [
"warp",
"warpjs",
"server",
"cloud",
"front-end",
"back-end",
"integration",
"node",
"proxy",
"bff",
"deployment",
"scale",
"gcp",
"aws",
"azure",
"serverless",
"lambda",
"functions"
"functions",
"lambda"
],
"bugs": {
"url": "https://github.com/ScaleDynamics/warp/issues"
"url": "https://github.com/WarpJS/warp/issues"
},

@@ -27,8 +38,8 @@ "homepage": "https://warpjs.com/",

"build": {
"date": "2020-01-07T11:25:55.293Z",
"date": "2020-02-13T09:40:48.902Z",
"user": "2834254",
"host": "shared-runners-manager-6.gitlab.com",
"sha": "b85ca415841c5d5d0e89a9341c26569a45d90215",
"id": "107509511"
"host": "shared-runners-manager-5.gitlab.com",
"sha": "32a9aff3616c942727571d74e06653e6190a6189",
"id": "117534634"
}
}

@@ -1,241 +0,19 @@

[![Warp Logo](https://raw.githubusercontent.com/ScaleDynamics/warp/b85ca415841c5d5d0e89a9341c26569a45d90215/doc/images/warpjs-black-colors-baseline.png)](https://warpjs.com)
[![Warp Logo](https://raw.githubusercontent.com/WarpJS/warp/32a9aff3616c942727571d74e06653e6190a6189/doc/images/warpjs-logo.png)](https://warpjs.com)
# Warp
# WarpJS/warp
_Warp_ is an API allowing a JavaScript program to easily execute some of its code on servers.
> WarpJS serverless is currently in free private beta. If you would like to test WarpJS, you need to know someone who is in the program to get an invitation link or to [request an invitation on Warpjs.com](https://warpjs.com)
```javascript
// runs `fnct` on a server
warper.callWithCallback(fnct, <args>, cb);
```
WarpJS is a function-as-a-service platform providing a unique backend/frontend integration feature: no backend project, declare your backend functions in the frontend project. From development to deployment, the workflow is streamlined to build and get a web application online, in minutes.
## Installing
It runs on top of serverless providers you can select from when you put your application online: AWS lambda, GCP functions or Azure functions.
_Warp_ module is an API and is not providing support for execution. You will need
to acquire a [**Warp runtime**](https://redirect.scaledynamics.io/warp/runtime) to use it.
Here is a frontend project sample with a “warped” backend function requesting a MongoDB resource:
To add _Warp_ API to your project:
![Sample](https://warpjs.dev/docs/assets/mongo-db-warpjs-serverless-sample-with-bg.gif)
```
$ npm install @warpjs/warp
```
## Resources
## How to use it
_Warp_ offers two main functions to start a [_warp_ task](#warp-task): `warper.call` and `warper.callWithCallback`.
We say that they perform a _warp_ call. By extension, we also say that code
_warps_ a function or that the function is _warped_.
Here is a very simple illustrative code sample where a CPU intensive function (`computePrimeFactorization`)
is executed on a server by a _warp_ call keeping the event loop free for any other processing. If you
want to see complete code, browse the [repository](https://github.com/ScaleDynamics/warp/tree/b85ca415841c5d5d0e89a9341c26569a45d90215/example).
The first example uses a Promise-based function, whereas the second uses a callback-based function.
### With promise
``` javascript
const warper = require('@warpjs/warp').defaultWarper;
function computePrimeFactorization(n) {
// Compute `f`
return f;
}
let a = await warper.call(computePrimeFactorization, 9007199254740991);
console.log(`Prime factors of ${a.value} are ${a.factors}`);
```
Prints out
> Prime factors of 9007199254740991 are 6361,69431,20394401
### With callback
``` javascript
const warper = require('@warpjs/warp').defaultWarper;
function computePrimeFactorization(n, done) {
// Compute `f`
done(f);
}
warper.callWithCallback(computePrimeFactorization, 9007199254740991,
(a) => console.log(`Prime factors of ${a.value} are ${a.factors}`));
```
Prints out
> Prime factors of 9007199254740991 are 6361,69431,20394401
## How it works
When you _warp_ call a function, the [execution context](#execution-context) (i.e. everything
reachable from the function scope) is captured, then this context and the _warped_ function are
sent to a server for execution.
Once the [_warp_ task](#warp-task) is finished, the server [context](#execution-context) is
captured and sent back to the client. Then,
in an event task of the client, the context is updated, and:
- If the _warp_ call is `call`, the Promise returned by `call` is resolved or
rejected with the value or the exception obtained at the end of the server [_warp_ task](#warp-task).
- If the _warp_ call is `callWithCallback`, the callback is executed in the main thread, with the arguments
given to the callback on the server.
### Scope
The _warped_ functions can read and modify anything reachable from their
[scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope).
In the following sample, the variable `o` is defined as a free variable (outside of the function).
The _warped_ function can read it, and also modify the object. Modifications made to `o` on the server are
applied and visible on the client.
``` javascript
const warper = require('@warpjs/warp').defaultWarper;
let o = {};
function computePrimeFactorization(n) {
// Compute `f`
o.result = f;
}
await warper.call(computePrimeFactorization, 9007199254740991);
console.log(`Prime factors of ${o.value} are ${o.factors}`);
```
Prints out
> Prime factors of 9007199254740991 are 6361,69431,20394401
### Shared resources and concurrency
_Warp_ offers no new synchronization or semaphore mechanism. If you need to manage concurrent access
to a value, you'll have to keep the modifying code on the client.
In the following example, we compute several prime factorizations in parallel.
``` javascript
const warper = require('@warpjs/warp').defaultWarper;
let primeFactorizations = [];
function computePrimeFactorization(index, n) {
// Compute `f`
primeFactorizations[index] = f;
}
let promises = []
for(let i = 0; i < 8; i++) {
promises.push(warper.call(computePrimeFactorization, i, 9007174991 + i));
}
await Promise.all(promises);
console.log(`Prime factorizations: %j`, primeFactorizations);
```
Prints out
> Prime factorizations: [{"value":9007174991,"factors":[17,131,4044533]},{"value":9007174997,"factors":{"0":59,"1":109,"2":1400587,"value":9007174993,"factors":[12071,746183]}},{"0":59,"1":109,"2":1400587,"value":9007174993,"factors":[12071,746183]},{"0":2,"1":2,"2":2251793749,"value":9007174994,"factors":[2,37,53,2296577]},{"value":9007174995,"factors":[3,5,7,13,41,227,709]},{"value":9007174997,"factors":{"0":59,"1":109,"2":1400587,"value":9007174993,"factors":[12071,746183]}},{"value":9007174997,"factors":{"0":59,"1":109,"2":1400587,"value":9007174993,"factors":[12071,746183]}},{"value":9007174998,"factors":[2,3,3,3,19,43,204161]}]
But you need to be careful about parallel access to shared variables or properties.
For instance, the following example shows bad usage of a shared value causing indetermism. There, you
don't know in which order the additions will be made, but it is guaranteed that each `fnct` sees
the initial value of `v` as `4`. This means you don't know what the final value of `v` will be, you
just know it will be either `7` or `8`.
``` javascript
const warper = require('warp').defaultWarper;
let v = 4;
function fnct(num) {
let inc = num;
v += inc;
return;
}
let promises = []
promises.push(warper.call(fnct, 3));
promises.push(warper.call(fnct, 4));
await Promise.all(promises);
console.log(`Value: ${v}`);
```
To obtain the correct behavior in this example, you'll need to apply the changes to the shared
variable on the client by using the `.then` of the promise.
``` javascript
const warper = require('warp').defaultWarper;
let v = 4;
function fnct(num) {
let inc = num;
return inc;
}
let promises = []
promises.push(warper.call(fnct, 3).then((inc) => v += inc));
promises.push(warper.call(fnct, 4).then((inc) => v += inc));
await Promise.all(promises);
console.log(`Value: ${v}`);
```
Prints out
> Value: 11
This way it'll end up with `11` as the final value.
# Definitions
## <a name="warp-task"></a>Warp task
A **warp task** is started by a _warp_ call (a call to `warper.call` or `warper.callWithCallback`).
The task is executed on a server. It starts by executing the given
function and continues until the end of the task is detected.
The end of the task is detected when:
- an exception is thrown by the task
**or**
- If the task was started by `warper.call`:
- the function given to `call` has returned
- **And**, if its returned value is a Promise, it is
[fulfilled or rejected](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Description).
- If the task was started by `warper.callWithCallback`
- The callback is called,
- **And** the function given to `callWithCallback` has returned.
Note that this doesn't mean that `return` was executed, it means that the execution has
returned to its caller. For example in the following code the end is detected during the line
`a = await 3` when `await` yields and `a` is still `2`.
``` javascript
let a = 0;
async function fnct(done) {
a = 1;
done();
a = 2;
a = await 3; // End is here at `await` before assignation of `a`
return;
}
```
**Warning:** If the task were to attempt to execute code after the end of execution were detected
(e.g. with a `setImmediate`, `setTimeout`, ...), then the behavior would be unspecified. This
means that if the function given to `callWithCallback` is starting any asynchronous processing, calling
the `callback` must be the very last thing done by the task.
## <a name="execution-context"></a>Execution context
An **execution context** is referring to everything accessible from the
[scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope) of the considered
function at a given point of the execution. This includes
- parameters of the call,
- local, free and global variables accessed by the function called or by all functions potentially
called directly or indirectly,
- any reachable objects (objects, arrays, functions, symbols) from the above defined parameters/variables.
- [Getting Started](https://warpjs.dev/docs/getting-started/)
- [Documentation](https://warpjs.dev/docs/api/index/)
- [Code samples on GitHub](https://github.com/WarpJS/samples)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc