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 4, 2019
1 parent 56a8a39 commit d01651e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 7 deletions.
6 changes: 5 additions & 1 deletion packages/-fastboot-test-app/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const Router = EmberRouter.extend({
rootURL: config.rootURL,
});

Router.map(function() {});
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>
3 changes: 3 additions & 0 deletions packages/-fastboot-test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"qunit-dom": "^0.9.0",
"typescript": "~3.6.3"
},
"fastbootDependencies": [
"crypto"
],
"engines": {
"node": "8.* || >= 10.*"
}
Expand Down
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 d01651e

Please sign in to comment.