Skip to content

Commit

Permalink
[BUGFIX release] Enable store.createRecord in FastBoot (#6568)
Browse files Browse the repository at this point in the history
* Add FastBoot test for store.createRecord

Fails at the moment.

* Enable store.createRecord in FastBoot

Changes the V4 UUID generation to leverage `FastBoot.require` when
possible. This currently requires that the host application add the
following to their `package.json`:

```
"fastbootDependencies": [
  "crypto"
]
```

* fix types
  • Loading branch information
rwjblue authored and igorT committed Nov 6, 2019
1 parent 487b3e2 commit e6e1ce8
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 6 deletions.
15 changes: 15 additions & 0 deletions packages/-fastboot-test-app/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import EmberRouter from '@ember/routing/router';
import config from './config/environment';

const Router = EmberRouter.extend({
location: config.locationType,
rootURL: config.rootURL,
});

Router.map(function() {
this.route('person', function() {
this.route('new');
});
});

export default Router;
10 changes: 10 additions & 0 deletions packages/-fastboot-test-app/app/routes/person/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
store: service(),

model() {
return this.store.createRecord('person');
},
});
6 changes: 6 additions & 0 deletions packages/-fastboot-test-app/app/templates/person/new.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<form>
<label>
Name
{{input class="person-name" value=this.model.name}}
</label>
</form>
65 changes: 65 additions & 0 deletions packages/-fastboot-test-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "fastboot-test-app",
"version": "3.15.0-alpha.1",
"private": true,
"description": "Small description for fastboot-test-app goes here",
"repository": "",
"license": "MIT",
"author": "",
"directories": {
"doc": "doc",
"test": "tests"
},
"scripts": {
"build": "ember build",
"lint:hbs": "ember-template-lint .",
"lint:js": "eslint --config ../../.eslintrc.js --ignore-path ../../.eslintignore .",
"start": "ember serve",
"test": "ember test --test-port=0"
},
"dependencies": {
"ember-data": "3.15.0-alpha.1",
"ember-inflector": "^3.0.1"
},
"devDependencies": {
"@ember/optional-features": "^1.1.0",
"@types/ember": "^3.1.1",
"@types/ember-qunit": "^3.4.7",
"@types/ember__test-helpers": "~0.7.9",
"@types/qunit": "^2.5.3",
"@types/rsvp": "^4.0.3",
"broccoli-asset-rev": "^3.0.0",
"ember-cli": "~3.13.1",
"ember-cli-app-version": "^3.2.0",
"ember-cli-babel": "^7.12.0",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-fastboot": "^2.2.1",
"ember-cli-fastboot-testing": "^0.2.3",
"ember-cli-htmlbars": "^4.0.8",
"ember-cli-inject-live-reload": "^2.0.2",
"ember-cli-template-lint": "^1.0.0-beta.1",
"ember-cli-typescript": "^3.0.0",
"ember-cli-typescript-blueprints": "^3.0.0",
"ember-export-application-global": "^2.0.0",
"ember-fetch": "^6.7.1",
"ember-load-initializers": "^2.1.0",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-qunit": "^4.5.1",
"ember-resolver": "^5.3.0",
"ember-simple-tree": "^0.7.1",
"ember-source": "^3.13.3",
"eslint": "^6.5.1",
"eslint-plugin-ember": "^7.2.0",
"eslint-plugin-node": "^10.0.0",
"loader.js": "^4.7.0",
"qunit": "^2.9.3",
"qunit-dom": "^0.9.0",
"typescript": "~3.6.4"
},
"fastbootDependencies": [
"crypto"
],
"engines": {
"node": "8.* || >= 10.*"
}
}
16 changes: 16 additions & 0 deletions packages/-fastboot-test-app/tests/fastboot/person/new-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';

module('FastBoot | /person/new', function(hooks) {
setup(hooks);

test('it does not error in SSR (GH#6563)', async function(assert) {
await visit('/person/new');

// from application.hbs
assert.dom('h1').hasText('Ember Data');
assert.dom('a').hasAttribute('href', '/tests');

assert.dom('.person-name').exists();
});
});
28 changes: 22 additions & 6 deletions packages/store/addon/-private/identifiers/utils/uuid-v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@ declare global {
}
}

const CRYPTO =
typeof window !== 'undefined' &&
window.msCrypto &&
typeof window.msCrypto.getRandomValues === 'function'
? window.msCrypto
: window.crypto;
const CRYPTO = (() => {
const hasWindow = typeof window !== 'undefined';
const isFastBoot = typeof FastBoot !== 'undefined';

if (isFastBoot) {
return {
getRandomValues(buffer: Uint8Array) {
return (FastBoot as FastBoot).require('crypto').randomFillSync(buffer);
},
};
} else if (hasWindow && typeof window.crypto !== 'undefined') {
return window.crypto;
} else if (
hasWindow &&
typeof window.msCrypto !== 'undefined' &&
typeof window.msCrypto.getRandomValues === 'function'
) {
return window.msCrypto;
} else {
throw new Error('ember-data: Cannot find a valid way to generate local identifiers');
}
})();

// we might be able to optimize this by requesting more bytes than we need at a time
function rng() {
Expand Down
4 changes: 4 additions & 0 deletions packages/store/types/fastboot/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface FastBoot {
require(moduleName: string): any;
}
const FastBoot: undefined | FastBoot;

0 comments on commit e6e1ce8

Please sign in to comment.