# Notes on Solidity in Depth

### Layout of a Solidity Source File

#### Importing other Source File

In the above, filename is always treated as a path with / as directory seperator.

All path names are treated as absolute paths unless thay start with . or ...

##### Remapping

For example, remap github.com/ethereum/dapp-bin/library to /usr/local/dapp-bin/library

solc

Single Line Comments: //

Multi-line Comments: /* ... */

Natspec Comments: /// or /** ... */

Natspec should be used directly above function declarations or statements.

### Structure of Contract

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Struct Types and Enum Types.

#### State Variables

State variables are values which are permanently stored in contract storage.

#### Functions

Functions are executable units of code within a contract.

#### Function Modifiers

Function modifiers can be used to amend the semantics of functions in a declarative way.

#### Events

Events are convenience interfaces with the EVM logging facilities.

#### Struct Types

Structs are custom defined types that can group several variables.

#### Types

• bool: true or false

• ! logical negation

• && logical and

• || logical or

• == logical equality

• != logical inequality

• int/uint: uint8 to uint256, int8 to int256

• <=, >=, ==, !=, <, >

• &, |, ^, ~, bit operators

• +, -, *, /, %, **, <<, >>

• address: holds a 20 bytes value(size of an Ethereum address)

• <=, =>, ==, !=, <, >

Starting with version 0.5.0 contracts do not derive from the address type, but can still be explicitly converted to address.

• balance

• transfer, address.transfer(value), send value Wei to the address.

• send: Send is the low-level counterpart of transfer. If the execution fails, the current contract will not stop with an exception, but send will return false

There are some dangers in using send: The transfer fails if the call stack depth is 1024 and it also fails if the recipient runs out of gas. So in order to make safe Ether transfers, always check the return value of send, use transfer or even better.

• call, callcode, and delegatecall

Furthermore, to interface with contracts that do not adhere to the ABI, the function call is provided which takes an arbitrary number of arguments of any type. The arguments are padding to 32 bytes and concatenated. One exception is the case where the first argument is encoded to exactly four bytes. In this case, it is not padding to allow the use of function signatures here.

call returns a boolean indicating whether the invoked function terminated (true) or caused an EVM exception (false). It is not possible to access the actual data returned.

It is possible to adjust the supplied gas with the .gas() modifier:

and supplied Ether value can be controlled too:

Chained

In a similar way, the function delegatecall can be used: the difference is that only the code of the given address is used, all other aspects(storage, balance, …) are taken from the current contract. The purpose of delegatecall is to use library code which is stored in another contract. The user has to ensure that the layout of storage in both contracts is suitable for delegatecall to be used. Prior to homestead, only a limited variant called callcode was available that did not provide access to the original msg.sender and msg.value.

All three functions call, delegatecall, and callcode are very low-level functions and should only be used as a last resort as they break the type-safety of Solidity.

The .gas() option is available on all three methods, while the .value() option is not supported for delegatecall.

All Contracts inherit the members of address, so it is possible to query the balance of the current contract using this.balance.

The use of callcode is discouraged and will be removed in the future.

#### Fixed-size byte array

• .length yields the fixed length of the byte array(read-only)

It is possible to use an array of bytes as byte[], but it is wasting a lot of space, 21 bytes every element, to be exact, when passing in calls. It is better to use bytes.

#### Dynamically-sized byte array

bytes: dynamically-sized array, see Array, not a value-type.

string: dynamically-sized UTF-8-encoded string, see Array, not a value-type.

Hexadecimal literals that pass the address checksum test, for example 0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF are of address type. Hexadecimal literals that are between 39 and 41 digits long and do not pass the checksum test produce a warning and are treated as regular rational number literals

#### Rational and Integer Literals

Integer literal are formed form a sequence of numbers in the range 0-9. They are interpreted as decimals, for example, 69 means sixty nine. Octal literals do not exist in Solidity and leading zeros are invalid.

Decimal fraction literals are formed by a . with at least one number on one side(1., .2, 1.3)

Scientific notation is also supported, where the base can have fractions, white the exponent cannot(2e10, -2e10, 2e-10, 2.5e10)

Number literal expressions retains arbitrary precision until they are converted to a non-literal type.

For example (2**800 + 1) - 2**800 result in the constant 1, .5 * 8 results in the integer 4

Number literal expressions are converted into a non-literal type as soon as they are used with non-literal expressions. Even though we know that the value of the expression assigned to b in the following example evaluates to an integer, but the partial expression 2.5 + a does not type check so the code does not compile.

#### String Literals

String literals are written with either double or sinlge-quotes. They do not imply trailing zeros as in C, "foo" represents three bytes not four.

String Literal supports escape characters, such as \n, \xNN and \uNNNN.

hex"001122FF"

#### Enums

Enums are one way to create a user-defined type in Solidity. They are explicitly convertable to and from all integer types but implicit conversion is not allowed. The explicit conversions check the value ranges at runtime, and a failure causes an exception.

#### Function Types

Function types are notated as follows:

In contrast to the parameter types, the return types cannot be empty - if the function type should not return anything, the whole returns (<return type>) part has to be omitted.

Public (or external) functions have a special member called selector, which returns the ABI function selector:

#### Reference Types

Complex types, i.e. types which do not always fit into 256 bits have to be handled more carefully than than the value-types we have already seen. Since copying them can be quite expensive, we have to think about whether we want them to be store in memory(which is not persisting) or storage(where the state variables are held).

#### Data location

Every complex type, i.e. arrays and structs has an additional annotation, the “data location” about where it is stored in memory or in storage. Depending on the context, there is always a default, but it can be overrideen by appeding either storage or memory to the type. The default for function parameters(including return parameters) is memory the default for local variable is storage and the location is forced to storage for state variables

There is also a third data location, calldata, which is a non-modificable, non-persistent area where function arguments are stored. Function parameter(not return parameters) of external functions are forced to calldata and behave mostly like memory.

Data locations are imortant because they change how assignment behave: assignments between storage and memory and also to a state variable always create an independent copy. Assignments to local storage variables only assign a reference though, and this reference always points to the state variable even if the latter is changed in the meantime. On the other hand, assignments from a memory stoted reference type to another memory-stored reference type do not create a copy.

##### Summary
• Forced data location:

• parameters(not return) of external functions: calldata

• state variables: storage

• Default data location:

• parameters(also return) of functions: memory

• all other local variables: storage

#### Arrays

An array of fixed size k and element type T is written as T[k]

An array of dynamic size as T[]

An array of 5 dynamic arrays of uint is uint[][5]. (note that the notation is reversed when compared to some other language). To access the second uint in the third dynamic array, you use x[2][1]

Variables of type bytes and string are special arrays. A bytes is similar to byte[], but it is packed tightly in calldata. string is equal to bytes but does not allow length or index access(for now).

So bytes should always be preferred over byte[] because it is cheaper.

#### Structs

Solidity provides a way to define new types in the form of structs, which is shown in the following example:

It is not possible for a struct to contain a member of its own type, although the struct itself can be the value type of a mapping member. This restriction is necessary, as the size of the struct has to be finite.

#### Mappings

Mapping types are declared as mapping(_keyType => _valueType). Here _keyType can be almost any type except for a mapping, a dynamically sized array, a contract, an enum, and a struct. _valueType can actually be any type, including mappings.

Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value.

Mappings are only allowed for state variables( or as storage reference types in internal function).

It is possible to mark mapping public and have Solidity create a getter. The _keyType will become a required parameter for the getter and it will return _valueType.

Mappings are not iterable, but it is possible to implement a data structure on top of them.

#### Operators Involving LValues

LValue, i.e. a variable or something that can be assigned to.

##### delete

delete a assigns the initial value for the type to a. I.e., for integers, it is equivalent to a = 0.

delete has no effect on whole mappints. So if you delete a struct, it will reset all members that are not mappings and also recurse into the memebers unless they are mappings. However, individual keys and what they map to can be deleted.

It’s really important to note that delete a really behaves like an assignment to a, it stores a new object in a.

#### Conversions between Elementary Types.

##### Implicit Conversions

In general, an implicit conversion between value-types is possible if it makes sense semantically and no information is lost: uint8 is convertible to uint16, but uint16 is not convertible to uint8. And int8 is not convertible to uint256(because uint256 cannot hold hold e.g. -1).

##### Explicit Conversions

If the compiler does not allow implicit conversion but you know what you are doing, an explicit type conversion is sometimes possible.

#### Type Deduction

For convenience, it is not always necessary to explicitly specify the type of a variable, the compiler automatically infers it from the type of the first expression that is assigned to the variable:

### Units and Globally Available Variables

#### Ether Units

A literal number can take a suffix of wei, finney, szabo, or ether to convert between the subdenominations of Ether, where Ether currency numbers without a postfix are assumed to be Wei.

2 ether === 2000 finney evaluates to true

#### Time Units

Suffixes like seconds, minutes, hours, days, weeks and years after literal numbers can be used to convert between units of time where seconds are the base unit.

Take care if you perform calendar calculations using these units, because not every year equals 365 days and not even every day has 24 hours because of leap seconds. Due to the fact that leap seconds cannot be predicted, an exact calendar library has to be updated by an external oracle.

Other Global Variables and Functions:

Post

# Solidity CheatSheet

### Global Variables

#### Block

• block.difficulty: uint - current block difficulty

• block.gaslimit: uint - current block gaslimit

• block.number: uint - current block number

• block.timestamp: uint - current block timestamp

#### Msg

• msg.data: bytes - complete calldata

• msg.gas: uint - remaining gas

• msg.sender: address - sender of the message(current call)

• msg.value: uint - number of wei sent with the message

#### Now

• now: uint - current block timestamp(alias for block.timestamp)

#### Tx

• tx.gasprice: uint - gas price of the transaction

• tx.origin: address - sender of the transaction (full call chain)

### Global Functions

• assert(bool condition): abort execution and revert state changes if condition is false(use for internal error)

• require(bool condition): abort execution and revert state changes if condition is false(use for malformed input or error in external component)

• revert(): abort execution and revert state changes

• block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most recent blocks

• sha256(…) returns (bytes32): compute the SHA-256 hash of the (tightly packed) arguments

• keccak256(…) returns (bytes32): compute the Ethereum-SHA-3(Keccak-256) hash of the (tightly packed) arguments

• sha3(…) returns (bytes32): alias for keccak256

• ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): recover address associated with the public key from elliptic curve signature

• addmod(uint x, uint y, uint k) returns (uint): compute (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2 ** 256

• mulmod(uint x, uint y, uint k) returns (uint): compute (x * y) % k where the addition is performed with arbitrary precision and does not wrap around 2 ** 256

• selfdestruct(address recipient): destroy the current contract, sending its funds to the given address

• suicide(address recipient): alias for selfdestruct

• .balance: uint256: balance of the Address in Wei

• .send(uint256 amount) returns (bool): send given amount of Wei to Address, returns false on failure

• .transfer(uint256 amount): send given amount of Wei to Address, throws on failure.

### Scope Variables

• this: current contract’s type - the current contract, explicitly convertable to address

• super: the contract one level higher in the inheritance hierarchy

### Function Visibility Specifier

• public: visible externally and internally(creates accessor function for storage/state variables)

• private: only visible in the current contract

• external: only visible externally(only for functions) - i.e. can only be message-called)

• internal: only visible internally

### Modifiers

• pure: for function - disallows modification or access of state - this is not enforced yet

• view: for function - disallows modifiction of state - this is not enforced yet

• payable: for function - allows them to receive Ether together with a call

• constant: for state variable - disallow assignment(except initialization), does not occupy storage slot

• contant: for function - same as view

• anonymous: for events - does not store event signature as topic

• indexed: for event parameters - store the parameter as topic

# HTTP/2 by Node.js

Original

Websites delivered using HTTP/2 enjoys a wide range of new features including -

• fully multiplexed connections: all requests and responses to a domain are fully multiplexed via a single connection, making best use of available bandwidth.

• header compression: repeated headers are compressed with HPACK compression so that they are not resent with every request and response.

• PUSH: resources can be pre-emptively pushed by the server to the client, speeding up page load times.

Node.js just launched support(v8.8.1) for HTTP/2 as part of their core. In this post, we will create a simple HTTP/2 server to serve static files and then demonstrate some cool features like HTTP/2 PUSH.

### Get an SSL certificate

Even though the HTTP/2 spec does not mandate HTTPS, browsers have decided that they will only support HTTP/2 on a HTTPS connection. This would also mitigate interference from older proxies which may not understand newer protocol.

For your local server, a self-signed certificate will work. You can find how to setup a self-signed certificate here

### Building a Static File Server

Let us start with a simple server which just serves static files. Note that if you are using node.js 8.7.0, you need to run node with the --expose-http2 flag.

We will be listening to the stream event and responding to it with the corresponding file from the server root(public, in this case) using the respondWithFile API. We are using the mime-type module to look up the correct mime type to send along with the response.

### Server PUSH Example

Now we have simpel HTTP/2 server running, lets try to use one of the new featues in HTTP/2 - HTTP/2 PUSH. This can lead to significant performance improvements in high latency environments, if done correctly.

We are loading a simple HTML file pointing to style.css which references to our font file. The request to the font file is only made after css file is discovered in the HTML, downloaded and then parsed. This is how the waterfall would have usually looked like:

You can initate a new PUSH with the pushStream API. Since we know that the browser is going to be requesting the font file in the future, we can PUSH the font file as soon as the server receives the request for the HTML file.

When the actual request for the font file takes place, it is claimed from the PUSH cache, instead of making a network request then.

# Rocket of Rust

### Introduction

Rocket’s design is centered around three core philosophies:

• Function declaration and parameter type should contain all necessary information to validate and process a request. This immediately prohibits APIs where request state is retrieved from a global context. As a result, request handling is self-contained in Rocket: handlers are regular functions with regular arguments.

• All request handling information should be typed. Because the web and HTTP are themselves untyped(or stringly typed, as some call it), this means that something or someone has to convert strings to native types. Rocket does this for you with zero programming overhead.

• Decisions should not be forced. Templates, serialization, sessions and just about everything else are all pluggable, optional components. While Rocket has official support and libraries for each of these, they are completely optional and swappable.

### Getting Started

It creates an index route, mount the route at the / path, and launches the application. Compile and run the program with cargo run, you should see the following:

### Overview

Rocket provides primitives to build web servers and applications with Rust: the rest is up to you. In short, Rocket provides routing, pre-processing of requests and psot-processing of response. Your application code instructs Rocket what to pre-process and post-process and fills the gaps between pre-processing and post-processing.

#### LifeCycle

Rocket’s main task is to listen for incoming web prequests, dispatch the request to the application code, and retur a response to the client. We call the process that goes form request to response the ‘lifecycle’. The lifecycle can be summarized as following sequence of steps:

• Routing

Rocket parse an incoming HTTP request into native structure that your code operates on indirectly. Rocket determines which request handler to invoke by matching against route attributes declared in your application.

• Validation

Rocket validates the incoming request against types and guards present in the matched route. If validation fails, Rocket forwards the request to the next matching route or calls an error handler.

• Processing

The request handler associated with the route is invoked with validated arguments. This is the main business logic of an application. Processing completes by returning a Response.

• Response

The returned Response is processed. Rocket generates the appropriate HTTP response and sends it to the client. This completes the lifecycle. Rocket continues listening for requesting, restarting the lifecycle for each incoming request.

#### Routing

Rocket applications are centered around routes and handlers. A route is a combination of:

• A set of parameters to match an incoming request against.

• A handler to process the request and return a response.

A handler is simply a function that takes an arbirary number of arguments and returns any arbitrary type.

The parameters to match against include static paths, dynamic paths, path segments, forms, query string, request format specifiers and body data. Rocket uses attributes, which look like function decoration in other languages to make declaring routes easy. Routes are declared by annotating a function, the handler, with a set of parameters to match against. A complete route declaration looks like this:

This declare the world route to match against the static path /world on incoming GET requests. The world route is simple, but additional route parameters are necessary when building more interesting application.

#### Mounting

Before Rocket can dispatch requests to a route, the route needs to be mounted. Mounting a route is like namespacing it. Routes are mounted via the mount method on a Rocket instance. A Rocket instance is typically created with the racket::ignite() static method.

The mount method takes:

• A path to namespace a list of routes under.

• A list of route handlers through the routes! macro, typing Rocket’s code generation to your application.

For instance, to mount the world route we declared above, we can write the following:

This create a new Rocket instance via the ignite function and mounts the world route to the /hello path. As a result, GET requests to the /hello/world path will be directed to the world function.

#### Namespacing

When a route is declared inside a module other than the root, you may find yourself with unexpected errors when mounting:

This occurs because the routes! macro implicitly converts the route’s name into the name of a structure generated by Rocket’s code generation. The solution is to name the route by a module path instead:

#### Launching

Now that Rocket knows about the route, you can tell Rocket to start accepting requests via the launch method. The method starts up the server and wait for incoming requests. When a request arrives, Rocket finds the matching route and dispatches the requests to the route’s handler.

We typically call launch from the main function. Our complete Hello World application thus looks like:

# Fastify Plugin Guide

In Fastify everything is a plugin, your routes, your utilities and so on are all plugins. And to add a new plugin, whatever its functionality is, in Fastify

### Registers

Register creates a new Fastify context, this means that if you do any change to the Fastify instance, that change will not be reflected into the context’s ancestors. In other words, encapsulated!

Required Plugins must expose a single function with the following signature:

The Fastify’s plugin module is fully reentrant and graph-based, it handles without any kind of problem asynchronous code and it guarantees the load order of the plugins, even the close order.

### Decorators

Fastify offers you a way nice and elegant to wrote an utility: decorator.

Now you can access your utility by doing fastify.util.

# Time to Start Fastify

### Getting Started

#### Register

Register routes in seperate files

or async/await:

### Server Methods

#### server

fastify.server: The Node Core server object.

Function called when all the plugins has been loaded. It takes an error parameter if something went wrong.

#### listen

Starts the server on the given port after all the plugins loaded, internally waits for the .ready() event. The callback is the same as the Node Core.

Specifying an address is also supported:

#### route

Method to add routes to the server, it also have shorthands.

#### routes iterator

The fastify instance is an Iterable object with all the registered routes. The route properties are the same the developer has declared.

#### close

fastify.close(callback), call this function to close the server instance and run the onClose hook.

#### decorate*

Function useful if you need to decorate the fastify instance, Reply or Request.

#### register

Fastify allows the user to extends its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever.

#### use

Function to add middlewares to Fastify.

Function to add a specific hook in the lifecycle of Fastify.

#### logger

The logger instance.

#### Inject

Fake http injection(for testing purpose).

#### setSchemaCompiler

Set the schema compiler for all routes.

#### setNotFoundHandler

fastify.setNotFoundHandler(handler(req, reply)): set the 404 handler. This call is fully encapsulated, so different plugins can set different not found handlers.

#### setErrorHandler

fastify.setErrorHandler(handler(err, reply)): set a function that will be called whenever an error happens. The handler is fully encapsulated, so different plugins can set different error handlers.

### Routes

• method: currently it supports DELETE, GET, HEAD, PATCH, POST, PUT, and OPTIONS, it could be an array of methods.

• url: the path of the url to match this route (alias: path)

• schema: an object containing the schemas for the request and response. They need to be in JSON Schema format.

• body: validates the body of the request if it is a POST or PUT.

• querystring: validates the querystring. This can be a complete JSON Schema Object, with the property type of object and properties object of parameters, or simply the values of what would be contained in the properties object as shown below.

• params: validates the params.

• response: fitler and generate a schema for the response, setting a schema allows us to have 10-20% more throughput.

• beforeHandler(req, res, done): a function called just before the request handler, useful if you need to perform authentication at route level for example, it could also be and array of functions.

• handler(req, reply): the function that will handle this request.

• request if defined in Request.

• reply is defined in Reply.

Exmaple

#### ShortHands

fastify.method(path, [options], handler)

fastify.all(path, [options], handler) will add the same handler to all the supported methods.

#### Route Prefixing

Now your client can access to /v1/user.

Be aware that if you use fastify-plugin this option won’t work.

### Logging

Logging is disabled by default, and you can enable it by passing { logger: true } or { logger: { level: 'info' } } when you create the fastify instance.

### Middlewares

Fastify provides out of the box an asynchronous middleare engine compatible with Express and Restify middlewares.

Fastify middleware don’t support the full syntax middleware(err, req, res, next) because error handling is done inside Fastify.

Also if you are using a middleware taht bundles different, smaller middlewares such as helmet, we recommand to use the single module to get better performances.

If you need to run a middleware only under certain path, just pass the path as first parameter to use and you are done.

Note that this does not support routes with parameters, (eg: /users/:id/comments) and wildcard is not supported in multiple paths.

### Hook

By using the hook you can interact directly inside the lifecycle of Fastify, there are three different Hooks that you can use(in order of execution):

• onRequest

• preHandler

• onResponse

• onClose

Example:

If you want to pass a custom error code to the user, just use the reply.code():

The error will be handled by Reply

The unique hook that is not inside the lifecycle is 'onClose', this one is triggered when you call fastify.close() to stop the server, and it is useful if you have some plugins that need a ‘shutdown’ part, such as a connection to database.

Only for this hook the parameters of the functions changes.

#### Scope

Except for onClose all the hooks are encapsulated this means that you can decide where your hooks should run by using register.

#### beforeHandler

beforeHandler is not a standard hook like preHandler, but is a function that your register right in the route option that will be executed only in the specified route.

beforeHandler is executed always after the preHandler hook.

### Decorators

If you need to add functionalities to the Fastify instance, the decorator api is what you need.

This api allows you to add new properties to the Fastify instance, a property value is not restricted to be a function, could also be an object or a string for example.

#### Usage

##### decorate

Just call the decorate api and pass the name of the new property and its value.

As said above, you can decorate the instance with other values and not only functions:

Once you decorate the instance you can access the value every time you need by using the name you passed as parameter:

Decorators are not overwritable, if you try to declare a decorator already declared, decorate will throw an exception.

As the name suggest, this api is needed if you want to add new methods to the Reply core object. Just call the decorateReply api and pass the name of the new property and its value.

Note: using an arrow function will break the binding of this to the Fastify reply instance.

##### decorateRequest

Note: using an arrow function will break the binding of this to the Fastify request instance.

##### extendServerError

If you need to extend the standard server error, this api is what you need.

You must pass a function that returns an Object, Fastify will extend the server error with the returned object of your function. The function will receive the original error object.

#### Sync and Async

decorate is synchronous API, if you need to add a decorator that has an asynchronous bootstrap, could happen that Fastify boots up before your decorator is ready. To avoid this issue you must use register api in combination with fastify-plugin.

#### Dependencies

If your decorator depends on another decorator, you can declare the dependencies of your function, it’s pretty easy, you just need to add an array of strings(representing the names of the decorators your are depending on) as third parameter.

If a dependency is not satisfied, decorate will throw an exception.

### Validation and Serialize

Fastify uses a schema based approach and even if it is not mandatory we recommend to use JSON Schema to validate you routes and serialize your output, internally Fastify compiles the schema in a highly performance function.

#### Validation

The route validation internally uses Ajv, which is highly performant JSON validator. Validate the input is very easy, just add the fields that you need inside the route schema and you are done.

The supported validations are

• body: Validates the body of the request if it is a POST or PUT

• querystring: Validates the querystring. This can be a complete JSON Schema object, with the property type of object and properties object of parameters, or simply the values of what would be contained in the properties object as shown below.

• params: Validates teh route params.

Example:

#### Schema Compiler

The schemaCompiler is a function that returns a function that validates the body, url parameters, headers and query string.

The default schemaCompiler returns a function that implements the ajv validation interface. fastify use it internally to speed the valiation up.

#### Serialize

Usually you will send your data to the clients via JSON, and Fastify has a powerful tools to help you: fast-json-stringify, which is used if you have provided an output schema in the route options.

As you can see the response schema is based on the status code, if you want to use the same schema for mutliple status code, you can use 2xx.

### Lifecycle

The second parameter of the handler function is Reply.

Reply is a core Fastify object that exposes the following functions:

• .code(statusCode)

• .type(value): Set the header Content-Type

• .redirect([code, ] url): default code 302

• .serializer(function): Set a custom serializer for the payload

• .sent: A boolean value that you can use if you need to know it send has already been called.

#### Code

If not set via reply.code, the resulting statusCode will be 200

Sets a custom header to the response.

If you not set a Content-Type header, Fastify assumes that you are using application/json, unless you are send a stream, in that case Fastify recognize it and sets the Content-Type at application/octet-stream.

#### Redirect

Redirects a request to the specified url, the status code is optional, default to 302.

#### Type

Sets the content type for the response.

It’s the shortcut for reply.header('Content-Type', 'application/json')

#### Objects

If you are sending JSON Objects, send will serialize the object with fast-json-stringify if you setted an output schema, otherwise fast-safe-stringify

#### Promises

Send handle natively the promsies and supports out of the box async-await:

#### Streams

Send can also handle streams out of box, internally uses pump to avoid leaks of file description. If you are sneding a stream and you have not setted a Content-Type header, send will set it at application/octet-stream.

#### Errors

If you pass to send an object that is an instance of Error, Fastify will automatically create error structured as the following:

### Request

The first parameter of the handler function is Request.

Request is a core Fastify object containing the following fields:

• query

• body

• params: the params matching the URL

• req: the incoming HTTP request from Node core

• log

### Content Type Parser

Natively Fastify supports only application/json content-type. If you need to support different content types you can use the addContentTypeParser api.

### Plugins

Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever.

The API you will need for one or more plugins is register.

By default, register creates a new scope, this means that if you do some changes to the Fastify instance(via decorate), this change will not be reflected to the current context ancestors, but only to its sons.

### Craete a Plugin

Create a plugin is very easy, you just need to create a function that takes three paremters, the fastify instance, an options object and the next callback.

#### Handle the Scope

You have two ways to tell Fastify to avoid the creation of a new context:

• Use the fastify-plugin module

• Use the skip-override hidden property

We recomended to use the fastify-plugin module, because it solves this problem for you, and you can pass as parameter a version range of Fastify taht your plguin support.

# Apollo With TypeScript

Original

Start with a simple React Component wrapped with the graphql higher order component from React Apollo.

The above code pulls some data from a GraphQL API using a query and includes lifecycle information, such as loading and error information.

With a few minor changes, we can tell TypeScript how to support us in writing code within this render function.

• We need to tell TS what the shape of our data from our graphql server will look like. We manually write the types for our response data.
• The last line is where the magic happens. We tell TS what the shape of the result will look like from the server when the graphql enhancer wraps a component.

If you already had your project set up with TS, and have already typed your response data, all you have to do is add the type to the graphql HOC and you are off!

### Take Control of Your Tree

Wrapped components are almost always exported and used by a component somewhere else in your tree, so if your exported component has prop requirements, we need to tell TS so it can help prevent errors elsewhere in our tree. Since the graphql wrapper suppors polymorphic types, we can use the second type parameter of it to do just that.

Then the code looks like:

# Source Code of Koa-Static

### Options

• maxage: default to 0.

• hidden: Allow transfer of hidden files, detault to false.

• index: Default file name, detault to ‘index.html’.

• defer: If true, serves after return next().

• gzip: default to true.

• extensions: default to false.

It’s very easy to integrate react-hot-loader into webpack with ts-loader.

### Config the webpack

#### Wrap Root Container into module.hot.accept

Note, if you are using ts with babel-preset-es2015, you can set babel to ignore es2015 module transform by

because webpack 3 has built-in support for ES2015 modules. You won’t need to re-require you root module.

Same treatment in ts if your target is es6

# Methods of Observable

### Observable

##### Direct Subclass
• ConnectableObservable

• GroupedObservable

• Subject

##### Indirect Subclass
• AnonymousSubject

• AsyncSubject

• BehaviorSubject

• ReplaySubject

#### Static Public Methods

• bindCallback

public static bindCallback(func: function, selector: function, schedular: Schedular): function(...params: *): Observable

Convert a callback API to a function that returns an Observable

Give it a function f of type f(x, callback) and it will return a function g taht when called as g(x) will output an Observable.

bindCallback is not an operator because its input and output are not Observables.

The input is a function with some parameters, but the last parameter must be a callback function.

The output of the bindCallback is a function that takes the same parameters as original function takes, except the last one(the callback). When the output function is called with arguments, it will return an Observable.

If the original function’s callback takes one argument, the Observable will emit that value. If on the other hand callback is called with multiple values, resulting Observable will emit an array with these argumetns.

##### More in Origin Article
• bindNodeCallback

public static bindNodeCallback(func: function, scheduler: Schedular): function(...param: *): Observable

• combineLatest

public static combineLatest(observable1: ObservableInput, observable2: ObservableInput, project: function, schedular: Schedular): Observable

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.

Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of the formula.

• concat

public static concat(input1: ObservableInput, input2: ObservableInput, scheduler: Schedular): Observable

Creates an output Observable which sequentially emits all values from given Observable and then moves on to the next.

• create

public static create(onSubscription: function(observer: Observer): TearDownLogic): Observable

Creates a new Observable, that will execute the specified function when an Observer subscribes to it.

• defer

public static defer(observableFactory: function(): SubscribableOrPromise): Observable

Creates an Observable that, on subscribe, calls an Observable factory to make an Observable for each new Observer

Creates the Observable lazily, that is, only when it is subscribed.

• empty

public static empty(scheduler: Scheduler): Observable

Creates an Observable that emits no items to the Observer and immediately emits a completion notification.

Just emits ‘complete’ and nothing else

• from

public static from(ish: ObservableInput<T>, scheduler: Scheduler): Observable<T>

Creates an Observable from an Array, an array-like Object, a Promise, an iterable object, or an Observable-like Object

Convert almost anything to an Observable

• fromEvent

public static fromEvent(target: EventTargetLike, eventName: string, options: EventListenerOptions, selector: SelectorMethodSignature<T>): Observable<T>

Creates an Observable from DOM events, or Node EventEmitter events or others

• fromEventPattern

public static fromEventPattern(addHandler: function(handler: Function): any, removeHandler: function(handler: Function, signal?: any): void, selector: function(...args: any): T): Observable<T>

Converts any addHandler/removeHandler API to an Observable

• fromPromise

public static fromPromise(promise: Promise<T>, scheduler: Scheduler): Observable<T>

Returns an Observable that just emit the Promise’s resolved value, then complete

• interval

public static interval(period: number, scheduler: Scheduler): Observable

Emits incremental numbers periodically in time

• merge

public static merge(observables: ...ObservableInput, concurrent: number, scheduler: Scheduler): Observable

Creates an output Observable which concurrently emits all values from every given input Observable

Flatten multiple Observables together by blending their values into one Observable

• never

public static never(): Observable

Creates an Observable that emits no items to the Observer

An Observable that never emits anything

• of

public static of(values: ...T, scheduler: Scheduler): Observable<T>

Creates an Observbale that emits some values you specify as arguments, immediately one after the other, and then emits a complete notification.

Emits the arguments you provide, then complete.

• range

public static range(start: number, count: number, scheduler: Scheduler): Observable

Creates an Observable that emits a sequence of numbers within a specified range.

Emits a sequence of numbers in a range

• throw

public static throw(error: any, scheduler: Scheduler): Observable

Just emit ‘error’ and nothing else.

• timer

public static timer(initialDelay: number|Date, period: number, scheduler: Scheduler): Observable

Creates an Observable that starts emitting after an initialDelay and emits ever increasing numbers after each period of time thereafter.

It’s like interval, but you can specify when should the emissions start.

• webSocket

public static webSocket(urlConfigOrSource: string | WebSocketSubjectConfig): Observable

• zip

public static zip(observables: *): Observable<R>

Combines multiple Observables to create an Observable whose values are calculated from the valeus, in order, of each of its input Observables.

• constructor

public constructor (subsribe: Function)

• audit

public audit(durationSelector: function(value: T): SubscribableOrPromise): Observable<T>

Ignores source value for a duration determined by another Observable, then emis the most recent value from the source Observable, then repeats this process.

It’s like auditTime, but the silencing duration is determined by a second Observable.

audit is similar to throttle, but emits the last value from the silenced time window.

• auditTime

public auditTime(duration: number, scheduler: Scheduler): Observable<T>

Ignores source values for duration milliseconds, then emits the most recent value from the source Observable, then repeats this process.

Similar to throttleTime

• buffer

public buffer(closingNotifier: Observable<any>): Observable<T>

Buffers the source Observable values until closingNotifier emits.

Collect values from the past as an array, and emits that array when another Observable emits.

• bufferCount

public bufferCount(bufferSize: number, startBufferEvery: number): Observable<T[]>

• bufferTime

public bufferTime(bufferTimeSpan: number, bufferCreationIntervel: number, maxBufferSize: number, scheduler: Scheduler): Observable<T[]>

Buffers the source Observable values for a specific time period.

Collect values from the past as an array, and emits those array periodically in time.

• bufferToggle

public bufferToggle(openings: SubscribableOrPromise<O>, closingSelector: function(value: O): SubscribableOrPromise): Observable<T[]>

• bufferWhen

public bufferWhen(closingSelector: function(): Obversable): Obversable<T[]>

Buffers the source Observable values, using a factory function of closing Observables to determine when to close, emit and reset the buffer.

• catch

public catch(selector: function): Obversable

Catches errors on the observable to be handled by returning a new obversable or throwing an error.

• combineAll

public combineAll(project: function): Observable

Convert a higher-order Observable into a first-order Observable by waiting for the outer Observable to complete, then applying combineLatest

Similar to CombineLatest

Flatten an Observable-of-Observables by applying combineLatest when the Observable-of-Observable completes

• combineLatest

public combineLatest(other: ObservableInput, project: function): Observable

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observable.

• concat

public concat(other: ObservableInput, scheduler: Scheduler): Observable

• concatAll

public concatAll(): Observable

Converts a higher-order Observable into a first-order Observable by concatenating the inner Observable in order.

Flatten an Observable-of-Observable by putting one inner Observable after the other

• concatMap

public concatMap(project: function(value: T, ?index: number): ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any): Observable

Projects each source value to an Observable which is merged in the output Observable, in a serialized fashion waiting for each one to complete before merging the next.

Map => Concat

Map each value to an Observable, then flatten all of these inner Observables using concatAll

• concatMaoTo

public concatMapTo(innerObservable: ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any): Observer

It’s like concatMap, but maps each value always to the same inner Observable

• count

public count(predicate: function(value: T, i: number, source: Observable<T>): boolean): Observable

Counts the number of emissions on the source and emits the number when the source completes.

• debounce

public debounce(durationSelector: function(value: T): SubscribableOrPromise): Observable

Emits a value from the source Observable only after a particular time span determined by another Observable has passes without another source emission.

It’s like debounceTime, but the time span of emission silence is determined by a second Observable.

• debounceTime

public debounceTime(dueTime: number, scheduler: Scheduler): Observable

Emits a value from the source Observable only after a particular time span has passed without another source emission.

It’s like delay, but passes only the most recent value from each burst of emissions.

• defaultEmpty

public defaultIfEmpty(defaultValue: any): Observable

Emits a given value if the source Observable completes without emitting any next value, otherwise mirrors the source Observable.

If the source Observable turns out to be empty, then this operator will emit a default value.

• delay

public delay(delay: number|Date, scheduler: Scheduler): Observable

Delays the emission of items from the source Observable by a given timeout or until a given Date.

• delayWhen

public delayWhen(delayDurationSelector: function(value: T): Observable): Observable

It’s like delay, but the time span of the delay duration is determined by a second Observable.

• dematerialize

public dematerialize(): Observable

Convert an Observable of Notification objects into the emissions that they represent.

Unwraps Notification objects as actual next, error, and complete emissions. The opposite of materialize.

dematerialize is assumed to operate an Observable that only emits Notification objects as next emission, and does not emit any error. Such Observable is the output of a materialize operation. Those notifications are then unwrapped using the metadata they contain, and emitted as next, errorand complete on the output Observable.

• distinct

public distinct(keySelector: function, flushes: Observable): Observable

Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.

Note: ‘1’ is different from 1

• distinctUntilChanged

public distinctUntilChanged(compare: function): Observable

Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item. (not items)

• distinctUntilKeyChanged

public distinctUntilKeyChanged(key: string, compare: function): Observable

• do

public do(nextOrObserver: Observer|function, error: function, complete: function): Observable

Perform a side effect for every emission on the source Observable, but return an Observable that is identical to the source.

Intercepts each emission on the source and runs a function, but returns an output which is identical to the source.

• elemetnAt

public elementAt(index: number, defaultValue: T): Observable

Emits the single value at the specified index in a sequence of emissions from the source Observable.

Emits only the i-th value , then completes.

• every

public every(predicate: function, thisArg: any): Observable

Returns an Observable that emits whether or not every item of the source satisfies the condition specified.

• exhaust

public exhaust(): Observable

Convert a higher-order Observable into a first-order Observable by dropping inner Observables while the previoud inner Observable has not yet completed.

Flatten an Observable-of-Observables by dropping the next inner Observable while the current inner is still executing.

exhaust ingores every new inner Observable if the previous Observable has not yet completed.

• exhaustMap

public exhaustMap(project: function(value: T, ?index: number): ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any): Observable

Projects each source value to an Observable which is merged in the output Observable only if the previous projected Observable has completed.

• expand

public expand(project: function(value: T, index: number), concurrent: number, scheduler: Scheduler): Observable

Recursively projects each source value to an Observable which is merged in the output Observable.

It’s similar to mergeMap, but applies the projection function to every source value as well as every output value. It’s recursive.

• filter

public filter(predicate: function(value T, index: number): boolean, thisArg: any): Observable

Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.

• find

public find(predicate: function(value: T, index: number, source: Observable<T>): boolean, thisArg: any): Observable<T>

Emits only the first value emitted by the source Observable that meets some condition.

• findIndex

public findIndex(predicate: function(value: T, index: number, source: Observable<T>): boolean, thisArg: any): Observable<T>

It’s like find, but emits the index of the found value, not the value itself.

• first

public first(predicate: function(value: T, index: number, source: Obsevable<T>): boolean, resultSelector: function(value: T, index: number): R, defaultValue: R): Observable<T|R>

Emits only the first value (or the fisrt value that meets some condition) emitted by the source Obsevable

• forEach

public forEach(next: Function, PromiseCtor: PromiseConstructor): Promise

• groupBy

public groupBy(keySelector: function(value: T): K, elementSelector: functioN(value: T): R, durationSelector: function(grouped: GroupedObservable<K|R>): Observable<any>): Observable<GroupedObservable<K, R>>

• ignoreElements

public ignoreElements(): Observable

Iggnores all items emitted by the source Observable and only pass calls of complete or error

• isEmpty()

public isEmpty(): Observable

• last

public last(predicate: function): Observable

• letProto

public letProto(func: *): Observable<T>

• lift

public lift(operator: Operator): Observable

Creates a new Observable, with this Observable as the source, and the passed operator defined as the new observable’s operator.

Return a new Observable with the Operator applied.

• map

public map(project: function(value: T, index: number): R, thisArg: any): Observable

Applies a given project function to each value emitted by the source Observable, and emits the resulting values as an Observable.

• mapTo

public mapTo(value: any): Observbale

Emits the given constant value on the output Observable every time the source Observable emits a value.

• materialize

public materialize(): Observable<Notification<T>>

Represents all of the notifications from the source Observable as next emission marked with their orignal types within Notification objects.

• max

public max(comparer: Function): Observable

• merge

public merge(other: ObservableInpt, concurrent: number, scheduler: Scheduer): Observable

Creates an output Observable which concurrently emits all values from every given input Observable.

Flatten multiple Observable together by blending their values into one Observable.

• mergeAll

public mergeAll(concurrent: number): Obsevable

Converts a higher-order Observable into a first-order Observable which concurrently delivers all values that are emitted on the inner Observables.

• mergeMap

public mergeMap(project: function(value: T, ?index: number): ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any, concurrent: number): Observable

Projects each source value to an Observable which is merged in the output Observable.

Map each value to an Observable, then flattn all of these inner Observable using mergeAll

• mergeMapTo

public mergeMapTo(innerObservable: ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any, concurrent: number): Observable

Projects each source value to the same Observable which is merged multiple times in the output Observable.

• mergeScan

public mergeScan(accumulator: function(acc: R, value: T): Observable<R>, seed: *, concurrent: number): Observable<R>

Applies an accumulator function over the source Observable where the accumulator function itself retunrs an Observable, then each intermediate Observable is merged into the output Obsevable.

• min

public min(comparer: Function): Observable<R>

• multicast

public multicast(subjectOrSubjectFactory: Function | Subject, selector: Function): Observable

Returns an Observable tghat emits the results of invoking a specified selector on items emitted by a ConnectableObservable that shares a single subscription to the underlying stream.

• observeOn

public observeOn(scheduler: *, delay: *): Observable<R>

• pairwise

public pairwise(): Observable<Array<T>>

Groups pairs of consecutive emissions together and emits them as an array of two values.

Puts the current value and previous value together as an array, and emits that.

• partition

public partition(predicate: function(value: T, index: number): Boolean, thisArg: this): [Observable, Observable]

Splits the source Observable into two, one with values that satisfy a predicate, and another values that don’t satisfy the predicate.

It’s like filter, but returns two Observable, one like the output of filter, and the other with values that did not pass the condition.

• pluck

public pluck(properties: ...string): Observable

Maps each source value (an object) to its specified nested property

Like map, but meant only for picking on eof the nested properties of every emitted object.

• publish

public publish(selector: Function): *

Returns a ConnectableObservable, which is a variety of Observable that waits until connect method is called before it begins emitting items to those Observers that have subscribed to it.

• publishBehavior

• publishLast

• publishReplay

• race

public race(...observables): Observable

Returns an Observable that mirrors the first source Observable to emit an item from the combination of this Observable and supplied Observables.

• reduce

public reduce(accumulator: function(acc: R, value: T, index: number): R, seed: R): Observable<R>

• repeat

public repeat(count: number): Observable

• repeatWhen

public repeatWhen(notifier: functioN(notification: Observable): Observable): Observable

1

• retry

public retry(count: number): Observable

Returns an Observable that mirrors the source Observable with the exception of an error, if the source Observable calls error, this method will resubscribe to the source Observable for a maximum of count resubscriptions rather than propagating the error call.

• retryWhen

public retryWhen(notifier: function(errors: Observable): Observable): Observable

• sample

public sample(notifier: Observable<any>): Observable<T>

Emits the most recently emitted value from the source Observable whenever another Observable, the notifier emits.

It’s like sampleTime, but sampels whenever the notifier Observable emits.

• sampleTime

public sampleTime(period: number, scheduler: Scheduler): Observable<T>

• scan

public scan(accumulate: function(acc: R, value: T, index: number): R, seed: T|R): Observable<R>

Applies an accumulator function over the source Observable, and returns each intermediate result, with an optional seed value.

It’s like reduce, but emits the current accumulation whenever the source emits a value

• sequenceEqual(compareTo: Observbale, comparor: function): Observable

Compares all values of two observbales in sequence using an optional comparor function and returns an observable of a single boolean value representing whether or not the two sequences are equal.

Checks to see of all values emitted by both observables are equal in order.

• share

public share(): Observable<T>

Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one Subscriber this Observable will be subscribed and emitting data. When all subscribers have unsubscribed it will unsubscribe from the source Observable. Because the Observable is multicasting it makes the stream hot. This is an alias for .publish().refCount().

• single

public single(predicate: Function): Observable<T>

• skip

public skip(count: number): Observable

• skipUntil

public skipUntil(notifier: Observable): Observable<T>

Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.

• skipWhile

public skipWhile(predicate: Function): Observable

Returns an Observable that skips all items emitted by the source Observable as long as a specified condition holds true, but emits all further source items as soon as the condition becomes false.

• startWith

public startWith(values: ...T, scheduler: Scheduler): Observable

Returns an Observable that emits the items you specified before it begins to emit items by the source Observable.

• subscribeOn

public subscribeOn(scheduler: Scheduler): Observable<T>

Asynchornously subscribes Observables to this Observable on the specified Scheduler.

• switch

public switch(): Observable<T>

Converts a higher-order Observable into a first-order Observable by subscribing to only the most recently emitted of those inner Observables.

Flatten an Observable-of-Observables by dropping the previous inner Obsevable once a new one appears.

• switchMap

public switchMap(project: function(value: T, ?index: number): ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any): Observable

Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recent projected Observable.

Map each value to an Observable, then flatten all of these Observable using switch.

• take

public take(count: number): Observable<T>

Emits only the first count values emitted by the source Observable.

Take the first count value from the source, then complete.

• takeLast

takeLast(count: number): Observable<T>

Emits only the last count values emitted by the source Observable.

Remembers the latest count values, then emits those only when the source completes.

• takeUntil

public takeUntil(notifier: Observable): Observable<T>

Emits the values emitted by the source Observable until a notifier Observable emits a value.

Let values pass until a second Observable, notifier, emits something. Then, it completes.

• takeWhile

public takeWhile(predicate: function(value: T, index: number): boolean): Observable<T>

Emits values emitted by the source Observable so long as each value satisfies the given predicate, and then completes as soon as this predicate is not satisfied.

Take values from the source only while they pass the condition given, when the first value does not satisfy, it completes.

• throttle

public throttle(durationSelector: function(value: T): SubscribableOrPromise): Observable<T>

Emits a value from the source Observable, then ignores subsequent source values for a duration determined by another Observable, then repeats this process.

It’s like throttleTime, but the silencing duration is determined by a second Observable.

Cannot emit more than one values until the second Observable emits a value.

• throttleTime

public throttleTime(duration: number, scheduler: Scheduler): Observable<T>

Emits a value from the source Observable, then ignores subsequent source values for duration milliseconds, then repeats this process.

Lets a value pass, then ignores source values for the next duration milliseconds.

• timeInterval

public timeInterval(scheduler: *): Observable<TimeInterval<any>>

• timeout

public timeout(due: number, scheduler: Scheduler): Observable<T>

• timeoutWith

public timeoutWith(due: *, withObservable: *, scheduler: Scheduler): Observable<R>

• timestamp

public timestamp(scheduler: *): Observable<Timestamp<>>any

• toArray

public toArray(): Observable<any[]>

• toPromise

public toPromise(PromiseCtor: *): Promise<T>

• window

public window(windowBoundaries: Observable<any>): Observable<Observable<T>>

Branch out the source Observable values as a nested Observable whenever windowBoundaries emits.

It’s like buffer, but emits a nested Observable instead of an array.

• windowCount

public windowCount(windowSize: number, startWindowEvery: number): Observable<Observable<T>>

Branch out the source Observable values as a nested Observable with each nested Observable emitting at most windowSize values.

It’s like bufferCount, but emits a nested Observable instead of an array.

• windowToggle

public windowToggle(openings: Observable<O>, closingSelectors: function(value: O): Observable): Observable<Observable<T>>

Branch out the source Observable values as a nested Observable starting from an emission from openings and ending when the output of closingSelector

• windowWhen(closingSelector: function(): Observable): Observable<Observable>

Branch out the source Observable values as a nested Observable using a factory function of closing Observables to determine when to start a new window.

It’s like bufferWhen, but emits a nested Observable instead of an array.

• withLatestFrom

public withLatestFrom(other: ObservableInput, project: Function): Observable

Combines the source Observable with other Observables to create an Observable whose values are calculated from the latest values of each, only when the source emit.

Whenever the source Observable emits a value, it computes a formula using that value plus the latest values from other input Observables, then emits the output of that formula.

• zipAll

public zipAll(project: *): Observable<R>

• zipProto

public zipProto(observables: *): Observable<R>

# HTTP Timings in Node.js

Original

### Some Basic Concepts:

• IP(Internet Protocol): IP is a network-layer protocol, deals with network addressing and routing. IP is responsible for delivering packets from the source host to the destination host based on the packet headers across one or more IP network. It also defines packet structures that encapsulate the data to be delivered.

• DNS(Domain Name Server): DNS is a hierarchical decentralized naming system used to resolve human-readable hostnames into machine-readable IP address.

• TCP(Transmission Control Protocol): The TCP standard defines how to establish and maintain a network conversation between applications to exchange data. TCP provides reliable, ordered, and error-checked deliver of a stream of octects between applications running on hosts communicating over an IP network. An HTTP client initiates a request by establishing a TCP connection.

• SSL(Secure Sockets Layer)/TLS(Transport Layer Security): TLS is a cryptographic protocol that provides communications security over a computer network. SSL is a deprecated predecessor to TLS. Both TLS and SSL use certificates to establish a secure connection. SSL certificates are not dependent on cryptographic protocols like TLS, a certificate contains a key pair: a public and a private key.

Timings Explained:

• DNS Lookup: Time spent performing the DNS Lookup. DNS lookup resolves domain names to IP addresses.

• TCP Connection: Time it took to establish TCP Connection between a source host and destination host. Connections must be properly established in a multi-step handshake process.

• TLS handshake: Time spent completing a TLS handshake. During the handshake process endpoints exchange authentication and keys to establish or resume secure sessions. There is no TLS handshake with a not HTTPS request.

• Time To First Byte(TTFB): Time spent waiting for the initial response. This time captures the latency of a round trip to the server in addition to the time spent waiting for the server to process the request and deliver the response.

• Content Transfer: Time spent receiving the response data. The size of the response data and the available network bandwidth determinates its duration.

### Measuring HTTP timings in Node.js

To measure HTTP timings in Node.js, we need to subscribe to a specific request, response and socket events. Here is a short code snippet how to do this.

# Tag Picture in Html

The picture element is a markup pattern that allows developers to declare multiple sources for an image.

This markup is a container used to specify multiple <source> elements for a specific <img> contained in it.

### Use the media attribute

The media attribute lets you specify a media query that the user agent will evaluates to select a <source> element. If the media query evaluates to false, the <source> element is skipped.

The srcset and sizes attributes extend the img and source elements to provide a list of available image sources and their sizes. Browsers can then use this information to pick the best image source.

srcset defines the set of images we will allow the browser to choose between, and what size each image is.

Before each comma, we write:

• An Image filename (elva-fair-320w.jpg)

• A space

• The image’s inherit width in pixels(480w) – note that this issue the w unit, not px as you might expect.

sizes defines a set of media conditions(e.g. screen widths) and indicates what image size would be best to choose, when certain media conditions are true.

Before each comma, we write:

• A media condition(max-width: 480px) – when the viewport width is 480 px or less

• A space

• The width of the slot the image will fill when the media condition is true(440px)

# Options of Webpack Dll Plugin

The DllPlugin and DllReferencePlugin provide means to split bundles in a way that can drastically improve build time performance.

### DllPlugin

This plugin used in a separate webpack config exclusively to create a dll-only-bundle.

It creates a manifest.json file, which is used by the DllReferencePlugin to map dependencies.

• context: (optional), context of requests in the manifest file(default to the webpack context).

• name: name of the exposed dll function(TemplatePaths: [hash] & [name])

• path: absolute path to the manifest json file(output)

Creates a manifest.json which is written to the given absolute path.

It contains mappings from require and import requests, to module ids. It is used by the DllReferencePlugin.

Combine this plugin with output.library option to expose(aka, put into the global scope) the dll function

output.library expose the name to global scope

DllReferencePlugin.name add the name to the mapping file.

So it’s important to keep output.library and DllReferencePlugin.name same to set a right mapping.

### DllReferencePlugin

This plugin is used in the primary webpack config, it references the dll-only-bundle to require the pre-built dependencies.

• context: absolute path, context of the requests in the manifest(or content property)

• manifest: an object containing content and name or a string to the absolute path of the JSON manifest to be loaded upon compilation.

• content: (optional), the mappings from request to module id(default to manifest.content)

• name: (optional), the name where the dll is exposed (default to manifest.name)

• scope: (optional), prefix which is used for accessing the content of the dll

• sourceType: (optional), how the dll is exposed.

References a dll manifest file to map dependencies names to module ids, then requires them as needed using the internal __webpack_require__ function.

Note: Keep the name consistent with output.library

### Modes

This plugin can be used in two different modes, scoped and mapped.

#### Scoped Mode

The content of the dll is accessible under a module prefix, i.e. with scope = 'xyz' a file named abc in the dll can be access via require(xyz/abc).

#### Mapped Mode

The content of the dll is mapped to the current directory. If a required file matches a file in the dll(after resolving), then the file from the dll is usd instead.

Because this happens after resolving every file in the dll bundle, the same paths must be available for the consumer of the dll bundle, i.e. if the dll contains lodash and the file abc, require('lodash') and require('./abc') will be used from the dll, rather than building them into the main bundle.

### Notes

#### Context in Dev Config

Set the DllReferencePlugin.context same as DllPlugin.context so the manifest will bundle and resolve the dll files from the same root directory.

#### Add dll bundle to html

After bundle the dll, I should insert it betore app.js in the html file to provide the global function.

• Step 1: Copy the dll files to the dist directory

I use the copy-webpack-plugin plugin

• Generate a manifest of the dll files for html-webpack-plugin to insert to the template

I use the webpack-manifest-plugin

It will generate the manifest.json in output.path

• Insert the Manifest into the html

# Optimize Web App

### Optimizing All Assets

One of the most powerful, but under-utilized ways to significantly improve performance starts with understanding hwo the browser analyzes and serves assets. It turns out that browsers are pretty great at discovering resources while parsing and determing their priority of the fly. Here’s where the critical Request comes in:

A request is critical if it contains assets that are necessary to render the content within the users’ viewport.

For most sites, it’d be HTML, necessary CSS, a logo, a web font and maybe an image.

We can control this behavior by carefully picking critical resources and adjusting theri priority.

With <link red='preload'> we are able to manually force assets’ priority to High ensuring that desired content will be rendered on time.

This technique can yield significant improvements in Time to Interactive metric, making optimal user experience possible.

Critical requests still seem like a black box for many, and the lack of sharable materials doesn’t help to change that.jj

### Optimizing Images

Images often account for most of a web page’s transferred payload.

#### Choosing the right format

The initial choice falls between vector and raster graphics:

• Vector: resolution independent, usually significantly smaller in size. Perfect for logos, iconography and simple assets comprising of basic shapes.

• Raster: offers much more detailed results, ideal for photographs.

After making this decision, there are a fair bit of formats to choose from: JPEG, GIF, PNG-8, PNG-24 or newest formats such as WEBP or JPEG-XR, JPEG-2000.

• JPEG: Imagery with many color(e.g. photos)

• PNG-8: Imagery with a few color

• PNG-24: Imagery with partial transparency

• GIF: Animated imagery

### Experimenting with new formats

WebP is easily the most popular contender, supporting both lossless and lossy compression, which makes it incredibly versatile. Lossless WebP is 26% smaller than PNGs and 25-34% smaller than JPGs. With 74% browser support it can safely be used with fallback. JPGs and PNGs can be converted to WebP in PS and other image processing app as well as through command line interface(brew install webp).

### Responsible and responsive Imagery

W’re perfectly equipped to do so with picture element and srcset attribute(both have 85% support)

#### The srcset attribute

srcset works best in the resolution switching scenario – when we want to display imagery based on user’s screen density and size. Baseed on a set of predefined rules in srcset and size attributes the browser will pick the best image to serve accordingly to the viewport.

#### The picture element

picture element and the media attribute are designed to make art direction easy. BY providing different sources for varying conditions.

### Optimizing Web Fonts

#### Choose the right format

There are four web font formats: EOT, TTF, WOFF and WOFF2.

TTF and WOFF are most widely adopted, boasting over 90% browser support. Depending on the support you’re targeting it’s most likely safe to server WOFF2 and fall back to WOFF for older browsers. The advantage of using WOFF2 is a set of custom preprocessing and compression algorithms resulting in approx 30% file-size reduction and impoved parsing capabilities.

When defining the sources of web fonts in @font-face use the format() hint to specify which format should be utilized.

#### use Unicode-range subsetting

Fonts are render-blocking – because the browser has to build both the DOM and CSSOM first. Wdb fonts won’t be downloaded before they’re used in a CSS selector that matches an existing node. This behavior significantly delays text rendering, often causing the Flash of Invisible Text(FOIT).

Implementing a font strategy prevents users from not being able to access your content.

font-display is a new CSS property providing a non-javascript reliant solution.

# Concept on BlockChain

A Blockchain is

A distributed database that is used to maintain a continuously growing list of records, called block.

A Block contains following information:

• Index(Block #) - Which block is it? (Genesis block has index 0)

• Hash: Is the block valid?

• Previous Hash: It the previous block valid?

• Timestamp: When was the block added?

• Data: What information is stored on the block?

• Nonce: How many iterations did we go through before we found a valid block?

### Genesis Block

Every Blockchain will st art with the Genesis Block, each block on the blockchain is dependent on the previous block, so the Genesis Block is needed to mine our first block.

### Hash

A hash value is a numeric value of a fixed length that uniquely identifies data.

The hash is calculated by taking the index, previous block hash, timestamp, block data, and nonce as input.

### Leading 0 in the block hash

Specified leading 0 is a minimum requirement for a valid hash. The number of leading 0 required is called difficulty.

The is also known as the Proof-of-Work system.

### Nonce

A nonce is a number used to find a valid hash

The nocne iterates until the hash is valid.

The process of finding a nonce that corresponds to a valid hash is mining.

As the difficulty increase, the number of possible valid hashes decrese. With less possible valid hashes, it takes more processing power to find a valid hash.

### Ethereum Virtual Machine (EVM)

The Ethereum Virtual Machine or EVM is the runtime environment for smart contracts in Ethereum. It is not only sandboxed but actually completely isolated, which means that code running inside the EVM has no access to network, filesystem or other processes. Smart contract even have limited access to other smart contracts.

#### Accounts

There are two kinds of accounts in Ethereum which share the same address space:

• External Account that are controlled by public-private key pairs(i.e. humans)

• Contract Account that are controlled by the code stored together with accounts

The address of an external account is determined from the public key while the address of a contract is determined at the time the contract is created(it is derived from the creator address and the number of transactions sent from the address, the so-called ‘nonce’).

Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage.

Every account has a balance in Ether(in Wei to be exact) which can be modified by sending transactions that include Ether.

#### transactions

A transaction is a message that is sent from one account to another account(which might be the same or the special zero-account). It can include binary data(its payload) and Ether.

If the target account contains code, that code is executed and the payload is provided as input data.

If the target account is the zero-account(the account with the address 0), the transaction creates a new contract.

As memtioned, the address of that contract is not the zero address but an address derived from the sender and its number of transactions sent(nonce). The payload of such a contract creation transaction is taken to be EVM bytecode and executed to generate the new contract

The output of the bytecode, namely the new contract, is permenantly stored.

#### Gas

Each transaction is charged with a certain with a certain amount of gas, whose purpose is to limit the amount of work that is needed to executed the transaction and to pay for this execution.

While the EVM executes the transaction, the gas is gradually depleted according to specific rules.

The gas price is a value set by the creator of the transaction, who has to pay gas_price * gas.

#### Storage, Memory and the Stack

Each account has a persistent memory area which is called storage. Storage is a key-value store taht maps 256-bit word to 256-bit word.

The second memory area is called memory, of which a contract obtains a freshly cleared instance for each message call. Memory is linear and can be addressed at byte level, but reads are limited to a width of 256 bits, white writes can be either 8btis or 256bits wide.

Memory is expanded by a word(256-bit), when accessing (either reading or writing) a previously untouched memory word. At the time of expansion, the cost in gas must be paid. Memory is more costly the larger it grows(it scales quadratically).

The EVM is not a register machine but a stack machine, so all computations are performed on an area called the stack. It has limited to the top end in the following way:

• it is possible to copy one of the topmost 16 elements to the top of the stack or swap the topmost 2(or 1, or more, depending on the opeartion) element fomr the stack and push the result onto the stack.

• All other operation take the topmost 2(or 1, or more, depending on the operation) elements from the stack and push the result onto the stack.

• It is possible to move stack elements to storage or memory

• It is not possible to just access arbitrary elements deeper in the stack without removing the top of the stack.

### Instruction Set

The instruction set of the EVM is kept minimal in order to avoid incorrect implementations which could cuase consensus problem. All instructions operate ont he basic data type, 256-bit words.

The usual arihmetic bit, logical and comparison operations are present. Conditional and unconditional jumps are possible. Furthermore, contracts can access relevant properties of the current block like its number and timestamp.

### Message Calls

Contracts can call other contracts or send Ether to non-contract accounts by the means of the message calls.

Message calls are similar to transactions, in that tehy have a source, a target, data payload, Ether, gas, and return data.

In fact, every transaction consists of a top-level message call which in turn can create further message calls.

A contract can decide how much of its remaining gas should be sent with the inner message call and how much it wants to retain. If an out-of-gas exception happens in the inner call(or any other exception), this will be signalled by an error value put onto the stack. In this case, only the gas sent together with the call is used up.

Summarily, a called contract(which can be the same as caller) will receive a freshly cleared instance of memoery, and has access to the call payload - which will be provided in a separate area called the calldata. After it has finished execution, it can return data which will be stored at a location in the caller’s memory preallocated by the caller.

Calls are limited to depth of 1024, which means that for more complex operations, loops should be preferred over recursive calls.

### Delegatecall / Callcode and Libraries

There exists a special variant of a message call, named delegatecall which is identical to a message call apart from the fact taht the code at the target address is executed in the context of the calling contract and msg.sender and msg.value do not change their values.

This means that a contract can dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract, only the code is taken from teh called address.

This makes it possible to implement the ‘library’ feature in Solidity: Reusable library code that can be applied to a contract’s storage to implement a complex data structure.

### Logs

It is possible to store data in a special indexed data structure that maps all the way up to the block level. This feature called logs is used by Solidity in order to implement events. Contracts cannot access log data after it has been created, but they can efficiently accessed from outside the blockchain.

Since some part of the log data is stored in bloom filters, it is possible to search for this data in an efficient and crypographically secure way, so network peers that do not download the whole blockchain(‘light clients’) can still find these logs.

### Create

Contracts can even create other contract s using a special opcode. The only difference between create calls and normal message calls is that the payload data is executed and the result stored as code and the caller/creator receives the address of the new contract on the stack.

### Self-destruct

The only possible that code is removed from the blockchain is when a contract at that address performs the selfdestruct operation. The remaining Ether stored at that address is sent to a designed target and then the storage and code is removed from the state.

Even if a contract’s code does not contain a call to selfdestruct, it can still perform that operation using delegatecall or callcode.

# Build Node CLI

### Dependencies

• chalk: colorize the output

• clear: clears the terminal screen

• clui: draws command line tables, gauges and spinners

• figlet: creates ASCII art from text

• inquirer - create interactive command line user interface

• minimist - parses argument options

• preferences - manage CLI application encrypted preferences

#### chalk

• chalk.<style>[.<style>...](string, [string...])

chalk.red.bold.underline(‘Hello’, ‘World’)

• chalkstring {<style> <style>… string} string

chalkThere are { bold 5280 feet } in a mile 

Display

• Gauge

• PrograssBar

• Sparklines

• Spinners

on Terminal

#### figlet

Display ASCII font on Terminal

#### inquirer

A collection of common interactive command line user interface.

• inquirer.prompt(questions) => promise

• questions an Array containing Question Object(using the reactive interface, you can also pass a Rx.observable instance)

• Question Object:

### Make CLI globally available

Add a shebang line to the top of index.js

Then add a bin prototype to package.json.

# Start Up With Ts, React, Router, Redux, Rxjs

The Repo

### Step 2: Skeleton of React App(React + Router)

In src/app.tsx` render the App to html

#### Create App Represental Component

Same structure in Body and Footer.

And use it in body container