Skip to content

Commit

Permalink
fix: replace Deno.statSync with Deno.stat to allow views to be us…
Browse files Browse the repository at this point in the history
…ed on Deno Deploy
  • Loading branch information
cmorten committed Aug 27, 2022
1 parent 489a695 commit 8eb0df3
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 49 deletions.
12 changes: 6 additions & 6 deletions .github/API/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The `app` object conventionally denotes the Opine application. Create it by
calling the top-level `opine()` function exported by the Opine module:

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down Expand Up @@ -69,7 +69,7 @@ sub-app was mounted.
> to a route.
```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine(); // the main app
const admin = opine(); // the sub app
Expand Down Expand Up @@ -487,7 +487,7 @@ starts listening for requests - this is provided for legacy reasons to aid in
transitions from Express on Node.

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down Expand Up @@ -523,7 +523,7 @@ This method is supported for legacy reasons to aid in transitions from Express
on Node.

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();
const PORT = 3000;
Expand All @@ -542,7 +542,7 @@ starts listening for requests - this is provided for legacy reasons to aid in
transitions from Express on Node.

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand All @@ -560,7 +560,7 @@ starts listening for requests - this is provided for legacy reasons to aid in
transitions from Express on Node.

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down
8 changes: 4 additions & 4 deletions .github/API/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ reasons.
> `toString` may not be a function and instead a string or other user-input.
```ts
import { json, opine } from "https://deno.land/x/[email protected].1/mod.ts";
import { json, opine } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down Expand Up @@ -77,7 +77,7 @@ reasons.
> recommended.
```ts
import { opine, raw } from "https://deno.land/x/[email protected].1/mod.ts";
import { opine, raw } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down Expand Up @@ -230,7 +230,7 @@ reasons.
> recommended.
```ts
import { opine, text } from "https://deno.land/x/[email protected].1/mod.ts";
import { opine, text } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down Expand Up @@ -277,7 +277,7 @@ reasons.
> `toString` may not be a function and instead a string or other user-input.
```ts
import { opine, urlencoded } from "https://deno.land/x/[email protected].1/mod.ts";
import { opine, urlencoded } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand Down
4 changes: 2 additions & 2 deletions .github/API/opine.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Creates an Opine application. The `opine()` function is a top-level function
exported by the Opine module:

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();
```

The `opine()` function is also exported as a named export:

```ts
import { opine } from "https://deno.land/x/[email protected].1/mod.ts";
import { opine } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();
```
4 changes: 2 additions & 2 deletions .github/API/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import {
json,
opine,
urlencoded,
} from "https://deno.land/x/[email protected].1/mod.ts";
} from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand All @@ -118,7 +118,7 @@ The following example shows how to implement your own simple body-parsing
middleware to transform `req.body` into a raw string:

```ts
import opine from "https://deno.land/x/[email protected].1/mod.ts";
import opine from "https://deno.land/x/[email protected].2/mod.ts";

import { readAll } from "https://deno.land/[email protected]/streams/conversion.ts";

Expand Down
4 changes: 2 additions & 2 deletions .github/API/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Opine has a top-level named function export `Router()` that creates a new
`router` object.

```ts
import { Router } from "https://deno.land/x/[email protected].1/mod.ts";
import { Router } from "https://deno.land/x/[email protected].2/mod.ts";

const router = Router(options);
```
Expand Down Expand Up @@ -260,7 +260,7 @@ function defined and work their way "down" the middleware stack processing for
each path they match.

```ts
import opine, { Router } from "https://deno.land/x/[email protected].1/mod.ts";
import opine, { Router } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();
const router = Router();
Expand Down
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## [2.3.2] - 27-08-2022

- fix: replace `Deno.statSync` with `Deno.stat` to allow views to be used on Deno Deploy

## [2.3.1] - 27-08-2022

- fix: improve error messages thrown from `read` module
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import express from "npm:express";
## Getting Started

```ts
import { opine } from "https://deno.land/x/[email protected].1/mod.ts";
import { opine } from "https://deno.land/x/[email protected].2/mod.ts";

const app = opine();

Expand All @@ -73,14 +73,14 @@ Before importing, [download and install Deno](https://deno.land/#installation).
You can then import Opine straight into your project:

```ts
import { opine } from "https://deno.land/x/[email protected].1/mod.ts";
import { opine } from "https://deno.land/x/[email protected].2/mod.ts";
```

Opine is also available on [nest.land](https://nest.land/package/opine), a
package registry for Deno on the Blockchain.

```ts
import { opine } from "https://x.nest.land/[email protected].1/mod.ts";
import { opine } from "https://x.nest.land/[email protected].2/mod.ts";
```

## Features
Expand Down
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h2>Table of Contents</h2>
<a href="#getting-started" id="getting-started" style="color: inherit; text-decoration: none;">
<h2>Getting Started</h2>
</a>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/[email protected].1/mod.ts&quot;</span>;
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/[email protected].2/mod.ts&quot;</span>;

<span class="hljs-keyword">const</span> app = opine();

Expand All @@ -129,11 +129,11 @@ <h2>Installation</h2>
repo and via the <a href="https://deno.land/x">Deno Registry</a>.</p>
<p>Before importing, <a href="https://deno.land/#installation">download and install Deno</a>.</p>
<p>You can then import Opine straight into your project:</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/[email protected].1/mod.ts&quot;</span>;
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/[email protected].2/mod.ts&quot;</span>;
</code></pre>
<p>Opine is also available on <a href="https://nest.land/package/opine">nest.land</a>, a
package registry for Deno on the Blockchain.</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://x.nest.land/[email protected].1/mod.ts&quot;</span>;
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://x.nest.land/[email protected].2/mod.ts&quot;</span>;
</code></pre>
<a href="#features" id="features" style="color: inherit; text-decoration: none;">
<h2>Features</h2>
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "opine",
"description": "Minimalist web framework for Deno ported from ExpressJS.",
"version": "2.3.1",
"version": "2.3.2",
"repository": "https://github.com/cmorten/opine",
"stable": true,
"checkFormat": false,
Expand Down
20 changes: 12 additions & 8 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ async function tryRender(view: any, options: any, callback: Function) {
* @param {Function} callback
* @public
*/
app.render = function render(
app.render = async function render(
name: string,
options: any,
callback: Function = () => {},
Expand Down Expand Up @@ -501,13 +501,17 @@ app.render = function render(
if (!view) {
const View = this.get("view");

view = new View(name, {
defaultEngine: this.get("view engine"),
engines,
root: this.get("views"),
});
try {
view = new View(name, {
defaultEngine: this.get("view engine"),
engines,
root: this.get("views"),
});
} catch (e) {
return done(e);
}

if (!view.path) {
if (!await view.pathPromise) {
const dirs = Array.isArray(view.root) && view.root.length > 1
? `directories "${view.root.slice(0, -1).join('", "')}" or "${
view.root[view.root.length - 1]
Expand All @@ -530,7 +534,7 @@ app.render = function render(
}

// render
tryRender(view, renderOptions, done);
await tryRender(view, renderOptions, done);
};

const isTlsOptions = (
Expand Down
25 changes: 13 additions & 12 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
* Return a stat, maybe.
*
* @param {string} path
* @return {Deno.FileInfo}
* @return {Promise<Deno.FileInfo>}
* @private
*/
function tryStat(path: string) {
async function tryStat(path: string) {
try {
return Deno.statSync(path);
} catch (e) {
return await Deno.stat(path);
} catch {
return undefined;
}
}
Expand All @@ -32,7 +32,7 @@ export class View {
name!: any;
root!: any;
engine!: any;
path!: any;
pathPromise!: any;

/**
* Initialize a new `View` with the given `name`.
Expand Down Expand Up @@ -82,7 +82,7 @@ export class View {
this.engine = options.engines[this.ext];

// lookup path
this.path = this.lookup(fileName);
this.pathPromise = this.lookup(fileName);
}

/**
Expand All @@ -92,9 +92,9 @@ export class View {
* @param {string} file
* @private
*/
resolve(dir: string, file: string) {
async resolve(dir: string, file: string) {
let path = join(dir, file);
let stat = tryStat(path);
let stat = await tryStat(path);

if (stat && stat.isFile) {
return path;
Expand All @@ -103,7 +103,7 @@ export class View {
// <path>/index.<ext>
const ext = this.ext;
path = join(dir, basename(file, ext), `index${ext}`);
stat = tryStat(path);
stat = await tryStat(path);

if (stat && stat.isFile) {
return path;
Expand All @@ -116,7 +116,7 @@ export class View {
* @param {string} name
* @private
*/
lookup(name: string) {
async lookup(name: string) {
const roots = [].concat(this.root);
let path;

Expand All @@ -125,7 +125,7 @@ export class View {
const loc = resolve(root, name);
const dir = dirname(loc);
const file = basename(loc);
path = this.resolve(dir, file);
path = await this.resolve(dir, file);
}

return path;
Expand All @@ -139,7 +139,8 @@ export class View {
* @private
*/
async render(options: object, callback: Function) {
const out = await this.engine(this.path, options);
const path = await this.pathPromise;
const out = await this.engine(path, options);
callback(undefined, out);
}
}
10 changes: 5 additions & 5 deletions test/units/app.render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("app", function () {

function View(this: any, name: string, options: any) {
this.name = name;
this.path = "fake";
this.pathPromise = Promise.resolve("fake");
}

View.prototype.render = function (options: any, fn: any) {
Expand Down Expand Up @@ -230,7 +230,7 @@ describe("app", function () {

function View(this: any, name: string, options: any) {
this.name = name;
this.path =
this.pathPromise =
"path is required by application.js as a signal of success even though it is not used there.";
}

Expand All @@ -255,7 +255,7 @@ describe("app", function () {

function View(this: any, name: string, options: any) {
this.name = name;
this.path = "fake";
this.pathPromise = "fake";
count++;
}

Expand Down Expand Up @@ -285,7 +285,7 @@ describe("app", function () {

function View(this: any, name: string, options: any) {
this.name = name;
this.path = "fake";
this.pathPromise = "fake";
count++;
}

Expand Down Expand Up @@ -364,7 +364,7 @@ describe("app", function () {

function View(this: any, name: string, options: any) {
this.name = name;
this.path = "fake";
this.pathPromise = "fake";
count++;
}

Expand Down
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Version of Opine.
*/
export const VERSION = "2.3.1";
export const VERSION = "2.3.2";

/**
* Supported version of Deno.
Expand Down

0 comments on commit 8eb0df3

Please sign in to comment.