@roomservice/browser
Advanced tools
Comparing version 0.4.0-2 to 0.4.0-4
@@ -6,3 +6,3 @@ interface RoomValue { | ||
} | ||
export default function authorize(authorizationUrl: string, roomReference: string): Promise<{ | ||
export default function authorize(authorizationUrl: string, roomReference: string, headers?: Headers): Promise<{ | ||
room: RoomValue; | ||
@@ -9,0 +9,0 @@ session: { |
@@ -6,3 +6,3 @@ "use strict"; | ||
const ky_universal_1 = tslib_1.__importDefault(require("ky-universal")); | ||
async function authorize(authorizationUrl, roomReference) { | ||
async function authorize(authorizationUrl, roomReference, headers) { | ||
// Generates and then records a session token | ||
@@ -15,2 +15,3 @@ const result = await ky_universal_1.default.post(authorizationUrl, { | ||
}, | ||
headers: headers || undefined, | ||
// This only works on sites that have setup DNS, | ||
@@ -17,0 +18,0 @@ // or the debugger on roomservice.dev/app, which |
import { Obj, Room, Session } from "./types"; | ||
/** | ||
* // Init | ||
* if (onServer) { | ||
* if (!ctx) throw new Error(); | ||
* | ||
* const { doc } = authorize(url, ctx) | ||
* return { doc } | ||
* } | ||
* | ||
* // setDoc and onSetDoc are noops on the server. | ||
* | ||
* | ||
*/ | ||
export default class DocClient<T extends Obj> { | ||
@@ -3,0 +16,0 @@ private readonly _peer; |
@@ -16,2 +16,15 @@ "use strict"; | ||
} | ||
/** | ||
* // Init | ||
* if (onServer) { | ||
* if (!ctx) throw new Error(); | ||
* | ||
* const { doc } = authorize(url, ctx) | ||
* return { doc } | ||
* } | ||
* | ||
* // setDoc and onSetDoc are noops on the server. | ||
* | ||
* | ||
*/ | ||
class DocClient { | ||
@@ -44,7 +57,2 @@ constructor(parameters) { | ||
this._socketURL = constants_1.ROOM_SERICE_SOCKET_URL; | ||
// Only read from actorid in the browser | ||
if (typeof window !== "undefined") { | ||
// Whenever possible, we try to use the actorId defined in storage | ||
this.readActorIdThenCreateDoc(parameters.defaultDoc); | ||
} | ||
// We define this here so we can debounce the save function | ||
@@ -93,2 +101,8 @@ // Otherwise we'll get quite the performance hit | ||
async init({ room, session }) { | ||
var _a; | ||
// If we're server side, we skip everything else | ||
// and just return the most recent state of the doc. | ||
if (typeof window === "undefined") { | ||
return { doc: (_a = room) === null || _a === void 0 ? void 0 : _a.state }; | ||
} | ||
if (!this._doc) { | ||
@@ -172,2 +186,6 @@ await this.readActorIdThenCreateDoc(); | ||
disconnect() { | ||
if (typeof window === "undefined") { | ||
console.warn("Attempting to call disconnect on the server, this is a no-op."); | ||
return; | ||
} | ||
if (this._socket) { | ||
@@ -179,2 +197,6 @@ socket_1.default.disconnect(this._socket); | ||
onSetDoc(callback) { | ||
if (typeof window === "undefined") { | ||
console.warn("Attempting to call onSetDoc on the server, this is a no-op."); | ||
return; | ||
} | ||
invariant_1.default(!this._onUpdateSocketCallback, "It looks like you've called onSetDoc multiple times. Since this can cause quite severe performance issues if used incorrectly, we're not currently supporting this behavior. If you've got a use-case we haven't thought of, file a github issue and we may change this."); | ||
@@ -220,2 +242,6 @@ const socketCallback = async (data) => { | ||
onConnect(callback) { | ||
if (typeof window === "undefined") { | ||
console.warn("Attempting to call onConnect on the server, this is a no-op."); | ||
return; | ||
} | ||
// If we're offline, cue this up for later. | ||
@@ -229,2 +255,6 @@ if (!this._socket) { | ||
onDisconnect(callback) { | ||
if (typeof window === "undefined") { | ||
console.warn("Attempting to call onDisconnect on the server, this is a no-op."); | ||
return; | ||
} | ||
// If we're offline, cue this up for later. | ||
@@ -242,5 +272,9 @@ if (!this._socket) { | ||
} | ||
const actorId = await offline_1.default.getOrCreateActor(); | ||
if (!actorId) { | ||
console.error("Unexpectedly didn't find offline support in an environment like a browser where we should have offline support."); | ||
} | ||
// We explictly do not add | ||
const offlineDoc = automerge_1.load(data, { | ||
actorId: await offline_1.default.getOrCreateActor() | ||
actorId | ||
}); | ||
@@ -252,2 +286,6 @@ this._doc = offlineDoc; | ||
setDoc(callback) { | ||
if (typeof window === "undefined") { | ||
console.warn("Attempting to call setDoc on the server, this is a no-op."); | ||
return {}; | ||
} | ||
if (!this._doc) { | ||
@@ -254,0 +292,0 @@ console.error("Attempting to call publishDoc .init() has finished."); |
@@ -11,2 +11,3 @@ "use strict"; | ||
const v4_1 = tslib_1.__importDefault(require("uuid/v4")); | ||
const invariant_1 = tslib_1.__importDefault(require("invariant")); | ||
const Offline = { | ||
@@ -31,2 +32,3 @@ getDoc: async (roomRef, docId) => { | ||
getOrCreateActor: async () => { | ||
invariant_1.default(typeof window !== "undefined", "getOrCreateActor was used on the server side; this is a bug in the client, if you're seeing this, let us know."); | ||
const actor = await idb_keyval_1.get("rs:actor"); | ||
@@ -33,0 +35,0 @@ if (actor) { |
@@ -16,3 +16,5 @@ import { PresenceMeta } from "./presence-client"; | ||
private _init; | ||
init(): Promise<Obj>; | ||
init(options?: { | ||
headers?: Headers; | ||
}): Promise<Obj>; | ||
restore(): Promise<any>; | ||
@@ -19,0 +21,0 @@ onConnect(callback: () => void): void; |
@@ -10,8 +10,8 @@ "use strict"; | ||
constructor(parameters) { | ||
this._init = lodash_1.throttle(async () => { | ||
var _a; | ||
this._init = lodash_1.throttle(async (options) => { | ||
var _a, _b; | ||
let room; | ||
let session; | ||
try { | ||
const params = await authorize_1.default(this._authorizationUrl, this._roomReference); | ||
const params = await authorize_1.default(this._authorizationUrl, this._roomReference, (_a = options) === null || _a === void 0 ? void 0 : _a.headers); | ||
room = params.room; | ||
@@ -30,3 +30,3 @@ session = params.session; | ||
} | ||
return { doc: (_a = room) === null || _a === void 0 ? void 0 : _a.state }; | ||
return { doc: (_b = room) === null || _b === void 0 ? void 0 : _b.state }; | ||
} | ||
@@ -60,4 +60,4 @@ // Presence client | ||
// supposed to be called once, but | ||
async init() { | ||
return this._init(); | ||
async init(options) { | ||
return this._init(options); | ||
} | ||
@@ -64,0 +64,0 @@ // Manually restore from cache |
{ | ||
"name": "@roomservice/browser", | ||
"version": "0.4.0-2", | ||
"version": "0.4.0-4", | ||
"main": "dist/index", | ||
@@ -5,0 +5,0 @@ "types": "dist/index", |
@@ -52,1 +52,22 @@ <p align="center"> | ||
``` | ||
## Server Side Rendering | ||
To render on the server, you must include any headers that should be passed along to your | ||
auth endpoint. In most cases, this is just your session cookie. | ||
For example, in Next.js: | ||
```ts | ||
const room = client.room("my-room"); | ||
MyComponent.getInitialProps = async ctx => { | ||
const { doc } = await room.init({ | ||
headers: { | ||
cookie: ctx.req.headers | ||
} | ||
}); | ||
return { doc }; | ||
}; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63219
1005
73