Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
blorenz committed Jun 7, 2022
0 parents commit 38ef44c
Show file tree
Hide file tree
Showing 71 changed files with 4,163 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 Remix Software Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @remix-run/server-runtime

[Remix](https://remix.run) supports multiple server runtimes:

- [Node](https://nodejs.org/en/)
- [Cloudflare](https://developers.cloudflare.com/workers/learning/how-workers-works/)
- [Deno](https://deno.land/) (Experimental 🧪)

Support for each runtime is provided by a corresponding Remix package:

- [`@remix-run/node`](https://github.com/remix-run/remix/tree/main/packages/remix-node)
- [`@remix-run/cloudflare`](https://github.com/remix-run/remix/tree/main/packages/remix-cloudflare)
- [`@remix-run/deno`](https://github.com/remix-run/remix/tree/main/packages/remix-deno)

This package defines a "Remix server runtime interface" that each runtime package must conform to.

Each Remix server runtime package MUST:

- Implement and export values for each type in [`interface.ts`](./interface.ts)
- Re-export types in [`reexport.ts`](./reexport.ts)

Each Remix server runtime package MAY:

- Re-export the [default implementations](./index.ts) as its implementations
- Export custom implementations adhering to the [interface types](./interface.ts)
- Provide additional exports relevant for that runtime
28 changes: 28 additions & 0 deletions build.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { DataFunctionArgs } from "./routeModules";
import type { EntryContext, AssetsManifest } from "./entry";
import type { ServerRouteManifest } from "./routes";
import type { AppLoadContext } from "./data";
/**
* The output of the compiler for the server build.
*/
export interface ServerBuild {
entry: {
module: ServerEntryModule;
};
routes: ServerRouteManifest;
assets: AssetsManifest;
}
export interface HandleDocumentRequestFunction {
(request: Request, responseStatusCode: number, responseHeaders: Headers, context: EntryContext, loadContext: AppLoadContext): Promise<Response> | Response;
}
export interface HandleDataRequestFunction {
(response: Response, args: DataFunctionArgs): Promise<Response> | Response;
}
/**
* A module that serves as the entry point for a Remix app during server
* rendering.
*/
export interface ServerEntryModule {
default: HandleDocumentRequestFunction;
handleDataRequest?: HandleDataRequestFunction;
}
69 changes: 69 additions & 0 deletions cookies.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { CookieParseOptions, CookieSerializeOptions } from "cookie";
import type { SignFunction, UnsignFunction } from "./crypto";
export type { CookieParseOptions, CookieSerializeOptions };
export interface CookieSignatureOptions {
/**
* An array of secrets that may be used to sign/unsign the value of a cookie.
*
* The array makes it easy to rotate secrets. New secrets should be added to
* the beginning of the array. `cookie.serialize()` will always use the first
* value in the array, but `cookie.parse()` may use any of them so that
* cookies that were signed with older secrets still work.
*/
secrets?: string[];
}
export declare type CookieOptions = CookieParseOptions & CookieSerializeOptions & CookieSignatureOptions;
/**
* A HTTP cookie.
*
* A Cookie is a logical container for metadata about a HTTP cookie; its name
* and options. But it doesn't contain a value. Instead, it has `parse()` and
* `serialize()` methods that allow a single instance to be reused for
* parsing/encoding multiple different values.
*
* @see https://remix.run/api/remix#cookie-api
*/
export interface Cookie {
/**
* The name of the cookie, used in the `Cookie` and `Set-Cookie` headers.
*/
readonly name: string;
/**
* True if this cookie uses one or more secrets for verification.
*/
readonly isSigned: boolean;
/**
* The Date this cookie expires.
*
* Note: This is calculated at access time using `maxAge` when no `expires`
* option is provided to `createCookie()`.
*/
readonly expires?: Date;
/**
* Parses a raw `Cookie` header and returns the value of this cookie or
* `null` if it's not present.
*/
parse(cookieHeader: string | null, options?: CookieParseOptions): Promise<any>;
/**
* Serializes the given value to a string and returns the `Set-Cookie`
* header.
*/
serialize(value: any, options?: CookieSerializeOptions): Promise<string>;
}
export declare type CreateCookieFunction = (name: string, cookieOptions?: CookieOptions) => Cookie;
/**
* Creates a logical container for managing a browser cookie from the server.
*
* @see https://remix.run/api/remix#createcookie
*/
export declare const createCookieFactory: ({ sign, unsign, }: {
sign: SignFunction;
unsign: UnsignFunction;
}) => CreateCookieFunction;
export declare type IsCookieFunction = (object: any) => object is Cookie;
/**
* Returns true if an object is a Remix cookie container.
*
* @see https://remix.run/api/remix#iscookie
*/
export declare const isCookie: IsCookieFunction;
183 changes: 183 additions & 0 deletions cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* @remix-run/server-runtime v1.5.1
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var cookie = require('cookie');

/**
* Creates a logical container for managing a browser cookie from the server.
*
* @see https://remix.run/api/remix#createcookie
*/
const createCookieFactory = ({
sign,
unsign
}) => (name, cookieOptions = {}) => {
let {
secrets,
...options
} = {
secrets: [],
path: "/",
...cookieOptions
};
return {
get name() {
return name;
},

get isSigned() {
return secrets.length > 0;
},

get expires() {
// Max-Age takes precedence over Expires
return typeof options.maxAge !== "undefined" ? new Date(Date.now() + options.maxAge * 1000) : options.expires;
},

async parse(cookieHeader, parseOptions) {
if (!cookieHeader) return null;
let cookies = cookie.parse(cookieHeader, { ...options,
...parseOptions
});
return name in cookies ? cookies[name] === "" ? "" : await decodeCookieValue(unsign, cookies[name], secrets) : null;
},

async serialize(value, serializeOptions) {
return cookie.serialize(name, value === "" ? "" : await encodeCookieValue(sign, value, secrets), { ...options,
...serializeOptions
});
}

};
};

/**
* Returns true if an object is a Remix cookie container.
*
* @see https://remix.run/api/remix#iscookie
*/
const isCookie = object => {
return object != null && typeof object.name === "string" && typeof object.isSigned === "boolean" && typeof object.parse === "function" && typeof object.serialize === "function";
};

async function encodeCookieValue(sign, value, secrets) {
let encoded = encodeData(value);

if (secrets.length > 0) {
encoded = await sign(encoded, secrets[0]);
}

return encoded;
}

async function decodeCookieValue(unsign, value, secrets) {
if (secrets.length > 0) {
for (let secret of secrets) {
let unsignedValue = await unsign(value, secret);

if (unsignedValue !== false) {
return decodeData(unsignedValue);
}
}

return null;
}

return decodeData(value);
}

function encodeData(value) {
return btoa(myUnescape(encodeURIComponent(JSON.stringify(value))));
}

function decodeData(value) {
try {
return JSON.parse(decodeURIComponent(myEscape(atob(value))));
} catch (error) {
return {};
}
} // See: https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.escape.js


function myEscape(value) {
let str = value.toString();
let result = "";
let index = 0;
let chr, code;

while (index < str.length) {
chr = str.charAt(index++);

if (/[\w*+\-./@]/.exec(chr)) {
result += chr;
} else {
code = chr.charCodeAt(0);

if (code < 256) {
result += "%" + hex(code, 2);
} else {
result += "%u" + hex(code, 4).toUpperCase();
}
}
}

return result;
}

function hex(code, length) {
let result = code.toString(16);

while (result.length < length) result = "0" + result;

return result;
} // See: https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.unescape.js


function myUnescape(value) {
let str = value.toString();
let result = "";
let index = 0;
let chr, part;

while (index < str.length) {
chr = str.charAt(index++);

if (chr === "%") {
if (str.charAt(index) === "u") {
part = str.slice(index + 1, index + 5);

if (/^[\da-f]{4}$/i.exec(part)) {
result += String.fromCharCode(parseInt(part, 16));
index += 5;
continue;
}
} else {
part = str.slice(index, index + 2);

if (/^[\da-f]{2}$/i.exec(part)) {
result += String.fromCharCode(parseInt(part, 16));
index += 2;
continue;
}
}
}

result += chr;
}

return result;
}

exports.createCookieFactory = createCookieFactory;
exports.isCookie = isCookie;
2 changes: 2 additions & 0 deletions crypto.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare type SignFunction = (value: string, secret: string) => Promise<string>;
export declare type UnsignFunction = (cookie: string, secret: string) => Promise<string | false>;
22 changes: 22 additions & 0 deletions data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { RouteMatch } from "./routeMatching";
import type { ServerRoute } from "./routes";
/**
* An object of arbitrary for route loaders and actions provided by the
* server's `getLoadContext()` function.
*/
export declare type AppLoadContext = any;
/**
* Data for a route that was returned from a `loader()`.
*/
export declare type AppData = any;
export declare function callRouteAction({ loadContext, match, request, }: {
loadContext: unknown;
match: RouteMatch<ServerRoute>;
request: Request;
}): Promise<Response>;
export declare function callRouteLoader({ loadContext, match, request, }: {
request: Request;
match: RouteMatch<ServerRoute>;
loadContext: unknown;
}): Promise<Response>;
export declare function extractData(response: Response): Promise<unknown>;
Loading

0 comments on commit 38ef44c

Please sign in to comment.